Uncaught RuntimeError: unaligned memory access (wasm only build)

28 views
Skip to first unread message

Александр Гурьянов

unread,
Dec 10, 2024, 3:39:46 PM12/10/24
to emscripte...@googlegroups.com
Hi, periodically my WebAssembly (Wasm) crashes with this error: "Uncaught RuntimeError: unaligned memory access." As far as I understand, Wasm can perform unaligned memory access. So why am I encountering this error?

Linker  flags:
"-sUSE_SDL=2" "-sUSE_SDL_IMAGE=2" "-sUSE_SDL_MIXER=2" "-sUSE_SDL_TTF=2" "-sUSE_ZLIB=1"
"-sSDL2_IMAGE_FORMATS=['bmp']"
"-sSTACK_SIZE=1Mb"
"-sTOTAL_MEMORY=512Mb"
"-sALLOW_MEMORY_GROWTH=0"
"-sPTHREAD_POOL_SIZE=6"
"-sPTHREAD_POOL_SIZE_STRICT=0"
"-sFORCE_FILESYSTEM=1"
"-sEXPORT_NAME=VCMI"
"-sMODULARIZE=1"
"-sINVOKE_RUN=0"
"-sEXPORTED_RUNTIME_METHODS=['addRunDependency', 'removeRunDependency', 'FS', \
'lengthBytesUTF8', 'stringToUTF8', 'UTF8ToString', 'callMain', 'run']"
"-sEXPORTED_FUNCTIONS=['_main', '_malloc', '_free']"
"-pthread"
"-fwasm-exceptions"
"-sMALLOC=mimalloc"
"-Oz"
# for debugging
# "-O1"
"--profiling-funcs"
# "-sASSERTIONS=2"

Full error stack  (not very useful):

Uncaught RuntimeError: unaligned memory access
    callUserCallback https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    runIter https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    requestAnimationFrame https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_scheduler_rAF https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9
    MainLoop_runner https://vcmi.dos.zone/launcher/vcmi/vcmiclient.js:9





Alon Zakai

unread,
Dec 10, 2024, 5:17:17 PM12/10/24
to emscripte...@googlegroups.com
AFAIK the one place wasm may trap on unaligned data is atomic operations. See

https://github.com/WebAssembly/threads/blob/main/proposals/threads/Overview.md#alignment ("Unlike normal memory accesses, misaligned atomic accesses trap")

You may need to avoid packing data if it will be accessed using atomics. Also atomic SIMD data may need special handling so it is fully aligned.

- Alon


--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/emscripten-discuss/CAKOm%3DVFGvOQOHqn%3D-bNOMdYmzNFSnbBveUhCphGaNGxOgCi3fw%40mail.gmail.com.

Sam Clegg

unread,
Dec 10, 2024, 5:22:48 PM12/10/24
to emscripte...@googlegroups.com
Also, I'm a little confused about why there are no wasm frames on the stack.  If you are using DevTools you should be able to see the exact wasm instruction that caused that trap, or at the very least it should give you the byte offset of the bad instruction and the function name/number.

Александр Гурьянов

unread,
Dec 11, 2024, 4:07:39 AM12/11/24
to emscripte...@googlegroups.com
1. I use only the simplest atomics, like atomic_int, atomic_bool, etc. I will try to figure it out.

2. Regarding the Wasm stack, I am also a bit frustrated. In Firefox, when you save a log, it never contains the Wasm stack. To get it, you need to manually click on the triangle near the error message to view the detailed stack. Unfortunately, these reports came from users who didn’t do this. Also, this problem never happens in Chrome (no reports yet).

ср, 11 дек. 2024 г. в 01:22, 'Sam Clegg' via emscripten-discuss <emscripte...@googlegroups.com>:
Reply all
Reply to author
Forward
0 new messages