emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.82k stars 3.31k forks source link

Cannot instantiate module using value_object with ASYNCIFY and DISABLE_EXCEPTION_CATCHING=0 enabled. #18254

Open pkpecz opened 1 year ago

pkpecz commented 1 year ago

It seems the adding emscripten binding basing on emscripten::value_object with -s ASYNCIFY -s NO_DISABLE_EXCEPTION_CATCHING -s MAIN_MODULE=2 included is not actually working. I isolated the problem to following example:

main.cpp

#include <emscripten/bind.h>

int main()
{
    return 0;
}

class TestEasyClass
{
public:
    int member;
};

EMSCRIPTEN_BINDINGS(scope) {
    emscripten::value_object<TestEasyClass>("TestEasyClass")
        .field("member", &TestEasyClass::member);
}

main.js

import sampleModule from "./sample.js"

async function getWasmSample(wasmFilePath) {
    try {
        var locateFileOverride = function (wasmFileName) {      
            return wasmFilePath + "/" + wasmFileName;
        };    
        const result = await sampleModule({ locateFile: locateFileOverride });
        return result;
    } catch (error) {
        console.warn(
            "[SampleLoader: getWasmSample] Failed to load module",
            error
        );
    }
}

let wasmSample = getWasmSample('.')

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Simple template</title>
    <script type="module" src="main.js"></script>
  </head>
  <body>
    <h3> Module Loading Test </h3>
  </body>
</html>

Compiling main.cpp using command line attached to bug report and then running in web browser causes following error occurs:

Screenshot 2022-11-25 at 17 22 10

Commenting out the EMSCRIPTEN_BINDINGS(scope) block allows wasm module to instantiate properly.

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.26(8eaf19f1c6a9a1b0cd0f9a91657366829e34ae5c)
clang version 16.0.0(https://github.com/llvm/llvm-project f81f0cb75a2808a67d2662f044ad07628fc9d900)
Target: wasm32-unknown-emscripten
Thread model: posix

Full link command and output with -v appended: em++ -s ASYNCIFY -s NO_DISABLE_EXCEPTION_CATCHING -s MAIN_MODULE=2 -s EXPORT_NAME=sampleModule -s EXPORT_ES6 --bind -v -g main.cpp -o src/sample.js

 "/Users/ppecz/.local/tools/emsdk/upstream/bin/clang" --version
 "/Users/ppecz/.local/tools/emsdk/upstream/bin/clang++" -target wasm32-unknown-emscripten -fPIC -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-cxx-exceptions -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -DEMSCRIPTEN -I/Users/ppecz/.local/tools/emsdk/upstream/emscripten/cache/sysroot/include/SDL --sysroot=/Users/ppecz/.local/tools/emsdk/upstream/emscripten/cache/sysroot -Xclang -iwithsysroot/include/compat -v -g3 main.cpp -c -o /var/folders/qz/_yz_6vbs6cjf0mh3t_4l2yrczxbvx8/T/emscripten_temp_rc9hkxwk/main_0.o
clang version 16.0.0 (https://github.com/llvm/llvm-project f81f0cb75a2808a67d2662f044ad07628fc9d900)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Users/ppecz/.local/tools/emsdk/upstream/bin
 (in-process)
 "/Users/ppecz/.local/tools/emsdk/upstream/bin/clang-16" -cc1 -triple wasm32-unknown-emscripten -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-feature +mutable-globals -target-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debug-info-kind=constructor -dwarf-version=4 -debugger-tuning=gdb -v -fcoverage-compilation-dir=/Users/ppecz/Development/dvwc/work/dvwc_dynamic_tflite/dynamic_samples/bug_report -resource-dir /Users/ppecz/.local/tools/emsdk/upstream/lib/clang/16 -D EMSCRIPTEN -I /Users/ppecz/.local/tools/emsdk/upstream/emscripten/cache/sysroot/include/SDL -isysroot /Users/ppecz/.local/tools/emsdk/upstream/emscripten/cache/sysroot -internal-isystem /Users/ppecz/.local/tools/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten/c++/v1 -internal-isystem /Users/ppecz/.local/tools/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1 -internal-isystem /Users/ppecz/.local/tools/emsdk/upstream/lib/clang/16/include -internal-isystem /Users/ppecz/.local/tools/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten -internal-isystem /Users/ppecz/.local/tools/emsdk/upstream/emscripten/cache/sysroot/include -fdeprecated-macro -fdebug-compilation-dir=/Users/ppecz/Development/dvwc/work/dvwc_dynamic_tflite/dynamic_samples/bug_report -ferror-limit 19 -fvisibility=default -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-cxx-exceptions -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /var/folders/qz/_yz_6vbs6cjf0mh3t_4l2yrczxbvx8/T/emscripten_temp_rc9hkxwk/main_0.o -x c++ main.cpp
clang -cc1 version 16.0.0 based upon LLVM 16.0.0git default target x86_64-apple-darwin21.6.0
ignoring nonexistent directory "/Users/ppecz/.local/tools/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten/c++/v1"
ignoring nonexistent directory "/Users/ppecz/.local/tools/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
 /Users/ppecz/.local/tools/emsdk/upstream/emscripten/cache/sysroot/include/SDL
 /Users/ppecz/.local/tools/emsdk/upstream/emscripten/cache/sysroot/include/compat
 /Users/ppecz/.local/tools/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1
 /Users/ppecz/.local/tools/emsdk/upstream/lib/clang/16/include
 /Users/ppecz/.local/tools/emsdk/upstream/emscripten/cache/sysroot/include
End of search list.
 "/Users/ppecz/.local/tools/emsdk/upstream/bin/wasm-ld" -o src/sample.wasm /var/folders/qz/_yz_6vbs6cjf0mh3t_4l2yrczxbvx8/T/emscripten_temp_rc9hkxwk/main_0.o -L/Users/ppecz/.local/tools/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/pic --whole-archive -lembind-rtti --no-whole-archive -lGL -lal -lhtml5 -lstubs-debug -lnoexit -lc-debug -ldlmalloc -lcompiler_rt -lc++ -lc++abi-debug -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-cxx-exceptions -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --import-memory --export-if-defined=main --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_argc_argv --export-if-defined=__wasm_apply_data_relocs --export-if-defined=fflush --export=setThrew --export=emscripten_stack_get_base --export=emscripten_stack_get_end --export=emscripten_stack_set_limits --export=emscripten_stack_get_free --export=emscripten_stack_get_current --export=stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export=__errno_location --export=getTempRet0 --export=setTempRet0 --export=__get_temp_ret --export=__set_temp_ret --export=malloc --export=free --export=__cxa_is_pointer_type --export=__cxa_can_catch --experimental-pic -pie --no-export-dynamic -z stack-size=5242880 --initial-memory=16777216 --no-entry --max-memory=16777216 --stack-first
 "/Users/ppecz/.local/tools/emsdk/upstream/bin/wasm-emscripten-finalize" -g --pass-arg=legalize-js-interface-export-originals --pass-arg=legalize-js-interface-exported-helpers --dwarf src/sample.wasm -o src/sample.wasm --detect-features
 "/Users/ppecz/.local/tools/emsdk/node/14.18.2_64bit/bin/node" /Users/ppecz/.local/tools/emsdk/upstream/emscripten/src/compiler.js /var/folders/qz/_yz_6vbs6cjf0mh3t_4l2yrczxbvx8/T/tmp1bkczn08.json
em++: warning: running limited binaryen optimizations because DWARF info requested (or indirectly required) [-Wlimited-postlink-optimizations]
 "/Users/ppecz/.local/tools/emsdk/upstream/bin/wasm-opt" --asyncify --pass-arg=asyncify-relocatable --pass-arg=asyncify-asserts --pass-arg=asyncify-imports@env.invoke_*,env.emscripten_sleep,env.emscripten_wget,env.emscripten_wget_data,env.emscripten_idb_load,env.emscripten_idb_store,env.emscripten_idb_delete,env.emscripten_idb_exists,env.emscripten_idb_load_blob,env.emscripten_idb_store_blob,env.SDL_Delay,env.emscripten_scan_registers,env.emscripten_lazy_load_code,env.emscripten_fiber_swap,wasi_snapshot_preview1.fd_sync,env.__wasi_fd_sync,env._emval_await,env._dlopen_js,env.__asyncjs__* --strip-producers src/sample.wasm -o src/sample.wasm -g --mvp-features --enable-mutable-globals --enable-sign-ext
sbc100 commented 1 year ago

@brendandahl