shader: heap-record lowering reads rh[0] when the index SGPR is overwritten before the load #3306

Closed
opened 2026-11-28 06:53:25 +00:00 by github-bot · 1 comment
Owner

@⁠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

main bbf4687b. tools/startup/gfx2hlsl.py is byte-identical to the #2861 head 8a133409. Translator only: no build, device or game files.

Reproduction

The program is the fixture's accepted direct form, plus one s_mov_b32 s22, 0 between the shift and the heap load. Run from the repository root:

import sys; sys.path.insert(0, 'tests')
import startup_compute_waterfall_heap_check as W
body = (W.SHIFT5, W.row_pair('s_mov_b32 s22, 0'), W.HEAP_LOAD, W.WAIT_LGKM, W.BODY_LOAD, W.WAIT_VM)
hlsl, manifest = W.translate(W.build_program(body=body))
for line in hlsl.splitlines():
    if line.strip().startswith(('s22 =', 's23 =', 'h0 =', 'if (exec & 1) v7 = rh')):
        print(line.strip())
print(manifest.splitlines()[-1])

Expected behavior

The program loads the record at s23 (key << 5), so h0 must be the key. Taking h0 from 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:

s22 = v5;
s23 = s22 << (0x00000005u & 31);
s22 = 0x00000000u;
h0 = s22;
if (exec & 1) v7 = rh[NonUniformResourceIndex(h0)].Load(a + 0);

Every lane reads rh[0]. The index is kept as the SGPR name ('s22' in the heap_index mark), and _capture_resheap_desc emits h0 = <expr>; only at the s_load. waterfall_heap_check drops s22 from local_idx on the write, but s23 stays in local_off5. The same happens in three more forms:

  • the pre-loop form (v_lshlrev_b32 v5, 5, v5, pointer add + src_scc carry from s22, then s22 = 0) gives h0 = (s22 >> 5); after s22 = 0x00000000u;;
  • with s_lshl_b32 s22, s22, 5; s_mov_b32 s24, s22; s_mov_b32 s22, 0; s_load ... s24 it is h0 = (s22 >> 5);;
  • with s_mov_b32 s24, s22; s_lshl_b32 s23, s24, 5; s_mov_b32 s24, 0 it is h0 = s24;.

A pointer pair that reuses the key register (s_add_u32 s22, s8, s23 + carry into s23) 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 cases tests/startup_compute_waterfall_heap_check.py still prints PASS (translator + 17 refused controls), exit 0:

  • M1: in the v_mov_b32 copy branch, take regs.get(rs[1], ('idx', 0)) and drop rs[1] in regs. Then v_mov_b32 v5, v9 with v9 unbounded (09030a7e) translates with rh[1] / resheap 1 2. Unmutated, it is refused.
  • M2: change regs[rs[1]][1] >> shift to >> (shift + 1). Then v_and_b32 v5, 3, v5; v_lshrrev_b32 v5, 1, v5 (810a0a2c; lanes hold 0..1) declares rh[1] / resheap 1 2. Unmutated, it declares rh[2]. v_lshrrev_b32 is 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 says gfx2hlsl.py "lowers one form of a load through that pointer". It does not mention the per-lane waterfall form or the space-2 resheap range.

Suggested fix and tests

  • Copy the index into a temporary where its mark is made (at the waterfall v_readfirstlane_b32, s_lshl_b32 ..., 5 or s_mov_b32), not at the load. Or refuse a write to the index's source register between the mark and the s_load.
  • Add the direct and pre-loop forms above as controls. If they are accepted, assert the exact h0 line.
  • Add a refused control for the unbounded v_mov_b32 copy, and accepted v_lshrrev_b32 cases with an exact rh[N] (bounded source, and the 0xffffffff >> shift form).
  • Update the docs paragraph.

Environment

Debian Linux, Python 3.13.5, on a read-only git archive export of main. I restored each mutant from a pristine copy and checked the sha256 (07ef1574a005…). I did not dispatch anything (no WARP, GPU or --oracle / --dispatch / --dxil runs), so the wrong record is inferred from the HLSL, not read back.

  • I reproduced this on a source-built input.
  • I did not include game files, dumps, distribution links or credentials.

Refs #2861, #2644.

This comment was AI-assisted.

**@⁠JohnConnorNPC** on GitHub · 2026-11-28 06:52 UTC · [original](https://github.com/R0Y00/Xbox2Windows/issues/3304) 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 `main` `bbf4687b`. `tools/startup/gfx2hlsl.py` is byte-identical to the #2861 head `8a133409`. Translator only: no build, device or game files. ### Reproduction The program is the fixture's accepted direct form, plus one `s_mov_b32 s22, 0` between the shift and the heap load. Run from the repository root: ```python import sys; sys.path.insert(0, 'tests') import startup_compute_waterfall_heap_check as W body = (W.SHIFT5, W.row_pair('s_mov_b32 s22, 0'), W.HEAP_LOAD, W.WAIT_LGKM, W.BODY_LOAD, W.WAIT_VM) hlsl, manifest = W.translate(W.build_program(body=body)) for line in hlsl.splitlines(): if line.strip().startswith(('s22 =', 's23 =', 'h0 =', 'if (exec & 1) v7 = rh')): print(line.strip()) print(manifest.splitlines()[-1]) ``` ### Expected behavior The program loads the record at `s23` (key << 5), so `h0` must be the key. Taking `h0` from 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: ``` s22 = v5; s23 = s22 << (0x00000005u & 31); s22 = 0x00000000u; h0 = s22; if (exec & 1) v7 = rh[NonUniformResourceIndex(h0)].Load(a + 0); ``` Every lane reads `rh[0]`. The index is kept as the SGPR name (`'s22'` in the `heap_index` mark), and `_capture_resheap_desc` emits `h0 = <expr>;` only at the `s_load`. `waterfall_heap_check` drops `s22` from `local_idx` on the write, but `s23` stays in `local_off5`. The same happens in three more forms: - the pre-loop form (`v_lshlrev_b32 v5, 5, v5`, pointer add + `src_scc` carry from `s22`, then `s22 = 0`) gives `h0 = (s22 >> 5);` after `s22 = 0x00000000u;`; - with `s_lshl_b32 s22, s22, 5; s_mov_b32 s24, s22; s_mov_b32 s22, 0; s_load ... s24` it is `h0 = (s22 >> 5);`; - with `s_mov_b32 s24, s22; s_lshl_b32 s23, s24, 5; s_mov_b32 s24, 0` it is `h0 = s24;`. A pointer pair that reuses the key register (`s_add_u32 s22, s8, s23` + carry into `s23`) 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 cases `tests/startup_compute_waterfall_heap_check.py` still prints `PASS (translator + 17 refused controls)`, exit 0: - **M1:** in the `v_mov_b32` copy branch, take `regs.get(rs[1], ('idx', 0))` and drop `rs[1] in regs`. Then `v_mov_b32 v5, v9` with `v9` unbounded (`09030a7e`) translates with `rh[1]` / `resheap 1 2`. Unmutated, it is refused. - **M2:** change `regs[rs[1]][1] >> shift` to `>> (shift + 1)`. Then `v_and_b32 v5, 3, v5; v_lshrrev_b32 v5, 1, v5` (`810a0a2c`; lanes hold 0..1) declares `rh[1]` / `resheap 1 2`. Unmutated, it declares `rh[2]`. `v_lshrrev_b32` is 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 says `gfx2hlsl.py` "lowers one form of a load through that pointer". It does not mention the per-lane waterfall form or the space-2 `resheap` range. ### Suggested fix and tests - Copy the index into a temporary where its mark is made (at the waterfall `v_readfirstlane_b32`, `s_lshl_b32 ..., 5` or `s_mov_b32`), not at the load. Or refuse a write to the index's source register between the mark and the `s_load`. - Add the direct and pre-loop forms above as controls. If they are accepted, assert the exact `h0` line. - Add a refused control for the unbounded `v_mov_b32` copy, and accepted `v_lshrrev_b32` cases with an exact `rh[N]` (bounded source, and the `0xffffffff >> shift` form). - Update the docs paragraph. ### Environment Debian Linux, Python 3.13.5, on a read-only `git archive` export of `main`. I restored each mutant from a pristine copy and checked the sha256 (`07ef1574a005…`). I did not dispatch anything (no WARP, GPU or `--oracle` / `--dispatch` / `--dxil` runs), so the wrong record is inferred from the HLSL, not read back. - [x] I reproduced this on a source-built input. - [x] I did not include game files, dumps, distribution links or credentials. Refs #2861, #2644. This comment was AI-assisted.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 06:53:25 +00:00
Author
Owner

@⁠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 reading rh[0]. I'll pin both of your mutants as fixture controls next to startup_compute_waterfall_heap_check, add a negative case for the overwrite, and add a docs line.

AI-Assisted

**@⁠SimSalabimse** on GitHub · 2026-11-28 07:55 UTC · [original](https://github.com/R0Y00/Xbox2Windows/issues/3304#issuecomment-7236248714) 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 reading `rh[0]`. I'll pin both of your mutants as fixture controls next to `startup_compute_waterfall_heap_check`, add a negative case for the overwrite, and add a docs line. AI-Assisted
This discussion has been locked. Commenting is limited to contributors.
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.

Dependencies

No dependencies set

Reference
github/R0Y00--Xbox2Windows#3306
No description provided.