//! Opt-in compatibility hooks for known title builds. //! //! Only per-build offsets and short byte signatures live here (asset policy: //! nothing of the title itself). Every hook checks the build's hash and the //! exact bytes it replaces, and does nothing otherwise. use windows_sys::Win32::System::Diagnostics::Debug::FlushInstructionCache; use windows_sys::Win32::System::Memory::{PAGE_EXECUTE_READWRITE, VirtualProtect}; use windows_sys::Win32::System::Threading::GetCurrentProcess; /// First 16 hex digits of the BLAKE3 of the Series X 1.0.2.1 executable. const BUILD_1_0_2_1: &str = "b99f876b74d4a77f"; /// Environment variable that turns the watchdog hook on. pub(crate) const NO_WATCHDOG_ENV: &str = "VICTORY_NO_WATCHDOG"; /// One code patch: `rva` must hold exactly `expect` before `new` is written. struct Patch { what: &'static str, rva: usize, expect: &'static [u8], new: &'static [u8], } /// The title's watchdog thread ends the process on purpose when its main /// thread has not advanced for about 30 one-second ticks. Two places do it: /// the handler it calls (`mov dword ptr [0], imm32`) and, when the handler /// returns or is unset, a fallback right after the call (`mov dword ptr [0], /// 0; int3`). Jumping over the whole block returns the loop to its normal /// path; the first patch makes the handler harmless if something else calls it. const WATCHDOG: &[Patch] = &[ Patch { what: "watchdog: skip the call and the fallback fault", rva: 0x2215c27, // mov rax, [rip + disp32] (the handler pointer) expect: &[0x48, 0x8B, 0x05, 0x1A, 0x07, 0xFA, 0x04], // jmp +0x18 (to the loop's normal continuation), then NOPs new: &[0xEB, 0x18, 0x90, 0x90, 0x90, 0x90, 0x90], }, Patch { what: "watchdog: handler's deliberate fault", rva: 0x23cf683, expect: &[0xC7, 0x04, 0x25, 0, 0, 0, 0, 0x21, 0xD4, 0x8B, 0x4D], // an eleven-byte NOP new: &[0x66, 0x66, 0x2E, 0x0F, 0x1F, 0x84, 0, 0, 0, 0, 0], }, ]; /// Applies the watchdog patches whose bytes match. Returns how many applied. /// /// # Safety /// `base` is the mapped image whose original BLAKE3 is `hash`, and no title /// code is running yet. pub(crate) unsafe fn disarm_watchdog(base: usize, hash: &blake3::Hash) -> usize { if !hash.to_hex().starts_with(BUILD_1_0_2_1) { return 0; } WATCHDOG .iter() // SAFETY: forwarded from this function's contract. .filter(|p| unsafe { apply(base, p) }) .count() } /// # Safety /// See `disarm_watchdog`. unsafe fn apply(base: usize, p: &Patch) -> bool { let at = (base + p.rva) as *mut u8; // SAFETY: the offset is inside the mapped .text of this exact build. let now = unsafe { std::slice::from_raw_parts(at, p.expect.len()) }; if now != p.expect || p.new.len() != p.expect.len() { tracing::warn!(what = p.what, "hook skipped: the bytes are not the expected ones"); return false; } let mut old = 0; // SAFETY: changing the protection of those bytes only, restored below. if unsafe { VirtualProtect(at as _, p.new.len(), PAGE_EXECUTE_READWRITE, &mut old) } == 0 { return false; } // SAFETY: writable now; the bytes are the ones checked above. unsafe { std::ptr::copy_nonoverlapping(p.new.as_ptr(), at, p.new.len()); VirtualProtect(at as _, p.new.len(), old, &mut old); FlushInstructionCache(GetCurrentProcess(), at as _, p.new.len()); } tracing::info!(what = p.what, "hook applied"); true }