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

Open
SuryaK5125 wants to merge 1 commit from upstream-pr/76 into main
SuryaK5125 commented 2026-11-24 00:15:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by SuryaK5125 on 2026-11-24T00:15:16Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6571620188 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/76). Originally posted by [SuryaK5125](https://github.com/SuryaK5125) on 2026-11-24T00:15:16Z. --- `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 commented 2026-11-24 06:54:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T00:24:34Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7118037743 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/76#issuecomment-7118037743). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T00:24:34Z. --- 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 #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 commented 2026-11-24 06:54:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by SkippySteve on 2026-11-24T01:29:28Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119150783 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/76#issuecomment-7119150783). Originally posted by [SkippySteve](https://github.com/SkippySteve) on 2026-11-24T01:29:28Z. --- 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** ```sh 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 2026-11-25 01:07:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:15:23Z.


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: #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 #1224) points to behaviour this PR keeps and #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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148317233 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/76#issuecomment-7148317233). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:15:23Z. --- <!-- victory-triage v1 head=ce93a8137a6b5a3cca7917468026b848329b2564 verdict=merge-candidate at=2026-11-25T11:25:25Z --> 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: #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 #1224) points to behaviour this PR keeps and #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](https://claude.ai/code)_
SuryaK5125 commented 2026-11-25 10:07:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by SuryaK5125 on 2026-11-25T09:18:31Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158155416 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/76#issuecomment-7158155416). Originally posted by [SuryaK5125](https://github.com/SuryaK5125) on 2026-11-25T09:18:31Z. --- 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.
fuzeofficlal commented 2026-11-26 02:07:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T01:20:49Z.


Code review and Windows 11 verification at head ce93a8137a6b merged cleanly onto main (63bcca43c6a0):

  • Code & rollback audit (crates/victory-hle/src/modules/xmem.rs:810-850, 908-924):
    • map splits [base, end) at both boundaries via split_at (which verifies a contiguous MEM_RESERVE | MEM_PRIVATE region from AllocationBase with no MEM_COMMIT pages before splitting) and maps segment-by-segment across existing placeholders without coalescing separate reservations.
    • On any mid-run map_view or alignment failure, every already-mapped segment in made is rolled back with UnmapViewOfFile2(..., MEM_PRESERVE_PLACEHOLDER) before title_memory_remapped/aliased, physical.views, or physical.shown are updated.
    • Also verified zero merge conflict and clean orthogonality with #2742 (#76 guards multi-placeholder mapping inside a single map() run; #2742 guards multi-run rollback in XMemMapPhysicalPages).
  • Verified (Windows 11, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy -p victory-hle --all-targets -- -D warnings: 0 warnings
    • victory-hle: 158/158 unit (including all 6 new multi-placeholder tests) + fidelityfx + no_dll_planting passed.

LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7179682076 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/76#issuecomment-7179682076). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T01:20:49Z. --- Code review and Windows 11 verification at head `ce93a8137a6b` merged cleanly onto `main` (`63bcca43c6a0`): - **Code & rollback audit (`crates/victory-hle/src/modules/xmem.rs:810-850, 908-924`)**: - `map` splits `[base, end)` at both boundaries via `split_at` (which verifies a contiguous `MEM_RESERVE | MEM_PRIVATE` region from `AllocationBase` with no `MEM_COMMIT` pages before splitting) and maps segment-by-segment across existing placeholders without coalescing separate reservations. - On any mid-run `map_view` or alignment failure, every already-mapped segment in `made` is rolled back with `UnmapViewOfFile2(..., MEM_PRESERVE_PLACEHOLDER)` before `title_memory_remapped`/`aliased`, `physical.views`, or `physical.shown` are updated. - Also verified zero merge conflict and clean orthogonality with `#2742` (`#76` guards multi-placeholder mapping inside a single `map()` run; `#2742` guards multi-run rollback in `XMemMapPhysicalPages`). - **Verified (Windows 11, Rust 1.98.1)**: - `cargo xtask policy`: `191 files ok`; `cargo clippy -p victory-hle --all-targets -- -D warnings`: 0 warnings - `victory-hle`: **158/158 unit (including all 6 new multi-placeholder tests) + `fidelityfx` + `no_dll_planting`** passed. LGTM.
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/76:upstream-pr/76'
git switch 'upstream-pr/76'

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