D3D12.X: concurrent COM use-after-free (AddRef on freed ID3D12Resource) in title_textures and res_release #1359

Open
opened 2026-11-26 04:51:57 +00:00 by github-bot · 3 comments
Owner

@⁠microwave805 on GitHub · 2026-11-24 15:12 UTC · original

Checked on main (930e8b4).

Problem

title_textures::TEXTURES (crates/xcl/src/hle/d3d12x/title_textures.rs:75-89) stores raw, un-AddRef'd ID3D12Resource* pointers and revives them into owned handles via ID3D12Resource::from_raw_borrowed(&p).map(|r| r.clone()) (IUnknown::AddRef) under the TEXTURES lock in refresh_once (title_textures.rs:775) and clone_resources (title_textures.rs:298-305).

In real::res_release (crates/xcl/src/hle/d3d12x/real.rs:1547-1595), the COM refcount can drop to 0 and destroy/free this before title_textures::forget(this as usize) runs at line 1581:

    let n = if super::late_release::enabled() {
        let add: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(orig_fn(this, 1));
        if add(this) == 2 {
            f(this);
            held = true;
            0
        } else {
            f(this);
            f(this) // destroys & frees COM object when two threads race at refcount 2
        }
    } else {
        f(this)     // destroys & frees COM object immediately when XCL_LATE_RELEASE=0
    };
    if n == 0 {
        // 4 mutex locks + sparse::forget_buffer + vram::forget run BEFORE title_textures::forget:
        ...
        super::sparse::forget_buffer(this as usize);
        super::vram::forget(this as usize);
        super::title_textures::forget(this as usize);

Two failure paths:

  1. COM UAF (AddRef on freed vtable): Whenever a tracked texture has refcount 2 (e.g., title holds 1 ref and title_textures::upload_all holds 1 ref from refresh_once) and both threads enter res_release concurrently, both fail add(this) == 2 (as noted at line 1550) and the second f(this) destroys/frees this while it is still present in TEXTURES across lines 1566–1580. With XCL_LATE_RELEASE=0, every final release hits this window. Any concurrent refresh_once, register, retire_overlapping, or forget calls r.clone() (AddRef) on the freed COM object.
  2. Revival during backing teardown (even with late_release enabled and no concurrent release): Between f(this) at line 1556 (held = true, refcount 1) and title_textures::forget at line 1581, refresh_once can AddRef this (1 -> 2) and schedule upload_all while res_release tears down its vram tiles/arena and UMA_BUFFERS backing.

(Related to #1040 / #1070, which defer backing-memory teardown for held resources, but does not synchronize TEXTURES removal before the COM object itself is destroyed.)

Suggested fix

Synchronize title_textures unregistration with final release so this is removed from TEXTURES before the underlying Release can drop the COM refcount to zero (and before tearing down vram/UMA_BUFFERS).


AI use: Antigravity (Gemini 4 Neon) performed the read-only source audit and drafted this issue; checked and verified on 930e8b4 before opening.

**@⁠microwave805** on GitHub · 2026-11-24 15:12 UTC · [original](https://github.com/xcl-dev/xcl/issues/1359) Checked on `main` (`930e8b4`). ### Problem `title_textures::TEXTURES` (`crates/xcl/src/hle/d3d12x/title_textures.rs:75-89`) stores raw, un-`AddRef`'d `ID3D12Resource*` pointers and revives them into owned handles via `ID3D12Resource::from_raw_borrowed(&p).map(|r| r.clone())` (`IUnknown::AddRef`) under the `TEXTURES` lock in `refresh_once` (`title_textures.rs:775`) and `clone_resources` (`title_textures.rs:298-305`). In `real::res_release` (`crates/xcl/src/hle/d3d12x/real.rs:1547-1595`), the COM refcount can drop to `0` and destroy/free `this` **before** `title_textures::forget(this as usize)` runs at line `1581`: ```rust let n = if super::late_release::enabled() { let add: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(orig_fn(this, 1)); if add(this) == 2 { f(this); held = true; 0 } else { f(this); f(this) // destroys & frees COM object when two threads race at refcount 2 } } else { f(this) // destroys & frees COM object immediately when XCL_LATE_RELEASE=0 }; if n == 0 { // 4 mutex locks + sparse::forget_buffer + vram::forget run BEFORE title_textures::forget: ... super::sparse::forget_buffer(this as usize); super::vram::forget(this as usize); super::title_textures::forget(this as usize); ``` Two failure paths: 1. **COM UAF (`AddRef` on freed vtable):** Whenever a tracked texture has refcount 2 (e.g., title holds 1 ref and `title_textures::upload_all` holds 1 ref from `refresh_once`) and both threads enter `res_release` concurrently, both fail `add(this) == 2` (as noted at line 1550) and the second `f(this)` destroys/frees `this` while it is still present in `TEXTURES` across lines 1566–1580. With `XCL_LATE_RELEASE=0`, every final release hits this window. Any concurrent `refresh_once`, `register`, `retire_overlapping`, or `forget` calls `r.clone()` (`AddRef`) on the freed COM object. 2. **Revival during backing teardown (even with `late_release` enabled and no concurrent release):** Between `f(this)` at line 1556 (`held = true`, refcount 1) and `title_textures::forget` at line 1581, `refresh_once` can `AddRef` `this` (`1 -> 2`) and schedule `upload_all` while `res_release` tears down its `vram` tiles/arena and `UMA_BUFFERS` backing. *(Related to #1040 / #1070, which defer backing-memory teardown for held resources, but does not synchronize `TEXTURES` removal before the COM object itself is destroyed.)* ### Suggested fix Synchronize `title_textures` unregistration with final release so `this` is removed from `TEXTURES` **before** the underlying `Release` can drop the COM refcount to zero (and before tearing down `vram`/`UMA_BUFFERS`). --- **AI use:** Antigravity (Gemini 4 Neon) performed the read-only source audit and drafted this issue; checked and verified on `930e8b4` before opening.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:51:57 +00:00
Author
Owner

@⁠Pipodi on GitHub · 2026-11-24 21:03 UTC · original

A data point from a retail-title run, which may be the same race seen from the texture's memory rather than its COM object. One occurrence; it did not repeat in the next run, which ended at 50 s for another reason.

Tree: main b9b6ea6 with #929 (b3a969b) merged locally. #929 changes only hle/d3d12x/title_list.rs. RTX 4090 (driver 617.42), Windows 11, release build, XCL_PSO_WAIT_MS=50.

What happened: at 34.57 s, while the title was presenting its loading screen at full rate, there was an unhandled 0xc0000005 in VCRUNTIME140.dll+0x1cc66, a read at 0xc49256000. The process then hung and was killed after 60 s of log silence. The address lies in the title's CPU memory; the log shows title textures in that range being "kept in step with 0xc49…". Symbolized with the build's PDB (return address − 1):

memcpy (VCRUNTIME140)
xcl::gpu::tiling::Chain::detile_level          gpu/tiling.rs:438
xcl::hle::d3d12x::title_textures::subresources  hle/d3d12x/title_textures.rs:1182
title_textures::subresources_on::{closure}      title_textures.rs:1131
title_textures::refresh -> own_work -> refresh  title_textures.rs:645 / 663

So refresh_once was detiling a tracked texture from its title VA (title_bytes(va, …) at :1180) after that memory had been released. This is consistent with a texture still registered in TEXTURES after the title released it, which is what #1404 and #1527 address. I have not checked whether either PR covers this path (a read of the texture's title memory, not AddRef on the resource). I have not run either PR yet.

AI use: Claude Code (Claude Opus 5.5) ran the run, symbolized the stack and wrote this comment.

**@⁠Pipodi** on GitHub · 2026-11-24 21:03 UTC · [original](https://github.com/xcl-dev/xcl/issues/1359#issuecomment-7144415877) A data point from a retail-title run, which may be the same race seen from the texture's memory rather than its COM object. One occurrence; it did not repeat in the next run, which ended at 50 s for another reason. **Tree:** `main` `b9b6ea6` with #929 (`b3a969b`) merged locally. #929 changes only `hle/d3d12x/title_list.rs`. RTX 4090 (driver 617.42), Windows 11, release build, `XCL_PSO_WAIT_MS=50`. **What happened:** at 34.57 s, while the title was presenting its loading screen at full rate, there was an unhandled `0xc0000005` in `VCRUNTIME140.dll+0x1cc66`, a read at `0xc49256000`. The process then hung and was killed after 60 s of log silence. The address lies in the title's CPU memory; the log shows title textures in that range being "kept in step with 0xc49…". Symbolized with the build's PDB (return address − 1): ``` memcpy (VCRUNTIME140) xcl::gpu::tiling::Chain::detile_level gpu/tiling.rs:438 xcl::hle::d3d12x::title_textures::subresources hle/d3d12x/title_textures.rs:1182 title_textures::subresources_on::{closure} title_textures.rs:1131 title_textures::refresh -> own_work -> refresh title_textures.rs:645 / 663 ``` So `refresh_once` was detiling a tracked texture from its title VA (`title_bytes(va, …)` at :1180) after that memory had been released. This is consistent with a texture still registered in `TEXTURES` after the title released it, which is what #1404 and #1527 address. I have not checked whether either PR covers this path (a read of the texture's title memory, not `AddRef` on the resource). I have not run either PR yet. **AI use:** Claude Code (Claude Opus 5.5) ran the run, symbolized the stack and wrote this comment.
Author
Owner

@⁠Anon64 on GitHub · 2026-11-25 00:28 UTC · original

Two more occurrences of the crash @⁠Pipodi describes, on another machine: RTX 3060 12 GB (581.80), Windows 11 26200, release builds of main 20cf130 + #1818 and main 80f4819 + #253, retail title, A pressed every 4 s.

  • Both are 0xc0000005 in VCRUNTIME140.dll+0x1cc66 (memcpy) ← gpu::tiling::Chain::detile_level (tiling.rs:438) ← title_textures::subresources (title_textures.rs:1182), at 36.7 s and 36.8 s, while the loading screen ran at full rate.
  • Read addresses 0xc49076010 and 0xc490d0000, in the same range as yours (0xc49256000). The second is exactly on a 64 KiB boundary, so the copy ran onto a page that was no longer readable, after title_bytes had found the whole range readable and borrowed it in place.
  • About 2 in 25 runs on this machine.
  • title_bytes at title_textures.rs:1158 checks readable(at, len) and then hands out a borrow of the title's memory. If the title frees or decommits that memory before the detile reads it, the read faults. That check-then-use gap is separate from the COM-reference race in this issue's description.
  • I have not checked whether #1404 or #1527 changes this.

Written with Claude Code (Claude Opus 5.5) from runs on this machine.

**@⁠Anon64** on GitHub · 2026-11-25 00:28 UTC · [original](https://github.com/xcl-dev/xcl/issues/1359#issuecomment-7148551396) Two more occurrences of the crash @⁠Pipodi describes, on another machine: RTX 3060 12 GB (581.80), Windows 11 26200, release builds of `main` `20cf130` + #1818 and `main` `80f4819` + #253, retail title, A pressed every 4 s. - Both are `0xc0000005` in `VCRUNTIME140.dll+0x1cc66` (memcpy) ← `gpu::tiling::Chain::detile_level` (tiling.rs:438) ← `title_textures::subresources` (title_textures.rs:1182), at 36.7 s and 36.8 s, while the loading screen ran at full rate. - Read addresses `0xc49076010` and `0xc490d0000`, in the same range as yours (`0xc49256000`). The second is exactly on a 64 KiB boundary, so the copy ran onto a page that was no longer readable, after `title_bytes` had found the whole range readable and borrowed it in place. - About 2 in 25 runs on this machine. - `title_bytes` at title_textures.rs:1158 checks `readable(at, len)` and then hands out a borrow of the title's memory. If the title frees or decommits that memory before the detile reads it, the read faults. That check-then-use gap is separate from the COM-reference race in this issue's description. - I have not checked whether #1404 or #1527 changes this. Written with Claude Code (Claude Opus 5.5) from runs on this machine.
Author
Owner

@⁠Anon64 on GitHub · 2026-11-25 02:52 UTC · original

Follow-up to my two occurrences above: 0 in 20 on main cbc296a. Same machine and setup: 20 scripts/smoke.ps1 runs of 60 s, A pressed every 4 s, default settings. No detile_level / VCRUNTIME140.dll+0x1cc66 access violation, and no hang.

The three crashes I saw were all on builds from 80f4819 or earlier: two single-thread ones and one with five threads at once. Since then title_textures.rs got #1986 (fb9d059, upload batches capped by count and 32 MiB), which would shorten the window between title_bytes and the detile but does not close it. So I would read this as "much rarer here now", not fixed. #751's safe copy, or a lock around title-memory frees, still covers the gap itself.

Written with Claude Code (Claude Opus 5.5) from runs on this machine.

**@⁠Anon64** on GitHub · 2026-11-25 02:52 UTC · [original](https://github.com/xcl-dev/xcl/issues/1359#issuecomment-7151289939) Follow-up to my two occurrences above: **0 in 20** on `main` `cbc296a`. Same machine and setup: 20 `scripts/smoke.ps1` runs of 60 s, A pressed every 4 s, default settings. No `detile_level` / `VCRUNTIME140.dll+0x1cc66` access violation, and no hang. The three crashes I saw were all on builds from `80f4819` or earlier: two single-thread ones and one with five threads at once. Since then `title_textures.rs` got #1986 (`fb9d059`, upload batches capped by count and 32 MiB), which would shorten the window between `title_bytes` and the detile but does not close it. So I would read this as "much rarer here now", not fixed. #751's safe copy, or a lock around title-memory frees, still covers the gap itself. Written with Claude Code (Claude Opus 5.5) from runs on this machine.
This discussion has been locked. Commenting is limited to contributors.
No description provided.