Skip to content

Shaders, GPU: a heap index computed from data stays inside the heap - #483

Closed
Vaximous wants to merge 1 commit into
Marvisak:mainfrom
Vaximous:gpu/guard-heap-indices
Closed

Vaximous wants to merge 1 commit into
Marvisak:mainfrom
Vaximous:gpu/guard-heap-indices

Conversation

@Vaximous

Copy link
Copy Markdown
Contributor

Refs #71, #264, #97 (one cause of the NVIDIA world DEVICE_HUNG; not shown to be the only one).

What

Every descriptor-heap index the lowering computes is bounded: an index at or above the bound heap's guard slot reads the guard slot instead. The host reserves one null slot at the end of each view heap for this and passes its index to the shader in a new user-data dword.

Why

Translated programs that load descriptors bindless read an index from a buffer, shift it left by 5 (4 for samplers), add it to the heap or table address and s_load_dwordx4 the descriptor there. Lower::heap_index turned that address into createHandleFromHeap(addr >> 5) with no bound. When the host's copy of the buffer is stale or zero, the index can be anything up to 2^27, far past the host view heap (PORTABLE_VIEWS = 1,000,000 slots on NVIDIA).

An index past the heap is undefined in D3D12, and NVIDIA does not fault on it: measured on an RTX 4070 Ti with the synthetic test below, a store through heap index 2^25 lands in slot 0: the hardware wraps the index onto an unrelated slot. In the title the wrapped slot holds a descriptor of another kind, the dispatch never finishes, and the driver removes the device with DXGI_ERROR_DEVICE_HUNG and no DRED page fault.

What pointed there, in the title on this machine (observed on a local integration branch of main plus many open PRs): the programs that hang have no barriers; VICTORY_SHADER_LOOP_LIMIT=64 still hangs; skipping one of them moves the hang to another program of the same kind (bindless descriptor fetch).

Changes

  • victory-shader, emit/lower.rs: heap_index ends in heap_guard(index, sampler): idx <u guard ? idx : guard (one compare, one select). The guard is read once per program from a user-data dword placed after every dword the program's root parameters reach, recorded as a UserData entry with root type ResourceHeap (view heap) or SamplerHeap. user() is split so the guard read shares the user-data load (user_dword).
  • emit/mod.rs: the binding notes and UserData::root doc describe the new entry. This is a change to the binding scheme the module notes say is announced: a host that fills user data must fill these entries (user_data_dwords() already covers them, so root-signature sizes stay consistent).
  • victory-gpu, descriptor/host.rs: a view heap gets one slot more after its DYNAMIC slots (null from the start, never made); HostHeap::guard_slot() returns it. A sampler heap's guard is its last slot (any sampler reads safely). The capped-heap path reserves the slot inside PORTABLE_VIEWS.
  • draw.rs: user_dword() fills the guard entries from the bound heaps at draw/dispatch (and in the device-removed description); other entries go through user_value as before.
  • Test harness (emit/host.rs): the draw/dispatch view heaps keep a null slot after their views; guard() gives the test helpers the value to fill. diff_tests passes u32::MAX (its views are all in range).

Cost: one compare and select per bindless descriptor fetch, and one user-data dword per stage that does one.

Tested

Machine: RTX 4070 Ti, Ryzen 9 7900X, Windows 11 26300.

  • New test emit::valu_tests::a_heap_index_past_the_heap_reads_the_guard_slot: a one-thread program loads a UAV descriptor at table address + (data << 5) and stores through it, for index 0, 1 (the guard), 2^25 and 0x07FF_FFFF. With the select removed from heap_guard (temporary edit) it fails on hardware: index 0x2000000 writes 7 into slot 0. With the change it passes on hardware and WARP.
  • cargo test -p victory-shader -- --test-threads=1: 70 passed, 0 failed (main: 69 passed; the extra one is the new test).
  • cargo test -p victory-gpu -- --test-threads=1: 43 passed, 1 ignored, 0 failed (same as main).
  • cargo clippy -p victory-shader -p victory-gpu --all-targets: no new warnings (the one existing warning is on main too).
  • cargo xtask policy --staged: ok.

Title (build 1.0.2.1, diag build, scripted Enter presses, VICTORY_D3D12_DRED=1):

  • Local integration branch (main + many open PRs) with this change (where the change was developed): the title ran 220 s in the world with no device removal. The same branch without the change hung with DEVICE_HUNG within seconds of world entry on every run.
  • This branch (plain main + this change), two runs: in the first, the world started about 160 s in and the run reached its 240 s limit (about 80 s in the world) with no device removal. In the second, the device was removed with DEVICE_HUNG (0x887A0006) about 45 s after the world started. DRED breadcrumbs stop at a mesh draw (DispatchMesh), not at one of the bindless compute dispatches this change is about. The guard dwords held the expected values (view heap capped at 1,000,000: guard 999,999; sampler guard 127).
  • Plain main, one run on the same machine for comparison: DEVICE_HUNG about 55 s after the world started, with no breadcrumbs.

So on plain main this change alone does not make the world stable on NVIDIA: another hang remains (mesh path; other open PRs, for example #89 for an invalid render-target view on a 16-bit packed format, address other NVIDIA failures). This PR removes one cause, the wrapped heap index, which the synthetic test reproduces in isolation. Scripted keys only; frame rate in the world is very low here (seconds per frame), so the time in the world is short in every run.

Not tested

AI use: written with Claude Code (Claude Opus 5.5 executing, Claude Fable 5.2 planning), tested on the submitter's hardware.

🤖 Generated with Claude Code

Translated programs that load their descriptors bindless add an index
read from a buffer (shifted to the console's descriptor size) to the
heap or table address, and the lowering turned that address into a
ResourceDescriptorHeap / SamplerDescriptorHeap index with no bound. When
the host's copy of that buffer is stale or zero, the index can be
anything up to 2^27, far past the host heap. An index past a heap is
undefined: on NVIDIA (RTX 4070 Ti) a store through index 2^25 lands in
slot 0, so the hardware wraps it onto an unrelated slot. In a title the
wrapped slot holds a descriptor of another kind, the dispatch never
finishes, and the driver removes the device with DXGI_ERROR_DEVICE_HUNG
and no page fault. Loop limits do not help: the programs that hang have
no barriers and no long loops.

Every heap index the lowering computes now goes through a guard: an
index at or above the bound heap's guard slot reads that slot instead
(an unsigned compare and a select). The guard slot comes in a user-data
dword of its own, after every dword the program's root parameters reach
(a UserData entry of root type ResourceHeap / SamplerHeap). The host
gives each view heap one slot more, after its dynamic slots, null from
the start and never made; a sampler heap's guard is its last slot (any
sampler reads safely). The pipeline's root constants already cover
user_data_dwords(), and draws and dispatches fill the new dwords from
the bound heaps.

Test: a_heap_index_past_the_heap_reads_the_guard_slot (without the
select, the store at index 2^25 lands in slot 0 on NVIDIA). The test
harness keeps a null slot after its views; the helpers fill the guard
dwords.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DaviTostes

Copy link
Copy Markdown

Verified 65a161d on Arch Linux x86_64, Windows MSVC release build under Wine 11.22 (victory-gpu, --test-threads=1): 43 passed, 0 failed, 1 ignored.

Not covered: a heap index computed by the title's data actually clamped at draw time (no game data here).

@miniongk

Copy link
Copy Markdown

Confirmed on an RTX 5080, driver 621.24, Windows 11 26300, using a local Xbox Series 1.0.2.1 copy at 65a161d.

  • emit::valu_tests::a_heap_index_past_the_heap_reads_the_guard_slot: passed in 0.28 s.
  • cargo test --workspace: 265 passed, 0 failed, 4 ignored.
  • The native run still ended in DXGI_ERROR_DEVICE_HUNG; DRED implicated indirect execution and mesh dispatch, consistent with the remaining hang your report describes.

The synthetic guard test works here, but the world is not stable on this hardware yet. Only aggregate counts and error classes shared. AI-assisted (Kimi Code).

@erickbobd

Copy link
Copy Markdown

Independent synthetic check on RTX 5070 Ti, NVIDIA 617.14, Windows 11 25H2 26200.9974, Rust 1.98.1 / MSVC x64, with the Windows SDK 10.0.26100.0 dxil.dll explicitly selected.

  • a_heap_index_past_the_heap_reads_the_guard_slot passes on hardware and WARP.
  • Negative control: removing only the final select from heap_guard makes the hardware test fail at index 0x02000000: slot 0 receives 7, expected 0. This independently reproduces the wrap on Blackwell. Restoring the select passes.
  • With GPU: drop draws whose fresh tables cannot be made; 2^18 dynamic slots #336's capped sizing, the heap has 1,000,000 slots, 65,536 dynamic slots, and guard 999,999. A synthetic allocation test consumes the whole dynamic range and verifies that the guard cannot be allocated. Replacing the guard calculation with count + DYNAMIC fails: 1,196,607 versus 999,999.

These were checked on local integration 8693373: main d2a4bae + #493 (including #336), #483 at 65a161d3, #189, #89, #205, #206, #469, #459, #454 and #211. Serial library suites after both mutations were restored: victory-gpu 55 passed / 1 ignored; victory-shader 88 passed. These counts include the other PRs and an additional local allocation test.

No world-stability claim: the local title probe was stopped during loading for low system commit headroom. No game files, captures or logs attached. AI-assisted (Codex).

@wwwwwwww3333333

Copy link
Copy Markdown
Contributor

RDNA2 result, including the negative control. On an AMD RX 6750 XT (RDNA2) / Windows 11 / Rust 1.98.1 MSVC, with this PR at 65a161d:

  • a_heap_index_past_the_heap_reads_the_guard_slot passes on hardware and WARP. There was no no SM 6.6 hardware device; skipped line under --nocapture, so it ran on the card. cargo test -p victory-shader -- --test-threads=1: 70 passed. cargo test -p victory-gpu: 43 passed, 1 ignored.
  • Negative control, same as @erickbobd's: I removed only the final select in heap_guard, so index is used unclamped. The hardware stage then fails with dispatch failed: device removed, and the message is the standard DXGI_ERROR_DEVICE_HUNG text ("the GPU will not respond to more commands, most likely because of an invalid command passed by the calling application"). That reproduced 2 of 2 runs. There was no visible driver reset; the device was lost only inside the test process. Restoring the select passes again (exit 0).

So where NVIDIA wraps an out-of-heap index onto slot 0, RDNA2 loses the device. The panic doesn't say which of the out-of-range cases triggered it. This doesn't prove the AMD world crashes (#59, #304, #557) come from this, but it shows that an unbounded heap index is enough to remove the device on AMD too, not only to corrupt a slot on NVIDIA.

Not runtime-tested with game files. AI-assisted (Claude did the analysis and the one-line control change; I ran the tests on my machine).

@iurysf

iurysf commented Nov 24, 2026

Copy link
Copy Markdown

On an RTX 4070, GBV on main d2a4bae reports the case this PR guards: id 936, Descriptor heap index out of bounds via direct indexing: Heap Index From HeapStart: [117506306], Num Descriptor Entries: 1000000, Shader Stage: PIXEL. Together with #493, the host device removal we hit every run on main is gone for 300 s (details in my comment on #493). Its tests pass on this machine. I could not isolate this PR in game without #493.

@etcetera-yt

Copy link
Copy Markdown

#483 puts a view heap's guard slot right after the dynamic slots (extra = DYNAMIC + 1,
guard_slot() = count + DYNAMIC). On main that is safe: HostHeap::dynamic hands out slots from a pool sized
DYNAMIC, so the guard is never given to a table.

#86 replaces that pool with one sized from the heap's spare slots (spare = extra). With both merged, the pool is
DYNAMIC + 1 slots long, and dynamic can return the guard slot itself; a table made there overwrites the guard,
so data-computed indices that #483 sends to the guard would read that table instead of a null view.

Whichever of the two lands second needs to keep the guard out of the pool. In my local merge I marked the last
spare slot of a view heap as taken when the heap is made (next: Mutex::new((0, taken)) with taken.last_mut()
set), and dynamic_slots_are_held_until_released and the rest of the victory-gpu tests still pass
(Windows 10 19045, RTX 5060 Ti).

AI use: Claude Code, Claude Opus 5.5.

@Vaximous

Copy link
Copy Markdown
Contributor Author

Thanks, that's right, and I hit the same thing in a local merge of both.

Whichever of #86 and this PR lands second should carry that change. If #86 merges first, I'll rebase this PR onto it and add the reservation and the test here.

AI use: Claude Code (Claude Opus 5.5).

r4tedx added a commit to r4tedx/VIctory that referenced this pull request Nov 24, 2026
…ys inside the heap)

Vaximous's Marvisak#483: every descriptor-heap index the lowering computes is
bounded by the bound heap's guard slot (an index at or above it reads
it), which the host passes in a user-data dword of its own.

Conflict resolutions:
- crates/victory-gpu/src/descriptor/host.rs: this tree's sizing is kept
  (Marvisak#86/Marvisak#83/Marvisak#251, and 2^18 dynamic slots on a capped heap); a view heap
  is made one slot larger, the guard slot, after its dynamic slots and
  outside the pool HostHeap::dynamic hands out (`spare` does not count
  it). On a capped heap that is 737,855 title slots, 262,144 dynamic
  ones and the guard at slot 999,999. Marvisak#483 had the guard at
  `count + DYNAMIC` with the pool sized by a constant; here the pool is
  sized by `spare` (Marvisak#86), so guard_slot() is `count + spare`.
- A sampler heap's guard is its last slot, dynamic ones included
  (`count + spare - 1`), not Marvisak#483's `count - 1`: Marvisak#86 makes sampler
  tables afresh in a capped sampler heap's dynamic slots, which
  `count - 1` would have clamped away.
- crates/victory-shader/src/emit/lower.rs, Lower::new: both fields are
  kept (`dead`, `guard_slots`).
- crates/victory-shader/src/emit/valu_tests.rs: this tree's tests and
  Marvisak#483's new one are both kept.

Changes beyond the PR:
- emit/lower.rs, fetched_index (Marvisak#307's per-lane heap index, which
  Marvisak#483 did not know): it goes through heap_guard too. It is an address
  from a lane's data, as heap_index's.
- emit/image_tests.rs (Marvisak#160's harness): the user data it fills gets the
  guard entries (host::guard).
- descriptor/host.rs: tests the_guard_slot_is_past_every_dynamic_slot,
  and the guard's place in a_capped_view_heap_keeps_its_dynamic_slots.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
r4tedx added a commit to r4tedx/VIctory that referenced this pull request Nov 24, 2026
Seven more upstream PRs are in (Marvisak#493, Marvisak#483, Marvisak#206, Marvisak#469, Marvisak#459, Marvisak#454,
Marvisak#579) and the wave64 stack is at Marvisak#439's head 965ce30. Records how each
was combined with what the tree had, that a capped view heap keeps
2^18 dynamic slots and why, Marvisak#189 and Marvisak#578 as left out, and a new
section with testers' reports from the title: the set without the
world-entry hang on three NVIDIA cards and how this tree differs from
it, VICTORY_SKIP_WAVE64 as the switch to try, and Marvisak#136 bisected as
making every frame black.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@walterwallaceson

Copy link
Copy Markdown

Windows 10 data point for #493 (c48b240) + #483 (65a161d): no device removal on an RTX 3070.

Setup: Windows 10 22H2 (19045.6466), RTX 3070, NVIDIA 621.14, Ryzen 5 5600X, release build, Agility SDK 1.620.2 via #175. Base stack: #175, #233 + #320 + #439 (f653ea4), #55, #89, #336 (e8d3b64), #334 (af2aecf). This GPU caps the view heap at 1,000,000, so #336/#493 take the 2^16 dynamic range. #493 and #483 conflict in descriptor/host.rs; I resolved it as described on #493: #493's DYNAMIC / MIN_DYNAMIC selection plus one guard slot (make(count + DYNAMIC + guard), capped branch PORTABLE_VIEWS - (MIN_DYNAMIC + guard)), the guard kept out of the dynamic pool, and guard_slot() returning count + self.dynamic.

base stack + #493 + #483
device removed yes, ~2–2.5 min into the world (0x887A0006 / DEVICE_HUNG) no, none in ~5.5 min in the world
what renders first mission title card, cutscene subtitles over a black scene the same, then the opening credits text and 2D credit art over the still-black 3D scene, for several minutes
no free dynamic descriptor slots ~8 s into the world ~2 min into the world
draws dropped (no dynamic slots) up to 16384–65536 up to 65536
  • Frame rate in the world 0.2–1.8 fps; 60 slow host submission lines, 0 host pipelines refused, 0 no host texture.
  • What ends the run now is the title, not the GPU: 0x80000003 (a breakpoint) at title +0x5149aec on 7 threads at once, all with the same stack (+0xa5db22, +0x5453ac7, +0x23d0f9a, …). The title then tries to upload a crash report, which VIctory's network policy blocks. I couldn't find that address in an issue yet.
  • cargo test --workspace: Shaders, GPU: a heap index computed from data stays inside the heap #483's a_heap_index_past_the_heap_reads_the_guard_slot fails here with "neither hardware nor WARP SM 6.6 device available", the same Windows 10 cause as the other valu_tests (Shader tests: use the Agility SDK runtime on Windows 10 (stacked on #175) #362). Otherwise only the known Windows 10 failures.

One run so far.

No game data in this report.

AI-assisted (Claude Code); everything above was run on my machine.

clypeal6 added a commit to clypeal6/VIctory that referenced this pull request Nov 24, 2026
resource::track takes no structure here, Parts carries options, the
guard user-data dword (Marvisak#483) gets the null slot past the views, and the
one-GPU-test-at-a-time lock is shared at module level.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fornorharry pushed a commit to NitricAcidDev/VIctory-Omega-Fork that referenced this pull request Nov 24, 2026
# Conflicts:
#	crates/victory-gpu/src/descriptor/host.rs
#	crates/victory-shader/src/emit/valu_tests.rs
This was referenced Nov 24, 2026
itsonlysimbo pushed a commit to itsonlysimbo/VIctory that referenced this pull request Nov 24, 2026
itsonlysimbo pushed a commit to itsonlysimbo/VIctory that referenced this pull request Nov 24, 2026
itsonlysimbo pushed a commit to itsonlysimbo/VIctory that referenced this pull request Nov 24, 2026
@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because another open pull request covers the same change.

See #1133. Heap indices past the host heap: main clamps every index to HEAP_VIEWS-1 / HEAP_SAMPLERS-1 and keeps the host heap that large (emit/shader.rs heap_handle). Only a host that caps its heap lower (NVIDIA) is left, and #1133 covers that with a translation-time bound instead of a new user-data dword per draw.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.