GPU: check overflow and SizeInBytes truncation in State::rectangles #2444

Closed
Texbirds wants to merge 1 commit from upstream-pr/2446 into main
Texbirds commented 2026-11-25 13:39:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-25T13:39:12Z.


Problem

In crates/victory-gpu/src/draw.rs, State::rectangles() validates each bound vertex buffer before scheduling triangle expansion into scratch memory:

  1. u64::from(first + count) * stride > u64::from(v.size) adds first + count in u32 before widening to u64 (panicking in debug builds or wrapping in release builds and bypassing the v.size check).
  2. v.address + u64::from(first) * stride (and the end of the source slice from + src_bytes) uses unchecked u64 arithmetic.
  3. D3D12_VERTEX_BUFFER_VIEW::SizeInBytes casts (rects * 6 * stride as u64) as u32, silently truncating if the expanded 2x triangle buffer exceeds u32::MAX while the source fit in v.size.

Change

  • Extract rect_vertex_range(v: VertexView, first: u32, count: u32) -> Option<(u64, u64, u32)>, performing widened u64 and checked arithmetic for first_bytes, (first + count) * stride, v.address + first_bytes, from + src_bytes, and u32::try_from for dst_bytes.
  • Update State::rectangles() to reject the draw cleanly (same once! warning and return false path as out-of-bounds views) when rect_vertex_range returns None, and pass dst_bytes directly to list.scratch and D3D12_VERTEX_BUFFER_VIEW::SizeInBytes.
  • Normal in-bounds rectangle draws are unchanged.

Test

rect_vertex_range_rejects_u32_wrap_address_overflow_and_size_truncation (synthetic VertexView values, no GPU or game data required):

  • Verifies a valid 1-rectangle slice starting at vertex 3 returns Some((0x1030, 48, 96)).
  • Verifies first = u32::MAX, count = 3 (u32 wrap) returns None.
  • Verifies v.address = u64::MAX - 8 with first = 1 and first = 0 (u64 address overflow on start and end) returns None.
  • Verifies a view whose source fits in u32::MAX (0xC000_0000 bytes) but whose 2x expanded buffer (0x1_8000_0000 bytes) overflows u32 returns None.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6605779272 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2446). Originally posted by [Texbirds](https://github.com/Texbirds) on 2026-11-25T13:39:12Z. --- ### Problem In `crates/victory-gpu/src/draw.rs`, `State::rectangles()` validates each bound vertex buffer before scheduling triangle expansion into scratch memory: 1. `u64::from(first + count) * stride > u64::from(v.size)` adds `first + count` in `u32` before widening to `u64` (panicking in debug builds or wrapping in release builds and bypassing the `v.size` check). 2. `v.address + u64::from(first) * stride` (and the end of the source slice `from + src_bytes`) uses unchecked `u64` arithmetic. 3. `D3D12_VERTEX_BUFFER_VIEW::SizeInBytes` casts `(rects * 6 * stride as u64) as u32`, silently truncating if the expanded 2x triangle buffer exceeds `u32::MAX` while the source fit in `v.size`. ### Change - Extract `rect_vertex_range(v: VertexView, first: u32, count: u32) -> Option<(u64, u64, u32)>`, performing widened `u64` and checked arithmetic for `first_bytes`, `(first + count) * stride`, `v.address + first_bytes`, `from + src_bytes`, and `u32::try_from` for `dst_bytes`. - Update `State::rectangles()` to reject the draw cleanly (same `once!` warning and `return false` path as out-of-bounds views) when `rect_vertex_range` returns `None`, and pass `dst_bytes` directly to `list.scratch` and `D3D12_VERTEX_BUFFER_VIEW::SizeInBytes`. - Normal in-bounds rectangle draws are unchanged. ### Test `rect_vertex_range_rejects_u32_wrap_address_overflow_and_size_truncation` (synthetic `VertexView` values, no GPU or game data required): - Verifies a valid 1-rectangle slice starting at vertex 3 returns `Some((0x1030, 48, 96))`. - Verifies `first = u32::MAX, count = 3` (`u32` wrap) returns `None`. - Verifies `v.address = u64::MAX - 8` with `first = 1` and `first = 0` (`u64` address overflow on start and end) returns `None`. - Verifies a view whose source fits in `u32::MAX` (`0xC000_0000` bytes) but whose 2x expanded buffer (`0x1_8000_0000` bytes) overflows `u32` returns `None`.
DenRakEiw commented 2026-11-25 14:07:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T13:57:21Z.

Review: APPROVED


Verified against origin/main (940d32f) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP:

  • Checking overflow and SizeInBytes truncation in State::rectangles cleanly rejects wrapping/overflowing vertex ranges.
  • cargo test -p victory-gpu draw::tests: 6/6 passed on RTX 5090, 6/6 passed on WARP (including rect_vertex_range_rejects_u32_wrap_address_overflow_and_size_truncation).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6058934394 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2446#pullrequestreview-6058934394). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T13:57:21Z. **Review:** APPROVED --- Verified against `origin/main` (`940d32f`) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP: - Checking overflow and `SizeInBytes` truncation in `State::rectangles` cleanly rejects wrapping/overflowing vertex ranges. - `cargo test -p victory-gpu draw::tests`: 6/6 passed on RTX 5090, 6/6 passed on WARP (including `rect_vertex_range_rejects_u32_wrap_address_overflow_and_size_truncation`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: 0 warnings. - `cargo xtask policy`: 183 files ok. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 15:03:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T14:11:21Z.

Review: COMMENTED


Overflow + SizeInBytes truncation checked in StateObject. Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1.

<!-- greyfinch-sync:Marvisak/VIctory:review:6059178489 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2446#pullrequestreview-6059178489). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T14:11:21Z. **Review:** COMMENTED --- Overflow + SizeInBytes truncation checked in StateObject. Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1.
Marvisak commented 2026-11-25 16:09:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T16:01:36Z.


Automated triage for the maintainers.

Proposed close: the checks added to State::rectangles in crates/victory-gpu/src/draw.rs (rect_vertex_range) cover inputs the title does not produce (a buffer address near the top of the 64-bit range, a start vertex near 2^32, an expansion over 4 GiB), and on main such ranges are already refused by crate::shadow::find or the scratch allocation. If a real draw from the title reaches one of these paths, please describe it. This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label. CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167526347 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2446#issuecomment-7167526347). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T16:01:36Z. --- <!-- victory-triage v1 head=25482e30acd8de8f3377d2d5fbfa8854dc43a7dc verdict=proposed-close at=2026-11-25T15:59:11Z --> Automated triage for the maintainers. Proposed close: the checks added to `State::rectangles` in `crates/victory-gpu/src/draw.rs` (`rect_vertex_range`) cover inputs the title does not produce (a buffer address near the top of the 64-bit range, a start vertex near 2^32, an expansion over 4 GiB), and on main such ranges are already refused by `crate::shadow::find` or the scratch allocation. If a real draw from the title reaches one of these paths, please describe it. This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync closed this pull request 2026-11-25 17:04:19 +00:00
Texbirds commented 2026-11-25 17:05:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-25T16:10:18Z.


Automated triage for the maintainers.

Proposed close: the checks added to State::rectangles in crates/victory-gpu/src/draw.rs (rect_vertex_range) cover inputs the title does not produce (a buffer address near the top of the 64-bit range, a start vertex near 2^32, an expansion over 4 GiB), and on main such ranges are already refused by crate::shadow::find or the scratch allocation. If a real draw from the title reaches one of these paths, please describe it. This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label. CI: passing.

Generated by Claude Code

Fair point — these overflow cases aren't hit by real title draws and shadow::find / list.scratch already reject out-of-range spans on main. Closing to keep the queue clean.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167655989 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2446#issuecomment-7167655989). Originally posted by [Texbirds](https://github.com/Texbirds) on 2026-11-25T16:10:18Z. --- > Automated triage for the maintainers. > > Proposed close: the checks added to `State::rectangles` in `crates/victory-gpu/src/draw.rs` (`rect_vertex_range`) cover inputs the title does not produce (a buffer address near the top of the 64-bit range, a start vertex near 2^32, an expansion over 4 GiB), and on main such ranges are already refused by `crate::shadow::find` or the scratch allocation. If a real draw from the title reaches one of these paths, please describe it. This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label. CI: passing. > > _Generated by [Claude Code](https://claude.ai/code)_ Fair point — these overflow cases aren't hit by real title draws and shadow::find / list.scratch already reject out-of-range spans on main. Closing to keep the queue clean.
kotekompotek commented 2026-11-25 17:05:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T16:52:27Z.


Verified the arithmetic: the old code had a real u32 wrap — u64::from(first + count) added in u32 first, so first=u32::MAX wrapped to 2 and bypassed the v.size bound; plus rects * 6 * stride truncated to u32 silently for SizeInBytes. The new checked_* chain covers both, and the semantics match the original (bound by first + count even though only rects * 3 vertices are read — conservative, unchanged). Test vectors check out ((3,3) → 0x1030/48/96).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7168661525 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2446#issuecomment-7168661525). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T16:52:27Z. --- Verified the arithmetic: the old code had a real u32 wrap — `u64::from(first + count)` added in u32 first, so `first=u32::MAX` wrapped to 2 and bypassed the `v.size` bound; plus `rects * 6 * stride` truncated to u32 silently for `SizeInBytes`. The new `checked_*` chain covers both, and the semantics match the original (bound by `first + count` even though only `rects * 3` vertices are read — conservative, unchanged). Test vectors check out (`(3,3)` → `0x1030/48/96`).

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