GPU: reject overflowing FirstElement and NumElements arithmetic in buffer view creation #2541

Closed
github-bot wants to merge 1 commit from gh-pr/2541/head into gh-pr/2541/base
Owner

@⁠reeledMarek on GitHub · 2026-11-25 15:33 UTC · original
reeledMarek:fix/gpu-buffer-view-overflow → main · +64 −16 · 1 files · 1 commits · closed without merging

Problem

In crates/victory-gpu/src/descriptor/host.rs, buffer_range, unordered_range, buffer_srv, and buffer_uav computed address + buffer.FirstElement * element and u64::from(buffer.NumElements) * element with unchecked u64 arithmetic.

A large FirstElement (such as 1 << 60 with StructureByteStride = 16, or u64::MAX) panics in debug builds and silently wraps in release builds before reaching within(), producing a bogus start address. Additionally, buffer_range and unordered_range did not verify ViewDimension == BUFFER before reading the Buffer union member.

Fix

  • Introduce checked_buffer_range(base, first, count, element) using checked_mul and checked_add, rejecting any u64 overflow.
  • Verify desc.ViewDimension == D3D12_SRV_DIMENSION_BUFFER / D3D12_UAV_DIMENSION_BUFFER before accessing desc.Anonymous.Buffer.

Testing

  • Added synthetic test overflowing_buffer_ranges_are_refused in crates/victory-gpu/src/descriptor/host.rs.
  • Verified cargo test -p xtask --locked passes asset policy.
**@⁠reeledMarek** on GitHub · 2026-11-25 15:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/2541) `reeledMarek:fix/gpu-buffer-view-overflow` → `main` · +64 −16 · 1 files · 1 commits · closed without merging ### Problem In `crates/victory-gpu/src/descriptor/host.rs`, `buffer_range`, `unordered_range`, `buffer_srv`, and `buffer_uav` computed `address + buffer.FirstElement * element` and `u64::from(buffer.NumElements) * element` with unchecked `u64` arithmetic. A large `FirstElement` (such as `1 << 60` with `StructureByteStride = 16`, or `u64::MAX`) panics in debug builds and silently wraps in release builds before reaching `within()`, producing a bogus `start` address. Additionally, `buffer_range` and `unordered_range` did not verify `ViewDimension == BUFFER` before reading the `Buffer` union member. ### Fix - Introduce `checked_buffer_range(base, first, count, element)` using `checked_mul` and `checked_add`, rejecting any `u64` overflow. - Verify `desc.ViewDimension == D3D12_SRV_DIMENSION_BUFFER` / `D3D12_UAV_DIMENSION_BUFFER` before accessing `desc.Anonymous.Buffer`. ### Testing - Added synthetic test `overflowing_buffer_ranges_are_refused` in `crates/victory-gpu/src/descriptor/host.rs`. - Verified `cargo test -p xtask --locked` passes asset policy.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:08:42 +00:00
github-bot closed this pull request 2026-11-26 05:08:42 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 18:34 UTC · original

Automated triage for the maintainers.

Replaces the unchecked base + FirstElement * element / NumElements * element arithmetic in buffer_range, unordered_range, buffer_srv and buffer_uav (crates/victory-gpu/src/descriptor/host.rs) with checked_buffer_range, and adds ViewDimension == BUFFER checks before the union read.

As it stands, the value is unclear:

  • Every caller of buffer_range reaches it only for a buffer resource (buffer_bytes(..)? or resource[0] == 1), and D3D12 only allows buffer-dimension views on buffers. So the new ViewDimension checks guard a case that cannot occur.
  • FirstElement/NumElements come from the title's own view descriptions. Release builds wrap, and shadow::for_view/within (which already saturates) then refuse the view. So the overflow only matters for a debug-build panic on a garbage descriptor.

What would change the verdict: a real case where such a view reaches these functions, such as a log line from a run or a descriptor path where uninitialised words are read. Without one, this is a guard for a case that does not occur, and a maintainer may prefer to close it.

CI: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 18:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/2541#issuecomment-7171086837) <!-- victory-triage v1 head=79ba82d0ebc5019c10dda0617fc28ceee4693521 verdict=needs-work at=2026-11-25T18:33:56Z --> Automated triage for the maintainers. Replaces the unchecked `base + FirstElement * element` / `NumElements * element` arithmetic in `buffer_range`, `unordered_range`, `buffer_srv` and `buffer_uav` (`crates/victory-gpu/src/descriptor/host.rs`) with `checked_buffer_range`, and adds `ViewDimension == BUFFER` checks before the union read. As it stands, the value is unclear: - Every caller of `buffer_range` reaches it only for a buffer resource (`buffer_bytes(..)?` or `resource[0] == 1`), and D3D12 only allows buffer-dimension views on buffers. So the new `ViewDimension` checks guard a case that cannot occur. - `FirstElement`/`NumElements` come from the title's own view descriptions. Release builds wrap, and `shadow::for_view`/`within` (which already saturates) then refuse the view. So the overflow only matters for a debug-build panic on a garbage descriptor. What would change the verdict: a real case where such a view reaches these functions, such as a log line from a run or a descriptor path where uninitialised words are read. Without one, this is a guard for a case that does not occur, and a maintainer may prefer to close it. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠reeledMarek on GitHub · 2026-11-25 20:05 UTC · original

Closing per triage — release builds wrap and within() already rejects invalid buffer views without a concrete triggering title descriptor. Thanks for the review!

**@⁠reeledMarek** on GitHub · 2026-11-25 20:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/2541#issuecomment-7173180624) Closing per triage — release builds wrap and `within()` already rejects invalid buffer views without a concrete triggering title descriptor. Thanks for the review!

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