tooling: add fixed-frame generators to the CPU fuzz test #920

Merged
denn0x merged 1 commit from upstream-pr/927 into main 2026-11-23 17:47:46 +00:00
denn0x commented 2026-11-23 17:31:47 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-23T17:31:47Z.


Refs #446

Adds the fixed-frame generator suggested in #446 after #894: fixed frames, saved and restored registers, partial spill overlaps, and a planted error. There are no calls.

Change

tools/fuzz_cpu.py --area frames generates functions for the shared cpu_switch_aot lifter from its own seed. The new CTest is cpu_aot_fuzz_frames; the default is seed 0xF4A3 with 16 functions.

  • Saves. One to three of RBX, RSI, RDI and R12–R15 are saved with PUSH. Alternatively, as MSVC does at /O2, RBX and RSI are saved into home space with MOV [rsp+8] / MOV [rsp+10h] before PUSH RDI.
  • Frame. SUB RSP allocates 16 to 40 bytes and leaves RSP 16-byte aligned. The prologue carries the matching .pushreg, .savereg and .allocstack directives.
  • Restores. Saved registers come back through MOV or POP, and ADD RSP releases the frame.
  • Entry values. Each saved register is overwritten from RCX and RDX before use. The translator rejects any read of a nonvolatile register's entry value, so a refusal of a generated function would be a real over-refusal.
  • Spill overlap. A random sequence of 8-, 4-, 2- and 1-byte stores fills a 16-byte region of the allocation from registers or immediates.
    • Some functions add a register to a piece of the region in place, or overwrite bytes on either side of a branch.
    • Four to six reloads of 1, 2, 4 or 8 bytes follow, zero- or sign-extended. The first is placed across a store boundary.
    • Every reload goes to the output buffer and into RAX.
  • Planted error. translate --mutant builds a second translation in which every frame reload narrower than 8 bytes reads one byte higher in the frame array. check --mutant runs it against native, and the test fails if it never differs.
    • This mutant is broader than the single-reload shift I described in #446. One shifted reload can be invisible to RAX and the buffer, for example when two adjacent bytes hold the same value, so the count would depend on the seed.
    • A dropped register restore isn't used because it can't be observed here, for the reason cpu_aot_stack_frames gives.
  • Leaf smoke. The existing smoke is unchanged. Its emitted cpu_fuzz.asm/.def for 0xA902/20 and 0xCCD1/96, and the generated cpu_fuzz_lifted.cpp, are byte-identical to main. Its output line is unchanged.
  • Reproduction line. It now includes --area and prints the seed in hex. Previously it printed decimal, which still parsed.

Measurements

Commit f9ad552 on 94bc632. AMD Ryzen 7 7700, Windows 11 build 26300, MSVC x64.

ctest --preset dev --no-tests=error -R '^cpu_aot_fuzz_(smoke|frames)$' -V
seed=0xa902 functions=20 inputs=313 comparisons=12520 default and /O2 matched
area=frames seed=0xf4a3 functions=16 inputs=313 comparisons=10016 default and /O2 matched
mutant reload_shift: 4945/5008 mismatches in 16/16 functions

--preset release gives the same three lines. Each test takes about 0.3 s.

Default seed contents:

  • 5 of the 16 functions use home-space saves, 9 have a branch and 9 have an in-place add.
  • The regions receive 90 stores.
  • There are 82 reloads: 19 of 8 bytes, 23 of 4, 24 of 2 and 16 of 1.
  • 44 reloads span two or more stores, with at least one in every function. 33 read part of one store, and 5 read one store exactly.

The failure paths were run by hand against the dev build:

  • Mutant passed as the translation. Exit 1 at the first mismatch, fuzz_frame_0 input 0. It printed native return=0x38409001c000 default return=0x38384061c000, then the first differing output byte (offset 0: 00 native, 07 mutant), the function's MASM including its prologue, and reproduce: python -B tools/fuzz_cpu.py check 0xf4a3 16 ... --area frames --only fuzz_frame_0 --input 0. That command repeats the case.
  • Clean translation passed as --mutant. Exit 1, printing the planted translator fault was not detected and mutant reload_shift: 0/5008 mismatches in 0/16 functions.

Longer manual run: cmake --preset dev -DCPU_FUZZ_FRAMES_SEED=<seed> -DCPU_FUZZ_FRAMES_FUNCTIONS=128, then the same test.

Seed Comparisons Time Refusals Mismatches Reloads spanning stores Mutant mismatches
0x1 80,128 1.24 s 0 0 360/645 38,837/40,064
0x2 80,128 1.25 s 0 0 362/647 39,065/40,064
0xBEEF 80,128 1.21 s 0 0 356/634 38,982/40,064
0xCCD1 80,128 1.23 s 0 0 372/652 38,496/40,064

These runs found no translator difference. In 3 of the 512 functions the mutant didn't differ:

  • In 2 of them (fuzz_frame_6 at 0x2 and fuzz_frame_77 at 0xCCD1), every reload is 8 bytes wide, and the mutant leaves those alone.
  • In the third, at 0x1, the shifted reloads gave the same results on all 313 inputs.

Wider checks:

  • ctest --preset dev --no-tests=error -R '^cpu_': 172/172 pass on the rebased tree. The only later change is the hex seed in the reproduction line.
  • tests/docs_links_check.py (13 files, 183 links), tests/bidi_controls_check.py and git diff --check pass.

Scope

The generator uses only RSP-based fixed frames. There is no RBP anchor and no LEAVE, so #907 and #895 can change Frame independently. Calls, call graphs, mid-body RSP changes and fault equivalence remain follow-ups.

docs/experiments.md gains a cpu_aot_fuzz_frames paragraph in the fuzz section.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6565058679 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/927). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-23T17:31:47Z. --- Refs #446 Adds the fixed-frame generator suggested in #446 after #894: fixed frames, saved and restored registers, partial spill overlaps, and a planted error. There are no calls. ## Change `tools/fuzz_cpu.py --area frames` generates functions for the shared `cpu_switch_aot` lifter from its own seed. The new CTest is `cpu_aot_fuzz_frames`; the default is seed `0xF4A3` with 16 functions. - **Saves.** One to three of RBX, RSI, RDI and R12–R15 are saved with `PUSH`. Alternatively, as MSVC does at `/O2`, RBX and RSI are saved into home space with `MOV [rsp+8]` / `MOV [rsp+10h]` before `PUSH RDI`. - **Frame.** `SUB RSP` allocates 16 to 40 bytes and leaves RSP 16-byte aligned. The prologue carries the matching `.pushreg`, `.savereg` and `.allocstack` directives. - **Restores.** Saved registers come back through `MOV` or `POP`, and `ADD RSP` releases the frame. - **Entry values.** Each saved register is overwritten from RCX and RDX before use. The translator rejects any read of a nonvolatile register's entry value, so a refusal of a generated function would be a real over-refusal. - **Spill overlap.** A random sequence of 8-, 4-, 2- and 1-byte stores fills a 16-byte region of the allocation from registers or immediates. - Some functions add a register to a piece of the region in place, or overwrite bytes on either side of a branch. - Four to six reloads of 1, 2, 4 or 8 bytes follow, zero- or sign-extended. The first is placed across a store boundary. - Every reload goes to the output buffer and into RAX. - **Planted error.** `translate --mutant` builds a second translation in which every frame reload narrower than 8 bytes reads one byte higher in the frame array. `check --mutant` runs it against native, and the test fails if it never differs. - This mutant is broader than the single-reload shift I described in #446. One shifted reload can be invisible to RAX and the buffer, for example when two adjacent bytes hold the same value, so the count would depend on the seed. - A dropped register restore isn't used because it can't be observed here, for the reason `cpu_aot_stack_frames` gives. - **Leaf smoke.** The existing smoke is unchanged. Its emitted `cpu_fuzz.asm`/`.def` for `0xA902`/20 and `0xCCD1`/96, and the generated `cpu_fuzz_lifted.cpp`, are byte-identical to `main`. Its output line is unchanged. - **Reproduction line.** It now includes `--area` and prints the seed in hex. Previously it printed decimal, which still parsed. ## Measurements Commit `f9ad552` on `94bc632`. AMD Ryzen 7 7700, Windows 11 build 26300, MSVC x64. ```powershell ctest --preset dev --no-tests=error -R '^cpu_aot_fuzz_(smoke|frames)$' -V ``` ``` seed=0xa902 functions=20 inputs=313 comparisons=12520 default and /O2 matched area=frames seed=0xf4a3 functions=16 inputs=313 comparisons=10016 default and /O2 matched mutant reload_shift: 4945/5008 mismatches in 16/16 functions ``` `--preset release` gives the same three lines. Each test takes about 0.3 s. Default seed contents: - 5 of the 16 functions use home-space saves, 9 have a branch and 9 have an in-place add. - The regions receive 90 stores. - There are 82 reloads: 19 of 8 bytes, 23 of 4, 24 of 2 and 16 of 1. - 44 reloads span two or more stores, with at least one in every function. 33 read part of one store, and 5 read one store exactly. The failure paths were run by hand against the dev build: - **Mutant passed as the translation.** Exit 1 at the first mismatch, `fuzz_frame_0` input 0. It printed `native return=0x38409001c000 default return=0x38384061c000`, then the first differing output byte (offset 0: `00` native, `07` mutant), the function's MASM including its prologue, and `reproduce: python -B tools/fuzz_cpu.py check 0xf4a3 16 ... --area frames --only fuzz_frame_0 --input 0`. That command repeats the case. - **Clean translation passed as `--mutant`.** Exit 1, printing `the planted translator fault was not detected` and `mutant reload_shift: 0/5008 mismatches in 0/16 functions`. Longer manual run: `cmake --preset dev -DCPU_FUZZ_FRAMES_SEED=<seed> -DCPU_FUZZ_FRAMES_FUNCTIONS=128`, then the same test. | Seed | Comparisons | Time | Refusals | Mismatches | Reloads spanning stores | Mutant mismatches | | --- | ---: | ---: | ---: | ---: | ---: | ---: | | `0x1` | 80,128 | 1.24 s | 0 | 0 | 360/645 | 38,837/40,064 | | `0x2` | 80,128 | 1.25 s | 0 | 0 | 362/647 | 39,065/40,064 | | `0xBEEF` | 80,128 | 1.21 s | 0 | 0 | 356/634 | 38,982/40,064 | | `0xCCD1` | 80,128 | 1.23 s | 0 | 0 | 372/652 | 38,496/40,064 | These runs found no translator difference. In 3 of the 512 functions the mutant didn't differ: - In 2 of them (`fuzz_frame_6` at `0x2` and `fuzz_frame_77` at `0xCCD1`), every reload is 8 bytes wide, and the mutant leaves those alone. - In the third, at `0x1`, the shifted reloads gave the same results on all 313 inputs. Wider checks: - `ctest --preset dev --no-tests=error -R '^cpu_'`: 172/172 pass on the rebased tree. The only later change is the hex seed in the reproduction line. - `tests/docs_links_check.py` (13 files, 183 links), `tests/bidi_controls_check.py` and `git diff --check` pass. ## Scope The generator uses only RSP-based fixed frames. There is no RBP anchor and no `LEAVE`, so #907 and #895 can change `Frame` independently. Calls, call graphs, mid-body RSP changes and fault equivalence remain follow-ups. `docs/experiments.md` gains a `cpu_aot_fuzz_frames` paragraph in the fuzz section. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-24 06:45:27 +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!920
No description provided.