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

Open
RomainKH wants to merge 2 commits from upstream-pr/2558 into main
RomainKH commented 2026-11-25 15:45:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by RomainKH on 2026-11-25T15:45:51Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6608263686 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2558). Originally posted by [RomainKH](https://github.com/RomainKH) on 2026-11-25T15:45:51Z. --- 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)
oni-swr commented 2026-11-25 17:05:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-25T16:12:18Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167684347 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2558#issuecomment-7167684347). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-25T16:12:18Z. --- **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 commented 2026-11-25 18:02:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Mericas77 on 2026-11-25T17:34:22Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7169695498 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2558#issuecomment-7169695498). Originally posted by [Mericas77](https://github.com/Mericas77) on 2026-11-25T17:34:22Z. --- 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.
Marvisak commented 2026-11-25 19:06:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T18:38:53Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7171199978 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2558#issuecomment-7171199978). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T18:38:53Z. --- <!-- 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)_
brainbudt commented 2026-11-25 22:04:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by brainbudt on 2026-11-25T21:04:03Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7174566005 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2558#issuecomment-7174566005). Originally posted by [brainbudt](https://github.com/brainbudt) on 2026-11-25T21:04:03Z. --- > [!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).
RomainKH commented 2026-11-26 10:05:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by RomainKH on 2026-11-26T09:52:37Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7188594077 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2558#issuecomment-7188594077). Originally posted by [RomainKH](https://github.com/RomainKH) on 2026-11-26T09:52:37Z. --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2558:upstream-pr/2558'
git switch 'upstream-pr/2558'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2558'
git switch 'upstream-pr/2558'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2558'
git switch 'upstream-pr/2558'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2558'
git switch 'main'
git merge --squash 'upstream-pr/2558'
git switch 'main'
git merge --ff-only 'upstream-pr/2558'
git switch 'main'
git merge 'upstream-pr/2558'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!2556
No description provided.