feat(relinker): frame red-zone leaves whose prologue or exit is shorter than a jump #10871

Merged
Chronyyx merged 4 commits from upstream-pr/10871 into main 2026-10-26 21:55:27 +00:00
Chronyyx commented 2026-10-25 06:59:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Chronyyx on 2026-10-25T06:59:21Z.


What

In short: on Windows, a fault AnyPS5 resumes on a guest thread can overwrite values a function keeps just below RSP (#361). --frame-red-zone protects such leaf functions by moving their stack, but it skipped every leaf whose start or end is shorter than the 5-byte jump it patches in, and every leaf without pushes. This PR frames those too. In ASTRO BOT (PPSA21564) that is 362 of 463 red-zone leaves, up from 146 on main. Agreed with @EagleBearerYS on #9514 before starting.

How:

  • A short exit (for example pop rbx; ret, 2 bytes) borrows the instructions just before it into its stub, the way #5640's operand stubs already do. The stub runs them with RSP still lowered, then raises RSP, pops and returns.
  • A short prologue (for example push rbx, 1 byte) borrows the instructions just after it: the stub pushes, lowers RSP, then runs them.
  • A leaf without pushes or an allocation is framed the same way: RSP is lowered on entry and raised before the ret.
  • Borrowed instructions with an RSP operand are re-encoded with the moved displacement.

When it still refuses, each with its own reason in the relinker output:

  • a branch, ret, RIP-relative instruction, push, pop or another site comes before there are 5 bytes;
  • a branch lands on a borrowed instruction (branch into the instructions before a short exit, … after a short prologue);
  • a loop jumps back to the first instruction of a widened prologue, which would lower RSP again on every pass (branch to the start of a widened prologue);
  • a leaf without pushes pushes or pops anywhere, like a function fragment that pops registers pushed before it (push or pop in a leaf without pushes). This also refuses shrink-wrapped leaves that push later, which must be refused: once RSP is lowered on entry, their pushes land on their own spills (ASTRO BOT's 0x10cffa0, below). A push rbp; mov rbp, rsp prologue is not affected (second commit).
  • Depends on #11061: a leaf with pushes that also pushes and pops in its body is framed wrongly, because its [rsp] operands after the body push are shifted past the pushed slot. That is on main already, and this PR reaches more such leaves: on its head a push rbx leaf whose prologue it widens, with a push rsi … pop rsi in its body, returns 0x44b4 instead of 0x6666 even without the clobber. #11061 refuses those leaves.

The relinker now prints how many prologues and exits it widened. docs/dev/debt/relinker.md and --frame-red-zone in USAGE.md describe what is framed now.

ASTRO BOT framing report, main and this PR
main this PR
framed 146 of 463 362 of 463
widened prologues / exits – 216 / 215
skipped: exit shorter than a jump 290 41
skipped: push or pop in a leaf without pushes – 22
skipped: indirect jump 10 10
skipped: branch into the instructions before a short exit – 9
skipped: other use of rsp 11 7
skipped: branch to the start of a widened prologue – 6
skipped: prologue shorter than a jump 5 5
skipped: branch leaves the function 1 1
  • A skipped leaf is counted under the first reason found, so 4 leaves that main reports under "other use of rsp" are reported under the earlier push/pop check here.
  • Widened prologues and exits are counted separately. A debug build listed 5 leaves whose prologue is widened and whose exits are not (three without pushes whose sub is 4 bytes while add …; ret is 5, two whose three pushes are 4 bytes while their pops and ret are 5), and two leaves with 2 and 4 widened exits: 216 − 5 + 1 + 3 = 215.
  • The relinker built on Linux gives the same counts. #9136 counts 642 "exit shorter than a jump" in GTA V.

Tested

In short: unit tests for each new case and refusal, the Linux execution test running the new stubs under a clobber below RSP, the full relinker suite on Windows and Linux, and ASTRO BOT to its title screen in 10 runs, with frame rates against main's framing.

  • red_zone_framer: new cases for a 1-byte push rbx with a 2-byte exit, a push rbp; mov rbp, rsp leaf with a 2-byte exit, a leaf without pushes, a loop back to a widened prologue's first instruction, a branch into the instructions borrowed before a short exit, and a fragment that pops without pushing. The existing short-prologue case now puts a RIP-relative load after the pushes, so it still covers a prologue that cannot be widened.
  • red_zone_framer_execution (Linux, WSL Ubuntu 26.04, GCC 15.2.0): three new leaves, one without pushes, one pushing only rbx and one with only push rbp; mov rbp, rsp, each keeping values below RSP or RBP across a loop. Under the single-step clobber the unframed leaves return 0xfffffffffffffff5, 0x2d2d2d2d2d2e605a and 0xfffffffffffffff5; framed, they return the expected 0x1aaa9, 0x1bbba and 0x1aaa9. The existing leaves still pass. The frame-pointer leaf found that the first commit's push/pop check refused it; the second commit fixes that.
  • Review cases (597f1392a8, by @EagleBearerYS, on review): a branch into the instructions borrowed after a short prologue, a widened prologue that allocates, a leaf without pushes with two ret exits, a VEX RSP operand in a widened exit, and a frame-pointer leaf with a balanced push and pop in its body, each as a unit case and, apart from the refusal, an execution leaf. red_zone_framer passes on Windows 11 and Linux; red_zone_framer_execution passes on Linux, where each new leaf returns the wrong value unframed under the clobber and the expected one framed.
  • Relinker-only ctest at this PR's head: Windows 11 (WinLibs GCC 15.2.0 r7) 260 of 260 on main af49a0788; Linux 264 of 264 on main e7e3e9c19 (no relinker file changed between the two bases). check_test_registration.py passes.
  • ASTRO BOT (PPSA21564) 01.007.000, Windows 11, Ryzen 9 3900X, RTX 3080, with #6182 and #9514 applied, default settings, no input, 150 s per run, this PR's framing (every commit frames the same 362 leaves in this title, which has no frame-pointer red-zone leaves): 9 of 10 runs reached the title screen with no crash. One ended at about 108 s of game time with vkWaitForFences recorder: Vulkan result -4 (device lost) in a compute dispatch; the same device loss happened once in a run of the title relinked without --frame-red-zone, out of 74 runs in all.
  • Speed, presents per 10 s on the title screen after its first 10 s, runs alternated: main's framing (146 leaves) 102.6, 108.0, 114.1 and 103.3 (mean 107.0); this PR (362 leaves) 111.8, 105.9 and 111.6 (mean 109.8), the fourth run being the device loss above. The difference is within the run-to-run spread; the title screen is GPU-bound, so a small CPU cost would not show.
  • The 31 s stop: a build before the push/pop check framed one more leaf, 0x10cffa0, and one of its 6 runs stopped without a crash report after 31 s. That leaf is shrink-wrapped: no pushes on entry, six pushes and pops on its slow path, spills down to rsp-0x58 after the pushes. That build lowered RSP by 0x60 on entry, so the six pushes land at rsp-0x68 to rsp-0x90 of the entry RSP, while the spills keep their addresses at rsp-0x58 to rsp-0x88: the spills overwrite saved registers whenever the slow path runs. That is a likely cause of the stop, not a measured one; the same build ran 4 more times without stopping. This PR refuses the leaf.
  • Not verified: other titles, anything past the title screen, and a game patch over a widened site (the existing refusal of a patch over a framed site applies to the larger sites, untested).
  • python3 tools/check_conventions.py --base origin/main passes.

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: #11061
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5002579989 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10871). Originally posted by [Chronyyx](https://github.com/Chronyyx) on 2026-10-25T06:59:21Z. --- ### What **In short:** on Windows, a fault AnyPS5 resumes on a guest thread can overwrite values a function keeps just below RSP (#361). `--frame-red-zone` protects such leaf functions by moving their stack, but it skipped every leaf whose start or end is shorter than the 5-byte jump it patches in, and every leaf without pushes. This PR frames those too. In ASTRO BOT (PPSA21564) that is **362 of 463** red-zone leaves, up from 146 on `main`. Agreed with @EagleBearerYS on #9514 before starting. **How:** - **A short exit** (for example `pop rbx; ret`, 2 bytes) borrows the instructions just before it into its stub, the way #5640's operand stubs already do. The stub runs them with RSP still lowered, then raises RSP, pops and returns. - **A short prologue** (for example `push rbx`, 1 byte) borrows the instructions just after it: the stub pushes, lowers RSP, then runs them. - **A leaf without pushes or an allocation** is framed the same way: RSP is lowered on entry and raised before the `ret`. - Borrowed instructions with an RSP operand are re-encoded with the moved displacement. **When it still refuses**, each with its own reason in the relinker output: - a branch, `ret`, RIP-relative instruction, push, pop or another site comes before there are 5 bytes; - a branch lands on a borrowed instruction (`branch into the instructions before a short exit`, `… after a short prologue`); - a loop jumps back to the first instruction of a widened prologue, which would lower RSP again on every pass (`branch to the start of a widened prologue`); - a leaf without pushes pushes or pops anywhere, like a function fragment that pops registers pushed before it (`push or pop in a leaf without pushes`). This also refuses shrink-wrapped leaves that push later, which must be refused: once RSP is lowered on entry, their pushes land on their own spills (ASTRO BOT's `0x10cffa0`, below). A `push rbp; mov rbp, rsp` prologue is not affected (second commit). - **Depends on #11061:** a leaf with pushes that also pushes and pops in its body is framed wrongly, because its `[rsp]` operands after the body push are shifted past the pushed slot. That is on `main` already, and this PR reaches more such leaves: on its head a `push rbx` leaf whose prologue it widens, with a `push rsi … pop rsi` in its body, returns 0x44b4 instead of 0x6666 even without the clobber. #11061 refuses those leaves. The relinker now prints how many prologues and exits it widened. `docs/dev/debt/relinker.md` and `--frame-red-zone` in `USAGE.md` describe what is framed now. <details><summary>ASTRO BOT framing report, <code>main</code> and this PR</summary> | | `main` | this PR | |---|---|---| | framed | 146 of 463 | 362 of 463 | | widened prologues / exits | – | 216 / 215 | | skipped: exit shorter than a jump | 290 | 41 | | skipped: push or pop in a leaf without pushes | – | 22 | | skipped: indirect jump | 10 | 10 | | skipped: branch into the instructions before a short exit | – | 9 | | skipped: other use of rsp | 11 | 7 | | skipped: branch to the start of a widened prologue | – | 6 | | skipped: prologue shorter than a jump | 5 | 5 | | skipped: branch leaves the function | 1 | 1 | - A skipped leaf is counted under the first reason found, so 4 leaves that `main` reports under "other use of rsp" are reported under the earlier push/pop check here. - Widened prologues and exits are counted separately. A debug build listed 5 leaves whose prologue is widened and whose exits are not (three without pushes whose `sub` is 4 bytes while `add …; ret` is 5, two whose three pushes are 4 bytes while their pops and `ret` are 5), and two leaves with 2 and 4 widened exits: 216 − 5 + 1 + 3 = 215. - The relinker built on Linux gives the same counts. #9136 counts 642 "exit shorter than a jump" in GTA V. </details> ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> **In short:** unit tests for each new case and refusal, the Linux execution test running the new stubs under a clobber below RSP, the full relinker suite on Windows and Linux, and ASTRO BOT to its title screen in 10 runs, with frame rates against `main`'s framing. - **`red_zone_framer`:** new cases for a 1-byte `push rbx` with a 2-byte exit, a `push rbp; mov rbp, rsp` leaf with a 2-byte exit, a leaf without pushes, a loop back to a widened prologue's first instruction, a branch into the instructions borrowed before a short exit, and a fragment that pops without pushing. The existing short-prologue case now puts a RIP-relative load after the pushes, so it still covers a prologue that cannot be widened. - **`red_zone_framer_execution`** (Linux, WSL Ubuntu 26.04, GCC 15.2.0): three new leaves, one without pushes, one pushing only `rbx` and one with only `push rbp; mov rbp, rsp`, each keeping values below RSP or RBP across a loop. Under the single-step clobber the unframed leaves return `0xfffffffffffffff5`, `0x2d2d2d2d2d2e605a` and `0xfffffffffffffff5`; framed, they return the expected `0x1aaa9`, `0x1bbba` and `0x1aaa9`. The existing leaves still pass. The frame-pointer leaf found that the first commit's push/pop check refused it; the second commit fixes that. - **Review cases (597f1392a8, by @EagleBearerYS, on review):** a branch into the instructions borrowed after a short prologue, a widened prologue that allocates, a leaf without pushes with two `ret` exits, a VEX RSP operand in a widened exit, and a frame-pointer leaf with a balanced push and pop in its body, each as a unit case and, apart from the refusal, an execution leaf. `red_zone_framer` passes on Windows 11 and Linux; `red_zone_framer_execution` passes on Linux, where each new leaf returns the wrong value unframed under the clobber and the expected one framed. - **Relinker-only `ctest`** at this PR's head: Windows 11 (WinLibs GCC 15.2.0 r7) 260 of 260 on `main` af49a0788; Linux 264 of 264 on `main` e7e3e9c19 (no relinker file changed between the two bases). `check_test_registration.py` passes. - **ASTRO BOT (PPSA21564) 01.007.000**, Windows 11, Ryzen 9 3900X, RTX 3080, with #6182 and #9514 applied, default settings, no input, 150 s per run, this PR's framing (every commit frames the same 362 leaves in this title, which has no frame-pointer red-zone leaves): 9 of 10 runs reached the title screen with no crash. One ended at about 108 s of game time with `vkWaitForFences recorder: Vulkan result -4` (device lost) in a compute dispatch; the same device loss happened once in a run of the title relinked without `--frame-red-zone`, out of 74 runs in all. - **Speed**, presents per 10 s on the title screen after its first 10 s, runs alternated: `main`'s framing (146 leaves) 102.6, 108.0, 114.1 and 103.3 (mean 107.0); this PR (362 leaves) 111.8, 105.9 and 111.6 (mean 109.8), the fourth run being the device loss above. The difference is within the run-to-run spread; the title screen is GPU-bound, so a small CPU cost would not show. - **The 31 s stop:** a build before the push/pop check framed one more leaf, `0x10cffa0`, and one of its 6 runs stopped without a crash report after 31 s. That leaf is shrink-wrapped: no pushes on entry, six pushes and pops on its slow path, spills down to `rsp-0x58` after the pushes. That build lowered RSP by 0x60 on entry, so the six pushes land at `rsp-0x68` to `rsp-0x90` of the entry RSP, while the spills keep their addresses at `rsp-0x58` to `rsp-0x88`: the spills overwrite saved registers whenever the slow path runs. That is a likely cause of the stop, not a measured one; the same build ran 4 more times without stopping. This PR refuses the leaf. - **Not verified:** other titles, anything past the title screen, and a game patch over a widened site (the existing refusal of a patch over a framed site applies to the larger sites, untested). - `python3 tools/check_conventions.py --base origin/main` passes. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: #11061 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!10871
No description provided.