GPU: reject overflowing rectangle vertex ranges #2727

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

@⁠fuzeofficlal on GitHub · 2026-11-25 19:01 UTC · original
fuzeofficlal:fix/gpu-rect-vertex-range-overflow → main · +34 −8 · 1 files · 1 commits · open

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.
**@⁠fuzeofficlal** on GitHub · 2026-11-25 19:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/2727) `fuzeofficlal:fix/gpu-rect-vertex-range-overflow` → `main` · +34 −8 · 1 files · 1 commits · open ## 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.
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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:12:51 +00:00
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 19:02 UTC · original

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.

**@⁠kotekompotek** on GitHub · 2026-11-25 19:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/2727#issuecomment-7171739272) 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.
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 20:58 UTC · original

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.

**@⁠oni-swr** on GitHub · 2026-11-25 20:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/2727#issuecomment-7174430812) 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.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 21:21 UTC · original

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-25 21:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/2727#issuecomment-7174969695) 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`.
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-25 21:24 UTC · original

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 [email protected]

AI-assisted with Codex. Synthetic vertices on WARP.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-25 21:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/2727#issuecomment-7175044684) 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](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/6faf2e058af50f73fa5e254b97a2527c15aa9e4b) 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 <[email protected]> AI-assisted with Codex. Synthetic vertices on WARP.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 21:29 UTC · original

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-25 21:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/2727#issuecomment-7175143814) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 23:16 UTC · original · edited 2026-11-26 02:16 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 23:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/2727#issuecomment-7177345556) · edited 2026-11-26 02:16 UTC <!-- victory-triage v1 head=c5a75f7c12ca7f06d273da5c8cdca426c3baf7aa verdict=merge-candidate at=2026-11-26T02:16:40Z --> 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](https://claude.ai/code)_
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 23:31 UTC · original

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** on GitHub · 2026-11-25 23:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/2727#issuecomment-7177672439) 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`).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/2727/head:github-bot/gh-2727'
git switch 'github-bot/gh-2727'

Merge

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