cpu: lower INT3 and UD2 to the host exceptions they raise #1014

Closed
opened 2026-11-23 21:34:55 +00:00 by legacy-code-archaeologist · 2 comments
legacy-code-archaeologist commented 2026-11-23 21:34:55 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-23T21:34:55Z.


Claim and design question: lower INT3 and UD2 to the host exceptions they raise

int3 and ud2 (Integer group of docs/coverage/x64_zen2.tsv) have no passing fixture. The shared lowering rejects both. The only translator that handles INT3 is tools/bink_descriptors_aot.py, which binds it to return 0; for its own stop sites. Compiled startup code uses both:

  • int3 is the failure path of checks and of __debugbreak(). MoonYueya's and Holastor's startup notes in #870 show the main executable stopping at an int3 after a failed check.
  • ud2 marks unreachable code after calls that do not return.

I found no open issue or PR about lowering them and would like to take a first, small increment.

Proposed behavior (shared lowering)

  • INT3 (CC) calls __debugbreak(). Like the original, it raises EXCEPTION_BREAKPOINT (0x80000003) to the caller's handlers or debugger, and no later guest instruction runs.
  • UD2 calls __ud2(), which raises STATUS_ILLEGAL_INSTRUCTION (0xC000001D) like the original. It also ends the block.
  • Guest state is not passed through: the exception address is in the translated code, not the guest RIP, and the context holds host registers. The PR would document this as the contract.
  • INT 3 in its two-byte form (CD 03), INT1/ICEBP, UD0/UD1 and INT n stay rejected in this increment. That includes INT 29h/__fastfail, which needs its own decision.

Proposed fixture (source-built MASM, child processes)

Exports:

  • an int3 after a guest store;
  • a ud2 after a guest store;
  • a conditional path that reaches int3 only for one input.

A child process calls each native and translated export. It must report the same exception code, and a marker written after the instruction must stay unwritten. The branch case must return normally for the other input. The translation is also built with cl /O2 and checked for the same results. An optional check confirms that __debugbreak and __ud2 remain in the /O2 code.

A control that drops the instruction must let the marker through, and a control that swaps the two instructions must report the wrong code.

Questions

  1. Is raising the host exception the intended first contract? The other option is a runtime hook that receives the guest RIP, so a later SEH or pe_map path can report guest addresses as #65 does for mapped images. I'd start with the host exception and keep the hook for later.
  2. Shared lowering only, or also cpu_switch_aot in the same PR? I'd keep it to the shared lowering first.

Test host: Intel Skylake (i7-6820HQ) on Windows 11 26100.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6569297537 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1021). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-23T21:34:55Z. --- ### Claim and design question: lower `INT3` and `UD2` to the host exceptions they raise `int3` and `ud2` (Integer group of `docs/coverage/x64_zen2.tsv`) have no passing fixture. The shared lowering rejects both. The only translator that handles `INT3` is `tools/bink_descriptors_aot.py`, which binds it to `return 0;` for its own stop sites. Compiled startup code uses both: - `int3` is the failure path of checks and of `__debugbreak()`. MoonYueya's and Holastor's startup notes in #870 show the main executable stopping at an `int3` after a failed check. - `ud2` marks unreachable code after calls that do not return. I found no open issue or PR about lowering them and would like to take a first, small increment. **Proposed behavior (shared lowering)** - `INT3` (`CC`) calls `__debugbreak()`. Like the original, it raises `EXCEPTION_BREAKPOINT` (`0x80000003`) to the caller's handlers or debugger, and no later guest instruction runs. - `UD2` calls `__ud2()`, which raises `STATUS_ILLEGAL_INSTRUCTION` (`0xC000001D`) like the original. It also ends the block. - Guest state is not passed through: the exception address is in the translated code, not the guest RIP, and the context holds host registers. The PR would document this as the contract. - `INT 3` in its two-byte form (`CD 03`), `INT1`/`ICEBP`, `UD0`/`UD1` and `INT n` stay rejected in this increment. That includes `INT 29h`/`__fastfail`, which needs its own decision. **Proposed fixture (source-built MASM, child processes)** Exports: - an `int3` after a guest store; - a `ud2` after a guest store; - a conditional path that reaches `int3` only for one input. A child process calls each native and translated export. It must report the same exception code, and a marker written after the instruction must stay unwritten. The branch case must return normally for the other input. The translation is also built with `cl /O2` and checked for the same results. An optional check confirms that `__debugbreak` and `__ud2` remain in the `/O2` code. A control that drops the instruction must let the marker through, and a control that swaps the two instructions must report the wrong code. **Questions** 1. Is raising the host exception the intended first contract? The other option is a runtime hook that receives the guest RIP, so a later SEH or `pe_map` path can report guest addresses as #65 does for mapped images. I'd start with the host exception and keep the hook for later. 2. Shared lowering only, or also `cpu_switch_aot` in the same PR? I'd keep it to the shared lowering first. Test host: Intel Skylake (i7-6820HQ) on Windows 11 26100. AI-assisted (Claude Code).
R0Y00 commented 2026-11-24 06:49:44 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T21:43:17Z.


Please take the shared Windows x64 lowerer first, for the exact CC and 0F 0B encodings. The observable contract is the host exception class and the effects before a trap when the caller unwinds. Exception addresses and registers belong to the host. Resuming through a debugger or a handler, mapping the guest RIP and expanding the switch lowerer remain separate work.

Use a child with an SEH boundary and a volatile or otherwise observable marker. Check the exact exception code, that the earlier store is visible and that later guest work does not run on the unwind path. The untaken branch must still return and produce its expected result. Run the same source-built controls in both Windows presets and verify that the required opcodes survive the optimized build. Keep the other interrupt, undefined-instruction and fast-fail encodings refused. Windows exception codes

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7114831321 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1021#issuecomment-7114831321). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T21:43:17Z. --- Please take the shared Windows x64 lowerer first, for the exact CC and 0F 0B encodings. The observable contract is the host exception class and the effects before a trap when the caller unwinds. Exception addresses and registers belong to the host. Resuming through a debugger or a handler, mapping the guest RIP and expanding the switch lowerer remain separate work. Use a child with an SEH boundary and a volatile or otherwise observable marker. Check the exact exception code, that the earlier store is visible and that later guest work does not run on the unwind path. The untaken branch must still return and produce its expected result. Run the same source-built controls in both Windows presets and verify that the required opcodes survive the optimized build. Keep the other interrupt, undefined-instruction and fast-fail encodings refused. [Windows exception codes](https://learn.microsoft.com/en-us/windows/win32/debug/getexceptioncode)
R0Y00 commented 2026-11-24 06:49:48 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T22:40:56Z.


Implemented in #1033. Windows Release reproduced both native exception codes, preserved the earlier writes, left later writes untouched, and returned normally on the untaken branch. The three mutation controls failed as intended. This closes the shared-lowering slice; guest context and resume semantics remain separate.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7116054243 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1021#issuecomment-7116054243). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T22:40:56Z. --- Implemented in #1033. Windows Release reproduced both native exception codes, preserved the earlier writes, left later writes untouched, and returned normally on the untaken branch. The three mutation controls failed as intended. This closes the shared-lowering slice; guest context and resume semantics remain separate.
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#1014
No description provided.