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

Open
AscilCH wants to merge 1 commit from upstream-pr/299 into main
AscilCH commented 2026-11-24 23:25:27 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-24T23:25:27Z.


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

<!-- greyfinch-sync:fxceless/victory-fork:issue:6593472233 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/299). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-24T23:25:27Z. --- ## 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.)*
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/299:upstream-pr/299'
git switch 'upstream-pr/299'

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