Formats: a depth pitch that overlaps its depth slices is refused #1472

Open
CwWwWwW wants to merge 1 commit from upstream-pr/1472 into main
CwWwWwW commented 2026-11-27 12:58:15 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by CwWwWwW on 2026-11-27T12:58:15Z.


The problem

Layout::walk (crates/victory-formats/src/texture.rs, shared by tile and detile) computes the linear buffer it needs from the title's row_pitch/depth_pitch, but it never checks that one depth slice ends before the next one begins. A 3D copy whose depth pitch is smaller than one slice's rows — (height-1)*row_pitch + row bytes — makes tile/detile read or write two slices' rows at overlapping addresses and still return Ok, silently losing the earlier slice's rows.

This is reachable from the title through xg's texture copy methods (0x40 into / 0x48 out of tiled memory), which pass the title's row/depth pitches straight into detile/tile. Silent data corruption, no memory unsafety (the copies stay inside the length they checked).

The change

Before copying anything, walk now computes one slice's extent — (height-1)*row_pitch + row bytes, checked — and requires depth_pitch >= it whenever depth > 1; a tighter pitch returns Error::TooSmall, exactly as an overlapping row pitch already does (row_pitch < bytes). Two-dimensional copies (depth == 1) are unaffected, as are exact and padded pitches.

Evidence

New test texture::tests::a_depth_pitch_that_overlaps_slices_is_refused on a 4x2x2 3D layout in LINEAR and SW_4KB_S modes:

  • a depth pitch one byte short of the slice: old code returns Ok(()) and the assertion expects Err(TooSmall) — the test fails on the old code;
  • exactly the slice's bytes and more: accepted before and after.

Verification (base 9a9091d):

  • cargo test -p victory-formats --lib --locked — 28 passed, 0 failed, 4 ignored
  • cargo test -p victory-hle --lib --locked -- --test-threads=1 — 103 passed, 0 failed (xg consumers unchanged)
  • cargo clippy -p victory-formats --all-targets --locked -- -D warnings and --all-features — clean; same for victory-hle
  • git diff --check clean; no new fmt diff (the 12 remaining texture.rs fmt entries on this toolchain are pre-existing lines, shifted by the added check).

Limitation: this rejects the overlapping pitch at validation time; it is a CPU-side contract fix and needs no GPU. A title that relies on the old silent behaviour would now get E_INVALIDARG from xg (the same treatment upstream chose for this class).

Coverage note

Open #653 (the upstream-picks bundle) also carries a fix for this defect, but it has been mergeable=false since 2026-11-25. This is an independent implementation written from this mission's audit evidence (the texture audit found the missing overlap check) and copies no code from #653 beyond knowing the problem exists.

<!-- greyfinch-sync:fxceless/victory-fork:issue:5527027513 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1472). Originally posted by [CwWwWwW](https://github.com/CwWwWwW) on 2026-11-27T12:58:15Z. --- ## The problem `Layout::walk` (`crates/victory-formats/src/texture.rs`, shared by `tile` and `detile`) computes the linear buffer it needs from the title's `row_pitch`/`depth_pitch`, but it never checks that one depth slice ends before the next one begins. A 3D copy whose depth pitch is smaller than one slice's rows — `(height-1)*row_pitch + row bytes` — makes `tile`/`detile` read or write two slices' rows at overlapping addresses **and still return `Ok`**, silently losing the earlier slice's rows. This is reachable from the title through xg's texture copy methods (0x40 into / 0x48 out of tiled memory), which pass the title's `row`/`depth` pitches straight into `detile`/`tile`. Silent data corruption, no memory unsafety (the copies stay inside the length they checked). ## The change Before copying anything, `walk` now computes one slice's extent — `(height-1)*row_pitch + row bytes`, checked — and requires `depth_pitch >=` it whenever `depth > 1`; a tighter pitch returns `Error::TooSmall`, exactly as an overlapping row pitch already does (`row_pitch < bytes`). Two-dimensional copies (`depth == 1`) are unaffected, as are exact and padded pitches. ## Evidence New test `texture::tests::a_depth_pitch_that_overlaps_slices_is_refused` on a `4x2x2` 3D layout in LINEAR and `SW_4KB_S` modes: - a depth pitch one byte short of the slice: old code returns `Ok(())` and the assertion expects `Err(TooSmall)` — the test fails on the old code; - exactly the slice's bytes and more: accepted before and after. Verification (base `9a9091d`): - `cargo test -p victory-formats --lib --locked` — 28 passed, 0 failed, 4 ignored - `cargo test -p victory-hle --lib --locked -- --test-threads=1` — 103 passed, 0 failed (xg consumers unchanged) - `cargo clippy -p victory-formats --all-targets --locked -- -D warnings` and `--all-features` — clean; same for `victory-hle` - `git diff --check` clean; no new fmt diff (the 12 remaining `texture.rs` fmt entries on this toolchain are pre-existing lines, shifted by the added check). Limitation: this rejects the overlapping pitch at validation time; it is a CPU-side contract fix and needs no GPU. A title that relies on the old silent behaviour would now get `E_INVALIDARG` from xg (the same treatment upstream chose for this class). ## Coverage note Open #653 (the upstream-picks bundle) also carries a fix for this defect, but it has been `mergeable=false` since 2026-11-25. This is an independent implementation written from this mission's audit evidence (the texture audit found the missing overlap check) and copies no code from #653 beyond knowing the problem exists.
akimfomin commented 2026-11-28 01:24:08 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by akimfomin on 2026-11-28T01:24:08Z.


shadPS4 #5453 fixes a separate image geometry boundary by checking legal extent. On 2fe51723 the depth-pitch rule could keep two synthetic layout checks tied together: reject a value one byte below a slice's true footprint, accept its exact footprint, and verify a padded six-slice layout still round-trips untouched padding. This compares validation behavior only; the addressing equations are different.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7231640707 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1472#issuecomment-7231640707). Originally posted by [akimfomin](https://github.com/akimfomin) on 2026-11-28T01:24:08Z. --- [shadPS4 #5453](https://github.com/shadps4-emu/shadPS4/pull/5453) fixes a separate image geometry boundary by checking legal extent. On `2fe51723` the depth-pitch rule could keep two synthetic layout checks tied together: reject a value one byte below a slice's true footprint, accept its exact footprint, and verify a padded six-slice layout still round-trips untouched padding. This compares validation behavior only; the addressing equations are different.
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/1472:upstream-pr/1472'
git switch 'upstream-pr/1472'

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