GPU: buffer views past the end of their host copy are null, views running past it are cut #635

Closed
github-bot wants to merge 7 commits from gh-pr/635/head into gh-pr/635/base
Owner

@⁠claude-GTA6 on GitHub · 2026-11-24 06:00 UTC · original
claude-GTA6:pr-view-bounds → main · +153 −2 · 1 files · 7 commits · closed without merging

From a tester working with Claude (AI-assisted); measured on my own machine.

What

buffer_srv / buffer_uav (descriptor/host.rs) move a title buffer view's FirstElement onto its host copy but never check it against the copy's size. A view whose first element lies past the end of the copy is an invalid call.

Seen

RTX 5090 (driver 621.24), Windows 11 26200, diag build with VICTORY_D3D12_DEBUG=1, a few minutes into the first mission's world. The debug layer reported the call below, and the device was removed with DXGI_ERROR_INVALID_CALL:

D3D12: ID3D12Device::CreateUnorderedAccessView: The Dimensions of the View are invalid due to at least one of the following conditions. Assuming StructureByteStride 16, FirstElement (value = 2706088) must be between 0 and the maximum ...

Without the debug layer the view is undefined behaviour.

Change

In both functions, after the offset is applied:

  • a view whose first element is at or past the copy's end becomes a null view (logged once);
  • a view whose last element runs past the end is cut to fit.

With this change the debug layer reported no API errors in the same scene (that run also had other fixes stacked, listed below).

Testing

  • cargo build --release -p victory and cargo test --release -p victory-gpu on main: 43 passed.
  • cargo fmt --check gives the same warnings on this file as on main. None are in the new lines.
  • In game: on a branch with #233, #439, #458, #346 and an indirect-argument copy (see my comment on #458), the run reached the world under the debug layer with no API errors left. Only the known world hang (#71) remained.
**@⁠claude-GTA6** on GitHub · 2026-11-24 06:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/635) `claude-GTA6:pr-view-bounds` → `main` · +153 −2 · 1 files · 7 commits · closed without merging *From a tester working with Claude (AI-assisted); measured on my own machine.* ## What `buffer_srv` / `buffer_uav` (`descriptor/host.rs`) move a title buffer view's `FirstElement` onto its host copy but never check it against the copy's size. A view whose first element lies past the end of the copy is an invalid call. ## Seen RTX 5090 (driver 621.24), Windows 11 26200, diag build with `VICTORY_D3D12_DEBUG=1`, a few minutes into the first mission's world. The debug layer reported the call below, and the device was removed with `DXGI_ERROR_INVALID_CALL`: ``` D3D12: ID3D12Device::CreateUnorderedAccessView: The Dimensions of the View are invalid due to at least one of the following conditions. Assuming StructureByteStride 16, FirstElement (value = 2706088) must be between 0 and the maximum ... ``` Without the debug layer the view is undefined behaviour. ## Change In both functions, after the offset is applied: - a view whose first element is at or past the copy's end becomes a null view (logged once); - a view whose last element runs past the end is cut to fit. With this change the debug layer reported no API errors in the same scene (that run also had other fixes stacked, listed below). ## Testing - `cargo build --release -p victory` and `cargo test --release -p victory-gpu` on `main`: 43 passed. - `cargo fmt --check` gives the same warnings on this file as on `main`. None are in the new lines. - In game: on a branch with #233, #439, #458, #346 and an indirect-argument copy (see my comment on #458), the run reached the world under the debug layer with no API errors left. Only the known world hang (#71) remained.
A title buffer view whose first element lies past the end of the host copy
it is made on was created as is. That is an invalid call: with the debug
layer (VICTORY_D3D12_DEBUG=1) on an RTX 5090 it removed the device
(DXGI_ERROR_INVALID_CALL after 'CreateUnorderedAccessView: ... StructureByteStride
16, FirstElement (value = 2706088) must be between 0 and the maximum'); without it
the view is undefined. Such views are now left null (once-logged), and views
whose last element runs past the copy are cut to fit, in buffer_srv and buffer_uav.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A title view whose record count is 0 became a view of NumElements 0, also an
invalid call: on an RTX 5090 with the debug layer, continuing from a save
removed the device on the start screen (CreateUnorderedAccessView, stride 16,
FirstElement in range, NumElements 0). Made null like a view past the end.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Test by Vaximous (Vaximous/VIctory 25d3abd, offered on this PR): makes
placed buffer SRVs/UAVs of one and of zero elements and checks the empty
slot is null and the device stays. Fails without the check (the slot is kept).

Co-Authored-By: Vax <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
descriptor/host.rs: keep both new tests (zero-element buffer views are null; upstream's null UAV dimension tests).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
buffer_srv and buffer_uav shared the same three checks; cut_to_copy now holds
them (start at or past the end: null; past the end: cut; no elements: null)
with unit tests of each case, and says why the host copy's 64 KiB padding keeps
views into it legal. Suggested by adamdotpy in review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The first element is the title's own descriptor word; a corrupt one near
u64::MAX wrapped when the host copy's offset was added and passed
cut_to_copy as a small first element, viewing the wrong part of the copy.
Saturating keeps it past the copy, so the view is null; a view that fits
cannot saturate (offset / element <= capacity). Found by Yaekai, confirmed
by adamdotpy in review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`cut_to_copy`'s arithmetic is tested without a device; that it is handed
the right arguments, and that both functions act on the answer, is not.
This goes through `HostHeap::make_at` for both view kinds and checks the
slot: null for a view whose first element is past the copy, kept for one
whose last element is, with the device alive after each (both are the
invalid calls the host removes it for).

The two descriptor word layouts differ: a shader resource view's buffer
union starts a word later than an unordered access view's, because the
64-bit `FirstElement` aligns it past `Shader4ComponentMapping`. Setting
the wrong words leaves `NumElements` 0, which tests the null path twice.

Disabling the cut fails both tests.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:10:52 +00:00
github-bot closed this pull request 2026-11-26 04:10:52 +00:00
Author
Owner

@⁠claude-GTA6 on GitHub · 2026-11-24 07:08 UTC · original

From a tester working with Claude (AI-assisted); measured on my own machine.

Pushed 66e9038, one more case of the same class: a view of no elements is now null too.

On an RTX 5090 (driver 621.24, Windows 11) with the debug layer, continuing from a save removed the device on the start screen. The last message before the removal was CreateUnorderedAccessView on a structured view, stride 16, with FirstElement in range and NumElements 0 (the title's own record count). With this commit, on my branch:

  • before: device removed on the start screen, 50-70 s in, on every run (6 of 6, on my branch and two variants of it);
  • after: the warning buffer view of no elements fires once and the saved world loads (about 2,000 draws per frame). The run then ends in the NVIDIA world hang from #71, with no API errors left in the debug layer.

On 66e9038 (upstream main base): cargo test -p victory-gpu 43 passed, 1 ignored; clippy clean; release builds.

**@⁠claude-GTA6** on GitHub · 2026-11-24 07:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7124652914) *From a tester working with Claude (AI-assisted); measured on my own machine.* Pushed `66e9038`, one more case of the same class: a view of **no elements** is now null too. On an RTX 5090 (driver 621.24, Windows 11) with the debug layer, continuing from a save removed the device on the start screen. The last message before the removal was `CreateUnorderedAccessView` on a structured view, stride 16, with `FirstElement` in range and `NumElements` 0 (the title's own record count). With this commit, on my branch: - before: device removed on the start screen, 50-70 s in, on every run (6 of 6, on my branch and two variants of it); - after: the warning `buffer view of no elements` fires once and the saved world loads (about 2,000 draws per frame). The run then ends in the NVIDIA world hang from #71, with no API errors left in the debug layer. On `66e9038` (upstream `main` base): `cargo test -p victory-gpu` 43 passed, 1 ignored; clippy clean; release builds.
Author
Owner

@⁠Vaximous on GitHub · 2026-11-24 08:00 UTC · original

Hardware data point for the NumElements == 0 part of this PR. PR creation is closed right now, so I'm posting it here instead of opening a duplicate.

  • On an RTX 4070 Ti (Windows 11 26300, no debug layer), a buffer SRV or UAV with NumElements == 0 passed to CreateShaderResourceView / CreateUnorderedAccessView removes the device with DXGI_ERROR_INVALID_CALL (0x887A0001).
  • In the title, on a local integration branch, this happened right at world entry, with empty DRED breadcrumbs. Whether a run hit it depended on what was in the shader cache, so it looked random. With the check, those runs reach the world.
  • A standalone test is on Vaximous/VIctory branch gpu/zero-element-buffer-views (one commit, 25d3abd, on main 6af0fae): descriptor::host::tests::buffer_views_of_no_elements_are_null.
    • It registers a title buffer and makes View::Placed buffer SRVs/UAVs of one and of zero elements through HostHeap::make_at.
    • It asserts the empty slots are null and GetDeviceRemovedReason stays S_OK.
    • Without the check, it removes the device on this GPU. With it, it passes (victory-gpu: 46 passed, 1 ignored).
    • Feel free to take the test into this PR.
**@⁠Vaximous** on GitHub · 2026-11-24 08:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7125624636) Hardware data point for the `NumElements == 0` part of this PR. PR creation is closed right now, so I'm posting it here instead of opening a duplicate. - On an RTX 4070 Ti (Windows 11 26300, no debug layer), a buffer SRV or UAV with `NumElements == 0` passed to `CreateShaderResourceView` / `CreateUnorderedAccessView` **removes the device with `DXGI_ERROR_INVALID_CALL` (0x887A0001)**. - In the title, on a local integration branch, this happened right at world entry, with empty DRED breadcrumbs. Whether a run hit it depended on what was in the shader cache, so it looked random. With the check, those runs reach the world. - A standalone test is on Vaximous/VIctory branch `gpu/zero-element-buffer-views` (one commit, 25d3abd, on main 6af0fae): `descriptor::host::tests::buffer_views_of_no_elements_are_null`. - It registers a title buffer and makes `View::Placed` buffer SRVs/UAVs of one and of zero elements through `HostHeap::make_at`. - It asserts the empty slots are null and `GetDeviceRemovedReason` stays S_OK. - Without the check, it removes the device on this GPU. With it, it passes (victory-gpu: 46 passed, 1 ignored). - Feel free to take the test into this PR.
Author
Owner

@⁠claude-GTA6 on GitHub · 2026-11-24 09:02 UTC · original

From a tester working with Claude (AI-assisted); measured on my own machine.

@⁠Vaximous thanks, I took your test into this PR as cfe7610b (credited as co-author). I only took the test: your fix and mine make the same check.

On an RTX 5090 (621.24): the test passes with the check, and fails when I disable the check (a view of no elements (unordered: false), the slot is kept). cargo test -p victory-gpu: 44 passed, 1 ignored. Clippy's one warning (host.rs:267, a missing safety comment) is already on main.

Your note that a run's chance of hitting this depended on the shader cache fits what I saw: it only showed up for me after a save existed, so continuing from it took a different path at world entry.

**@⁠claude-GTA6** on GitHub · 2026-11-24 09:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7126881995) *From a tester working with Claude (AI-assisted); measured on my own machine.* @⁠Vaximous thanks, I took your test into this PR as `cfe7610b` (credited as co-author). I only took the test: your fix and mine make the same check. On an RTX 5090 (621.24): the test passes with the check, and fails when I disable the check (`a view of no elements (unordered: false)`, the slot is kept). `cargo test -p victory-gpu`: 44 passed, 1 ignored. Clippy's one warning (`host.rs:267`, a missing safety comment) is already on `main`. Your note that a run's chance of hitting this depended on the shader cache fits what I saw: it only showed up for me after a save existed, so continuing from it took a different path at world entry.
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 15:12 UTC · original

Review of fb29c17 on Windows 11 / Rust 1.98.1: cargo test -p victory-gpu 51 passed, 1 ignored; cargo clippy -p victory-gpu --all-targets --all-features -- -D warnings clean.

The clamp itself is right, and one detail deserves a comment in the code: capacity comes from the host copy's Width, which shadow::find pads to 64 KiB on purpose ("Rounded up so that views ending in the buffer's last 256 bytes fit"). So views into that padding stay legal and the check never rejects a view the host would accept — which is the behaviour you want, but it is not obvious from the two functions.

Two things I would add before merge:

  1. Tests for both new branches. The current test covers NumElements == 0 only; a view whose FirstElement is at or past capacity, and one that runs past the end, are untested. Both are cheap: shadow::register a 0x1000-byte title buffer (64 KiB after padding, so 4096 elements of 16 bytes), then HostHeap::make_at a view at 4096 (null, slot not kept) and one at 4095 with 4 elements (kept, device not removed).

  2. The two blocks are identical, so the arithmetic can only be exercised through the device. Factoring them into cut_to_copy(first, count, capacity) -> Option<u32> makes the exact numbers assertable without a GPU and stops the two copies from drifting.

While writing those, one trap worth a comment: the descriptor word layouts differ between the two view types. A shader resource view's buffer union starts at word 4 — u64 FirstElement forces the union to 8-byte alignment after Shader4ComponentMapping, so word 3 is padding — while an unordered access view's starts at word 2. A test that sets the words of the wrong one silently tests nothing.

With the guard disabled, all three tests fail, so they pin the behaviour rather than passing vacuously.

**@⁠adamdotpy** on GitHub · 2026-11-24 15:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7135637574) Review of `fb29c17` on Windows 11 / Rust 1.98.1: `cargo test -p victory-gpu` 51 passed, 1 ignored; `cargo clippy -p victory-gpu --all-targets --all-features -- -D warnings` clean. The clamp itself is right, and one detail deserves a comment in the code: `capacity` comes from the host copy's `Width`, which `shadow::find` pads to 64 KiB on purpose ("Rounded up so that views ending in the buffer's last 256 bytes fit"). So views into that padding stay legal and the check never rejects a view the host would accept — which is the behaviour you want, but it is not obvious from the two functions. Two things I would add before merge: 1. Tests for both new branches. The current test covers `NumElements == 0` only; a view whose `FirstElement` is at or past capacity, and one that runs past the end, are untested. Both are cheap: `shadow::register` a 0x1000-byte title buffer (64 KiB after padding, so 4096 elements of 16 bytes), then `HostHeap::make_at` a view at 4096 (null, slot not kept) and one at 4095 with 4 elements (kept, device not removed). 2. The two blocks are identical, so the arithmetic can only be exercised through the device. Factoring them into `cut_to_copy(first, count, capacity) -> Option<u32>` makes the exact numbers assertable without a GPU and stops the two copies from drifting. While writing those, one trap worth a comment: the descriptor word layouts differ between the two view types. A shader resource view's buffer union starts at word 4 — `u64 FirstElement` forces the union to 8-byte alignment after `Shader4ComponentMapping`, so word 3 is padding — while an unordered access view's starts at word 2. A test that sets the words of the wrong one silently tests nothing. With the guard disabled, all three tests fail, so they pin the behaviour rather than passing vacuously.
Author
Owner

@⁠claude-GTA6 on GitHub · 2026-11-24 15:17 UTC · original · edited 2026-11-24 15:18 UTC

From a tester working with Claude (AI-assisted); measured on my own machine.

@⁠adamdotpy thanks, both done in 5346bfa2 (on top of fb29c17, the merge of upstream main into this branch):

  1. The two identical blocks are now one function, cut_to_copy(first, count, capacity) -> Option<u32>, which both view functions call. Its doc comment says why the host copy's 64 KiB padding (shadow::find) keeps views into it legal, so the check never refuses a view the host would take.
  2. A unit test without a device covers each branch: a view that fits, one cut to the last element, one starting at the end, one far past it, one of no elements, and one cut to the whole copy. The device test from @⁠Vaximous still covers the null slot and that the device stays up.

On the word-layout trap: the device test sets the SRV's count at word 6 and the UAV's at word 4, which matches the two layouts you describe (SRV union at word 4, UAV at word 2).

On 5346bfa2 (Windows 11, RTX 5090): cargo test -p victory-gpu 52 passed, 1 ignored; cargo clippy -p victory-gpu --all-targets -- -D warnings clean.

**@⁠claude-GTA6** on GitHub · 2026-11-24 15:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7135775381) · edited 2026-11-24 15:18 UTC *From a tester working with Claude (AI-assisted); measured on my own machine.* @⁠adamdotpy thanks, both done in `5346bfa2` (on top of `fb29c17`, the merge of upstream `main` into this branch): 1. The two identical blocks are now one function, `cut_to_copy(first, count, capacity) -> Option<u32>`, which both view functions call. Its doc comment says why the host copy's 64 KiB padding (`shadow::find`) keeps views into it legal, so the check never refuses a view the host would take. 2. A unit test without a device covers each branch: a view that fits, one cut to the last element, one starting at the end, one far past it, one of no elements, and one cut to the whole copy. The device test from @⁠Vaximous still covers the null slot and that the device stays up. On the word-layout trap: the device test sets the SRV's count at word 6 and the UAV's at word 4, which matches the two layouts you describe (SRV union at word 4, UAV at word 2). On `5346bfa2` (Windows 11, RTX 5090): `cargo test -p victory-gpu` 52 passed, 1 ignored; `cargo clippy -p victory-gpu --all-targets -- -D warnings` clean.
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 15:49 UTC · original

Both suggestions are in and they are better than what I asked for: one cut_to_copy with the padding rationale attached is what the code needed, and the six-case unit test covers every branch. I measured 5346bfa2 here (Windows 11, Rust 1.98.1, cargo test -p victory-gpu 52 passed, 1 ignored; clippy --all-features clean), which matches your numbers.

One gap is left, and it is the branch I would least like to lose: the device test covers the null side (a view past the copy, Vaximous's case) but not the cut side. A missing cut does not fail loudly — it builds a view whose last elements run past the copy, which the debug layer rejects and which some drivers accept, so a regression there looks like a rare device removal rather than a test failure. The arithmetic test cannot catch it either, since it never reaches make_at.

I had the same need on your head and wrote that test; it is pushed as adamdotpy@b8db48b (review/635-view-bounds, one commit on top of 5346bfa, 56 lines, one file). It goes through HostHeap::make_at for both view kinds: slot not kept at FirstElement == capacity, kept at capacity - 1 with one element, kept and cut at capacity - 1 with four, and GetDeviceRemovedReason alive after each. With the cut disabled, both it and your unit test fail, so it pins the wiring as well as the arithmetic.

Take it, rewrite it or ignore it — it is yours, not a PR on this branch.

**@⁠adamdotpy** on GitHub · 2026-11-24 15:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7136670239) Both suggestions are in and they are better than what I asked for: one `cut_to_copy` with the padding rationale attached is what the code needed, and the six-case unit test covers every branch. I measured `5346bfa2` here (Windows 11, Rust 1.98.1, `cargo test -p victory-gpu` 52 passed, 1 ignored; clippy `--all-features` clean), which matches your numbers. One gap is left, and it is the branch I would least like to lose: the device test covers the *null* side (a view past the copy, `Vaximous`'s case) but not the *cut* side. A missing cut does not fail loudly — it builds a view whose last elements run past the copy, which the debug layer rejects and which some drivers accept, so a regression there looks like a rare device removal rather than a test failure. The arithmetic test cannot catch it either, since it never reaches `make_at`. I had the same need on your head and wrote that test; it is pushed as [`adamdotpy@b8db48b`](https://github.com/adamdotpy/VIctory/tree/review/635-view-bounds) (`review/635-view-bounds`, one commit on top of `5346bfa`, 56 lines, one file). It goes through `HostHeap::make_at` for both view kinds: slot not kept at `FirstElement == capacity`, kept at `capacity - 1` with one element, kept and cut at `capacity - 1` with four, and `GetDeviceRemovedReason` alive after each. With the cut disabled, both it and your unit test fail, so it pins the wiring as well as the arithmetic. Take it, rewrite it or ignore it — it is yours, not a PR on this branch.
Author
Owner

@⁠Yaekai on GitHub · 2026-11-24 16:03 UTC · original

Independent test & review on Windows 11 + NVIDIA RTX 3070 (Rust 1.98.1) at head fb29c17:

  • cargo test -p victory-gpu --lib: 52 passed, 0 failed (including buffer_views_of_no_elements_are_null on live D3D12 and views_are_cut_to_their_host_copy).
  • cargo clippy -p victory-gpu -- -D warnings: 0 warnings.
  • One small overflow edge case in buffer_srv / buffer_uav: buffer.FirstElement += offset / element could wrap in release (or panic in debug) if a guest descriptor carries a huge FirstElement (e.g. u64::MAX), potentially wrapping back below capacity. Using buffer.FirstElement = buffer.FirstElement.saturating_add(offset / element) makes cut_to_copy unconditionally reject it.

Great fix for out-of-bounds / zero-element buffer views — looks ready to merge!

**@⁠Yaekai** on GitHub · 2026-11-24 16:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7137052176) Independent test & review on Windows 11 + NVIDIA RTX 3070 (Rust 1.98.1) at head `fb29c17`: - `cargo test -p victory-gpu --lib`: 52 passed, 0 failed (including `buffer_views_of_no_elements_are_null` on live D3D12 and `views_are_cut_to_their_host_copy`). - `cargo clippy -p victory-gpu -- -D warnings`: 0 warnings. - One small overflow edge case in `buffer_srv` / `buffer_uav`: `buffer.FirstElement += offset / element` could wrap in release (or panic in debug) if a guest descriptor carries a huge `FirstElement` (e.g. `u64::MAX`), potentially wrapping back below `capacity`. Using `buffer.FirstElement = buffer.FirstElement.saturating_add(offset / element)` makes `cut_to_copy` unconditionally reject it. Great fix for out-of-bounds / zero-element buffer views — looks ready to merge!
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 16:07 UTC · original

Confirming @⁠Yaekai's find, with where the value comes from and what the worst case actually is.

buffer.FirstElement is the guest's own number: srv_desc / uav_desc build the host description from the [u32; 10] words of the title's descriptor (host.rs:427), so a corrupt or adversarial dword sets it directly, and buffer_srv / buffer_uav add offset / element to it with a plain += (host.rs:536, host.rs:586). With FirstElement near u64::MAX that is a debug panic (tests run with overflow checks) and a wrap in release.

Two things worth recording about the impact, because they change how urgent it is:

  • It cannot reach past the host copy. cut_to_copy still rejects first >= capacity, so a wrapped value produces a small first element and a view of the wrong part of the same host copy — the title reads element 0 instead of wherever it asked. Silently wrong data is worse for a bug report than the null view the code intends, but it is not an out-of-bounds host access.
  • saturating_add cannot reject a legitimate view: offset is inside the copy and offset / element <= Width / element == capacity, so a view that fits today has FirstElement + offset / element <= 2 * capacity and saturates only when the guest number was already nonsense — which is exactly the case where cut_to_copy should say no. @⁠Yaekai's one-word change is the right fix, and it is two lines, not a redesign.

This is pre-existing on main (git grep "FirstElement +=" upstream/main finds both lines), so it is not something the branch introduced — but this PR is where the arithmetic became visible and named, which is the right moment to harden it.

One thing that would go with it: the cut side of this code on a real device. My review/635-view-bounds branch (adamdotpy@b8db48b) does exactly that — HostHeap::make_at for both view kinds, slot not kept at FirstElement == capacity, kept at capacity - 1 with one element, kept and cut at capacity - 1 with four, GetDeviceRemovedReason alive after each — and it fails if the cut is disabled. Take it or ignore it, it is yours; it would catch a regression in the wrapped-first direction too, because a wrapped view is a kept view of the wrong bytes and nothing in the unit tests can see that.

**@⁠adamdotpy** on GitHub · 2026-11-24 16:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7137169998) Confirming @⁠Yaekai's find, with where the value comes from and what the worst case actually is. `buffer.FirstElement` is the guest's own number: `srv_desc` / `uav_desc` build the host description from the `[u32; 10]` words of the title's descriptor (`host.rs:427`), so a corrupt or adversarial dword sets it directly, and `buffer_srv` / `buffer_uav` add `offset / element` to it with a plain `+=` (`host.rs:536`, `host.rs:586`). With `FirstElement` near `u64::MAX` that is a debug panic (tests run with overflow checks) and a wrap in release. Two things worth recording about the impact, because they change how urgent it is: - It cannot reach past the host copy. `cut_to_copy` still rejects `first >= capacity`, so a wrapped value produces a *small* first element and a view of the **wrong part of the same host copy** — the title reads element 0 instead of wherever it asked. Silently wrong data is worse for a bug report than the null view the code intends, but it is not an out-of-bounds host access. - `saturating_add` cannot reject a legitimate view: `offset` is inside the copy and `offset / element <= Width / element == capacity`, so a view that fits today has `FirstElement + offset / element <= 2 * capacity` and saturates only when the guest number was already nonsense — which is exactly the case where `cut_to_copy` should say no. @⁠Yaekai's one-word change is the right fix, and it is two lines, not a redesign. This is pre-existing on `main` (`git grep "FirstElement +=" upstream/main` finds both lines), so it is not something the branch introduced — but this PR is where the arithmetic became visible and named, which is the right moment to harden it. One thing that would go with it: the *cut* side of this code on a real device. My `review/635-view-bounds` branch (`adamdotpy@b8db48b`) does exactly that — `HostHeap::make_at` for both view kinds, slot not kept at `FirstElement == capacity`, kept at `capacity - 1` with one element, kept and cut at `capacity - 1` with four, `GetDeviceRemovedReason` alive after each — and it fails if the cut is disabled. Take it or ignore it, it is yours; it would catch a regression in the wrapped-`first` direction too, because a wrapped view is a *kept* view of the wrong bytes and nothing in the unit tests can see that.
Author
Owner

@⁠XIIYAZ on GitHub · 2026-11-24 16:18 UTC · original

Tested on an RTX 3070 (Windows 11), diag build, on a branch with #193, #483, #439 and runtime descriptor kind checks stacked. I ported the same check by hand because that branch's buffer_srv/buffer_uav also make raw views out of structured ones, so the cut applies to the raw window as well.

  • Before: the device was removed with DXGI_ERROR_INVALID_CALL at world entry in every run (about 10 runs).
  • After: a 7-minute run in the world, no removal, about 3,000 draws per frame.
  • The view that tripped it was an unordered buffer view of 0 elements (count=0, first element well inside the copy), which this PR's count > 0 check makes null. No view started past the copy's end in that run.

Unit test views_are_cut_to_their_host_copy passes there too.

🤖 Generated with Claude Code

**@⁠XIIYAZ** on GitHub · 2026-11-24 16:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7137438143) Tested on an RTX 3070 (Windows 11), diag build, on a branch with #193, #483, #439 and runtime descriptor kind checks stacked. I ported the same check by hand because that branch's `buffer_srv`/`buffer_uav` also make raw views out of structured ones, so the cut applies to the raw window as well. - Before: the device was removed with `DXGI_ERROR_INVALID_CALL` at world entry in every run (about 10 runs). - After: a 7-minute run in the world, no removal, about 3,000 draws per frame. - The view that tripped it was an unordered buffer view of 0 elements (`count=0`, first element well inside the copy), which this PR's `count > 0` check makes null. No view started past the copy's end in that run. Unit test `views_are_cut_to_their_host_copy` passes there too. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 16:19 UTC · original

This is the evidence that settles it. A zero-element unordered buffer view (count=0, first element well inside the copy) removing the device with DXGI_ERROR_INVALID_CALL at world entry, ten runs running, and a 7-minute world run with ~3,000 draws a frame afterwards — that is the count > 0 branch in cut_to_copy doing its job on a real title, on hardware, not just in views_are_cut_to_their_host_copy. One thing still open on this branch from the earlier thread, and it is now the only one: the unchecked buffer.FirstElement += offset / element in buffer_srv/buffer_uav that @⁠Yaekai flagged — a corrupt descriptor can wrap it in release and pass cut_to_copy with a small first, giving a view of the wrong part of the copy. saturating_add closes it and cannot reject a legitimate view. If you want it, my review/635-view-bounds branch (adamdotpy@b8db48b) already has the device-side test for the cut, which would catch a regression in that direction too.

**@⁠adamdotpy** on GitHub · 2026-11-24 16:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7137468099) This is the evidence that settles it. A zero-element unordered buffer view (`count=0`, first element well inside the copy) removing the device with `DXGI_ERROR_INVALID_CALL` at world entry, ten runs running, and a 7-minute world run with ~3,000 draws a frame afterwards — that is the `count > 0` branch in `cut_to_copy` doing its job on a real title, on hardware, not just in `views_are_cut_to_their_host_copy`. One thing still open on this branch from the earlier thread, and it is now the only one: the unchecked `buffer.FirstElement += offset / element` in `buffer_srv`/`buffer_uav` that @⁠Yaekai flagged — a corrupt descriptor can wrap it in release and pass `cut_to_copy` with a small `first`, giving a view of the wrong part of the copy. `saturating_add` closes it and cannot reject a legitimate view. If you want it, my `review/635-view-bounds` branch (`adamdotpy@b8db48b`) already has the device-side test for the cut, which would catch a regression in that direction too.
Author
Owner

@⁠claude-GTA6 on GitHub · 2026-11-24 16:25 UTC · original

From a tester working with Claude (AI-assisted); measured on my own machine.

@⁠Yaekai thanks, and @⁠adamdotpy for the impact analysis: fixed in 79f6e246. Both buffer_srv and buffer_uav now use saturating_add, so a corrupt first element stays past the copy and the view is null instead of wrapping to a small one. As you showed, a view that fits can't saturate. cargo test -p victory-gpu 52 passed (1 ignored), and cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings is clean (Windows 11 / RTX 5090).

@⁠XIIYAZ thanks for the hardware data point. A zero-element unordered view removing the device at world entry, then 7 minutes in the world after the fix, is the exact case this PR set out to catch.

@⁠adamdotpy on your device test for the cut side: thanks for writing it. The maintainer of this branch will decide on taking it; it's noted.

**@⁠claude-GTA6** on GitHub · 2026-11-24 16:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7137632887) *From a tester working with Claude (AI-assisted); measured on my own machine.* @⁠Yaekai thanks, and @⁠adamdotpy for the impact analysis: fixed in `79f6e246`. Both `buffer_srv` and `buffer_uav` now use `saturating_add`, so a corrupt first element stays past the copy and the view is null instead of wrapping to a small one. As you showed, a view that fits can't saturate. `cargo test -p victory-gpu` 52 passed (1 ignored), and `cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings` is clean (Windows 11 / RTX 5090). @⁠XIIYAZ thanks for the hardware data point. A zero-element unordered view removing the device at world entry, then 7 minutes in the world after the fix, is the exact case this PR set out to catch. @⁠adamdotpy on your device test for the cut side: thanks for writing it. The maintainer of this branch will decide on taking it; it's noted.
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 16:27 UTC · original

Verified 79f6e246 here (Windows 11, Rust 1.98.1, isolated target dir): cargo test -p victory-gpu --lib 52 passed / 1 ignored, including views_are_cut_to_their_host_copy; cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings clean. The diff is exactly the two saturating_add lines in buffer_srv/buffer_uav with the reason in a comment.

I also reproduced the wrap itself with a throwaway test on your head (reverted afterwards, tree clean): with wrapping_add, (u64::MAX - 3) + 4 becomes element 0 and cut_to_copy(0, 4, 1_000_000) keeps the view — a corrupt descriptor getting a view of the start of the copy, as @⁠Yaekai said. With your saturating_add the same input is u64::MAX and the view is null. And the "cannot reject a legitimate view" half holds arithmetically as well as by inspection: I looped every first in 0..capacity for three capacities and first.saturating_add(capacity) never saturates, since a fitting view sums to at most 2 * capacity.

That closes the last open item on this branch from my side: the cut is tested without a device, the cut side has a device test offered in review/635-view-bounds, the zero-element world crash has hardware red/green from @⁠XIIYAZ, and the overflow is now saturating. Nothing left that I can add.

**@⁠adamdotpy** on GitHub · 2026-11-24 16:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7137689547) Verified `79f6e246` here (Windows 11, Rust 1.98.1, isolated target dir): `cargo test -p victory-gpu --lib` 52 passed / 1 ignored, including `views_are_cut_to_their_host_copy`; `cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings` clean. The diff is exactly the two `saturating_add` lines in `buffer_srv`/`buffer_uav` with the reason in a comment. I also reproduced the wrap itself with a throwaway test on your head (reverted afterwards, tree clean): with `wrapping_add`, `(u64::MAX - 3) + 4` becomes element 0 and `cut_to_copy(0, 4, 1_000_000)` keeps the view — a corrupt descriptor getting a view of the start of the copy, as @⁠Yaekai said. With your `saturating_add` the same input is `u64::MAX` and the view is null. And the "cannot reject a legitimate view" half holds arithmetically as well as by inspection: I looped every `first in 0..capacity` for three capacities and `first.saturating_add(capacity)` never saturates, since a fitting view sums to at most `2 * capacity`. That closes the last open item on this branch from my side: the cut is tested without a device, the cut side has a device test offered in `review/635-view-bounds`, the zero-element world crash has hardware red/green from @⁠XIIYAZ, and the overflow is now saturating. Nothing left that I can add.
Author
Owner

@⁠SlopComp on GitHub · 2026-11-24 17:08 UTC · original

Same crash on an RTX 5080 (driver 616.92), with a case this check doesn't catch: the first element is inside the copy but the view has no elements.

CreateUnorderedAccessView: ... StructureByteStride 16, FirstElement (value = 2670472) must be between 0 and the maximum offset of the Buffer, 11403263 ... NumElements (value = 0) must be between 1 and 8732792

The device was removed with DXGI_ERROR_INVALID_CALL. Making a view with NumElements == 0 a null view as well fixed it here.

**@⁠SlopComp** on GitHub · 2026-11-24 17:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7138741091) Same crash on an RTX 5080 (driver 616.92), with a case this check doesn't catch: the first element is inside the copy but the view has no elements. ``` CreateUnorderedAccessView: ... StructureByteStride 16, FirstElement (value = 2670472) must be between 0 and the maximum offset of the Buffer, 11403263 ... NumElements (value = 0) must be between 1 and 8732792 ``` The device was removed with `DXGI_ERROR_INVALID_CALL`. Making a view with `NumElements == 0` a null view as well fixed it here.
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 17:16 UTC · original

Checked this exact case against the current head 79f6e246 (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1): a NumElements == 0 buffer view is already a null view there, and no CreateUnorderedAccessView call can see it.

Every buffer view path — View::Shader on a buffer, View::Unordered on a buffer, and View::Placed on a buffer — funnels into buffer_uav/buffer_srv (descriptor/host.rs), and both now do, in order: saturate FirstElement, compute capacity = shadow Width / element, then cut_to_copy(first, count, capacity). cut_to_copy returns None when first >= capacity and when the cut count is 0, i.e. for NumElements == 0 with first anywhere inside the copy — exactly the case in your log (FirstElement = 2670472 inside the copy, NumElements = 0). The None becomes a null view plus a one-time log ("buffer view past the end of its host copy, or of no elements: null"), so the runtime error you quoted ("NumElements must be between 1 and 8732792") is never reached.

Evidence run here: cargo test -p victory-gpu --lib views_are_cut on 79f6e246 passes, and that test pins the case directly: assert_eq!(cut_to_copy(8, 0, 4096), None, "no elements") alongside the wrap case cut_to_copy(u64::MAX, 1, 4096) == None. So if you still see the NumElements == 0 device removal, the build was not 79f6e246 — on main (ffe310e) the cut does not exist yet (buffer.FirstElement += offset / element straight into CreateUnorderedAccessView), which reproduces your log exactly.

So: no code change needed on this branch for the zero-element case; it is covered since 5346bfa and red/green'd by @⁠XIIYAZ's world-entry crash (device removed before, 7 minutes in the world after). If you can still reproduce on 79f6e246 itself, send the branch sha you ran and the once! log line that fired (or didn't) — that would mean a path that bypasses buffer_uav, and I'll dig again.

**@⁠adamdotpy** on GitHub · 2026-11-24 17:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7138944943) Checked this exact case against the current head `79f6e246` (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1): **a `NumElements == 0` buffer view is already a null view there, and no `CreateUnorderedAccessView` call can see it.** Every buffer view path — `View::Shader` on a buffer, `View::Unordered` on a buffer, and `View::Placed` on a buffer — funnels into `buffer_uav`/`buffer_srv` (`descriptor/host.rs`), and both now do, in order: saturate `FirstElement`, compute `capacity = shadow Width / element`, then `cut_to_copy(first, count, capacity)`. `cut_to_copy` returns `None` when `first >= capacity` **and** when the cut count is 0, i.e. for `NumElements == 0` with `first` anywhere inside the copy — exactly the case in your log (`FirstElement = 2670472` inside the copy, `NumElements = 0`). The `None` becomes a null view plus a one-time log ("buffer view past the end of its host copy, or of no elements: null"), so the runtime error you quoted ("NumElements must be between 1 and 8732792") is never reached. Evidence run here: `cargo test -p victory-gpu --lib views_are_cut` on `79f6e246` passes, and that test pins the case directly: `assert_eq!(cut_to_copy(8, 0, 4096), None, "no elements")` alongside the wrap case `cut_to_copy(u64::MAX, 1, 4096) == None`. So if you still see the `NumElements == 0` device removal, the build was not `79f6e246` — on `main` (`ffe310e`) the cut does not exist yet (`buffer.FirstElement += offset / element` straight into `CreateUnorderedAccessView`), which reproduces your log exactly. So: no code change needed on this branch for the zero-element case; it is covered since `5346bfa` and red/green'd by @⁠XIIYAZ's world-entry crash (device removed before, 7 minutes in the world after). If you can still reproduce on `79f6e246` itself, send the branch sha you ran and the `once!` log line that fired (or didn't) — that would mean a path that bypasses `buffer_uav`, and I'll dig again.
Author
Owner

@⁠claude-GTA6 on GitHub · 2026-11-24 18:45 UTC · original

From a tester working with Claude (AI-assisted); measured on my own machine.

Housekeeping: I rewrote this branch's history to fix commit author metadata (a wrong local git identity), so three commit IDs changed. The contents are identical (same tree, same commits):

was now commit
fb29c17c c2f81ff3 Merge upstream main (the approved head)
5346bfa2 4a05b26d one cut_to_copy, tested without a device
79f6e246 8e58f536 saturating first element

@⁠adamdotpy: I took your device test as 62f3e9b5 (you're the author) on top of 8e58f536. On Windows 11 / RTX 5090, cargo test -p victory-gpu passes 53 (1 ignored), and clippy --all-targets --all-features --locked -D warnings is clean. I also checked that it bites: with the cut in cut_to_copy disabled, both it and views_are_cut_to_their_host_copy fail. Thanks for writing it.

**@⁠claude-GTA6** on GitHub · 2026-11-24 18:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7141103874) *From a tester working with Claude (AI-assisted); measured on my own machine.* Housekeeping: I rewrote this branch's history to fix commit author metadata (a wrong local git identity), so three commit IDs changed. The contents are identical (same tree, same commits): | was | now | commit | |---|---|---| | `fb29c17c` | `c2f81ff3` | Merge upstream main (the approved head) | | `5346bfa2` | `4a05b26d` | one `cut_to_copy`, tested without a device | | `79f6e246` | `8e58f536` | saturating first element | @⁠adamdotpy: I took your device test as `62f3e9b5` (you're the author) on top of `8e58f536`. On Windows 11 / RTX 5090, `cargo test -p victory-gpu` passes 53 (1 ignored), and clippy `--all-targets --all-features --locked -D warnings` is clean. I also checked that it bites: with the cut in `cut_to_copy` disabled, both it and `views_are_cut_to_their_host_copy` fail. Thanks for writing it.
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 19:04 UTC · original

Verified the new head 62f3e9b here (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1): the added device test a view past its host copy makes a null slot, one running past it a cut one passes — a FirstElement past the copy yields a null slot (kept(0) false) for both SRV and UAV, the last in-copy element is kept, and a view running past the end is cut. cargo test -p victory-gpu 53 passed / 0 failed / 1 ignored, clippy --all-targets --all-features --locked -D warnings clean, cargo build --release -p victory builds. Together with the host-side views_are_cut_to_their_host_copy and @⁠XIIYAZ's/SlopComp's hardware crashes this now covers past-the-end, wrapping, and zero-element views from both sides. Nothing left open from my side on this branch.

**@⁠adamdotpy** on GitHub · 2026-11-24 19:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7141566467) Verified the new head `62f3e9b` here (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1): the added device test `a view past its host copy makes a null slot, one running past it a cut one` passes — a `FirstElement` past the copy yields a null slot (`kept(0)` false) for both SRV and UAV, the last in-copy element is kept, and a view running past the end is cut. `cargo test -p victory-gpu` 53 passed / 0 failed / 1 ignored, clippy `--all-targets --all-features --locked -D warnings` clean, `cargo build --release -p victory` builds. Together with the host-side `views_are_cut_to_their_host_copy` and @⁠XIIYAZ's/SlopComp's hardware crashes this now covers past-the-end, wrapping, and zero-element views from both sides. Nothing left open from my side on this branch.
Author
Owner

@⁠claude-GTA6 on GitHub · 2026-11-24 21:48 UTC · original

From a tester working with Claude (AI-assisted); measured on my own machine.

Closing this as superseded by 4051149c on main ("null views for title buffer views that are empty or run past their copy"). It covers the cases this PR was for: zero-element views (@⁠XIIYAZ's and SlopComp's world-entry crash) and views past the copy's end. Its within saturates, so the wrap @⁠Yaekai found is covered too, and its views now take their own copy via shadow::for_view. This branch now conflicts with that redesign.

The one behavioural difference: here, a view that starts inside its copy but runs past the end was cut to the part that fits, while main makes it null, and within's test pins that on purpose. If cutting turns out to matter for a title (shaders reading zeros for the valid part), it can come back as a small follow-up on top of main.

Thanks to everyone who tested and reviewed: @⁠adamdotpy (including the device test for the cut), @⁠Vaximous, @⁠Yaekai, @⁠XIIYAZ, @⁠DenRakEiw and SlopComp.

**@⁠claude-GTA6** on GitHub · 2026-11-24 21:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/635#issuecomment-7145421668) *From a tester working with Claude (AI-assisted); measured on my own machine.* Closing this as superseded by `4051149c` on `main` ("null views for title buffer views that are empty or run past their copy"). It covers the cases this PR was for: zero-element views (@⁠XIIYAZ's and SlopComp's world-entry crash) and views past the copy's end. Its `within` saturates, so the wrap @⁠Yaekai found is covered too, and its views now take their own copy via `shadow::for_view`. This branch now conflicts with that redesign. The one behavioural difference: here, a view that starts inside its copy but runs past the end was cut to the part that fits, while `main` makes it null, and `within`'s test pins that on purpose. If cutting turns out to matter for a title (shaders reading zeros for the valid part), it can come back as a small follow-up on top of `main`. Thanks to everyone who tested and reviewed: @⁠adamdotpy (including the device test for the cut), @⁠Vaximous, @⁠Yaekai, @⁠XIIYAZ, @⁠DenRakEiw and SlopComp.

Pull request closed

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!635
No description provided.