Repository navigation
fix(libc): bridge guest and host exceptions on macOS - #23395
simonbronk wants to merge 2 commits into
Conversation
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>
|
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.
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.
|
|
Tested on an Apple M1 (macOS 26.5, Apple clang 21.0.0), x86_64 Release under Rosetta 2. The tree was
The test is built into AI-assisted: yes (Claude Code) |
MarcZX10
left a comment
There was a problem hiding this comment.
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>
MarcZX10
left a comment
There was a problem hiding this comment.
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
HostPrimaryreads the unwind exception class by subtracting one_Unwind_Exceptionfrom 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.hostTerminateRoutedinstalls the host terminate handler during static initialization (core/libs/prx/libc/src/exception/Runtime.cpp:145). A later host call tostd::set_terminatereplaces 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.
|
Retested commit 2 (head Same test group as in the Tested section (
AI-assisted: yes (Claude Code) |
|
Tested
AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports. |
Jazeupoi
left a comment
There was a problem hiding this comment.
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)
|
Thanks for the review. On the two non-blocking points:
About the red AI-assisted: yes (Claude Code) |
|
I reproduced the new A temporary test-only probe changed AI-assisted: yes (Claude Code) |
ligmaSec
left a comment
There was a problem hiding this comment.
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.
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:
catchon macOS names libc++ types, whose mangled names carry the inline namespace__1(NSt3__112system_errorE), while guest types useSt12system_error.Equalnow also matches a libc++stdname with that namespace dropped. Nested names are matched too (NSt3__18ios_base7failureEandNSt8ios_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 ofLibcException::ThrowCppErrorin 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_Resumeas an ld64 alias of_Unwind_Resume_nid_postfix, asGCC_GLOBAL_ALIASdoes on Linux. Cleanups in libc and in images linked against it now resume through the guest unwinder._Unwind_Resume_nid_postfixforwards other exceptions to libSystem's_Unwind_Resume, which it looks up in libSystem, mirroring the LinuxRTLD_NEXTpath.guest_host_typed_catchthrows_Throw_Cpp_errorthrough a host frame with a destructor. It checks that the destructor runs and thatcatch (const std::system_error&)andcatch (const std::runtime_error&)catch the exception, whilecatch (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_patchand any other plainthrowin host code goes through libc++abi. When nothing caught it, libc++abi's terminate skipped the guest'sstd::set_terminatehandler and the run report, and the guest's current-exception queries returned null. Routing those throws through__cxa_throw_nid_postfixwas tried and regressed about 20 tests (I described this in the issue), because hostcatch (...) { 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:LibcException::Terminate, which calls the guest handler and writes the run report._ZSt13set_terminatePFvvE_nid_postfixno longer copies each guest handler into the host.__cxa_current_exception_type_nid_postfix,__cxa_current_primary_exception_nid_postfixand the terminate message read libc++abi's caught exception through its publicabi::functions. No private__cxa_exceptionlayout is read.exception_ptr: a libc++abi primary held in a guestexception_ptris recognised by the libc++abi exception class (CLNGC++\0) in the_Unwind_Exceptionthat the Itanium ABI places right before the object. Astatic_assertchecks that the guestHeaderalso ends with it. Its reference count, rethrow and type go to libc++abi; the type is read by rethrowing it. libc linksc++abion macOS, because libc++ does not re-export__cxa_rethrow_primary_exception.guest_host_current_exceptioncatches a hoststd::runtime_error, reads its type and primary through the guest queries, holds it in a guestexception_ptr, rethrows it throughstd::rethrow_exceptionand releases it.DefaultTerminateis 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_virtualsstill fails, but later than before. Its nestedsystem_errorcatch 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.guest_host_typed_catchaborts onmainand passes with this change, both with and without fix(libc): track guest exceptions caught by macOS host frames #22091 applied.ctest -R 'exception|unwind|throw|terminate|guest_libc_internal|run_report|app_archive|typed_catch', 244 tests): the 4 tests that fail onmainstill fail (run_report,guest_throw_bad_array_new_length,guest_libc_internal_exception_virtuals,guest_kernel_app_archive_mmap) and no other test fails. With fix(libc): track guest exceptions caught by macOS host frames #22091 also applied, 3 fail;guest_throw_bad_array_new_lengthpasses.ctest -E '^agc_', 4043 tests, before the rebase onto currentmain, with fix(libc): track guest exceptions caught by macOS host frames #22091, fix(libkernel): report a reset stream socket polled for no event as hung up on macOS #20161 and fix(libkernel): refuse a reconnect after a dropped connection on macOS #17879 also applied locally): 11 failures, all of which also fail onmainon this machine (or pass with the open PRs that fix them). No new failures.dyld_info -fixups: libSceLibcInternal and the test executables bind_Unwind_Resumeto libc.prx.run_report,guest_kernel_app_archive_mmap,guest_host_current_exceptionandguest_host_typed_catchpass. In the fullctest -j8suite, compared by test name, failures went from 19 of 5933 with commit 1 alone to 17 of 5934 with commit 2.run_reportandguest_kernel_app_archive_mmapnow pass. The only other differences are two AGC tests that fail in some runs and pass in others:agc_driver_graphicsfailed in the full run and passed 3 of 3 runs on its own, andagc_graphics_profile_drawdid the opposite.guest_throw_bad_array_new_length(cause 1) andguest_libc_internal_exception_virtuals(nested catch) still fail.pr_overlap.py --branch: feat(libSceLibcInternal): forward the firmware libc-internal export surface #19434 also touches__cxa_decrement_exception_refcount(it forwards the libc-internal export); the two changes do not conflict.check_conventions.py --base origin/mainpasses. Linux and Windows were not built: the alias is APPLE-only, and the Linux_Unwind_Resumepath is unchanged.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code