Skip to content

xmem: serve the seven missing imports (physical pages, default allocators, XMemIsToolsMemAvailable); XMemVirtualAlloc takes 2 MiB pages and placeholders - #6919

Merged
shayderrr merged 1 commit into
ProjectLeonida:mainfrom
NiiRoZz:rt-xmem-physical-pages
Nov 23, 2026

Conversation

@NiiRoZz

@NiiRoZz NiiRoZz commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

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

Depends on: none. Rebased onto #6908 (merged): its attribute-word recording and PAGE_GRAPHICS_* handling are kept as they are. Overlaps in part with #4531 (2 MiB pages), #5253 (XMemAllocDefault/XMemFreeDefault) and #5381 (the physical-page mechanism). Whichever lands first, I'll rebase this one onto it.

What and why

xmem.dll had seven imports with no shim, which a dry run listed as unimplemented. XMemVirtualAlloc still refused some of the calls a title actually makes: five ask for 2 MiB pages, and several reserve placeholder regions or carve windows out of them. This PR serves all seven imports and adds those XMemVirtualAlloc forms. The argument shapes were measured locally at a title's call sites. The comments in runtime/shims/xmem.cpp give each shape, as counts and argument roles only.

New imports

  • XMemAllocDefault / XMemFreeDefault take XMemAlloc's and XMemFree's arguments and share their heap, so a block from either allocator can be freed by either.
  • XMemAllocatePhysicalPages(attributes, UINT64* count, UINT64* pages) → BOOL, with an in/out count. Each call is backed by one pagefile-backed section of 64 KiB pages.
    • A page number is a 4 KiB frame number: a non-zero multiple of 16 below 2^25, because a caller packs them that way. 0 means "unmap".
    • Freed numbers are reused.
  • XMemMapPhysicalPages(address, UINT64 count, pages) → the address. A null array unmaps.
  • XMemMapPhysicalPagesScatter(addresses, UINT32 count, pages) → BOOL. A 0 entry, or a null array, unmaps.
  • XMemFreePhysicalPages(UINT64 count, pages) → BOOL.
  • Every mapped page is its own MapViewOfFile3 view in a 64 KiB placeholder. So a page keeps its contents across unmap and remap, can be mapped at several addresses, and can be remapped on its own.
  • XMemIsToolsMemAvailable() → FALSE: a PC has no devkit tools memory. Callers only ask on an out-of-memory path.

XMemVirtualAlloc

  • 2 MiB guest pages are committed 2 MiB-aligned through VirtualAlloc2, without the host's large-page privilege.
  • A reserve-only 64 KiB-page region becomes a placeholder window, and its protection (after xmem: record XMemVirtualAlloc's attribute word and drop the PAGE_GRAPHICS_* protection bits (retail exe GPU pools) #6908's host_protect) is recorded. Mapped pages take the window's protection, as AWE does. A window reserved PAGE_NOACCESS maps its pages read-write, because they are GPU memory the D3D12 layer reads on the CPU.
  • MEM_RESERVE_PLACEHOLDER and MEM_REPLACE_PLACEHOLDER are passed through. A reserve-only replacement stays a window.

The placeholder APIs are looked up in kernelbase.dll at run time, so nothing new is linked. A host without them gets ERROR_NOT_SUPPORTED.

ASSUMPTION (in the code and in docs/runtime_imports.md): nothing commits memory into a window through kernel32!VirtualAlloc, which a placeholder would refuse.

The startup trace records the default allocators in xmem_attributes and adds an xmem_physical_pages event. runtime/README.md (shim list, trace events) and docs/runtime_imports.md describe the new behaviour.

Tested

Windows 11, MSVC 19.44, scripts/build_runtime.ps1 (CPU build, no Vulkan headers):

  • xmem_test: 173 checks, OK (including xmem: record XMemVirtualAlloc's attribute word and drop the PAGE_GRAPHICS_* protection bits (retail exe GPU pools) #6908's). The old check that expected 2 MiB pages to be refused now checks that they succeed. New checks cover:
    • the default allocators, freed by either free function
    • physical pages: allocate, map, unmap, remap in reverse order with contents kept, aliasing across two windows, a 0 entry, scatter maps and unmaps, invalid addresses and page numbers, double free, and page numbers reused after a free
    • placeholder reservation and replacement, both reserve-only and committing
    • 2 MiB alignment and rounding
  • ctest: 107/107 passed. The skipped tests are the ones that need game files, the same as on main.
  • gta6rt --dry-run against a local copy: unimplemented imports went from 30 to 23, with none left in xmem.dll.

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

@jlric

jlric commented Nov 23, 2026 •

Copy link
Copy Markdown

Tested c9c90632 on CachyOS x86-64, Wine 11.22, clang-cl/xwin. xmem_test: 156/156 checks passed, exit 0. Synthetic, CPU-only. AI-assisted.

@theswiss24

Copy link
Copy Markdown

Tested c9c906327 against the retail executable (gta6rt --run, Windows 11, RTX 4070, LEONIDA_D3D12=vulkan, five runs). The game's own code was only looked at locally; nothing of it is in this comment.

Result: every XMemVirtualAlloc the title makes at startup now succeeds. That's five requests in the trace:

  • the CPU and GPU 64 KiB reservations (attributes 0x21 and 3),
  • the 2 MiB commit (0x20003000, attributes 0x283, protect 0x2404),
  • a 20 MiB 64 KiB commit (0x20403000, attributes 0x181).

The title no longer stops at any of its allocation checks.

Where it stops next: the first operator new after these allocations. In 3 runs it reads 0xffffffffffffffff; in 2 it exits with 127 at the same point and no exception is recorded. That isn't an xmem problem: the title's allocator fetches the current thread's heap from its static TLS through ThreadLocalStoragePointer[0], which on main is gta6rt.exe's own block. That's what #4313 (RT-259) fixes, so the next useful test is this PR together with #4313. #4313 conflicts with main right now.

Tested and written by Claude (Claude Code).

@IAmNotSiob IAmNotSiob left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

tested on win10 VM (RTX 6000 passthrough, MSVC, RelWithDebInfo) at this pr's head: builds clean, xmem test passes. full ctest 255/260; the 5 failures are gameinput_host, root_signature_oracle, d3d12_indirect_dispatch_count, d3d12_close_parity, d3d12_indirect_rt, none of which touch xmem (that VM has no controller / its own d3d12 quirks). didn't rerun them on main, so treat those as "not caused by this", not "known".

code read, looks careful:

  • placeholder windows + section-backed 64 KiB pages under one mutex is the right model for map/unmap, and page numbers as non-zero multiples of 16 below 2^25 match what #6824 describes
  • freed slots reused, so a long run can't exhaust page numbers
  • VirtualAlloc2/MapViewOfFile3 resolved at run time with ERROR_NOT_SUPPORTED otherwise, nice, no onecore.lib
  • dropping PAGE_WRITECOMBINE too is a good call given the D3D12 layer reads these pages on the CPU

small things, non-blocking:

  • the ASSUMPTION that nobody kernel32!VirtualAlloc-commits inside a window: if the title ever does, the placeholder refuses it with a fairly opaque error. a one-line trace there would make that obvious from a boot log
  • XMemIsToolsMemAvailable logging once to stderr is fine, but the title hits it right before a deliberate int3 on OOM, so maybe mention that in the message (so people reading a boot log know the break after it is expected)

also: this covers what #6709, #6814, #6878, #6908 and #6733 do for XMemVirtualAlloc (attribute words, protection bits, error/trace), and it's the only one that unblocks the #6824 stop. all six touch xmem.cpp, so i'll point those at this one.

AI-assisted (Claude Code).

TL;DR: approve: builds and xmem test passes on Windows, design is sound, two optional logging nits. it supersedes five other xmem prs.

@Vencite

Vencite commented Nov 23, 2026

Copy link
Copy Markdown

Independently tested c9c906327 on native Windows with Visual Studio 2022, MSVC 19.44.35230, Windows SDK 10.0.26100.0, x64 RelWithDebInfo. ctest -R "^xmem$" -V passed all 156 checks, including 2 MiB alignment, placeholder replacement, scatter mapping, and content preservation across remaps. This was a synthetic, CPU-only test. AI-assisted.

@ICUMADETHIS

Copy link
Copy Markdown
Contributor

On c9c90632 merged onto 1cc6e3c1, I reserved two 64 KiB pages and replaced only the first. VirtualQuery confirmed the second page was still reserved, yet XMemMapPhysicalPages returned ERROR_INVALID_ADDRESS (487) for it. record_window() removes the entire original range from the shim’s bookkeeping. Please preserve the unaffected range when splitting a tracked reservation and add a test for mapping both pages afterward.

@Vyral1978

Copy link
Copy Markdown
Contributor

Tested c9c90632 with my own 1.0.2.1 copy (game_scarlett_master.exe, SHA-256 e95020b2…a08993) under gta6rt --run. Setup: Windows 11, NVIDIA RTX 3070, LEONIDA_D3D12=vulkan, MSVC (VS 18) + Ninja, writes sent to an --overlay folder. Pass: the boot gets past XMemAllocatePhysicalPages.

Local integration branch: main 1cc6e3c1 + this PR + #6582 + #4313 + #4348. #4313 and #4348 had add/add conflicts with main, resolved by keeping both sides. I left out #6709 and #4531: #6709 conflicts with this PR in XMemVirtualAlloc, and this PR already covers what both were needed for.

From the startup trace (--trace):

Counts, API names and offsets only. AI-assisted (Claude Code).

@0klavair

Copy link
Copy Markdown
Contributor

Tested on the retail executable: pass, and on its own it covers the whole xmem part of the startup.

Own 1.0.2.1 copy (exe sha256 e95020b2…08993), Windows 11 23H2, RTX 5060 / 621.01, Ryzen 9 5950X, LEONIDA_D3D12=vulkan, LEONIDA_UNIMPLEMENTED=stop, MSVC 19.44. Base: main 1cc6e3c1 with #4313, #6792, #6582, #6733, #4348 and #6803 merged. For this test, this PR's xmem.cpp was taken whole, in place of #6709/#3390/#6814 and my local 2 MiB / physical-page stand-ins. Local stand-ins for device slots 26/48/73 (#6964, #6954 and #6935 cover them).

The startup trace shows every xmem call succeeding, with no xmem_virtual_alloc_failed:

call type attributes protect size
XMemVirtualAlloc 0x20402000 0x21 0x4 24 GiB
XMemVirtualAlloc 0x20402000 0x3 0x801 24 GiB
XMemVirtualAlloc 0x20003000 0x183 0x22004 1,228,931,072
XMemVirtualAlloc 0x20003000 0x283 0x2404 2,305,818,624
XMemVirtualAlloc 0x20403000 0x181 0x4 20 MiB
XMemAllocatePhysicalPages 0x280 11,360 pages
XMemVirtualAlloc 0x20401000 0x283 0x42404 64 KiB (from the D3D12 setup function, +0x24786be)

The run then gets through D3D12XboxCreateDevice, four queues, fences, an allocator and the first command list. It stops where my other runs stop: the new list's console slots 131/132, which @tedin7's command-list order item covers (#6750 (comment)). The other xmem PRs reach the same point with more pieces.

AI-assisted (Claude Code).

@shayderrr shayderrr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Summary
runtime/shims/xmem.cpp adds seven measured xmem.dll imports: XMemAllocDefault/XMemFreeDefault share a heap with the existing allocators, XMemIsToolsMemAvailable returns FALSE, and physical-page calls use Windows placeholders (VirtualAlloc2, MapViewOfFile3, UnmapViewOfFile2) for pagefile-backed 64 KiB pages. XMemVirtualAlloc now accepts the attribute word, strips console graphics/write-combine bits and commits 2 MiB-aligned pages; new xmem_virtual_alloc/xmem_physical_pages trace events and SHIM_TABLE entries, with tests in xmem_test.cpp and docs in runtime_imports.md/runtime/README.md.

Why merge
Grounds the runtime's memory shim in measured title behavior, unblocking physical-page and large-page paths during static recompilation. Thoroughly tested and documented.

Verdict: Approve

@shayderrr

Copy link
Copy Markdown
Collaborator

Summary
runtime/shims/xmem.cpp adds seven measured xmem.dll imports: XMemAllocDefault/XMemFreeDefault share a heap with the existing allocators, XMemIsToolsMemAvailable returns FALSE, and physical-page calls use Windows placeholders (VirtualAlloc2, MapViewOfFile3, UnmapViewOfFile2) for pagefile-backed 64 KiB pages. XMemVirtualAlloc now accepts the attribute word, strips console graphics/write-combine bits and commits 2 MiB-aligned pages; new xmem_virtual_alloc/xmem_physical_pages trace events and SHIM_TABLE entries, with tests in xmem_test.cpp and docs in runtime_imports.md/runtime/README.md.

Why merge
Grounds the runtime's memory shim in measured title behavior, unblocking physical-page and large-page paths during static recompilation. Thoroughly tested and documented.

Verdict: Approve

ASTROWwwW pushed a commit to ASTROWwwW/Leonida that referenced this pull request Nov 23, 2026
…orts (physical pages, default allocators, XMemIsToolsMemAvailable)

Author: NiiRoZz

Conflicts with ItsNotRexed#6709 in xmem.cpp, xmem_test.cpp and
docs/runtime_imports.md, resolved to this PR's side: it accepts every
attribute word and reserves 64 KiB-page regions as placeholders, which
replaces the two allow-listed reservation forms of 6709 and the tests that
expected the other forms to be refused. Not built here.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UkEUa9YfGQH4p9g1Guw9gq
@NiiRoZz NiiRoZz changed the title xmem: serve the seven missing imports (physical pages, default allocators, XMemIsToolsMemAvailable); XMemVirtualAlloc takes attributes, 2 MiB pages and placeholders xmem: serve the seven missing imports (physical pages, default allocators, XMemIsToolsMemAvailable); XMemVirtualAlloc takes 2 MiB pages and placeholders Nov 23, 2026
…emVirtualAlloc takes 2 MiB pages and placeholders

Serve the seven xmem.dll imports the shim table lacked, with argument shapes
measured at a title's call sites:

- XMemAllocDefault / XMemFreeDefault share XMemAlloc's heap.
- XMemAllocatePhysicalPages / XMemFreePhysicalPages / XMemMapPhysicalPages /
  XMemMapPhysicalPagesScatter: 64 KiB pages of pagefile-backed sections, one
  MapViewOfFile3 view per page in a placeholder window, so pages keep their
  contents across remaps and can alias. Page numbers are non-zero multiples
  of 16 below 2^25; 0 unmaps.
- XMemIsToolsMemAvailable returns FALSE.

XMemVirtualAlloc no longer refuses 2 MiB pages (aligned through
VirtualAlloc2), makes reserve-only 64 KiB regions placeholder windows, and
passes MEM_RESERVE_PLACEHOLDER and MEM_REPLACE_PLACEHOLDER through. The
attribute word and PAGE_GRAPHICS_* handling from ItsNotRexed#6908 is kept as is.

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

Kam471 commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Retail-exe validation on Windows 11 / RTX 5070 Ti, game_scarlett_master.exe SHA-256 E95020B2..., with the change cherry-picked onto the exe-day integration branch:

  • Before: the game called XMemVirtualAlloc with type=0x20003000, attributes=0x183, protect=0x22004, size ~1.2 GiB (and a second 2.3 GiB call); the old shim returned null, and the game hit its own int3 at game+0x1e1d01b.
  • After: three XMemVirtualAlloc calls succeed (0x20003000/0x183, 0x20003000/0x283, 0x20403000/0x181; 20 MiB 64 KiB-page call included) and the game advances to XCRDAPI!DStorageGetFactory. No assert on the xmem path.
  • Local xmem_test: 167/167 checks after merging the hook-aware XMemAlloc/XMemFree in our branch (the two initial failures were only double xmem_attributes tracing from calling the default allocator internally; fixed).
  • No game data included; RVAs/counts only.

This is the fix that gets the retail title past its boot-time virtual-memory allocation. Thank you.

wavehs added a commit to wavehs/Leonida that referenced this pull request Nov 23, 2026
…1cc6e3c snapshot

tupacfan04: the page still said seven imports had no shim and that
XMemVirtualAlloc refused all 23 calls, while docs/xcl_compare.md (this PR)
and docs/runtime_imports.md say the four physical-page calls are served.
042c5f3 (ItsNotRexed#6919) did give all 12 imports a shim and made XMemVirtualAlloc
accept the attribute word, so the old text was stale, not a different
measurement.

The call-site counts stay measured at 1cc6e3c (they are properties of the
exe); the shim column is relabelled as the current state and all 12 rows
read yes. Five of the 23 allocations are still refused, but for the
allocation type, not the attributes: 0x20406000 carries MEM_PHYSICAL,
which the shim does not forward. The physical-page section now describes
the pagefile-backed pool that ItsNotRexed#6919 implements instead of proposing one.
Cryptoteep added a commit to Cryptoteep/Leonida that referenced this pull request Nov 23, 2026
…gainst main

Update docs/retail_boot_chain.md "What clears it" column against current main:
- Stops 1, 2, 6, 10, 11 landed on main (ItsNotRexed#6919, 042c5f3: physical pages,
  2 MiB pages, and placeholder reservations).
- Stop 3 landed on main (e417570: XErrorReport in runtime/shims/xgameruntime.cpp).
- Replace closed unmerged PR ItsNotRexed#4313 (Stop 4) with tracking open PRs ItsNotRexed#8379, ItsNotRexed#7524.
- Replace closed unmerged PR ItsNotRexed#4348 (Stop 5) with active takeover PR ItsNotRexed#8461, ItsNotRexed#7784.
- Link open tracking PRs for Stops 7, 9, 12 (ItsNotRexed#8851, ItsNotRexed#9061, ItsNotRexed#2490, ItsNotRexed#9045, ItsNotRexed#8414).

Addresses agulerman's finding in ItsNotRexed#8868.
JoshRob297 added a commit to JoshRob297/Leonida that referenced this pull request Nov 23, 2026
…hitelist

PR 6709 proposed a whitelist for XMemVirtualAlloc: attributes 0x21 with PAGE_READWRITE, or 0x03
with 0x801, everything else ERROR_NOT_SUPPORTED. The branch has moved past that: ItsNotRexed#6908 records
whatever attribute word the title passes (note_attributes) and ItsNotRexed#6919 serves physical pages, 2 MiB
pages and placeholders, with host_protect dropping the PAGE_GRAPHICS_* bits.

The whitelist is a restriction, not a capability, and it would reject the calls the retail
executive actually makes next (0x183, 0x283, 0x181: 2 MiB reserve plus commit) - the ones the PR's
own comments document. So the general implementation stays, and what the PR knew is kept where it
belongs: a comment on why the two 64 KiB reservation forms need no special case, and two tests that
pin both attribute pairs, the 65,537-byte size resolving to two 64 KiB regions, the alignment, and
the release.
dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
…tion forms reached at startup

Squashed from ItsNotRexed#6709 by Stalkerino, head ecee287 (security scan PASS).

Conflicts: xmem.cpp, xmem_test.cpp, docs/runtime_imports.md. Main (ItsNotRexed#6908/ItsNotRexed#6919) already accepts every attribute word and strips PAGE_GRAPHICS_* bits, a superset of the PR's two-pair allowlist, and reserves 64 KiB reserve-only regions as placeholders for physical pages. Kept main's code and docs; dropped the PR's allowlist and its checks that other pairs and commit forms are refused, which contradict main. Kept the PR's alignment/rounding/reserve-only check for both forms, without the AllocationProtect check (a placeholder reports PAGE_NOACCESS).
dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
…lAlloc with console page-size bits (what ItsNotRexed#6919 leaves)

Squashed from ItsNotRexed#4531 by labj1987, head 01fc4b1 (security scan PASS).

Rescan of the new commit only (007ae5e..01fc4b1, a test-only variable rename): sec_prescan flags=0, Haiku read PASS. Superset check: the head is already cut down to what ItsNotRexed#6908/ItsNotRexed#6919 leave (4 MiB pages, page-size bits on the title's VirtualAlloc, aligned reservations without the placeholder API) and keeps main's attribute acceptance and PAGE_GRAPHICS_* stripping, so it is taken whole. Conflict: xmem.cpp, where the PR turns XMemVirtualAlloc into a wrapper over xmem_virtual_alloc next to ItsNotRexed#7584's map_view; kept both (wrapper, then ItsNotRexed#7584's per-placeholder map_view; the old unmap_page body ItsNotRexed#7584 replaced stays gone). xmem_test under Wine 11.22: 592 checks OK.
TreXito added a commit to TreXito/Leonida that referenced this pull request Nov 23, 2026
ItsNotRexed#6919

The row still said the shim refuses it. main reserves it 2 MiB-aligned through VirtualAlloc2 and refuses it
only without the placeholder API; the placeholder calls (0x4000, 0x40000) are made as windows too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.