Skip to content

Memory: a freed sparse block is decommitted before its address is reused - #348

Merged
theaustinayers merged 1 commit into
ivall:mainfrom
Kalmarv:fix/sparse-free-decommit-first
Nov 24, 2026
Merged

theaustinayers merged 1 commit into
ivall:mainfrom
Kalmarv:fix/sparse-free-decommit-first

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

d3d12x::sparse::free gave a block's address back to the sparse windows (sparse_release) and only then decommitted it. In between, another thread could take the same address, commit it and write there, and the late decommit then took the pages away under it. That is the write access violation at the first title addresses of sparse window 0 (0x42001xxxx–0x42004xxxx) that ends cargo test -p xcl --lib now and then. FreeTitlePhysicalPages frees homes this way while other tests allocate graphics memory. A title freeing physical pages on one thread while another allocates graphics memory would hit the same thing. The block is now decommitted first and released last, as VirtualFree's own sparse path already does.

What is in it

  • sparse::free: looks the reservation up (sparse_reservation), decommits it (decommitted and VirtualFree(MEM_DECOMMIT)), then releases it.
  • Test a_freed_block_is_decommitted_before_its_address_is_reused: 4 threads, each 2000 times taking a 64 KiB block, writing both ends, reading back and freeing it. It needs no device: the sparse windows are address space only.

Tested

  • RTX 5070 Ti, NVIDIA 610.74, Windows 11 Pro 26200; MSVC stable, debug.
  • cargo test --workspace -- --test-threads=4 on ebbf377 (this PR, on main 9ab7d76): 652 passed, 0 failed, 40 ignored (the xcl library: 612 passed, 20 ignored). SKIPPED GPU tests: 2, both no wave64 (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware).
  • The new test, --exact, CPU only, under a debugger that passes first-chance exceptions on and catches the second-chance one with its thread and stack:
    • main 9ab7d76 with the test added: 10 of 10 crashed, each a write AV at 0x42001xxxx–0x42002xxxx in the test;
    • this change: 20 of 20 passed.
  • Independent run by @TheHoracio (Memory: a freed sparse block is decommitted before its address is reused #348 (comment)): AMD Radeon RX 7800 XT (RDNA3, driver 32.0.32015.7029), Ryzen 7 9800X3D, Windows 11 26300, Rust 1.100.0, debug, ebbf377 on 9ab7d76. cargo test --workspace --no-fail-fast -- --test-threads=4: 652 passed, 0 failed, 40 ignored, no SKIPPED GPU tests; the new test passes.
  • The flake as found: cargo test -p xcl --lib -- hle::kernelx (default threads), 30 runs each.
  • In 10 runs of the whole --lib suite on main d4f17ca, 2 crashed with this AV, in xmem_placeholder_split_replace_and_coalesce and console_alloc_flags_are_stripped.
  • hle::d3d12x::sparse and hle::kernelx together: 29 passed, 1 ignored, 3 of 3 runs.
  • Earlier smoke runs by @CuteBooty, RTX 3070 (NVIDIA 617.14), release, on main 9ab7d76 and this head: 918 and 916 frames, no crash or device removal (Memory: a freed sparse block is decommitted before its address is reused #348 (comment)).

Regression run

Our own runs, one after the other on the machine under Tested (RTX 5070 Ti), release builds, no uncommitted changes.

main:

xcl-smoke v1
commit: 0e946cfc68244edee1fba6e0425371f687a05ccd
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1492 (PresentX 1492)
presents per 2 s (last 3): 61 59 60
device removals: 0
crashes: 0
peak commit: 14752 MB

PR:

xcl-smoke v1
commit: ebbf3779791aff8988c4b742ba629c561928333c
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1487 (PresentX 1487)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14741 MB

Not tested

  • A title freeing sparse blocks: the smoke runs above are a regression check only.
  • With a D3D12 device: the test runs before the sparse state exists, so decommitted takes its early path. The order of decommit and release is the same either way.
  • Two threads freeing the same block at once (a caller bug): both may now decommit it before one releases it. Before, only the first got past sparse_release.
  • Wine.
  • macOS / Linux: not built.

AI use

  • Claude Code, Claude Opus 5.5: found the crash (debugger captures of the suite and of hle::kernelx), wrote the change, the test and this description; the runs above are its own.

🤖 Generated with Claude Code

d3d12x::sparse::free gave the block's address back to the sparse windows
(sparse_release) and only then decommitted it. In between another thread
could get the same address, commit it and write there; the late decommit
then took the pages away under it: write access violations at the first
title addresses of sparse window 0 (0x42001xxxx), which ended
cargo test -p xcl --lib now and then (FreeTitlePhysicalPages frees homes
this way while other tests allocate graphics memory).

The block is now decommitted first and released last, as VirtualFree's
own sparse path already does.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@throny

throny commented Nov 24, 2026

Copy link
Copy Markdown

Test report for ebbf377, merged onto main a6f5f7a (clean merge, merge commit 4a0bceb): pass, and the new test crashes without the fix.

Tested

  • Windows 11 Pro 10.0.26300, NVIDIA GeForce RTX 4080 (driver 610.88, 32-lane waves), Intel i9-12900K, 64 GB; Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug; DXC 1.9.2602.17 (Windows SDK 10.0.26100.0)
  • cargo build --workspace && cargo test --workspace -- --test-threads=4: 677 passed, 0 failed, 41 ignored (lib: 637 / 0 / 21, a_freed_block_is_decommitted_before_its_address_is_reused ok)
  • SKIPPED on this adapter: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64)
  • The new test alone, cargo test -p xcl --lib a_freed_block_is_decommitted: 10 of 10 runs pass (1 passed each, so the test is not returning early here: the sparse windows exist).
  • Negative control: with sparse::free put back to main's order (sparse_release, then decommitted and VirtualFree), the same command ends with the test process crashing in 5 of 5 runs.

Reviewed (read the whole diff)

  • sparse_reservation(p).filter(start == p) keeps the old behaviour for an address inside a block (not freed, false). The block still belongs to the caller while it is decommitted, so no other thread can be handed the address before sparse_release.
  • Two threads freeing the same block at once would both decommit and one gets false from sparse_release; with main's order the second returned before decommitting. A double free is the caller's bug either way.

Not tested

  • No regression run (scripts/smoke.ps1) on a title, no XCL_SAMPLE_* game-data tests, AMD and wave64 hardware, release build.
  • I did not catch the control's crash under a debugger, so I cannot confirm the faulting address the description gives, only that the process dies.

AI use

  • Claude Code (Claude Fable 5.2) ran the build, the tests and the control, read the diff and wrote this report. The runs above are real, on the hardware named.

@TheHoracio

Copy link
Copy Markdown
Contributor

Test report on AMD RDNA3: RX 7800 XT (16 GB), 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.

Tested: the PR head ebbf377 as it is (on its base 9ab7d76, not merged onto current main).

  • cargo build --workspace, then cargo test --workspace --no-fail-fast -- --test-threads=4: 652 passed, 0 failed, 40 ignored, no SKIPPED tests.
  • The new a_freed_block_is_decommitted_before_its_address_is_reused passes.

Not tested: a regression run on a title, a release build, and 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.

@CuteBooty

Copy link
Copy Markdown

Regression run on a retail title for ebbf377, which the description lists as pending: pass (no device removal, no crash, frames and peak commit level with main).

Tested

  • RTX 3070 8 GB (32-lane waves), NVIDIA 617.14; Ryzen 5 3600, 32 GB; Windows 11 Pro 26200; rustc 1.100.0 MSVC; release build.
  • scripts\smoke.ps1 -Game <folder> -Seconds 60 on the PR's head as it is and on main 9ab7d76, the commit it is based on, each built from a clean checkout of its commit.
  • Each build ran twice back to back and the blocks are the second runs. The first runs: 912 frames (main) and 939 frames (PR), also with no device removal or crash. The main block is one run, shared with my reports on Recompiler: pixel comparison samples apply their LOD bias #349 and Recompiler: comparison samples use their supplied pixel gradients #350.
  • Video memory at 60 s, from XCL's memory: line: 6311 MB local / 4619 MB non-local (main), 6248 MB local / 4619 MB non-local (PR).

main:

xcl-smoke v1
commit: 9ab7d763197c7f420bff10cd9bf1310f07e2a9aa
gpu: NVIDIA GeForce RTX 3070
title: game
seconds: 60
exit: 0x0
frames shown: 918 (PresentX 918)
presents per 2 s (last 3): 40 41 40
device removals: 0
crashes: 0
peak commit: 14793 MB

PR:

xcl-smoke v1
commit: ebbf3779791aff8988c4b742ba629c561928333c
gpu: NVIDIA GeForce RTX 3070
title: game
seconds: 60
exit: 0x0
frames shown: 916 (PresentX 916)
presents per 2 s (last 3): 40 41 40
device removals: 0
crashes: 0
peak commit: 14732 MB

Not tested

  • The test suite on this head (others have posted it), anything past the first 60 s, and whether the title frees and reuses sparse blocks from several threads in these 60 s (the case this changes).

AI use: Claude Code with Claude Fable 5.2 made the runs with me present and wrote this comment.

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