Skip to content

fix(libkernel): end guest thread, fiber and context frame chains at a zero record - #7123

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
thuanquang:fix/guest-frame-chain-zero-record
Oct 18, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
thuanquang:fix/guest-frame-chain-zero-record

Conversation

@thuanquang

Copy link
Copy Markdown
Contributor

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], follows rbp = [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:

Guest threads now start through Aps5CallGuestEntry_nid_no_patch, a small thunk next to RunThread that pushes a {saved rbp 0, return address 0} record, points rbp at it and calls the entry; the fiber trampolines push the same record before calling Aps5FiberMain; and UserContext.cpp is built with frame pointers, so Aps5MakecontextStart_nid_no_patch's own frame, over the zero return address makecontext already stores, is that record. The walk then ends at the record, as it does on the main thread. The thunk is sysv_abi, so on Windows the compiler saves the registers the guest may clobber around the call, as it did for the direct call; it keeps rsp 16-byte aligned at the call and has CFI for unwinders. Neither thread exit path runs through it: Windows exits with _endthreadex, POSIX longjmps back to StartNativeThread.

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 example libSceJobManager'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:

  • New 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 reaches scePthreadJoin through the thunk. On main it 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. On main it fails: frame chain climbs the stack to a frame whose return address is 0.
  • guest_user_context now walks the chain from the made context's function, bounded to the context's stack, and requires the record. With UserContext.cpp built as on main it fails: the frame chain of a made context ends on its stack at a frame whose saved frame pointer and return address are 0.
  • With this change all three pass, and so do the other thread, guest_pthread, user context and fiber tests (44 in all).
  • Full ctest --parallel 4 --timeout 120 on main e09dbc2b4 with this change (main has since moved to c9e6c45de without 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) and agc_driver_layered_volume_vertex_no_gpl (AMD Windows: GPL-mode cache failures (alpha/wave32 pass after local driver update) #4768) fail as on main here. Six Python-driven tests hit their 30 s timeout, which happens on this PC only (slow Python start-up); the conventions_function_bodies shards pass when rerun on their own.
  • python tools/check_conventions.py --base origin/main with this title and body passes.
  • Not tested: Linux and macOS (CI builds and runs these tests there), and the titles themselves; @Tamalero (LEGO Batman, Linux) and the Returnal reporters on Returnal (PPSA01285) on Windows: intermittent fault in a frame-pointer stack walk at app.exe+0x337aa9d (UE4 Main Thread, read of 0x8) #4239 could rerun.

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 under Silent stubs in their library's technical debt file
  • 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
  • Unblocks: none
  • GPUs tested: none needed (CPU-only change); built and run on AMD Radeon RX 6700 XT, Windows 11 26200
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

… 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>
@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

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.

Pull request Shared files not compared
PR 3986 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 4972 core/libs/prx/libkernel/CMakeLists.txt
PR 5021 core/libs/prx/libSceLibcInternal/CMakeLists.txt
core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 5918 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 6044 core/libs/prx/libSceLibcInternal/CMakeLists.txt
core/libs/prx/libkernel/CMakeLists.txt
core/libs/tests/Fiber.cpp
core/libs/tests/GuestUserContext.cpp
PR 6222 core/libs/prx/libSceFiber/Export.cpp
core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 6287 core/libs/prx/libkernel/CMakeLists.txt
PR 6361 core/libs/prx/libSceFiber/Export.cpp
PR 6534 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 6879 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 6908 core/libs/prx/libkernel/CMakeLists.txt
PR 6942 core/libs/prx/libkernel/CMakeLists.txt
PR 6977 core/libs/prx/libkernel/CMakeLists.txt
PR 7129 core/libs/prx/libkernel/CMakeLists.txt
PR 7135 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 7192 core/libs/prx/libkernel/CMakeLists.txt
PR 7201 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 7332 core/libs/prx/libSceLibcInternal/CMakeLists.txt
core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, fiber and guest_user_context pass 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.txt only, on main (no thunk, no fiber change, UserContext.cpp built as on main): 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 it
    • fiber: frame chain climbs the stack to a frame whose return address is 0
    • guest_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 of main plus guest_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.

@Tamalero

Copy link
Copy Markdown
Contributor

Title check on LEGO Batman: Legacy of the Dark Knight (PPSA16833), Linux, RTX 4090, main 5d9dcc42d + the title's start-up changes (#6694, #6699, the ToS hunk of #6447), the same route in every run.

Build Runs Frame-pointer walk
without this PR 4 faults inside the title's walker (the #4239 chain, ending in StartNativeThread's saved rbp 0x21)
with this PR (cherry-picked unchanged) 1 completes
with an equivalent local change (threads only) 1 completes

With this PR the run reached an Unreal LowLevelFatalError (GameThread timed out waiting for RenderThread after 120.00 secs, a separate problem I'm still looking at), and Unreal printed the call stack: four title frames and the return into Aps5CallGuestEntry_nid_no_patch, then the walk stopped at the zero record. The run with the local change did the same on a different fatal error (Could not decompress shader group with Oodle, 62 frames). In the four runs without it, no fatal-error text was logged at all, because the walk that precedes it faulted first. After the call stack, both runs end in the title's own crash path, the same in both.

Tested and written by Claude (AI assistant) on a contributor's machine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants