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

Closed
github-bot wants to merge 1 commit from gh-pr/674/head into gh-pr/674/base
Owner

@⁠DenRakEiw on GitHub · 2026-11-24 06:37 UTC · original
fix/xg-tiling-and-copy-bounds → main · +113 −13 · 1 files · 1 commits · closed without merging

## 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).

**@⁠DenRakEiw** on GitHub · 2026-11-24 06:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/674) `fix/xg-tiling-and-copy-bounds` → `main` · +113 −13 · 1 files · 1 commits · closed without merging ## 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).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:12:32 +00:00
github-bot closed this pull request 2026-11-26 04:12:32 +00:00
Author
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-24 09:38 UTC · original

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.

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 09:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/674#issuecomment-7127692831) 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.
Author
Owner

@⁠ErRusso on GitHub · 2026-11-24 11:23 UTC · original

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.

**@⁠ErRusso** on GitHub · 2026-11-24 11:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/674#issuecomment-7130036564) ## 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.
Author
Owner

@⁠molodayamama on GitHub · 2026-11-24 14:02 UTC · original

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.

**@⁠molodayamama** on GitHub · 2026-11-24 14:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/674#issuecomment-7133738394) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:28 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 01:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/674#issuecomment-7149593395) <!-- 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)_
Author
Owner

@⁠DenRakEiw on GitHub · 2026-11-25 02:57 UTC · original

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).

**@⁠DenRakEiw** on GitHub · 2026-11-25 02:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/674#issuecomment-7151427763) 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

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