From 008201c4ee8204d1e4c28fc6eee0409b6b26314b Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 25 Nov 2026 19:24:20 -0700 Subject: [PATCH 1/4] D3D12.X: a put-off call's segment that does not close is not submitted The path that puts a call off to a segment of its own, recorded at the submit (#2118), ignored the native Close result twice: deferred_segment added the new empty segment to the recording whatever its Close returned, and record_deferred closed the segment after recording the call, ignored the result and chained the next segment to it. A segment whose recording the runtime refused was submitted with the rest: in a test, submitting one removed the device (0x887A0001, an invalid call). Found by KOLESNiii in a static review on #2088. A put-off segment that does not close when it is made is not used: it goes back to the spares and the call waits in its list, as before #2118. One that does not close after its call is recorded leaves the recording (a list whose Close failed is still open and cannot be reset), its call is dropped and counted, and the next one does not follow it. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/list_x.rs | 176 +++++++++++++++++++++++- crates/xcl/src/hle/d3d12x/title_list.rs | 10 ++ 2 files changed, 184 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index ee544dec22..dddb6c4d56 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -2711,12 +2711,45 @@ pub(super) unsafe fn record_deferred(wrapper: *mut c_void) { Deferred::Indirect(sig, max, args, args_off, count_buf, count_off) => super::bind::execute_indirect(seg, sig, max, args, args_off, count_buf, count_off), } let ext = *(seg as *const usize); - pc:: i32>(seg, PC_CLOSE)(seg); + let hr = segment_close(wrapper, seg); real::preserve_vtable(seg, ext); + if hr < 0 { + // a recording the runtime refused is not submitted: the segment leaves the recording + // (a list whose Close failed is still open and cannot be reset), its call is dropped, + // and the next one does not follow it + static CLOSE_ERR: AtomicU64 = AtomicU64::new(0); + warn(&CLOSE_ERR, "deferred call", format_args!("Close failed ({hr:#x}), the call is dropped")); + super::bind::dispatch_dropped(); + super::title_list::drop_segment(wrapper, seg); + continue; + } last = Some(seg); } } +/// (tests) What the next native Close of a put-off segment of this wrapper does instead +/// (`segment_close`; tests run in parallel, so it is the given wrapper's only). +#[cfg(test)] +type CloseHook = Box i32 + Send>; +#[cfg(test)] +static CLOSE_HOOK: Mutex> = Mutex::new(Vec::new()); + +/// The native Close of `wrapper`'s put-off segment `p`: its HRESULT. +unsafe fn segment_close(wrapper: *mut c_void, p: *mut c_void) -> i32 { + #[cfg(test)] + { + let hook = { + let mut g = CLOSE_HOOK.lock().unwrap(); + g.iter().position(|h| h.0 == wrapper as usize).map(|i| g.remove(i).1) + }; + if let Some(hook) = hook { + return hook(p); + } + } + let _ = wrapper; + pc:: i32>(p, PC_CLOSE)(p) +} + /// The allocator `list`'s put-off calls recorded on is reset once the GPU is done with them, so /// their split dispatches' scratch does not outlive them. unsafe fn reset_own_alloc(list: *mut c_void) { @@ -2969,8 +3002,15 @@ unsafe fn deferred_segment(wrapper: *mut c_void, list: *mut c_void, ty: D3D12_CO None => real::create_raw_pc_command_list(dev.as_raw(), ty, super::alloc_pool::current(own), 0)?, }; let ext = *(p as *const usize); - pc:: i32>(p, PC_CLOSE)(p); + let hr = segment_close(wrapper, p); real::preserve_vtable(p, ext); + if hr < 0 { + // a list that did not close is no segment: the call waits in its list instead + static CLOSE_ERR: AtomicU64 = AtomicU64::new(0); + warn(&CLOSE_ERR, "put-off segment", format_args!("Close failed ({hr:#x})")); + super::title_list::put_spare(wrapper, p); + return None; + } super::title_list::push_segment(wrapper, p); Some(p) } @@ -7079,6 +7119,138 @@ mod tests { } } + /// A direct queue, its allocator and a title list wrapped as XCL wraps one, bound to `standin` + /// (None: no adapter). + unsafe fn put_off_rig(standin: usize) -> Option<(ID3D12Device, ID3D12CommandQueue, ID3D12CommandAllocator, *mut c_void, *mut c_void)> { + use super::super::{bind, title_list}; + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + use windows::Win32::Graphics::Dxgi::*; + let mut dev: Option = None; + if D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + let warp = CreateDXGIFactory1::().and_then(|f| f.EnumWarpAdapter::()); + if warp.and_then(|w| D3D12CreateDevice(&w, D3D_FEATURE_LEVEL_11_0, &mut dev)).is_err() { + return None; + } + } + let dev = dev.unwrap(); + let ty = D3D12_COMMAND_LIST_TYPE_DIRECT; + let queue: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: ty, ..Default::default() }).unwrap(); + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(ty).unwrap(); + let raw: ID3D12GraphicsCommandList = dev.CreateCommandList(0, ty, &alloc, None).unwrap(); + let pc0 = raw.into_raw(); + real::adopt(pc0, real::Kind::CommandList); + on_create(pc0, alloc.as_raw() as usize, 0, ty); + bind::reset(pc0, alloc.as_raw() as usize, standin); + let wrap = title_list::wrap(pc0); + Some((dev, queue, alloc, pc0, wrap)) + } + + /// A put-off call whose recording the driver refuses at the submit (its segment's Close + /// fails) is not submitted as it is: the segment goes out empty, the call is dropped, and the + /// rest of the submission runs (its fence is signalled, the device stays). + #[test] + fn a_put_off_call_whose_close_fails_is_not_submitted() { + use super::super::{bind, pso_async, title_list}; + use std::time::{Duration, Instant}; + unsafe { + let standin = 0x7E57_C105usize; + static BUILD: AtomicBool = AtomicBool::new(false); + BUILD.store(false, Ordering::Release); + pso_async::submit(standin, true, Box::new(|| { + let t = Instant::now(); + while !BUILD.load(Ordering::Acquire) && t.elapsed() < Duration::from_secs(10) { + std::thread::sleep(Duration::from_millis(5)); + } + 0 + })); + let Some((dev, queue, _alloc, pc0, wrap)) = put_off_rig(standin) else { + crate::gpu::recomp::runner::skip_test("no GPU"); + return; + }; + bind::dispatch(pc0, 1, 1, 1); + let mut segs = Vec::new(); + assert!(title_list::push_segments(wrap, &mut segs)); + assert_eq!(segs.len(), 3, "the dispatch was put off"); + + // the replay's recording fails in the runtime: a query begun and never ended + let mut heap: Option = None; + dev.CreateQueryHeap(&D3D12_QUERY_HEAP_DESC { Type: D3D12_QUERY_HEAP_TYPE_OCCLUSION, Count: 1, NodeMask: 0 }, &mut heap).unwrap(); + let heap = heap.unwrap(); + let h = heap.as_raw() as usize; + static REFUSED: AtomicU32 = AtomicU32::new(0); + CLOSE_HOOK.lock().unwrap().push((wrap as usize, Box::new(move |p| unsafe { + pc::(p, PC_BEGIN_QUERY)(p, h, D3D12_QUERY_TYPE_OCCLUSION, 0); + let hr = pc:: i32>(p, PC_CLOSE)(p); + REFUSED.store(hr as u32, Ordering::Release); + hr + }))); + let close_fn: unsafe extern "system" fn(*mut c_void) -> i32 = std::mem::transmute(*(*(wrap as *const *const usize)).add(9)); + assert_eq!(close_fn(wrap), 0); + BUILD.store(true, Ordering::Release); + record_deferred(wrap); + assert!(!CLOSE_HOOK.lock().unwrap().iter().any(|h| h.0 == wrap as usize), "the replay closed its segment"); + assert!((REFUSED.load(Ordering::Acquire) as i32) < 0, "the runtime refused the recording ({:#x})", REFUSED.load(Ordering::Acquire)); + // what the submit collects after the replay (`q_execute_command_lists`) + segs.clear(); + assert!(title_list::push_segments(wrap, &mut segs)); + assert_eq!(segs.len(), 2, "the refused segment is not submitted"); + + let done: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + type ExecFn = unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void); + let exec: ExecFn = std::mem::transmute(*(*(queue.as_raw() as *const *const usize)).add(PC_QUEUE_EXECUTE_COMMAND_LISTS)); + exec(queue.as_raw(), segs.len() as u32, segs.as_ptr()); + queue.Signal(&done, 1).unwrap(); + crate::gpu::fence::wait(&done, 1, &dev, None, 2000).expect("the submission runs"); + assert!(dev.GetDeviceRemovedReason().is_ok(), "the device stays"); + + let rel_fn: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(wrap as *const *const usize)).add(2)); + rel_fn(wrap); + let _ = pso_async::released(standin); + } + } + + /// A put-off segment that does not close when it is made is not used: the call waits in its + /// list instead of being put off. + #[test] + fn a_put_off_segment_that_does_not_close_is_not_used() { + use super::super::{bind, pso_async, title_list}; + unsafe { + let standin = 0x7E57_C106usize; + static BUILD: AtomicBool = AtomicBool::new(false); + BUILD.store(false, Ordering::Release); + pso_async::submit(standin, true, Box::new(|| { + let t = std::time::Instant::now(); + while !BUILD.load(Ordering::Acquire) && t.elapsed() < std::time::Duration::from_secs(10) { + std::thread::sleep(std::time::Duration::from_millis(5)); + } + 0 + })); + let Some((_dev, _queue, _alloc, pc0, wrap)) = put_off_rig(standin) else { + crate::gpu::recomp::runner::skip_test("no GPU"); + return; + }; + CLOSE_HOOK.lock().unwrap().push((wrap as usize, Box::new(|p| unsafe { + pc:: i32>(p, PC_CLOSE)(p); + windows::Win32::Foundation::E_FAIL.0 + }))); + // (the call that is not put off waits for the build in the call; started here, after + // the device is made, so the build is still going when the call comes) + let ends = std::thread::spawn(|| { + std::thread::sleep(std::time::Duration::from_millis(300)); + BUILD.store(true, Ordering::Release); + }); + bind::dispatch(pc0, 1, 1, 1); + ends.join().unwrap(); + assert!(!CLOSE_HOOK.lock().unwrap().iter().any(|h| h.0 == wrap as usize), "the put-off segment was closed"); + let mut segs = Vec::new(); + assert!(title_list::push_segments(wrap, &mut segs)); + assert_eq!(segs.len(), 1, "nothing was put off"); + let rel_fn: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(wrap as *const *const usize)).add(2)); + rel_fn(wrap); + let _ = pso_async::released(standin); + } + } + /// Reproduces the exact intra-list fork-join pattern seen on retail compute queues: /// q_compute list O_c: Write64(cell_A, frame) -> Wait64(cell_B >= frame) -> Write64(cell_C, frame) /// q_direct list O_d: Wait64(cell_A >= frame) -> Write64(cell_B, frame) diff --git a/crates/xcl/src/hle/d3d12x/title_list.rs b/crates/xcl/src/hle/d3d12x/title_list.rs index a41e5643fc..2aa8a6cb86 100644 --- a/crates/xcl/src/hle/d3d12x/title_list.rs +++ b/crates/xcl/src/hle/d3d12x/title_list.rs @@ -292,6 +292,16 @@ pub(super) fn put_spare(wrapper: *mut c_void, pc: *mut c_void) { sg.get_or_insert_with(HashMap::new).entry(wrapper as usize).or_default().spares.push(pc as usize); } +/// Take segment `pc` out of `wrapper`'s recording, so it is not submitted (its recording failed); +/// it stays with the spares, released with them. +pub(super) fn drop_segment(wrapper: *mut c_void, pc: *mut c_void) { + let mut sg = SEGMENTS.lock().unwrap(); + if let Some(b) = sg.as_mut().and_then(|m| m.get_mut(&(wrapper as usize))) { + b.extra_active.retain(|&p| p != pc as usize); + b.spares.push(pc as usize); + } +} + /// Add the closed segment `pc` to `wrapper`'s recording, after the ones it has. pub(super) fn push_segment(wrapper: *mut c_void, pc: *mut c_void) { SEGMENTS.lock().unwrap().get_or_insert_with(HashMap::new).entry(wrapper as usize).or_default().extra_active.push(pc as usize); From f0f9daee2d7e7cf5b46aa0d9b6a1beb46b94b5dc Mon Sep 17 00:00:00 2001 From: unknown <125705260+KDenKC@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:22:23 -0700 Subject: [PATCH 2/4] D3D12.X: release refused deferred segments Apply thehollowedone's patch and reference-count regression from #2622 comment 7187891221. A list whose Close failed cannot be reset, so release it rather than losing its owner on later spare reuse, and clear its dispatch-chain predecessor. Adjust the suggested initial-refusal Release call to use the adopted vtable too: cl_release must clean address-keyed state instead of bypassing it through orig_fn. This is the same lifecycle class discussed in #1453 and #2691. Assisted-by: Codex (GPT-6.1-Sol) --- crates/xcl/src/hle/d3d12x/list_x.rs | 14 +++++++++++++- crates/xcl/src/hle/d3d12x/title_list.rs | 11 +++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index dddb6c4d56..6d88ca036e 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -3008,7 +3008,9 @@ unsafe fn deferred_segment(wrapper: *mut c_void, list: *mut c_void, ty: D3D12_CO // a list that did not close is no segment: the call waits in its list instead static CLOSE_ERR: AtomicU64 = AtomicU64::new(0); warn(&CLOSE_ERR, "put-off segment", format_args!("Close failed ({hr:#x})")); - super::title_list::put_spare(wrapper, p); + real::dispatch_chain::list_reset(p as usize); + let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(p as *const *const usize)).add(2)); + release(p); return None; } super::title_list::push_segment(wrapper, p); @@ -7171,6 +7173,10 @@ mod tests { let mut segs = Vec::new(); assert!(title_list::push_segments(wrap, &mut segs)); assert_eq!(segs.len(), 3, "the dispatch was put off"); + let refused = segs[1]; + let add_ref: unsafe extern "system" fn(*mut c_void) -> u32 = + std::mem::transmute(*(*(refused as *const *const usize)).add(1)); + add_ref(refused); // Keep a test reference so final Release can reveal a lost owner. // the replay's recording fails in the runtime: a query begun and never ended let mut heap: Option = None; @@ -7203,8 +7209,14 @@ mod tests { crate::gpu::fence::wait(&done, 1, &dev, None, 2000).expect("the submission runs"); assert!(dev.GetDeviceRemovedReason().is_ok(), "the device stays"); + // The failed-Close list cannot Reset. Its next spare reuse must not lose the + // wrapper's owning reference when a new segment is made instead. + assert!(deferred_segment(wrap, pc0, D3D12_COMMAND_LIST_TYPE_DIRECT).is_some()); let rel_fn: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(wrap as *const *const usize)).add(2)); rel_fn(wrap); + let release_ref: unsafe extern "system" fn(*mut c_void) -> u32 = + std::mem::transmute(*(*(refused as *const *const usize)).add(2)); + assert_eq!(release_ref(refused), 0, "the refused segment retained an untracked owner"); let _ = pso_async::released(standin); } } diff --git a/crates/xcl/src/hle/d3d12x/title_list.rs b/crates/xcl/src/hle/d3d12x/title_list.rs index 2aa8a6cb86..882139e80f 100644 --- a/crates/xcl/src/hle/d3d12x/title_list.rs +++ b/crates/xcl/src/hle/d3d12x/title_list.rs @@ -292,14 +292,17 @@ pub(super) fn put_spare(wrapper: *mut c_void, pc: *mut c_void) { sg.get_or_insert_with(HashMap::new).entry(wrapper as usize).or_default().spares.push(pc as usize); } -/// Take segment `pc` out of `wrapper`'s recording, so it is not submitted (its recording failed); -/// it stays with the spares, released with them. -pub(super) fn drop_segment(wrapper: *mut c_void, pc: *mut c_void) { +/// Take a refused segment out of `wrapper`'s recording and release it. A list whose Close failed +/// cannot be reset, so it cannot be kept as a reusable spare. +pub(super) unsafe fn drop_segment(wrapper: *mut c_void, pc: *mut c_void) { let mut sg = SEGMENTS.lock().unwrap(); if let Some(b) = sg.as_mut().and_then(|m| m.get_mut(&(wrapper as usize))) { b.extra_active.retain(|&p| p != pc as usize); - b.spares.push(pc as usize); } + drop(sg); + super::real::dispatch_chain::list_reset(pc as usize); + let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(pc_slot(pc, 2)); + release(pc); } /// Add the closed segment `pc` to `wrapper`'s recording, after the ones it has. From 8cb07b8d610c866066eec7c21bd592008303d61f Mon Sep 17 00:00:00 2001 From: unknown <125705260+KDenKC@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:01:26 -0700 Subject: [PATCH 3/4] Tests: skip deferred Close failure test when the runtime accepts the query Suggested-by: Royalflamejlh Assisted-by: Codex (GPT-6.2-Sol) --- crates/xcl/src/hle/d3d12x/list_x.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 6d88ca036e..35bde4a828 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -7195,7 +7195,18 @@ mod tests { BUILD.store(true, Ordering::Release); record_deferred(wrap); assert!(!CLOSE_HOOK.lock().unwrap().iter().any(|h| h.0 == wrap as usize), "the replay closed its segment"); - assert!((REFUSED.load(Ordering::Acquire) as i32) < 0, "the runtime refused the recording ({:#x})", REFUSED.load(Ordering::Acquire)); + let release = |list: *mut c_void| { + let f: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(list as *const *const usize)).add(2)); + f(list) + }; + if (REFUSED.load(Ordering::Acquire) as i32) >= 0 { + // Some runtimes accept Close with an open query, so this run never hits the failure path. + release(wrap); + release(refused); + let _ = pso_async::released(standin); + crate::gpu::recomp::runner::skip_test("the runtime closed a list with a query left open"); + return; + } // what the submit collects after the replay (`q_execute_command_lists`) segs.clear(); assert!(title_list::push_segments(wrap, &mut segs)); @@ -7212,11 +7223,8 @@ mod tests { // The failed-Close list cannot Reset. Its next spare reuse must not lose the // wrapper's owning reference when a new segment is made instead. assert!(deferred_segment(wrap, pc0, D3D12_COMMAND_LIST_TYPE_DIRECT).is_some()); - let rel_fn: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(wrap as *const *const usize)).add(2)); - rel_fn(wrap); - let release_ref: unsafe extern "system" fn(*mut c_void) -> u32 = - std::mem::transmute(*(*(refused as *const *const usize)).add(2)); - assert_eq!(release_ref(refused), 0, "the refused segment retained an untracked owner"); + release(wrap); + assert_eq!(release(refused), 0, "the refused segment retained an untracked owner"); let _ = pso_async::released(standin); } } From b4a8ad8601bf37ed34b822fe44dbf25e82b57913 Mon Sep 17 00:00:00 2001 From: unknown <125705260+KDenKC@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:09:49 -0700 Subject: [PATCH 4/4] Tests: remove redundant unsafe from deferred Close hooks Reported-by: Decodere Assisted-by: Codex (GPT-6.2-Sol) --- crates/xcl/src/hle/d3d12x/list_x.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 35bde4a828..91d47708ae 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -7184,7 +7184,7 @@ mod tests { let heap = heap.unwrap(); let h = heap.as_raw() as usize; static REFUSED: AtomicU32 = AtomicU32::new(0); - CLOSE_HOOK.lock().unwrap().push((wrap as usize, Box::new(move |p| unsafe { + CLOSE_HOOK.lock().unwrap().push((wrap as usize, Box::new(move |p| { pc::(p, PC_BEGIN_QUERY)(p, h, D3D12_QUERY_TYPE_OCCLUSION, 0); let hr = pc:: i32>(p, PC_CLOSE)(p); REFUSED.store(hr as u32, Ordering::Release); @@ -7249,7 +7249,7 @@ mod tests { crate::gpu::recomp::runner::skip_test("no GPU"); return; }; - CLOSE_HOOK.lock().unwrap().push((wrap as usize, Box::new(|p| unsafe { + CLOSE_HOOK.lock().unwrap().push((wrap as usize, Box::new(|p| { pc:: i32>(p, PC_CLOSE)(p); windows::Win32::Foundation::E_FAIL.0 })));