Skip to content

UTF8ToString does not work with -sWASM_WORKERS if window.crossOriginIsolated is false #22837

@ThomasMertes

Description

@ThomasMertes

I compile with -sWASM_WORKERS but the code with UTF8ToString should also work if crossOriginIsolated is false.

I open the worker only if window.crossOriginIsolated holds. In case of a missing worker I have an alternate solution without worker.
The problem is: I use UTF8ToString. E.g.:

        windowIdAndFlags = EM_ASM_INT({
          if (typeof window !== "undefined") {
            let windowName = Module.UTF8ToString($4);
            let windowTitle = Module.UTF8ToString($5);

and this fails for the combination -sWASM_WORKERS and crossOriginIsolated===false with:

Uncaught (in promise) ReferenceError: SharedArrayBuffer is not defined
    UTF8ArrayToString                    http://localhost:1080/demo/klondike.js:2179
    UTF8ToString                         http://localhost:1080/demo/klondike.js:4882
    6879322                              http://localhost:1080/demo/klondike.js:1103
    runEmAsmFunction                     http://localhost:1080/demo/klondike.js:5183
    _emscripten_asm_const_int            http://localhost:1080/demo/klondike.js:5186
    x                                    http://localhost:1080/demo/klondike.js:5570
    dynCall_ijjjji                       http://localhost:1080/demo/klondike.js:5991
    invoke_ijjjji                        http://localhost:1080/demo/klondike.js:6117
    x                                    http://localhost:1080/demo/klondike.js:5570
    _main                                http://localhost:1080/demo/klondike.js:5917
    callMain                             http://localhost:1080/demo/klondike.js:6156
    doRun                                http://localhost:1080/demo/klondike.js:6190
    run                                  http://localhost:1080/demo/klondike.js:6200
    runCaller                            http://localhost:1080/demo/klondike.js:6139
    removeRunDependency                  http://localhost:1080/demo/klondike.js:532
    receiveInstance                      http://localhost:1080/demo/klondike.js:671
    receiveInstantiationResult           http://localhost:1080/demo/klondike.js:680
    promise callback*instantiateAsync/<  http://localhost:1080/demo/klondike.js:637
    promise callback*instantiateAsync    http://localhost:1080/demo/klondike.js:631
    createWasm                           http://localhost:1080/demo/klondike.js:697
    <anonymous>                          http://localhost:1080/demo/klondike.js:5913

The line klondike.js:2179 is:

    return UTF8Decoder.decode(heapOrArray.buffer instanceof SharedArrayBuffer ? heapOrArray.slice(idx, endPtr) : heapOrArray.subarray(idx, endPtr));

Obvously UTF8ArrayToString checks for SharedArrayBuffer. So UTF8ArrayToString might be intended to be usable for -sWASM_WORKERS and crossOriginIsolated===false but obviously the checks are not good enough.

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.69 (c55ff404dbe1eb9f263c48b584d5ba627224763a)
clang version 20.0.0git (https:/github.com/llvm/llvm-project 50866e84d1da8462aeb96607bf6d9e5bbd5869c5)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/tm/emsdk/upstream/bin

Full link command and output with -v appended:

emcc -g -gsource-map -s TOTAL_STACK\=1048576 -s ASSERTIONS\=0 -s WASM_WORKERS -s ALLOW_MEMORY_GROWTH\=1 -s EXIT_RUNTIME -s EXPORTED_RUNTIME_METHODS\=\[\'ccall\'\,\'cwrap\'\,\'UTF8ToString\'\] -s ASYNCIFY -s ASYNCIFY_STACK_SIZE\=16384 -o klondike.js tmp_klondike.o /home/tm/seed7_5/bin/s7_data_emc.a /home/tm/seed7_5/bin/s7_draw_emc.a /home/tm/seed7_5/bin/s7_con_emc.a /home/tm/seed7_5/bin/seed7_05_emc.a --pre-js /home/tm/seed7_5/bin/pre_js.js -lnodefs.js -v
 /home/tm/emsdk/upstream/bin/clang --version
 /home/tm/emsdk/upstream/bin/wasm-ld -o klondike.wasm tmp_klondike.o /home/tm/seed7_5/bin/s7_data_emc.a /home/tm/seed7_5/bin/s7_draw_emc.a /home/tm/seed7_5/bin/s7_con_emc.a /home/tm/seed7_5/bin/seed7_05_emc.a -L/home/tm/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten /home/tm/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/crtbegin.o -lGL-ww-getprocaddr -lal -lhtml5 -lbulkmemory -lstubs -lc-ww -ldlmalloc-ww -lcompiler_rt-ww -lc++-ww-noexcept -lc++abi-ww-noexcept -lsockets-ww -lwasm_workers -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr /tmp/tmptx3jcqb7libemscripten_js_symbols.so --import-memory --shared-memory --export=_emscripten_stack_alloc --export=__get_temp_ret --export=__set_temp_ret --export=__funcs_on_exit --export=__wasm_call_ctors --export=emscripten_stack_get_current --export=_emscripten_stack_restore --export=_emscripten_wasm_worker_initialize --export=malloc --export=free --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=main --export-if-defined=__main_argc_argv --export-if-defined=fflush --export-table -z stack-size=1048576 --max-memory=2147483648 --initial-memory=16777216 --no-entry --stack-first --table-base=1
 /usr/bin/python3 -E /home/tm/emsdk/upstream/emscripten/tools/wasm-sourcemap.py klondike.wasm --dwarfdump=/home/tm/emsdk/upstream/bin/llvm-dwarfdump -o klondike.wasm.map --basepath=/home/tm/seed7_5/prg
 /home/tm/emsdk/upstream/bin/llvm-objcopy klondike.wasm klondike.wasm --remove-section=producers
 /home/tm/emsdk/upstream/bin/wasm-emscripten-finalize -g --pass-arg=legalize-js-interface-exported-helpers --dwarf --output-source-map-url=klondike.wasm.map klondike.wasm -o klondike.wasm --detect-features --input-source-map=klondike.wasm.map --output-source-map=klondike.wasm.map
 /home/tm/emsdk/node/20.18.0_64bit/bin/node /home/tm/emsdk/upstream/emscripten/src/compiler.mjs /tmp/tmp1fy7c9tp.json
emcc: warning: running limited binaryen optimizations because DWARF info requested (or indirectly required) [-Wlimited-postlink-optimizations]
 /home/tm/emsdk/upstream/bin/wasm-opt --asyncify --pass-arg=asyncify-propagate-addlist [email protected]_*,env.__asyncjs__*,*.fd_sync,*.emscripten_promise_await,*.emscripten_idb_load,*.emscripten_idb_store,*.emscripten_idb_delete,*.emscripten_idb_exists,*.emscripten_idb_clear,*.emscripten_idb_load_blob,*.emscripten_idb_store_blob,*.emscripten_sleep,*.emscripten_wget_data,*.emscripten_scan_registers,*.emscripten_lazy_load_code,*._load_secondary_module,*.emscripten_fiber_swap,*.SDL_Delay klondike.wasm -o klondike.wasm -g --mvp-features --enable-threads --enable-bulk-memory --enable-multivalue --enable-mutable-globals --enable-reference-types --enable-sign-ext --input-source-map=klondike.wasm.map --output-source-map=klondike.wasm.map
 /home/tm/emsdk/node/20.18.0_64bit/bin/node /home/tm/emsdk/upstream/emscripten/tools/acorn-optimizer.mjs /tmp/emscripten_temp_4qooxtml/klondike.js growableHeap --closure-friendly -o /tmp/emscripten_temp_4qooxtml/klondike.jso1.js
 /home/tm/emsdk/node/20.18.0_64bit/bin/node /home/tm/emsdk/upstream/emscripten/tools/preprocessor.mjs /tmp/emscripten_temp_4qooxtml/settings.js wasm_worker.js --expandMacros

Hopefully this can be fixed.

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