| 1 |
// Copyright 2015 Ted Mielczarek. See the COPYRIGHT |
| 2 |
// file at the top-level directory of this distribution. |
| 3 |
|
| 4 |
// Note since x86 and Amd64 have basically the same ABI, this implementation |
| 5 |
// is written to largely erase the details of the two wherever possible, |
| 6 |
// so that it can be copied between the two with minimal changes. It's not |
| 7 |
// worth the effort to *actually* unify the implementations. |
| 8 |
|
| 9 |
use super::impl_prelude::*; |
| 10 |
use minidump::format::CONTEXT_X86; |
| 11 |
use minidump::{MinidumpContext, MinidumpContextValidity, MinidumpModuleList, MinidumpRawContext}; |
| 12 |
use std::collections::HashSet; |
| 13 |
use tracing::trace; |
| 14 |
|
| 15 |
type Pointer = u32; |
| 16 |
const POINTER_WIDTH: Pointer = 4; |
| 17 |
const INSTRUCTION_REGISTER: &str = "eip"; |
| 18 |
const STACK_POINTER_REGISTER: &str = "esp"; |
| 19 |
const FRAME_POINTER_REGISTER: &str = "ebp"; |
| 20 |
const CALLEE_SAVED_REGS: &[&str] = &["ebp", "ebx", "edi", "esi"]; |
| 21 |
|
| 22 |
async fn get_caller_by_cfi<P>( |
| 23 |
ctx: &CONTEXT_X86, |
| 24 |
args: &GetCallerFrameArgs<'_, P>, |
| 25 |
) -> Option<StackFrame> |
| 26 |
where |
| 27 |
P: SymbolProvider + Sync, |
| 28 |
{ |
| 29 |
trace!("trying cfi"); |
| 30 |
|
| 31 |
if let MinidumpContextValidity::Some(ref which) = args.valid() { |
| 32 |
if !which.contains(STACK_POINTER_REGISTER) { |
| 33 |
return None; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
let mut stack_walker = CfiStackWalker::from_ctx_and_args(ctx, args, callee_forwarded_regs)?; |
| 38 |
|
| 39 |
args.symbol_provider |
| 40 |
.walk_frame(stack_walker.module, &mut stack_walker) |
| 41 |
.await?; |
| 42 |
let caller_ip = stack_walker.caller_ctx.eip; |
| 43 |
let caller_sp = stack_walker.caller_ctx.esp; |
| 44 |
|
| 45 |
trace!( |
| 46 |
"cfi evaluation was successful -- caller_ip: 0x{:08x}, caller_sp: 0x{:08x}", |
| 47 |
caller_ip, |
| 48 |
caller_sp, |
| 49 |
); |
| 50 |
|
| 51 |
// Do absolutely NO validation! Yep! As long as CFI evaluation succeeds |
| 52 |
// (which does include ip and sp resolving), just blindly assume the |
| 53 |
// values are correct. I Don't Like This, but it's what breakpad does and |
| 54 |
// we should start with a baseline of parity. |
| 55 |
|
| 56 |
// FIXME?: breakpad is actually a little weary of the output of STACK WIN |
| 57 |
// cfi, and does check that instruction_seems_valid() for eip. However, |
| 58 |
// it doesn't immediately discard the results. It tentatively tries to |
| 59 |
// scan, and then if that doesn't return anything compelling, it just goes |
| 60 |
// forward with whatever STACK WIN came up with. |
| 61 |
// |
| 62 |
// The current layering of this code means that we don't actually know what |
| 63 |
// kind of cfi was used here, and the code that *does* can't do scanning. |
| 64 |
// For now let's just trust the results unconditionally. We can do something |
| 65 |
// more hacky/robust if we find a compelling need to. |
| 66 |
// |
| 67 |
// It also has some weird scanning to try to adjust the computed bp? |
| 68 |
|
| 69 |
trace!("cfi result seems valid"); |
| 70 |
|
| 71 |
let context = MinidumpContext { |
| 72 |
raw: MinidumpRawContext::X86(stack_walker.caller_ctx), |
| 73 |
valid: MinidumpContextValidity::Some(stack_walker.caller_validity), |
| 74 |
}; |
| 75 |
Some(StackFrame::from_context(context, FrameTrust::CallFrameInfo)) |
| 76 |
} |
| 77 |
|
| 78 |
fn callee_forwarded_regs(valid: &MinidumpContextValidity) -> HashSet<&'static str> { |
| 79 |
match valid { |
| 80 |
MinidumpContextValidity::All => CALLEE_SAVED_REGS.iter().copied().collect(), |
| 81 |
MinidumpContextValidity::Some(ref which) => CALLEE_SAVED_REGS |
| 82 |
.iter() |
| 83 |
.filter(|®| which.contains(reg)) |
| 84 |
.copied() |
| 85 |
.collect(), |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
fn get_caller_by_frame_pointer<P>( |
| 90 |
ctx: &CONTEXT_X86, |
| 91 |
args: &GetCallerFrameArgs<'_, P>, |
| 92 |
) -> Option<StackFrame> |
| 93 |
where |
| 94 |
P: SymbolProvider + Sync, |
| 95 |
{ |
| 96 |
trace!("trying frame pointer"); |
| 97 |
if let MinidumpContextValidity::Some(ref which) = args.valid() { |
| 98 |
if !which.contains(FRAME_POINTER_REGISTER) { |
| 99 |
return None; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
let last_bp = ctx.ebp; |
| 104 |
// Assume that the standard %bp-using x86 calling convention is in |
| 105 |
// use. |
| 106 |
// |
| 107 |
// The typical x86 calling convention, when frame pointers are present, |
| 108 |
// is for the calling procedure to use CALL, which pushes the return |
| 109 |
// address onto the stack and sets the instruction pointer (%ip) to |
| 110 |
// the entry point of the called routine. The called routine then |
| 111 |
// PUSHes the calling routine's frame pointer (%bp) onto the stack |
| 112 |
// before copying the stack pointer (%sp) to the frame pointer (%bp). |
| 113 |
// Therefore, the calling procedure's frame pointer is always available |
| 114 |
// by dereferencing the called procedure's frame pointer, and the return |
| 115 |
// address is always available at the memory location immediately above |
| 116 |
// the address pointed to by the called procedure's frame pointer. The |
| 117 |
// calling procedure's stack pointer (%sp) is 2 pointers higher than the |
| 118 |
// value of the called procedure's frame pointer at the time the calling |
| 119 |
// procedure made the CALL: 1 pointer for the return address pushed by the |
| 120 |
// CALL itself, and 1 pointer for the callee's PUSH of the caller's frame |
| 121 |
// pointer. |
| 122 |
// |
| 123 |
// %ip_new = *(%bp_old + ptr) |
| 124 |
// %bp_new = *(%bp_old) |
| 125 |
// %sp_new = %bp_old + ptr*2 |
| 126 |
|
| 127 |
if last_bp >= u32::MAX - POINTER_WIDTH * 2 { |
| 128 |
// Although this code generally works fine if the pointer math overflows, |
| 129 |
// debug builds will still panic, and this guard protects against it without |
| 130 |
// drowning the rest of the code in checked_add. |
| 131 |
return None; |
| 132 |
} |
| 133 |
let caller_ip = args |
| 134 |
.stack_memory |
| 135 |
.get_memory_at_address(last_bp as u64 + POINTER_WIDTH as u64)?; |
| 136 |
let caller_bp = args.stack_memory.get_memory_at_address(last_bp as u64)?; |
| 137 |
let caller_sp = last_bp + POINTER_WIDTH * 2; |
| 138 |
|
| 139 |
// NOTE: minor divergence from x64 impl here: doing extra validation on the |
| 140 |
// value of `caller_sp` and `caller_bp` here encourages the stack scanner |
| 141 |
// to kick in and start outputting extra frames for `/testdata/test.dmp`. |
| 142 |
// Since breakpad also doesn't output those frames, let's assume that's |
| 143 |
// desirable. |
| 144 |
|
| 145 |
trace!( |
| 146 |
"frame pointer seems valid -- caller_ip: 0x{:08x}, caller_sp: 0x{:08x}", |
| 147 |
caller_ip, |
| 148 |
caller_sp, |
| 149 |
); |
| 150 |
|
| 151 |
let caller_ctx = CONTEXT_X86 { |
| 152 |
eip: caller_ip, |
| 153 |
esp: caller_sp, |
| 154 |
ebp: caller_bp, |
| 155 |
..CONTEXT_X86::default() |
| 156 |
}; |
| 157 |
let mut valid = HashSet::new(); |
| 158 |
valid.insert(INSTRUCTION_REGISTER); |
| 159 |
valid.insert(STACK_POINTER_REGISTER); |
| 160 |
valid.insert(FRAME_POINTER_REGISTER); |
| 161 |
let context = MinidumpContext { |
| 162 |
raw: MinidumpRawContext::X86(caller_ctx), |
| 163 |
valid: MinidumpContextValidity::Some(valid), |
| 164 |
}; |
| 165 |
Some(StackFrame::from_context(context, FrameTrust::FramePointer)) |
| 166 |
} |
| 167 |
|
| 168 |
async fn get_caller_by_scan<P>( |
| 169 |
ctx: &CONTEXT_X86, |
| 170 |
args: &GetCallerFrameArgs<'_, P>, |
| 171 |
) -> Option<StackFrame> |
| 172 |
where |
| 173 |
P: SymbolProvider + Sync, |
| 174 |
{ |
| 175 |
trace!("trying scan"); |
| 176 |
// Stack scanning is just walking from the end of the frame until we encounter |
| 177 |
// a value on the stack that looks like a pointer into some code (it's an address |
| 178 |
// in a range covered by one of our modules). If we find such an instruction, |
| 179 |
// we assume it's an ip value that was pushed by the CALL instruction that created |
| 180 |
// the current frame. The next frame is then assumed to end just before that |
| 181 |
// ip value. |
| 182 |
let last_bp = match args.valid() { |
| 183 |
MinidumpContextValidity::All => Some(ctx.ebp), |
| 184 |
MinidumpContextValidity::Some(ref which) => { |
| 185 |
if !which.contains(STACK_POINTER_REGISTER) { |
| 186 |
trace!("cannot scan without stack pointer"); |
| 187 |
return None; |
| 188 |
} |
| 189 |
if which.contains(FRAME_POINTER_REGISTER) { |
| 190 |
Some(ctx.ebp) |
| 191 |
} else { |
| 192 |
None |
| 193 |
} |
| 194 |
} |
| 195 |
}; |
| 196 |
let last_sp = ctx.esp; |
| 197 |
|
| 198 |
// Number of pointer-sized values to scan through in our search. |
| 199 |
let default_scan_range = 40; |
| 200 |
let extended_scan_range = default_scan_range * 4; |
| 201 |
|
| 202 |
// Breakpad devs found that the first frame of an unwind can be really messed up, |
| 203 |
// and therefore benefits from a longer scan. Let's do it too. |
| 204 |
let scan_range = if let FrameTrust::Context = args.callee_frame.trust { |
| 205 |
extended_scan_range |
| 206 |
} else { |
| 207 |
default_scan_range |
| 208 |
}; |
| 209 |
|
| 210 |
for i in 0..scan_range { |
| 211 |
let address_of_ip = last_sp.checked_add(i * POINTER_WIDTH)?; |
| 212 |
let caller_ip = args |
| 213 |
.stack_memory |
| 214 |
.get_memory_at_address(address_of_ip as u64)?; |
| 215 |
if instruction_seems_valid(caller_ip, args.modules, args.symbol_provider).await { |
| 216 |
// ip is pushed by CALL, so sp is just address_of_ip + ptr |
| 217 |
let caller_sp = address_of_ip.checked_add(POINTER_WIDTH)?; |
| 218 |
|
| 219 |
// Try to restore bp as well. This can be possible in two cases: |
| 220 |
// |
| 221 |
// 1. This function has the standard prologue that pushes bp and |
| 222 |
// sets bp = sp. If this is the case, then the current bp should be |
| 223 |
// immediately after (before in memory) address_of_ip. |
| 224 |
// |
| 225 |
// 2. This function does not use bp, and has just preserved it |
| 226 |
// from the caller. If this is the case, bp should be before |
| 227 |
// (after in memory) address_of_ip. |
| 228 |
// |
| 229 |
// We then try our best to eliminate bogus-looking bp's with some |
| 230 |
// simple heuristics like "is a valid stack address". |
| 231 |
let mut caller_bp = None; |
| 232 |
|
| 233 |
// Max reasonable size for a single x86 frame is 128 KB. This value is used in |
| 234 |
// a heuristic for recovering of the EBP chain after a scan for return address. |
| 235 |
// This value is based on a stack frame size histogram built for a set of |
| 236 |
// popular third party libraries which suggests that 99.5% of all frames are |
| 237 |
// smaller than 128 KB. |
| 238 |
const MAX_REASONABLE_GAP_BETWEEN_FRAMES: Pointer = 128 * 1024; |
| 239 |
|
| 240 |
// If we're on the first iteration of the scan, there can't possibly be a frame pointer, |
| 241 |
// because the entire stack frame is taken up by the return pointer. And if we're |
| 242 |
// not on the first iteration, then the last iteration already loaded the location |
| 243 |
// we expect the frame pointer to be in, so we can unconditionally load it here. |
| 244 |
if i > 0 { |
| 245 |
let address_of_bp = address_of_ip - POINTER_WIDTH; |
| 246 |
let bp = args |
| 247 |
.stack_memory |
| 248 |
.get_memory_at_address(address_of_bp as u64)?; |
| 249 |
|
| 250 |
if bp > address_of_ip && bp - address_of_bp <= MAX_REASONABLE_GAP_BETWEEN_FRAMES { |
| 251 |
// Sanity check that resulting bp is still inside stack memory. |
| 252 |
if args |
| 253 |
.stack_memory |
| 254 |
.get_memory_at_address::<Pointer>(bp as u64) |
| 255 |
.is_some() |
| 256 |
{ |
| 257 |
caller_bp = Some(bp); |
| 258 |
} |
| 259 |
} else if let Some(last_bp) = last_bp { |
| 260 |
if last_bp >= caller_sp { |
| 261 |
// Sanity check that resulting bp is still inside stack memory. |
| 262 |
if args |
| 263 |
.stack_memory |
| 264 |
.get_memory_at_address::<Pointer>(last_bp as u64) |
| 265 |
.is_some() |
| 266 |
{ |
| 267 |
caller_bp = Some(last_bp); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
trace!( |
| 274 |
"scan seems valid -- caller_ip: 0x{:08x}, caller_sp: 0x{:08x}", |
| 275 |
caller_ip, |
| 276 |
caller_sp, |
| 277 |
); |
| 278 |
|
| 279 |
let caller_ctx = CONTEXT_X86 { |
| 280 |
eip: caller_ip, |
| 281 |
esp: caller_sp, |
| 282 |
ebp: caller_bp.unwrap_or(0), |
| 283 |
..CONTEXT_X86::default() |
| 284 |
}; |
| 285 |
let mut valid = HashSet::new(); |
| 286 |
valid.insert(INSTRUCTION_REGISTER); |
| 287 |
valid.insert(STACK_POINTER_REGISTER); |
| 288 |
if caller_bp.is_some() { |
| 289 |
valid.insert(FRAME_POINTER_REGISTER); |
| 290 |
} |
| 291 |
let context = MinidumpContext { |
| 292 |
raw: MinidumpRawContext::X86(caller_ctx), |
| 293 |
valid: MinidumpContextValidity::Some(valid), |
| 294 |
}; |
| 295 |
return Some(StackFrame::from_context(context, FrameTrust::Scan)); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
None |
| 300 |
} |
| 301 |
|
| 302 |
/// The most strict validation we have for instruction pointers. |
| 303 |
/// |
| 304 |
/// This is only used for stack-scanning, because it's explicitly |
| 305 |
/// trying to distinguish between total garbage and correct values. |
| 306 |
/// cfi and frame_pointer approaches do not use this validation |
| 307 |
/// because by default they're working with plausible/trustworthy |
| 308 |
/// data. |
| 309 |
/// |
| 310 |
/// Specifically, not using this validation allows cfi/fp methods |
| 311 |
/// to unwind through frames we don't have mapped modules for (such as |
| 312 |
/// OS APIs). This may seem confusing since we obviously don't have cfi |
| 313 |
/// for unmapped modules! |
| 314 |
/// |
| 315 |
/// The way this works is that we will use cfi to unwind some frame we |
| 316 |
/// know about and *end up* in a function we know nothing about, but with |
| 317 |
/// all the right register values. At this point, frame pointers will |
| 318 |
/// often do the correct thing even though we don't know what code we're |
| 319 |
/// in -- until we get back into code we do know about and cfi kicks back in. |
| 320 |
/// At worst, this sets scanning up in a better position for success! |
| 321 |
/// |
| 322 |
/// If we applied this more rigorous validation to cfi/fp methods, we |
| 323 |
/// would just discard the correct register values from the known frame |
| 324 |
/// and immediately start doing unreliable scans. |
| 325 |
async fn instruction_seems_valid<P>( |
| 326 |
instruction: Pointer, |
| 327 |
modules: &MinidumpModuleList, |
| 328 |
symbol_provider: &P, |
| 329 |
) -> bool |
| 330 |
where |
| 331 |
P: SymbolProvider + Sync, |
| 332 |
{ |
| 333 |
if instruction == 0 { |
| 334 |
return false; |
| 335 |
} |
| 336 |
|
| 337 |
super::instruction_seems_valid_by_symbols(instruction as u64, modules, symbol_provider).await |
| 338 |
} |
| 339 |
|
| 340 |
/* |
| 341 |
// x86 is currently hyper-permissive, so we don't use this, |
| 342 |
// but here it is in case we change our minds! |
| 343 |
fn stack_seems_valid( |
| 344 |
caller_sp: Pointer, |
| 345 |
callee_sp: Pointer, |
| 346 |
stack_memory: UnifiedMemory<'_, '_>, |
| 347 |
) -> bool { |
| 348 |
// The stack shouldn't *grow* when we unwind |
| 349 |
if caller_sp <= callee_sp { |
| 350 |
return false; |
| 351 |
} |
| 352 |
|
| 353 |
// The stack pointer should be in the stack |
| 354 |
stack_memory |
| 355 |
.get_memory_at_address::<Pointer>(caller_sp as u64) |
| 356 |
.is_some() |
| 357 |
} |
| 358 |
*/ |
| 359 |
|
| 360 |
pub async fn get_caller_frame<P>( |
| 361 |
ctx: &CONTEXT_X86, |
| 362 |
args: &GetCallerFrameArgs<'_, P>, |
| 363 |
) -> Option<StackFrame> |
| 364 |
where |
| 365 |
P: SymbolProvider + Sync, |
| 366 |
{ |
| 367 |
// .await doesn't like closures, so don't use Option chaining |
| 368 |
let mut frame = None; |
| 369 |
if frame.is_none() { |
| 370 |
frame = get_caller_by_cfi(ctx, args).await; |
| 371 |
} |
| 372 |
if frame.is_none() { |
| 373 |
frame = get_caller_by_frame_pointer(ctx, args); |
| 374 |
} |
| 375 |
if frame.is_none() { |
| 376 |
frame = get_caller_by_scan(ctx, args).await; |
| 377 |
} |
| 378 |
let mut frame = frame?; |
| 379 |
|
| 380 |
// We now check the frame to see if it looks like unwinding is complete, |
| 381 |
// based on the frame we computed having a nonsense value. Returning |
| 382 |
// None signals to the unwinder to stop unwinding. |
| 383 |
|
| 384 |
// if the instruction is within the first ~page of memory, it's basically |
| 385 |
// null, and we can assume unwinding is complete. |
| 386 |
if frame.context.get_instruction_pointer() < 4096 { |
| 387 |
trace!("instruction pointer was nullish, assuming unwind complete"); |
| 388 |
return None; |
| 389 |
} |
| 390 |
// If the new stack pointer is at a lower address than the old, |
| 391 |
// then that's clearly incorrect. Treat this as end-of-stack to |
| 392 |
// enforce progress and avoid infinite loops. |
| 393 |
if frame.context.get_stack_pointer() <= ctx.esp as u64 { |
| 394 |
trace!("stack pointer went backwards, assuming unwind complete"); |
| 395 |
return None; |
| 396 |
} |
| 397 |
|
| 398 |
// Ok, the frame now seems well and truly valid, do final cleanup. |
| 399 |
|
| 400 |
// A caller's ip is the return address, which is the instruction |
| 401 |
// *after* the CALL that caused us to arrive at the callee. Set |
| 402 |
// the value to one less than that, so it points within the |
| 403 |
// CALL instruction. This is important because we use this value |
| 404 |
// to lookup the CFI we need to unwind the next frame. |
| 405 |
let ip = frame.context.get_instruction_pointer(); |
| 406 |
frame.instruction = ip - 1; |
| 407 |
|
| 408 |
Some(frame) |
| 409 |
} |