D3D12.X: clamp destination rows, slices, and buffer_bytes in footprint_plan and guard GetCopyableFootprints failure #3432

Open
Kalmarv wants to merge 1 commit from upstream-pr/3432 into main
Kalmarv commented 2026-11-29 15:42:18 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T15:42:18Z.


What changed

  • Added buffer_bytes: u64 to FootprintSide::Destination in crates/xcl/src/hle/d3d12x/list_x.rs and passed buffer.GetDesc().Width from copy_texture_region_realigned.
  • Clamped FootprintSide::Destination rows to ..rows, slices to ..depth, and each RowCopy's byte length to buffer_bytes.saturating_sub(at) (breaking once at >= buffer_bytes), matching FootprintSide::Source.
  • Added overflow checks for scratch_bytes in footprint_plan (passing through on overflow) and saturating range arithmetic in copy_texture_region_realigned.
  • Guarded copy_texture_region_realigned::unit() against GetCopyableFootprints failure (rows == 0 || row_size == 0 || row_size == u64::MAX).
  • Added unit tests in footprint_plan_tests covering destination row/slice clamping to scratch_bytes, tight destination buffer_bytes truncation/omission, and overflowing scratch_bytes.

Why

  • FootprintSide::Source clamped rows, slices, and per-row copy lengths against buffer_bytes, while FootprintSide::Destination iterated ys and zs unclamped with no destination buffer bound. A texture-to-buffer CopyTextureRegion whose box or subresource extent exceeded the placed footprint's Height/Depth or targeted a tightly sized buffer emitted RowCopy regions past scratch_bytes or buffer_bytes, causing out-of-bounds CopyBufferRegion calls.
  • When GetCopyableFootprints fails (e.g. invalid subresource index), D3D12 writes u64::MAX into row_size, which unit() divided into a huge per_block value instead of returning None.

Tested

  • cargo check --workspace --all-targets (0 errors, 0 new warnings).
  • cargo build --workspace and cargo build --release -p xcl.
  • Targeted unit tests: footprint_plan_tests (10/10 passed; texture_to_buffer_clamps_rows_slices_and_buffer_bytes reproduced OOB RowCopy past scratch_bytes on origin/main 37be0959 before the fix).
  • Full workspace GPU test suite on NVIDIA GeForce RTX 5070 Ti (cargo test --workspace -- --test-threads=4, exit 0, 0 failures across all crates).
  • 150 s xcl-smoke on PR head (def60f8d) and main-ancestor baseline (f0e8fdd5):
xcl-smoke v1
commit: def60f8dc27b06db6c06fbc92d03e121b4a17401
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1666 (PresentX 1666)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 32667 MB
xcl-smoke v1
commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1695 (PresentX 1695)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 31546 MB

Not tested

  • AMD or Intel GPUs (tested on NVIDIA GeForce RTX 5070 Ti only).
  • Longer-than-150 s title sessions or titles beyond ea3b5e4d.
  • Direct D3D12 debug-layer execution of a failing GetCopyableFootprints call inside copy_texture_region_realigned.

AI use

  • Authored and verified with an AI coding assistant; all code, tests, builds, GPU test runs, and 150 s smoke verification were executed locally against clean-room tests and real hardware.
<!-- greyfinch-sync:ivall/xcl:issue:5552308366 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3432). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-29T15:42:18Z. --- **What changed** - Added `buffer_bytes: u64` to `FootprintSide::Destination` in `crates/xcl/src/hle/d3d12x/list_x.rs` and passed `buffer.GetDesc().Width` from `copy_texture_region_realigned`. - Clamped `FootprintSide::Destination` `rows` to `..rows`, `slices` to `..depth`, and each `RowCopy`'s byte length to `buffer_bytes.saturating_sub(at)` (breaking once `at >= buffer_bytes`), matching `FootprintSide::Source`. - Added overflow checks for `scratch_bytes` in `footprint_plan` (passing through on overflow) and saturating range arithmetic in `copy_texture_region_realigned`. - Guarded `copy_texture_region_realigned::unit()` against `GetCopyableFootprints` failure (`rows == 0 || row_size == 0 || row_size == u64::MAX`). - Added unit tests in `footprint_plan_tests` covering destination row/slice clamping to `scratch_bytes`, tight destination `buffer_bytes` truncation/omission, and overflowing `scratch_bytes`. **Why** - `FootprintSide::Source` clamped `rows`, `slices`, and per-row copy lengths against `buffer_bytes`, while `FootprintSide::Destination` iterated `ys` and `zs` unclamped with no destination buffer bound. A texture-to-buffer `CopyTextureRegion` whose box or subresource extent exceeded the placed footprint's `Height`/`Depth` or targeted a tightly sized buffer emitted `RowCopy` regions past `scratch_bytes` or `buffer_bytes`, causing out-of-bounds `CopyBufferRegion` calls. - When `GetCopyableFootprints` fails (e.g. invalid subresource index), D3D12 writes `u64::MAX` into `row_size`, which `unit()` divided into a huge `per_block` value instead of returning `None`. **Tested** - `cargo check --workspace --all-targets` (0 errors, 0 new warnings). - `cargo build --workspace` and `cargo build --release -p xcl`. - Targeted unit tests: `footprint_plan_tests` (10/10 passed; `texture_to_buffer_clamps_rows_slices_and_buffer_bytes` reproduced OOB `RowCopy` past `scratch_bytes` on `origin/main` `37be0959` before the fix). - Full workspace GPU test suite on NVIDIA GeForce RTX 5070 Ti (`cargo test --workspace -- --test-threads=4`, exit 0, 0 failures across all crates). - 150 s `xcl-smoke` on PR head (`def60f8d`) and `main`-ancestor baseline (`f0e8fdd5`): ``` xcl-smoke v1 commit: def60f8dc27b06db6c06fbc92d03e121b4a17401 gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1666 (PresentX 1666) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 32667 MB ``` ``` xcl-smoke v1 commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1695 (PresentX 1695) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 31546 MB ``` **Not tested** - AMD or Intel GPUs (tested on NVIDIA GeForce RTX 5070 Ti only). - Longer-than-150 s title sessions or titles beyond `ea3b5e4d`. - Direct D3D12 debug-layer execution of a failing `GetCopyableFootprints` call inside `copy_texture_region_realigned`. **AI use** - Authored and verified with an AI coding assistant; all code, tests, builds, GPU test runs, and 150 s smoke verification were executed locally against clean-room tests and real hardware.
Foniass commented 2026-12-01 03:33:16 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T03:33:16Z.


merge-check
base: d0832e614f
head: def60f8dc2
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2891 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none. The pipeline_library / warm_library tests that failed in this PR's CI run all pass here, so that CI failure doesn't come from this diff.
review: read the whole diff (list_x.rs, +53/-10). The clamp fixes a real out-of-bounds write-back: on main the Destination branch of footprint_plan doesn't clamp rows or slices, so r = z*rows + y can point at the next slice or past the scratch buffer, and stale scratch bytes are written over the title's buffer or the CopyBufferRegion goes out of bounds. Valid copies get the same copy list as on main; only copies whose box already runs past the footprint's Height/Depth or the buffer's end change. The new test's expected values are right (rows 6 and 7, then 32 and 20 bytes), and its logic would fail on main. Findings: (1) the unit() guard for a failed GetCopyableFootprints (~6770) relies on row_size coming back as u64::MAX, which Microsoft doesn't document, and no test covers it; harmless as a fallback. (2) bytes: bx0..bx0 + w.div_ceil(bw) * per_block is still not saturating while rows and slices now are (low risk with the guard). (3) Description: AI use names neither tool nor model; Tested has no passed/failed/ignored counts; both of the PR's smoke runs end at presents per 2 s: 0 0 0, so the title had stopped presenting and the copy path is probably not reached there, which Not tested should say.
performance: 60 s smoke of a retail title, main d0832e61 vs this merge (eb221f70): 1516 -> 1669 frames shown (110 %; 60 presents per 2 s in both), peak commit 18133 -> 16913 MB, no device removal, no crash. Valid copies get the same copy list as on main; one more GetDesc per scratch-path copy.
verdict: safe to merge (after a CI rerun; the description should name the AI model and add the suite counts)
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7299825913 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3432#issuecomment-7299825913). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T03:33:16Z. --- merge-check base: d0832e614fa7d6ae027bb4feb27ccb9121f54a8f head: def60f8dc27b06db6c06fbc92d03e121b4a17401 merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2891 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none. The pipeline_library / warm_library tests that failed in this PR's CI run all pass here, so that CI failure doesn't come from this diff. review: read the whole diff (list_x.rs, +53/-10). The clamp fixes a real out-of-bounds write-back: on main the Destination branch of `footprint_plan` doesn't clamp rows or slices, so `r = z*rows + y` can point at the next slice or past the scratch buffer, and stale scratch bytes are written over the title's buffer or the CopyBufferRegion goes out of bounds. Valid copies get the same copy list as on main; only copies whose box already runs past the footprint's Height/Depth or the buffer's end change. The new test's expected values are right (rows 6 and 7, then 32 and 20 bytes), and its logic would fail on main. Findings: (1) the `unit()` guard for a failed GetCopyableFootprints (~6770) relies on `row_size` coming back as u64::MAX, which Microsoft doesn't document, and no test covers it; harmless as a fallback. (2) `bytes: bx0..bx0 + w.div_ceil(bw) * per_block` is still not saturating while rows and slices now are (low risk with the guard). (3) Description: AI use names neither tool nor model; **Tested** has no passed/failed/ignored counts; both of the PR's smoke runs end at `presents per 2 s: 0 0 0`, so the title had stopped presenting and the copy path is probably not reached there, which **Not tested** should say. performance: 60 s smoke of a retail title, main d0832e61 vs this merge (eb221f70): 1516 -> 1669 frames shown (110 %; 60 presents per 2 s in both), peak commit 18133 -> 16913 MB, no device removal, no crash. Valid copies get the same copy list as on main; one more GetDesc per scratch-path copy. verdict: safe to merge (after a CI rerun; the description should name the AI model and add the suite counts) hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5 AI-written and posted by Foniass without a human check of each point.
bmjask commented 2026-12-02 05:44:27 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T05:44:27Z.

Review: COMMENTED


Review of def60f8d: 3 finding(s). 2 are inline. These are on lines outside the diff:

crates/xcl/src/hle/d3d12x/list_x.rs:6687: The saturating math wasn't applied to the Source arm: offset + r0 * p (6687) and offset + r * p (6696) are still unchecked. A buffer-to-texture copy with a junk title Offset (e.g. 0xFFFF_FFFF_FFFF_FF00) and pitch 100 panics in debug inside an extern "system" hook, which aborts the process. In release it wraps to a small at, buffer_bytes.saturating_sub(at) is nonzero, and CopyBufferRegion reads the wrong bytes into the texture.


These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.

<!-- greyfinch-sync:ivall/xcl:review:6145396250 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3432#pullrequestreview-6145396250). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T05:44:27Z. **Review:** COMMENTED --- Review of `def60f8d`: 3 finding(s). 2 are inline. These are on lines outside the diff: **`crates/xcl/src/hle/d3d12x/list_x.rs:6687`**: The saturating math wasn't applied to the Source arm: `offset + r0 * p` (6687) and `offset + r * p` (6696) are still unchecked. A buffer-to-texture copy with a junk title Offset (e.g. 0xFFFF_FFFF_FFFF_FF00) and pitch 100 panics in debug inside an `extern "system"` hook, which aborts the process. In release it wraps to a small `at`, `buffer_bytes.saturating_sub(at)` is nonzero, and CopyBufferRegion reads the wrong bytes into the texture. --- _These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it._
bmjask commented 2026-12-02 05:44:28 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T05:44:28Z.

Code review location: crates/xcl/src/hle/d3d12x/list_x.rs; line 6707

@@ -6700,13 +6703,19 @@ fn footprint_plan(offset: u64, row_pitch: u32, format: u32, height: u32, depth:
                 }
             }
         }
-        FootprintSide::Destination { rows: ys, slices: zs, bytes: xs } => {
+        FootprintSide::Destination { buffer_bytes, rows: ys, slices: zs, bytes: xs } => {
+            let (ys, zs) = (ys.start..ys.end.min(rows as u32), zs.start..zs.end.min(depth));

When a texture-to-buffer box runs past the footprint, the Destination arm cuts rows/slices down to the footprint, but the PC texture copy into the scratch (~6817) still uses the original out-of-range y/z/bx, and the clipped rows are still written back to the title's buffer. A CopyTextureRegion into a placed footprint with Height=4, dst y=2 and a box of height 4 (rows 2..6) has a scratch footprint with the same Height/Depth, so PC D3D12 rejects the copy or does something undefined. footprint_plan still returns ViaScratch with write-backs for rows 2..4, so whatever the scratch held from earlier copies overwrites the title's buffer. Returning PassThrough/false (with a warning) when the box goes past fp.Height/Depth/Width would avoid writing garbage.

<!-- greyfinch-sync:ivall/xcl:review-comment:4765280868 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3432#discussion_r4765280868). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T05:44:28Z. **Code review location:** `crates/xcl/src/hle/d3d12x/list_x.rs`; line 6707 ~~~~diff @@ -6700,13 +6703,19 @@ fn footprint_plan(offset: u64, row_pitch: u32, format: u32, height: u32, depth: } } } - FootprintSide::Destination { rows: ys, slices: zs, bytes: xs } => { + FootprintSide::Destination { buffer_bytes, rows: ys, slices: zs, bytes: xs } => { + let (ys, zs) = (ys.start..ys.end.min(rows as u32), zs.start..zs.end.min(depth)); ~~~~ --- When a texture-to-buffer box runs past the footprint, the Destination arm cuts `rows`/`slices` down to the footprint, but the PC texture copy into the scratch (~6817) still uses the original out-of-range `y`/`z`/`bx`, and the clipped rows are still written back to the title's buffer. A CopyTextureRegion into a placed footprint with Height=4, dst y=2 and a box of height 4 (rows 2..6) has a scratch footprint with the same Height/Depth, so PC D3D12 rejects the copy or does something undefined. `footprint_plan` still returns ViaScratch with write-backs for rows 2..4, so whatever the scratch held from earlier copies overwrites the title's buffer. Returning PassThrough/false (with a warning) when the box goes past fp.Height/Depth/Width would avoid writing garbage.
bmjask commented 2026-12-02 05:44:28 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T05:44:28Z.

Code review location: crates/xcl/src/hle/d3d12x/list_x.rs; line 6674

@@ -6670,7 +6671,9 @@ fn footprint_plan(offset: u64, row_pitch: u32, format: u32, height: u32, depth:
     }
     let (p, q) = (row_pitch as u64, (row_pitch as u64).next_multiple_of(align));
     let rows = height.div_ceil(block_height(format)) as u64;
-    let scratch_bytes = rows * depth as u64 * q;
+    let Some(scratch_bytes) = rows.checked_mul(depth as u64).and_then(|n| n.checked_mul(q)) else {

When scratch_bytes overflows, footprint_plan returns PassThrough, so the caller sends the misaligned footprint (Offset%512 != 0 or RowPitch%256 != 0) to PC D3D12 unchanged. That's invalid API use, and unlike the copy_scratch failure path nothing is logged, so the copy is dropped silently or the debug layer reports it. Returning a distinct skip/fail result with a warn(&N, W, ...) would match the other failure path.

<!-- greyfinch-sync:ivall/xcl:review-comment:4765280873 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3432#discussion_r4765280873). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T05:44:28Z. **Code review location:** `crates/xcl/src/hle/d3d12x/list_x.rs`; line 6674 ~~~~diff @@ -6670,7 +6671,9 @@ fn footprint_plan(offset: u64, row_pitch: u32, format: u32, height: u32, depth: } let (p, q) = (row_pitch as u64, (row_pitch as u64).next_multiple_of(align)); let rows = height.div_ceil(block_height(format)) as u64; - let scratch_bytes = rows * depth as u64 * q; + let Some(scratch_bytes) = rows.checked_mul(depth as u64).and_then(|n| n.checked_mul(q)) else { ~~~~ --- When `scratch_bytes` overflows, `footprint_plan` returns PassThrough, so the caller sends the misaligned footprint (Offset%512 != 0 or RowPitch%256 != 0) to PC D3D12 unchanged. That's invalid API use, and unlike the `copy_scratch` failure path nothing is logged, so the copy is dropped silently or the debug layer reports it. Returning a distinct skip/fail result with a `warn(&N, W, ...)` would match the other failure path.
bmjask commented 2026-12-02 14:13:53 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T14:13:53Z.


@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.

<!-- greyfinch-sync:ivall/xcl:comment:7348201534 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3432#issuecomment-7348201534). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T14:13:53Z. --- @Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.
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/3432:upstream-pr/3432'
git switch 'upstream-pr/3432'

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