diff --git a/crates/xcl/src/eh.rs b/crates/xcl/src/eh.rs index 2a94cd36e0..b231dcf253 100644 --- a/crates/xcl/src/eh.rs +++ b/crates/xcl/src/eh.rs @@ -143,6 +143,23 @@ unsafe extern "system" fn hk_nt_terminate_process(h: isize, status: i32) -> i32 orig(h, status) } +/// The process ends without the hook's logging and flushes, for a caller that has suspended the +/// other threads: one of them may hold a lock that work takes (stderr, the log file, the heap). +/// No allocation, no lock. +pub(crate) fn terminate_unhooked(code: u32) -> ! { + unsafe { + match ORIG_NT_TERMINATE.load(Ordering::Acquire) { + 0 => { + windows_sys::Win32::System::Threading::TerminateProcess(windows_sys::Win32::System::Threading::GetCurrentProcess(), code); + } + f => { + std::mem::transmute:: i32>(f)(-1, code as i32); + } + } + } + std::process::abort() +} + pub fn install_crash_handler() { unsafe { if let Err(e) = hooks::hook("ntdll.dll", "NtTerminateProcess", hk_nt_terminate_process as usize, &ORIG_NT_TERMINATE) { @@ -1083,6 +1100,49 @@ pub fn start_probes() { mod tests { use super::*; + /// The end watchdog's terminate with the hook installed and a suspended thread holding stderr + /// (which the hook's "process ends" line is echoed to): the process still ends. + #[test] + fn terminate_unhooked_ends_past_a_suspended_stderr_holder() { + const CHILD: &str = "XCL_TERMINATE_UNHOOKED_CHILD"; + if std::env::var_os(CHILD).is_some() { + unsafe { + hooks::hook("ntdll.dll", "NtTerminateProcess", hk_nt_terminate_process as usize, &ORIG_NT_TERMINATE).unwrap(); + let (tx, rx) = std::sync::mpsc::channel(); + let holder = std::thread::spawn(move || { + let _g = std::io::stderr().lock(); + tx.send(()).unwrap(); + loop { + std::thread::park(); + } + }); + rx.recv().unwrap(); + use std::os::windows::io::AsRawHandle; + windows_sys::Win32::System::Threading::SuspendThread(holder.as_raw_handle() as _); + terminate_unhooked(7); + } + } + let mut child = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "eh::tests::terminate_unhooked_ends_past_a_suspended_stderr_holder", "--nocapture"]) + .env(CHILD, "1") + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .spawn() + .unwrap(); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20); + let status = loop { + if let Some(s) = child.try_wait().unwrap() { + break s; + } + if std::time::Instant::now() >= deadline { + let _ = child.kill(); + panic!("the process did not end within 20 s"); + } + std::thread::sleep(std::time::Duration::from_millis(50)); + }; + assert_eq!(status.code(), Some(7)); + } + #[test] fn bounded_cstr_stops_at_nul_limit_and_unreadable_memory() { unsafe { diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 8fd7473c0f..0386ff4898 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4215,9 +4215,13 @@ pub fn end_title(code: u32) -> ! { let _ = std::thread::Builder::new().name("xcl-end-watchdog".into()).spawn(move || { let _ = tx.send(windows_sys::Win32::System::Threading::GetCurrentThreadId()); std::thread::sleep(std::time::Duration::from_secs(15)); - windows_sys::Win32::System::Threading::TerminateProcess(windows_sys::Win32::System::Threading::GetCurrentProcess(), code); + crate::eh::terminate_unhooked(code); }); let watchdog = rx.recv_timeout(std::time::Duration::from_secs(1)).unwrap_or(0); + // what the NtTerminateProcess hook writes out, while no thread is suspended: the process + // ends past the hook (a suspended thread may hold stderr, the log file or the heap) + crate::hle::d3d12x::pso_async::flush_used_bounded(std::time::Duration::from_millis(100)); + flush_indirect_split_records(std::time::Duration::from_millis(100)); crate::log::flush(); // the title's threads stop first: with its fences at the maximum a running title thinks // the GPU is done and resets command allocators the GPU still runs (Fortnite: a device @@ -4231,9 +4235,8 @@ pub fn end_title(code: u32) -> ! { } drain_gpu(); crate::log::flush(); - windows_sys::Win32::System::Threading::TerminateProcess(windows_sys::Win32::System::Threading::GetCurrentProcess(), code); + crate::eh::terminate_unhooked(code) } - std::process::abort() } /// Every thread of the process but this one and `keep` (XCL's end watchdog), suspended – twice