Repository navigation
Closed
Description
I noticed that there wasn't already a tracking issue for C++ exceptions, so I wanted to open one up to at least track the progress of this. Currently wasi-sdk does not support C++ exceptions, and trying to fiddle with the build locally shows that it's not a turnkey solution at this time or a small PR to some build configuration. Some questions that I ran into locally are:
- Should exceptions support be provided in existing targets, or a new target? My assumption is that the entire C++ standard library needs to be built with/without exceptions meaning that technically
-fexceptionswants an entirely different sysroot than-fno-exceptions. I don't believe that Clang currently dynamically selects a different sysroot based on this flag, so the next best thing we could do would be to add more targets. That would double the size of the target list, however. - Right now using the standard wasm exceptions proposal requires
-mllvm -wasm-use-legacy-eh=false, and I'm not sure if that would require tripling the sysroot where one uses legacy eh, one doesn't, and one uses no eh. - Currently
-fwasm-exceptionslooks to be required, but I'm not sure if this is a temporary state of affairs or one that's intended to last. - Wasi-sdk looks like it will need to build LLVM's libunwind. There's a few build issues with this where it doesn't natively build for wasm as-is (even with all exception-related things turned on). It's not clear to me if (a) libunwind is not expected to build for wasm, (b) libunwind should be buildable as-is but maybe with different configuration, or (c) perhaps there are Emscripten patches to libunwind which haven't made their way into LLVM itself.
- I don't know where the responsibility of passing
-lunwindlies. Should the Clang driver automatically pass that? Should users be required to pass that?
These are the questions I've run into locally myself. From an implementation perspective all the "meat" of C++ exceptions is done in the sense that I was able to edit enough files to get something compiling and executing. The remaining bits of work are all on the toolchain side I believe (all of the above questions)
Reacted by Christof Petig, Frederik Seiffert, Ryuta Suzuki, Mendy Berger, TÖRÖK Attila, Milan Raj and Tian Xia
Metadata
Metadata
Assignees
Labels
No labels
Activity
dicej commentedon Oct 8, 2025
Regarding the existing targets vs. new targets question: perhaps a good first step would be to add support for building all existing targets with exceptions enabled, but not make that the default. So we'd continue publishing builds without exception support, but allow people to experiment with them by building from source.
yamt commentedon Oct 8, 2025
there are at least a few attempts to fix the build.
https://gist.github.com/yamt/b699eb2604d2598810a2876ff2ffc8d8 (mine)
llvm/llvm-project#140365
llvm/llvm-project#156087 (i haven't looked at this one closely)
whitequark commentedon Oct 22, 2025
See also #334, which is less detailed than this issue but was filed earlier.
yerzham commentedon Nov 16, 2025
I applied changes to
libunwindfrom https://gist.github.com/yamt/b699eb2604d2598810a2876ff2ffc8d8 and addedlibunwindto be included in wasi-sdk sysroot build. I made sure to use-fwasm-exceptionsand-mllvm -wasm-use-legacy-eh=falsewhere possible, but I kept having issues with__cpp_exceptionbeing undefined when building sysroot.Some debugging revealed that code around llvm/llvm-project#159143 used by emscripten toolchain was likely responsible for that. Defining
__cpp_exceptiontag moved to the emscripten toolchain. Previous version wouldn't have worked anyway because fPIC is enabled for wasip2. Correct me if I am wrong.So I kept the
__cpp_exceptiontag emission and marking it weak (whatever weak means, idk) if it is targeting WASI instead of checking ifisPositionIndependent. I suppose defining__cpp_exceptioncan be also moved to wasi-sdk/libc just like emscripten did?At the end, I was able to build the whole wasi-sdk with patched llvm project, and successfully compiled a C++ code with exception throws. In my set up I did not have to pass
-fwasm-exceptionsor-lunwindduring the sample C++ code compilation. Running it withwasmtime run -W exceptions=yworked. Without the exception feature, wasmtime correctly errored withexception refs not supported without the exception handling feature.I hope my couple day debugging session around this helps :)
EDIT: relevant conversation already happened at llvm/llvm-project#103592 (comment)
yerzham commentedon Nov 16, 2025
Ok, my attempts to fix the older version of llvm project were irrelevant, sorry. As a note for people like me who used the latest release of wasi-sdk (28) and wanted to understand if and when C++ exceptions are going to be supported:
-fwasm-exceptionstoo.whitequark commentedon Nov 16, 2025
I think the biggest difficulty here is that it makes sense to ship both exceptions-enabled and non-exceptions-enabled builds of the sysroot side by side, but building this and threading it through the compiler is not quite trivial.
torokati44 commentedon Nov 16, 2025
What's the argument for exceptionless builds? I suppose, technical details like "smaller", "faster", etc.? AFAIK working exceptions are a non-optional part of C++, therefore, if a toolchain can't do exceptions, it can't really do C++.…? More like "C with classes"? 😛
whitequark commentedon Nov 16, 2025
There are runtimes in active widespread use that do not have exceptions and may not gain support for exceptions quickly. As much as I wish exceptions were available 5 years ago (seriously, I've had to patch so much stuff to build on, essentially, non-conformant C++ compilers) the reality is that they still aren't uniformly available. (For example, Wazero still does not implement EH.)
That being said, if wasmtime fully supports exceptions now, I personally would not shed a tear for exception-less builds of WASI SDK. (It used not to not so long ago, at least...)
whitequark commentedon Nov 16, 2025
Looks like the status as of Wasmtime 37 is:
torokati44 commentedon Nov 16, 2025
Sounds like chicken and egg issue?
whitequark commentedon Nov 16, 2025
I think it is mostly a result of implementation complexity, coupled with the fact that the EH spec only recently became fully usable to implementers.
yerzham commentedon Nov 16, 2025
I tested wasi-sdk build result with the newest version wasmtime and it worked, yes. I was super happy to see C++ with exceptions working without requiring me to rewrite a big-ish library to work without exceptions.
As starters, I would love to see some experimental support added for building wasi-sdk from source with exceptions enabled with short docs included. I can make a PR, but I do not trust myself on this. Also, libunwind needs to be patched first anyway. That would make it easier for people experimenting with large C++ codebases to get into WASI without too much pain in between.
As for the question of adding additional sysroot builds and how to distribute the builds, that can be done later?
whitequark commentedon Nov 16, 2025
I can review the libunwind LLVM PR if you make one.
alexcrichton commentedon Nov 17, 2025
Personally I think the best path forward would be to first get everything landed/implemented such that locally wasi-sdk can be built with/without exceptions (e.g. a CMake-based toggle). At that point I think it would be worth taking stock again to figure out how exactly to manage its rollout. For example we'd, at that point, probably be better equipped to answer the questions of:
Figuring out how best to roll this out is going to require a balance of concerns with what's possible in the ecosystem engine-wise and what the cost of various options here are in terms of toolchain complexity. The easiest option here is likely to be "just turns exceptions on by default" in wasi-sdk, but that would require modules to be runnable in a breadth of runtimes because turning off exceptions won't be easy (it'd require using an older wasi-sdk or compiling your own wasi-sdk). The hardest option is probably "ship multiple sysroots and select the right one based on compiler exception-related flags". I'm not even sure if that would work but it's at least worth writing down as an option I think.
sbc100 commentedon Nov 17, 2025
Another option is to use new subdirectory within the existing sysroot. This is what we do for the LTO variants: https://github.com/llvm/llvm-project/blob/6f5c8fe1c1d24604d3328b82f5a1ed348e59326f/clang/lib/Driver/ToolChains/WebAssembly.cpp#L259-L265
This way the we would only need to compile/ship the system libraries that actually use exception in this mode (i.e. libc++ and libc++abi, and not libc or compiler-rt).
22 remaining items
alexcrichton commentedon Apr 30, 2026
If anyone subscribed here has a repo to test on, I'd appreciate if you could test out the binaries at https://github.com/WebAssembly/wasi-sdk/releases/tag/wasi-sdk-33 before they're officially published
vapier commentedon Apr 30, 2026
test in what way ? whether wasi-sdk-33 works with enabling exceptions ? is there a big diff between 32 & 33 ? when i tried wasi-sdk-32, it didn't seem to be working for me.
juntyr commentedon Apr 30, 2026
My build env combines the wasi-sysroot and libclang-rt from the wasi-sdk with normal clang (so as to not rely on only supporting the platforms that the release includes). Since the LLVM patches in this release haven't been included in releases yet, I unfortunately can't test the release, sorry. I was previously using https://github.com/alexcrichton/wasi-sdk/releases/download/wasi-sdk-30.0-cpp-exn, which worked for me
alexcrichton commentedon Apr 30, 2026
@vapier test insofar as compile code using C++ exceptions with wasi-sdk-33 using these instructions. The wasi-sdk-33 release specifically includes #606 which closed this issue which means that C++ exceptions should be supported.
@juntyr makes sense yeah. Would you be able to temporarily use the wasi-sdk-33 toolchain to confirm everything works though? If not that's totally fine, I mostly figured I'd solicit thoughts/opinions here if anyone's interested before the final release.
whitequark commentedon Apr 30, 2026
I'd like to test this with YoWASP but I may need a day or two to do it.
whitequark commentedon May 1, 2026
Preliminary testing with Yosys suggests catch() is broken but I haven't reduced yet. Builds and throws fine.
whitequark commentedon May 1, 2026
Are you aware that LTO plus exceptions builds are currently broken? I couldn't find an issue.
CMake error
whitequark commentedon May 1, 2026
@alexcrichton I built nextpnr with exception support and it does seem to work in my testing with wasmtime CLI,
however I'm hitting a problem with the wasmtime-py API where the application crashes (with an exception that propagates to the top frame) and I'm not sure why. The CLI has version 44.0.0 while wasmtime-py has version 44.0.1, could that be the cause? If not, how would one start debugging this? I would be happy already just knowing the Wasm file offset of the original throw instruction.Nevermind, the error was caused by a preopen that didn't work due to a subtle environment mismatch. Nextpnr fully works with exception handling enabled, both throwing and catching.
alexcrichton commentedon May 1, 2026
Thanks! I'll poke at #629 and see if I can't see what's going on.
Thanks for testing!
flumf commentedon May 1, 2026
Yosys also works as expected with exceptions enabled (throwing and catching), though LTO needs to be disabled.
vapier commentedon May 2, 2026
I was seeing
undefined symbolerrors with__cxa_allocate_exception&__cxa_throwwith wasi-sdk-32, but that's fixed in wask-sdk-33. I'll try and test the actual program later this weekend.whitequark commentedon May 2, 2026
@flumf But you had to disable LTO as well, right? Or it wouldn't catch properly?
flumf commentedon May 2, 2026
@whitequark That is correct, yes.
[WebAssembly] Clang support for exception-based lookup paths (#185775)