tooling: add a jump-table switch area to the CPU differential fuzz #1322

Merged
tedin7 merged 1 commit from upstream-pr/1329 into main 2026-11-24 08:33:20 +00:00
tedin7 commented 2026-11-24 08:16:25 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T08:16:25Z.


What

Adds --area switches to tools/fuzz_cpu.py and the CTest cpu_aot_fuzz_switches (same structure as cpu_aot_fuzz_frames/calls/chains, own seed CPU_FUZZ_SWITCHES_SEED=0x5717 and CPU_FUZZ_SWITCHES_FUNCTIONS=48).

  • 48 exports, fn(selector, y, out), each a jump-table switch in a shape cpu_switch_aot.py documents and tests/cpu/switch*.asm already exercise: MSVC image-relative and LLVM relative tables, JA/JAE and inverted JBE/JB bound branches, tables of 2 to 24 entries, index from ECX, RCX, an AND mask, MOVZX CL/CX or a signed clamp (TEST + CMOVS), shared and distinct arms, tables in and out of order, fall-through arms, arms with their own RET, default before or after the check, 32- and 64-bit arms. Every arm writes its own marker word. All 48 layout x bound x index-source combinations appear once.
  • Four refused controls through the existing refused mechanism: no bound check, signed JG bound, index not zero-extended after a 32-bit compare, table base not RIP-relative.
  • Planted translator fault table_rotate (each table entry read one slot late); the test fails unless it differs from native.
  • inputs_for gets an optional area argument: for switches the first inputs are selectors 0 to 39 plus selectors with upper bits set. Other areas get byte-identical inputs and emitted MASM.
  • emit declares EXTERN __ImageBase:BYTE when a function uses it.
  • docs/experiments.md: new paragraph after the chains one.

Why

The switch lowering is only covered by hand-written fixtures; a generated set varies case counts, bound forms, index sources and arm layouts against native execution.

Tested

Windows (not run locally):

cmake --build --preset dev --target cpu_fuzz_switches cpu_fuzz_switches_lifted cpu_fuzz_switches_mutant cpu_fuzz_switches_lifted_o2
ctest --preset dev --no-tests=error -R '^cpu_aot_fuzz_switches$' -V

Ran on Linux: py_compile, tests/docs_links_check.py, tests/bidi_controls_check.py, git diff --check, x2w_pipe.py preflight (OK). Generator output deterministic per seed and different across seeds. emit output of the four existing areas unchanged versus origin/main.

Also on Linux, as a stand-in for MSVC/ML64: the generated MASM was rewritten (IMAGEREL to @IMGREL, LABEL DWORD to a plain label, LEA of a label to [rip+label]), assembled with llvm-ml-19, linked with lld-link-19, translated with the real cpu_switch_aot, built with MinGW at -O0 and -O2 and compared against native under Wine with a small loader. Default seed: 48 exports x 313 inputs = 15,024 comparisons per build, no mismatch. Seeds 0x1, 0x2, 0xBEEF at 96 exports: 30,048 each, no refusal, no mismatch. The four controls refused with the expected messages. The planted fault differed in 4,028 of 15,024 calls, in all 48 exports.

NOT run locally: MSVC, ML64, the Windows ctest, and the Python check command itself (it loads PE DLLs with ctypes, Windows only). The native-vs-lifted comparison above used the loader, not run_check.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6577538397 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1329). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T08:16:25Z. --- ## What Adds `--area switches` to `tools/fuzz_cpu.py` and the CTest `cpu_aot_fuzz_switches` (same structure as `cpu_aot_fuzz_frames/calls/chains`, own seed `CPU_FUZZ_SWITCHES_SEED`=0x5717 and `CPU_FUZZ_SWITCHES_FUNCTIONS`=48). - 48 exports, fn(selector, y, out), each a jump-table switch in a shape `cpu_switch_aot.py` documents and `tests/cpu/switch*.asm` already exercise: MSVC image-relative and LLVM relative tables, `JA`/`JAE` and inverted `JBE`/`JB` bound branches, tables of 2 to 24 entries, index from ECX, RCX, an `AND` mask, `MOVZX CL/CX` or a signed clamp (`TEST` + `CMOVS`), shared and distinct arms, tables in and out of order, fall-through arms, arms with their own `RET`, default before or after the check, 32- and 64-bit arms. Every arm writes its own marker word. All 48 layout x bound x index-source combinations appear once. - Four refused controls through the existing `refused` mechanism: no bound check, signed `JG` bound, index not zero-extended after a 32-bit compare, table base not RIP-relative. - Planted translator fault `table_rotate` (each table entry read one slot late); the test fails unless it differs from native. - `inputs_for` gets an optional area argument: for `switches` the first inputs are selectors 0 to 39 plus selectors with upper bits set. Other areas get byte-identical inputs and emitted MASM. - `emit` declares `EXTERN __ImageBase:BYTE` when a function uses it. - `docs/experiments.md`: new paragraph after the chains one. ## Why The switch lowering is only covered by hand-written fixtures; a generated set varies case counts, bound forms, index sources and arm layouts against native execution. ## Tested Windows (not run locally): ``` cmake --build --preset dev --target cpu_fuzz_switches cpu_fuzz_switches_lifted cpu_fuzz_switches_mutant cpu_fuzz_switches_lifted_o2 ctest --preset dev --no-tests=error -R '^cpu_aot_fuzz_switches$' -V ``` Ran on Linux: `py_compile`, `tests/docs_links_check.py`, `tests/bidi_controls_check.py`, `git diff --check`, `x2w_pipe.py preflight` (OK). Generator output deterministic per seed and different across seeds. `emit` output of the four existing areas unchanged versus origin/main. Also on Linux, as a stand-in for MSVC/ML64: the generated MASM was rewritten (`IMAGEREL` to `@IMGREL`, `LABEL DWORD` to a plain label, `LEA` of a label to `[rip+label]`), assembled with `llvm-ml-19`, linked with `lld-link-19`, translated with the real `cpu_switch_aot`, built with MinGW at -O0 and -O2 and compared against native under Wine with a small loader. Default seed: 48 exports x 313 inputs = 15,024 comparisons per build, no mismatch. Seeds 0x1, 0x2, 0xBEEF at 96 exports: 30,048 each, no refusal, no mismatch. The four controls refused with the expected messages. The planted fault differed in 4,028 of 15,024 calls, in all 48 exports. NOT run locally: MSVC, ML64, the Windows `ctest`, and the Python `check` command itself (it loads PE DLLs with ctypes, Windows only). The native-vs-lifted comparison above used the loader, not `run_check`.
upstream-sync closed this pull request 2026-11-24 09:04:33 +00:00
tedin7 commented 2026-11-24 09:04:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T08:19:48Z.


Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207) at commit 632b893940b6c92eb9adf527d7824e9428dcb665:

  • Debug (--preset dev):
    • Build (all 19 cpu_fuzz* targets): 4.92s (configure 1.54s)
    • Test (ctest --preset dev -R '^(cpu_aot_fuzz_calls|cpu_aot_fuzz_chains|cpu_aot_fuzz_frames|cpu_aot_fuzz_smoke|cpu_aot_fuzz_switches)$' --parallel 4 --no-tests=error --output-on-failure): 5/5 passed (1.35s: cpu_aot_fuzz_frames 0.38s, cpu_aot_fuzz_smoke 0.41s, cpu_aot_fuzz_chains 0.39s, cpu_aot_fuzz_calls 0.70s, cpu_aot_fuzz_switches 0.89s)
  • Release /O2 (--preset release):
    • Build (all 19 cpu_fuzz* targets): 2.42s (configure 1.69s)
    • Test (ctest --preset release -R '^(cpu_aot_fuzz_calls|cpu_aot_fuzz_chains|cpu_aot_fuzz_frames|cpu_aot_fuzz_smoke|cpu_aot_fuzz_switches)$' --parallel 4 --no-tests=error --output-on-failure): 5/5 passed (1.35s: cpu_aot_fuzz_frames 0.38s, cpu_aot_fuzz_chains 0.38s, cpu_aot_fuzz_smoke 0.41s, cpu_aot_fuzz_calls 0.67s, cpu_aot_fuzz_switches 0.89s)
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7126001922 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1329#issuecomment-7126001922). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T08:19:48Z. --- Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207) at commit `632b893940b6c92eb9adf527d7824e9428dcb665`: - **Debug (`--preset dev`)**: - Build (all 19 `cpu_fuzz*` targets): `4.92s` (configure `1.54s`) - Test (`ctest --preset dev -R '^(cpu_aot_fuzz_calls|cpu_aot_fuzz_chains|cpu_aot_fuzz_frames|cpu_aot_fuzz_smoke|cpu_aot_fuzz_switches)$' --parallel 4 --no-tests=error --output-on-failure`): **5/5 passed** (`1.35s`: `cpu_aot_fuzz_frames` `0.38s`, `cpu_aot_fuzz_smoke` `0.41s`, `cpu_aot_fuzz_chains` `0.39s`, `cpu_aot_fuzz_calls` `0.70s`, `cpu_aot_fuzz_switches` `0.89s`) - **Release `/O2` (`--preset release`)**: - Build (all 19 `cpu_fuzz*` targets): `2.42s` (configure `1.69s`) - Test (`ctest --preset release -R '^(cpu_aot_fuzz_calls|cpu_aot_fuzz_chains|cpu_aot_fuzz_frames|cpu_aot_fuzz_smoke|cpu_aot_fuzz_switches)$' --parallel 4 --no-tests=error --output-on-failure`): **5/5 passed** (`1.35s`: `cpu_aot_fuzz_frames` `0.38s`, `cpu_aot_fuzz_chains` `0.38s`, `cpu_aot_fuzz_smoke` `0.41s`, `cpu_aot_fuzz_calls` `0.67s`, `cpu_aot_fuzz_switches` `0.89s`)
chatgpt-codex-connector[bot commented 2026-11-24 09:04:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T08:20:01Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T08:25:08.953741Z 632b893 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7126006230 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1329#issuecomment-7126006230). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T08:20:01Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"632b893940b6c92eb9adf527d7824e9428dcb665","has_any_findings":false,"has_unresolved_findings":false,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-24T08:25:08.953741Z">2026-11-24T08:25:08.953741Z</relative-time> | `632b893` | Draft marked ready | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
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!1322
No description provided.