shader: heap-record lowering reads rh[0] when the index SGPR is overwritten before the load #3306
Labels
No labels
accessibility
awaiting author
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
priority: P0
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/R0Y00--Xbox2Windows#3306
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@JohnConnorNPC on GitHub · 2026-11-28 06:52 UTC · original
The per-lane heap-record lowering from #2861 (merged as
48ca9ab1) can accept a compute waterfall and translate it to the wrong record. The suite stays green. Below are a reproduction with the PR's own fixture helpers, two surviving mutants and a stale doc paragraph.Commit or build
mainbbf4687b.tools/startup/gfx2hlsl.pyis byte-identical to the #2861 head8a133409. Translator only: no build, device or game files.Reproduction
The program is the fixture's accepted direct form, plus one
s_mov_b32 s22, 0between the shift and the heap load. Run from the repository root:Expected behavior
The program loads the record at
s23(key << 5), soh0must be the key. Takingh0from the value before the write, or refusing the program, would both be correct.Observed behavior
The program is accepted (
resheap 4 2), and the HLSL reads the overwritten register:Every lane reads
rh[0]. The index is kept as the SGPR name ('s22'in theheap_indexmark), and_capture_resheap_descemitsh0 = <expr>;only at thes_load.waterfall_heap_checkdropss22fromlocal_idxon the write, buts23stays inlocal_off5. The same happens in three more forms:v_lshlrev_b32 v5, 5, v5, pointer add +src_scccarry froms22, thens22 = 0) givesh0 = (s22 >> 5);afters22 = 0x00000000u;;s_lshl_b32 s22, s22, 5; s_mov_b32 s24, s22; s_mov_b32 s22, 0; s_load ... s24it ish0 = (s22 >> 5);;s_mov_b32 s24, s22; s_lshl_b32 s23, s24, 5; s_mov_b32 s24, 0it ish0 = s24;.A pointer pair that reuses the key register (
s_add_u32 s22, s8, s23+ carry intos23) still translates correctly, because the resheap add and carry emit no HLSL. Only an emitted write to the source register in between triggers the bug.Two bound forms the fixture does not pin
Each mutant below was a one-line edit to
vgpr_heap_bounds. In both casestests/startup_compute_waterfall_heap_check.pystill printsPASS (translator + 17 refused controls), exit 0:v_mov_b32copy branch, takeregs.get(rs[1], ('idx', 0))and droprs[1] in regs. Thenv_mov_b32 v5, v9withv9unbounded (09030a7e) translates withrh[1]/resheap 1 2. Unmutated, it is refused.regs[rs[1]][1] >> shiftto>> (shift + 1). Thenv_and_b32 v5, 3, v5; v_lshrrev_b32 v5, 1, v5(810a0a2c; lanes hold 0..1) declaresrh[1]/resheap 1 2. Unmutated, it declaresrh[2].v_lshrrev_b32is listed as a bounding form in #2861 but appears 0 times in the fixture.gfx2hlsl's own decoder reads both hand-encoded words as the instruction named. I did not run them through llvm-mc.
Docs
docs/experiments.md, "An input named by a resource heap record", still saysgfx2hlsl.py"lowers one form of a load through that pointer". It does not mention the per-lane waterfall form or the space-2resheaprange.Suggested fix and tests
v_readfirstlane_b32,s_lshl_b32 ..., 5ors_mov_b32), not at the load. Or refuse a write to the index's source register between the mark and thes_load.h0line.v_mov_b32copy, and acceptedv_lshrrev_b32cases with an exactrh[N](bounded source, and the0xffffffff >> shiftform).Environment
Debian Linux, Python 3.13.5, on a read-only
git archiveexport ofmain. I restored each mutant from a pristine copy and checked the sha256 (07ef1574a005…). I did not dispatch anything (no WARP, GPU or--oracle/--dispatch/--dxilruns), so the wrong record is inferred from the HLSL, not read back.Refs #2861, #2644.
This comment was AI-assisted.
@SimSalabimse on GitHub · 2026-11-28 07:55 UTC · original
I'll take this one.
My plan is to copy the index SGPR into a temp at the waterfall mark, so the per-lane heap-record read uses the value from the mark even if the SGPR gets overwritten before the
s_load. If an in-between write can't be handled safely, the lowering will refuse it instead of quietly readingrh[0]. I'll pin both of your mutants as fixture controls next tostartup_compute_waterfall_heap_check, add a negative case for the overwrite, and add a docs line.AI-Assisted
github-bot referenced this issue2026-11-28 20:27:46 +00:00