HLE/xmem: a page-mapping copy that cannot be served leaves the buffer it showed #841

Manually merged
github merged 1 commit from github-bot/gh-841 into main 2026-11-29 08:22:29 +00:00 AGit
Owner

@⁠CwWwWwW on GitHub · 2026-11-28 07:34 UTC · original
CwWwWwW:fix/xmem-map-atomicity → main · +334 −38 · 1 files · 1 commits · merged 2026-11-29 08:16 UTC

Ported to trustno1-coder/VIctory-NVIDIA from the same change submitted to fxceless/victory-fork as PR #1460 (head 605182c69c, author CwWwWwW). The fork's own line was dropped when it rebased onto 959dcaf (5dbf5ce), so this is submitted here directly. The description below is the original one; its 'Verification' numbers refer to the fork tree. Rebased onto main 63b902f.

The problem

XMemMapPhysicalPages takes the destination's views away before it looks at the page numbers it is given:

  • crates/victory-hle/src/modules/xmem.rs:853 - unmap(base, n * PAGE) turns every view covering the range back into an uncommitted placeholder;
  • only then, at :861, each page number is looked up in the physical page table; an unknown number returns null (:863);
  • a number that is unknown later in the list leaves the earlier runs mapped and the rest as a hole (:875-876).

A title keeps what its buffer showed on a remap that failed (XMemMapPhysicalPages returns null). Its next read or write is an access violation on the placeholder, and the GPU side has already been told the range changed (title_memory_remapped), so in-place copies see a hole too.

A title's page list can hold such a number: XMemFreePhysicalPages (:626-636) drops numbers from the table while views keep the section alive, and late page lists are a documented class here (dstorage.rs:1249-1254: "a page the title has just made ... comes right a little later", runs 175-178 lost reads to it). Open #619 recycles freed numbers, which narrows the window but does not change this call's failure behaviour.

The change

Every page number is now looked at before the destination is touched; when one cannot be served the call returns null with the memory unchanged. A call that can serve every page unmaps and maps as before.

Evidence

New test xmem::tests::a_remap_with_an_unknown_page_leaves_the_pages_it_showed (maps two pages, frees the second number, remaps [known, freed]) fails on the old code - the second page is MEM_RESERVE (8192) instead of MEM_COMMIT (4096) after the failed call - and passes with the change.

Verification on this branch (base 9a9091d):

  • cargo test -p victory-hle --lib --locked -- --test-threads=1 - 104 passed, 0 failed
  • cargo clippy -p victory-hle --all-targets --locked -- -D warnings - clean
  • cargo clippy -p victory-hle --all-targets --locked --all-features -- -D warnings - clean

No game launch; the test drives the real host mapping APIs (VirtualAlloc2/MapViewOfFile3) offline, as the module's existing tests do.

Scope and overlap

One file, crates/victory-hle/src/modules/xmem.rs. Open #299 fixes the sibling map_alias path (source snapshot before the destination is unmapped); this change is the XMemMapPhysicalPages path, which #299/#1060/#1103/#619/#1005 do not touch, and no other open PR was found for it. Host failures inside map() (commit limit, "not a placeholder") still leave a partial result as before; that needs rollback and is not attempted here.

CI's check may be red from main's own clippy and asset-policy failures (#1440, #1322), which this diff does not touch.

Follow-up after the tree replacement: the batched path is atomic too

The third commit (b656e5d) covers the new tree's batched page-mapping path, map_aliases (taken when side_by_side() > 1, default VICTORY_MAP_THREADS=8, and what victory-gpu's CopyPageMappingsBatchX uses). Before it, an unknown source page only set ok=false while that range's destination page was still unmapped, so a failed batch could leave the destination changed.

  • map_aliases keeps its gates and calls map_aliases_by_page(pages).
  • groups_of(pages) computes the existing group boundaries once (same rules: a group never shows a page that another group in the batch changes or copies from).
  • missing_source(pages, &groups) runs before any change/unmap: under PHYSICAL it resolves every source address with the same lookup as the single path (physical.slice_at), with a planned map for pages earlier groups will map. The first page it cannot serve: warning + false, nothing unmapped.
  • The apply loop uses the same precomputed groups and locks; the single path from 508f00f is untouched.

Regression test xmem::tests::a_batched_copy_with_an_unknown_source_leaves_every_destination_it_showed: three pages, the second group copying from an unmapped reserved address; asserts false and that every destination keeps its committed bytes. Without the pre-validation it fails (left: 90, right: 17 ??an earlier group had applied); with it, passes.

Verification: cargo check -p victory-hle --all-targets --locked clean; cargo test -p victory-hle --lib xmem::tests 26 passed / 1 ignored; full victory-hle lib suite 176 passed / 2 ignored; cargo clippy -p victory-hle --all-targets clean. Remaining gaps: a host failure inside change (as opposed to an unknown source page) can still leave earlier groups applied, and a thread unmapping a validated source mid-batch can fail after earlier groups changed; both are out of scope and documented in the commit.

Verification on this tree (base 63b902f, 2026-11-27)

  • cargo check --workspace --all-targets --locked: pass (Rust 1.98.1, the version pinned in rust-toolchain.toml)
  • cargo check --workspace --all-targets --all-features --locked: pass
  • cargo clippy --workspace --all-targets --locked -- -D warnings (and --all-features): this change adds no clippy finding. The only failures are on unmodified lines of main (clippy::manual_is_multiple_of at crates/victory-gpu/src/shadow.rs:1708 and crates/victory-gpu/src/descriptor.rs:1234, clippy::let_and_return at crates/victory-shader/src/emit/mod.rs:2768); CI run 46104876336 on 63b902f failed on those before this change.
  • cargo test was not run in the author's environment (Windows VM without an MSVC toolchain; test binaries could not be linked there). CI on Windows and a run on the card should confirm.

Hardware verification suggested

  • This changes xmem map failure semantics (pages are validated before the destination is touched). It does not cover XMemMapPhysicalPagesScatter; a failed batch there can still leave a hole (follow-up).
**@⁠CwWwWwW** on GitHub · 2026-11-28 07:34 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/841) `CwWwWwW:fix/xmem-map-atomicity` → `main` · +334 −38 · 1 files · 1 commits · merged 2026-11-29 08:16 UTC Ported to trustno1-coder/VIctory-NVIDIA from the same change submitted to fxceless/victory-fork as PR #1460 (head 605182c69c04c526e0bd5ed4f70462088c47b663, author CwWwWwW). The fork's own line was dropped when it rebased onto 959dcaf (5dbf5ce), so this is submitted here directly. The description below is the original one; its 'Verification' numbers refer to the fork tree. Rebased onto main 63b902f. ## The problem `XMemMapPhysicalPages` takes the destination's views away before it looks at the page numbers it is given: - `crates/victory-hle/src/modules/xmem.rs:853` - `unmap(base, n * PAGE)` turns every view covering the range back into an uncommitted placeholder; - only then, at `:861`, each page number is looked up in the physical page table; an unknown number returns null (`:863`); - a number that is unknown later in the list leaves the earlier runs mapped and the rest as a hole (`:875-876`). A title keeps what its buffer showed on a remap that failed (`XMemMapPhysicalPages` returns null). Its next read or write is an access violation on the placeholder, and the GPU side has already been told the range changed (`title_memory_remapped`), so in-place copies see a hole too. A title's page list can hold such a number: `XMemFreePhysicalPages` (`:626-636`) drops numbers from the table while views keep the section alive, and late page lists are a documented class here (`dstorage.rs:1249-1254`: "a page the title has just made ... comes right a little later", runs 175-178 lost reads to it). Open #619 recycles freed numbers, which narrows the window but does not change this call's failure behaviour. ## The change Every page number is now looked at before the destination is touched; when one cannot be served the call returns null with the memory unchanged. A call that can serve every page unmaps and maps as before. ## Evidence New test `xmem::tests::a_remap_with_an_unknown_page_leaves_the_pages_it_showed` (maps two pages, frees the second number, remaps `[known, freed]`) fails on the old code - the second page is `MEM_RESERVE` (8192) instead of `MEM_COMMIT` (4096) after the failed call - and passes with the change. Verification on this branch (base `9a9091d`): - `cargo test -p victory-hle --lib --locked -- --test-threads=1` - 104 passed, 0 failed - `cargo clippy -p victory-hle --all-targets --locked -- -D warnings` - clean - `cargo clippy -p victory-hle --all-targets --locked --all-features -- -D warnings` - clean No game launch; the test drives the real host mapping APIs (`VirtualAlloc2`/`MapViewOfFile3`) offline, as the module's existing tests do. ## Scope and overlap One file, `crates/victory-hle/src/modules/xmem.rs`. Open #299 fixes the sibling `map_alias` path (source snapshot before the destination is unmapped); this change is the `XMemMapPhysicalPages` path, which #299/#1060/#1103/#619/#1005 do not touch, and no other open PR was found for it. Host failures inside `map()` (commit limit, "not a placeholder") still leave a partial result as before; that needs rollback and is not attempted here. CI's `check` may be red from main's own clippy and asset-policy failures (#1440, #1322), which this diff does not touch. ## Follow-up after the tree replacement: the batched path is atomic too The third commit (b656e5d) covers the new tree's batched page-mapping path, `map_aliases` (taken when `side_by_side() > 1`, default `VICTORY_MAP_THREADS=8`, and what `victory-gpu`'s `CopyPageMappingsBatchX` uses). Before it, an unknown source page only set `ok=false` while that range's destination page was still unmapped, so a failed batch could leave the destination changed. - `map_aliases` keeps its gates and calls `map_aliases_by_page(pages)`. - `groups_of(pages)` computes the existing group boundaries once (same rules: a group never shows a page that another group in the batch changes or copies from). - `missing_source(pages, &groups)` runs before any `change`/unmap: under `PHYSICAL` it resolves every source address with the same lookup as the single path (`physical.slice_at`), with a `planned` map for pages earlier groups will map. The first page it cannot serve: warning + `false`, **nothing unmapped**. - The apply loop uses the same precomputed groups and locks; the single path from 508f00f is untouched. Regression test `xmem::tests::a_batched_copy_with_an_unknown_source_leaves_every_destination_it_showed`: three pages, the second group copying from an unmapped reserved address; asserts `false` and that every destination keeps its committed bytes. Without the pre-validation it fails (`left: 90, right: 17` ??an earlier group had applied); with it, passes. Verification: `cargo check -p victory-hle --all-targets --locked` clean; `cargo test -p victory-hle --lib xmem::tests` 26 passed / 1 ignored; full `victory-hle` lib suite 176 passed / 2 ignored; `cargo clippy -p victory-hle --all-targets` clean. Remaining gaps: a host failure inside `change` (as opposed to an unknown source page) can still leave earlier groups applied, and a thread unmapping a validated source mid-batch can fail after earlier groups changed; both are out of scope and documented in the commit. ## Verification on this tree (base 63b902f, 2026-11-27) - cargo check --workspace --all-targets --locked: pass (Rust 1.98.1, the version pinned in rust-toolchain.toml) - cargo check --workspace --all-targets --all-features --locked: pass - cargo clippy --workspace --all-targets --locked -- -D warnings (and --all-features): this change adds no clippy finding. The only failures are on unmodified lines of main (clippy::manual_is_multiple_of at crates/victory-gpu/src/shadow.rs:1708 and crates/victory-gpu/src/descriptor.rs:1234, clippy::let_and_return at crates/victory-shader/src/emit/mod.rs:2768); CI run 46104876336 on 63b902f failed on those before this change. - cargo test was not run in the author's environment (Windows VM without an MSVC toolchain; test binaries could not be linked there). CI on Windows and a run on the card should confirm. ## Hardware verification suggested - This changes xmem map failure semantics (pages are validated before the destination is touched). It does not cover XMemMapPhysicalPagesScatter; a failed batch there can still leave a hole (follow-up).
XMemMapPhysicalPages unmapped the destination views before looking at the page numbers; an unknown page then returned null with earlier runs mapped and the rest a hole, so the title kept a buffer whose pages were a placeholder (access violation on the next read, and the GPU side had been told the range changed). Look at every page number before touching the destination and leave the memory unchanged when one cannot be served.

Regression tests included.

Ported from fxceless/victory-fork PR #1460 (head b656e5db7f1cffac84453dd816f6d071c104d5f1).
Original author: CwWwWwW <[email protected]>.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 14:28:45 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-28 07:46 UTC · original · approved these changes

Tested locally merged onto main (63b902f).

  • Merge & Policy: Clean merge; cargo xtask policy passed (294 files ok).
  • Tests: cargo test -p victory-hle --lib xmem passed (27 passed; 0 failed; 1 ignored, including the new rollback/aliasing tests a_remap_with_an_unknown_page_leaves_the_pages_it_showed, a_batched_copy_with_an_unknown_source_leaves_every_destination_it_showed, a_failed_alias_leaves_the_destination_it_showed, an_alias_that_shifts_over_its_source_keeps_both_pages, and an_alias_of_a_page_over_itself_keeps_it) with 0 compiler warnings.

Strong transactional/rollback guarantees for page-mapping copies and aliases. LGTM.

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

**@⁠Dukestar69** on GitHub · 2026-11-28 07:46 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/841#pullrequestreview-6096611656) · **approved these changes** Tested locally merged onto `main` (`63b902f`). - **Merge & Policy**: Clean merge; `cargo xtask policy` passed (`294 files ok`). - **Tests**: `cargo test -p victory-hle --lib xmem` passed (`27 passed; 0 failed; 1 ignored`, including the new rollback/aliasing tests `a_remap_with_an_unknown_page_leaves_the_pages_it_showed`, `a_batched_copy_with_an_unknown_source_leaves_every_destination_it_showed`, `a_failed_alias_leaves_the_destination_it_showed`, `an_alias_that_shifts_over_its_source_keeps_both_pages`, and `an_alias_of_a_page_over_itself_keeps_it`) with 0 compiler warnings. Strong transactional/rollback guarantees for page-mapping copies and aliases. LGTM. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
github manually merged commit 0a52f7d772 into main 2026-11-29 08:22:29 +00:00
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/trustno1-coder--VIctory-NVIDIA!841
No description provided.