D3D12.X: note_execute references its RTV resource before using it (#2381) #2520
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!2520
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2520"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Anka-1623 on GitHub · 2026-11-25 21:24 UTC · original
Anka-1623:present-note-execute-rtv-reference→main· +111 −3 · 1 files · 1 commits · draftFixes #2381.
note_execute(the PresentX fallback inpresent.rs) copies a resource pointer out ofRTVS, 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_rtvreleases 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, andnote_executethen reads a freed COM object.This takes the reference while the table is still locked, so
note_rtvcannot release it first. The fallback only runs until the title's firstPresentX.What is in it
present.rs: newrtv_resource_addref(handle)looks the handle up andadd_refs the resource under theRTVSlock. 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_executeused to take that one itself, with anadd_refjust beforeCANDIDATE.replace). The reference count ends up the same on every path; only the order changed. The cost is one extraAddRef/Releasepair on the two paths that took none before.present.rs, tests:rtv_resource_addref_holds_a_reference_of_its_own(deterministic: the reference outlivesnote_rtvclearing the handle and the title's own release) andrtv_resource_addref_never_returns_a_freed_resource(one thread keeps rewriting a handle withnote_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_resourcealso return bare pointers, but their other callers (real.rs,bind.rs) are theXCL_TRACE_FRAMEdiagnostics.present_nowreadsCANDIDATE's resource the same way: it copiesresout of theCANDIDATElock and callsbackbuffer_like(res)after the guard is gone, whilenote_executecanreplacethe candidate and release the old one. It looks like the same kind of window; I did not change or test it.Tested
x86_64-pc-windows-msvctarget, nothing linked):cargo check --locked --target x86_64-pc-windows-msvc -p xcl --lib --testspasses onmain(6d12f58) and on this branch, with the same 3 + 2 warnings and none inpresent.rs. A local shim stood in forlib.exeonly so thatlibudis86-sys's build script could run; nothing in the repository changes for it.RTVS/note_rtv/add_ref/releasecode 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). Withrtv_resource_addrefit 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.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 inpresent.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
cargo test --workspacehas not been run, no GPU of any vendor, no retail title.scripts/smoke.ps1run, so there are no Regression run blocks. This PR changescrates/xcl/src, soPR checkswill 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.present.rsfor one that changes this lookup ornote_execute's candidate update and found none. #1691 (addsforget_queue, which also locksCANDIDATE) and #1771 (RTVSentries of a released heap) change nearby code, so a rebase may be needed.AI use
claude-sonnet-5-5): checked that #2381 is still unfixed onmainand not covered by an open PR, wrote the change and both tests, set up the type-check and the Linux model, and drafted this description. The change is small enough to review in a few minutes.🤖 Generated with Claude Code
@DavidM1911 on GitHub · 2026-11-25 21:42 UTC · original
Verification on RTX 3060 Ti (Windows 11), branch merged with current
main:cargo test -p xcl --lib: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.)Thanks!
@Anka-1623 on GitHub · 2026-11-25 22:17 UTC · original
A summary of what this PR does, what has been checked, and what is still open.
The problem (#2381)
In the PresentX fallback,
note_executelooks up the resource of an RTV handle inRTVS, drops the table's lock, and then calls into the resource (backbuffer_likereads its desc through the vtable). It takes its own reference only later, when the resource becomes the present candidate.note_rtvreleases 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_executereads a freed COM object.The change
rtv_resource_addrefdoes the lookup and theadd_refunder the sameRTVSlock, so the table cannot let go first.note_executereleases 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 extraAddRef/Releasepair on the two paths that took none. In the "already the candidate" case the release runs under theCANDIDATElock, but the candidate holds its own reference there, so it cannot be the last one.What has been checked
main(present.rs,note_execute), and I searched the diffs of the open PRs that touchpresent.rsfor changes toRTVS,note_rtv,note_executeandCANDIDATE: none changes this lookup.cargo check --locked --target x86_64-pc-windows-msvc -p xcl --lib --testspasses with the same warnings asmain(type-check only, run on Linux).RTVS/note_rtv/add_ref/releasecode 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_addrefread 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
note_executeis between the lookup and the call, and the fallback only runs until the title's firstPresentX.cargo test --workspace, norscripts/smoke.ps1on 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:
To see the race test fail, comment out the
add_ref(res);line inrtv_resource_addrefand rerun the first command. Please name the commit (e37abac) in a report and paste the twoxcl-smokeblocks under Regression run in the description or here. The PR stays a draft until those exist, and I will rebase ifmainmoves.Comment drafted with Claude Code (Claude Sonnet 5.5).
@ahmetbagci8 on GitHub · 2026-11-25 22:22 UTC · original
Tested
e37abacbfa60c39bdb60912659c8c999762a8cf232.0.16.1714), Windows 11 Pro (Build 26300), DXCv1.9.2609cargo test -j 2 -p xcl --lib present -- --test-threads=4:11 passed; 0 failed; 0 ignored (0.07s)Not tested
scripts/smoke.ps1(no unpacked title locally).AI use
@Anka-1623 on GitHub · 2026-11-25 22:29 UTC · original
Thanks for running it on
e37abac. Thepresentfilter matches the 11 tests inpresent.rs, so that includes the two new ones (rtv_resource_addref_holds_a_reference_of_its_ownandrtv_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=4and the twoxcl-smokeblocks (mainande37abac, same title, same time). The PR stays a draft until those are in.@SlopTart on GitHub · 2026-11-25 22:49 UTC · original
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 targetx86_64-pc-windows-gnurun 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_ownandrtv_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 on GitHub · 2026-11-25 22:57 UTC · original
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.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.