Skip to content

[Emscripten-EH] Implement __cxa_throw in terms of _Unwind_RaiseException - #27498

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:libunwind_js
Aug 8, 2026
Merged

[Emscripten-EH] Implement __cxa_throw in terms of _Unwind_RaiseException#27498
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:libunwind_js

Conversation

@sbc100

@sbc100 sbc100 commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

It turns out the libunwind symbols are supposed to always be available, enen non-C++ programs. This means that __cxa_throw should be implemented in terms of _Unwind_RaiseException and not the other way around.

  • Moves the libunwind code into a new libunwind.js file (only use for Emscripten EH).
  • Add support for _Unwind_Resume
  • Implement __cxa_throw and other exception-handling APIs in terms of the lower level _Unwind API.

Inspired by #27496

@sbc100
sbc100 requested review from aheejin and kripken August 7, 2026 22:12
@sbc100
sbc100 requested a review from guybedford August 7, 2026 22:16
@sbc100 sbc100 changed the title [Emscripten-EH] Move JS unwinding implementation to src/lib/libunwind.js [Emscripten-EH] Move JS unwinding implementation to new libunwind.js Aug 7, 2026
Comment thread src/lib/libunwind.js Outdated
@sbc100
sbc100 force-pushed the libunwind_js branch 2 times, most recently from 5f0e9ae to 637ec5b Compare August 7, 2026 22:45
Comment thread src/lib/libexceptions.js Outdated
@sbc100
sbc100 force-pushed the libunwind_js branch 3 times, most recently from 6dcdb9f to 4f4cd6d Compare August 7, 2026 22:58
@sbc100

sbc100 commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator Author

I could potentially split this change up, land an NFC that just moves the old unwind code into its own file? Would that be useful?

@sbc100 sbc100 changed the title [Emscripten-EH] Move JS unwinding implementation to new libunwind.js [Emscripten-EH] Implement __cxa_throw in terms of _Unwind_RaiseException Aug 7, 2026
Extract `_Unwind_*` functions and `uncaughtExceptionCount` /
`exceptionLast` JS state from libcore.js and libexceptions.js into a
dedicated `libunwind.js`.

This new libunwind.js is included as long as wasm EH is not being
used.

Also, invert the dependency between `__cxa_throw` and
`_Unwind_RaiseException` so that `__cxa_throw` now calls
`_Unwind_RaiseException` rather than the other way around.  This is
important as it allows C programs (or rather non-C++ programs like Rust) to
call `_Unwind_RaiseException` without linking as C++.

Inspired by emscripten-core#27496
@sbc100

sbc100 commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator Author

The reason we see a light code size saving here is because __cxa_throw previously had an unconditional dependency on uncaughtExceptionCount even in DISABLE_EXCEPTION_CATCHING mode (when it technically didn't need it).

@sbc100

sbc100 commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator Author

Funny how I can work on compilers for decades and not be aware of how/when libunwind function are linked / used...

@sbc100
sbc100 enabled auto-merge (squash) August 8, 2026 00:48
@sbc100
sbc100 merged commit 2f3d9cc into emscripten-core:main Aug 8, 2026
41 checks passed
@sbc100
sbc100 deleted the libunwind_js branch August 8, 2026 01:32
Comment thread src/lib/libexceptions.js
Comment on lines -9 to -12
$uncaughtExceptionCount: '0',
#if !DISABLE_EXCEPTION_CATCHING
$exceptionLast: null,
#endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In libcxxabi native code (and thus in Wasm EH), managing information like uncaught exception count and last exception is done by libcxxabi, not libunwind. It is true in libcxxabi/libunwind that __cxa_throw calls _Unwind_RaiseException, but all exception status managing is done within libcxxabi. Wouldn't it be consistent to manage it within libexceptions.js and leave libunwind simple?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, we can make it consistent in that way. I'll make a followup PR.

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.

3 participants