wave_diff: validate cross-lane/mask operands, EXEC=0 dsts, DS offsets, and immutability (WAVE-DIFF-REF-VALIDATION) #1809

Merged
leonida/mandy
ShotTechOp merged 1 commit from fix/wave-diff-ref-validation into main 2026-12-01 15:16:45 +00:00

Pull request successfully merged and closed

ShotTechOp commented 2026-11-25 12:21:57 +00:00

Summary

Claimed in #1608 as WAVE-DIFF-REF-VALIDATION: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162135689

Hardens tools/wave_diff.py (WaveRef) so malformed or unsupported cross-lane and whole-mask operands raise cf_ref.Unsupported instead of silently reading the low register, flipping sign bits, skipping destination validation under EXEC == 0, raising unhandled ValueError, or mutating the caller's parsed instruction dicts:

  1. Reject multi-dword VGPRs and abs/neg modifiers:
    • _vsrc() requires a single VGPR (n == 1) with no abs/neg modifiers across v_readlane_b32, v_readfirstlane_b32, ds_bpermute_b32, and v_permlane16/x16_b32.
    • _scalar_int_src(), _lane_sel(), _count_src(), and s_not_b32/b64 reject abs/neg modifiers and unexpected instruction modifiers.
  2. Validate VGPR destinations even when EXEC == 0 or referenced source lanes are off:
    • Calls _check_vgpr_dst() upfront in v_writelane_b32, ds_bpermute_b32, and v_permlane16/x16_b32 so invalid destinations (s6, v[2:3]) cannot slip through when no lanes write.
  3. Validate ds_bpermute_b32 offset: modifiers:
    • Rejects duplicate offset: modifiers, non-integer values (raises cf_ref.Unsupported instead of ValueError), and values outside the 16-bit unsigned DS offset range (0..65535).
  4. Do not mutate caller's info["insns"] in WaveRef.__init__:
    • Copies unencoded instruction dicts when assigning placeholder enc/pc instead of mutating caller-owned dicts in place.

Independent of #1780 (tools/cf_ref.py); zero PR dependencies.

Testing

Environment: Windows 11 x64, Python 3.12.10, CPU-only (no GPU or game files; based on 0d80bc5).

  • Negative control on unpatched main (0d80bc5):
    • python -m unittest tests.test_wave_diff.Reference -> FAILED (failures=24, errors=1)
  • After fix:
    • python -I tests/test_wave_diff.py -> 16/16 OK (including canaries and seeded wave32/wave64 fuzz runs)
    • python -m compileall -q tools/wave_diff.py tests/test_wave_diff.py -> exit 0
    • python -I scripts/check_no_game_data.py tools/wave_diff.py tests/test_wave_diff.py -> exit 0
    • git diff --check -> clean

AI-assisted: yes (Antigravity). Synthetic inputs only; no game data.

## Summary Claimed in #1608 as `WAVE-DIFF-REF-VALIDATION`: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162135689 Hardens `tools/wave_diff.py` (`WaveRef`) so malformed or unsupported cross-lane and whole-mask operands raise `cf_ref.Unsupported` instead of silently reading the low register, flipping sign bits, skipping destination validation under `EXEC == 0`, raising unhandled `ValueError`, or mutating the caller's parsed instruction dicts: 1. **Reject multi-dword VGPRs and `abs`/`neg` modifiers**: - `_vsrc()` requires a single VGPR (`n == 1`) with no `abs`/`neg` modifiers across `v_readlane_b32`, `v_readfirstlane_b32`, `ds_bpermute_b32`, and `v_permlane16/x16_b32`. - `_scalar_int_src()`, `_lane_sel()`, `_count_src()`, and `s_not_b32/b64` reject `abs`/`neg` modifiers and unexpected instruction modifiers. 2. **Validate VGPR destinations even when `EXEC == 0` or referenced source lanes are off**: - Calls `_check_vgpr_dst()` upfront in `v_writelane_b32`, `ds_bpermute_b32`, and `v_permlane16/x16_b32` so invalid destinations (`s6`, `v[2:3]`) cannot slip through when no lanes write. 3. **Validate `ds_bpermute_b32` `offset:` modifiers**: - Rejects duplicate `offset:` modifiers, non-integer values (raises `cf_ref.Unsupported` instead of `ValueError`), and values outside the 16-bit unsigned DS offset range (`0..65535`). 4. **Do not mutate caller's `info["insns"]` in `WaveRef.__init__`**: - Copies unencoded instruction dicts when assigning placeholder `enc`/`pc` instead of mutating caller-owned dicts in place. Independent of `#1780` (`tools/cf_ref.py`); zero PR dependencies. ## Testing Environment: Windows 11 x64, Python 3.12.10, CPU-only (no GPU or game files; based on `0d80bc5`). - Negative control on unpatched `main` (`0d80bc5`): - `python -m unittest tests.test_wave_diff.Reference` -> `FAILED (failures=24, errors=1)` - After fix: - `python -I tests/test_wave_diff.py` -> 16/16 OK (including canaries and seeded wave32/wave64 fuzz runs) - `python -m compileall -q tools/wave_diff.py tests/test_wave_diff.py` -> exit 0 - `python -I scripts/check_no_game_data.py tools/wave_diff.py tests/test_wave_diff.py` -> exit 0 - `git diff --check` -> clean AI-assisted: yes (Antigravity). Synthetic inputs only; no game data.
GeorgiyDemo commented 2026-11-25 12:49:59 +00:00

Tested 30e5b8b93 on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against main 98c2f7777 (Python CPU reference; no GPU):

  • pytest 9.2.0 on tests/test_wave_diff.py: 16 passed, 25 subtests passed at this head.
  • The same file on main: 25 failed, 15 passed — the cross-lane/mask operand, EXEC=0 destination, DS-offset and immutability validation is not there.
  • Not run: hardware — wave_diff is the reference side.

AI-assisted; synthetic fixtures only, no game data.

Tested `30e5b8b93` on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against `main` `98c2f7777` (Python CPU reference; no GPU): - pytest 9.2.0 on `tests/test_wave_diff.py`: **16 passed, 25 subtests passed** at this head. - The same file on `main`: **25 failed**, 15 passed — the cross-lane/mask operand, EXEC=0 destination, DS-offset and immutability validation is not there. - Not run: hardware — `wave_diff` is the reference side. AI-assisted; synthetic fixtures only, no game data.
DeadlyChirp commented 2026-11-25 15:52:48 +00:00

Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main.

python -m pytest -q tests/test_wave_diff.py:

  • PR: 16 passed. main: 12 passed. The new tests pass on Windows.

(AI-assisted test run; output checked by hand.)

Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main. `python -m pytest -q tests/test_wave_diff.py`: - PR: 16 passed. main: 12 passed. The new tests pass on Windows. (AI-assisted test run; output checked by hand.)
TheGeogeo commented 2026-11-28 23:38:23 +00:00

Tested 30e5b8b93 merged (no commit) with main aa918e5d9: Python 3.12.4, Windows 11 10.0.26300, CPU only. check_no_game_data.py is clean.

Blocker: it breaks once merged with current main. On its own head, tests/test_wave_diff.py passes (16 tests). Merged with main, it gives 35 errors and 1 failure: TypeError: WaveRef._check_vgpr_dst() missing 1 required positional argument: 'dst'.

Cause: since this branch, main's base class tools/cpu_ref.py defines its own _check_vgpr_dst(self, dst) and calls it with one argument (cpu_ref.py:373, :387). This PR's WaveRef._check_vgpr_dst(self, op, dst) overrides it with another signature. Git merges cleanly, so nothing flags it. A rebase that renames the helper or reuses cpu_ref's should fix it. #1780 has the same clash in cf_ref.py.

AI-assisted (Claude Code).

Tested `30e5b8b93` merged (no commit) with `main` `aa918e5d9`: Python 3.12.4, Windows 11 10.0.26300, CPU only. `check_no_game_data.py` is clean. **Blocker: it breaks once merged with current `main`.** On its own head, `tests/test_wave_diff.py` passes (16 tests). Merged with `main`, it gives 35 errors and 1 failure: `TypeError: WaveRef._check_vgpr_dst() missing 1 required positional argument: 'dst'`. Cause: since this branch, `main`'s base class `tools/cpu_ref.py` defines its own `_check_vgpr_dst(self, dst)` and calls it with one argument (`cpu_ref.py:373`, `:387`). This PR's `WaveRef._check_vgpr_dst(self, op, dst)` overrides it with another signature. Git merges cleanly, so nothing flags it. A rebase that renames the helper or reuses `cpu_ref`'s should fix it. #1780 has the same clash in `cf_ref.py`. AI-assisted (Claude Code).
zmasi commented 2026-11-29 01:31:21 +00:00

Independent verification on Windows 11 (10.0.26200, x64) of 30e5b8b932 alone and merged onto origin/main (53b77df14f, 0 git text conflicts), tested under both Python 3.12.10 and Python 3.14.5:

  • PR head alone (30e5b8b932):
    • python -I -B tests/test_wave_diff.py: Ran 16 tests, OK (2.23 s).
  • Merged onto origin/main (53b77df14f) — reproduces #7247564981 blocker:
    • python -I -B tests/test_wave_diff.py: Ran 16 tests, FAILED (failures=1, errors=35) on both Python 3.12.10 and Python 3.14.5 (TypeError: WaveRef._check_vgpr_dst() missing 1 required positional argument: 'dst' when base class tools/cpu_ref.py invokes self._check_vgpr_dst(dst)).
    • Verified minimal fix: renaming WaveRef._check_vgpr_dst to _check_wave_vgpr_dst in tools/wave_diff.py makes python -I -B tests/test_wave_diff.py pass 16/16 (2.59 s, OK) when merged with origin/main 53b77df14f.
  • Hygiene: git diff --check and python -I scripts/check_no_game_data.py tools/wave_diff.py tests/test_wave_diff.py exited 0.

AI-assisted.

Independent verification on Windows 11 (`10.0.26200`, x64) of `30e5b8b932` alone and merged onto `origin/main` (`53b77df14f`, `0` git text conflicts), tested under both `Python 3.12.10` and `Python 3.14.5`: - **PR head alone (`30e5b8b932`)**: - `python -I -B tests/test_wave_diff.py`: `Ran 16 tests`, `OK` (`2.23 s`). - **Merged onto `origin/main` (`53b77df14f`) — reproduces `#7247564981` blocker**: - `python -I -B tests/test_wave_diff.py`: `Ran 16 tests`, `FAILED (failures=1, errors=35)` on both `Python 3.12.10` and `Python 3.14.5` (`TypeError: WaveRef._check_vgpr_dst() missing 1 required positional argument: 'dst'` when base class `tools/cpu_ref.py` invokes `self._check_vgpr_dst(dst)`). - **Verified minimal fix**: renaming `WaveRef._check_vgpr_dst` to `_check_wave_vgpr_dst` in `tools/wave_diff.py` makes `python -I -B tests/test_wave_diff.py` pass `16/16` (`2.59 s`, `OK`) when merged with `origin/main` `53b77df14f`. - **Hygiene**: `git diff --check` and `python -I scripts/check_no_game_data.py tools/wave_diff.py tests/test_wave_diff.py` exited `0`. AI-assisted.
OreoFR commented 2026-11-29 10:24:02 +00:00

@ShotTechOp proposed fix for the _check_vgpr_dst clash with main reported above. main's cpu_ref.Machine._check_vgpr_dst(self, dst) is called with one argument from _write_vgpr, so WaveRef._check_vgpr_dst(self, op, dst) overrides it with an incompatible signature. Renaming the PR's helper keeps its checks at its three call sites and leaves the base method alone. The name differs from the one proposed on #1780, so the two PRs do not override each other either:

diff --git a/tools/wave_diff.py b/tools/wave_diff.py
index 448803181..1b2ef7a10 100644
--- a/tools/wave_diff.py
+++ b/tools/wave_diff.py
@@ -93,7 +93,7 @@ class WaveRef(cf_ref.WaveMachine):
         if arg.get("abs") or arg.get("neg"):
             raise cf_ref.Unsupported(op, self.pc, "abs/neg modifiers are not modelled")
 
-    def _check_vgpr_dst(self, op, dst):
+    def _check_wave_vgpr_dst(self, op, dst):
         self._reject_mods(op, dst)
         if dst["k"] != "vgpr" or dst.get("n", 1) != 1:
             raise cf_ref.Unsupported(op, self.pc, "destination must be a single VGPR")
@@ -180,7 +180,7 @@ class WaveRef(cf_ref.WaveMachine):
             self._expect(ins, 3)
             if ins.get("mods"):
                 raise cf_ref.Unsupported(op, self.pc, "modifier is not modelled")
-            self._check_vgpr_dst(op, a[0])
+            self._check_wave_vgpr_dst(op, a[0])
             self._write_vgpr(a[0], self._lane_sel(op, a[2]), self._scalar_int_src(op, a[1]))
             return None
         if base == "ds_bpermute_b32":
@@ -198,7 +198,7 @@ class WaveRef(cf_ref.WaveMachine):
                     raise cf_ref.Unsupported(op, self.pc, f"invalid offset modifier {mods[0]!r}") from e
                 if not 0 <= off <= 0xFFFF:
                     raise cf_ref.Unsupported(op, self.pc, f"offset {off} outside 16-bit DS range")
-            self._check_vgpr_dst(op, a[0])
+            self._check_wave_vgpr_dst(op, a[0])
             addr, data = self._vsrc(ins, a[1]), self._vsrc(ins, a[2])
             out = {}
             for lane in self._active_lanes():
@@ -214,7 +214,7 @@ class WaveRef(cf_ref.WaveMachine):
             self._expect(ins, 4)
             if ins.get("mods"):
                 raise cf_ref.Unsupported(op, self.pc, "op_sel (FI/BOUND_CTRL) is not modelled")
-            self._check_vgpr_dst(op, a[0])
+            self._check_wave_vgpr_dst(op, a[0])
             s1, s2 = self._scalar_int_src(op, a[2]), self._scalar_int_src(op, a[3])
             data = self._vsrc(ins, a[1])
             out = {}

Checked on Windows 11, Python 3.12, CPU only, on main 54b2a81e9 + this PR (30e5b8b93, merged locally, not pushed):

  • without the fix: tests/test_wave_diff.py FAILED (failures=1, errors=35)
  • with the fix: test_wave_diff.py 16 OK, test_cf_ref.py 26 OK, test_cf_gpu_run.py 17 OK, test_cf_ref_edge_fixes.py 5 OK, test_cpu_ref.py 39 OK, test_wave_trace.py 10 OK, test_cf_diff.py (pytest) 42 passed, semantic/test_loop_ballot_barrier.py 15 OK, semantic/test_saveexec_variants.py 9 OK
  • full run_semantic_tests.py tests/semantic: 529 files, 6525 tests, the same 2 failing files as plain main (test_interp_computed_ij_fallback, test_valu_cvt_f16_int16_mods)
  • main + this PR + #1780 with both renames: the same files pass too.

— OreoFR's agent (AI-assisted)

@ShotTechOp proposed fix for the `_check_vgpr_dst` clash with `main` reported above. `main`'s `cpu_ref.Machine._check_vgpr_dst(self, dst)` is called with one argument from `_write_vgpr`, so `WaveRef._check_vgpr_dst(self, op, dst)` overrides it with an incompatible signature. Renaming the PR's helper keeps its checks at its three call sites and leaves the base method alone. The name differs from the one proposed on #1780, so the two PRs do not override each other either: ```diff diff --git a/tools/wave_diff.py b/tools/wave_diff.py index 448803181..1b2ef7a10 100644 --- a/tools/wave_diff.py +++ b/tools/wave_diff.py @@ -93,7 +93,7 @@ class WaveRef(cf_ref.WaveMachine): if arg.get("abs") or arg.get("neg"): raise cf_ref.Unsupported(op, self.pc, "abs/neg modifiers are not modelled") - def _check_vgpr_dst(self, op, dst): + def _check_wave_vgpr_dst(self, op, dst): self._reject_mods(op, dst) if dst["k"] != "vgpr" or dst.get("n", 1) != 1: raise cf_ref.Unsupported(op, self.pc, "destination must be a single VGPR") @@ -180,7 +180,7 @@ class WaveRef(cf_ref.WaveMachine): self._expect(ins, 3) if ins.get("mods"): raise cf_ref.Unsupported(op, self.pc, "modifier is not modelled") - self._check_vgpr_dst(op, a[0]) + self._check_wave_vgpr_dst(op, a[0]) self._write_vgpr(a[0], self._lane_sel(op, a[2]), self._scalar_int_src(op, a[1])) return None if base == "ds_bpermute_b32": @@ -198,7 +198,7 @@ class WaveRef(cf_ref.WaveMachine): raise cf_ref.Unsupported(op, self.pc, f"invalid offset modifier {mods[0]!r}") from e if not 0 <= off <= 0xFFFF: raise cf_ref.Unsupported(op, self.pc, f"offset {off} outside 16-bit DS range") - self._check_vgpr_dst(op, a[0]) + self._check_wave_vgpr_dst(op, a[0]) addr, data = self._vsrc(ins, a[1]), self._vsrc(ins, a[2]) out = {} for lane in self._active_lanes(): @@ -214,7 +214,7 @@ class WaveRef(cf_ref.WaveMachine): self._expect(ins, 4) if ins.get("mods"): raise cf_ref.Unsupported(op, self.pc, "op_sel (FI/BOUND_CTRL) is not modelled") - self._check_vgpr_dst(op, a[0]) + self._check_wave_vgpr_dst(op, a[0]) s1, s2 = self._scalar_int_src(op, a[2]), self._scalar_int_src(op, a[3]) data = self._vsrc(ins, a[1]) out = {} ``` Checked on Windows 11, Python 3.12, CPU only, on `main` `54b2a81e9` + this PR (`30e5b8b93`, merged locally, not pushed): - without the fix: `tests/test_wave_diff.py` FAILED (failures=1, errors=35) - with the fix: `test_wave_diff.py` 16 OK, `test_cf_ref.py` 26 OK, `test_cf_gpu_run.py` 17 OK, `test_cf_ref_edge_fixes.py` 5 OK, `test_cpu_ref.py` 39 OK, `test_wave_trace.py` 10 OK, `test_cf_diff.py` (pytest) 42 passed, `semantic/test_loop_ballot_barrier.py` 15 OK, `semantic/test_saveexec_variants.py` 9 OK - full `run_semantic_tests.py tests/semantic`: 529 files, 6525 tests, the same 2 failing files as plain `main` (`test_interp_computed_ij_fallback`, `test_valu_cvt_f16_int16_mods`) - `main` + this PR + #1780 with both renames: the same files pass too. — OreoFR's agent (AI-assisted)
aslan2142 approved these changes 2026-12-01 11:43:04 +00:00
aslan2142 left a comment

Reviewed the diff against main: tools/wave_diff.py:72-218 cleanly stops mutating caller info["insns"], rejects abs/neg and multi-dword VGPR operands on cross-lane/mask ops, validates destinations even when EXEC=0, and range-checks 16-bit DS offset: modifiers, with tests in tests/test_wave_diff.py. Clean-room verified. Approving; please merge/rebase current main so CI runs green before merge.

(AI-assisted review.)

Reviewed the diff against `main`: `tools/wave_diff.py:72-218` cleanly stops mutating caller `info["insns"]`, rejects `abs`/`neg` and multi-dword VGPR operands on cross-lane/mask ops, validates destinations even when `EXEC=0`, and range-checks 16-bit DS `offset:` modifiers, with tests in `tests/test_wave_diff.py`. Clean-room verified. Approving; please merge/rebase current `main` so CI runs green before merge. *(AI-assisted review.)*
Sign in to join this conversation.
No description provided.