tooling: add an x87 area to the CPU fuzz test #1660
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1667"
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 RenzoWit35 on 2026-11-24T16:44:09Z.
Change
Adds
cpu_aot_fuzz_x87(--area x87) totools/fuzz_cpu.py, with its own seed (0x8087, 40 exports) and a CMake block likecpu_aot_fuzz_loops(CPU_FUZZ_X87_SEED,CPU_FUZZ_X87_FUNCTIONS).ffx_version_query.lower(..., x87=True),tools/x87_lower.py), not incpu_switch_aot. The area adds a small adapter,x87_translate, that lowers each export's function range the same way thecpu_aot_x87_*fixtures do, withINTEGER_HEADERandX87_HEADER, intolifted_<name>(x, y, out).translator_module("x87")returns it, sotranslate,check, the controls and the mutant run unchanged.FNSTCWthen stores the control word at 48. The lowering fixes that word at 0x027F, so a native run under another word would mismatch there. Then up to 17 operations (two loads, one of each kind in turn, and 6 to 14 more), each chosen to suit the stack depth:FLDm32/m64/m80/ST(i),FILDm16/m32/m64, and the seven constant loads;FADD/FSUB/FSUBR/FMUL/FDIV/FDIVRwith memory,ST, ST(i),ST(i), STand the popping forms, andFIADDtoFIDIVRwith m16/m32;FCHS,FABS,FSQRT,FRNDINT,FXCH;FST/FSTPto m32/m64/m80/ST(i),FIST,FISTP,FISTTP;FCOMI/FUCOMI(P)thenSETcc;FCOM/FCOMP/FCOMPP/FUCOM*/FTST/FICOM(P)/FXAM, thenFNSTSW AXand eitherSAHF+SETccorAND AH, 45h+ a byte store;FCMOVccafterFCOMI,SAHForCMP RCX, RDX.The remaining stack is drained with popping stores. Results go to bytes 64 to 255, and RAX is the first result qword.
RET. PF (SETP/SETNP/FCMOVU/FCMOVNU) is read only afterFCOMIorSAHF. C1, the exception flags and AL afterFNSTSWare never read, and RAX is overwritten before the return. These are exactly the profile's documented rules.x87_soft.hclaims a bit-exact model there, and nothing is masked.x87 stack overflow), a store from an empty stack (x87 stack underflow), a return with a value (x87 stack not empty at return) andFINCSTP(unsupported x87 operation; the profile documentsFINCSTPandFDECSTPas outside its model, and open PR #1663 proposes to lowerFSIN, so that one is not used as the control).subr_as_sub. It lowersFSUBRandFSUBRPlikeFSUBandFSUBP, by patchingx87_lower.ARITHMETICin the translate process. The code stays straight-line, so the mutant is bounded. The plant fails if no export usesFSUBRorFSUBRP.docs/experiments.mdparagraph after the loops entry, with the manual long-run command.Out of scope:
FXTRACT,FSCALE, the checked entry'sFLDCW,FNINIT,FPREM/FPREM1,FBLD/FBSTP,FNSTSWm16,FFREE/FFREEP, branches or calls with x87 state, and a caller value on the x87 stack. Thecpu_aot_x87_*fixtures cover those throughx87_harness.asm.Verification
Environment: Windows 11 Enterprise build 26300, MSVC 19.51 (Build Tools, toolset 14.51.36231), ML64 14.51, Python 3.14.5, at the commit of this PR, rebased on
c864f138.dev: 9/9 passed, 0 failed, 0 skipped, 11.1 s total.cpu_aot_fuzz_x87took 1.7 s.release: 9/9 passed, 0 failed, 0 skipped, 10.5 s total.cpu_aot_fuzz_x87took 1.7 s.cpu_aot_fuzz_x87 -V:area=x87 seed=0x8087 functions=40 inputs=313 comparisons=25040 default and /O2 matched. All four controls were refused with the expected messages, andmutant subr_as_sub: 675/12520 mismatches in 7/40 functions(10 of the 40 exports useFSUBR/FSUBRP).--mutantexits 1 withthe planted translator fault was not detected.fuzz_x87_6, input 1) and prints the MASM and the reproduction command.0ccde933; no translator file changed since) through the documented CMake variables (cmake --preset dev -DCPU_FUZZ_X87_SEED=<seed> -DCPU_FUZZ_X87_FUNCTIONS=160, build the three x87 targets, thenctest --preset dev --no-tests=error -R '^cpu_aot_fuzz_x87$' -V):0x1,0x2,0xBEEFand0xCCD1at 160 exports: 100,160 comparisons each, about 4 to 5 s, no refusal and no mismatch;0x8087with 400 exports and at0x1,0x2,0x3and0xBEEFwith 300 exports also had no mismatch (938,400 comparisons).0ccde933):emit:python -B tools/fuzz_cpu.py emit <seed> <count> a.asm a.def --area <area>for every area at its CMake seed and count (leaf0xA902/20, frames0xF4A3/16, calls0xCA11/16, chains0xCA11/16, switches0x5717/48, scalar_fp0xF10A/60, rsp0xB0D1/36, loops0x100F/32). The SHA-256 of the.asmand.defis identical for all eight areas.translate: origin/main'stools/(git archive origin/main tools) translated the native DLLs built on this branch. The resulting*_lifted.cppand*_mutant.cppare byte-identical to the branch's own for all eight areas (15 files), in both thedevandreleasebuilds.tests/docs_links_check.pyandtests/bidi_controls_check.pypass.Not verified: CI on GitHub, other processors (only this machine's Intel CPU; the x87 NaN-selection rules are the ones the
cpu_aot_x87_*fixtures already rely on), and non-Windows hosts.Evidence and follow-up
Refs #446. This adds the x87 area that the issue lists as optional. No real mismatch between native and translated x87 code was found at the default seed or in about 1.4 million comparisons over 2,280 generated exports, so the generator has no exclusion. Possible follow-ups:
FXTRACTandFSCALE, the checked entry (FLDCW,FNINIT,FPREM) with a status-aware comparison, and x87 state across branches.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T16:55:08Z.
Review: COMMENTED
Independent technical peer review of PR #1667 at head commit
79a8bca3d343d19f76163147572fdb9a7b3b79d8.Summary
CMakeLists.txt,docs/experiments.md,tools/fuzz_cpu.py).X87Builder,generate_x87,x87_inputs,x87_translate, andplant_subr_as_subare sound; stack depth and flag-liveness (fcomivsinteger) are tracked consistently.Findings
docs/experiments.md:2723): The doc lists the fourth negative control as`FSIN` (`unsupported x87 operation`), whereasX87_CONTROLS["fincstp"]intools/fuzz_cpu.pyactually emitsfld1/fincstp/fdecstp/fstp QWORD PTR [r8+64]/ret.CMakeLists.txt):cpu_fuzz_x87_lifted_o2is anadd_custom_target(... ALL ...)whose output DLL is passed by file path toadd_test(NAME cpu_aot_fuzz_x87 ...); selective builds that only build targets referenced byadd_testmust also includecpu_fuzz_x87_lifted_o2explicitly (otherwisecpu_aot_fuzz_x87fails withFileNotFoundErrorforcpu_fuzz_x87_lifted_o2.dll).Verification Executed Locally
git diff --check origin/main...HEAD— passed./root/.local/share/x2w-rev-venv/bin/python -B tests/docs_links_check.py— passed (16 Markdown files, 352 links)./root/.local/share/x2w-rev-venv/bin/python -B tests/bidi_controls_check.py— passed (1277 files).python3 -m py_compile tools/fuzz_cpu.py— passed.python3 tools/fuzz_cpu.py emit 0x8087 40 /tmp/rev-x2w-80/test_x87.asm /tmp/rev-x2w-80/test_x87.def --area x87— passed (44 functions, 45 exports in.defincluding controls).x2w_pipe.py win2025 1667 --targets cpu_fuzz_x87,cpu_fuzz_x87_lifted,cpu_fuzz_x87_lifted_o2,cpu_fuzz_x87_mutant --regex '^cpu_aot_fuzz_x87$' --force:--preset dev):cpu_aot_fuzz_x871/1 passed (1.06s)./O2(--preset release):cpu_aot_fuzz_x871/1 passed (0.98s)."fsubr",with"fsub",inX87_ARITHMETIC(tools/fuzz_cpu.py) and ranfuzz_cpu.plant_subr_as_sub(None, None, fuzz_cpu.generate_x87(0x8087, 40))— failed as expected withthe subr_as_sub fault found no FSUBR or FSUBRP(exit=1), then restored viagit checkout -- ..AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T18:08:25Z.
Built and ran; the
--mutantcontrol is realAt
79a8bca3, after configuring and building the four DLLs the test loads:One practical note: running the test after a configure but before building those targets fails with
FileNotFoundError: Could not find module ...cpu_fuzz_x87.dll, not with a message about the missing build. Anyone touchingthis fixture will hit it once; a sentence in the docs entry saying which targets to build first would save them the trip.
Is the negative control load-bearing?
--mutantis only useful if the harness fails when the mutant does not diverge, so I pointed it at an unmodified liftedDLL:
It fails, which is the correct behaviour: the control proves the comparison can see a difference, rather than passing
because nothing was compared.
Two questions about determinism
inputs=313for 40 functions — are those inputs fixed constants or seeded per machine? x87 results depend on the hostprecision-control word and on how the lifted code is compiled, so a seed that is merely "stable on my machine" would
make this fixture the first one in the CPU area whose pass depends on the host. If they are fixed tables, one line in
the docs entry saying so is enough.
80-bit intermediate) in the x87 area? That is the failure mode this area can see and most of the others cannot, and if
the fixture does not exercise it, the
--area x87name promises slightly more than it checks.Neither blocks the change; both are the kind of thing a reader will assume from the summary line.
Verdict
Reviewed
79a8bca3. The fixture runs, the comparisons are counted, the refused controls are named individually, and themutant check fails when it should. No objections.
Notes
Imported from R0Y00/Xbox2Windows. Originally posted by RenzoWit35 on 2026-11-24T23:27:17Z.
Thanks both.
b7137699. The fourth control is thefld1/fincstp/fdecstpsequence, notFSIN.x87_inputs(seed), which usesrandom.Random(seed ^ 0x8087)after a fixed table of 17 operand words. Python'srandom.Randomgives the same sequence on every machine, so the inputs depend only on the seed (default0x8087), not on the host.FNSTCWat output offset 48. The lowering fixes that word at 0x027F (53-bit double precision, the Windows x64 default), so a native run under another control word mismatches there. This area has no 53-bit vs 64-bit control of its own.cpu_aot_x87_mulandcpu_aot_x87_divalready have one: a "64-bit precision" mutated translation that must mismatch.cpu_aot_fuzz_x87needscpu_fuzz_x87_lifted,cpu_fuzz_x87_lifted_o2andcpu_fuzz_x87_mutantbuilt. The docs entry names these targets for the longer-run command.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T02:29:41Z.
Review: COMMENTED
Independent technical peer review of PR #1667 at head commit
b7137699e64b917575400874288d69dd40c0a581.Summary
CMakeLists.txt,docs/experiments.md,tools/fuzz_cpu.py(+293/-3). Title and body are neutral; no clean-room concerns. Mergeable.X87Buildertracks stack depth and flag liveness (FCOMI/SAHF vs integer flags) consistently. The four negative controls and thesubr_as_submutant are wired like the sibling areas (MUTANTS,AREAS,translator_module). Other areas'emitoutput is untouched.X87_CONTROLS["fincstp"].Findings
CMakeLists.txt,cpu_fuzz_x87_lifted_o2): the test takes the/O2DLL by path, and the target is onlyALL. A selective build must namecpu_fuzz_x87_lifted_o2(the docs already list it in the build command). The sibling areas behave the same way.tools/fuzz_cpu.py,plant_subr_as_sub): it mutatesx87_lower.ARITHMETICin place. Fine for a one-shot process, and the same global-patch style as the other planted faults.Verification executed locally (Linux)
python3 tests/docs_links_check.py: self-test passed, 16 Markdown files, 352 links.python3 tests/bidi_controls_check.py: 1277 files, no bidirectional controls.git diff --check origin/main...HEAD: clean.python3 -B tools/fuzz_cpu.py emit 0x8087 40 ... --area x87: runs, 1534 asm lines, byte-identical on a second run (deterministic).while self.depth:inX87Builder.finishwithwhile False:. The emitted asm changed (stack-popping instructions fell from 191 to 94, loads 203 to 200). Exports then return with values on the x87 stack, which the lowering refuses withx87 stack not empty at return, so the translate step of the test would fail. Reverted withgit checkout -- ..translateandcheckneed Windows (PE image). I relied on the passing Windows CI job for this head and did not run a separate Win2025 job. The PR does not touch gfx2hlsl or shaders.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by stan495 on 2026-11-25T06:34:52Z.
Review: APPROVED
Verified head
b7137699on Windows x64 (devpreset, MSVC19.44.35228.0, Python 3.12):cmake --build --preset dev --target cpu_fuzz_x87 cpu_fuzz_x87_lifted cpu_fuzz_x87_lifted_o2 cpu_fuzz_x87_mutant-> succeededctest --preset dev --no-tests=error -R '^cpu_aot_fuzz_x87$' -V-> 1/1 passed (area=x87 seed=0x8087 functions=40 inputs=313 comparisons=25040 default and /O2 matched,mutant subr_as_sub: 675/12520 mismatches in 7/40 functions, 4 refused controls checked)Verdict: Approved at
b7137699.Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T18:57:39Z.
Review: COMMENTED
Review of the PR change at
7840b6c8(own diff against origin/main). The branch was then updated with main (a plain merge, no change to the PR's own files), and the Windows run below is on that updated head.What I checked
tools/fuzz_cpu.py: new--area x87adapter and generator. Stack depth is tracked, push depth stays at 8 or less, the stack is empty atRET, PF is read only after FCOMI/SAHF, C1 and exception flags are never read. This matches the documented x87 profile.--mutant(subr_as_sub) fails unless it differs from native, so the test guards the FSUBR/FSUBRP lowering.CMakeLists.txt: the block mirrorscpu_aot_fuzz_loops(seed/count cache vars,emit/translatecustom commands,/O2build, 60 s timeout).DEPENDSincludesffx_version_query.py,x87_lower.py,x87_soft.h, so edits to the lowering rebuild the test.docs/experiments.md: the new paragraph is consistent with the code. Other areas are unchanged:emit --area loopsoutput is byte-identical to origin/main.Commands run (Linux)
python3 -I -m py_compile tools/fuzz_cpu.py: okpython3 tests/docs_links_check.py: 19 files, 471 links, okpython3 tests/bidi_controls_check.py: okgit diff --check origin/main...HEAD: cleanfuzz_cpu.py emit 0x8087 40 ... --area x87twice: identical output, 44 PROC entriesemit --area loopsvs origin/main: identicalThe test itself needs MSVC/ML64, so the real check is the Windows run on the head (dev and release); the receipt follows in a separate comment. No blocking findings.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T19:01:54Z.
Win2025 WARP receipt for head
6d332122after updating with main.Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit
6d3321227d59b0123b71852af114749206590301:--preset dev):cmake --build --preset dev --target cpu_fuzz cpu_fuzz_calls cpu_fuzz_calls_lifted cpu_fuzz_calls_mutant cpu_fuzz_chains cpu_fuzz_chains_lifted cpu_fuzz_chains_mutant cpu_fuzz_frames cpu_fuzz_frames_lifted cpu_fuzz_frames_mutant cpu_fuzz_lifted cpu_fuzz_loops cpu_fuzz_loops_lifted cpu_fuzz_loops_mutant cpu_fuzz_rsp cpu_fuzz_rsp_lifted cpu_fuzz_rsp_mutant cpu_fuzz_scalar_fp cpu_fuzz_scalar_fp_lifted cpu_fuzz_scalar_fp_mutant cpu_fuzz_switches cpu_fuzz_switches_lifted cpu_fuzz_switches_mutant cpu_fuzz_x87 cpu_fuzz_x87_lifted cpu_fuzz_x87_mutant cpu_fuzz_calls_lifted_o2 cpu_fuzz_chains_lifted_o2 cpu_fuzz_frames_lifted_o2 cpu_fuzz_lifted_o2 cpu_fuzz_loops_lifted_o2 cpu_fuzz_rsp_lifted_o2 cpu_fuzz_scalar_fp_lifted_o2 cpu_fuzz_switches_lifted_o2 cpu_fuzz_x87_lifted_o2 -- -j 2):23.04s(configure3.97s)ctest --preset dev -R '^cpu_aot_fuzz' --parallel 2 --no-tests=error --output-on-failure): 9/9 passed (7.84s:cpu_aot_fuzz_frames0.81s,cpu_aot_fuzz_smoke1.15s,cpu_aot_fuzz_chains0.85s,cpu_aot_fuzz_calls1.51s,cpu_aot_fuzz_switches2.03s,cpu_aot_fuzz_scalar_fp2.80s,cpu_aot_fuzz_rsp2.02s,cpu_aot_fuzz_loops1.12s,cpu_aot_fuzz_x871.57s)/O2(--preset release):cmake --build --preset release --target cpu_fuzz cpu_fuzz_calls cpu_fuzz_calls_lifted cpu_fuzz_calls_mutant cpu_fuzz_chains cpu_fuzz_chains_lifted cpu_fuzz_chains_mutant cpu_fuzz_frames cpu_fuzz_frames_lifted cpu_fuzz_frames_mutant cpu_fuzz_lifted cpu_fuzz_loops cpu_fuzz_loops_lifted cpu_fuzz_loops_mutant cpu_fuzz_rsp cpu_fuzz_rsp_lifted cpu_fuzz_rsp_mutant cpu_fuzz_scalar_fp cpu_fuzz_scalar_fp_lifted cpu_fuzz_scalar_fp_mutant cpu_fuzz_switches cpu_fuzz_switches_lifted cpu_fuzz_switches_mutant cpu_fuzz_x87 cpu_fuzz_x87_lifted cpu_fuzz_x87_mutant cpu_fuzz_calls_lifted_o2 cpu_fuzz_chains_lifted_o2 cpu_fuzz_frames_lifted_o2 cpu_fuzz_lifted_o2 cpu_fuzz_loops_lifted_o2 cpu_fuzz_rsp_lifted_o2 cpu_fuzz_scalar_fp_lifted_o2 cpu_fuzz_switches_lifted_o2 cpu_fuzz_x87_lifted_o2 -- -j 2):23.82s(configure3.53s)ctest --preset release -R '^cpu_aot_fuzz' --parallel 2 --no-tests=error --output-on-failure): 9/9 passed (6.36s:cpu_aot_fuzz_frames0.71s,cpu_aot_fuzz_smoke0.93s,cpu_aot_fuzz_chains0.67s,cpu_aot_fuzz_calls1.21s,cpu_aot_fuzz_switches1.85s,cpu_aot_fuzz_scalar_fp2.28s,cpu_aot_fuzz_rsp1.43s,cpu_aot_fuzz_loops0.93s,cpu_aot_fuzz_x871.25s)sccache: 0/85 hits; wall70.0sAI-assisted; commands listed were executed locally.