hle/xg: validate subresource slice and pitches before creating copy slices #3321

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

@⁠SalmanBarkat on GitHub · 2026-11-26 17:49 UTC · original
SalmanBarkat:fix/xg-copy-bounds → main · +241 −31 · 1 files · 1 commits · open

Summary

Hardens xg::copy_args, xg::copy_into, and xg::copy_from in crates/victory-hle/src/modules/xg.rs so subresource bounds, swizzle addressability, and linear/tiled byte spans are validated with checked arithmetic before constructing raw slices.

What Was Wrong

Previously, copy_args only checked plane and mip = subresource % c.mips, and computed linear = (depths - 1) * depth + (m.height - 1) * row + bytes with unchecked arithmetic. As a result, copy_into and copy_from entered unsafe ( iled.add(offset) and std::slice::from_raw_parts(_mut)) before Layout::tile / Layout::detile rejected:

  • out-of-range array slices (slice >= layout.slices) or non-zero slices on 3D textures,
  • unaddressable XOR swizzle modes,
  • undersized
    ow or depth pitches (
    ow < bytes, or depth < slice_bytes when depth > 1), or
  • overflowing or > isize::MAX byte-span calculations.

CopyArgs also computed (p.offset + l.size) only for copy_into/copy_from to index planes[plane] again and subtract offset back out.

How It Was Resolved

  • Updated copy_args to return Result<CopyArgs<'_>, Error>, store offset and iled (layout.size) directly, and validate:
    • non-zero c.mips, plane, mip, and slice bounds (slice == 0 for Tex3d, slice < layout.slices otherwise),
    • layout.addressable() (Error::Unsupported -> E_NOTIMPL),
    • checked row bytes (ytes <= row), checked slice bytes, checked depth pitch (depths > 1 => depth >= slice_bytes), and checked linear, offset, and iled spans bounded by isize::MAX.
  • Checked null iled/linear pointers up front in copy_into and copy_from (preserving E_INVALIDARG) and unified warning/HRESULT handling in inish_copy.
  • Added synthetic unit tests in crates/victory-hle/src/modules/xg.rs (copy_args_and_copies_validate_bounds_before_slicing) covering invalid planes, out-of-range array and 3D slices, undersized row/depth pitches, null pointers, unaddressable XOR modes (E_NOTIMPL), 3D round-trips, and multi-plane depth-stencil stencil-plane copies.

Verification

  • cargo clippy -p victory-hle --all-targets --locked -- -D warnings: passed (0 warnings)
  • cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings: passed (0 warnings)
  • cargo test -p victory-hle --locked: passed (153 lib tests + 1 idelityfx + 1
    o_dll_planting, 0 failed, 0 ignored)
  • cargo xtask policy: passed (sset policy: 191 files ok)
  • No game files or proprietary binaries were used; all new tests are synthetic.
**@⁠SalmanBarkat** on GitHub · 2026-11-26 17:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/3321) `SalmanBarkat:fix/xg-copy-bounds` → `main` · +241 −31 · 1 files · 1 commits · open ## Summary Hardens xg::copy_args, xg::copy_into, and xg::copy_from in crates/victory-hle/src/modules/xg.rs so subresource bounds, swizzle addressability, and linear/tiled byte spans are validated with checked arithmetic before constructing raw slices. ## What Was Wrong Previously, copy_args only checked plane and mip = subresource % c.mips, and computed linear = (depths - 1) * depth + (m.height - 1) * row + bytes with unchecked arithmetic. As a result, copy_into and copy_from entered unsafe ( iled.add(offset) and std::slice::from_raw_parts(_mut)) before Layout::tile / Layout::detile rejected: - out-of-range array slices (slice >= layout.slices) or non-zero slices on 3D textures, - unaddressable XOR swizzle modes, - undersized ow or depth pitches ( ow < bytes, or depth < slice_bytes when depth > 1), or - overflowing or > isize::MAX byte-span calculations. CopyArgs also computed (p.offset + l.size) only for copy_into/copy_from to index planes[plane] again and subtract offset back out. ## How It Was Resolved - Updated copy_args to return Result<CopyArgs<'_>, Error>, store offset and iled (layout.size) directly, and validate: - non-zero c.mips, plane, mip, and slice bounds (slice == 0 for Tex3d, slice < layout.slices otherwise), - layout.addressable() (Error::Unsupported -> E_NOTIMPL), - checked row bytes (ytes <= row), checked slice bytes, checked depth pitch (depths > 1 => depth >= slice_bytes), and checked linear, offset, and iled spans bounded by isize::MAX. - Checked null iled/linear pointers up front in copy_into and copy_from (preserving E_INVALIDARG) and unified warning/HRESULT handling in inish_copy. - Added synthetic unit tests in crates/victory-hle/src/modules/xg.rs (copy_args_and_copies_validate_bounds_before_slicing) covering invalid planes, out-of-range array and 3D slices, undersized row/depth pitches, null pointers, unaddressable XOR modes (E_NOTIMPL), 3D round-trips, and multi-plane depth-stencil stencil-plane copies. ## Verification - `cargo clippy -p victory-hle --all-targets --locked -- -D warnings`: passed (0 warnings) - `cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings`: passed (0 warnings) - `cargo test -p victory-hle --locked`: passed (153 lib tests + 1 idelityfx + 1 o_dll_planting, 0 failed, 0 ignored) - `cargo xtask policy`: passed (sset policy: 191 files ok) - No game files or proprietary binaries were used; all new tests are synthetic.
Validate array and 3D slice bounds, swizzle addressability, row/depth pitches, and checked byte spans in xg::copy_args before copy_into and copy_from form raw slices, avoiding redundant plane offset arithmetic while keeping E_INVALIDARG and E_NOTIMPL behaviour unchanged.

Adds synthetic unit tests covering out-of-range slices, 3D copies, undersized pitches, unaddressable XOR layouts, null pointers, and multi-plane depth-stencil copies.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 17:53:53 +00:00
Author
Owner

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

Reviewed the diff and verified locally on Windows 11 x64:

  • Validating c.mips, slice bounds (slice == 0 for Tex3d, slice < l.slices otherwise), l.addressable(), minimum row/depth pitches, checked span arithmetic, and isize::MAX slice limits before constructing raw slices cleanly hardens xg copy operations.
  • cargo test -p victory-hle --lib modules::xg:: -- --test-threads=1: 5 passed, 0 failed (including copy_args_and_copies_validate_bounds_before_slicing).
  • cargo clippy -p victory-hle --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 18:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/3321#pullrequestreview-6078796300) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64: - Validating `c.mips`, `slice` bounds (`slice == 0` for `Tex3d`, `slice < l.slices` otherwise), `l.addressable()`, minimum row/depth pitches, checked span arithmetic, and `isize::MAX` slice limits before constructing raw slices cleanly hardens `xg` copy operations. - `cargo test -p victory-hle --lib modules::xg:: -- --test-threads=1`: 5 passed, 0 failed (including `copy_args_and_copies_validate_bounds_before_slicing`). - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 18:21 UTC · original

Independent CPU/WARP verification of #3321 at exact head a3069f837a9969a7b760098ec1761c42a9d213ec. It merges cleanly onto main 63bcca43c6a00a758510398fa43096ce7e4f7df0, and the PR's merge-base is that main commit. No code changes by me, and no physical GPU or game files were used.

Environment: Windows 11 Pro 10.0.26200 x64, rustc/cargo 1.98.1, Windows SDK 10.0.26100.0 x64 dxil.dll 1.8.2502.11 on PATH, VICTORY_WARP=1, VICTORY_D3D12_WARP=1, --test-threads=1, -j 4. The xg tests don't touch D3D12, so WARP and dxil.dll were only set as a precaution.

Code review (xg::copy_args, copy_into, copy_from; no blocking findings):

  • Slice validation is right for both cases: 3D requires slice == 0, 2D and array require slice < l.slices. mips == 0 can no longer divide by zero.
  • l.addressable() turns the unaddressable XOR swizzle into Unsupported (E_NOTIMPL) before any slice is built.
  • All span arithmetic is checked, and linear, tiled and offset are capped at isize::MAX before from_raw_parts.
  • The tiled span is now l.size from tiled.add(plane offset). That equals the old (offset + size) - offset, so there is no behavior change for valid input.
  • The null check now runs before computer(this) and copy_args.
  • Row pitch must be at least the row bytes. Depth pitch must be at least one slice's bytes, and is only enforced when the subresource has more than one depth slice, so 2D with a zero depth pitch still works.
  • One nit, not a blocker: finish_copy logs Unsupported and Invalid at warn!.

Tests:

  • cargo test -j 4 -p victory-hle --lib xg -- --test-threads=1: 24 passed, 0 failed. This includes the new copy_args_and_copies_validate_bounds_before_slicing.
  • cargo test -j 4 -p victory-hle --lib -- --test-threads=1: 152 passed, 1 failed, modules::dstorage::tests::a_page_list_filled_after_its_request_starts. This failure is pre-existing and flaky. On main the same test fails 3/3 and the full lib run shows 151 passed, 1 failed. On the PR it passed 2 of 3 isolated reruns.
  • cargo clippy -j 4 -p victory-hle --all-targets -- -D warnings: clean.
  • cargo run -j 4 -p xtask -- policy: asset policy: 191 files ok.

Control on origin/main (fail-before / pass-after):

  • The PR's new test, applied to main unchanged, does not compile there. It calls the new Result-returning copy_args and the new offset field: 6× E0599 unwrap_err on Option, plus E0609. So it fails before and passes after, but only as a compile break from the API change.
  • A behavioral control, using only the extern-call assertions from that test (out-of-range array slice, undersized row pitch, XOR swizzle, 3D with slice 1 or a too-small depth pitch), passes on main as well. Those inputs were already rejected there, through E_INVALIDARG or layout errors. I could not show a return-code difference between main and the PR for them.
  • The PR's change is therefore defensive hardening of the unsafe slice creation, not something I could show as a fixed misbehavior.

Not tested: real titles or game memory, an actual out-of-bounds or isize::MAX-sized buffer (I can't safely build one), any non-Windows host, the GPU/WARP shader suites (this PR doesn't touch them), and the full workspace test suite.

AI-assisted: Claude Code (Claude Sonnet 5.5).

**@⁠oni-swr** on GitHub · 2026-11-26 18:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/3321#issuecomment-7200366242) Independent CPU/WARP verification of #3321 at exact head `a3069f837a9969a7b760098ec1761c42a9d213ec`. It merges cleanly onto main `63bcca43c6a00a758510398fa43096ce7e4f7df0`, and the PR's merge-base is that main commit. No code changes by me, and no physical GPU or game files were used. **Environment:** Windows 11 Pro 10.0.26200 x64, rustc/cargo 1.98.1, Windows SDK 10.0.26100.0 x64 `dxil.dll` 1.8.2502.11 on `PATH`, `VICTORY_WARP=1`, `VICTORY_D3D12_WARP=1`, `--test-threads=1`, `-j 4`. The xg tests don't touch D3D12, so WARP and `dxil.dll` were only set as a precaution. **Code review** (`xg::copy_args`, `copy_into`, `copy_from`; no blocking findings): - Slice validation is right for both cases: 3D requires `slice == 0`, 2D and array require `slice < l.slices`. `mips == 0` can no longer divide by zero. - `l.addressable()` turns the unaddressable XOR swizzle into `Unsupported` (`E_NOTIMPL`) before any slice is built. - All span arithmetic is checked, and `linear`, `tiled` and `offset` are capped at `isize::MAX` before `from_raw_parts`. - The tiled span is now `l.size` from `tiled.add(plane offset)`. That equals the old `(offset + size) - offset`, so there is no behavior change for valid input. - The null check now runs before `computer(this)` and `copy_args`. - Row pitch must be at least the row bytes. Depth pitch must be at least one slice's bytes, and is only enforced when the subresource has more than one depth slice, so 2D with a zero depth pitch still works. - One nit, not a blocker: `finish_copy` logs `Unsupported` and `Invalid` at `warn!`. **Tests:** - `cargo test -j 4 -p victory-hle --lib xg -- --test-threads=1`: 24 passed, 0 failed. This includes the new `copy_args_and_copies_validate_bounds_before_slicing`. - `cargo test -j 4 -p victory-hle --lib -- --test-threads=1`: 152 passed, 1 failed, `modules::dstorage::tests::a_page_list_filled_after_its_request_starts`. This failure is pre-existing and flaky. On main the same test fails 3/3 and the full lib run shows 151 passed, 1 failed. On the PR it passed 2 of 3 isolated reruns. - `cargo clippy -j 4 -p victory-hle --all-targets -- -D warnings`: clean. - `cargo run -j 4 -p xtask -- policy`: `asset policy: 191 files ok`. **Control on `origin/main` (fail-before / pass-after):** - The PR's new test, applied to main unchanged, does not compile there. It calls the new `Result`-returning `copy_args` and the new `offset` field: 6× E0599 `unwrap_err` on `Option`, plus E0609. So it fails before and passes after, but only as a compile break from the API change. - A behavioral control, using only the extern-call assertions from that test (out-of-range array slice, undersized row pitch, XOR swizzle, 3D with slice 1 or a too-small depth pitch), passes on main as well. Those inputs were already rejected there, through `E_INVALIDARG` or layout errors. I could not show a return-code difference between main and the PR for them. - The PR's change is therefore defensive hardening of the `unsafe` slice creation, not something I could show as a fixed misbehavior. **Not tested:** real titles or game memory, an actual out-of-bounds or `isize::MAX`-sized buffer (I can't safely build one), any non-Windows host, the GPU/WARP shader suites (this PR doesn't touch them), and the full workspace test suite. AI-assisted: Claude Code (Claude Sonnet 5.5).
Author
Owner

@⁠borosf on GitHub · 2026-11-26 19:03 UTC · original

Independent local verification on Windows 11 x64, Intel Arc B580 12 GB (12 cores), at this PR's head a3069f8:

cargo test -p victory-hle --lib --locked --no-fail-fast -- --test-threads=1
cargo clippy -p victory-hle --all-targets --locked -- -D warnings
cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings

Result: victory-hle lib: exit 0, 153 passed / 0 failed, including the new copy_args_and_copies_validate_bounds_before_slicing test.

Both clippy runs exit 0 (-D warnings, both feature sets). The B580 baseline failures pristine main also hits are: victory-gpu descriptor::host::tests::dynamic_slots_are_held_until_released + memory::tests::lists_share_a_table_and_copies_keep_their_views (#1744/#1829); victory-shader emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros + emit::valu_tests::fspx_programs_run_with_the_hardwares_dot_products. Fully green on this machine.

Synthetic tests only; no game data used. Full log kept locally. — borosf

**@⁠borosf** on GitHub · 2026-11-26 19:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/3321#issuecomment-7201150289) Independent local verification on Windows 11 x64, Intel Arc B580 12 GB (12 cores), at this PR's head `a3069f8`: ``` cargo test -p victory-hle --lib --locked --no-fail-fast -- --test-threads=1 cargo clippy -p victory-hle --all-targets --locked -- -D warnings cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings ``` Result: victory-hle lib: exit 0, 153 passed / 0 failed, including the new copy_args_and_copies_validate_bounds_before_slicing test. Both clippy runs exit 0 (-D warnings, both feature sets). The B580 baseline failures pristine main also hits are: victory-gpu descriptor::host::tests::dynamic_slots_are_held_until_released + memory::tests::lists_share_a_table_and_copies_keep_their_views (#1744/#1829); victory-shader emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros + emit::valu_tests::fspx_programs_run_with_the_hardwares_dot_products. Fully green on this machine. Synthetic tests only; no game data used. Full log kept locally. — borosf
Author
Owner

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

LGTM — copy_args, copy_into, and copy_from cleanly validate subresource bounds, addressable(), checked span arithmetic, and isize::MAX limits before constructing raw slices, without rejecting valid layouts (including depth = 0 on single-depth textures and block-aligned BC textures).

Verified

  • cargo clippy --locked -p victory-hle --all-targets -- -D warnings (default & --all-features): clean.
  • rustfmt --edition 2024: clean on all PR-touched hunks in crates/victory-hle/src/modules/xg.rs.
  • cargo test --locked -p victory-hle --lib modules::xg:: -- --test-threads=1: 5 passed, 0 failed (including copy_args_and_copies_validate_bounds_before_slicing).
  • cargo test --locked -p victory-hle --all-targets -- --test-threads=1: 153 lib + 2 integration tests passed, 0 failed.

Minor non-blocking nits

  • crates/victory-hle/src/modules/xg.rs:392: offset and tiled are each capped at isize::MAX; checking offset.checked_add(tiled) against isize::MAX as well would cover the whole-texture allocation bound on 32-bit targets.
  • xg.rs:949: the XOR E_NOTIMPL check passes the same 1-byte one buffer to both src and dst; using separate buffers avoids potential aliasing/size assumptions if preflight ordering ever changes.
  • xg.rs:973: in the stencil-plane round-trip test, asserting that plane 0 bytes remain untouched would directly verify a.offset is applied in both copy_into and copy_from.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 20:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/3321#pullrequestreview-6080279546) · **approved these changes** LGTM — `copy_args`, `copy_into`, and `copy_from` cleanly validate subresource bounds, `addressable()`, checked span arithmetic, and `isize::MAX` limits before constructing raw slices, without rejecting valid layouts (including `depth = 0` on single-depth textures and block-aligned BC textures). ### Verified - `cargo clippy --locked -p victory-hle --all-targets -- -D warnings` (default & `--all-features`): clean. - `rustfmt --edition 2024`: clean on all PR-touched hunks in `crates/victory-hle/src/modules/xg.rs`. - `cargo test --locked -p victory-hle --lib modules::xg:: -- --test-threads=1`: 5 passed, 0 failed (including `copy_args_and_copies_validate_bounds_before_slicing`). - `cargo test --locked -p victory-hle --all-targets -- --test-threads=1`: 153 lib + 2 integration tests passed, 0 failed. ### Minor non-blocking nits - `crates/victory-hle/src/modules/xg.rs:392`: `offset` and `tiled` are each capped at `isize::MAX`; checking `offset.checked_add(tiled)` against `isize::MAX` as well would cover the whole-texture allocation bound on 32-bit targets. - `xg.rs:949`: the XOR `E_NOTIMPL` check passes the same 1-byte `one` buffer to both `src` and `dst`; using separate buffers avoids potential aliasing/size assumptions if preflight ordering ever changes. - `xg.rs:973`: in the stencil-plane round-trip test, asserting that plane 0 bytes remain untouched would directly verify `a.offset` is applied in both `copy_into` and `copy_from`.
This pull request can be merged automatically.
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/3321/head:github-bot/gh-3321'
git switch 'github-bot/gh-3321'

Merge

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