fix(relinker): end the guest frame-pointer chain at the entry stub #2339

Merged
johnmaia merged 1 commit from upstream-pr/2339 into main 2026-10-10 04:47:12 +00:00
johnmaia commented 2026-10-09 15:51:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by johnmaia on 2026-10-09T15:51:35Z.


What

The entry stubs call the title's entry with rbp = 0: on Linux as the process starts, on Windows as the argument loop (dec ebp) leaves it. The title's start code pushes rbp, so the frame-pointer chain of its main thread ends in a null frame pointer.

Unreal Engine walks that chain to capture a call stack (for a fatal error or an ensure). It stops at a return address of 0, but it reads [rbp + 8] of each saved frame pointer first, so a null link makes it read address 8 and crash. On the console the chain ends in a frame record whose return address is 0.

  • Linux: the stub pushes a zero frame record (saved rbp 0, return address 0) and points rbp at it before call. Two 8-byte pushes keep the 16-byte alignment at the call.
  • Windows: the stub writes the record into unused slots of its own frame (rsp+0x40, rsp+0x48; it uses 0x20-0x3c and 0x58-0x5c) and points rbp at it before calling the entry.

Tested

Linux (Ubuntu 24.04, GCC 13.3), RX 7600.

  • New entry_stub test: the Linux stub passes the initial stack pointer, aligns, pushes the zero record, points rbp at it, clears rsi, calls the entry and ends with ud2.
  • Full ctest 517/517. tools/check_conventions.py and tools/check_test_registration.py pass.
  • Disney Epic Mickey: Rebrushed (PPSA15657, UE4), on its testing stack (#2255):
    • Before: in about half of its runs the title screen ends in a SIGSEGV with no message. gdb stops in Unreal's stack walk at eboot 0x186cfbf, mov 0x8(%rdx),%rax with rdx = 0, after following the chain to the stub's rbp.
    • After: the same walk ends at the stub's record (a gdb walk of the chain shows 24 frames, then saved=0 ret=0), so Unreal reports its own error instead of crashing in the walk. Here that is LowLevelFatalError ... RemoveFromOuterMap NumRemoved = 0, a separate problem.
  • The Windows stub change is covered by the build and the existing Windows stub tests only; it was not run on Windows.

Guest threads started by RunThread call their entry from host frames too. That needs a trampoline with unwind information and is left for a separate change.

Overlaps #2097, which changes the same function's call encoding: its new test helper reads the call at entry + 10, and with this change the call is at entry + 17. Whichever lands second needs that offset updated.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_0134RWCyiyydJBbfKz8g9Bwe

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4802733991 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2339). Originally posted by [johnmaia](https://github.com/johnmaia) on 2026-10-09T15:51:35Z. --- ### What The entry stubs call the title's entry with `rbp` = 0: on Linux as the process starts, on Windows as the argument loop (`dec ebp`) leaves it. The title's start code pushes `rbp`, so the frame-pointer chain of its main thread ends in a null frame pointer. Unreal Engine walks that chain to capture a call stack (for a fatal error or an ensure). It stops at a return address of 0, but it reads `[rbp + 8]` of each saved frame pointer first, so a null link makes it read address 8 and crash. On the console the chain ends in a frame record whose return address is 0. - Linux: the stub pushes a zero frame record (saved `rbp` 0, return address 0) and points `rbp` at it before `call`. Two 8-byte pushes keep the 16-byte alignment at the call. - Windows: the stub writes the record into unused slots of its own frame (`rsp+0x40`, `rsp+0x48`; it uses `0x20`-`0x3c` and `0x58`-`0x5c`) and points `rbp` at it before calling the entry. ### Tested Linux (Ubuntu 24.04, GCC 13.3), RX 7600. - New `entry_stub` test: the Linux stub passes the initial stack pointer, aligns, pushes the zero record, points `rbp` at it, clears `rsi`, calls the entry and ends with `ud2`. - Full `ctest` 517/517. `tools/check_conventions.py` and `tools/check_test_registration.py` pass. - Disney Epic Mickey: Rebrushed (PPSA15657, UE4), on its testing stack (#2255): - Before: in about half of its runs the title screen ends in a SIGSEGV with no message. gdb stops in Unreal's stack walk at eboot `0x186cfbf`, `mov 0x8(%rdx),%rax` with `rdx` = 0, after following the chain to the stub's `rbp`. - After: the same walk ends at the stub's record (a gdb walk of the chain shows 24 frames, then `saved=0 ret=0`), so Unreal reports its own error instead of crashing in the walk. Here that is `LowLevelFatalError ... RemoveFromOuterMap NumRemoved = 0`, a separate problem. - The Windows stub change is covered by the build and the existing Windows stub tests only; it was not run on Windows. Guest threads started by `RunThread` call their entry from host frames too. That needs a trampoline with unwind information and is left for a separate change. Overlaps #2097, which changes the same function's call encoding: its new test helper reads the call at `entry + 10`, and with this change the call is at `entry + 17`. Whichever lands second needs that offset updated. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_0134RWCyiyydJBbfKz8g9Bwe
Kikoarl commented 2026-10-09 17:52:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-09T17:52:44Z.


Tested on Linux (CachyOS x86_64, Intel RPL-S iGPU / Mesa 26.3.0-devel / Vulkan 1.4.363, GCC 16.2.1) at 60a614cf:

  • Build: Built cleanly in RAM (/tmp/anyps5_build) with zero warnings/errors.
  • Conventions: python3 tools/check_conventions.py --base origin/main --head HEAD -> PASS (0 errors).
  • Verification: Verified frame pointer chain termination in EntryStubBuilder.cpp. Prevents stack unwinding from walking into unmapped host memory past the entry stub.

LGTM!

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6086306127 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2339#issuecomment-6086306127). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-09T17:52:44Z. --- Tested on Linux (CachyOS x86_64, Intel RPL-S iGPU / Mesa 26.3.0-devel / Vulkan 1.4.363, GCC 16.2.1) at `60a614cf`: - **Build**: Built cleanly in RAM (`/tmp/anyps5_build`) with zero warnings/errors. - **Conventions**: `python3 tools/check_conventions.py --base origin/main --head HEAD` -> **PASS (0 errors)**. - **Verification**: Verified frame pointer chain termination in `EntryStubBuilder.cpp`. Prevents stack unwinding from walking into unmapped host memory past the entry stub. LGTM!
Yuva1024 commented 2026-10-10 02:38:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Yuva1024 on 2026-10-10T02:38:21Z.


Windows results, since the description has Linux only. main c963432d with this PR merged (--no-ff, as the Build workflow does); Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, Ryzen 5 5600G, RTX 4060.

  • Full ctest: 573/574. entry_stub passes. The one failure is video_out_flip_lifetime, which aborts intermittently on unchanged main too (filed as #2540); this merge only rebuilds the relinker.
  • The Windows stub frame: the prologue saves five registers and reserves 0x60; the stub and its helpers only address rsp+0x20..0x3f and the dword at 0x58, and WindowsGuestStartup has no rsp-relative accesses, so 0x40..0x4f is free as described.
  • Probe outside the repository: the main_fixture entry replaced with mov rax,[rbp]; or rax,[rbp+8]; jnz bad; mov eax,42; ret, converted with --windows and run:
    • main: 0xc0000005 in 3/3 runs (rbp is 0 at the entry).
    • with this PR: exit 42 in 3/3 runs (rbp points at a {0, 0} record).

Not tested on a title.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6092859022 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2339#issuecomment-6092859022). Originally posted by [Yuva1024](https://github.com/Yuva1024) on 2026-10-10T02:38:21Z. --- Windows results, since the description has Linux only. `main` `c963432d` with this PR merged (`--no-ff`, as the Build workflow does); Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, Ryzen 5 5600G, RTX 4060. - Full `ctest`: 573/574. `entry_stub` passes. The one failure is `video_out_flip_lifetime`, which aborts intermittently on unchanged `main` too (filed as #2540); this merge only rebuilds the relinker. - The Windows stub frame: the prologue saves five registers and reserves `0x60`; the stub and its helpers only address `rsp+0x20`..`0x3f` and the dword at `0x58`, and `WindowsGuestStartup` has no `rsp`-relative accesses, so `0x40`..`0x4f` is free as described. - Probe outside the repository: the `main_fixture` entry replaced with `mov rax,[rbp]; or rax,[rbp+8]; jnz bad; mov eax,42; ret`, converted with `--windows` and run: - `main`: `0xc0000005` in 3/3 runs (`rbp` is 0 at the entry). - with this PR: exit 42 in 3/3 runs (`rbp` points at a `{0, 0}` record). Not tested on a title. AI-assisted: yes (Claude Code).
github-actions[bot] commented 2026-10-10 04:21:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T04:21:00Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#2545 core/relinker/elfpatcher/tests/EntryStubTests.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6093709569 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2339#issuecomment-6093709569). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T04:21:00Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #2545 | | | `core/relinker/elfpatcher/tests/EntryStubTests.cpp` |
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!2339
No description provided.