Skip to content

fix(libc): track guest exceptions caught by macOS host frames - #22091

Open
chaseb64 wants to merge 1 commit into
boykopovar:mainfrom
chaseb64:fix/macos-guest-host-catch
Open

chaseb64 wants to merge 1 commit into
boykopovar:mainfrom
chaseb64:fix/macos-guest-host-catch

Conversation

@chaseb64

@chaseb64 chaseb64 commented Nov 17, 2026 •

Copy link
Copy Markdown

What

On macOS, a host catch calls libc++abi's __cxa_begin_catch, leaving AnyPS5's guest caught-exception state empty. Register an AnyPS5 exception when the unwind dispatcher installs the selected host C++ handler. Its Itanium cleanup callback retires the guest catch before releasing the exception. A rethrow from a host-owned catch stays with libc++abi.

The macOS personality regression exercises current type, current primary exception, uncaught count, retained references, and a dependent exception rethrown through both entry points. This addresses the catch-tracking part of #21538. The typed RTTI and host-thrown terminate/report issues remain open.

Tested

Apple M5 Pro, macOS 27.3 (26C5035r), Apple Clang 21.0.0, CMake 4.5.0 and Ninja 1.13.2. Full-runtime Release configuration with BUILD_TESTING=ON and CMAKE_OSX_ARCHITECTURES=x86_64, running under Rosetta 2.

Both macOS builds used the libc++abi enum fix from #26739 (6d85fbf66b). The base was upstream 21940f44a5 and the candidate was #22091 rebased as b4635cf2d2. The #26739 change was applied to each build for validation.

macOS test Base With #22091
guest_throw_bad_array_new_length Aborted Passed
exception_personality Passed with the original test Passed with the expanded test
exception_compact_unwind, guest_unwind_accessors, unwind_cursor, unwind_enclosing_function, guest_dynamic_cast, guest_kernel_app_archive_mmap Passed Passed
guest_libc_internal_exception_virtuals Aborted on unhandled guest std::future_error Same failure
run_report Missing the abort report in child-uncaught-missing-function Same failure

The expanded exception_personality aborted when run against the base runtime with #26739 applied. Conventions and whitespace checks passed on the rebased commit.

The contributor ran identical focused CTest commands before and after applying the patch in separate Release builds with BUILD_TESTING=ON.

Platform and toolchain Base Patched
Linux x86-64, Ubuntu 26.04.1 LTS under WSL2, GCC/G++ 15.2.0 11/11 passed 11/11 passed
Native Windows x86-64, Windows 11 Pro 26H2 build 26300.9974, WinLibs GCC 15.2.0 (15.2.0posix-14.0.0-ucrt-r7) 8/8 passed 8/8 passed

All four runs had zero failures and zero skips. exception_compact_unwind, exception_runtime, and exception_c_cleanup passed on Linux and are unregistered on Windows. CMake's DWARF exception probe passed on Windows. run_report passed on both platforms before and after applying the patch.

The Linux and Windows test base was d642b4efeeeb3ed33c39301fcbe9eb4dc2170347. The PC commit b2f4ac498ecad8ca890579692c0fb2e087385f03 had the same stable Git patch ID as the attachment from e01ba12971f79e564bb42d86708f831c3cdaa751, and the attachment's SHA-256 was unchanged. On the PC, python3 tools/check_conventions.py --base origin/main passed against a72186d3cfdb657c2b0c062471ca47c9b0d9cf13. Both whitespace checks passed.

PC logs are under validation-logs. verification.log records the test-to-target mapping, and linux-run.log and windows-run.log record the exact commands.

Manually reviewed the PRs named in the overlap report. #25253 adds a separate test block at the same spot in ExceptionPersonality.cpp. A three-way merge of that file conflicts, and the two blocks can coexist. #23395 also changes the macOS exception bridge. The combined run reported in review covers the original #22091 head. #16791, #25204, and #25935 address other unwind paths.

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

@PrivacyOnion PrivacyOnion 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.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against df18f19b23:

  • Conventions: tools/check_conventions.py passed.
  • Build: exception_personality_tests built cleanly.
  • CTest (exception_personality):
    1/1 Test #1303: exception_personality ............   Passed    0.00 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/include/exceptions/Runtime.hpp, core/libs/prx/libc/src/exception/Runtime.cpp, core/libs/prx/libc/src/exception/Unwind.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1303: exception_personality ............   Passed    0.00 sec
    100% tests passed, 0 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified RecordHostCatch and Cleanup in core/libs/prx/libc/src/exception/Runtime.cpp properly track caught guest exceptions and decrement handlers upon foreign exception cleanup
    • Checked __cxa_end_catch_nid_postfix correctly delegates host-caught rethrow via abi::__cxa_rethrow() on macOS
    • Verified CallPersonality dispatch logic in core/libs/prx/libc/src/exception/Unwind.cpp routing __gxx_personality_v0 to CallHostCxxPersonality on Apple targets
    • Audited Header struct padding and alignment across platforms in core/libs/prx/libc/include/exceptions/Runtime.hpp
    • Verified technical debt fragment format in docs/dev/debt/changes/fix-macos-guest-host-catch.md matches AGENTS.md requirements

Looks Good To Me

@github-actions

github-actions Bot commented Nov 17, 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 25253 core/libs/tests/ExceptionPersonality.cpp L89

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 23395 core/libs/prx/libc/src/exception/Runtime.cpp
core/libs/prx/libc/src/exception/Unwind.cpp

@Jazeupoi

Copy link
Copy Markdown
Contributor

Tested this head (df18f19b23) on Apple M4, macOS 27.0.1, Apple clang 21.0.0, x86-64 Release under Rosetta 2. Base: main e798a424c7 with #17425 and #11049 (needed for the macOS build); "this PR" is the same tree with this head merged (clean merge).

Each test run alone 10 times:

test base this PR negative control
guest_throw_bad_array_new_length 10/10 fail 0/10 10/10 fail
exception_personality 0/10 fail (the base's version of the test) 0/10 10/10 fail (aborts)

The negative control is this PR's tree with Runtime.hpp, Runtime.cpp and Unwind.cpp reverted to the base, so the expanded exception_personality runs against the old runtime. It fails every time, so the new checks do catch the missing catch tracking.

ctest -R 'exception|unwind|guest_throw|guest_kernel_app_archive_mmap|run_report' -E '^agc_' (166 tests), twice per build: the base fails 4 both times, this PR 3. The difference is guest_throw_bad_array_new_length. The other three, run_report, guest_libc_internal_exception_virtuals and guest_kernel_app_archive_mmap, fail on both, as the description says. Nothing that passes on the base fails with this PR.

AI-assisted: yes (Claude Code)

@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 df18f19b23) on top.

Without this PR, all four of these fail 3 of 3 runs. With it:

test result
guest_throw_bad_array_new_length passes 3/3
guest_libc_internal_exception_virtuals still aborts 3/3: unhandled exception 'St12future_error', terminate called after throwing an instance of 'std::future_error'
guest_kernel_app_archive_mmap still fails 3/3
run_report still fails 3/3: child-uncaught-missing-function: the report of the run that ended in std::terminate lacks "exit": "abort"

This matches the description: on the M1 the catch-tracking fix clears guest_throw_bad_array_new_length, and the other three fail the same way as on the base. I didn't run the other focused unwind tests the description lists.

AI-assisted: yes (Claude Code)

@simonbronk

Copy link
Copy Markdown
Contributor

Tested on Apple M4 (macOS 27.2.1, Apple clang version 21.0.0 (clang-2100.4.1.1)), x86_64 Release under Rosetta 2. The tree was main d80403640a with #17425 and #11049, which the macOS build needs, plus #23395 (head b1b16e7f84). The two PRs share Runtime.cpp and Unwind.cpp, so I checked them together: this PR's head df18f19b23 merges onto #23395 without conflicts. I built the full tree both ways and ran each focused set 3 times.

Test #23395 alone #23395 + this PR
guest_throw_bad_array_new_length fails (aborted), 3 of 3 runs pass, 3 of 3 runs
exception_personality pass, 3 of 3 runs (test as on main) pass, 3 of 3 runs (expanded test)
guest_host_typed_catch pass, 3 of 3 runs pass, 3 of 3 runs
guest_host_current_exception pass, 3 of 3 runs pass, 3 of 3 runs
run_report pass, 3 of 3 runs pass, 3 of 3 runs
guest_kernel_app_archive_mmap pass, 3 of 3 runs pass, 3 of 3 runs
guest_libc_internal_exception_virtuals fails (aborted) fails (aborted)

guest_libc_internal_exception_virtuals is the nested foreign-catch limit recorded in #23395's debt fragment. The failure is the same with and without this PR.

With both PRs applied, ctest -R 'exception|catch|throw|personality|unwind|terminate|run_report|app_archive' runs 179 tests. 178 pass and guest_libc_internal_exception_virtuals fails. I didn't run the full suite or a title.

AI-assisted: yes (Claude Code)

@ycbuzi

ycbuzi commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Tested df18f19b23 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, 2 tests failed out of 6427; 99% tests passed, 2 tests failed out of 6427. 2 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.

@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 — looks good

The reviewed change at df18f19b2357a660b37c55f390e8ed95e2d020f9 addresses the described macOS guest exception catch tracking, and the inspected diff and changed files show no verified blocking defect.

What I ran

No build or tests were run in this review. I read the prepared diff and all five changed files through repo_read.py; test results in the PR description are author-reported.

Results

Check Result Note
Diff and changed files passed All five reviewed files read
Tests in this review not run Author-reported results only

Findings

Blocking

None.

Non-blocking

None.

Not verified

Build and runtime behavior were not independently verified. No files were excluded from review; all five files listed as reviewed in the manifest were read.

To get this to a good state

Nothing is required.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 24, 2026
@chaseb64
chaseb64 force-pushed the fix/macos-guest-host-catch branch from df18f19 to b4635cf Compare November 25, 2026 04:15
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 25, 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 — looks good

Nothing blocks this change; the host catch registration and cleanup logic are consistent with the existing guest catch accounting.

What I ran

Reviewed head b4635cf2d292e27a8471d62773821d6f52cc2ae9. Read the prepared description, commit summary, diff, manifest, and all five changed files listed in the manifest. Inspected the related catch bookkeeping and host personality dispatch. No tests or builds were run.

Results

Check Result Note
Changed code review passed Five changed files read
Tests and builds not run Contributor results are author reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

Tests and builds were not run in this review. review/files.json was not available through the reader, so excluded-by-rule files could not be checked; the manifest lists all five changed files as included.

To get this to a good state

Nothing is required.

@Romannachwievor Romannachwievor 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.

macOS measurement on an M1 (x86_64 build, Release, BUILD_TESTING=ON, same flags as my local nightly).

Base is f42464a6fe: origin/main at a901acf65c plus #17425, #11049, #26739 and #27621, which the macOS build needs (without #17425 the curl backend builds arm64 and 31 tests do not run; without #27621 libwebrtc does not build).
PR head b4635cf2d2, merged into the base as ad906a95d9 (supplement run: 558467c75c, same head).

Tests: tools/select_tests.py select picked 6621 of 6781 tests and 3603 build targets. Tests whose names match ^agc_|videoout|vulkan|shader|gpu are dropped from the selection because the PR touches no AGC, shader recompiler or VideoOut code. That leaves 4529 selected tests. Run 1 (ctest -j8 --timeout 300) had a mistake in my script and covered 4483 of them plus 131 unselected ones; run 2 ran the 46 missing selected tests (47 with one CTest fixture) against the PR build, 47/47 passed.

  • Base (4665 non-GPU tests, run once): 18 failed, the known macOS failures
  • PR: 17 failed in run 1, all 17 also fail on the base; guest_firmware_constant_returns and guest_posix_spawn passed on a serial retry
  • guest_throw_bad_array_new_length fails on the base ("Subprocess aborted") and 0/3 serially on base + #24415 (which does not touch exceptions), but passes with this PR
  • exception_personality (touched by the PR) passed on base and PR
  • guest_exception_pointer, guest_libcxx_exception_pointer and guest_libc_internal_exception_virtuals fail on both (0/3 serial with the PR)

A test counts as broken by the PR only if it passes on the base and fails 3 of 3 serial reruns with the PR. None did.

The only build failure, on both base and PR, is core/libs/prx/libSceAgcDriver/tests/execution/PipelinedFlushReadOrder.cpp:232 (use of undeclared identifier 'mapped'). It is already on main.

@NobleBrains NobleBrains 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.

Confirmed on an older macOS and toolchain than the ones in the description. This matters because the macos-15 runner and most users' Macs are on macOS 15.

Setup: Apple M4, macOS 15.7.9, Apple clang 17.0.0 (clang-1700.6.3.2), x86_64 Release under Rosetta, BUILD_TESTING=ON.
Tree: main 93d64f2d1e + the open macOS build fixes #27201, #27739, #27843, #17425, #27827, #27767 (main does not build on macOS without them, see #28168), then this PR at b4635cf2d2. It merges cleanly.

Full ctest -j 6 against the same tree without this PR:

  • Now passes: guest_throw_bad_array_new_length. It aborted without this PR in each of 3 earlier runs and passed 3/3 with it.
  • Still pass: exception_personality (the expanded test), exception_compact_unwind, guest_unwind_accessors, unwind_cursor, unwind_enclosing_function, guest_dynamic_cast, guest_kernel_app_archive_mmap.
  • Unchanged, as the description says: guest_libc_internal_exception_virtuals and run_report (3/3 each). guest_exception_pointer and guest_libcxx_exception_pointer also still abort; those belong to the rest of #21538.
  • No regressions. guest_http2_cookies and guest_net_oob_inline failed once during the parallel full run, then passed 3/3 when rerun. They don't fail on their own.

So the results in the description hold on macOS 15 / Apple clang 17 as well.

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.

CheckHostCatch now covers the host and guest rethrow routes and verifies that TLS queries are cleared after each catch. Could it also count destruction of the retained primary exception? Checking zero destructions before the final release and one afterward would verify that the host cleanup unlinks the catch record and balances ownership, including the dependent exception created by __cxa_rethrow_primary_exception.

Source review at b4635cf2d2. A trial merge onto 484e8e6b77 succeeds. exception_personality, guest_exception_abi_entry_points, and guest_exception_pointer passed on Linux with this head merged into the native test base 6c6b29320f. The new host-catch cases are macOS-only and remain unverified 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants