cpu: lower UD1 to the host exception it raises; keep UD0 refused for its vendor-dependent length #1600

Closed
opened 2026-11-24 14:52:40 +00:00 by legacy-code-archaeologist · 3 comments
legacy-code-archaeologist commented 2026-11-24 14:52:40 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-24T14:52:40Z.


Follow-up to #1021/#1033, where INT3 and UD2 were lowered to the host exceptions they raise and UD0 and UD1 stayed refused. I'd like to take UD1 now.

AMD64 APM Vol. 3 (24594 rev. 3.39), "UD0, UD1, UD2":

  • The encodings are UD1 0F B9 /r and UD0 0F FF.
  • All three "generate an invalid opcode exception" and "are intended to stay undefined".
  • rFLAGS: none affected.
  • The page also notes: "On some AMD64 processor implementations, UD1 may report an invalid opcode exception regardless of whether fetching the ModRM byte could trigger a paging or segmentation exception."

Proposed slice:

  • UD1 (0F B9 /r), any ModRM: lowered to __ud2(); like UD2. The host raises STATUS_ILLEGAL_INSTRUCTION and no operand is evaluated, so a memory form with an inaccessible address still gives #UD rather than an access violation. iced decodes UD1 with the same length in its default and AMD modes (for example 0f b9 80 00 10 00 00, 7 bytes, in both).
  • UD0 stays refused. The APM gives 0F FF with no ModRM. iced's default decoder, which the shared lowering uses, reads a ModRM (Intel convention: 0f ff c3 decodes as ud0 eax,ebx, 3 bytes, but as ud0, 2 bytes, with DecoderOptions.AMD). Accepting it would fix a length that does not match the target. The refusal message would say so.

Fixture plan, extending cpu_aot_trap (tests/cpu/trap.asm/trap.py):

  • New exports with UD1 in register form, [rcx] and [rcx+disp32], after guest stores.
  • rcx points at a PAGE_NOACCESS page, so the expected result is STATUS_ILLEGAL_INSTRUCTION, not an access violation, natively and translated. The guest stores before it must be done.
  • Controls that must fail: UD1 lowered to a load of its memory operand, which faults with an access violation; and UD1 lowered as a no-op, which returns.
  • Refusal: ud0 (all three spellings) with the length message.

The /O2 build and both presets, as for the trap fixture.

Does this scope work, or do you want UD0 handled differently, for example decoded with the AMD option?

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6584570108 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1607). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-24T14:52:40Z. --- Follow-up to #1021/#1033, where INT3 and UD2 were lowered to the host exceptions they raise and UD0 and UD1 stayed refused. I'd like to take UD1 now. **AMD64 APM Vol. 3 (24594 rev. 3.39), "UD0, UD1, UD2":** - The encodings are `UD1 0F B9 /r` and `UD0 0F FF`. - All three "generate an invalid opcode exception" and "are intended to stay undefined". - rFLAGS: none affected. - The page also notes: "On some AMD64 processor implementations, UD1 may report an invalid opcode exception regardless of whether fetching the ModRM byte could trigger a paging or segmentation exception." **Proposed slice:** - **UD1 (`0F B9 /r`), any ModRM:** lowered to `__ud2();` like UD2. The host raises `STATUS_ILLEGAL_INSTRUCTION` and no operand is evaluated, so a memory form with an inaccessible address still gives #UD rather than an access violation. iced decodes UD1 with the same length in its default and AMD modes (for example `0f b9 80 00 10 00 00`, 7 bytes, in both). - **UD0 stays refused.** The APM gives `0F FF` with no ModRM. iced's default decoder, which the shared lowering uses, reads a ModRM (Intel convention: `0f ff c3` decodes as `ud0 eax,ebx`, 3 bytes, but as `ud0`, 2 bytes, with `DecoderOptions.AMD`). Accepting it would fix a length that does not match the target. The refusal message would say so. **Fixture plan**, extending `cpu_aot_trap` (`tests/cpu/trap.asm`/`trap.py`): - New exports with UD1 in register form, `[rcx]` and `[rcx+disp32]`, after guest stores. - `rcx` points at a `PAGE_NOACCESS` page, so the expected result is `STATUS_ILLEGAL_INSTRUCTION`, not an access violation, natively and translated. The guest stores before it must be done. - **Controls that must fail:** UD1 lowered to a load of its memory operand, which faults with an access violation; and UD1 lowered as a no-op, which returns. - **Refusal:** `ud0` (all three spellings) with the length message. The `/O2` build and both presets, as for the trap fixture. Does this scope work, or do you want UD0 handled differently, for example decoded with the AMD option?
R0Y00 commented 2026-11-24 16:03:32 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T15:14:21Z.


Go ahead with UD1 in the existing trap fixture, limited to fully readable, decoded instruction bytes. Compare native and generated exception status and the visible store before the trap in separate subprocesses. Include a register form, a memory form with an inaccessible operand, and the omitted-trap control. Run both Windows presets. Keep UD0 refused while its decoded length varies by target, and leave instruction-fetch boundary behavior outside this slice. https://docs.amd.com/v/u/en-US/24594_3.37

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7135686987 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1607#issuecomment-7135686987). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T15:14:21Z. --- Go ahead with UD1 in the existing trap fixture, limited to fully readable, decoded instruction bytes. Compare native and generated exception status and the visible store before the trap in separate subprocesses. Include a register form, a memory form with an inaccessible operand, and the omitted-trap control. Run both Windows presets. Keep UD0 refused while its decoded length varies by target, and leave instruction-fetch boundary behavior outside this slice. https://docs.amd.com/v/u/en-US/24594_3.37
triggered02 commented 2026-11-24 16:03:36 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by triggered02 on 2026-11-24T16:03:04Z.


Taking this, following the scope you set: UD1 in the existing trap fixture, fully readable decoded bytes, native and generated compared in separate subprocesses, and UD0 left refused.

Lowering. UD1 (0F B9 /r) lowers to __ud2();, the same host trap UD2 uses, so the host reports STATUS_ILLEGAL_INSTRUCTION and no operand is evaluated — a memory form whose operand is inaccessible still gives #UD rather than an access violation.

Fixture (tests/cpu/trap.asm / trap.py, extending cpu_aot_trap): three new exports, each storing to the caller's state word before the trap —

  • register form (0F B9 C0),
  • memory form with rcx pointing at a PAGE_NOACCESS page,
  • memory form with a disp32 off the same page,

each expecting the same tuple as the existing traps: exception 0xC000001D, result 0, store visible, guest work after the trap not executed. Controls that must fail: UD1 lowered to a load of its operand (faults with an access violation instead of #UD — this is the one that separates "traps" from "touches memory"), and UD1 lowered as a no-op (returns instead of trapping).

Refusal kept: ud0 in all three spellings, with the message saying why — iced's default decoder reads a ModRM for 0F FF while the AMD manual has no ModRM byte, so accepting it would fix a length that does not match the target. Instruction-fetch boundary behaviour stays out of this slice, as you said.

Both Windows presets will be run. I will report the measured statuses rather than the intention.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7137039164 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1607#issuecomment-7137039164). Originally posted by [triggered02](https://github.com/triggered02) on 2026-11-24T16:03:04Z. --- Taking this, following the scope you set: UD1 in the existing trap fixture, fully readable decoded bytes, native and generated compared in separate subprocesses, and UD0 left refused. **Lowering.** `UD1 (0F B9 /r)` lowers to `__ud2();`, the same host trap UD2 uses, so the host reports `STATUS_ILLEGAL_INSTRUCTION` and no operand is evaluated — a memory form whose operand is inaccessible still gives #UD rather than an access violation. **Fixture** (`tests/cpu/trap.asm` / `trap.py`, extending `cpu_aot_trap`): three new exports, each storing to the caller's state word before the trap — - register form (`0F B9 C0`), - memory form with `rcx` pointing at a `PAGE_NOACCESS` page, - memory form with a `disp32` off the same page, each expecting the same tuple as the existing traps: exception `0xC000001D`, result 0, store visible, guest work after the trap not executed. Controls that must fail: UD1 lowered to a load of its operand (faults with an access violation instead of #UD — this is the one that separates "traps" from "touches memory"), and UD1 lowered as a no-op (returns instead of trapping). **Refusal kept:** `ud0` in all three spellings, with the message saying why — iced's default decoder reads a ModRM for `0F FF` while the AMD manual has no ModRM byte, so accepting it would fix a length that does not match the target. Instruction-fetch boundary behaviour stays out of this slice, as you said. Both Windows presets will be run. I will report the measured statuses rather than the intention.
R0Y00 commented 2026-11-24 18:02:56 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T17:14:15Z.


The bounded UD1 change is on main via #1652 at 685dcc3a. The final Windows Release native/lifted trap test passed for the register, memory and disp32 memory cases, with the no-op and unintended-load controls. UD0 remains refused and instruction-fetch boundary behavior still needs its own evidence.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7138891972 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1607#issuecomment-7138891972). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T17:14:15Z. --- The bounded UD1 change is on main via #1652 at 685dcc3a. The final Windows Release native/lifted trap test passed for the register, memory and disp32 memory cases, with the no-op and unintended-load controls. UD0 remains refused and instruction-fetch boundary behavior still needs its own evidence.
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
R0Y00/Xbox2Windows#1600
No description provided.