Skip to content

wasm: try/catch and finally, through wasm exception handling - #816

Merged
ASDAlexander77 merged 2 commits into
mainfrom
wasm-eh
Nov 12, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
wasm-eh

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

try/catch and finally now work on wasm32 and wasm64 (not Emscripten), using wasm exception handling. Before this, every throw ended the program, and the test runner skipped every test that catches.

Compiler

  • Codegen (obj.cpp): wasm triples get the exception-handling feature, -wasm-enable-eh, and ExceptionModel = Wasm. The exception model must be set before the target machine is created, because its MCAsmInfo reads it from there. With only the flag, the code generator still lowered every invoke to a plain call.
  • WasmExceptionPass (new, after LandingPadFixPass): tslang keeps emitting the Itanium form it uses on Linux. The pass turns each landing pad into a catch-all funclet that it leaves straight away:
    catchswitch → catchpad [ptr null] → llvm.wasm.get.exception → catchret to the old pad block.
    The handler code after the pad then runs as ordinary code, as it does on Linux. A catch-all pad needs no personality routine, no LSDA and no C++ runtime.
  • What the personality would decide, the pass now decides after the catch:
    • A pad's selector is the first clause that takes the exception. __tslang_wasm_catches (internal, generated) matches the same type, or a class pointer against a pointer to one of its bases, through __si_class_type_info. llvm.eh.typeid.for becomes the type_info's address.
    • A pad with only typed clauses rethrows an exception none of them takes.
    • When a pointer clause matches (a string, a class), the catch binds the thrown pointer, as __cxa_begin_catch does in libc++abi.
    • resume becomes llvm.wasm.throw.
  • The C++ runtime's type_infos for fundamental types and the type_info vtables get weak definitions. Before, as undefined data symbols under --allow-undefined, all of them sat at address 0, so every type looked the same. Class type_infos are already emitted as definitions in each module that uses them.

Host (docs/how/wasm/test)

  • The module imports the __cpp_exception tag. host.instantiate(bytes) creates the tag, reading from the module's memory section whether its parameter is i32 or i64. run.mjs and index.html now instantiate through it.
  • __cxa_throw throws a WebAssembly.Exception of that tag. Begin catch, end catch and rethrow keep handler counts the way libc++abi does. A JS exception (exit(), an assert) is not of this tag, so no catch sees it.
  • Two host gaps the newly running default-library tests found are fixed:
    • Node's ERR_FS_EISDIR from rmSync is now reported as EISDIR.
    • An environment variable name containing = is now refused with EINVAL.
  • run_tests.mjs no longer skips tests that catch. json.ts is skipped under none only: it builds a 100,000-character string one character at a time, which under -mm=none is about 5 GB of unfreed strings.

Default library

No source change, but the wasm archives must be rebuilt with this compiler (scripts\build_wasm.bat) for exceptions to be caught inside default-library code. I rebuilt them locally for these runs. The release workflow builds them with the compiler it releases.

Tests

  • Native ctest: 6976/6976.
  • wasm suites, release and debug, 0 failures, on wasm32 and wasm64:
Suite none / rc own
tester 637 pass (was 577) 472 pass (was 421)
defaultlib none 194, rc 195 (was 181) 194 pass (was 180)
  • index.html runs a wasm32 and a wasm64 catching test in Chromium (the app's browser pane).

🤖 Generated with Claude Code

ASDAlexander77 and others added 2 commits November 12, 2026 00:12
wasm32/wasm64 (not Emscripten) compile with the exception-handling feature and the wasm exception
model, set before the target machine is made (its MCAsmInfo takes it from there; without it every
invoke was lowered to a call).

WasmExceptionPass turns each Itanium landing pad into a catch-all catchpad left at once
(catchswitch, catchpad [ptr null], llvm.wasm.get.exception, catchret), so the code after the pad
runs as ordinary code, as it does on Linux; `resume` becomes llvm.wasm.throw. What the personality
would decide is decided after the catch: a pad's selector is the first clause that takes the
exception (__tslang_wasm_catches: the same type, or a base class's pointer), a pad of typed clauses
only rethrows what none takes, and a matched pointer clause binds the thrown pointer. The C++
runtime's type_infos and type_info vtables get definitions, so each has an address of its own.

The JS host imports the __cpp_exception tag (i32 or i64, read from the module by
host.instantiate), throws a WebAssembly.Exception of it from __cxa_throw, and keeps begin/end catch
and rethrow as libc++abi does. It also maps Node's ERR_FS_* codes and refuses an environment name
with '='.

run_tests.mjs no longer skips tests that catch; json.ts under none is listed (5 GB of strings).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 45a42fc into main Nov 12, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the wasm-eh branch November 12, 2026 00:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant