Skip to content

Add Support for WASM Exceptions #334

Closed

Description

@woodsmc

There are plans to add support for exceptions in WAMR. Are there any plans to add support to the WASI-SDK tool chain to support Native WASM Exceptions?

(edit: fix link)

Activity

  1. sunfishcode commented on Jul 6, 2023

    @sunfishcode
    Member

    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.

  2. woodsmc commented on Jul 7, 2023

    @woodsmc
    Author

    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.

  3. sbc100 commented on Jul 7, 2023

    @sbc100
    Member

    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

  4. woodsmc commented on Jul 7, 2023

    @woodsmc
    Author

    Whooha... Rockstar's both of you, and I've got some reading to do. Thanks for the pointers. Let me take a look.

  5. abrown commented on Jul 25, 2023

    @abrown
    Collaborator

    There's also a draft PR with initial support for this in #198, which you may be interested in.

  6. aheejin commented on Aug 3, 2023

    @aheejin
    Member

    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.

  7. aheejin commented on Sep 22, 2023

    @aheejin
    Member

    Thank you for the wait. Wasm EH's libcxxabi/libunwind changes have now been upstreamed:
    llvm/llvm-project@e6cbba7
    llvm/llvm-project@058222b

  8. sroussey commented on Jan 6, 2024

    @sroussey

    What does -fwasm-exceptions do? Adding or removing that makes no difference, I still end up with

    wasm-ld: error: /var/folders/74/djsf8lv965l2tkgr29ybpz4h0000gn/T/lib-858918.o: undefined symbol: __cxa_allocate_exception
    wasm-ld: error: /var/folders/74/djsf8lv965l2tkgr29ybpz4h0000gn/T/lib-858918.o: undefined symbol: __cxa_free_exception
    wasm-ld: error: /var/folders/74/djsf8lv965l2tkgr29ybpz4h0000gn/T/lib-858918.o: undefined symbol: __cxa_throw
    

    thx

  9. anuraaga commented on Jan 22, 2024

    @anuraaga
    Contributor

    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?

  10. whitequark commented on Jan 22, 2024

    @whitequark
    Contributor

    I think you should be able to lower SjLj to exceptions.

  11. sbc100 commented on Jan 22, 2024

    @sbc100
    Member

    Indeed, llvm already uses wasm exception to implement SJLJ, when wasm exceptions are enabled, so it should just work once we enable exceptions.

  12. sunfishcode commented on Jan 22, 2024

    @sunfishcode
    Member

    Indeed, wasm exceptions is how we've always envisioned setjmp/longjmp would work.

  13. anuraaga commented on Jan 23, 2024

    @anuraaga
    Contributor

    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)

  14. cpetig commented on Jan 23, 2024

    @cpetig

    What does -fwasm-exceptions do? Adding or removing that makes no difference, I still end up with

    wasm-ld: error: /var/folders/74/djsf8lv965l2tkgr29ybpz4h0000gn/T/lib-858918.o: undefined symbol: __cxa_allocate_exception
    wasm-ld: error: /var/folders/74/djsf8lv965l2tkgr29ybpz4h0000gn/T/lib-858918.o: undefined symbol: __cxa_free_exception
    wasm-ld: error: /var/folders/74/djsf8lv965l2tkgr29ybpz4h0000gn/T/lib-858918.o: undefined symbol: __cxa_throw
    

    thx

    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.

  15. woodsmc commented on Jan 25, 2024

    @woodsmc
    Author

    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.

  16. 4 remaining items

  17. yamt commented on Jan 31, 2024

    @yamt
    Contributor

    Indeed, llvm already uses wasm exception to implement SJLJ, when wasm exceptions are enabled, so it should just work once we enable exceptions.

    do you mean to use saveSetjmp getTempRet0 etc for WASI as well?

    Right, and this seems to have been moved to compiler-rt in Emscripten: https://github.com/emscripten-core/emscripten/blob/main/system/lib/compiler-rt/emscripten_setjmp.c

    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:

    compiler-rt: https://github.com/emscripten-core/emscripten/blob/main/system/lib/compiler-rt/emscripten_setjmp.c (created)

    libc++abi: https://github.com/llvm/llvm-project/blob/main/libcxxabi/src/cxa_personality.cpp (modified) https://github.com/llvm/llvm-project/blob/main/libcxxabi/src/cxa_exception.cpp (modified) https://github.com/llvm/llvm-project/blob/main/libcxxabi/src/cxa_exception.h (modified)

    libunwind: https://github.com/emscripten-core/emscripten/blob/main/system/lib/libunwind/src/Unwind-wasm.c (created)

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

  18. yamt commented on Jan 31, 2024

    @yamt
    Contributor
  19. alepvn33 commented on Jun 27, 2025

    @alepvn33

    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:

    #include <iostream>
    #include <stdexcept>
    
    int may_throw(bool should_throw) {
        if (should_throw) {
            throw std::runtime_error("Oops! Something went wrong.");
        }
        return 42;
    }
    
    int main() {
        try {
            std::cout << "Trying operation that does not throw..." << std::endl;
            int result = may_throw(false);
            std::cout << "Result: " << result << std::endl;
    
            std::cout << "Trying operation that will throw..." << std::endl;
            result = may_throw(true);
            std::cout << "Result: " << result << std::endl;
    
        } catch (const std::exception& e) {
            std::cerr << "Caught exception: " << e.what() << std::endl;
        }
    
        std::cout << "Program continues after exception." << std::endl;
    
        return 0;
    }
    

    with this command

    /opt/wasi-sdk/bin/clang++ --target=wasm32-wasi -O2 -fwasm-exceptions -o test_exceptions.wasm test_exceptions.cpp

    but I still get these errors

    
    wasm-ld: error: /tmp/test_exceptions-72798d.o: undefined symbol: __cxa_allocate_exception
    wasm-ld: error: /tmp/test_exceptions-72798d.o: undefined symbol: __cxa_free_exception
    wasm-ld: error: /tmp/test_exceptions-72798d.o: undefined symbol: __cxa_throw
    wasm-ld: error: /tmp/test_exceptions-72798d.o: undefined symbol: __wasm_lpad_context
    wasm-ld: error: /tmp/test_exceptions-72798d.o: undefined symbol: __wasm_lpad_context
    wasm-ld: error: /tmp/test_exceptions-72798d.o: undefined symbol: _Unwind_CallPersonality
    wasm-ld: error: /tmp/test_exceptions-72798d.o: undefined symbol: __wasm_lpad_context
    wasm-ld: error: /tmp/test_exceptions-72798d.o: undefined symbol: __cxa_begin_catch
    wasm-ld: error: /tmp/test_exceptions-72798d.o: undefined symbol: __cxa_end_catch
    wasm-ld: error: /tmp/test_exceptions-72798d.o: undefined symbol: __cxa_end_catch
    wasm-ld: error: /tmp/test_exceptions-72798d.o: undefined symbol: __cxa_begin_catch
    wasm-ld: error: /tmp/test_exceptions-72798d.o: undefined symbol: __cxa_end_catch
    wasm-ld: error: /tmp/test_exceptions-72798d.o: undefined symbol: __cxa_end_catch
    wasm-ld: error: /tmp/test_exceptions-72798d.o: undefined symbol: __cxa_allocate_exception
    wasm-ld: error: /tmp/test_exceptions-72798d.o: undefined symbol: __cxa_free_exception
    wasm-ld: error: /tmp/test_exceptions-72798d.o: undefined symbol: __cxa_throw
    
  20. yamt commented on Jul 2, 2025

    @yamt
    Contributor

    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% 
  21. added a commit that references this issue on Mar 11, 2026
    b2947d1
  22. added a commit that references this issue on Mar 30, 2026
    2f7a86e
  23. whitequark commented on Apr 8, 2026

    @whitequark
    Contributor

    Thank you!

  24. added a commit that references this issue on Apr 13, 2026
    003cf14
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