perf(libc): index .eh_frame and find unwind modules without a syscall #1423
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1423"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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'sLibcUnwind. Two steps ofDecodeFramemade each throw expensive:VirtualQueryto find its image, a system call per frame..eh_frameand no.ehmetaindex (the MinGW runtime DLLslibstdc++-6.dll,libgcc_s_seh-1.dllandlibwinpthread-1.dll, whose frames most host exceptions cross) was searched by walking its.eh_framefrom 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.
DecodeFramefirst asks the loader withRtlPcToFileHeader, which walks the loader's module list without entering the kernel. It falls back toVirtualQueryfor images the loader does not know, as before..eh_frameis 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..ehmetamodules keep their existing binary search.Tested
windows_exception_tests("typed catch, base catch, catch-all, rethrow, destructors, cross DLL passed"),guest_exception_handlerandguest_raise_exceptionpass.LibcUnwind::DecodeCandidateandDecodeFrame;ZwQueryVirtualMemory, called fromDecodeFrame;APS5_PROFILE_DRAW) for that scene:Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-08T05:13:00Z.
DecodeFrametriesRtlPcToFileHeaderand falls back toVirtualQuerywhen it fails (Unwind.cpp:186-191). Fallbacks aren't allowed here. Which images does the loader not know? If none, drop theVirtualQuerypath; if some do, pick the lookup for them up front.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:VirtualQueryis kept as a fallback whenRtlPcToFileHeaderfails, which is what @oneandonlydean asked about. Which images does the loader not know? If none, drop theVirtualQuerypath; 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_frameaddress 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 callingDecodeCandidatewithpc = 0andtext = data = 0. That only works because the function setsstart/lengthbefore returning false on the range check (line 135), andtextrel/datarelpointer encodings would give wrong starts. Split the start/length decoding out ofDecodeCandidate(or pass the real text/data bases) so the index doesn't rely on a failed return.windows_exception_teststhat throws through a plain-.eh_framemodule 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.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 theVirtualQueryfallback. Every image is loaded throughLoadLibraryExW, soRtlPcToFileHeaderknows all of them; a pc it doesn't know is not in a module and returns false.Unwind.cpp: the.eh_frameindex cache is now keyed on image base,TimeDateStamp,CheckSum,SizeOfImageand 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 ofDecodeCandidateintoDecodeRange; the index no longer relies on a failed return.WindowsExceptions.cpp: new case that throws throughstd::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.
Imported from boykopovar/AnyPS5. Originally posted by orishamir on 2026-10-11T09:40:53Z.
The new
testUnwindThroughRuntimeModulescheck inwindows_exceptionsfails on Windows here, every run (8/8), with the merge commit3ac63a6d4onmain09bd4aa1:Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7 (UCRT, POSIX threads, SEH), Release,
BUILD_TESTING=ON, the CIwindowsjob's configure flags. The old checks in the same test passed here onmain262c0316last night (719/719).It is not the fallback removal: with
Unwind.cppfrom3ac63a6d4^ (theVirtualQueryfallback back in) and the new check kept, the same line fails 3/3; restored, 1/1 fails again. So the exception thrown insidestd::call_once's callable is not caught by thecatchin the test frame with either version; the unwinder loses the chain in the runtime DLL frames (libwinpthread-1.dll/libstdc++-6.dllforcall_once) on this machine. CI agrees: the firstwindowsjob on amainthat contains the commit, run 38128072167 on4c9686fe, fails exactly this test and nothing else (821/822) with the samewhat(): through runtime frames, somain'swindowsjob is red since 09:16Z.AI-assisted: yes (Claude Code Fable 5.1 xhigh)