From 56f3dd72f2171c18776e196cb22b2feb9cb313c4 Mon Sep 17 00:00:00 2001 From: Tiago Leal <183942127+tiagonleal@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:03:23 +0000 Subject: [PATCH 1/2] Tests: the GPU-wait watchdog releases a bridged Wait64 whose producer was never submitted Part of #1452. Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/hle/d3d12x/list_x.rs | 82 +++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index ee544dec22..73332a0370 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -6225,6 +6225,88 @@ mod tests { } } + /// A bridged `Wait64BitValueX` whose `Write64BitValueX` producer was recorded but never + /// submitted (`prod=qNone`, wait-before-signal) holds its queue only until the sync + /// watchdog releases it on the CPU after `gpu_wait_watchdog_timeout()`; work submitted to + /// that queue afterwards must then run. The timeout is a `OnceLock` read from + /// `XCL_GPU_WAIT_TIMEOUT_MS`, so the test uses whatever it resolved to (5 s by default) + /// instead of setting a process-wide variable that could race with the other tests. + #[test] + fn unsubmitted_write64_producer_wait_is_released_by_the_watchdog() { + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + use windows::Win32::Graphics::Dxgi::*; + + unsafe { + let mut dev: Option = None; + if D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + let Ok(factory) = CreateDXGIFactory1::() else { + crate::gpu::recomp::runner::skip_test("no GPU"); + return; + }; + let Ok(warp) = factory.EnumWarpAdapter::() else { + crate::gpu::recomp::runner::skip_test("no GPU"); + return; + }; + if D3D12CreateDevice(&warp, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + crate::gpu::recomp::runner::skip_test("no GPU"); + return; + } + } + let dev = dev.unwrap(); + let dq: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: D3D12_COMMAND_LIST_TYPE_DIRECT, ..Default::default() }).unwrap(); + let da: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).unwrap(); + let ca: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE).unwrap(); + let dl: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &da, None).unwrap(); + let cl: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COMPUTE, &ca, None).unwrap(); + let done_fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + + let cell = Box::new(AtomicU64::new(0)); + let va = &*cell as *const AtomicU64 as u64; + let (dl_ptr, cl_ptr) = (dl.as_raw(), cl.as_raw()); + on_create(dl_ptr, da.as_raw() as usize, 0, D3D12_COMMAND_LIST_TYPE_DIRECT); + on_create(cl_ptr, ca.as_raw() as usize, 0, D3D12_COMMAND_LIST_TYPE_COMPUTE); + + // The COMPUTE list records the producer but is never submitted. + note_write64_recorded(cl_ptr, va, 0x677); + wait64_x(dl_ptr, va, D3D12_COMPARISON_FUNC_GREATER_EQUAL.0 as u32, 0x677, 1, 0); + dl.Close().unwrap(); + cl.Close().unwrap(); + + let t0 = Instant::now(); + // The DIRECT list is not executed: the queue hook would register the blocked queue + // among the title's known queues (`present::QUEUES`) for the other tests to find. + execute_with_waits_on(dq.as_raw(), &[dl_ptr], |_| {}); + // Work submitted after the wait is held behind it... + dq.Signal(&done_fence, 1).unwrap(); + std::thread::sleep(Duration::from_millis(300)); + assert_eq!(done_fence.GetCompletedValue(), 0, "the queue must be held by the armed wait"); + // (a queue hook installed by another test can run the submission twice: at least one) + let armed = |va| SYNC_GRAPH.lock().unwrap().as_ref().and_then(|g| g.cells.get(&va)).map(|c| (!c.armed_waits.is_empty() && c.armed_waits.iter().all(|a| a.val == 0x677), c.submitted_max, c.cpu_signaled_max)); + assert_eq!(armed(va), Some((true, 0, 0)), "armed wait(s) for 0x677, no producer signal yet"); + + // ...until the watchdog releases the wait on the CPU. + let timeout = gpu_wait_watchdog_timeout(); + while done_fence.GetCompletedValue() < 1 && t0.elapsed() < timeout + Duration::from_secs(15) { + std::thread::sleep(Duration::from_millis(25)); + } + let held = t0.elapsed(); + assert_eq!(done_fence.GetCompletedValue(), 1, "the watchdog must release a wait whose producer was never submitted"); + assert!(held >= timeout, "released after {held:?}, before the {timeout:?} timeout"); + assert_eq!(armed(va), Some((false, 0, 0x677)), "the expired waits are dropped and recorded as released on CPU"); + + // The queue runs more work after the release. + dq.Signal(&done_fence, 2).unwrap(); + crate::gpu::fence::wait(&done_fence, 2, &dev, None, 2000).expect("queue must keep executing after the release"); + + on_release(dl_ptr as usize); + on_release(cl_ptr as usize); + let tf = done_fence.as_raw() as usize; + crate::hle::d3d12x::fence_x::forget_fence_for_test(tf); + SYNC_GRAPH.lock().unwrap().as_mut().map(|gr| gr.title_fences.remove(&tf)); + SYNC_GRAPH.lock().unwrap().as_mut().map(|gr| gr.cells.remove(&va)); + } + } + /// Cross-queue cycle detection (`DIRECT` waits on `COMPUTE` while `COMPUTE` is already /// blocked on an unsubmitted write from `DIRECT`) and same-queue FIFO bypass must both /// complete immediately without stalling CPU or hanging GPU. From 82d0e7add48ae6312799b09bd35ffaa73319aece Mon Sep 17 00:00:00 2001 From: Tiago Leal <183942127+tiagonleal@users.noreply.github.com> Date: Wed, 25 Nov 2026 22:05:07 +0000 Subject: [PATCH 2/2] Tests: the Wait64 watchdog test cleans up its global state on a failed assertion too The test registered its two fake command lists and the fence in the global sync graph, but released them only after its last assertion. A failed assertion left raw addresses of dropped COM objects in global state, so other tests in the same process could fail for unrelated reasons. A local guard created right after the registrations now runs the same cleanup (on_release for both lists, forget_fence_for_test, title_fences and cells entries) on normal return and on unwind, before the COM lists and the cell drop. It takes over poisoned locks instead of panicking. With a deliberate panic after the guard, only that test failed and the other 245 tests of hle::d3d12x passed in the same process. Test alone x5 and hle::d3d12x x3: pass. cargo test --workspace --no-fail-fast x2: 1353 passed, 5 failed, 46 ignored (fuzz_validate_and_gpu_0..3 and lower_dxil::a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier, which also fails without this change), 25 SKIPPED lines. Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/hle/d3d12x/list_x.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 73332a0370..a1bfe3a740 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -6266,6 +6266,28 @@ mod tests { on_create(dl_ptr, da.as_raw() as usize, 0, D3D12_COMMAND_LIST_TYPE_DIRECT); on_create(cl_ptr, ca.as_raw() as usize, 0, D3D12_COMMAND_LIST_TYPE_COMPUTE); + // Releases the lists' and the fence's entries in the global test state on a normal + // return and when an assertion unwinds. Declared after the COM objects and `cell` + // so it drops before them; it never panics (poisoned locks are taken over). + struct GlobalStateGuard { + lists: [usize; 2], + fence: usize, + va: u64, + } + impl Drop for GlobalStateGuard { + fn drop(&mut self) { + for l in self.lists { + on_release(l); + } + crate::hle::d3d12x::fence_x::forget_fence_for_test(self.fence); + if let Some(gr) = SYNC_GRAPH.lock().unwrap_or_else(|e| e.into_inner()).as_mut() { + gr.title_fences.remove(&self.fence); + gr.cells.remove(&self.va); + } + } + } + let _state_guard = GlobalStateGuard { lists: [dl_ptr as usize, cl_ptr as usize], fence: done_fence.as_raw() as usize, va }; + // The COMPUTE list records the producer but is never submitted. note_write64_recorded(cl_ptr, va, 0x677); wait64_x(dl_ptr, va, D3D12_COMPARISON_FUNC_GREATER_EQUAL.0 as u32, 0x677, 1, 0); @@ -6297,13 +6319,6 @@ mod tests { // The queue runs more work after the release. dq.Signal(&done_fence, 2).unwrap(); crate::gpu::fence::wait(&done_fence, 2, &dev, None, 2000).expect("queue must keep executing after the release"); - - on_release(dl_ptr as usize); - on_release(cl_ptr as usize); - let tf = done_fence.as_raw() as usize; - crate::hle::d3d12x::fence_x::forget_fence_for_test(tf); - SYNC_GRAPH.lock().unwrap().as_mut().map(|gr| gr.title_fences.remove(&tf)); - SYNC_GRAPH.lock().unwrap().as_mut().map(|gr| gr.cells.remove(&va)); } }