Skip to content

Support for C++ exceptions #565

Closed

Description

@alexcrichton

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 -fexceptions wants 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-exceptions looks 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 -lunwind lies. 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)

Activity

  1. dicej commented on Oct 8, 2025

    @dicej
    Collaborator

    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.

  2. yamt commented on Oct 8, 2025

    @yamt
    Contributor

    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.

    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)

  3. whitequark commented on Oct 22, 2025

    @whitequark
    Contributor

    See also #334, which is less detailed than this issue but was filed earlier.

  4. yerzham commented on Nov 16, 2025

    @yerzham

    I applied changes to libunwind from https://gist.github.com/yamt/b699eb2604d2598810a2876ff2ffc8d8 and added libunwind to be included in wasi-sdk sysroot build. I made sure to use -fwasm-exceptions and -mllvm -wasm-use-legacy-eh=false where possible, but I kept having issues with __cpp_exception being 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_exception tag 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_exception tag emission and marking it weak (whatever weak means, idk) if it is targeting WASI instead of checking if isPositionIndependent. I suppose defining __cpp_exception can 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-exceptions or -lunwind during the sample C++ code compilation. Running it with wasmtime run -W exceptions=y worked. Without the exception feature, wasmtime correctly errored with exception 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)

  5. yerzham commented on Nov 16, 2025

    @yerzham

    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:

    1. The changes from [WebAssembly] Require tags for Wasm EH and Wasm SJLJ to be defined externally llvm/llvm-project#159143 and [WebAssembly] Define llvm-internal WasmEH tags in compiler-rt llvm/llvm-project#160959 already address the support for compiling code with C++ exceptions to Wasm, moving emscripten related code out in favor of standard exception handling. With this, compiler-rt needs to be built with -fwasm-exceptions too.
    2. Now this issue is tracking how to enable the C++ exception handling in wasi-sdk.
  6. whitequark commented on Nov 16, 2025

    @whitequark
    Contributor

    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.

  7. torokati44 commented on Nov 16, 2025

    @torokati44

    it makes sense to ship both exceptions-enabled and non-exceptions-enabled builds

    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"? 😛

  8. whitequark commented on Nov 16, 2025

    @whitequark
    Contributor

    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...)

  9. whitequark commented on Nov 16, 2025

    @whitequark
    Contributor

    Looks like the status as of Wasmtime 37 is:

    Wasmtime now fully implements the WebAssembly exception-handling proposal. Support is still disabled by default but is ready for testing. The proposal will be enabled by default in a future release of Wasmtime. bytecodealliance/wasmtime#11326

  10. torokati44 commented on Nov 16, 2025

    @torokati44

    runtimes in active widespread use that do not have exceptions

    Sounds like chicken and egg issue?

  11. whitequark commented on Nov 16, 2025

    @whitequark
    Contributor

    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.

  12. yerzham commented on Nov 16, 2025

    @yerzham

    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?

  13. whitequark commented on Nov 16, 2025

    @whitequark
    Contributor

    I can review the libunwind LLVM PR if you make one.

  14. alexcrichton commented on Nov 17, 2025

    @alexcrichton
    CollaboratorAuthor

    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:

    • Should this be a new compiler target?
    • Should this have a separate sysroot which is selected on the basis of compiler flags?
    • Should exceptions get enabled-by-default in wasi-sdk?

    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.

  15. sbc100 commented on Nov 17, 2025

    @sbc100
    Member
    • Should this be a new compiler target?
    • Should this have a separate sysroot which is selected on the basis of compiler flags?
    • Should exceptions get enabled-by-default in wasi-sdk?

    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).

  16. 22 remaining items

  17. alexcrichton commented on Apr 30, 2026

    @alexcrichton
    CollaboratorAuthor

    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

  18. vapier commented on Apr 30, 2026

    @vapier

    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.

  19. juntyr commented on Apr 30, 2026

    @juntyr

    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

  20. alexcrichton commented on Apr 30, 2026

    @alexcrichton
    CollaboratorAuthor

    @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.

  21. whitequark commented on Apr 30, 2026

    @whitequark
    Contributor

    I'd like to test this with YoWASP but I may need a day or two to do it.

  22. whitequark commented on May 1, 2026

    @whitequark
    Contributor

    Preliminary testing with Yosys suggests catch() is broken but I haven't reduced yet. Builds and throws fine.

  23. whitequark commented on May 1, 2026

    @whitequark
    Contributor

    Are you aware that LTO plus exceptions builds are currently broken? I couldn't find an issue.

    CMake error
    CMake Error at /usr/share/cmake-3.31/Modules/CMakeTestCCompiler.cmake:67 (message):
      The C compiler
    
        "/usr/bin/ccache"
    
      is not able to compile a simple test program.
    
      It fails with the following output:
    
        Change Dir: '/home/whitequark/Projects/yowasp/nextpnr/prjtrellis-build/CMakeFiles/CMakeScratch/TryCompile-K8OX7P'
        
        Run Build Command(s): /usr/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile cmTC_d7dc1/fast
        /usr/bin/gmake  -f CMakeFiles/cmTC_d7dc1.dir/build.make CMakeFiles/cmTC_d7dc1.dir/build
        gmake[1]: Entering directory '/home/whitequark/Projects/yowasp/nextpnr/prjtrellis-build/CMakeFiles/CMakeScratch/TryCompile-K8OX7P'
        Building C object CMakeFiles/cmTC_d7dc1.dir/testCCompiler.c.obj
        /usr/bin/ccache /home/whitequark/Projects/yowasp/nextpnr/wasi-sdk-33.0-x86_64-linux/bin/clang --target=wasm32-wasip1   --sysroot /home/whitequark/Projects/yowasp/nextpnr/wasi-sdk-33.0-x86_64-linux/share/wasi-sysroot -fwasm-exceptions -mllvm -wasm-use-legacy-eh=false -flto  -MD -MT CMakeFiles/cmTC_d7dc1.dir/testCCompiler.c.obj -MF CMakeFiles/cmTC_d7dc1.dir/testCCompiler.c.obj.d -o CMakeFiles/cmTC_d7dc1.dir/testCCompiler.c.obj -c /home/whitequark/Projects/yowasp/nextpnr/prjtrellis-build/CMakeFiles/CMakeScratch/TryCompile-K8OX7P/testCCompiler.c
        Linking C executable cmTC_d7dc1
        /usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_d7dc1.dir/link.txt --verbose=1
        clang: warning: argument unused during compilation: '-mllvm -wasm-use-legacy-eh=false' [-Wunused-command-line-argument]
        wasm-ld: error: symbol type mismatch: __cpp_exception
        >>> defined as WASM_SYMBOL_TYPE_FUNCTION in /home/whitequark/Projects/yowasp/nextpnr/wasi-sdk-33.0-x86_64-linux/share/wasi-sysroot/lib/wasm32-wasip1/eh/llvm-lto/22.1.0-wasi-sdk/libunwind.a(Unwind-wasm.c.o)
        >>> defined as WASM_SYMBOL_TYPE_TAG in cmTC_d7dc1.lto.o
        clang: error: linker command failed with exit code 1 (use -v to see invocation)
        /usr/bin/ccache /home/whitequark/Projects/yowasp/nextpnr/wasi-sdk-33.0-x86_64-linux/bin/clang --target=wasm32-wasip1 --sysroot /home/whitequark/Projects/yowasp/nextpnr/wasi-sdk-33.0-x86_64-linux/share/wasi-sysroot -fwasm-exceptions -mllvm -wasm-use-legacy-eh=false -flto  -fwasm-exceptions -lunwind -flto -Wl,--strip-all  CMakeFiles/cmTC_d7dc1.dir/testCCompiler.c.obj -o cmTC_d7dc1
        gmake[1]: *** [CMakeFiles/cmTC_d7dc1.dir/build.make:103: cmTC_d7dc1] Error 1
        gmake[1]: Leaving directory '/home/whitequark/Projects/yowasp/nextpnr/prjtrellis-build/CMakeFiles/CMakeScratch/TryCompile-K8OX7P'
        gmake: *** [Makefile:134: cmTC_d7dc1/fast] Error 2
    
  24. whitequark commented on May 1, 2026

    @whitequark
    Contributor

    @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.

  25. alexcrichton commented on May 1, 2026

    @alexcrichton
    CollaboratorAuthor

    Are you aware that LTO plus exceptions builds are currently broken? I couldn't find an issue.

    Thanks! I'll poke at #629 and see if I can't see what's going on.

    Nextpnr fully works with exception handling enabled, both throwing and catching.

    Thanks for testing!

  26. flumf commented on May 1, 2026

    @flumf

    Yosys also works as expected with exceptions enabled (throwing and catching), though LTO needs to be disabled.

  27. vapier commented on May 2, 2026

    @vapier

    I was seeing undefined symbol errors with __cxa_allocate_exception & __cxa_throw with wasi-sdk-32, but that's fixed in wask-sdk-33. I'll try and test the actual program later this weekend.

  28. whitequark commented on May 2, 2026

    @whitequark
    Contributor

    @flumf But you had to disable LTO as well, right? Or it wouldn't catch properly?

  29. flumf commented on May 2, 2026

    @flumf

    @whitequark That is correct, yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions