Skip to content

[jsapi] inconsistent utf-8 decoding #915

Closed

Description

@xtuc

Two UTF-8 decoders are specified:

From the WHATWG algorythm, the rules

  1. Set UTF-8 lower boundary to 0x80 and UTF-8 upper boundary to 0xBF.
  1. Set UTF-8 code point to (UTF-8 code point << 6) | (byte & 0x3F)

are not used in Wasm.

My understanding is that U+DC01 and U+FFFD should be equal in the JS API, as tested here

assert_sections(WebAssembly.Module.customSections(module, "na\uFFFDme"), [
bytes,
]);
assert_sections(WebAssembly.Module.customSections(module, "na\uDC01me"), [
bytes,
While in Wasm they would be considered as two different sections, which could cause sublte mismatchs.

Note that this is the only occurence of UTF-8 decoding in the JS spec.

Activity

  1. binji commented on Nov 27, 2018

    @binji
    Member

    I brought up something similar when I was reviewing the JS API tests here: #883 (comment)

    @binji: This is a bit surprising to me, since the customSections algorithm says it uses UTF-8 decode without BOM or fail, which doesn't seem to convert the 0xdc01 to a 0xfffd (though I see that elsewhere in the spec). Does the replacement happen somewhere else?

    @Ms2ger: customSections takes a USVString, which introduces the replacement character; the "UTF-8 decode without BOM or fail" algorithm is only applied to the bytes in the module, which contain an explicit U+FFFD from the emit_string call.

    You can see that here:

    An ECMAScript value V is converted to an IDL USVString value by running the following algorithm:

    1. Let string be the result of converting V to a DOMString.
    2. Return an IDL USVString value that is the result of converting string to a sequence of Unicode scalar values.

    "Converting a string..." links to here, which replaces U+DC01:

    0xDC00 ≤ c ≤ 0xDFFF
    Append to U a U+FFFD REPLACEMENT CHARACTER.

    It's worth mentioning that ECMAScript doesn't have this restriction -- you can use '\udc01' and it is not converted to '\ufffd'.

    So I think there actually might be two issues here:

    1. The WebAssembly JS API spec is defined using WebIDL, so it inherits restrictions from WebIDL that perhaps should not apply to JavaScript. In particular, the conversion of '\udc01' to '\ufffd' in the API surface for WebAssembly.Module.customSections because it uses USVString.
    2. customSections uses the WhatWG UTF-8 decoder, which is more restrictive than the one defined in the WebAssembly core spec. If the wasm module has a custom section with an unmatched trailing surrogate (U+DC00 through U+DFFF), the module will be valid according to the core spec, but will fail in the "UTF-8 decode without BOM or fail" algorithm, and thus the assertion "Assert: name is not failure (moduleObject.[[Module]] is valid)." will be false.
  2. binji commented on Nov 27, 2018

    @binji
    Member

    @littledan @titzer thoughts? Maybe we're OK with issue 1, but I think we should probably address issue 2.

    The simplest solution would be to skip any custom section whose name can't be decoded using the WhatWG decoder. That would mean that there would be sections that you couldn't access via JavaScript (without decoding the module on your own). Another solution would be to restrict the core spec to make surrogate pairs invalid. A third solution would be to rewrite the JS API spec to allow an unmatched trailing surrogate.

  3. Ms2ger commented on Nov 28, 2018

    @Ms2ger
    Collaborator

    If the wasm module has a custom section with an unmatched trailing surrogate (U+DC00 through U+DFFF), the module will be valid according to the core spec

    This seems like a very poor idea in the first place. Can we disallow that in the core spec instead?

  4. rossberg commented on Nov 28, 2018

    @rossberg
    Member

    I think it was an oversight on my part that the core spec does not currently rule out the surrogate space. It should, and the reference interpreter indeed flags any such code point as an error. I think we also have various tests for that. (Note that not just unmatched surrogates are illegal, any occurrence of a codepoint in the space of surrogates is illegal as the result of UTF-8 decoding.)

    I prepared a fix for the core spec.

    As for replacements, it is very much intentional that no decoding or interpretation is needed to compare Wasm names. That was the basis of the agreement to require UTF-8 in the first place. So for the core spec the choice to not do any replacements is working as intended. Note that JS is not the only customer of Wasm names.

    It doesn't sound like a wise idea nor necessary that the JS API specifies something different regarding replacements. I don't think that was the case originally, and maybe that should be fixed?

  5. Ms2ger commented on Nov 28, 2018

    @Ms2ger
    Collaborator

    If I understand correctly, when matching, we've got the name (which is conceptually a sequence of Unicode scalar values, and could be stored as UTF-8 or UTF-16), and the argument, which is a JS string (so UTF-16 plus unpaired surrogates). I can think of three reasonable ways to compare right now:

    1. convertToJSString(name) == argument (so an argument with unpaired surrogates will never match)
    2. name == convertToUnicodeScalarValues(argument, ReplaceUnpairedSurrogates) (this is what the spec currently does)
    3. name == convertToUnicodeScalarValues(argument, ThrowOnUnpairedSurrogates)

    I suppose the first one is cheapest if we assume JS engines will decode the names to UTF-16 ahead of time anyway, but I'm not entirely sure it's the most helpful.

    (I have no strong personal preference, though.)

  6. rossberg commented on Nov 28, 2018

    @rossberg
    Member

    So the case in question is the one where the user-provided argument is invalid UTF-16?

    In that case, it seems like a bad idea to make that magically succeed on some accidental occasions (option 2).

    Invalid UTF-16 is a reality of JavaScript strings. Of all string-consuming functions out there, it's not the responsibility of the Wasm API to suddenly start worrying about that as a malformed argument (option 3).

    So option 1 is the natural choice IMO.

  7. xtuc commented on Dec 3, 2018

    @xtuc
    ContributorAuthor

    I agree with the option one.

    FYI SpiderMonkey has the same issue, and I would expect all the other engines to use the UTF8 decoder "for the web", which is the one specified by WHATWG.

  8. littledan commented on Dec 5, 2018

    @littledan
    Collaborator

    I'm happy with the solution this thread ended up on. With #923 merged, is this issue ready to close?

  9. xtuc commented on Dec 5, 2018

    @xtuc
    ContributorAuthor

    @littledan The WHATWG algorithm stills uses

    Set UTF-8 lower boundary to 0x80 and UTF-8 upper boundary to 0xBF.

    Which means that it will truncate some code points compared to the WebAssembly decoder.

  10. littledan commented on Dec 5, 2018

    @littledan
    Collaborator

    @xtuc Which kinds of strings do you think will be truncated in one algorithm and valid in another?

  11. binji commented on Dec 5, 2018

    @binji
    Member

    To be clear, the surrogates aren't truncated, they are replaced with U+FFFD (see #915 (comment))

  12. littledan commented on Dec 6, 2018

    @littledan
    Collaborator

    I don't think lower/upper boundaries result in truncation either.

  13. rossberg commented on Dec 6, 2018

    @rossberg
    Member

    Doh, @xtuc is right, there was another side condition missing in the Wasm spec, see #928. Without, it the utf8 function would not actually be a function.

  14. xtuc commented on Dec 6, 2018

    @xtuc
    ContributorAuthor

    If I understand correctly, the current Wasm decoder would allow much high code points than the WHATWG does, and these would be truncated (or set the upper boundary) when going to JS.

    @rossberg is the goal to comply with the WHATWG decoder? Since the WebAssembly UTF8 decoder is referenced elsewhere it seems like a restriction for Wasm usage.

    I agree with @Ms2ger first option, which doesn't require such a spec change, only about the custom sections or the js-api.

  15. rossberg commented on Dec 6, 2018

    @rossberg
    Member

    The Wasm spec clearly allows all legal code points up to U+110000, as specified by Unicode. I think the same is true for the algorithm given in the WHATWG spec, though it's much more difficult to deduce (you have to look at the table in Unicode section 3.9 to understand why this algorithm makes sense).

  16. xtuc commented on Jan 17, 2019

    @xtuc
    ContributorAuthor

    It took me some time to understand and after a chat with @Ms2ger, here are the changes needed:

    • Switching from USVString to DOMString in the WebIDL specification.
      • The result of using the WHATWG UTF-8 Decoder is a USVString (which corresponds to a WHATWG scalar value string and does not contain surrogates. When doing the comparison between the WebAssembly name (USVString) and the JavaScript argument (DOMString), the name needs to be upward-casted into a DOMString.
    • The current test
      assert_sections(WebAssembly.Module.customSections(module, "na\uFFFDme"), [
      bytes,
      ]);
      assert_sections(WebAssembly.Module.customSections(module, "na\uDC01me"), [
      bytes,
      won't match anymore since the surrogates from the JavaScript argument (DOMString) are not normalized.
  17. Ms2ger commented on Jan 17, 2019

    @Ms2ger
    Collaborator

    The first check with the explicit U+FFFD will still match, but the second call with the surrogate should indeed start returning an empty array.

  18. xtuc commented on Jan 17, 2019

    @xtuc
    ContributorAuthor

    Yes, what I mean with "normalizing surrogates" is replacing them with U+FFFD.

  19. rossberg commented on Jan 17, 2019

    @rossberg
    Member
    • Switching from USVString to DOMString in the WebIDL specification.

    To clarify, does this apply only to the parameter of customSections, or are you also talking about the descriptor records?

  20. xtuc commented on Jan 17, 2019

    @xtuc
    ContributorAuthor

    @rossberg I would like to switch every USVString to a DOMString, otherwise it theoretically won't match when the USVString contained non noramlized surrogates. I expect all the JavaScript engines to use a DOMString anyway.

    Edit:
    See @Ms2ger review on #947.

  21. added a commit that references this issue on Jan 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions