tooling: add direct-call generators to the CPU fuzz test #1157

Merged
roansystemlabs merged 1 commit from upstream-pr/1164 into main 2026-11-24 02:48:57 +00:00
roansystemlabs commented 2026-11-24 02:09:47 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by roansystemlabs on 2026-11-24T02:09:47Z.


Refs #446

Adds the direct-call generator claimed in #446 after #927: framed callers that make direct calls and tail jumps to callees from the existing leaf and fixed-frame generators, shared callees reached with different values, a case that observes the expected volatile-register clobbers, and a planted wrong-callee fault. No recursion and no call chains deeper than one level.

Coordination: as agreed in #446 (owner's note and https://github.com/R0Y00/Xbox2Windows/issues/446#issuecomment-7119501743), this is the first direct-call PR. @tedin7 opened #1129 for the same generator; a review of the shared-callee and tail-jump cases, or an independent run of the wrong-callee mutation on Linux/LLVM, would be welcome, and deeper call chains could follow on top.

Change

tools/fuzz_cpu.py --area calls generates callers and callees 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_calls; the default is seed 0xCA11 with 16 callers.

  • Callees. Half as many as callers, alternating leaf functions sampled from the leaf generator and functions from the frame generator, plus fuzz_clobber (below). They keep their generator names, are exported, and are compared on their own as well.
  • Callers. Three of RBX, RSI, RDI and R12–R15 are saved with PUSH and 32 or 48 bytes are allocated, so RSP is 8 mod 16 from entry at every call and the callee's home space lies below the return address, as #425 requires. The prologue carries .pushreg and .allocstack.
  • Calls. One to three direct CALLs. Before each one, RCX, RDX and R8 are defined again from the saved registers, or from RAX after a call. After a call the caller reads only RAX. Every callee may overwrite the volatile registers, and a frame callee also overwrites the nonvolatile registers it saves, often the caller's own.
  • Shared callees. With an even count, each generated callee after the first two is the first call of two callers; the first two are the first call of one caller each, because the first two callers start with fuzz_clobber. Later calls and tail jumps reach every callee again, and a later call may repeat the previous callee with its result as an argument. RDX gets a displacement that is unique in the program, so callers that share a callee pass it different values.
  • Volatile clobbers. For any seed, fuzz_call_0 and fuzz_call_1 first call fuzz_clobber with different values. It overwrites RAX, RCX, RDX and R8–R11 with values derived from its arguments and writes each one to its output segment before the next replaces it, so the clobbers are compared through memory and RAX, the results a caller may observe.
  • Refused reads. fuzz_clobber_read_r10 and fuzz_clobber_read_rcx are in the same DLL but not run: each reads R10 or RCX after calling fuzz_clobber. check translates each one alone and fails unless it is refused with undefined register r10 or undefined register rcx.
  • Frame kept across calls. Some callers keep a spill above the callee's home space across their calls.
  • Tail jumps. Half the callers end with JMP callee after ADD RSP and the POPs.
  • Output. Each call writes its callee's output into its own 48-byte segment of the buffer, and the caller stores RAX after each call from byte 192.
  • Planted error. translate --mutant builds a second translation in which the first call of the first caller goes to the next callee in the generated order. check --mutant runs it against native, and the test fails if it never differs. The mutant hook is now per area; frames keeps reload_shift, and --mutant with the leaf area is an error in both translate and check.
  • Mismatch report. A failing caller prints its MASM and its callees' MASM before the reproduction line.
  • Existing tests. The emitted cpu_fuzz.asm/.def, cpu_fuzz_lifted.cpp, cpu_fuzz_frames.asm/.def, cpu_fuzz_frames_lifted.cpp and cpu_fuzz_frames_mutant.cpp are byte-identical to main in dev and release, and their output lines are unchanged.

Measurements

Commit e916584 on a7710b7. 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_(smoke|frames|calls)$' -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
area=calls seed=0xca11 functions=25 inputs=313 comparisons=15650 default and /O2 matched
controls refused: fuzz_clobber_read_r10 (undefined register r10), fuzz_clobber_read_rcx (undefined register rcx)
mutant call_retarget: 313/7825 mismatches in 1/25 functions
Preset Runs cpu_aot_fuzz_calls
dev 3 0.48–0.55 s
release 3 0.49–0.52 s

Default seed contents: 16 callers and 9 callees (fuzz_clobber, 4 leaf, 4 frame); 35 calls and 6 tail jumps; 19 reach a frame callee, 14 of them one that saves a register the caller keeps a value in; 6 calls repeat the previous callee; 17 arguments are derived from RAX; 4 callers keep a spill across their calls; every callee is reached by at least two callers. For input (0x1234, 0x5678) the two native fuzz_clobber segments differ, as intended.

The mutant changes one call site in the generated C++ (fuzz_call_0's call to fuzz_clobber goes to fuzz_1) and changes all 313 results of that caller.

Failure paths, run by hand against the dev build:

  • Mutant passed as the translation. Exit 1 at fuzz_call_0 input 0, with the native and translated RAX, the first differing output byte, the MASM of the caller and its callees, and the reproduce: line. That command repeats the case.
  • Clean translation passed as --mutant. Exit 1, the planted translator fault was not detected.
  • --mutant with the leaf area. Exit 1, no planted translator fault for --area leaf, from both translate and check.
  • A control expecting a different refusal. Exit 1, fuzz_clobber_read_r10 refused for another reason: undefined register r10 at RVA ....
  • An accepted function in place of a control. Exit 1, ... was translated; expected refusal: undefined register rcx.

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

Seed Exports Comparisons Time Refusals Mismatches Calls Tail jumps Mutant mismatches
0x1 193 120,818 2.48 s 0 0 263 63 313/60,409
0x2 193 120,818 2.58 s 0 0 251 55 313/60,409
0xBEEF 193 120,818 2.52 s 0 0 247 67 313/60,409
0xCCD1 193 120,818 2.49 s 0 0 253 60 313/60,409

Both controls were refused as expected at every seed.

Wider checks:

  • ctest --preset dev --no-tests=error -R '^cpu_': 212/212 pass, with 2 capability skips (cpu_aot_fences_hints_clwb, cpu_aot_crypto_sha).
  • tests/docs_links_check.py (15 files, 252 links), tests/bidi_controls_check.py and git diff --check pass.

Not run: MSVC 19.52, Linux, more than 128 callers.

Scope

Callees come only from the leaf and frame generators plus fuzz_clobber, so the call graph is one level deep. Recursion, indirect calls, import calls, mid-body RSP changes and fault equivalence are not generated. Deeper chains and conditional calls, as in #1129, could follow on top.

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

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6573005309 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1164). Originally posted by [roansystemlabs](https://github.com/roansystemlabs) on 2026-11-24T02:09:47Z. --- Refs #446 Adds the direct-call generator claimed in #446 after #927: framed callers that make direct calls and tail jumps to callees from the existing leaf and fixed-frame generators, shared callees reached with different values, a case that observes the expected volatile-register clobbers, and a planted wrong-callee fault. No recursion and no call chains deeper than one level. Coordination: as agreed in #446 (owner's note and https://github.com/R0Y00/Xbox2Windows/issues/446#issuecomment-7119501743), this is the first direct-call PR. @tedin7 opened #1129 for the same generator; a review of the shared-callee and tail-jump cases, or an independent run of the wrong-callee mutation on Linux/LLVM, would be welcome, and deeper call chains could follow on top. ## Change `tools/fuzz_cpu.py --area calls` generates callers and callees 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_calls`; the default is seed `0xCA11` with 16 callers. - **Callees.** Half as many as callers, alternating leaf functions sampled from the leaf generator and functions from the frame generator, plus `fuzz_clobber` (below). They keep their generator names, are exported, and are compared on their own as well. - **Callers.** Three of RBX, RSI, RDI and R12–R15 are saved with `PUSH` and 32 or 48 bytes are allocated, so RSP is 8 mod 16 from entry at every call and the callee's home space lies below the return address, as #425 requires. The prologue carries `.pushreg` and `.allocstack`. - **Calls.** One to three direct `CALL`s. Before each one, RCX, RDX and R8 are defined again from the saved registers, or from RAX after a call. After a call the caller reads only RAX. Every callee may overwrite the volatile registers, and a frame callee also overwrites the nonvolatile registers it saves, often the caller's own. - **Shared callees.** With an even count, each generated callee after the first two is the first call of two callers; the first two are the first call of one caller each, because the first two callers start with `fuzz_clobber`. Later calls and tail jumps reach every callee again, and a later call may repeat the previous callee with its result as an argument. RDX gets a displacement that is unique in the program, so callers that share a callee pass it different values. - **Volatile clobbers.** For any seed, `fuzz_call_0` and `fuzz_call_1` first call `fuzz_clobber` with different values. It overwrites RAX, RCX, RDX and R8–R11 with values derived from its arguments and writes each one to its output segment before the next replaces it, so the clobbers are compared through memory and RAX, the results a caller may observe. - **Refused reads.** `fuzz_clobber_read_r10` and `fuzz_clobber_read_rcx` are in the same DLL but not run: each reads R10 or RCX after calling `fuzz_clobber`. `check` translates each one alone and fails unless it is refused with `undefined register r10` or `undefined register rcx`. - **Frame kept across calls.** Some callers keep a spill above the callee's home space across their calls. - **Tail jumps.** Half the callers end with `JMP callee` after `ADD RSP` and the `POP`s. - **Output.** Each call writes its callee's output into its own 48-byte segment of the buffer, and the caller stores RAX after each call from byte 192. - **Planted error.** `translate --mutant` builds a second translation in which the first call of the first caller goes to the next callee in the generated order. `check --mutant` runs it against native, and the test fails if it never differs. The mutant hook is now per area; `frames` keeps `reload_shift`, and `--mutant` with the leaf area is an error in both `translate` and `check`. - **Mismatch report.** A failing caller prints its MASM and its callees' MASM before the reproduction line. - **Existing tests.** The emitted `cpu_fuzz.asm`/`.def`, `cpu_fuzz_lifted.cpp`, `cpu_fuzz_frames.asm`/`.def`, `cpu_fuzz_frames_lifted.cpp` and `cpu_fuzz_frames_mutant.cpp` are byte-identical to `main` in dev and release, and their output lines are unchanged. ## Measurements Commit `e916584` on `a7710b7`. 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_(smoke|frames|calls)$' -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 area=calls seed=0xca11 functions=25 inputs=313 comparisons=15650 default and /O2 matched controls refused: fuzz_clobber_read_r10 (undefined register r10), fuzz_clobber_read_rcx (undefined register rcx) mutant call_retarget: 313/7825 mismatches in 1/25 functions ``` | Preset | Runs | `cpu_aot_fuzz_calls` | | --- | ---: | ---: | | dev | 3 | 0.48–0.55 s | | release | 3 | 0.49–0.52 s | Default seed contents: 16 callers and 9 callees (`fuzz_clobber`, 4 leaf, 4 frame); 35 calls and 6 tail jumps; 19 reach a frame callee, 14 of them one that saves a register the caller keeps a value in; 6 calls repeat the previous callee; 17 arguments are derived from RAX; 4 callers keep a spill across their calls; every callee is reached by at least two callers. For input `(0x1234, 0x5678)` the two native `fuzz_clobber` segments differ, as intended. The mutant changes one call site in the generated C++ (`fuzz_call_0`'s call to `fuzz_clobber` goes to `fuzz_1`) and changes all 313 results of that caller. Failure paths, run by hand against the dev build: - **Mutant passed as the translation.** Exit 1 at `fuzz_call_0` input 0, with the native and translated RAX, the first differing output byte, the MASM of the caller and its callees, and the `reproduce:` line. That command repeats the case. - **Clean translation passed as `--mutant`.** Exit 1, `the planted translator fault was not detected`. - **`--mutant` with the leaf area.** Exit 1, `no planted translator fault for --area leaf`, from both `translate` and `check`. - **A control expecting a different refusal.** Exit 1, `fuzz_clobber_read_r10 refused for another reason: undefined register r10 at RVA ...`. - **An accepted function in place of a control.** Exit 1, `... was translated; expected refusal: undefined register rcx`. Longer manual run: `cmake --preset dev -DCPU_FUZZ_CALLS_SEED=<seed> -DCPU_FUZZ_CALLS_FUNCTIONS=128`, then the same test. | Seed | Exports | Comparisons | Time | Refusals | Mismatches | Calls | Tail jumps | Mutant mismatches | | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | | `0x1` | 193 | 120,818 | 2.48 s | 0 | 0 | 263 | 63 | 313/60,409 | | `0x2` | 193 | 120,818 | 2.58 s | 0 | 0 | 251 | 55 | 313/60,409 | | `0xBEEF` | 193 | 120,818 | 2.52 s | 0 | 0 | 247 | 67 | 313/60,409 | | `0xCCD1` | 193 | 120,818 | 2.49 s | 0 | 0 | 253 | 60 | 313/60,409 | Both controls were refused as expected at every seed. Wider checks: - `ctest --preset dev --no-tests=error -R '^cpu_'`: 212/212 pass, with 2 capability skips (`cpu_aot_fences_hints_clwb`, `cpu_aot_crypto_sha`). - `tests/docs_links_check.py` (15 files, 252 links), `tests/bidi_controls_check.py` and `git diff --check` pass. Not run: MSVC 19.52, Linux, more than 128 callers. ## Scope Callees come only from the leaf and frame generators plus `fuzz_clobber`, so the call graph is one level deep. Recursion, indirect calls, import calls, mid-body RSP changes and fault equivalence are not generated. Deeper chains and conditional calls, as in #1129, could follow on top. `docs/experiments.md` gains a `cpu_aot_fuzz_calls` paragraph in the fuzz section. AI-assisted.
upstream-sync closed this pull request 2026-11-24 06:46:37 +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!1157
No description provided.