Skip to content

RT-217: Handle console allocation flags on Windows - #3390

Open
jewmaster1337 wants to merge 5 commits into
ItsNotRexed:mainfrom
jewmaster1337:fix/rt-217-console-virtual-alloc
Open

jewmaster1337 wants to merge 5 commits into
ItsNotRexed:mainfrom
jewmaster1337:fix/rt-217-console-virtual-alloc

Conversation

@jewmaster1337

@jewmaster1337 jewmaster1337 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

RT-217 (host-backed allocation). Recognize the console allocation and protection tags on mapped kernel32.dll and kernelbase.dll VirtualAlloc imports and on XMemVirtualAlloc. Remove the console tags before calling Windows. A tagged call with a guest page-mode flag uses the existing XMem page handling. Ordinary Win32 calls keep their native flag interpretation.

The memory is backed by the host. A graphics allocation arena is a separate design decision in #313.

Coordination claim

Verification

  • Built xmem_test, gta6rt, and tiny_game with MSVC 19.44 and Windows SDK 10.0.26100.0.
  • The new registry test failed on the original code for both the missing VirtualAlloc shim and the rejected tagged XMemVirtualAlloc call.
  • After the fix, all five targeted CTests passed: xmem, run_tiny_game, run_tiny_game_alloc, run_tiny_game_tls, and run_tiny_game_eh. The new mapped executable allocates, writes, and releases a tagged allocation through its imported APIs.
  • git diff --check passed. Windows CPU builds and synthetic fixtures only.

AI-assisted with Codex.

Integration with #6908

Main now strips MEM_GRAPHICS, MEM_TITLE, and the low PAGE_GRAPHICS_* bits from mapped allocations and records the XMem attribute word. This update keeps that behavior and adds the remaining RT-217 cases: the high protection annotation, tagged guest page sizes through mapped VirtualAlloc, and allocation tags through XMemVirtualAlloc. Untagged protection zero still gets the native Win32 invalid-parameter error. Both entry points share the guest allocation logic, and only actual XMem calls record XMem attributes.

  • Negative control on upstream d215ec8b using the PR tests: four failed assertions covering the protection tag, tagged page size, native error, and XMem allocation tags.
  • Native Windows 11, MSVC 19.44, Ninja RelWithDebInfo: ctest --test-dir build/cpu -R "^(xmem|run_tiny_game.*)$" --output-on-failure — 12 passed. The Windows loader and synthetic PE fixtures were compiled for this run.
  • git diff --check and the no-game-data scan passed. CPU-only tests with synthetic inputs. AI-assisted (Codex).

@daemosofchaos

Copy link
Copy Markdown
Contributor

Independent Windows 11 build 26300 / MSVC 19.52 check of c9f450cd, using synthetic inputs only. Built xmem_test, gta6rt and tiny_game; xmem, run_tiny_game, run_tiny_game_alloc, run_tiny_game_tls and run_tiny_game_eh passed (5/5). The mapped stand-in allocated, wrote and released memory through the shimmed import.

I added two local edge probes: an untagged PAGE_GUARD allocation retained the same state and protection as native VirtualAlloc, and a tagged 64 KiB reservation followed by a commit at an interior address committed the expected aligned guest page. Both passed; the extended xmem test reported 73 checks. The allocations were released after testing.

No game files or GPU were used. This does not validate a graphics-memory arena or the game's actual allocation calls.

— LocalFirst-AI

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

AI-assisted independent Windows CPU review of c9f450c. Mapped VirtualAlloc inherits the XMem fallback at runtime/shims/xmem.cpp:136/180: allocation type 0x30400000 (graphics plus 64 KiB annotations), size 1, PAGE_READWRITE and no reserve/commit operation returns 65,536 committed bytes through the kernel32 registry entry. Native VirtualAlloc with annotations removed fails with ERROR_INVALID_PARAMETER (87). Preserve that failure for the mapped API and add a regression.

Three Release CTests passed, including 67 xmem checks and mapped tiny-game controls. Untagged physical/large-page controls matched native; tagged reserve, unaligned commit, decommit and release passed. Windows build 26200, MSVC 19.50.35721, SDK 10.0.26100.0. Synthetic inputs only; no GPU/game files.

@towade

towade commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Review of c9f450c. I had RT-217 claimed in #3381 a few minutes before this opened and wrote the same fix locally; rather than a second PR I'm releasing the item to this one. Two gaps I hit in mine that this one has as well:

  1. API-set import names. The shim is registered for kernel32.dll and kernelbase.dll only. A module linked against the API-set umbrella libraries imports VirtualAlloc from api-ms-win-core-memory-l1-1-0.dll instead (Windows' own dbghelp.dll does, for example). That import misses the shim and still gets ERROR_INVALID_PARAMETER for the console bits. process.cpp and library_loader.cpp register their functions under the matching api-ms-win-core-* names for this reason. I registered api-ms-win-core-memory-l1-1-0 to -l1-1-3 too.
  2. Protection bits. Only 0x04000000 is removed. Any other console bit above the host's protection flags still reaches the host and fails the same way. I masked protect to 0x7FF, PAGE_NOACCESS through PAGE_EXECUTE_WRITECOPY plus PAGE_GUARD / PAGE_NOCACHE / PAGE_WRITECOMBINE. If you'd rather keep a known-bits list, a one-time log of any other high bit would at least show it.

A test for 1 that fails without the extra names: resolve VirtualAlloc through rt::shims::find under kernel32.dll, kernelbase.dll and api-ms-win-core-memory-l1-1-0.dll, check all three are the same shim, then allocate with MEM_RESERVE | MEM_COMMIT | 0x10000000, PAGE_READWRITE and check the region is committed and writable.

Measured here, Windows 11 Pro 26300: the real VirtualAlloc returns null with error 87 for MEM_GRAPHICS, for MEM_TITLE, and for protection 0x04000004, and succeeds for the plain call. That matches your description.

AI-assisted (Claude Code).

@daemosofchaos

Copy link
Copy Markdown
Contributor

Follow-up on 6933cfc7, Windows 11 build 26300 / MSVC 19.52, using synthetic inputs only. The updated xmem suite passed 102 checks, and xmem, run_tiny_game, run_tiny_game_alloc, run_tiny_game_tls and run_tiny_game_eh passed 5/5.

I also built a tiny standalone PE that imports VirtualAlloc from api-ms-win-core-memory-l1-1-0.dll (confirmed with dumpbin /imports) and ran the same executable through both runtimes:

  • Previous c9f450cd: the loader resolved the import to the host and the tagged 64 KiB allocation failed.
  • Updated 6933cfc7: the loader reported the shim, the allocation succeeded, both ends of the 64 KiB region were writable, and it was released successfully. A tagged call without an allocation operation returned ERROR_INVALID_PARAMETER.

As a separate control, I linked the same source against kernel32.dll: the previous runtime incorrectly accepted the tagged no-operation call, whereas the updated runtime rejected it and the fixture passed. No game files or GPU were used. This does not resolve the separate question about other protection bits.

— LocalFirst-AI

@vibryxcx

Copy link
Copy Markdown

Tested 6933cfc on AMD Radeon RX 6800 (RDNA2), AMD 26.8.1 (Windows 32.0.21045.5002), Windows 11 Pro 25H2 (build 26200): pass

This is the head with the review fixes; the earlier checks here were on c9f450c.

  • The five CTests from the PR body (xmem, run_tiny_game, run_tiny_game_alloc, run_tiny_game_tls, run_tiny_game_eh): 5/5 passed. xmem_test: OK (102 checks).
  • Full ctest -C RelWithDebInfo: 0 failed out of 141 (142 registered, gameinput disabled because SDL3 isn't installed). 7 skipped: shader_library_corpus, bink_decode, fsr_dispatch, bvh_builder, bink_audio (need LEONIDA_GAME_DIR), d3d12_first_light (needs LEONIDA_FIRST_LIGHT_DIR) and vk_sync_validation (needs LEONIDA_VK_VALIDATION=1, run below).
  • With LEONIDA_VK_VALIDATION=1: vk_sync_validation passed (synchronization validation active), and xmem and run_tiny_game_alloc passed again.
  • main f847748, built and run the same way: 140 tests (run_tiny_game_alloc is new here), the same skips, and one failure: vk_pipeline_check_cli (--count : expected usage error, got Process terminated due to timeout). With --repeat until-fail:5 it passed 5/5 on both main and this branch, so it looks like a timeout under load, not something this PR changes.
  • run_gpu_tests.py: 184/199 passed, 15 skipped, 0 failed, the same as main.

From reading the diff: the second commit adds the operation check from @kacper-serewis's review and the api-ms-win-core-memory-l1-1-0 to -l1-1-3 names from point 1 of @towade's review. I don't see a change for point 2 (other high protection bits); only 0x04000000 is removed.

Built with scripts\build_runtime.ps1 (MSVC 14.44, VS 2022 17.14, Windows SDK 10.0.26100), Vulkan SDK 1.4.363, Python 3.12.10. No game files.

AI-assisted (Claude Code): it built both commits and ran the tests.

@memorypressure

Copy link
Copy Markdown

Verified on Linux: the branch conflicts with current main in runtime/CMakeLists.txt, tiny_game.cpp and xmem_test.cpp (re-confirmed), and its premise is partly on main already — 4c52ce2 (2026-11-22) added TitleVirtualAlloc covering kernel32/kernelbase and api-ms memory-l1-1-0 through -6, a superset of this branch's -0...-3. The genuinely new deltas (dropping the 0x04000000 protection tag, requiring an explicit operation for console-tagged calls) are exactly what main's comment marks as not done, so they may still be wanted — but as a delta on 4c52ce2, because merging this branch would register kernel32.dll!VirtualAlloc twice (xmem table + title_memory table) and make lookup depend on registration order. CI shows windows/linux-gpu-tools red on the merge attempt. A rebase against main's current shim registry looks like the right next step; Wine behaviour unverifiable here. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@thehollowedone

Copy link
Copy Markdown

Native Windows recheck of 21f54763 on 561b65a8. Windows 11 x64, MSVC 19.44.35230, Windows SDK 10.0.26100.0.

The stock mapped run_tiny_game_alloc test passed allocation, write and release. The focused xmem fixture passed all 84 checks. With only xmem.cpp restored to base, the mapped allocation failed and the focused fixture failed three checks covering the protection tag, tagged 64 KiB routing, and combined tagged XMem allocation. The existing registry alias and ordinary Win32 allocation checks passed in both runs.

The scratch xmem fixture ran the positive cases only. Synthetic inputs, CPU only. AI-assisted.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 21f5476 on NVIDIA GeForce RTX 4080, driver 617.42: xmem_test exits 0 and prints "xmem_test: OK (120 checks)", including "PASS tagged Win32 VirtualAlloc still requires an allocation operation", "PASS ordinary invalid Win32 protection keeps the host error" and "PASS XMemVirtualAlloc also accepts the console allocation and protection tags". The gta6rt-based run_tiny_game tests were not run.

AI-assisted.

@sh1shu1

sh1shu1 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 21f5476 on NVIDIA RTX 4070 SUPER (driver 616.92), Windows 11 26200, MSVC 14.44, Vulkan SDK 1.4.363, RelWithDebInfo: pass, no regression against main 561b65a.

  • ctest: 244/248 passed; d3d12_indirect_dispatch_count, d3d12_indirect_rt, dxgi_device, dxgi_swapchain fails on main 561b65a too (not from this PR), same 11 skips as main 561b65a
  • xmem: 1/1
  • run_gpu_tests (LEONIDA_VK_VALIDATION=1): 183/232 native, 214/232 with LEONIDA_W64_EMULATE=1, 2 failed; ms_wave_info_s3 fails on main 561b65a too
    AI-assisted (Claude Code, Claude Fable 5.1).

@DeSunShine

Copy link
Copy Markdown
Contributor

Tested 9a9c97f45 merged into main 1cc6e3c12 on NVIDIA GeForce RTX 4090 (driver 621.24), Windows 11 Pro 26200, MSVC (VS Build Tools 18) + Ninja, Vulkan SDK 1.4.363: pass.

  • ctest -R "xmem|run_tiny_game" --output-on-failure: 12/12 passed (xmem, run_tiny_game, _alloc, _console_paths, _exit_code, _abort_exit_codes, _fixed, _fixed_exit_code, _tls, _eh, _stack, _dir).

AI-assisted (Claude Code).

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.

10 participants