xmem: replacing a title page leaves its neighbours mapped #2423

Open
faycalbaklouti wants to merge 1 commit from upstream-pr/2425 into main
faycalbaklouti commented 2026-11-25 13:14:29 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by faycalbaklouti on 2026-11-25T13:14:29Z.


What goes wrong

On main (63bcca43c6a0), XMemMapPhysicalPages maps consecutive slices of one section as a single host view (xmem.rs:715–734). After alias and scatter operations leave three separate placeholders, mapping the same three-page destination directly returns null. There is also a lifetime problem with a successful grouped view: removing just the middle page unmaps the whole view, then maps its outer pages again as read-write (xmem.rs:803–823,945–995). A retry handler cannot preserve an untouched page's mapping or its protection.

Change

Map each independently replaceable 64 KiB title page as its own view. Removing or replacing a page no longer requires unmapping its neighbours. The change is limited to crates/victory-hle/src/modules/xmem.rs; the proposed split-fault handler and unrelated allocator file are not in the patch. Private DirectStorage copy/decode views still use runs. The trade-off is more host views per mapping, not a speedup.

Verification

The same isolated test ran in fresh target directories with the Windows MSVC target and a non-root Wine runner:

cargo xwin test -vv --target x86_64-pc-windows-msvc --locked -p victory-hle --lib -- --exact modules::xmem::tests::replacing_a_middle_page_keeps_its_neighbours_protected
  • Main + only the new test (63bcca43c6a0), exit 101. At xmem.rs:1371, XMemMapPhysicalPages(dst, 3, pages.as_ptr()) returned 0x0, expected 0x100230000 (dst). The alias and scatter phases had completed; the direct phase never reached the sibling-protection assertion. test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 152 filtered out; finished in 0.00s This is not the old PR's alias: unmapping touched a sibling failure (440d0115f512, actual protection 4, expected 2).
  • Revised head (b9d511a2852b), exit 0. test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 152 filtered out; finished in 0.00s All three mapping paths completed.

On the byte-identical pre-amend tree (a02c764c0c83, Rust 1.98.1):

cargo xwin clippy -vv --target x86_64-pc-windows-msvc --workspace --all-targets --locked -- -D warnings: exit 0
cargo xwin clippy -vv --target x86_64-pc-windows-msvc --workspace --all-targets --all-features --locked -- -D warnings: exit 0
cargo xwin test -vv --target x86_64-pc-windows-msvc --workspace --locked: exit 1

The full workspace run did not pass under Wine. Unchanged main reproduced all five failures seen on the branch: DLL thread attach, two stand-in window tests, loopback, and the NTFS hard-link test. The branch run was stopped after the nested game-save test stalled; later tests and native Windows CI are unverified.


About our fork & censorship-resistant preservation effort:
This fix comes from gta6-vi-port, our open-source Linux (GE-Proton) + NVIDIA RTX 4090 fork of VIctory and peer-to-peer Radicle archival effort (mirroring 92+ community porting repositories across public Radicle seeds so open-source porting research survives centralized takedowns; open-source code only, no game files/keys/binaries).

<!-- greyfinch-sync:Marvisak/VIctory:issue:5492035042 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2425). Originally posted by [faycalbaklouti](https://github.com/faycalbaklouti) on 2026-11-25T13:14:29Z. --- ## What goes wrong On main (`63bcca43c6a0`), `XMemMapPhysicalPages` maps consecutive slices of one section as a single host view (`xmem.rs:715–734`). After alias and scatter operations leave three separate placeholders, mapping the same three-page destination directly returns null. There is also a lifetime problem with a successful grouped view: removing just the middle page unmaps the whole view, then maps its outer pages again as read-write (`xmem.rs:803–823,945–995`). A retry handler cannot preserve an untouched page's mapping or its protection. ## Change Map each independently replaceable 64 KiB title page as its own view. Removing or replacing a page no longer requires unmapping its neighbours. The change is limited to `crates/victory-hle/src/modules/xmem.rs`; the proposed split-fault handler and unrelated allocator file are not in the patch. Private DirectStorage copy/decode views still use runs. The trade-off is **more host views per mapping, not a speedup**. ## Verification The same isolated test ran in fresh target directories with the Windows MSVC target and a non-root Wine runner: ```text cargo xwin test -vv --target x86_64-pc-windows-msvc --locked -p victory-hle --lib -- --exact modules::xmem::tests::replacing_a_middle_page_keeps_its_neighbours_protected ``` - **Main + only the new test (`63bcca43c6a0`), exit 101.** At `xmem.rs:1371`, `XMemMapPhysicalPages(dst, 3, pages.as_ptr())` returned `0x0`, expected `0x100230000` (`dst`). The alias and scatter phases had completed; the direct phase never reached the sibling-protection assertion. `test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 152 filtered out; finished in 0.00s` This is not the old PR's `alias: unmapping touched a sibling` failure (`440d0115f512`, actual protection 4, expected 2). - **Revised head (`b9d511a2852b`), exit 0.** `test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 152 filtered out; finished in 0.00s` All three mapping paths completed. On the byte-identical pre-amend tree (`a02c764c0c83`, Rust 1.98.1): ```text cargo xwin clippy -vv --target x86_64-pc-windows-msvc --workspace --all-targets --locked -- -D warnings: exit 0 cargo xwin clippy -vv --target x86_64-pc-windows-msvc --workspace --all-targets --all-features --locked -- -D warnings: exit 0 cargo xwin test -vv --target x86_64-pc-windows-msvc --workspace --locked: exit 1 ``` The full workspace run **did not pass** under Wine. Unchanged main reproduced all five failures seen on the branch: DLL thread attach, two stand-in window tests, loopback, and the NTFS hard-link test. The branch run was stopped after the nested game-save test stalled; later tests and native Windows CI are unverified. --- **About our fork & censorship-resistant preservation effort:** This fix comes from **[gta6-vi-port](https://github.com/go2afri/gta6-vi-port)**, our open-source Linux (GE-Proton) + NVIDIA RTX 4090 fork of VIctory and peer-to-peer Radicle archival effort (mirroring 92+ community porting repositories across public Radicle seeds so open-source porting research survives centralized takedowns; open-source code only, no game files/keys/binaries). - **Canonical Radicle repo:** https://radicle.network/nodes/rosa.radicle.network/rad:z39ofPhQC1siudH9BF3xsmAMmUqxc (`rad:z39ofPhQC1siudH9BF3xsmAMmUqxc`) - **Live 1080p development stream:** https://vdo.ninja/?view=gta6rtx4090live1080p60 (Tor v3 `.onion` VP9+Opus viewer: `http://kwnoaeecpmxd6eetqfa7fxlymvxnh5du2i6wxr44t4mc7cgch5pw23id.onion/`)
catminou4 commented 2026-11-25 13:19:33 +00:00 (Migrated from github.com)

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

Review: COMMENTED


xmem: coalesce contiguous 64 KiB MapViewOfFile3 calls (+793, apps+hle). 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. Large; perf-motivated.

<!-- greyfinch-sync:Marvisak/VIctory:review:6058347205 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2425#pullrequestreview-6058347205). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T13:19:33Z. **Review:** COMMENTED --- xmem: coalesce contiguous 64 KiB MapViewOfFile3 calls (+793, apps+hle). 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. Large; perf-motivated.
Marvisak commented 2026-11-25 16:09:15 +00:00 (Migrated from github.com)

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


Automated triage for the maintainers.

Proposed close. This maps runs of contiguous physical pages as one view in crates/victory-hle/src/modules/xmem.rs (map_alias, scatter, unmap). Because unmap must then split those views, title memory is briefly unmapped while other threads may use it, and the PR adds a first-chance exception handler (on_split_fault) that spins and resumes faults inside that window or a ring of recent splits.

  • It adds a race main does not have. main maps one view per page and never unmaps live title memory to remap part of it; the new handler only papers over the window, and it runs ahead of the existing guard handler for every access violation in the process.
  • Motivation is outside the target. The stated reason is vm.max_map_count under Linux/Wine; no problem on Windows is shown.
  • It adds apps/victory/src/private_heap.rs with no mod declaration (that file belongs to #2424), and CI is failing.

A measured Windows-side reason to coalesce views, and a split that never leaves live memory unmapped, would change this verdict.

This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167002743 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2425#issuecomment-7167002743). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:32:30Z. --- <!-- victory-triage v1 head=440d0115f5124352c55033c851f6f63763fa6c91 verdict=proposed-close at=2026-11-25T15:36:00Z --> Automated triage for the maintainers. **Proposed close.** This maps runs of contiguous physical pages as one view in `crates/victory-hle/src/modules/xmem.rs` (`map_alias`, scatter, `unmap`). Because `unmap` must then split those views, title memory is briefly unmapped while other threads may use it, and the PR adds a first-chance exception handler (`on_split_fault`) that spins and resumes faults inside that window or a ring of recent splits. - **It adds a race `main` does not have.** `main` maps one view per page and never unmaps live title memory to remap part of it; the new handler only papers over the window, and it runs ahead of the existing guard handler for every access violation in the process. - **Motivation is outside the target.** The stated reason is `vm.max_map_count` under Linux/Wine; no problem on Windows is shown. - It adds `apps/victory/src/private_heap.rs` with no `mod` declaration (that file belongs to #2424), and CI is failing. A measured Windows-side reason to coalesce views, and a split that never leaves live memory unmapped, would change this verdict. This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label. --- _Generated by [Claude Code](https://claude.ai/code)_
kotekompotek commented 2026-11-25 17:05:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T16:56:26Z.


Both mechanisms hold up to close reading:

Coalescing — run detection requires same-section Arc + contiguous index, chunk_len stays inside both the source view and len, map_run falls back to per-page when a single view can't cover a reservation boundary, and MEM_COALESCE_PLACEHOLDERS is only applied to spans of ≥2 adjacent MEM_RESERVE placeholder regions (the kernel call itself rejects non-placeholder spans, so a mixed span just stays split → per-page fallback — safe degradation). The test pins the split-then-realise → single-view cycle end to end.

Split VEH — window opens before UnmapViewOfFile2 (fault can't slip in front), end writes HI before LO so a reader's lo < hi can't see a stale half-open range, the RECENT_SPLITS ring covers faults arriving microseconds after remap, and VirtualQuery + prot check before CONTINUE_EXECUTION keeps genuinely-unmapped addresses propagating. Allocation-free/lock-free as a VEH must be.

One robustness note: the spin loop in on_split_fault is unbounded — if the remapping thread were killed or panicked between begin/end, a faulting thread spins forever (silent deadlock). A bounded spin (say ~10 ms) then CONTINUE_SEARCH keeps the retry semantics for real remaps while making a stuck window a crash instead of a hang. Worth adding before this merges.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7168760447 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2425#issuecomment-7168760447). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T16:56:26Z. --- Both mechanisms hold up to close reading: **Coalescing** — run detection requires same-section `Arc` + contiguous `index`, `chunk_len` stays inside both the source view and `len`, `map_run` falls back to per-page when a single view can't cover a reservation boundary, and `MEM_COALESCE_PLACEHOLDERS` is only applied to spans of ≥2 adjacent `MEM_RESERVE` placeholder regions (the kernel call itself rejects non-placeholder spans, so a mixed span just stays split → per-page fallback — safe degradation). The test pins the split-then-realise → single-view cycle end to end. **Split VEH** — window opens before `UnmapViewOfFile2` (fault can't slip in front), `end` writes HI before LO so a reader's `lo < hi` can't see a stale half-open range, the RECENT_SPLITS ring covers faults arriving microseconds after remap, and `VirtualQuery` + prot check before `CONTINUE_EXECUTION` keeps genuinely-unmapped addresses propagating. Allocation-free/lock-free as a VEH must be. One robustness note: the spin loop in `on_split_fault` is unbounded — if the remapping thread were killed or panicked between `begin`/`end`, a faulting thread spins forever (silent deadlock). A bounded spin (say ~10 ms) then `CONTINUE_SEARCH` keeps the retry semantics for real remaps while making a stuck window a crash instead of a hang. Worth adding before this merges.
kotekompotek commented 2026-11-25 17:05:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T16:59:39Z.


Read both halves carefully. The coalescing logic is sound — run detection checks Arc::ptr_eq + index contiguity, map_run degrades to per-page on reservation boundaries, and the scatter batching preserves the map/unmap interleaving correctly. A few things need attention before merge:

1. private_heap.rs is dead code. Nothing declares mod private_heap and no #[global_allocator] installs PrivateHeap — the file compiles to nothing. It's also not mentioned in the PR body. If the intent is the VEH-safe allocator the doc comment describes (which is a real concern for on_split_fault's future growth), wire it in and explain it; otherwise drop the file.

2. Unbounded spin in on_split_fault. A faulting thread spins with no yield while the remapping thread (which holds the physical mutex) finishes. On a busy 4-core box a higher-priority faulting thread can livelock the remapper for a quantum at a time — and if the remapper ever dies mid-window, this spins forever. Suggest a bounded spin then SwitchToThread/YieldProcessor backoff.

3. coalesce_placeholders never checks AllocationBase. It coalesces any run of adjacent MEM_RESERVE regions — including a plain VirtualAlloc reservation parked next to our placeholders. The MEM_COALESCE_PLACEHOLDERS call fails atomically on non-placeholder spans, so today it just silently fails (result ignored → per-page fallback, safe). Still, comparing mbi.AllocationBase while extending span_end is a one-line tightening that avoids relying on the kernel rejecting mixed spans.

4. Sticky ring + masked protect bits. RECENT_SPLITS entries are never cleared — any address that was ever in a split window stays "recently split" forever (16 slots, overwritten). Combined with prot & 0xff stripping PAGE_GUARD/modifier bits, a guard-page fault inside a stale range would be swallowed by EXCEPTION_CONTINUE_EXECUTION before the intended handler sees it. Today nothing in the process uses PAGE_GUARD (gpu write-watch uses PAGE_READONLY, which correctly fails the write-prot check), but worth a defensive Protect & PAGE_GUARD != 0 -> CONTINUE_SEARCH to keep the handler a pure retry mechanism rather than a fault-eater for future protection tricks.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7168840585 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2425#issuecomment-7168840585). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T16:59:39Z. --- Read both halves carefully. The coalescing logic is sound — run detection checks `Arc::ptr_eq` + index contiguity, `map_run` degrades to per-page on reservation boundaries, and the scatter batching preserves the map/unmap interleaving correctly. A few things need attention before merge: **1. `private_heap.rs` is dead code.** Nothing declares `mod private_heap` and no `#[global_allocator]` installs `PrivateHeap` — the file compiles to nothing. It's also not mentioned in the PR body. If the intent is the VEH-safe allocator the doc comment describes (which is a real concern for `on_split_fault`'s future growth), wire it in and explain it; otherwise drop the file. **2. Unbounded spin in `on_split_fault`.** A faulting thread spins with no yield while the remapping thread (which holds the `physical` mutex) finishes. On a busy 4-core box a higher-priority faulting thread can livelock the remapper for a quantum at a time — and if the remapper ever dies mid-window, this spins forever. Suggest a bounded spin then `SwitchToThread`/`YieldProcessor` backoff. **3. `coalesce_placeholders` never checks `AllocationBase`.** It coalesces any run of adjacent `MEM_RESERVE` regions — including a plain `VirtualAlloc` reservation parked next to our placeholders. The `MEM_COALESCE_PLACEHOLDERS` call fails atomically on non-placeholder spans, so today it just silently fails (result ignored → per-page fallback, safe). Still, comparing `mbi.AllocationBase` while extending `span_end` is a one-line tightening that avoids relying on the kernel rejecting mixed spans. **4. Sticky ring + masked protect bits.** `RECENT_SPLITS` entries are never cleared — any address that was ever in a split window stays "recently split" forever (16 slots, overwritten). Combined with `prot & 0xff` stripping `PAGE_GUARD`/modifier bits, a guard-page fault inside a stale range would be swallowed by `EXCEPTION_CONTINUE_EXECUTION` before the intended handler sees it. Today nothing in the process uses `PAGE_GUARD` (gpu write-watch uses `PAGE_READONLY`, which correctly fails the write-prot check), but worth a defensive `Protect & PAGE_GUARD != 0 -> CONTINUE_SEARCH` to keep the handler a pure retry mechanism rather than a fault-eater for future protection tricks.
faycalbaklouti commented 2026-11-30 19:18:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by faycalbaklouti on 2026-11-30T19:18:33Z.


Thank you for identifying the lifetime problem. map_alias on mirror main maps one page per alias (xmem.rs:773–797). Coalescing these aliases into larger views makes partial unmapping remove live siblings before they are remapped (xmem.rs:981–996). Bounded spinning, narrower placeholder checks, or stricter recent-range and protection checks would not remove that interval. The allocator file also belongs only to #2424.

We have neither a gap-free replacement nor a measured Windows benefit. We are not asking you to merge this version. This is a comment only, not a code update, a request to close the PR, or a claim that CI is green.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7289180140 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2425#issuecomment-7289180140). Originally posted by [faycalbaklouti](https://github.com/faycalbaklouti) on 2026-11-30T19:18:33Z. --- Thank you for identifying the lifetime problem. `map_alias` on mirror main maps one page per alias (`xmem.rs:773–797`). Coalescing these aliases into larger views makes partial unmapping remove live siblings before they are remapped (`xmem.rs:981–996`). Bounded spinning, narrower placeholder checks, or stricter recent-range and protection checks would not remove that interval. The allocator file also belongs only to #2424. We have neither a gap-free replacement nor a measured Windows benefit. We are not asking you to merge this version. This is a comment only, not a code update, a request to close the PR, or a claim that CI is green.
faycalbaklouti commented 2026-11-30 20:52:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by faycalbaklouti on 2026-11-30T20:52:41Z.


Thank you for flagging the lifetime issue. I dropped the coalescing and retry handler rather than trying to spin through a fault. XMemMapPhysicalPages already groups adjacent pages on main: with just the new test on 63bcca43c6a0, the three-page direct remap after alias and scatter returned null (exit 101). That is distinct from the old PR's observed loss of a sibling's read-only protection. On b9d511a2852b each title page has its own view; the same test completes all three paths and keeps both neighbours read-only (1 passed, exit 0). This costs host views; it isn't a speedup. Both Clippy modes passed. The workspace run under Wine did not finish; main reproduced the five failures it reported.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7291595666 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2425#issuecomment-7291595666). Originally posted by [faycalbaklouti](https://github.com/faycalbaklouti) on 2026-11-30T20:52:41Z. --- Thank you for flagging the lifetime issue. I dropped the coalescing and retry handler rather than trying to spin through a fault. `XMemMapPhysicalPages` already groups adjacent pages on main: with just the new test on `63bcca43c6a0`, the three-page direct remap after alias and scatter returned null (exit 101). That is distinct from the old PR's observed loss of a sibling's read-only protection. On `b9d511a2852b` each title page has its own view; the same test completes all three paths and keeps both neighbours read-only (1 passed, exit 0). This costs host views; it isn't a speedup. Both Clippy modes passed. The workspace run under Wine did not finish; main reproduced the five failures it reported.
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/2425:upstream-pr/2425'
git switch 'upstream-pr/2425'

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