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

Open
opened 2026-11-26 05:31:20 +00:00 by github-bot · 2 comments
Owner

@⁠microwave805 on GitHub · 2026-11-25 19:30 UTC · original

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.
**@⁠microwave805** on GitHub · 2026-11-25 19:30 UTC · [original](https://github.com/xcl-dev/xcl/issues/2457) 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:31:21 +00:00
Author
Owner

@⁠razormontana on GitHub · 2026-11-25 20:16 UTC · original

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).

**@⁠razormontana** on GitHub · 2026-11-25 20:16 UTC · [original](https://github.com/xcl-dev/xcl/issues/2457#issuecomment-7173426827) 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).
Author
Owner

@⁠thehollowedone on GitHub · 2026-11-26 00:11 UTC · original

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.

**@⁠thehollowedone** on GitHub · 2026-11-26 00:11 UTC · [original](https://github.com/xcl-dev/xcl/issues/2457#issuecomment-7178490399) Item 2 follow-up for the two open fixes: [#2528 at `237882e`](https://git.gtahub.st/github/xcl-dev--xcl/pulls/2528) and [#2539 at `23e129a`](https://git.gtahub.st/github/xcl-dev--xcl/pulls/2539). The [#2539 test-only reproduction and control](https://git.gtahub.st/github/xcl-dev--xcl/pulls/2539) 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](https://github.com/xcl-dev/xcl/blob/237882e85be2919904333b6b084baee4cc4c4411/crates/xcl/src/hle/d3d12x/real.rs#L1663-L1693). 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.
This discussion has been locked. Commenting is limited to contributors.
No description provided.