You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The rough plan is to look at Emscripten's port of libunwind to see if it can be either ported to upstream LLVM, or if not, ported to wasi-sdk. I don't know of anyone working on this yet.
It's also worth noting that Wasm Exceptions are not yet standardized; currently the proposal is in phase 3.
We hope to have an initial contribution to WAMR with exception support in the next few months. There is such a swathe of C++ software in-particular that is excluded from the WASM ecosystem because of the lack of exception support. It would be awesome to see a solution.
Although, digging through the code that is generated by the emscripten tool chain we can see, that at least for C++ it generates a single WASM exception throw and catch. This mimics almost exactly the approach used by traditional C++ compilers, but it means that the majority of the, quite elegant and nuanced behavior described in the WASM Exception proposal is rarely used. - This might be different for other programming languages - I've not checked.
As Dan mentioned, the easy part is enabling exceptions in the compiler (e.g. passing -fwasm-exceptions). The more compex/nuanced part is getting libc++abi/libunwind configured correctly.
I have a plan to upstream the current libc++abi/libunwind changes to the upstream LLVM. I think it can happen within a couple months if having this is a priority; the timeline can depend on the receptions from the reviewers though.
By the way there is a discussions going on (WebAssembly/exception-handling#280) which can change the spec itself. I don't think the library side will change much with this though.
Reacted by Andrew Brown, Chris Woods and DavindoIndo
Just wanted to check, would WASM exceptions make sense for implementing C setjmp / longjmp (i.e., how postgres uses them to implement exceptions), or are they too different and only make sense with language-native exceptions like in C++? If the latter, is there currently no plan that would be able to support setjmp / longjmp in WASI?
I found that llvm 17 was branched off end of June and above exception patches were merged mid September, so we need to wait for llvm 18 (should branch off today, first RC on Friday) or upgrade to a git version.
The -fwasm-exceptions flag allows the compiler to output the try / catch and other exception handling instructions in WebAssembly bytecode. But it is up to the language implementation to determine how these instructions are used.
Languages aren't used to dealing with WASM exception instructions. Instead they traditionally use setjmp / longjmp, C++ is a good example of this. Therefore the language's supporting library (also confusingly called the C++ runtime) needs to be updated to use exception handling.
When the C++ compiler encounters an exception in the code you are attempting to compile, behind the scenes it generates calls to it's runtime library to implement the exception handling. It is inside the runtime library that the exception instructions are used.
The error your seeing is an error generated by WASI-SDK, which bundles the C++ runtime library, the version of the library supplied by WASI-SDK doesn't include the three functions listed above - which are all designed for exception handling.
So when you compile C++ with an exception in your code, the compiler generates it faithfully, with calls to functions in the C++ runtime library which do not exist, this is why, at link time you get the error message you mentioned.
The Emscripten folks have worked on this and have an update which includes the C++ runtime functions we're missing. I understand they were hoping to upstream this to LLVM, and then WASI-SDK would be able to pull this down. - I'm not sure of the current state of this work, @sunfishcode and @aheejin and, I think the best folks to be able to help on that front.
Even though the file name says 'emscripten', some of the functions in there are also used by Wasm SjLj handling (which is, SjLj handling using the instructions in the Wasm EH proposal) That file was newly added in Emscripten and didn't come from the compiler-rt in LLVM.
I understand this is getting confusing somewhat, which I think stemmed from that Emscipten uses its custom build system.. I guess we should consider making LLVM's CMake system to reflect the Emscripten setup. In the meantime, this is the list of library files created/modified for Wasm EH in Emscripten:
Why is it still not working to me? I am using the latest version of wasi-sdk:
because no one enabled it for wasi-sdk?
except some issues in llvm, i guess we can enable it if we find a nice way to keep compatibility with runtimes without exception-handling.
i was able to run your test code with a few hacks.
spacetanuki% /opt/wasi-sdk-25.0/bin/wasm32-wasip1-clang++ -fwasm-exceptions --sysroot=/Volumes/PortableSSD/git/wasi-sdk/build/install/share/wasi-sysroot a.cxx
-lunwind
spacetanuki% ~/git/wasm/binaryen/b/bin/wasm-opt --translate-to-exnref -all -o a a.out
spacetanuki% toywasm --wasi a
Trying operation that does not throw...
Result: 42
Trying operation that will throw...
Caught exception: Oops! Something went wrong.
Program continues after exception.
spacetanuki%
Activity
sunfishcode commentedon Jul 6, 2023
The rough plan is to look at Emscripten's port of libunwind to see if it can be either ported to upstream LLVM, or if not, ported to wasi-sdk. I don't know of anyone working on this yet.
It's also worth noting that Wasm Exceptions are not yet standardized; currently the proposal is in phase 3.
woodsmc commentedon Jul 7, 2023
We hope to have an initial contribution to WAMR with exception support in the next few months. There is such a swathe of C++ software in-particular that is excluded from the WASM ecosystem because of the lack of exception support. It would be awesome to see a solution.
Although, digging through the code that is generated by the emscripten tool chain we can see, that at least for C++ it generates a single WASM exception
throwandcatch. This mimics almost exactly the approach used by traditional C++ compilers, but it means that the majority of the, quite elegant and nuanced behavior described in the WASM Exception proposal is rarely used. - This might be different for other programming languages - I've not checked.sbc100 commentedon Jul 7, 2023
As Dan mentioned, the easy part is enabling exceptions in the compiler (e.g. passing
-fwasm-exceptions). The more compex/nuanced part is getting libc++abi/libunwind configured correctly.To see that current differences from upstream that were made in emscripten you can take a look here: https://github.com/emscripten-core/emscripten/blob/main/system/lib/libcxxabi/readme.txt. Specifically check out the link at the end that shows you the diffs from upstream llvm 16: llvm/llvm-project@llvmorg-16.0.4...emscripten-core:emscripten-libs-16
woodsmc commentedon Jul 7, 2023
Whooha... Rockstar's both of you, and I've got some reading to do. Thanks for the pointers. Let me take a look.
abrown commentedon Jul 25, 2023
There's also a draft PR with initial support for this in #198, which you may be interested in.
aheejin commentedon Aug 3, 2023
I have a plan to upstream the current libc++abi/libunwind changes to the upstream LLVM. I think it can happen within a couple months if having this is a priority; the timeline can depend on the receptions from the reviewers though.
By the way there is a discussions going on (WebAssembly/exception-handling#280) which can change the spec itself. I don't think the library side will change much with this though.
aheejin commentedon Sep 22, 2023
Thank you for the wait. Wasm EH's libcxxabi/libunwind changes have now been upstreamed:
llvm/llvm-project@e6cbba7
llvm/llvm-project@058222b
sroussey commentedon Jan 6, 2024
What does
-fwasm-exceptionsdo? Adding or removing that makes no difference, I still end up withthx
anuraaga commentedon Jan 22, 2024
Just wanted to check, would WASM exceptions make sense for implementing C
setjmp/longjmp(i.e., how postgres uses them to implement exceptions), or are they too different and only make sense with language-native exceptions like in C++? If the latter, is there currently no plan that would be able to supportsetjmp/longjmpin WASI?whitequark commentedon Jan 22, 2024
I think you should be able to lower SjLj to exceptions.
sbc100 commentedon Jan 22, 2024
Indeed, llvm already uses wasm exception to implement SJLJ, when wasm exceptions are enabled, so it should just work once we enable exceptions.
sunfishcode commentedon Jan 22, 2024
Indeed, wasm exceptions is how we've always envisioned
setjmp/longjmpwould work.anuraaga commentedon Jan 23, 2024
Thanks for confirming! I implemented stack unwind for wazero
wazero/wazero#1808
and look forward to trying it out with wasi SDK (from my reading of the thread it isn't quite ready yet for use with the published SDKs)
cpetig commentedon Jan 23, 2024
I found that llvm 17 was branched off end of June and above exception patches were merged mid September, so we need to wait for llvm 18 (should branch off today, first RC on Friday) or upgrade to a git version.
woodsmc commentedon Jan 25, 2024
The
-fwasm-exceptionsflag allows the compiler to output the try / catch and other exception handling instructions in WebAssembly bytecode. But it is up to the language implementation to determine how these instructions are used.Languages aren't used to dealing with WASM exception instructions. Instead they traditionally use setjmp / longjmp, C++ is a good example of this. Therefore the language's supporting library (also confusingly called the C++ runtime) needs to be updated to use exception handling.
When the C++ compiler encounters an exception in the code you are attempting to compile, behind the scenes it generates calls to it's runtime library to implement the exception handling. It is inside the runtime library that the exception instructions are used.
The error your seeing is an error generated by WASI-SDK, which bundles the C++ runtime library, the version of the library supplied by WASI-SDK doesn't include the three functions listed above - which are all designed for exception handling.
So when you compile C++ with an exception in your code, the compiler generates it faithfully, with calls to functions in the C++ runtime library which do not exist, this is why, at link time you get the error message you mentioned.
The Emscripten folks have worked on this and have an update which includes the C++ runtime functions we're missing. I understand they were hoping to upstream this to LLVM, and then WASI-SDK would be able to pull this down. - I'm not sure of the current state of this work, @sunfishcode and @aheejin and, I think the best folks to be able to help on that front.
4 remaining items
yamt commentedon Jan 31, 2024
thank you.
i did an experiment to use sjlj with wasi. it seems working well.
https://github.com/yamt/garbage/tree/master/wasm/longjmp
(i wrote the runtime (rt.c) by myself because i didn't know where emscripten version was.)
yamt commentedon Jan 31, 2024
WebAssembly/wasi-libc#467
alepvn33 commentedon Jun 27, 2025
Why is it still not working to me? I am using the latest version of wasi-sdk:
25.0
wasi-libc: 574b88da4815
llvm: ab4b5a2db582
llvm-version: 19.1.5
config: f992bcc08219
I am trying to compile this:
with this command
/opt/wasi-sdk/bin/clang++ --target=wasm32-wasi -O2 -fwasm-exceptions -o test_exceptions.wasm test_exceptions.cppbut I still get these errors
yamt commentedon Jul 2, 2025
because no one enabled it for wasi-sdk?
except some issues in llvm, i guess we can enable it if we find a nice way to keep compatibility with runtimes without exception-handling.
i was able to run your test code with a few hacks.
Upgrade %wasi.r for SDK v. 25, build fixes
Upgrade %wasi.r for SDK v. 25, build fixes
Upgrade %wasi.r for SDK v. 25, build fixes
Upgrade %wasi.r for SDK v. 25, build fixes
Upgrade %wasi.r for SDK v. 25, build fixes
Upgrade %wasi.r for SDK v. 25, build fixes
Build a sysroot that supports C++ exceptions by default
Build a sysroot that supports C++ exceptions by default
whitequark commentedon Apr 8, 2026
Thank you!
Build a sysroot that supports C++ exceptions by default (WebAssembly#606