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

Open
reeledMarek wants to merge 1 commit from upstream-pr/3358 into main
reeledMarek commented 2026-11-26 20:25:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-26T20:25:35Z.


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
<!-- greyfinch-sync:Marvisak/VIctory:issue:5516694065 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3358). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-26T20:25:35Z. --- ### 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`
Dukestar69 commented 2026-11-26 20:29:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T20:29:00Z.

Review: APPROVED


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

<!-- greyfinch-sync:Marvisak/VIctory:review:6080166875 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3358#pullrequestreview-6080166875). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T20:29:00Z. **Review:** APPROVED --- 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).
Jackster commented 2026-11-26 22:29:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T22:29:12Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7204720918 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3358#issuecomment-7204720918). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-26T22:29:12Z. --- 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.
fuzeofficlal commented 2026-11-26 23:15:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T23:15:15Z.

Review: APPROVED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6081392852 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3358#pullrequestreview-6081392852). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T23:15:15Z. **Review:** APPROVED --- 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.
borosf commented 2026-11-27 11:35:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-27T11:35:51Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7217257152 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3358#issuecomment-7217257152). Originally posted by [borosf](https://github.com/borosf) on 2026-11-27T11:35:51Z. --- 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 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/3358:upstream-pr/3358'
git switch 'upstream-pr/3358'

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