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

Open
github-bot wants to merge 1 commit from github-bot/gh-1911 into main AGit
Owner

@⁠ShotTechOp on GitHub · 2026-11-25 15:36 UTC · original
ShotTechOp:fix/salu-diff-ref-validation → main · +154 −22 · 2 files · 1 commits · open

Claimed in #1608 as SALU-DIFF-REF-VALIDATION (#1608).

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.

**@⁠ShotTechOp** on GitHub · 2026-11-25 15:36 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1911) `ShotTechOp:fix/salu-diff-ref-validation` → `main` · +154 −22 · 2 files · 1 commits · open Claimed in #1608 as `SALU-DIFF-REF-VALIDATION` (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608). ### 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:10:54 +00:00
Author
Owner

@⁠DeadlyChirp on GitHub · 2026-11-25 15:50 UTC · original

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.)

**@⁠DeadlyChirp** on GitHub · 2026-11-25 15:50 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1911#issuecomment-7167353180) 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.)
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1911/head:github-bot/gh-1911'
git switch 'github-bot/gh-1911'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1911'
git switch 'github-bot/gh-1911'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1911'
git switch 'github-bot/gh-1911'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1911'
git switch 'main'
git merge --squash 'github-bot/gh-1911'
git switch 'main'
git merge --ff-only 'github-bot/gh-1911'
git switch 'main'
git merge 'github-bot/gh-1911'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.