shader: preserve StartInstanceLocation store in indexed_indirect_guard and add tests #3663

Manually merged
github merged 3 commits from github-bot/gh-3661 into gh-pr/3661/base 2026-12-01 03:19:45 +00:00 AGit
Owner

@⁠Ammmae0x on GitHub · 2026-11-30 22:12 UTC · original
Ammmae0x:tests/startup-indexed-indirect-guard → main · +1120 −3 · 6 files · 3 commits · merged 2026-12-01 03:13 UTC

Refs #1149, #2252.

Fixes two real Windows bugs found during WARP+NVIDIA verification, and adds D3D12 cs_5_0 dispatch + boundary + configure-time HLSL mutation tests for x2w_startup::indexed_indirect_guard (src/startup_stubs/indexed_indirect_guard.h).

1) FXC cs_5_0 drops Store(24, start_instance). With D3DCompile(..., "cs_5_0", flags=0) (as in d3d12_draw.inl), verified.Store(4, args.w); verified.Store4(8, args); verified.Store(24, start_instance); verified.Store(0, 1); hit an FXC optimizer bug and omitted Store(24, start_instance) from DXBC (store_raw emitted only offsets 0 and 8, never writing word 6), so verified[6] stayed 0xDEADBEEF on valid_u16_high_low and valid_u32_neg_base. Replaced with:
verified.Store3(4, uint3(args.w, args.x, args.y)); (root constant + count, instances)
verified.Store3(16, uint3(args.z, args.w, start_instance)); (start_index, base, start_instance)
verified.Store(0, 1);
This emits all 7 words (verified under D3DDisassemble cs_5_0 flags=0: store_raw u0.xyz, l(16) + store_raw u0.xyzw, l(0)).

2) startup_indexed_indirect_guard_arg_align_mut failed to trip. In D3D12 ByteAddressBuffer::Load4(arg_offset) ignores bits [1:0] (ld_raw reads at arg_offset & ~3, i.e. 0 when arg_offset=2). dispatch() uploaded arg_words only at offset 2, so Load4(2) read {0x00030000, ...} (count > 4096) and dropping (arg_offset & 3) still refused. dispatch() now uploads arg_words and start_instance at spec.arg_offset & ~3u (0), so the aligned-down Load4(2) reads valid {3, 1, 0, 0}, the mutant accepts the draw (verified[0]=1) and is killed by WILL_FAIL TRUE. The Python model mirrors this with offset & ~3 in load() and the same aligned upload in its fixture.

What landed (commit 2d813325, on top of f019f0fb + 78b8379d):

  • src/startup_stubs/indexed_indirect_guard.h: the two Store3 writes above, with a comment pinning the FXC bug.
  • tests/startup_indexed_indirect_guard_check.cpp: dispatch() uploads at arg_offset & ~3u; 2 valid draws (valid_u16_high_low with index_offset = 2, valid_u32_neg_base with base = -2) with exact words 0..6, and 10 refusals (refuse_arg_align, refuse_arg_oob, refuse_elem_size, refuse_index_align, refuse_count_bounds with counts 2, 4, 4098, refuse_instance_nonone, refuse_start_instance, refuse_neg_base_underflow, refuse_pos_base_overflow, refuse_vertex_oob). Supports <warp|hardware> [<case>], honors XBOX2WINDOWS_ADAPTER, exits 77 when no hardware adapter is available, never skips WARP.
  • tests/cmake/startup_indexed_indirect_guard.cmake: registers _warp and _hardware tests (TIMEOUT 60, SKIP_RETURN_CODE 77) plus 5 WILL_FAIL TRUE configure-time HLSL mutation controls (drop arg-align clause, drop start-instance clause, drop negative-base underflow clause, low-half-only u16 read, drop vertices clause).
  • tests/startup_indexed_indirect_guard_meta_check.py + tests/checks/python-linux/startup_indexed_indirect_guard.txt: Python uint32 model of guard() verifying all cases and killing all 5 HLSL mutations locally (12 cases, 5/5 killed), now pinning the Store3 lines, the arg_offset & ~3u upload, and the offset & ~3 load.
  • docs/experiments/startup-indexed-indirect-guard.md: case and control notes, plus a Windows findings section for both bugs, including why refuse_neg_base_underflow uses a UINT_MAX vertex count and why refuse_count_bounds uses 4098 indices.

Linux (on 2d813325): meta check passes (12 cases, 5/5 mutations killed), run_checks_parallel.py --from-dir tests/checks/python-linux 73/73, form/docs/bidi pass (193 files/750 links, 2765 files), py_compile passes, diff --check clean, git merge-tree --write-tree origin/main HEAD exits 0 (no upstream remote in this clone; origin/main is a3bfd019), zero personal paths in the diff.

Windows Verification (2d813325d7bf93ec6df710b51044d943e2a6e661)

Commit: 2d813325d7bf93ec6df710b51044d943e2a6e661 (merge-tree upstream/main HEAD -> 25918c21, exit 0; git diff --check clean)
Adapter: NVIDIA GeForce RTX 4050 Laptop GPU (vendor=10de device=28e1, XBOX2WINDOWS_ADAPTER=NVIDIA) and WARP
dev: cmake --build --preset dev --target startup_indexed_indirect_guard_check ... && ctest --preset dev --no-tests=error -R '^startup_indexed_indirect_guard' --output-on-failure -> 7/7 passed (2.03s; warp 0.11s, hardware 0.64s, 12/12 cases, 5/5 mutations killed)
release: cmake --build --preset release --target startup_indexed_indirect_guard_check ... && ctest --preset release --no-tests=error -R '^startup_indexed_indirect_guard' --output-on-failure -> 7/7 passed (1.16s; warp 0.19s, hardware 0.20s, 12/12 cases, 5/5 mutations killed)
Checks: startup_indexed_indirect_guard_meta_check.py (12 cases, 5/5 mutations killed), form_coverage_check.py, docs_links_check.py (193 files, 750 links), bidi_controls_check.py (2765 files) -> passed

AI-assisted.

**@⁠Ammmae0x** on GitHub · 2026-11-30 22:12 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3661) `Ammmae0x:tests/startup-indexed-indirect-guard` → `main` · +1120 −3 · 6 files · 3 commits · merged 2026-12-01 03:13 UTC Refs #1149, #2252. Fixes two real Windows bugs found during WARP+NVIDIA verification, and adds D3D12 `cs_5_0` dispatch + boundary + configure-time HLSL mutation tests for `x2w_startup::indexed_indirect_guard` (`src/startup_stubs/indexed_indirect_guard.h`). **1) FXC `cs_5_0` drops `Store(24, start_instance)`.** With `D3DCompile(..., "cs_5_0", flags=0)` (as in `d3d12_draw.inl`), `verified.Store(4, args.w); verified.Store4(8, args); verified.Store(24, start_instance); verified.Store(0, 1);` hit an FXC optimizer bug and omitted `Store(24, start_instance)` from DXBC (`store_raw` emitted only offsets 0 and 8, never writing word 6), so `verified[6]` stayed `0xDEADBEEF` on `valid_u16_high_low` and `valid_u32_neg_base`. Replaced with: `verified.Store3(4, uint3(args.w, args.x, args.y));` (root constant + count, instances) `verified.Store3(16, uint3(args.z, args.w, start_instance));` (start_index, base, start_instance) `verified.Store(0, 1);` This emits all 7 words (verified under D3DDisassemble `cs_5_0` flags=0: `store_raw u0.xyz, l(16)` + `store_raw u0.xyzw, l(0)`). **2) `startup_indexed_indirect_guard_arg_align_mut` failed to trip.** In D3D12 `ByteAddressBuffer::Load4(arg_offset)` ignores bits [1:0] (`ld_raw` reads at `arg_offset & ~3`, i.e. 0 when `arg_offset=2`). `dispatch()` uploaded `arg_words` only at offset 2, so `Load4(2)` read `{0x00030000, ...}` (count > 4096) and dropping `(arg_offset & 3)` still refused. `dispatch()` now uploads `arg_words` and `start_instance` at `spec.arg_offset & ~3u` (0), so the aligned-down `Load4(2)` reads valid `{3, 1, 0, 0}`, the mutant accepts the draw (`verified[0]=1`) and is killed by `WILL_FAIL TRUE`. The Python model mirrors this with `offset & ~3` in `load()` and the same aligned upload in its fixture. What landed (commit 2d813325, on top of f019f0fb + 78b8379d): - `src/startup_stubs/indexed_indirect_guard.h`: the two `Store3` writes above, with a comment pinning the FXC bug. - `tests/startup_indexed_indirect_guard_check.cpp`: `dispatch()` uploads at `arg_offset & ~3u`; 2 valid draws (`valid_u16_high_low` with `index_offset = 2`, `valid_u32_neg_base` with `base = -2`) with exact words `0..6`, and 10 refusals (`refuse_arg_align`, `refuse_arg_oob`, `refuse_elem_size`, `refuse_index_align`, `refuse_count_bounds` with counts 2, 4, 4098, `refuse_instance_nonone`, `refuse_start_instance`, `refuse_neg_base_underflow`, `refuse_pos_base_overflow`, `refuse_vertex_oob`). Supports `<warp|hardware> [<case>]`, honors `XBOX2WINDOWS_ADAPTER`, exits 77 when no hardware adapter is available, never skips WARP. - `tests/cmake/startup_indexed_indirect_guard.cmake`: registers `_warp` and `_hardware` tests (`TIMEOUT 60`, `SKIP_RETURN_CODE 77`) plus 5 `WILL_FAIL TRUE` configure-time HLSL mutation controls (drop arg-align clause, drop start-instance clause, drop negative-base underflow clause, low-half-only u16 read, drop vertices clause). - `tests/startup_indexed_indirect_guard_meta_check.py` + `tests/checks/python-linux/startup_indexed_indirect_guard.txt`: Python `uint32` model of `guard()` verifying all cases and killing all 5 HLSL mutations locally (12 cases, 5/5 killed), now pinning the `Store3` lines, the `arg_offset & ~3u` upload, and the `offset & ~3` load. - `docs/experiments/startup-indexed-indirect-guard.md`: case and control notes, plus a Windows findings section for both bugs, including why `refuse_neg_base_underflow` uses a `UINT_MAX` vertex count and why `refuse_count_bounds` uses 4098 indices. Linux (on 2d813325): meta check passes (12 cases, 5/5 mutations killed), `run_checks_parallel.py --from-dir tests/checks/python-linux` 73/73, form/docs/bidi pass (193 files/750 links, 2765 files), `py_compile` passes, `diff --check` clean, `git merge-tree --write-tree origin/main HEAD` exits 0 (no `upstream` remote in this clone; `origin/main` is `a3bfd019`), zero personal paths in the diff. ### Windows Verification (`2d813325d7bf93ec6df710b51044d943e2a6e661`) ```text Commit: 2d813325d7bf93ec6df710b51044d943e2a6e661 (merge-tree upstream/main HEAD -> 25918c21, exit 0; git diff --check clean) Adapter: NVIDIA GeForce RTX 4050 Laptop GPU (vendor=10de device=28e1, XBOX2WINDOWS_ADAPTER=NVIDIA) and WARP dev: cmake --build --preset dev --target startup_indexed_indirect_guard_check ... && ctest --preset dev --no-tests=error -R '^startup_indexed_indirect_guard' --output-on-failure -> 7/7 passed (2.03s; warp 0.11s, hardware 0.64s, 12/12 cases, 5/5 mutations killed) release: cmake --build --preset release --target startup_indexed_indirect_guard_check ... && ctest --preset release --no-tests=error -R '^startup_indexed_indirect_guard' --output-on-failure -> 7/7 passed (1.16s; warp 0.19s, hardware 0.20s, 12/12 cases, 5/5 mutations killed) Checks: startup_indexed_indirect_guard_meta_check.py (12 cases, 5/5 mutations killed), form_coverage_check.py, docs_links_check.py (193 files, 750 links), bidi_controls_check.py (2765 files) -> passed ``` AI-assisted. <!-- WIN-VERIFY: passed | sha=2d813325d7bf93ec6df710b51044d943e2a6e661 | targets=startup_indexed_indirect_guard_check startup_indexed_indirect_guard_arg_align_mut startup_indexed_indirect_guard_start_inst_mut startup_indexed_indirect_guard_neg_underflow_mut startup_indexed_indirect_guard_u16_shift_mut startup_indexed_indirect_guard_vertex_oob_mut | ctest=^startup_indexed_indirect_guard | release=true -->
Refs #1149. D3D12 cs_5_0 dispatch + boundary tests for x2w_startup::indexed_indirect_guard (src/startup_stubs/indexed_indirect_guard.h), without changing the header or device code: 2 valid draws (u16 high/low halves, u32 negative base) and 10 refusals, with the verified buffer cleared to a sentinel before each dispatch. The CMake fragment registers warp/hardware tests plus 5 WILL_FAIL TRUE configure-time HLSL mutation controls, each killed by its designated control case in the Python uint32 model (12 cases, 5/5 killed). Linux: meta check passes, run_checks_parallel 73/73, form/docs/bidi pass.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 22:16:16 +00:00
MSVC rejects the braced count list without <initializer_list> (C3312)
and narrows the UINT16 descriptor fields (C4244); cast them explicitly.
github-bot changed title from tests: add D3D12 guard and mutation tests for indexed_indirect_guard.h to shader: preserve StartInstanceLocation store in indexed_indirect_guard and add tests 2026-12-01 01:19:38 +00:00
FXC cs_5_0 (flags=0) dropped the separate Store(24, start_instance),
leaving verified[6] at the sentinel on valid draws; write verified
with two Store3 calls so all seven words are emitted. D3D12 Load4
ignores bits [1:0], so upload the draw arguments at arg_offset & ~3
and model offset & ~3 in the Python load(), making arg_align_mut
accept the misaligned draw.

Refs #1149, #2252
Author
Owner

@⁠lukaslw-lab on GitHub · 2026-12-01 02:42 UTC · original · approved these changes

Reviewed 2d813325 by reading the diff; not built or run.

Verdict: approve. The two Store3 writes produce the same seven-word record as before, and the new D3D12 check pins every word. No MUST, no SHOULD.

  • Checked, no finding:
    • Layout (src/startup_stubs/indexed_indirect_guard.h:41-43): word 1 is the base-vertex root constant, and words 2–6 are D3D12_DRAW_INDEXED_ARGUMENTS (count, instances, start index, base vertex, start instance). This matches the 24-byte command signature in d3d12_draw.inl:2609-2611 and the old Store/Store4/Store sequence. Store(0, 1) still comes last, so a refused draw leaves the count at 0.
    • Production impact of the dropped store: the guard returns unless start_instance == 0, and the runtime writes into a fresh committed default-heap buffer per validation (d3d12_draw.inl:2620-2628), which D3D12 zero-fills. So word 6 was 0 in real runs either way. The fix removes the reliance on zero-fill, and the sentinel-filled test buffer is what exposed the dropped store.
    • The arg_offset & ~3u upload in the test is a fixture change only. It makes the aligned-down Load4 read a valid record, so the arg-align control can actually be killed. The guard keeps its own (arg_offset & 3) refusal.
    • Each of the five CMake mutations removes one guard clause that this PR does not touch and runs on WARP with the case that clause must refuse. The u16 mutant uses valid_u16_high_low with index_offset = 2, so only the high-half read can tell them apart. No existing test or source besides the header changes.

Tests: startup_indexed_indirect_guard_check proves the guard's 2 accepted draws word for word (including word 6 against a sentinel) and 10 refusals on WARP/hardware. The five *_mut controls prove each clause is needed. CI at this head: Linux ran the Python model. The pr filter (CMakePresets.json) does not include startup_indexed_indirect_guard*, so the D3D12 evidence is the author's WARP and RTX 4050 dev + release runs (7/7).

**@⁠lukaslw-lab** on GitHub · 2026-12-01 02:42 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3661#pullrequestreview-6124219860) · **approved these changes** Reviewed `2d813325` by reading the diff; not built or run. **Verdict: approve.** The two `Store3` writes produce the same seven-word record as before, and the new D3D12 check pins every word. No MUST, no SHOULD. - Checked, no finding: - Layout (`src/startup_stubs/indexed_indirect_guard.h:41-43`): word 1 is the base-vertex root constant, and words 2–6 are `D3D12_DRAW_INDEXED_ARGUMENTS` (count, instances, start index, base vertex, start instance). This matches the 24-byte command signature in `d3d12_draw.inl:2609-2611` and the old `Store`/`Store4`/`Store` sequence. `Store(0, 1)` still comes last, so a refused draw leaves the count at 0. - Production impact of the dropped store: the guard returns unless `start_instance == 0`, and the runtime writes into a fresh committed default-heap buffer per validation (`d3d12_draw.inl:2620-2628`), which D3D12 zero-fills. So word 6 was 0 in real runs either way. The fix removes the reliance on zero-fill, and the sentinel-filled test buffer is what exposed the dropped store. - The `arg_offset & ~3u` upload in the test is a fixture change only. It makes the aligned-down `Load4` read a valid record, so the arg-align control can actually be killed. The guard keeps its own `(arg_offset & 3)` refusal. - Each of the five CMake mutations removes one guard clause that this PR does not touch and runs on WARP with the case that clause must refuse. The u16 mutant uses `valid_u16_high_low` with `index_offset = 2`, so only the high-half read can tell them apart. No existing test or source besides the header changes. Tests: `startup_indexed_indirect_guard_check` proves the guard's 2 accepted draws word for word (including word 6 against a sentinel) and 10 refusals on WARP/hardware. The five `*_mut` controls prove each clause is needed. CI at this head: Linux ran the Python model. The `pr` filter (`CMakePresets.json`) does not include `startup_indexed_indirect_guard*`, so the D3D12 evidence is the author's WARP and RTX 4050 dev + release runs (7/7).
github-bot changed target branch from main to gh-pr/3661/base 2026-12-01 03:19:43 +00:00
github manually merged commit 2d813325d7 into gh-pr/3661/base 2026-12-01 03:19:45 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.