fix(relinker): let REX.W override 0x66 when sizing decoder immediates #1163

Merged
oguzhanozfe merged 1 commit from upstream-pr/1163 into main 2026-10-08 08:23:36 +00:00
oguzhanozfe commented 2026-10-07 15:52:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oguzhanozfe on 2026-10-07T15:52:54Z.


What

Fixes #1162.

In 64-bit mode REX.W takes precedence over the 0x66 operand-size prefix (Intel SDM Vol. 1 §3.6.1, Table 3-4; Vol. 2A §2.2.1.2). The operand size is 64 bits, so the immediate of 81 /n, C7 /0, 69, 68, A9, the 05..3D accumulator forms and the F7 /0 TEST form stays imm32. X64InstructionDecoder::Decode applied this only to B8+r and returned a length two bytes short for the others.

The decoder now computes the operand-size immediate once after the prefix loop and uses it at the three sites. The decoderLengths test gains the seven 66 48 forms and four controls: 66 alone, a REX before 66 (ignored), REX without W, and 66 F7 TEST.

Tested

  • Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs x86_64-ucrt-posix-seh r7), configured like CI (-G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON). I built core/relinker/all and ran its ctest suite. On this branch 29/29 tests pass. With only the fix reverted, amd64_only_converter fails, so the new cases catch the bug.
  • Windows 11, MSVC 14.50: I compiled X64InstructionDecoder.cpp with a standalone program that runs the new decoderLengths sequences through Decode and DecodeInstruction.
    • Before the fix, all 7 66 48 cases were 2 bytes short and the controls passed.
    • After the fix, all cases pass with no warnings.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4775087816 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1163). Originally posted by [oguzhanozfe](https://github.com/oguzhanozfe) on 2026-10-07T15:52:54Z. --- ### What Fixes #1162. In 64-bit mode REX.W takes precedence over the 0x66 operand-size prefix (Intel SDM Vol. 1 §3.6.1, Table 3-4; Vol. 2A §2.2.1.2). The operand size is 64 bits, so the immediate of `81 /n`, `C7 /0`, `69`, `68`, `A9`, the `05`..`3D` accumulator forms and the `F7 /0` TEST form stays imm32. `X64InstructionDecoder::Decode` applied this only to `B8+r` and returned a length two bytes short for the others. The decoder now computes the operand-size immediate once after the prefix loop and uses it at the three sites. The `decoderLengths` test gains the seven `66 48` forms and four controls: `66` alone, a REX before `66` (ignored), REX without W, and `66 F7` TEST. ### Tested - Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs `x86_64-ucrt-posix-seh` r7), configured like CI (`-G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON`). I built `core/relinker/all` and ran its ctest suite. On this branch 29/29 tests pass. With only the fix reverted, `amd64_only_converter` fails, so the new cases catch the bug. - Windows 11, MSVC 14.50: I compiled `X64InstructionDecoder.cpp` with a standalone program that runs the new `decoderLengths` sequences through `Decode` and `DecodeInstruction`. - Before the fix, all 7 `66 48` cases were 2 bytes short and the controls passed. - After the fix, all cases pass with no warnings. ### 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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Dextroid17 commented 2026-10-07 16:00:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-07T16:00:25Z.


Independent check on Linux, because the diagnosis is reproducible without MSVC.

I compiled X64InstructionDecoder.cpp from main and from this PR into a standalone harness (g++ -std=c++20 -O0, includes codegen/include + domain/include) and ran your seven new sequences plus the four controls through Decode, checking the decoded length against the byte count:

sequence main this PR
66 48 81 C0 imm32 6 (want 8) 8
66 48 05 imm32 5 (want 7) 7
66 48 C7 C0 imm32 6 (want 8) 8
66 48 69 C0 imm32 6 (want 8) 8
66 48 68 imm32 5 (want 7) 7
66 48 A9 imm32 5 (want 7) 7
66 48 F7 C0 imm32 6 (want 8) 8
66 81 C0 iw 5 5
48 66 81 C0 iw 6 6
66 41 81 C0 iw 6 6
66 F7 C0 iw 5 5

The seven cases are two bytes short on main and all fourteen pass here, with the controls unchanged — so the added cases do fail without the change, and the change touches nothing else. 66 48 81 C0 imm32 giving 6 also confirms the downstream effect the report describes: InstructionScanner decodes the trailing two bytes of the immediate as the next instruction.

I also swept the decoder for other operand-size immediate sites this rule could reach. There are none left: CALL/JMP rel32 (line 190) is always rel32 in 64-bit and RET imm16 / ENTER (lines 201-206) are fixed-width by definition, so neither is affected by 0x66; every other immediate in the table is imm8. The three sites you changed (the imm32 group, the B8+r sibling, and F7 /0) are the whole set.

One note for the record: the added sequences sit inside decoderLengths, which runs as part of the registered amd64_only_converter ctest target, so CI does execute them on Linux — the missing MinGW toolchain is only a local gap.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6041699732 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1163#issuecomment-6041699732). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-07T16:00:25Z. --- Independent check on Linux, because the diagnosis is reproducible without MSVC. I compiled `X64InstructionDecoder.cpp` from `main` and from this PR into a standalone harness (`g++ -std=c++20 -O0`, includes `codegen/include` + `domain/include`) and ran your seven new sequences plus the four controls through `Decode`, checking the decoded length against the byte count: | sequence | `main` | this PR | | --- | --- | --- | | `66 48 81 C0 imm32` | 6 (want 8) | **8** | | `66 48 05 imm32` | 5 (want 7) | **7** | | `66 48 C7 C0 imm32` | 6 (want 8) | **8** | | `66 48 69 C0 imm32` | 6 (want 8) | **8** | | `66 48 68 imm32` | 5 (want 7) | **7** | | `66 48 A9 imm32` | 5 (want 7) | **7** | | `66 48 F7 C0 imm32` | 6 (want 8) | **8** | | `66 81 C0 iw` | 5 | 5 | | `48 66 81 C0 iw` | 6 | 6 | | `66 41 81 C0 iw` | 6 | 6 | | `66 F7 C0 iw` | 5 | 5 | The seven cases are two bytes short on `main` and all fourteen pass here, with the controls unchanged — so the added cases do fail without the change, and the change touches nothing else. `66 48 81 C0 imm32` giving 6 also confirms the downstream effect the report describes: `InstructionScanner` decodes the trailing two bytes of the immediate as the next instruction. I also swept the decoder for other operand-size immediate sites this rule could reach. There are none left: `CALL/JMP rel32` (line 190) is always rel32 in 64-bit and `RET imm16` / `ENTER` (lines 201-206) are fixed-width by definition, so neither is affected by `0x66`; every other immediate in the table is imm8. The three sites you changed (the imm32 group, the `B8+r` sibling, and `F7 /0`) are the whole set. One note for the record: the added sequences sit inside `decoderLengths`, which runs as part of the registered `amd64_only_converter` ctest target, so CI does execute them on Linux — the missing MinGW toolchain is only a local gap.
adriadam10 commented 2026-10-08 08:23:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T08:23:33Z.


Checked: built on top of current main, ctest 424/424 green. Reverting only the decoder change makes amd64_only_converter fail, so the new 66 48 cases and controls pin the fix. The REX-before-66 control matches the decoder resetting rexPresent on later prefixes. CI green on the head; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6055828863 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1163#issuecomment-6055828863). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T08:23:33Z. --- Checked: built on top of current `main`, ctest 424/424 green. Reverting only the decoder change makes `amd64_only_converter` fail, so the new `66 48` cases and controls pin the fix. The REX-before-`66` control matches the decoder resetting `rexPresent` on later prefixes. CI green on the head; merging.
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!1163
No description provided.