fix(libkernel): end unwinding at the guest main stack's entry frame #15691
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/15691"
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 GiannisG1 on 2026-11-04T06:35:43Z.
What
Since #7532,
EnterGuestMainStack_nid_no_patchmoves the stack pointer to the guest main stack and calls the guest entry from inline asm (Thread.cppL263-L274). The compiler's unwind information forEnterGuestMainStackstill describes its frame on the host stack. An unwinder that returns into it from the guest entry computes a CFA past the top of the guest main stack and reads there. libc's_Unwind_Backtracefaults inLibcUnwind::Stepat0x7eeffc0e8,0xe8above the console's main stack top. Onmain04a9eb4fa2, vkQuake (PPSA99010) and RetroArch (PPSA99169) crash this way at startup. The same walk runs when a C++ exception is thrown on the guest main thread (_Unwind_RaiseException), so an unimplemented call reached there ends in that fault instead of naming the call. Before #7532, vkQuake exited with 0 and RetroArch stopped atsceSystemServiceLoadExecon this PC.The stack switch is now a routine of its own,
Aps5CallOnGuestMainStack_nid_no_patch, whose unwind information marks the return address undefined (.cfi_undefined %rip), as glibc's_start(start.SL57-L59: "Clearing frame pointer is insufficient, use CFI.") andclone(clone.SL92-L94) mark their outermost frames. libc's unwinder readsDW_CFA_undefinedas a return address of 0 (Unwind.hppL215,Unwind.cppL654) and stops there (L676), so a walk from the guest main stack ends at the entry with_URC_END_OF_STACK. gdb's backtrace ends there too. The routine runs the instructions the inline asm ran.guest_main_stack(both modes) now calls_Unwind_Backtracefrom the entry on the guest main stack. It checks that the walk ends with_URC_END_OF_STACK, passes at least the entry and the routine, that no frame's CFA is above the stack top, and thatdladdrnamesAps5CallOnGuestMainStack_nid_no_patchfor the outermost frame. The inline asm can't meet that last check in a frame-pointer build either: there its walk stops cleanly atEnterGuestMainStack_nid_no_patch.Landing order: #15803 (with #15804 and #15807 stacked on it) wraps
EnterGuestMainStack_nid_no_patchinANYPS5_GUEST_LIFTEDon the same lines. How the two compose is on #15803 (https://github.com/boykopovar/AnyPS5/pull/15803#issuecomment-6700921364); whichever lands second rebases.Tested
Linux Mint 22.3, kernel 7.0.0-38, glibc 2.39, GCC 13.3.0, Release,
BUILD_TESTING=ON, head onmain04a9eb4fa2(merges cleanly with currentmain):guest_main_stackandguest_main_stack_adopted: 3/3 each. On04a9eb4fa2both fail 3/3 with a segfault inLibcUnwind::Step, at the instruction where the titles crash.-fno-omit-frame-pointer(as the ASan and fuzzer builds use), on04a9eb4fa2'sThread.cpp: this PR's first version of the test passed 6/6, because the walk ends atEnterGuestMainStack_nid_no_patchthere, as gabichuuuuu pointed out. With the outermost-frame check it fails 6/6, and with this PR it passes 6/6. Default flags: 6/6 with this PR.readelf --debug-dump=frames: the routine's CIE ends withDW_CFA_undefined: r16 (rip).04a9eb4fa2's relinker, GTX 1050 Ti under Xvfb: with04a9eb4fa2'slibkernel.prxboth segfault inLibcUnwind::Stepafter 1 s and 2 s. With thislibkernel.prx, vkQuake exits with 0 and RetroArch stops atsceSystemServiceLoadExec: /app0/picker/picker.bin without its converted executable, as before #7532.ps5-v0.1.0) onmain85529b7483with this commit cherry-picked, GTX 1050 Ti under Xvfb: on85529b7483it segfaults inLibcUnwind::Stepunder_Unwind_RaiseExceptionand__cxa_throw, thrown byNotImplementedforsceVideoOutGetCurrentOutputMode_on the main thread. With thislibkernel.prxit aborts withsceVideoOutGetCurrentOutputMode_ not implemented.LibcUnwind::Stepat0x7eeffc0e8onmain41614f0558whensceHttp2SendRequestAsyncthrows, one step pastEnterGuestMainStack_nid_no_patch, and ends withsceHttp2SendRequestAsync not implementedwith this PR (comment).tools/suite.pysplit run against a--fullrun of04a9eb4fa2: all 3828 tests on the GTX 1050 Ti, and the 932 that depend on the GPU on the HD Graphics 630 and llvmpipe. No new failure and no new skip; the HD Graphics 630 failsagc_driver_f64_rounding_denormal_modesandagc_driver_image_sample_lod_clampon04a9eb4fa2too (Mesa).check_conventions.py,check_test_registration.py,check_main_sync.pyandcheck_compile.pyare clean.pr_overlap.py --branch: the 28 open PRs that share files merge cleanly with this one, and the 9 it could not compare (they conflict withmain) change other functions ofThread.cpp.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)LibcUnwind::Steppast the guest main stack top, per milescoviello's runs on an RTX 4070 Ti (https://github.com/boykopovar/AnyPS5/pull/15691#issuecomment-6697045295); PPSA99010 (vkQuake) and PPSA99169 (RetroArch), catalog homebrew, back to their state before #7532🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-11-06T07:29:35Z.
Review: APPROVED
Checked head
3109f4f6daa4d806fd6ba0143bd51063776ef66f; PR base5b1d381d5ad5d9f923326b18479c3bd889cdd588. Control: parent04a9eb4fa2877eb6e40bf2c83adf972e40d89b24.Linux builds and both
guest_main_stacktests pass. I also ran a separate host libgcc probe through the actualEnterGuestMainStackcall, in bounded initial/adopted child processes.dladdrconfirms all three unwind APIs come fromlibgcc_s.so.1(14.2).Both head cases preserve the arguments and visit three nested guest frames, then
Aps5CallOnGuestMainStack, then zero IP. They return_URC_END_OF_STACK; every CFA stays inside the guest stack. Both parent controls reachEnterGuestMainStack, then die with SIGSEGV.The emitted FDE covers
[0xbf8f0, 0xbf903), including return PC0xbf901. Its CIE marks RIP undefined, consistent with GCC's outermost-frame rule and glibc startup code. Conventions pass.No findings in this Linux boundary change. No title, console, or Windows/macOS runtime claim.
Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T07:27:31Z.
Review: COMMENTED
A Windows check, and a question about what the new check covers.
#if defined(__linux__)inThread.cpp(L138–348), andguest_main_stack_testsis registered only whenCMAKE_SYSTEM_NAMEis Linux, so neither runs on Windows or macOS. On Windows 11 x64 (WinLibs GCC 15.2.0 r7 UCRT, Release) I merged head3109f4f6ontomainc1e920e7and ran the libkernel context, exception and unwind tests (ctest -R '^(guest_main_stack|guest_user_context|guest_.*exception|guest_.*unwind|guest_.*context)'). That is 171 tests, none of themguest_main_stack, and they pass onmainand with this PR.ANYPS5_ENABLE_ASANand the fuzzer build add-fno-omit-frame-pointer(top-levelCMakeLists.txtL110, L119), and-O0keeps it as well. The old inline asm leftrbpattop-16after its twopushq $0. With a frame pointer,EnterGuestMainStack's CFA would berbp+16, which istop, and its return slot would hold the pushed 0.LibcUnwind::Stepstops at a zero return address (Unwind.cppL636), so the walk would end in_URC_END_OF_STACKwithhighestCfa == high, and all threeRequires would pass without the fix. If that is right, a check the old code cannot meet would also cover those builds, for example requiring the last frame's IP to lie inAps5CallOnGuestMainStack_nid_no_patch.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T10:45:47Z.
Review: COMMENTED
Follow-up on my earlier comment, read against head
24babea951. Not run here: the change andguest_main_stackare Linux-only, and I have no Linux build.24babea951answers my question. The walk now has to end in a frame thatdladdrnamesAps5CallOnGuestMainStack_nid_no_patch. The old inline asm can't meet that in a frame-pointer build, where the walk stops cleanly atEnterGuestMainStack_nid_no_patch, and your-fno-omit-frame-pointerruns show it: 6/6 red on04a9eb4fa2'sThread.cpp, 6/6 green with this PR. The commit only touchesGuestMainStack.cpp, so my earlier Windows result for3109f4f6(171 context, exception and unwind tests, same onmainand the PR) still applies.Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-08T15:16:28Z.
Review: APPROVED
What I ran
24babea951399f0e08b92bf2d294212f76e661bf04a9eb4fa2877eb6e40bf2c83adf972e40d89b24main(828e44310ec98912f041d0e51b8adc809848741b, 0 conflicts).Tools and Checks
python tools/check_conventions.py --base upstream/main --head pr-15691: 0 errors.git diff --check upstream/main...pr-15691: clean (no trailing whitespace or line ending issues).python tools/pr_overlap.py --branch pr-15691: probed 1836 open pull requests against main; 24 shareThread.cpp. 10 merge cleanly, 14 stale branches conflict withmainon unrelated lines.Windows Compilation & ABI Verification
libkernelbuilt cleanly (core/libs/prx/libkernel/CMakeFiles/libkernel.dir/Pthread/src/Thread.cpp.obj).Aps5CallOnGuestMainStack_nid_no_patch:APS5_VABIcalling convention passes arguments in%rdi(top),%rsi(arguments), and%rdx(entry).mov %rdi, %rspfollowed by two 8-byte pushes (push $0; push $0) preserves standard 16-byte alignment atcall *%rdx.old %rbp == 0,return address == 0) terminating frame pointer-based walks..cfi_undefined %ripcorrectly instructs the DWARF unwinder to terminate the backtrace with_URC_END_OF_STACK.APS5_ASM_FUNCTIONmacro generates proper symbol definitions on Windows MinGW without assembly warnings.Windows Regression Tests
guest_kernel_is_stackunwind_record_boundsguest_exception_handlerguest_raise_exceptionguest_pthread_attr_stackguest_pthread_frame_chainguest_pthread_start(10,000 thread starts)guest_pthread_stackLGTM!