GPU/descriptor: validate buffer extent/view in own_bytes and require 16-byte alignment for raw views #802

Open
AscilCH wants to merge 1 commit from upstream-pr/803 into main
AscilCH commented 2026-11-25 08:19:14 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-25T08:19:14Z.


Summary

Fix two own_bytes() / own_bytes_if() bugs introduced in a26859f (crates/victory-gpu/src/descriptor/host.rs):

  1. Validate buffer extent and view validity before calling shadow::exact().

    • own_bytes_if() previously checked only element > 0 && count > 0 and offset % element != 0, without checking (first + count) * element <= len (the title buffer's actual extent) or buffer_view_ok.
    • Out-of-bounds or invalid console views (including zero-length placed buffers) could allocate arbitrary-sized exact copies via shadow::exact(), after which buffer_srv()/buffer_uav() ran buffer_view_ok against the newly allocated exact buffer's width instead of the title buffer's len, completely bypassing the bounds check.
    • Pass the title buffer's len and DXGI_FORMAT into own_bytes() / own_bytes_if() and require len > 0, (first + count) * element <= len, and buffer_view_ok (including structured-to-raw conversion via raw_window) before allocating an exact copy.
  2. Enforce D3D12's 16-byte alignment requirement for RAW buffer SRVs/UAVs in own_bytes().

    • own_bytes_if() previously checked only offset.is_multiple_of(element) (4 bytes for R32_TYPELESS RAW views), ignoring buffer_view_ok's 16-byte raw alignment check (first.is_multiple_of(4)).
    • Valid raw views at offsets 4, 8, or 12 mod 16 returned None, shifted FirstElement by offset / 4, failed buffer_view_ok, and were left NULL.
    • Require offset.is_multiple_of(4) && (first + offset / 4).is_multiple_of(4) for raw views to stay on the whole buffer copy, routing misaligned raw views to shadow::exact() starting at FirstElement = 0.

Verification

  • cargo test -p victory-gpu --locked -- descriptor::: 30 passed; 0 failed; 0 ignored, including new out_of_bounds_and_invalid_views_do_not_allocate_exact_copies and raw_views_off_16_byte_alignment_get_exact_copies.
  • cargo clippy -p victory-gpu --all-targets --all-features --locked: 0 warnings in crates/victory-gpu/src/descriptor/host.rs (only pre-existing 9376fd6 warnings in draw.rs and indirect.rs).
  • cargo xtask policy: 171 files ok.

(AI-assisted; synthetic unit tests only, no game data.)

<!-- greyfinch-sync:fxceless/victory-fork:issue:6600221219 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/803). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-25T08:19:14Z. --- ## Summary Fix two `own_bytes()` / `own_bytes_if()` bugs introduced in `a26859f` (`crates/victory-gpu/src/descriptor/host.rs`): 1. **Validate buffer extent and view validity before calling `shadow::exact()`.** - `own_bytes_if()` previously checked only `element > 0 && count > 0` and `offset % element != 0`, without checking `(first + count) * element <= len` (the title buffer's actual extent) or `buffer_view_ok`. - Out-of-bounds or invalid console views (including zero-length placed buffers) could allocate arbitrary-sized `exact` copies via `shadow::exact()`, after which `buffer_srv()`/`buffer_uav()` ran `buffer_view_ok` against the newly allocated `exact` buffer's width instead of the title buffer's `len`, completely bypassing the bounds check. - Pass the title buffer's `len` and `DXGI_FORMAT` into `own_bytes()` / `own_bytes_if()` and require `len > 0`, `(first + count) * element <= len`, and `buffer_view_ok` (including structured-to-raw conversion via `raw_window`) before allocating an `exact` copy. 2. **Enforce D3D12's 16-byte alignment requirement for RAW buffer SRVs/UAVs in `own_bytes()`.** - `own_bytes_if()` previously checked only `offset.is_multiple_of(element)` (4 bytes for `R32_TYPELESS` `RAW` views), ignoring `buffer_view_ok`'s 16-byte raw alignment check (`first.is_multiple_of(4)`). - Valid raw views at offsets 4, 8, or 12 mod 16 returned `None`, shifted `FirstElement` by `offset / 4`, failed `buffer_view_ok`, and were left NULL. - Require `offset.is_multiple_of(4) && (first + offset / 4).is_multiple_of(4)` for raw views to stay on the whole buffer copy, routing misaligned raw views to `shadow::exact()` starting at `FirstElement = 0`. ## Verification - `cargo test -p victory-gpu --locked -- descriptor::`: `30 passed; 0 failed; 0 ignored`, including new `out_of_bounds_and_invalid_views_do_not_allocate_exact_copies` and `raw_views_off_16_byte_alignment_get_exact_copies`. - `cargo clippy -p victory-gpu --all-targets --all-features --locked`: 0 warnings in `crates/victory-gpu/src/descriptor/host.rs` (only pre-existing `9376fd6` warnings in `draw.rs` and `indirect.rs`). - `cargo xtask policy`: `171 files ok`. *(AI-assisted; synthetic unit tests only, no game data.)*
MamaLeVrai commented 2026-11-25 10:11:15 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by MamaLeVrai on 2026-11-25T09:54:08Z.


Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present. I merged this PR's head ba5600d into main 9376fd6 in a throwaway tree (no conflict). cargo clippy --workspace --all-targets --locked -- -D warnings fails, with the same two errors as main itself on this machine (function placed_filler_twins is never used, field depth_written is never read, victory-gpu): this PR adds none and removes none. cargo test --workspace --locked: 542 passed, 0 failed, 10 ignored, two runs (main alone: 540 passed). The title was not run, so this says nothing about in-game effect. No game data.

AI-assisted (Claude Code); no game data.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7158953866 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/803#issuecomment-7158953866). Originally posted by [MamaLeVrai](https://github.com/MamaLeVrai) on 2026-11-25T09:54:08Z. --- Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present. I merged this PR's head `ba5600d` into `main` `9376fd6` in a throwaway tree (no conflict). `cargo clippy --workspace --all-targets --locked -- -D warnings` fails, with the same two errors as `main` itself on this machine (`function placed_filler_twins is never used`, `field depth_written is never read`, `victory-gpu`): this PR adds none and removes none. `cargo test --workspace --locked`: 542 passed, 0 failed, 10 ignored, two runs (`main` alone: 540 passed). The title was not run, so this says nothing about in-game effect. No game data. AI-assisted (Claude Code); no game data.
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/803:upstream-pr/803'
git switch 'upstream-pr/803'

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/803'
git switch 'upstream-pr/803'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/803'
git switch 'upstream-pr/803'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/803'
git switch 'main'
git merge --squash 'upstream-pr/803'
git switch 'main'
git merge --ff-only 'upstream-pr/803'
git switch 'main'
git merge 'upstream-pr/803'
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
fxceless/victory-fork!802
No description provided.