Skip to content

JS exception/crash in emsdk 6.0.2 when calling UTF8ToString #27241

Description

@floooh

Please include the following in your bug report:

Version of emscripten/emsdk:
6.0.2

Description

I'm getting a 'crash' in 6.0.2 inside UTF8toString called from _emscripten_glShaderSource (this is a regression in 6.0.2 which didn't happen in 6.0.1):

Uncaught TypeError: Failed to execute 'decode' on 'TextDecoder': The provided ArrayBuffer value must not be resizable
    at UTF8ArrayToString (v6502r.js:919:28)
    at UTF8ToString (v6502r.js:964:20)
    at Object.getSource (v6502r.js:2366:21)
    at _emscripten_glShaderSource (v6502r.js:3607:23)
    at v6502r.wasm._sg_gl_compile_shader (v6502r.wasm:0x6caca)
    at v6502r.wasm._sg_gl_create_shader (v6502r.wasm:0x6c17c)
    at v6502r.wasm._sg_create_shader (v6502r.wasm:0x3ff32)
    at v6502r.wasm._sg_init_shader (v6502r.wasm:0x3e30a)
    at v6502r.wasm.sg_make_shader (v6502r.wasm:0x4413a)
    at v6502r.wasm.simgui_setup (v6502r.wasm:0x50794)

The code around the bottom of the callstack is:

  var UTF8ArrayToString = (heapOrArray, idx = 0, maxBytesToRead, ignoreNul) => {
  
      var endPtr = findStringEnd(heapOrArray, idx, maxBytesToRead, ignoreNul);
  
      // When using conditional TextDecoder, skip it for short strings as the overhead of the native call is not worth it.
      if (endPtr - idx > 16 && heapOrArray.buffer && UTF8Decoder) {
        // ========> CRASH HAPPENS HERE
        return UTF8Decoder.decode(heapOrArray.subarray(idx, endPtr));
      }
      var str = '';

...the crash happens on the first call to UTF8Decoder.decode(), there are plenty of earlier calls to UTF8ArrayToString(), but those all skip the if() condition (I guess this is why the problem didn't show up during testing, assuming the if-body isn't covered by tests).

...I guess this is related to this change in 6.0.2:

The GROWABLE_ARRAYBUFFERS setting now defaults to 1, which means it will be used when available. Note that this only affects programs that are built with ALLOW_MEMORY_GROWTH, which is not enabled by default. (#27212)

PS: cc @sbc100, and also link to Discord message I wrote: https://discord.com/channels/453584038356058112/590215642444202044/1522227441165275307

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions