emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.69k stars 3.29k forks source link

Runtime Errors: "Alignment Fault" and "Memory Access Out of Bounds" #22606

Open slashpot opened 2 weeks ago

slashpot commented 2 weeks ago

emcc -v output

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.65 (7f8a05dd4e37cbd7ffde6d624f91fd545f7b52e3)
clang version 20.0.0git (https:/github.com/llvm/llvm-project 547917aebd1e79a8929b53f0ddf3b5185ee4df74)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Users/eric.lin88/Development/opensource/emsdk/upstream/bin

Hi,

I tried to compile cloudflare's doom-wasm project but encountered the following runtime error:

Uncaught RuntimeError: Aborted(alignment fault)
    at abort (websockets-doom.js:663:41)
    at alignfault (websockets-doom.js:343:3)
    at imports.<computed> (websockets-doom.js:9675:20)
    at websockets-doom.wasm (websockets-doom.wasm:0x5ad94a)
    at websockets-doom.wasm.SaveGameSettings (d_net.c:137:31)
    at websockets-doom.wasm.D_CheckNetGame (d_net.c:238:5)
    at websockets-doom.wasm.D_DoomMain (d_main.c:1648:5)
    at websockets-doom.wasm.main (i_main.c:68:5)
    at ret.<computed> (websockets-doom.js:9701:20)
    at websockets-doom.js:695:12

I found that this error is caused by the emscripten flag SAFE_HEAP=1. After setting this flag to 0, the game reaches the menu screen. However, when starting a level, the game freezes with the following runtime error:

websockets-doom.js:54 Uncaught RuntimeError: memory access out of bounds
    at websockets-doom.wasm.P_PlayerThink (p_user.c:267:40)
    at websockets-doom.wasm.P_Ticker (p_tick.c:145:6)
    at websockets-doom.wasm.G_Ticker (g_game.c:870:9)
    at websockets-doom.wasm.RunTic (d_net.c:88:5)
    at websockets-doom.wasm.TryRunTics (d_loop.c:696:13)
    at websockets-doom.wasm.D_RunFrame (d_main.c:395:5)
    at websockets-doom.wasm.dynCall_v (websockets-doom.wasm:0x4fe465)
    at ret.<computed> (websockets-doom.js:9439:24)
    at websockets-doom.js:647:12
    at browserIterationFunc (websockets-doom.js:8607:41)

I tried the following flags, but the issue persists:

-s STACK_SIZE=64mb -s INITIAL_HEAP=64mb -s DEFAULT_PTHREAD_STACK_SIZE=64mb -s INITIAL_MEMORY=256MB

I've tested on my Mac M1, x86_64 Linux, and the Gitpod workspace for this repo (https://github.com/gitpod-io/doom), all resulting in the same error. I'm running out of ideas — can anyone help check this?

Step to reporduce

# install 
brew install emscripten
brew install automake
brew install sdl2 sdl2_mixer sdl2_net

# build
./scripts/build.sh

# run project
cd src
python -m SimpleHTTPServer
sbc100 commented 1 week ago

Maybe open a bug in https://github.com/cloudflare/doom-wasm?

It looks like a crash in P_PlayerThink? You would build with -g and then debug in devtools, or add some tracing/logging do see how/why there is an OOB memory access there.