HLE (xg): validate null tiling output, slice bounds, and checked copy spans #674

Closed
DenRakEiw wants to merge 1 commit from upstream-pr/674 into main
DenRakEiw commented 2026-11-24 06:37:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T06:37:46Z.


## Summary

Fixes memory-safety / bounds-validation issues in crates/victory-hle/src/modules/xg.rs:

  1. Reject null out in tiling when *count > 0:
    • tiling previously wrote entries to out.add(i as usize).write(entry) without checking out.is_null(), causing null-pointer write UB when a caller passed *count > 0 and a null out buffer.
    • Now returns E_INVALIDARG upfront before mutating any output pointers when count is non-null with *count > 0 and out.is_null() (while still allowing *count == 0 with null out).
    • Also uses standard.saturating_mul(slices) when computing wanted.
  2. Validate slice < l.slices and use checked span arithmetic in copy_args:
    • copy_args previously checked mip bounds via l.mips.get(mip) but did not check slice < l.slices, allowing copy_into / copy_from to construct raw slices from caller pointers for out-of-range subresources before Layout::tile / Layout::detile rejected the slice.
    • Now validates slice < l.slices, uses checked arithmetic (checked_sub, checked_mul, checked_add, usize::try_from) for linear and tiled spans, and ensures slice lengths stay within isize::MAX.

Tests

Added 2 synthetic unit tests in crates/victory-hle/src/modules/xg.rs:

  • tiling_rejects_null_out_when_count_nonzero
  • copy_rejects_out_of_range_slice_and_plane

Verification

  • cargo test -p victory-hle xg: 15 passed, 0 failed (all 6 modules::xg::tests passed)
  • cargo clippy -p victory-hle --all-targets: 0 warnings in xg.rs
  • cargo xtask policy: ok

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:issue:6576167418 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/674). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-24T06:37:46Z. --- ## Summary Fixes memory-safety / bounds-validation issues in `crates/victory-hle/src/modules/xg.rs`: 1. **Reject null `out` in `tiling` when `*count > 0`:** - `tiling` previously wrote entries to `out.add(i as usize).write(entry)` without checking `out.is_null()`, causing null-pointer write UB when a caller passed `*count > 0` and a null `out` buffer. - Now returns `E_INVALIDARG` upfront before mutating any output pointers when `count` is non-null with `*count > 0` and `out.is_null()` (while still allowing `*count == 0` with null `out`). - Also uses `standard.saturating_mul(slices)` when computing `wanted`. 2. **Validate `slice < l.slices` and use checked span arithmetic in `copy_args`:** - `copy_args` previously checked `mip` bounds via `l.mips.get(mip)` but did not check `slice < l.slices`, allowing `copy_into` / `copy_from` to construct raw slices from caller pointers for out-of-range subresources before `Layout::tile` / `Layout::detile` rejected the slice. - Now validates `slice < l.slices`, uses checked arithmetic (`checked_sub`, `checked_mul`, `checked_add`, `usize::try_from`) for linear and tiled spans, and ensures slice lengths stay within `isize::MAX`. ## Tests Added 2 synthetic unit tests in `crates/victory-hle/src/modules/xg.rs`: - `tiling_rejects_null_out_when_count_nonzero` - `copy_rejects_out_of_range_slice_and_plane` ## Verification - `cargo test -p victory-hle xg`: 15 passed, 0 failed (all 6 `modules::xg::tests` passed) - `cargo clippy -p victory-hle --all-targets`: 0 warnings in `xg.rs` - `cargo xtask policy`: ok AI-assisted (Gemini 4 Neon).
LAKSHYA-cmd-tech commented 2026-11-24 06:54:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by LAKSHYA-cmd-tech on 2026-11-24T06:50:20Z.

Review: APPROVED


LGTM! Tested on Windows 11 with MSVC Rust 1.98.1. All 88 tests in ictory-hle passed cleanly, including both new tests in modules::xg::tests ( iling_rejects_null_out_when_count_nonzero and copy_rejects_out_of_range_slice_and_plane). Validating out nullness upfront and checking slice bounds prevents out-of-bounds slicing and undefined behavior in texture tiling.

<!-- greyfinch-sync:Marvisak/VIctory:review:6034867047 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/674#pullrequestreview-6034867047). Originally posted by [LAKSHYA-cmd-tech](https://github.com/LAKSHYA-cmd-tech) on 2026-11-24T06:50:20Z. **Review:** APPROVED --- LGTM! Tested on Windows 11 with MSVC Rust 1.98.1. All 88 tests in ictory-hle passed cleanly, including both new tests in modules::xg::tests ( iling_rejects_null_out_when_count_nonzero and copy_rejects_out_of_range_slice_and_plane). Validating out nullness upfront and checking slice bounds prevents out-of-bounds slicing and undefined behavior in texture tiling.
catminou4 commented 2026-11-24 07:01:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T06:56:25Z.

Review: COMMENTED


Static review only (victory-hle doesn't build off Windows):

This is a solid hardening pass on the tiled-copy path:

  • copy_args now carries tiled_offset/tiled_len explicitly instead of the end offset re-derived by callers — removes the duplicated planes[plane].offset fetch and the a.tiled - offset subtraction.
  • The new slice >= l.slices check closes a real gap: mip was bounds-checked via l.mips.get(mip) but slice was not.
  • Checked arithmetic (checked_mul/checked_add/try_from) replaces wrapping usize math on title-controlled depth/row/width — old code could overflow on 64-bit with large u32 inputs, and the isize::MAX guards make the subsequent slice::from_raw_parts offsets sound.
  • tiling() returning E_INVALIDARG for count > 0 && out.is_null() matches the usual D3D-style contract.

One nit: usize::try_from(m.width) where width: u32 is infallible on 64-bit — harmless, just slightly noisy. No objections otherwise.

<!-- greyfinch-sync:Marvisak/VIctory:review:6034939996 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/674#pullrequestreview-6034939996). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T06:56:25Z. **Review:** COMMENTED --- Static review only (victory-hle doesn't build off Windows): This is a solid hardening pass on the tiled-copy path: - `copy_args` now carries `tiled_offset`/`tiled_len` explicitly instead of the end offset re-derived by callers — removes the duplicated `planes[plane].offset` fetch and the `a.tiled - offset` subtraction. - The new `slice >= l.slices` check closes a real gap: `mip` was bounds-checked via `l.mips.get(mip)` but `slice` was not. - Checked arithmetic (`checked_mul`/`checked_add`/`try_from`) replaces wrapping `usize` math on title-controlled `depth`/`row`/`width` — old code could overflow on 64-bit with large u32 inputs, and the `isize::MAX` guards make the subsequent `slice::from_raw_parts` offsets sound. - `tiling()` returning `E_INVALIDARG` for `count > 0 && out.is_null()` matches the usual D3D-style contract. One nit: `usize::try_from(m.width)` where `width: u32` is infallible on 64-bit — harmless, just slightly noisy. No objections otherwise.
geiyiliyabumo commented 2026-11-24 10:03:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-24T09:38:40Z.


Independent verification of b9e5f2e9f merged with main (8e79c80, clean merge) on Windows 11 Pro (26100), Rust 1.98.1 (x86_64-pc-windows-msvc), AMD Radeon RX 9060 XT: PASS.

  • cargo xtask policy → asset policy: 153 files ok
  • cargo clippy --workspace --all-targets --locked -- -D warnings → pass (0 warnings)
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings → pass (0 warnings)
  • cargo test --workspace --locked → 313 passed, 0 failed, 6 ignored (both new modules::xg::tests::tiling_rejects_null_out_when_count_nonzero and copy_rejects_out_of_range_slice_and_plane pass)
  • Diff review: copy_args now validates slice < l.slices, checks linear/tiled span arithmetic against overflow and isize::MAX before from_raw_parts(_mut), and tiling rejects *count > 0 && out.is_null() upfront without mutating caller outputs while still permitting *count == 0 with null out.

AI-assisted; synthetic tests only, no game files.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7127692831 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/674#issuecomment-7127692831). Originally posted by [geiyiliyabumo](https://github.com/geiyiliyabumo) on 2026-11-24T09:38:40Z. --- Independent verification of `b9e5f2e9f` merged with `main` (`8e79c80`, clean merge) on Windows 11 Pro (`26100`), Rust `1.98.1` (`x86_64-pc-windows-msvc`), **AMD Radeon RX 9060 XT**: **PASS**. - `cargo xtask policy` → `asset policy: 153 files ok` - `cargo clippy --workspace --all-targets --locked -- -D warnings` → **pass** (0 warnings) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` → **pass** (0 warnings) - `cargo test --workspace --locked` → **313 passed, 0 failed, 6 ignored** (both new `modules::xg::tests::tiling_rejects_null_out_when_count_nonzero` and `copy_rejects_out_of_range_slice_and_plane` pass) - Diff review: `copy_args` now validates `slice < l.slices`, checks linear/tiled span arithmetic against overflow and `isize::MAX` before `from_raw_parts(_mut)`, and `tiling` rejects `*count > 0 && out.is_null()` upfront without mutating caller outputs while still permitting `*count == 0` with null `out`. AI-assisted; synthetic tests only, no game files.
mercuryVM commented 2026-11-24 11:03:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T10:39:03Z.

Review: APPROVED


Tested b9e5f2e on Windows 11, RTX 5070, MSVC: cargo test -p victory-hle 89 passed, including tiling_rejects_null_out_when_count_nonzero and copy_rejects_out_of_range_slice_and_plane. Clippy shows 1 field_reassign_with_default warning at xmem.rs:747, which this PR doesn't touch.

  • copy_args now checks slice against l.slices and does all the span arithmetic with checked operations, so a bad subresource fails with E_INVALIDARG before any from_raw_parts call. Carrying tiled_offset/tiled_len instead of recomputing end - offset at the call sites also removes the chance of an underflow there.
  • The null-out check in tiling runs before any output is written, and the test confirms that a failed call leaves tiles and count untouched.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6037697426 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/674#pullrequestreview-6037697426). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T10:39:03Z. **Review:** APPROVED --- Tested b9e5f2e on Windows 11, RTX 5070, MSVC: `cargo test -p victory-hle` 89 passed, including `tiling_rejects_null_out_when_count_nonzero` and `copy_rejects_out_of_range_slice_and_plane`. Clippy shows 1 `field_reassign_with_default` warning at xmem.rs:747, which this PR doesn't touch. - `copy_args` now checks `slice` against `l.slices` and does all the span arithmetic with checked operations, so a bad subresource fails with `E_INVALIDARG` before any `from_raw_parts` call. Carrying `tiled_offset`/`tiled_len` instead of recomputing `end - offset` at the call sites also removes the chance of an underflow there. - The null-`out` check in `tiling` runs before any output is written, and the test confirms that a failed call leaves `tiles` and `count` untouched. AI-assisted (Claude Code).
ErRusso commented 2026-11-24 12:04:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ErRusso on 2026-11-24T11:23:32Z.


Audit: ✅ Looks good (needs Windows to compile the GPU-dependent crate)

Reviewed this PR as part of an audit of mergeable PRs.

  • Scope: crates/victory-hle/src/modules/xg.rs (+113/-13). Three improvements: (1) all arithmetic in copy_args() uses checked_mul, checked_add, checked_sub, and caps values at isize::MAX; (2) tiling() rejects null out when *count > 0; (3) slice >= l.slices early-return prevents out-of-range subresource access. Also fixes a standard * slices multiplication that could overflow by using saturating_mul.
  • Merges cleanly into main.
  • Test note: The GPU crate doesn't compile on this branch on Linux (pre-existing #[cfg(windows)] gating gap), so the new tiling_rejects_null_out_when_count_nonzero and copy_rejects_out_of_range_slice_and_plane tests couldn't run here. The logic is sound on review.
  • Design improvement: CopyArgs now stores tiled_offset + tiled_len instead of deriving tiled - offset at each call site — cleaner and the safety fix in one place.

No issues found.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7130036564 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/674#issuecomment-7130036564). Originally posted by [ErRusso](https://github.com/ErRusso) on 2026-11-24T11:23:32Z. --- ## Audit: ✅ Looks good (needs Windows to compile the GPU-dependent crate) Reviewed this PR as part of an audit of mergeable PRs. - **Scope:** `crates/victory-hle/src/modules/xg.rs` (+113/-13). Three improvements: (1) all arithmetic in `copy_args()` uses `checked_mul`, `checked_add`, `checked_sub`, and caps values at `isize::MAX`; (2) `tiling()` rejects null `out` when `*count > 0`; (3) `slice >= l.slices` early-return prevents out-of-range subresource access. Also fixes a `standard * slices` multiplication that could overflow by using `saturating_mul`. - **Merges cleanly** into `main`. - **Test note:** The GPU crate doesn't compile on this branch on Linux (pre-existing `#[cfg(windows)]` gating gap), so the new `tiling_rejects_null_out_when_count_nonzero` and `copy_rejects_out_of_range_slice_and_plane` tests couldn't run here. The logic is sound on review. - **Design improvement:** `CopyArgs` now stores `tiled_offset` + `tiled_len` instead of deriving `tiled - offset` at each call site — cleaner and the safety fix in one place. No issues found.
molodayamama commented 2026-11-24 14:04:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by molodayamama on 2026-11-24T14:02:24Z.


Independent Windows check of b9e5f2e9ff3d03c1cc3f89402aacbb63eb4c559e (Windows 10 22H2 19045, Rust 1.98.1, MSVC x64).

  • cargo test -p victory-hle --locked --lib tiling_rejects_null_out_when_count_nonzero -- --test-threads=1 --nocapture: 1 passed.
  • cargo test -p victory-hle --locked --lib copy_rejects_out_of_range_slice_and_plane -- --test-threads=1 --nocapture: 1 passed.

The new XG fixtures check rejecting a null tiling-output pointer for a nonzero count, and refusing out-of-range slices and planes before a texture copy. No GPU execution.

Only source-built synthetic fixtures were used. AI-assisted verification.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7133738394 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/674#issuecomment-7133738394). Originally posted by [molodayamama](https://github.com/molodayamama) on 2026-11-24T14:02:24Z. --- Independent Windows check of `b9e5f2e9ff3d03c1cc3f89402aacbb63eb4c559e` (Windows 10 22H2 19045, Rust 1.98.1, MSVC x64). - `cargo test -p victory-hle --locked --lib tiling_rejects_null_out_when_count_nonzero -- --test-threads=1 --nocapture`: 1 passed. - `cargo test -p victory-hle --locked --lib copy_rejects_out_of_range_slice_and_plane -- --test-threads=1 --nocapture`: 1 passed. The new XG fixtures check rejecting a null tiling-output pointer for a nonzero count, and refusing out-of-range slices and planes before a texture copy. No GPU execution. Only source-built synthetic fixtures were used. AI-assisted verification.
Marvisak commented 2026-11-25 02:06:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:28:50Z.


Automated triage for the maintainers.

Proposed close. This PR adds bounds checks to crates/victory-hle/src/modules/xg.rs, but what they guard against is already handled on main or cannot occur:

  • Out-of-range slice: copy_into / copy_from build the tiled slice over the whole plane (p.offset .. p.offset + l.size), whatever the slice. Nothing is read or written before Layout::walk in crates/victory-formats/src/texture.rs rejects first >= self.slices with Error::Invalid (so E_INVALIDARG). The new check returns the same result one step earlier.
  • Checked span arithmetic: the operands are u32 values and our own layout's sizes, so they cannot overflow a 64-bit usize. Layout::walk already checks the linear span against the buffer with checked arithmetic.
  • Null out with *count > 0 in tiling: this is a call a title does not make, and standard (at most the mip count) times slices cannot overflow.

What would change the verdict: a concrete input that reaches out-of-bounds memory on main.

CI: no check runs on the head commit.

This pull request will be closed in 48 hours unless the author pushes a change or a maintainer removes the triage: proposed close label.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149593395 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/674#issuecomment-7149593395). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:28:50Z. --- <!-- victory-triage v1 head=b9e5f2e9ff3d03c1cc3f89402aacbb63eb4c559e verdict=proposed-close at=2026-11-25T01:28:48Z --> Automated triage for the maintainers. **Proposed close.** This PR adds bounds checks to `crates/victory-hle/src/modules/xg.rs`, but what they guard against is already handled on `main` or cannot occur: - **Out-of-range slice:** `copy_into` / `copy_from` build the tiled slice over the whole plane (`p.offset .. p.offset + l.size`), whatever the slice. Nothing is read or written before `Layout::walk` in `crates/victory-formats/src/texture.rs` rejects `first >= self.slices` with `Error::Invalid` (so `E_INVALIDARG`). The new check returns the same result one step earlier. - **Checked span arithmetic:** the operands are `u32` values and our own layout's sizes, so they cannot overflow a 64-bit `usize`. `Layout::walk` already checks the linear span against the buffer with checked arithmetic. - **Null `out` with `*count > 0` in `tiling`:** this is a call a title does not make, and `standard` (at most the mip count) times `slices` cannot overflow. What would change the verdict: a concrete input that reaches out-of-bounds memory on `main`. CI: no check runs on the head commit. This pull request will be closed in 48 hours unless the author pushes a change or a maintainer removes the `triage: proposed close` label. --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync closed this pull request 2026-11-25 03:01:47 +00:00
DenRakEiw commented 2026-11-25 03:03:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T02:57:51Z.


Makes sense — Layout::walk already rejects first >= self.slices with E_INVALIDARG before touching the buffers, and the u32 span terms cannot overflow 64-bit usize. Closing this PR to keep the queue clean. Thanks for the clear triage!

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7151427763 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/674#issuecomment-7151427763). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T02:57:51Z. --- Makes sense — `Layout::walk` already rejects `first >= self.slices` with `E_INVALIDARG` before touching the buffers, and the `u32` span terms cannot overflow 64-bit `usize`. Closing this PR to keep the queue clean. Thanks for the clear triage! AI-assisted (Gemini 4 Neon).

Pull request closed

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!674
No description provided.