fix(relinker): preserve opcode-encoded stack register uses #10999

Merged
plexerPK merged 2 commits from upstream-pr/10999 into main 2026-10-28 00:50:41 +00:00
plexerPK commented 2026-10-25 13:43:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by plexerPK on 2026-10-25T13:43:56Z.


What

A red-zone leaf that reads RSP through an opcode-encoded operand, such as PUSH RSP; POP RAX, can observe the lowered stack address after framing. Extend the existing rejection of other RSP uses to opcode-encoded PUSH, POP, XCHG, immediate MOV and BSWAP operands, including ESP, SP and SPL. Respect REX.B and distinguish AH from SPL so unrelated registers remain eligible.

Tested

macOS arm64, Release relinker-only build at de2b5de1b76afe51ec909ec9f5991e676edbcf06 on main e05ba6feebd000cfc316c1d1f00e37e030aa3d8b. The build and all four affected CTests pass: red_zone_framer, rewrite_boundaries, cli_args and game_patches. The BSWAP RSP/ESP and REX.R regressions failed on the previous head, while REX.B R12/R12D controls remain eligible. The added framer regressions cover eleven stack-register cases and five REX.B/AH controls; the regression failed before the guard. python3 tools/check_conventions.py --base origin/main, git diff --check and the publication-time official overlap check pass. Linux x86-64 red_zone_framer_execution is not registered on this host. These results cover framing decisions and generated patch metadata; no Linux/Windows guest execution, title, GPU or hosted-CI result is claimed.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5005265954 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10999). Originally posted by [plexerPK](https://github.com/plexerPK) on 2026-10-25T13:43:56Z. --- ### What A red-zone leaf that reads RSP through an opcode-encoded operand, such as `PUSH RSP; POP RAX`, can observe the lowered stack address after framing. Extend the existing rejection of other RSP uses to opcode-encoded PUSH, POP, XCHG, immediate MOV and BSWAP operands, including ESP, SP and SPL. Respect REX.B and distinguish AH from SPL so unrelated registers remain eligible. ### Tested macOS arm64, Release relinker-only build at `de2b5de1b76afe51ec909ec9f5991e676edbcf06` on main `e05ba6feebd000cfc316c1d1f00e37e030aa3d8b`. The build and all four affected CTests pass: `red_zone_framer`, `rewrite_boundaries`, `cli_args` and `game_patches`. The BSWAP RSP/ESP and REX.R regressions failed on the previous head, while REX.B R12/R12D controls remain eligible. The added framer regressions cover eleven stack-register cases and five REX.B/AH controls; the regression failed before the guard. `python3 tools/check_conventions.py --base origin/main`, `git diff --check` and the publication-time official overlap check pass. Linux x86-64 `red_zone_framer_execution` is not registered on this host. These results cover framing decisions and generated patch metadata; no Linux/Windows guest execution, title, GPU or hosted-CI result is claimed. <!-- 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. --> ### 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: none <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
MarcZX10 commented 2026-10-25 13:52:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T13:52:34Z.

Review: APPROVED


Reviewed the current head 46566d2d032f312d2775d2e70033a050ea0aa3d5. The opcode-register checks correctly account for the REX.B extension when identifying RSP, and the added cases cover PUSH/POP/XCHG/immediate MOV stack-register encodings plus non-stack controls. The change is limited to red-zone eligibility detection and its regression tests.

Local validation: relinker-only Release build succeeded; all five focused tests passed (red_zone_framer, red_zone_framer_execution, rewrite_boundaries, cli_args, game_patches); conventions and git diff --check passed. The full 296-test CTest run completed with 294 passing; masked_store_lowering and amd64_only_converter both terminate with Illegal and reproduce when rerun alone, unrelated to the touched code. Hosted checks passed for Linux, Windows, macOS arm64, macOS x86_64, ASan, conventions, intake, eligibility and labels; product-only jobs were skipped by workflow policy.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643524849 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10999#pullrequestreview-5643524849). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T13:52:34Z. **Review:** APPROVED --- Reviewed the current head `46566d2d032f312d2775d2e70033a050ea0aa3d5`. The opcode-register checks correctly account for the REX.B extension when identifying RSP, and the added cases cover PUSH/POP/XCHG/immediate MOV stack-register encodings plus non-stack controls. The change is limited to red-zone eligibility detection and its regression tests. Local validation: relinker-only Release build succeeded; all five focused tests passed (`red_zone_framer`, `red_zone_framer_execution`, `rewrite_boundaries`, `cli_args`, `game_patches`); conventions and `git diff --check` passed. The full 296-test CTest run completed with 294 passing; `masked_store_lowering` and `amd64_only_converter` both terminate with `Illegal` and reproduce when rerun alone, unrelated to the touched code. Hosted checks passed for Linux, Windows, macOS arm64, macOS x86_64, ASan, conventions, intake, eligibility and labels; product-only jobs were skipped by workflow policy.
ahmadmuj commented 2026-10-25 14:24:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T14:24:46Z.

Review: COMMENTED


Reviewed head 46566d2d03 on base e05ba6feeb (main is at 3a2c51afd2 now, no core/relinker/codegen/ changes since). Linux x86_64, GCC, Release, relinker-only build.

What I ran:

  • red_zone_framer and red_zone_framer_execution on the head: pass.
  • Your tests with the base RedZoneFramer.cpp: fail with an opcode-encoded rsp use was framed, so the regression is real.
  • Read the decoder for the no-ModRM forms. 50-5f, 90-97 and b0-bf all come out with HasModRm false. A REX followed by a legacy prefix gets zeroed, same as the CPU. So 41 66 bc imm16 (mov sp) still gets skipped and 66 41 bc (mov r12w) still gets framed. Checked both through the framer. I found no false positives.

One gap: BSWAP. 0f c8+r has no ModRM either, and the new branch returns early on IsTwoByteOpcode, so bswap rsp / bswap esp still get framed. I added {0x48, 0x0f, 0xcc, 0x48, 0x0f, 0xcc} to skipsOpcodeEncodedRspUses locally, and it fails on this head (framed=1, 2 sites). 0f cc and 4c 0f cc (REX.R) give the same result. No compiler emits this in a leaf, but inc rsp, not rsp and friends already skip through the mod == 3 rule, so this is the one register-operand form left.

This passes all your tests plus the two bswap cases, and a 49 0f cc (r12) control still frames:

        if ((info.RexPrefix & 1) == 0
            && ((!info.IsTwoByteOpcode && (op == 0x54 || op == 0x5c || op == 0x94 || op == 0xbc || (op == 0xb4 && info.RexPrefix != 0)))
                || (info.IsTwoByteOpcode && op == 0xcc)))

Small, up to you whether it goes in here.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643654740 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10999#pullrequestreview-5643654740). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-10-25T14:24:46Z. **Review:** COMMENTED --- Reviewed head `46566d2d03` on base `e05ba6feeb` (main is at `3a2c51afd2` now, no `core/relinker/codegen/` changes since). Linux x86_64, GCC, Release, relinker-only build. What I ran: - `red_zone_framer` and `red_zone_framer_execution` on the head: pass. - Your tests with the base `RedZoneFramer.cpp`: fail with `an opcode-encoded rsp use was framed`, so the regression is real. - Read the decoder for the no-ModRM forms. `50-5f`, `90-97` and `b0-bf` all come out with `HasModRm` false. A REX followed by a legacy prefix gets zeroed, same as the CPU. So `41 66 bc imm16` (mov sp) still gets skipped and `66 41 bc` (mov r12w) still gets framed. Checked both through the framer. I found no false positives. One gap: BSWAP. `0f c8+r` has no ModRM either, and the new branch returns early on `IsTwoByteOpcode`, so `bswap rsp` / `bswap esp` still get framed. I added `{0x48, 0x0f, 0xcc, 0x48, 0x0f, 0xcc}` to `skipsOpcodeEncodedRspUses` locally, and it fails on this head (`framed=1`, 2 sites). `0f cc` and `4c 0f cc` (REX.R) give the same result. No compiler emits this in a leaf, but `inc rsp`, `not rsp` and friends already skip through the `mod == 3` rule, so this is the one register-operand form left. This passes all your tests plus the two bswap cases, and a `49 0f cc` (r12) control still frames: ```cpp if ((info.RexPrefix & 1) == 0 && ((!info.IsTwoByteOpcode && (op == 0x54 || op == 0x5c || op == 0x94 || op == 0xbc || (op == 0xb4 && info.RexPrefix != 0))) || (info.IsTwoByteOpcode && op == 0xcc))) ``` Small, up to you whether it goes in here.
roboterspinne commented 2026-10-25 15:05:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-25T15:05:18Z.

Review: COMMENTED


Reviewed head de2b5de1b merged onto upstream/main 3a2c51afd (2026-10-25); the merge is clean.

Environment: Windows 11 Home 10.0.26200, Intel i9-12900H, WinLibs GCC 15.2.0 UCRT, CMake 4.3.1, Ninja 1.13.2, -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, Release.

Results

  • Full relinker-only suite on the merged tree: 292 tests, 290 pass, long_paths skipped. The one failure is patch_advisor_unit. It fails the same way on plain main on this machine, because the temp path contains a space (#10667), and it is unrelated. red_zone_framer passes. red_zone_framer_execution is registered only on Linux x86-64 and does not run here.
  • Counter-check: with main's RedZoneFramer.cpp under this test file, red_zone_framer_tests fails with an opcode-encoded rsp use was framed.
  • Mutants of the guard:
    • Dropping 0x54, 0x94, 0xbc, the 0xb4 clause or the new 0F CC clause fails the suite.
    • So does dropping the REX.B check, which an existing case catches (a leaf that allocates with sub after its pushes and spills below its allocation was not framed).
    • So does accepting 0xb4 without a REX prefix.
    • Only 0x5c survives (inline comment).
  • The decoder keeps the last REX byte and clears it after a later legacy prefix. 41 40 b4 10 is therefore treated as SPL, 40 41 b4 10 as r12b, and 48 66 bc imm16 as mov sp, all as on hardware. b4 10 without REX (AH) still frames.
  • With the bswap addition from de2b5de1b, the guard covers every form that encodes RSP/ESP/SP/SPL in the opcode (50+4, 58+4, 90+4, B8+4, B0+4 with REX, 0F C8+4), with or without 0x66 and REX.W. The REX.B controls (r12) still frame.
  • check_conventions.py --base upstream/main passes with this title and body.

Nit: see the inline comment on line 297.

Not checked: execution of framed code (the execution test is Linux-only; ahmadmuj ran it on Linux for the previous head); Linux and macOS locally.

Apart from the nit, I found nothing to change. For disclosure: none of my open PRs touches RedZoneFramer.cpp or its tests.

Written with AI assistance (Claude Code). I ran the build, the tests, the counter-check and the mutants above on my Windows 11 machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643788105 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10999#pullrequestreview-5643788105). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-25T15:05:18Z. **Review:** COMMENTED --- Reviewed head de2b5de1b merged onto `upstream/main` 3a2c51afd (2026-10-25); the merge is clean. **Environment:** Windows 11 Home 10.0.26200, Intel i9-12900H, WinLibs GCC 15.2.0 UCRT, CMake 4.3.1, Ninja 1.13.2, `-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`, Release. **Results** - Full relinker-only suite on the merged tree: 292 tests, 290 pass, `long_paths` skipped. The one failure is `patch_advisor_unit`. It fails the same way on plain `main` on this machine, because the temp path contains a space (#10667), and it is unrelated. `red_zone_framer` passes. `red_zone_framer_execution` is registered only on Linux x86-64 and does not run here. - Counter-check: with `main`'s `RedZoneFramer.cpp` under this test file, `red_zone_framer_tests` fails with `an opcode-encoded rsp use was framed`. - Mutants of the guard: - Dropping `0x54`, `0x94`, `0xbc`, the `0xb4` clause or the new `0F CC` clause fails the suite. - So does dropping the REX.B check, which an existing case catches (`a leaf that allocates with sub after its pushes and spills below its allocation was not framed`). - So does accepting `0xb4` without a REX prefix. - Only `0x5c` survives (inline comment). - The decoder keeps the last REX byte and clears it after a later legacy prefix. `41 40 b4 10` is therefore treated as SPL, `40 41 b4 10` as `r12b`, and `48 66 bc imm16` as `mov sp`, all as on hardware. `b4 10` without REX (AH) still frames. - With the `bswap` addition from de2b5de1b, the guard covers every form that encodes RSP/ESP/SP/SPL in the opcode (`50+4`, `58+4`, `90+4`, `B8+4`, `B0+4` with REX, `0F C8+4`), with or without `0x66` and REX.W. The REX.B controls (`r12`) still frame. - `check_conventions.py --base upstream/main` passes with this title and body. **Nit:** see the inline comment on line 297. **Not checked:** execution of framed code (the execution test is Linux-only; ahmadmuj ran it on Linux for the previous head); Linux and macOS locally. Apart from the nit, I found nothing to change. For disclosure: none of my open PRs touches `RedZoneFramer.cpp` or its tests. Written with AI assistance (Claude Code). I ran the build, the tests, the counter-check and the mutants above on my Windows 11 machine.
MarcZX10 commented 2026-10-25 15:06:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T15:06:32Z.

Review: APPROVED


Reviewed exact head de2b5de1b76afe51ec909ec9f5991e676edbcf06. I configured and built the relinker-only Release target locally; red_zone_framer, rewrite_boundaries, cli_args and game_patches all pass (4/4), and exact-head diff/conventions checks pass. The opcode-encoded register handling correctly distinguishes RSP/ESP/SP/SPL from REX.B-extended registers and preserves AH-versus-SPL behavior; the added cases cover both skipped stack-register uses and eligible non-stack controls. Hosted Ubuntu, Windows, macOS arm64, macOS x86_64 and ASan relinker jobs pass; product jobs are skipped by path policy.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643792999 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10999#pullrequestreview-5643792999). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T15:06:32Z. **Review:** APPROVED --- Reviewed exact head `de2b5de1b76afe51ec909ec9f5991e676edbcf06`. I configured and built the relinker-only Release target locally; `red_zone_framer`, `rewrite_boundaries`, `cli_args` and `game_patches` all pass (4/4), and exact-head diff/conventions checks pass. The opcode-encoded register handling correctly distinguishes RSP/ESP/SP/SPL from REX.B-extended registers and preserves AH-versus-SPL behavior; the added cases cover both skipped stack-register uses and eligible non-stack controls. Hosted Ubuntu, Windows, macOS arm64, macOS x86_64 and ASan relinker jobs pass; product jobs are skipped by path policy.
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!10999
No description provided.