Loader: suspend a thread before reading its TEB; skip bad dump times
Stack dumps (VICTORY_STACKS, diagnostics builds) read another thread's TEB right after NtQueryInformationThread and suspended it only later: a thread exiting in between frees its TEB, and the read faults. The thread is now suspended first and resumed on every way out. The dump times went through Duration::from_secs_f64, which panics on `inf` or `1e20`, killing the dump thread (the crash hook logs it as a crash). They are parsed by `schedule` now, which leaves out NaN and times no Duration holds. 1 new test. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit acdcc41687be2ecd2bf2efc43ab7449329483382)
This commit is contained in:
1 parent
e190726c68
commit
d6e0e3f07f
1 file changed
+43
-15
@@ -27,21 +27,30 @@ pub fn dump_stacks_on_schedule() {
|
|||||||
let Ok(list) = std::env::var("VICTORY_STACKS") else {
|
let Ok(list) = std::env::var("VICTORY_STACKS") else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let mut times: Vec<f64> = list
|
let times = schedule(&list);
|
||||||
.split(',')
|
|
||||||
.filter_map(|t| t.trim().parse().ok())
|
|
||||||
.collect();
|
|
||||||
times.sort_by(f64::total_cmp);
|
|
||||||
let start = std::time::Instant::now();
|
let start = std::time::Instant::now();
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
for t in times {
|
for at in times {
|
||||||
let at = std::time::Duration::from_secs_f64(t.max(0.0));
|
|
||||||
std::thread::sleep(at.saturating_sub(start.elapsed()));
|
std::thread::sleep(at.saturating_sub(start.elapsed()));
|
||||||
dump_stacks();
|
dump_stacks();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The times in a `VICTORY_STACKS` list (seconds, comma-separated), in
|
||||||
|
/// order. Negative times are now; times no `Duration` holds (`inf`, `NaN`,
|
||||||
|
/// `1e20`) are left out instead of panicking the dump thread.
|
||||||
|
fn schedule(list: &str) -> Vec<std::time::Duration> {
|
||||||
|
let mut times: Vec<_> = list
|
||||||
|
.split(',')
|
||||||
|
.filter_map(|t| t.trim().parse::<f64>().ok())
|
||||||
|
.filter(|t| !t.is_nan())
|
||||||
|
.filter_map(|t| std::time::Duration::try_from_secs_f64(t.max(0.0)).ok())
|
||||||
|
.collect();
|
||||||
|
times.sort();
|
||||||
|
times
|
||||||
|
}
|
||||||
|
|
||||||
/// `THREAD_BASIC_INFORMATION` (ntddk).
|
/// `THREAD_BASIC_INFORMATION` (ntddk).
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
struct BasicInfo {
|
struct BasicInfo {
|
||||||
@@ -125,6 +134,12 @@ fn frames_of(thread: HANDLE) -> (Vec<u64>, (i32, i32)) {
|
|||||||
priority: 0,
|
priority: 0,
|
||||||
base_priority: 0,
|
base_priority: 0,
|
||||||
};
|
};
|
||||||
|
// Suspended first: a running thread may exit and free its TEB between
|
||||||
|
// the query and the read below.
|
||||||
|
// SAFETY: a thread of this process that is not this one.
|
||||||
|
if unsafe { SuspendThread(thread) } == u32::MAX {
|
||||||
|
return (frames, (0, 0));
|
||||||
|
}
|
||||||
// SAFETY: writes `info`, whose layout and size are the system's.
|
// SAFETY: writes `info`, whose layout and size are the system's.
|
||||||
let status = unsafe {
|
let status = unsafe {
|
||||||
NtQueryInformationThread(
|
NtQueryInformationThread(
|
||||||
@@ -136,21 +151,20 @@ fn frames_of(thread: HANDLE) -> (Vec<u64>, (i32, i32)) {
|
|||||||
)
|
)
|
||||||
};
|
};
|
||||||
if status != 0 || info.teb.is_null() {
|
if status != 0 || info.teb.is_null() {
|
||||||
|
// SAFETY: suspended above.
|
||||||
|
unsafe { ResumeThread(thread) };
|
||||||
return (frames, (0, 0));
|
return (frames, (0, 0));
|
||||||
}
|
}
|
||||||
// SAFETY: the TEB of a live thread of this process: its NT_TIB holds
|
// SAFETY: the TEB of a suspended thread of this process: its NT_TIB
|
||||||
// the stack base at +8 and the stack limit at +0x10.
|
// holds the stack base at +8 and the stack limit at +0x10.
|
||||||
let (high, low) = unsafe { (info.teb.add(1).read(), info.teb.add(2).read()) };
|
let (high, low) = unsafe { (info.teb.add(1).read(), info.teb.add(2).read()) };
|
||||||
// SAFETY: zeroed is a valid CONTEXT.
|
// SAFETY: zeroed is a valid CONTEXT.
|
||||||
let mut context = Aligned(unsafe { std::mem::zeroed() });
|
let mut context = Aligned(unsafe { std::mem::zeroed() });
|
||||||
context.0.ContextFlags = CONTEXT_FULL_AMD64;
|
context.0.ContextFlags = CONTEXT_FULL_AMD64;
|
||||||
// SAFETY: suspends, reads and resumes a thread that is not this one;
|
// SAFETY: reads and resumes the thread suspended above; nothing until
|
||||||
// nothing between suspend and resume allocates or takes a lock the
|
// the resume allocates or takes a lock the thread could hold besides
|
||||||
// thread could hold besides the unwinder's own.
|
// the unwinder's own.
|
||||||
unsafe {
|
unsafe {
|
||||||
if SuspendThread(thread) == u32::MAX {
|
|
||||||
return (frames, (info.priority, info.base_priority));
|
|
||||||
}
|
|
||||||
if GetThreadContext(thread, &mut context.0) != 0 {
|
if GetThreadContext(thread, &mut context.0) != 0 {
|
||||||
unwind_into(context.0, low, high, &mut frames);
|
unwind_into(context.0, low, high, &mut frames);
|
||||||
}
|
}
|
||||||
@@ -221,4 +235,18 @@ mod tests {
|
|||||||
stop_tx.send(()).unwrap();
|
stop_tx.send(()).unwrap();
|
||||||
parked.join().unwrap();
|
parked.join().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Times no `Duration` holds are left out instead of panicking the dump
|
||||||
|
/// thread (`from_secs_f64` panicked on `inf`); the rest come in order.
|
||||||
|
#[test]
|
||||||
|
fn schedules_skip_times_out_of_range() {
|
||||||
|
let secs = |list| {
|
||||||
|
schedule(list)
|
||||||
|
.iter()
|
||||||
|
.map(|d| d.as_secs_f64())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
};
|
||||||
|
assert_eq!(secs("30, 5,x,-2,inf,NaN,1e20"), [0.0, 5.0, 30.0]);
|
||||||
|
assert!(schedule("").is_empty());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user