Repository navigation
fix(libkernel): end guest thread, fiber and context frame chains at a zero record - #7123
Conversation
… zero record
Unreal Engine walks the frame-pointer chain until it reads a return
address of 0 and never checks the frame pointer itself. Guest threads
started with whatever rbp the host code held, so the walk ran into host
start-up frames (glibc's saved rbp 0x21 in a LEGO Batman core dump,
address 8 in Returnal on Windows) and faulted. Fibers ended their chain
with a null frame, and makecontext started its function with rbp 0, so
the same walker read address 8 there too.
Guest threads now start through a thunk that points rbp at a
{saved rbp 0, return address 0} record, as the relinker's entry stub
does for the main thread; the fiber trampolines push the same record,
and the makecontext start function is built with frame pointers so its
frame, over the zero return address makecontext stores, ends the chain.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
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.
|
jotitadev
left a comment
There was a problem hiding this comment.
Linux run, since the description notes the change was tested on Windows only.
Machine: Linux x86_64, i7-8700, Ubuntu 26.04, GCC 15.2, Release, BUILD_TESTING=ON, full build.
Head ff9bf2228 merged onto main 40315fcf8 (clean merge).
guest_pthread_frame_chain,fiberandguest_user_contextpass with the PR. The thread, fiber and context tests (ctest -R "frame_chain|fiber|user_context|pthread|thread", 45 tests) all pass.- The three test files and the test registration in
libkernel/CMakeLists.txtonly, onmain(no thunk, no fiber change,UserContext.cppbuilt as onmain): all three abort, with the same messages as on Windows:guest_pthread_frame_chain:the frame chain of a guest thread stays on its stack and climbs itfiber:frame chain climbs the stack to a frame whose return address is 0guest_user_context:the frame chain of a made context ends on its stack at a frame whose saved frame pointer and return address are 0
- Non-GPU suite (
ctest -E "^agc|gpu|vulkan|video_out"): 683/683, the 682 tests ofmainplusguest_pthread_frame_chain.
Not checked: the titles from #4239.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
|
Title check on LEGO Batman: Legacy of the Dark Knight (PPSA16833), Linux, RTX 4090,
With this PR the run reached an Unreal Tested and written by Claude (AI assistant) on a contributor's machine. |
What
Unreal Engine walks the frame-pointer chain to capture call stacks (for
ensure(), hitch reports and fatal errors): it reads the return address at[rbp + 8], followsrbp = [rbp], and stops when the return address is 0. It never checks the frame pointer itself, so the walk only ends cleanly at a frame record whose return address is 0. The relinker's entry stub gives the main thread such a record (#2339), but guest threads did not get one:RunThreadcalled the guest thread entry directly, so the entry's savedrbpwas whatever the host code left in it. On Linux, Tamalero's LEGO Batman core dump on Returnal (PPSA01285) on Windows: intermittent fault in a frame-pointer stack walk at app.exe+0x337aa9d (UE4 Main Thread, read of 0x8) #4239 shows the chain running from the guest entry intoStartNativeThreadand then to glibc's frame with savedrbp0x21; on Windows, Returnal's walk on itsUE4 Main Threadreads address0x8(Returnal (PPSA01285) on Windows: intermittent fault in a frame-pointer stack walk at app.exe+0x337aa9d (UE4 Main Thread, read of 0x8) #4239). Both titles fault inside the walker.Aps5FiberMain's frame has a savedrbpof 0 but a non-zero return address, so the same walker reads[0 + 8]from inside a fiber.makecontextstarts its function withrbp0 throughAps5MakecontextStart_nid_no_patch, which is built without frame pointers, so a walk from a made context reads[0 + 8]as well.Guest threads now start through
Aps5CallGuestEntry_nid_no_patch, a small thunk next toRunThreadthat pushes a{saved rbp 0, return address 0}record, pointsrbpat it and calls the entry; the fiber trampolines push the same record before callingAps5FiberMain; andUserContext.cppis built with frame pointers, soAps5MakecontextStart_nid_no_patch's own frame, over the zero return addressmakecontextalready stores, is that record. The walk then ends at the record, as it does on the main thread. The thunk issysv_abi, so on Windows the compiler saves the registers the guest may clobber around the call, as it did for the direct call; it keepsrsp16-byte aligned at the call and has CFI for unwinders. Neither thread exit path runs through it: Windows exits with_endthreadex, POSIXlongjmps back toStartNativeThread.Not covered here: module initializers that the Windows entry stub runs before it writes its record, guest
signal()handlers that run on threads the OS creates, and guest callbacks reached through host code that does not keep a frame pointer (for examplelibSceJobManager's worker loop) can still see host frames in their chain.Tested
Windows 11 26200, Ryzen 5 5600X, WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON:guest_pthread_frame_chain: a guest thread walks its chain the way Unreal does (bounded to its own stack, required to climb) and must end at a{0, 0}record after the walker's and the entry's frames; it also checks that the entry gets its argument and its return value reachesscePthreadJointhrough the thunk. Onmainit fails:the frame chain of a guest thread stays on its stack and climbs it.fiber's frame-chain check now walks the same way and requires the{0, 0}record instead of a null frame. Onmainit fails:frame chain climbs the stack to a frame whose return address is 0.guest_user_contextnow walks the chain from the made context's function, bounded to the context's stack, and requires the record. WithUserContext.cppbuilt as onmainit fails:the frame chain of a made context ends on its stack at a frame whose saved frame pointer and return address are 0.thread,guest_pthread, user context and fiber tests (44 in all).ctest --parallel 4 --timeout 120onmaine09dbc2b4with this change (mainhas since moved toc9e6c45dewithout touching these files): 1384 tests;agc_driver_lds_mixed_width_atomics(agc_* tests: 3 of 483 fail on AMD Radeon RX 6400, pass on NVIDIA RTX 4080 SUPER #5271, fixed by fix(shader): release the LDS lock with an exchange and drop fences inside it #6976) andagc_driver_layered_volume_vertex_no_gpl(AMD Windows: GPL-mode cache failures (alpha/wave32 pass after local driver update) #4768) fail as onmainhere. Six Python-driven tests hit their 30 s timeout, which happens on this PC only (slow Python start-up); theconventions_function_bodiesshards pass when rerun on their own.python tools/check_conventions.py --base origin/mainwith this title and body passes.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)🤖 Generated with Claude Code