GPU: harden CopyMemoryToMemoryX against unmapped or read-only pages #2297

Open
denkencar-sketch wants to merge 1 commit from upstream-pr/2299 into main
denkencar-sketch commented 2026-11-25 10:05:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by denkencar-sketch on 2026-11-25T10:05:34Z.


Crash-hardening for \CopyMemoryToMemoryX\ at submission:

  • On \main, \copy_memory\ checks \ exture::committed\ and then runs a raw \std::ptr::copy. While writes into \watch-guarded blocks are already caught by the vectored handler on \main, a page that is unmapped concurrently or marked read-only by the title still faults \std::ptr::copy.
  • \copy_title_memory\ now uses \host::write_mapped\ under \watch::writing: disjoint ranges copy directly in one pass with no allocation or double-copy, and only overlapping ranges allocate a staging buffer (bounded by \ exture::committed) to preserve \memmove\ semantics.
  • Tested with disjoint copies, overlapping copies, a \PAGE_READONLY\ destination page, and absurd sizes (\usize::MAX). Not run in the title.
<!-- greyfinch-sync:Marvisak/VIctory:issue:5489191326 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2299). Originally posted by [denkencar-sketch](https://github.com/denkencar-sketch) on 2026-11-25T10:05:34Z. --- Crash-hardening for \CopyMemoryToMemoryX\ at submission: - On \main\, \copy_memory\ checks \ exture::committed\ and then runs a raw \std::ptr::copy\. While writes into \watch\-guarded blocks are already caught by the vectored handler on \main\, a page that is unmapped concurrently or marked read-only by the title still faults \std::ptr::copy\. - \copy_title_memory\ now uses \host::write_mapped\ under \watch::writing\: disjoint ranges copy directly in one pass with no allocation or double-copy, and only overlapping ranges allocate a staging buffer (bounded by \ exture::committed\) to preserve \memmove\ semantics. - Tested with disjoint copies, overlapping copies, a \PAGE_READONLY\ destination page, and absurd sizes (\usize::MAX\). Not run in the title.
catminou4 commented 2026-11-25 10:19:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T10:19:16Z.

Review: COMMENTED


CopyMemoryToMemoryX copies through the kernel (memmove semantics, partial writability) instead of ptr::copy. Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1. Safer copy path for title memory.

<!-- greyfinch-sync:Marvisak/VIctory:review:6055713184 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2299#pullrequestreview-6055713184). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T10:19:16Z. **Review:** COMMENTED --- CopyMemoryToMemoryX copies through the kernel (memmove semantics, partial writability) instead of ptr::copy. Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1. Safer copy path for title memory.
Marvisak commented 2026-11-25 16:09:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15:25:48Z.


Automated triage for the maintainers.

Merge candidate. copy_memory in crates/victory-gpu/src/list.rs now defers to copy_title_memory, which checks the source with texture::committed, copies disjoint ranges straight through host::write_mapped under watch::writing (no buffer, one pass), and stages through a buffer only when the ranges overlap, so memmove order is kept. Both points from the last triage are addressed: the description is now framed as crash hardening, and the per-copy allocation is gone for the common disjoint case. A destination page the title made read-only is now skipped rather than faulting; that is a hardening trade-off the maintainer may want to note.

On your reply: confirmed in the diff, both changes are there. CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7166828742 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2299#issuecomment-7166828742). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:25:48Z. --- <!-- victory-triage v1 head=2caaace8bf066394b321ff1b88ef8fa705e435c3 verdict=merge-candidate at=2026-11-26T00:20:07Z --> Automated triage for the maintainers. **Merge candidate.** `copy_memory` in `crates/victory-gpu/src/list.rs` now defers to `copy_title_memory`, which checks the source with `texture::committed`, copies disjoint ranges straight through `host::write_mapped` under `watch::writing` (no buffer, one pass), and stages through a buffer only when the ranges overlap, so `memmove` order is kept. Both points from the last triage are addressed: the description is now framed as crash hardening, and the per-copy allocation is gone for the common disjoint case. A destination page the title made read-only is now skipped rather than faulting; that is a hardening trade-off the maintainer may want to note. On your reply: confirmed in the diff, both changes are there. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync changed title from [PR #2299] GPU: CopyMemoryToMemoryX copies through the kernel and tells the write watchers to [PR #2299] GPU: harden CopyMemoryToMemoryX against unmapped or read-only pages 2026-11-26 00:01:50 +00:00
denkencar-sketch commented 2026-11-26 00:03:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by denkencar-sketch on 2026-11-25T23:38:07Z.


Addressed both triage points and rebased onto current \main\ (\842b0cb):

  • Reframed the PR description accurately as crash-hardening (removing the inaccurate claim about write-watch notifications on \main).
  • Avoided the per-submission \Vec\ allocation and double-copy for non-overlapping ranges by calling \host::write_mapped\ directly under \watch::writing, keeping the staging buffer only for overlapping ranges (\memmove\ semantics).
  • Added #[cfg(windows)]\ and a disjoint-copy check to the unit test. \cargo test\ and \clippy\ pass.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7177837834 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2299#issuecomment-7177837834). Originally posted by [denkencar-sketch](https://github.com/denkencar-sketch) on 2026-11-25T23:38:07Z. --- Addressed both triage points and rebased onto current \main\ (\842b0cb\): - Reframed the PR description accurately as crash-hardening (removing the inaccurate claim about write-watch notifications on \main\). - Avoided the per-submission \Vec\ allocation and double-copy for non-overlapping ranges by calling \host::write_mapped\ directly under \watch::writing\, keeping the staging buffer only for overlapping ranges (\memmove\ semantics). - Added \#[cfg(windows)]\ and a disjoint-copy check to the unit test. \cargo test\ and \clippy\ pass.
fuzeofficlal commented 2026-11-26 19:51:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T19:51:55Z.

Review: CHANGES_REQUESTED


Tested 2caaace8bf066394b321ff1b88ef8fa705e435c3 merged onto origin/main (63bcca43c6a00a758510398fa43096ce7e4f7df0) on Windows 11 x64 (RTX 5090 + Microsoft WARP, --features diagnostics, Rust 1.98.1 MSVC).

Verified the positive cases cleanly: disjoint copies, overlapping (memmove) copies, PAGE_READONLY destinations, huge sizes, and copying between two watch-guarded (PAGE_READONLY) pool pages (confirming texture::committed accepts watch-guarded src and watch::writing opens and dirties dst).

Blocking Regression: high-address dst overflows host::for_pieces (panic in debug/diag builds, infinite wraparound loop in release builds)

On origin/main, copy_memory guarded both src and dst with crate::texture::committed, which rejected overflowing and top-of-address-space dst ranges (committed(usize::MAX - 4, 16) == false, committed(usize::MAX - 100, 16) == false).

In this PR, copy_title_memory (crates/victory-gpu/src/list.rs:1104-1124) removes committed(dst, len) and calls crate::host::write_mapped(dst as *mut u8, ...) directly. When src is valid committed memory and dst is near usize::MAX, the whole-range ReadProcessMemory fails and falls into host::for_pieces (crates/victory-gpu/src/host.rs:2576-2584), which has two unchecked overflows:

  1. dst + len overflows (dst = usize::MAX - 4, len = 16):
    • host.rs:2577 (let end = start + len;) panics in debug/diag builds:
      panicked at crates\victory-gpu\src\host.rs:2577:15: attempt to add with overflow
  2. dst lies inside the top 64 KiB without dst + len overflowing (dst = usize::MAX - 100, len = 16):
    • host.rs:2580 (let next = ((at / step + 1) * step).min(end);) panics in debug/diag builds:
      panicked at crates\victory-gpu\src\host.rs:2580:20: attempt to multiply with overflow
    • In --release builds (wrapping arithmetic), (at / 65536 + 1) * 65536 wraps to 0, so next = 0.min(end) = 0 and at resets to 0, turning while at < end into a 2^48-iteration loop that hangs the submission thread indefinitely while calling ReadProcessMemory across the entire 64-bit address space.

Suggested Fix

  1. Reject overflowing destination ranges up front in copy_title_memory (crates/victory-gpu/src/list.rs:1105):
    if len == 0 || dst == src || dst.checked_add(len).is_none() {
        return;
    }
    
  2. Make for_pieces (crates/victory-gpu/src/host.rs:2576-2584) overflow-safe at the top of the address space:
    fn for_pieces(start: usize, len: usize, step: usize, mut f: impl FnMut(usize, usize)) {
        let end = start.saturating_add(len);
        let mut at = start;
        while at < end {
            let next = (at / step)
                .checked_add(1)
                .and_then(|q| q.checked_mul(step))
                .map_or(end, |n| n.min(end));
            f(at, next - at);
            at = next;
        }
    }
    

Verified this fix locally in both --features diagnostics (debug) and --release builds: copy_title_memory returns instantaneously with zero panics or wraparound for dst = usize::MAX - 4, usize::MAX - 100, and usize::MAX - 4095, while all list::tests and host::title_copies tests pass (4/4 in 0.00s).

<!-- greyfinch-sync:Marvisak/VIctory:review:6079792759 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2299#pullrequestreview-6079792759). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T19:51:55Z. **Review:** CHANGES_REQUESTED --- Tested `2caaace8bf066394b321ff1b88ef8fa705e435c3` merged onto `origin/main` (`63bcca43c6a00a758510398fa43096ce7e4f7df0`) on Windows 11 x64 (RTX 5090 + Microsoft WARP, `--features diagnostics`, Rust 1.98.1 MSVC). Verified the positive cases cleanly: disjoint copies, overlapping (`memmove`) copies, `PAGE_READONLY` destinations, huge sizes, and copying between two `watch`-guarded (`PAGE_READONLY`) pool pages (confirming `texture::committed` accepts `watch`-guarded `src` and `watch::writing` opens and dirties `dst`). ### Blocking Regression: high-address `dst` overflows `host::for_pieces` (panic in debug/diag builds, infinite wraparound loop in release builds) On `origin/main`, `copy_memory` guarded both `src` and `dst` with `crate::texture::committed`, which rejected overflowing and top-of-address-space `dst` ranges (`committed(usize::MAX - 4, 16) == false`, `committed(usize::MAX - 100, 16) == false`). In this PR, `copy_title_memory` (`crates/victory-gpu/src/list.rs:1104-1124`) removes `committed(dst, len)` and calls `crate::host::write_mapped(dst as *mut u8, ...)` directly. When `src` is valid committed memory and `dst` is near `usize::MAX`, the whole-range `ReadProcessMemory` fails and falls into `host::for_pieces` (`crates/victory-gpu/src/host.rs:2576-2584`), which has two unchecked overflows: 1. **`dst + len` overflows (`dst = usize::MAX - 4`, `len = 16`)**: - `host.rs:2577` (`let end = start + len;`) panics in debug/diag builds: `panicked at crates\victory-gpu\src\host.rs:2577:15: attempt to add with overflow` 2. **`dst` lies inside the top 64 KiB without `dst + len` overflowing (`dst = usize::MAX - 100`, `len = 16`)**: - `host.rs:2580` (`let next = ((at / step + 1) * step).min(end);`) panics in debug/diag builds: `panicked at crates\victory-gpu\src\host.rs:2580:20: attempt to multiply with overflow` - In `--release` builds (wrapping arithmetic), `(at / 65536 + 1) * 65536` wraps to `0`, so `next = 0.min(end) = 0` and `at` resets to `0`, turning `while at < end` into a `2^48`-iteration loop that hangs the submission thread indefinitely while calling `ReadProcessMemory` across the entire 64-bit address space. #### Suggested Fix 1. Reject overflowing destination ranges up front in `copy_title_memory` (`crates/victory-gpu/src/list.rs:1105`): ```rust if len == 0 || dst == src || dst.checked_add(len).is_none() { return; } ``` 2. Make `for_pieces` (`crates/victory-gpu/src/host.rs:2576-2584`) overflow-safe at the top of the address space: ```rust fn for_pieces(start: usize, len: usize, step: usize, mut f: impl FnMut(usize, usize)) { let end = start.saturating_add(len); let mut at = start; while at < end { let next = (at / step) .checked_add(1) .and_then(|q| q.checked_mul(step)) .map_or(end, |n| n.min(end)); f(at, next - at); at = next; } } ``` Verified this fix locally in both `--features diagnostics` (debug) and `--release` builds: `copy_title_memory` returns instantaneously with zero panics or wraparound for `dst = usize::MAX - 4`, `usize::MAX - 100`, and `usize::MAX - 4095`, while all `list::tests` and `host::title_copies` tests pass (`4/4` in `0.00s`).
Dukestar69 commented 2026-11-26 21:50:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T21:50:02Z.

Review: APPROVED


LGTM — verified on RTX 5090 (Windows / D3D12) against origin/main (63bcca4).

  • Hardening CopyMemoryToMemoryX against unmapped or read-only pages prevents faults cleanly.
  • cargo test -p victory-gpu --lib list:: -- --test-threads=1: 5 passed, 0 failed (including memory_copies_do_not_fault_on_read_only_destinations).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6080861429 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2299#pullrequestreview-6080861429). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T21:50:02Z. **Review:** APPROVED --- LGTM — verified on RTX 5090 (Windows / D3D12) against `origin/main` (`63bcca4`). - Hardening `CopyMemoryToMemoryX` against unmapped or read-only pages prevents faults cleanly. - `cargo test -p victory-gpu --lib list:: -- --test-threads=1`: 5 passed, 0 failed (including `memory_copies_do_not_fault_on_read_only_destinations`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
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/2299:upstream-pr/2299'
git switch 'upstream-pr/2299'

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