Skip to content

xmem: a commit-only XMemVirtualAlloc into a page-mode window commits there - #8289

Closed
BastienRodz wants to merge 2 commits into
ProjectLeonida:mainfrom
BastienRodz:xmem-commit-into-window
Closed

BastienRodz wants to merge 2 commits into
ProjectLeonida:mainfrom
BastienRodz:xmem-commit-into-window

Conversation

@BastienRodz

@BastienRodz BastienRodz commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: #7599 (comment)

Depends on: none. Textual conflicts: #4531, #6995, #7331, #7712, #7742 and #7799 (xmem.cpp; #4531, #7712 and #7742 also xmem_test.cpp), #7492 (xmem_test.cpp). #7503 changes TitleVirtualAlloc in the same file, which this does not touch, and merges cleanly.

What and why

Since #6919 a reserve-only XMemVirtualAlloc in a page mode (MEM_RESERVE | 0x20400000 or | 0x20000000) is a host placeholder recorded as a physical-page window. A later commit-only call into it (MEM_COMMIT | 0x20400000 at an address inside) fell through to a plain VirtualAlloc, which a placeholder refuses, so an ordinary reserve-then-commit returned NULL. Before #6919 it worked.

A commit-only call is still tried with a plain VirtualAlloc first, so every range that worked before is served as before. Only when that fails and the range lies in one window does commit_in_window run, under g_phys_mutex. It widens the range to the 64 KiB allocation granularity and checks every part before changing anything. It then commits each part with the call's protection: plain MEM_COMMIT first, otherwise isolate_placeholder and VirtualAlloc2(MEM_REPLACE_PLACEHOLDER | MEM_RESERVE | MEM_COMMIT).

Choices, each closest to what a plain commit on an ordinary reservation does:

  • Committing the same range again succeeds and keeps the contents (Windows accepts a repeated MEM_COMMIT). So does a range over an earlier committed part plus placeholders.
  • A range that leaves the window fails with the plain call's error, and nothing changes.
  • A range with a physical page mapped in it fails with ERROR_INVALID_ADDRESS, and nothing changes. The mapped-page table decides this, and the mapped page is not replaced.
  • A commit without a page mode returns its 4 KiB-rounded address, but the host commits the whole 64 KiB around it, since placeholders split on 64 KiB only.
  • The window record is not split (my claim said it would be). The rest of the window still takes physical pages, and a map onto the committed part fails because it is no longer a placeholder.

Not changed: a window that pages were mapped or memory committed into is several host allocations, so the title's kernel32!VirtualFree(base, 0, MEM_RELEASE) frees only the first piece. That has been true since #6919 for mapped pages, and the shim does not see the release, so the window record stays. The test releases piece by piece. #7742 is the open PR for that release.

Whether the console accepts this commit is not known here: the XMemVirtualAlloc reference is an NDA topic. The retail executable has one commit-only 64 KiB-page call site (0x20401000, docs/xmem_exe_calls.md); which reservation it commits into is not measured. #7799 reports that the title's own kernel32!VirtualAlloc never targets a window; this PR is about the XMemVirtualAlloc export, and it adds a sentence to the comment #7799 rewrites.

Tested

This is a synthetic test; it was not measured on the retail executable. No GPU involved.

  • GitHub Actions windows-latest, MSVC, ctest (runs of runtime.yml on my fork):
    • With only the new test on the base commit, xmem_test fails: 200 checks, 10 failures, all in the new test.
    • With the fix xmem passes (203 checks, counted on the first commit of this PR; the second commit does not change the test).
    • Overall ctest on the head of this PR: 262 of 263 pass, with the same single failure as an unchanged main run (d3d12_sparse_queue_family, which cannot create a Vulkan instance on the runner).
  • Wine 11.0 (winehq-stable), same CI, head of this PR: xmem passes, and the other results match the unchanged run. Wine 9.0 (ubuntu): xmem passed on the first commit; that job is cancelled by GPU test timeouts in every run, an unchanged main included, so it has no result for the head.
  • git diff --check; python -I scripts/check_no_game_data.py on both changed files.

The test reserves 1 MiB of 64 KiB pages and 4 MiB of 2 MiB pages and commits inside each. It checks the returned address and the VirtualQuery state, data written and read back, a repeated commit, a commit over committed and placeholder parts, physical pages mapping beside the committed range but not over it, the refusal over a mapped page and past the window end, a commit without a page mode, and the release.

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. (Not applicable: no translator change.)

🤖 Generated with Claude Code

BastienRodz and others added 2 commits November 23, 2026 17:19
…there

Since ItsNotRexed#6919 a reserve-only XMemVirtualAlloc in a page mode (MEM_RESERVE |
0x20400000 or 0x20000000) is a host placeholder recorded as a physical-page
window. A later commit-only call into it fell through to a plain VirtualAlloc,
which a placeholder refuses, so reserve then commit returned NULL.

A commit-only call is still tried with a plain VirtualAlloc first, so every
range that worked before is served as before. When that fails and the range
lies in one window, commit_in_window commits it part by part under
g_phys_mutex: a placeholder part is isolated and replaced with committed
memory (VirtualAlloc2, MEM_REPLACE_PLACEHOLDER), a part committed before is
committed again, as Windows accepts a repeated MEM_COMMIT. A range with a
physical page mapped in it, or one that leaves the window, fails with nothing
changed. The window record keeps the committed part, so a later commit over it
and the placeholders beside it works, and the rest of the window still takes
physical pages.

The test reserves 1 MiB of 64 KiB pages and 4 MiB of 2 MiB pages, commits
inside, and checks the address, the committed state, data, maps beside the
committed range, the edge cases and the release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lock is taken after the plain commit fails

commit_in_window checks g_mapped for the range before it looks at the host's
regions, so the refusal does not depend on how the host reports a section
view. XMemVirtualAlloc tries the plain commit before taking g_phys_mutex, so
an ordinary commit does not wait behind a map call that holds the lock.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@MartinIndra02

Copy link
Copy Markdown
Contributor

Independent build + test: PASS

AI-assisted (Claude Code). Windows 11 x64, MSVC 19.44.35220, Ninja RelWithDebInfo. GPU (not used): NVIDIA GeForce RTX 3080 driver 621.24 / 32.0.16.2124.

Head a581d6333, merged locally with current main 34702237c (clean merge).

Test PR (merged) runtime/shims/xmem.cpp reverted to main
xmem_test.exe exit 0, xmem_test: OK (203 checks) exit 1; the 8 new commit-into-window checks fail, e.g. FAIL a commit-only 64 KiB-page call into the window returns the requested address, FAIL a commit-only call without a page mode commits there too

I wanted to see what the granularity widening in commit_in_window does to the pages next to a commit, so I added a temporary probe to test_commit_into_window (window granule at base + 12*64K):

RO commit of g (4 KiB)         -> g; page g+3 state MEM_COMMIT prot PAGE_READONLY
RW commit of g+4 pages (4 KiB) -> g+0x4000; page g still PAGE_READONLY
commit at g+4 pages+16, 16 bytes -> g+0x4000 (page-aligned, like VirtualAlloc)

So the first commit into a placeholder granule commits the whole 64 KiB with that call's protection. The test comment says this ("the host commits the whole 64 KiB around it"). Pages the title never asked for become committed and accessible, which differs from a plain reserve+commit, where they would stay reserved and fault. Later commits in that granule go through the plain VirtualAlloc path and change only their own pages. That's probably acceptable for windows, but it's worth one line in the ASSUMPTION comment. No other issues found.

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

The core MEM_COMMIT path is sound, but the fallback condition is broader than commit-only.

runtime/shims/xmem.cpp:477 uses:

if (commit && !(host_type & MEM_RESERVE) && address != nullptr) {

That accepts any commit without MEM_RESERVE:

  • MEM_COMMIT | MEM_RESET, MEM_COMMIT | MEM_RESET_UNDO, and MEM_COMMIT | MEM_TOP_DOWN are invalid Windows combinations. On an ordinary reservation the plain VirtualAlloc returns the error; on a page-mode window the plain call fails on the placeholder, find_window succeeds, and commit_in_window commits with plain MEM_COMMIT (:340-344) — dropping the invalid/extra flag and returning success.
  • MEM_COMMIT | MEM_WRITE_WATCH is valid. On a normal reservation the plain call registers write watch; on a page-mode window the fallback commits plain MEM_COMMIT, so no watch is registered and a later XMemGetWriteWatch sees nothing.

Repro:

void* base = XMemVirtualAlloc(nullptr, 0x10000, MEM_RESERVE | kXmem64kPages, 0, PAGE_READWRITE);
void* r = XMemVirtualAlloc(base, 0x10000, MEM_COMMIT | MEM_RESET, 0, PAGE_READWRITE);

Windows rejects MEM_COMMIT | MEM_RESET with ERROR_INVALID_PARAMETER; the new path returns base and leaves the range committed. The tests only use plain MEM_COMMIT for window commits (runtime/test/xmem_test.cpp:508-565); MEM_WRITE_WATCH is tested only on a normal reserve-and-commit region (:183-232).

Fix: restrict the fallback to exactly the commit-only form (host_type == MEM_COMMIT, or (host_type & ~MEM_COMMIT) == 0) before find_window/commit_in_window, and otherwise restore the plain call's error. If write-watch window commits are intended, forward the flag and register the watch. Add MEM_COMMIT | MEM_RESET (fails, state unchanged) and MEM_COMMIT | MEM_WRITE_WATCH (working watch or clean failure) window tests.

Secondary: the test's piecewise release (:484-497) is labelled “the window and its committed parts release” (:585); the title's single VirtualFree(base, 0, MEM_RELEASE) frees only the first piece, which is #7742's release bug, not a new commit-path defect. Synthetic/API analysis only; no game files. AI-assisted (OpenCode).

dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
…tion succeeds instead of failing (EGEN-XMEM-COMMIT-PLACEHOLDER)

Squashed from ItsNotRexed#8599 by evilclanker9000, head c81559b (security scan PASS).

Takes the place of ItsNotRexed#8289 (orchestrator's swap), applied as the PR's own diff (gh pr diff); Haiku PASS at c81559b. Conflict with Wave 1's xmem work: this PR's commit-into-window check runs ahead of the large-page reservation branch, which keeps Wave 1's (large_pages || aligned_reservation) condition rather than this PR's older page_mode test.
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.

4 participants