GPU, Shaders: clamp view/sampler heap indices to dedicated null sentinels and bound StrideTable (#95) #144

Open
github-bot wants to merge 2 commits from github-bot/gh-144 into main AGit
Owner

@⁠ESAT586 on GitHub · 2026-11-25 16:48 UTC · original
ESAT586:belobug/fix-amd-heap-bounds-and-staging-uaf → main · +362 −64 · 11 files · 2 commits · open

Addresses #95.

Root Causes Found in Code

  1. Unclamped sampler heap indices (crates/victory-shader/src/emit/shader.rs): Shader::heap_handle() skipped clamping whenever sampler == true (!sampler), allowing out-of-bounds sampler indices to index past the end of SamplerDescriptorHeap.
  2. Sampler heap sizing & missing null sentinel (crates/victory-gpu/src/descriptor/host.rs): Smaller sampler heaps were not padded to SAMPLERS (2048), and capped sampler heaps handed out the final slot (2047) through HostHeap::dynamic().
  3. View clamp sentinel collided with dynamic slot allocator (crates/victory-gpu/src/descriptor/host.rs): On Tier-3 view heaps (1_245_184 + 1_048_576 == 2_293_760 == OWN_SLOTS), HostHeap::dynamic() could allocate slot OWN_SLOTS - 1 (2_293_759) — the exact slot OOB view accesses clamped to. When a temporary dynamic slot (e.g., constant_at CBV or buffer view) was released without being nulled, clamped OOB image/buffer loads read a stale or mismatched descriptor.
  4. Hardcoded OWN_SLOTS clamp & short StrideTable on smaller/1M-capped heaps (crates/victory-gpu/src/host.rs, crates/victory-gpu/src/descriptor/host.rs, crates/victory-shader/src/emit/mod.rs):
    • Shader::heap_handle() always clamped views to OWN_SLOTS - 1 (2_293_759), which is 1.29M descriptors past the end of a PORTABLE_VIEWS (1_000_000) heap.
    • StrideTable::new() allocated COUNTS_AT + len dwords instead of covering up to the clamp sentinel, allowing slot_dword reads at OWN_SLOTS - 1 to overrun the root SRV on smaller or capped heaps.
  5. Misleading DRED resource names on pooled staging slabs (crates/victory-gpu/src/texpool.rs, crates/victory-gpu/src/texture.rs): texture.rs overwrote the D3D12 debug name of shared texpool::staging slabs on every texture upload, making unrelated textures show up in DRED/RGD reports for the shared slab. (Audit of Staging lifetime confirmed actual GPU lifetime is already guarded by Arc<Staging> in HostList/Early/InFlight and Staging::drop -> retire((buffer, slot)) with KEEP_FRAMES = 32.)

Changes

  • victory-shader (emit/mod.rs, emit/shader.rs, emit/host.rs, emit/split_tests.rs, emit/image_tests.rs):
    • Extended Target with view_slots and sampler_slots (plus Target::own_slots(), clamp_view_last(), clamp_sampler_last()) and widened Target::key() to 16 bytes so shader cache entries stay separated across 2.29M Tier-3 vs 1M-capped heaps.
    • Added thread-local RAII ClampGuard around lower::lower() in emit() (no edits to lower.rs).
    • Updated Shader::heap_handle() to clamp both view and sampler indices via dx.op.binary.i32 (umin, opcode 40) against the target's sentinel slots.
    • Added GPU + WARP + dxil.dll regression test out_of_bounds_view_and_sampler_indices_clamp_to_sentinel_slots.
  • victory-gpu (host.rs, descriptor/host.rs, shader_cache.rs, texpool.rs, texture.rs):
    • Host::target() probes CreateDescriptorHeap(CBV_SRV_UAV, VIEW_SLOTS, SHADER_VISIBLE) once to choose VIEW_SLOTS (2_294_784) vs PORTABLE_VIEWS (1_000_000) and sets sampler_slots = SAMPLER_SLOTS (2048).
    • Permanently reserved memory_first - 1 (view) and SAMPLERS - 1 (sampler) as null sentinels by reducing DYNAMIC to (1 << 20) - 1, DYNAMIC_CAPPED to (1 << 16) - 1, always backing sampler heaps with SAMPLERS descriptors, and reserving SAMPLERS - 1 as a null point sampler.
    • Sized StrideTable to COUNTS_AT + len.max(COUNTS_AT) (zero-initialized) so slot_dword reads at the sentinel never overrun the root SRV.
    • Logged actual heap.GetDesc().NumDescriptors in GPU_RANGES for accurate DRED diagnostics.
    • Named shared texpool::staging slabs once on creation (texpool staging slab ({bytes})) while preserving per-texture debug names on dedicated non-pooled upload buffers.

Verification

  • victory-shader (out_of_bounds_view_and_sampler_indices_clamp_to_sentinel_slots): 1 passed, 0 failed (RTX 3060 Ti + WARP + dxil.dll validation).
  • victory-gpu on RTX 3060 Ti (descriptor::, shader_cache::, texture::, skipping pre-existing 1M-cap #130 test): 24 passed, 0 failed.
  • victory-gpu with D3D12 Debug Layer (VICTORY_D3D12_DEBUG=1 RUST_LOG=warn, --features diagnostics): 6 passed, 0 failed, 0 D3D12 warnings/errors.
  • victory-gpu on WARP (VICTORY_D3D12_WARP=1, --features diagnostics, full descriptor::, shader_cache::, texture:: including dynamic_slots_are_held_until_released): 25 passed, 0 failed.
  • cargo clippy -p victory-shader -p victory-gpu --all-targets --all-features --locked: 0 warnings in modified code.
  • cargo xtask policy --staged: asset policy: 10 files ok.
**@⁠ESAT586** on GitHub · 2026-11-25 16:48 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/144) `ESAT586:belobug/fix-amd-heap-bounds-and-staging-uaf` → `main` · +362 −64 · 11 files · 2 commits · open Addresses #95. ### Root Causes Found in Code 1. **Unclamped sampler heap indices (`crates/victory-shader/src/emit/shader.rs`)**: `Shader::heap_handle()` skipped clamping whenever `sampler == true` (`!sampler`), allowing out-of-bounds sampler indices to index past the end of `SamplerDescriptorHeap`. 2. **Sampler heap sizing & missing null sentinel (`crates/victory-gpu/src/descriptor/host.rs`)**: Smaller sampler heaps were not padded to `SAMPLERS` (`2048`), and capped sampler heaps handed out the final slot (`2047`) through `HostHeap::dynamic()`. 3. **View clamp sentinel collided with dynamic slot allocator (`crates/victory-gpu/src/descriptor/host.rs`)**: On Tier-3 view heaps (`1_245_184 + 1_048_576 == 2_293_760 == OWN_SLOTS`), `HostHeap::dynamic()` could allocate slot `OWN_SLOTS - 1` (`2_293_759`) — the exact slot OOB view accesses clamped to. When a temporary dynamic slot (e.g., `constant_at` CBV or buffer view) was released without being nulled, clamped OOB image/buffer loads read a stale or mismatched descriptor. 4. **Hardcoded `OWN_SLOTS` clamp & short `StrideTable` on smaller/1M-capped heaps (`crates/victory-gpu/src/host.rs`, `crates/victory-gpu/src/descriptor/host.rs`, `crates/victory-shader/src/emit/mod.rs`)**: - `Shader::heap_handle()` always clamped views to `OWN_SLOTS - 1` (`2_293_759`), which is 1.29M descriptors past the end of a `PORTABLE_VIEWS` (`1_000_000`) heap. - `StrideTable::new()` allocated `COUNTS_AT + len` dwords instead of covering up to the clamp sentinel, allowing `slot_dword` reads at `OWN_SLOTS - 1` to overrun the root SRV on smaller or capped heaps. 5. **Misleading DRED resource names on pooled staging slabs (`crates/victory-gpu/src/texpool.rs`, `crates/victory-gpu/src/texture.rs`)**: `texture.rs` overwrote the D3D12 debug name of shared `texpool::staging` slabs on every texture upload, making unrelated textures show up in DRED/RGD reports for the shared slab. (Audit of `Staging` lifetime confirmed actual GPU lifetime is already guarded by `Arc<Staging>` in `HostList`/`Early`/`InFlight` and `Staging::drop -> retire((buffer, slot))` with `KEEP_FRAMES = 32`.) ### Changes - **`victory-shader` (`emit/mod.rs`, `emit/shader.rs`, `emit/host.rs`, `emit/split_tests.rs`, `emit/image_tests.rs`)**: - Extended `Target` with `view_slots` and `sampler_slots` (plus `Target::own_slots()`, `clamp_view_last()`, `clamp_sampler_last()`) and widened `Target::key()` to 16 bytes so shader cache entries stay separated across 2.29M Tier-3 vs 1M-capped heaps. - Added thread-local RAII `ClampGuard` around `lower::lower()` in `emit()` (no edits to `lower.rs`). - Updated `Shader::heap_handle()` to clamp **both** view and sampler indices via `dx.op.binary.i32` (`umin`, opcode `40`) against the target's sentinel slots. - Added GPU + WARP + `dxil.dll` regression test `out_of_bounds_view_and_sampler_indices_clamp_to_sentinel_slots`. - **`victory-gpu` (`host.rs`, `descriptor/host.rs`, `shader_cache.rs`, `texpool.rs`, `texture.rs`)**: - `Host::target()` probes `CreateDescriptorHeap(CBV_SRV_UAV, VIEW_SLOTS, SHADER_VISIBLE)` once to choose `VIEW_SLOTS` (`2_294_784`) vs `PORTABLE_VIEWS` (`1_000_000`) and sets `sampler_slots = SAMPLER_SLOTS` (`2048`). - Permanently reserved `memory_first - 1` (view) and `SAMPLERS - 1` (sampler) as null sentinels by reducing `DYNAMIC` to `(1 << 20) - 1`, `DYNAMIC_CAPPED` to `(1 << 16) - 1`, always backing sampler heaps with `SAMPLERS` descriptors, and reserving `SAMPLERS - 1` as a null point sampler. - Sized `StrideTable` to `COUNTS_AT + len.max(COUNTS_AT)` (zero-initialized) so `slot_dword` reads at the sentinel never overrun the root SRV. - Logged actual `heap.GetDesc().NumDescriptors` in `GPU_RANGES` for accurate DRED diagnostics. - Named shared `texpool::staging` slabs once on creation (`texpool staging slab ({bytes})`) while preserving per-texture debug names on dedicated non-pooled upload buffers. ### Verification - `victory-shader` (`out_of_bounds_view_and_sampler_indices_clamp_to_sentinel_slots`): **1 passed, 0 failed** (RTX 3060 Ti + WARP + `dxil.dll` validation). - `victory-gpu` on RTX 3060 Ti (`descriptor::`, `shader_cache::`, `texture::`, skipping pre-existing 1M-cap `#130` test): **24 passed, 0 failed**. - `victory-gpu` with D3D12 Debug Layer (`VICTORY_D3D12_DEBUG=1 RUST_LOG=warn`, `--features diagnostics`): **6 passed, 0 failed, 0 D3D12 warnings/errors**. - `victory-gpu` on WARP (`VICTORY_D3D12_WARP=1`, `--features diagnostics`, full `descriptor::`, `shader_cache::`, `texture::` including `dynamic_slots_are_held_until_released`): **25 passed, 0 failed**. - `cargo clippy -p victory-shader -p victory-gpu --all-targets --all-features --locked`: **0 warnings** in modified code. - `cargo xtask policy --staged`: **`asset policy: 10 files ok`**.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:36:35 +00:00
Author
Owner

@⁠KernelSmoker on GitHub · 2026-11-25 18:32 UTC · original

Workspace compile check on PR head 7210c4c (cargo check --workspace --all-targets --locked --offline --target x86_64-pc-windows-gnu, Rust 1.98.1) failed when building apps/victory:

error[E0063]: missing fields `sampler_slots` and `view_slots` in initializer of `victory_shader::emit::Target`
   --> apps/victory/src/translate.rs:180:18
    |
180 |         target = emit::Target {
    |                  ^^^^^^^^^^^^ missing `sampler_slots` and `view_slots`

Using ..target there preserves view_slots and sampler_slots from either emit::Target::ANY or Host::new(false)?.target() when --pipelines is set. All other crates (victory-shader, victory-gpu, victory-hle, etc.) passed cargo check.

**@⁠KernelSmoker** on GitHub · 2026-11-25 18:32 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/144#issuecomment-7171044335) Workspace compile check on PR head `7210c4c` (`cargo check --workspace --all-targets --locked --offline --target x86_64-pc-windows-gnu`, Rust 1.98.1) failed when building `apps/victory`: ``` error[E0063]: missing fields `sampler_slots` and `view_slots` in initializer of `victory_shader::emit::Target` --> apps/victory/src/translate.rs:180:18 | 180 | target = emit::Target { | ^^^^^^^^^^^^ missing `sampler_slots` and `view_slots` ``` Using `..target` there preserves `view_slots` and `sampler_slots` from either `emit::Target::ANY` or `Host::new(false)?.target()` when `--pipelines` is set. All other crates (`victory-shader`, `victory-gpu`, `victory-hle`, etc.) passed `cargo check`.
Author
Owner

@⁠ESAT586 on GitHub · 2026-11-25 20:27 UTC · original

Good catch, @⁠KernelSmoker — thanks for running the full workspace check! Fixed in $sha\ by preserving ..target\ in \pps/victory/src/translate.rs:180\ and verified \cargo check --workspace --all-targets --locked\ + \cargo xtask policy --staged\ pass cleanly.

**@⁠ESAT586** on GitHub · 2026-11-25 20:27 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/144#issuecomment-7173709942) Good catch, @⁠KernelSmoker — thanks for running the full workspace check! Fixed in \$sha\ by preserving \..target\ in \pps/victory/src/translate.rs:180\ and verified \cargo check --workspace --all-targets --locked\ + \cargo xtask policy --staged\ pass cleanly.
Author
Owner

@⁠zGLados on GitHub · 2026-11-25 20:46 UTC · original

Tested in the game on an RX 7700 XT 12 GB (RDNA3), Windows 11, diag build with DRED: on 000ffdd this PR stopped a device loss that came every run (page faults inside freed staging/heap allocations, see #26). Two runs with it, 12 and 16 minutes, no device removal; also merged with #138 (adrianmjim's test/main-144-138).

Same build note as @⁠KernelSmoker above: against current main it needs ..target in the emit::Target { lanes_min, lanes_max } initializer in apps/victory/src/translate.rs (the new view_slots / sampler_slots fields).

**@⁠zGLados** on GitHub · 2026-11-25 20:46 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/144#issuecomment-7174179748) Tested in the game on an RX 7700 XT 12 GB (RDNA3), Windows 11, diag build with DRED: on `000ffdd` this PR stopped a device loss that came every run (page faults inside freed staging/heap allocations, see #26). Two runs with it, 12 and 16 minutes, no device removal; also merged with #138 (adrianmjim's `test/main-144-138`). Same build note as @⁠KernelSmoker above: against current `main` it needs `..target` in the `emit::Target { lanes_min, lanes_max }` initializer in `apps/victory/src/translate.rs` (the new `view_slots` / `sampler_slots` fields).
Author
Owner

@⁠ESAT586 on GitHub · 2026-11-25 21:50 UTC · original

Awesome news on the RX 7700 XT 12 GB — 12 and 16 minute runs with zero device removals! Thanks for testing in-game, @⁠zGLados. Commit \646733a\ (just pushed) includes the ..target\ fix in \pps/victory/src/translate.rs:180\ so the full workspace builds cleanly.

**@⁠ESAT586** on GitHub · 2026-11-25 21:50 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/144#issuecomment-7175606885) Awesome news on the RX 7700 XT 12 GB — 12 and 16 minute runs with zero device removals! Thanks for testing in-game, @⁠zGLados. Commit \646733a\ (just pushed) includes the \..target\ fix in \pps/victory/src/translate.rs:180\ so the full workspace builds cleanly.
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-26 00:05 UTC · original

Tested on an RX 6950 XT (RDNA2), Windows 11 build 26300, Rust 1.98.1, with this PR (646733a) merged on main 4f62c2a, release lib tests:

main with this PR
victory-gpu 112 passed, 1 failed 112 passed, 2 failed
victory-shader 275 passed, 2 failed 276 passed, 2 failed
victory-hle 97 passed, 5 failed 97 passed, 5 failed

None of the failures is new:

  • victory-gpu: the extra failure is watch::tests::written_pages_are_reported_to_every_asker, the flake of #102. The other is list::tests::lists_are_cut_at_waits_that_follow_work, which fails on every release run on main (#210).
  • victory-shader: the two failures are the RDNA2 wave-size tests that #110 and #204 fix.
  • victory-hle: the five xmem tests fixed by #93.

Merged together with #110, #214 and one more local branch of mine, victory-shader gives 279 passed and 0 failed in two runs out of two. This PR conflicts with #204 in emit/mod.rs.

Why it matters on RDNA2: the RX 6950 XT here lost the device (0x887A0007) 38 to 77 s after full-world rendering began, in both runs I have logs for (main and 000ffdd, Series X profile). #151 on an RX 6600 lost it at the same place. On #26 (RX 7700 XT, RDNA3), 000ffdd lost the device at about 7.5 min, while 000ffdd + this PR ran 12 min, and main + this PR + #138 ran 16 min, both with no loss. I have built main + this PR for that machine; it has not been run in the game yet, and I will report what it does.

harness machine: rx6950xt-w11 | AMD Radeon RX 6950 XT (RDNA2), driver 32.0.21045.29003 | 32 GB RAM | Windows 11 build 26300

AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-26 00:05 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/144#issuecomment-7178383912) Tested on an RX 6950 XT (RDNA2), Windows 11 build 26300, Rust 1.98.1, with this PR (`646733a`) merged on `main` `4f62c2a`, release lib tests: | | `main` | with this PR | |---|---|---| | `victory-gpu` | 112 passed, 1 failed | 112 passed, 2 failed | | `victory-shader` | 275 passed, 2 failed | 276 passed, 2 failed | | `victory-hle` | 97 passed, 5 failed | 97 passed, 5 failed | None of the failures is new: - `victory-gpu`: the extra failure is `watch::tests::written_pages_are_reported_to_every_asker`, the flake of #102. The other is `list::tests::lists_are_cut_at_waits_that_follow_work`, which fails on every release run on `main` (#210). - `victory-shader`: the two failures are the RDNA2 wave-size tests that #110 and #204 fix. - `victory-hle`: the five `xmem` tests fixed by #93. Merged together with #110, #214 and one more local branch of mine, `victory-shader` gives 279 passed and 0 failed in two runs out of two. This PR conflicts with #204 in `emit/mod.rs`. Why it matters on RDNA2: the RX 6950 XT here lost the device (`0x887A0007`) 38 to 77 s after full-world rendering began, in both runs I have logs for (`main` and `000ffdd`, Series X profile). #151 on an RX 6600 lost it at the same place. On #26 (RX 7700 XT, RDNA3), `000ffdd` lost the device at about 7.5 min, while `000ffdd` + this PR ran 12 min, and `main` + this PR + #138 ran 16 min, both with no loss. I have built `main` + this PR for that machine; it has not been run in the game yet, and I will report what it does. harness machine: rx6950xt-w11 | AMD Radeon RX 6950 XT (RDNA2), driver 32.0.21045.29003 | 32 GB RAM | Windows 11 build 26300 AI-assisted (Claude Code); no game data.
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/144/head:github-bot/gh-144'
git switch 'github-bot/gh-144'

Merge

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