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

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

@⁠sharan104 on GitHub · 2026-11-26 14:14 UTC · original
sharan104:fix/gfx10-sgpr-dst → main · +41 −0 · 1 files · 1 commits · open

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

**@⁠sharan104** on GitHub · 2026-11-26 14:14 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/279) `sharan104:fix/gfx10-sgpr-dst` → `main` · +41 −0 · 1 files · 1 commits · open 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)
VOP3 compares, v_readlane_b32 and v_readfirstlane_b32 name their SGPR
destination in an 8-bit field, and the decoder passed codes 128-255 through
as Operand::Scalar, whose contract is that the decoder gives none above 127.
The IR takes such codes for parked scalars (add_regs lets them through), and
ir::build then indexed its table of the 128 machine codes with one: a panic
("index out of bounds: the len is 128 but the index is 159", ir.rs:336), in
release builds too, from nothing but the words of a corrupt shader.

decode now refuses such an instruction with ShaderError::Isa, as it refuses
other code that names nothing. v_cmpx is left alone: it writes EXEC
whatever its destination field holds, and the IR never reads that field.
A sweep of a million random short programs through decode, text and
ir::build (debug build) found no other panic.

Test: gfx10::tests::rejects_sgpr_destinations_past_exec_hi (panicked at
ir.rs:336 before the fix).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:22:22 +00:00
Author
Owner

@⁠compaccount1-coder on GitHub · 2026-11-26 15:20 UTC · original

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)

**@⁠compaccount1-coder** on GitHub · 2026-11-26 15:20 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/279#issuecomment-7196130786) 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 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/279/head:github-bot/gh-279'
git switch 'github-bot/gh-279'

Merge

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