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

Merged
thuanquang merged 1 commit from upstream-pr/7123 into main 2026-10-18 09:04:32 +00:00
thuanquang commented 2026-10-17 12:28:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-17T12:28:37Z.


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:

  • RunThread called the guest thread entry directly, so the entry's saved rbp was whatever the host code left in it. On Linux, Tamalero's LEGO Batman core dump on #4239 shows the chain running from the guest entry into StartNativeThread and then to glibc's frame with saved rbp 0x21; on Windows, Returnal's walk on its UE4 Main Thread reads address 0x8 (#4239). Both titles fault inside the walker.
  • Fibers end their chain with a null frame (#4770): Aps5FiberMain's frame has a saved rbp of 0 but a non-zero return address, so the same walker reads [0 + 8] from inside a fiber.
  • makecontext starts its function with rbp 0 through Aps5MakecontextStart_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 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 (#5271, fixed by #6976) and agc_driver_layered_volume_vertex_no_gpl (#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 #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

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4900044619 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7123). Originally posted by [thuanquang](https://github.com/thuanquang) on 2026-10-17T12:28:37Z. --- ### 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: - `RunThread` called the guest thread entry directly, so the entry's saved `rbp` was whatever the host code left in it. On Linux, Tamalero's LEGO Batman core dump on #4239 shows the chain running from the guest entry into `StartNativeThread` and then to glibc's frame with saved `rbp` `0x21`; on Windows, Returnal's walk on its `UE4 Main Thread` reads address `0x8` (#4239). Both titles fault inside the walker. - Fibers end their chain with a null frame (#4770): `Aps5FiberMain`'s frame has a saved `rbp` of 0 but a non-zero return address, so the same walker reads `[0 + 8]` from inside a fiber. - `makecontext` starts its function with `rbp` 0 through `Aps5MakecontextStart_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 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 `longjmp`s 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` (#5271, fixed by #6976) and `agc_driver_layered_volume_vertex_no_gpl` (#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 #4239 could rerun. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: none needed (CPU-only change); built and run on AMD Radeon RX 6700 XT, Windows 11 26200 - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-17 12:42:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T12:42:55Z.


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
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6234013296 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7123#issuecomment-6234013296). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-17T12:42:55Z. --- <!-- pr-overlap --> 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](https://www.github.com/boykopovar/AnyPS5/pull/3986) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 4972](https://www.github.com/boykopovar/AnyPS5/pull/4972) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libSceLibcInternal/CMakeLists.txt`<br>`core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 5918](https://www.github.com/boykopovar/AnyPS5/pull/5918) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/prx/libSceLibcInternal/CMakeLists.txt`<br>`core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/tests/Fiber.cpp`<br>`core/libs/tests/GuestUserContext.cpp` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/libs/prx/libSceFiber/Export.cpp`<br>`core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 6287](https://www.github.com/boykopovar/AnyPS5/pull/6287) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6361](https://www.github.com/boykopovar/AnyPS5/pull/6361) | `core/libs/prx/libSceFiber/Export.cpp` | | [PR 6534](https://www.github.com/boykopovar/AnyPS5/pull/6534) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 6908](https://www.github.com/boykopovar/AnyPS5/pull/6908) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6942](https://www.github.com/boykopovar/AnyPS5/pull/6942) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 7129](https://www.github.com/boykopovar/AnyPS5/pull/7129) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 7135](https://www.github.com/boykopovar/AnyPS5/pull/7135) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 7192](https://www.github.com/boykopovar/AnyPS5/pull/7192) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/libs/prx/libSceLibcInternal/CMakeLists.txt`<br>`core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/prx/libkernel/Pthread/src/Thread.cpp` |
jotitadev commented 2026-10-17 15:16:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-17T15:16:36Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5558428617 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7123#pullrequestreview-5558428617). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-17T15:16:36Z. **Review:** COMMENTED --- 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 commented 2026-10-17 23:18:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Tamalero on 2026-10-17T23:18:27Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6242973875 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7123#issuecomment-6242973875). Originally posted by [Tamalero](https://github.com/Tamalero) on 2026-10-17T23:18:27Z. --- 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 in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!7123
No description provided.