Shader dxbc: use checked arithmetic for bounds slicing #3380

Open
Arkenoz wants to merge 1 commit from upstream-pr/3380 into main
Arkenoz commented 2026-11-26 21:04:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Arkenoz on 2026-11-26T21:04:40Z.


Tracked in #1633.

Problem

  1. u16_at, u32_at, and u64_at in crates/victory-shader/src/dxbc.rs computed at + 2, at + 4, and at + 8 with unchecked addition, panicking on overflow when at is near usize::MAX instead of returning Err(ShaderError::Malformed("truncated")).
  2. Container::parse computed offset + 4, offset + 8, and offset + 8 + size with unchecked addition.

Changes

  • Updated u16_at, u32_at, and u64_at to use checked_add, returning Err(ShaderError::Malformed("truncated")) on overflow or out-of-bounds.
  • Updated Container::parse to use checked_add for part header and body bounds, returning ShaderError::Malformed("part header out of bounds") / ShaderError::Malformed("part data out of bounds").
  • Added unit tests (helpers_overflow_safely, parse_rejects_max_offset_and_size) covering near-max offsets and u32::MAX part offsets/sizes.

Verification

  • cargo test -p victory-shader dxbc (4 passed)
  • cargo clippy -p victory-shader --all-targets (0 warnings)

AI-assisted with Gemini.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5517123788 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3380). Originally posted by [Arkenoz](https://github.com/Arkenoz) on 2026-11-26T21:04:40Z. --- Tracked in #1633. ### Problem 1. `u16_at`, `u32_at`, and `u64_at` in `crates/victory-shader/src/dxbc.rs` computed `at + 2`, `at + 4`, and `at + 8` with unchecked addition, panicking on overflow when `at` is near `usize::MAX` instead of returning `Err(ShaderError::Malformed("truncated"))`. 2. `Container::parse` computed `offset + 4`, `offset + 8`, and `offset + 8 + size` with unchecked addition. ### Changes - Updated `u16_at`, `u32_at`, and `u64_at` to use `checked_add`, returning `Err(ShaderError::Malformed("truncated"))` on overflow or out-of-bounds. - Updated `Container::parse` to use `checked_add` for part header and body bounds, returning `ShaderError::Malformed("part header out of bounds")` / `ShaderError::Malformed("part data out of bounds")`. - Added unit tests (`helpers_overflow_safely`, `parse_rejects_max_offset_and_size`) covering near-max offsets and `u32::MAX` part offsets/sizes. ### Verification - `cargo test -p victory-shader dxbc` (4 passed) - `cargo clippy -p victory-shader --all-targets` (0 warnings) AI-assisted with Gemini.
Dukestar69 commented 2026-11-26 21:07:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T21:07:13Z.

Review: APPROVED


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

  • Using checked_add for part header/body bounds in Container::parse and in u16_at/u32_at/u64_at cleanly eliminates arithmetic overflow panics on malformed DXBC containers.
  • cargo test -p victory-shader --lib dxbc:: -- --test-threads=1: 4 passed, 0 failed (including helpers_overflow_safely and parse_rejects_max_offset_and_size).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

<!-- greyfinch-sync:Marvisak/VIctory:review:6080485336 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3380#pullrequestreview-6080485336). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T21:07:13Z. **Review:** APPROVED --- LGTM — verified on Windows against `origin/main` (`63bcca4`). - Using `checked_add` for part header/body bounds in `Container::parse` and in `u16_at`/`u32_at`/`u64_at` cleanly eliminates arithmetic overflow panics on malformed DXBC containers. - `cargo test -p victory-shader --lib dxbc:: -- --test-threads=1`: 4 passed, 0 failed (including `helpers_overflow_safely` and `parse_rejects_max_offset_and_size`). - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Arkenoz commented 2026-11-26 21:10:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Arkenoz on 2026-11-26T21:10:23Z.


The CI failure is an unrelated flaky test in victory-gpu (descriptor::host::tests::views_bound_while_another_slot_gets_its_texture_do_not_wait). Could someone re-run the check?

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

* Using `checked_add` for part header/body bounds in `Container::parse` and in `u16_at`/`u32_at`/`u64_at` cleanly eliminates arithmetic overflow panics on malformed DXBC containers.

* `cargo test -p victory-shader --lib dxbc:: -- --test-threads=1`: 4 passed, 0 failed (including `helpers_overflow_safely` and `parse_rejects_max_offset_and_size`).

* `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean.

* `cargo xtask policy`: 191 files ok.

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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7203461955 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3380#issuecomment-7203461955). Originally posted by [Arkenoz](https://github.com/Arkenoz) on 2026-11-26T21:10:23Z. --- The CI failure is an unrelated flaky test in `victory-gpu` (`descriptor::host::tests::views_bound_while_another_slot_gets_its_texture_do_not_wait`). Could someone re-run the check? > LGTM — verified on Windows against `origin/main` (`63bcca4`). > > * Using `checked_add` for part header/body bounds in `Container::parse` and in `u16_at`/`u32_at`/`u64_at` cleanly eliminates arithmetic overflow panics on malformed DXBC containers. > > * `cargo test -p victory-shader --lib dxbc:: -- --test-threads=1`: 4 passed, 0 failed (including `helpers_overflow_safely` and `parse_rejects_max_offset_and_size`). > > * `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. > > * `cargo xtask policy`: 191 files ok. > > > Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
fuzeofficlal commented 2026-11-26 21:23:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T21:23:59Z.

Review: APPROVED


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

  • Checked arithmetic (checked_add + .get(..)) across Container::parse and u16_at / u32_at / u64_at (crates/victory-shader/src/dxbc.rs:88-145) prevents input-driven integer overflow and slice panics on both 32-bit and 64-bit targets without rejecting valid DXBC layouts.
  • Pre-existing unchecked arithmetic in dxbc::build (dxbc.rs:149), dxil.rs:88, and signature decoding (emit/mod.rs:544, 551, 625) is outside this PR's scope.
  • Checks:
    • cargo test --locked -p victory-shader --lib -- --test-threads=1 dxbc::: 4 passed, 0 failed (including helpers_overflow_safely and parse_rejects_max_offset_and_size).
    • cargo clippy -p victory-shader --all-targets --locked -- -D warnings (default and --all-features): clean.
    • cargo xtask policy: 191 files ok; rustfmt --edition 2024: clean.

Synthetic tests only; no game data used.

<!-- greyfinch-sync:Marvisak/VIctory:review:6080601627 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3380#pullrequestreview-6080601627). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T21:23:59Z. **Review:** APPROVED --- LGTM — verified against `origin/main` (`63bcca43`) on Windows x64. - Checked arithmetic (`checked_add` + `.get(..)`) across `Container::parse` and `u16_at` / `u32_at` / `u64_at` (`crates/victory-shader/src/dxbc.rs:88-145`) prevents input-driven integer overflow and slice panics on both 32-bit and 64-bit targets without rejecting valid DXBC layouts. - Pre-existing unchecked arithmetic in `dxbc::build` (`dxbc.rs:149`), `dxil.rs:88`, and signature decoding (`emit/mod.rs:544, 551, 625`) is outside this PR's scope. - **Checks**: - `cargo test --locked -p victory-shader --lib -- --test-threads=1 dxbc::`: 4 passed, 0 failed (including `helpers_overflow_safely` and `parse_rejects_max_offset_and_size`). - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings` (default and `--all-features`): clean. - `cargo xtask policy`: 191 files ok; `rustfmt --edition 2024`: clean. Synthetic tests only; no game data used.
futurepro62 commented 2026-11-27 07:26:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-27T07:26:55Z.


NVIDIA full-suite run, to go with the reports above by Dukestar69, fuzeofficlal, since none of them names a GPU run. Tested exact head c214b33 (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 868 passed, 0 failed, 10 ignored.
  • The new tests ran and passed: helpers_overflow_safely, parse_rejects_max_offset_and_size.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7212480731 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3380#issuecomment-7212480731). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-27T07:26:55Z. --- NVIDIA full-suite run, to go with the reports above by Dukestar69, fuzeofficlal, since none of them names a GPU run. Tested exact head `c214b33` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **868 passed, 0 failed, 10 ignored**. - The new tests ran and passed: `helpers_overflow_safely`, `parse_rejects_max_offset_and_size`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3380:upstream-pr/3380'
git switch 'upstream-pr/3380'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3380'
git switch 'upstream-pr/3380'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3380'
git switch 'upstream-pr/3380'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3380'
git switch 'main'
git merge --squash 'upstream-pr/3380'
git switch 'main'
git merge --ff-only 'upstream-pr/3380'
git switch 'main'
git merge 'upstream-pr/3380'
git push origin 'main'
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!3380
No description provided.