Skip to content

xmem: the title's VirtualFree releases a whole physical-page reservation - #7742

Closed
ASTROWwwW wants to merge 2 commits into
ProjectLeonida:mainfrom
ASTROWwwW:fix/xmem-title-virtualfree
Closed

ASTROWwwW wants to merge 2 commits into
ProjectLeonida:mainfrom
ASTROWwwW:fix/xmem-title-virtualfree

Conversation

@ASTROWwwW

Copy link
Copy Markdown

Claimed in the coordination issue as: #7694 (comment) (new RT item, follow-up of #6919)

Depends on: none. Touches the same file as #7580 and #7712, in other functions.

What and why

The retail executable releases its physical-page reservations with kernel32!VirtualFree(base, 0, MEM_RELEASE). #6995 measured it, and main's own record_window comment says so. On main that call goes straight to the host. Once pages have been mapped, the reservation is one placeholder per 64 KiB page plus the views in it, so the host releases only the first piece. The rest stays reserved and mapped, and g_windows / g_mapped keep describing it.

VirtualFree is now registered next to TitleVirtualAlloc, for the same DLL names (kernel32, kernelbase, api-ms-win-core-memory-l1-1-0 to -6). A MEM_RELEASE at the base of a reservation or window that XMemVirtualAlloc made, with size 0 or its full size, does three things: it unmaps every view in it, releases every host allocation in it (placeholders and committed MEM_REPLACE_PLACEHOLDER ranges), and drops its window and reservation records. A window carved out of a reservation can be released on its own, and the rest of the reservation stays reserved. Every other call goes to the host unchanged.

This is the second half of what #6995 still added over main (#7154 (comment)). It adapts #6995's TitleVirtualFree to main's window model. The first half is #7580. #7712 cleans up the records after a release the shim did not see, and this PR makes the title's own release one the shim sees; the two don't overlap.

Tested

Windows 11 Pro 26300, MSVC 2022 BuildTools, RelWithDebInfo, no game files, no GPU involved.

  • xmem_test, with the new test_title_virtual_free (a mapped window; a placeholder reservation with a mapped carved window and a committed range; a carved window released on its own; ordinary memory):
    • on main, falling back to the host's VirtualFree: 8 failures out of 191. VirtualFree(window, 0, MEM_RELEASE) fails on the view, and VirtualFree(base, …) returns TRUE but leaves the later placeholders and views reserved.
    • with this change: 191/191.
  • Full ctest -C RelWithDebInfo: 111/111 (GPU and game-file tests skip here).
  • python -I scripts/check_no_game_data.py on both files: clean.

Not measured: a retail run. I have no game copy here, so whether the title reaches such a release during boot is still for someone with one to check.

AI-assisted: yes (Claude Code)

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (no translator change)

🤖 Generated with Claude Code

The retail executable releases its physical-page reservations with
kernel32!VirtualFree(base, 0, MEM_RELEASE) (ItsNotRexed#6995 measured it; main's own
record_window comment says so). Once pages have been mapped, such a
reservation is one placeholder per 64 KiB page plus the views in it, so the
host call releases only the first piece: the rest stays reserved and mapped,
and g_windows / g_mapped keep describing it.

VirtualFree is now registered next to TitleVirtualAlloc for the same DLL
names. A MEM_RELEASE at the base of a reservation or window XMemVirtualAlloc
made unmaps its views, releases every host allocation in it (placeholders
and committed replacements) and drops its records. Every other call goes to
the host.

xmem_test: 8 checks fail on main with the host's VirtualFree and all 191
pass here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jebaczydow

Copy link
Copy Markdown

Read the diff (not run). Two points on xmem.cpp:

  1. release_range frees every allocation it finds in [base, base + size) when owner >= base (if (owner < base || !VirtualFree(info.AllocationBase, 0, MEM_RELEASE)) ok = false;), and nothing checks that the allocation also ends inside [base, end). It trusts the recorded size, and records can go stale: g_reservations[result] = size; overwrites only the same key, and release_range erases only keys inside the range it frees, so an outer reservation record keeps its full size after a window carved out of it is released. If that hole is later re-reserved (XMemVirtualAlloc with an address, or the host) and the title then frees the outer base, release_range(outer, recorded) would also release the new allocation, which a plain host VirtualFree(outer, 0, MEM_RELEASE) would not. I have not built a reproducer. The new test covers carve -> release carved -> release outer, with no reuse of the hole in between. Trimming or erasing overlapping g_reservations entries when one is inserted (as record_window does for windows) and checking that each owner's region ends inside [base, end) before freeing would close it.

  2. xmem: trim windows on sub-range replacement and clear stale mapped-page entries #7712 rewrites the same lines in XMemVirtualAlloc (the if (result && window) block that calls record_window, which becomes if (window) record_window(...) else clear_window_range(...) there), so whichever lands second needs a rebase. xmem: trim windows on sub-range replacement and clear stale mapped-page entries #7712 does not touch g_reservations, so its clear_window_range will not clear reservation records either.

AI-assisted (Claude); code read only, nothing run.

@tupacfan04 tupacfan04 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A stale g_reservations record can free a new reservation after a release the shim did not see.

  • xmem.cpp:265 adds g_reservations (base → size), written only by XMemVirtualAlloc (:409-416).
  • record_window (:267-279) erases overlapping g_windows only; it never touches g_reservations, even though its comment describes the title releasing an overlapped region with host VirtualFree that the shim does not see.
  • TitleVirtualFree (:727-744) trusts g_reservations; release_range (:697-721) only refuses an allocation whose base is before the requested base.

Repro: reserve A through the shim; release A with host VirtualFree (unseen); reserve B at A + 64 KiB through the shim; call the shim’s VirtualFree(A, 0, MEM_RELEASE). The stale record makes release_range walk to B (owner = A+64K > base), release B’s whole placeholder and its mapped views, then erase B’s g_windows/g_reservations. B is gone.

Expected: drop the stale record and return FALSE, or validate the host allocation base before release_range; add g_reservations to record_window/#7712’s cleanup. The test (xmem_test.cpp:491-557) only frees through the shim and never covers an unseen/partial release or overlapping reuse.

Whole-reservation release and the shim-visible paths look correct; partial MEM_RELEASE is also untested. Head f4324028; synthetic only; no game files. AI-assisted (OpenCode).

@Racurs156

Copy link
Copy Markdown

Independent AI-assisted check on Windows 11 build 26200 with llvm-mingw Clang 23.1.6, CPU-only, with the test harness's standard filesystem path conversions from #7823. At f432402, xmem_test passed 191/191. Main dde82f2 with this PR's test failed at the missing VirtualFree shim, so that run stopped before exercising its release cases.

I also combined this head with #7712 4bd14d7 on dde82f2. The three-way merge conflicts where XMemVirtualAlloc records a reservation and in two test insertion points. Keeping this PR's reservation record and #7712's branch that clears stale windows for plain placeholders, plus both test functions, gave 217/217 checks. No GPU or game files were used.

Review on ItsNotRexed#7742 (jebaczydow): release_range freed every allocation it found
in the recorded range, so once the title had released a window carved out
of a reservation and something else took that hole, releasing the outer
reservation freed the newcomer too.

A reservation in such a hole is now recorded (XMemVirtualAlloc's own
reservations already were; the title's VirtualAlloc and XMemVirtualAlloc's
plain paths now are when they land inside one), and release_range leaves
every recorded reservation nested in the range alone, with its windows and
mapped pages. It also skips an allocation that does not end inside the
range, and drops only its own reservation record.

xmem_test: the hole-reuse case crashes on the previous head (the outer
release freed the committed hole) and passes here; 203 checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASTROWwwW

Copy link
Copy Markdown
Author

@jebaczydow, point 1 was real. 51736cd2f adds your case to the test: carve a window, release it, take the hole again with the title's VirtualAlloc (and, in a second pass, with XMemVirtualAlloc), then release the outer reservation. On the previous head the test crashes, because the outer release freed the committed hole and the next read faults.

The fix:

  • A reservation that lands inside a recorded one is recorded too: the title's VirtualAlloc and XMemVirtualAlloc's plain and 2 MiB paths now do it (its placeholder paths already did).
  • release_range leaves every recorded reservation nested in its range alone, with its windows and mapped pages. It skips an allocation that does not end inside the range, and erases only its own reservation record.

xmem_test 203/203, full ctest 111/111 (Windows 11, MSVC 2022).

Point 2: agreed. Racurs156's three-way merge with #7712 (above) gives 217/217, and I'll rebase on whichever lands first.

Also measured since: the retail executable imports VirtualFree from api-ms-win-core-memory-l1-1-0.dll (one of the names this PR registers), next to VirtualAlloc and VirtualQueryEx.

@DeSunShine

Copy link
Copy Markdown
Contributor

Tested 51736cd2f merged into main dde82f27e on NVIDIA GeForce RTX 4090 (driver 621.24), Windows 11 Pro 26200, MSVC (VS Build Tools 18) + Ninja, Vulkan SDK 1.4.363: pass.

  • ctest -R "xmem|tiny_game" --output-on-failure: 100% passed out of 11 (xmem and the run_tiny_game* tests).

AI-assisted (Claude Code).

@nullmarsh

Copy link
Copy Markdown
Contributor

Tested 51736cd2 on Windows 11 26200, MSVC 14.44 (xmem_test built from xmem_test.cpp + xmem.cpp + registry.cpp): pass, and tupacfan04's case from the f4324028 review is fixed.

I added their scenario to a local copy of test_title_virtual_free (not committed): reserve A through the shim, release it with the host's VirtualFree behind the shim's back, reserve B at A + 64 KiB through the shim, then call the title's VirtualFree(A, 0, MEM_RELEASE). I ran it for a placeholder reservation and for a plain 64 KiB-page one:

Head placeholder 64 KiB pages xmem_test
f4324028 B released B released FAILED (195 checks, 2 failures: the probe)
51736cd2 B alive B alive OK (207 checks: 203 + the probe's 4)

One remaining difference: the title's VirtualFree(A) still returns TRUE for the stale A on both heads, where the host's VirtualFree on an already-released base would return FALSE. B is left alone either way.

AI-assisted (Claude Code), no game files.

@NolanCotter

Copy link
Copy Markdown
Contributor

Reviewed and tested on its head (clang-cl 20 + xwin cross-build, Wine): xmem_test OK, 203 checks.

Correctness reads clean:

  • release_range's nesting logic is right: reservations recorded via note_nested_allocation (made later, in a hole the title released) are excluded from unmap/free/erase, so an outer MEM_RELEASE leaves a reallocated hole alone — and the test proves it for both TitleVirtualAlloc and XMemVirtualAlloc paths.
  • unmap_page/allocation_end are documented for the held g_phys_mutex — no lock re-entry issue.
  • Conservative on spanning allocations: one that starts inside the range but ends past it is left in place and reported (ok = false), can't be half-released. Partial failures still erase the reservation record — right call, since a partial release can't be redone.
  • size == 0 || size == recorded covers both the strict host MEM_RELEASE shape and a title passing the size anyway; everything else falls through to the host.

The hole-reallocation edge case (the one most likely to regress silently) is the one this nails down best.

AI-assisted (Devin); no game files used.

@Kerwinss

Copy link
Copy Markdown

Tested 51736cd on Windows 11 Pro 10.0.26300, MSVC 14.51 (VS Build Tools 2026), Windows SDK 10.0.26100, NVIDIA GeForce RTX 4060 / 621.24, Khronos Vulkan headers via -DLEONIDA_VULKAN_HEADERS_FETCH=ON: pass, no regression.

Merged onto main 867d922 (clean), full runtime build, ctest -C RelWithDebInfo -j4: 237/239, same as main alone on this GPU; the two failures are main's own d3d12_depth_resolve (#7985) and d3d12_vulkan_device:6574; xmem passes.

Together with #7580, which edits the same file: main + #7742 + #7580 merges without conflicts (xmem.cpp and xmem_test.cpp auto-merge), builds, and gives the same 237/239 with xmem passing. So the two can land in either order.

Code read, one note: release_range scans all of g_mapped on every VirtualFree(MEM_RELEASE) of a recorded reservation. With the title's ~100k mapped pages (the RT-671 numbers), each release costs a full pass over the map, plus a VirtualQuery and VirtualFree per 64 KiB placeholder in the range. That probably doesn't matter unless releases are frequent, but it's worth timing next to RT-671's watchdog.

Picked from the "No game copy" list in #7154. AI-assisted (Claude Code). No game files.

Demon12377 added a commit to Demon12377/Leonida that referenced this pull request Nov 23, 2026
Dependency snapshot: ItsNotRexed#7742 at 51736cd. The subsequent commits contain the new regression cases and failure-handling fix.
Demon12377 added a commit to Demon12377/Leonida that referenced this pull request Nov 23, 2026
The 290-check suite completes with 24 failures on the ItsNotRexed#7742 implementation. Failure callbacks are compiled only into xmem_test. Claim: ItsNotRexed#8344 (comment)

AI-assisted: yes (Codex).
@Demon12377

Copy link
Copy Markdown

Follow-up #8825 adds controlled failure-and-retry cases on top of your 51736cd2 implementation. If an unmap fails while other pieces release, the view remains but its original retry range is lost. The same 290 CPU checks give 24 failures before the ownership/error-handling fix and 0 afterward (Windows 10 x64, MSVC 14.44, SDK 10.0.26100.0).

The follow-up preserves surviving spans and the first native error, protects independent nested owners, and handles address reuse without freeing the new owner. Your implementation is credited and listed as the dependency. Synthetic allocations and injected API failures only; no GPU/game data or retail-run claim. AI-assisted (Codex).

dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
…a whole physical-page reservation

Squashed from ItsNotRexed#7742 by ASTROWwwW, head 51736cd (security scan PASS).

Conflicts in xmem.cpp and xmem_test.cpp with ItsNotRexed#7584, ItsNotRexed#4531 and ItsNotRexed#7712. Adapted to ItsNotRexed#7584's run-of-views model: release_range unmaps every recorded view (g_views) whose base is in the range and not inside a nested reservation, via unmap_range, instead of the per-page g_mapped table. XMemVirtualAlloc's reservation branch records g_reservations and keeps ItsNotRexed#7712's record_window/clear_window_range split; the aligned and plain paths keep ItsNotRexed#4531's title flags and ItsNotRexed#7712's clearing, then note nested allocations. TitleVirtualAlloc is ItsNotRexed#4531's version (page bits through xmem_virtual_alloc) with note_nested_allocation on the plain host path. Both new test functions kept (a merged closing brace restored). xmem_test under Wine 11.22: 656 checks OK.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants