HLE: Preserve xmem bookkeeping when unmapping fails #3526

Open
github-bot wants to merge 2 commits from github-bot/gh-3526 into main AGit
Owner

@⁠GaussianGuaicai on GitHub · 2026-11-27 14:20 UTC · original
GaussianGuaicai:fix/xmem-unmap-bookkeeping → main · +85 −23 · 1 files · 2 commits · open

Summary

Keep the tracked view, alias records, and GPU mapping state intact when UnmapViewOfFile2 fails. Only update those records after the OS releases the view, and return failure from XMemMapPhysicalPages or map_alias rather than proceeding. Also return failure if a view outside the requested range cannot be restored. Earlier successful changes in a multi-view request are not rolled back.

This addresses the OS unmap failure path, separate from the input validation and mapping rollback in #2742. It is not a verified fix for any VM bugcheck.

Tests

  • On Windows, cargo test -p victory-hle --lib --profile diag --locked --offline modules::xmem:: passed: 15 passed, 0 failed, 0 ignored.
  • The new regression injects an unmap failure with real mapped views, verifies that the view and alias remain tracked, then retries through the OS and checks that the mapping is released.
  • No game run or game data required.

AI-assisted with GitHub Copilot.

**@⁠GaussianGuaicai** on GitHub · 2026-11-27 14:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/3526) `GaussianGuaicai:fix/xmem-unmap-bookkeeping` → `main` · +85 −23 · 1 files · 2 commits · open ## Summary Keep the tracked view, alias records, and GPU mapping state intact when `UnmapViewOfFile2` fails. Only update those records after the OS releases the view, and return failure from `XMemMapPhysicalPages` or `map_alias` rather than proceeding. Also return failure if a view outside the requested range cannot be restored. Earlier successful changes in a multi-view request are not rolled back. This addresses the OS unmap failure path, separate from the input validation and mapping rollback in #2742. It is not a verified fix for any VM bugcheck. ## Tests - On Windows, `cargo test -p victory-hle --lib --profile diag --locked --offline modules::xmem::` passed: 15 passed, 0 failed, 0 ignored. - The new regression injects an unmap failure with real mapped views, verifies that the view and alias remain tracked, then retries through the OS and checks that the mapping is released. - No game run or game data required. AI-assisted with GitHub Copilot.
Keep views and aliases tracked until the OS releases their mapping, and propagate failures to the callers. Cover a failed unmap followed by a successful retry with real aliased views.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 14:25:36 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-27 14:47 UTC · original

Independent CPU-only verification (no physical GPU, no game, no game data). AI use: Claude Code (Claude Sonnet 5.5).

Environment: AMD Ryzen 7 5700X3D 8C/16T, Windows 11 25H2 build 10.0.26200, rustc 1.98.1 (48a229cea 2026-09-01), -j 4, one shared target dir.
SHAs: head 240291893ce9c07777dc8d7db7f5478c6471068f (unchanged when I started and when I posted), merge base 63bcca43c6a00a758510398fa43096ce7e4f7df0 (current main tip).

victory-hle --lib --locked base head
modules::xmem:: 14 passed, 0 failed 15 passed, 0 failed (new failed_unmap_preserves_view_and_alias_bookkeeping ok)
full lib 152 passed, 0 failed 153 passed, 0 failed

No regressions. The PR has no other comments, so nothing to duplicate.

Diff read (no blocking issue found):

  • Bookkeeping order is right: unmap_with calls the OS unmap first (xmem.rs:995) and only then removes the view from views/shown and calls title_memory_remapped, so a failed UnmapViewOfFile2 leaves the records untouched. XMemMapPhysicalPages (:709) and map_alias (:778) now return failure instead of proceeding.
  • Behaviour note, success path: title_memory_remapped(at, size) (:1009) now runs after the OS unmap instead of before it. Looks harmless, but it is a small ordering change on the default path.
  • Restore-failure path (:1010-1017): if the re-map of a part outside the requested range fails, the view is already unmapped and already removed from the records, so records and OS state still agree (that range is left as a placeholder). The right-hand restore is still attempted when the left one fails. But the loop then stops, so later views in the range stay mapped and the call returns failure with the range part-unmapped. The doc comment says so ("does not undo earlier changes"). Callers get null and may treat the old mapping as intact.
  • Test gap: the regression drives only the injected-failure branch via unmap_with. The real UnmapViewOfFile2 wrapper's Err path and the "outside the requested range restore fails" return are not exercised.

Verified on CPU only; not a statement about VM bugchecks.

**@⁠oni-swr** on GitHub · 2026-11-27 14:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/3526#issuecomment-7221238630) Independent CPU-only verification (no physical GPU, no game, no game data). AI use: Claude Code (Claude Sonnet 5.5). **Environment:** AMD Ryzen 7 5700X3D 8C/16T, Windows 11 25H2 build 10.0.26200, rustc 1.98.1 (48a229cea 2026-09-01), `-j 4`, one shared target dir. **SHAs:** head `240291893ce9c07777dc8d7db7f5478c6471068f` (unchanged when I started and when I posted), merge base `63bcca43c6a00a758510398fa43096ce7e4f7df0` (current main tip). | `victory-hle --lib --locked` | base | head | |---|---|---| | `modules::xmem::` | 14 passed, 0 failed | 15 passed, 0 failed (new `failed_unmap_preserves_view_and_alias_bookkeeping` ok) | | full lib | 152 passed, 0 failed | 153 passed, 0 failed | No regressions. The PR has no other comments, so nothing to duplicate. **Diff read (no blocking issue found):** - Bookkeeping order is right: `unmap_with` calls the OS unmap first (`xmem.rs:995`) and only then removes the view from `views`/`shown` and calls `title_memory_remapped`, so a failed `UnmapViewOfFile2` leaves the records untouched. `XMemMapPhysicalPages` (`:709`) and `map_alias` (`:778`) now return failure instead of proceeding. - Behaviour note, success path: `title_memory_remapped(at, size)` (`:1009`) now runs after the OS unmap instead of before it. Looks harmless, but it is a small ordering change on the default path. - Restore-failure path (`:1010-1017`): if the re-map of a part outside the requested range fails, the view is already unmapped and already removed from the records, so records and OS state still agree (that range is left as a placeholder). The right-hand restore is still attempted when the left one fails. But the loop then stops, so later views in the range stay mapped and the call returns failure with the range part-unmapped. The doc comment says so ("does not undo earlier changes"). Callers get null and may treat the old mapping as intact. - Test gap: the regression drives only the injected-failure branch via `unmap_with`. The real `UnmapViewOfFile2` wrapper's `Err` path and the "outside the requested range restore fails" return are not exercised. Verified on CPU only; not a statement about VM bugchecks.
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-27 15:13 UTC · original

Independent CPU-only verification on Windows / Rust 1.100.0 / MSVC.

  • PR head: 240291893ce9c07777dc8d7db7f5478c6471068f, merge base 63bcca43c6a00a758510398fa43096ce7e4f7df0.
  • Command: cargo test --locked -p victory-hle --lib -- modules::xmem::tests::failed_unmap_preserves_view_and_alias_bookkeeping --exact --test-threads=1 --quiet
  • Result: test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 152 filtered out, exit 0.
  • Negative control: git grep -F -c failed_unmap_preserves_view_and_alias_bookkeeping 63bcca43 -- . → exit 1 / 0 hits (test genuinely new); present on PR head.
  • git diff --check 63bcca43 HEAD → exit 0.
  • Caveat: only the named synthetic bookkeeping test was replayed; full crate suite and real unmap failure paths were not exercised.
  • GPU/driver: N/A (CPU-only test). No game data, assets, extracted shaders or DRM involved.
  • AI-assisted: Hy4 flash.
**@⁠cloph-dsp** on GitHub · 2026-11-27 15:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/3526#issuecomment-7221782258) Independent CPU-only verification on Windows / Rust 1.100.0 / MSVC. - PR head: `240291893ce9c07777dc8d7db7f5478c6471068f`, merge base `63bcca43c6a00a758510398fa43096ce7e4f7df0`. - Command: `cargo test --locked -p victory-hle --lib -- modules::xmem::tests::failed_unmap_preserves_view_and_alias_bookkeeping --exact --test-threads=1 --quiet` - Result: `test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 152 filtered out`, exit 0. - Negative control: `git grep -F -c failed_unmap_preserves_view_and_alias_bookkeeping 63bcca43 -- .` → exit 1 / 0 hits (test genuinely new); present on PR head. - `git diff --check 63bcca43 HEAD` → exit 0. - Caveat: only the named synthetic bookkeeping test was replayed; full crate suite and real unmap failure paths were not exercised. - GPU/driver: N/A (CPU-only test). No game data, assets, extracted shaders or DRM involved. - AI-assisted: Hy4 flash.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3526'
git switch 'github-bot/gh-3526'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3526'
git switch 'github-bot/gh-3526'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3526'
git switch 'main'
git merge --squash 'github-bot/gh-3526'
git switch 'main'
git merge --ff-only 'github-bot/gh-3526'
git switch 'main'
git merge 'github-bot/gh-3526'
git push origin 'main'
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/Marvisak--VIctory!3526
No description provided.