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

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

@⁠RomainKH on GitHub · 2026-11-25 15:45 UTC · original
RomainKH:shader/clamp-view-index → main · +188 −53 · 4 files · 2 commits · open

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 #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:

  • #1906 has landed and sized the buffer to address_slots(). This PR takes address_slots().max(HEAP_VIEWS), and #1906's test view_addresses_cover_tables_made_afresh now checks the buffer up to HEAP_VIEWS: the entries past the dynamic slots read 0 up to the last one and are not written, and there is none past it. Its old check of a mark past the buffer's end cannot run any more, since 32 MiB fills the allocation. r4tedx worked out this resolution first and is co-author of the commit.
  • #2185 and #1777 size the clamp to a capped host heap. That is a different bound, on the host's heaps, not on these two tables.

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 #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

**@⁠RomainKH** on GitHub · 2026-11-25 15:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/2558) `RomainKH:shader/clamp-view-index` → `main` · +188 −53 · 4 files · 2 commits · open 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 #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: - #1906 has landed and sized the buffer to `address_slots()`. This PR takes `address_slots().max(HEAP_VIEWS)`, and #1906's test `view_addresses_cover_tables_made_afresh` now checks the buffer up to `HEAP_VIEWS`: the entries past the dynamic slots read 0 up to the last one and are not written, and there is none past it. Its old check of a mark past the buffer's end cannot run any more, since 32 MiB fills the allocation. r4tedx worked out this resolution first and is co-author of the commit. - #2185 and #1777 size the clamp to a capped host heap. That is a different bound, on the host's heaps, not on these two tables. ## 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 #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](https://claude.com/claude-code)
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 <[email protected]>
`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 slot only, 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 slots past the title's read 0.

Test: 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).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:09:13 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 16:12 UTC · original

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.

**@⁠oni-swr** on GitHub · 2026-11-25 16:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/2558#issuecomment-7167684347) **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.
Author
Owner

@⁠Mericas77 on GitHub · 2026-11-25 17:34 UTC · original

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, #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.

**@⁠Mericas77** on GitHub · 2026-11-25 17:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/2558#issuecomment-7169695498) 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, #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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 18:38 UTC · original · edited 2026-11-25 22:15 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 18:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/2558#issuecomment-7171199978) · edited 2026-11-25 22:15 UTC <!-- victory-triage v1 head=b0fbdcb60ebb311a92a4901321e47ce0e171b174 verdict=needs-rebase at=2026-11-25T22:17:00Z --> 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](https://claude.ai/code)_
Author
Owner

@⁠brainbudt on GitHub · 2026-11-25 21:04 UTC · original

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).

**@⁠brainbudt** on GitHub · 2026-11-25 21:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/2558#issuecomment-7174566005) > [!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](https://devblogs.microsoft.com/directx/coming-to-directx-12-more-control-over-memory-allocation/) 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).
github-bot force-pushed github-bot/gh-2558 from b0fbdcb60e to eb0dae2fea 2026-11-26 11:10:15 +00:00 Compare
Author
Owner

@⁠RomainKH on GitHub · 2026-11-26 09:52 UTC · original · edited 2026-11-26 10:01 UTC

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, #1906) up to HEAP_VIEWS are cleared with one write_bytes. The title and dynamic entries below that were already written.

  • Conflict with #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).

**@⁠RomainKH** on GitHub · 2026-11-26 09:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/2558#issuecomment-7188594077) · edited 2026-11-26 10:01 UTC 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, #1906) up to `HEAP_VIEWS` are cleared with one `write_bytes`. The title and dynamic entries below that were already written. - Conflict with #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).
This pull request can be merged automatically.
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/2558/head:github-bot/gh-2558'
git switch 'github-bot/gh-2558'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2558'
git switch 'github-bot/gh-2558'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2558'
git switch 'github-bot/gh-2558'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2558'
git switch 'main'
git merge --squash 'github-bot/gh-2558'
git switch 'main'
git merge --ff-only 'github-bot/gh-2558'
git switch 'main'
git merge 'github-bot/gh-2558'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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/Marvisak--VIctory!2558
No description provided.