fix(relinker): decode the MOV moffs address and give REX.W precedence over 0x66 #1226

Closed
Dextroid17 wants to merge 1 commit from upstream-pr/1226 into main
Dextroid17 commented 2026-10-07 18:27:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-07T18:27:44Z.


What

Fixes #1162 and #1167. Both are X64InstructionDecoder::Decode returning a short length — one for an immediate it sized wrong, one for an address it never counted — so they are one change to one function.

#1162 — REX.W wins over 0x66

Decode sized the immediate as imm16 whenever the 0x66 operand-size prefix was present, even when REX.W was too. In 64-bit mode REX.W takes precedence (Intel SDM Vol. 1 §3.6.1 Table 3-4; Vol. 2A §2.2.1.2), so the operand size is 64 bits and the immediate stays imm32. The B8+r case in the same function already applied that rule; the imm32 opcodes and the F7 /0 TEST form did not, and DecodeInstruction and InstructionScanner inherited the short length.

Bytes Instruction Before Now
66 48 81 C0 imm32 add rax, imm32 6 8
66 48 05 imm32 add rax, imm32 5 7
66 48 C7 C0 imm32 mov rax, imm32 6 8
66 48 69 C0 imm32 imul rax, rax, imm32 6 8
66 48 68 imm32 push imm32 5 7
66 48 A9 imm32 test rax, imm32 5 7
66 48 F7 C0 imm32 test rax, imm32 6 8

The four controls the report lists as already correct are in the test and unchanged: 66 81 C0 iw (5), 48 66 81 C0 iw (REX not directly before the opcode, 6), 66 41 81 C0 iw (REX without W, 6), 66 48 B8 imm64 (11).

#1167 — the MOV moffs address

One-byte A0-A3 had no case, so the 8-byte address (4 bytes with 0x67) was never counted. Decode now records the 0x67 prefix and sizes the operand at imm64, or imm32 with 0x67.

Bytes Before Now
A1 + 8-byte address 1 9
48 A1 + 8-byte address 2 10
48 A3 + 8-byte address 2 10
64 48 A1 + 8-byte address 3 11
67 A1 + 4-byte address 2 6

This is the one with a visible consequence: InstructionScanner sweeps linearly, so for 48 A1 66 0F 78 C0 08 00 00 00 C3 it reported an EXTRQ inside the address, which --to-intel would have rewritten.

Tested

Linux (CachyOS), Release, lavapipe with GALLIUM_OVERRIDE_CPU_CAPS=avx and ANYPS5_REQUIRE_VULKAN=1: the full suite reports 100% tests passed out of 399. Windows through CI.

Not tested: no title, and I did not re-run the reporter's MSVC reproduction; the sequences are decoded on Linux instead.

The new block in decoderLengths covers all fifteen sequences above and fails on the old decoder rather than merely passing on the new one: with X64InstructionDecoder.cpp reverted it reports An immediate or moffs operand was decoded with the wrong length and exits 1.

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:4776779328 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1226). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-07T18:27:44Z. --- ### What Fixes #1162 and #1167. Both are `X64InstructionDecoder::Decode` returning a short length — one for an immediate it sized wrong, one for an address it never counted — so they are one change to one function. #### #1162 — REX.W wins over 0x66 `Decode` sized the immediate as imm16 whenever the 0x66 operand-size prefix was present, even when REX.W was too. In 64-bit mode REX.W takes precedence (Intel SDM Vol. 1 §3.6.1 Table 3-4; Vol. 2A §2.2.1.2), so the operand size is 64 bits and the immediate stays imm32. The `B8+r` case in the same function already applied that rule; the imm32 opcodes and the `F7 /0` TEST form did not, and `DecodeInstruction` and `InstructionScanner` inherited the short length. | Bytes | Instruction | Before | Now | |---|---|---|---| | `66 48 81 C0 imm32` | add rax, imm32 | 6 | 8 | | `66 48 05 imm32` | add rax, imm32 | 5 | 7 | | `66 48 C7 C0 imm32` | mov rax, imm32 | 6 | 8 | | `66 48 69 C0 imm32` | imul rax, rax, imm32 | 6 | 8 | | `66 48 68 imm32` | push imm32 | 5 | 7 | | `66 48 A9 imm32` | test rax, imm32 | 5 | 7 | | `66 48 F7 C0 imm32` | test rax, imm32 | 6 | 8 | The four controls the report lists as already correct are in the test and unchanged: `66 81 C0 iw` (5), `48 66 81 C0 iw` (REX not directly before the opcode, 6), `66 41 81 C0 iw` (REX without W, 6), `66 48 B8 imm64` (11). #### #1167 — the MOV moffs address One-byte `A0`-`A3` had no case, so the 8-byte address (4 bytes with `0x67`) was never counted. `Decode` now records the `0x67` prefix and sizes the operand at imm64, or imm32 with `0x67`. | Bytes | Before | Now | |---|---|---| | `A1` + 8-byte address | 1 | 9 | | `48 A1` + 8-byte address | 2 | 10 | | `48 A3` + 8-byte address | 2 | 10 | | `64 48 A1` + 8-byte address | 3 | 11 | | `67 A1` + 4-byte address | 2 | 6 | This is the one with a visible consequence: `InstructionScanner` sweeps linearly, so for `48 A1 66 0F 78 C0 08 00 00 00 C3` it reported an EXTRQ inside the address, which `--to-intel` would have rewritten. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. --> Linux (CachyOS), Release, lavapipe with `GALLIUM_OVERRIDE_CPU_CAPS=avx` and `ANYPS5_REQUIRE_VULKAN=1`: the full suite reports `100% tests passed out of 399`. Windows through CI. Not tested: no title, and I did not re-run the reporter's MSVC reproduction; the sequences are decoded on Linux instead. The new block in `decoderLengths` covers all fifteen sequences above and fails on the old decoder rather than merely passing on the new one: with `X64InstructionDecoder.cpp` reverted it reports `An immediate or moffs operand was decoded with the wrong length` and exits 1. ### 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 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
Dextroid17 commented 2026-10-07 19:09:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-07T19:09:35Z.


Closing this — both halves are already covered by open PRs, and both were opened first.

  • The REX.W-over-0x66 half (#1162) is #1163 by @oguzhanozfe. It is equivalent and I checked it against mine: the same seven imm32 opcodes, the same F7 /0 TEST form, the same test vectors and controls, and a clearer formulation (operandImmediateSize computed once) than the per-branch booleans I used here.
  • The MOV moffs half (#1167) is #1211 by @emirplus. Same constants, same addressSizeOverride tracking, same sizing, same five test vectors — plus a scannerMovMoffs test that pins the scanner consequence directly, which this PR did not.

Two PRs for one change is two reviews for one result, so I would rather not spend yours.

Sorry for the noise, @oguzhanozfe and @emirplus — I read the issues and wrote the fix without first checking the open PRs, which is my mistake and now a standing step for me.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6044932518 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1226#issuecomment-6044932518). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-07T19:09:35Z. --- Closing this — **both halves are already covered by open PRs**, and both were opened first. - **The REX.W-over-0x66 half (#1162)** is #1163 by @oguzhanozfe. It is equivalent and I checked it against mine: the same seven imm32 opcodes, the same `F7 /0` TEST form, the same test vectors and controls, and a clearer formulation (`operandImmediateSize` computed once) than the per-branch booleans I used here. - **The MOV moffs half (#1167)** is #1211 by @emirplus. Same constants, same `addressSizeOverride` tracking, same sizing, same five test vectors — plus a `scannerMovMoffs` test that pins the scanner consequence directly, which this PR did not. Two PRs for one change is two reviews for one result, so I would rather not spend yours. Sorry for the noise, @oguzhanozfe and @emirplus — I read the issues and wrote the fix without first checking the open PRs, which is my mistake and now a standing step for me.

Pull request closed

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!1226
No description provided.