feat(libc): exception runtime on macOS hosts #927

Merged
mugurc merged 3 commits from upstream-pr/927 into main 2026-10-08 10:35:52 +00:00
mugurc commented 2026-10-06 17:47:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mugurc on 2026-10-06T17:47:44Z.


What

Exception support for the macOS port, opened first as suggested in #1. Two commits:

  1. fix(libc): export the C personality to guest code. This is the only change that reaches Linux and Windows. Guest C built with -fexceptions imports __gcc_personality_v0 for its cleanup frames, and libc.prx did not export it. The routine becomes __gcc_personality_v0_nid_postfix. The host __gcc_personality_v0 forwards to it, as __gxx_personality_v0 already does, and CallPersonality recognizes both names.
  2. feat(libc): exception runtime on macOS hosts. On macOS the host keeps libc++abi, because libc++ binds __cxa_throw two-level. libc.prx serves guest code:
    • The guest's libstdc++ std::exception_ptr is modelled directly, and the Itanium type_info layouts that libc++abi keeps private are declared. Both are compiled only under _LIBCPP_VERSION / _LIBCPPABI_VERSION.
    • Guest frames are found through their PT_GNU_EH_FRAME, using a dl_iterate_phdr emulation over the ELF program headers that relinked images keep in __APS5DATA,__meta.
    • Host frames are found by scanning __TEXT,__eh_frame.
    • sceKernelGetModuleInfoForUnwind gets a macOS branch that uses the same emulation and falls back to dladdr for host images. #904 changes the Windows branch of the same function; whichever lands second will be rebased.
    • exception_c_cleanup is not built on macOS, because it throws from host code.

Everything in the second commit is __APPLE__ or libc++ only, so it stays inert until the macOS build and relinker --macos (which writes __APS5DATA,__meta) come in the next pull requests.

Tested

  • Linux and Windows: this branch's CI run on my fork, https://github.com/mugurc/AnyPS5/actions/runs/37502814680. Linux passed 359/359, including exception_runtime and exception_c_cleanup; Windows passed 357/357.
  • macOS 26.5 on an M5 Max, x86-64 under Rosetta 2. The branch was built together with the rest of the port, rebased on current main:
    • exception_runtime passes;
    • the relinked guest fixtures pass: a C++ exception crossing guest and host frames, and C cleanup frames.
  • No commercial title was tested; I have no PS5.

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
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • 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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4762565388 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/927). Originally posted by [mugurc](https://github.com/mugurc) on 2026-10-06T17:47:44Z. --- ### What Exception support for the macOS port, opened first as suggested in #1. Two commits: 1. `fix(libc): export the C personality to guest code`. This is the only change that reaches Linux and Windows. Guest C built with `-fexceptions` imports `__gcc_personality_v0` for its cleanup frames, and libc.prx did not export it. The routine becomes `__gcc_personality_v0_nid_postfix`. The host `__gcc_personality_v0` forwards to it, as `__gxx_personality_v0` already does, and `CallPersonality` recognizes both names. 2. `feat(libc): exception runtime on macOS hosts`. On macOS the host keeps libc++abi, because libc++ binds `__cxa_throw` two-level. libc.prx serves guest code: - The guest's libstdc++ `std::exception_ptr` is modelled directly, and the Itanium `type_info` layouts that libc++abi keeps private are declared. Both are compiled only under `_LIBCPP_VERSION` / `_LIBCPPABI_VERSION`. - Guest frames are found through their `PT_GNU_EH_FRAME`, using a `dl_iterate_phdr` emulation over the ELF program headers that relinked images keep in `__APS5DATA,__meta`. - Host frames are found by scanning `__TEXT,__eh_frame`. - `sceKernelGetModuleInfoForUnwind` gets a macOS branch that uses the same emulation and falls back to `dladdr` for host images. #904 changes the Windows branch of the same function; whichever lands second will be rebased. - `exception_c_cleanup` is not built on macOS, because it throws from host code. Everything in the second commit is `__APPLE__` or libc++ only, so it stays inert until the macOS build and `relinker --macos` (which writes `__APS5DATA,__meta`) come in the next pull requests. ### Tested - Linux and Windows: this branch's CI run on my fork, https://github.com/mugurc/AnyPS5/actions/runs/37502814680. Linux passed 359/359, including `exception_runtime` and `exception_c_cleanup`; Windows passed 357/357. - macOS 26.5 on an M5 Max, x86-64 under Rosetta 2. The branch was built together with the rest of the port, rebased on current `main`: - `exception_runtime` passes; - the relinked guest fixtures pass: a C++ exception crossing guest and host frames, and C cleanup frames. - No commercial title was tested; I have no PS5. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-07 13:57:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-07T13:57:03Z.


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
#1128 _ZNSt15__exception_ptr13exception_ptrC1EPv_nid_postfix
_ZNSt15__exception_ptr13exception_ptrC2EPv_nid_postfix
_ZSt17current_exceptionv_nid_postfix
#1176 core/libs/prx/libc/CMakeLists.txt L246-250
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6039514786 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/927#issuecomment-6039514786). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-07T13:57:03Z. --- <!-- pr-overlap --> 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 | |---|---|---|---| | #1128 | | `_ZNSt15__exception_ptr13exception_ptrC1EPv_nid_postfix`<br>`_ZNSt15__exception_ptr13exception_ptrC2EPv_nid_postfix`<br>`_ZSt17current_exceptionv_nid_postfix` | | | #1176 | `core/libs/prx/libc/CMakeLists.txt` [L246-250](https://github.com/boykopovar/AnyPS5/blob/2cc5735d30ae753244f1733cd89d8fd53f88de72/core/libs/prx/libc/CMakeLists.txt#L246-L250) | | |
github-actions[bot] commented 2026-10-08 09:48:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T09:48:50Z.


Progress report (30e89cc → 0ae0bbf)

➖ System libraries: 85.4% (+0.0%, +1 implemented functions)

✅ 1 implemented
Library Name
libc __gcc_personality_v0_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6057212448 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/927#issuecomment-6057212448). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T09:48:50Z. --- <!-- progress-report --> ### Progress report (30e89cc → 0ae0bbf) ➖ **System libraries**: 85.4% (+0.0%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libc | `__gcc_personality_v0_nid_postfix` | </details>
adriadam10 commented 2026-10-08 09:56:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T09:56:10Z.

Review: CHANGES_REQUESTED


Build + ctest on this PR merged onto current main: 433/433 pass. Findings:

  • The only change that reaches Linux/Windows (__gcc_personality_v0_nid_postfix exported to guest code, core/libs/prx/libc/src/exception/Personality.cpp:74) has no test that fails without it. exception_c_cleanup links the host __gcc_personality_v0, and it already passed on main, so it doesn't show the guest export or CallPersonality matching __gcc_personality_v0_nid_postfix (Unwind.cpp:31). Please add a test that reaches the personality through the _nid_postfix symbol (the way a guest binds it) and fails on main.
  • macOS half (GuestImages.cpp, the __eh_frame scan in Unwind.cpp:150-171, the Module.cpp:114-158 branch): inert on Linux/Windows, and I can't run it here (no macOS, no macOS CI). It also depends on MetaVersion = 2 and the __APS5DATA,__meta offsets (GuestImages.cpp:11-14), which the relinker side doesn't have in this repo yet. Please attach the macOS test output (exception_runtime and the relinked guest fixtures) to the PR, and keep the layout constants in sync with the relinker --macos PR when it lands.
  • Minor: two topics in one PR (guest export vs. macOS runtime). Fine given boykopovar's request in #1; if the test above needs more code, I'd split the export into its own PR.

The overlap bot's #1128 and #1176 hits don't apply (formatting-only / already merged).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5454807156 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/927#pullrequestreview-5454807156). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T09:56:10Z. **Review:** CHANGES_REQUESTED --- Build + ctest on this PR merged onto current `main`: 433/433 pass. Findings: - The only change that reaches Linux/Windows (`__gcc_personality_v0_nid_postfix` exported to guest code, `core/libs/prx/libc/src/exception/Personality.cpp:74`) has no test that fails without it. `exception_c_cleanup` links the host `__gcc_personality_v0`, and it already passed on `main`, so it doesn't show the guest export or `CallPersonality` matching `__gcc_personality_v0_nid_postfix` (`Unwind.cpp:31`). Please add a test that reaches the personality through the `_nid_postfix` symbol (the way a guest binds it) and fails on `main`. - macOS half (`GuestImages.cpp`, the `__eh_frame` scan in `Unwind.cpp:150-171`, the `Module.cpp:114-158` branch): inert on Linux/Windows, and I can't run it here (no macOS, no macOS CI). It also depends on `MetaVersion = 2` and the `__APS5DATA,__meta` offsets (`GuestImages.cpp:11-14`), which the relinker side doesn't have in this repo yet. Please attach the macOS test output (`exception_runtime` and the relinked guest fixtures) to the PR, and keep the layout constants in sync with the `relinker --macos` PR when it lands. - Minor: two topics in one PR (guest export vs. macOS runtime). Fine given boykopovar's request in #1; if the test above needs more code, I'd split the export into its own PR. The overlap bot's #1128 and #1176 hits don't apply (formatting-only / already merged).
Zheorgyan commented 2026-10-08 10:15:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Zheorgyan on 2026-10-08T10:15:48Z.


ran this on Linux since you only have the Mac. merged onto current main at 6e037e98, Ubuntu 24.04 in Docker with GCC 14.2. the only conflict is core/libs/prx/libc/CMakeLists.txt, where main added the guest_image_anonymous and guest_collate tests right after the block you wrap in if(NOT APPLE). keeping main's lines and wrapping just the exception_c_cleanup part resolves it

with that the build is clean and exception_runtime, exception_c_cleanup and guest_image_anonymous pass. the full suite has 26 failures, but every one of them fails the same way on main in that container, which is x86-64 emulated on Apple Silicon, so none come from this PR. I can't test Windows

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6057671288 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/927#issuecomment-6057671288). Originally posted by [Zheorgyan](https://github.com/Zheorgyan) on 2026-10-08T10:15:48Z. --- ran this on Linux since you only have the Mac. merged onto current main at `6e037e98`, Ubuntu 24.04 in Docker with GCC 14.2. the only conflict is `core/libs/prx/libc/CMakeLists.txt`, where main added the `guest_image_anonymous` and `guest_collate` tests right after the block you wrap in `if(NOT APPLE)`. keeping main's lines and wrapping just the `exception_c_cleanup` part resolves it with that the build is clean and `exception_runtime`, `exception_c_cleanup` and `guest_image_anonymous` pass. the full suite has 26 failures, but every one of them fails the same way on main in that container, which is x86-64 emulated on Apple Silicon, so none come from this PR. I can't test Windows
mugurc commented 2026-10-08 10:20:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mugurc on 2026-10-08T10:20:17Z.


Thanks for the review. I pushed an update rebased on current main, with one new commit.

Test for the guest export. test(libc): reach the C personality through its guest export adds exception_personality (core/libs/tests/ExceptionPersonality.cpp). It builds a call-site table by hand and drives the routine the way the unwinder does, through __gcc_personality_v0_nid_postfix and through CallPersonality: cleanup phase installs the landing pad, search phase continues, ranges before and after the call site, no landing pad, no LSDA, signal frame, unsupported version, unknown personality. On main it does not link, since the export does not exist. With the _nid_postfix comparison removed from CallPersonality (Unwind.cpp:31) the test aborts.

macOS output. Run on macOS under Rosetta with the libc files of this PR on top of my port branch, which is based on 5895ea72, not current main; the port's build changes are not in this PR yet. ctest: 357 tests, 216 passed, 141 skipped (GPU tests, no Vulkan environment), 0 failed. Passing: exception_runtime, exception_personality, and macos_fixtures (argv, import, tls, exception, c-cleanup, threads, module, tls-modules, intel-unconverted, intel).

Layout constants. Noted. MetaVersion = 2 and the __APS5DATA,__meta offsets in GuestImages.cpp will be kept in sync with the relinker --macos PR.

Split. The export and its test are self-contained in the first and last commits, so I can move them into their own PR if you prefer.

Thanks also to @Zheorgyan for running this on Linux. In the CMakeLists.txt conflict I wrapped guest_image_anonymous in the same if(NOT APPLE) as exception_c_cleanup, because it reads /proc/self/exe and would fail on macOS; on Linux the result is the same.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6057742823 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/927#issuecomment-6057742823). Originally posted by [mugurc](https://github.com/mugurc) on 2026-10-08T10:20:17Z. --- Thanks for the review. I pushed an update rebased on current `main`, with one new commit. **Test for the guest export.** `test(libc): reach the C personality through its guest export` adds `exception_personality` (`core/libs/tests/ExceptionPersonality.cpp`). It builds a call-site table by hand and drives the routine the way the unwinder does, through `__gcc_personality_v0_nid_postfix` and through `CallPersonality`: cleanup phase installs the landing pad, search phase continues, ranges before and after the call site, no landing pad, no LSDA, signal frame, unsupported version, unknown personality. On `main` it does not link, since the export does not exist. With the `_nid_postfix` comparison removed from `CallPersonality` (`Unwind.cpp:31`) the test aborts. **macOS output.** Run on macOS under Rosetta with the libc files of this PR on top of my port branch, which is based on `5895ea72`, not current `main`; the port's build changes are not in this PR yet. `ctest`: 357 tests, 216 passed, 141 skipped (GPU tests, no Vulkan environment), 0 failed. Passing: `exception_runtime`, `exception_personality`, and `macos_fixtures` (argv, import, tls, exception, c-cleanup, threads, module, tls-modules, intel-unconverted, intel). **Layout constants.** Noted. `MetaVersion = 2` and the `__APS5DATA,__meta` offsets in `GuestImages.cpp` will be kept in sync with the `relinker --macos` PR. **Split.** The export and its test are self-contained in the first and last commits, so I can move them into their own PR if you prefer. Thanks also to @Zheorgyan for running this on Linux. In the `CMakeLists.txt` conflict I wrapped `guest_image_anonymous` in the same `if(NOT APPLE)` as `exception_c_cleanup`, because it reads `/proc/self/exe` and would fail on macOS; on Linux the result is the same.
adriadam10 commented 2026-10-08 10:35:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T10:35:48Z.

Review: APPROVED


Checked on this PR merged onto current main: build + ctest 439/439, exception_personality passes; linux, windows, conventions and report CI green. Without the __gcc_personality_v0_nid_postfix comparison in CallPersonality (Unwind.cpp:31) the new test aborts, and on main it doesn't link, so it covers the guest export. The macOS half is inert on Linux/Windows; we can't run it here, so it's merged on code review plus the macOS output you attached. Please keep MetaVersion and the __APS5DATA,__meta offsets (GuestImages.cpp:11-14) in sync with the relinker --macos PR. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5455270201 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/927#pullrequestreview-5455270201). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T10:35:48Z. **Review:** APPROVED --- Checked on this PR merged onto current `main`: build + ctest 439/439, `exception_personality` passes; linux, windows, conventions and report CI green. Without the `__gcc_personality_v0_nid_postfix` comparison in `CallPersonality` (`Unwind.cpp:31`) the new test aborts, and on `main` it doesn't link, so it covers the guest export. The macOS half is inert on Linux/Windows; we can't run it here, so it's merged on code review plus the macOS output you attached. Please keep `MetaVersion` and the `__APS5DATA,__meta` offsets (`GuestImages.cpp:11-14`) in sync with the `relinker --macos` PR. Merging.
mugurc commented 2026-10-08 11:02:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mugurc on 2026-10-08T11:02:23Z.


A correction to the macOS output above: exception_runtime passed in that run only because the test's assertions were compiled out in Release at the time (742fa2e3 later added #undef NDEBUG). With assertions on it does not pass on macOS, because host code there keeps libc++abi while the test expects the project's runtime to serve host throws. The guest side is covered by the relinked exception and c-cleanup fixtures and by exception_personality, which do pass. I will register exception_runtime only outside macOS in the macOS build PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6058434581 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/927#issuecomment-6058434581). Originally posted by [mugurc](https://github.com/mugurc) on 2026-10-08T11:02:23Z. --- A correction to the macOS output above: `exception_runtime` passed in that run only because the test's assertions were compiled out in Release at the time (`742fa2e3` later added `#undef NDEBUG`). With assertions on it does not pass on macOS, because host code there keeps libc++abi while the test expects the project's runtime to serve host `throw`s. The guest side is covered by the relinked `exception` and `c-cleanup` fixtures and by `exception_personality`, which do pass. I will register `exception_runtime` only outside macOS in the macOS build PR.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!927
No description provided.