Skip to content

GPU: reject overflowing rectangle vertex ranges - #2727

Open
fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/gpu-rect-vertex-range-overflow
Open

fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/gpu-rect-vertex-range-overflow

Conversation

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Summary

Fix integer overflow, address wraparound, and u32 buffer-size truncation in CPU rectangle vertex expansion (crates/victory-gpu/src/draw.rs):

  • State::rectangles previously checked u64::from(first + count) * stride > u64::from(v.size), adding first + count in u32 before widening — overflowing in debug builds or wrapping around in release builds to bypass the v.size bounds check (e.g., first = u32::MAX - 2, count = 3 or first = 1, count = u32::MAX).
  • Address arithmetic (v.address + u64::from(first) * stride, from + src_bytes) and byte-length calculations (rects * 3 * stride, rects * 6 * stride) lacked checked arithmetic, and D3D12_VERTEX_BUFFER_VIEW::SizeInBytes silently truncated (rects * 6 * stride) as u32 when the expanded buffer exceeded u32::MAX (e.g., a 3 GiB source buffer expanding to 6 GiB).
  • State::draw computed vertices / 3 * 6 in u32, which could overflow even on pipelines without per-vertex input slots.

Extract rectangle_vertex_range and expanded_rectangle_vertices using checked arithmetic and u32::try_from, rejecting overflowing or out-of-bounds rectangle draws cleanly before allocating scratch memory or issuing DrawInstanced.

Tests

Added 6 deterministic unit tests in crates/victory-gpu/src/draw.rs covering valid ranges, u32 first + count wraparound, u64 address wraparound (v.address + offset and from + src_bytes), u64 byte-offset multiplication overflow, u32 SizeInBytes truncation (stride = 0x8000_0000 and 0x4000_0000), and expanded_rectangle_vertices overflow boundaries.

Verification

  • cargo clippy --locked -p victory-gpu --all-targets -- -D warnings and --features diagnostics: 0 warnings.
  • NVIDIA GeForce RTX 5090 (rustc 1.98.1 MSVC): cargo test --locked -p victory-gpu --lib draw::tests -> 17 passed, 0 failed.
  • WARP (VICTORY_D3D12_WARP=1): cargo test --locked -p victory-gpu --features diagnostics --lib -- --test-threads=1 -> 204 passed, 0 failed, 1 ignored.
  • cargo xtask policy: 184 files ok.

@kotekompotek

Copy link
Copy Markdown

All the overflow paths closed correctly: first+count widened to u64 before adding, checked_mul on every product, checked_add on addresses, try_from for SizeInBytes. Especially good that expanded_rectangle_vertices is computed before prepare/run so the draw-side count can't overflow after rectangles() already succeeded, and a None count falls into not_run rather than silently drawing a truncated count. Per-slot once! rejection still names the slot.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Independent WARP/CPU verification of f712e159 (#2727).

Review: Every arithmetic step in rectangle_vertex_range is widened to u64 or checked (first+count, *stride, address add, rects*3*stride, rects*6*stride into u32), and expanded_rectangle_vertices checks vertices/3*6. The draw path skips with the existing "outside their buffer" message on any failure. The unsafe slices now use the checked src_bytes/dst_bytes. Scope is one file, draw.rs, and I found no correctness problems.

Results (WARP/CPU):

  • origin/main: cargo test -p victory-gpu --lib: 195 passed, 0 failed.
  • PR: 201 passed, 0 failed (the six new tests).
  • cargo clippy --locked -p victory-gpu --all-targets [--all-features] -- -D warnings: clean. cargo test -p xtask (policy): 6 passed.

Negative control (mutating the production code on the PR head):

  • checked_mul replaced by wrapping_mul in expanded_rectangle_vertices: expanded_rectangle_vertices_rejects_overflow fails, as it should.
  • checked_mul replaced by wrapping_mul for end_bytes in rectangle_vertex_range: all tests still pass. The chosen overflow inputs are still rejected by the later size comparison, so none of them pins that particular checked_mul. This is a test-coverage gap, not a bug in the PR. An input where (first+count)*stride wraps to a value below size would close it.

Method: no physical GPU. VICTORY_WARP=1, VICTORY_D3D12_WARP=1, Windows SDK 10.0.26100 dxil.dll on PATH, Windows 11, rustc 1.98.1 MSVC, --locked. Compared origin/main (13f91ca) with the PR head. The PR merges cleanly onto that main. No game files, dumps, shader names or retail hashes were used or are needed.

AI disclosure: this verification was done by Claude Code / Claude Sonnet 5.5.

@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

Thanks for the independent verification and negative-control check! Good observation on end_bytes checked_mul: because first and count are u32 and count >= 3 requires dst_bytes = (count / 3) * 6 * stride <= u32::MAX (forcing stride <= u32::MAX / 6), (first + count) * stride is bounded by (2^33 - 2) * (u32::MAX / 6) < 2^64 for any input that passes the dst_bytes check—so checked_mul is defensive against future widening while dst_bytes already bounds stride.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I found a separate valid range that still gets dropped on f712e159. A 56-byte view with a 16-byte stride starts drawing at vertex 1. Its last float2 is fully present at bytes 48–55. The same buffer renders the expected top-left pixel through the normal triangle path on WARP.

6faf2e0 is one commit on your exact HEAD. It keeps the checked arithmetic, accepts the final vertex when its declared attributes fit, and initializes the missing scratch padding to zero.

The same guest rectangle draw and texture readback return two clear texels on main 13f91ca and on f712e159. With the patch both are [1, 0, 0, 1]. The fully padded rectangle, native input-assembler triangle and 55-byte incomplete-position controls pass in all builds. Six paired Windows/WARP runs across both debug settings. The full GPU suite passed 205 tests with one ignored in each setting. Strict GPU Clippy in both configurations and asset policy also passed.

Please keep my Git author if you cherry-pick or rebase the commit. If you fold the change or test into another commit, please add this trailer.

Co-authored-by: andrewpozdnakov7-jpg andrewpozdnakov7-jpg@users.noreply.github.com

AI-assisted with Codex. Synthetic vertices on WARP.

@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

Thanks for investigating this and putting together 6faf2e0 with a concrete reproduction!

Because an unpadded trailing vertex is a pre-existing limitation on origin/main 13f91ca as well and spans draw.rs, primitive.rs (expand_rects, which also overlaps #2603), and pipeline/host.rs, let's keep #2727 narrowly scoped to the arithmetic overflow/truncation checks in draw.rs so it stays small and independent for triage. Please go ahead and open 6faf2e0 as a standalone PR—happy to help review and run it on RTX 5090 + WARP once opened.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate (recommending a look, not an approval). Thanks for reducing it as asked. The PR is now one helper, rectangle_vertex_bounds in crates/victory-gpu/src/draw.rs, used by State::rectangles: it widens first + count to u64 before multiplying by the stride (on main that add is in u32 and can panic in debug builds on bad draw arguments), and the scratch size/SizeInBytes now come from one checked u32::try_from instead of a truncating cast. The shadow::find check is kept, and there is one focused test. I checked it against the current loop on main; it merges cleanly and CI check is passing. If #2729 lands first this will need a small rebase.


Generated by Claude Code

State::rectangles previously evaluated u64::from(first + count) * stride, adding first and count in u32 before widening so overflowing draw arguments could panic in debug builds (or wrap in release). Widen both operands to u64 before adding and check the expanded D3D12_VERTEX_BUFFER_VIEW::SizeInBytes conversion via rectangle_vertex_bounds.
@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

Rebased onto origin/main (842b0cb) and reduced in c5a75f7c per triage feedback:

  • Dropped the State::draw vertex-count check and the extra u64::MAX/4 GiB source-address guards.
  • Kept only the minimal rectangle_vertex_bounds check widening u64::from(first) + u64::from(count) before multiplying by stride plus the checked SizeInBytes conversion, with one focused unit test (rectangle_vertex_bounds_widens_first_plus_count).
  • Happy to rebase on #2729 if that lands first.

Verified on Windows 11 (RTX 5090 + WARP): draw::tests (21/21 RTX 5090, 21/21 WARP), cargo clippy -p victory-gpu --all-targets -- -D warnings (default and --features diagnostics), and cargo xtask policy (191 files ok).

@fuzeofficlal
fuzeofficlal force-pushed the fix/gpu-rect-vertex-range-overflow branch from f712e15 to c5a75f7 Compare November 25, 2026 23:31

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090):

  • cargo test -p victory-gpu --lib draw::tests: 21 passed, 0 failed (including new rectangle_vertex_bounds_widens_first_plus_count).
  • 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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants