fix(relinker): classify rewrite reference sites with the instruction decoder #1393

Merged
xsvm merged 2 commits from upstream-pr/1393 into main 2026-10-08 13:03:48 +00:00
xsvm commented 2026-10-08 01:39:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xsvm on 2026-10-08T01:39:36Z.


What

X64InstructionRewriter::ClassifyInstruction assumed that every opcode other than a near branch is followed by a ModRM byte. During a length-changing rewrite this:

  • adjusted immediates that happen to look like a RIP-relative ModRM, e.g. MOVABS rax, imm64 whose low byte is 05;
  • missed RIP-relative operands of VEX (C5/C4) and 0F 38 / 0F 3A instructions;
  • missed the rel8 targets of LOOP/LOOPcc/JRCXZ (E0-E3).

It now takes the RIP-relative displacement offset and the branch target from X64InstructionDecoder::DecodeInstruction. Rel32InstructionLength became unused and is removed.

The converter currently only rewrites in place with equal length, so this affects the Rewrite contract and length-changing callers (as exercised by rewriterStrayRex), not today's --to-intel output.

The rewriter entry in TechnicalDebt no longer lists VEX/0F38/0F3A RIP-relative operands among the references it does not adjust.

Tested

  • Windows 11, MinGW-w64 GCC 13.2: built amd64_only_converter_tests from the CMake source list; the new rewriterReferenceSites cases (MOVABS imm64, VEX RIP load, 66 0F 38 00 RIP load, LOOP rel8) fail on current main and pass with the fix. The rest of the suite, including the *Execution tests, passes.
  • DecodeInstruction's RIP-relative flag/offset and branch displacement match Capstone 5 on 2.6M instructions from system DLLs and 27k synthetic encodings including EVEX.
  • Rebased onto main after #1218 was merged; its TechnicalDebt edit is on the adjacent line and both edits are kept. #1163, #1168, #1303 and #1381 are already in main.
  • Trial-merged with the open #1457, which touches the same test file: no conflicts, tests pass.
  • python tools/check_conventions.py --base upstream/main passes.

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
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4780617629 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1393). Originally posted by [xsvm](https://github.com/xsvm) on 2026-10-08T01:39:36Z. --- ### What `X64InstructionRewriter::ClassifyInstruction` assumed that every opcode other than a near branch is followed by a ModRM byte. During a length-changing rewrite this: - adjusted immediates that happen to look like a RIP-relative ModRM, e.g. `MOVABS rax, imm64` whose low byte is `05`; - missed RIP-relative operands of VEX (`C5`/`C4`) and `0F 38` / `0F 3A` instructions; - missed the rel8 targets of LOOP/LOOPcc/JRCXZ (`E0`-`E3`). It now takes the RIP-relative displacement offset and the branch target from `X64InstructionDecoder::DecodeInstruction`. `Rel32InstructionLength` became unused and is removed. The converter currently only rewrites in place with equal length, so this affects the `Rewrite` contract and length-changing callers (as exercised by `rewriterStrayRex`), not today's `--to-intel` output. The rewriter entry in TechnicalDebt no longer lists VEX/0F38/0F3A RIP-relative operands among the references it does not adjust. ### Tested - Windows 11, MinGW-w64 GCC 13.2: built `amd64_only_converter_tests` from the CMake source list; the new `rewriterReferenceSites` cases (MOVABS imm64, VEX RIP load, `66 0F 38 00` RIP load, LOOP rel8) fail on current `main` and pass with the fix. The rest of the suite, including the `*Execution` tests, passes. - `DecodeInstruction`'s RIP-relative flag/offset and branch displacement match Capstone 5 on 2.6M instructions from system DLLs and 27k synthetic encodings including EVEX. - Rebased onto `main` after #1218 was merged; its TechnicalDebt edit is on the adjacent line and both edits are kept. #1163, #1168, #1303 and #1381 are already in `main`. - Trial-merged with the open #1457, which touches the same test file: no conflicts, tests pass. - `python tools/check_conventions.py --base upstream/main` passes. ### 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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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 - [x] AI-assisted: yes
adriadam10 commented 2026-10-08 13:03:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T13:03:45Z.


Checked: build + full ctest on this head merged onto current main (452/452). With the code change reverted and the test kept, amd64_only_converter fails, so the new rewriterReferenceSites cases (imm64 05, VEX and 0F 38 RIP loads, LOOP rel8) pin the fix. ClassifyInstruction only receives the matched instruction length, so the decoder never reads past it; FF /2 and FF /4 RIP-relative still go through the RIP branch. The TechnicalDebt line is correct. Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6060486220 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1393#issuecomment-6060486220). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T13:03:45Z. --- Checked: build + full ctest on this head merged onto current `main` (452/452). With the code change reverted and the test kept, `amd64_only_converter` fails, so the new `rewriterReferenceSites` cases (imm64 `05`, VEX and `0F 38` RIP loads, LOOP rel8) pin the fix. `ClassifyInstruction` only receives the matched instruction length, so the decoder never reads past it; `FF /2` and `FF /4` RIP-relative still go through the RIP branch. The TechnicalDebt line is correct. Merging once CI is green.
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!1393
No description provided.