tooling: add counted-loop generators to the CPU fuzz test #1461
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1468"
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-24T12:03:45Z.
Refs #446
Adds the loop area claimed in #446 (https://github.com/R0Y00/Xbox2Windows/issues/446#issuecomment-7129845006): counted loops that end in a backward conditional branch, with values carried between iterations in registers and in the 256-byte output buffer, data-dependent early exits and a nested loop. Every trip count is masked, so each input terminates. A planted fault retargets every back edge and must be detected.
As asked in #446 (https://github.com/R0Y00/Xbox2Windows/issues/446#issuecomment-7130023965): the area has its own seed,
0x100F; the existing generators' emitted cases are unchanged; under the planted back-edge fault every input still terminates, and the test requires a native/AOT mismatch at that fixed seed. The branch sits on top of #1409, with the loop registration and CMake block after therspones.Change
tools/fuzz_cpu.py --area loopsgenerates leaf functions for the sharedcpu_switch_aotlifter from its own seed, in one MASM DLL and one translation. The new CTest iscpu_aot_fuzz_loops; the default is seed0x100Fwith 32 exports.DECorSUB 1, thenJNZ) or from n-1 down to 0 (DEC, thenJNS); an index from 0 up to n-1 (INC,CMPwith the count, thenJB,JNEorJL); a counter that may start at 0, behind aTESTandJZthat skips the loop; and a counter that drops by 2 or 3 while it stays above 0 (SUB, thenJG).ADD,SUB,XOR, three-operandIMUL,ROL,ROR,LEA,NOT,NEG). Some exports push one or two of RBX, RSI and RDI, carry a value in the first of them and pop them on both exits.TESTof AL or R10D or an unsignedCMPof EAX with a constant leads to a second exit that records the counter (the count, in the up form) and returns another combination.DECandJNZ.translate --mutant(back_edge_exit) retargets every backward conditional branch to its own fallthrough while the translator decodes it, so each loop body runs once and every input still terminates.check --mutantruns it against native, and the test fails if it never differs..defof the leaf, frames, calls, chains, switches, scalar_fp and rsp areas, and their clean and mutant translations, are byte-identical tomainin dev and release.Measurements
Commit
1e788c6on85f9dd8, after #1409. Also run on the merge withbb6c9a0: the same two output lines in dev and release (3 runs each), and-R '^cpu_aot_fuzz_'8/8 in both presets. Intel Core i7-10700, Windows 11 build 26200, MSVC 19.44.35230, CMake/Ninja, Python 3.12.cpu_aot_fuzz_loopsDefault seed contents: the 16 combinations of form, nesting and early exit, twice; 48 backward branches (outer: 10
JNZ, 6JNS, 8JG, 3JB, 3JNE, 2JL; inner: 16JNZ); 16 exports with pushed registers and 8 with a guard. Over the 10,016 native calls, 1,573 take the early exit, 632 skip the guarded loop and 6,380 run the outer loop more than once, up to 16 times. In the generated C++, only the 48 backwardgotos change, each now targeting its fallthrough (their labels move with them).Failure paths, run by hand against the dev build:
fuzz_loop_0input 1 (native RAX0x2a, translated0xfffffffffffffffa), with the first differing output byte, 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.Longer manual run:
cmake --preset dev -DCPU_FUZZ_LOOPS_SEED=<seed> -DCPU_FUZZ_LOOPS_FUNCTIONS=128, then the same test.0x10x20xBEEF0xCCD1Wider checks:
ctest --preset dev --no-tests=error -R '^cpu_aot_fuzz_'and the same with--preset release: 8/8 pass.ctest --preset dev --no-tests=error -R '^cpu_'after a full dev build: 245/245 pass, with 2 capability skips (cpu_aot_fences_hints_clwb,cpu_aot_crypto_sha).tests/docs_links_check.py(16 files, 325 links),tests/bidi_controls_check.pyandgit diff --checkpass.Not run: MSVC 19.52, Linux, more than 128 exports.
Scope
Leaf functions only, with optional pushed registers. Counts not bounded by a mask, unconditional backward
JMPs,LOOPandREPforms, frames with stack slots, calls inside a loop and loops that share a head are not generated.docs/experiments.mdgains acpu_aot_fuzz_loopsparagraph after therspone.AI-assisted.