-
Notifications
You must be signed in to change notification settings - Fork 191
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
C++, no exceptions, and OOM in operator new
#406
Comments
Also cc @abrown on this since, by default, compiling with threads sets |
For comparison, here is what emscripten does: That code will abort rather than return 0 from Slightly complicating this is that emscripten also does the same for That is definitely nonstandard (but very useful for the above reason) and so we have an option to turn that off (to return 0 from malloc), which some users require as they want to recover from OOMs. |
Ah makes sense, and thanks for the links! My sense of spec compliance originated from libcxx's current source where the intention seems to be to return |
This example program is intended to allocate 4 wasm pages of memory:
This can be compiled to have a max wasm memory size of 2 pages, though, which will cause the program to OOM at runtime:
which produces foo.wasm.gz.
When run:
this program will (currently for me) infinite loop.
The issue seems to be that when compiled with
-fno-exceptions
, which I believe is currently required of C++ code compiled with wasi-libc to wasm since__cxa_throw
isn't otherwise defined, then theoperator new
operator is defined to returnNULL
for failed allocations. For a native platform this is fine because it probably faults quickly when accessing this address. For Wasm, however, this is a valid address so the program keeps going.This was, for me, an unfortunately slow way for me to discover that the original program I was working with was OOM-ing and needed a
--max-memory
argument to be passed that was larger than the base. I'm not sure how best to improve this, though, since I suspect aborting on failure would not be spec-compliant with C++.The text was updated successfully, but these errors were encountered: