Skip to content

D3D12.X: title fence sync entries own their ID3D12Fence (#2840) - #2843

Draft
oni-swr wants to merge 1 commit into
xcl-dev:mainfrom
oni-swr:t3/xcl-2840-title-fences-uaf
Draft

oni-swr wants to merge 1 commit into
xcl-dev:mainfrom
oni-swr:t3/xcl-2840-title-fences-uaf

Conversation

@oni-swr

@oni-swr oni-swr commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2840

SYNC_GRAPH.title_fences kept the title's raw ID3D12Fence pointer without a reference. If the title released the fence while an entry still had waits or signals, the next prune_title_fence called GetCompletedValue on a freed object. TitleFenceSync now owns a reference taken when the entry is created, and the entry is removed (reference released) once it has no waits or signals left. The released references are dropped after SYNC_GRAPH is unlocked, so Release never runs under the global lock. XCL does not wrap title fences with a release hook (the title gets PC fences directly), so the owned reference is the fix that covers every path.

What is in it

  • crates/xcl/src/hle/d3d12x/list_x.rs: TitleFenceSync.fence, prune_title_fence(tf), take_idle_title_fence(s), title_fence_entry; note_title_queue_wait/signal and break_cycles_before_title_signal drop the released references after unlocking. Two unit tests with a synthetic COM fence (counts AddRef/Release, records a GetCompletedValue at refcount 0).
  • crates/xcl/src/hle/d3d12x/real.rs: the fake fence in queue_slot_18_before_the_layout_is_known_writes_8_bytes gets AddRef/Release slots (the graph now calls them).
  • Idle entries emptied inside would_signal_close_cycle are removed on the next wait/signal note, not in that call (it runs under a lock held by its caller).

Tested

  • Windows 11 Pro 10.0.26200 (host has an RX 7800 XT, unused for this run), rustc 1.100.0, debug. CPU-only, no GPU test was run (the GPU was in use by another job).
  • cargo check --workspace --tests -j 4: finished, no new warnings.
  • cargo test -p xcl --lib -j 4 title_fence_entry: 2 passed, 0 failed, 0 ignored.
  • cargo test -p xcl --lib -j 4 queue_slot_18_before: 1 passed, 0 failed, 0 ignored.
  • With the owned reference replaced by a non-owning copy (the old behaviour), both new tests fail at refs == 2.

Regression run
Pending: the paired scripts/smoke.ps1 runs (main and this head) and the full GPU workspace suite wait for the GPU slot. This PR stays a draft until they are pasted here.

Not tested

  • Full cargo test --workspace on a GPU, including the GPU sync-graph tests in list_x.rs (one of them builds TitleFenceSync { .., ..Default::default() }; it compiles but was not run).
  • Retail-title smoke run; NVIDIA, Linux.

AI use

  • Claude Code (Claude Sonnet 5.5): wrote the fix, the tests and this description; the author reviews before marking ready.

🤖 Generated with Claude Code

SYNC_GRAPH.title_fences stored the title's raw ID3D12Fence pointer without
a reference. If the title released the fence while an entry still had waits
or signals, the next prune_title_fence called GetCompletedValue on a freed
object (also for a new fence allocated at the same address).

TitleFenceSync now holds its own reference, taken when the entry is created.
Entries are removed once their waits and signals are empty (completed, or
u64::MAX after device removal). The removed references are dropped after
SYNC_GRAPH is unlocked so Release never runs under the global lock.

Tests use a synthetic COM ID3D12Fence that counts AddRef/Release and records
a GetCompletedValue made at refcount 0; they fail without the owned reference.
The queue_dispatch test's fake fence gets AddRef/Release slots.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@pickles976

Copy link
Copy Markdown

Independent Linux/Wine verification of 932e689cda69b29192b06772a668054f9a731efa, with a matched parent control at 8b957bec6f82495aec940944c6bf47faa02db590.

Tested: Windows MSVC target, Rust 1.98.1, Wine 11.0, single test thread, 9 GiB memory / 512 MiB swap cgroup limits and 180 s timeout. Fresh Compiling xcl output identifies the intended worktree on both legs; this matters when sharing a Cargo target directory.

cargo test --locked --target x86_64-pc-windows-msvc -p xcl --lib title_fence_entry_ -- --nocapture --test-threads=1
  • Clean PR head: 2 passed, 0 failed, 0 ignored, 0.00 s after a 29.44 s compilation. Both title_fence_entry_outlives_the_titles_release_and_is_released_when_complete and title_fence_entry_is_released_when_the_device_is_removed passed.
  • Parent production code, with only the PR's MockFence helper and first regression test transplanted: 0 passed, 1 failed, 0.01 s after a 25.39 s compilation. It fails at the first ownership assertion: the graph takes its own reference, actual refcount 1, expected 2. The control stops there; it does not deliberately dereference a freed native COM object. Temporary parent probe restored afterward.

These are CPU/mock-COM lifetime tests, not a GPU run. The mock detects accesses at refcount zero but does not physically free its backing allocation. The machine has an RTX 4060 Laptop GPU / 16 GiB RAM and a diagnostic-relaxed vkd3d installation, but neither establishes native GPU coverage for these fixtures.

Not tested: full suite, native Windows, real D3D12 fence destruction, title launch, RAM savings, or correction of our Game Init stall (cpu_writes legacy fence remains at 0/wanted 1). That symptom is not evidence against this fix, nor evidence that this fix resolves it. Pending fences may legitimately remain retained until graph activity observes completion.

Suggested additional regression: make the mock's Release callback assert it can acquire SYNC_GRAPH when pruning drops the final graph reference. The current tests prove retention/release counts; a callback check would also protect the important release-outside-lock guarantee.

Local evidence: .compat-xcl/logs/verify-pr2843-wine-fence-ownership.log and verify-pr2843-parent-wine-fence-ownership.log. No game assets used. AI-assisted verification/source review (OpenAI GPT-6 Astra).

@sage-sage

Copy link
Copy Markdown

Test report: passes on NVIDIA. Exact head 932e689c (base main 8b957be).

Tested

  • RTX 4080 SUPER (driver 621.40), Ryzen 5 7600X, Windows 11 26H2 build 26300.10075, rustc 1.100.0 MSVC, DXC 1.9.2602.17 (Windows SDK 10.0.26100), debug build
  • cargo test --workspace -- --test-threads=4: 1448 passed, 0 failed, 47 ignored; both new title_fence_entry_… tests pass
  • SKIPPED: 29 lines, the same as on main on this machine (wave64, debug layer, Developer Mode, heap windows)
  • The whole lib test binary 60 times at --test-threads=12: 2 runs failed, both on flakes main has too (a_dispatch_with_a_pipeline_still_building_waits_at_the_submit, D3D12.X: forget a command list before the runtime frees it #2691; flush_parked_does_not_free_or_reuse_tiles_while_unmap_is_in_flight, Tests: wait for every quarantine mark before draining sparse tiles #2775). No failure in list_x's sync-graph tests. main 8b957be the same way: 3 of 100 runs, all the dispatch flake.

Not tested

  • No game, so not a title releasing a fence with waits still recorded; AMD, Intel.

Claude Code (Claude Opus 5.5) ran the tests and wrote this report.

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.

D3D12.X: use-after-free on released title ID3D12Fence pointers in SYNC_GRAPH.title_fences

3 participants