perf(libc): index .eh_frame and find unwind modules without a syscall #1423

Merged
gjonn merged 2 commits from upstream-pr/1423 into main 2026-10-11 09:16:13 +00:00
gjonn commented 2026-10-08 04:12:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gjonn on 2026-10-08T04:12:39Z.


What

On Windows a C++ exception thrown in a host module unwinds through libc.prx's LibcUnwind. Two steps of DecodeFrame made each throw expensive:

  • Module lookup: every frame called VirtualQuery to find its image, a system call per frame.
  • FDE search: a module with a plain .eh_frame and no .ehmeta index (the MinGW runtime DLLs libstdc++-6.dll, libgcc_s_seh-1.dll and libwinpthread-1.dll, whose frames most host exceptions cross) was searched by walking its .eh_frame from the start and fully decoding each FDE (CIE, augmentation, pointer encodings) until one matched.

A throw from the AGC driver (for example a draw rejected while its resources are built) therefore took about 1 ms. A title that issues many such draws per frame spent most of its GPU worker time in the unwinder.

  • DecodeFrame first asks the loader with RtlPcToFileHeader, which walks the loader's module list without entering the kernel. It falls back to VirtualQuery for images the loader does not know, as before.
  • A module's .eh_frame is indexed once, on first use, into FDE start addresses sorted for binary search. The index lives in a per-module cache keyed by the section address and guarded by a mutex. The FDE before the pc is then decoded and its range checked as before. .ehmeta modules keep their existing binary search.

Tested

  • Windows 11, MinGW-w64 GCC 15.2.0: windows_exception_tests ("typed catch, base catch, catch-all, rethrow, destructors, cross DLL passed"), guest_exception_handler and guest_raise_exception pass.
  • A title whose draws are mostly rejected, sampled from outside the process with a stack sampler during the same scene:
    • before, about 75% of the GPU worker's samples were in LibcUnwind::DecodeCandidate and DecodeFrame;
    • with the index only, 55% were in ZwQueryVirtualMemory, called from DecodeFrame;
    • with both changes, the unwinder no longer stands out.
  • The driver's draw profile (APS5_PROFILE_DRAW) for that scene:
    • time per rejected draw went from 1170 us to about 156 us;
    • draw packets processed per 10 s went from about 8 100 to up to 56 700;
    • presents per 10 s went from 3-4 to 30-70.

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:4781659735 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1423). Originally posted by [gjonn](https://github.com/gjonn) on 2026-10-08T04:12:39Z. --- ### What On Windows a C++ exception thrown in a host module unwinds through `libc.prx`'s `LibcUnwind`. Two steps of `DecodeFrame` made each throw expensive: - **Module lookup:** every frame called `VirtualQuery` to find its image, a system call per frame. - **FDE search:** a module with a plain `.eh_frame` and no `.ehmeta` index (the MinGW runtime DLLs `libstdc++-6.dll`, `libgcc_s_seh-1.dll` and `libwinpthread-1.dll`, whose frames most host exceptions cross) was searched by walking its `.eh_frame` from the start and fully decoding each FDE (CIE, augmentation, pointer encodings) until one matched. A throw from the AGC driver (for example a draw rejected while its resources are built) therefore took about 1 ms. A title that issues many such draws per frame spent most of its GPU worker time in the unwinder. - `DecodeFrame` first asks the loader with `RtlPcToFileHeader`, which walks the loader's module list without entering the kernel. It falls back to `VirtualQuery` for images the loader does not know, as before. - A module's `.eh_frame` is indexed once, on first use, into FDE start addresses sorted for binary search. The index lives in a per-module cache keyed by the section address and guarded by a mutex. The FDE before the pc is then decoded and its range checked as before. `.ehmeta` modules keep their existing binary search. ### Tested - Windows 11, MinGW-w64 GCC 15.2.0: `windows_exception_tests` ("typed catch, base catch, catch-all, rethrow, destructors, cross DLL passed"), `guest_exception_handler` and `guest_raise_exception` pass. - A title whose draws are mostly rejected, sampled from outside the process with a stack sampler during the same scene: - before, about 75% of the GPU worker's samples were in `LibcUnwind::DecodeCandidate` and `DecodeFrame`; - with the index only, 55% were in `ZwQueryVirtualMemory`, called from `DecodeFrame`; - with both changes, the unwinder no longer stands out. - The driver's draw profile (`APS5_PROFILE_DRAW`) for that scene: - time per rejected draw went from 1170 us to about 156 us; - draw packets processed per 10 s went from about 8 100 to up to 56 700; - presents per 10 s went from 3-4 to 30-70. ### 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
oneandonlydean commented 2026-10-08 05:13:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-08T05:13:00Z.


DecodeFrame tries RtlPcToFileHeader and falls back to VirtualQuery when it fails (Unwind.cpp:186-191). Fallbacks aren't allowed here. Which images does the loader not know? If none, drop the VirtualQuery path; if some do, pick the lookup for them up front.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6052802992 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1423#issuecomment-6052802992). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-08T05:13:00Z. --- `DecodeFrame` tries `RtlPcToFileHeader` and falls back to `VirtualQuery` when it fails (`Unwind.cpp:186-191`). Fallbacks aren't allowed here. Which images does the loader not know? If none, drop the `VirtualQuery` path; if some do, pick the lookup for them up front.
adriadam10 commented 2026-10-09 23:22:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T23:22:18Z.

Review: CHANGES_REQUESTED


Thanks for the numbers. Points to address:

  • Unwind.cpp:213-218: VirtualQuery is kept as a fallback when RtlPcToFileHeader fails, which is what @oneandonlydean asked about. Which images does the loader not know? If none, drop the VirtualQuery path; if some do (e.g. manually mapped modules), pick the lookup for them up front instead of falling back.
  • Unwind.cpp:154-179: the index cache is keyed only by the .eh_frame address and never invalidated. If a DLL is unloaded and another one is mapped at the same address, the stale index is used. Key it on something that changes with the module (e.g. image base + size/timestamp) or drop entries on unload.
  • Unwind.cpp:169-173: the index is built by calling DecodeCandidate with pc = 0 and text = data = 0. That only works because the function sets start/length before returning false on the range check (line 135), and textrel/datarel pointer encodings would give wrong starts. Split the start/length decoding out of DecodeCandidate (or pass the real text/data bases) so the index doesn't rely on a failed return.
  • Please add a case to windows_exception_tests that throws through a plain-.eh_frame module at several depths/addresses, so the index lookup (including a pc just before the first FDE and just past the last one) is pinned by a test.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5476283493 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1423#pullrequestreview-5476283493). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-09T23:22:18Z. **Review:** CHANGES_REQUESTED --- Thanks for the numbers. Points to address: - `Unwind.cpp:213-218`: `VirtualQuery` is kept as a fallback when `RtlPcToFileHeader` fails, which is what @oneandonlydean asked about. Which images does the loader not know? If none, drop the `VirtualQuery` path; if some do (e.g. manually mapped modules), pick the lookup for them up front instead of falling back. - `Unwind.cpp:154-179`: the index cache is keyed only by the `.eh_frame` address and never invalidated. If a DLL is unloaded and another one is mapped at the same address, the stale index is used. Key it on something that changes with the module (e.g. image base + size/timestamp) or drop entries on unload. - `Unwind.cpp:169-173`: the index is built by calling `DecodeCandidate` with `pc = 0` and `text = data = 0`. That only works because the function sets `start`/`length` before returning false on the range check (line 135), and `textrel`/`datarel` pointer encodings would give wrong starts. Split the start/length decoding out of `DecodeCandidate` (or pass the real text/data bases) so the index doesn't rely on a failed return. - Please add a case to `windows_exception_tests` that throws through a plain-`.eh_frame` module at several depths/addresses, so the index lookup (including a pc just before the first FDE and just past the last one) is pinned by a test.
adriadam10 commented 2026-10-11 09:12:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T09:12:16Z.

Review: APPROVED


Thanks for the numbers and the profile. The last review's points had no push in 24 h, so I fixed them on your branch (rebased on current main):

  • Unwind.cpp: dropped the VirtualQuery fallback. Every image is loaded through LoadLibraryExW, so RtlPcToFileHeader knows all of them; a pc it doesn't know is not in a module and returns false.
  • Unwind.cpp: the .eh_frame index cache is now keyed on image base, TimeDateStamp, CheckSum, SizeOfImage and the section's address/size, so another DLL mapped at the same address gets its own index.
  • Unwind.cpp: the FDE start/length decoding is split out of DecodeCandidate into DecodeRange; the index no longer relies on a failed return.
  • WindowsExceptions.cpp: new case that throws through std::call_once (libwinpthread/libstdc++ frames) at depths 0, 1, 7 and 64 and checks the destructors run.

Checked: libs/libc build and ctest on Linux. The Windows branch can't be built here; merging once the full Build's Windows job is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5482705940 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1423#pullrequestreview-5482705940). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T09:12:16Z. **Review:** APPROVED --- Thanks for the numbers and the profile. The last review's points had no push in 24 h, so I fixed them on your branch (rebased on current `main`): - `Unwind.cpp`: dropped the `VirtualQuery` fallback. Every image is loaded through `LoadLibraryExW`, so `RtlPcToFileHeader` knows all of them; a pc it doesn't know is not in a module and returns false. - `Unwind.cpp`: the `.eh_frame` index cache is now keyed on image base, `TimeDateStamp`, `CheckSum`, `SizeOfImage` and the section's address/size, so another DLL mapped at the same address gets its own index. - `Unwind.cpp`: the FDE start/length decoding is split out of `DecodeCandidate` into `DecodeRange`; the index no longer relies on a failed return. - `WindowsExceptions.cpp`: new case that throws through `std::call_once` (libwinpthread/libstdc++ frames) at depths 0, 1, 7 and 64 and checks the destructors run. Checked: libs/libc build and ctest on Linux. The Windows branch can't be built here; merging once the full Build's Windows job is green.
orishamir commented 2026-10-11 09:40:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orishamir on 2026-10-11T09:40:53Z.


The new testUnwindThroughRuntimeModules check in windows_exceptions fails on Windows here, every run (8/8), with the merge commit 3ac63a6d4 on main 09bd4aa1:

[libc] unhandled exception 'St13runtime_error' thrown from 00007ffc1e69ac46
terminate called after throwing an instance of 'std::runtime_error'
  what():  through runtime frames
FATAL: abort() (SIGABRT)

Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7 (UCRT, POSIX threads, SEH), Release, BUILD_TESTING=ON, the CI windows job's configure flags. The old checks in the same test passed here on main 262c0316 last night (719/719).

It is not the fallback removal: with Unwind.cpp from 3ac63a6d4^ (the VirtualQuery fallback back in) and the new check kept, the same line fails 3/3; restored, 1/1 fails again. So the exception thrown inside std::call_once's callable is not caught by the catch in the test frame with either version; the unwinder loses the chain in the runtime DLL frames (libwinpthread-1.dll/libstdc++-6.dll for call_once) on this machine. CI agrees: the first windows job on a main that contains the commit, run 38128072167 on 4c9686fe, fails exactly this test and nothing else (821/822) with the same what(): through runtime frames, so main's windows job is red since 09:16Z.

AI-assisted: yes (Claude Code Fable 5.1 xhigh)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6107681179 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1423#issuecomment-6107681179). Originally posted by [orishamir](https://github.com/orishamir) on 2026-10-11T09:40:53Z. --- The new `testUnwindThroughRuntimeModules` check in `windows_exceptions` fails on Windows here, every run (8/8), with the merge commit 3ac63a6d4 on `main` 09bd4aa1: ``` [libc] unhandled exception 'St13runtime_error' thrown from 00007ffc1e69ac46 terminate called after throwing an instance of 'std::runtime_error' what(): through runtime frames FATAL: abort() (SIGABRT) ``` Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7 (UCRT, POSIX threads, SEH), Release, `BUILD_TESTING=ON`, the CI `windows` job's configure flags. The old checks in the same test passed here on `main` 262c0316 last night (719/719). It is not the fallback removal: with `Unwind.cpp` from 3ac63a6d4^ (the `VirtualQuery` fallback back in) and the new check kept, the same line fails 3/3; restored, 1/1 fails again. So the exception thrown inside `std::call_once`'s callable is not caught by the `catch` in the test frame with either version; the unwinder loses the chain in the runtime DLL frames (`libwinpthread-1.dll`/`libstdc++-6.dll` for `call_once`) on this machine. CI agrees: the first `windows` job on a `main` that contains the commit, [run 38128072167](https://github.com/boykopovar/AnyPS5/actions/runs/38128072167) on 4c9686fe, fails exactly this test and nothing else (821/822) with the same `what(): through runtime frames`, so `main`'s `windows` job is red since 09:16Z. AI-assisted: yes (Claude Code Fable 5.1 xhigh)
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!1423
No description provided.