Skip to content

D3D12.X: forget command list before its final release - #2852

Open
guinpereira wants to merge 1 commit into
xcl-dev:mainfrom
guinpereira:fix/cl-release-forget
Open

guinpereira wants to merge 1 commit into
xcl-dev:mainfrom
guinpereira:fix/cl-release-forget

Conversation

@guinpereira

@guinpereira guinpereira commented Nov 26, 2026 •

Copy link
Copy Markdown

Fixes #2690.

Forgets list/bind/dred state before the runtime frees the list, closing the address-reuse window.

What is in it

  • crates/xcl/src/hle/d3d12x/real.rs: forget command list state before final release.

Tested

  • Windows 11, NVIDIA GeForce RTX 5060 Ti, Rust stable (x86_64-pc-windows-msvc), debug build.
  • cargo test -p xcl --lib -- --test-threads=4: 1315 passed, 4 failed (GPU OOM in dxil fuzz tests, environment), 27 ignored.
  • cargo build -p xcl: clean.

Regression run

xcl-smoke v1
commit: 05adb5a9cfc718119c0c9c097365983e67982d05
gpu: NVIDIA GeForce RTX 5060 Ti
title: gta6
seconds: 60
exit: 0x0
frames shown: 1043 (PresentX 1043)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15536 MB

Reference run on the author's local main (05adb5a). A run on ivall/xcl main and on this PR's head commit is still pending (cold shader cache crashes the title's startup precompile on this machine).

Not tested

  • Smoke on ivall/xcl main baseline and on this PR's head commit (pending, see above).
  • The address-reuse race itself not reproduced.

AI use
OpenCode, Beam Free - drafted the change; smoke run and review by the author.

@pickles976

Copy link
Copy Markdown

69ebece8a98ae956556eb02d3721db712501e9b2 introduces an unmatched AddRef on every nonfinal cl_release call.

In crates/xcl/src/hle/d3d12x/real.rs, the new code executes add(this) unconditionally, but only executes the compensating f(this) inside if last. Starting with two references: AddRef returns 3, last is false, the sole Release returns 2. The caller's reference was never consumed. Repeated calls remain at two rather than reaching destruction. This needs correction before considering the change a memory-lifetime fix.

Minimal local test, inserted in the existing readback_routing_tests module on both PR and parent production code:

#[test]
fn lab_command_list_nonfinal_release_consumes_one_reference() {
    use std::sync::atomic::{AtomicU32, Ordering};
    #[repr(C)]
    struct MockList { vtable: *const usize, refs: AtomicU32 }
    unsafe extern "system" fn add(this: *mut c_void) -> u32 {
        (*(this as *mut MockList)).refs.fetch_add(1, Ordering::SeqCst) + 1
    }
    unsafe extern "system" fn release(this: *mut c_void) -> u32 {
        (*(this as *mut MockList)).refs.fetch_sub(1, Ordering::SeqCst) - 1
    }
    let original = [0, add as usize, release as usize];
    let extended = [original.as_ptr() as usize, 0];
    let mut list = MockList {
        vtable: unsafe { extended.as_ptr().add(1) },
        refs: AtomicU32::new(2),
    };
    let left = unsafe { cl_release((&raw mut list).cast()) };
    assert_eq!((left, list.refs.load(Ordering::SeqCst)), (1, 1));
}

The mock uses orig_fn's actual original-vtable prefix layout and calls production cl_release; it does not substitute release-wrapper logic or require a D3D12 device. It does not physically free its own backing allocation.

At minimum, balance the temporary AddRef on every branch, and retain the nonfinal-reference regression. That alone is not proof of a complete #2690 fix: reference-count sampling is not exclusive finalization. The comment says concurrent last-but-one releases fall back to cleanup afterward, but the new code contains no such post-release cleanup. Please also cover concurrent final releases/address reuse with an ownership protocol; do not treat an AddRef return value as synchronization. The existing single-reference DMA-release fixture cannot exercise this ordinary two-reference leak.

No title launch, GPU destruction, driver validation, or measured host-RAM delta. AI-assisted verification/source review (OpenAI GPT-6 Astra).

Matched execution result (Wine 11.0 / Rust 1.98.1, Windows MSVC target):

  • PR 69ebece8a98ae956556eb02d3721db712501e9b2: 0 passed, 1 failed, observed (return, actual_refs) = (2, 2) instead of (1, 1); cargo exit101.
  • Parent/main 8b957bec6f82495aec940944c6bf47faa02db590: 1 passed, 0 failed, same fixture; cargo exit0.
  • Fresh Compiling xcl from the intended worktree on each leg:25.71s and25.30s. Both tests finished in0.00s. Only the identical local test was added; production unchanged. One test thread,9GiB memory/512MiB swap scope,180s timeout per leg.

Command on each checkout:

cargo test --locked --target x86_64-pc-windows-msvc -p xcl --lib lab_command_list_nonfinal_release_consumes_one_reference -- --nocapture --test-threads=1

Local logs: .compat-xcl/logs/verify-xcl-pr2852-wine-nonfinal-release.log and verify-xcl-pr2843-parent-wine-nonfinal-release.log (the latter checkout is the exact common parent, reused from an earlier verification). This is a demonstrated regression in the wrapper's COM accounting, not just an inferred memory-retention risk.

@throny

throny commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

Two contributors checked independently and found that this PR makes the same change as #2691 (for #2690): #1607, group D140. They suggest keeping #2691. If you agree, closing this PR or joining the other one would save the maintainers a review. If the two differ in a way the checks missed, a note in #1607 helps.

This is a hint from the community triage in #1607, not a maintainer decision.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

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: a command list is forgotten after the runtime frees it, so a new list at its address can lose its state

3 participants