Skip to content

xmem: 4 MiB pages, and a title's VirtualAlloc with console page-size bits (what #6919 leaves) - #4531

Open
labj1987 wants to merge 2 commits into
ItsNotRexed:mainfrom
labj1987:xmem-large-pages
Open

labj1987 wants to merge 2 commits into
ItsNotRexed:mainfrom
labj1987:xmem-large-pages

Conversation

@labj1987

@labj1987 labj1987 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

After #6919, XMemVirtualAlloc serves 64 KiB and 2 MiB guest pages. Two console page-size cases still failed:

  • 4 MiB pages (MEM_4MB_PAGES, 0x80000000, alone or with MEM_LARGE_PAGES) were refused. They now take xmem: serve the seven missing imports (physical pages, default allocators, XMemIsToolsMemAvailable); XMemVirtualAlloc takes 2 MiB pages and placeholders #6919's 2 MiB path with a 4 MiB granularity and alignment (placeholders and windows included).
  • A title's own VirtualAlloc with console page-size bits passed them to the host, which reads 0x20000000 as host large pages (needs SeLockMemoryPrivilege) and fails. It now goes through the same page-size handling, but as ordinary aligned reservations (no XMem placeholder windows, so a later plain MEM_COMMIT inside works as on Win32) and without an xmem_attributes trace record. Win32 rules are kept: an operation is required, MEM_COMMIT without MEM_RESERVE at a null address fails, the placeholder flags are refused, and MEM_RESET/MEM_RESET_UNDO drop the page bits and go to the host.

Also: an address that rounds down to 0 at the guest page size fails with ERROR_INVALID_ADDRESS instead of being treated as unplaced.

Proof: xmem_test 576 checks, 0 failures (clang-cl 21 + xwin cross build, Wine 10.0), covering 4 MiB reserve/commit/release and placeholder replace, title reserve-then-commit at 2 MiB and 4 MiB, reset, refused flag combinations. xmem, xmem_probe, unimplemented_policy and the run_tiny_game* ctests pass except run_tiny_game_abort_exit_codes, which fails the same way on main under Wine. Reviewed in two rounds by a second model; all findings fixed.

Earlier versions of this PR also served 2 MiB pages and the attribute word; #6908 and #6919 cover those now, so that part is gone. #3390 overlaps the title VirtualAlloc part (console-tagged calls only, before #6919).

AI-assisted (Claude Code).

🤖 Generated with Claude Code

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

Built and ran xmem_test at a57fa304 on Windows 11: 146 checks, 0 failures, exit 0.

The console's 2 MiB and 4 MiB page requests work through XMemVirtualAlloc and a title's VirtualAlloc without the host large-page privilege, including the 64 KiB one-page case and the expected Win32 failure. No blocking issue.

@Flyander Flyander 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 shim keeps console page-size bits away from Win32, validates unsupported combinations before allocation, and rounds the requested region with overflow checks. New reservations use VirtualAlloc2 alignment when available and a bounded release-and-retry fallback otherwise; existing-address commits are expanded to the requested guest page granularity. The tests cover both allocator entry points and the fallback. I found no correctness issue in the diff.

@kapitche

Copy link
Copy Markdown
Contributor

Tested a57fa30 merged with main d0835dd (merge 249f477c9, no conflicts) on native Windows 11, MSVC 14.51, full runtime/ build: pass.

  • ctest -R xmem: xmem and the new xmem_probe both pass (104 checks). tests/test_docs_*.py (7 files): OK on Linux Python 3.12.16.
  • Negative control 1: runtime/shims/xmem.cpp from main with the PR's tests: both tests FAILED (104 checks, 24 failures).
  • Negative control 2 (does xmem_probe really exercise the fallback?): in the PR's reserve_aligned fallback I moved the aligned base by 0x1000: xmem still passes and xmem_probe FAILS, so the probe path is covered only by the new test.
  • Read the diff: a title VirtualAlloc without MEM_RESERVE/MEM_COMMIT and a page-size bit, and the combinations 0x00400000 alone / MEM_4MB_PAGES|MEM_PHYSICAL, fail with ERROR_INVALID_PARAMETER as docs/runtime_imports.md says. Not run: a title actually using these flags (no game copy), so only the shim contract is checked.

@memorypressure

Copy link
Copy Markdown

One conflict with current main: runtime/test/xmem_test.cpp, where main added the attributes-trace test at the same spot as your page-size tests — keeping both resolves it (brace balance and both call sites verified in a test merge). Static soundness reads: zero-size rejected, overflow-guarded page rounding, invalid page-bit combinations refused; LEONIDA_XMEM_NO_VIRTUALALLOC2 documented so the env-var guard passes. Actual page-size behaviour is Windows-only; checks pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only review).

@Krashgamer

Copy link
Copy Markdown
Contributor

Tested fc1f8dd8 (it already contains main 3faac557) on native Windows 11 Pro (26200), MSVC 14.51 (VS 2026 Build Tools), Ninja, RelWithDebInfo, full runtime/ build: pass. This is the native Windows run the description asks for, on the current head. CPU-only, so no GPU was involved (RTX 3080 / 610.88 in the box).

  • ctest -R "xmem|loader|dll_thread|run_tiny": 20/20 pass. xmem_test: OK, 158 checks. xmem_probe (LEONIDA_XMEM_NO_VIRTUALALLOC2=1): OK, 158 checks.
  • Native Windows takes the VirtualAlloc2 + MEM_ADDRESS_REQUIREMENTS path, which the Wine runs could not separate from the fallback. Control: with requirements.Alignment = alignment changed to 0 in reserve_aligned (runtime/shims/xmem.cpp:195), xmem FAILS (158 checks, 33 failures: the 2 MiB alignment, the one-page commit and the MEM_TOP_DOWN cases among them) while xmem_probe still passes. So on Windows 10/11 the aligned-VirtualAlloc2 call is what the tests exercise, and the probe ctest covers the fallback on its own.
  • Negative control: main's xmem.cpp with this PR's tests: FAILED (116 checks, 24 failures), with and without LEONIDA_XMEM_NO_VIRTUALALLOC2. That's the same 24 the description reports from Wine.
  • The full build gives no warnings in xmem.cpp or xmem_test.cpp.

Not checked: a title that actually requests these page sizes (no executable), and a host where VirtualAlloc2 refuses the alignment parameter, which would be the ERROR_INVALID_PARAMETER fallback inside the first branch. Only the forced fallback was run.

AI-assisted (Claude Code).

@thehollowedone

Copy link
Copy Markdown

Thanks for the broader Windows run, @Krashgamer. A scratch trace adds direct confirmation of the requested 4 MiB path on fc1f8dd8 with 3faac557, Windows 11 x64 and MSVC 19.44.35230.

All 18 default 4 MiB reservations succeeded through VirtualAlloc2, with zero fallbacks. All 18 forced-fallback reservations succeeded without a VirtualAlloc2 attempt. The focused synthetic xmem and xmem_probe runs passed 114 checks each, both with the exact runtime and with the temporary trace enabled. AI-assisted.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested fc1f8dd on NVIDIA GeForce RTX 4080, driver 617.42: xmem_test exits 0 with "xmem_test: OK (158 checks)". The log has PASS lines for the 2 MiB and 4 MiB VirtualAlloc cases, e.g. "PASS kernelbase.dll!VirtualAlloc, 4 MiB (0x80000000) pages, tags 0x50000000: aligned, two pages (error 0)". The xmem_probe leg with LEONIDA_XMEM_NO_VIRTUALALLOC2=1 was not run.

AI-assisted.

@Stalkerino

Copy link
Copy Markdown

I checked the next startup allocation against the exact XMem and registry sources at fc1f8dd8f, after the reserve-only fix in #6709. This is a small synthetic comparison on CachyOS, Wine 11.22, clang-cl 23.1.5 and the 10.0.26100 SDK. Both requests used a null address, 2 MiB of memory, and MEM_RESERVE | MEM_COMMIT | 0x20000000.

Access attributes Protection Result
0 PAGE_READWRITE Success. VirtualQuery reported MEM_COMMIT and the base was 2 MiB aligned.
0x183 PAGE_READWRITE | PAGE_GRAPHICS_READWRITE | PAGE_GRAPHICS_COHERENT Null, GetLastError() = 50 (ERROR_NOT_SUPPORTED).

On our prepared Linux runtime, the second form is the next observed startup blocker after the two 64 KiB address reservations. This identifies a consumer for the aligned allocation work and a separate requirement for the CPU/GPU access and coherence flags. The control exercises #4531's existing aligned-page support. I have not established the full meaning of the access word or GPU coherence for ordinary host pages. AI-assisted.

@aidity

aidity commented Nov 23, 2026

Copy link
Copy Markdown

Windows/MSVC integration validation: combined this PR with the narrow reserve-only forms from #6709 and allocation-failure tracing from #6733. All 170 XMem checks passed on Windows x64, MSVC 14.44 / SDK 10.0.26100.

The aligned-page implementation gets past its synthetic 2/4-MiB tests. The next startup allocation is still rejected with ERROR_NOT_SUPPORTED by the nonzero-access-attributes check. It is a reserve+commit graphics-coherent read/write request, rather than a reserve-only operation, so the reservation changes cannot cover it. No large-page alignment failure was observed for this case because validation rejects it first.

No game data, binary excerpts, addresses or trace files are attached. AI-assisted with Codex.

@sneylol

sneylol commented Nov 23, 2026

Copy link
Copy Markdown

Tested fc1f8dd against the retail exe, on top of #6709.

The page-size handling works, but the startup 2 MiB reserve+commit (1.15 GiB) still fails here: XMemVirtualAlloc refuses its nonzero attribute word (0x183) before reaching the page-size path. As a local experiment, accepting that call and dropping the PAGE_GRAPHICS_* bits from protect lets the allocation succeed and startup continue past the memory pools.

Note: this PR currently conflicts with main, and taking this branch's xmem.cpp wholesale drops main's XMemTransferMemory shim (it then shows as unimplemented).

Windows 11 26200, AMD RX 9070 XT, MSVC 14.44, Windows SDK 10.0.26100, own Xbox 1.0.2.1 package (with game_scarlett_master.exe). gta6rt --run with --overlay, inspected under cdb. Outcomes only; no game data, RVAs or traces.

AI-assisted (Claude Code).

@quackottu

Copy link
Copy Markdown
Contributor

Tested on the retail engine game_scarlett_master.exe (120,181,760 bytes, 1.0.2.1) under gta6rt --run, RTX 5070 / Windows 11, main 1cc6e3c + this PR + the other boot PRs named below. Outcomes only.

Needed by the retail exe: its GPU pools are committed with 2 MiB pages (0x20003000) at exe+0x1e1d181/0x1e1d288/0x1e1d369 and elsewhere; they succeed with this PR once the attribute word (0x183/0x283/0x181) and the PAGE_GRAPHICS_* protect bits (0x22004) are accepted (see my note on #6709). It conflicts with #6709 in XMemVirtualAlloc; I resolved it by taking this PR's console_virtual_alloc path.

@undeadhuman37

Copy link
Copy Markdown
Contributor

Measured on the 1.0.2.1 package (own copy, Windows 11): with #6709 applied the title's third XMemVirtualAlloc at startup is address=0, size=0x49400000, allocation_type=0x20003000 (MEM_LARGE_PAGES|MEM_COMMIT|MEM_RESERVE), attributes=0x183, protect=0x22004, and a null return stops the boot at exe+0x1e1d3a7. So after #4531 (2 MiB page serving) two more things are needed: (1) attributes 0x183 must be accepted (#4531 still returns ERROR_NOT_SUPPORTED for any nonzero word; #6709 only allows 0x21 and 0x03), and (2) protect carries console graphics bits (0x22004 = PAGE_READWRITE 0x4 plus 0x22000) that must be stripped before the host VirtualAlloc, as #6709 does for PAGE_GRAPHICS_NOACCESS. Since both PRs rewrite the same function, I'm not opening a third one on top; it should land as a small follow-up after both merge, or be folded into #4531. (worker 1 for the team; AI-assisted)

@itaskaev2

Copy link
Copy Markdown

Tested 007ae5e5 (contains main 042c5f37) on native Windows 11 (26300), NVIDIA GeForce RTX 4080 SUPER, driver 616.92, MSVC 19.51 (14.51.36231), Ninja, RelWithDebInfo, full runtime/ build: pass. As far as I can see this is the first native Windows run of this cut-down head; the earlier native runs were on fc1f8dd8.
AI-assisted (Claude Code), no game files.

  • xmem_test.exe: xmem_test: OK (576 checks), exit 0. The 4 MiB cases pass, for example PASS XMemVirtualAlloc commits 4 MiB guest pages, PASS a 4 MiB-page region is 4 MiB-aligned, PASS a commit replacement rounds to the 4 MiB window boundaries and PASS replace a subrange of the two-page 4 MiB placeholder.
  • ctest -R "xmem|unimplemented_policy|run_tiny_game": 12/12 pass, including run_tiny_game_abort_exit_codes. That test fails under Wine in the PR description; it passes natively.
  • Negative control: I put runtime/shims/xmem.cpp back to 042c5f37 and kept the new xmem_test.cpp. The result is xmem_test: FAILED (332 checks, 96 failures), exit 1. Among the failures are XMemVirtualAlloc commits 4 MiB guest pages, reserves a 4 MiB-page window, reserve an explicit two-page 4 MiB placeholder and kernel32.dll!VirtualAlloc page bits 0x20000000 tags 0 returns aligned memory. With the PR's file restored it is OK again (576).
  • Full ctest (-j 4): 266 of 271 pass or skip (11 skipped: package-gated or no layer). The 5 failures are not from this PR:
    • d3d12_indirect_dispatch_count, d3d12_indirect_rt and xbox_present (a crash; RTSS is running here) already fail on main on this machine.
    • d3d12_vulkan_device stops at tiles == 1, in the CreateReservedResource2 / GetResourceTiling block. Current main dde82f27 stops at the same check (line 6574 there), so it is a separate pre-existing failure on this driver.
    • d3d12_bindless_churn failed only under the parallel run; on its own it passes, with every sampled texel matching.
  • One new /W4 warning from the PR, xmem_test.cpp(260): C4456: the inner bytes hides the earlier local. It is cosmetic.

Not covered: the retail executable. I have no own copy, so I can't confirm the boot stop at the third XMemVirtualAlloc that is described above.

…SVC C4456)

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

Copy link
Copy Markdown
Contributor Author

Note

🤖 Claude Opus 5.5 responding on behalf of labj1987

Thanks @itaskaev2, especially for the negative control. The C4456 warning is fixed in 01fc4b14 (test-only rename of the inner pointer; xmem_test still 576/576). Good to know run_tiny_game_abort_exit_codes passes natively: that confirms it as a Wine-only failure.

@DavidM1911

Copy link
Copy Markdown

Tested fc1f8dd, Windows 11, MSVC 14.50 (VS Build Tools 18), RelWithDebInfo, own 1.0.2.1 copy:

AI-assisted. No game files or game-derived bytes in this comment.

@itaskaev2

Copy link
Copy Markdown

Tested 01fc4b14 (contains main 042c5f37) on Windows 11, NVIDIA GeForce RTX 4080 SUPER, driver 616.92, MSVC 14.51, RelWithDebInfo: pass.
AI-assisted (Claude Code), no game files.

Re-check of the C4456 fix only (the head differs from 007ae5e5 in runtime/test/xmem_test.cpp alone):

  • xmem_test builds with no warnings now. With the 007ae5e5 version of that file put back, the same build prints test\xmem_test.cpp(260): warning C4456 (the inner bytes); with the PR's file restored the warning is gone.
  • xmem_test.exe: xmem_test: OK (576 checks), exit 0.

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.