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

Open
Royalflamejlh wants to merge 1 commit from upstream-pr/3738 into main
Royalflamejlh commented 2026-12-02 22:50:41 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Royalflamejlh on 2026-12-02T22:50:41Z.


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 #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

  • AMD and Intel GPUs; xg_oracle against the console's library; a retail run that reaches a refused call (no run of #684 logged one).

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.
<!-- greyfinch-sync:ivall/xcl:issue:5616121052 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3738). Originally posted by [Royalflamejlh](https://github.com/Royalflamejlh) on 2026-12-02T22:50:41Z. --- 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 #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** - AMD and Intel GPUs; `xg_oracle` against the console's library; a retail run that reaches a refused call (no run of #684 logged one). **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.
Foniass commented 2026-12-02 22:53:28 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T22:53:28Z.


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. #976 (the same writable switch) has nothing left once this lands, and #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.

<!-- greyfinch-sync:ivall/xcl:comment:7361292677 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3738#issuecomment-7361292677). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-02T22:53:28Z. --- **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 `VirtualFree`s 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)`. <details><summary>Checked and fine</summary> - 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. #976 (the same `writable` switch) has nothing left once this lands, and #992 (the source check) can rebase onto it, as the description says. </details> <sub>Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point.</sub>
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/3738:upstream-pr/3738'
git switch 'upstream-pr/3738'

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