wasmer
wasmer copied to clipboard
"RuntimeError: out of bounds memory access" (C++ -> Wasm via Enscripten)
Describe the bug
I'm using enscripten to convert a C++ project (https://github.com/KieranP/Game-Of-Life-Implementations/tree/master/c%2B%2B) into WASM, and then using wasmer to run it. Compiling/running the application as C++ has no issues and enscripten reports no issues converting to wasm. When running with wasmer, am getting unexpected "RuntimeError: out of bounds memory access" errors.
wasmer 5.0.4 (??????? 2024-12-17)
binary: wasmer-cli
commit-hash:
commit-date: 2024-12-17
host: aarch64-apple-darwin
compiler: cranelift
c_api backend:
rustc 1.83.0 (90b35a623 2024-11-26) (Homebrew)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: aarch64-apple-darwin
release: 1.83.0
LLVM version: 19.1.6
Steps to reproduce
$ emcc -o play.wasm play.cpp
$ wasmer run play.wasm
error: RuntimeError: out of bounds memory access
╰─▶ 1: RuntimeError: out of bounds memory access
Expected behavior
Should run without error. Errors should also indicate the cause a bit more clearly.
Actual behavior
Throws error that doesn't exist when running as C++ project. Location of out of bounds memory access unknown.
If I change play.cpp lines 19-21 from:
if (!minimal) {
cout << world->render();
}
to
if (!minimal) {
cout << world->render();
cout << "here?";
}
it executes the first cout call, but not the second.
So somewhere after successfully calling cout << world->render();, something is getting messed up before it reaches the next line.
Hello! Thanks for the report. Sounds like an issue on our side, yes.
Before taking on this issue, can you try compiling it with our clang, i.e. wasmer run clang/clang play.cpp -o play.wasm and run that .wasm file?
@xdoardo Sure, happy to help debug, but the command doesn't seem to work :-/
$ wasmer run clang/clang play.cpp -o play.wasm
error: unexpected argument '-o' found
tip: to pass '-o' as a value, use '-- -o'
Usage: wasmer run [OPTIONS] <INPUT> [ARGS]...
For more information, try '--help'.
$ wasmer run clang/clang play.cpp -- -o play.wasm
clang-16: error: no such file or directory: 'play.cpp'
$ ls play.cpp
play.cpp
Any ideas?
Edit: Found online docs that suggest using --dir=., so tried running the following, but it has been stuck at 100% CPU for over 5 minutes now.
wasmer run clang/clang --dir=. -- play.cpp -o play.wasm
Edit: I'm also seeing error when running the emcc generated wasm file in wasmtime and wazero (output below), so perhaps it's less wasmer's issue and an issue should be filed with emscripten?
$ wasmtime run play.wasm
Error: failed to run main module `play.wasm`
Caused by:
0: failed to invoke command default
1: error while executing at wasm backtrace:
0: 0x18bc - <unknown>!<wasm function 9>
1: 0x29b3 - <unknown>!<wasm function 18>
2: memory fault at wasm address 0xd97b5d96 in linear memory of size 0x1020000
3: wasm trap: out of bounds memory access
$ wazero run play.wasm
error instantiating wasm binary: module[] function[_start] failed: wasm error: out of bounds memory access
wasm stack trace:
.$9() i32
.$18()
Thanks for taking the time to investigate and for pointing out the issue with clang/clang.
Yes, assuming that it works natively, if it fails with other runtimes there's a chance that emcc is generating incorrect code, and it would be good to file this issue on emscripten.
Nonetheless, until further notice, let's keep this issue open so I can investigate it.
@xdoardo Just a quick follow up. Just upgraded emscripten from version 3.1.74 to the newly released 4.0.0, and still seeing the same issue. emscripten compiles fine without errors (one warning about implicit int to float, but fixing it doesn't affect anything). But the resulting wasm file still hits out of bounds memory access errors in wasmer, wasmtime, and wazero. Is there anything you would like me to do to help debug this?
Is there anything you would like me to do to help debug this?
Not for now, thanks a lot for asking. We're in kind of a crunch in this moment, and we'll take care of this as soon as possible.