Skip to content

D3D12.X: read_committed survives concurrent decommit and unmap - #454

Closed
kxiserino wants to merge 2 commits into
xcl-dev:mainfrom
kxiserino:fix/title-textures-read-committed-race
Closed

kxiserino wants to merge 2 commits into
xcl-dev:mainfrom
kxiserino:fix/title-textures-read-committed-race

Conversation

@kxiserino

@kxiserino kxiserino commented Nov 24, 2026 •

Copy link
Copy Markdown

What is in it

  • Replace raw std::ptr::copy_nonoverlapping in hle::d3d12x::title_textures::read_committed with copy_readable_range backed by ReadProcessMemory on the current process so concurrent physical page decommits / unmaps between VirtualQuery and the copy leave unmapped pages zeroed instead of raising STATUS_ACCESS_VIOLATION (0xC0000005).
  • On ReadProcessMemory partial failure (ERROR_PARTIAL_COPY), advance past any confirmed bytes and retry the remainder one 4 KiB page at a time so readable leading pages before a hole are preserved even when Windows reports read == 0.
  • Guard against overflow / zero-progress ranges in read_committed's VirtualQuery loop (saturating_add and end <= p check).
  • Add read_committed_skips_unmapped_holes_and_survives_concurrent_decommit covering non-page-aligned holes, direct copy_readable_range over fixed holes at aligned and unaligned offsets, and a 1,000-iteration concurrent commit/decommit stress loop.

Tested

  • Windows 11 25H2 (build 26200), AMD Ryzen 9 9950X3D, NVIDIA GeForce RTX 3090 (driver 32.0.16.1714, wave32-only), DXC 1.9.2609.5.
  • cargo test -p xcl --lib read_committed_skips_unmapped_holes_and_survives_concurrent_decommit: 11/11 consecutive runs passed (1,000 concurrent iterations each + fixed-hole assertions at +0, +0x7b, +0x104d).
  • cargo test -p xcl --lib title_textures: 8 passed, 0 failed.
  • cargo test --workspace -- --test-threads=4 (lib, on base 2018c67): 636 passed, 1 failed (hle::kernelx::tests::xmem_virtual_query_marks_cpu_readable_memory, pre-existing failure in this environment; confirmed passing on RDNA3/Blackwell by reviewers), 21 ignored; 2 GPU tests reported SKIPPED: gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask: no wave64 and gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware: no wave64.
  • cargo test --workspace --test "*" -- --test-threads=4: 40 passed, 0 failed across all 12 integration test binaries.

Regression run

xcl-smoke v1
commit: a6f5f7a74cc620c1f88a9e3669a633d10c3c8212
gpu: NVIDIA GeForce RTX 3090
title: Game
seconds: 30
exit: 0x0
frames shown: 626 (PresentX 626)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14471 MB
xcl-smoke v1
commit: 1b021625defa5e5f939e4fcf123450097ae2a34a
gpu: NVIDIA GeForce RTX 3090
title: Game
seconds: 30
exit: 0x0
frames shown: 622 (PresentX 622)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14454 MB

Not tested

  • Wave64-capable GPUs (AMD / Intel) locally — RTX 3090 is wave32-only (RDNA3 & RDNA4 tested in comments).
  • hle::kernelx::tests::xmem_virtual_query_marks_cpu_readable_memory failed in this environment (unrelated to this PR).

AI use

  • Implemented, tested, and verified with Antigravity (Gemini 4 Neon).

title_textures::read_committed checked VirtualQuery and then ran raw copy_nonoverlapping over the returned region. When a streaming thread concurrently unmapped or decommitted pages (e.g. MapTitlePhysicalPages / VirtualFree) mid-copy, xcl.exe faulted with 0xC0000005 in VCRUNTIME140.dll!memcpy.

Use ReadProcessMemory on non-coherent regions and skip past any concurrently unreadable 4 KiB page (leaving it zeroed) instead of faulting. Add a unit test exercising holes and concurrent commit/decommit.

Assisted-by: Antigravity (Gemini 4 Neon)
@PHeitor1

Copy link
Copy Markdown

Tested 2018c67 merged onto main 2997c9a.

RTX 5060 Ti 8 GB (Blackwell, 32-lane waves), NVIDIA 621.24, Ryzen 5 5600X, Windows 11 Pro 26300 (native, Graphics Tools + XGameRuntime installed, so the debug-layer tests run), rustc 1.100.0 MSVC, debug, cargo test --workspace --no-fail-fast -- --test-threads=4, no XCL_SAMPLE_*.

Pass: 677 passed, 0 failed, 41 ignored. SKIPPED: only the 2 no wave64 tests (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware). Build clean.

Run and written with Claude Code (Claude Opus 5.5).

@TheHoracio

Copy link
Copy Markdown
Contributor

Test report on AMD RDNA3: RX 7800 XT (16 GB, wave32 and wave64), driver 32.0.32015.7029, Ryzen 7 9800X3D, Windows 11 Pro 10.0.26300, Rust 1.100.0 MSVC, DXC 1.9.2602.17, debug build. No retail data was used. The PR's own runs are all on NVIDIA, so this adds an AMD GPU with 64-lane waves.

Tested: the PR head 2018c67 as it is (on its base a6f5f7a).

  • cargo build --workspace, then cargo test --workspace --no-fail-fast -- --test-threads=4: 677 passed, 0 failed, 41 ignored, no SKIPPED tests (the two no wave64 tests run here).
  • The new read_committed_skips_unmapped_holes_and_survives_concurrent_decommit passes. xmem_virtual_query_marks_cpu_readable_memory, which failed in your environment, passes here.

Not tested: a regression run on a title, a release build, the head merged onto current main.

AI use: Claude Code ran the commands and drafted this comment; the runs are real runs on the hardware above.

@4usphs

4usphs commented Nov 24, 2026 •

Copy link
Copy Markdown

An RDNA4 run, next to the RDNA3 one above. The crash is fixed, but the page skip zeroes readable pages, and the new test catches that now and then: it failed 2 of 50 runs here.

Tested: 2018c67 on RX 9070 XT (RDNA4, wave64), driver 32.0.32029.2010, Ryzen 7 9800X3D, Windows 11 Pro 26200, Rust 1.100.0, debug.

  • cargo test --workspace -- --test-threads=4: 677 passed, 0 failed, 41 ignored, 0 SKIPPED. Since a6f5f7a, main (2997c9a) changes only README.md, so this is the same code as the merge.
  • Negative control: with only the copy_readable_range call put back to copy_nonoverlapping, the new test crashes 5 of 5 runs (0xc0000005).
  • The new test alone, on the clean head, 50 runs: 48 passed, 2 failed, both at title_textures.rs:1705 (buf[..4 * PAGE] not all 0x5A). Pages 0–3 are never written after setup, so the zeros come from the skip.

Why. On this Windows build, when ReadProcessMemory reaches an unreadable page it returns ERROR_PARTIAL_COPY (299) and usually read = 0, not the count up to the bad page. copy_readable_range then moves on one page at a time from start, and each retry fails until it is past the hole. The pages it stepped over stay zero.

  • A fixed hole: pages 0–3 committed, 4–5 reserved, 6–7 committed. copy_readable_range over all 8 pages returns pages 0–3 as zero (0x4000 bytes; the same with starts at +0x7b and +0x104d). Pages 6–7 are correct.
  • The test's own layout: while the middle is committed, VirtualQuery(base) reports one 32-page region, so pages 0–3 are copied in the same call as the middle. With counters added locally, over 20 000 iterations of the test's loop: about 215 ReadProcessMemory failures per run (about 50 with read > 0), and pages 0–3 came back zero in 0, 4 and 4 iterations across three runs. The 400 iterations in the test saw 0, 16 and 0 failures, so the test usually never reaches the skip at all.
  • I did not work out why most failures leave pages 0–3 intact. A guess, not checked: the bytes before the fault are already in the buffer even when read = 0.

One possible fix: when a read fails, copy that range one page at a time.

Not tested: smoke or a retail title, a release build, whether a title ever reads the zeroed pages.

AI use: Claude Code (Claude Opus 5.5) ran the tests and the local probes on my PC and wrote this comment. I have not reviewed the code; the measurements are from runs on my PC.


Update: 1b02162 on the same RX 9070 XT (RDNA4). The fix works here.

  • PR head 1b02162 as is (base a6f5f7a), cargo test --workspace -- --test-threads=4: 677 passed, 0 failed, 41 ignored, 0 SKIPPED.
  • read_committed_skips_unmapped_holes_and_survives_concurrent_decommit alone, each run its own process: 50 of 50 passed (on 2018c67 it was 48 of 50).
  • Negative control: with only the copy_readable_range body set back to 2018c67, the new test fails 10 of 10 runs at title_textures.rs:1697, leading pages zeroed at off=0x0. So the fixed-hole check catches the bug every time, not only now and then.

Overlap with #484. main now has #484 (27c3571, merged 03:38 UTC), which fixes the same crash in the same function a different way: copy_committed_region, which splits a failed range in two at page boundaries. This PR now conflicts with main in title_textures.rs. I took main beaa245 with #484's code, added this PR's test (the one direct call changed to copy_committed_region(base + off, dst, len)), and ran it: 50 of 50 passed, along with #484's two read_committed tests. So on this GPU, main already passes this PR's test, including the fixed hole and the 1 000-iteration stress loop.

Not tested: smoke or a retail title, a release build, the full suite on main + this test.

AI use: Claude Code (Claude Opus 5.5) ran the tests and the control on my PC and wrote this update. I have not reviewed the code.

When ReadProcessMemory crosses an unreadable hole after VirtualQuery, Windows can return ERROR_PARTIAL_COPY with read == 0. Advancing by a page from cur + read skipped readable leading pages and left them zero. Retry the uncopied remainder one page at a time so every readable page before and after a hole is preserved, and cover fixed holes (aligned and unaligned) directly in the unit test.

Assisted-by: Antigravity (Gemini 4 Neon)
@kxiserino

Copy link
Copy Markdown
Author

Thanks @4usphs for catching that! Fixed in 1b02162: when ReadProcessMemory fails with ERROR_PARTIAL_COPY, copy_readable_range now advances past any confirmed bytes and retries the remainder one 4 KiB page at a time so readable leading pages before a hole are preserved even when Windows returns
ead == 0. Added direct fixed-hole assertions (at +0, +0x7b, +0x104d) and bumped the concurrent stress loop to 1,000 iterations (11/11 local runs passed + fresh 30s smoke run).

@PHeitor1

Copy link
Copy Markdown

Retested the new head 1b02162 merged onto main 2997c9a on NVIDIA: RTX 5060 Ti 8 GB (Blackwell), NVIDIA 621.24, Ryzen 5 5600X, Windows 11 Pro 26300, rustc 1.100.0 MSVC, debug.

  • cargo test --workspace --no-fail-fast -- --test-threads=4: 677 passed, 0 failed, 41 ignored. SKIPPED: only the 2 no wave64 tests.
  • read_committed_skips_unmapped_holes_and_survives_concurrent_decommit alone, each run its own process: 50 of 50 passed. That is the count @4usphs used for the 2-of-50 failure on 2018c67.

Run and written with Claude Code (Claude Opus 5.5).

@raylitebull

Copy link
Copy Markdown
Member

Closing as a duplicate of #484, which has already been merged into main.

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