salu_diff: validate branch skips, register operands, arity, SIMM16, and comma sizing (SALU-DIFF-REF-VALIDATION) #1911

Merged
leonida/mandy
ShotTechOp merged 1 commit from fix/salu-diff-ref-validation into main 2026-11-28 11:44:27 +00:00

Pull request successfully merged and closed

ShotTechOp commented 2026-11-25 15:36:17 +00:00

Claimed in #1608 as SALU-DIFF-REF-VALIDATION (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7167019096).

What and why

tools/salu_diff.py's scalar reference interpreter and instruction sizer silently accepted malformed operands and out-of-bounds branch skips (or raised raw ValueError/IndexError instead of RefError):

  1. Branch skip parsing & bounds (branch_skip, reference): rest.startswith("+") accepted s_cbranch_scc0 +-1, making skip = -1 decrement indefinitely (-1, -2, ...) and skip every remaining instruction in the program; reference() also silently allowed branches past the end of lines even though listing() rejects them. branch_skip() now requires +<non-negative integer> (+0 preserved for drop() during shrinking) and reference() checks i + 1 + br[1] <= len(lines).
  2. Register kind, pair shape, and operand arity (_parse_src, _write, reference): _write() and v_mov_b32 only stripped the first character (int(tok[1:])), so s_add_u32 v4, 1, 2 silently wrote sgpr[4], v_mov_b32 s8, 5 silently wrote vgpr[8], s_movrels_b32 s4, v5 silently read sgpr[5 + m0], non-consecutive pairs like s[4:6] were accepted, and extra operands were ignored. All register operands, pairs (s[N:N+1]), and per-form operand counts are now validated and raise RefError.
  3. SIMM16 range (cmpk, movk, sopk): out-of-range immediates (outside -0x8000..0xFFFF) were silently truncated to 16 bits instead of raising RefError.
  4. Comma-delimited operand splitting (reference, insn_size, _is_literal): splitting on ", " broke compact comma spacing (s_add_u32 s4,1,2 crashed with IndexError, and insn_size("s_add_u32 s4,s5,0x12345") returned 4 instead of 8, corrupting branch offsets); _is_literal("") crashed with IndexError.

Verification (Windows 11 x64, Python 3.12.10, no GPU / no game data)

Check Base (origin/main b0f3d39) This PR
python -I tests/test_salu_diff.py 29/29 OK 31/31 OK
New regression tests against base tools/salu_diff.py (negative control) FAILED (16 failures, 10 errors) 2/2 OK
python -I tools/salu_diff.py --self-test pass pass
python -m compileall -q tools/salu_diff.py tests/test_salu_diff.py — pass
python -I scripts/check_no_game_data.py tools/salu_diff.py tests/test_salu_diff.py — pass
git diff --check — clean

AI-assisted: yes. Synthetic unit tests only; no game data, no GPU required.

Claimed in #1608 as `SALU-DIFF-REF-VALIDATION` (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7167019096). ### What and why `tools/salu_diff.py`'s scalar reference interpreter and instruction sizer silently accepted malformed operands and out-of-bounds branch skips (or raised raw `ValueError`/`IndexError` instead of `RefError`): 1. **Branch skip parsing & bounds (`branch_skip`, `reference`):** `rest.startswith("+")` accepted `s_cbranch_scc0 +-1`, making `skip = -1` decrement indefinitely (`-1, -2, ...`) and skip every remaining instruction in the program; `reference()` also silently allowed branches past the end of `lines` even though `listing()` rejects them. `branch_skip()` now requires `+<non-negative integer>` (`+0` preserved for `drop()` during shrinking) and `reference()` checks `i + 1 + br[1] <= len(lines)`. 2. **Register kind, pair shape, and operand arity (`_parse_src`, `_write`, `reference`):** `_write()` and `v_mov_b32` only stripped the first character (`int(tok[1:])`), so `s_add_u32 v4, 1, 2` silently wrote `sgpr[4]`, `v_mov_b32 s8, 5` silently wrote `vgpr[8]`, `s_movrels_b32 s4, v5` silently read `sgpr[5 + m0]`, non-consecutive pairs like `s[4:6]` were accepted, and extra operands were ignored. All register operands, pairs (`s[N:N+1]`), and per-form operand counts are now validated and raise `RefError`. 3. **SIMM16 range (`cmpk`, `movk`, `sopk`):** out-of-range immediates (outside `-0x8000..0xFFFF`) were silently truncated to 16 bits instead of raising `RefError`. 4. **Comma-delimited operand splitting (`reference`, `insn_size`, `_is_literal`):** splitting on `", "` broke compact comma spacing (`s_add_u32 s4,1,2` crashed with `IndexError`, and `insn_size("s_add_u32 s4,s5,0x12345")` returned `4` instead of `8`, corrupting branch offsets); `_is_literal("")` crashed with `IndexError`. ### Verification (Windows 11 x64, Python 3.12.10, no GPU / no game data) | Check | Base (`origin/main` `b0f3d39`) | This PR | |---|---|---| | `python -I tests/test_salu_diff.py` | 29/29 OK | **31/31 OK** | | New regression tests against base `tools/salu_diff.py` (negative control) | **FAILED (16 failures, 10 errors)** | **2/2 OK** | | `python -I tools/salu_diff.py --self-test` | pass | **pass** | | `python -m compileall -q tools/salu_diff.py tests/test_salu_diff.py` | — | **pass** | | `python -I scripts/check_no_game_data.py tools/salu_diff.py tests/test_salu_diff.py` | — | **pass** | | `git diff --check` | — | **clean** | AI-assisted: yes. Synthetic unit tests only; no game data, no GPU required.
DeadlyChirp commented 2026-11-25 15:50:02 +00:00

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

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

  • PR: 31 passed. main: 29 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_salu_diff.py`: - PR: 31 passed. main: 29 passed. The new tests pass on Windows. (AI-assisted test run; output checked by hand.)
sodove commented 2026-11-28 11:28:05 +00:00

#1911 | head f51d6070f7f8542486ca8fb1e51db59868e2c1a4 | python3 -I tests/test_salu_diff.py -v | red: 31 run, 16 failures, 10 errors | green: 31 passed | Red → green

The test file exercised reference semantics, branch skips, register operands, arity, SIMM16 and compact comma spacing.

Copied the PR tests onto clean main 4a54aedcb6b66525e0c9f72a6ea10263337c9141 for the baseline, then merged that main into the recorded PR head and reran those files. Ubuntu 26.04.1 (WSL2), Python 3.14.4. Unittest failure/error counts include subtests where applicable. AI-assisted with OpenAI Codex. Batch claim.

#1911 | head `f51d6070f7f8542486ca8fb1e51db59868e2c1a4` | `python3 -I tests/test_salu_diff.py -v` | red: 31 run, 16 failures, 10 errors | green: 31 passed | **Red → green** The test file exercised reference semantics, branch skips, register operands, arity, SIMM16 and compact comma spacing. Copied the PR tests onto clean main `4a54aedcb6b66525e0c9f72a6ea10263337c9141` for the baseline, then merged that main into the recorded PR head and reran those files. Ubuntu 26.04.1 (WSL2), Python 3.14.4. Unittest failure/error counts include subtests where applicable. AI-assisted with OpenAI Codex. [Batch claim](https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7238450840).
DadaCap commented 2026-11-28 11:44:29 +00:00

Merged, thank you @ShotTechOp! This PR (salu_diff: validate branch skips, register operands, arity, SIMM16, and comma sizing (SALU-DIFF-REF-VALIDATION)) passed its own tests with main merged in during sodove's verification of this shader batch, so it went in with the batch.

AI-assisted (Claude Code, Claude Opus 5.5), posted for the maintainers.

Merged, thank you @ShotTechOp! This PR (salu_diff: validate branch skips, register operands, arity, SIMM16, and comma sizing (SALU-DIFF-REF-VALIDATION)) passed its own tests with main merged in during sodove's verification of this shader batch, so it went in with the batch. AI-assisted (Claude Code, Claude Opus 5.5), posted for the maintainers.
Sign in to join this conversation.
No description provided.