Shaders: refuse an SGPR destination past exec_hi when decoding (ir::build panicked, release too) #279

Open
sharan104 wants to merge 1 commit from upstream-pr/279 into main
sharan104 commented 2026-11-26 14:14:36 +00:00 (Migrated from github.com)

Imported from avatarmanz/VIctory-7900XTX. Originally posted by sharan104 on 2026-11-26T14:14:36Z.


VOP3 compares, v_readlane_b32 and v_readfirstlane_b32 take their SGPR destination from an 8-bit field. Codes from 128 up don't name a register. But fields.rs handed them on as Operand::Scalar, which promises a code below 128. The IR treats such codes as parked scalars, and ir::build then indexes its 128-entry table of machine scalars past the end (ir.rs:336). Bounds checks stay on in release builds, so a corrupt shader's words alone crashed the process, in release too.

A throwaway sweep of a million random short programs through decode, text and ir::build (debug build, not committed) found 1,073 panics, all at that spot. After this change it found none.

Fix: decode refuses such an instruction with ShaderError::Isa("an SGPR destination past exec_hi"), so the shader is reported as not translating instead of crashing. v_cmpx is exempt: it writes EXEC, and the IR never reads its destination field. Any shader this now refuses used to panic, so no shader that translated before is lost.

Test: gfx10::tests::rejects_sgpr_destinations_past_exec_hi.

  • Valid v_cmp_lt_f32 s6, v_readfirstlane_b32 s2 and v_readlane_b32 s2 still decode.
  • The same instructions with destination codes 0x9F, 0xCF and 0x80 are refused by both decode and ir::build.
  • A v_cmpx with code 0x9F is still accepted.

The test fails before the fix and passed 10 runs out of 10 after it.

Results:

  • cargo test -p victory-shader --locked: 303 passed, 0 failed, 4 ignored.
  • Clippy -D warnings is clean, with and without --all-features.

🤖 Generated with Claude Code

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:issue:6628528236 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/pull/279). Originally posted by [sharan104](https://github.com/sharan104) on 2026-11-26T14:14:36Z. --- VOP3 compares, `v_readlane_b32` and `v_readfirstlane_b32` take their SGPR destination from an 8-bit field. Codes from 128 up don't name a register. But `fields.rs` handed them on as `Operand::Scalar`, which promises a code below 128. The IR treats such codes as parked scalars, and `ir::build` then indexes its 128-entry table of machine scalars past the end (`ir.rs:336`). Bounds checks stay on in release builds, so a corrupt shader's words alone crashed the process, in release too. A throwaway sweep of a million random short programs through `decode`, `text` and `ir::build` (debug build, not committed) found 1,073 panics, all at that spot. After this change it found none. **Fix:** `decode` refuses such an instruction with `ShaderError::Isa("an SGPR destination past exec_hi")`, so the shader is reported as not translating instead of crashing. `v_cmpx` is exempt: it writes EXEC, and the IR never reads its destination field. Any shader this now refuses used to panic, so no shader that translated before is lost. **Test:** `gfx10::tests::rejects_sgpr_destinations_past_exec_hi`. - Valid `v_cmp_lt_f32 s6`, `v_readfirstlane_b32 s2` and `v_readlane_b32 s2` still decode. - The same instructions with destination codes 0x9F, 0xCF and 0x80 are refused by both `decode` and `ir::build`. - A `v_cmpx` with code 0x9F is still accepted. The test fails before the fix and passed 10 runs out of 10 after it. **Results:** - `cargo test -p victory-shader --locked`: 303 passed, 0 failed, 4 ignored. - Clippy `-D warnings` is clean, with and without `--all-features`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
compaccount1-coder commented 2026-11-26 15:20:37 +00:00 (Migrated from github.com)

Imported from avatarmanz/VIctory-7900XTX. Originally posted by compaccount1-coder on 2026-11-26T15:20:37Z.


Independent confirmation from a random-input run, in case it helps this get merged:

I fed mutated GFX10 instruction words (taken from the test programs here, with random bits flipped and some fully random words) into gfx10::decode → ir::build → structure::structure on main (c9c932c). Within 20,000 cases it panicked twice, both at ir.rs:336 in ir::unspill (used[usize::from(c)], used is [bool; 128]):

index out of bounds: the len is 128 but the index is 181
  wave64=false vgprs=300 words=[43d42199, 7e02c864, 340e0b06, 2c0c4bc5, d4e99bb5, befede33, f4080202, 7d884334, a0020000, bf810000]
index out of bounds: the len is 128 but the index is 247
  wave64=true  vgprs=0   words=[be840d00, bf8c0170, 80030300, be850380, 3d4debcd, d4c4c3f7, 2c0c00a1, d8d8df48, bf800001, bf810000]

On this PR's branch (2799904), both are refused by gfx10::decode, and 100,000 more random cases through the same three stages gave no panic. Of 500,000 random compute programs through the whole pipeline on top of it (spill::build → structure → resource::track → io::raise → emit::emit, on a CS skeleton like wave64_tests::native), one other panic came up. It is unrelated to this one (a debug_assert in cross_lane, a lowering gap rather than a decoding one) and fixed in #308.

So the decoding crash the random run found is the one this PR fixes. Windows 11, Rust 1.98.1, the scratch harness not committed anywhere.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:comment:7196130786 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/pull/279#issuecomment-7196130786). Originally posted by [compaccount1-coder](https://github.com/compaccount1-coder) on 2026-11-26T15:20:37Z. --- Independent confirmation from a random-input run, in case it helps this get merged: I fed mutated GFX10 instruction words (taken from the test programs here, with random bits flipped and some fully random words) into `gfx10::decode` → `ir::build` → `structure::structure` on `main` (`c9c932c`). Within 20,000 cases it panicked twice, both at `ir.rs:336` in `ir::unspill` (`used[usize::from(c)]`, `used` is `[bool; 128]`): ``` index out of bounds: the len is 128 but the index is 181 wave64=false vgprs=300 words=[43d42199, 7e02c864, 340e0b06, 2c0c4bc5, d4e99bb5, befede33, f4080202, 7d884334, a0020000, bf810000] index out of bounds: the len is 128 but the index is 247 wave64=true vgprs=0 words=[be840d00, bf8c0170, 80030300, be850380, 3d4debcd, d4c4c3f7, 2c0c00a1, d8d8df48, bf800001, bf810000] ``` On this PR's branch (`2799904`), both are refused by `gfx10::decode`, and 100,000 more random cases through the same three stages gave no panic. Of 500,000 random compute programs through the whole pipeline on top of it (`spill::build` → `structure` → `resource::track` → `io::raise` → `emit::emit`, on a CS skeleton like `wave64_tests::native`), one other panic came up. It is unrelated to this one (a `debug_assert` in `cross_lane`, a lowering gap rather than a decoding one) and fixed in #308. So the decoding crash the random run found is the one this PR fixes. Windows 11, Rust 1.98.1, the scratch harness not committed anywhere. AI-assisted: yes (Claude Code)
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/279:upstream-pr/279'
git switch 'upstream-pr/279'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/279'
git switch 'upstream-pr/279'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/279'
git switch 'upstream-pr/279'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/279'
git switch 'main'
git merge --squash 'upstream-pr/279'
git switch 'main'
git merge --ff-only 'upstream-pr/279'
git switch 'main'
git merge 'upstream-pr/279'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
avatarmanz/VIctory-7900XTX!279
No description provided.