WebAssembly / wasi-sdk

WASI-enabled WebAssembly C/C++ toolchain
Apache License 2.0
1.27k stars 190 forks source link

Execution of wasm file generated using wasi-sdk fails #470

Closed Sandesh-verma closed 2 months ago

Sandesh-verma commented 2 months ago

I have a simple hello world program for C++. Then i created a .wasm file for the same using the below command

/opt/wasi-sdk/bin/clang++ -O3 -z stack-size=4096 -Wl,--initial-memory=65536 -o test.wasm main.cpp -Wl,--export=main -Wl,--export=__main_argc_argv -Wl,--export=__data_end -Wl,--export=__heap_base -Wl,--no-entry -Wl,--allow-undefined -fno-exceptions -nostdlib

On trying to execute the generated test.wasm, using WAMR/Wasmedge, I get the below errors..

iwasm test.wasm 

[02:07:06:602 - 7FDB24141740]: failed to check signature '(i)' and resolve pointer params for import function (env, abort)
[02:07:06:602 - 7FDB24141740]: warning: failed to link import function (env, _ZNKSt3__28ios_base6getlocEv)
[02:07:06:602 - 7FDB24141740]: warning: failed to link import function (env, _ZNKSt3__26locale9use_facetERNS0_2idE)
[02:07:06:602 - 7FDB24141740]: warning: failed to link import function (env, _ZNSt3__26localeD1Ev)
[02:07:06:602 - 7FDB24141740]: warning: failed to link import function (env, _ZNSt3__213basic_ostreamIcNS_11char_traitsIcEEE3putEc)
[02:07:06:602 - 7FDB24141740]: warning: failed to link import function (env, _ZNSt3__213basic_ostreamIcNS_11char_traitsIcEEE5flushEv)
[02:07:06:602 - 7FDB24141740]: warning: failed to link import function (env, _ZNSt3__213basic_ostreamIcNS_11char_traitsIcEEE6sentryC1ERS3_)
[02:07:06:602 - 7FDB24141740]: warning: failed to link import function (env, _ZNSt3__28ios_base5clearEj)
[02:07:06:602 - 7FDB24141740]: warning: failed to link import function (env, _ZNSt3__213basic_ostreamIcNS_11char_traitsIcEEE6sentryD1Ev)
[02:07:06:602 - 7FDB24141740]: warning: failed to link import function (env, _Znwm)
[02:07:06:602 - 7FDB24141740]: warning: failed to link import function (env, _ZdlPv)
[02:07:06:602 - 7FDB24141740]: warning: failed to link import function (env, abort)
[02:07:06:602 - 7FDB24141740]: warning: allocate 16 bytes memory failed
Exception: allocate memory failed

wasmedge test.wasm 

[2024-08-06 11:08:30.429] [error] instantiation failed: unknown import, Code: 0x62
[2024-08-06 11:08:30.429] [error]     When linking module: "env" , function name: "_ZNKSt3__28ios_base6getlocEv"
[2024-08-06 11:08:30.429] [error]     At AST node: import description
[2024-08-06 11:08:30.429] [error]     This may be the import of host environment like JavaScript or Golang. Please check that you've registered the necessary host modules from the host programming language.
[2024-08-06 11:08:30.429] [error]     At AST node: import section
[2024-08-06 11:08:30.429] [error]     At AST node: module

Am I missing anything here when creating the wasm module?

yamt commented 2 months ago

/opt/wasi-sdk/bin/clang++ -O3 -z stack-size=4096 -Wl,--initial-memory=65536 -o test.wasm main.cpp -Wl,--export=main -Wl,--export=__main_argc_argv -Wl,--export=__data_end -Wl,--export=__heap_base -Wl,--no-entry -Wl,--allow-undefined -fno-exceptions -nostdlib

start from something simpler, like: /opt/wasi-sdk/bin/clang++ -O3 -fno-exceptions -o test.wasm main.cpp

Sandesh-verma commented 2 months ago

closing the issue as the thread is being discussed in WAMR#3690

sbc100 commented 2 months ago

For anyone else that arrives here looking for answers, the problem here is that you are linking with -nostdlib which means that C++ standard library is not being includes, and hence all the undefined C++ symbols such as _ZNSt3__28ios_base5clearEj.

In general you should also avoid --allow-undefined since it delayes these issues until run time, when they are better solved at link time.