GPU: a raw buffer view's range stays inside its buffer (a description the driver refuses leaves the slot undefined) #1266

Open
github-bot wants to merge 4 commits from github-bot/gh-1266 into main AGit
Owner

@⁠lichenlang89 on GitHub · 2026-11-26 05:39 UTC · original
lichenlang89:fix/raw-buffer-view-range → main · +242 −6 · 1 files · 4 commits · open

What

descriptor/host.rs' raw_at() and raw_uav_at() turn the title's byte offset and length into a buffer view without checking either against the buffer:

let elements = (len / 4).clamp(1, u64::from(u32::MAX)) as u32;
...
FirstElement: offset / 4,
NumElements: elements,

CreateShaderResourceView and CreateUnorderedAccessView return nothing, so a description that runs past the buffer is refused with no line from us, and what a refused creation leaves in the slot is undefined. The slot is shader-visible: a later draw or dispatch may read through it.

This branch keeps the range inside the buffer. The start is used exactly as asked, the length is cut at what the buffer holds and at u32::MAX, and a request that lies outside the buffer gets the null view the file already has (null_raw_at, null_raw_uav_at).

Correctness

  • The start is never moved. asked = offset / 4 is what goes into FirstElement; nothing narrows it to 32 bits and nothing clamps it back. A test asserts first == offset / 4 over every accepted case.
  • A request entirely outside the buffer is refused: asked >= size / 4 → None → null view, never a view of the buffer's tail.
  • Only the length is cut, at the dwords the buffer has left and at u32::MAX (NumElements is 32-bit, FirstElement is 64-bit).
  • A start off 16 bytes is refused rather than moved. Callers already pass aligned offsets: draw.rs biases by offset & 15 on the UAV path and makes a null view for a root SRV that is not on 16 bytes; memory.rs passes 0.
  • A length under a dword keeps the previous floor of one element (the buffer's end still caps it), so nothing that was one element before becomes nothing.
  • The counters name why a range was lost — Past, Length, Short, Unaligned, each for SRV and UAV — and say so on the first and every 4096th event. A legal, uncut request touches no counter and logs nothing.

Correction after review

The first revision of this branch clamped the start to the buffer's end, which mapped a request that lies entirely past the buffer onto the buffer's tail:

buffer size = 100 bytes
offset      = 112
length      = 16      -> the old code made a view of bytes [96, 100), which the request never named

That was an error introduced by this PR's first revision — it is not in be721d5a, where such a request simply produces an out-of-range description. It is fixed in dafd86a: the clamp is gone, the start is used as asked, and the table test above is the regression test. The first revision also narrowed the start to u32::MAX; that is gone too.

Testing

cargo test -p victory-gpu --lib raw_range                       6 passed; 0 failed; 214 filtered out
cargo test -p victory-gpu --lib -- --test-threads=1           214 passed; 0 failed; 6 ignored
cargo check -p victory-gpu                                     finished, no new warnings
cargo build --release -p victory                               finished, no new warnings

Six focused tests: the review's table including the counterexample; the four cause labels plus "no counter when nothing was cut"; a start past 32 bits keeps its 64-bit value; the length is cut at u32::MAX (1 TiB, len = u64::MAX → (0, u32::MAX)); an aligned start past the buffer is refused as Past rather than through the unaligned branch; and 640 shapes asserting the start is unchanged, the range ends inside the buffer, the start is on 16 bytes, and no request the input rules allow is refused.

One known flaky test, unrelated to this change but visible here: inplace::tests::warp_a_title_buffer_in_place_shows_its_bytes_at_its_offset. Controlled on one machine, alternating, ten parallel runs each:

tree runs failed crashed
be721d5a (untouched) 10 5 1 (STATUS_ACCESS_VIOLATION)
this branch 10 6 0

Always the same test, which uses the WARP adapter and never calls raw_at/raw_uav_at; it passes on both trees when run alone, and both pass with --test-threads=1. The rates are indistinguishable at ten runs each, so this branch neither causes it nor is cleared of making it worse — it is reported rather than worked around: no test was skipped, disabled or changed.

Not run: a real-GPU regression with the new commits. No earlier run's result is offered in its place.

Limitations

  • This is descriptor range correctness / hardening, not a proven device-removal fix. The runs in which removals stopped carried both this guard and the constant-buffer cap (constant_view_size(), fork PR #1206), and the guard's counters stayed at 0, so it has never fired on the machine we measured.
  • One run of 1 h 28 min in the world without a removal used a build that predates the counters, its log has since been overwritten, and the same build carried the constant-buffer cap: it cannot be used as evidence about this branch.
  • GetDesc() is called once per raw view. Not measured against keeping the width beside the slot.
  • Unknown, and not addressed here: whether a descriptor slot rewritten on the CPU can still be referenced by an in-flight or not-yet-submitted list. That belongs to the descriptor heap's existing ownership and retirement rules, not to this range arithmetic.

🤖 Generated with Claude Code

**@⁠lichenlang89** on GitHub · 2026-11-26 05:39 UTC · [original](https://github.com/fxceless/victory-fork/pull/1266) `lichenlang89:fix/raw-buffer-view-range` → `main` · +242 −6 · 1 files · 4 commits · open ## What `descriptor/host.rs`' `raw_at()` and `raw_uav_at()` turn the title's byte offset and length into a buffer view without checking either against the buffer: ```rust let elements = (len / 4).clamp(1, u64::from(u32::MAX)) as u32; ... FirstElement: offset / 4, NumElements: elements, ``` `CreateShaderResourceView` and `CreateUnorderedAccessView` return nothing, so a description that runs past the buffer is refused with no line from us, and what a refused creation leaves in the slot is undefined. The slot is shader-visible: a later draw or dispatch may read through it. This branch keeps the range inside the buffer. The start is used exactly as asked, the length is cut at what the buffer holds and at `u32::MAX`, and a request that lies outside the buffer gets the null view the file already has (`null_raw_at`, `null_raw_uav_at`). ## Correctness - **The start is never moved.** `asked = offset / 4` is what goes into `FirstElement`; nothing narrows it to 32 bits and nothing clamps it back. A test asserts `first == offset / 4` over every accepted case. - **A request entirely outside the buffer is refused**: `asked >= size / 4` → `None` → null view, never a view of the buffer's tail. - **Only the length is cut**, at the dwords the buffer has left and at `u32::MAX` (`NumElements` is 32-bit, `FirstElement` is 64-bit). - **A start off 16 bytes is refused** rather than moved. Callers already pass aligned offsets: `draw.rs` biases by `offset & 15` on the UAV path and makes a null view for a root SRV that is not on 16 bytes; `memory.rs` passes 0. - **A length under a dword keeps the previous floor** of one element (the buffer's end still caps it), so nothing that was one element before becomes nothing. - **The counters** name why a range was lost — `Past`, `Length`, `Short`, `Unaligned`, each for SRV and UAV — and say so on the first and every 4096th event. A legal, uncut request touches no counter and logs nothing. ## Correction after review The first revision of this branch clamped the start to the buffer's end, which mapped a request that lies entirely past the buffer onto the buffer's tail: ```text buffer size = 100 bytes offset = 112 length = 16 -> the old code made a view of bytes [96, 100), which the request never named ``` That was an error introduced by this PR's first revision — it is not in `be721d5a`, where such a request simply produces an out-of-range description. It is fixed in `dafd86a`: the clamp is gone, the start is used as asked, and the table test above is the regression test. The first revision also narrowed the start to `u32::MAX`; that is gone too. ## Testing ``` cargo test -p victory-gpu --lib raw_range 6 passed; 0 failed; 214 filtered out cargo test -p victory-gpu --lib -- --test-threads=1 214 passed; 0 failed; 6 ignored cargo check -p victory-gpu finished, no new warnings cargo build --release -p victory finished, no new warnings ``` Six focused tests: the review's table including the counterexample; the four cause labels plus "no counter when nothing was cut"; a start past 32 bits keeps its 64-bit value; the length is cut at `u32::MAX` (1 TiB, `len = u64::MAX` → `(0, u32::MAX)`); an aligned start past the buffer is refused as `Past` rather than through the unaligned branch; and 640 shapes asserting the start is unchanged, the range ends inside the buffer, the start is on 16 bytes, and no request the input rules allow is refused. **One known flaky test, unrelated to this change but visible here**: `inplace::tests::warp_a_title_buffer_in_place_shows_its_bytes_at_its_offset`. Controlled on one machine, alternating, ten parallel runs each: | tree | runs | failed | crashed | |---|---|---|---| | `be721d5a` (untouched) | 10 | 5 | 1 (`STATUS_ACCESS_VIOLATION`) | | this branch | 10 | 6 | 0 | Always the same test, which uses the WARP adapter and never calls `raw_at`/`raw_uav_at`; it passes on both trees when run alone, and both pass with `--test-threads=1`. The rates are indistinguishable at ten runs each, so this branch neither causes it nor is cleared of making it worse — it is reported rather than worked around: no test was skipped, disabled or changed. **Not run:** a real-GPU regression with the new commits. No earlier run's result is offered in its place. ## Limitations - This is **descriptor range correctness / hardening**, not a proven device-removal fix. The runs in which removals stopped carried both this guard and the constant-buffer cap (`constant_view_size()`, fork PR #1206), and the guard's counters stayed at 0, so it has never fired on the machine we measured. - One run of 1 h 28 min in the world without a removal used a build that predates the counters, its log has since been overwritten, and the same build carried the constant-buffer cap: it cannot be used as evidence about this branch. - `GetDesc()` is called once per raw view. Not measured against keeping the width beside the slot. - **Unknown, and not addressed here:** whether a descriptor slot rewritten on the CPU can still be referenced by an in-flight or not-yet-submitted list. That belongs to the descriptor heap's existing ownership and retirement rules, not to this range arithmetic. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
CreateShaderResourceView and CreateUnorderedAccessView return nothing, so a
description the driver refuses leaves the slot holding whatever resource it
described before. A compute dispatch that reads such a slot reports a page
fault and removes the device.

Build the view from the buffer's own size: FirstElement and NumElements stay
inside GetDesc().Width, and an empty range installs the null view instead of a
description the driver will refuse.
Review of the previous commit: FirstElement is 64-bit, so clamping it to
u32::MAX would move a view's start on a resource larger than 16 GiB, and
the patch had no way to say whether it ever applied.

The start now keeps its own value and is only clamped to the buffer's end
(and put back on the 16-byte boundary a raw view needs); only NumElements,
which is 32-bit, is cut at u32::MAX. A raw view asked for off that boundary
is named once. Both paths count the ranges they cut or empty and say so on
the first and every 4096th, so a run shows whether the guard applied at all.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:07:23 +00:00
Review of the previous commit: clamping the start to the buffer's end turned a
request that lies entirely past the buffer into a view of its tail -- 100 bytes
of buffer with an offset of 112 viewed bytes 96 to 99. The clamp is gone: a
start at or past the last dword is a view of nothing, an offset off the 16 bytes
a raw view needs is refused rather than moved, and the length alone is cut, at
the buffer's end and at u32::MAX.

The counters say why a range was lost (past the end, cut, under a dword,
unaligned) and for which path (SRV, UAV), and the tests cover the table of the
review, a start past 32 bits, and the invariant over the shapes the title uses.
The second review: name the range function for what it is -- pure arithmetic
over (size, offset, length), with the counting and the log left to the callers,
so that the tests exercise it without touching a counter -- and cover what the
first round of tests left implicit: the start is never moved, an acceptable
request is never refused, an aligned start past the buffer is refused as such
(not through the unaligned branch), and the length alone is cut at u32::MAX.

The comments no longer claim what a refused description does to the device, and
no longer claim to know why the title asks for less than a dword.
Author
Owner

@⁠CuteBooty on GitHub · 2026-11-26 08:28 UTC · original

RTX 3070 8 GB, Series S: ported; all eight counters stayed at zero for a whole run

  • Machine and tree as above (CuteBooty/victory-8gb, where the port is now in the published tree, credited, behind VICTORY_RAW_VIEW_CLAMP, on by default).
  • One 13-minute run in town and in open water, 117 five-second lines, 7,000-9,000 draws for most of it: raw view clamp (past end, shortened, sub-dword, unaligned) since start: SRV [0, 0, 0, 0], UAV [0, 0, 0, 0] from the first line to the last.
  • No errors, no change in picture or frame time (201 ms at 7,000-9,000 draws against 203-223 on the same tree without it; within the spread of runs).

So on this title, card and mode the guarded cases do not occur, and the fix costs nothing. The port's six tests pass here (quick suite 379 passed). If you know a scene or card where the counters move, I can run it.

**@⁠CuteBooty** on GitHub · 2026-11-26 08:28 UTC · [original](https://github.com/fxceless/victory-fork/pull/1266#issuecomment-7186646109) **RTX 3070 8 GB, Series S: ported; all eight counters stayed at zero for a whole run** - Machine and tree as above (CuteBooty/victory-8gb, where the port is now in the published tree, credited, behind `VICTORY_RAW_VIEW_CLAMP`, on by default). - One 13-minute run in town and in open water, 117 five-second lines, 7,000-9,000 draws for most of it: `raw view clamp (past end, shortened, sub-dword, unaligned) since start: SRV [0, 0, 0, 0], UAV [0, 0, 0, 0]` from the first line to the last. - No errors, no change in picture or frame time (201 ms at 7,000-9,000 draws against 203-223 on the same tree without it; within the spread of runs). So on this title, card and mode the guarded cases do not occur, and the fix costs nothing. The port's six tests pass here (quick suite 379 passed). If you know a scene or card where the counters move, I can run it.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/1266/head:github-bot/gh-1266'
git switch 'github-bot/gh-1266'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1266'
git switch 'github-bot/gh-1266'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1266'
git switch 'github-bot/gh-1266'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1266'
git switch 'main'
git merge --squash 'github-bot/gh-1266'
git switch 'main'
git merge --ff-only 'github-bot/gh-1266'
git switch 'main'
git merge 'github-bot/gh-1266'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.