fix(gpu): stop copy_mapped on non-writable title destinations instead of faulting #3358

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

@⁠reeledMarek on GitHub · 2026-11-26 20:25 UTC · original
reeledMarek:fix/gpu-copy-mapped-readonly-dst → main · +79 −6 · 1 files · 1 commits · open

Summary

  • When copy_mapped(dst, src, len, title_dst = true) fails its initial ReadProcessMemory call because the destination contains committed PAGE_READONLY (or execute-only) pages, crate::texture::committed(dst, len) previously returned true (since committed only rejects PAGE_NOACCESS | PAGE_GUARD for readable sources).
  • The fallback loop then failed read() on each 4 KiB piece and executed unsafe { to.write_bytes(0, n) }, causing an immediate access violation on the read-only title destination.
  • Adds a writable(dst, len) check (requiring MEM_COMMIT, no PAGE_NOACCESS | PAGE_GUARD, and a writable base protection) before entering the fallback for title_dst, and routes title_dst zero-fills through read(ZERO_4K.as_ptr(), to, n) so concurrent protection changes cannot fault either.
  • Adds regression test title_copies::title_copies_do_not_fault_on_read_only_destinations using VirtualProtect(..., PAGE_READONLY).

Verification

  • cargo clippy -p victory-gpu --all-targets --target x86_64-pc-windows-msvc --locked -- -D warnings
  • cargo test -p xtask --locked
**@⁠reeledMarek** on GitHub · 2026-11-26 20:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/3358) `reeledMarek:fix/gpu-copy-mapped-readonly-dst` → `main` · +79 −6 · 1 files · 1 commits · open ### Summary - When `copy_mapped(dst, src, len, title_dst = true)` fails its initial `ReadProcessMemory` call because the destination contains committed `PAGE_READONLY` (or execute-only) pages, `crate::texture::committed(dst, len)` previously returned `true` (since `committed` only rejects `PAGE_NOACCESS | PAGE_GUARD` for readable sources). - The fallback loop then failed `read()` on each 4 KiB piece and executed `unsafe { to.write_bytes(0, n) }`, causing an immediate access violation on the read-only title destination. - Adds a `writable(dst, len)` check (requiring `MEM_COMMIT`, no `PAGE_NOACCESS | PAGE_GUARD`, and a writable base protection) before entering the fallback for `title_dst`, and routes `title_dst` zero-fills through `read(ZERO_4K.as_ptr(), to, n)` so concurrent protection changes cannot fault either. - Adds regression test `title_copies::title_copies_do_not_fault_on_read_only_destinations` using `VirtualProtect(..., PAGE_READONLY)`. ### Verification - `cargo clippy -p victory-gpu --all-targets --target x86_64-pc-windows-msvc --locked -- -D warnings` - `cargo test -p xtask --locked`
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 20:26:21 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 20:29 UTC · original · approved these changes

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

  • Checking writable(dst, len) (instead of only committed) and using read(ZERO_4K.as_ptr(), to, n) on title_dst prevents copy_mapped from faulting on read-only title destination pages during the fallback path.
  • cargo test -p victory-gpu --lib host::title_copies:: -- --test-threads=1: 3 passed, 0 failed (including title_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).

**@⁠Dukestar69** on GitHub · 2026-11-26 20:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/3358#pullrequestreview-6080166875) · **approved these changes** LGTM — verified on Windows against `origin/main` (`63bcca4`). - Checking `writable(dst, len)` (instead of only `committed`) and using `read(ZERO_4K.as_ptr(), to, n)` on `title_dst` prevents `copy_mapped` from faulting on read-only title destination pages during the fallback path. - `cargo test -p victory-gpu --lib host::title_copies:: -- --test-threads=1`: 3 passed, 0 failed (including `title_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).
Author
Owner

@⁠Jackster on GitHub · 2026-11-26 22:29 UTC · original

Windows 11, 63bcca4 + this PR, Radeon RX 7900 XTX:

  • host::title_copies::title_copies_do_not_fault_on_read_only_destinations by name: 1 passed (the test lives in host::title_copies, not in host::tests).
  • cargo test -p victory-gpu --lib: 232 passed / 0 failed / 1 ignored (unpatched 63bcca4 here: 231).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.

Synthetic only. AI-assisted.

**@⁠Jackster** on GitHub · 2026-11-26 22:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/3358#issuecomment-7204720918) Windows 11, `63bcca4` + this PR, Radeon RX 7900 XTX: - `host::title_copies::title_copies_do_not_fault_on_read_only_destinations` by name: 1 passed (the test lives in `host::title_copies`, not in `host::tests`). - `cargo test -p victory-gpu --lib`: 232 passed / 0 failed / 1 ignored (unpatched `63bcca4` here: 231). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. Synthetic only. AI-assisted.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 23:15 UTC · original · approved these changes

Reviewed ec8343cb merged onto main (63bcca43) — solid fix eliminating faults on non-writable title destinations with zero fast-path overhead.

Verification

  • cargo xtask policy: 191 files ok; cargo clippy --locked -p victory-gpu --all-targets -- -D warnings (both default and --features diagnostics): 0 warnings.
  • host::title_copies:: (3/3, including the new title_copies_do_not_fault_on_read_only_destinations test) passed on both RTX 5090 and WARP with --features diagnostics and VICTORY_D3D12_DEBUG=1.
  • Audited writable (crates/victory-gpu/src/host.rs:2542-2575), the ZERO_4K fallback (host.rs:2516, 2528, bounded by for_pieces's 4 KiB step), and all copy_mapped callers (host.rs, texture.rs) — early returns leave no mismatched mappings or leaks, and normal writable copies never incur VirtualQuery.
  • Adversarial VM protection & TOCTOU race probe (RTX 5090 and WARP):
    • Verified writable and copy_mapped(..., title_dst = true) reject and stop cleanly without faulting on PAGE_NOACCESS, PAGE_EXECUTE_READ, PAGE_READWRITE | PAGE_GUARD, cross-region RW + MEM_DECOMMIT + RW, and usize::MAX address overflow, while allowing and copying cleanly to PAGE_READWRITE | PAGE_NOCACHE and PAGE_EXECUTE_READWRITE (plus len == 0).
    • Stressed 300 iterations of holed-source copy_mapped (triggering the 4 KiB ZERO_4K fallback) while a concurrent thread continuously flipped the middle destination block between PAGE_READWRITE and PAGE_READONLY (116,111–124,113 concurrent VirtualProtect flips): 0 faults.

LGTM.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 23:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/3358#pullrequestreview-6081392852) · **approved these changes** Reviewed `ec8343cb` merged onto `main` (`63bcca43`) — solid fix eliminating faults on non-writable title destinations with zero fast-path overhead. ### Verification - `cargo xtask policy`: 191 files ok; `cargo clippy --locked -p victory-gpu --all-targets -- -D warnings` (both default and `--features diagnostics`): 0 warnings. - `host::title_copies::` (3/3, including the new `title_copies_do_not_fault_on_read_only_destinations` test) passed on both RTX 5090 and WARP with `--features diagnostics` and `VICTORY_D3D12_DEBUG=1`. - Audited `writable` (`crates/victory-gpu/src/host.rs:2542-2575`), the `ZERO_4K` fallback (`host.rs:2516, 2528`, bounded by `for_pieces`'s 4 KiB step), and all `copy_mapped` callers (`host.rs`, `texture.rs`) — early returns leave no mismatched mappings or leaks, and normal writable copies never incur `VirtualQuery`. - **Adversarial VM protection & TOCTOU race probe (RTX 5090 and WARP)**: - Verified `writable` and `copy_mapped(..., title_dst = true)` reject and stop cleanly without faulting on `PAGE_NOACCESS`, `PAGE_EXECUTE_READ`, `PAGE_READWRITE | PAGE_GUARD`, cross-region `RW + MEM_DECOMMIT + RW`, and `usize::MAX` address overflow, while allowing and copying cleanly to `PAGE_READWRITE | PAGE_NOCACHE` and `PAGE_EXECUTE_READWRITE` (plus `len == 0`). - Stressed 300 iterations of holed-source `copy_mapped` (triggering the 4 KiB `ZERO_4K` fallback) while a concurrent thread continuously flipped the middle destination block between `PAGE_READWRITE` and `PAGE_READONLY` (`116,111`–`124,113` concurrent `VirtualProtect` flips): 0 faults. LGTM.
Author
Owner

@⁠borosf on GitHub · 2026-11-27 11:35 UTC · original

A measurement and one dead end, both from copy_mapped on Intel Arc B580 with the game (origin/main 63bcca43 + #2958, diag build, world frame):

The fallback path is worth this PR's attention. In the world the diagnostics report 167.8 fallbacks (77.6 ms), i.e. ~0.46 ms each — about 6 % of a 1,200 ms frame spent entirely on copies that met an unmapped page. Anything that keeps a copy out of the fallback is measurable, so your committed() check is on the right track.

One shortcut that does not work, so nobody spends a day on it: resuming the piecewise walk where the kernel stopped, instead of starting it again at the beginning. ReadProcessMemory's lpNumberOfBytesRead is 0 when the call fails partway, not the count it transferred, so the fault offset is not available that way. I checked it with a test rather than by reading the docs: a 3 × 64 KiB source whose middle block is unmapped copies through this path, and a counter on the bytes the fallback re-reads came back at 196608 — all three blocks, the full range, with done == 0. So the leading bytes are copied twice on every fallback, and there is no cheap way to know where the hole was; the win has to come from not entering the fallback (as you are doing, and as #3125 argues for the 64 KiB page bookkeeping).

The existing doc comment on copy_mapped explains why VirtualQuery is not asked before each copy, but not this; worth one line if you touch that comment, since the resume-looking fix is the natural one to try.

Nothing here argues against your change — I have not run it. Synthetic tests only in the tree; the numbers are my own dump. Machine: Windows 11 x64, Ryzen 5 5600, 32 GB, Arc B580 12 GB.

**@⁠borosf** on GitHub · 2026-11-27 11:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/3358#issuecomment-7217257152) A measurement and one dead end, both from `copy_mapped` on Intel Arc B580 with the game (`origin/main` 63bcca43 + #2958, diag build, world frame): **The fallback path is worth this PR's attention.** In the world the diagnostics report `167.8 fallbacks (77.6 ms)`, i.e. ~0.46 ms each — about 6 % of a 1,200 ms frame spent entirely on copies that met an unmapped page. Anything that keeps a copy out of the fallback is measurable, so your `committed()` check is on the right track. **One shortcut that does not work, so nobody spends a day on it:** resuming the piecewise walk where the kernel stopped, instead of starting it again at the beginning. `ReadProcessMemory`'s `lpNumberOfBytesRead` is **0** when the call fails partway, not the count it transferred, so the fault offset is not available that way. I checked it with a test rather than by reading the docs: a 3 × 64 KiB source whose middle block is unmapped copies through this path, and a counter on the bytes the fallback re-reads came back at `196608` — all three blocks, the full range, with `done == 0`. So the leading bytes are copied twice on every fallback, and there is no cheap way to know where the hole was; the win has to come from *not entering* the fallback (as you are doing, and as #3125 argues for the 64 KiB page bookkeeping). The existing doc comment on `copy_mapped` explains why `VirtualQuery` is not asked before each copy, but not this; worth one line if you touch that comment, since the resume-looking fix is the natural one to try. Nothing here argues against your change — I have not run it. Synthetic tests only in the tree; the numbers are my own dump. Machine: Windows 11 x64, Ryzen 5 5600, 32 GB, Arc B580 12 GB.
This pull request can be merged automatically.
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/3358/head:github-bot/gh-3358'
git switch 'github-bot/gh-3358'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3358'
git switch 'github-bot/gh-3358'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3358'
git switch 'github-bot/gh-3358'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3358'
git switch 'main'
git merge --squash 'github-bot/gh-3358'
git switch 'main'
git merge --ff-only 'github-bot/gh-3358'
git switch 'main'
git merge 'github-bot/gh-3358'
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!3358
No description provided.