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:
Manuel Pérez authored and local committed 2026-11-25 01:02:57 -06:00
1 parent 8b4f50f2e1
commit 91d41b2c90
1 file changed
+43 -15
+43 -15
View File
@@ -27,21 +27,30 @@ pub fn dump_stacks_on_schedule() {
let Ok(list) = std::env::var("VICTORY_STACKS") else {
return;
};
let mut times: Vec<f64> = list
.split(',')
.filter_map(|t| t.trim().parse().ok())
.collect();
times.sort_by(f64::total_cmp);
let times = schedule(&list);
let start = std::time::Instant::now();
std::thread::spawn(move || {
for t in times {
let at = std::time::Duration::from_secs_f64(t.max(0.0));
for at in times {
std::thread::sleep(at.saturating_sub(start.elapsed()));
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).
#[repr(C)]
struct BasicInfo {
@@ -125,6 +134,12 @@ fn frames_of(thread: HANDLE) -> (Vec<u64>, (i32, i32)) {
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.
let status = unsafe {
NtQueryInformationThread(
@@ -136,21 +151,20 @@ fn frames_of(thread: HANDLE) -> (Vec<u64>, (i32, i32)) {
)
};
if status != 0 || info.teb.is_null() {
// SAFETY: suspended above.
unsafe { ResumeThread(thread) };
return (frames, (0, 0));
}
// SAFETY: the TEB of a live thread of this process: its NT_TIB holds
// the stack base at +8 and the stack limit at +0x10.
// SAFETY: the TEB of a suspended thread of this process: its NT_TIB
// 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()) };
// 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.
// SAFETY: reads and resumes the thread suspended above; nothing until
// the 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));
}
if GetThreadContext(thread, &mut context.0) != 0 {
unwind_into(context.0, low, high, &mut frames);
}
@@ -221,4 +235,18 @@ mod tests {
stop_tx.send(()).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());
}
}