fix(relinker): detect ModRM and vector RSP operands in RedZoneFramer #16432

Merged
Dinsuljevic336 merged 1 commit from upstream-pr/16432 into main 2026-11-12 22:01:51 +00:00
Dinsuljevic336 commented 2026-11-06 17:23:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dinsuljevic336 on 2026-11-06T17:23:50Z.


What

Classify missed direct RSP general-purpose register operands (and stop misclassifying F3 0F 7E movq xmm, xmm4 as an ESP read) in RedZoneFramer::memoryUse (core/relinker/codegen/src/x86/RedZoneFramer.cpp).

When --frame-red-zone lowers RSP at leaf entry and raises it at leaf exit, any leaf that reads or writes RSP directly outside the recognized prologue/epilogue adjustments must be skipped (other use of rsp) rather than framed with a shifted RSP. Previously, memoryUse missed several instruction families whose ModRM.reg, ModRM.rm (mod == 3), or XOP.vvvv fields encode RSP/ESP/SP/SPL, while treating F3 0F 7E (movq xmm, xmm/m64) as a GPR rm read:

  • 1-byte ModRM.reg (mod != 3): movsxd rsp, m32 (0x63) and 3-operand imul rsp, m, imm (0x69, 0x6B).
  • 2-byte 0F scalar and vector-to/from-GPR forms: lar/lsl (0F 02/03), sldt/str/smsw (0F 00/01), cvtsi2ss/cvtsi2sd (F3/F2 0F 2A), cvt[t]ss2si/cvt[t]sd2si (F3/F2 0F 2C/2D), movmskps/movmskpd (0F 50, 66 0F 50), setcc spl (40 0F 90..9F), rdfsbase/rdgsbase/wrfsbase/wrgsbase (F3 0F AE /0..3), pinsrw/pextrw (0F C4/C5), rdrand/rdseed/rdpid (0F C7 /6..7), and pmovmskb (0F D7), while distinguishing 66 0F 7E / 0F 7E (movd/movq to GPR) from F3 0F 7E (movq xmm, xmm4).
  • 3-byte 0F 38 / 0F 3A forms (where DecodeInstruction records the escape byte 0x38 / 0x3A in info.Opcode and the opcode byte at OpcodeOffset + 2): movbe (0F 38 F0/F1), crc32 (F2 0F 38 F0/F1, including REX-gated spl vs ah), adcx/adox (66/F3 0F 38 F6), movdir64b/movdiri (66 0F 38 F8, 0F 38 F9), pextrb/pextrw/pextrd/pextrq (66 0F 3A 14..16), extractps (66 0F 3A 17), and pinsrb/pinsrd/pinsrq (66 0F 3A 20, 22).
  • VEX / EVEX / XOP GPR forms: vmovd/vmovq (map=1, 0x6E/0x7E with pp=1), vcvtsi2ss/sd and vcvt[t]ss2si/sd2si (0x2A/0x2C/0x2D), vmovmskps/pd (0x50), vpmovmskb (0xD7), vpinsrw/vpextrw (0xC4/0xC5), vpextrb/w/d/q, vextractps, and vpinsrb/d/q (map=3, 0x14..17, 0x20, 0x22), plus XOP TBM (map=9, 0x01/0x02) and immediate bextr (map=10, 0x10).

Tested

Linux x86-64 (GCC 14.2.0, -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON), Release and ASan + UBSan (-fsanitize=address,undefined):

  • Verified red_zone_framer and red_zone_framer_execution fail without the RedZoneFramer.cpp fix (0/2 passed) and pass with the fix (2/2 passed, including real-hardware single-step red-zone clobber execution of movq xmm0, xmm4 and execution verification of skipped vmovq/pinsrq/movbe/imul RSP leaves, plus 557/557 CTest suite passing).
  • Not tested on game titles or homebrew.

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:5191869962 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16432). Originally posted by [Dinsuljevic336](https://github.com/Dinsuljevic336) on 2026-11-06T17:23:50Z. --- ### What Classify missed direct `RSP` general-purpose register operands (and stop misclassifying `F3 0F 7E` `movq xmm, xmm4` as an `ESP` read) in `RedZoneFramer::memoryUse` (`core/relinker/codegen/src/x86/RedZoneFramer.cpp`). When `--frame-red-zone` lowers `RSP` at leaf entry and raises it at leaf exit, any leaf that reads or writes `RSP` directly outside the recognized prologue/epilogue adjustments must be skipped (`other use of rsp`) rather than framed with a shifted `RSP`. Previously, `memoryUse` missed several instruction families whose `ModRM.reg`, `ModRM.rm` (`mod == 3`), or `XOP.vvvv` fields encode `RSP`/`ESP`/`SP`/`SPL`, while treating `F3 0F 7E` (`movq xmm, xmm/m64`) as a GPR `rm` read: - **1-byte `ModRM.reg` (`mod != 3`)**: `movsxd rsp, m32` (`0x63`) and 3-operand `imul rsp, m, imm` (`0x69`, `0x6B`). - **2-byte `0F` scalar and vector-to/from-GPR forms**: `lar`/`lsl` (`0F 02`/`03`), `sldt`/`str`/`smsw` (`0F 00`/`01`), `cvtsi2ss`/`cvtsi2sd` (`F3`/`F2 0F 2A`), `cvt[t]ss2si`/`cvt[t]sd2si` (`F3`/`F2 0F 2C`/`2D`), `movmskps`/`movmskpd` (`0F 50`, `66 0F 50`), `setcc spl` (`40 0F 90..9F`), `rdfsbase`/`rdgsbase`/`wrfsbase`/`wrgsbase` (`F3 0F AE /0..3`), `pinsrw`/`pextrw` (`0F C4`/`C5`), `rdrand`/`rdseed`/`rdpid` (`0F C7 /6..7`), and `pmovmskb` (`0F D7`), while distinguishing `66 0F 7E` / `0F 7E` (`movd`/`movq` to GPR) from `F3 0F 7E` (`movq xmm, xmm4`). - **3-byte `0F 38` / `0F 3A` forms** (where `DecodeInstruction` records the escape byte `0x38` / `0x3A` in `info.Opcode` and the opcode byte at `OpcodeOffset + 2`): `movbe` (`0F 38 F0`/`F1`), `crc32` (`F2 0F 38 F0`/`F1`, including REX-gated `spl` vs `ah`), `adcx`/`adox` (`66`/`F3 0F 38 F6`), `movdir64b`/`movdiri` (`66 0F 38 F8`, `0F 38 F9`), `pextrb`/`pextrw`/`pextrd`/`pextrq` (`66 0F 3A 14..16`), `extractps` (`66 0F 3A 17`), and `pinsrb`/`pinsrd`/`pinsrq` (`66 0F 3A 20`, `22`). - **VEX / EVEX / XOP GPR forms**: `vmovd`/`vmovq` (`map=1`, `0x6E`/`0x7E` with `pp=1`), `vcvtsi2ss`/`sd` and `vcvt[t]ss2si`/`sd2si` (`0x2A`/`0x2C`/`0x2D`), `vmovmskps`/`pd` (`0x50`), `vpmovmskb` (`0xD7`), `vpinsrw`/`vpextrw` (`0xC4`/`0xC5`), `vpextrb`/`w`/`d`/`q`, `vextractps`, and `vpinsrb`/`d`/`q` (`map=3`, `0x14..17`, `0x20`, `0x22`), plus XOP TBM (`map=9`, `0x01`/`0x02`) and immediate `bextr` (`map=10`, `0x10`). ### 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. --> Linux x86-64 (GCC 14.2.0, `-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`), Release and ASan + UBSan (`-fsanitize=address,undefined`): - Verified `red_zone_framer` and `red_zone_framer_execution` fail without the `RedZoneFramer.cpp` fix (`0/2` passed) and pass with the fix (`2/2` passed, including real-hardware single-step red-zone clobber execution of `movq xmm0, xmm4` and execution verification of skipped `vmovq`/`pinsrq`/`movbe`/`imul` `RSP` leaves, plus `557/557` CTest suite passing). - Not tested on game titles or homebrew. ### 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 -->
gabichuuuuu commented 2026-11-08 01:33:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T01:33:31Z.

Review: COMMENTED


Windows check of head 5610f1f9e1, merged onto main 210676ad86 (clean merge).

Setup: Windows 11, WinLibs GCC 15.2, Release, relinker-only.

  • red_zone_framer and red_zone_framer_execution pass 2/2. The execution test ran rather than skipping.
  • Negative control: main's RedZoneFramer.cpp with this PR's two test files fails both: direct rsp operand case 0 was framed and only 5 of 6 leaves were framed.
  • I read the new tables against the SDM/APM encodings for the Zen 2 instruction set and found no wrong entry. The pp split for 0F 6E/7E, the REX gate for setcc/crc32 with spl against ah, the /1–/3 opcode extension excluded from reg for BMI1 F3, and the F2-over-66 priority in legacyPp all match. The gaps I found are privileged or absent on Zen 2 (VERR/VERW, ENQCMD[S], SENDUIPI, CET, AVX-512 usi conversions), so they do not affect PS5 code.

Merge order: this conflicts textually with #16371 in RedZoneFramer.cpp. #16371 merges cleanly with main on its own, so whichever lands second needs a rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5815957274 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16432#pullrequestreview-5815957274). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-08T01:33:31Z. **Review:** COMMENTED --- Windows check of head `5610f1f9e1`, merged onto `main` `210676ad86` (clean merge). **Setup:** Windows 11, WinLibs GCC 15.2, Release, relinker-only. - `red_zone_framer` and `red_zone_framer_execution` pass 2/2. The execution test ran rather than skipping. - Negative control: `main`'s `RedZoneFramer.cpp` with this PR's two test files fails both: `direct rsp operand case 0 was framed` and `only 5 of 6 leaves were framed`. - I read the new tables against the SDM/APM encodings for the Zen 2 instruction set and found no wrong entry. The pp split for `0F 6E`/`7E`, the REX gate for `setcc`/`crc32` with `spl` against `ah`, the `/1`–`/3` opcode extension excluded from `reg` for BMI1 `F3`, and the `F2`-over-`66` priority in `legacyPp` all match. The gaps I found are privileged or absent on Zen 2 (`VERR`/`VERW`, `ENQCMD[S]`, `SENDUIPI`, CET, AVX-512 `usi` conversions), so they do not affect PS5 code. Merge order: this conflicts textually with #16371 in `RedZoneFramer.cpp`. #16371 merges cleanly with `main` on its own, so whichever lands second needs a rebase.
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!16432
No description provided.