fix(gpu): guard rectangle vertex expansion against bounds overflow and partial slot updates #3371

Open
github-bot wants to merge 1 commit from github-bot/gh-3371 into main AGit
Owner

@⁠reeledMarek on GitHub · 2026-11-26 20:40 UTC · original
reeledMarek:fix/gpu-rectangles-bounds-overflow → main · +90 −12 · 1 files · 1 commits · open

Summary

  • In State::rectangles, u64::from(first + count) * stride > u64::from(v.size) evaluated first + count in 32-bit arithmetic (panicking in debug or wrapping in release to bypass the bounds check), v.address + u64::from(first) * stride could overflow u64, and (rects * 6 * stride as u64) as u32 could silently truncate D3D12_VERTEX_BUFFER_VIEW::SizeInBytes (or overflow (vertices / 3) * 6 in DrawInstanced).
  • Additionally, if a later vertex slot failed validation, earlier slots had already scheduled defer_host expansions and overwritten IASetVertexBuffers with scratch views.
  • Extracts rect_vertex_range with full checked arithmetic for bounds, addresses, and expanded sizes, and validates/allocates all slots before scheduling deferred expansions or calling IASetVertexBuffers.
  • Adds regression test rect_vertex_range_rejects_overflow_and_out_of_bounds.

Verification

  • cargo clippy -p victory-gpu --all-targets --target x86_64-pc-windows-msvc --locked -- -D warnings
  • cargo test -p xtask --locked
**@⁠reeledMarek** on GitHub · 2026-11-26 20:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/3371) `reeledMarek:fix/gpu-rectangles-bounds-overflow` → `main` · +90 −12 · 1 files · 1 commits · open ### Summary - In `State::rectangles`, `u64::from(first + count) * stride > u64::from(v.size)` evaluated `first + count` in 32-bit arithmetic (panicking in debug or wrapping in release to bypass the bounds check), `v.address + u64::from(first) * stride` could overflow `u64`, and `(rects * 6 * stride as u64) as u32` could silently truncate `D3D12_VERTEX_BUFFER_VIEW::SizeInBytes` (or overflow `(vertices / 3) * 6` in `DrawInstanced`). - Additionally, if a later vertex slot failed validation, earlier slots had already scheduled `defer_host` expansions and overwritten `IASetVertexBuffers` with scratch views. - Extracts `rect_vertex_range` with full checked arithmetic for bounds, addresses, and expanded sizes, and validates/allocates all slots before scheduling deferred expansions or calling `IASetVertexBuffers`. - Adds regression test `rect_vertex_range_rejects_overflow_and_out_of_bounds`. ### Verification - `cargo clippy -p victory-gpu --all-targets --target x86_64-pc-windows-msvc --locked -- -D warnings` - `cargo test -p xtask --locked`
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 20:43:39 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 20:47 UTC · original · approved these changes

LGTM — verified on RTX 5090 (Windows / D3D12) against origin/main (63bcca4).

  • Guarding rectangle vertex expansion against bounds overflow and avoiding partial slot updates on failure prevents out-of-bounds reads and inconsistent vertex buffer state.
  • cargo test -p victory-gpu --lib draw:: -- --test-threads=1: 21 passed, 0 failed (including rect_vertex_range_rejects_overflow_and_out_of_bounds).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 20:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/3371#pullrequestreview-6080335923) · **approved these changes** LGTM — verified on RTX 5090 (Windows / D3D12) against `origin/main` (`63bcca4`). - Guarding rectangle vertex expansion against bounds overflow and avoiding partial slot updates on failure prevents out-of-bounds reads and inconsistent vertex buffer state. - `cargo test -p victory-gpu --lib draw:: -- --test-threads=1`: 21 passed, 0 failed (including `rect_vertex_range_rejects_overflow_and_out_of_bounds`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 21:11 UTC · original · approved these changes

Clean, focused hardening of CPU rectangle vertex expansion (crates/victory-gpu/src/draw.rs):

  • rect_vertex_range (draw.rs:48-72) replaces unchecked 32-bit first + count, unchecked v.address + first * stride, and truncating (rects * 6 * stride) as u32 with checked arithmetic, preserving exact bounds behavior for valid draws with non-zero first.
  • State::rectangles (draw.rs:1635-1688) now validates all active vertex slots and allocates all scratch ranges before scheduling any defer_host closure or calling IASetVertexBuffers, preventing partial slot updates on failure.
  • Verification (merged onto origin/main 63bcca43):
    • cargo xtask policy (191 files ok) and cargo clippy --locked -p victory-gpu --tests -- -D warnings (default and --features diagnostics) pass cleanly.
    • All 21/21 draw::tests::* (including rect_vertex_range_rejects_overflow_and_out_of_bounds) pass on both NVIDIA GeForce RTX 5090 and Microsoft WARP with VICTORY_D3D12_DEBUG=1.

Two minor non-blocking edge notes:

  • rect_vertex_range checks v.address.checked_add(first * stride) but not from.checked_add(in_bytes) directly (e.g., address = u64::MAX - 31, size = 48, stride = 16, first = 0, count = 3 returns Some((u64::MAX - 31, 48, 96))); State::rectangles still safely rejects it right after via shadow::find(from, in_bytes) (address.checked_add(len.max(1))?).
  • When a rectangle pipeline has no per-vertex input slots (plan.is_empty()), State::rectangles returns true without checking vertices / 3 * 6 against u32::MAX in State::draw (draw.rs:1576).

LGTM!

**@⁠fuzeofficlal** on GitHub · 2026-11-26 21:11 UTC · [original](https://github.com/Marvisak/VIctory/pull/3371#pullrequestreview-6080515288) · **approved these changes** Clean, focused hardening of CPU rectangle vertex expansion (`crates/victory-gpu/src/draw.rs`): - `rect_vertex_range` (`draw.rs:48-72`) replaces unchecked 32-bit `first + count`, unchecked `v.address + first * stride`, and truncating `(rects * 6 * stride) as u32` with checked arithmetic, preserving exact bounds behavior for valid draws with non-zero `first`. - `State::rectangles` (`draw.rs:1635-1688`) now validates all active vertex slots and allocates all scratch ranges before scheduling any `defer_host` closure or calling `IASetVertexBuffers`, preventing partial slot updates on failure. - **Verification** (merged onto `origin/main` `63bcca43`): - `cargo xtask policy` (`191 files ok`) and `cargo clippy --locked -p victory-gpu --tests -- -D warnings` (default and `--features diagnostics`) pass cleanly. - All 21/21 `draw::tests::*` (including `rect_vertex_range_rejects_overflow_and_out_of_bounds`) pass on both NVIDIA GeForce RTX 5090 and Microsoft WARP with `VICTORY_D3D12_DEBUG=1`. Two minor non-blocking edge notes: - `rect_vertex_range` checks `v.address.checked_add(first * stride)` but not `from.checked_add(in_bytes)` directly (e.g., `address = u64::MAX - 31, size = 48, stride = 16, first = 0, count = 3` returns `Some((u64::MAX - 31, 48, 96))`); `State::rectangles` still safely rejects it right after via `shadow::find(from, in_bytes)` (`address.checked_add(len.max(1))?`). - When a rectangle pipeline has no per-vertex input slots (`plan.is_empty()`), `State::rectangles` returns `true` without checking `vertices / 3 * 6` against `u32::MAX` in `State::draw` (`draw.rs:1576`). LGTM!
This pull request can be merged automatically.
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/3371/head:github-bot/gh-3371'
git switch 'github-bot/gh-3371'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3371'
git switch 'github-bot/gh-3371'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3371'
git switch 'github-bot/gh-3371'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3371'
git switch 'main'
git merge --squash 'github-bot/gh-3371'
git switch 'main'
git merge --ff-only 'github-bot/gh-3371'
git switch 'main'
git merge 'github-bot/gh-3371'
git push origin 'main'
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!3371
No description provided.