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

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

📋 Pull Request Information

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

Base: main ← Head: fix/cf-ref-validation


📝 Commits (1)

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

📊 Changes

2 files changed (+213 additions, -70 deletions)

View changed files

📝 tests/test_cf_ref.py (+82 -0)
📝 tools/cf_ref.py (+131 -70)

📄 Description

Summary

Claimed in #1608 as CF-REF-OPERAND-VALIDATION: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7161734635

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.


🔄 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/1780 **Author:** [@ShotTechOp](https://github.com/ShotTechOp) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/cf-ref-validation` --- ### 📝 Commits (1) - [`3df0d83`](https://github.com/ubiquitarianism/leonida/commit/3df0d83ea50966a136b3794e6037220e666869d7) cf_ref: validate mask widths, integer/export/branch modifiers, EXEC=0 operands, and duplicate PCs (CF-REF-OPERAND-VALIDATION) ### 📊 Changes **2 files changed** (+213 additions, -70 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_cf_ref.py` (+82 -0) 📝 `tools/cf_ref.py` (+131 -70) </details> ### 📄 Description ## Summary Claimed in #1608 as `CF-REF-OPERAND-VALIDATION`: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7161734635 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. --- <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#1791
No description provided.