Skip to content

xmem: reject null addresses in XMemMapPhysicalPages and XMemMapPhysicalPagesScatter - #7805

Open
MartinIndra02 wants to merge 2 commits into
ItsNotRexed:mainfrom
MartinIndra02:xmem-reject-null-map-address
Open

MartinIndra02 wants to merge 2 commits into
ItsNotRexed:mainfrom
MartinIndra02:xmem-reject-null-map-address

Conversation

@MartinIndra02

@MartinIndra02 MartinIndra02 commented Nov 23, 2026 •

Copy link
Copy Markdown

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

Depends on: none (applies cleanly to main).

What and why

In runtime/shims/xmem.cpp, aligned_page_address(uintptr_t address) checked (address & (kPhysicalPageSize - 1)) == 0, which returns true for 0 (nullptr):

  • XMemMapPhysicalPages(nullptr, 1, nullptr) and XMemMapPhysicalPages(nullptr, 0, nullptr) entered the unmap path and returned nullptr without setting ERROR_INVALID_PARAMETER.
  • XMemMapPhysicalPagesScatter([nullptr], 1, nullptr) and XMemMapPhysicalPagesScatter([nullptr], 1, [0]) returned TRUE instead of FALSE with ERROR_INVALID_PARAMETER.

Changes:

  • runtime/shims/xmem.cpp: require address != 0 in aligned_page_address().
  • runtime/test/xmem_test.cpp: add four regression checks covering all four null-address cases above.

Tested

Windows 11 x64, MSVC 19.44.35220 (RelWithDebInfo, Windows SDK 10.0.26100.0), CPU-only test, no game files:

  • Before fix: xmem_test.exe failed on all 4 new checks (FAILED (177 checks, 4 failures)).
  • After fix: xmem_test.exe passed (xmem_test: OK (177 checks), exit 0).
  • python -I scripts/check_no_game_data.py runtime/shims/xmem.cpp runtime/test/xmem_test.cpp and git diff --check: clean.

AI-assisted: yes.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md).
  • Translator contract update (no translator change).

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

Tested de0fb40ab on Windows 11 x64, MSVC 19.44.35230.0 (VS 2022 Build Tools), CMake + Ninja RelWithDebInfo, Windows SDK 10.0.26100.0 (CPU-only, no game files): pass.

  • Merges cleanly onto upstream/main (dde82f27e).
  • Built xmem_test with zero warnings in runtime/shims/xmem.cpp and runtime/test/xmem_test.cpp.
  • PR head (de0fb40ab): xmem_test.exe -> xmem_test: OK (177 checks) (exit 0).
  • Negative control (runtime/shims/xmem.cpp from upstream/main, PR test file kept): xmem_test.exe -> xmem_test: FAILED (177 checks, 4 failures) (exit 1) on all four new null-address checks.
  • python -I scripts/check_no_game_data.py runtime/shims/xmem.cpp runtime/test/xmem_test.cpp: clean (exit 0).
  • Diff review: requiring address != 0 in aligned_page_address() cleanly rejects nullptr across both XMemMapPhysicalPages and XMemMapPhysicalPagesScatter (including unmap paths) with ERROR_INVALID_PARAMETER.

AI-assisted.

@Envyre

Envyre commented Nov 23, 2026

Copy link
Copy Markdown

Rejecting NULL looks right: the comment at runtime/shims/xmem.cpp:554-556 and docs/xmem_exe_calls.md:74 say every measured call targets an address inside a window XMemVirtualAlloc reserved, and nothing points to a VirtualAlloc(NULL)-style "let the system pick" meaning. A NULL map already failed in find_window (ERROR_INVALID_ADDRESS, :578-582); what changes is that NULL unmap and zero-count calls now set ERROR_INVALID_PARAMETER instead of returning without an error, matching the alignment check (:565, :608).

Small requests:

  • Scatter fails part-way: addresses are validated one entry at a time inside the loop (:605-622), so {valid, NULL} maps entry 0 and then returns FALSE with that mapping left in place (before this PR, a NULL entry with page 0 or a null page array was a harmless no-op). Validating all addresses before the loop changes anything would make it all-or-nothing; otherwise worth documenting with a test of a valid entry followed by NULL.
  • The map tests (runtime/test/xmem_test.cpp:443-448) pass only a null page array; map(nullptr, 1, pages) is the case whose error changed from ERROR_INVALID_ADDRESS to ERROR_INVALID_PARAMETER.
  • Nit: aligned_page_address (:466) now also means non-null; a rename or one-line comment would help.

AI-assisted (Claude Code).

Review follow-up: XMemMapPhysicalPagesScatter checked addresses one entry
at a time inside the mapping loop, so {valid, NULL} mapped entry 0 and then
failed with that mapping left in place. All addresses are now checked up
front, so an invalid address fails the call with nothing changed.

Also rename aligned_page_address to valid_page_address (it now rejects
NULL too) and test map(nullptr, 1, pages), whose error changed from
ERROR_INVALID_ADDRESS to ERROR_INVALID_PARAMETER.

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

Copy link
Copy Markdown
Author

Thanks, all three addressed in c179730:

  • Scatter is now all-or-nothing for address validation. Every address is checked (non-null, 64 KiB-aligned) before the placeholder check and before any entry is mapped, so {valid, NULL} and {valid, unaligned} fail with ERROR_INVALID_PARAMETER and leave the valid entry unmapped. Later failures (address outside any window, page not live) still leave earlier entries mapped, same as XMemMapPhysicalPages; the function comment now says so.
  • New tests (runtime/test/xmem_test.cpp, written while live pages are still allocated):
    • map(nullptr, 1, pages) fails with ERROR_INVALID_PARAMETER (the case whose error changed from ERROR_INVALID_ADDRESS)
    • scatter({window+3*64K, nullptr}, 2, {p0, p1}) fails with ERROR_INVALID_PARAMETER and window+3*64K stays reserved-only
    • scatter({window+3*64K, window+2*64K+1}, 2, {p0, p1}): same, with an unaligned second entry
    • Before the fix, both scatter tests failed (xmem_test: FAILED (180 checks, 2 failures)).
  • Nit: renamed aligned_page_address to valid_page_address and added a comment explaining why NULL is rejected.

Test run: cmake --build <build> --target xmem_test, then xmem_test.exe gave OK (180 checks). python -I scripts/check_no_game_data.py runtime/shims/xmem.cpp runtime/test/xmem_test.cpp and git diff --check are clean.

Environment: Windows 11 x64, MSVC 19.44.35220, NVIDIA GeForce RTX 3080 (driver 621.24 / 32.0.16.2124). AI-assisted (Claude Code).

@comtek21516

Copy link
Copy Markdown

Tested de0fb40ab (it sits directly on main at dde82f27e) on Windows Server 2025, MSVC 14.52 (Visual Studio 18), Ninja RelWithDebInfo, no GPU: pass.

  • Build: all 428 steps, no new warnings from the two changed files.
  • xmem_test: OK (177 checks); the four new checks pass. ctest xmem passes.
  • Negative control: with runtime/shims/xmem.cpp taken from main and this PR's test kept, xmem_test ends FAILED (177 checks, 4 failures), and the four are exactly the new ones (XMemMapPhysicalPages(nullptr, 1, nullptr), (nullptr, 0, nullptr), XMemMapPhysicalPagesScatter([nullptr], 1, nullptr) and ([nullptr], 1, [0])).
  • Whole suite: 146 passed, 122 skipped (no Vulkan driver here), 4 failed. The four are gameinput_host, d3d12_sparse_queue_family, vk_bindless_ps_tex and vk_bindless_ps_imgatomic, which fail the same way on main on this machine (runtime tests: skip, not fail, without a Vulkan driver or a working host GameInput (4 ctests) #7452) and do not involve xmem.
  • Code read: aligned_page_address has two callers, the address check of XMemMapPhysicalPages and the per-entry check of XMemMapPhysicalPagesScatter; both are the paths the new checks exercise, so no other caller changes behaviour.

Not checked: the retail executable (no game files used).

AI-assisted (Claude Code).

@AirKyzzZ

Copy link
Copy Markdown
Contributor

Tested c17973086 on RTX 2060 (NVIDIA 566.14), Windows 10 22H2, clang-cl 23.1.6 with the MSVC 14.44 STL, RelWithDebInfo: pass.

Built xmem_test and ran ctest -R ^xmem: passes (also with /EHa added). With main's runtime/shims/xmem.cpp and this test: xmem_test: FAILED (180 checks, 7 failures), the null-address cases for XMemMapPhysicalPages and XMemMapPhysicalPagesScatter and the two "after a valid one fails without mapping the valid entry" cases.

No game files. 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.

5 participants