fix(relinker): stop reading pop r/m without REX as XOP in red-zone framing #11063

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

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


What

extensionBits and memoryUse in core/relinker/codegen/src/x86/RedZoneFramer.cpp tell VEX, EVEX and XOP encodings apart by the lead byte, and both took every 8F without REX for an XOP prefix. 8F is also pop r/m (8F /0). For it, the ModRM byte was read as XOP's inverted R, X and B bits. Bit 5 of a /0 ModRM byte is always clear, so B was always set, and the base of pop qword [rsp-0x10] (8f 44 24 f0) looked like R12. memoryUse also skipped its general-purpose register checks, so pop rsp through 8f c4 did not count as a use of RSP. As a result, --frame-red-zone framed a leaf with a frame pointer that pops to an RSP operand, with the operand left unmoved, and framed leaves of both kinds that pop into RSP through 8F. The REX.W forms (48 8f 44 24 f0) are refused as other use of rsp. With the framer of untouched main (RedZoneFramer.cpp is the same from 3a2c51afd2 to 0119103fa9), the leaf push rbp; mov rbp, rsp; push r15; push rbx; mov [rbp-0x20], rcx; push rdx; pop qword [rsp-0x10]; mov rax, [rbp-0x20]; pop rbx; pop r15; pop rbp; ret, framed with FrameRedZoneFunctions and run natively on Windows, returns RCX instead of RDX, because after framing the pop stores 0x80 bytes lower. No framer test contained a pop r/m.

  • Rule. xopPrefixed takes 8F for XOP only when the map field (the low five bits) of the next byte is 8 or more. This is the rule X64InstructionDecoder already applies (XopMapMin), and the one AMD's APM gives for telling XOP from POP Ev. extensionBits and the vex flag of memoryUse now use it, so pop r/m without REX goes through the same checks as its REX forms. In a leaf with a frame pointer, an RSP operand or pop rsp is other use of rsp. In a leaf without one, pop rsp is other use of rsp, and a pop to an RSP operand has its operand moved like any other RSP operand. That such a leaf also moves RSP in its body is what #11061 refuses.
  • Tests. tellsPopsWithoutRexFromXop in RedZoneFramerTests.cpp has two parts:
    • Three leaves must be refused as other use of rsp: a leaf with a frame pointer with push rdx; pop qword [rsp-0x10], one with push rdx; pop rsp (8f c4), and a leaf without a frame pointer with pop rsp. All three are framed on main, where the test fails at its first require.
    • Two XOP controls in leaves without a frame pointer must still be read as XOP: vprotb xmm0, [r12], 1 (8f c8 78 c0 04 24 01, XOP.B clear) is not an RSP operand and needs no stub, and vprotb xmm0, [rsp-0x10], 1 (8f e8 78 c0 44 24 f0 01) is moved.
  • Overlap. pr_overlap.py reports no overlap with open pull requests. Three other changes touch the same file: #10999 changes the start of memoryUse, #10871 changes the exits and prologues, and #11061 adds the push and pop check. This branch merges cleanly with each, and red_zone_framer passes on each merge.

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 fails with a pop without REX to an rsp operand or into rsp, read as an XOP prefix, was framed; with the change it passes. Run leaf by leaf, main frames all three pop leaves, and frames the two XOP controls with the same moved operands as the change.
  • Mutants: dropping XOP recognition altogether, or reverting either of the two call sites, each fails the test.
  • 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 leaf above returns 0x1111 instead of 0x2222. With the change it is refused and returns 0x2222.
    • The same leaf with the REX.W form is refused on both and returns 0x2222.
  • objdump -M intel decodes the test bytes as described: 8f 44 24 f0 is pop QWORD PTR [rsp-0x10], 8f c4 is pop rsp, and 8f c8 78 c0 04 24 01 is vprotb xmm0, [r12], 0x1. The relinker's decoder gives the same lengths.
  • Full suite: ctest -j 8: 292 tests, 289 passed, long_paths skipped. Two failures come from this machine, not from the change: patch_advisor_unit fails because the path contains spaces (fixed by open #10667), and pr_overlap_encoding fails because the build directory is outside the git checkout; run from the checkout, it passes.
  • python tools/check_conventions.py --base upstream/main with this title and description passed.
  • 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:5006477869 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11063). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-25T16:22:11Z. --- ### What `extensionBits` and `memoryUse` in `core/relinker/codegen/src/x86/RedZoneFramer.cpp` tell VEX, EVEX and XOP encodings apart by the lead byte, and both took every `8F` without REX for an XOP prefix. `8F` is also `pop r/m` (`8F /0`). For it, the ModRM byte was read as XOP's inverted R, X and B bits. Bit 5 of a `/0` ModRM byte is always clear, so B was always set, and the base of `pop qword [rsp-0x10]` (`8f 44 24 f0`) looked like R12. `memoryUse` also skipped its general-purpose register checks, so `pop rsp` through `8f c4` did not count as a use of RSP. As a result, `--frame-red-zone` framed a leaf with a frame pointer that pops to an RSP operand, with the operand left unmoved, and framed leaves of both kinds that pop into RSP through `8F`. The REX.W forms (`48 8f 44 24 f0`) are refused as `other use of rsp`. With the framer of untouched `main` (`RedZoneFramer.cpp` is the same from 3a2c51afd2 to 0119103fa9), the leaf `push rbp; mov rbp, rsp; push r15; push rbx; mov [rbp-0x20], rcx; push rdx; pop qword [rsp-0x10]; mov rax, [rbp-0x20]; pop rbx; pop r15; pop rbp; ret`, framed with `FrameRedZoneFunctions` and run natively on Windows, returns RCX instead of RDX, because after framing the pop stores 0x80 bytes lower. No framer test contained a `pop r/m`. - **Rule.** `xopPrefixed` takes `8F` for XOP only when the map field (the low five bits) of the next byte is 8 or more. This is the rule `X64InstructionDecoder` already applies (`XopMapMin`), and the one AMD's APM gives for telling XOP from `POP Ev`. `extensionBits` and the `vex` flag of `memoryUse` now use it, so `pop r/m` without REX goes through the same checks as its REX forms. In a leaf with a frame pointer, an RSP operand or `pop rsp` is `other use of rsp`. In a leaf without one, `pop rsp` is `other use of rsp`, and a pop to an RSP operand has its operand moved like any other RSP operand. That such a leaf also moves RSP in its body is what #11061 refuses. - **Tests.** `tellsPopsWithoutRexFromXop` in `RedZoneFramerTests.cpp` has two parts: - Three leaves must be refused as `other use of rsp`: a leaf with a frame pointer with `push rdx; pop qword [rsp-0x10]`, one with `push rdx; pop rsp` (`8f c4`), and a leaf without a frame pointer with `pop rsp`. All three are framed on `main`, where the test fails at its first `require`. - Two XOP controls in leaves without a frame pointer must still be read as XOP: `vprotb xmm0, [r12], 1` (`8f c8 78 c0 04 24 01`, XOP.B clear) is not an RSP operand and needs no stub, and `vprotb xmm0, [rsp-0x10], 1` (`8f e8 78 c0 44 24 f0 01`) is moved. - **Overlap.** `pr_overlap.py` reports no overlap with open pull requests. Three other changes touch the same file: #10999 changes the start of `memoryUse`, #10871 changes the exits and prologues, and #11061 adds the push and pop check. This branch merges cleanly with each, and `red_zone_framer` passes on each merge. ### 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` fails with `a pop without REX to an rsp operand or into rsp, read as an XOP prefix, was framed`; with the change it passes. Run leaf by leaf, `main` frames all three pop leaves, and frames the two XOP controls with the same moved operands as the change. - Mutants: dropping XOP recognition altogether, or reverting either of the two call sites, each fails the test. - 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 leaf above returns 0x1111 instead of 0x2222. With the change it is refused and returns 0x2222. - The same leaf with the REX.W form is refused on both and returns 0x2222. - `objdump -M intel` decodes the test bytes as described: `8f 44 24 f0` is `pop QWORD PTR [rsp-0x10]`, `8f c4` is `pop rsp`, and `8f c8 78 c0 04 24 01` is `vprotb xmm0, [r12], 0x1`. The relinker's decoder gives the same lengths. - Full suite: `ctest -j 8`: 292 tests, 289 passed, `long_paths` skipped. Two failures come from this machine, not from the change: `patch_advisor_unit` fails because the path contains spaces (fixed by open #10667), and `pr_overlap_encoding` fails because the build directory is outside the git checkout; run from the checkout, it passes. - `python tools/check_conventions.py --base upstream/main` with this title and description passed. - 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:37:13 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed head 3398046c3c. The 0x8F discriminator matches the decoder’s existing XOP map threshold, so POP /0 no longer inherits XOP extension bits while valid XOP forms retain them. The added regressions cover POP to an RSP-based memory operand, POP RSP, and XOP controls. The relinker matrix and ASan checks are green on Linux, Windows, and macOS, and conventions, eligibility, intake, and labels pass. I found no blocking issue in the reviewed diff.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5644158901 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11063#pullrequestreview-5644158901). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T16:37:13Z. **Review:** APPROVED --- Reviewed head 3398046c3c57a7af50861eecb5abd2e489a52fb6. The 0x8F discriminator matches the decoder’s existing XOP map threshold, so POP /0 no longer inherits XOP extension bits while valid XOP forms retain them. The added regressions cover POP to an RSP-based memory operand, POP RSP, and XOP controls. The relinker matrix and ASan checks are green on Linux, Windows, and macOS, and conventions, eligibility, intake, and labels pass. I found no blocking issue in the reviewed diff.
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!11063
No description provided.