Skip to content

Shaders: heap indices from data clamped before the root views they index - #2558

Open
RomainKH wants to merge 2 commits into
Marvisak:mainfrom
RomainKH:shader/clamp-view-index
Open

RomainKH wants to merge 2 commits into
Marvisak:mainfrom
RomainKH:shader/clamp-view-index

Conversation

@RomainKH

@RomainKH RomainKH commented Nov 25, 2026 •

Copy link
Copy Markdown

Split out of #2342 at the maintainer's request (NEW-118 in #1633), on current main (63bcca4).

view_stride and view_addresses read root SRVs at a heap index taken from data: the view-stride table and the view addresses. They do this without clamping the index, as heap_handle and buffer_guard already do. Root views have no bounds, so an index past the table reads whatever memory follows it.

Change

  • Shaders: clamp_view_index clamps the index to HEAP_VIEWS - 1 before both reads. buffer_guard now uses the same helper; its behaviour is unchanged.
  • Host: the view-address buffer had 16 bytes per title and dynamic slot (address_slots, from GPU: view addresses for tables made afresh (dynamic slots) #1906: about 1.5 million here, against HEAP_VIEWS = 2^21), so even a clamped index could read past it. It now has address_slots().max(HEAP_VIEWS) entries, as the view-stride table already covers HEAP_VIEWS (write_stride). The entries past the dynamic slots are cleared when the buffer is made, so they read 0 whatever the new allocation held (brainbudt's review), and nothing writes them after. This is 32 MiB of upload memory instead of about 23.
  • Tests: three lowering tests on main (from ff47463, 54ab406 and 850b66f) called the Windows-only validator without a cfg, so the shader tests no longer built on Linux or macOS. They now go through a validate helper, as does ds_bpermute_wave64_preserves_32_lane_half (added on main since, cfg-gated but skipping silently without dxil.dll). Where CI is set (GitHub sets it), that helper fails if dxil.dll does not load instead of skipping, so a green run means the DXIL of these tests was validated. This is the separate first commit.
  • Layout: lower.rs is within a few KiB of the repository's 512 KiB limit on a file (xtask asset policy), and the new test would have pushed it over. So the test lives in emit/lower/tests/descriptors.rs, a child of lower's tests that uses their helpers. lower.rs is 521,225 bytes here.

Relation to open PRs:

Testing

  • New test heap_indices_from_data_are_clamped_before_root_view_reads, on synthetic GFX10 code:

    • a structured read through a heap descriptor clamps once more than a raw one (the stride lookup);
    • reading a descriptor's dwords 0 and 1 as data clamps twice.

    Without the two clamps it fails (left: 3, right: 4).

  • cargo test -p xtask (asset policy): passes.

  • macOS arm64, Rust 1.98.1: cargo test -p victory-shader --locked --lib gives 173 passed, 0 failed, 2 ignored. rustfmt reports nothing new on the changed lines.

  • Windows target, type-checked from macOS: cargo clippy --target x86_64-pc-windows-msvc --workspace --all-targets --locked -- -D warnings, with and without --all-features, is clean. The GPU tests, including the changed GPU: view addresses for tables made afresh (dynamic slots) #1906 test, run in the Windows CI only.

  • Not run: the GPU tests (no GPU here) and the title.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Independent verification (software WARP only)

  • PR head: b0fbdcb60ebb311a92a4901321e47ce0e171b174 (checked out detached)
  • origin/main: 67e8ae1401df112248aed36d0024483181b718b7; read-only git merge-tree --write-tree origin/main <head> merges cleanly (exit 0); GitHub reports MERGEABLE.
  • Environment: Windows 11 Pro 10.0.26200, rustc 1.98.1, VICTORY_D3D12_WARP=1 with VICTORY_DXIL pointing at the Windows SDK 10.0.26100 dxil.dll. Default features, no extra test flags.
  • Not tested: hardware GPU, the title (no game files used).

Commands and results

  • cargo test --locked -p victory-shader
  • cargo test --locked -p victory-gpu
  • focused: cargo test --locked -p victory-shader --lib heap_indices_from_data
  • victory-shader lib: 271 passed, 0 failed, 3 ignored (focused heap_indices_from_data_are_clamped_before_root_view_reads: 1 passed). With VICTORY_DXIL set the new validate helper really validated the DXIL of the affected tests on Windows. One existing test (normalized_buffer_formats_load_their_ends_exactly) takes about 80 s in a debug build; it is not touched by this PR.
  • victory-gpu lib: 195 passed, 0 failed, 1 ignored.

Review: Looks correct. clamp_view_index (unsigned compare to HEAP_VIEWS - 1) now guards the view-stride lookup and both view-address lookups, and buffer_guard reuses it unchanged. The host address buffer is sized to max(count, HEAP_VIEWS) slots, so a clamped index stays in bounds (cost: about 32 MiB of upload memory, as the description says). The validate helper keeps the shader tests building off Windows. The counting test is a reasonable pin. Only the shader and host emit paths were verified here, not behaviour on hardware.

Review and verification by Claude Code (Claude Sonnet 5.5), AI-assisted; no game files, dumps, shader names or hashes involved.

@Mericas77

Copy link
Copy Markdown

Tested on NVIDIA (the GPU tests weren't run here yet): RTX 4070, driver 621.01, Windows 11, rustc 1.100.0, dxil.dll 1.9.2602.17. Head b0fbdcb against main 67e8ae1. This card has a capped view heap (NVIDIA's 1,000,000-descriptor limit).

  • cargo test -p victory-gpu --lib --locked: main 193 passed / 2 failed / 1 ignored, this PR the same. The two failures are the same on both and are the known heap-cap ones (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views "past the dynamic slots"). The larger view-address buffer causes no new failure.
  • cargo test -p victory-shader --lib --locked (with --skip heap_direct_texture_reads_mark_their_heap_index, which crashes the process on main here too, Shader tests: heap_direct_texture_reads_mark_their_heap_index kills the test process on a real GPU (RTX 5070, main 67e8ae1) #2571): main 266 / 3 / 3, this PR 267 / 3 / 3. Same 3 known NVIDIA failures.
  • heap_indices_from_data_are_clamped_before_root_view_reads: passes. Since validate() skips silently when dxil.dll doesn't load, I re-ran it with CI=1: still passes, so the DXIL really was validated.

No game data; the title was not run.

AI-assisted.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This PR no longer merges cleanly into main: main has moved since the last triage, and the branch now conflicts in crates/victory-gpu/src/descriptor/host.rs and crates/victory-shader/src/emit/lower.rs. It will be reviewed again once it is rebased onto current main.

Previous verdict for this head: merge candidate.


Generated by Claude Code

@brainbudt

Copy link
Copy Markdown

Note

🤖 Gemini 4 Neon (with GPT-6.2 Astra, Mistral Large 4, and DeepSeek V4.1 Flash) responding on behalf of brainbudt

Independently verified b0fbdcb merged onto 13f91ca. The index clamps and host allocation size look correct, with one issue to address before merge:

  • Explicitly zero the padded tail in HostHeap::view_addresses (crates/victory-gpu/src/descriptor/host.rs:546-560): The buffer allocates max(count, HEAP_VIEWS) slots (32 MiB) but only initializes the count title slots. D3D12 documents default committed allocations as "almost always" zeroed rather than guaranteed. In an isolated WARP probe with a diagnostic 0xA5 prefill, the final slot retained all four prefilled words; clearing [made.len(), slots) during initialization made the same probe pass.

Verification: Windows MSVC workspace cargo check --tests passed; Linux x86-64 passed 159 victory-shader tests (2 ignored); Windows WARP + Microsoft DXIL Validator 1.9.2609.5 passed 6/6 focused shader tests and 13/13 host descriptor tests (synthetic fixtures).

Three tests in `emit/lower.rs` (from ff47463, 54ab406 and 850b66f) called
the Windows-only `validator` module without a `cfg`, so the shader crate's
tests no longer built on Linux or macOS (E0433). They now call `validate`,
which validates where `dxil.dll` is and does nothing off Windows.

Where tests run in CI (`CI` set, as on GitHub's runners) a validator that
does not load fails the test instead of skipping the check, so a green run
means the DXIL was validated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@RomainKH
RomainKH force-pushed the shader/clamp-view-index branch from b0fbdcb to 8e11f47 Compare November 26, 2026 09:51
@RomainKH

RomainKH commented Nov 26, 2026 •

Copy link
Copy Markdown
Author

Rebased onto main 63bcca4 (head eb0dae2).

  • @brainbudt: done as you suggested. When the buffer is made, the entries from address_slots() (title plus dynamic slots, GPU: view addresses for tables made afresh (dynamic slots) #1906) up to HEAP_VIEWS are cleared with one write_bytes. The title and dynamic entries below that were already written.

  • Conflict with GPU: view addresses for tables made afresh (dynamic slots) #1906: the buffer has address_slots().max(HEAP_VIEWS) entries. view_addresses_cover_tables_made_afresh now checks that:

    • the buffer is 16 * HEAP_VIEWS bytes;
    • address_at(end) and address_at(HEAP_VIEWS - 1) read 0;
    • note_address(end) writes nothing;
    • there is no entry at HEAP_VIEWS.

    The old check of a mark past the end could not run any more, since 32 MiB fills the allocation. @r4tedx worked out this resolution first and is co-author of the commit.

  • ds_bpermute_wave64_preserves_32_lane_half, added on main, now goes through the validate helper too.

Checked from macOS:

  • victory-shader lib: 173 passed, 0 failed, 2 ignored;
  • Windows-target cargo clippy --workspace --all-targets -- -D warnings, with and without --all-features: clean.

The GPU tests run in the Windows CI.

Edit: the first push of this rebase failed the CI asset policy, because lower.rs went past the repository's 512 KiB file limit by 118 bytes. The new test now lives in emit/lower/tests/descriptors.rs (head eb0dae2); see Layout in the description.

AI-assisted (Claude Code).

`view_stride` and `view_addresses` read the view-stride table and the view
addresses, root SRVs with no bounds of their own, at a heap index taken
from data, without clamping it as `heap_handle` and `buffer_guard` do. An
index past the table read whatever memory follows it. Both now clamp the
index to `HEAP_VIEWS` - 1 first (`clamp_view_index`, which `buffer_guard`
now uses too).

The host's view-address buffer had 16 bytes per title and dynamic slot
only (`address_slots`), fewer than `HEAP_VIEWS` (about 1.2 million against
2^21 for this title), so even a clamped index could read past it. It now
covers the larger of the two, as the view-stride table already does. The
entries past the dynamic slots are cleared when it is made rather than
left to the new allocation's contents, and nothing writes them after.

Tests: a structured read through a heap descriptor clamps once more than a
raw one (the stride lookup), and reading a descriptor's dwords 0 and 1 as
data clamps twice. Without the clamps it fails (3 against 4).
`view_addresses_cover_tables_made_afresh` (Marvisak#1906) now checks that the
buffer reaches `HEAP_VIEWS`, that the entries past the dynamic slots read
0 up to the last and are not written, and that there is none past it.

The new lowering test lives in `emit/lower/tests/descriptors.rs`, a child
of `lower`'s tests: `lower.rs` is within a few KiB of the repository's
512 KiB limit on a file, and with the test it would pass it.

Clearing the tail was suggested by brainbudt in review; the conflict
resolution with Marvisak#1906 and its test follow r4tedx's reference rebase.

Co-Authored-By: r4. <192970269+r4tedx@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@RomainKH
RomainKH force-pushed the shader/clamp-view-index branch from 8e11f47 to eb0dae2 Compare November 26, 2026 10:00
RomainKH added a commit to RomainKH/VIctory that referenced this pull request Nov 26, 2026
A descriptor every lane shares can reach its use through a tree of joins
and SCC selects: a select on an edge into a join, a join on an arm of a
select, or a join at a loop header whose back edge is lowered after the
body. `uniform_heap_index`, which took nested joins of loads (Marvisak#1012), now
also takes selects, so either may hold the other. A loop header's phi goes
to `pending_descriptors` and `fill_phis` fills it once every predecessor
has a tail; an edge no lane takes brings 0, as before.

`view_stride` takes the same trees, so a structured read through one gets
its stride; it refused a join ("a structured buffer of unknown stride").
An SCC select between two immediate samplers samples with both, as a join
of two already does. A tree with a leaf that a waterfall loop reads per
lane gets a non-uniform handle.

Tests, synthetic GFX10 programs, each validated by dxil.dll where it runs
(required in CI): a select inside nested joins and a descriptor phi at a
loop header, each with a raw and a structured read, and an immediate
sampler chosen by SCC. On `main` they are refused with "a descriptor chosen
by control flow" / "... by a condition".

`lower.rs` is within a few KiB of the repository's 512 KiB limit on a
file. `uniform_heap_index` and its two new helpers (`fill_descriptor_phi`,
`waterfall_leaf`) move to a child module, `emit/lower/trees.rs`, and the
tests join Marvisak#2558's in `emit/lower/tests/descriptors.rs`; `lower.rs` ends
slightly smaller than on `main`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants