-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Heap corruption after fetch when using WASM exceptions #23168
Comments
IIUC the This is because the old way that |
That's interesting, I've just revisited the documentation and noticed the note to the same effect as what you said, at the bottom of https://emscripten.org/docs/api_reference/emscripten.h.html#c.emscripten_set_main_loop. I hadn't noticed it before as it's only mentioned there, and not for So does this mean that with wasm exceptions turned on, current behaviour is just as if In that case, what's the current best practice approach for maintaining state while running an infinite loop, if I want to use wasm exceptions? Is it just to allocate |
There is a little of of nuance. So if your current behaviour relies on C++ heap objects (with destructors) outliving the call to |
I see, thanks for the clarification. In that case it's clear how to work around this; I think just a warning about this behaviour would be useful, for example when simulate_infinite_loop is true and wasm exceptions are enabled. It doesn't need to be an error necessarily, as long as the user is somehow alerted to the change in behaviour. |
Agreed. @aheejin @dschuff @kripken, do you think we just stuck with this feature degradation here? Or is there some way we could conditionally disable the running of destructors during unwind? If we are stuck with this when we should either have a warning or just a hard error when |
Hmm, I can't think of a good way to fix this myself, but others know more about that area. I agree a warning or error makes sense, when using wasm EH + simulating an infinite loop. |
The default behavior of ensuring that C++ destructors run when JS exceptions unwind through wasm frames is implemented by using The other option might be to instead of implementing the return to event loop with a JS throw, it could be implemented with a wasm trap. This would also have the effect of uncatchably unwinding all the wasm frames (and would propagate through any JS frames as |
Interesting idea, yeah, a trap would work here. We do already have an export for that, too. |
This is a weird one!
After calling
emscripten_fetch
, heap-allocated memory held by members of objects allocated on the stack (such as thetest_vector
in the example below) becomes corrupted with garbage values. This only happens when exception mode is WASM exceptions.I isolated a minimal example of this with
emscripten_fetch
, but I believe it's not limited to the fetch functionality, as I get similar symptoms elsewhere when enabling WASM exceptions in more complex programs.Below is a minimal example to trigger this behaviour, with a CMakeLists to aid in setting the right flags:
main.cpp
CMakeLists.txt
Build and run:
emcmake cmake -B build emmake cmake --build build -j$(nproc) emrun build/client.html
Example console output in Chromium 131.0.6778.108:
Example output in Firefox Nightly:
Note that we expect the
test_vector
contents to be123
,456
,789
but these values get replaced after theemscripten_fetch
call.Switch
EXCEPTION_HANDLING
inCMakeLists.txt
fromwasm
tojs
ornone
to observe that this issue only happens when using WASM exceptions.Comment out
emscripten_fetch
to observe the heap corruption goes away, even in WASM exception mode.You can create a
test.txt
file to trigger the on_success callback, but the issue occurs even if the download doesn't succeed.The text was updated successfully, but these errors were encountered: