relinker: x86 decoder returns a length 2 bytes short for 0x66 + REX.W instructions with an imm32 #1162

Closed
opened 2026-10-07 15:52:34 +00:00 by oguzhanozfe · 0 comments
oguzhanozfe commented 2026-10-07 15:52:34 +00:00 (Migrated from github.com)
Contributor

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


X64InstructionDecoder::Decode sizes the immediate as imm16 whenever the 0x66 operand-size prefix is present, even when REX.W is also present. In 64-bit mode REX.W takes precedence over 0x66 (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 already applies this rule (core/relinker/codegen/src/x86/X64InstructionDecoder.cpp:175, covered by 66 48 B8 imm64 in decoderLengths). The other imm32 opcodes at line 178 and the F7 /0 TEST form at line 282 do not.

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

DecodeInstruction takes its length from Decode, so it is wrong too. InstructionScanner sweeps linearly, so the bytes after the short length are decoded as the next instruction and the rest of the sweep is out of step with the real instruction stream.

Controls that are decoded correctly: 66 81 C0 iw (5), 48 66 81 C0 iw (REX not directly before the opcode is ignored, 6), 66 41 81 C0 iw (REX without W, 6), 66 48 B8 imm64 (11).

Reproduced by compiling the current X64InstructionDecoder.cpp (main at 4e742835) with MSVC 14.50 and calling Decode on each sequence followed by eight 0x90 bytes.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5747849400 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1162). Originally posted by [oguzhanozfe](https://github.com/oguzhanozfe) on 2026-10-07T15:52:34Z. --- `X64InstructionDecoder::Decode` sizes the immediate as imm16 whenever the 0x66 operand-size prefix is present, even when REX.W is also present. In 64-bit mode REX.W takes precedence over 0x66 (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 already applies this rule (`core/relinker/codegen/src/x86/X64InstructionDecoder.cpp:175`, covered by `66 48 B8 imm64` in `decoderLengths`). The other imm32 opcodes at line 178 and the `F7 /0` TEST form at line 282 do not. | Bytes | Instruction | Expected | `Decode` | |---|---|---|---| | `66 48 81 C0 imm32` | add rax, imm32 | 8 | 6 | | `66 48 05 imm32` | add rax, imm32 | 7 | 5 | | `66 48 C7 C0 imm32` | mov rax, imm32 | 8 | 6 | | `66 48 69 C0 imm32` | imul rax, rax, imm32 | 8 | 6 | | `66 48 68 imm32` | push imm32 | 7 | 5 | | `66 48 A9 imm32` | test rax, imm32 | 7 | 5 | | `66 48 F7 C0 imm32` | test rax, imm32 | 8 | 6 | `DecodeInstruction` takes its length from `Decode`, so it is wrong too. `InstructionScanner` sweeps linearly, so the bytes after the short length are decoded as the next instruction and the rest of the sweep is out of step with the real instruction stream. Controls that are decoded correctly: `66 81 C0 iw` (5), `48 66 81 C0 iw` (REX not directly before the opcode is ignored, 6), `66 41 81 C0 iw` (REX without W, 6), `66 48 B8 imm64` (11). Reproduced by compiling the current `X64InstructionDecoder.cpp` (main at 4e742835) with MSVC 14.50 and calling `Decode` on each sequence followed by eight 0x90 bytes.
Sign in to join this conversation.
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#1162
No description provided.