loader(crash): verify unwind against known caller frames when RtlCaptureStackBackTrace returns empty
(cherry picked from commit 121412bec21b0790d04d56399f49e2d140652864)
This commit is contained in:
1 parent
64c51bf223
commit
4e0591f628
1 file changed
+103
-14
@@ -766,26 +766,115 @@ mod tests {
|
||||
EXCEPTION_RECORD, RtlCaptureContext, RtlCaptureStackBackTrace,
|
||||
};
|
||||
|
||||
/// What [`capture_frames`] saw: our [`unwind`], then the system's walk.
|
||||
type Captured = (Vec<u64>, Vec<usize>);
|
||||
|
||||
/// 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<u64> {
|
||||
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<usize> = 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]
|
||||
|
||||
Reference in new issue
Block a user