From 49c68f2c13ed6741ba93fe1a27f9870390483d92 Mon Sep 17 00:00:00 2001 From: Sergey Aksenov Date: Thu, 26 Nov 2026 01:31:31 +0100 Subject: [PATCH] D3D12.X: a list vtable at a reused heap address gets its own methods Agility SDK 1.620 keeps an open command list's vtable in heap memory that is freed with the list. class_for cached the extended vtable by the runtime vtable's address and copied the runtime's methods only the first time, so a direct list whose open vtable came where a released compute list's had been called the compute list's methods: its ClearDepthStencilView made Close fail with E_FAIL. That is how vram's D32S8 test failed in parallel runs (2-5 in 10 with hle::d3d12x and one more module on 4 threads, RTX 5080; 0 in 12 with this change). A class of a vtable outside any module now keeps the methods it copied and is taken only while they are still the ones there. Fixes #2588 --- crates/xcl/src/hle/d3d12x/real.rs | 85 +++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 9238c582dc..c1893996e5 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -944,6 +944,9 @@ struct Class { iface: &'static Iface, public: usize, scanned: usize, + /// The runtime methods copied into `ext`, for a vtable outside any module: Agility SDK 1.620+ + /// frees an open command list's heap vtable with the list, and another's can take its address. + copied: Option>, } static CLASSES: Mutex> = Mutex::new(Vec::new()); @@ -973,6 +976,7 @@ pub(super) fn test_command_list_class(orig: usize, ext: usize) -> TestCommandLis iface, public: iface.methods.len(), scanned: iface.methods.len(), + copied: None, }); TestCommandListClass { ext } } @@ -1157,10 +1161,11 @@ pub unsafe fn adopt(obj: *mut c_void, kind: Kind) { } unsafe fn class_for(obj: *mut c_void, orig: usize, kind: Kind) -> usize { - if let Some(c) = CLASSES.lock().unwrap().iter().find(|c| c.orig == orig && c.kind == kind) { + let ot = orig as *const usize; + let same_methods = |c: &Class| c.copied.as_ref().is_none_or(|m| m.iter().enumerate().all(|(i, &f)| *ot.add(i) == f)); + if let Some(c) = CLASSES.lock().unwrap().iter().find(|c| c.orig == orig && c.kind == kind && same_methods(c)) { return c.ext; } - let ot = orig as *const usize; // most derived PC interface this object implements let chain = layout::chain(kind.base()); let qi: QiFn = std::mem::transmute(*ot); @@ -1272,7 +1277,8 @@ unsafe fn class_for(obj: *mut c_void, orig: usize, kind: Kind) -> usize { scanned - public, crate::modules::describe_address(orig) ); - CLASSES.lock().unwrap().push(Class { orig, kind, ext, iface, public, scanned }); + let copied = (module_of(orig) == 0).then(|| (0..scanned).map(|i| *ot.add(i)).collect()); + CLASSES.lock().unwrap().push(Class { orig, kind, ext, iface, public, scanned, copied }); ext } @@ -11656,6 +11662,7 @@ mod mesh_stream_tests { iface, public: iface.methods.len(), scanned: iface.methods.len(), + copied: None, }); { let mut roots = ABI_RS_SAMPLERS.lock().unwrap(); @@ -12396,3 +12403,75 @@ mod heap_release_order_tests { } } +#[cfg(test)] +mod class_tests { + use super::*; + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + use windows::Win32::Graphics::Dxgi::Common::*; + + #[test] + fn a_direct_list_at_a_released_compute_lists_vtable_records_with_its_own_methods() { + let Ok(dev) = crate::gpu::agility::create_device(None, D3D_FEATURE_LEVEL_11_0) else { + return crate::gpu::recomp::runner::skip_test("no GPU"); + }; + unsafe { + adopt(dev.as_raw(), Kind::Device); + let desc = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + Width: 64, + Height: 64, + DepthOrArraySize: 1, + MipLevels: 1, + Format: DXGI_FORMAT_D32_FLOAT, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Flags: D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, + ..Default::default() + }; + let clear = D3D12_CLEAR_VALUE { Format: DXGI_FORMAT_D32_FLOAT, Anonymous: D3D12_CLEAR_VALUE_0 { DepthStencil: D3D12_DEPTH_STENCIL_VALUE { Depth: 1.0, Stencil: 0 } } }; + let heap = D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }; + let mut depth: Option = None; + dev.CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_DEPTH_WRITE, Some(&clear), &mut depth).unwrap(); + let dsv_heap: ID3D12DescriptorHeap = dev.CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { Type: D3D12_DESCRIPTOR_HEAP_TYPE_DSV, NumDescriptors: 1, ..Default::default() }).unwrap(); + let dsv = dsv_heap.GetCPUDescriptorHandleForHeapStart(); + dev.CreateDepthStencilView(depth.as_ref().unwrap(), None, dsv); + let compute_alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE).unwrap(); + let direct_alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).unwrap(); + let runtime_vtable = |l: &ID3D12GraphicsCommandList| { + let v = vt(l.as_raw()); + if is_ext(v as usize) { *v.sub(1) } else { v as usize } + }; + // the only class of `c` is one made from the methods there now + let made_from_now = |c: usize| { + let classes = CLASSES.lock().unwrap(); + let mut of_c = classes.iter().filter(|k| k.orig == c && k.kind == Kind::CommandList); + match (of_c.next(), of_c.next()) { + (Some(k), None) => k.copied.as_ref().is_some_and(|m| m.iter().enumerate().all(|(i, &f)| *(c as *const usize).add(i) == f)), + _ => false, + } + }; + let mut compute_made = HashSet::new(); + for _ in 0..200 { + let compute: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COMPUTE, &compute_alloc, None).unwrap(); + let c = runtime_vtable(&compute); + if made_from_now(c) { + compute_made.insert(c); + } + compute.Close().unwrap(); + drop(compute); + compute_alloc.Reset().unwrap(); + + let direct: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &direct_alloc, None).unwrap(); + let d = runtime_vtable(&direct); + if compute_made.contains(&d) { + direct.ClearDepthStencilView(dsv, D3D12_CLEAR_FLAG_DEPTH, 0.5, 0, None); + assert!(direct.Close().is_ok(), "ClearDepthStencilView of a direct list at a compute list's vtable {d:#x} failed its Close"); + return; + } + direct.Close().unwrap(); + drop(direct); + direct_alloc.Reset().unwrap(); + } + } + crate::gpu::recomp::runner::skip_test("no direct list got a released compute list's vtable"); + } +}