Skip to content

xmem: map a run of physical pages over several placeholders - #76

Open
SuryaK5125 wants to merge 1 commit into
Marvisak:mainfrom
SuryaK5125:fix/xmem-placeholder-remap
Open

SuryaK5125 wants to merge 1 commit into
Marvisak:mainfrom
SuryaK5125:fix/xmem-placeholder-remap

Conversation

@SuryaK5125

@SuryaK5125 SuryaK5125 commented Nov 24, 2026 •

Copy link
Copy Markdown

XMemMapPhysicalPages fails with ERROR_INVALID_ADDRESS when mapping a run of physical pages across multiple placeholders. This can happen after a partial unmap or scatter mapping, since unmapping doesn't join the placeholders back together.

Instead of mapping the entire run as one view, this change maps each placeholder separately and rolls back if a mapping fails. Existing placeholder boundaries are preserved, since coalescing can also join unrelated reservations.

The split operation now checks that the affected memory is reserved before proceeding to avoid freeing committed pages.

Rebased onto current main (940d32f) as ce93a81. The upstream alias tracking in map() is kept: the GPU side is told once that the whole run was remapped, then each page of each view is linked with its other views, as for a single view.

Testing (ce93a81):

  • Six regression tests: remapping after a partial unmap, a run across two placeholders, a title-split piece kept, a run over two adjacent reservations that stay separate allocations, alias links for a run over several placeholders, and rollback of a failed run. The first five fail on unmodified main (XMemMapPhysicalPages returns NULL).
  • cargo test -p victory-hle on Windows 11: 149 passed, 0 failed.
  • Clippy reports nothing for xmem.rs. rustfmt is clean for the changed lines.
  • Not tested in GTA VI, and no game files used.

Earlier testing (dea90cc, the previous revision on 9aef3d9):

  • Independently verified under Wine 11.0 on Linux: all 13 XMem tests pass, including the three remapping regressions that fail on unmodified main. See the testing report in the PR discussion. No title execution was tested. Thanks to @SkippySteve for the independent testing.
  • ce93a81 has not been tested under Wine.

Also checked compatibility with #53 and #61. All three changes together passed the HLE tests at dea90cc.

@DaviTostes

Copy link
Copy Markdown

Tested b44fd4b stacked on #39 on Arch Linux x86_64, rustc 1.98.1: pass.

  • cargo test --workspace: 156 passed, 0 failed, 3 ignored; merge with Build: make workspace tests run on Linux #39 clean.
  • The PR adds 4 #[test]s for the placeholder mapping, included in that run.
  • The mapping itself is a Windows VirtualAlloc2/placeholder path, so the tests that exercise it are the portable ones only.
  • No game data used.

AI-assisted (DeepSeek V4.1 Flash coding agent).

@SkippySteve

Copy link
Copy Markdown

I ran the victory-hle tests for this PR under Wine on Linux. No game files were used and the title was not run.

Environment

  • Arch-based Linux, kernel 7.2.15
  • UMU-Proton-11.0-2e (wine-11.0), using Proton's files/bin/wine directly with a fresh, dedicated WINEPREFIX and WINEDEBUG=-all
  • Cross-built with cargo xwin test -p victory-hle --target x86_64-pc-windows-msvc --no-run (Rust 1.98.1 from rust-toolchain.toml)
  • Proton's lib/vkd3d/x86_64-windows/libvkd3d-*.dll copied next to the test exe, because the test binary imports d3d12/dxgi and plain wine (unlike proton run/umu) doesn't put them in the prefix

Commands

cargo xwin test -p victory-hle --target x86_64-pc-windows-msvc --no-run
WINEPREFIX=... "$PROTON/files/bin/wine" victory_hle-<hash>.exe --test-threads=1

Results (victory_hle unit tests)

Build Passed Failed Notes
PR head dea90cc (on 9aef3d9) 89 1 all 13 xmem tests pass
PR merged into current main (d2a4bae) 89 1 all 13 xmem tests pass
main d2a4bae, unchanged 85 1
main + only this PR's 4 new tests (code unchanged) 86 4 3 new xmem tests fail
  • On main, a_run_maps_again_after_a_partial_unmap, a_run_maps_across_two_placeholders and a_run_keeps_the_placeholders_it_spans all fail the same way under Wine: XMemMapPhysicalPages returns null (left: 0x0, right: 0x1012b0000, etc.). This PR's code makes all three pass. So the multi-placeholder bug happens on Wine too, not just on Windows, and the fix works there. a_failed_run_leaves_no_view passes on both, which is expected because main also returns null in that case.
  • The single failure in every row is modules::net::tests::only_loopback_is_reachable (ws2_32.dll lacks WSAConnectByList). Wine's ws2_32 doesn't export WSAConnectByList, and the test fails the same way on main, so it has nothing to do with this PR.
  • I ran the xmem filter on the PR build 5× serially and 1× with parallel test threads: 13/13 passed every time.
  • The integration test no_dll_planting passes (1/1).

This only covers synthetic tests. I didn't check a title load under Proton.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. Recommending a look, not approving. The head is the same as at the earlier note, and it still merges cleanly onto main.

  • map in crates/victory-hle/src/modules/xmem.rs maps one view for each placeholder the run covers. If any view fails, it unmaps the views already made. split_at splits only reserved memory. The GPU alias bookkeeping runs once every view has succeeded. Placeholders are never joined, so separate reservations stay separate.
  • Overlap the earlier note missed: fix(hle): coalesce split placeholders when mapping physical pages #1224 fixes the same remap failure the other way, by joining split placeholders (coalesce_placeholders). It is open with its own needs-work note. Your comparison in the thread (6/6 regressions here, 3/6 on fix(hle): coalesce split placeholders when mapping physical pages #1224) points to behaviour this PR keeps and fix(hle): coalesce split placeholders when mapping physical pages #1224 does not: a run that starts inside a placeholder, placeholders the title split itself, and adjacent reservations kept separate. The cost you measured is slower map/unmap when a run spans many placeholders (about 100 µs against 31–54 µs at 16 placeholders, debug build). Only one of the two can land, so the maintainer should pick.

CI: check passing.

On your reply: thanks for the side-by-side comparison. It makes the choice clearer and leaves this verdict unchanged.


Generated by Claude Code

MapViewOfFile3 with MEM_REPLACE_PLACEHOLDER replaces exactly one
placeholder: the range must be the placeholder's own. map() split the
placeholder around the range and mapped one view over it, so it worked
only when the range lay in a single placeholder. Every mapping leaves its
reservation split at its ends and unmapping does not join the pieces, so
after a partial unmap, a scattered mapping or a shorter earlier run, a
run over those pages failed with ERROR_INVALID_ADDRESS and
XMemMapPhysicalPages returned NULL, its range already unmapped.

map() now splits only at the run's two ends and gives each placeholder in
the range a view of its part. Placeholders are never coalesced: the host
also coalesces placeholders of separate reservations, and pieces a title
split itself would stay joined after the unmap. If a view fails, those
made before it become placeholders again. The split call also frees
memory that replaced a placeholder back to one, committed pages
included, so split_at() splits only where everything below the split
point is reserved.

The GPU side is told once that the whole run is remapped, then each page
of each view is linked with the other views showing it, as for a single
view.

New tests: a run remapped after a partial unmap, a run from one
placeholder into the next, a title-split piece kept through a map and an
unmap, a run over two adjacent reservations that leaves each its own
allocation, a run over several placeholders that links and then forgets
its aliases, and a failed run that is rolled back without touching
committed memory next to it. All but the last fail on main
(XMemMapPhysicalPages returns NULL). cargo test -p victory-hle passes on
Windows 11.
@SuryaK5125

Copy link
Copy Markdown
Author

Rebased onto 940d32f (ce93a81). All 149 HLE tests and CI pass.

Since #1224 addresses the same issue, I tested both approaches against the same upstream base using six regression tests:

Implementation Passed
Unmodified main 1/6
#76 6/6
#1224 (0c81ed4) 3/6

The three failures on #1224 involve a run starting inside a placeholder, preservation of title-created placeholder boundaries, and preservation of adjacent independent reservations. The first appears fixable independently of the coalescing approach.

There is a performance trade-off: in a synthetic Windows debug-build benchmark with 16 pages across 16 placeholders, #76 took approximately 100/77 µs for map/unmap, versus 31–54/24–29 µs for #1224. At four placeholders or fewer, the difference was close to measurement noise. Wine performance was not measured.

Coalescing only internally created splits might eventually combine the advantages of both approaches, but would require additional reservation tracking.

All testing was synthetic, without game files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants