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

Open
lichenlang89 wants to merge 4 commits from upstream-pr/1266 into main
lichenlang89 commented 2026-11-26 05:39:04 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by lichenlang89 on 2026-11-26T05:39:04Z.


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

<!-- greyfinch-sync:fxceless/victory-fork:issue:5504662768 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1266). Originally posted by [lichenlang89](https://github.com/lichenlang89) on 2026-11-26T05:39:04Z. --- ## 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)
CuteBooty commented 2026-11-26 09:08:04 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by CuteBooty on 2026-11-26T08:28:55Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7186646109 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1266#issuecomment-7186646109). Originally posted by [CuteBooty](https://github.com/CuteBooty) on 2026-11-26T08:28:55Z. --- **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.
lichenlang89 commented 2026-11-26 10:06:46 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by lichenlang89 on 2026-11-26T09:16:20Z.


Thanks for the independent run — and for the periodic cumulative line, which is better than what this branch does (first event, then every 4096th, so a finished run cannot read a total). Two notes from this side:

  • Same outcome on a different card and mode: RTX 4060 Laptop 8 GB, Windows 11, Series X. The counters were at 0 in every run that carried them (three sessions of 9 to 14 minutes), and the guard has not fired once.
  • Where they would move, if anywhere: Unaligned and Past stay at 0 only while the callers keep their side of the contract — draw.rs biases the UAV offset by offset & 15 and makes a null view for a root SRV that is not on 16 bytes, and memory.rs passes 0. A caller added later that passes its own offset is the likeliest source. Length needs a title window that asks past its host buffer, which neither of us has seen.

So the shape I would keep: the guard stays small, and if a counter ever moves it names the cause, which is what Past / Length / Unaligned are for.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7187696368 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1266#issuecomment-7187696368). Originally posted by [lichenlang89](https://github.com/lichenlang89) on 2026-11-26T09:16:20Z. --- Thanks for the independent run — and for the periodic cumulative line, which is better than what this branch does (first event, then every 4096th, so a finished run cannot read a total). Two notes from this side: - Same outcome on a different card and mode: RTX 4060 Laptop 8 GB, Windows 11, Series X. The counters were at 0 in every run that carried them (three sessions of 9 to 14 minutes), and the guard has not fired once. - Where they would move, if anywhere: `Unaligned` and `Past` stay at 0 only while the callers keep their side of the contract — `draw.rs` biases the UAV offset by `offset & 15` and makes a null view for a root SRV that is not on 16 bytes, and `memory.rs` passes 0. A caller added later that passes its own offset is the likeliest source. `Length` needs a title window that asks past its host buffer, which neither of us has seen. So the shape I would keep: the guard stays small, and if a counter ever moves it names the cause, which is what `Past` / `Length` / `Unaligned` are for.
lichenlang89 commented 2026-11-28 06:09:57 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by lichenlang89 on 2026-11-28T06:09:57Z.


Hi, I noticed that after the switch to VIctory-NVIDIA in 5dbf5ce2, the raw buffer range implementation and its regression tests from this PR are already present in the new baseline.

Glad to see these changes have made their way into the main branch!

Since the original PR is still open and now has merge conflicts, should we consider it superseded by the imported changes and close it?

Also, could you clarify how the original contribution is credited in the new baseline?

Thanks!

<!-- greyfinch-sync:fxceless/victory-fork:comment:7235121642 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1266#issuecomment-7235121642). Originally posted by [lichenlang89](https://github.com/lichenlang89) on 2026-11-28T06:09:57Z. --- Hi, I noticed that after the switch to VIctory-NVIDIA in `5dbf5ce2`, the raw buffer range implementation and its regression tests from this PR are already present in the new baseline. Glad to see these changes have made their way into the main branch! Since the original PR is still open and now has merge conflicts, should we consider it superseded by the imported changes and close it? Also, could you clarify how the original contribution is credited in the new baseline? Thanks!
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/1266:upstream-pr/1266'
git switch 'upstream-pr/1266'

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/1266'
git switch 'upstream-pr/1266'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1266'
git switch 'upstream-pr/1266'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1266'
git switch 'main'
git merge --squash 'upstream-pr/1266'
git switch 'main'
git merge --ff-only 'upstream-pr/1266'
git switch 'main'
git merge 'upstream-pr/1266'
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
fxceless/victory-fork!1263
No description provided.