Skip to content

fix(libc): bridge guest and host exceptions on macOS - #23395

Open
simonbronk wants to merge 2 commits into
boykopovar:mainfrom
simonbronk:fix/macos-libcxx-typed-catch
Open

simonbronk wants to merge 2 commits into
boykopovar:mainfrom
simonbronk:fix/macos-libcxx-typed-catch

Conversation

@simonbronk

@simonbronk simonbronk commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What

Part of #21538: cause 2 (a macOS host catch never matches a guest exception's type) and cause 3 (an exception thrown from host code never enters the guest runtime). One commit per cause, as asked in the issue.

Commit 1, cause 2:

  • RTTI names: a host catch on macOS names libc++ types, whose mangled names carry the inline namespace __1 (NSt3__112system_errorE), while guest types use St12system_error. Equal now also matches a libc++ std name with that namespace dropped. Nested names are matched too (NSt3__18ios_base7failureE and NSt8ios_base7failureE), template names are not. This gives the name parity Linux already has with libstdc++.
  • _Unwind_Resume: once the handler is selected, a cleanup between the throw and the catch called libSystem's _Unwind_Resume, because two-level binding skips libc. One example is the cleanup of LibcException::ThrowCppError in libc itself. libunwind then passed the guest exception to libc++abi's personality, which ran the host handler as a cleanup and ended in an abort. On macOS, libc now exports _Unwind_Resume as an ld64 alias of _Unwind_Resume_nid_postfix, as GCC_GLOBAL_ALIAS does on Linux. Cleanups in libc and in images linked against it now resume through the guest unwinder. _Unwind_Resume_nid_postfix forwards other exceptions to libSystem's _Unwind_Resume, which it looks up in libSystem, mirroring the Linux RTLD_NEXT path.
  • Test (macOS only): guest_host_typed_catch throws _Throw_Cpp_error through a host frame with a destructor. It checks that the destructor runs and that catch (const std::system_error&) and catch (const std::runtime_error&) catch the exception, while catch (const std::logic_error&) does not. It is registered only on macOS, because I have not run it on Linux or Windows.

Commit 2, cause 3: on macOS the host keeps libc++abi, so NotImplemented_nid_no_patch and any other plain throw in host code goes through libc++abi. When nothing caught it, libc++abi's terminate skipped the guest's std::set_terminate handler and the run report, and the guest's current-exception queries returned null. Routing those throws through __cxa_throw_nid_postfix was tried and regressed about 20 tests (I described this in the issue), because host catch (...) { throw; } then splits one exception across two runtimes. This commit keeps host throws on libc++abi and connects the two runtimes in three places, all macOS only:

  • Terminate: libc installs one host terminate handler at load, and it runs LibcException::Terminate, which calls the guest handler and writes the run report. _ZSt13set_terminatePFvvE_nid_postfix no longer copies each guest handler into the host.
  • Queries: while no guest exception is caught, __cxa_current_exception_type_nid_postfix, __cxa_current_primary_exception_nid_postfix and the terminate message read libc++abi's caught exception through its public abi:: functions. No private __cxa_exception layout is read.
  • exception_ptr: a libc++abi primary held in a guest exception_ptr is recognised by the libc++abi exception class (CLNGC++\0) in the _Unwind_Exception that the Itanium ABI places right before the object. A static_assert checks that the guest Header also ends with it. Its reference count, rethrow and type go to libc++abi; the type is read by rethrowing it. libc links c++abi on macOS, because libc++ does not re-export __cxa_rethrow_primary_exception.
  • Test (macOS only): guest_host_current_exception catches a host std::runtime_error, reads its type and primary through the guest queries, holds it in a guest exception_ptr, rethrows it through std::rethrow_exception and releases it.
  • DefaultTerminate is the only edit outside #ifdef __APPLE__. On Linux and Windows its output is unchanged, except that a caught native exception with a null type now prints "without an active exception".

guest_libc_internal_exception_virtuals still fails, but later than before. Its nested system_error catch begins a second foreign exception in libc++abi's __cxa_begin_catch, which holds one at a time and terminates. The libc debt fragment records this, along with the remaining gaps.

Tested

Apple M4, macOS 27.2.1, Apple Clang 21, full x86_64 build under Rosetta 2 with BUILD_TESTING=ON. #17425 and #11049 were applied locally only so the full macOS build links; they are not part of this PR.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

On macOS a host catch clause names libc++ types, whose mangled names
carry the inline namespace __1, so a guest std::system_error never
matched catch (const std::system_error&). Match the libc++ name with
the inline namespace dropped against the guest name.

Once the handler is selected, cleanups between the throw and the catch
called libSystem's _Unwind_Resume, which handed the guest exception to
libunwind and libc++abi and ended in a terminate. Export _Unwind_Resume
from libc on macOS, as GCC_GLOBAL_ALIAS does on Linux, so libc's own
cleanups and those of images linked against it resume through the guest
unwinder; other exceptions are forwarded to libSystem.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 19434 __cxa_decrement_exception_refcount

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 7275 core/libs/CMakeLists.txt
PR 7715 core/libs/prx/libc/CMakeLists.txt
PR 8965 core/libs/prx/libc/CMakeLists.txt
PR 9302 core/libs/CMakeLists.txt
PR 9584 core/libs/prx/libc/CMakeLists.txt
PR 10113 core/libs/prx/libc/CMakeLists.txt
PR 10226 core/libs/prx/libc/CMakeLists.txt
PR 12130 core/libs/CMakeLists.txt
PR 12682 core/libs/prx/libc/CMakeLists.txt
PR 12688 core/libs/prx/libc/CMakeLists.txt
PR 12700 core/libs/prx/libc/CMakeLists.txt
PR 12717 core/libs/prx/libc/CMakeLists.txt
PR 12849 core/libs/prx/libc/CMakeLists.txt
PR 12965 core/libs/prx/libc/CMakeLists.txt
PR 12994 core/libs/CMakeLists.txt
PR 15838 core/libs/CMakeLists.txt
PR 16018 core/libs/CMakeLists.txt
PR 16052 core/libs/CMakeLists.txt
PR 16214 core/libs/prx/libc/CMakeLists.txt
PR 16516 core/libs/prx/libc/CMakeLists.txt
PR 16560 core/libs/prx/libc/CMakeLists.txt
PR 16793 core/libs/CMakeLists.txt
PR 16919 core/libs/prx/libc/CMakeLists.txt
PR 16924 core/libs/prx/libc/CMakeLists.txt
PR 16944 core/libs/prx/libc/CMakeLists.txt
PR 16949 core/libs/prx/libc/CMakeLists.txt
PR 16975 core/libs/prx/libc/CMakeLists.txt
PR 17261 core/libs/prx/libc/CMakeLists.txt
PR 17277 core/libs/prx/libc/CMakeLists.txt
PR 17296 core/libs/prx/libc/CMakeLists.txt
PR 17331 core/libs/prx/libc/CMakeLists.txt
PR 17539 core/libs/prx/libc/CMakeLists.txt
PR 17555 core/libs/prx/libc/CMakeLists.txt
PR 17556 core/libs/prx/libc/CMakeLists.txt
PR 17557 core/libs/prx/libc/CMakeLists.txt
PR 17558 core/libs/prx/libc/CMakeLists.txt
PR 17568 core/libs/prx/libc/CMakeLists.txt
PR 17578 core/libs/prx/libc/CMakeLists.txt
PR 17607 core/libs/prx/libc/CMakeLists.txt
PR 17610 core/libs/prx/libc/CMakeLists.txt
PR 17936 core/libs/CMakeLists.txt
PR 18300 core/libs/CMakeLists.txt
PR 18693 core/libs/prx/libc/CMakeLists.txt
PR 19181 core/libs/prx/libc/CMakeLists.txt
PR 19434 core/libs/CMakeLists.txt
PR 21236 core/libs/prx/libc/CMakeLists.txt
PR 21464 core/libs/CMakeLists.txt
PR 22091 core/libs/prx/libc/src/exception/Runtime.cpp
core/libs/prx/libc/src/exception/Unwind.cpp
PR 23921 core/libs/prx/libc/src/exception/ExceptionPointer.cpp
PR 23975 core/libs/CMakeLists.txt
PR 24015 core/libs/CMakeLists.txt
PR 24217 core/libs/prx/libc/CMakeLists.txt
PR 24731 core/libs/CMakeLists.txt
PR 25289 core/libs/prx/libc/CMakeLists.txt
PR 25454 core/libs/CMakeLists.txt
PR 25470 core/libs/CMakeLists.txt
PR 25477 core/libs/CMakeLists.txt
PR 25493 core/libs/CMakeLists.txt
PR 25500 core/libs/CMakeLists.txt
PR 25674 core/libs/prx/libc/CMakeLists.txt
PR 25708 core/libs/prx/libc/CMakeLists.txt
PR 25744 core/libs/prx/libc/CMakeLists.txt
PR 25750 core/libs/prx/libc/CMakeLists.txt
PR 25759 core/libs/prx/libc/CMakeLists.txt
PR 25763 core/libs/prx/libc/CMakeLists.txt
PR 26796 core/libs/prx/libc/CMakeLists.txt
PR 26805 core/libs/CMakeLists.txt
PR 27164 core/libs/prx/libc/CMakeLists.txt
PR 27540 core/libs/prx/libc/CMakeLists.txt
PR 27547 core/libs/CMakeLists.txt
PR 27566 core/libs/prx/libc/CMakeLists.txt
PR 27568 core/libs/prx/libc/CMakeLists.txt
PR 27569 core/libs/prx/libc/CMakeLists.txt
PR 27578 core/libs/prx/libc/CMakeLists.txt
PR 27581 core/libs/prx/libc/CMakeLists.txt
PR 27646 core/libs/prx/libc/CMakeLists.txt
PR 27750 core/libs/CMakeLists.txt
PR 27815 core/libs/prx/libc/CMakeLists.txt
PR 27823 core/libs/prx/libc/CMakeLists.txt
PR 28043 core/libs/CMakeLists.txt
PR 28051 core/libs/prx/libc/CMakeLists.txt
PR 28071 core/libs/prx/libc/CMakeLists.txt
PR 28394 core/libs/prx/libc/CMakeLists.txt
PR 28655 core/libs/prx/libc/CMakeLists.txt
PR 28709 core/libs/prx/libc/CMakeLists.txt
PR 28710 core/libs/prx/libc/CMakeLists.txt
PR 28742 core/libs/prx/libc/CMakeLists.txt
PR 28758 core/libs/prx/libc/CMakeLists.txt
PR 28808 core/libs/prx/libc/CMakeLists.txt
PR 28839 core/libs/prx/libc/CMakeLists.txt
PR 29099 core/libs/prx/libc/CMakeLists.txt
PR 29100 core/libs/prx/libc/CMakeLists.txt
PR 29111 core/libs/prx/libc/CMakeLists.txt
PR 29159 core/libs/CMakeLists.txt
PR 29213 core/libs/CMakeLists.txt
PR 29253 core/libs/prx/libc/CMakeLists.txt
PR 29268 core/libs/prx/libc/CMakeLists.txt
PR 29269 core/libs/prx/libc/CMakeLists.txt
PR 29270 core/libs/prx/libc/CMakeLists.txt
PR 29271 core/libs/prx/libc/CMakeLists.txt
PR 29272 core/libs/prx/libc/CMakeLists.txt
PR 29273 core/libs/prx/libc/CMakeLists.txt

@Romannachwievor

Copy link
Copy Markdown
Contributor

Tested on an Apple M1 (macOS 26.5, Apple clang 21.0.0), x86_64 Release under Rosetta 2. The tree was main 245210b07e with #17425 and #11049, which the macOS build needs. I applied this PR's diff (head 0b4e69eb68) on top and rebuilt libc and guest_host_typed_catch_tests.

guest_host_typed_catch (new)
without this PR test does not exist
with this PR pass, 3 of 3 runs

The test is built into build-full/tests/guest_host_typed_catch_tests after applying the diff, and its Require checks abort on failure, so a pass means the typed catches matched. I didn't run it without the libc change, and I didn't run a title that throws across the guest/host boundary.

AI-assisted: yes (Claude Code)

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed — no blocking findings

The inspected changes are coherent, but I could not run the build or tests in this review environment, so a formal approval is not justified.

What I ran

Reviewed head 0b4e69eb68abf0b3d537fd17b27c68410be1fee8 using the prepared review bundle and pinned repository reader. No commands or tests were run.

Results

Check Result Note
Changed-file review passed All six changed files read
Build and tests not run Reader-only review environment
Author-reported checks not run See PR description; not independently verified

Findings

Blocking

None.

Non-blocking

None.

Not verified

Build, test execution, and the reported macOS behavior were not independently verified. The author reports an Apple M4 / macOS 27.2.1 build and test run in the PR description.

To get this to a good state

Nothing is required. Optional: run the reported macOS checks on this exact head.

Files read: core/libs/CMakeLists.txt, core/libs/prx/libc/CMakeLists.txt, core/libs/prx/libc/src/exception/Runtime.cpp, core/libs/prx/libc/src/exception/Unwind.cpp, core/libs/tests/GuestHostTypedCatch.cpp, and docs/dev/debt/changes/fix-macos-libcxx-typed-catch.md. All six files listed as changed in manifest.json were reviewed; no changed files were excluded.

On macOS the host keeps libc++abi, so an exception that host code throws
with a plain throw, NotImplemented_nid_no_patch included, never enters
libc's runtime. When nothing caught it, libc++abi's terminate skipped the
guest's std::set_terminate handler and the run report, and the guest's
current-exception queries saw nothing.

Install one host terminate handler at load that runs the guest terminate
path, instead of copying each guest handler into the host. While no guest
exception is caught, the guest's __cxa_current_exception_type,
__cxa_current_primary_exception and the terminate message read
libc++abi's caught exception through its public ABI. A libc++abi primary
held in a guest exception_ptr is recognised by its exception class, and
its reference count, type and rethrow go to libc++abi.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@simonbronk simonbronk changed the title fix(libc): catch guest exceptions by libc++ type on macOS fix(libc): bridge guest and host exceptions on macOS Nov 19, 2026

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed — comments

The review covers all eight changed files at head b1b16e7f84774d4673e5462df3e8b2b7ee02129f; tests were not run in this review.

What I ran

Read the prepared description, commit list, diff, manifest, and the full text of all changed files listed as reviewed. Repository context was read through repo_read.py tools at the pinned head. No build or tests were run.

Results

Check Result Note
Changed-file review passed All eight reviewed files read
Build and tests not run Author-reported results only

Findings

Blocking

None.

Non-blocking

  • HostPrimary reads the unwind exception class by subtracting one _Unwind_Exception from the supplied object (core/libs/prx/libc/src/exception/Runtime.cpp:75). This assumes the ABI object is immediately preceded by that header; the assertion at line 73 checks the guest header layout, not the host libc++abi layout. Consider checking this layout assumption against the supported libc++abi ABI or documenting its source.
  • hostTerminateRouted installs the host terminate handler during static initialization (core/libs/prx/libc/src/exception/Runtime.cpp:145). A later host call to std::set_terminate replaces it and bypasses the guest terminate path, as the change’s debt note itself describes. Consider documenting this limitation in the PR description or adding a test for the expected behavior.

Not verified

No commands, build, tests, or runtime behavior were verified in this review; test results in the PR description are author-reported. The ruling’s validation script was not run. No changed files were excluded; all eight manifest entries marked for review were read.

To get this to a good state

Nothing is required. The points above are optional.

@Romannachwievor

Copy link
Copy Markdown
Contributor

Retested commit 2 (head b1b16e7f84) on a second Mac: Apple M1, macOS 26.5, Apple clang 21.0.0, x86_64 Release under Rosetta 2. Base is main 4fa81a86fb with #17425 and #11049 (needed for the macOS build) and the #23659 mincore fix. The PR head merged onto it without conflicts, and I rebuilt the whole tree.

Same test group as in the Tested section (ctest -j8 -R 'exception|unwind|throw|terminate|guest_libc_internal|run_report|app_archive|typed_catch' -E '^agc_'):

base this PR
tests 243 245
run_report fail pass
guest_kernel_app_archive_mmap fail pass
guest_throw_bad_array_new_length abort abort
guest_libc_internal_exception_virtuals abort abort
other failures none none

run_report, guest_kernel_app_archive_mmap, guest_host_current_exception and guest_host_typed_catch also passed 3 of 3 runs on their own. This matches your M4 results. I did not run the full suite or any title.

AI-assisted: yes (Claude Code)

@ycbuzi

ycbuzi commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Tested b1b16e7f84 merged into main, with the whole ctest suite on each machine below. Nothing fails that does not also fail on main on the same machine.

  • Windows Server 2022 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 99% tests passed, 3 tests failed out of 6427; 99% tests passed, 2 tests failed out of 6427. 3 of the failing tests fail on main too. run
  • Windows Server 2025 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 100% tests passed, 0 tests failed out of 6427; 100% tests passed, 0 tests failed out of 6427. run

AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports.

@Jazeupoi Jazeupoi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ran it on an M4 with macOS 27.0.1, which is not in the runs above (M1 / macOS 26.5, and the author's M4 / macOS 27.2.1). Apple clang (Command Line Tools), x86_64 Release build under Rosetta 2. Head b1b16e7f84 applied onto main 0a025bd3c6 with #17425 and #11049 (needed for the macOS build); clean apply. Each test was run 3 times on its own, on both trees.

Test main this PR
guest_kernel_app_archive_mmap fails 3 of 3 passes 3 of 3
run_report fails 3 of 3 passes 3 of 3
guest_libc_internal_exception_virtuals fails 3 of 3 fails 3 of 3
guest_throw_bad_array_new_length fails 3 of 3 fails 3 of 3

This matches the description: the two that pass are the ones listed for commit 2, and guest_libc_internal_exception_virtuals is noted as still failing. I did not run guest_host_typed_catch or guest_host_current_exception; they are new in this PR and need a CMake reconfigure that I did not do.

AI-assisted: yes (Claude Code)

@simonbronk

Copy link
Copy Markdown
Contributor Author

Thanks for the review. On the two non-blocking points:

  1. HostPrimary layout (Runtime.cpp:75). libc++abi puts _Unwind_Exception unwindHeader as the last member of __cxa_exception and __cxa_dependent_exception, right before the thrown object. Its own cxa_exception_from_exception_unwind_exception relies on this, since it returns cxa_exception_from_thrown_object(unwind_exception + 1) (libcxxabi/src/cxa_exception.h). The Itanium C++ ABI fixes the same layout. The static_assert on line 73 checks the matching guest-side layout: Header::unwind also ends right at the object. So for either kind of primary in an exception_ptr, the word read is an exception class, and only libc++abi's primary class CLNGC++\0 matches HostClass. libc++abi does not ship __cxa_exception in a public header, so the host side can't be checked with a static_assert.
  2. A later host std::set_terminate. This limit is recorded in the debt fragment (docs/dev/debt/changes/fix-macos-libcxx-typed-catch.md, "Exception runtime on macOS"): a host program that calls std::set_terminate after libc loads bypasses both the guest handler and the run report. A test for it would only pin the bypass, so I left it as a documented gap.

About the red windows check: the one failure is guest_thread_cpu_clock (Exit code 0xc0000409, 1 of 5325). That test covers the Windows thread CPU clock (#23238, #23756), and every libc change in this PR is inside #ifdef __APPLE__. ycbuzi's full ctest run of this head on Windows Server 2022 and 2025 found no failure that main doesn't also have. A maintainer re-run of the job should clear it.

AI-assisted: yes (Claude Code)

@Romannachwievor

Copy link
Copy Markdown
Contributor

I reproduced the new guest_libcxx_exception_pointer failure on Apple M1, macOS 26.5, x86_64 Release under Rosetta 2. The test builds on main 5b308313c3 plus #17425 and #11049. It fails 3/3 on main, 3/3 with this PR at b1b16e7f84, and 3/3 with this PR plus #22091 at df18f19b23. Each run prints libc++ exception_ptr did not rethrow its guest exception.

A temporary test-only probe changed catch (int value) to catch (int& value) and printed the caught value and addresses. The caught value is 0, while the guest exception object holds 37; the catch reference address differs from the guest object address. I reverted the probe and rebuilt the test. This narrows the failure to the value delivered to the host catch after the guest dependent rethrow. I have not identified a safe general fix yet.

AI-assisted: yes (Claude Code)

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The current fixtures cover typed catches and exception-pointer lifetime. The latest commit also installs a process-wide host terminate handler, but neither fixture invokes that route. Could a macOS subprocess test install a guest terminate handler and trigger host termination, then check a distinct child exit status or marker from the guest callback? This would exercise the new direction of the bridge without terminating the test runner.

Source review at b1b16e7f84. A trial merge onto 484e8e6b77 conflicts in libc's CMakeLists.txt. Native macOS execution is unavailable here.

AI-assisted: yes.

This branch has not been deployed

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

Labels

build docs libc needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants