Skip to content

D3D12.X: sparse::release_if_unused bypasses #2345 GPU fence quarantine on unpin, and place_buffer races FORGOTTEN pointer reuse across dev_create_reserved_resource #2457

Backlog

Description

@microwave805

Static audit on main @ c7e1799 found two critical tile-lifetime / GPU UAF bugs in crates/xcl/src/hle/d3d12x/sparse.rs (affecting #2345 and #1981). Part of #1773, Part of #1833.

1. [Critical] release_if_unused bypasses #2345's GPU fence quarantine when a released placed buffer unpins a parked tile

  • Where: crates/xcl/src/hle/d3d12x/sparse.rs:1411-1419 (release_if_unused), 1497-1518 (flush_parked), 1710-1730 (forget_buffer, forget_in), and real.rs:1692, 1705 (res_release)
  • Root cause:
    1. flush_parked() (sparse.rs:1507-1509) unmaps parked tiles on self.queue and advances self.fence, but skips adding any tile in self.pinned to quarantine_tiles.
    2. When the pinning placed buffer B hits its last title Release, real.rs:1692 calls sparse::forget_buffer(B) immediately, before late_release::hold(B) (real.rs:1705) waits for in-flight GPU work referencing B.
    3. forget_in() (sparse.rs:1719-1730) decrements s.pinned[tile] to 0 and calls s.release_if_unused(tile) (sparse.rs:1411-1419), which pushes tile directly onto self.free without waiting on either self.fence (the window unmap) or self.queue_fences (title queues still executing commands on B).
  • Trigger: Sparse tile P is pinned by placed buffer B (in-flight on queue Q), its VA is decommitted and flushed by flush_parked(), then B is released while Q is still reading/writing B → P goes straight to self.free, gets reallocated by take_tile(), and is overwritten by upload_zeros/upload_dirty while Q still accesses B (GPU UAF / corruption / device removal).
  • Fix direction: Instead of pushing straight to self.free in release_if_unused, queue unpinned unused tiles in a pending_quarantine: Vec<(u32, u32)> (avoiding q.Signal inside res_release where D3D12 may hold internal locks) and drain pending_quarantine through quarantine_tiles() in before_execute, take_tile, and flush_parked (and exclude pools with pending_quarantine tiles from retire_free_pools).

2. [Critical] place_buffer holds STATE across dev_create_reserved_resource, racing with FORGOTTEN pointer reuse

  • Where: crates/xcl/src/hle/d3d12x/sparse.rs:1647-1684 (place_buffer), 1688-1717 (FORGOTTEN, lock_state, forget_released, forget_buffer), real.rs:1692, 1705, late_release.rs:102-112
  • Root cause: place_buffer() acquires lock_state() (draining FORGOTTEN) at line 1648, then holds STATE across real::dev_create_reserved_resource(...) (line 1667) before inserting the returned pointer raw into s.buffers (line 1681).
    • While STATE is held, another thread running final Release on a resource at address P calls sparse::forget_buffer(P) → STATE.try_lock() returns WouldBlock, leaving P queued in FORGOTTEN.
    • P is then freed (immediately under XCL_LATE_RELEASE=0, or synchronously inside late_release::hold's drain_known(true) / idle spin at late_release.rs:105-109 when queues are idle) and reused by dev_create_reserved_resource for the new placed buffer B2.
    • place_buffer inserts s.buffers[P] = B2_tiles (also silently leaking B1's pins if P was previously a sparse buffer), releases STATE, and the very next lock_state() drains FORGOTTEN → forget_in(s, P) deletes live B2 and unpins all of B2's tiles while B2 is active on the GPU.
  • Fix direction: Either create the reserved resource before acquiring STATE (matching vram::place_on_pages), or drain forget_released(&mut g) again right after dev_create_reserved_resource returns and before s.buffers.insert(raw as usize, ...).

AI use

  • Antigravity (Gemini 4 Neon): static code audit on main @ c7e1799 and issue drafting.

Activity

  1. razormontana commented on Nov 25, 2026

    @razormontana
    Contributor

    I’ll take item 1 and keep the tile pool from making a newly unpinned tile available until the relevant fence retirement completes. I’ll add a synthetic allocator-state test for both the final-pin and fence completion orders.

    Assisted by OpenAI Codex (GPT-6.2 Sol).

  2. thehollowedone commented on Nov 26, 2026

    @thehollowedone
    Contributor

    Item 2 follow-up for the two open fixes: #2528 at 237882e and #2539 at 23e129a. The #2539 test-only reproduction and control cover the ordering where an old object's final-Release notification arrives after a replacement has registered at the same COM address.

    I checked that ordering against #2528 separately. In its existing a_buffer_released_during_a_placement_does_not_take_the_new_one_with_it test, the only code change was moving FORGOTTEN.lock().unwrap().push(RES) from before record_buffer(&mut g, RES, vec![new]) to immediately after it. With that test-only change, cargo test -p xcl --lib a_buffer_released_during_a_placement_does_not_take_the_new_one_with_it -- --nocapture --test-threads=1 failed 0 passed, 1 failed: the new buffer was let go (left: None, right: Some([(7, 2)])). After restoring the exact clean PR head, the same test passed 1/1.

    The production ordering is possible because the original COM Release returns zero before sparse::forget_buffer is called. Both current tests cover notifications delivered before the new registration; this added case is a synthetic bookkeeping check, not an observed driver race or retail title failure. A release identity captured before the driver can destroy the old object would let deferred cleanup target that object rather than whichever buffer currently occupies its address. Other address-keyed final-release cleanup may need the same ownership audit.

    Windows 11 build 26300, RTX 3080 Ti / 621.35, rustc 1.99.1. No game data or title run. AI use: Codex (GPT-6.2 Sol) ran the two test variants and drafted this report; GPT-6.2 Astra reviewed the ownership ordering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions