HLE/xmem: track and coalesce fragmented placeholders on remap, snapshot map_alias sources #299

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

@⁠AscilCH on GitHub · 2026-11-24 23:25 UTC · original
AscilCH:fix/xmem-coalesce-placeholders-and-alias → main · +272 −26 · 1 files · 1 commits · open

Problem

Two bugs in physical-page remapping in crates/victory-hle/src/modules/xmem.rs:

  1. Fragmented placeholders break multi-page XMemMapPhysicalPages remapping:
    • UnmapViewOfFile2(..., MEM_PRESERVE_PLACEHOLDER) turns each unmapped view back into an independent placeholder of that view's exact size; Windows does not auto-coalesce adjacent placeholders (and VirtualQueryEx merges identical reserved regions, hiding placeholder boundaries).
    • map() only called VirtualFree(base, len, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER), which can split one larger placeholder but cannot coalesce multiple adjacent placeholders.
    • Example: reserve 3 pages at a, map 1 page at a (splitting into view [a, a+PAGE) + placeholder [a+PAGE, a+3*PAGE)), then remap 2 contiguous physical pages at a. Unmapping [a, a+PAGE) leaves two separate placeholders; XMemMapPhysicalPages coalesces the physical run to 2 * PAGE and MapViewOfFile3(..., MEM_REPLACE_PLACEHOLDER) fails with ERROR_INVALID_ADDRESS (487), returning NULL and leaving [a, a+PAGE) unmapped.
  2. map_alias unmaps destination before resolving source slices:
    • map_alias(to, from, len) called unmap(&mut physical, to, len) before reading [from, from + len) from physical.views. Overlapping aliases destroyed their own source mappings, and a missing source page left to unmapped before returning false.

Fix

  • Track exact placeholder intervals in Physical::placeholders (updated in XMemVirtualAlloc, map, and unmap).
  • Add prepare_placeholder(physical, base, len): splits any tracked placeholder straddling base or base + len, then calls VirtualFree(base, len, MEM_RELEASE | MEM_COALESCE_PLACEHOLDERS) whenever multiple placeholders tile [base, base + len) before MapViewOfFile3.
  • In map_alias, snapshot all source Slice entries for [from, from + len) before calling unmap(&mut physical, to, len).
  • Add unit tests remapping_contiguous_run_over_fragmented_placeholders_succeeds and map_alias_handles_overlapping_ranges_and_missing_sources.

Verification

  • rustfmt --edition 2024 --check crates/victory-hle/src/modules/xmem.rs (changed hunks): clean
  • cargo test -p victory-hle --locked xmem -- --test-threads=1: 13 passed, 0 failed
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-hle --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic tests only, no game data.)

**@⁠AscilCH** on GitHub · 2026-11-24 23:25 UTC · [original](https://github.com/fxceless/victory-fork/pull/299) `AscilCH:fix/xmem-coalesce-placeholders-and-alias` → `main` · +272 −26 · 1 files · 1 commits · open ## Problem Two bugs in physical-page remapping in `crates/victory-hle/src/modules/xmem.rs`: 1. **Fragmented placeholders break multi-page `XMemMapPhysicalPages` remapping**: - `UnmapViewOfFile2(..., MEM_PRESERVE_PLACEHOLDER)` turns each unmapped view back into an independent placeholder of that view's exact size; Windows does not auto-coalesce adjacent placeholders (and `VirtualQueryEx` merges identical reserved regions, hiding placeholder boundaries). - `map()` only called `VirtualFree(base, len, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER)`, which can split one larger placeholder but cannot coalesce multiple adjacent placeholders. - Example: reserve 3 pages at `a`, map 1 page at `a` (splitting into view `[a, a+PAGE)` + placeholder `[a+PAGE, a+3*PAGE)`), then remap 2 contiguous physical pages at `a`. Unmapping `[a, a+PAGE)` leaves two separate placeholders; `XMemMapPhysicalPages` coalesces the physical run to `2 * PAGE` and `MapViewOfFile3(..., MEM_REPLACE_PLACEHOLDER)` fails with `ERROR_INVALID_ADDRESS` (487), returning `NULL` and leaving `[a, a+PAGE)` unmapped. 2. **`map_alias` unmaps destination before resolving source slices**: - `map_alias(to, from, len)` called `unmap(&mut physical, to, len)` before reading `[from, from + len)` from `physical.views`. Overlapping aliases destroyed their own source mappings, and a missing source page left `to` unmapped before returning `false`. ## Fix - Track exact placeholder intervals in `Physical::placeholders` (updated in `XMemVirtualAlloc`, `map`, and `unmap`). - Add `prepare_placeholder(physical, base, len)`: splits any tracked placeholder straddling `base` or `base + len`, then calls `VirtualFree(base, len, MEM_RELEASE | MEM_COALESCE_PLACEHOLDERS)` whenever multiple placeholders tile `[base, base + len)` before `MapViewOfFile3`. - In `map_alias`, snapshot all source `Slice` entries for `[from, from + len)` before calling `unmap(&mut physical, to, len)`. - Add unit tests `remapping_contiguous_run_over_fragmented_placeholders_succeeds` and `map_alias_handles_overlapping_ranges_and_missing_sources`. ## Verification - `rustfmt --edition 2024 --check crates/victory-hle/src/modules/xmem.rs` (changed hunks): clean - `cargo test -p victory-hle --locked xmem -- --test-threads=1`: 13 passed, 0 failed - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-hle --all-targets --locked -- -D warnings`: clean - `cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings`: clean *(AI-assisted; synthetic tests only, no game data.)*
Fix two physical-page mapping bugs in `crates/victory-hle/src/modules/xmem.rs`:

1. `UnmapViewOfFile2(..., MEM_PRESERVE_PLACEHOLDER)` returns each unmapped
   view as a separate placeholder of that view's size without coalescing
   neighbors, and `VirtualQueryEx` merges adjacent identical reserved regions
   so individual placeholder boundaries cannot be queried back from Windows.
   When `XMemMapPhysicalPages` later mapped a contiguous multi-page run across
   fragmented placeholders, `VirtualFree(..., MEM_PRESERVE_PLACEHOLDER)` could
   not coalesce them and `MapViewOfFile3(..., MEM_REPLACE_PLACEHOLDER)` failed
   with `ERROR_INVALID_ADDRESS` (487), returning `NULL`.
   Track exact placeholder intervals in `Physical::placeholders` (updated on
   `XMemVirtualAlloc`, `map`, and `unmap`), split any straddling boundary
   placeholders, and coalesce fragmented ranges with
   `MEM_COALESCE_PLACEHOLDERS` before `MapViewOfFile3`.
2. `map_alias(to, from, len)` called `unmap(to, len)` before reading source
   views from `[from, from + len)`, destroying source mappings on overlapping
   aliases and unmapping `to` even when a source page was unmapped. Snapshot
   all source slices first before unmapping `to`.

Add unit tests `remapping_contiguous_run_over_fragmented_placeholders_succeeds`
and `map_alias_handles_overlapping_ranges_and_missing_sources`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:40:15 +00:00
This pull request has changes conflicting with the target branch.
  • crates/victory-hle/src/modules/xmem.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-299'
git switch 'github-bot/gh-299'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-299'
git switch 'github-bot/gh-299'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-299'
git switch 'main'
git merge --squash 'github-bot/gh-299'
git switch 'main'
git merge --ff-only 'github-bot/gh-299'
git switch 'main'
git merge 'github-bot/gh-299'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.