Skip to content

xmem: trim windows on sub-range replacement and clear stale mapped-page entries - #7712

Open
TillusStark26 wants to merge 1 commit into
ItsNotRexed:mainfrom
TillusStark26:fix/xmem-window-trim-and-stale-map
Open

TillusStark26 wants to merge 1 commit into
ItsNotRexed:mainfrom
TillusStark26:fix/xmem-window-trim-and-stale-map

Conversation

@TillusStark26

Copy link
Copy Markdown

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

What and why

Fixes three tracking bugs in runtime/shims/xmem.cpp (record_window / XMemVirtualAlloc) when physical-page windows are partially replaced or when addresses released outside the shim are reused:

  1. Carving a sub-window out of an existing window (MEM_REPLACE_PLACEHOLDER): record_window erased every overlapping g_windows entry in full instead of trimming it, dropping the untouched prefix and suffix so subsequent XMemMapPhysicalPages calls into those ranges failed with ERROR_INVALID_ADDRESS.
  2. Committing a sub-range of an existing window (MEM_REPLACE_PLACEHOLDER | MEM_COMMIT): the committed range was never removed from g_windows, so find_window still accepted a multi-page XMemMapPhysicalPages spanning into the committed range, partially mapping earlier pages before failing.
  3. Reusing an address released externally (e.g. host UnmapViewOfFile / VirtualFree): record_window cleared g_windows but left stale entries in g_mapped. The next XMemMapPhysicalPages called unmap_page -> UnmapViewOfFile2(..., MEM_PRESERVE_PLACEHOLDER) on the fresh placeholder, failing permanently with ERROR_INVALID_ADDRESS. Likewise, reserving a plain MEM_RESERVE_PLACEHOLDER or normal MEM_RESERVE region at an externally freed window address could leave a ghost g_windows entry behind.

Added clear_window_range() (called only after the host has freshly reserved the range or isolate_placeholder has confirmed it is MEM_RESERVE, so no live mapped views exist in [base, base + size)) to split/trim overlapping g_windows entries and erase stale g_mapped entries in the range, plus synthetic regression checks in runtime/test/xmem_test.cpp.

Tested

  • Setup: Windows 11 Pro (10.0.26100 x64), MSVC 19.44.35230.0 (RelWithDebInfo, Ninja), Vulkan SDK 1.4.363.1, NVIDIA GeForce RTX 3050 (driver 621.14, GPU not used by this CPU-only test), no game files.
  • Before fix (new test against unmodified main 603d53e): xmem_test.exe failed 6 checks (prefix/suffix map & protection after sub-window carve, upfront failure across committed sub-range, and remap after external UnmapViewOfFile + address reuse).
  • After fix:
    • cmake --build <build> --target xmem_test gta6rt: PASS (0 warnings on touched files).
    • xmem_test.exe: PASS — 199 checks, 0 failures (exit 0).
    • ctest --test-dir <build> -R xmem --output-on-failure: 1/1 passed.
    • python -I scripts/check_no_game_data.py runtime/shims/xmem.cpp runtime/test/xmem_test.cpp: PASS (exit 0).
    • git diff --check: PASS (exit 0).

AI-assisted: yes

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • Not a shader translator rule change (docs/design.md contract table not affected).

@MartinIndra02

Copy link
Copy Markdown

AI-assisted review/test.

PASS on commit 4bd14d7.

  • Environment: Windows 11 x64, MSVC 19.44.35220, Windows SDK 10.0.26100.0, Vulkan SDK 1.4.363.1, NVIDIA GeForce RTX 3080 (driver 32.0.16.2124 / 621.24).
  • Build: cmake --build --target xmem_test (Ninja, RelWithDebInfo) — succeeded cleanly.
  • Test: xmem_test.exe — OK (199 checks), including the new carved sub-window, split-window commit, and external-unmap / address-reuse cases.

@Envyre

Envyre commented Nov 23, 2026

Copy link
Copy Markdown

Code read + xmem_test build of the PR head (Windows 11, VS 18, Ninja): correct for replacements; one side effect on fresh reservations.

Checked: trimming is exact (prefix and suffix kept for a middle range, a fully covered window removed; overlap test > at xmem.cpp:278, loop < end at :280), stale mapped-page entries are dropped only inside the range (:288-294), both tables change only under g_phys_mutex, and bookkeeping uses the same outward-rounded address/size the host saw, so it can't drift. A range spanning two windows fails in isolate_placeholder (:317-325) before any bookkeeping, and bookkeeping only changes after the host call succeeds. xmem_test: 199 checks pass; with the base xmem.cpp, 6 of the new checks fail (prefix/suffix mappable and protection, mapping across a committed sub-range, remap at a reused address), so they guard it.

Side effect (:285-286, :299-301): plain fresh reservations now trim too instead of erasing. If window [A, A+4P) was released outside the shim and the title then reserves [A+P, A+2P), the base code dropped the whole old window; now [A, A+P) and [A+2P, A+4P) survive as windows and a mapped-page entry at A+3P stays. Low impact (a later map there fails in isolate_placeholder), but trimming only on MEM_REPLACE_PLACEHOLDER and erasing whole overlapping windows on a fresh reserve would keep the old behaviour there. Also every plain MEM_RESERVE now takes g_phys_mutex and scans g_mapped (:457-460), negligible at the title's call counts.

AI-assisted (Claude Code).

@Racurs156

Copy link
Copy Markdown

Independent AI-assisted check on Windows 11 build 26200 with llvm-mingw Clang 23.1.6, CPU-only. At 4bd14d7 the suite passed 199/199. Running this PR's tests against main dde82f2 reproduced six failures. My build used only the test harness's two standard filesystem path conversions from #7823.

I also combined this head with #7742 f432402 on dde82f2. The three-way merge conflicts in XMemVirtualAlloc's reservation branch and in the two test insertion points. Keeping both the reservation registration and window clearing in that branch and retaining both test functions produced 217/217 passing checks.

@Jadiac5

Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 4bd14d7 on Windows 11, AMD Radeon RX 9070 XT (RDNA4), driver 32.0.32029.2010: pass

  • Built xmem_test with MSVC 19.44 (RelWithDebInfo, native Windows, no game files) and ran it: OK, 199 checks. No GPU is used by this test.
  • Negative control: put runtime/shims/xmem.cpp back to the merge base and kept the new tests. The test fails: 195 checks, 6 failures (prefix and suffix of a carved sub-window stay mappable, both keep the original protection, a mapping across a committed sub-range fails up front, mapping at a reused address). Restored the file and it passes again.
  • Read the diff: every clear_window_range call site holds g_phys_mutex.
  • Not done: no run together with xmem: the title's VirtualFree releases a whole physical-page reservation #7742 and xmem: a mapped physical page keeps its window's PAGE_NOACCESS #7693, which touch the same file in other functions. Not run against the retail executable.

AI-assisted (Claude Code).

@memorypressure

Copy link
Copy Markdown

Tested head 4bd14d7 vs main dde82f2 (merged tree 658148e43, clean). C++/Windows — not runnable here; CI is the execution gate, and the branch reports NO checks — this address-tracking product change must not land on an empty list. Review: the g_phys_mutex contract holds at all five clear_window_range call sites (XMemVirtualAlloc's replace branch takes the lock before isolate_placeholder), the start+size overflow clamp is correct, and trimming prefix/suffix for sub-range replacement is the right fix; three-scenario test lands in the existing xmem_test target. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run, C++ review-only).

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.

6 participants