Skip to content

xmem: map consecutive physical pages as one view, so the retail boot's 100,000-page XMemMapPhysicalPages fits the title's hang watchdog under Wine - #7584

Closed
gkissel wants to merge 10 commits into
ProjectLeonida:mainfrom
gkissel:fix/xmem-physical-page-runs
Closed

gkissel wants to merge 10 commits into
ProjectLeonida:mainfrom
gkissel:fix/xmem-physical-page-runs

Conversation

@gkissel

@gkissel gkissel commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: RT-671 (#8504 (comment))

What and why

XMemMapPhysicalPages (#6919) maps each 64 KiB page as its own view: a placeholder split plus a MapViewOfFile3 per page. Under Wine that is slow, and it gets slower as views pile up. On the retail boot, one call maps 99,776 pages into a single window (counted with a temporary log, not part of this PR):

  • the first 65,536 pages took 28.6 s, about 0.36 ms a page, and each block of 16,384 pages took longer than the one before (5.9, 6.3, 8.0, 8.4 s);
  • the title has a hang watchdog that ends the process when its main thread hasn't checked in for 30 seconds. The mapping call is still running when it fires, so every run stops there.

The smaller calls before it (16 to 1,600 pages) were each one contiguous run of a single allocation, and so is this one.

The change:

  • Consecutive pages of one allocation (same section, consecutive offsets) are mapped as one view. g_mapped (address → page) becomes g_views (view base → page count, section, first offset, protection).
  • Unmapping or remapping a page inside a larger view splits it. The view is unmapped whole (the placeholder is kept), and the pages on either side are mapped again from the same section and offsets, so they keep their contents. The view holds a reference to the section, so this works even if the pages were freed while mapped. XMemFreePhysicalPages already allows that.
  • XMemMapPhysicalPages unmaps its range first, then maps runs. One behaviour change: when a page number isn't a live page, the pages before it stay mapped as before, but the rest of the range is now left unmapped, not left as it was. The measured callers only test the result for zero.
  • The scatter function and a 0 page entry go through the same split path.

Tested

Linux, Wine 11.21, clang-cl + xwin cross build, GTX 1650 Mobile, own copy of the retail executable (outcomes only):

Related

#7492 and #7499 (cache modifiers in the view protection) touch view_protect, not this code. They should merge with this cleanly, and the integration run above had the same change applied locally.

AI-assisted (Claude Code). No game files, bytes, disassembly or addresses in this PR.

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. (Not applicable: no translator change.)

…MemMapPhysicalPages fits the title's hang watchdog under Wine

One view per page cost a placeholder split and a MapViewOfFile3 per 64 KiB page. Under Wine that took
0.36 ms a page and more as views accumulate; the retail boot's 99,776-page call ran past the title's
30-second hang watchdog. Consecutive pages of one allocation now map as one view. A page remapped or
unmapped later splits its view and the neighbours are mapped again from the same section.
Copilot AI balanced review requested due to automatic review settings November 23, 2026 17:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@MartinIndra02

Copy link
Copy Markdown
Contributor

Independent Windows test of 5ba2a7f95: 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.

@aslan2142

Copy link
Copy Markdown
Contributor

Independent timing of XMemMapPhysicalPages under Wine 11.22 on Linux (build: clang for mingw; synthetic calls, no game code). Consecutive pages, mapped in one call:

Pages main (bf85ff703, one view per page) this PR's xmem.cpp
1,000 17 ms 0.1 ms
10,000 338 ms 0.6 ms
99,776 (the boot's count, per cynbun) 21.7 s 10.4 ms

At 99,776 pages that's the difference cynbun hit under Wine 11.20: on main the title's startup watchdog ends the process before mapping finishes (#7599 (comment)).

AI-assisted (Claude Code, Claude Opus 5.5).

@dotaku22 dotaku22 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reproduced a remapping regression on native Windows 11 build 26200, MSVC 19.44, Windows SDK 10.0.26100, RelWithDebInfo. This used CPU-only synthetic memory.

In test_physical_pages, after the existing check that the four pages are reserved again, call api.map(window, kPages, pages) a second time. The existing middle-page unmap and remap leave the range split into separate host placeholders. The second whole-run map should return window and preserve the page contents.

  • Parent 203a348cf with the same added check passes, including readback of the first and last pages.
  • PR 5ba2a7f95 returns null on the second whole-run map with Win32 error 87 (ERROR_INVALID_PARAMETER). The stock checks pass.
  • With the inline suggestion, the entire augmented xmem CTest passes, including readback, cleanup, and the existing assertion that a fresh contiguous run uses a single view.

The coalesced size must fit one matching placeholder for MEM_REPLACE_PLACEHOLDER. Microsoft's MapViewOfFile3 documentation

AI-assisted (Codex).

Comment thread runtime/shims/xmem.cpp Outdated
Comment on lines +438 to +446
bool map_view(uintptr_t address, const View& view) {
const SIZE_T size = static_cast<SIZE_T>(view.count * kPhysicalPageSize);
void* const target = reinterpret_cast<void*>(address);
if (!isolate_placeholder(target, size)) return false;
if (!placeholder_api().map_view3(view.section->handle, GetCurrentProcess(), target, view.offset, size,
MEM_REPLACE_PLACEHOLDER, view.protect, nullptr, 0)) {
return false;
}
g_mapped.erase(it);
g_views.emplace(address, view);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A guest run can span several host placeholders after a partial unmap. Splitting the mapping at the host boundaries fixed the reproduction locally. A fresh placeholder still maps as one view.

Suggested change
bool map_view(uintptr_t address, const View& view) {
const SIZE_T size = static_cast<SIZE_T>(view.count * kPhysicalPageSize);
void* const target = reinterpret_cast<void*>(address);
if (!isolate_placeholder(target, size)) return false;
if (!placeholder_api().map_view3(view.section->handle, GetCurrentProcess(), target, view.offset, size,
MEM_REPLACE_PLACEHOLDER, view.protect, nullptr, 0)) {
return false;
}
g_mapped.erase(it);
g_views.emplace(address, view);
bool map_view(uintptr_t address, const View& view) {
// Previous partial unmaps can leave several host placeholders in one guest run.
for (uint64_t mapped = 0; mapped < view.count;) {
const uintptr_t base = address + mapped * kPhysicalPageSize;
void* const target = reinterpret_cast<void*>(base);
MEMORY_BASIC_INFORMATION info{};
if (!VirtualQuery(target, &info, sizeof(info)) || info.State != MEM_RESERVE ||
info.RegionSize < kPhysicalPageSize) {
SetLastError(ERROR_INVALID_ADDRESS);
return false;
}
const uint64_t available = info.RegionSize / kPhysicalPageSize;
const uint64_t remaining = view.count - mapped;
const uint64_t count = available < remaining ? available : remaining;
const SIZE_T size = static_cast<SIZE_T>(count * kPhysicalPageSize);
const uint64_t offset = view.offset + mapped * kPhysicalPageSize;
if ((info.AllocationBase != target || info.RegionSize != size) &&
!VirtualFree(target, size, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER)) {
return false;
}
if (!placeholder_api().map_view3(view.section->handle, GetCurrentProcess(), target, offset, size,
MEM_REPLACE_PLACEHOLDER, view.protect, nullptr, 0)) {
return false;
}
g_views.emplace(base, View{count, view.section, offset, view.protect});
mapped += count;
}

After a partial unmap or remap, a run of consecutive pages can lie across several host placeholders, and
MEM_REPLACE_PLACEHOLDER needs the view to fill one placeholder exactly, so mapping the whole run again failed with
ERROR_INVALID_PARAMETER. map_view now maps one view per placeholder the run spans; a fresh window is still one view.
Found and fixed by dotaku22 in review; xmem_test gains their check (the whole run maps again over a split range).
@gkissel

gkissel commented Nov 23, 2026

Copy link
Copy Markdown
Author

@dotaku22 thanks, confirmed and fixed in 4a7bfd5c4.

  • Your check reproduces under Wine 11.21 too. With it added to test_physical_pages on 5ba2a7f95, the result is FAIL a run maps again over a range an earlier partial unmap split, xmem_test: FAILED (179 checks, 1 failure).
  • map_view now maps one view per host placeholder the run spans, as in your suggestion. One simplification: the split goes through the existing isolate_placeholder, which already skips the VirtualFree when the placeholder matches the range exactly. A fresh window is still one view, and the existing "consecutive pages … one view" check still passes.
  • The test keeps your check, plus an unmap of the remapped run.
  • After the change: xmem_test: OK (179 checks) under Wine 11.21 (clang-cl + xwin). check_no_game_data.py exits 0.

AI-assisted (Claude Code).

@throny throny 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 4a7bfd5 on Windows 11, MSVC 14.44, i9-12900K (no SSE4a/CLZERO/MONITORX, has CLWB): pass, with one cost to weigh

  • Build (CMake + Ninja, RelWithDebInfo, -j 6): OK. xmem_test: OK (179 checks). Full ctest -j 6 with the Vulkan driver hidden: 3 of 271 failed, all three at vkCreateInstance because I hid the driver (same at every head I tested today).
  • Native timing (scratch harness linking this head's xmem.cpp, and the merge-base 203a348's, through rt::shims::find; not pushed). A single XMemMapPhysicalPages of consecutive pages takes 16,384 pages: 205.6 → 0.7 ms, and 99,776 pages (the boot's count): 480.7 → 6.3 ms.
  • Contents correct on both builds at both sizes. After a scatter-unmap of 3 pages inside the run, the neighbours keep their contents. Remapping the whole range in reversed page order over the split views shows every page's own marker, and so does remapping in the original order. The final unmap leaves the range reserved again.
  • Cost: unmap_range (runtime/shims/xmem.cpp:469) splits a run by unmapping it whole and remapping both sides. With 99,776 pages, XMemMapPhysicalPagesScatter unmapping 3 single pages inside one run takes 96.3 ms here, versus 0.1 ms on main: about 32 ms per page, growing with the run's size. A title that later frees or replaces single pages inside a big boot-time run pays that per page. A reversed remap of the full range is also slower (842 vs 600 ms). Not a blocker for the boot, which this fixes; worth a follow-up if per-page unmaps show up after boot.

AI-assisted.

…cheap

unmap_range splits a view by unmapping it and remapping both sides; with one view per run
that cost grew with the run (review on ItsNotRexed#7584). Capping views at 64 MiB bounds it.
@gkissel

gkissel commented Nov 23, 2026

Copy link
Copy Markdown
Author

Thanks, good catch. Pushed b170ab9: map_view now caps each host view at 1024 pages (64 MiB), so a single-page unmap inside a long run only remaps the rest of one capped view.

Measured under Wine 11.21 (Linux, Ryzen 7 5800H), with temporary timing in xmem_test (removed before the commit), 2 runs each, on a 99,776-page run:

view cap map whole run unmap 3 single pages remap whole run
none (previous head) 11–15 ms 21–26 ms 12–21 ms
256 pages 74–77 ms 0.3 ms 32 ms
1024 pages (pushed) 26–27 ms 0.5 ms 15–19 ms

1024 keeps the boot map close to the uncapped time and makes the split cost flat. A new check (test_long_run: 1,100 pages, region size, contents after a single-page unmap) fails without the cap and passes with it. xmem_test: OK (186 checks) under Wine. I haven't measured it on native Windows; your numbers there would be welcome.

@morodirule

Copy link
Copy Markdown

Independent check of the approach on Arch Linux + Wine 11.22 (clang-cl 23.1 + xwin cross build): full xmem_test green (186 checks), and a temporary 8192-page map/unmap benchmark (removed afterwards) gives map 0.6 ms / unmap 0.0 ms on this head — same order as the reporter's real-boot numbers, ~4 orders of magnitude under the per-page path (my own measurement of the old path on the same box: 570 ms / 542 ms). The 1024-page cap + per-placeholder spans read as the right call vs a single whole-run view: unmap cost stays bounded and fragmented placeholders are handled without a fallback path. No game files (synthetic pages only). AI-assisted.

@morodirule

Copy link
Copy Markdown

Independent Linux check on Arch + Wine 11.22 (clang-cl 23.1 + xwin): xmem_test 186/186 OK. Plus a head-to-head benchmark of the run approach (temporary 8192-page map/unmap harness, removed after): map 0.6 ms, unmap 0.0 ms here, vs 570 ms / 542 ms for the per-page path measured the same way. Same order as the reporter's 28.6 s real-boot figure, confirming the approach on a second machine. The per-placeholder spans + 1024-page cap are the right call over a single whole-run view where placeholders are fragmented; closing my duplicate #8095 in favor of this one. No game files. AI-assisted.

@isfcrctfs

Copy link
Copy Markdown

Tested b170ab9 on Linux + Wine 11.22-staging (clang-cl 23.1.5 + xwin cross build), 16 cores, no GPU. The head applied cleanly to main@12806e56; xmem_test: OK (186 checks). Negative control (this head's tests against main's xmem.cpp): FAILED (186 checks, 2 failures) — the one-view check and the 1024-page cap check. The "177 checks / four new checks" in the description predates test_long_run; this head adds 13 checks.

Synthetic 64 KiB pages, consecutive pages of one allocation, mapped in one call; ms, median where more than one run (single runs for main at 65k and 99,776):

pages main (one view per page) b170ab9
4,096 296 1.1
16,384 2,818 7.1
65,536 47,000–67,000 30–56
99,776 175,369 59–87

At 99,776 pages that is 175.4 s → 60 ms; the null-array unmap of the same run goes 2,030 ms → 1.8 ms. main cannot finish that call inside the 30 s watchdog, this head returns in 60 ms.

Coverage gaps (behaviour is correct where I exercised it, so this is tests, not code): no check that a freed page number cannot be mapped again (only a never-allocated one), and none for a multi-page partial unmap or a remap inside a live run. I exercised those plus a split after XMemFreePhysicalPages while the pages are still mapped; the neighbours keep their contents (51 independent checks).

AI-assisted.

@OlaiFG7

OlaiFG7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Reviewed 28f1918, code read only (not built, no MSVC, Wine or GPU here), AI-assisted: the page arithmetic and run detection look correct. start + count*64K is guarded in xmem.cpp (~:668) and map_view / unmap_range stay inside that range; run detection compares the section and offset + count*64K, so page 0 and duplicate pages fall out as separate views. One note: unmap_range has no rollback, so if the left or right remap fails after the whole-view unmap, the pages around the range stay unmapped and the caller only sees nullptr; please say so in the doc comment (it is no worse than before). The VirtualQuery check in wait_for_split masks the protection with & 0xff, so pages with extra protect bits from #7492/#7499 still pass. The test comment about the placeholder flags now sits above test_split_under_reader / test_long_run, away from test_placeholders, and the multi-thread test is probabilistic. No blockers; no other open PR touches xmem.

@jtrudeau1530

Copy link
Copy Markdown

@gkissel One scope concern in 28f1918, from code review only (not built or run here): wait_for_split does not require the fault address to belong to a published split span before returning EXCEPTION_CONTINUE_EXECUTION.

If no split is active, or the address is outside the active span, the loop immediately breaks. The following VirtualQuery can still accept any unrelated committed, mapped RW/RO page. Thus the handler's retry decision applies beyond the views being split; for example, an unrelated continuable software-raised read AV naming a mapped readable page would be consumed rather than reaching the next handler.

Could the retry retain evidence that the address belongs to a split being completed, including the delayed-delivery case, and add a deterministic negative test for an unrelated mapped page? The concurrent-reader fixture covers the intended retry, but not this exclusion. This is separate from the previously noted remap-failure rollback limitation.

No competing implementation. AI-assisted source review (Codex).
agent: yes

@isfcrctfs

Copy link
Copy Markdown

This is the re-run promised above. While I was measuring, the branch moved to 28f19186 (the split guard for the reader-thread defect), so I tested both: 2cf0ff8b1, the head our earlier comment named, and 28f19186. Same host and bench as before: Linux + Wine 11.22-staging, clang-cl 23.1.5 + xwin cross build, 16 cores, no GPU. Clean git clone --depth 1 of current main@867d922f0 and git apply; the patched files hash to the head blobs (3a7a27d4/0ea00759 for 2cf0ff8, 6caff79f/894545f4 for 28f19186).

Checks.

  • 2cf0ff8b1: xmem_test OK (191 checks), 7 runs. Negative control (this head's xmem_test.cpp against main's xmem.cpp): FAILED (191 checks, 2 failures), consecutive pages of one allocation are mapped as one view and it is mapped as views of at most 1024 pages — the same two the 186-check head failed. This head adds five checks (186 → 191).
  • 28f19186: xmem_test OK (194 checks), 21 runs, no reader fault. Negative control: FAILED (194 checks, 2 failures), same two. It adds the split guard and three checks (191 → 194).

Mutations. One guard removed per run, rebuilt and re-run under Wine. The first five rows are the five added checks; the new head gets the same edits and gives the same target failures (counts move with its three extra checks):

guard removed xmem_test on 2cf0ff8 new check that fails
g_pages.erase in XMemFreePhysicalPages FAILED (191, 4) a freed page does not map again
unmap_range handles only the first view in the range FAILED (191, 5) a multi-page range across two views unmaps
right remainder mapped with the view's offset, not the split offset FAILED (191, 4) the pages on both sides of it keep their contents
range not unmapped before mapping FAILED (191, 3) pages mapped over the middle of a live run replace it there
left remainder offset by one page FAILED (191, 4) and the run's pages around them keep their contents
View::section shared_ptr<Section> → raw pointer OK (191, 0) —

All five new checks can fail, and the first three gaps are genuinely covered. The fourth — a split after XMemFreePhysicalPages while the pages are still mapped — is not: with the reference that keeps the section alive removed, both heads stay green. The 51-check external probe of that case also stayed green under Wine, so the guard's failure mode is not observable on this host, but nothing in test_long_run frees pages while they are mapped and then splits the view. That part of the description's claim is still unpinned.

The new split guard is real, but its test is timing-dependent. With const SplitGuard guard(base, view_end); removed from 28f19186, xmem_test still reported OK (194 checks) in 15 of 15 runs here. In 2 of those 15 the reader thread took an unhandled page fault inside the split; the process continued, reader.join() returned, and the check passed — reads > 0 and wrong == 0 still held, because the faulting read never returns to increment wrong. With the guard in place, 15 of 15 runs had no reader fault. So the guard prevents the fault, but on this host the new check does not fail the suite when the guard is removed; the author's negative control saw the race 1 in 3. It is a timing-dependent pin, not a deterministic one.

Numbers (same bench as before: 64 KiB pages, consecutive pages of one allocation, one XMemMapPhysicalPages call; ms, median of 3, single runs for main at 65,536 and 99,776):

pages main (one view per page) 2cf0ff8
4,096 388 1.19
16,384 3,958 5.73
65,536 50,626 30.4
99,776 129,520 58.2

At 99,776 pages that is 129.5 s → 58 ms (null-array unmap of the same run: 2,913 ms → 1.74 ms); main cannot finish the map inside the 30 s watchdog. xmem.cpp is byte-identical between b170ab90 and 2cf0ff8 (sha256 78ea64c5…); only the test file changed, so these are the expected 186-check-head numbers. 28f19186 adds only the split path, which a fresh run does not enter. The 1,024-page cap is not new in 2cf0ff8 — b170ab90 already had it. An uncapped scratch build maps the same 99,776 pages in 33.0 ms vs 58.2 ms capped here (about 25 ms), which is what the cap costs on the big map; the bounded split is what it buys.

The host was shared during these runs, with load varying from other builds, so treat small differences from our previous comment as noise; the main/head pair above was measured in one session.

Still uncovered: the freed-while-mapped split, and a deterministic negative control for the new split check.

Synthetic pages only, no game files.

AI-assisted.

@gkissel

gkissel commented Nov 23, 2026

Copy link
Copy Markdown
Author

@OlaiFG7 thanks for the review. Pushed 4b07516:

  • The unmap_range doc comment now says there is no rollback: if mapping a remainder again fails, those neighbouring pages stay unmapped and the caller only sees the failure.
  • The test comments now sit above their own tests: the placeholder line above test_placeholders, the capped-views lines above test_long_run, and only the split line above test_split_under_reader.
  • Agreed that test_split_under_reader is probabilistic. It can't give a false failure with the guard in place, but without the guard it doesn't always catch the fault: here the negative control faulted in 1 of 3 runs, and in 2 of 3 in aslan2142's run. I left it as a smoke test rather than add a hook to force the window open.

xmem_test: OK (194 checks) under Wine after the change.

@hazben

hazben commented Nov 23, 2026

Copy link
Copy Markdown

checked: #7584 at 28f19186d (4b075160b since only adds and moves comments, so the code is the same) | base: main@867d922f0, the PR's merge base | on: native Windows 11 26200, MSVC 14.44 + Ninja RelWithDebInfo, RTX 4070 SUPER / NVIDIA 621.14 | result: pass with no regressions, but the new race test never catches the defect on native Windows

  • Full ctest, head vs base, with LEONIDA_GAME_DIR set: 2 of 275 fail on both trees (d3d12_vulkan_device, d3d12_depth_resolve), at the same checks. No other test changes result, so the process-wide wait_for_split handler changes nothing across the suite here.
  • xmem_test: OK (194 checks) in 10 of 10 runs.
  • Negative control: 2cf0ff8b1's xmem.cpp (no guard) with this head's test. test_split_under_reader passed in 63 of 63 runs. In 50 of them the reader made about 26.5 million reads with no fault and no wrong value. The split itself does happen here: a local VirtualQuery probe shows page 11's region start at page 11 once page 10 is unmapped (1,013 pages, MEM_MAPPED). So on native Windows the smoke test never hit the window that Wine hits in 1–2 of 3 runs, and what it covers in practice is the Wine case. I didn't measure why Windows doesn't fault.

AI-assisted (Claude Code).

@gkissel

gkissel commented Nov 23, 2026

Copy link
Copy Markdown
Author

Thanks @jtrudeau1530, @isfcrctfs and @hazben. Pushed f262cde (the change) and cf69f16 (merge of current main).

Why the reader test almost never caught anything (isfcrctfs, hazben). The old loop unmapped and remapped page 10 2,000 times, but after the first iteration page 10 is its own one-page view. So only the first of the 2,000 unmaps split the view holding page 11; the other 1,999 never touched the reader's page. That is why native Windows passed 63/63 without the guard and Wine caught it 1–2 times in 3.

New test_split_under_reader. A 2,048-page run, mapped as two 1,024-page views. A reader thread alternates between pages 1023 and 2047. The main thread unmaps pages 0..2046 one at a time (skipping 1023), so each of the 2,046 unmaps splits the view whose last page is being read.

A gap in the handler that this test exposed. With splits back to back, the handler waited out split k. By the time it ran VirtualQuery, split k+1 had already unmapped the page again, so the fault went on unhandled. A title that unmaps pages one by one inside a run another thread reads would hit the same thing. The handler now reads a split counter (g_splits) before waiting. If the page is still unmapped and the counter has moved, it waits again. If the counter has not moved, the fault goes on.

Scope (jtrudeau1530). A fault is retried only when its address is inside the active split span, or inside the last finished one (for delivery after the split ends). Any other access violation goes to the next handler. Only the last finished span is kept, so a fault the handler sees only after two later splits of other spans is still passed on; the comment says so.

Deterministic checks. read_fault_reaches_caller raises a continuable read AV with RaiseException inside __try/__except:

  • naming a page in the last split span → retried;
  • naming a mapped page outside it → reaches the caller.

Freed-while-mapped split (isfcrctfs). At the end of test_long_run: four pages are mapped as one view, freed while still mapped, then one of them is unmapped. The other three must keep their memory.

Results (Wine 11.21, clang-cl + xwin): xmem_test: OK (199 checks), 14 runs, no reader fault. Mutations, one at a time, 3 runs each:

mutation result
SplitGuard removed from unmap_range the reader faults 3/3; with the reader's read stubbed out, "a read fault inside the last split span is retried" fails 3/3
span check skipped (any mapped page retried) "a read fault on a mapped page outside it reaches the caller" fails 3/3
View::section as a raw pointer "unmapping one of them afterwards keeps the others' memory" fails 3/3

Not run here: native Windows. Synthetic pages only, no game files.

AI-assisted (Claude Code).

@jtrudeau1530

Copy link
Copy Markdown

@gkissel Thanks for taking the scope report into f262cde. Reading that delta, the new mapped-outside-span RaiseException check covers the quiescent case we reported. I have not built or run this revision.

One concurrent scope gap remains: the begin/end atomics are not read as a coherent pair. For example, with two disjoint synthetic spans A=[100,200) and B=[300,400), the handler can read the old g_last_split_begin=100, then the writer publishes B's end=400, then the handler reads that new end. A mapped address 250 now passes the apparent [100,400) check despite belonging to neither split. Clearing last_begin to zero before publishing B does not prevent a reader that already loaded A's begin. Acquire/release ordering alone does not make the pair a snapshot.

The successful VirtualQuery path immediately returns CONTINUE_EXECUTION; it does not recheck g_splits or otherwise validate that the span stayed in one generation. This can still consume an unrelated continuable AV on a mapped readable page, the same observable behavior the new negative test correctly rejects when no writer races. The active begin/end pair also needs consistent-generation reads.

Could the publication/read protocol validate one stable span generation, with a deterministic test forcing a reader across publication of disjoint spans? This is an interleaving found by source review, not a reproduced runtime fault. The stronger reader test and freed-while-mapped lifetime test are useful additions; I am not claiming a duplicate fix.

AI-assisted review (Codex, with Claude preflight).
agent: yes

@gkissel

gkissel commented Nov 23, 2026

Copy link
Copy Markdown
Author

@jtrudeau1530 agreed, the two words could come from different splits. Pushed cef56a2: each span (active and last finished) is now a single std::atomic<uint64_t>. Views are 64 KiB aligned and at most kMaxViewPages (1,024) pages, so the base keeps its low 16 bits for the page count (static_assert(kMaxViewPages < 0x10000)). One load gives begin and size from the same split, so the [100,400) mix can no longer be formed.

On the success path: once the address is inside a span read as one word, it belongs to a split that really happened (active, or the last finished one), so a retry after VirtualQuery shows the page mapped with the right protection is the intended case. An unrelated AV on a mapped page outside both spans still goes on, as the negative check shows.

I didn't add a test that forces a reader across two publications: with one word per span there is no pair left to tear, and forcing that interleaving would need a test hook inside the handler. The remaining limit is the one in the comment: only the last finished span is kept.

xmem_test: OK (199 checks), 5 runs under Wine 11.21. Synthetic pages only, no game files.

AI-assisted (Claude Code).

@jtrudeau1530

Copy link
Copy Markdown

@gkissel Reviewed cef56a2 against cf69f16. Publishing each span in one atomic word addresses my specific mixed-generation begin/end finding: span_holds derives both bounds from the same loaded value.

I also checked the packing assumptions at the call site: SplitGuard receives an existing g_views entry; map_view caps each stored entry at 1,024 pages, and the physical-map entry points require 64 KiB alignment. Those invariants leave the low 16 bits available for the count, as the new static assertion requires.

This is source-review resolution of that finding, not a runtime pass or a claim that all split/delayed-delivery races are resolved. Only one finished span is still retained, as documented. I have not built or run this revision; the 199-check runs above are yours.

AI-assisted review.
agent: yes

@jtrudeau1530

Copy link
Copy Markdown

checked: cef56a2 | synthetic xmem_test | Wine 11.18, clang-cl 23.1.6 + xwin | 199/199 pass

Independent validation following the span-publication review: built only xmem_test from this exact GitHub PR head in a frozen snapshot. No game inputs or retail run. The test exits 0: xmem_test: OK (199 checks).

Negative control: a separate snapshot changes only wait_for_split by removing if (!in_split) return EXCEPTION_CONTINUE_SEARCH;. The test file is byte-identical. It exits 1 with exactly one failed check: a read fault on a mapped page outside it reaches the caller; the other 198 checks pass. The unmodified PR build passes that check.

This demonstrates that the deterministic outside-span test detects removal of the refusal. The concurrent-reader check passed in BOTH runs, so this is not a deterministic reproduction of the split race and does not replace the race coverage requested by other reviewers. Late delivery after another finished split, write-to-readonly inside the span, and an unmapped page inside the span without a new split were not separately tested here.

Execution by our cooperating Claude worker; Codex checked the logs, identical test-file hashes, binary provenance and the one-line negative-control patch. AI-assisted. No game data or new implementation claim.

agent: yes

dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
… one view, so the retail boot's 100,000-page XMemMapPhysicalPages fits the title's hang watchdog under Wine

Squashed from ItsNotRexed#7584 by gkissel, head 5ba2a7f (security scan PASS).

Merged cleanly on top of ItsNotRexed#7550 (view_protect still feeds the view's protection). Added the fix from the PR's review (dotaku22): after a partial unmap a guest run spans several host placeholders, and mapping the whole run again failed with ERROR_INVALID_PARAMETER; map_view now maps one view per placeholder. xmem_test gains that re-map check, which failed under Wine 11.22 before the fix and passes after (189 checks).
dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
…e view per host placeholder it spans)

Squashed from ItsNotRexed#7584 by gkissel, head 4a7bfd5 (security scan PASS).

Rescan of the new commit only (5ba2a7f..4a7bfd5): sec_prescan flags=0, Haiku read PASS (map_view uses VirtualQuery, isolate_placeholder and MapViewOfFile3 only). It is the author's version of the review fix the first ItsNotRexed#7584 commit on this branch carried locally; this replaces that local text with upstream's (same behaviour, isolate_placeholder instead of an open-coded VirtualFree) and takes upstream's test wording.
dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
…lAlloc with console page-size bits (what ItsNotRexed#6919 leaves)

Squashed from ItsNotRexed#4531 by labj1987, head 01fc4b1 (security scan PASS).

Rescan of the new commit only (007ae5e..01fc4b1, a test-only variable rename): sec_prescan flags=0, Haiku read PASS. Superset check: the head is already cut down to what ItsNotRexed#6908/ItsNotRexed#6919 leave (4 MiB pages, page-size bits on the title's VirtualAlloc, aligned reservations without the placeholder API) and keeps main's attribute acceptance and PAGE_GRAPHICS_* stripping, so it is taken whole. Conflict: xmem.cpp, where the PR turns XMemVirtualAlloc into a wrapper over xmem_virtual_alloc next to ItsNotRexed#7584's map_view; kept both (wrapper, then ItsNotRexed#7584's per-placeholder map_view; the old unmap_page body ItsNotRexed#7584 replaced stays gone). xmem_test under Wine 11.22: 592 checks OK.
dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
…ment and clear stale mapped-page entries

Squashed from ItsNotRexed#7712 by TillusStark26, head 4bd14d7 (security scan PASS).

Conflict: the aligned-reservation branch of XMemVirtualAlloc, where ItsNotRexed#4531 keeps the title's host flags (console_windows); kept that and added ItsNotRexed#7712's clear_window_range after it. Adapted to ItsNotRexed#7584's model: the PR clears stale entries of the per-page g_mapped table, which ItsNotRexed#7584 replaced with g_views (view base -> run), so clear_window_range now drops every g_views record that overlaps the freshly reserved range (a view the host no longer has is stale as a whole). Review note kept as is: plain fresh reservations now trim old windows instead of erasing them (low impact, flagged on the PR). xmem_test under Wine 11.22: 626 checks OK, including the PR's reuse-after-external-release case.
dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
…a whole physical-page reservation

Squashed from ItsNotRexed#7742 by ASTROWwwW, head 51736cd (security scan PASS).

Conflicts in xmem.cpp and xmem_test.cpp with ItsNotRexed#7584, ItsNotRexed#4531 and ItsNotRexed#7712. Adapted to ItsNotRexed#7584's run-of-views model: release_range unmaps every recorded view (g_views) whose base is in the range and not inside a nested reservation, via unmap_range, instead of the per-page g_mapped table. XMemVirtualAlloc's reservation branch records g_reservations and keeps ItsNotRexed#7712's record_window/clear_window_range split; the aligned and plain paths keep ItsNotRexed#4531's title flags and ItsNotRexed#7712's clearing, then note nested allocations. TitleVirtualAlloc is ItsNotRexed#4531's version (page bits through xmem_virtual_alloc) with note_nested_allocation on the plain host path. Both new test functions kept (a merged closing brace restored). xmem_test under Wine 11.22: 656 checks OK.
@byPreaZy

Copy link
Copy Markdown

checked: #7584 | with: applied the diff on main 5d74c27, built and ran xmem_test (MSVC 14.44, Ninja RelWithDebInfo) | on: Windows 11, RX 9060 XT | result: PASS — xmem_test OK (199 checks), including the grouped physical-page view cases and the placeholder carve/split checks

AI-assisted check.

@BatSmacker84

Copy link
Copy Markdown

Tested cef56a296 merged onto main 5d74c2723 on AMD Radeon RX 7800 XT, driver 26.11.2, Windows 11 26200, MSVC 19.44 + Ninja RelWithDebInfo: pass.

  • ctest -R '^xmem$': 199/199.
  • Control: the updated xmem_test against main's implementation fails the single-view, 1,024-page cap and split-span fault-retry checks, so the new tests catch what the change fixes.

AI-assisted: Codex GPT-6.2 Sol ran the builds and tests, Claude Opus 5.5 reviewed the report. Synthetic fixtures only, no game data.

@itsRevela

Copy link
Copy Markdown
Contributor

Tested cef56a29 merged onto main 867d922f on an NVIDIA GeForce RTX 3070 Ti (driver 610.88), Windows 11, MSVC 19.44 + Ninja, RelWithDebInfo: pass.

Full runtime ctest (267 tests): xmem passes (with the single-word split span and the new read_fault_reaches_caller checks), as do loader, pe_loader, pe_loader_bounds and xgameruntime from the main merge in this head. The only failures are d3d12_depth_resolve, d3d12_indirect_rt and d3d12_vulkan_device, which fail the same way on main 867d922f alone. No new failure against that main.

AI-assisted (Claude Code).

@hazben

hazben commented Nov 23, 2026

Copy link
Copy Markdown

checked: #7584 at cef56a296 | with: xmem_test only | on: native Windows 11 26200, MSVC 14.44 + Ninja RelWithDebInfo | result: pass, and the rewritten test_split_under_reader now catches a missing guard on native Windows (10 of 10 runs)

@gkissel, this follows up my 63/63 negative control on the old loop.

  • Head as is: xmem_test: OK (199 checks) in 10 of 10 runs.
  • Negative control: I removed only const SplitGuard guard(base, view_end); in unmap_range; the test file is unchanged. The process dies with an access violation (0xC0000005) in 10 of 10 runs. In a separate local build with stdout unbuffered, the last line before the crash is PASS a 2,048-page run maps for the split test in 5 of 5 runs. So the fault happens inside test_split_under_reader while the 2,046 pages are being unmapped, before its own checks report. The old one-page-view loop passed 63 of 63 here with no guard, so splitting the reader's view on every unmap is what makes the test bite on native Windows too.
  • The crash comes before the deterministic read_fault_reaches_caller checks, so this control doesn't exercise them. jtrudeau1530's Wine control above covers the outside-span refusal.

Not run: full ctest at this head (itsRevela posted an NVIDIA full run above). Synthetic pages only, 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.