GPU: buffer views past the end of their host copy are null, views running past it are cut #635
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!635
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/635/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 mergingFrom 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'sFirstElementonto 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 withDXGI_ERROR_INVALID_CALL:Without the debug layer the view is undefined behaviour.
Change
In both functions, after the offset is applied:
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 victoryandcargo test --release -p victory-gpuonmain: 43 passed.cargo fmt --checkgives the same warnings on this file as onmain. None are in the new lines.@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
CreateUnorderedAccessViewon a structured view, stride 16, withFirstElementin range andNumElements0 (the title's own record count). With this commit, on my branch:buffer view of no elementsfires 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(upstreammainbase):cargo test -p victory-gpu43 passed, 1 ignored; clippy clean; release builds.@Vaximous on GitHub · 2026-11-24 08:00 UTC · original
Hardware data point for the
NumElements == 0part of this PR. PR creation is closed right now, so I'm posting it here instead of opening a duplicate.NumElements == 0passed toCreateShaderResourceView/CreateUnorderedAccessViewremoves the device withDXGI_ERROR_INVALID_CALL(0x887A0001).gpu/zero-element-buffer-views(one commit, 25d3abd, on main6af0fae):descriptor::host::tests::buffer_views_of_no_elements_are_null.View::Placedbuffer SRVs/UAVs of one and of zero elements throughHostHeap::make_at.GetDeviceRemovedReasonstays S_OK.@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 onmain.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.
@adamdotpy on GitHub · 2026-11-24 15:12 UTC · original
Review of
fb29c17on Windows 11 / Rust 1.98.1:cargo test -p victory-gpu51 passed, 1 ignored;cargo clippy -p victory-gpu --all-targets --all-features -- -D warningsclean.The clamp itself is right, and one detail deserves a comment in the code:
capacitycomes from the host copy'sWidth, whichshadow::findpads 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:
Tests for both new branches. The current test covers
NumElements == 0only; a view whoseFirstElementis at or past capacity, and one that runs past the end, are untested. Both are cheap:shadow::registera 0x1000-byte title buffer (64 KiB after padding, so 4096 elements of 16 bytes), thenHostHeap::make_ata view at 4096 (null, slot not kept) and one at 4095 with 4 elements (kept, device not removed).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 FirstElementforces the union to 8-byte alignment afterShader4ComponentMapping, 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.
@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 offb29c17, the merge of upstreammaininto this branch):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.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-gpu52 passed, 1 ignored;cargo clippy -p victory-gpu --all-targets -- -D warningsclean.@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_copywith the padding rationale attached is what the code needed, and the six-case unit test covers every branch. I measured5346bfa2here (Windows 11, Rust 1.98.1,cargo test -p victory-gpu52 passed, 1 ignored; clippy--all-featuresclean), 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 reachesmake_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 of5346bfa, 56 lines, one file). It goes throughHostHeap::make_atfor both view kinds: slot not kept atFirstElement == capacity, kept atcapacity - 1with one element, kept and cut atcapacity - 1with four, andGetDeviceRemovedReasonalive 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.
@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 (includingbuffer_views_of_no_elements_are_nullon live D3D12 andviews_are_cut_to_their_host_copy).cargo clippy -p victory-gpu -- -D warnings: 0 warnings.buffer_srv/buffer_uav:buffer.FirstElement += offset / elementcould wrap in release (or panic in debug) if a guest descriptor carries a hugeFirstElement(e.g.u64::MAX), potentially wrapping back belowcapacity. Usingbuffer.FirstElement = buffer.FirstElement.saturating_add(offset / element)makescut_to_copyunconditionally reject it.Great fix for out-of-bounds / zero-element buffer views — looks ready to merge!
@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.FirstElementis the guest's own number:srv_desc/uav_descbuild 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, andbuffer_srv/buffer_uavaddoffset / elementto it with a plain+=(host.rs:536,host.rs:586). WithFirstElementnearu64::MAXthat 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:
cut_to_copystill rejectsfirst >= 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_addcannot reject a legitimate view:offsetis inside the copy andoffset / element <= Width / element == capacity, so a view that fits today hasFirstElement + offset / element <= 2 * capacityand saturates only when the guest number was already nonsense — which is exactly the case wherecut_to_copyshould 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/mainfinds 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-boundsbranch (adamdotpy@b8db48b) does exactly that —HostHeap::make_atfor both view kinds, slot not kept atFirstElement == capacity, kept atcapacity - 1with one element, kept and cut atcapacity - 1with four,GetDeviceRemovedReasonalive 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-firstdirection too, because a wrapped view is a kept view of the wrong bytes and nothing in the unit tests can see that.@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_uavalso make raw views out of structured ones, so the cut applies to the raw window as well.DXGI_ERROR_INVALID_CALLat world entry in every run (about 10 runs).count=0, first element well inside the copy), which this PR'scount > 0check makes null. No view started past the copy's end in that run.Unit test
views_are_cut_to_their_host_copypasses there too.🤖 Generated with Claude Code
@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 withDXGI_ERROR_INVALID_CALLat world entry, ten runs running, and a 7-minute world run with ~3,000 draws a frame afterwards — that is thecount > 0branch incut_to_copydoing its job on a real title, on hardware, not just inviews_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 uncheckedbuffer.FirstElement += offset / elementinbuffer_srv/buffer_uavthat @Yaekai flagged — a corrupt descriptor can wrap it in release and passcut_to_copywith a smallfirst, giving a view of the wrong part of the copy.saturating_addcloses it and cannot reject a legitimate view. If you want it, myreview/635-view-boundsbranch (adamdotpy@b8db48b) already has the device-side test for the cut, which would catch a regression in that direction too.@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. Bothbuffer_srvandbuffer_uavnow usesaturating_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-gpu52 passed (1 ignored), andcargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warningsis 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.
@adamdotpy on GitHub · 2026-11-24 16:27 UTC · original
Verified
79f6e246here (Windows 11, Rust 1.98.1, isolated target dir):cargo test -p victory-gpu --lib52 passed / 1 ignored, includingviews_are_cut_to_their_host_copy;cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warningsclean. The diff is exactly the twosaturating_addlines inbuffer_srv/buffer_uavwith 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) + 4becomes element 0 andcut_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 yoursaturating_addthe same input isu64::MAXand the view is null. And the "cannot reject a legitimate view" half holds arithmetically as well as by inspection: I looped everyfirst in 0..capacityfor three capacities andfirst.saturating_add(capacity)never saturates, since a fitting view sums to at most2 * 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.@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.
The device was removed with
DXGI_ERROR_INVALID_CALL. Making a view withNumElements == 0a null view as well fixed it here.@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): aNumElements == 0buffer view is already a null view there, and noCreateUnorderedAccessViewcall can see it.Every buffer view path —
View::Shaderon a buffer,View::Unorderedon a buffer, andView::Placedon a buffer — funnels intobuffer_uav/buffer_srv(descriptor/host.rs), and both now do, in order: saturateFirstElement, computecapacity = shadow Width / element, thencut_to_copy(first, count, capacity).cut_to_copyreturnsNonewhenfirst >= capacityand when the cut count is 0, i.e. forNumElements == 0withfirstanywhere inside the copy — exactly the case in your log (FirstElement = 2670472inside the copy,NumElements = 0). TheNonebecomes 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_cuton79f6e246passes, and that test pins the case directly:assert_eq!(cut_to_copy(8, 0, 4096), None, "no elements")alongside the wrap casecut_to_copy(u64::MAX, 1, 4096) == None. So if you still see theNumElements == 0device removal, the build was not79f6e246— onmain(ffe310e) the cut does not exist yet (buffer.FirstElement += offset / elementstraight intoCreateUnorderedAccessView), which reproduces your log exactly.So: no code change needed on this branch for the zero-element case; it is covered since
5346bfaand red/green'd by @XIIYAZ's world-entry crash (device removed before, 7 minutes in the world after). If you can still reproduce on79f6e246itself, send the branch sha you ran and theonce!log line that fired (or didn't) — that would mean a path that bypassesbuffer_uav, and I'll dig again.@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):
fb29c17cc2f81ff35346bfa24a05b26dcut_to_copy, tested without a device79f6e2468e58f536@adamdotpy: I took your device test as
62f3e9b5(you're the author) on top of8e58f536. On Windows 11 / RTX 5090,cargo test -p victory-gpupasses 53 (1 ignored), and clippy--all-targets --all-features --locked -D warningsis clean. I also checked that it bites: with the cut incut_to_copydisabled, both it andviews_are_cut_to_their_host_copyfail. Thanks for writing it.@adamdotpy on GitHub · 2026-11-24 19:04 UTC · original
Verified the new head
62f3e9bhere (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1): the added device testa view past its host copy makes a null slot, one running past it a cut onepasses — aFirstElementpast 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-gpu53 passed / 0 failed / 1 ignored, clippy--all-targets --all-features --locked -D warningsclean,cargo build --release -p victorybuilds. Together with the host-sideviews_are_cut_to_their_host_copyand @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.@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
4051149conmain("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. Itswithinsaturates, so the wrap @Yaekai found is covered too, and its views now take their own copy viashadow::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
mainmakes it null, andwithin'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 ofmain.Thanks to everyone who tested and reviewed: @adamdotpy (including the device test for the cut), @Vaximous, @Yaekai, @XIIYAZ, @DenRakEiw and SlopComp.
Pull request closed