fix(libc): unwind guest exceptions through Windows DLL frames #4511

Merged
rebelittle merged 1 commit from upstream-pr/4511 into main 2026-10-12 18:30:11 +00:00
rebelittle commented 2026-10-12 13:52:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rebelittle on 2026-10-12T13:52:37Z.


What

On Windows, a guest exception could not cross a frame of a module without the project's .ehmeta/.ehfram tables: the libc unwinder found no FDE there and stopped, so the exception was reported as unhandled. WinLibs GCC 15.2's libstdc++-6.dll and libwinpthread-1.dll have empty .eh_frame sections and only PE unwind data, so std::call_once (through __once_proxy and pthread_once) was enough to hit it. #3411 found this through the std::call_once test of #1423; #3415 replaced that test with guest-only frames and recorded the gap in TechnicalDebt, and #3411 was closed with it.

When DecodeFrame finds no FDE and the frame's image has no .ehmeta or .ehfram section, the unwinder now looks up the frame's RUNTIME_FUNCTION (RtlLookupFunctionEntry) and gets the caller's registers from RtlVirtualUnwind, including the Windows nonvolatile rsi, rdi and xmm6-xmm15 that LibcRestoreRegisters restores. Such a frame has no personality, so both phases step through it. The fallback is limited to images without those sections, so a guest or project frame without an FDE still stops the walk as before.

Windows exception and termination handlers are not run, so a frame whose unwind data has one (RtlVirtualUnwind returns its handler for UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER) still stops the walk, and the exception is reported as unhandled as it was before. In the WinLibs runtime DLLs and Windows 10's ucrtbase.dll:

DLL functions with a handler on the std::call_once path
libwinpthread-1.dll 1 of 263 pthread_once: none
libstdc++-6.dll 1864 of 5913 (__gxx_personality_seh0) __once_proxy: none
ucrtbase.dll 960 of 3894 qsort has one, bsearch none

windows_exceptions gets the std::call_once case of #1423 back, next to the guest-frame case of #3415: an exception thrown in the std::call_once callable is caught at depths 0, 1, 7 and 64, and every destructor on the way runs. The TechnicalDebt entry now describes the remaining gap (frames with handlers).

Tested

Windows 10 22H2 (19045), WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON, based on main c6efe068a:

  • windows_exceptions with the restored case: fails without the change ([libc] unhandled exception 'St13runtime_error' ... what(): through runtime frames), passes 3/3 with it.
  • Full ctest on an NVIDIA RTX 3090 (driver 610.47, ANYPS5_GPU=3090): 968 tests, 961 passed, 7 skipped (the video out tests without a display or Vulkan device, and agc_driver_stencil_ref_export, as this driver lacks VK_EXT_shader_stencil_export), 0 failed; tools/check_skips.py --job windows passes.
  • By hand, with a comparator that throws (not added as a test, since system DLL unwind data can change between Windows versions): through bsearch in ucrtbase.dll the exception is caught, where it was reported as unhandled without the change; through qsort, whose frame has a handler, it is reported as unhandled as before.
  • Linux and macOS are unchanged: the new code is under _WIN32.
  • tools/check_conventions.py and tools/check_test_registration.py pass.

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
  • Unblocks: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4830394489 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4511). Originally posted by [rebelittle](https://github.com/rebelittle) on 2026-10-12T13:52:37Z. --- ### What On Windows, a guest exception could not cross a frame of a module without the project's `.ehmeta`/`.ehfram` tables: the libc unwinder found no FDE there and stopped, so the exception was reported as unhandled. WinLibs GCC 15.2's `libstdc++-6.dll` and `libwinpthread-1.dll` have empty `.eh_frame` sections and only PE unwind data, so `std::call_once` (through `__once_proxy` and `pthread_once`) was enough to hit it. #3411 found this through the `std::call_once` test of #1423; #3415 replaced that test with guest-only frames and recorded the gap in TechnicalDebt, and #3411 was closed with it. When `DecodeFrame` finds no FDE and the frame's image has no `.ehmeta` or `.ehfram` section, the unwinder now looks up the frame's `RUNTIME_FUNCTION` (`RtlLookupFunctionEntry`) and gets the caller's registers from `RtlVirtualUnwind`, including the Windows nonvolatile `rsi`, `rdi` and `xmm6`-`xmm15` that `LibcRestoreRegisters` restores. Such a frame has no personality, so both phases step through it. The fallback is limited to images without those sections, so a guest or project frame without an FDE still stops the walk as before. Windows exception and termination handlers are not run, so a frame whose unwind data has one (`RtlVirtualUnwind` returns its handler for `UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER`) still stops the walk, and the exception is reported as unhandled as it was before. In the WinLibs runtime DLLs and Windows 10's `ucrtbase.dll`: | DLL | functions with a handler | on the `std::call_once` path | |---|---|---| | `libwinpthread-1.dll` | 1 of 263 | `pthread_once`: none | | `libstdc++-6.dll` | 1864 of 5913 (`__gxx_personality_seh0`) | `__once_proxy`: none | | `ucrtbase.dll` | 960 of 3894 | `qsort` has one, `bsearch` none | `windows_exceptions` gets the `std::call_once` case of #1423 back, next to the guest-frame case of #3415: an exception thrown in the `std::call_once` callable is caught at depths 0, 1, 7 and 64, and every destructor on the way runs. The TechnicalDebt entry now describes the remaining gap (frames with handlers). ### Tested Windows 10 22H2 (19045), WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`, based on `main` c6efe068a: - `windows_exceptions` with the restored case: fails without the change (`[libc] unhandled exception 'St13runtime_error' ... what(): through runtime frames`), passes 3/3 with it. - Full `ctest` on an NVIDIA RTX 3090 (driver 610.47, `ANYPS5_GPU=3090`): 968 tests, 961 passed, 7 skipped (the video out tests without a display or Vulkan device, and `agc_driver_stencil_ref_export`, as this driver lacks `VK_EXT_shader_stencil_export`), 0 failed; `tools/check_skips.py --job windows` passes. - By hand, with a comparator that throws (not added as a test, since system DLL unwind data can change between Windows versions): through `bsearch` in `ucrtbase.dll` the exception is caught, where it was reported as unhandled without the change; through `qsort`, whose frame has a handler, it is reported as unhandled as before. - Linux and macOS are unchanged: the new code is under `_WIN32`. - `tools/check_conventions.py` and `tools/check_test_registration.py` pass. ### 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 - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-12 16:03:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T16:03:16Z.


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
#4570 core/libs/prx/libc/src/exception/Unwind.cpp L83-142
#4630 docs/dev/TechnicalDebt.md L485-486
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6127932358 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4511#issuecomment-6127932358). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T16:03:16Z. --- <!-- 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 | |---|---|---|---| | #4570 | `core/libs/prx/libc/src/exception/Unwind.cpp` [L83-142](https://github.com/boykopovar/AnyPS5/blob/4756c051eff1c702dbfc678ddba314b19770fae6/core/libs/prx/libc/src/exception/Unwind.cpp#L83-L142) | | | | #4630 | `docs/dev/TechnicalDebt.md` [L485-486](https://github.com/boykopovar/AnyPS5/blob/4756c051eff1c702dbfc678ddba314b19770fae6/docs/dev/TechnicalDebt.md#L485-L486) | | |
xXPBSXx commented 2026-10-12 16:27:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T16:27:21Z.


Windows test of head 74392900d merged onto current main (2a5eca11c, clean merge; built together with two other open PRs, so the run is for the combination): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build 0 errors, -j2.

windows_exceptions (the test this PR changes) passes. The 25 tests matching ^(guest_memory|guest_np_auth|windows_exceptions|guest_exception|guest_kernel) all pass.

Not checked: that the test fails without the change, Linux, a title run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6128328672 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4511#issuecomment-6128328672). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T16:27:21Z. --- Windows test of head `74392900d` merged onto current `main` (`2a5eca11c`, clean merge; built together with two other open PRs, so the run is for the combination): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build 0 errors, `-j2`. `windows_exceptions` (the test this PR changes) passes. The 25 tests matching `^(guest_memory|guest_np_auth|windows_exceptions|guest_exception|guest_kernel)` all pass. Not checked: that the test fails without the change, Linux, a title run. AI-assisted (Claude Code).
xXPBSXx commented 2026-10-12 16:31:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T16:31:34Z.


Windows test: head 74392900d merged onto current main 2a5eca11c (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j2: full build 0 errors; ctest with the filter windows_exception: windows_exceptions passed (1 test(s), 0 failed); python tools/check_conventions.py --base upstream/main passed. I did not run the new tests against main without the change.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6128392396 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4511#issuecomment-6128392396). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T16:31:34Z. --- Windows test: head `74392900d` merged onto current `main` `2a5eca11c` (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, `-j2`: full build 0 errors; ctest with the filter `windows_exception`: windows_exceptions passed (1 test(s), 0 failed); `python tools/check_conventions.py --base upstream/main` passed. I did not run the new tests against `main` without the change. AI-assisted (Claude Code).
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!4511
No description provided.