Skip to content

xmem: the documented default routines, allocation hooks and heap queries - #5253

Open
maxxgabriel wants to merge 2 commits into
ItsNotRexed:mainfrom
maxxgabriel:xmem-documented-exports
Open

maxxgabriel wants to merge 2 commits into
ItsNotRexed:mainfrom
maxxgabriel:xmem-documented-exports

Conversation

@maxxgabriel

@maxxgabriel maxxgabriel commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: RT-394

Depends on: #3601, #4531, #3390 (all three also edit runtime/shims/xmem.cpp and runtime/test/xmem_test.cpp; I'll rebase on whichever lands first)

What and why

xmem.dll only served the seven exports the package's DLLs were seen calling. The game's executable will use more of the GDK's XMem surface, so this adds the exports Microsoft documents publicly:

  • XMemAllocDefault / XMemFreeDefault: the default routines, on the existing _aligned_malloc path (64-byte aligned, as before).
  • XMemSetAllocationHooks: XMemAlloc and XMemFree now go through installed hooks. Both routines or neither, as the docs require; two nulls restore the defaults. The hook pair is two atomics, so the allocation path takes no lock.
  • XMemMakeTag: packs a four-character code into the top 24 bits of the flags word. The order inside the documented alphabet isn't public, so that's marked as an assumption in the code; nothing reads the tag back.
  • XMemSetOption: logged once and ignored. Every option configures the console's heap manager, which has no counterpart on the host, and the argument isn't read because its layout isn't public.
  • XMemSetWin32HeapTrackingHooks (accepted; the host heap never calls them), XMemIsToolsMemAvailable (FALSE), XMemGetTitleHeap (NULL, the documented answer when there is no title heap) and XMemPreallocateFreeSpace (TRUE).

The physical-page family has no public description, so it stays with the unimplemented-import stub, and a run reports it if the title calls it.

Two commits: the hook layer first, then the remaining exports.

Tested

  • xmem_test: 97 checks pass at the second commit (91 at the first). New checks cover hook routing, refusing a lone hook, restoring the defaults, XMemMakeTag writing only the top 24 bits and telling codes apart, and the heap queries.
  • Full ctest: 179/179 passed, 10 skipped as on main (package-gated, validation layer, SDL3). That run was on a local branch that also carried two other shims (not in this PR); xmem.cpp and xmem_test.cpp are identical to this PR's.
  • Windows 11 10.0.26200, MSVC 14.51 (Ninja, RelWithDebInfo), GeForce GTX 1660 SUPER, driver 610.88.

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

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested ba149b2 on NVIDIA GeForce RTX 4080, driver 617.42: xmem_test exits 0 and prints "xmem_test: OK (97 checks)", including "PASS a lone alloc hook is refused", "PASS XMemAlloc and XMemFree go through the installed hooks", "PASS two null routines restore the defaults" and "PASS XMemMakeTag fills only the top 24 bits". I did not check whether the three PRs this one depends on are ancestors of this head.

AI-assisted.

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

Reviewed ba149b2. git diff --check clean (2 files, +154/−2).

Commands in a fresh worktree:

cmake -S runtime -B runtime/build-5253 -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_TESTING=ON -DLEONIDA_BUILD_WINDOWS_RUNTIME=ON -DVulkan_INCLUDE_DIR=C:/VulkanSDK/1.4.363.0/Include -DVulkan_LIBRARY=C:/VulkanSDK/1.4.363.0/Lib/vulkan-1.lib
cmake --build runtime/build-5253 --target xmem_test
runtime/build-5253/xmem_test.exe

Result: PASS — 97 checks, 0 failures, exit 0. Covers hook routing (alloc and free each observed once), lone-hook refusal, default restore after two nulls, MakeTag writing only the top 24 bits and distinguishing codes, tools=FALSE, title heap=NULL, preallocate=TRUE, and XMemSetOption returning without reading its argument, plus the existing alloc/query/trace checks.

Verdict: no code blockers.
The MakeTag alphabet order and bit packing are unverified assumptions, but inert here: nothing in the shim reads a tag back.

Non-blocking: (1) runtime/README.md still describes xmem.dll as the four measured exports; the ten new exports are undocumented there, while #3601/#4531/#3390 update that file. (2) Those three PRs are all still open and rewrite both files this PR changes, so merge order needs coordination as the description already notes.

@SirKingg

Copy link
Copy Markdown
Contributor

Independent Windows XMem check, AI-assisted, at ba149b2.

Windows 11 build 26300, VS 2022 Build Tools, MSVC 19.44.35230 / 14.44.35207, Windows SDK 10.0.26100.0, Ninja, RelWithDebInfo. CPU-only; no GPU or game files.

  • Configured CMake with BUILD_TESTING=ON and LEONIDA_BUILD_WINDOWS_RUNTIME=ON; built only xmem_test. ctest --test-dir <scratch-build> -R ^xmem$ --output-on-failure passed 1/1; direct xmem_test.exe passed 97 checks. This includes default 64-byte alignment, normal hook routing, a lone alloc hook being ignored, restoring defaults, heap queries and the MakeTag mask/distinction checks.
  • A separate synthetic executable linked the same xmem.cpp and registry objects. All nine new names resolved; alloc hook received the size and full 64-bit attributes, free hook received the pointer and full attributes; either lone-hook registration left the existing pair unchanged; the direct default allocator bypassed the hook and returned 64-byte alignment; reset sent new allocations to the default. The heap-tracking registration accepted nulls. This checks registry resolution and calls using locally declared x64 signatures, not independent proof of the console ABI.
  • Transition caveat: the synthetic probe allocated a sentinel with hook pair A, installed pair B, then freed the sentinel: B's free hook received it, not A's. Likewise, the code routes a free after reset to the default regardless of where its block originated. This can mismatch allocators if hooks change while blocks remain live. The implementation states the assumption that titles install once at startup before allocating; I could not verify the supported hook-change/lifetime contract. This is a conditional risk, not a confirmed violation, and no concurrency guarantee was tested.
  • The MakeTag checks do not validate the console's alphabet/order or bit mapping; these remain assumptions. Microsoft Learn's XMemSetAllocationHooks reference currently reports restricted access, so I could not independently verify its contract or signatures there.
  • Current heads of Runtime: xmem honours the attribute word's alignment and reports the memory kind (RT-202) #3601, xmem: 4 MiB pages, and a title's VirtualAlloc with console page-size bits (what #6919 leaves) #4531 and RT-217: Handle console allocation flags on Windows #3390 are not ancestors of this tested head; those PRs were not merged or tested together here. No full suite was run.

@tunczzz

tunczzz commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Tested ba149b2b (merge base 3faac557) on Windows 11 10.0.26300 with an NVIDIA GeForce RTX 5070 Ti (Blackwell, subgroup 32..32), driver 616.56. Result: pass.

  • Runtime built from this head: MSVC 19.44 (BuildTools 2022), Ninja, RelWithDebInfo, -DLEONIDA_VULKAN_HEADERS_FETCH=ON. Vulkan SDK 1.4.363.0 was installed, so the *_validated ctests ran with its validation layer. glslangValidator and llvm-mc 21 came from WSL Ubuntu. Build exit 0.
  • ctest -j 3: 177/177 passed.
  • scripts/run_gpu_tests.py --build <this build>: 170/217 passed, 47 skipped, 0 failed.
  • Every test gives the same result as on main 3faac557 (ctest 177/177, GPU 170/217).
  • As on main, run_gpu_tests.py records cs_fmt_d16_xyz twice and skips cs_tbuffer_store. That's main's runner bug (fixed by run_gpu_tests: record cs_tbuffer_store's result; fail a run that records a test twice #4784), not this PR.

Not run: other GPUs/vendors, Linux. No game files.

AI-assisted (Claude Code).

Edited: an earlier version said the Vulkan SDK and its validation layer weren't installed. SDK 1.4.363.0 had been installed the night before this run; the results above are unchanged.

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.

5 participants