tooling: add mid-body RSP generators to the CPU fuzz test #1402

Merged
RenzoWit35 merged 1 commit from upstream-pr/1409 into main 2026-11-24 11:26:19 +00:00
RenzoWit35 commented 2026-11-24 10:52:30 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by RenzoWit35 on 2026-11-24T10:52:30Z.


Change

Adds --area rsp to tools/fuzz_cpu.py and the CTest cpu_aot_fuzz_rsp, with the same structure as cpu_aot_fuzz_switches and cpu_aot_fuzz_scalar_fp: its own seed (CPU_FUZZ_RSP_SEED=0xB0D1, CPU_FUZZ_RSP_FUNCTIONS=36), one MASM DLL, one translation built at the preset's optimization and at /O2, a mutant translation, and TIMEOUT 60. Refs #446 (the "mid-body RSP changes" generator from the issue's list; it stays open for x87, pe_map header mutation and fault equivalence).

  • Each export is fn(x, y, out) for cpu_switch_aot. The prologue pushes one to three of RBX, RSI, RDI and R12 to R15 and allocates 16 to 48 bytes, with unwind directives. The body then moves RSP: SUB/ADD RSP by 8 to 48, PUSH/POP of saved registers that hold data, and 1-, 2-, 4- and 8-byte stores, reloads and in-place updates anywhere in the allocation, the caller's home space and what the body allocated. Reloads prefer bytes stored at another RSP, so the same bytes are reached through different displacements. Every reloaded value goes to the output buffer and RAX, and the saved registers' final values are written to the buffer.
  • Six shapes, cycled: adjustments only; mostly PUSH/POP; a TEST+Jcc whose two arms move RSP by the same amount in different ways, so the join sees one depth; a mid-body SUB RSP that aligns one or two direct calls to frameless callees that no export names; and two RBP-anchored shapes (PUSH RBP, MOV RBP, RSP, .setframe) that mix [RBP+d] and [RSP+d] and end with LEAVE or with ADD RSP + POP RBP.
  • The generator tracks which stack bytes are defined: bytes released by ADD RSP or POP, the callee's home space after a call, and bytes only one branch arm wrote are not read again.
  • Four refused controls through the existing refused mechanism: reload of released bytes (undefined stack byte), join at two depths (stack depth differs at join), read below RSP (stack access below RSP), SUB RSP, 4 (unsupported stack adjustment).
  • Planted translator fault stale_rsp: every [RSP+d] operand is resolved against the RSP after the prologue's allocation (the first SUB RSP), as if no later SUB, ADD, PUSH or POP had moved RSP. PUSH, POP, [RBP+d] operands and the frame proof are unchanged, and an operand whose moved bytes would leave the frame array keeps its index, so the mutant never indexes outside the array. The test fails unless it differs from native.
  • docs/experiments.md: one paragraph after the scalar_fp one.

Only AREAS and MUTANTS gain an entry; no existing generator, inputs_for, run_check or translate code changes, and the new CMake block sits after cpu_aot_fuzz_scalar_fp. No open PR touches tools/fuzz_cpu.py or the fuzz CMake section.

Verification

Commit dd73bde1 on origin/main 07211552. Windows 11 Enterprise build 26300.9974, Visual Studio Build Tools (MSVC 19.51.36248, ML64 14.51.36248), Windows SDK 26100, Python 3.14.5.

. .\tools\dev.ps1
cmake --preset dev
cmake --build --preset dev --target cpu_fuzz_rsp cpu_fuzz_rsp_lifted cpu_fuzz_rsp_mutant cpu_fuzz_rsp_lifted_o2
ctest --preset dev --no-tests=error -R '^cpu_aot_fuzz_rsp$' -V
area=rsp seed=0xb0d1 functions=36 inputs=313 comparisons=22536 default and /O2 matched
controls refused: fuzz_rsp_refused_released (undefined stack byte), fuzz_rsp_refused_join (stack depth differs at join), fuzz_rsp_refused_below (stack access below RSP), fuzz_rsp_refused_unaligned (unsupported stack adjustment)
mutant stale_rsp: 9045/11268 mismatches in 31/36 functions
  • ctest --preset dev --no-tests=error -R '^cpu_aot_fuzz_': 7/7 passed (smoke, frames, calls, chains, switches, scalar_fp, rsp), 0 failed, 0 skipped.
  • Same with --preset release after cmake --preset release and building the 27 cpu_fuzz* targets: 7/7 passed, the same three lines as above.
  • Mutant control: passing the clean translation as --mutant fails with mutant stale_rsp: 0/11268 mismatches in 0/36 functions and the planted translator fault was not detected (exit 1). Passing the mutant as the translation fails at the first mismatch (fuzz_rsp_0, input 1, first differing byte at output offset 24) with the MASM and the reproduction command (exit 1). The five exports the mutant does not change are two anchored ones that read through RBP and three calling ones whose moved operands would leave the frame array.
  • Longer runs (cmake --preset dev -DCPU_FUZZ_RSP_SEED=<seed> -DCPU_FUZZ_RSP_FUNCTIONS=120, then the build and ctest above): seeds 0x1, 0x2, 0xBEEF, 0xCCD1 each made 75,120 comparisons with no refusal and no mismatch; the mutant differed in 28,393, 30,027, 29,171 and 31,033 of 37,560 calls (97 to 107 of 120 exports).
  • Existing streams unchanged: python -B tools/fuzz_cpu.py emit <seed> <count> ... --area <area> before and after the change, compared with diff -r, byte-identical for leaf 0xA902/20 and 0xCCD1/96, frames 0xF4A3/16 and 0xBEEF/128, calls 0xCA11/16 and 0x1/128, chains 0xCA11/16 and 0x2/64, switches 0x5717/48 and 0xBEEF/96, scalar_fp 0xF10A/60 and 0x2/120 (24 files).
  • tests/docs_links_check.py (16 files, 317 links) and tests/bidi_controls_check.py pass; git diff --check is clean.

Not run: the Ubuntu job and other MSVC versions.

Evidence and follow-up

The fixture is generated MASM from a fixed seed; no third-party inputs. At the default seed the 36 exports have 110 mid-body SUB RSP, 77 mid-body ADD RSP, 52 PUSH and 13 POP after the prologue, 11 branches, 10 calls, 6 LEAVE epilogues, 362 [RSP+d] and 72 [RBP+d] operands, and 168 reloads; 123 reloads and POPs read bytes stored at another RSP (at least one in every export). Not generated: MOV RSP, RBP, LEA RSP, ENTER (refused as a dynamic frame), an RBP anchor moved after the prologue, stack arguments, calls in an anchored frame. x87, pe_map header mutation and bad-pointer fault equivalence remain open in #446.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6580149514 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1409). Originally posted by [RenzoWit35](https://github.com/RenzoWit35) on 2026-11-24T10:52:30Z. --- ## Change Adds `--area rsp` to `tools/fuzz_cpu.py` and the CTest `cpu_aot_fuzz_rsp`, with the same structure as `cpu_aot_fuzz_switches` and `cpu_aot_fuzz_scalar_fp`: its own seed (`CPU_FUZZ_RSP_SEED`=`0xB0D1`, `CPU_FUZZ_RSP_FUNCTIONS`=36), one MASM DLL, one translation built at the preset's optimization and at `/O2`, a mutant translation, and `TIMEOUT 60`. Refs #446 (the "mid-body RSP changes" generator from the issue's list; it stays open for x87, `pe_map` header mutation and fault equivalence). - Each export is fn(x, y, out) for `cpu_switch_aot`. The prologue pushes one to three of RBX, RSI, RDI and R12 to R15 and allocates 16 to 48 bytes, with unwind directives. The body then moves RSP: `SUB`/`ADD RSP` by 8 to 48, `PUSH`/`POP` of saved registers that hold data, and 1-, 2-, 4- and 8-byte stores, reloads and in-place updates anywhere in the allocation, the caller's home space and what the body allocated. Reloads prefer bytes stored at another RSP, so the same bytes are reached through different displacements. Every reloaded value goes to the output buffer and RAX, and the saved registers' final values are written to the buffer. - Six shapes, cycled: adjustments only; mostly `PUSH`/`POP`; a `TEST`+`Jcc` whose two arms move RSP by the same amount in different ways, so the join sees one depth; a mid-body `SUB RSP` that aligns one or two direct calls to frameless callees that no export names; and two RBP-anchored shapes (`PUSH RBP`, `MOV RBP, RSP`, `.setframe`) that mix `[RBP+d]` and `[RSP+d]` and end with `LEAVE` or with `ADD RSP` + `POP RBP`. - The generator tracks which stack bytes are defined: bytes released by `ADD RSP` or `POP`, the callee's home space after a call, and bytes only one branch arm wrote are not read again. - Four refused controls through the existing `refused` mechanism: reload of released bytes (`undefined stack byte`), join at two depths (`stack depth differs at join`), read below RSP (`stack access below RSP`), `SUB RSP, 4` (`unsupported stack adjustment`). - Planted translator fault `stale_rsp`: every `[RSP+d]` operand is resolved against the RSP after the prologue's allocation (the first `SUB RSP`), as if no later `SUB`, `ADD`, `PUSH` or `POP` had moved RSP. `PUSH`, `POP`, `[RBP+d]` operands and the frame proof are unchanged, and an operand whose moved bytes would leave the frame array keeps its index, so the mutant never indexes outside the array. The test fails unless it differs from native. - `docs/experiments.md`: one paragraph after the scalar_fp one. Only `AREAS` and `MUTANTS` gain an entry; no existing generator, `inputs_for`, `run_check` or `translate` code changes, and the new CMake block sits after `cpu_aot_fuzz_scalar_fp`. No open PR touches `tools/fuzz_cpu.py` or the fuzz CMake section. ## Verification Commit `dd73bde1` on origin/main `07211552`. Windows 11 Enterprise build 26300.9974, Visual Studio Build Tools (MSVC 19.51.36248, ML64 14.51.36248), Windows SDK 26100, Python 3.14.5. ```powershell . .\tools\dev.ps1 cmake --preset dev cmake --build --preset dev --target cpu_fuzz_rsp cpu_fuzz_rsp_lifted cpu_fuzz_rsp_mutant cpu_fuzz_rsp_lifted_o2 ctest --preset dev --no-tests=error -R '^cpu_aot_fuzz_rsp$' -V ``` ``` area=rsp seed=0xb0d1 functions=36 inputs=313 comparisons=22536 default and /O2 matched controls refused: fuzz_rsp_refused_released (undefined stack byte), fuzz_rsp_refused_join (stack depth differs at join), fuzz_rsp_refused_below (stack access below RSP), fuzz_rsp_refused_unaligned (unsupported stack adjustment) mutant stale_rsp: 9045/11268 mismatches in 31/36 functions ``` - `ctest --preset dev --no-tests=error -R '^cpu_aot_fuzz_'`: 7/7 passed (smoke, frames, calls, chains, switches, scalar_fp, rsp), 0 failed, 0 skipped. - Same with `--preset release` after `cmake --preset release` and building the 27 `cpu_fuzz*` targets: 7/7 passed, the same three lines as above. - Mutant control: passing the clean translation as `--mutant` fails with `mutant stale_rsp: 0/11268 mismatches in 0/36 functions` and `the planted translator fault was not detected` (exit 1). Passing the mutant as the translation fails at the first mismatch (`fuzz_rsp_0`, input 1, first differing byte at output offset 24) with the MASM and the reproduction command (exit 1). The five exports the mutant does not change are two anchored ones that read through RBP and three calling ones whose moved operands would leave the frame array. - Longer runs (`cmake --preset dev -DCPU_FUZZ_RSP_SEED=<seed> -DCPU_FUZZ_RSP_FUNCTIONS=120`, then the build and ctest above): seeds `0x1`, `0x2`, `0xBEEF`, `0xCCD1` each made 75,120 comparisons with no refusal and no mismatch; the mutant differed in 28,393, 30,027, 29,171 and 31,033 of 37,560 calls (97 to 107 of 120 exports). - Existing streams unchanged: `python -B tools/fuzz_cpu.py emit <seed> <count> ... --area <area>` before and after the change, compared with `diff -r`, byte-identical for leaf `0xA902`/20 and `0xCCD1`/96, frames `0xF4A3`/16 and `0xBEEF`/128, calls `0xCA11`/16 and `0x1`/128, chains `0xCA11`/16 and `0x2`/64, switches `0x5717`/48 and `0xBEEF`/96, scalar_fp `0xF10A`/60 and `0x2`/120 (24 files). - `tests/docs_links_check.py` (16 files, 317 links) and `tests/bidi_controls_check.py` pass; `git diff --check` is clean. Not run: the Ubuntu job and other MSVC versions. ## Evidence and follow-up The fixture is generated MASM from a fixed seed; no third-party inputs. At the default seed the 36 exports have 110 mid-body `SUB RSP`, 77 mid-body `ADD RSP`, 52 `PUSH` and 13 `POP` after the prologue, 11 branches, 10 calls, 6 `LEAVE` epilogues, 362 `[RSP+d]` and 72 `[RBP+d]` operands, and 168 reloads; 123 reloads and `POP`s read bytes stored at another RSP (at least one in every export). Not generated: `MOV RSP, RBP`, `LEA RSP`, `ENTER` (refused as a dynamic frame), an RBP anchor moved after the prologue, stack arguments, calls in an anchored frame. x87, `pe_map` header mutation and bad-pointer fault equivalence remain open in #446. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
upstream-sync closed this pull request 2026-11-24 12:02:46 +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!1402
No description provided.