feat(libc): implement sceLibcBacktraceSelf and GetBufferSize on Windows #8137
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8137"
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 hamb3r on 2026-10-19T23:46:28Z.
What
During the startup and runtime of Unity Engine titles such as God of War: Sons of Sparta (PPSA28997), the IL2CPP runtime (
Il2cppUserAssemblies.prx) and Boehm garbage collector query stack backtraces viasceLibcBacktraceSelfandsceLibcBacktraceGetBufferSize.PR #7060 implemented these exports for Linux using
backtrace()anddladdr(), leaving other platforms callingNotImplemented_nid_no_patch(__func__). On Windows, attempting to capture a backtrace aborts the process immediately with an unhandled runtime error:This PR implements the missing Windows support for
CaptureFramesinlibc/src/Backtrace.cpp:_Unwind_Backtrace_nid_postfix/_Unwind_GetIP_nid_postfix), which unwinds relinked guest and AnyPS5.ehmeta/.ehframframes and steps through foreign PE frames without exception/termination handlers viaVirtualUnwind(Unwind.cpp:417).GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, ...)and queries the module path through internal helperBacktraceModulePath_nid_no_patch(handle, initialCapacity), which starts atMAX_PATHin production and grows exponentially up to 32 KiB onERROR_INSUFFICIENT_BUFFERorlength == capacity./\\) and trims the.guest.prxsuffix, producing the same clean module names as the Linux implementation.guest_backtrace(core/libs/tests/GuestBacktrace.cpp) to Windows (callingBacktraceModulePath_nid_no_patch(handle, 1)to exercise the buffer-growth loop directly without depending on a long checkout path) and registers it incore/libs/prx/libc/CMakeLists.txtrestricted toLinuxandWIN32.docs/dev/debt/libc.mdnoting that both Linux and Windows are supported and documenting that on Windows, unwinding stops at a frame without.ehmetaor.ehframtables whose PE unwind data has an exception or termination handler, leaving the trace empty or cut short while returning 0.Tested
Automated Test Suite:
guest_backtraceinctestpasses 100% on Windows (MinGW-w64 GCC 15, Release):guest_backtraceaborts with:Title Verification (God of War: Sons of Sparta, PPSA28997):
main+ this PR: BecauseIl2cppUserAssemblies.prximports 7 Dinkumware thread helpers directly fromlibc.prx(which onmainare currently exported only fromlibSceAmpr.prxvia #3743 / #3558), launching against standalonemainstops at the ELF import resolution stage before reaching guest code:libc.prxDinkumware thread imports resolved (tested together with #8148):Il2cppUserAssemblies.prxinvokessceLibcBacktraceSelfand terminates immediately withstd::runtime_error: sceLibcBacktraceSelf not implemented.sceLibcBacktraceSelfreturns 0 and populates valid frame records (Il2cppUserAssemblies.prx,libkernel.prx,libc.prx, etc.), allowing the title to pass engine initialization into menus and in-game rendering.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T23:53:05Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mdImported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T10:45:05Z.
Code review location:
core/libs/tests/GuestBacktrace.cpp; line 110The Windows assertion currently proves only that the first frame resolves to the same executable module. The Linux branch also checks the exact caller symbol (
caller.dli_saddr == self.dli_saddr), so it would catch an off-by-one in the skipped frames that this Windows branch could miss. Sincehandlecame from&CheckBacktrace, could this assert thatreinterpret_cast<std::uintptr_t>(handle) + records.front().offset == reinterpret_cast<std::uintptr_t>(&CheckBacktrace)as well?Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T11:29:52Z.
Head
57c8c0118merged ontomain02fb1ce53(clean merge).Windows 11 (10.0.26300), Ryzen 5 9600X, WinLibs GCC 15.2.0 UCRT, Release,
BUILD_TESTING=ON:guest_backtrace: passes 3 of 3. The build log has no warnings. WithBacktrace.cppreverted tomain, the test does not link (BacktraceModulePath_nid_no_patchis missing), so there is no run to compare against.dli_saddr). So I checked the offsets with a separate program. Its call chain ismain -> LevelOne -> LevelTwo -> LevelThree -> sceLibcBacktraceSelf, every functionnoinlineand without destructors. The program is linked against thislibc.prxand has no.ehmetatables.bt_probe.exe+0x181a(insideLevelThree, 0x1760 to 0x18f0),+0x18f9(LevelTwo),+0x1909(LevelOne),+0x924f(main), and+0x13ba(CRT startup). The count and size matchsceLibcBacktraceGetBufferSize.CreateThreadthread: the same first three frames, then the thread procedure, thenKERNEL32.DLL+0x2dda7.sceLibcBacktraceSelfheld astd::vector), both calls return 0 withcount = 0and no records. The libc unwinder stops at a frame whose PE unwind data has a handler, which is the known limit indebt/libc.md. TheRtlCaptureStackBackTracefallback runs only when the unwind returns zero frames, so it did not take over here. Relinked guest frames carry the project's tables, so this probably affects only native callers. Still, a silent empty trace is not recorded in the debt entry: falling back whenever no frame is left after the two skipped ones, or a line in the debt entry, would cover it.libc/CMakeLists.txthunk adds two blank lines afterBinaryWordIo.cppthat are unrelated to the change.Ubuntu 24.04 (WSL2), GCC 13.3, Release, same merge:
guest_backtracepasses 3 of 3 and the build has no warnings. The Linux path is unchanged.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by hamb3r on 2026-10-20T11:50:03Z.
Thanks @MarcZX10 and @wachi35676 for the review and catches! Addressed in the latest commit:
core/libs/tests/GuestBacktrace.cpp: On Windows, bracket thesceLibcBacktraceSelf_nid_postfixcall with a[[gnu::noinline]]CurrentIp()helper and verify thatrecords.front().offsetlies strictly betweenbeforeandafterinsideCheckBacktrace, and thatrecords[1].offsetmatches__builtin_return_address(0)(the caller ofCheckBacktraceinmain).core/libs/prx/libc/src/Backtrace.cpp: Changed theRtlCaptureStackBackTracefallback check fromif (captured == 0)toif (captured <= 2)so that if_Unwind_Backtrace_nid_postfixstops at a caller frame after only the two skipped internallibcframes (CaptureFramesandsceLibcBacktrace*),RtlCaptureStackBackTracetakes over instead of returning an empty frame list.core/libs/prx/libc/CMakeLists.txt: Removed the two stray blank lines afterBinaryWordIo.cpp.Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T12:11:08Z.
Re-ran
28d0b1664merged ontomain02fb1ce53(clean merge; also merges cleanly ontod50ec76da). Windows 11, WinLibs GCC 15.2.0, and Ubuntu 24.04 (WSL2) GCC 13.3, both Release.guest_backtrace: passes 3 of 3 on each OS, with no warnings in either build log. On Windows the newCurrentIp()range check and therecords[1]return-address check both hold. WithBacktrace.cppatmain, the Windows test can't load (0xc0000139,BacktraceModulePath_nid_no_patchis missing), and the Linux test still passes because its path is unchanged.Backtrace.cppset back to57c8c0118, the test still passes, so thecaptured <= 2change is untested.The case of a native caller with a C++ cleanup frame is unchanged. I re-ran the probe 3 times, and the output was identical with
Backtrace.cppat57c8c0118and at28d0b1664:sceLibcBacktraceSelfholds astd::vector, the call returns 0 withcount = 0.RtlCaptureStackBackTraceis called fromCaptureFrames), but it returns 2. Both of those frames are then skipped.libc.prxis built with-fno-asynchronous-unwind-tables, and its.pdatahas no entry forCaptureFramesorsceLibcBacktraceSelf_nid_postfix. So the system unwinder can't step out ofCaptureFrames, and the fallback can't give caller frames from this build.So the change doesn't fix the empty trace. Building
Backtrace.cppalone with-fasynchronous-unwind-tablesdoesn't assemble with this toolchain (Error: .seh_handlerdata used outside of .seh_proc block), so it isn't a one-line fix. A line in thedebt/libc.mdentry would cover it: behind a frame with a handler, the trace is empty or cut short, and the call still returns success. The stray blank lines inlibc/CMakeLists.txtare gone.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by hamb3r on 2026-10-20T12:23:03Z.
Thanks @wachi35676 for digging into that in
gdb! Sincelibc.prxis built with-fno-asynchronous-unwind-tables(.ehframwithout.pdataentries forCaptureFrames/sceLibcBacktrace),RtlCaptureStackBackTraceindeed cannot step out ofCaptureFrames. Removed the unreachableRtlCaptureStackBackTracefallback fromBacktrace.cppand documented the PE unwind handler behavior indocs/dev/debt/libc.md.Imported from boykopovar/AnyPS5. Originally posted by hamb3r on 2026-10-20T12:48:37Z.
Code review location:
core/libs/tests/GuestBacktrace.cpp; line 110Fixed in
28d0b1664b: sincerecords.front().offsetis the call-site return RVA insideCheckBacktracerather than the function entry&CheckBacktrace, the Windows branch now bracketssceLibcBacktraceSelf_nid_postfixwithCurrentIp()(address > before && address < after) and checks thatrecords[1].offsetmatches__builtin_return_address(0).Imported from boykopovar/AnyPS5. Originally posted by hamb3r on 2026-10-20T19:06:58Z.
Rebased / synced with
main(50a4d5bfb3). cc @oneandonlydean