From a8d712f1ecfcd0fff2b3755841306d343f98929e Mon Sep 17 00:00:00 2001 From: abilliontokens <39147239+abilliontokens@users.noreply.github.com> Date: Mon, 23 Nov 2026 19:33:25 -0800 Subject: [PATCH] xcl run: a title's minidump of itself is declined, not left to hang A retail title's crash handler wakes its dump thread, which calls dbgcore!MiniDumpWriteDump on its own process while the crashed thread waits for it. In-process, dbgcore suspends every other thread and then takes the process heap's lock. When one of the suspended threads holds that lock, the dump never finishes: the process hangs after XCL's crash line, the window frozen and not responding. Seen at the title's world-loading crash (+0x23cf683) on an RTX 5060 Ti: one run ended a second after the crash, the next hung for minutes. cdb on the hung process showed every thread suspended once more than cdb itself suspends them, the dump thread in dbgcore!MiniDumpWriteDump waiting in RtlAllocateHeap's critical section, and the crashed thread in XCL's unhandled filter -> the title's filter -> WaitForSingleObject. The title loads dbghelp.dll by name and asks it for MiniDumpWriteDump. It is now routed to a small HLE module whose MiniDumpWriteDump returns FALSE with ERROR_NOT_SUPPORTED for the current process and calls Windows' for any other. The rest of dbghelp is Windows' own. XCL's log already holds the exception and every thread by then. Assisted-by: Claude Code (Claude Opus 5.5) --- crates/xcl/src/hle/dbghelp.rs | 65 +++++++++++++++++++++++++++++++++++ crates/xcl/src/hle/mod.rs | 5 +++ 2 files changed, 70 insertions(+) create mode 100644 crates/xcl/src/hle/dbghelp.rs diff --git a/crates/xcl/src/hle/dbghelp.rs b/crates/xcl/src/hle/dbghelp.rs new file mode 100644 index 0000000000..5d3969a760 --- /dev/null +++ b/crates/xcl/src/hle/dbghelp.rs @@ -0,0 +1,65 @@ +//! dbghelp: a title's crash handler writes a minidump of its own process (`MiniDumpWriteDump` +//! from its dump thread, while the crashed thread waits for it). In-process, dbgcore suspends +//! every other thread and then takes the process heap's lock – held, often enough, by one of the +//! threads it has just suspended: the dump never finishes, the crashed thread never returns and +//! the process hangs with XCL's window frozen (a retail title's world-loading crash: it ended +//! within a second once, and hung on the next run). XCL has logged the exception and every +//! thread by then, so a dump of the process itself is declined; dumps of other processes, and +//! every other dbghelp function, are Windows' own. + +use std::ffi::c_void; + +use windows_sys::Win32::Foundation::{SetLastError, ERROR_NOT_SUPPORTED, HANDLE}; +use windows_sys::Win32::System::Threading::{GetCurrentProcessId, GetProcessId}; + +pub const DBGHELP: &str = "dbghelp.dll"; + +type MiniDumpFn = unsafe extern "system" fn(HANDLE, u32, HANDLE, i32, *const c_void, *const c_void, *const c_void) -> i32; + +/// A dump of this process is not written (see the module comment); FALSE with +/// ERROR_NOT_SUPPORTED, as for a dump type the system cannot write. +#[allow(non_snake_case)] +unsafe extern "system" fn MiniDumpWriteDump(process: HANDLE, pid: u32, file: HANDLE, ty: i32, exception: *const c_void, user: *const c_void, callback: *const c_void) -> i32 { + let me = GetCurrentProcessId(); + if pid == me || GetProcessId(process) == me { + static ONCE: std::sync::Once = std::sync::Once::new(); + ONCE.call_once(|| crate::xlog_warn!("dbghelp: the title's minidump of its own process is not written (an in-process dump can hang the process; XCL's log has the exception and the threads)")); + SetLastError(ERROR_NOT_SUPPORTED); + return 0; + } + let name = crate::pe::ImportName::Name { hint: 0, name: "MiniDumpWriteDump".into() }; + match crate::resolver::system_proc(DBGHELP, &name) { + Some(f) => std::mem::transmute::(f)(process, pid, file, ty, exception, user, callback), + None => { + SetLastError(ERROR_NOT_SUPPORTED); + 0 + } + } +} + +pub fn exports() -> Vec<(&'static str, usize)> { + crate::hle_exports![MiniDumpWriteDump] +} + +#[cfg(test)] +mod tests { + use super::*; + use windows_sys::Win32::Foundation::GetLastError; + use windows_sys::Win32::System::Threading::GetCurrentProcess; + + /// What the title loads by name and asks for: `MiniDumpWriteDump` is XCL's and refuses a dump + /// of this process at once (Windows' would write it – or hang); the rest of dbghelp is + /// Windows' own. + #[test] + fn a_minidump_of_the_process_itself_is_declined() { + let name = |s: &str| crate::pe::ImportName::Name { hint: 0, name: s.into() }; + let h = crate::resolver::load_library("dbghelp.dll").expect("LoadLibrary(dbghelp.dll)"); + let f = crate::resolver::get_proc_address(h, &name("MiniDumpWriteDump")).expect("GetProcAddress(MiniDumpWriteDump)"); + assert_eq!(f, MiniDumpWriteDump as usize); + let f: MiniDumpFn = unsafe { std::mem::transmute(f) }; + let ok = unsafe { f(GetCurrentProcess(), GetCurrentProcessId(), std::ptr::null_mut(), 0, std::ptr::null(), std::ptr::null(), std::ptr::null()) }; + assert_eq!((ok, unsafe { GetLastError() }), (0, ERROR_NOT_SUPPORTED)); + let sym = crate::resolver::get_proc_address(h, &name("SymInitializeW")).expect("GetProcAddress(SymInitializeW)"); + assert_eq!(Some(sym), crate::resolver::system_proc(DBGHELP, &name("SymInitializeW"))); + } +} diff --git a/crates/xcl/src/hle/mod.rs b/crates/xcl/src/hle/mod.rs index da0eafbd2d..26fa6daca0 100644 --- a/crates/xcl/src/hle/mod.rs +++ b/crates/xcl/src/hle/mod.rs @@ -13,6 +13,7 @@ use crate::util::glob_match; pub mod acphal; pub mod d3d12_layout; pub mod d3d12x; +pub mod dbghelp; pub mod dstorage; pub mod dxcompiler; pub mod gameinput; @@ -108,6 +109,7 @@ fn build(config: &Config) -> Registry { HleModule::new(XG, xg::exports(), &[]), HleModule::new(ACPHAL, acphal::exports(), &[]), HleModule::new(GAMEINPUT, gameinput::exports(), &[]), + HleModule::new(dbghelp::DBGHELP, dbghelp::exports(), &["dbghelp.dll"]), ]; let offline = netoff::enabled(); if offline { @@ -186,6 +188,9 @@ fn build(config: &Config) -> Registry { for p in ["dxcompiler_xs.dll", "dxcompiler_x.dll"] { routes.push((p.into(), Route::Hle(c))); } + // a title's minidump of itself (its crash handler): `dbghelp` + let dh = idx(dbghelp::DBGHELP); + routes.push((dbghelp::DBGHELP.into(), Route::Hle(dh))); if offline { let n = idx(netoff::NETOFF); for p in ["winhttp.dll", "ws2_32.dll"] {