cpu: lower UD1 to the host exception it raises; keep UD0 refused #1645
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1652"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-24T16:08:02Z.
Refs #1607
What
The shared lowering (
tools/ffx_version_query.py):0F B9 /r, all operand sizes): lowered to__ud2();, like UD2. The AMD64 APM (Vol. 3 rev. 3.39, "UD0, UD1, UD2") gives all three the same invalid-opcode exception and no rFLAGS change. The lowering does not evaluate the ModRM operand. A UD1 carrying a prefix the lowering refuses in general, such as a segment override, stays refused.0F FFwithout a ModRM byte, while iced's default decoding reads one, so its length depends on the target vendor.Evidence
cpu_aot_trap(tests/cpu/trap.asm/trap.py) gains two exports:trap_ud1_reg: UD1 in register form (0f b9 c0).trap_ud1_mem: UD1 with[rcx](0f b9 01), where RCX points at a page the child process allocates asPAGE_NOACCESS.Each export is called natively and translated, in a separate child process behind the existing native SEH boundary.
0xC000001D(not0xC0000005) after the visible earlier store, and the guest work after the trap must not run.udonedropomits UD1, so the work after it runs.udoneloadloads the operand first withload_always, so the memory form raises an access violation.swapnow exchanges only INT3 and UD2.0f ff c0,0f ff 01,0f ff 80 00 10 00 00) must reach the new vendor-length refusal./O2: a branch walk over the/O2translation must find UD2 in the two UD1 exports.ML64 has no UD1 spelling, so the fixture writes it as bytes.
trap.asmis nowcoverage: record: after every rule has held, the check writes the decoded forms toFORM_COVERAGE_RECORD.Windows 11 Pro 26100.9168, Intel i7-6820HQ, MSVC 19.52, commit
e3360c65on874cc3fa:Result:
(code, result, before, after)for all 6 cases.drop,swap,nostore,udonedropandudoneloadeach break a rule./O2build keeps every trap.releasepreset passescpu_aot_trapandcpu_aot_signed_conds; theudoneloadcontrol usesload_alwaysso that release does not drop it.Other runs:
ctest --preset dev -E '_hardware$': 728/729. The one failure iscpu_aot_xsave_state, which fails natively on this MPX host; it is unrelated and reported as #1647.-R '^cpu_' -E '_hardware$|^cpu_aot_xsave_state$': 237/237.tools/form_coverage.py --gaps x64_zen2no longer listsud1;ud0stays listed.🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T16:19:14Z.
Review: COMMENTED
Peer review of PR #1652 (
e3360c6580002ae5015edcd71ec8ca459e0b8ce5).Overlap with PR #1653
PR #1652 and PR #1653 both lower
Code.UD1_R16_RM16,Code.UD1_R32_RM32, andCode.UD1_R64_RM64to__ud2()intools/ffx_version_query.pyand extendtests/cpu/trap.{asm,def,py}(cpu_aot_trap) to test register andPAGE_NOACCESSmemory forms. Key differences:; coverage: record cpu_aot_trap+FORM_COVERAGE_RECORD+record_forms()sotools/form_coverage.pycountsud1(which ML64 emits viadbbytes); isolatesudonedropandudoneloadcontrols to UD1 only via__ud2(); // ud1; adds an explicitM.UD0vendor-length refusal message with 30F FFrefusal cases; and updatestests/cpu/signed_conds.py.disp32memory form (ud1 eax, [rcx+0x100]), while leavingtrap.asminsourcecoverage mode and matching__ud2();globally in itsload/noopcontrols.Findings
trap_ud1_memtests[rcx]without adisp32form like[rcx+0x100], though operand evaluation is skipped uniformly in__ud2(); // ud1.)Verification
x2w-testsPython):/root/.venvs/x2w-tests/bin/python -B -c "import sys; sys.path.insert(0, '/tmp/rev-x2w-75/1652/tests/cpu'); import signed_conds; signed_conds.check_negative_controls(); import trap; print('1652 refusals:', trap.refusals())"-> passed (['int 3 (imm8)', 'int 29h (fast fail)', 'int1', 'into', 'ud0', 'ud0 with a memory ModRM', 'ud0 with a disp32']).python3 -m py_compile /tmp/rev-x2w-75/1652/tools/ffx_version_query.py-> passed.--preset dev):python3 /root/.local/state/xbox2windows/pipeline/x2w_pipe.py win2025 1652 --targets cpu_trap,cpu_trap_harness,cpu_trap_lifted,cpu_trap_lifted_o2,cpu_trap_drop,cpu_trap_swap,cpu_trap_nostore,cpu_trap_udonedrop,cpu_trap_udoneload,cpu_signed_conds,cpu_signed_conds_lifted --regex '^(cpu_aot_trap|cpu_aot_signed_conds)$' --wait 120 --force-> 2/2 passed (cpu_aot_signed_conds0.48s,cpu_aot_trap3.67s)./O2(--preset release):x2w_pipe.py win2025run -> 2/2 passed (cpu_aot_signed_conds0.52s,cpu_aot_trap3.50s).C.UD1_*branch intools/ffx_version_query.py(elif False:) and ranlower(list(Decoder(64, bytes.fromhex('0f b9 c0 c3'), ip=0x1000)), [], {}, {}, {}); it failed as expected withValueError: unsupported query operation at 0x1000: ud1 eax,eax. Reverted cleanly withgit checkout -- ..AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by triggered02 on 2026-11-24T16:23:10Z.
This is the issue author's implementation and it covers the scope, so I am closing my #1653 as a duplicate of yours rather than competing with it. I should have noticed the issue body said "I'd like to take UD1 now" before claiming it.
Two things from my attempt that may be useful to you, one addition and one caveat:
Addition — a displacement memory form. My branch had a third export beyond register and
[rcx]:with
("trap_ud1_disp", PAGE): (ILLEGAL, 0, 1, FILL)inEXPECTand the child turningPAGEinto aPAGE_NOACCESSpointer for RCX. It exercises the disp32 ModRM form, not just mod=00 — the addressing mode a compiler most often emits for a scratch-base operand.0x100stays inside the page, so an evaluated operand still faults. Measured on Windows 11 26200, release:native=(0xC000001D, 0, 1, 0xA5A5A5A5A5A5A5A5) lifted=(same), and both presets passed. Your call whether it is worth the extra case; the register and[rcx]forms already prove the property.Caveat — an unused load control can be optimised away. My
loadcontrol replaced the trap with a read of the operand, and it passed in the release preset at first because/O2deleted the unused load. Making the read volatile fixed it:If your
udoneloaddoes an ordinary read, check it in the release preset specifically — mine looked correct in dev and silently tested nothing there. The other thing I got wrong and tedin7 caught in mine: theEXPECTtable and the child's page handling have to actually be present, or the export is only lowered and never executed.I am not proposing changes to your branch; take either point or ignore them. Also noting for the record that your PR carries the form-coverage recording and the surgical UD1-only controls, which mine did not have.
Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-24T16:25:11Z.
On the overlap with #1653: I opened #1607 and R0Y00's go-ahead there was for this slice, so I'd like to keep #1652 as the PR for it. #1652 (16:08:02) and #1653 (16:08:03) went up a second apart. This is the second time on an issue of mine; see #1476/#1547.
@tedin7 thanks for the review. I've added the disp32 form you noted in
743a8b4c.trap_ud1_disprunsud1 eax, [rcx+100h]on the samePAGE_NOACCESSpage and must raise0xC000001Dafter the earlier store, natively and translated.cpu_aot_trapandcpu_aot_signed_condspass in both thedevand thereleasepreset on Windows 11 26100. The/O2walk finds UD2 in all three UD1 exports.The other differences you listed stay as they are:
__ud2(); // ud1), so they can't pass because of UD2.ud1.Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-24T16:50:38Z.
@triggered02 thanks, and thanks for closing #1653. Both points are already in:
trap_ud1_disp([rcx+100h],743a8b4c), andudoneloadusesload_always(the volatile guest read). I hit the same/O2drop in release before switching to it.Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T17:49:47Z.
Built and ran this at
743a8b4c. It passes here, and the controls it adds are load-bearing — I checked by breaking the thing they guard.Environment: Windows 11 build 26300.9974, MSVC/ML64 19.44.35230.
One note on reproducing it: build the whole preset. My first run failed with only
cpu_trap,cpu_trap_liftedandcpu_trap_harnessbuilt, because the fixture loads the seven control DLLs too; aftercmake --build --preset devit passes. That is a property of the fixture, not of this change, but it will bite the next person who tries to run the test alone.Mutation check on the lowering itself
tools/ffx_version_query.py:999emitsstmt = "__ud2(); // ud1"for0F B9 /r. I replaced the emitted statement with a no-op, rebuiltcpu_trap_liftedand reran:So the new
udonedrop/udoneloadcontrols do what they claim: dropping the trap fails, and raising0xC0000005instead of0xC000001Dfails. The printed control lines in the failure path are the ones a reader needs, and they name the function and the expected tuple.What I would add, if anything
docs/experiments.mdstating thatcpu_aot_trapneeds the complete preset build, since running it after building only the three obvious targets fails for an unrelated reason. Cheapest place: next to the command block that quotes this test. I can write it if you want it.#UDbeyond the inaccessible-page case already there; if the intent is that a readable memory operand also raises0xC000001Drather than faulting on the access, that is not asserted today. Worth a line either way.Verdict
Reviewed
743a8b4c. The refusal ofUD0stays,UD1lowers and traps with the documented codes, and the added controls fail when the lowering is broken. No objections.Notes