test(relinker): check decoder lengths against an LLVM-generated table #1552

Merged
arvindfroi merged 2 commits from upstream-pr/1552 into main 2026-10-10 05:36:17 +00:00
arvindfroi commented 2026-10-08 12:10:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by arvindfroi on 2026-10-08T12:10:03Z.


What

InstructionScanner sweeps code linearly with X64InstructionDecoder::Decode, so one wrong length puts the rest of the sweep out of step (#1162, #1167). decoderLengths checks about 40 hand-picked encodings. This adds decoder_length: 946 encodings whose lengths come from LLVM's x86 disassembler, each checked three ways:

  • Decode on a buffer of exactly the instruction returns its length;
  • the same with 15 NOPs after it;
  • one byte short throws CodegenException (no read past the end).

DecoderLengthCases.hpp was generated by a script that is not in the repository: it builds every one-byte, 0F, 0F 38 and 0F 3A opcode with no prefix, 66, F2, F3, REX.W, 66 REX.W and 67, and every VEX map 1-3 opcode for each pp, L and W, each with a register ModRM and a SIB + disp32 memory ModRM. It has llvm-objdump decode them and keeps one case per opcode for each distinct length, plus all four operand-size prefix combinations where they change the length. It skips encodings LLVM rejects, VMREAD/VMWRITE (0F 78/0F 79 without an SSE4a prefix, which the decoder rejects on purpose) and 66-prefixed near branches, whose length differs between Intel (ignored) and AMD (rel16). Its output is deterministic.

The table catches the three length bugs fixed today: run against the decoder of each earlier revision, it fails with Wrong length for 66480584651122 before #1163, and Cannot decode 0fa8: Instruction truncated before ModRM byte before #1381; MOV moffs (a1…, 67 a1…) fails before #1168. On current main all 946 pass.

Tested

On macOS (Apple clang, arm64): decoder_length_tests built from DecoderLengthTests.cpp and X64InstructionDecoder.cpp with only the two include directories the CMake target lists, -Wall -Wextra clean, passes on main; built against the decoder at 2959dc53 and e66d78c6 it fails as described. A second LLVM pass over the table, with int3 padding, agrees with every length. tools/check_conventions.py --base origin/main passes. Running the script again reproduces the header byte for byte (--objdump objdump --clang clang with Xcode's LLVM 21). No x86-64 toolchain on this machine, so the Linux and Windows builds are left to CI.

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 (Claude Code, Claude Opus 5.5)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4786561290 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1552). Originally posted by [arvindfroi](https://github.com/arvindfroi) on 2026-10-08T12:10:03Z. --- ### What `InstructionScanner` sweeps code linearly with `X64InstructionDecoder::Decode`, so one wrong length puts the rest of the sweep out of step (#1162, #1167). `decoderLengths` checks about 40 hand-picked encodings. This adds `decoder_length`: 946 encodings whose lengths come from LLVM's x86 disassembler, each checked three ways: - `Decode` on a buffer of exactly the instruction returns its length; - the same with 15 NOPs after it; - one byte short throws `CodegenException` (no read past the end). `DecoderLengthCases.hpp` was generated by a script that is not in the repository: it builds every one-byte, `0F`, `0F 38` and `0F 3A` opcode with no prefix, `66`, `F2`, `F3`, `REX.W`, `66 REX.W` and `67`, and every VEX map 1-3 opcode for each `pp`, `L` and `W`, each with a register ModRM and a SIB + disp32 memory ModRM. It has `llvm-objdump` decode them and keeps one case per opcode for each distinct length, plus all four operand-size prefix combinations where they change the length. It skips encodings LLVM rejects, VMREAD/VMWRITE (`0F 78`/`0F 79` without an SSE4a prefix, which the decoder rejects on purpose) and `66`-prefixed near branches, whose length differs between Intel (ignored) and AMD (rel16). Its output is deterministic. The table catches the three length bugs fixed today: run against the decoder of each earlier revision, it fails with `Wrong length for 66480584651122` before #1163, and `Cannot decode 0fa8: Instruction truncated before ModRM byte` before #1381; MOV moffs (`a1…`, `67 a1…`) fails before #1168. On current `main` all 946 pass. ### Tested On macOS (Apple clang, arm64): `decoder_length_tests` built from `DecoderLengthTests.cpp` and `X64InstructionDecoder.cpp` with only the two include directories the CMake target lists, `-Wall -Wextra` clean, passes on `main`; built against the decoder at `2959dc53` and `e66d78c6` it fails as described. A second LLVM pass over the table, with `int3` padding, agrees with every length. `tools/check_conventions.py --base origin/main` passes. Running the script again reproduces the header byte for byte (`--objdump objdump --clang clang` with Xcode's LLVM 21). No x86-64 toolchain on this machine, so the Linux and Windows builds are left to CI. ### 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 (Claude Code, Claude Opus 5.5) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
oneandonlydean commented 2026-10-09 22:31:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T22:31:09Z.


The tests are worth having. Could you drop the generator script and keep only the generated table and the test? A new tool needing llvm-objdump and clang isn't something we want in tools/ just to regenerate it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090339204 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1552#issuecomment-6090339204). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T22:31:09Z. --- The tests are worth having. Could you drop the generator script and keep only the generated table and the test? A new tool needing llvm-objdump and clang isn't something we want in tools/ just to regenerate it.
github-actions[bot] commented 2026-10-10 05:26:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T05:26:41Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#2555 core/relinker/codegen/tests/DecoderLengthCases.hpp
core/relinker/codegen/tests/DecoderLengthTests.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094200498 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1552#issuecomment-6094200498). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T05:26:41Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #2555 | | | `core/relinker/codegen/tests/DecoderLengthCases.hpp`<br>`core/relinker/codegen/tests/DecoderLengthTests.cpp` |
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!1552
No description provided.