fix(relinker): refuse red-zone leaves without a frame pointer that push or pop #11061

Merged
roboterspinne merged 1 commit from upstream-pr/11061 into main 2026-10-27 11:00:26 +00:00
roboterspinne commented 2026-10-25 16:16:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-25T16:16:25Z.


What

In a leaf without a frame pointer, --frame-red-zone lowers RSP after the prologue and adds the shift to every RSP operand in the body (frameSegment in core/relinker/codegen/src/x86/RedZoneFramer.cpp). That is right only while RSP stays where the prologue left it. The framer refuses explicit RSP arithmetic in the body (other use of rsp) but not opcode-encoded pushes and pops. After push rax in the body, [rsp] addresses the pushed slot and needs no shift, yet the framer turned it into [rsp+0x10], a slot of the leaf's red zone. On untouched 160f6592b5, the leaf push rbx; push r14; push r15; mov [rsp-8], rcx; push rdx; mov rax, [rsp]; pop rdx; pop r15; pop r14; pop rbx; ret, framed with FrameRedZoneFunctions and run natively on Windows, returns RCX instead of RDX, and stc; pushfq; and qword [rsp], -2; popfq; sbb rax, rax returns -1 instead of 0. CI did not catch it: no red_zone_framer test frames a body that pushes or pops (the ones #11063 added are all refused as other use of rsp), and red_zone_framer_execution, which runs framed leaves, is registered only on Linux x86-64 and has no push in a body either.

  • Rule. A leaf without a frame pointer that pushes or pops between its prologue and its exits keeps its red zone and is reported as push or pop inside the body. pushesOrPops counts push and pop of a register (also with REX or 66), push imm8/imm32, push r/m (FF /6), pop r/m (8F /0), pushf, popf and push or pop of fs or gs. The pops of an exit, which the framer already matches against the prologue, still frame. The check runs after the body loop, so a reason found inside the loop, such as other use of rsp, is still the one reported. A leaf with neither pushes nor an allocation that pushes or pops is now reported under the new reason instead of prologue shorter than a jump, which is checked after it. Tracking the push depth instead would need the depth at every branch and branch target; not done.
  • Leaves with a frame pointer still frame when they push and pop: their RSP operands are already refused, and the pop reads the pushed slot wherever the push put it. The one difference left is a leaf that reads back through RBP, after the pop, the red-zone slot its push overwrote; the frame-pointer leaf under Tested returns 0x4444 unframed and 0x3333 framed, on main and with this change. Compilers do not read a slot they have pushed over.
  • Tests. skipsLeavesWithoutFramePointerThatPushOrPop in RedZoneFramerTests.cpp: seven leaves without a frame pointer that store below RSP and then push and pop in one of the forms above (three with an RSP operand between push and pop, one that pushes from and pops to RSP operands) must be refused with the new reason, and a leaf with a frame pointer that pushes and pops must still frame. On main before #11063, all eight leaves are framed. Since #11063, the leaf that pushes from and pops to RSP operands is refused as rsp operand that cannot move and has no room for a stub, and the other seven are still framed. Either way the test fails at its first require.
  • Pop r/m without REX. #11063, now in main, stopped memoryUse from reading 8F without REX as an XOP prefix, so pop qword [rsp+8] (8f 44 24 08) in a leaf with a frame pointer is refused as other use of rsp, like its REX.W form. In a leaf without a frame pointer this PR refuses it as push or pop inside the body, and pop rsp (8f c4) is still refused as other use of rsp inside the loop. After merging main, both changes are in RedZoneFramer.cpp, and red_zone_framer runs #11063's tellsPopsWithoutRexFromXop next to this PR's test.
  • Docs. The --frame-red-zone entry in docs/dev/debt/relinker.md lists these leaves among those that keep their red zone.
  • Overlap. #10871, which frames leaves whose prologue or exit is shorter than a jump, changes the same lines. It lists this PR as its dependency, so pr_overlap.py no longer reports the pair.
    • #10871 adds a pushesOrPops(code, insn) helper with the same opcodes at the same place, and refuses push or pop only in leaves without pushes or an allocation. This PR refuses them in every leaf without a frame pointer, and neither PR touches leaves with a frame pointer.
    • The two conflict in three places in RedZoneFramer.cpp: the helper, the body-loop line next to its push or pop in a leaf without pushes check, and the line after the loop next to its rewritten prologue checks. They also conflict in the same sentence of relinker.md.
    • Whichever lands second keeps one helper. With #10871's ExitSite, the new check compares exit.Begin <= offset && offset < exit.End.
    • Rechecked against #10871 at f8a99b6b8a (this PR at 1d403f96fe): merging in either order gives the same tree after the same resolution of the three conflicts in RedZoneFramer.cpp (#10871's pushesOrPops(code, insn) helper kept, both body-loop checks kept, this PR's check using ExitSite) and the one in the --frame-red-zone debt entry, now one bullet that states both rules; red_zone_framer (with the tests #10871 added in 597f1392a8), amd64_only_windows and amd64_only_converter pass on the combined tree on Windows, and the native Windows probe, extended with the five execution leaves from 597f1392a8, gives 0 wrong results on the combined tree against 4 on #10871 alone (the three body-push leaves and a push rbx leaf whose prologue #10871 widens).
    • With main f95acfc948 (which includes #11063) merged into this branch, #10871 at f8a99b6b8a still conflicts only in the same three hunks of RedZoneFramer.cpp and the same debt sentence. #11063's changes to extensionBits and memoryUse merge outside them, so the resolved hunks posted on #10871 still apply.
    • Order: agreed on #10871 that this PR lands first; #10871 lists this PR as its dependency and takes the resolved hunks posted there.
    • #10999 also changes RedZoneFramer.cpp and its tests; it merges cleanly with this branch, and the merged red_zone_framer tests pass. At de2b5de1b7 it also merges cleanly with main f95acfc948 merged into this branch.
    • pr_overlap.py cannot compare #5021, #10479 and #10861, which also change relinker.md, because they do not merge with main. Checked by hand: #5021 and #10479 each add one other entry, #10861 rewrites the PT_SCE_DYNLIBDATA entries and adds one on guest lifecycle arrays, and none touches the --frame-red-zone sentence.

Tested

Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON); synthetic leaves only, no title or homebrew.

  • Counter-check: red_zone_framer with the new test and main's RedZoneFramer.cpp (unchanged between 3a2c51afd2 and 160f6592b5) fails with a leaf without a frame pointer that pushes or pops between its prologue and its exits was framed, so its rsp operands after a push were moved past the pushed slot. With the change it passes.
  • Native run: a probe linked against relinker_core places each leaf in executable memory from VirtualAlloc, frames it with FrameRedZoneFunctions, applies the displacements and sites the way RedZoneFramerExecutionTests.cpp does, and calls it with RCX = 0x1111 and RDX = 0x2222.
    • On main, the push leaf above returns 0x1111 instead of 0x2222, both with [rsp] moved to a stub and with [rsp+0] moved in place, and the pushfq leaf returns -1 instead of 0. With the change, all three are refused and return their unframed results.
    • A control that stores and reloads below RSP without a push is framed and returns 0x3333 on both.
    • The frame-pointer leaf push rbp; mov rbp, rsp; push r15; push rbx; mov [rbp-0x18], rcx; push rdx; pop rax; add rax, [rbp-0x18]; pop rbx; pop r15; pop rbp; ret returns 0x4444 unframed and 0x3333 framed, on both.
  • No converted PE was run: a Windows output needs the AnyPS5 runtime, which a relinker-only build does not have. The probe runs the framer's sites and displacements through the same calls as the Linux execution test.
  • Merged current main (f95acfc948, which includes #11063) into the branch without conflicts (fa941e9301). On fa941e9301, ctest -R 'red_zone|amd64_only|RedZone' -j 6: 3 tests passed: red_zone_framer (0.7 s, with this PR's skipsLeavesWithoutFramePointerThatPushOrPop and #11063's tellsPopsWithoutRexFromXop), amd64_only_converter and amd64_only_windows.
  • Full suite on fa941e9301: ctest --test-dir build-relinker -j 6: 310 tests, 307 passed, long_paths skipped; failures known on this machine on main as well: select_tests (Windows symlink fixture) and patch_advisor_unit (path with spaces, which open #10667 fixes); both failed again alone.
  • python tools/check_conventions.py --base upstream/main with this title and description passed on fa941e9301.
  • Not tested on Linux or macOS; CI runs the tests on all three hosts.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5006435731 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11061). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-25T16:16:25Z. --- ### What In a leaf without a frame pointer, `--frame-red-zone` lowers RSP after the prologue and adds the shift to every RSP operand in the body (`frameSegment` in `core/relinker/codegen/src/x86/RedZoneFramer.cpp`). That is right only while RSP stays where the prologue left it. The framer refuses explicit RSP arithmetic in the body (`other use of rsp`) but not opcode-encoded pushes and pops. After `push rax` in the body, `[rsp]` addresses the pushed slot and needs no shift, yet the framer turned it into `[rsp+0x10]`, a slot of the leaf's red zone. On untouched 160f6592b5, the leaf `push rbx; push r14; push r15; mov [rsp-8], rcx; push rdx; mov rax, [rsp]; pop rdx; pop r15; pop r14; pop rbx; ret`, framed with `FrameRedZoneFunctions` and run natively on Windows, returns RCX instead of RDX, and `stc; pushfq; and qword [rsp], -2; popfq; sbb rax, rax` returns -1 instead of 0. CI did not catch it: no `red_zone_framer` test frames a body that pushes or pops (the ones #11063 added are all refused as `other use of rsp`), and `red_zone_framer_execution`, which runs framed leaves, is registered only on Linux x86-64 and has no push in a body either. - **Rule.** A leaf without a frame pointer that pushes or pops between its prologue and its exits keeps its red zone and is reported as `push or pop inside the body`. `pushesOrPops` counts push and pop of a register (also with REX or `66`), `push imm8`/`imm32`, `push r/m` (`FF /6`), `pop r/m` (`8F /0`), `pushf`, `popf` and push or pop of `fs` or `gs`. The pops of an exit, which the framer already matches against the prologue, still frame. The check runs after the body loop, so a reason found inside the loop, such as `other use of rsp`, is still the one reported. A leaf with neither pushes nor an allocation that pushes or pops is now reported under the new reason instead of `prologue shorter than a jump`, which is checked after it. Tracking the push depth instead would need the depth at every branch and branch target; not done. - **Leaves with a frame pointer** still frame when they push and pop: their RSP operands are already refused, and the pop reads the pushed slot wherever the push put it. The one difference left is a leaf that reads back through RBP, after the pop, the red-zone slot its push overwrote; the frame-pointer leaf under **Tested** returns 0x4444 unframed and 0x3333 framed, on `main` and with this change. Compilers do not read a slot they have pushed over. - **Tests.** `skipsLeavesWithoutFramePointerThatPushOrPop` in `RedZoneFramerTests.cpp`: seven leaves without a frame pointer that store below RSP and then push and pop in one of the forms above (three with an RSP operand between push and pop, one that pushes from and pops to RSP operands) must be refused with the new reason, and a leaf with a frame pointer that pushes and pops must still frame. On `main` before #11063, all eight leaves are framed. Since #11063, the leaf that pushes from and pops to RSP operands is refused as `rsp operand that cannot move and has no room for a stub`, and the other seven are still framed. Either way the test fails at its first `require`. - **Pop r/m without REX.** #11063, now in `main`, stopped `memoryUse` from reading `8F` without REX as an XOP prefix, so `pop qword [rsp+8]` (`8f 44 24 08`) in a leaf with a frame pointer is refused as `other use of rsp`, like its REX.W form. In a leaf without a frame pointer this PR refuses it as `push or pop inside the body`, and `pop rsp` (`8f c4`) is still refused as `other use of rsp` inside the loop. After merging `main`, both changes are in `RedZoneFramer.cpp`, and `red_zone_framer` runs #11063's `tellsPopsWithoutRexFromXop` next to this PR's test. - **Docs.** The `--frame-red-zone` entry in `docs/dev/debt/relinker.md` lists these leaves among those that keep their red zone. - **Overlap.** #10871, which frames leaves whose prologue or exit is shorter than a jump, changes the same lines. It lists this PR as its dependency, so `pr_overlap.py` no longer reports the pair. - #10871 adds a `pushesOrPops(code, insn)` helper with the same opcodes at the same place, and refuses push or pop only in leaves without pushes or an allocation. This PR refuses them in every leaf without a frame pointer, and neither PR touches leaves with a frame pointer. - The two conflict in three places in `RedZoneFramer.cpp`: the helper, the body-loop line next to its `push or pop in a leaf without pushes` check, and the line after the loop next to its rewritten prologue checks. They also conflict in the same sentence of `relinker.md`. - Whichever lands second keeps one helper. With #10871's `ExitSite`, the new check compares `exit.Begin <= offset && offset < exit.End`. - Rechecked against #10871 at f8a99b6b8a (this PR at 1d403f96fe): merging in either order gives the same tree after the same resolution of the three conflicts in `RedZoneFramer.cpp` (#10871's `pushesOrPops(code, insn)` helper kept, both body-loop checks kept, this PR's check using `ExitSite`) and the one in the `--frame-red-zone` debt entry, now one bullet that states both rules; `red_zone_framer` (with the tests #10871 added in 597f1392a8), `amd64_only_windows` and `amd64_only_converter` pass on the combined tree on Windows, and the native Windows probe, extended with the five execution leaves from 597f1392a8, gives 0 wrong results on the combined tree against 4 on #10871 alone (the three body-push leaves and a `push rbx` leaf whose prologue #10871 widens). - With `main` f95acfc948 (which includes #11063) merged into this branch, #10871 at f8a99b6b8a still conflicts only in the same three hunks of `RedZoneFramer.cpp` and the same debt sentence. #11063's changes to `extensionBits` and `memoryUse` merge outside them, so the resolved hunks posted on #10871 still apply. - Order: agreed on #10871 that this PR lands first; #10871 lists this PR as its dependency and takes the resolved hunks posted there. - #10999 also changes `RedZoneFramer.cpp` and its tests; it merges cleanly with this branch, and the merged `red_zone_framer` tests pass. At de2b5de1b7 it also merges cleanly with `main` f95acfc948 merged into this branch. - `pr_overlap.py` cannot compare #5021, #10479 and #10861, which also change `relinker.md`, because they do not merge with `main`. Checked by hand: #5021 and #10479 each add one other entry, #10861 rewrites the `PT_SCE_DYNLIBDATA` entries and adds one on guest lifecycle arrays, and none touches the `--frame-red-zone` sentence. ### 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. --> Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`); synthetic leaves only, no title or homebrew. - Counter-check: `red_zone_framer` with the new test and `main`'s `RedZoneFramer.cpp` (unchanged between 3a2c51afd2 and 160f6592b5) fails with `a leaf without a frame pointer that pushes or pops between its prologue and its exits was framed, so its rsp operands after a push were moved past the pushed slot`. With the change it passes. - Native run: a probe linked against `relinker_core` places each leaf in executable memory from `VirtualAlloc`, frames it with `FrameRedZoneFunctions`, applies the displacements and sites the way `RedZoneFramerExecutionTests.cpp` does, and calls it with RCX = 0x1111 and RDX = 0x2222. - On `main`, the push leaf above returns 0x1111 instead of 0x2222, both with `[rsp]` moved to a stub and with `[rsp+0]` moved in place, and the `pushfq` leaf returns -1 instead of 0. With the change, all three are refused and return their unframed results. - A control that stores and reloads below RSP without a push is framed and returns 0x3333 on both. - The frame-pointer leaf `push rbp; mov rbp, rsp; push r15; push rbx; mov [rbp-0x18], rcx; push rdx; pop rax; add rax, [rbp-0x18]; pop rbx; pop r15; pop rbp; ret` returns 0x4444 unframed and 0x3333 framed, on both. - No converted PE was run: a Windows output needs the AnyPS5 runtime, which a relinker-only build does not have. The probe runs the framer's sites and displacements through the same calls as the Linux execution test. - Merged current `main` (f95acfc948, which includes #11063) into the branch without conflicts (fa941e9301). On fa941e9301, `ctest -R 'red_zone|amd64_only|RedZone' -j 6`: 3 tests passed: `red_zone_framer` (0.7 s, with this PR's `skipsLeavesWithoutFramePointerThatPushOrPop` and #11063's `tellsPopsWithoutRexFromXop`), `amd64_only_converter` and `amd64_only_windows`. - Full suite on fa941e9301: `ctest --test-dir build-relinker -j 6`: 310 tests, 307 passed, `long_paths` skipped; failures known on this machine on `main` as well: `select_tests` (Windows symlink fixture) and `patch_advisor_unit` (path with spaces, which open #10667 fixes); both failed again alone. - `python tools/check_conventions.py --base upstream/main` with this title and description passed on fa941e9301. - Not tested on Linux or macOS; CI runs the tests on all three hosts. ### 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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes (Claude Code)
MarcZX10 commented 2026-10-25 16:43:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T16:43:24Z.

Review: DISMISSED


I manually checked the overlap with #10871. git merge-tree of the current heads conflicts in RedZoneFramer.cpp and docs/dev/debt/relinker.md; both branches add the pushesOrPops handling in the same area, and the debt description also conflicts. Your What section describes how to combine the helpers, but the checklist still says Depends on: none, and I do not see an agreement on order from #10871’s author. Please coordinate with that author on which change lands first, then record the dependency/order and recheck the combined tree. The standalone relinker CI is green; this is specifically the repository’s shared-file ordering requirement.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5644179226 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11061#pullrequestreview-5644179226). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T16:43:24Z. **Review:** DISMISSED --- I manually checked the overlap with #10871. `git merge-tree` of the current heads conflicts in `RedZoneFramer.cpp` and `docs/dev/debt/relinker.md`; both branches add the `pushesOrPops` handling in the same area, and the debt description also conflicts. Your What section describes how to combine the helpers, but the checklist still says `Depends on: none`, and I do not see an agreement on order from #10871’s author. Please coordinate with that author on which change lands first, then record the dependency/order and recheck the combined tree. The standalone relinker CI is green; this is specifically the repository’s shared-file ordering requirement.
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!11061
No description provided.