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

Open
opened 2026-11-26 21:19:24 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1911
Author: @ShotTechOp
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/salu-diff-ref-validation


📝 Commits (1)

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

📊 Changes

2 files changed (+154 additions, -22 deletions)

View changed files

📝 tests/test_salu_diff.py (+45 -0)
📝 tools/salu_diff.py (+109 -22)

📄 Description

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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1911 **Author:** [@ShotTechOp](https://github.com/ShotTechOp) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/salu-diff-ref-validation` --- ### 📝 Commits (1) - [`f51d607`](https://github.com/ubiquitarianism/leonida/commit/f51d6070f7f8542486ca8fb1e51db59868e2c1a4) salu_diff: validate branch skips, register operands, arity, SIMM16, and comma sizing (SALU-DIFF-REF-VALIDATION) ### 📊 Changes **2 files changed** (+154 additions, -22 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_salu_diff.py` (+45 -0) 📝 `tools/salu_diff.py` (+109 -22) </details> ### 📄 Description 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#1920
No description provided.