From 4e0591f62886d2d00087e36c625b2979b3472815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BB=99=E4=BC=8A=E8=8E=89=E9=9B=85=E8=A1=A5=E9=AD=94?= <98532177+geiyiliyabumo@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:23:05 +0800 Subject: [PATCH] loader(crash): verify unwind against known caller frames when RtlCaptureStackBackTrace returns empty (cherry picked from commit 121412bec21b0790d04d56399f49e2d140652864) --- crates/victory-loader/src/crash.rs | 117 +++++++++++++++++++++++++---- 1 file changed, 103 insertions(+), 14 deletions(-) diff --git a/crates/victory-loader/src/crash.rs b/crates/victory-loader/src/crash.rs index 62388ad..7062bbd 100644 --- a/crates/victory-loader/src/crash.rs +++ b/crates/victory-loader/src/crash.rs @@ -766,26 +766,115 @@ mod tests { EXCEPTION_RECORD, RtlCaptureContext, RtlCaptureStackBackTrace, }; + /// What [`capture_frames`] saw: our [`unwind`], then the system's walk. + type Captured = (Vec, Vec); + + /// A frame we expect [`unwind`] to report: the helper it is in, and the + /// helper's name for the message. + type Expected = (&'static str, fn() -> Captured); + + /// The runtime function entry start covering `at`: the image base the + /// lookup writes, plus the entry's `BeginAddress`. Asking the system for + /// both sides of the comparison means a thunk or an incremental-link jump + /// is not mistaken for a different function. + fn entry_start_at(at: u64) -> Option { + let mut image = 0; + // SAFETY: looks up the function table entry covering `at`; nothing + // keeps a reference to the entry past this call. + let entry = unsafe { RtlLookupFunctionEntry(at, &mut image, null_mut()) }; + if entry.is_null() { + return None; + } + // SAFETY: a non-null entry belongs to `image`, which the lookup wrote, + // and `BeginAddress` is an offset into that image. + Some(image + u64::from(unsafe { (*entry).BeginAddress })) + } + #[test] - #[inline(never)] fn unwinds_like_the_system_walk() { - // SAFETY: zeroed CONTEXT is valid; both calls write our buffers. - let (frames, walked) = unsafe { - // `RtlCaptureContext` wants it 16-byte aligned too. + // Asking the system's own walk whether the stack looks right is not + // reliable: on Windows 10 x64 debug builds `RtlCaptureStackBackTrace` + // returns no frames about half the time. The frames above us are + // known instead (see `level_a`), so check those first. + let (frames, walked) = level_a(); + // Frame 0 is in `capture_frames`, which took the context; then its + // callers in call order, each named by its runtime function entry. + let helpers: [Expected; 4] = [ + ("capture_frames", capture_frames), + ("level_c", level_c), + ("level_b", level_b), + ("level_a", level_a), + ]; + assert!(frames.len() >= helpers.len(), "{frames:?}"); + for (i, (name, helper)) in helpers.iter().enumerate() { + let want = entry_start_at(*helper as usize as u64) + .unwrap_or_else(|| panic!("no unwind data for {name}")); + let got = entry_start_at(frames[i]) + .unwrap_or_else(|| panic!("no unwind data for frame {i} at {:#x}", frames[i])); + assert_eq!(got, want, "frame {i} should be {name}: {frames:?}"); + } + // A walk that repeats an address is not a walk. + let mut distinct = frames[..helpers.len()].to_vec(); + distinct.sort_unstable(); + distinct.dedup(); + assert_eq!(distinct.len(), helpers.len(), "repeated: {frames:?}"); + // Where the system's walk does return frames, it must still agree with + // ours: frame 0 differs (both are in `capture_frames`, at different + // call sites), and the callers above are the same. + if walked.len() >= helpers.len() { + assert!(frames.len() >= walked.len(), "{frames:?} vs {walked:?}"); + let ours: Vec = frames[1..walked.len()] + .iter() + .map(|&f| f as usize) + .collect(); + assert_eq!(ours.as_slice(), &walked[1..]); + } + } + + /// The frames of this call, both ways: our [`unwind`], and the system's + /// walk, which Windows 10 can come up empty on. + #[inline(never)] + fn capture_frames() -> Captured { + // SAFETY: a zeroed CONTEXT is valid and both calls write buffers of + // ours; `RtlCaptureContext` wants the context 16-byte aligned too. + unsafe { let mut context = Aligned16(std::mem::zeroed()); RtlCaptureContext(&mut context.0); let mut walked = [null_mut(); 8]; let n = RtlCaptureStackBackTrace(0, 8, walked.as_mut_ptr(), null_mut()); - (unwind(context.0), walked[..usize::from(n)].to_vec()) - }; - // Frame 0 differs (two call sites in this function); callers agree. - assert!(walked.len() >= 4, "{walked:?}"); - let ours: Vec<_> = frames[1..walked.len()] - .iter() - .map(|&f| f as usize) - .collect(); - let system: Vec<_> = walked[1..].iter().map(|&f| f as usize).collect(); - assert_eq!(ours, system); + let frames = unwind(context.0); + let walked = walked[..usize::from(n)] + .iter() + .map(|&f| f as usize) + .collect(); + (frames, walked) + } + } + + /// `level_a` → `level_b` → `level_c` → [`capture_frames`]: three known + /// callers above the capture. `black_box` after each call keeps it from + /// becoming a tail call, which would erase the caller's frame. + #[inline(never)] + fn level_a() -> Captured { + let captured = level_b(); + std::hint::black_box(&captured); + captured + } + + /// See [`level_a`]. + #[inline(never)] + fn level_b() -> Captured { + let captured = level_c(); + std::hint::black_box(&captured); + captured + } + + /// See [`level_a`]. + #[inline(never)] + fn level_c() -> Captured { + let captured = capture_frames(); + std::hint::black_box(&captured); + captured } #[test]