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

Closed
reeledMarek wants to merge 1 commit from upstream-pr/2541 into main
reeledMarek commented 2026-11-25 15:33:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-25T15:33:43Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6608063727 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2541). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-25T15:33:43Z. --- ### 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.
Marvisak commented 2026-11-25 19:06:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T18:34:07Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7171086837 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2541#issuecomment-7171086837). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T18:34:07Z. --- <!-- 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)_
reeledMarek commented 2026-11-25 20:06:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-25T20:05:36Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7173180624 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2541#issuecomment-7173180624). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-25T20:05:36Z. --- Closing per triage — release builds wrap and `within()` already rejects invalid buffer views without a concrete triggering title descriptor. Thanks for the review!
upstream-sync closed this pull request 2026-11-25 21:02:33 +00:00

Pull request closed

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
Marvisak/VIctory!2539
No description provided.