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

Open
github-bot wants to merge 1 commit from github-bot/gh-2299 into main AGit
Owner

@⁠denkencar-sketch on GitHub · 2026-11-25 10:05 UTC · original
denkencar-sketch:fix/list-003 → main · +63 −8 · 1 files · 1 commits · open

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.
**@⁠denkencar-sketch** on GitHub · 2026-11-25 10:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/2299) `denkencar-sketch:fix/list-003` → `main` · +63 −8 · 1 files · 1 commits · open 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.
Use host::write_mapped under watch::writing so CopyMemoryToMemoryX does not fault if pages are unmapped or read-only between the committed check and the copy. Disjoint ranges copy directly in one pass with no temporary buffer; overlapping ranges keep a committed-bounded staging buffer for memmove order.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:01:26 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 15:25 UTC · original · edited 2026-11-26 00:20 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 15:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/2299#issuecomment-7166828742) · edited 2026-11-26 00:20 UTC <!-- 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)_
Author
Owner

@⁠denkencar-sketch on GitHub · 2026-11-25 23:38 UTC · original

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.
**@⁠denkencar-sketch** on GitHub · 2026-11-25 23:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/2299#issuecomment-7177837834) 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.
Author
Owner

@⁠catminou4 on GitHub · 2026-11-25 10:19 UTC · original · reviewed

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.

**@⁠catminou4** on GitHub · 2026-11-25 10:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/2299#pullrequestreview-6055713184) · **reviewed** 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.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 19:51 UTC · original · requested changes

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

**@⁠fuzeofficlal** on GitHub · 2026-11-26 19:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/2299#pullrequestreview-6079792759) · **requested changes** 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`).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2299/head:github-bot/gh-2299'
git switch 'github-bot/gh-2299'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2299'
git switch 'github-bot/gh-2299'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2299'
git switch 'github-bot/gh-2299'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2299'
git switch 'main'
git merge --squash 'github-bot/gh-2299'
git switch 'main'
git merge --ff-only 'github-bot/gh-2299'
git switch 'main'
git merge 'github-bot/gh-2299'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2299
No description provided.