tooling: add bit-operation generators to the CPU fuzz test #1777
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1784"
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 roansystemlabs on 2026-11-24T18:38:05Z.
Refs #446.
Adds the bits area claimed in #446 (https://github.com/R0Y00/Xbox2Windows/issues/446#issuecomment-7139407793): bit tests, bit counts, bit scans, byte swaps and immediate shifts on the shared
cpu_switch_aotlifter, with every flag read only under the conditions the translator defines for its producer. Six refused controls cover the documented limits, and a planted fault in the bit-test lowering must be detected.As asked for the other areas in #446: the area has its own seed,
0xB175, and the existing generators' emitted cases and translations are unchanged.Change
tools/fuzz_cpu.py --area bitsgenerates straight-line leaf functions from its own seed, in one MASM DLL and one translation. The new CTest iscpu_aot_fuzz_bits; the default is seed0xB175with 40 exports.BT,BTS,BTRorBTCon a 16-, 32- or 64-bit register base with an immediate or register offset; the same on a memory base with an immediate offset;POPCNT,LZCNTorTZCNT, andBSForBSR, from a register or memory source; a 32- or 64-bitBSWAP, sometimes between a bit test and its readers; andSHL,SHRorSARof an 8- to 64-bit register by 1 up to the operand width minus 1. About one immediate bit offset in five (21 of 123 at the default seed) is past the operand width. Exports are straight-line; everyJccjumps forward, so every input terminates, under the planted fault too.BSForBSRshows its unchanged destination, upper half included.SETccinto the buffer,CMOVccinto a carried register and a forwardJccaround an update of one, only under the defined conditions: B and AE after a bit test, E and NE after a bit scan, B, AE, E, NE, BE and A afterLZCNTandTZCNT, all sixteen afterPOPCNTand a shift by 1, and all but O, NO, L, GE, LE and G after a larger shift.SETZafterBT,SETSafterLZCNT,SETCafterBSF,SETCafter an 8-bitSHLby 8 andSETOafterSHRby 3; each must be refused with the translator's reason.host_bit_countsintests/cpu/switch_bit_counts.asm, is called first; without POPCNT, BMI1 and ABM the test is skipped (exit 77), ascpu_aot_switch_bit_countsis. It is the only generic change:Function.host_featuresmarks such a probe, which is exported but never translated or compared; the other areas set no probe, so their filters select the same functions as before.translate --mutant(set_reset_swap) makesBTSclear the selected bit andBTRset it; CF,BTandBTCstay right.check --mutantruns it against native, and the test fails if it never differs..defof the eight existing areas (at their default seed and one longer seed each), and their clean and mutant translations in dev and release, are byte-identical tomain.Measurements
Commit
f5edebconb4c961c. Intel Core i7-10700 (POPCNT, BMI1, ABM), Windows 11 build 26200, MSVC 19.44.35230, CMake/Ninja, Python 3.12.cpu_aot_fuzz_bitsDefault seed contents, counted from the emitted MASM: 154 bit tests (66 on memory, 31 with a register offset, 21 with an immediate offset past the width), 57 counts, 54 scans, 57 byte swaps (33 at 32 bits, 24 at 64) and 63 shifts, read by 331
SETcc, 160CMOVccand 143Jcc. Replaying the sources on the 313 inputs, 5,515 of 16,902 scans and 2,982 of 10,016LZCNTandTZCNTcalls have a zero source. The three exports the mutant leaves equal to native have noBTSorBTR.Failure paths, run by hand against the dev build:
fuzz_bits_0input 0 (native RAX0x1231f1d, translated0x1231f1f; first differing byte at output offset 16, the step'sBTS), with the MASM and thereproduce:line. That command repeats the case; with the clean translation it exits 0.--mutant. Exit 1,the planted translator fault was not detected.capability skip: ....Longer manual run:
cmake --preset dev -DCPU_FUZZ_BITS_SEED=<seed> -DCPU_FUZZ_BITS_FUNCTIONS=128, then the same test (on9ffb852;0xCCD1also onf58d242, with the same counts).0x10x20xBEEF0xCCD1Wider checks:
ctest --preset dev --no-tests=error -R '^cpu_aot_fuzz_'and the same with--preset release: 9/9 pass.ctest --preset dev --no-tests=error -R '^cpu_'after a full dev build onf58d242: 258/258 pass, with 3 capability skips (cpu_aot_switch_fsgsbase_rdpid,cpu_aot_fences_hints_clwb,cpu_aot_crypto_sha). The commits since touch no CPU tool, CPU test or fuzz block.tests/docs_links_check.py(17 files, 367 links),tests/bidi_controls_check.pyandgit diff --checkpass.Not run: MSVC 19.5x, Linux, an AMD processor, a host without BMI1 or ABM (the skip was only simulated), more than 128 exports.
Scope
Leaf functions only. 16-bit
BSWAP(refused: its result is undefined), shifts by CL, rotates as flag producers,SHLDandSHRD, and the BMI1 and BMI2 forms beyondLZCNTandTZCNTare not generated. #1667 (x87) touches the same three files; if it lands first, I'll rebase and rerun these checks.docs/experiments.mdgains acpu_aot_fuzz_bitsparagraph after theloopsone.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T18:56:57Z.
Review: COMMENTED
Peer review of head
f5edebc41582022943f797c47827353ae230b30c.Result: no P1/P2 defects found in the local verification.
Read the full diff (CMakeLists.txt, docs/experiments.md, tools/fuzz_cpu.py) and checked:
Win2025 (log from the coordinator run at this head), Debug
devand Release/O2: build of cpu_fuzz_bits, cpu_fuzz_bits_lifted, cpu_fuzz_bits_lifted_o2 and cpu_fuzz_bits_mutant succeeded;ctest -R '^cpu_aot_fuzz_bits$'passed 1/1 in both presets.Linux checks run locally in a clean worktree at this head:
git diff --check origin/main...HEAD: cleantests/docs_links_check.py: passed (17 Markdown files, 367 links)tests/bidi_controls_check.py: passed (1304 files)python -B -m py_compile tools/fuzz_cpu.py: passedfuzz_cpu.py emit 0xB175 40 ... --area bitsrun twice: .asm and .def are byte-identical; seed 0x1 gives a different .asm;--area loopsemit still works.sets,seto).Mutation (local, reverted afterwards): adding
"e"toBITS_CONDITIONS["test"]made the emitted sources contain many ZF readers after bit tests, and the static check above flagged them all (exit 1). The tree was restored withgit checkout -- .and is clean. The translator-side planted fault (set_reset_swap) is exercised by the Win2025 run, where the test requires the mutant to differ from native.Non-blocking note: the docs state that a scan of a zero source leaves the whole destination register unchanged, upper half included. That is the behavior on the Win2025 host, but vendors differ on how a 32-bit scan treats the upper half, so the comparison could differ on other processors. Worth a sentence in the docs.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by stan495 on 2026-11-25T05:48:18Z.
Review: APPROVED
Verified head
f5edebc4on Windows x64 (devpreset, MSVC19.44.35228.0, Python 3.12).cmake --build --preset dev --target cpu_fuzz_bits cpu_fuzz_bits_lifted cpu_fuzz_bits_lifted_o2 cpu_fuzz_bits_mutant-> succeededctest --preset dev --no-tests=error -R '^cpu_aot_fuzz_bits$' -V-> 1/1 passedVerdict: Approved at
f5edebc4.Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T19:03:25Z.
Review: COMMENTED
Review of head
b1bb4224against main. Verdict: one blocking finding, the new test fails on this head.Blocking: the
bit_stringrefused control is stale.tools/fuzz_cpu.py:1599expectsbt DWORD PTR [r8], ecxto be refused withunsupported bit-string operand.tools/cpu_switch_aot.py:1268-1272).ctest --preset dev -R cpu_aot_fuzz_bits):fuzz_bits_refused_bit_string was translated; expected refusal: unsupported bit-string operand, test FAILED (thearea=bits ... default and /O2 matchedline is printed, so only the refused-control part breaks).[r8d]) or a frame-resident base, and keep the expected message. Then re-run; the other five refused controls and the planted fault were not reported as failing.Other checks:
emitoutput for leaf, frames, loops, scalar_fp and switches is byte-identical between main and this head (seed 0x1234, 20 exports); the bits area is deterministic for seed 0xB175.python3 tests/docs_links_check.py,python3 tests/bidi_controls_check.py,git diff --check origin/main...HEAD: clean.*_lifted_o2custom targets, so a barewin2025run of the fuzz tests fails on a missing DLL for every area; I built the bits targets explicitly (cpu_fuzz_bits,_lifted,_mutant,_lifted_o2).CMakeLists.txtregion, so the second one to land will need a merge.Not merged. Happy to re-run on Win2025 once the control is updated.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by roansystemlabs on 2026-11-25T19:25:18Z.
Thanks for catching this. #2031 lowers
BTwith a register offset on a plain 64-bit base, so the control was stale.I rebased the branch onto main at
32a2e2f6fas one commit (it also merges cleanly with the currente2faf858c). The conflicts with #1667 were only adjacent insertions (theAREASandMUTANTStables, the generator block, the CMake region and the docs paragraph), and both areas are kept. The control now uses a 32-bit base, whichcpu_switch_aot.pystill refuses with the same message:The docs now name the control as a register bit offset through a 32-bit base register.
Run on Windows 11 x64, MSVC 19.44,
devandrelease:cmake --build --preset <preset> --target cpu_fuzz_bits cpu_fuzz_bits_lifted cpu_fuzz_bits_mutant cpu_fuzz_bits_lifted_o2: succeededctest --preset <preset> --no-tests=error -R '^cpu_aot_fuzz_bits$' -V: 1/1 passed in both; 25,040 comparisons matched, all six controls refused for their own reason, the planted fault differed in 11,581 of 12,520 calls[r8]control on the same base,devfails exactly as you reported (fuzz_bits_refused_bit_string was translated)emitfor the other nine areas is byte-identical to main; the bits emit only changes that one linetests/docs_links_check.py,tests/bidi_controls_check.py,git diff --check origin/main...HEAD: cleanThanks also for the offer to re-run on Win2025; that would be welcome.
AI-assisted.