Skip to content

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

Merged
R0Y00 merged 1 commit into
R0Y00:mainfrom
legacy-code-archaeologist:cpu/lower-trap
Nov 23, 2026
Merged

R0Y00 merged 1 commit into
R0Y00:mainfrom
legacy-code-archaeologist:cpu/lower-trap

Conversation

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

Change

As agreed in #1021, the shared lowering in tools/ffx_version_query.py lowers the exact CC (INT3) and 0F 0B (UD2) encodings to __debugbreak() and __ud2().

  • Like the original instructions, they raise EXCEPTION_BREAKPOINT (0x80000003) and STATUS_ILLEGAL_INSTRUCTION (0xC000001D) to the caller's handlers.
  • Guest stores before the trap stay done, and no later guest instruction runs when the caller unwinds.
  • The exception address and the registers in the exception context are the host's.
  • Not covered: resuming after the trap, mapping the guest RIP, and the jump-table translator.
  • Still refused: INT imm8 (including CD 03 and the CD 29 fast fail), INT1, INTO, UD0 and UD1.

Tests

cpu_aot_trap (trap.asm) calls each native and translated export in a child process. The call goes through a native SEH boundary (trap_harness.cpp: __try / __except, GetExceptionCode()), and the child reports the exception code, the result and two state words.

  • trap_int3 must raise 0x80000003 and trap_ud2 0xC000001D. In both, the store before the trap must be visible and the store after it must not happen.
  • trap_branch(1) must trap like trap_int3. trap_branch(0) skips the INT3 and must return 7 with both stores.
  • Each refused encoding (CD 03, CD 29, F1, CE, 0F FF C0, 0F B9 C0), decoded alone, must still reach the lowering's unsupported query operation.

Controls (mutations of the generated C++, each must break a rule):

  • drop removes the trap.
  • swap exchanges the two intrinsics.
  • nostore removes the earlier store.

/O2: the translation is also built with cl /O2. A walk that follows every branch must still find INT3 in trap_int3 and trap_branch and UD2 in trap_ud2, and every rule must hold.

Two changes outside the new fixture

  1. tests/cpu/signed_conds.py (from cpu: lower signed condition codes, Jcc, CMOVcc, SETcc, NEG and CWD #805) used UD2 as its example of a refused instruction, so it failed once UD2 is lowered. The example is now UD0 (0f ff c0), which stays refused with the same unsupported query operation diagnostic. The control keeps its purpose; please check that this replacement is what you want.
  2. tools/form_coverage.py now counts MASM's int 3, which ML64 encodes as CC, as the reference's int3 form instead of int. ML64 has no int3 mnemonic. I measured that int 3, int 3h and int 03h all assemble to CC, while int 29h stays CD 29. tests/form_coverage_check.py covers these spellings.
    • Side effect: int returns to the gap list. Before this change it was credited by the int 3 in bink_descriptors.asm, which ML64 assembles as CC too.
    • Note: that fixture binds its int 3 through host_ops as a host stop (return 0), so it now credits int3 without executing a trap. If you'd rather credit int3 only through trap.asm, I can follow up with an exclusion for that fixture.

Verification

At 838baf7 (on main 64ecc87) on Windows 11 Pro (build 26100), Intel Core i7-6820HQ, MSVC 19.52, Python 3.12:

  • ctest --preset dev --no-tests=error -R '^cpu_aot_trap$' -V: passes. The output includes:
    • refused, as before: int 3 (imm8), int 29h (fast fail), int1, into, ud0, ud1;
    • the drop control: trap_int3(0): (code, result, before, after) (0, 7, 1, 2);
    • the /O2 walk keeps INT3 and UD2 in every export.
  • ctest --preset release --no-tests=error -R '^cpu_aot_trap$': passes. Only this fixture's targets were built in release.
  • ctest --preset dev --no-tests=error -R '^cpu_' -j 4: 197 of 198 pass, including cpu_aot_signed_conds, with three capability skips.
  • tools/form_coverage.py on a JUnit run of that suite no longer lists int3 or ud2.
  • tests/docs_links_check.py, tests/bidi_controls_check.py and tests/form_coverage_check.py pass.

Not tested: behaviour under an attached debugger. A debugger stops at the first EXCEPTION_BREAKPOINT, as it does for the original.

Refs #1021

  • I did not include game files, dumps, distribution links or secrets.

AI-assisted: written with Claude Code; design and diff reviewed by fresh agents; checked and submitted by the account owner.

🤖 Generated with Claude Code

As agreed in R0Y00#1021, the shared lowering lowers the exact CC (INT3) and
0F 0B (UD2) encodings to __debugbreak() and __ud2(). Like the original
instructions they raise EXCEPTION_BREAKPOINT and
STATUS_ILLEGAL_INSTRUCTION to the caller's handlers. Earlier guest stores
stay done, and no later guest instruction runs on the unwind path. The
exception address and registers are the host's. INT imm8 (including CD
03 and the CD 29 fast-fail), INT1, INTO, UD0 and UD1 stay refused.

cpu_aot_trap calls each native and translated export in a child process
behind a native SEH boundary. It checks the exact code, the visible
earlier store, the skipped later store and the untaken branch's result.
Three mutated controls (drop, swap, nostore) must fail, and the /O2
build keeps both opcodes.

signed_conds.py used UD2 as its example of a refused instruction; it now
uses UD0, which stays refused. form_coverage.py counts MASM's int 3 (also
3h and 03h), which ML64 encodes as CC, as the int3 form instead of int.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@R0Y00
R0Y00 merged commit 42452cd into R0Y00:main Nov 23, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants