Skip to content

Conversation

@kripken
Copy link
Member

@kripken kripken commented Apr 23, 2024

This adds fuzzing for string.new_wtf16_array and string.from_code_point. The
latter was also missing interpreter support, which this adds.

@tlively I seem to get the wrong results for unicode here - am I using the wrong
API or using it incorrectly? The last testcase here traps in V8 on being an invalid
codePoint but String::convertUTF16ToUTF8 returns that it is valid.

For comparison, related V8 code: https://chromium.googlesource.com/v8/v8/+/a19a2ff2be5b60f6df6c7c7160860ede2cb85e27%5E%21/#F3

[[fallthrough]];
}
case 2: {
// Construct an interesting WTF-8 string from parts and use string.const.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is unchanged.

Comment on lines 1904 to 1914
std::string str;
str += char(codePoint & 0xff);
str += char((codePoint >> 8) & 0xff);
if (codePoint > 0xffff) {
str += char((codePoint >> 16) & 0xff);
str += char((codePoint >> 24) & 0xff);
}
std::stringstream null;
if (!String::convertUTF16ToUTF8(null, str)) {
trap("invalid code point");
}
Copy link
Member

@tlively tlively Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check for invalid code points is probably not behaving correctly because this is not the correct way to encode a code point as UTF-16.

To check for validity, all you have to do is check codePoint <= 0x10FFFF. After that, you can use String::writeWTF16CodePoint to get the proper encoding, then you can convert that from a string to a string literal.

edit: Note that the string literal may have two code units because the encoding of the code point may require a surrogate pair.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I am still very fuzzy on the UTF stuff 😄 I thought I just need to emit two 16-bit chunks here...

@kripken kripken marked this pull request as ready for review April 23, 2024 20:49
@kripken kripken requested a review from tlively April 23, 2024 20:50
// empty. Write it all out but the null terminator.
assert(!str.empty());
for (size_t i = 0; i < str.size() - 1; i++) {
contents.push_back(Literal(int32_t(str[i])));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this part you need to consume two bytes for each entry in the array. The Literal(std::string_view) constructor can do this for you, though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, done.

(string.from_code_point
(i32.const -83)
)
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other test cases to add:

An isolated high surrogate: i32.const 0xD800
An isolated low surrogate: i32.const 0xDC00
A codepoint requiring a surrogate pair: 10348 (𐍈)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, added. I also noticed a fuzz error on an unsigned case (that now works) that I also added.

Copy link
Member

@tlively tlively left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@kripken
Copy link
Member Author

kripken commented Apr 23, 2024

Fuzzed for 5,000 iterations with an extra patch that emits even more strings and no issues, landing.

@kripken kripken merged commit 42db73a into WebAssembly:main Apr 23, 2024
@kripken kripken deleted the fuzz.string2 branch April 23, 2024 23:42
@gkdn gkdn mentioned this pull request Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants