Repository navigation
Conversation
…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>
|
Verification on RTX 3060 Ti (Windows 11), branch merged with current
Thanks! |
|
A summary of what this PR does, what has been checked, and what is still open. The problem (#2381) In the PresentX fallback, The change
What has been checked
What is an assumption, and has not been tested
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 Comment drafted with Claude Code (Claude Sonnet 5.5). |
Tested
Not tested
AI use
|
|
Thanks for running it on Still open: |
|
Tested: current PR head merged onto current
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. |
|
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, |
Fixes #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. D3D12.X: a queue the title releases is not kept alive by submit tracking #1691 (addsforget_queue, which also locksCANDIDATE) and D3D12.X: a released descriptor heap's RTV entries go with it #1771 (RTVSentries of a released heap) change nearby code, so a rebase may be needed.AI use
claude-sonnet-5-5): checked that D3D12.X: TOCTOU use-after-free on RTVS resource pointer in present::note_execute #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