cf_ref: validate mask widths, integer/export/branch modifiers, EXEC=0 operands, and duplicate PCs (CF-REF-OPERAND-VALIDATION) #1780

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

@⁠ShotTechOp on GitHub · 2026-11-25 11:58 UTC · original
ShotTechOp:fix/cf-ref-validation → main · +213 −70 · 2 files · 1 commits · open

Summary

Claimed in #1608 as CF-REF-OPERAND-VALIDATION: #1608

Hardens tools/cf_ref.py (WaveMachine) so malformed or unsupported control-flow, mask, integer, export, and branch operands raise cpu_ref.Unsupported / cpu_ref.ProgramError instead of silently truncating, wrapping, or skipping validation when EXEC == 0:

  1. Exact mask operand width checks (_read_mask, _write_mask):
    • Accept an optional width (32 or 64, defaulting to self.wave so subclasses like tools/wave_diff.py stay compatible).
    • Reject SGPR counts that do not match the operation width (n != 2 on 64-bit mask ops/compares such as s0 or s[0:3]; n != 1 on 32-bit mask ops/compares such as s[0:1]), and reject 32-bit *_lo/*_hi special mask registers on 64-bit ops (s_mov_b64, s_<op>_b64, s_*_saveexec_b64, wave64 v_cmp_*).
    • Preserve wave32 exec_hi read-as-0 / ignored-write and vcc_hi scalar behaviour covered by tests/test_cf_ref_edge_fixes.py.
  2. Validate operands even when EXEC == 0:
    • Validate vector destinations (_check_vgpr_dst) and integer sources (_check_int_src) before the active-lane loop in v_add_nc_u32, v_sub_nc_u32, v_subrev_nc_u32, v_mul_lo_u32, v_mbcnt_lo_u32_b32, v_mbcnt_hi_u32_b32, wave32 VOP2 v_cndmask_b32, and v_cmp_* / v_cmpx_*.
  3. Reject abs/neg and multi-dword operands on integer, mask, and export ops:
    • Reject abs/neg modifiers on mask operands, scalar destinations, integer sources, and exp mrt0 operands, plus multi-dword VGPRs on exp mrt0 and integer vector ops.
  4. Branch simm16 range/modifier checks and duplicate PC guard:
    • Reject abs/neg modifiers and out-of-range offsets outside [-32768, 65535] in _branch.
    • Raise ProgramError on duplicate instruction PCs in WaveMachine.__init__.

Independent of #1457 (tools/cpu_ref.py); zero PR dependencies and zero file overlap.

Testing

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

  • python -I tests/test_cf_ref.py -> 31/31 OK (negative control on 0d80bc5 tools/cf_ref.py: 42 failures, 1 error across the 5 new regression tests)
  • python -I tests/test_cf_ref_edge_fixes.py -> 5/5 OK
  • python -I tests/test_wave_diff.py -> 12/12 OK
  • python -m compileall -q tools/cf_ref.py tests/test_cf_ref.py -> exit 0
  • python -I scripts/check_no_game_data.py tools/cf_ref.py tests/test_cf_ref.py -> exit 0
  • git diff --check -> clean

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

**@⁠ShotTechOp** on GitHub · 2026-11-25 11:58 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1780) `ShotTechOp:fix/cf-ref-validation` → `main` · +213 −70 · 2 files · 1 commits · open ## Summary Claimed in #1608 as `CF-REF-OPERAND-VALIDATION`: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 Hardens `tools/cf_ref.py` (`WaveMachine`) so malformed or unsupported control-flow, mask, integer, export, and branch operands raise `cpu_ref.Unsupported` / `cpu_ref.ProgramError` instead of silently truncating, wrapping, or skipping validation when `EXEC == 0`: 1. **Exact mask operand width checks (`_read_mask`, `_write_mask`)**: - Accept an optional `width` (`32` or `64`, defaulting to `self.wave` so subclasses like `tools/wave_diff.py` stay compatible). - Reject SGPR counts that do not match the operation width (`n != 2` on 64-bit mask ops/compares such as `s0` or `s[0:3]`; `n != 1` on 32-bit mask ops/compares such as `s[0:1]`), and reject 32-bit `*_lo`/`*_hi` special mask registers on 64-bit ops (`s_mov_b64`, `s_<op>_b64`, `s_*_saveexec_b64`, wave64 `v_cmp_*`). - Preserve wave32 `exec_hi` read-as-0 / ignored-write and `vcc_hi` scalar behaviour covered by `tests/test_cf_ref_edge_fixes.py`. 2. **Validate operands even when `EXEC == 0`**: - Validate vector destinations (`_check_vgpr_dst`) and integer sources (`_check_int_src`) before the active-lane loop in `v_add_nc_u32`, `v_sub_nc_u32`, `v_subrev_nc_u32`, `v_mul_lo_u32`, `v_mbcnt_lo_u32_b32`, `v_mbcnt_hi_u32_b32`, wave32 VOP2 `v_cndmask_b32`, and `v_cmp_*` / `v_cmpx_*`. 3. **Reject `abs`/`neg` and multi-dword operands on integer, mask, and export ops**: - Reject `abs`/`neg` modifiers on mask operands, scalar destinations, integer sources, and `exp mrt0` operands, plus multi-dword VGPRs on `exp mrt0` and integer vector ops. 4. **Branch `simm16` range/modifier checks and duplicate PC guard**: - Reject `abs`/`neg` modifiers and out-of-range offsets outside `[-32768, 65535]` in `_branch`. - Raise `ProgramError` on duplicate instruction PCs in `WaveMachine.__init__`. Independent of `#1457` (`tools/cpu_ref.py`); zero PR dependencies and zero file overlap. ## Testing Environment: Windows 11 x64, Python 3.12.10, CPU-only (no GPU or game files; rebased on `0d80bc5`). - `python -I tests/test_cf_ref.py` -> 31/31 OK (negative control on `0d80bc5` `tools/cf_ref.py`: 42 failures, 1 error across the 5 new regression tests) - `python -I tests/test_cf_ref_edge_fixes.py` -> 5/5 OK - `python -I tests/test_wave_diff.py` -> 12/12 OK - `python -m compileall -q tools/cf_ref.py tests/test_cf_ref.py` -> exit 0 - `python -I scripts/check_no_game_data.py tools/cf_ref.py tests/test_cf_ref.py` -> exit 0 - `git diff --check` -> clean AI-assisted: yes (Antigravity). Synthetic inputs only; no game data.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:04:50 +00:00
Author
Owner

@⁠lixonik on GitHub · 2026-11-25 12:13 UTC · original · reviewed

Independent native Windows A/B at 3df0d83e against 0d80bc5d. With the new cf_ref test file run against the base implementation I saw 42 failed assertions and one error. At this head all 31 cf_ref tests, five cf_ref edge tests and 12 wave_diff tests passed. The last suite checks an existing consumer of the reference machine.

AI-assisted. Synthetic instructions and CPU only.

**@⁠lixonik** on GitHub · 2026-11-25 12:13 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1780#pullrequestreview-6057394848) · **reviewed** Independent native Windows A/B at `3df0d83e` against `0d80bc5d`. With the new cf_ref test file run against the base implementation I saw 42 failed assertions and one error. At this head all 31 cf_ref tests, five cf_ref edge tests and 12 wave_diff tests passed. The last suite checks an existing consumer of the reference machine. AI-assisted. Synthetic instructions and CPU only.
Author
Owner

@⁠GeorgiyDemo on GitHub · 2026-11-25 12:20 UTC · original

Tested 3df0d83ea on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against main 0d80bc5df (Python CPU reference; no GPU):

  • pytest 9.2.0 on tests/test_cf_ref.py: 31 passed, 106 subtests passed at this head.
  • The same file on main: 43 failed, 30 passed, 64 subtests — the mask-width, EXEC=0 operand, integer/export and branch-modifier validation is not there.
  • Not run: anything on hardware — cf_ref is the CPU reference model.

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

**@⁠GeorgiyDemo** on GitHub · 2026-11-25 12:20 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1780#issuecomment-7162234100) Tested `3df0d83ea` on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against `main` `0d80bc5df` (Python CPU reference; no GPU): - pytest 9.2.0 on `tests/test_cf_ref.py`: **31 passed, 106 subtests passed** at this head. - The same file on `main`: **43 failed**, 30 passed, 64 subtests — the mask-width, EXEC=0 operand, integer/export and branch-modifier validation is not there. - Not run: anything on hardware — `cf_ref` is the CPU reference model. AI-assisted; synthetic fixtures only, no game data.
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/1780/head:github-bot/gh-1780'
git switch 'github-bot/gh-1780'

Merge

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