D3D12.X: bounds-check readback reads in present::write_frame #1414

Closed
b3IKK wants to merge 1 commit from upstream-pr/1424 into main
b3IKK commented 2026-11-24 15:48:50 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T15:48:50Z.


present::write_frame unconditionally indexed fp.Offset + step * y * pitch + 4 * step * x as a 4-byte texel for all x < w / step, y < h / step. For 1/2-bpp intermediate render targets (e.g. R8_UNORM, R16_FLOAT) dumped by XCL_TRACE_FRAME, 4 * step * x exceeds RowPitch; for block-compressed targets whose readback footprint contains only ceil(Height / 4) block-rows, step * y * pitch reads past the end of the mapped readback buffer.

This adds readback_texel_offset() checking both row_off + 4 <= RowPitch and off + 4 <= rb.GetDesc().Width with checked arithmetic, treating out-of-bounds samples as undecoded rather than reading past the mapped buffer.

What is in it

  • crates/xcl/src/hle/d3d12x/present.rs: add readback_texel_offset() and use it in write_frame; add unit test readback_texel_offset_bounds_checks_narrow_and_bc_footprints.

Tested

  • hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 752 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 no wave64 — gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware)
  • cargo test -p xcl --lib readback_texel_offset_bounds_checks_narrow_and_bc_footprints: 1 passed, 0 failed

Regression run

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1226 (PresentX 1226)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15341 MB

PR:

xcl-smoke v1
commit: 39b7e4e61ae655503d0fe23d55b05f4e9b0f0b25
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1640 (PresentX 1640)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14599 MB

Not tested

  • AMD GPUs and native wave64 hardware
  • other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine

AI use

  • OpenCode, Gemini 4 Neon: audited present.rs, wrote the fix and unit test, ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.
<!-- greyfinch-sync:ivall/xcl:issue:5475520082 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1424). Originally posted by [b3IKK](https://github.com/b3IKK) on 2026-11-24T15:48:50Z. --- `present::write_frame` unconditionally indexed `fp.Offset + step * y * pitch + 4 * step * x` as a 4-byte texel for all `x < w / step`, `y < h / step`. For 1/2-bpp intermediate render targets (e.g. `R8_UNORM`, `R16_FLOAT`) dumped by `XCL_TRACE_FRAME`, `4 * step * x` exceeds `RowPitch`; for block-compressed targets whose readback footprint contains only `ceil(Height / 4)` block-rows, `step * y * pitch` reads past the end of the mapped readback buffer. This adds `readback_texel_offset()` checking both `row_off + 4 <= RowPitch` and `off + 4 <= rb.GetDesc().Width` with checked arithmetic, treating out-of-bounds samples as undecoded rather than reading past the mapped buffer. **What is in it** - `crates/xcl/src/hle/d3d12x/present.rs`: add `readback_texel_offset()` and use it in `write_frame`; add unit test `readback_texel_offset_bounds_checks_narrow_and_bc_footprints`. **Tested** - hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (`stable-x86_64-pc-windows-msvc`), DXC 1.9.2609.5, debug (`scripts/test.ps1`) and release (`scripts/smoke.ps1`) - `.\scripts\test.ps1` (`cargo build --workspace` + `cargo test --workspace -- --test-threads=4`): 752 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 `no wave64` — `gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware`) - `cargo test -p xcl --lib readback_texel_offset_bounds_checks_narrow_and_bc_footprints`: 1 passed, 0 failed **Regression run** main: ``` xcl-smoke v1 commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1226 (PresentX 1226) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15341 MB ``` PR: ``` xcl-smoke v1 commit: 39b7e4e61ae655503d0fe23d55b05f4e9b0f0b25 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1640 (PresentX 1640) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14599 MB ``` **Not tested** - AMD GPUs and native wave64 hardware - other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine **AI use** - OpenCode, Gemini 4 Neon: audited `present.rs`, wrote the fix and unit test, ran the test suite and `scripts/smoke.ps1` regression runs, and drafted this PR description.
futurepro62 commented 2026-11-24 16:07:25 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-24T15:58:05Z.


Full-suite run, since this PR has no test report yet. Tested exact head 39b7e4e (on its base 930e8b4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 752 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: readback_texel_offset_bounds_checks_narrow_and_bc_footprints.
  • Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 752 passed, 0 failed, 41 ignored, no skips.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:ivall/xcl:comment:7136908206 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1424#issuecomment-7136908206). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-24T15:58:05Z. --- Full-suite run, since this PR has no test report yet. Tested exact head `39b7e4e` (on its base `930e8b4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed. - `cargo build --workspace`, then `XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4`: **752 passed, 0 failed, 41 ignored**. SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - The new tests ran and passed: `readback_texel_offset_bounds_checks_narrow_and_bc_footprints`. - Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, `XCL_ADAPTER=radeon`), `cargo test --workspace -- --test-threads=4`: **752 passed, 0 failed, 41 ignored**, no skips. **Not tested** - a release build, a game (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
DavidM1911 commented 2026-11-24 18:08:59 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T17:21:27Z.


Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored.

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7139071802 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1424#issuecomment-7139071802). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T17:21:27Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~21 ignored. Thanks!
grahamster2 commented 2026-11-25 01:10:53 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by grahamster2 on 2026-11-25T00:48:44Z.


Tested 39b7e4e (CPU tests only, no game run): RTX 3080 10 GB (610.88), Ryzen 5 7600X, Windows 11 26200, rustc 1.94.0, debug.

  • cargo build --workspace + cargo test --workspace --no-fail-fast -- --test-threads=4: 752 passed, 0 failed, 41 ignored; SKIPPED: 2 (no wave64).
  • Merged locally onto current main (6567c5f): no conflicts; cargo test -p xcl --lib present:: 7 passed, including readback_texel_offset_bounds_checks_narrow_and_bc_footprints.
  • The bound is right for the reported case: with the 4-byte step, an R8 target's samples past x = RowPitch/16 now come out undecoded instead of being read past the row. A small suggestion, not blocking: the test covers the helper but not write_frame itself. A test that maps a small R8 footprint through write_frame would pin the end-to-end behaviour. Taking the stride from the format would be a separate follow-up.
  • The branch is 205 commits behind main; a rebase would let CI run on the current tree.

Not tested: AMD, and a game run with XCL_FRAME_DUMP_FULL=1.

Claude Code with Claude Opus 5.5 ran the tests and wrote this note.

<!-- greyfinch-sync:ivall/xcl:comment:7148902060 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1424#issuecomment-7148902060). Originally posted by [grahamster2](https://github.com/grahamster2) on 2026-11-25T00:48:44Z. --- Tested `39b7e4e` (CPU tests only, no game run): RTX 3080 10 GB (610.88), Ryzen 5 7600X, Windows 11 26200, rustc 1.94.0, debug. - `cargo build --workspace` + `cargo test --workspace --no-fail-fast -- --test-threads=4`: 752 passed, 0 failed, 41 ignored; SKIPPED: 2 (`no wave64`). - Merged locally onto current main (`6567c5f`): no conflicts; `cargo test -p xcl --lib present::` 7 passed, including `readback_texel_offset_bounds_checks_narrow_and_bc_footprints`. - The bound is right for the reported case: with the 4-byte step, an R8 target's samples past x = RowPitch/16 now come out undecoded instead of being read past the row. A small suggestion, not blocking: the test covers the helper but not `write_frame` itself. A test that maps a small R8 footprint through `write_frame` would pin the end-to-end behaviour. Taking the stride from the format would be a separate follow-up. - The branch is 205 commits behind main; a rebase would let CI run on the current tree. **Not tested:** AMD, and a game run with `XCL_FRAME_DUMP_FULL=1`. _Claude Code with Claude Opus 5.5 ran the tests and wrote this note._
github-actions[bot] commented 2026-11-28 08:01:11 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-11-28T08:01:11Z.


Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

<!-- greyfinch-sync:ivall/xcl:comment:7236308567 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1424#issuecomment-7236308567). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-28T08:01:11Z. --- Nothing has happened on this PR for 3 days, so it is marked `stale` and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto `main` if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.
Foniass commented 2026-12-01 05:42:39 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:42:39Z.


Triage (#1607) – head 39b7e4e

Main fixed the out-of-bounds read another way: commit 337c3094 (merged with #3203) makes write_frame read each sample at bytes * step * x with bytes = targets::texel_bytes(format), so a 1- or 2-byte target no longer runs past its row. The fixed 4-byte read this PR bounds-checks is gone, so the PR conflicts and its helper would read at the wrong stride now.
The BC case in the test does not reach write_frame, which only dumps render targets and the frame.
Recommendation: close (superseded by main commit 337c309).

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7302054530 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1424#issuecomment-7302054530). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:42:39Z. --- **Triage (#1607)** – head 39b7e4e Main fixed the out-of-bounds read another way: commit 337c3094 (merged with #3203) makes `write_frame` read each sample at `bytes * step * x` with `bytes = targets::texel_bytes(format)`, so a 1- or 2-byte target no longer runs past its row. The fixed 4-byte read this PR bounds-checks is gone, so the PR conflicts and its helper would read at the wrong stride now. The BC case in the test does not reach `write_frame`, which only dumps render targets and the frame. Recommendation: close (superseded by main commit 337c309). AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
throny commented 2026-12-01 12:45:58 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-12-01T12:45:58Z.


Closing as superseded: this change is already on main (337c3094). main present.rs write_frame reads each texel at its own size via targets::texel_bytes, row index scaled by bytes; the 1-2 byte over-read is gone (PR's BC-format bounds check not needed for render-target dumps)

Two independent checks found this: Foniass's close list on #1607 and ours against current main. If something here is still missing on main, say so and reopen it, or ask and I will.

AI use: Claude Code (Claude Opus 5.5); the per-PR comparison was done by Claude Sonnet 5.5 sub-agents.

<!-- greyfinch-sync:ivall/xcl:comment:7311614149 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1424#issuecomment-7311614149). Originally posted by [throny](https://github.com/throny) on 2026-12-01T12:45:58Z. --- Closing as superseded: this change is already on `main` (337c3094). main present.rs write_frame reads each texel at its own size via targets::texel_bytes, row index scaled by bytes; the 1-2 byte over-read is gone (PR's BC-format bounds check not needed for render-target dumps) Two independent checks found this: Foniass's close list on #1607 and ours against current `main`. If something here is still missing on `main`, say so and reopen it, or ask and I will. AI use: Claude Code (Claude Opus 5.5); the per-PR comparison was done by Claude Sonnet 5.5 sub-agents.

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
xcl-dev/xcl!1414
No description provided.