GPU: avoid shift and arithmetic overflow panics in subresource and copyable_footprints (victory-gpu) #1543

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

@⁠Texbirds on GitHub · 2026-11-24 18:58 UTC · original
Texbirds:fix/gpu-subresource-shift-overflow → main · +138 −23 · 1 files · 1 commits · open

Problem

In crates/victory-gpu/src/resource.rs, subresource and copyable_footprints perform unchecked shifts and alignment/offset arithmetic on title-supplied values:

  • u32::from(desc.depth_or_array_size) >> mip panics for 3D textures when mip >= 32; desc.width >> mip and u64::from(desc.height) >> mip panic when mip >= 64 (since mip_levels is an unbounded u16).
  • desc.width.next_multiple_of(PITCH), row_size.next_multiple_of(PITCH), first + i, end.next_multiple_of(PLACEMENT), and offset/line multiplications panic or wrap on extreme widths, first, or base instead of returning the documented all-ones sentinel (NO_FOOTPRINT, u32::MAX, u64::MAX).

Fix

  • Use checked_shr(mip).unwrap_or(0) for mip extent shifts and checked_next_multiple_of / checked_add / checked_mul in subresource and copyable_footprints.
  • Add regression test copyable_footprints_handles_large_mips_and_overflows_without_panicking.

Verification

  • Pending windows-latest CI (cargo clippy default + --all-features and cargo test --workspace --locked).
**@⁠Texbirds** on GitHub · 2026-11-24 18:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/1543) `Texbirds:fix/gpu-subresource-shift-overflow` → `main` · +138 −23 · 1 files · 1 commits · open ### Problem In `crates/victory-gpu/src/resource.rs`, `subresource` and `copyable_footprints` perform unchecked shifts and alignment/offset arithmetic on title-supplied values: - `u32::from(desc.depth_or_array_size) >> mip` panics for 3D textures when `mip >= 32`; `desc.width >> mip` and `u64::from(desc.height) >> mip` panic when `mip >= 64` (since `mip_levels` is an unbounded `u16`). - `desc.width.next_multiple_of(PITCH)`, `row_size.next_multiple_of(PITCH)`, `first + i`, `end.next_multiple_of(PLACEMENT)`, and offset/line multiplications panic or wrap on extreme widths, `first`, or `base` instead of returning the documented all-ones sentinel (`NO_FOOTPRINT`, `u32::MAX`, `u64::MAX`). ### Fix - Use `checked_shr(mip).unwrap_or(0)` for mip extent shifts and `checked_next_multiple_of` / `checked_add` / `checked_mul` in `subresource` and `copyable_footprints`. - Add regression test `copyable_footprints_handles_large_mips_and_overflows_without_panicking`. ### Verification - Pending `windows-latest` CI (`cargo clippy` default + `--all-features` and `cargo test --workspace --locked`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:35:43 +00:00
Author
Owner

@⁠acetonahard-code on GitHub · 2026-11-24 22:28 UTC · original

Tested on Windows 11, RTX 4070 SUPER (driver 621.14), with the repo toolchain, merged into current main (70564ae):

cargo test -p victory-gpu passed failed ignored
main 76 2 1
main + this PR 77 2 1
main, --all-features 79 2 -
main + this PR, --all-features 80 2 -
  • The new test passes.
  • No GPU test skipped for lack of a device.
  • cargo clippy --all-targets -- -D warnings, with and without --all-features: clean.
  • Minor: cargo fmt --check would rewrap 5 blocks this PR adds (resource.rs). main isn't rustfmt-clean either, so take it or leave it.
  • Two victory-gpu tests fail on current main on this NVIDIA card: dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views. That's the 1,000,000-view heap cap, see #1744. They fail the same way with this PR, and no other test does.

I didn't test with game data.


Tested locally with an AI-assisted script (Claude Code); I checked the results before posting.

**@⁠acetonahard-code** on GitHub · 2026-11-24 22:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/1543#issuecomment-7146268959) Tested on Windows 11, RTX 4070 SUPER (driver 621.14), with the repo toolchain, merged into current `main` (70564ae): | `cargo test -p victory-gpu` | passed | failed | ignored | |---|---|---|---| | `main` | 76 | 2 | 1 | | `main` + this PR | 77 | 2 | 1 | | `main`, `--all-features` | 79 | 2 | - | | `main` + this PR, `--all-features` | 80 | 2 | - | - The new test passes. - No GPU test skipped for lack of a device. - `cargo clippy --all-targets -- -D warnings`, with and without `--all-features`: clean. - Minor: `cargo fmt --check` would rewrap 5 blocks this PR adds (`resource.rs`). `main` isn't rustfmt-clean either, so take it or leave it. - Two `victory-gpu` tests fail on current `main` on this NVIDIA card: `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views`. That's the 1,000,000-view heap cap, see #1744. They fail the same way with this PR, and no other test does. I didn't test with game data. --- <sub>Tested locally with an AI-assisted script (Claude Code); I checked the results before posting.</sub>
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:30 UTC · original · edited 2026-11-25 12:25 UTC

Automated triage for the maintainers.

Merge candidate. Thanks for 15f1999. The mip handling now follows the module's contract. In crates/victory-gpu/src/resource.rs, subresource returns None when mip_count exceeds full_mip_count (the same full-chain computation as before, factored out) or when a shift overflows. copyable_footprints then reports the documented all-ones values. The test now asserts that sentinel for 3D mip=32, 2D mip=64 and one-past-the-chain descriptions, instead of a 1×1×1 footprint.

The checked arithmetic for the index, base offset, buffer width and row pitch is unchanged from the last review and fine. full_mip_count gives the same result as main's 0 branch, so valid descriptions behave exactly as before. CI check passes at 15f1999.

For the maintainers: none of these inputs come from a valid description, so this is robustness (no panic inside an extern "system" function), not a fix for an observed crash.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/1543#issuecomment-7149623965) · edited 2026-11-25 12:25 UTC <!-- victory-triage v1 head=15f1999d60a59bbb76e8507436e314aa641f3cf9 verdict=merge-candidate at=2026-11-25T12:53:00Z --> Automated triage for the maintainers. **Merge candidate.** Thanks for `15f1999`. The mip handling now follows the module's contract. In `crates/victory-gpu/src/resource.rs`, `subresource` returns `None` when `mip_count` exceeds `full_mip_count` (the same full-chain computation as before, factored out) or when a shift overflows. `copyable_footprints` then reports the documented all-ones values. The test now asserts that sentinel for 3D `mip=32`, 2D `mip=64` and one-past-the-chain descriptions, instead of a 1×1×1 footprint. The checked arithmetic for the index, base offset, buffer width and row pitch is unchanged from the last review and fine. `full_mip_count` gives the same result as `main`'s `0` branch, so valid descriptions behave exactly as before. CI `check` passes at `15f1999`. For the maintainers: none of these inputs come from a valid description, so this is robustness (no panic inside an `extern "system"` function), not a fix for an observed crash. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠Texbirds on GitHub · 2026-11-25 12:15 UTC · original

Updated in 15f1999 (CI check is passing):

  • subresource now returns None when mip_count(desc) exceeds full_mip_count(desc) (the full chain from the 0 branch) or when checked_shr(mip) overflows, so invalid mip descriptions produce the documented (NO_FOOTPRINT, u32::MAX, u64::MAX) / u64::MAX sentinel instead of clamping to 1×1×1.
  • Updated copyable_footprints_handles_large_mips_and_overflows_without_panicking to assert the all-ones sentinel for 3D (mip=32), 2D (mip=64), and one-past-full-chain (16x16, mip_levels=6) descriptions as well as index, base-offset, and buffer-width overflows.
**@⁠Texbirds** on GitHub · 2026-11-25 12:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/1543#issuecomment-7162120253) Updated in `15f1999` (CI `check` is passing): - `subresource` now returns `None` when `mip_count(desc)` exceeds `full_mip_count(desc)` (the full chain from the `0` branch) or when `checked_shr(mip)` overflows, so invalid mip descriptions produce the documented `(NO_FOOTPRINT, u32::MAX, u64::MAX)` / `u64::MAX` sentinel instead of clamping to 1×1×1. - Updated `copyable_footprints_handles_large_mips_and_overflows_without_panicking` to assert the all-ones sentinel for 3D (`mip=32`), 2D (`mip=64`), and one-past-full-chain (`16x16`, `mip_levels=6`) descriptions as well as index, base-offset, and buffer-width overflows.
Author
Owner

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

Reviewed and verified 15f1999d merged locally onto origin/main (63bcca43) on Windows 11 x64 (NVIDIA GeForce RTX 5090 and WARP, Rust 1.98.1 MSVC, Windows SDK 10.0.26100.0 dxil.dll; synthetic tests only, no game files).

Verification

  • git merge-tree against origin/main (63bcca43): clean.
  • cargo xtask policy (PR head): 182 files ok.
  • cargo clippy --locked -p victory-gpu --all-targets -- -D warnings (default and --all-features, PR head and merged): 0 warnings.
  • RTX 5090 & WARP (VICTORY_D3D12_DEBUG=1, --features diagnostics, merged): resource::tests + draw::tests — 39 passed, 0 failed on both adapters (including copyable_footprints_handles_large_mips_and_overflows_without_panicking).
  • RTX 5090 (--release, merged): resource::tests + draw::tests — 39 passed, 0 failed.

Review Notes

  • Factoring full_mip_count (crates/victory-gpu/src/resource.rs:296-308) and rejecting mips > full_mip_count(desc) (:364-366) together with checked_shr(mip)? (:384-395) and checked alignment/offset arithmetic (:359, :387-398, :430-446) eliminates unchecked shift and arithmetic panics across an extern "system" FFI entry point while preserving identical results for every valid resource description.
  • Invalid or overflowing inputs cleanly return the documented all-ones sentinel (NO_FOOTPRINT, u32::MAX, u64::MAX).
  • Note for maintainers: #1543 and #1984 touch the same extent-shift lines in subresource (resource.rs), so whichever lands second will need a small merge (checked_shr(extent_shift)?).

Looks good to merge.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 13:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/1543#pullrequestreview-6075005289) · **approved these changes** Reviewed and verified `15f1999d` merged locally onto `origin/main` (`63bcca43`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 and WARP, Rust `1.98.1` MSVC, Windows SDK `10.0.26100.0` `dxil.dll`; synthetic tests only, no game files). ### Verification - `git merge-tree` against `origin/main` (`63bcca43`): clean. - `cargo xtask policy` (PR head): `182 files ok`. - `cargo clippy --locked -p victory-gpu --all-targets -- -D warnings` (default and `--all-features`, PR head and merged): 0 warnings. - **RTX 5090 & WARP (`VICTORY_D3D12_DEBUG=1`, `--features diagnostics`, merged):** `resource::tests` + `draw::tests` — **39 passed, 0 failed** on both adapters (including `copyable_footprints_handles_large_mips_and_overflows_without_panicking`). - **RTX 5090 (`--release`, merged):** `resource::tests` + `draw::tests` — **39 passed, 0 failed**. ### Review Notes - Factoring `full_mip_count` (`crates/victory-gpu/src/resource.rs:296-308`) and rejecting `mips > full_mip_count(desc)` (`:364-366`) together with `checked_shr(mip)?` (`:384-395`) and checked alignment/offset arithmetic (`:359`, `:387-398`, `:430-446`) eliminates unchecked shift and arithmetic panics across an `extern "system"` FFI entry point while preserving identical results for every valid resource description. - Invalid or overflowing inputs cleanly return the documented all-ones sentinel (`NO_FOOTPRINT`, `u32::MAX`, `u64::MAX`). - Note for maintainers: `#1543` and `#1984` touch the same extent-shift lines in `subresource` (`resource.rs`), so whichever lands second will need a small merge (`checked_shr(extent_shift)?`). Looks good to merge.
Author
Owner

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

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

  • Validating mips <= full_mip_count(desc) and using checked_shr / checked_next_multiple_of / checked_mul / checked_add across subresource and copyable_footprints cleanly prevents panics across the FFI boundary and returns the documented D3D12 all-ones sentinel on invalid/overflowing inputs.
  • cargo test -p victory-gpu --lib resource:: -- --test-threads=1: 19 passed, 0 failed (including copyable_footprints_handles_large_mips_and_overflows_without_panicking).
  • 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 19:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/1543#pullrequestreview-6079556110) · **approved these changes** LGTM — verified on Windows against `origin/main` (`63bcca4`). - Validating `mips <= full_mip_count(desc)` and using `checked_shr` / `checked_next_multiple_of` / `checked_mul` / `checked_add` across `subresource` and `copyable_footprints` cleanly prevents panics across the FFI boundary and returns the documented D3D12 all-ones sentinel on invalid/overflowing inputs. - `cargo test -p victory-gpu --lib resource:: -- --test-threads=1`: 19 passed, 0 failed (including `copyable_footprints_handles_large_mips_and_overflows_without_panicking`). - `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).
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/1543/head:github-bot/gh-1543'
git switch 'github-bot/gh-1543'

Merge

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