fix(relinker): do not add the F6/F7 TEST immediate to EVEX instructions #1303

Merged
overlordxrz-source merged 1 commit from upstream-pr/1303 into main 2026-10-08 09:35:40 +00:00
overlordxrz-source commented 2026-10-07 22:51:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by overlordxrz-source on 2026-10-07T22:51:12Z.


What

X64InstructionDecoder::Decode adds the TEST r/m, imm immediate when the opcode is F6/F7 and ModRM.reg is 0 or 1. The check skips VEX and two-byte opcodes but not EVEX, so after an EVEX prefix the map-1 opcode F6 (VPSADBW, EVEX.66.0F.WIG F6 /r, no immediate) with ModRM.reg 0 or 1 is sized as if it carried an imm8:

Bytes Instruction Expected Decode
62 F1 75 48 F6 C2 vpsadbw zmm0, zmm1, zmm2 6 7
62 F1 7D 48 F6 05 disp32 vpsadbw zmm0, zmm0, [rip+0x10] 10 11
62 F1 6D 48 F6 4C 24 01 vpsadbw zmm1, zmm2, [rsp+0x40] 8 9

InstructionScanner sweeps linearly, so the byte after such an instruction is skipped and the rest of the sweep is out of step. The check now also excludes EVEX. Expected lengths are from Intel SDM Vol. 2B (VPSADBW) and agree with LLVM MC and Capstone 5.0.7.

PS5 code does not use AVX-512, so this is decoder correctness, not a fix for a title.

Tested

aarch64 Debian 13 with clang (Zig 0.16 toolchain, libc++). 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 (21 of 27 functions, decoderLengths included). The new decoderLengths entries fail without the fix. The 6 *Execution functions were not run.
  • Differential check of Decode and DecodeInstruction 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) and on 6,782 unique instructions from x86-64 -march=znver2 -O2 objects. The VPSADBW length mismatches are gone, and no other length, RIP-relative flag or displacement offset changed.
  • X64InstructionDecoder.cpp compiles for x86_64-linux with -Wall -Wextra -Werror.
  • python3 tools/check_conventions.py --base origin/main passes.
  • Merges cleanly with the open decoder PRs #1163, #1168 and #1211, and the tests pass when merged with #1163.

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); no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • No new third-party code
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4779436967 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1303). Originally posted by [overlordxrz-source](https://github.com/overlordxrz-source) on 2026-10-07T22:51:12Z. --- ### What `X64InstructionDecoder::Decode` adds the `TEST r/m, imm` immediate when the opcode is `F6`/`F7` and ModRM.reg is 0 or 1. The check skips VEX and two-byte opcodes but not EVEX, so after an EVEX prefix the map-1 opcode `F6` (`VPSADBW`, EVEX.66.0F.WIG F6 /r, no immediate) with ModRM.reg 0 or 1 is sized as if it carried an imm8: | Bytes | Instruction | Expected | `Decode` | |---|---|---|---| | `62 F1 75 48 F6 C2` | vpsadbw zmm0, zmm1, zmm2 | 6 | 7 | | `62 F1 7D 48 F6 05 disp32` | vpsadbw zmm0, zmm0, [rip+0x10] | 10 | 11 | | `62 F1 6D 48 F6 4C 24 01` | vpsadbw zmm1, zmm2, [rsp+0x40] | 8 | 9 | `InstructionScanner` sweeps linearly, so the byte after such an instruction is skipped and the rest of the sweep is out of step. The check now also excludes EVEX. Expected lengths are from Intel SDM Vol. 2B (VPSADBW) and agree with LLVM MC and Capstone 5.0.7. PS5 code does not use AVX-512, so this is decoder correctness, not a fix for a title. ### Tested aarch64 Debian 13 with clang (Zig 0.16 toolchain, libc++). 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 (21 of 27 functions, `decoderLengths` included). The new `decoderLengths` entries fail without the fix. The 6 `*Execution` functions were not run. - Differential check of `Decode` and `DecodeInstruction` 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) and on 6,782 unique instructions from x86-64 `-march=znver2 -O2` objects. The VPSADBW length mismatches are gone, and no other length, RIP-relative flag or displacement offset changed. - `X64InstructionDecoder.cpp` compiles for x86_64-linux with `-Wall -Wextra -Werror`. - `python3 tools/check_conventions.py --base origin/main` passes. - Merges cleanly with the open decoder PRs #1163, #1168 and #1211, and the tests pass when merged with #1163. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] No new third-party code - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
Dextroid17 commented 2026-10-08 02:20:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T02:20:22Z.

Review: COMMENTED


Built and ran it, and proved the new cases actually bite rather than passing either way.

Linux (CachyOS, GCC, Release):

cmake --build build --target amd64_only_converter_tests --parallel 16
ctest -R ^amd64_only_converter$                      →  1/1 Passed

Mutation check — the decoder from pr-1303^ with your test kept, so only the guard differs:

pr-1303   (your decoder):  1/1 Passed
pr-1303^  (parent decoder): ***Failed

So the three new 0x62 … F6/F7 cases genuinely exercise the bug, and the !evexPresent guard is what makes them pass. That is the check worth having: a decoder test that passes on both sides tells you nothing.

The guard is now complete, and it matches how the prefix is treated elsewhere. The one-byte TEST rule already excluded VEX and the 0F two-byte map; EVEX was the third encoding that can carry an F6/F7 byte where it is not TEST. Line 98 in the same file already treats the two prefix kinds together (if (vexPresent || evexPresent)), so this makes the immediate rule consistent with that rather than adding a special case.

Why it matters beyond one instruction: the length this function computes is what the scan uses to step to the next instruction. Consuming a byte as a TEST immediate that is not one shortens or lengthens this instruction by one, and every boundary after it in that pass is then wrong — so a single misread is a misplacement, not a local error. The three cases cover both a plain ModRM and a disp8 form, including the RIP-relative one, which is the shape a relink actually has to relocate.

I checked for the same gap elsewhere in the file and did not find one. The other scanner (the RIP-relative/control-flow pass) handles VEX2/VEX3/EVEX with an early return before it reaches its own !twoByteOpcode block, so it never applies a one-byte immediate rule to a prefixed instruction. The equivalent of this fix was already there by construction; no follow-up needed.

This is a comment, not an approval — I have no merge rights here.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5450641033 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1303#pullrequestreview-5450641033). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T02:20:22Z. **Review:** COMMENTED --- Built and ran it, and proved the new cases actually bite rather than passing either way. **Linux (CachyOS, GCC, Release):** ``` cmake --build build --target amd64_only_converter_tests --parallel 16 ctest -R ^amd64_only_converter$ → 1/1 Passed ``` **Mutation check** — the decoder from `pr-1303^` with your test kept, so only the guard differs: ``` pr-1303 (your decoder): 1/1 Passed pr-1303^ (parent decoder): ***Failed ``` So the three new `0x62 … F6/F7` cases genuinely exercise the bug, and the `!evexPresent` guard is what makes them pass. That is the check worth having: a decoder test that passes on both sides tells you nothing. **The guard is now complete, and it matches how the prefix is treated elsewhere.** The one-byte TEST rule already excluded VEX and the `0F` two-byte map; EVEX was the third encoding that can carry an `F6/F7` byte where it is not `TEST`. Line 98 in the same file already treats the two prefix kinds together (`if (vexPresent || evexPresent)`), so this makes the immediate rule consistent with that rather than adding a special case. **Why it matters beyond one instruction:** the length this function computes is what the scan uses to step to the next instruction. Consuming a byte as a `TEST` immediate that is not one shortens or lengthens this instruction by one, and every boundary after it in that pass is then wrong — so a single misread is a misplacement, not a local error. The three cases cover both a plain ModRM and a disp8 form, including the RIP-relative one, which is the shape a relink actually has to relocate. **I checked for the same gap elsewhere in the file and did not find one.** The other scanner (the RIP-relative/control-flow pass) handles `VEX2`/`VEX3`/`EVEX` with an early `return` before it reaches its own `!twoByteOpcode` block, so it never applies a one-byte immediate rule to a prefixed instruction. The equivalent of this fix was already there by construction; no follow-up needed. This is a comment, not an approval — I have no merge rights here. Written with AI assistance.
overlordxrz-source commented 2026-10-08 02:48:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by overlordxrz-source on 2026-10-08T02:48:59Z.


@Dextroid17 Thanks for taking the time to build it and run the mutation check, and for looking at the other scanner too. Much appreciated.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6051153534 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1303#issuecomment-6051153534). Originally posted by [overlordxrz-source](https://github.com/overlordxrz-source) on 2026-10-08T02:48:59Z. --- @Dextroid17 Thanks for taking the time to build it and run the mutation check, and for looking at the other scanner too. Much appreciated.
Dextroid17 commented 2026-10-08 03:05:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T03:05:43Z.


You're welcome — the guard is a real fix, and the 0x62 … F6/F7 cases earning a ***Failed on the parent decoder is what makes it worth merging. Good luck with it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6051328786 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1303#issuecomment-6051328786). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T03:05:43Z. --- You're welcome — the guard is a real fix, and the `0x62 … F6/F7` cases earning a `***Failed` on the parent decoder is what makes it worth merging. Good luck with it.
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!1303
No description provided.