tooling: add counted-loop generators to the CPU fuzz test #1461

Merged
roansystemlabs merged 1 commit from upstream-pr/1468 into main 2026-11-24 12:29:24 +00:00
roansystemlabs commented 2026-11-24 12:03:45 +00:00 (Migrated from github.com)

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 the rsp ones.

Change

tools/fuzz_cpu.py --area loops generates leaf functions for the shared cpu_switch_aot lifter from its own seed, in one MASM DLL and one translation. The new CTest is cpu_aot_fuzz_loops; the default is seed 0x100F with 32 exports.

  • Loop forms. A counter from n down to 1 (DEC or SUB 1, then JNZ) or from n-1 down to 0 (DEC, then JNS); an index from 0 up to n-1 (INC, CMP with the count, then JB, JNE or JL); a counter that may start at 0, behind a TEST and JZ that skips the loop; and a counter that drops by 2 or 3 while it stays above 0 (SUB, then JG).
  • Bounded. n is x masked to four bits (three in a nested loop), plus one where there is no guard, and only the loop's own update writes the counter: at most 16 iterations, or 8 by 8 when nested.
  • Carried in registers. Each iteration updates RAX and one or two of RDX and R10 from each other and from the counter (64- and 32-bit ADD, SUB, XOR, three-operand IMUL, 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.
  • Carried in memory. The loop updates the qword at output offset 24 in place and reads it back into a register, counts its iterations at offset 32, and writes RAX into a per-iteration slot from offset 64 indexed by the counter, so a wrong trip count shows in the buffer even when the final registers agree.
  • Early exits. Half the exports have a data-dependent early exit: a TEST of AL or R10D or an unsigned CMP of EAX with a constant leads to a second exit that records the counter (the count, in the up form) and returns another combination.
  • Nested loop. Half nest an inner loop whose count is the low three bits of the carried RAX plus one, so it changes from one outer iteration to the next; it adds R10 into a per-iteration slot from offset 192 and ends with DEC and JNZ.
  • Planted error. 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 --mutant runs it against native, and the test fails if it never differs.
  • Existing tests. The emitted MASM and .def of the leaf, frames, calls, chains, switches, scalar_fp and rsp areas, and their clean and mutant translations, are byte-identical to main in dev and release.

Measurements

Commit 1e788c6 on 85f9dd8, after #1409. Also run on the merge with bb6c9a0: 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.

ctest --preset dev --no-tests=error -R '^cpu_aot_fuzz_loops$' -V
area=loops seed=0x100f functions=32 inputs=313 comparisons=20032 default and /O2 matched
mutant back_edge_exit: 7439/10016 mismatches in 32/32 functions
Preset Runs cpu_aot_fuzz_loops
dev 3 0.46–0.54 s
release 3 0.46–0.52 s

Default seed contents: the 16 combinations of form, nesting and early exit, twice; 48 backward branches (outer: 10 JNZ, 6 JNS, 8 JG, 3 JB, 3 JNE, 2 JL; inner: 16 JNZ); 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 backward gotos change, each now targeting its fallthrough (their labels move with them).

Failure paths, run by hand against the dev build:

  • Mutant passed as the translation. Exit 1 at fuzz_loop_0 input 1 (native RAX 0x2a, translated 0xfffffffffffffffa), with the first differing output byte, the MASM and the reproduce: line. That command repeats the case; with the clean translation it exits 0.
  • Clean translation passed as --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.

Seed Exports Comparisons Time Refusals Mismatches Backward branches Mutant mismatches
0x1 128 80,128 1.85 s 0 0 192 29,616/40,064
0x2 128 80,128 1.60 s 0 0 192 29,248/40,064
0xBEEF 128 80,128 1.71 s 0 0 192 29,564/40,064
0xCCD1 128 80,128 1.63 s 0 0 192 29,894/40,064

Wider 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.py and git diff --check pass.

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, LOOP and REP forms, frames with stack slots, calls inside a loop and loops that share a head are not generated. docs/experiments.md gains a cpu_aot_fuzz_loops paragraph after the rsp one.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6581333333 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1468). Originally posted by [roansystemlabs](https://github.com/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 the `rsp` ones. ## Change `tools/fuzz_cpu.py --area loops` generates leaf functions for the shared `cpu_switch_aot` lifter from its own seed, in one MASM DLL and one translation. The new CTest is `cpu_aot_fuzz_loops`; the default is seed `0x100F` with 32 exports. - **Loop forms.** A counter from n down to 1 (`DEC` or `SUB 1`, then `JNZ`) or from n-1 down to 0 (`DEC`, then `JNS`); an index from 0 up to n-1 (`INC`, `CMP` with the count, then `JB`, `JNE` or `JL`); a counter that may start at 0, behind a `TEST` and `JZ` that skips the loop; and a counter that drops by 2 or 3 while it stays above 0 (`SUB`, then `JG`). - **Bounded.** n is x masked to four bits (three in a nested loop), plus one where there is no guard, and only the loop's own update writes the counter: at most 16 iterations, or 8 by 8 when nested. - **Carried in registers.** Each iteration updates RAX and one or two of RDX and R10 from each other and from the counter (64- and 32-bit `ADD`, `SUB`, `XOR`, three-operand `IMUL`, `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. - **Carried in memory.** The loop updates the qword at output offset 24 in place and reads it back into a register, counts its iterations at offset 32, and writes RAX into a per-iteration slot from offset 64 indexed by the counter, so a wrong trip count shows in the buffer even when the final registers agree. - **Early exits.** Half the exports have a data-dependent early exit: a `TEST` of AL or R10D or an unsigned `CMP` of EAX with a constant leads to a second exit that records the counter (the count, in the up form) and returns another combination. - **Nested loop.** Half nest an inner loop whose count is the low three bits of the carried RAX plus one, so it changes from one outer iteration to the next; it adds R10 into a per-iteration slot from offset 192 and ends with `DEC` and `JNZ`. - **Planted error.** `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 --mutant` runs it against native, and the test fails if it never differs. - **Existing tests.** The emitted MASM and `.def` of the leaf, frames, calls, chains, switches, scalar_fp and rsp areas, and their clean and mutant translations, are byte-identical to `main` in dev and release. ## Measurements Commit `1e788c6` on `85f9dd8`, after #1409. Also run on the merge with `bb6c9a0`: 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. ```powershell ctest --preset dev --no-tests=error -R '^cpu_aot_fuzz_loops$' -V ``` ``` area=loops seed=0x100f functions=32 inputs=313 comparisons=20032 default and /O2 matched mutant back_edge_exit: 7439/10016 mismatches in 32/32 functions ``` | Preset | Runs | `cpu_aot_fuzz_loops` | | --- | ---: | ---: | | dev | 3 | 0.46–0.54 s | | release | 3 | 0.46–0.52 s | Default seed contents: the 16 combinations of form, nesting and early exit, twice; 48 backward branches (outer: 10 `JNZ`, 6 `JNS`, 8 `JG`, 3 `JB`, 3 `JNE`, 2 `JL`; inner: 16 `JNZ`); 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 backward `goto`s change, each now targeting its fallthrough (their labels move with them). Failure paths, run by hand against the dev build: - **Mutant passed as the translation.** Exit 1 at `fuzz_loop_0` input 1 (native RAX `0x2a`, translated `0xfffffffffffffffa`), with the first differing output byte, the MASM and the `reproduce:` line. That command repeats the case; with the clean translation it exits 0. - **Clean translation passed as `--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. | Seed | Exports | Comparisons | Time | Refusals | Mismatches | Backward branches | Mutant mismatches | | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | | `0x1` | 128 | 80,128 | 1.85 s | 0 | 0 | 192 | 29,616/40,064 | | `0x2` | 128 | 80,128 | 1.60 s | 0 | 0 | 192 | 29,248/40,064 | | `0xBEEF` | 128 | 80,128 | 1.71 s | 0 | 0 | 192 | 29,564/40,064 | | `0xCCD1` | 128 | 80,128 | 1.63 s | 0 | 0 | 192 | 29,894/40,064 | Wider 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.py` and `git diff --check` pass. 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 `JMP`s, `LOOP` and `REP` forms, frames with stack slots, calls inside a loop and loops that share a head are not generated. `docs/experiments.md` gains a `cpu_aot_fuzz_loops` paragraph after the `rsp` one. AI-assisted.
upstream-sync closed this pull request 2026-11-24 13:00:39 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!1461
No description provided.