Repository navigation
xmem: a commit-only XMemVirtualAlloc into a page-mode window commits there - #8289
BastienRodz wants to merge 2 commits into
Conversation
…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>
|
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
I wanted to see what the granularity widening in 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 |
tupacfan04
left a comment
There was a problem hiding this comment.
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, andMEM_COMMIT | MEM_TOP_DOWNare invalid Windows combinations. On an ordinary reservation the plainVirtualAllocreturns the error; on a page-mode window the plain call fails on the placeholder,find_windowsucceeds, andcommit_in_windowcommits with plainMEM_COMMIT(:340-344) — dropping the invalid/extra flag and returning success.MEM_COMMIT | MEM_WRITE_WATCHis valid. On a normal reservation the plain call registers write watch; on a page-mode window the fallback commits plainMEM_COMMIT, so no watch is registered and a laterXMemGetWriteWatchsees 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).
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 alsoxmem_test.cpp), #7492 (xmem_test.cpp). #7503 changesTitleVirtualAllocin the same file, which this does not touch, and merges cleanly.What and why
Since #6919 a reserve-only
XMemVirtualAllocin a page mode (MEM_RESERVE | 0x20400000or| 0x20000000) is a host placeholder recorded as a physical-page window. A later commit-only call into it (MEM_COMMIT | 0x20400000at an address inside) fell through to a plainVirtualAlloc, 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
VirtualAllocfirst, so every range that worked before is served as before. Only when that fails and the range lies in one window doescommit_in_windowrun, underg_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: plainMEM_COMMITfirst, otherwiseisolate_placeholderandVirtualAlloc2(MEM_REPLACE_PLACEHOLDER | MEM_RESERVE | MEM_COMMIT).Choices, each closest to what a plain commit on an ordinary reservation does:
MEM_COMMIT). So does a range over an earlier committed part plus placeholders.ERROR_INVALID_ADDRESS, and nothing changes. The mapped-page table decides this, and the mapped page is not replaced.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
XMemVirtualAllocreference 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 ownkernel32!VirtualAllocnever targets a window; this PR is about theXMemVirtualAllocexport, 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.
windows-latest, MSVC,ctest(runs ofruntime.ymlon my fork):xmem_testfails: 200 checks, 10 failures, all in the new test.xmempasses (203 checks, counted on the first commit of this PR; the second commit does not change the test).cteston the head of this PR: 262 of 263 pass, with the same single failure as an unchangedmainrun (d3d12_sparse_queue_family, which cannot create a Vulkan instance on the runner).xmempasses, and the other results match the unchanged run. Wine 9.0 (ubuntu):xmempassed on the first commit; that job is cancelled by GPU test timeouts in every run, an unchangedmainincluded, so it has no result for the head.git diff --check;python -I scripts/check_no_game_data.pyon 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
VirtualQuerystate, 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
docs/design.md, with how it was confirmed. (Not applicable: no translator change.)