Skip to content

D3D12.X: note_execute references its RTV resource before using it (#2381) - #2520

Draft
Anka-1623 wants to merge 1 commit into
xcl-dev:mainfrom
Anka-1623:present-note-execute-rtv-reference
Draft

Anka-1623 wants to merge 1 commit into
xcl-dev:mainfrom
Anka-1623:present-note-execute-rtv-reference

Conversation

@Anka-1623

@Anka-1623 Anka-1623 commented Nov 25, 2026 •

Copy link
Copy Markdown

Fixes #2381.

note_execute (the PresentX fallback in present.rs) copies a resource pointer out of RTVS, drops the table's lock, and then calls into the resource: backbuffer_like(res) reads its desc through its vtable. It only takes its own reference several lines later, when the resource becomes the present candidate. note_rtv releases the table's reference every time it replaces or clears a handle, so on another thread it can take the count to zero between those two points, and note_execute then reads a freed COM object.

This takes the reference while the table is still locked, so note_rtv cannot release it first. The fallback only runs until the title's first PresentX.

What is in it

  • present.rs: new rtv_resource_addref(handle) looks the handle up and add_refs the resource under the RTVS lock. The caller releases it.
  • present.rs, note_execute: uses it instead of the bare lookup. The reference is released when the resource is not back-buffer-like, or when it is already the candidate; otherwise it becomes the candidate's reference (note_execute used to take that one itself, with an add_ref just before CANDIDATE.replace). The reference count ends up the same on every path; only the order changed. The cost is one extra AddRef/Release pair on the two paths that took none before.
  • present.rs, tests: rtv_resource_addref_holds_a_reference_of_its_own (deterministic: the reference outlives note_rtv clearing the handle and the title's own release) and rtv_resource_addref_never_returns_a_freed_resource (one thread keeps rewriting a handle with note_rtv, another calls the helper; a fake COM object records the release that takes it to zero).

Left alone on purpose (one problem per PR):

  • rtv_resource / rt0_resource also return bare pointers, but their other callers (real.rs, bind.rs) are the XCL_TRACE_FRAME diagnostics.
  • present_now reads CANDIDATE's resource the same way: it copies res out of the CANDIDATE lock and calls backbuffer_like(res) after the guard is gone, while note_execute can replace the candidate and release the old one. It looks like the same kind of window; I did not change or test it.

Tested

  • Type-check only, on Linux (Rust 1.100.0, x86_64-pc-windows-msvc target, nothing linked): cargo check --locked --target x86_64-pc-windows-msvc -p xcl --lib --tests passes on main (6d12f58) and on this branch, with the same 3 + 2 warnings and none in present.rs. A local shim stood in for lib.exe only so that libudis86-sys's build script could run; nothing in the repository changes for it.
  • A model of the race, not the repository's tests: I copied the exact RTVS / note_rtv / add_ref / release code and the test's fake COM object into a standalone program and ran it on Linux x86_64 (12 threads). With the old pattern (a bare pointer copied out of the table) the reader found an already-freed resource in 30 of 30 runs at the test's 20,000 rounds (888 to 1,587 times in each 200,000-round run). With rtv_resource_addref it found none in 5 runs of 200,000 rounds, and every resource was released to exactly zero. This only shows the test can fail and that the helper closes this window.
  • Reported by another contributor on e37abac (comment): RTX 3070 Laptop (driver 32.0.16.1714), i7-11800H, Windows 11 Pro 26300, cargo test -p xcl --lib present -- --test-threads=4: 11 passed, 0 failed, 0 ignored. That covers the 11 tests in present.rs, both new ones included. Not run by them: the full workspace suite and smoke.
  • cargo test --workspace: not run (XCL does not build or run on Linux).

Regression run
Not run: no Windows machine, GPU or title here (see Not tested).

Not tested

  • Anything on Windows: not built with MSVC, the new tests have not been run, cargo test --workspace has not been run, no GPU of any vendor, no retail title.
  • No scripts/smoke.ps1 run, so there are no Regression run blocks. This PR changes crates/xcl/src, so PR checks will fail until someone with a Windows + GPU + title setup adds them; that is why it is a draft. I did not write the blocks by hand.
  • The race itself was never seen in a real title. The evidence is the code path and the Linux model above.
  • Overlap with other open PRs: I read the diffs of the open PRs that touch present.rs for one that changes this lookup or note_execute's candidate update and found none. D3D12.X: a queue the title releases is not kept alive by submit tracking #1691 (adds forget_queue, which also locks CANDIDATE) and D3D12.X: a released descriptor heap's RTV entries go with it #1771 (RTVS entries of a released heap) change nearby code, so a rebase may be needed.

AI use

🤖 Generated with Claude Code

…l-dev#2381)

note_execute copied a resource pointer out of RTVS and dropped the lock, then
called backbuffer_like(res) on it (a COM call through the resource's vtable), and
only took its own reference a few lines later, when it made the resource the
present candidate. note_rtv releases the table's reference whenever it replaces or
clears a handle, so on another thread it can take the count to zero between the
two: the resource is freed while note_execute is still reading it.

rtv_resource_addref takes the reference while RTVS is locked, so the table cannot
let go of it first. note_execute releases it where the resource does not become
the candidate (not back-buffer like, or already the candidate); otherwise it is
the candidate's own reference, as before. The reference count ends up the same on
every path; only the order changed.

This path only runs until the title's first PresentX (the fallback guess).
rtv_resource and rt0_resource are not changed: their other callers are the
XCL_TRACE_FRAME diagnostics.

Tests: one for the reference (it outlives note_rtv clearing the handle and the
title's own release), and one that races note_rtv against rtv_resource_addref with
a fake COM object that records the release that takes it to zero.

Fixes xcl-dev#2381

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

@Anka-1623

Copy link
Copy Markdown
Author

A summary of what this PR does, what has been checked, and what is still open.

The problem (#2381)

In the PresentX fallback, note_execute looks up the resource of an RTV handle in RTVS, drops the table's lock, and then calls into the resource (backbuffer_like reads its desc through the vtable). It takes its own reference only later, when the resource becomes the present candidate. note_rtv releases the table's reference whenever it replaces or clears a handle. If that was the last reference and the release runs on another thread between the lookup and the call, note_execute reads a freed COM object.

The change

rtv_resource_addref does the lookup and the add_ref under the same RTVS lock, so the table cannot let go first. note_execute releases that reference where the resource does not become the candidate (not back-buffer-like, or already the candidate); otherwise it is the candidate's reference, as before. The net reference count is the same on every path. What differs is the order, and one extra AddRef/Release pair on the two paths that took none. In the "already the candidate" case the release runs under the CANDIDATE lock, but the candidate holds its own reference there, so it cannot be the last one.

What has been checked

  • The ordering is visible in the code on current main (present.rs, note_execute), and I searched the diffs of the open PRs that touch present.rs for changes to RTVS, note_rtv, note_execute and CANDIDATE: none changes this lookup.
  • cargo check --locked --target x86_64-pc-windows-msvc -p xcl --lib --tests passes with the same warnings as main (type-check only, run on Linux).
  • A standalone Linux model with the same RTVS / note_rtv / add_ref / release code and the test's fake COM object: the old pattern (a bare pointer out of the table) read an already-freed resource in 30 of 30 runs at the test's 20,000 rounds; rtv_resource_addref read none in 5 runs of 200,000 rounds, and every resource ended at exactly zero references.

What is an assumption, and has not been tested

  • That this window is reached in a real title. It follows from reading the code, as the issue report itself did; I have not seen it happen. It needs a second title thread to replace or clear the same RTV handle while note_execute is between the lookup and the call, and the fallback only runs until the title's first PresentX.
  • Everything on Windows: the two new tests have not been run, nor cargo test --workspace, nor scripts/smoke.ps1 on any title. The Linux model shows the race test can fail and that the helper closes this window in the model. It is not the repository's test run.

To move it forward

Someone with Windows, a GPU and a title can check it with:

cargo test -p xcl --lib rtv_resource_addref -- --nocapture
cargo test --workspace -- --test-threads=4
scripts/smoke.ps1 -Game <your game folder>      # on main, then on e37abac, same title, same time

To see the race test fail, comment out the add_ref(res); line in rtv_resource_addref and rerun the first command. Please name the commit (e37abac) in a report and paste the two xcl-smoke blocks under Regression run in the description or here. The PR stays a draft until those exist, and I will rebase if main moves.

Comment drafted with Claude Code (Claude Sonnet 5.5).

@ahmetbagci8

Copy link
Copy Markdown

Tested

  • Commit: e37abacbfa60c39bdb60912659c8c999762a8cf2
  • Hardware / OS: Intel Core i7-11800H, NVIDIA GeForce RTX 3070 Laptop GPU (driver 32.0.16.1714), Windows 11 Pro (Build 26300), DXC v1.9.2609
  • cargo test -j 2 -p xcl --lib present -- --test-threads=4: 11 passed; 0 failed; 0 ignored (0.07s)

Not tested

  • Full workspace suite and scripts/smoke.ps1 (no unpacked title locally).

AI use

  • Antigravity harness (Gemini 4 Neon) ran the targeted unit tests on this commit and drafted this report; the author reviewed it before posting.

@Anka-1623

Copy link
Copy Markdown
Author

Thanks for running it on e37abac. The present filter matches the 11 tests in present.rs, so that includes the two new ones (rtv_resource_addref_holds_a_reference_of_its_own and rtv_resource_addref_never_returns_a_freed_resource). I'll add your run to Tested with a link to this comment.

Still open: cargo test --workspace -- --test-threads=4 and the two xcl-smoke blocks (main and e37abac, same title, same time). The PR stays a draft until those are in.

@SlopTart

Copy link
Copy Markdown

Tested: current PR head merged onto current origin/main (clean), Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT (NAVI31), cargo target x86_64-pc-windows-gnu run under wine-11.0 (Proton), rustc 1.94.0, vkd3d-proton 3.1 with the XCL patch, official MS DXC (binaries 2026-09-29).

gpu_ suite (cold cache): 164 passed / 3 failed, no crashes. The three are the known baseline on this lane (seed-1003 fuzz, mad/mac contraction, promoted-UAV-clear driver gap, PR #2490); none caused by this PR.
Targeted run of rtv_resource_addref_holds_a_reference_of_its_own and rtv_resource_addref_never_returns_a_freed_resource: 2 passed.

Not tested: retail titles (no title data on this lane, counts only).

AI use: Hermes agent (local Qwen-class) ran both machines and drafted this; I checked it before posting.

@Anka-1623

Copy link
Copy Markdown
Author

Thanks, good to see both new tests pass under Wine too. I'm keeping the description's Tested section to runs on the supported setup (Windows, x86_64-pc-windows-msvc) that name the exact commit, so I'll leave this one here in the thread as an extra data point.

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…(port upstream xcl-dev#2520 + xcl-dev#2691)

(cherry picked from commit 87b65e230fd1e87fda0839146a41c2bca2420fb3)
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: TOCTOU use-after-free on RTVS resource pointer in present::note_execute

4 participants