Skip to content

xmem: a physical-page map, scatter or free that fails on a bad entry changes nothing - #7580

Closed
ASTROWwwW wants to merge 1 commit into
ProjectLeonida:mainfrom
ASTROWwwW:fix/xmem-map-validate
Closed

ASTROWwwW wants to merge 1 commit into
ProjectLeonida:mainfrom
ASTROWwwW:fix/xmem-map-validate

Conversation

@ASTROWwwW

Copy link
Copy Markdown

Claimed in the coordination issue as: #7496 (comment) (new RT item, a bug #6919 brought into main)

What and why

Since #6919, XMemMapPhysicalPages maps page by page, and map_page unmaps an address before it looks up the new page number. A call with a valid page followed by an unknown one returns ERROR_INVALID_PARAMETER, but the first address is already remapped and the second unmapped, so the title's next read there faults. XMemMapPhysicalPagesScatter has the same order, and an unaligned entry failed only after the entries before it were unmapped. XMemFreePhysicalPages freed the entries before an unknown or repeated one.

All three now check every address, window and page number first and change nothing when one is bad. A host failure part way through a map (MapViewOfFile3 refusing) is left as it was: #7331 rolls that back, but it is stacked on #6995, which no longer applies to main.

Found by building #6995's xmem_physical_test against main's xmem.cpp: #7154 (comment). That test's "the mapping it would have replaced is untouched" check passes with this change. Not in scope: the view protection (#7492 / #7499 / #7550), the commit charge (#7556), the title's VirtualFree on a physical window.

Tested

Windows 11 Pro 26300, MSVC 2022 BuildTools, RelWithDebInfo, Intel Core Ultra 9 285K, RTX 5080 (no GPU test involved). No game files.

  • xmem_test: 6 new checks fail on main 5eb8f1d (183 checks, 6 failures) and pass with this change (183 checks, OK).
  • Full ctest -C RelWithDebInfo: 111/111 pass (GPU and game-file tests skip without a Vulkan SDK or LEONIDA_GAME_DIR), the same as on main.
  • python -I scripts/check_no_game_data.py on both files: clean.

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

…changes nothing

Since ProjectLeonida#6919, XMemMapPhysicalPages maps page by page and map_page unmaps an
address before it looks up the new page number. A call with a valid page
followed by an unknown one returned ERROR_INVALID_PARAMETER but left the
first address remapped and the second unmapped, so the title's next read
there faults. XMemMapPhysicalPagesScatter had the same order, and an
unaligned entry failed only after the entries before it were unmapped.
XMemFreePhysicalPages freed the entries before an unknown or repeated one.

All three now check every address, window and page number first and
change nothing when one is bad. A host failure part-way through a map is
unchanged (ProjectLeonida#7331 covers rolling that back).

xmem_test: 6 new checks fail on main and pass here (183 checks).

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

Copy link
Copy Markdown
Contributor

Independent Windows test of c09b9f3c7: pass.

Setup: Windows 11 x64, MSVC 19.44.35220 (RelWithDebInfo, Windows SDK 10.0.26100.0), no game files.

  • xmem_test.exe: PASS (exit 0).

AI-assisted review/test.

@SkillIssueXO

Copy link
Copy Markdown

Native Windows integration test of c09b9f3c7 with #7492. Both #7492 commits (7e8e3c1db and 954fcb106) cherry-picked cleanly onto this exact head.

I linked the same combined xmem_test against three versions of xmem.cpp and ran each three times.

Shim Result on every run
Parent 1bd565fc 183/190, the six map/scatter/free assertions plus the cache-modifier assertion failed
#7580 c09b9f3c7 189/190, only the cache-modifier assertion failed
#7580 + #7492 190/190

Windows 11 build 26300, LLVM-MinGW Clang 23.1.6. The test copy uses path.c_str() in its two ifstream calls for libc++ portability. All three shim variants were compiled without local edits beyond the two cherry-picks in the combined variant. Synthetic pages only. AI-assisted testing (Codex).

@DeSunShine

Copy link
Copy Markdown
Contributor

Tested c09b9f3c7 merged into main dde82f27e 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|tiny_game" --output-on-failure: 100% passed out of 11 (xmem and the run_tiny_game* tests).

AI-assisted (Claude Code).

@scryptxz

scryptxz commented Nov 23, 2026 •

Copy link
Copy Markdown

Tested c09b9f3 on RTX 5060, Windows 11, MSVC 2022 RelWithDebInfo: its xmem_test passes (183 checks). I also ran 9 refusal checks of my own against its xmem.cpp, and all pass. They cover a bad page after a good one, a bad page over a mapped page, a scatter with an unknown page, a scatter entry outside any window, and a 0 entry before a refused one.

AI-assisted.

@NolanCotter

Copy link
Copy Markdown
Contributor

Reviewed and tested on its head (clang-cl 20 + xwin cross-build, Wine): xmem_test OK, 183 checks — all six new atomicity assertions pass.

Correctness reads clean:

  • Validate-then-mutate is the right structure in all three spots. known_page(0) returning true keeps the page-0-unmaps entry working, while XMemFreePhysicalPages rejects 0 explicitly (a 0 in a free array is a bug, not an unmap).
  • The windows[] pre-lookup in XMemMapPhysicalPagesScatter is safe: find_window hands out std::map node pointers, stable under the non-window mutations map_page/unmap_page make, and only dereferenced for nonzero pages.
  • Stored-host-failure semantics deliberately unchanged (partial map on MapViewOfFile3 refusing) — matches the doc comment.

Merge note: test-merged on top of #7742 (the VirtualFree reservation-release PR, same file) — merges cleanly, different functions. No ordering constraint between them.

AI-assisted (Devin); no game files used.

@Kerwinss

Copy link
Copy Markdown

Tested c09b9f3 on Windows 11 Pro 10.0.26300, MSVC 14.51 (VS Build Tools 2026), Windows SDK 10.0.26100, NVIDIA GeForce RTX 4060 / 621.24, Khronos Vulkan headers via -DLEONIDA_VULKAN_HEADERS_FETCH=ON: pass, no regression.

Merged onto main 867d922 (clean), full runtime build, ctest -C RelWithDebInfo -j4: 237/239, same as main alone on this GPU; the two failures are main's own d3d12_depth_resolve (#7985) and d3d12_vulkan_device:6574; xmem passes. Also merged together with #7742 (same file): no conflicts, same 237/239, xmem passes.

Code read: map, scatter and free now check every entry (known_page, the window lookup and duplicates in free) before changing anything, so a bad entry no longer leaves the first pages remapped or freed. The scatter path keeps the windows it looked up for the second pass, so it does no extra find_window calls.

Picked from the "No game copy" list in #7154. AI-assisted (Claude Code). No game files.

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.

8 participants