From 2cc7279e8dfddf25ae98603769f4cc81c6f22542 Mon Sep 17 00:00:00 2001 From: Tiago Leal <183942127+tiagonleal@users.noreply.github.com> Date: Sat, 28 Nov 2026 15:53:00 +0000 Subject: [PATCH] D3D12.X: end the title past the NtTerminateProcess hook end_title's 15 s watchdog promised that the process ends "whatever happens", but it called TerminateProcess, which runs XCL's own NtTerminateProcess hook with every other thread suspended. The hook formats and writes a "process ends" line (heap allocation, the log file mutex, then an echo to stderr through std's stderr lock), flushes the log and the two used-pipeline journals. A suspended thread holding the stderr lock, the log mutex or the heap lock blocks the watchdog forever. end_title's own final TerminateProcess had the same exposure. Observed once (release build, retail title, XCL_RUN_SECONDS=60): the title ended at 60.4 s, the hook's "process ends" line came from the watchdog exactly 15.0 s later (so the drain was stuck), and 20 minutes on the process was still alive with 145 threads Suspended: the kernel had never been asked to terminate. The file write had succeeded, so the block was after it (stderr echo, heap, or a flush). Both terminate calls now go through eh::terminate_unhooked, which calls the original NtTerminateProcess (no allocation, no lock). The journal flushes the hook did run before the threads are suspended; under suspension their writer thread could not complete them anyway. The test suspends a thread holding stderr with the hook installed: the child process ends with the code given; through the hook it hangs. --- crates/xcl/src/eh.rs | 60 +++++++++++++++++++++++++++++++ crates/xcl/src/hle/d3d12x/real.rs | 9 +++-- 2 files changed, 66 insertions(+), 3 deletions(-) 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