Skip to content

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

Closed
b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:d3d12x/present-write-frame-bounds
Closed

b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:d3d12x/present-write-frame-bounds

Conversation

@b3IKK

@b3IKK b3IKK commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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.

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.

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

Assisted-by: OpenCode (Gemini 4 Neon)
@futurepro62

futurepro62 commented Nov 24, 2026 •

Copy link
Copy Markdown

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.

gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 24, 2026
…#1395 in-place vtables

Integrates 16 upstream PRs (xcl-dev#1380, xcl-dev#1383, xcl-dev#1384, xcl-dev#1395, xcl-dev#1398, xcl-dev#1400,
xcl-dev#1404, xcl-dev#1405, xcl-dev#1419, xcl-dev#1420, xcl-dev#1421, xcl-dev#1423, xcl-dev#1424, xcl-dev#1427, xcl-dev#1428, xcl-dev#1429)
on top of the Linux/Proton fork, plus three Proton/vkd3d-proton fixes
caught during verification:
- Recompiler (xcl-dev#1428): flatten IO_GROUP_LIMIT early-return in compute
  shaders so vkd3d-proton v3.1 dxil-spirv accepts the emitted CFG.
- D3D12.X (xcl-dev#1395 + xcl-dev#867): skip UnadoptGuard on in-place-patched vtables
  and restore native vtables in runner::submit_list when a prior test
  adopted the singleton ID3D12Device.
- Tests: match Wine centered default in window_position_modes.

Verified under GE-Proton11-14 (RX 9060 XT / RADV):
- gpu_sparse_vram_descriptors_buffers_and_xmem + gpu_valu_float_and_int
  + window_position_modes: 3 passed, 0 failed
- Broad sweep (present, real, sparse, clamp, dstorage, gpu_valu,
  gpu_typed_buffer): 82 passed, 0 failed, 1 ignored, 0 skipped
- 2.5 min retail title run (XCL_MEM_LIMIT=12G): 3,000+ frames at 30.0
  fps, 6.0-6.1 GB flat committed RAM, 6.4 GB local VRAM, 0 DS failures.
@DavidM1911

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the stale No activity for 3 days; closed 4 days later unless something happens label Nov 28, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 39b7e4e

Main fixed the out-of-bounds read another way: commit 337c309 (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

throny commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Closing as superseded: this change is already on main (337c309). 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.

@throny throny added the duplicate This issue or pull request already exists label Dec 1, 2026
@throny throny closed this Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists stale No activity for 3 days; closed 4 days later unless something happens

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants