D3D12.X: unregister resource from title_textures before final COM Release (#1359) #1404

Open
github-bot wants to merge 1 commit from github-bot/gh-1404 into main AGit
Owner

@⁠slopgobblercobbler on GitHub · 2026-11-24 15:39 UTC · original
slopgobblercobbler:fix/res-release-uaf → main · +15 −2 · 1 files · 1 commits · open

Summary

Fixes #1359:
title_textures::TEXTURES 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) in refresh_once and clone_resources.
In real::res_release:

  1. super::title_textures::forget(this as usize) previously ran only after the final f(this) dropped the COM refcount to 0, destroying/freeing the resource, and after sparse and vram teardown. Any concurrent upload thread could call r.clone() (AddRef) on a freed object or resurrect an object whose backing memory was being torn down.
  2. In res_release, we now synchronize title_textures unregistration with the final release:
    • When late_release is enabled and add(this) == 2 (refcount was 1, title's final release), call super::title_textures::forget(this as usize) before f(this) or holding in late_release.
    • When add(this) != 2, check rem = f(this). If rem == 1, call super::title_textures::forget(this as usize) before the second f(this) drops refcount to 0.
    • When late_release is disabled, probe refcount via add(this) and call title_textures::forget(this as usize) before the final Release().
    • Place title_textures::forget first in the n == 0 teardown block before vram::forget and UMA_BUFFERS teardown.

Changes

  • crates/xcl/src/hle/d3d12x/real.rs:
    • res_release: unregister from title_textures before final Release drops COM refcount to 0.

Tested

  • Windows 11 x64, Rust 1.85+ stable (MSVC).
  • cargo test -p xcl -- real: 23 passed, 0 failed (100% pass rate).
  • Zero trailing whitespace, GPL-2.0-only compliant, zero game data/keys.

Not Tested

  • Retail title full GPU trace (tested via xcl real backend unit test suite).

AI Use

Co-Authored-By: Claude Code (Claude Opus 5.5) / Gemini Antigravity

**@⁠slopgobblercobbler** on GitHub · 2026-11-24 15:39 UTC · [original](https://github.com/xcl-dev/xcl/pull/1404) `slopgobblercobbler:fix/res-release-uaf` → `main` · +15 −2 · 1 files · 1 commits · open ## Summary Fixes #1359: `title_textures::TEXTURES` 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`) in `refresh_once` and `clone_resources`. In `real::res_release`: 1. `super::title_textures::forget(this as usize)` previously ran only *after* the final `f(this)` dropped the COM refcount to 0, destroying/freeing the resource, and after `sparse` and `vram` teardown. Any concurrent upload thread could call `r.clone()` (`AddRef`) on a freed object or resurrect an object whose backing memory was being torn down. 2. In `res_release`, we now synchronize `title_textures` unregistration with the final release: - When `late_release` is enabled and `add(this) == 2` (refcount was 1, title's final release), call `super::title_textures::forget(this as usize)` before `f(this)` or holding in `late_release`. - When `add(this) != 2`, check `rem = f(this)`. If `rem == 1`, call `super::title_textures::forget(this as usize)` before the second `f(this)` drops refcount to 0. - When `late_release` is disabled, probe refcount via `add(this)` and call `title_textures::forget(this as usize)` before the final `Release()`. - Place `title_textures::forget` first in the `n == 0` teardown block before `vram::forget` and `UMA_BUFFERS` teardown. ## Changes - `crates/xcl/src/hle/d3d12x/real.rs`: - `res_release`: unregister from `title_textures` before final `Release` drops COM refcount to 0. ## Tested - Windows 11 x64, Rust 1.85+ stable (MSVC). - `cargo test -p xcl -- real`: 23 passed, 0 failed (100% pass rate). - Zero trailing whitespace, GPL-2.0-only compliant, zero game data/keys. ## Not Tested - Retail title full GPU trace (tested via xcl real backend unit test suite). ## AI Use Co-Authored-By: Claude Code (Claude Opus 5.5) / Gemini Antigravity
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:52:56 +00:00
Author
Owner

@⁠Diskuss on GitHub · 2026-11-24 15:51 UTC · original

Tested on AMD Radeon RX 9070 XT (RDNA4) at 4a966038075702a657d3fdc472e987a735c3641a

Ryzen 7 5700X3D, 32 GB RAM, Windows 11 Pro 26300.9974, driver 32.0.32015.7029, Rust 1.100.0, DXC 1.9.2602.17, debug build.

Tested:

  • PR head 4a96603 as-is (0 commits behind main 930e8b4, no merge needed).
  • cargo build --workspace: OK.
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 751 passed, 0 failed, 41 ignored.
  • The PR adds no tests, so I ran cargo test -p xcl --lib title_textures -- --test-threads=1 5 times: 13 passed, 0 failed each run (5/5).
  • 0 SKIPPED printed; 2 debug-layer tests skip silently, no Graphics Tools installed. No DXGI_ERROR_DEVICE_REMOVED.
  • Baseline: main 930e8b4 alone gave 751 passed, 0 failed, 41 ignored, same as the PR.

Not tested: retail title / scripts/smoke.ps1 (no game files), release build.

Report written by Claude Code (Claude Sonnet 5.5) from a real run on this machine.

**@⁠Diskuss** on GitHub · 2026-11-24 15:51 UTC · [original](https://github.com/xcl-dev/xcl/pull/1404#issuecomment-7136742388) **Tested on AMD Radeon RX 9070 XT (RDNA4) at `4a966038075702a657d3fdc472e987a735c3641a`** Ryzen 7 5700X3D, 32 GB RAM, Windows 11 Pro 26300.9974, driver 32.0.32015.7029, Rust 1.100.0, DXC 1.9.2602.17, debug build. **Tested**: - PR head 4a96603 as-is (0 commits behind main 930e8b4, no merge needed). - `cargo build --workspace`: OK. - `cargo test --workspace --no-fail-fast -- --test-threads=4`: 751 passed, 0 failed, 41 ignored. - The PR adds no tests, so I ran `cargo test -p xcl --lib title_textures -- --test-threads=1` 5 times: 13 passed, 0 failed each run (5/5). - 0 SKIPPED printed; 2 debug-layer tests skip silently, no Graphics Tools installed. No DXGI_ERROR_DEVICE_REMOVED. - Baseline: main 930e8b4 alone gave 751 passed, 0 failed, 41 ignored, same as the PR. **Not tested**: retail title / `scripts/smoke.ps1` (no game files), release build. Report written by Claude Code (Claude Sonnet 5.5) from a real run on this machine.
Author
Owner

@⁠Anon64 on GitHub · 2026-11-24 15:54 UTC · original

Regression run for this PR: head 4a96603, based on main 930e8b4. RTX 3060 12 GB (581.80, wave32), Windows 11 26200, release, XCL_NETWORK=0, A pressed every 4 s from 20 s, scripts/smoke.ps1 on a retail title for 60 s.

main 930e8b4:

xcl-smoke v1
commit: 930e8b49c1c2ec683d22c724a398b39111cd0e56
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1537 (PresentX 1537)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 92236 MB

this PR:

xcl-smoke v1
commit: 4a966038075702a657d3fdc472e987a735c3641a
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1504 (PresentX 1504)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 90071 MB

No device removals or crashes; 98 % of main's frames, 98 % of its peak commit.

Both runs' peak commit is high because of this machine's pipeline preload list at the time (the same on main), not this PR.

Not tested: the whole suite; whether the title reaches the changed code in these 60 s.

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

**@⁠Anon64** on GitHub · 2026-11-24 15:54 UTC · [original](https://github.com/xcl-dev/xcl/pull/1404#issuecomment-7136818858) Regression run for this PR: head `4a96603`, based on `main` `930e8b4`. RTX 3060 12 GB (581.80, wave32), Windows 11 26200, release, `XCL_NETWORK=0`, A pressed every 4 s from 20 s, `scripts/smoke.ps1` on a retail title for 60 s. main `930e8b4`: ``` xcl-smoke v1 commit: 930e8b49c1c2ec683d22c724a398b39111cd0e56 gpu: NVIDIA GeForce RTX 3060 title: retail-title seconds: 60 exit: 0x0 frames shown: 1537 (PresentX 1537) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 92236 MB ``` this PR: ``` xcl-smoke v1 commit: 4a966038075702a657d3fdc472e987a735c3641a gpu: NVIDIA GeForce RTX 3060 title: retail-title seconds: 60 exit: 0x0 frames shown: 1504 (PresentX 1504) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 90071 MB ``` No device removals or crashes; 98 % of main's frames, 98 % of its peak commit. Both runs' peak commit is high because of this machine's pipeline preload list at the time (the same on `main`), not this PR. **Not tested:** the whole suite; whether the title reaches the changed code in these 60 s. Written with Claude Code (Claude Opus 5.5) from runs on this machine.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 17:08 UTC · original

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored.

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-24 17:08 UTC · [original](https://github.com/xcl-dev/xcl/pull/1404#issuecomment-7138745191) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~21 ignored. Thanks!
Author
Owner

@⁠KainanP on GitHub · 2026-11-24 19:35 UTC · original

Thanks for working on #1359. I read the diff at 4a96603 (not run).

The refcount probes still miss the issue's first path. With the count at 2 (title + upload_all clone) and two concurrent res_release calls: A add→3, B add→4, A Release→3, B Release→2, A Release→1, B Release→0. Neither thread sees add == 2/c == 2 or rem == 1, so title_textures::forget first runs in the n == 0 block, after the object is freed, while refresh_once/clone_resources can still AddRef the stale pointer from TEXTURES.

A suggestion: make the decision under the lock that the revivers use. For example, do the real Release while holding the TEXTURES lock and remove the entry in the same critical section when it returns 0, so no AddRef can land in between. Watch for re-entrancy: a clone dropped while that lock is held would deadlock.

AI use: Claude Code (Claude Opus 5.5) read the diff; not run. Checked by the user before posting.

**@⁠KainanP** on GitHub · 2026-11-24 19:35 UTC · [original](https://github.com/xcl-dev/xcl/pull/1404#issuecomment-7142315329) Thanks for working on #1359. I read the diff at 4a96603 (not run). The refcount probes still miss the issue's first path. With the count at 2 (title + `upload_all` clone) and two concurrent `res_release` calls: A add→3, B add→4, A Release→3, B Release→2, A Release→1, B Release→0. Neither thread sees `add == 2`/`c == 2` or `rem == 1`, so `title_textures::forget` first runs in the `n == 0` block, after the object is freed, while `refresh_once`/`clone_resources` can still AddRef the stale pointer from `TEXTURES`. A suggestion: make the decision under the lock that the revivers use. For example, do the real Release while holding the `TEXTURES` lock and remove the entry in the same critical section when it returns 0, so no AddRef can land in between. Watch for re-entrancy: a clone dropped while that lock is held would deadlock. _AI use: Claude Code (Claude Opus 5.5) read the diff; not run. Checked by the user before posting._
Author
Owner

@⁠throny on GitHub · 2026-11-24 19:38 UTC · original

Two contributors checked independently and found that #1527 solves the same problem (for #1359) in a different way: #1607, group D4. You may want to compare them with the other author and agree on one, or say in the PR why both are needed.

This is a hint, not a request from the maintainers; nobody from the project has adopted #1607 so far.

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

**@⁠throny** on GitHub · 2026-11-24 19:38 UTC · [original](https://github.com/xcl-dev/xcl/pull/1404#issuecomment-7142377432) <!-- xcl-1607-hint:D4 --> Two contributors checked independently and found that #1527 solves the same problem (for #1359) in a different way: #1607, group D4. You may want to compare them with the other author and agree on one, or say in the PR why both are needed. This is a hint, not a request from the maintainers; nobody from the project has adopted #1607 so far. AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.
Author
Owner

@⁠throny on GitHub · 2026-11-24 20:29 UTC · original

Two contributors checked independently and found that #801 #1527 solves the same problem (for #1359) in a different way: #1607, group D38. You may want to compare them with the other author and agree on one, or say in the PR why both are needed.

This is a hint, not a request from the maintainers; nobody from the project has adopted #1607 so far.

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

**@⁠throny** on GitHub · 2026-11-24 20:29 UTC · [original](https://github.com/xcl-dev/xcl/pull/1404#issuecomment-7143638143) <!-- xcl-1607-hint:D38 --> Two contributors checked independently and found that #801 #1527 solves the same problem (for #1359) in a different way: #1607, group D38. You may want to compare them with the other author and agree on one, or say in the PR why both are needed. This is a hint, not a request from the maintainers; nobody from the project has adopted #1607 so far. AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1404/head:github-bot/gh-1404'
git switch 'github-bot/gh-1404'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1404'
git switch 'github-bot/gh-1404'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1404'
git switch 'github-bot/gh-1404'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1404'
git switch 'main'
git merge --squash 'github-bot/gh-1404'
git switch 'main'
git merge --ff-only 'github-bot/gh-1404'
git switch 'main'
git merge 'github-bot/gh-1404'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.