mirror of
https://github.com/gamingwithhomie/larp.git
synced 2026-12-06 07:38:36 +00:00
@ -42,6 +42,7 @@ pub fn run(
|
||||
if let Some(heartbeat) = larp_loader::watchdog_heartbeat(image.base(), &prepared.original_hash) {
|
||||
larp_hle::modules::d3d12x::feed_watchdog_at(heartbeat);
|
||||
}
|
||||
quiet_title_watchdog(image.base(), &prepared.original_hash);
|
||||
// The program is mapped from the copy `load` wrote: the file's bytes
|
||||
// and their patched copy are not read again, and were held for the
|
||||
// whole run (two blocks of 114.7 MB each for the title, in
|
||||
@ -643,3 +644,32 @@ pub fn menu_choices(exe: &Path, stream: Option<u64>, card: Option<u64>) -> (Opti
|
||||
let budget = |key: &str| value(key).and_then(|v| v.parse::<u64>().ok()).filter(|&mb| mb > 0);
|
||||
(stream.or_else(|| budget("stream_budget")), card.or_else(|| budget("card_budget")))
|
||||
}
|
||||
/// `LARP_TITLE_WATCHDOG=0`: the title's watchdog is told to quit. It ends
|
||||
/// the process when its heartbeat has not changed for 30 one-second ticks,
|
||||
/// by writing to address 0 (`+0x23cf683`, which reads as a crash); larp
|
||||
/// feeds the heartbeat while a draw waits for its pipeline, but a stall
|
||||
/// elsewhere (stream-in, a long driver call) still trips it. In
|
||||
/// VIctory-NVIDIA every run on an RTX 5060 Laptop that ended at
|
||||
/// `+0x23cf683` had stood 30 s with its frames stopped first. The flag is
|
||||
/// set again every half second, as the title may clear it while it starts.
|
||||
/// Off by default: with it a real hang stays a hang instead of ending.
|
||||
fn quiet_title_watchdog(base: usize, hash: &blake3::Hash) {
|
||||
if !std::env::var("LARP_TITLE_WATCHDOG").is_ok_and(|v| v.trim() == "0") {
|
||||
return;
|
||||
}
|
||||
let Some(flag) = larp_loader::watchdog_quit_flag(base, hash) else {
|
||||
eprintln!("LARP_TITLE_WATCHDOG=0: the title's watchdog is not where it is in 1.0.2.1; left alone");
|
||||
return;
|
||||
};
|
||||
eprintln!("LARP_TITLE_WATCHDOG=0: the title's 30 s watchdog is told to quit");
|
||||
let started = std::thread::Builder::new().name("larp-watchdog-off".to_owned()).spawn(move || loop {
|
||||
// SAFETY: a byte of the image's writable data (the flag the
|
||||
// watchdog reads), found by `watchdog_quit_flag`.
|
||||
let byte = unsafe { &*(flag as *const std::sync::atomic::AtomicU8) };
|
||||
byte.fetch_or(1, std::sync::atomic::Ordering::SeqCst);
|
||||
std::thread::sleep(std::time::Duration::from_millis(500));
|
||||
});
|
||||
if let Err(error) = started {
|
||||
eprintln!("LARP_TITLE_WATCHDOG=0: the title's watchdog could not be told to quit: {error}");
|
||||
}
|
||||
}
|
||||
|
||||
@ -364,10 +364,33 @@ unsafe extern "system" fn handler(info: *mut EXCEPTION_POINTERS) -> i32 {
|
||||
if FATAL.contains(&unsafe { (*(*info).ExceptionRecord).ExceptionCode }) {
|
||||
// SAFETY: as above.
|
||||
unsafe { report("first-chance exception", info, false) };
|
||||
// SAFETY: as above.
|
||||
if unsafe { writes_zero(&*(*info).ExceptionRecord) } && stacks_at_kill() {
|
||||
crate::stacks::dump_stacks();
|
||||
}
|
||||
}
|
||||
EXCEPTION_CONTINUE_SEARCH
|
||||
}
|
||||
|
||||
/// A write to address 0: how the title kills itself on purpose. Its
|
||||
/// watchdog (`hooks::HEARTBEAT`) writes there 30 s after the frame loop
|
||||
/// stops (`+0x23cf683`).
|
||||
fn writes_zero(record: &windows_sys::Win32::System::Diagnostics::Debug::EXCEPTION_RECORD) -> bool {
|
||||
record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION
|
||||
&& record.NumberParameters >= 2
|
||||
&& record.ExceptionInformation[0] == 1
|
||||
&& record.ExceptionInformation[1] == 0
|
||||
}
|
||||
|
||||
/// `LARP_STACKS_AT_KILL=1`: on the first such write, every other thread's
|
||||
/// stack, to name what the frame loop was stuck in.
|
||||
fn stacks_at_kill() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
static DONE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
||||
*ON.get_or_init(|| std::env::var_os("LARP_STACKS_AT_KILL").is_some_and(|v| v == "1"))
|
||||
&& !DONE.swap(true, std::sync::atomic::Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// For an unhandled-exception filter: reports the exception that is about
|
||||
/// to end the process, and appends it to the crash log.
|
||||
///
|
||||
@ -570,6 +593,24 @@ pub(crate) fn unwind_into(context: CONTEXT, low: usize, high: usize, frames: &mu
|
||||
context.Rip = unsafe { (rsp as *const u64).read() };
|
||||
context.Rsp += 8;
|
||||
} else {
|
||||
// A function that keeps a frame register is unwound through it:
|
||||
// RtlVirtualUnwind reads the stack at that register. Walking a
|
||||
// thread that was stopped anywhere (`stacks::dump_stacks`), the
|
||||
// register can hold anything, and the read faulted inside ntdll.
|
||||
// Stop where it does not point into this thread's stack.
|
||||
// SAFETY: `entry` is the table entry the lookup gave for `image`;
|
||||
// its unwind information lies in that image, the frame register
|
||||
// in the low four bits of its fourth byte.
|
||||
let frame_register = unsafe {
|
||||
let info = image as usize + (*entry).Anonymous.UnwindInfoAddress as usize;
|
||||
(info as *const u8).add(3).read() & 0x0F
|
||||
};
|
||||
if frame_register != 0 {
|
||||
let value = register(context, frame_register) as usize;
|
||||
if value < low || value >= high {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let (mut data, mut frame) = (null_mut(), 0);
|
||||
// SAFETY: `entry` belongs to `image`; the context is our copy.
|
||||
unsafe {
|
||||
@ -592,6 +633,122 @@ pub(crate) fn unwind_into(context: CONTEXT, low: usize, high: usize, frames: &mu
|
||||
}
|
||||
}
|
||||
|
||||
/// The general register an unwind code numbers `n` (0 rax .. 15 r15).
|
||||
fn register(context: &CONTEXT, n: u8) -> u64 {
|
||||
match n {
|
||||
1 => context.Rcx,
|
||||
2 => context.Rdx,
|
||||
3 => context.Rbx,
|
||||
4 => context.Rsp,
|
||||
5 => context.Rbp,
|
||||
6 => context.Rsi,
|
||||
7 => context.Rdi,
|
||||
8 => context.R8,
|
||||
9 => context.R9,
|
||||
10 => context.R10,
|
||||
11 => context.R11,
|
||||
12 => context.R12,
|
||||
13 => context.R13,
|
||||
14 => context.R14,
|
||||
15 => context.R15,
|
||||
_ => context.Rax,
|
||||
}
|
||||
}
|
||||
|
||||
/// [`unwind_into`] over a copy of a thread's stack: `copy` holds the bytes
|
||||
/// from `context.Rsp` up (`len` of them, then zeros), taken while the
|
||||
/// thread was suspended. Every address the unwind data makes it read is
|
||||
/// then in the copy, never in a live stack the thread may have grown or
|
||||
/// left (walking live threads read reserved stack pages and faulted).
|
||||
/// Registers that point into the copied range are moved into the copy; a
|
||||
/// frame register that points elsewhere ends the walk.
|
||||
pub(crate) fn unwind_copy(context: CONTEXT, copy: &[u8], len: usize, frames: &mut Vec<u64>) {
|
||||
let mut aligned = Aligned16(context);
|
||||
let context = &mut aligned.0;
|
||||
frames.push(context.Rip);
|
||||
let (real_lo, buf_lo) = (context.Rsp as usize, copy.as_ptr() as usize);
|
||||
let (real_hi, buf_hi) = (real_lo + len, buf_lo + len);
|
||||
let moved = |x: u64| -> u64 {
|
||||
let x = x as usize;
|
||||
(if x >= real_lo && x < real_hi { x - real_lo + buf_lo } else { x }) as u64
|
||||
};
|
||||
let move_all = |c: &mut CONTEXT| {
|
||||
for r in [
|
||||
&mut c.Rbx, &mut c.Rbp, &mut c.Rsi, &mut c.Rdi, &mut c.R12, &mut c.R13, &mut c.R14,
|
||||
&mut c.R15,
|
||||
] {
|
||||
*r = moved(*r);
|
||||
}
|
||||
};
|
||||
context.Rsp = buf_lo as u64;
|
||||
move_all(context);
|
||||
while frames.len() < MAX_FRAMES {
|
||||
let rsp = context.Rsp as usize;
|
||||
if rsp < buf_lo || rsp + 8 > buf_hi {
|
||||
break;
|
||||
}
|
||||
let mut image = 0;
|
||||
// SAFETY: looks up the function table entry covering `Rip`, if any.
|
||||
let entry = unsafe { RtlLookupFunctionEntry(context.Rip, &mut image, null_mut()) };
|
||||
if entry.is_null() {
|
||||
// SAFETY: `rsp` is inside the copy.
|
||||
context.Rip = unsafe { (rsp as *const u64).read_unaligned() };
|
||||
context.Rsp += 8;
|
||||
} else {
|
||||
// SAFETY: as in `unwind_into`.
|
||||
let frame_register = unsafe {
|
||||
let info = image as usize + (*entry).Anonymous.UnwindInfoAddress as usize;
|
||||
(info as *const u8).add(3).read() & 0x0F
|
||||
};
|
||||
if frame_register != 0 {
|
||||
let register = match frame_register {
|
||||
1 => &mut context.Rcx,
|
||||
2 => &mut context.Rdx,
|
||||
3 => &mut context.Rbx,
|
||||
5 => &mut context.Rbp,
|
||||
6 => &mut context.Rsi,
|
||||
7 => &mut context.Rdi,
|
||||
8 => &mut context.R8,
|
||||
9 => &mut context.R9,
|
||||
10 => &mut context.R10,
|
||||
11 => &mut context.R11,
|
||||
12 => &mut context.R12,
|
||||
13 => &mut context.R13,
|
||||
14 => &mut context.R14,
|
||||
15 => &mut context.R15,
|
||||
_ => break,
|
||||
};
|
||||
*register = moved(*register);
|
||||
let at = *register as usize;
|
||||
if at < buf_lo || at >= buf_hi {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let (mut data, mut frame) = (null_mut(), 0);
|
||||
// SAFETY: `entry` belongs to `image`; the context is our copy and
|
||||
// its stack registers point into `copy`, whose zeros after `len`
|
||||
// take reads past the frames copied.
|
||||
unsafe {
|
||||
RtlVirtualUnwind(
|
||||
UNW_FLAG_NHANDLER,
|
||||
image,
|
||||
context.Rip,
|
||||
entry,
|
||||
context,
|
||||
&mut data,
|
||||
&mut frame,
|
||||
null_mut(),
|
||||
)
|
||||
};
|
||||
move_all(context);
|
||||
}
|
||||
if context.Rip == 0 {
|
||||
break;
|
||||
}
|
||||
frames.push(context.Rip);
|
||||
}
|
||||
}
|
||||
|
||||
/// `module+rva`, or the bare address.
|
||||
pub fn locate(address: usize) -> String {
|
||||
module_of(address).unwrap_or_else(|| format!("{address:#x}"))
|
||||
|
||||
@ -22,3 +22,27 @@ const HEARTBEAT: usize = 0x71b6338;
|
||||
pub fn watchdog_heartbeat(base: usize, hash: &blake3::Hash) -> Option<usize> {
|
||||
hash.to_hex().starts_with(BUILD_1_0_2_1).then_some(base + HEARTBEAT)
|
||||
}
|
||||
|
||||
/// The watchdog's quit flag (bit 0 of the byte at title+0x71b6340): set,
|
||||
/// its thread leaves before it counts (found in VIctory-NVIDIA #1428).
|
||||
const WATCHDOG_QUIT: usize = 0x71b6340;
|
||||
|
||||
/// Where the watchdog kills the process (title+0x23cf683):
|
||||
/// `mov dword ptr [0], 0x4d8bd421`.
|
||||
const WATCHDOG_TRAP: usize = 0x23cf683;
|
||||
const WATCHDOG_TRAP_BYTES: [u8; 11] = [0xc7, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, 0x21, 0xd4, 0x8b, 0x4d];
|
||||
|
||||
/// The address of the title's watchdog quit flag ([`WATCHDOG_QUIT`]) in the
|
||||
/// image at `base` whose original BLAKE3 is `hash`, when the watchdog's trap
|
||||
/// is where it was found; `None` for another build.
|
||||
pub fn watchdog_quit_flag(base: usize, hash: &blake3::Hash) -> Option<usize> {
|
||||
if !hash.to_hex().starts_with(BUILD_1_0_2_1) {
|
||||
return None;
|
||||
}
|
||||
// SAFETY: inside the mapped image of the build the offsets were read
|
||||
// from; code is readable.
|
||||
let found = unsafe {
|
||||
std::slice::from_raw_parts((base + WATCHDOG_TRAP) as *const u8, WATCHDOG_TRAP_BYTES.len())
|
||||
};
|
||||
(found == WATCHDOG_TRAP_BYTES).then_some(base + WATCHDOG_QUIT)
|
||||
}
|
||||
|
||||
@ -47,7 +47,7 @@ pub use detour::{DetourError, call_site, call_stub, detour, near_data, patch, re
|
||||
pub use prepare::{PrepareError, PreparedImage, prepare};
|
||||
pub use relocate::{MoveError, Moved, function_ends, move_data};
|
||||
pub use replay::{MAX_ADDED, detour_found, replay_bytes, replay_dirty, replay_for, replay_install, replayed};
|
||||
pub use hooks::watchdog_heartbeat;
|
||||
pub use hooks::{watchdog_heartbeat, watchdog_quit_flag};
|
||||
pub use resolve::{ImportResolver, Provenance, Resolution};
|
||||
pub use stacks::{dump_stacks, dump_stacks_on_schedule};
|
||||
pub use threads::disable_thread_calls;
|
||||
|
||||
@ -9,17 +9,26 @@ use windows_sys::Wdk::System::Threading::{
|
||||
};
|
||||
use windows_sys::Win32::Foundation::{CloseHandle, HANDLE, INVALID_HANDLE_VALUE, LocalFree};
|
||||
use windows_sys::Win32::System::Diagnostics::Debug::{
|
||||
CONTEXT, CONTEXT_FULL_AMD64, GetThreadContext,
|
||||
CONTEXT, CONTEXT_FULL_AMD64, GetThreadContext, ReadProcessMemory,
|
||||
};
|
||||
use windows_sys::Win32::System::Diagnostics::ToolHelp::{
|
||||
CreateToolhelp32Snapshot, TH32CS_SNAPTHREAD, THREADENTRY32, Thread32First, Thread32Next,
|
||||
};
|
||||
use windows_sys::Win32::System::Threading::{
|
||||
GetCurrentProcessId, GetCurrentThreadId, GetThreadDescription, OpenThread, ResumeThread,
|
||||
GetCurrentProcess, GetCurrentProcessId, GetCurrentThreadId, GetThreadDescription, OpenThread, ResumeThread,
|
||||
SuspendThread, THREAD_GET_CONTEXT, THREAD_QUERY_INFORMATION, THREAD_SUSPEND_RESUME,
|
||||
};
|
||||
|
||||
use crate::crash::{MAX_FRAMES, locate, unwind_into};
|
||||
use crate::crash::{MAX_FRAMES, locate, unwind_copy};
|
||||
|
||||
/// The most of a stack copied for a walk ([`frames_of`]), and the zeros after.
|
||||
const COPY_BYTES: usize = 512 << 10;
|
||||
const COPY_SLACK: usize = 64 << 10;
|
||||
|
||||
thread_local! {
|
||||
/// A walking thread's copy of the stack it walks, made before any suspend.
|
||||
static COPY: std::cell::RefCell<Vec<u8>> = std::cell::RefCell::new(vec![0; COPY_BYTES + COPY_SLACK]);
|
||||
}
|
||||
|
||||
/// Reads `LARP_STACKS` and, if set, starts a thread that prints every
|
||||
/// thread's stack at each listed time (seconds from now).
|
||||
@ -117,6 +126,9 @@ pub fn dump_stacks() {
|
||||
/// The thread's frames and its current and base scheduling priority (0-31).
|
||||
fn frames_of(thread: HANDLE) -> (Vec<u64>, (i32, i32)) {
|
||||
let mut frames = Vec::with_capacity(MAX_FRAMES);
|
||||
// The copy's buffer is made now, before the suspend below (a suspended
|
||||
// thread may hold the heap's lock).
|
||||
COPY.with(|_| ());
|
||||
let mut info: BasicInfo = BasicInfo {
|
||||
exit_status: 0,
|
||||
teb: std::ptr::null(),
|
||||
@ -144,18 +156,43 @@ fn frames_of(thread: HANDLE) -> (Vec<u64>, (i32, i32)) {
|
||||
// SAFETY: zeroed is a valid CONTEXT.
|
||||
let mut context = Aligned(unsafe { std::mem::zeroed() });
|
||||
context.0.ContextFlags = CONTEXT_FULL_AMD64;
|
||||
// SAFETY: suspends, reads and resumes a thread that is not this one;
|
||||
// nothing between suspend and resume allocates or takes a lock the
|
||||
// thread could hold besides the unwinder's own.
|
||||
unsafe {
|
||||
if SuspendThread(thread) == u32::MAX {
|
||||
return (frames, (info.priority, info.base_priority));
|
||||
// The stack is copied while the thread is suspended and unwound after
|
||||
// the resume, in the copy (`unwind_copy`): unwinding the live stack can
|
||||
// read pages the thread never committed and fault. The read goes through
|
||||
// the kernel, which fails where memory is missing instead of faulting;
|
||||
// the buffer was made before the suspend.
|
||||
COPY.with(|copy| {
|
||||
let mut copy = copy.borrow_mut();
|
||||
let mut got = None;
|
||||
// SAFETY: suspends, reads and resumes a thread that is not this one;
|
||||
// nothing between suspend and resume allocates or takes a lock the
|
||||
// thread could hold.
|
||||
unsafe {
|
||||
if SuspendThread(thread) == u32::MAX {
|
||||
return;
|
||||
}
|
||||
if GetThreadContext(thread, &mut context.0) != 0 {
|
||||
let rsp = context.0.Rsp as usize;
|
||||
let want = high.saturating_sub(rsp).min(COPY_BYTES);
|
||||
let mut read = 0usize;
|
||||
let ok = rsp >= low
|
||||
&& want > 0
|
||||
&& ReadProcessMemory(
|
||||
GetCurrentProcess(),
|
||||
rsp as *const c_void,
|
||||
copy.as_mut_ptr().cast(),
|
||||
want,
|
||||
&mut read,
|
||||
) != 0;
|
||||
got = Some(if ok { want } else { read });
|
||||
}
|
||||
ResumeThread(thread);
|
||||
}
|
||||
if GetThreadContext(thread, &mut context.0) != 0 {
|
||||
unwind_into(context.0, low, high, &mut frames);
|
||||
if let Some(len) = got {
|
||||
copy[len..].fill(0);
|
||||
unwind_copy(context.0, ©, len, &mut frames);
|
||||
}
|
||||
ResumeThread(thread);
|
||||
}
|
||||
});
|
||||
(frames, (info.priority, info.base_priority))
|
||||
}
|
||||
|
||||
|
||||
@ -79,6 +79,8 @@ Set these in `Larp\bin\launch.ps1` (or in your shell before running `larp.exe ru
|
||||
| `LARP_LANGUAGE` | `auto`, `en`, `de`, `fr`, `es`, `pl`, `pt-BR`, `tr`, `zh-CN`, `zh-TW` | `auto` (OS UI) | UI/overlay/CLI language (also switchable live in the `F10` menu or via `--lang`). |
|
||||
| `LARP_DSTORAGE_WORKERS` | `<1..64>` | logical cores | Worker thread count for DirectStorage decompression. |
|
||||
| `LARP_DSTORAGE_STAGING_MB` | `<16..512>` | `32` | DirectStorage staging buffer size in MiB for fast NVMe drives. |
|
||||
| `LARP_STACKS_AT_KILL` | `0`, `1` | `0` | `1` prints every thread's stack when the title's 30 s watchdog kills the run (write to address 0, `+0x23cf683`), to name what the frame loop was stuck in. |
|
||||
| `LARP_TITLE_WATCHDOG` | `0` | unset (left alone) | `0` tells the title's 30 s watchdog to quit (1.0.2.1 only), so a long stall is not ended as a crash at `+0x23cf683`. A real hang then stays a hang. |
|
||||
| `LARP_NO_ADVISOR` | `0`, `1` | `0` | Silences startup NVIDIA shader heap and XMP/EXPO RAM speed warnings. |
|
||||
| `LARP_D3D12_DEBUG` | `0`, `1` | `0` | Enables the Windows D3D12 debug layer (requires Graphics Tools optional feature). |
|
||||
| `LARP_D3D12_DRED` | set / unset | unset | Device-removed data: on a device removal the log names the command each list stopped at and the faulting GPU address. Small CPU cost. |
|
||||
|
||||
@ -106,6 +106,7 @@ Match the **`+0x...`** offset after `dll` to the table below:
|
||||
|
||||
### `+0x2215c35` or `+0x23cf683` — Freeze for 30 Seconds Followed by Crash
|
||||
- **Exact Cause:** The title's 30-second hang watchdog fired because the main loop stalled (usually heavy first-time shader compilation or disk I/O stall).
|
||||
- **To see what stalled:** `set LARP_STACKS_AT_KILL=1` prints every thread's stack at the kill. **To ride out a stall:** `set LARP_TITLE_WATCHDOG=0` tells the watchdog to quit (a real hang then no longer ends by itself).
|
||||
- **Fix:** Simply relaunch — already-compiled pipeline blobs in `Larp\cache` are kept, so the next start picks up right where it left off.
|
||||
|
||||
---
|
||||
|
||||
Reference in New Issue
Block a user