fix(libc): run the cleanups of frames that use the C personality #478

Merged
oneandonlydean merged 1 commit from upstream-pr/478 into main 2026-10-03 22:34:58 +00:00
oneandonlydean commented 2026-10-03 19:32:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-03T19:32:24Z.


What

libc's unwinder called only its own C++ personality. A frame whose CIE names any other personality made _Unwind_RaiseException return _URC_FATAL_PHASE1_ERROR (or phase 2 fail), so the exception reached terminate ("[libc] unhandled exception") even with a matching catch above.

GCC gives such frames the C personality, __gcc_personality_v0:

  • C code compiled with -fexceptions that has cleanups (__attribute__((cleanup)));
  • functions that only run cleanups once link-time optimization has merged them: I hit this while trying LTO on libSceAgcDriver and its recompiler, where 11 tests failed this way.

Under the Itanium C++ ABI the C personality never catches. In the search phase it continues unwinding; in the cleanup phase it installs the call site's landing pad, if there is one, with the exception in the first data register and 0 in the second, and continues otherwise. The landing pad ends in _Unwind_Resume. This is what libgcc's unwind-c.c does.

  • Personality.cpp: libc defines __gcc_personality_v0 with that behaviour, reading the same LSDA layout as the C++ personality. Like __gxx_personality_v0, it is a host symbol, so code linked against libc binds to it.
  • Unwind.cpp: OwnPersonality becomes CallPersonality, which calls the C++ or the C personality for the frame and still fails the phase for any other personality.

Tested

  • Linux (CachyOS, x86-64, GCC 16): full build with BUILD_TESTING=ON; ctest passes (146/146).
  • New exception_c_cleanup test (tests/ExceptionCCleanup.c built with -fexceptions, driven from tests/ExceptionCCleanup.cpp): an int thrown from C++ through C frames with cleanups is caught in C++ with the cleanups run (one frame, two nested frames, a C++ destructor frame mixed in), a call made before the cleanup's scope leaves it unrun, and a throw with no handler still terminates without running the C cleanup. Without the change the test aborts with "[libc] unhandled exception" on the first case.
  • LTO on libSceAgcDriver and shader_recompiler_agc_driver only (INTERPROCEDURAL_OPTIMIZATION, not part of this PR): both then import __gcc_personality_v0. With this change ctest passes 146/146; with main's unwinder 12 tests abort, the new one and 11 driver tests (agc_driver, agc_driver_graphics, agc_shader_memory, guest_indexed_draw and others).
  • Windows CI: the change builds and passes; the new test is registered on Unix only, next to exception_runtime, since it forks for the terminate case.
  • PPSA21564: not affected; its modules have no C personality frames today.

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
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4729140958 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/478). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-03T19:32:24Z. --- ### What libc's unwinder called only its own C++ personality. A frame whose CIE names any other personality made `_Unwind_RaiseException` return `_URC_FATAL_PHASE1_ERROR` (or phase 2 fail), so the exception reached `terminate` ("[libc] unhandled exception") even with a matching `catch` above. GCC gives such frames the C personality, `__gcc_personality_v0`: - C code compiled with `-fexceptions` that has cleanups (`__attribute__((cleanup))`); - functions that only run cleanups once link-time optimization has merged them: I hit this while trying LTO on `libSceAgcDriver` and its recompiler, where 11 tests failed this way. Under the Itanium C++ ABI the C personality never catches. In the search phase it continues unwinding; in the cleanup phase it installs the call site's landing pad, if there is one, with the exception in the first data register and 0 in the second, and continues otherwise. The landing pad ends in `_Unwind_Resume`. This is what libgcc's `unwind-c.c` does. - `Personality.cpp`: libc defines `__gcc_personality_v0` with that behaviour, reading the same LSDA layout as the C++ personality. Like `__gxx_personality_v0`, it is a host symbol, so code linked against libc binds to it. - `Unwind.cpp`: `OwnPersonality` becomes `CallPersonality`, which calls the C++ or the C personality for the frame and still fails the phase for any other personality. ### Tested - Linux (CachyOS, x86-64, GCC 16): full build with `BUILD_TESTING=ON`; `ctest` passes (146/146). - New `exception_c_cleanup` test (`tests/ExceptionCCleanup.c` built with `-fexceptions`, driven from `tests/ExceptionCCleanup.cpp`): an `int` thrown from C++ through C frames with cleanups is caught in C++ with the cleanups run (one frame, two nested frames, a C++ destructor frame mixed in), a call made before the cleanup's scope leaves it unrun, and a throw with no handler still terminates without running the C cleanup. Without the change the test aborts with "[libc] unhandled exception" on the first case. - LTO on `libSceAgcDriver` and `shader_recompiler_agc_driver` only (`INTERPROCEDURAL_OPTIMIZATION`, not part of this PR): both then import `__gcc_personality_v0`. With this change `ctest` passes 146/146; with `main`'s unwinder 12 tests abort, the new one and 11 driver tests (`agc_driver`, `agc_driver_graphics`, `agc_shader_memory`, `guest_indexed_draw` and others). - Windows CI: the change builds and passes; the new test is registered on Unix only, next to `exception_runtime`, since it forks for the terminate case. - PPSA21564: not affected; its modules have no C personality frames today. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
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!478
No description provided.