Skip to content

XG: CopyFromSubresource refuses a destination span it cannot write - #3738

Open
Royalflamejlh wants to merge 1 commit into
xcl-dev:mainfrom
Royalflamejlh:xg/copy-from-destination-span
Open

Royalflamejlh wants to merge 1 commit into
xcl-dev:mainfrom
Royalflamejlh:xg/copy-from-destination-span

Conversation

@Royalflamejlh

@Royalflamejlh Royalflamejlh commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Takes over #684 (its author has been inactive since 2026-11-24); keeps only its destination-span guard, since the ABI half landed in adccb7d.

The guard and its first tests are @JGonzalezRodriguez's, and the commit keeps him as its author; the writable check is the one @Marukooh proposed in #976. Thanks to both, and to everyone who tested #684.

CopyFromSubresource writes rows to dst at the title's row and slice pitches without checking them. With a wrong pitch the rows land far past the destination and the process dies in copy_from (the first crash of #662, from the misread argument that adccb7d fixed). With this change a row pitch under one row, a slice pitch under one slice (3D), or a span that is not writable memory returns E_INVALIDARG and logs the call (8 at most), as #684 did.

One change from #684: the span is checked with dstorage::writable, not gpu::shader::readable. On today's main, readable is wrong in both directions here: it refuses a destination in the title's lazy pages (mapped, committed on first use, which the copy itself would commit), and spans over 256 MiB; and it lets a committed read-only destination through, which then faults. Both cases fail under readable in the tests below. That is #976's switch, so #976 has nothing left once this is in; #992 (the source check) can rebase onto it.

Part of #662

What is in it

  • crates/xcl/src/hle/xg.rs: the span check in copy_from, after copy_args; tests copy_from_refuses_a_destination_span_it_cannot_write and copy_from_writes_a_padded_volume_into_lazy_pages.
  • crates/xcl/src/hle/dstorage.rs: writable is pub(super).
  • Dropped from XG: fix CopyFromSubresource 7-argument ABI and guard destination span #684: its 7-argument copy_from signature, the doc-comment change and the xg_oracle.rs change (main's copy_args takes both forms), and its two round-trip tests (subresource_copies_take_both_argument_forms on main covers the 2D one; the 3D one is now the lazy-pages test).

Tested

  • Head 45a8d7b1b on main 078358a05.
  • Linux, cross-built for x86_64-pc-windows-gnu (debug), tests under Wine: cargo check --workspace with and without --tests: no errors, no warnings in the two files. cargo test -p xcl --lib -- hle::xg::tests: 6 passed, 0 failed, 0 ignored. cargo test -p xcl --test xg_oracle: 6 passed, 0 failed, 12 ignored (XCL_SAMPLE_XG unset).
  • Red without it: with the guard off, copy_from_refuses_a_destination_span_it_cannot_write fails (row pitch under a row returns S_OK). With readable in place of writable, copy_from_writes_a_padded_volume_into_lazy_pages fails (the lazy destination refused) and the read-only case faults the test process.
  • Windows, NVIDIA GeForce RTX 2070 SUPER 8 GB (driver 621.01), Ryzen 7 5700X3D, Windows 11 Pro 26200, rustc 1.100.0, debug: cargo test --workspace -- --test-threads=4: 3,008 passed, 0 failed, 52 ignored; 29 SKIPPED lines (22 gpu_ tests, 7 others), hle::xg::tests among the passed.

Regression run
RTX 2070 SUPER 8 GB (driver 621.01), Ryzen 7 5700X3D, Windows 11 Pro 26200, release builds by scripts/smoke.ps1, the same retail title for 60 s each.

main:

xcl-smoke v1
commit: 5eaadb54c14e0fc29597b2a1689d7dbd83491303
gpu: NVIDIA GeForce RTX 2070 SUPER
title: 5330474d
seconds: 60
exit: 0x0
frames shown: 1575 (PresentX 1575)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15670 MB

this PR's head:

xcl-smoke v1
commit: 45a8d7b1b39b3ab296a6019dd20662d824338391
gpu: NVIDIA GeForce RTX 2070 SUPER
title: 5330474d
seconds: 60
exit: 0x0
frames shown: 1439 (PresentX 1439)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 15814 MB

Not tested

AI use

  • Claude Code (Claude Opus 5.5) ported the guard onto main, switched it to writable, wrote the tests, ran the builds and tests and wrote this description.

CopyFromSubresource wrote its rows to `dst` at the title's row and slice
pitches without checking them. A wrong pitch (on main before adccb7d, the
low half of the source pointer) sent the rows gigabytes past the
destination and the title died in `copy_from` (xcl-dev#662). adccb7d reads the
arguments right; this keeps a bad span from becoming an access violation:
a row pitch under one row, a slice pitch under one slice, or a span that
is not writable memory returns E_INVALIDARG and logs the call (8 at most).

The span is checked with DirectStorage's `writable`, not
`gpu::shader::readable` as first written: `readable` refused a
destination in the title's lazy pages (mapped, committed on first use,
which the copy itself would commit) and spans over 256 MiB, and let a
read-only one through to fault.

The guard is from xcl-dev#684; the writable check is the one xcl-dev#976 proposed.

Co-authored-by: Marco Luzzaro <marcoluzzaro99@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Review of 45a8d7b1 (code reading only, not a merge-check; draft) · 1 nit

In short: The PR takes over #684's destination-span guard for CopyFromSubresource. A row pitch under one row, a slice pitch under one slice (3D), or a destination span that is not writable memory now returns E_INVALIDARG and is logged, at most 8 times. It checks the span with dstorage::writable, not gpu::shader::readable as #684 did. The guard and its arithmetic are right. Nothing blocks it; there is one test-hygiene nit.

Nit

  1. The lazy-pages test leaves its page marked. copy_from_writes_a_padded_volume_into_lazy_pages calls kernelx::lazy::mark(lazy) and then VirtualFrees the reservation without unmarking it, so LAZY keeps a stale entry. A later lazy::ensure over that address in the same test process commits it. That includes writable from another test, such as the 31 GB "rows past the allocation" span here. If the address is reused by another test's reservation, it gets committed under it. Unmark the page before freeing it, for example through a small #[cfg(test)] helper in lazy.rs, since unmark is pub(super).
Checked and fine
  • The span matches what the copy writes. 2D writes m.height rows of row bytes, row_pitch apart, so (m.height - 1) * row_pitch + row. 3D writes m.depth slices of those rows, slice_pitch apart. The slice_pitch < rows check applies only when m.depth > 1.
  • No overflow: with a u32 pitch and at most 16K rows or slices, the products stay far below usize::MAX, and writable uses checked_add for dst + span.
  • writable commits the destination's lazy pages first, as the copy itself would, then accepts the committed UMA window, sparse memory it knows, or regions VirtualQuery reports committed and writable. That is why a lazy destination now passes and a read-only one is refused, both of which readable got wrong.
  • The guard runs after copy_args, so both argument forms are checked. The tests cover row pitch, a span past the allocation, read-only memory, slice pitch, and a padded volume into lazy pages compared row by row.
  • Scope: writable becomes pub(super), and nothing else outside xg.rs changes. XG: require writable memory for CopyFromSubresource #976 (the same writable switch) has nothing left once this lands, and XG: check the tiled source before copying a subresource #992 (the source check) can rebase onto it, as the description says.

Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point.

@github-actions github-actions Bot added area:directstorage DirectStorage queues, requests, decompression area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH labels Dec 2, 2026
@Royalflamejlh
Royalflamejlh marked this pull request as ready for review December 2, 2026 23:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:directstorage DirectStorage queues, requests, decompression area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants