fix(relinker): report RIP-relative operands of VEX, EVEX and 0F38/0F3A instructions #432

Merged
adriadam10 merged 1 commit from upstream-pr/432 into main 2026-10-04 01:05:25 +00:00
adriadam10 commented 2026-10-03 06:10:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-03T06:10:48Z.


What

X64InstructionDecoder::DecodeInstruction never reported a RIP-relative operand for:

  • VEX/EVEX: it returned before reading the ModRM byte.
  • 0F 38 / 0F 3A: it didn't treat them as having a ModRM byte (the third opcode byte sits where ModRM would be).

What this breaks:

  • --to-intel: an EXTRQ/INSERTQ site shorter than a jmp rel32 takes the next instructions into the stub only if they can move, and RIP-relative ones can't (Amd64OnlyConverter.cpp). A following vmovdqa xmm0, [rip+x] or pshufb xmm0, [rip+x] was copied into the stub with its displacement unchanged, so it loaded from the wrong address without any error. Now the relink fails with "cannot move", like for the other RIP-relative instructions.
  • unused-filter=2 (StrictReachability): references from these instructions to import slots and pointer slots were missing. GOT loads use mov, so I don't know of a title where this changes the result.

Fix: read ModRM after the VEX/EVEX payload and the 0F38/0F3A opcode byte, through one shared helper. The only VEX opcode with no ModRM (0F 77, VZEROUPPER/VZEROALL) is also the whole instruction, so the existing pos < Length guard covers it.

Tested

Linux x86-64, no title.

  • New tests in Amd64OnlyConverterTests.cpp:
    • decoderRipRelative: VEX2, VEX3, EVEX, 0F38 and 0F3A loads report the displacement at the right offset. Register forms, [rax] and VZEROUPPER are not reported.
    • converterRipRelativeFollower: a short register-form EXTRQ followed by each of those loads now fails at the follower's offset. The register forms still go into the stub.
  • Both fail on main. Negative checks: removing each piece (VEX ModRM read, 0F38/0F3A in the ModRM list, the third-byte skip) makes the test fail again.
  • Differential sweep (local harness, not committed): 3 001 860 unique instructions from libavcodec, libcrypto, libLLVM, libx264, libdav1d, libz and libc, checked against objdump (operand [rip±d] and the resolved displacement). On main, 79 827 RIP-relative instructions were not reported. With this PR, 0 missed, 0 false positives, 0 wrong displacement offsets out of 870 483 RIP-relative instructions.
  • Full Release build + ctest: 125/125.

Out of scope, seen in the same sweep: Decode gives EVEX F6/F7 (e.g. vpsadbw zmm) one byte too many because the TEST-immediate rule doesn't exclude EVEX. Zen 2 has no AVX-512, so it can't show up in a PS5 binary.

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 in TechnicalDebt (n/a)
  • 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 (n/a)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4724535595 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/432). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-03T06:10:48Z. --- ### What `X64InstructionDecoder::DecodeInstruction` never reported a RIP-relative operand for: - **VEX/EVEX**: it returned before reading the ModRM byte. - **0F 38 / 0F 3A**: it didn't treat them as having a ModRM byte (the third opcode byte sits where ModRM would be). What this breaks: - `--to-intel`: an EXTRQ/INSERTQ site shorter than a `jmp rel32` takes the next instructions into the stub only if they can move, and RIP-relative ones can't (`Amd64OnlyConverter.cpp`). A following `vmovdqa xmm0, [rip+x]` or `pshufb xmm0, [rip+x]` was copied into the stub with its displacement unchanged, so it loaded from the wrong address without any error. Now the relink fails with "cannot move", like for the other RIP-relative instructions. - `unused-filter=2` (`StrictReachability`): references from these instructions to import slots and pointer slots were missing. GOT loads use `mov`, so I don't know of a title where this changes the result. Fix: read ModRM after the VEX/EVEX payload and the 0F38/0F3A opcode byte, through one shared helper. The only VEX opcode with no ModRM (`0F 77`, VZEROUPPER/VZEROALL) is also the whole instruction, so the existing `pos < Length` guard covers it. ### Tested Linux x86-64, no title. - New tests in `Amd64OnlyConverterTests.cpp`: - `decoderRipRelative`: VEX2, VEX3, EVEX, 0F38 and 0F3A loads report the displacement at the right offset. Register forms, `[rax]` and VZEROUPPER are not reported. - `converterRipRelativeFollower`: a short register-form EXTRQ followed by each of those loads now fails at the follower's offset. The register forms still go into the stub. - Both fail on `main`. Negative checks: removing each piece (VEX ModRM read, 0F38/0F3A in the ModRM list, the third-byte skip) makes the test fail again. - Differential sweep (local harness, not committed): 3 001 860 unique instructions from libavcodec, libcrypto, libLLVM, libx264, libdav1d, libz and libc, checked against `objdump` (operand `[rip±d]` and the resolved displacement). On `main`, 79 827 RIP-relative instructions were not reported. With this PR, 0 missed, 0 false positives, 0 wrong displacement offsets out of 870 483 RIP-relative instructions. - Full Release build + `ctest`: 125/125. Out of scope, seen in the same sweep: `Decode` gives EVEX `F6`/`F7` (e.g. `vpsadbw zmm`) one byte too many because the TEST-immediate rule doesn't exclude EVEX. Zen 2 has no AVX-512, so it can't show up in a PS5 binary. ### Checklist - [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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt (n/a) - [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 (n/a) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
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!432
No description provided.