fix(libc): bridge guest and host exceptions on macOS #23395
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/23395"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-11-19T00:09:07Z.
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 #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 #22091 also applied, 3 fail;guest_throw_bad_array_new_lengthpasses.ctest -E '^agc_', 4043 tests, before the rebase onto currentmain, with #22091, #20161 and #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: #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
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T03:22:24Z.
Review: COMMENTED
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
0b4e69eb68abf0b3d537fd17b27c68410be1fee8using the prepared review bundle and pinned repository reader. No commands or tests were run.Results
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, anddocs/dev/debt/changes/fix-macos-libcxx-typed-catch.md. All six files listed as changed inmanifest.jsonwere reviewed; no changed files were excluded.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T21:47:19Z.
Review: COMMENTED
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.pytools at the pinned head. No build or tests were run.Results
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.
Imported from boykopovar/AnyPS5. Originally posted by Jazeupoi on 2026-11-20T15:00:23Z.
Review: COMMENTED
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
b1b16e7f84applied ontomain0a025bd3c6with #17425 and #11049 (needed for the macOS build); clean apply. Each test was run 3 times on its own, on both trees.mainguest_kernel_app_archive_mmaprun_reportguest_libc_internal_exception_virtualsguest_throw_bad_array_new_lengthThis matches the description: the two that pass are the ones listed for commit 2, and
guest_libc_internal_exception_virtualsis noted as still failing. I did not runguest_host_typed_catchorguest_host_current_exception; they are new in this PR and need a CMake reconfigure that I did not do.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T19:48:59Z.
Review: COMMENTED
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 onto484e8e6b77conflicts in libc'sCMakeLists.txt. Native macOS execution is unavailable here.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.