fix(relinker): decode UD1 as a trap #1457

Merged
drk1rd merged 1 commit from upstream-pr/1457 into main 2026-10-08 14:17:07 +00:00
drk1rd commented 2026-10-08 07:39:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by drk1rd on 2026-10-08T07:39:51Z.


What

UD1 (0F B9 /r) always raises #UD, like UD2 (Intel SDM Vol. 2B "UD—Undefined Instruction"; AMD APM Vol. 3 "UD1"), but DecodeInstruction reported it as Sequential. The control-flow walk and the strict reachability analysis then followed the bytes after it, so an import reachable only through that fallthrough was kept by the strict unused NID filter. UD1 is now a Trap. It keeps its ModRM, so a RIP-relative operand is still reported.

Found by comparing X64InstructionDecoder against iced-x86 (AMD decoder options) over ~326k encodings: lengths and RIP-relative offsets matched except for opcodes already fixed in #1163, #1168 and #1381. Clang emits UD1 for -fsanitize-trap. UD0 is left alone: its ModRM differs between Intel and AMD.

Tested

Windows 11 (10.0.26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, -DBUILD_TESTING=ON. Full ctest: 419/419 pass. The new checks in strict_nid_filter and amd64_only_converter fail without the decoder change. check_conventions.py and pr_overlap.py --branch are clean.

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:4783528935 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1457). Originally posted by [drk1rd](https://github.com/drk1rd) on 2026-10-08T07:39:51Z. --- ### What `UD1` (`0F B9 /r`) always raises #UD, like `UD2` (Intel SDM Vol. 2B "UD—Undefined Instruction"; AMD APM Vol. 3 "UD1"), but `DecodeInstruction` reported it as `Sequential`. The control-flow walk and the strict reachability analysis then followed the bytes after it, so an import reachable only through that fallthrough was kept by the strict unused NID filter. `UD1` is now a `Trap`. It keeps its ModRM, so a RIP-relative operand is still reported. Found by comparing `X64InstructionDecoder` against iced-x86 (AMD decoder options) over ~326k encodings: lengths and RIP-relative offsets matched except for opcodes already fixed in #1163, #1168 and #1381. Clang emits `UD1` for `-fsanitize-trap`. `UD0` is left alone: its ModRM differs between Intel and AMD. ### Tested Windows 11 (10.0.26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, `-DBUILD_TESTING=ON`. Full `ctest`: 419/419 pass. The new checks in `strict_nid_filter` and `amd64_only_converter` fail without the decoder change. `check_conventions.py` and `pr_overlap.py --branch` are clean. ### 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
Zheorgyan commented 2026-10-08 08:43:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Zheorgyan on 2026-10-08T08:43:16Z.


heads up, this and #1381 merge cleanly but don't build together. #1381 changes the readModRm lambda in DecodeInstruction to take (modrmPos, registerOnly), and the new UD1 branch here still calls readModRm(pos), so whichever lands second breaks main. if #1381 goes in first the fix is just readModRm(pos, false)

I checked by merging both heads into current main and building X64InstructionDecoder.cpp with GCC 14, each one builds fine on its own

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6056141095 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1457#issuecomment-6056141095). Originally posted by [Zheorgyan](https://github.com/Zheorgyan) on 2026-10-08T08:43:16Z. --- heads up, this and #1381 merge cleanly but don't build together. #1381 changes the `readModRm` lambda in `DecodeInstruction` to take `(modrmPos, registerOnly)`, and the new UD1 branch here still calls `readModRm(pos)`, so whichever lands second breaks main. if #1381 goes in first the fix is just `readModRm(pos, false)` I checked by merging both heads into current main and building `X64InstructionDecoder.cpp` with GCC 14, each one builds fine on its own
drk1rd commented 2026-10-08 09:58:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by drk1rd on 2026-10-08T09:58:06Z.


Thanks for catching this. #1381 is merged now, so I rebased onto main and the UD1 branch calls readModRm(pos, false). Rebuilt and ran the full ctest on Windows (GCC 15.2.0): 429/429 pass.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6057366955 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1457#issuecomment-6057366955). Originally posted by [drk1rd](https://github.com/drk1rd) on 2026-10-08T09:58:06Z. --- Thanks for catching this. #1381 is merged now, so I rebased onto main and the UD1 branch calls `readModRm(pos, false)`. Rebuilt and ran the full `ctest` on Windows (GCC 15.2.0): 429/429 pass.
adriadam10 commented 2026-10-08 14:17:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T14:17:03Z.


Checked on current main (PR merged on top): build + full ctest 459/459. With the decoder change reverted, amd64_only_converter and strict_nid_filter both fail, so the new cases pin the fix. UD1 raising #UD matches the SDM/APM, the ModRM/RIP-relative operand is kept, and readModRm(pos, false) is consistent with #1381. Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6061855024 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1457#issuecomment-6061855024). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T14:17:03Z. --- Checked on current `main` (PR merged on top): build + full ctest 459/459. With the decoder change reverted, `amd64_only_converter` and `strict_nid_filter` both fail, so the new cases pin the fix. UD1 raising #UD matches the SDM/APM, the ModRM/RIP-relative operand is kept, and `readModRm(pos, false)` is consistent with #1381. Merging once CI is green.
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!1457
No description provided.