From b3a969b72cbcbf951f0b99a3b2a67aa6b03a1176 Mon Sep 17 00:00:00 2001 From: shelbykb2 <80599220+shelbykb2@users.noreply.github.com> Date: Tue, 24 Nov 2026 02:17:18 -0700 Subject: [PATCH] D3D12.X: newer list layout #139 Reset goes through title_list::reset On the newer console command-list layout (real::QUEUE_SHIFTED), titles reset pooled lists through slot #139 Reset(allocator, initial_pso, flags), which real.rs routes to list_x::reset_x. title_list's wrapper vtable only overrode slots 0, 1, 2, 9, and 10, leaving slot #139 as a raw forwarder to F_BACKING. Once a mid-list Wait64 split switched F_BACKING to a continuation segment, #139 reset only that continuation segment and never ran title_list::reset: F_PRIMARY stayed bound to the previous recording/allocator, F_BACKING was never restored to F_PRIMARY, and extra_active kept accumulating split segments across frames. On pre-#897 main that caused D3D12 device removal (0x887a0001 / debug-layer id 541) as soon as the old allocator was reused; even with #897/#907 allocator pooling, stale primary/prior segments continue to be resubmitted and extra_active grows until MAX_EXTRA_SEGMENTS. The wrapper's #139 is now a guard::make_layout_split of its own: a title on the newer layout goes to a four-argument adapter that runs wrapper reset, while a title on the older layout and the runtime take the generated forwarder. The forwarder's tail jump keeps the caller's return address and arguments, which the backing list's own layout guard at #139 needs to tell a title from the runtime; calling the backing slot from Rust would hide it and send an older-layout title to the PC method. Tests: one covers the forwarder and the newer-layout reset on fake lists; one calls the slot from a caller in a registered mapped image, with the backing slot behind a layout guard as real.rs installs it and a test-local layout flag (flag clear: the older handler with the title's arguments; flag set: the primary is reset and the split segment reclaimed). Co-Authored-By: Antigravity Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/title_list.rs | 167 ++++++++++++++++++++++++ 1 file changed, 167 insertions(+) diff --git a/crates/xcl/src/hle/d3d12x/title_list.rs b/crates/xcl/src/hle/d3d12x/title_list.rs index 05de94b66d..22b86335bd 100644 --- a/crates/xcl/src/hle/d3d12x/title_list.rs +++ b/crates/xcl/src/hle/d3d12x/title_list.rs @@ -70,6 +70,7 @@ fn vtable() -> usize { v[2] = release as usize; v[9] = close as usize; v[10] = reset as usize; + v[139] = slot_139(v[139], &super::real::QUEUE_SHIFTED); Vtable(Box::leak(v.into_boxed_slice()).as_ptr() as usize) }) .0 @@ -184,6 +185,20 @@ unsafe extern "system" fn reset(this: *mut c_void, alloc: usize, pso: usize) -> hr } +/// #139: a title on the newer console list layout (`newer_layout`) calls Reset(allocator, +/// initial_pso, flags) there, which goes through wrapper `reset` so `F_PRIMARY` is reset and any +/// mid-list split segments are reclaimed (forwarded, it reset only the last continuation segment +/// and left `F_PRIMARY` on an old recording/allocator). A title on the older layout and the +/// runtime keep `forwarder`, the tail jump to `F_BACKING`: the backing list's own layout guard at +/// #139 tells a title by its return address, which a call from here would hide. +fn slot_139(forwarder: usize, newer_layout: &'static std::sync::atomic::AtomicBool) -> usize { + super::guard::make_layout_split(super::guard::LayoutTargets { older: forwarder, newer: reset_139 as usize, pc: forwarder, newer_layout }) +} + +unsafe extern "system" fn reset_139(this: *mut c_void, alloc: usize, pso: usize, _flags: usize) -> HRESULT { + reset(this, alloc, pso) +} + /// The console object for the PC list `pc` (adopted; one reference moves to it). pub unsafe fn wrap(pc: *mut c_void) -> *mut c_void { if pc.is_null() { @@ -441,4 +456,156 @@ mod tests { drop(Box::from_raw(fake)); } } + + #[repr(C)] + struct ResetFake { + vt: *const usize, + refs: u32, + reset_calls: u32, + slot139_calls: u32, + last_alloc: usize, + last_pso: usize, + last_arg3: usize, + } + + unsafe extern "system" fn rfake_reset(this: *mut c_void, alloc: usize, pso: usize) -> HRESULT { + let f = &mut *(this as *mut ResetFake); + f.reset_calls += 1; + f.last_alloc = alloc; + f.last_pso = pso; + 0 + } + unsafe extern "system" fn rfake_slot139(this: *mut c_void, a1: usize, a2: usize, a3: usize) -> HRESULT { + let f = &mut *(this as *mut ResetFake); + f.slot139_calls += 1; + f.last_alloc = a1; + f.last_pso = a2; + f.last_arg3 = a3; + 0 + } + unsafe extern "system" fn rfake_release(this: *mut c_void) -> u32 { + let f = &mut *(this as *mut ResetFake); + f.refs -= 1; + f.refs + } + + /// Newer-layout slot #139 after a mid-list split resets `F_PRIMARY`, restores `F_BACKING`, + /// and moves split segments to `spares` so stale primary lists are never resubmitted; on the + /// older layout slot #139 still forwards unchanged to `F_BACKING`. + #[test] + fn newer_layout_slot_139_resets_primary_and_reclaims_split_segments() { + let mut vt = vec![0usize; LIST_SLOTS]; + vt[2] = rfake_release as usize; + vt[10] = rfake_reset as usize; + vt[139] = rfake_slot139 as usize; + let primary = Box::into_raw(Box::new(ResetFake { vt: vt.as_ptr(), refs: 1, reset_calls: 0, slot139_calls: 0, last_alloc: 0, last_pso: 0, last_arg3: 0 })); + let seg1 = Box::into_raw(Box::new(ResetFake { vt: vt.as_ptr(), refs: 1, reset_calls: 0, slot139_calls: 0, last_alloc: 0, last_pso: 0, last_arg3: 0 })); + unsafe { + let o = wrap(primary as *mut c_void); + activate_segment(o, primary as *mut c_void, seg1 as *mut c_void); + assert_eq!(*((o as usize + F_BACKING) as *const usize), seg1 as usize); + + // Not a title caller: forwards slot #139 to the active backing segment without resetting. + static NEWER: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(true); + let s: unsafe extern "system" fn(*mut c_void, usize, usize, usize) -> HRESULT = std::mem::transmute(slot_139(forward(139), &NEWER)); + assert_eq!(s(o, 11, 22, 33), 0); + assert_eq!((*seg1).slot139_calls, 1); + assert_eq!(((*seg1).last_alloc, (*seg1).last_pso, (*seg1).last_arg3), (11, 22, 33)); + assert_eq!((*primary).reset_calls, 0); + + // Newer layout: runs wrapper Reset on F_PRIMARY and reclaims split segments. + assert_eq!(reset_139(o, 0xA110_C002, 0x5555, 0), 0); + assert_eq!((*primary).reset_calls, 1, "primary must receive Reset"); + assert_eq!((*primary).last_alloc, 0xA110_C002); + assert_eq!((*primary).last_pso, 0x5555); + assert_eq!((*seg1).reset_calls, 0, "split continuation must not receive #139 Reset"); + assert_eq!(*((o as usize + F_BACKING) as *const usize), primary as usize); + assert_eq!(wrapper_of(primary as *mut c_void), Some(o)); + assert_eq!(wrapper_of(seg1 as *mut c_void), None); + let mut segs = Vec::new(); + assert!(push_segments(o, &mut segs)); + assert_eq!(segs, vec![primary as *mut c_void]); + assert_eq!(pop_spare(o), Some(seg1 as *mut c_void)); + + let rel: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(o as *const *const usize)).add(2)); + assert_eq!(rel(o), 0); + drop(Box::from_raw(primary)); + drop(Box::from_raw(seg1)); + } + } + + unsafe extern "system" fn older_139(this: *mut c_void, a1: usize, a2: usize, a3: usize) -> HRESULT { + rfake_slot139(this, a1, a2, a3); + 1 + } + unsafe extern "system" fn newer_139(_: *mut c_void, _: usize, _: usize, _: usize) -> HRESULT { + 2 + } + unsafe extern "system" fn pc_139(_: *mut c_void, _: usize, _: usize, _: usize) -> HRESULT { + 3 + } + + /// A title calling #139 reaches the backing list's layout guard (as `real.rs` installs it) + /// with its own return address: on the older layout the older handler runs with the title's + /// arguments, not the PC method; on the newer layout the primary is reset. The layout flag is + /// the test's own, so no other test sees it change. + #[test] + fn slot_139_keeps_the_title_caller_for_the_backing_layout_guard() { + use std::sync::atomic::{AtomicBool, Ordering}; + use std::sync::Arc; + use windows_sys::Win32::System::Memory::{VirtualAlloc, VirtualFree, MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_EXECUTE_READWRITE}; + static NEWER: AtomicBool = AtomicBool::new(false); + let mut vt = vec![0usize; LIST_SLOTS]; + vt[2] = rfake_release as usize; + vt[10] = rfake_reset as usize; + vt[139] = super::super::guard::make_layout_split(super::super::guard::LayoutTargets { + older: older_139 as usize, + newer: newer_139 as usize, + pc: pc_139 as usize, + newer_layout: &NEWER, + }); + let primary = Box::into_raw(Box::new(ResetFake { vt: vt.as_ptr(), refs: 1, reset_calls: 0, slot139_calls: 0, last_alloc: 0, last_pso: 0, last_arg3: 0 })); + let seg1 = Box::into_raw(Box::new(ResetFake { vt: vt.as_ptr(), refs: 1, reset_calls: 0, slot139_calls: 0, last_alloc: 0, last_pso: 0, last_arg3: 0 })); + let s = slot_139(forward(139), &NEWER); + // the title: `sub rsp, 0x28; mov rax, s; call rax; add rsp, 0x28; ret` in a mapped image + let code = unsafe { VirtualAlloc(std::ptr::null(), 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE) as usize }; + assert_ne!(code, 0); + let mut c = vec![0x48, 0x83, 0xEC, 0x28, 0x48, 0xB8]; + c.extend_from_slice(&(s as u64).to_le_bytes()); + c.extend_from_slice(&[0xFF, 0xD0, 0x48, 0x83, 0xC4, 0x28, 0xC3]); + crate::modules::register_mapped(Arc::new(crate::modules::MappedModule { + name: "title_list_139.exe".into(), path: "title_list_139.exe".into(), base: code, size: 0x1000, entry_rva: 0, is_dll: false, + exports: Default::default(), tls_callbacks: Mutex::new(Vec::new()), no_thread_calls: AtomicBool::new(false), + initialized: AtomicBool::new(true), stack_reserve: 0, + })); + unsafe { + std::ptr::copy_nonoverlapping(c.as_ptr(), code as *mut u8, c.len()); + let title: unsafe extern "system" fn(*mut c_void, usize, usize, usize) -> HRESULT = std::mem::transmute(code); + let runtime: unsafe extern "system" fn(*mut c_void, usize, usize, usize) -> HRESULT = std::mem::transmute(s); + let o = wrap(primary as *mut c_void); + activate_segment(o, primary as *mut c_void, seg1 as *mut c_void); + + // older layout: the title's call reaches the older handler on the active segment + assert_eq!(title(o, 11, 22, 33), 1, "older layout: the older handler, not the PC method"); + assert_eq!(((*seg1).slot139_calls, (*seg1).last_alloc, (*seg1).last_pso, (*seg1).last_arg3), (1, 11, 22, 33)); + assert_eq!((*primary).reset_calls, 0); + // the runtime still reaches the PC method + assert_eq!(runtime(o, 0, 0, 0), 3); + + // newer layout: the title's call resets the primary and reclaims the split segment + NEWER.store(true, Ordering::Release); + assert_eq!(title(o, 0xA110_C002, 0x5555, 0), 0); + assert_eq!(((*primary).reset_calls, (*primary).last_alloc, (*primary).last_pso), (1, 0xA110_C002, 0x5555)); + assert_eq!((*seg1).slot139_calls, 1); + assert_eq!(*((o as usize + F_BACKING) as *const usize), primary as usize); + assert_eq!(pop_spare(o), Some(seg1 as *mut c_void)); + + crate::modules::unregister_mapped(code); + VirtualFree(code as *mut _, 0, MEM_RELEASE); + let rel: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(o as *const *const usize)).add(2)); + assert_eq!(rel(o), 0); + drop(Box::from_raw(primary)); + drop(Box::from_raw(seg1)); + } + } }