fix(relinker): decode 0F A8-AA without a ModRM and MOV CR/DR without a memory operand #1381

Merged
overlordxrz-source merged 1 commit from upstream-pr/1381 into main 2026-10-08 09:35:45 +00:00
overlordxrz-source commented 2026-10-08 01:14:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by overlordxrz-source on 2026-10-08T01:14:35Z.


What

Two groups of two-byte opcodes are sized with bytes they do not have by X64InstructionDecoder::Decode, and described wrongly by DecodeInstruction:

  • PUSH GS, POP GS and RSM (0F A8, 0F A9, 0F AA) have no ModRM byte, but they are inside the 0F A3-0F AB ModRM range (TwoByteModRmRangeA). The decoder reads the next byte as a ModRM and adds its SIB and displacement. The range now ends at 0F A7, and BTS (0F AB) is listed on its own.
  • MOV to and from control and debug registers (0F 20-0F 23) ignores the ModRM mod field: the operand is always a register (Intel SDM Vol. 2B, MOV—Move to/from Control Registers and Debug Registers). The decoder treated mod 00/01/10 as memory forms, added a SIB and displacement, and DecodeInstruction reported mod 00 r/m 101 as a RIP-relative disp32. The mod field is now ignored for these opcodes. The 0F 38 opcodes 20-23 (PMOVSX*) keep their memory forms.
Bytes Instruction Expected Decode on main
0F A8 push gs 2 3 to 8, by the bytes after it
0F A9 pop gs 2 3 to 8, by the bytes after it
0F AA rsm 2 3 to 8, by the bytes after it
0F 20 05 mov rbp, cr0 3 7 (and reported RIP-relative)
0F 22 04 mov cr0, rsp 3 4 (SIB)
0F 21 45 mov rbp, dr0 3 4 (disp8)

InstructionScanner sweeps linearly, so one of these, or data in .text that looks like one, puts the rest of the sweep out of step. Compilers do not emit them in user code, so this is decoder correctness, not a fix for a title. Expected lengths agree with Capstone 5.0.7.

Tested

aarch64 Debian 13 with clang (Zig 0.16 toolchain). A full CMake build was not possible on this host because the project needs x86-64.

  • amd64_only_converter_tests: every case that does not execute x86 code passes, including the new decoderTwoByteOpcodeLengths, which fails without the fix. The 6 *Execution functions were not run.
  • Differential check against Capstone 5.0.7 on about 64,000 generated encodings (legacy, 0F, 0F38, 0F3A, VEX and EVEX opcodes across prefix and ModRM/SIB forms): the push gs, pop gs, rsm and MOV CR/DR length mismatches are gone, and the only changed outputs are for 0F 20-23 and 0F A8-AA. On 6,782 unique instructions from x86-64 -march=znver2 -O2 objects nothing changed (0 mismatches before and after).
  • X64InstructionDecoder.cpp compiles for x86_64-linux-gnu and x86_64-windows-gnu with -Wall -Wextra -Werror.
  • python3 tools/check_conventions.py --base origin/main passes.
  • Merges cleanly with the open decoder PRs #1163, #1168, #1211 and #1303, and the tests pass when merged with #1163, #1168 and #1303.

Left as they are: 0F B9 (UD1) keeps its ModRM, as in the Intel SDM and LLVM (Capstone decodes it without one); 0F A6/0F A7, which are undefined in 64-bit mode, keep their old ModRM sizing.

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:4780442817 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1381). Originally posted by [overlordxrz-source](https://github.com/overlordxrz-source) on 2026-10-08T01:14:35Z. --- ### What Two groups of two-byte opcodes are sized with bytes they do not have by `X64InstructionDecoder::Decode`, and described wrongly by `DecodeInstruction`: - `PUSH GS`, `POP GS` and `RSM` (`0F A8`, `0F A9`, `0F AA`) have no ModRM byte, but they are inside the `0F A3`-`0F AB` ModRM range (`TwoByteModRmRangeA`). The decoder reads the next byte as a ModRM and adds its SIB and displacement. The range now ends at `0F A7`, and `BTS` (`0F AB`) is listed on its own. - `MOV` to and from control and debug registers (`0F 20`-`0F 23`) ignores the ModRM mod field: the operand is always a register (Intel SDM Vol. 2B, MOV—Move to/from Control Registers and Debug Registers). The decoder treated mod 00/01/10 as memory forms, added a SIB and displacement, and `DecodeInstruction` reported mod 00 r/m 101 as a RIP-relative disp32. The mod field is now ignored for these opcodes. The 0F 38 opcodes 20-23 (`PMOVSX*`) keep their memory forms. | Bytes | Instruction | Expected | `Decode` on main | |---|---|---|---| | `0F A8` | push gs | 2 | 3 to 8, by the bytes after it | | `0F A9` | pop gs | 2 | 3 to 8, by the bytes after it | | `0F AA` | rsm | 2 | 3 to 8, by the bytes after it | | `0F 20 05` | mov rbp, cr0 | 3 | 7 (and reported RIP-relative) | | `0F 22 04` | mov cr0, rsp | 3 | 4 (SIB) | | `0F 21 45` | mov rbp, dr0 | 3 | 4 (disp8) | `InstructionScanner` sweeps linearly, so one of these, or data in `.text` that looks like one, puts the rest of the sweep out of step. Compilers do not emit them in user code, so this is decoder correctness, not a fix for a title. Expected lengths agree with Capstone 5.0.7. ### Tested aarch64 Debian 13 with clang (Zig 0.16 toolchain). A full CMake build was not possible on this host because the project needs x86-64. - `amd64_only_converter_tests`: every case that does not execute x86 code passes, including the new `decoderTwoByteOpcodeLengths`, which fails without the fix. The 6 `*Execution` functions were not run. - Differential check against Capstone 5.0.7 on about 64,000 generated encodings (legacy, 0F, 0F38, 0F3A, VEX and EVEX opcodes across prefix and ModRM/SIB forms): the push gs, pop gs, rsm and MOV CR/DR length mismatches are gone, and the only changed outputs are for `0F 20`-`23` and `0F A8`-`AA`. On 6,782 unique instructions from x86-64 `-march=znver2 -O2` objects nothing changed (0 mismatches before and after). - `X64InstructionDecoder.cpp` compiles for x86_64-linux-gnu and x86_64-windows-gnu with `-Wall -Wextra -Werror`. - `python3 tools/check_conventions.py --base origin/main` passes. - Merges cleanly with the open decoder PRs #1163, #1168, #1211 and #1303, and the tests pass when merged with #1163, #1168 and #1303. Left as they are: `0F B9` (`UD1`) keeps its ModRM, as in the Intel SDM and LLVM (Capstone decodes it without one); `0F A6`/`0F A7`, which are undefined in 64-bit mode, keep their old ModRM sizing. ### 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
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!1381
No description provided.