| 1 |
// Copyright 2015 Ted Mielczarek. See the COPYRIGHT |
| 2 |
// file at the top-level directory of this distribution. |
| 3 |
|
| 4 |
use crate::*; |
| 5 |
use minidump::format::CONTEXT_ARM; |
| 6 |
use minidump::system_info::{Cpu, Os}; |
| 7 |
use std::collections::HashMap; |
| 8 |
use test_assembler::*; |
| 9 |
|
| 10 |
struct TestFixture { |
| 11 |
pub raw: CONTEXT_ARM, |
| 12 |
pub modules: MinidumpModuleList, |
| 13 |
pub system_info: SystemInfo, |
| 14 |
pub symbols: HashMap<String, String>, |
| 15 |
} |
| 16 |
|
| 17 |
impl TestFixture { |
| 18 |
pub fn new() -> TestFixture { |
| 19 |
TestFixture { |
| 20 |
raw: CONTEXT_ARM::default(), |
| 21 |
// Give the two modules reasonable standard locations and names |
| 22 |
// for tests to play with. |
| 23 |
modules: MinidumpModuleList::from_modules(vec![ |
| 24 |
MinidumpModule::new(0x40000000, 0x10000, "module1"), |
| 25 |
MinidumpModule::new(0x50000000, 0x10000, "module2"), |
| 26 |
]), |
| 27 |
system_info: SystemInfo { |
| 28 |
os: Os::Ios, |
| 29 |
os_version: None, |
| 30 |
os_build: None, |
| 31 |
cpu: Cpu::Arm, |
| 32 |
cpu_info: None, |
| 33 |
cpu_microcode_version: None, |
| 34 |
cpu_count: 1, |
| 35 |
}, |
| 36 |
symbols: HashMap::new(), |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
pub async fn walk_stack(&self, stack: Section) -> CallStack { |
| 41 |
let context = MinidumpContext { |
| 42 |
raw: MinidumpRawContext::Arm(self.raw.clone()), |
| 43 |
valid: MinidumpContextValidity::All, |
| 44 |
}; |
| 45 |
let base = stack.start().value().unwrap(); |
| 46 |
let size = stack.size(); |
| 47 |
let stack = stack.get_contents().unwrap(); |
| 48 |
let stack_memory = MinidumpMemory { |
| 49 |
desc: Default::default(), |
| 50 |
base_address: base, |
| 51 |
size, |
| 52 |
bytes: &stack, |
| 53 |
endian: scroll::LE, |
| 54 |
}; |
| 55 |
let symbolizer = Symbolizer::new(string_symbol_supplier(self.symbols.clone())); |
| 56 |
let mut stack = CallStack::with_context(context); |
| 57 |
|
| 58 |
walk_stack( |
| 59 |
0, |
| 60 |
(), |
| 61 |
&mut stack, |
| 62 |
Some(UnifiedMemory::Memory(&stack_memory)), |
| 63 |
&self.modules, |
| 64 |
&self.system_info, |
| 65 |
&symbolizer, |
| 66 |
) |
| 67 |
.await; |
| 68 |
|
| 69 |
stack |
| 70 |
} |
| 71 |
|
| 72 |
pub fn add_symbols(&mut self, name: String, symbols: String) { |
| 73 |
self.symbols.insert(name, symbols); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
#[tokio::test] |
| 78 |
async fn test_simple() { |
| 79 |
let mut f = TestFixture::new(); |
| 80 |
let stack = Section::new(); |
| 81 |
stack.start().set_const(0x80000000); |
| 82 |
// There should be no references to the stack in this walk: we don't |
| 83 |
// provide any call frame information, so trying to reconstruct the |
| 84 |
// context frame's caller should fail. So there's no need for us to |
| 85 |
// provide stack contents. |
| 86 |
f.raw.set_register("pc", 0x4000c020); |
| 87 |
f.raw.set_register("fp", 0x80000000); |
| 88 |
|
| 89 |
let s = f.walk_stack(stack).await; |
| 90 |
assert_eq!(s.frames.len(), 1); |
| 91 |
let f = &s.frames[0]; |
| 92 |
let m = f.module.as_ref().unwrap(); |
| 93 |
assert_eq!(m.code_file(), "module1"); |
| 94 |
} |
| 95 |
|
| 96 |
#[tokio::test] |
| 97 |
async fn test_scan_without_symbols() { |
| 98 |
// Scanning should work without any symbols |
| 99 |
let mut f = TestFixture::new(); |
| 100 |
let mut stack = Section::new(); |
| 101 |
stack.start().set_const(0x80000000); |
| 102 |
|
| 103 |
let return_address1 = 0x50000100u32; |
| 104 |
let return_address2 = 0x50000900u32; |
| 105 |
let frame1_sp = Label::new(); |
| 106 |
let frame2_sp = Label::new(); |
| 107 |
|
| 108 |
stack = stack |
| 109 |
// frame 0 |
| 110 |
.append_repeated(0, 16) // space |
| 111 |
.D32(0x40090000) // junk that's not |
| 112 |
.D32(0x60000000) // a return address |
| 113 |
.D32(return_address1) // actual return address |
| 114 |
// frame 1 |
| 115 |
.mark(&frame1_sp) |
| 116 |
.append_repeated(0, 16) // space |
| 117 |
.D32(0xF0000000) // more junk |
| 118 |
.D32(0x0000000D) |
| 119 |
.D32(return_address2) // actual return address |
| 120 |
// frame 2 |
| 121 |
.mark(&frame2_sp) |
| 122 |
.append_repeated(0, 32); // end of stack |
| 123 |
|
| 124 |
f.raw.set_register("pc", 0x40005510); |
| 125 |
// set an invalid non-zero value for the frame pointer |
| 126 |
// to force stack scanning |
| 127 |
f.raw.set_register("fp", 0x00000001); |
| 128 |
f.raw |
| 129 |
.set_register("sp", stack.start().value().unwrap() as u32); |
| 130 |
|
| 131 |
let s = f.walk_stack(stack).await; |
| 132 |
assert_eq!(s.frames.len(), 3); |
| 133 |
|
| 134 |
{ |
| 135 |
// Frame 0 |
| 136 |
let frame = &s.frames[0]; |
| 137 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 138 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 139 |
} |
| 140 |
|
| 141 |
{ |
| 142 |
// Frame 1 |
| 143 |
let frame = &s.frames[1]; |
| 144 |
let valid = &frame.context.valid; |
| 145 |
assert_eq!(frame.trust, FrameTrust::Scan); |
| 146 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 147 |
assert_eq!(which.len(), 2); |
| 148 |
} else { |
| 149 |
unreachable!(); |
| 150 |
} |
| 151 |
|
| 152 |
if let MinidumpRawContext::Arm(ctx) = &frame.context.raw { |
| 153 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address1); |
| 154 |
assert_eq!( |
| 155 |
ctx.get_register("sp", valid).unwrap(), |
| 156 |
frame1_sp.value().unwrap() as u32 |
| 157 |
); |
| 158 |
} else { |
| 159 |
unreachable!(); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
{ |
| 164 |
// Frame 2 |
| 165 |
let frame = &s.frames[2]; |
| 166 |
let valid = &frame.context.valid; |
| 167 |
assert_eq!(frame.trust, FrameTrust::Scan); |
| 168 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 169 |
assert_eq!(which.len(), 2); |
| 170 |
} else { |
| 171 |
unreachable!(); |
| 172 |
} |
| 173 |
|
| 174 |
if let MinidumpRawContext::Arm(ctx) = &frame.context.raw { |
| 175 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address2); |
| 176 |
assert_eq!( |
| 177 |
ctx.get_register("sp", valid).unwrap(), |
| 178 |
frame2_sp.value().unwrap() as u32 |
| 179 |
); |
| 180 |
} else { |
| 181 |
unreachable!(); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
#[tokio::test] |
| 187 |
async fn test_scan_first_frame() { |
| 188 |
// The first (context) frame gets extra long scans, this test checks that. |
| 189 |
let mut f = TestFixture::new(); |
| 190 |
let mut stack = Section::new(); |
| 191 |
stack.start().set_const(0x80000000); |
| 192 |
|
| 193 |
let return_address1 = 0x50000100u32; |
| 194 |
let return_address2 = 0x50000900u32; |
| 195 |
let frame1_sp = Label::new(); |
| 196 |
let frame2_sp = Label::new(); |
| 197 |
|
| 198 |
stack = stack |
| 199 |
// frame 0 |
| 200 |
.append_repeated(0, 16) // space |
| 201 |
.D32(0x40090000) // junk that's not |
| 202 |
.D32(0x60000000) // a return address |
| 203 |
.append_repeated(0, 96) // more space |
| 204 |
.D32(return_address1) // actual return address |
| 205 |
// frame 1 |
| 206 |
.mark(&frame1_sp) |
| 207 |
.append_repeated(0, 32) // space |
| 208 |
.D32(0xF0000000) // more junk |
| 209 |
.D32(0x0000000D) |
| 210 |
.append_repeated(0, 336) // more space |
| 211 |
.D32(return_address2) // actual return address (won't be found) |
| 212 |
// frame 2 |
| 213 |
.mark(&frame2_sp) |
| 214 |
.append_repeated(0, 64); // end of stack |
| 215 |
|
| 216 |
f.raw.set_register("pc", 0x40005510); |
| 217 |
// set an invalid non-zero value for the frame pointer |
| 218 |
// to force stack scanning |
| 219 |
f.raw.set_register("fp", 0x00000001); |
| 220 |
f.raw |
| 221 |
.set_register("sp", stack.start().value().unwrap() as u32); |
| 222 |
|
| 223 |
let s = f.walk_stack(stack).await; |
| 224 |
assert_eq!(s.frames.len(), 2); |
| 225 |
|
| 226 |
{ |
| 227 |
// Frame 0 |
| 228 |
let frame = &s.frames[0]; |
| 229 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 230 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 231 |
} |
| 232 |
|
| 233 |
{ |
| 234 |
// Frame 1 |
| 235 |
let frame = &s.frames[1]; |
| 236 |
let valid = &frame.context.valid; |
| 237 |
assert_eq!(frame.trust, FrameTrust::Scan); |
| 238 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 239 |
assert_eq!(which.len(), 2); |
| 240 |
} else { |
| 241 |
unreachable!(); |
| 242 |
} |
| 243 |
|
| 244 |
if let MinidumpRawContext::Arm(ctx) = &frame.context.raw { |
| 245 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address1); |
| 246 |
assert_eq!( |
| 247 |
ctx.get_register("sp", valid).unwrap(), |
| 248 |
frame1_sp.value().unwrap() as u32 |
| 249 |
); |
| 250 |
} else { |
| 251 |
unreachable!(); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
#[tokio::test] |
| 257 |
async fn test_invalid_lr() { |
| 258 |
let mut f = TestFixture::new(); |
| 259 |
f.system_info.os = Os::Linux; |
| 260 |
|
| 261 |
let mut stack = Section::new(); |
| 262 |
stack.start().set_const(0x80000000); |
| 263 |
|
| 264 |
let lr = Label::new(); |
| 265 |
let return_address1 = 0x50000100u32; |
| 266 |
let return_address2 = 0x50000900u32; |
| 267 |
let frame1_sp = Label::new(); |
| 268 |
let frame2_sp = Label::new(); |
| 269 |
let frame1_fp = Label::new(); |
| 270 |
let frame2_fp = Label::new(); |
| 271 |
|
| 272 |
stack = stack |
| 273 |
// frame 0 |
| 274 |
.append_repeated(0, 32) // space |
| 275 |
.mark(&lr) // the LR points to something on the stack |
| 276 |
.D32(0x0000000D) // junk that's not |
| 277 |
.D32(0xF0000000) // a return address |
| 278 |
.mark(&frame1_fp) // next fp will point to the next value |
| 279 |
.D32(&frame2_fp) // save current frame pointer |
| 280 |
.D32(return_address1) // save current link register |
| 281 |
.mark(&frame1_sp) |
| 282 |
// frame 1 |
| 283 |
.append_repeated(0, 32) // space |
| 284 |
.D32(0x0000000D) // junk that's not |
| 285 |
.D32(0xF0000000) // a return address |
| 286 |
.mark(&frame2_fp) |
| 287 |
.D32(0) |
| 288 |
.D32(return_address2) |
| 289 |
.mark(&frame2_sp); |
| 290 |
|
| 291 |
f.raw.set_register("pc", 0x40005510); |
| 292 |
f.raw.set_register("lr", lr.value().unwrap() as u32); |
| 293 |
f.raw.set_register("fp", frame1_fp.value().unwrap() as u32); |
| 294 |
f.raw |
| 295 |
.set_register("sp", stack.start().value().unwrap() as u32); |
| 296 |
|
| 297 |
let s = f.walk_stack(stack).await; |
| 298 |
assert_eq!(s.frames.len(), 3); |
| 299 |
|
| 300 |
{ |
| 301 |
// Frame 0 |
| 302 |
let frame = &s.frames[0]; |
| 303 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 304 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 305 |
} |
| 306 |
|
| 307 |
{ |
| 308 |
// Frame 1 |
| 309 |
let frame = &s.frames[1]; |
| 310 |
let valid = &frame.context.valid; |
| 311 |
assert_eq!(frame.trust, FrameTrust::Scan); |
| 312 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 313 |
assert_eq!(which.len(), 2); |
| 314 |
} else { |
| 315 |
unreachable!(); |
| 316 |
} |
| 317 |
|
| 318 |
if let MinidumpRawContext::Arm(ctx) = &frame.context.raw { |
| 319 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address1); |
| 320 |
assert_eq!( |
| 321 |
ctx.get_register("sp", valid).unwrap(), |
| 322 |
frame1_sp.value().unwrap() as u32 |
| 323 |
); |
| 324 |
} else { |
| 325 |
unreachable!(); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
{ |
| 330 |
// Frame 2 |
| 331 |
let frame = &s.frames[2]; |
| 332 |
let valid = &frame.context.valid; |
| 333 |
assert_eq!(frame.trust, FrameTrust::Scan); |
| 334 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 335 |
assert_eq!(which.len(), 2); |
| 336 |
} else { |
| 337 |
unreachable!(); |
| 338 |
} |
| 339 |
|
| 340 |
if let MinidumpRawContext::Arm(ctx) = &frame.context.raw { |
| 341 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address2); |
| 342 |
assert_eq!( |
| 343 |
ctx.get_register("sp", valid).unwrap(), |
| 344 |
frame2_sp.value().unwrap() as u32 |
| 345 |
); |
| 346 |
} else { |
| 347 |
unreachable!(); |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
#[tokio::test] |
| 353 |
async fn test_frame_pointer() { |
| 354 |
// Frame-pointer-based unwinding |
| 355 |
let mut f = TestFixture::new(); |
| 356 |
let mut stack = Section::new(); |
| 357 |
stack.start().set_const(0x80000000); |
| 358 |
|
| 359 |
let return_address1 = 0x50000100u32; |
| 360 |
let return_address2 = 0x50000900u32; |
| 361 |
let frame1_sp = Label::new(); |
| 362 |
let frame2_sp = Label::new(); |
| 363 |
let frame0_fp = Label::new(); |
| 364 |
let frame1_fp = Label::new(); |
| 365 |
let frame2_fp = Label::new(); |
| 366 |
|
| 367 |
stack = stack |
| 368 |
// frame 0 |
| 369 |
.append_repeated(0, 32) // space |
| 370 |
.D32(0x0000000D) // junk that's not |
| 371 |
.D32(0xF0000000) // a return address |
| 372 |
.mark(&frame0_fp) // next fp will point to the next value |
| 373 |
.D32(&frame1_fp) // save current frame pointer |
| 374 |
.D32(return_address1) // save current link register |
| 375 |
.mark(&frame1_sp) |
| 376 |
// frame 1 |
| 377 |
.append_repeated(0, 32) // space |
| 378 |
.D32(0x0000000D) // junk that's not |
| 379 |
.D32(0xF0000000) // a return address |
| 380 |
.mark(&frame1_fp) |
| 381 |
.D32(&frame2_fp) |
| 382 |
.D32(return_address2) |
| 383 |
.mark(&frame2_sp) |
| 384 |
// frame 2 |
| 385 |
.append_repeated(0, 32) // Whatever values on the stack. |
| 386 |
.D32(0x0000000D) // junk that's not |
| 387 |
.D32(0xF0000000) // a return address. |
| 388 |
.mark(&frame2_fp) |
| 389 |
.D32(0) |
| 390 |
.D32(0); |
| 391 |
|
| 392 |
f.raw.set_register("pc", 0x40005510); |
| 393 |
f.raw.set_register("lr", return_address1); |
| 394 |
f.raw.set_register("fp", frame0_fp.value().unwrap() as u32); |
| 395 |
f.raw |
| 396 |
.set_register("sp", stack.start().value().unwrap() as u32); |
| 397 |
|
| 398 |
let s = f.walk_stack(stack).await; |
| 399 |
assert_eq!(s.frames.len(), 3); |
| 400 |
|
| 401 |
{ |
| 402 |
// Frame 0 |
| 403 |
let frame = &s.frames[0]; |
| 404 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 405 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 406 |
} |
| 407 |
|
| 408 |
{ |
| 409 |
// Frame 1 |
| 410 |
let frame = &s.frames[1]; |
| 411 |
let valid = &frame.context.valid; |
| 412 |
assert_eq!(frame.trust, FrameTrust::FramePointer); |
| 413 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 414 |
assert_eq!(which.len(), 3); |
| 415 |
} else { |
| 416 |
unreachable!(); |
| 417 |
} |
| 418 |
|
| 419 |
if let MinidumpRawContext::Arm(ctx) = &frame.context.raw { |
| 420 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address1); |
| 421 |
assert_eq!( |
| 422 |
ctx.get_register("sp", valid).unwrap(), |
| 423 |
frame1_sp.value().unwrap() as u32 |
| 424 |
); |
| 425 |
assert_eq!( |
| 426 |
ctx.get_register("fp", valid).unwrap(), |
| 427 |
frame1_fp.value().unwrap() as u32 |
| 428 |
); |
| 429 |
} else { |
| 430 |
unreachable!(); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
{ |
| 435 |
// Frame 2 |
| 436 |
let frame = &s.frames[2]; |
| 437 |
let valid = &frame.context.valid; |
| 438 |
assert_eq!(frame.trust, FrameTrust::FramePointer); |
| 439 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 440 |
assert_eq!(which.len(), 3); |
| 441 |
} else { |
| 442 |
unreachable!(); |
| 443 |
} |
| 444 |
|
| 445 |
if let MinidumpRawContext::Arm(ctx) = &frame.context.raw { |
| 446 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address2); |
| 447 |
assert_eq!( |
| 448 |
ctx.get_register("sp", valid).unwrap(), |
| 449 |
frame2_sp.value().unwrap() as u32 |
| 450 |
); |
| 451 |
assert_eq!( |
| 452 |
ctx.get_register("fp", valid).unwrap(), |
| 453 |
frame2_fp.value().unwrap() as u32 |
| 454 |
); |
| 455 |
} else { |
| 456 |
unreachable!(); |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
#[tokio::test] |
| 462 |
async fn test_frame_pointer_stackless_leaf() { |
| 463 |
// Same as test_frame_pointer but frame0 is a stackless leaf. |
| 464 |
// |
| 465 |
// In the current implementation we will misunderstand this slightly |
| 466 |
// and basically "lose" frame 1, but still properly recover frame 2. |
| 467 |
// THIS TEST BREAKING MIGHT MEAN YOU'VE MADE THINGS WORK BETTER! |
| 468 |
let mut f = TestFixture::new(); |
| 469 |
let mut stack = Section::new(); |
| 470 |
stack.start().set_const(0x80000000); |
| 471 |
|
| 472 |
let return_address1 = 0x50000100u32; |
| 473 |
let return_address2 = 0x50000900u32; |
| 474 |
let frame1_sp = Label::new(); |
| 475 |
let frame2_sp = Label::new(); |
| 476 |
let frame1_fp = Label::new(); |
| 477 |
let frame2_fp = Label::new(); |
| 478 |
|
| 479 |
stack = stack |
| 480 |
// frame 0 (literally nothing!) |
| 481 |
.mark(&frame1_sp) |
| 482 |
// frame 1 (this is sadly dropped) |
| 483 |
.append_repeated(0, 32) // space |
| 484 |
.D32(0x0000000D) // junk that's not |
| 485 |
.D32(0xF0000000) // a return address |
| 486 |
.mark(&frame1_fp) |
| 487 |
.D32(&frame2_fp) |
| 488 |
.D32(return_address2) |
| 489 |
.mark(&frame2_sp) |
| 490 |
// frame 2 |
| 491 |
.append_repeated(0, 32) // Whatever values on the stack. |
| 492 |
.D32(0x0000000D) // junk that's not |
| 493 |
.D32(0xF0000000) // a return address. |
| 494 |
.mark(&frame2_fp) |
| 495 |
.D32(0) |
| 496 |
.D32(0); |
| 497 |
|
| 498 |
f.raw.set_register("pc", 0x40005510); |
| 499 |
f.raw.set_register("lr", return_address1); // we will sadly ignore this |
| 500 |
f.raw.set_register("fp", frame1_fp.value().unwrap() as u32); |
| 501 |
f.raw |
| 502 |
.set_register("sp", stack.start().value().unwrap() as u32); |
| 503 |
|
| 504 |
let s = f.walk_stack(stack).await; |
| 505 |
assert_eq!(s.frames.len(), 2); |
| 506 |
|
| 507 |
{ |
| 508 |
// Frame 0 |
| 509 |
let frame = &s.frames[0]; |
| 510 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 511 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 512 |
} |
| 513 |
|
| 514 |
{ |
| 515 |
// Frame 2 (Found as Frame 1) |
| 516 |
let frame = &s.frames[1]; |
| 517 |
let valid = &frame.context.valid; |
| 518 |
assert_eq!(frame.trust, FrameTrust::FramePointer); |
| 519 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 520 |
assert_eq!(which.len(), 3); |
| 521 |
} else { |
| 522 |
unreachable!(); |
| 523 |
} |
| 524 |
|
| 525 |
if let MinidumpRawContext::Arm(ctx) = &frame.context.raw { |
| 526 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address2); |
| 527 |
assert_eq!( |
| 528 |
ctx.get_register("sp", valid).unwrap(), |
| 529 |
frame2_sp.value().unwrap() as u32 |
| 530 |
); |
| 531 |
assert_eq!( |
| 532 |
ctx.get_register("fp", valid).unwrap(), |
| 533 |
frame2_fp.value().unwrap() as u32 |
| 534 |
); |
| 535 |
} else { |
| 536 |
unreachable!(); |
| 537 |
} |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
#[tokio::test] |
| 542 |
async fn test_frame_pointer_stackful_leaf() { |
| 543 |
// Same as test_frame_pointer but frame0 is a stackful leaf. |
| 544 |
// |
| 545 |
// In the current implementation we will misunderstand this slightly |
| 546 |
// and basically "lose" frame 1, but still properly recover frame 2. |
| 547 |
// THIS TEST BREAKING MIGHT MEAN YOU'VE MADE THINGS WORK BETTER! |
| 548 |
let mut f = TestFixture::new(); |
| 549 |
let mut stack = Section::new(); |
| 550 |
stack.start().set_const(0x80000000); |
| 551 |
|
| 552 |
let return_address1 = 0x50000100u32; |
| 553 |
let return_address2 = 0x50000900u32; |
| 554 |
let frame1_sp = Label::new(); |
| 555 |
let frame2_sp = Label::new(); |
| 556 |
let frame1_fp = Label::new(); |
| 557 |
let frame2_fp = Label::new(); |
| 558 |
|
| 559 |
stack = stack |
| 560 |
// frame 0 (all junk!) |
| 561 |
.append_repeated(0, 64) // space |
| 562 |
.D64(0x0000000D) // junk that's not |
| 563 |
.D64(0xF0000000) // a return address |
| 564 |
.mark(&frame1_sp) |
| 565 |
// frame 1 (this is sadly dropped) |
| 566 |
.append_repeated(0, 32) // space |
| 567 |
.D32(0x0000000D) // junk that's not |
| 568 |
.D32(0xF0000000) // a return address |
| 569 |
.mark(&frame1_fp) |
| 570 |
.D32(&frame2_fp) |
| 571 |
.D32(return_address2) |
| 572 |
.mark(&frame2_sp) |
| 573 |
// frame 2 |
| 574 |
.append_repeated(0, 32) // Whatever values on the stack. |
| 575 |
.D32(0x0000000D) // junk that's not |
| 576 |
.D32(0xF0000000) // a return address. |
| 577 |
.mark(&frame2_fp) |
| 578 |
.D32(0) |
| 579 |
.D32(0); |
| 580 |
|
| 581 |
f.raw.set_register("pc", 0x40005510); |
| 582 |
f.raw.set_register("lr", return_address1); // we will sadly ignore this |
| 583 |
f.raw.set_register("fp", frame1_fp.value().unwrap() as u32); |
| 584 |
f.raw |
| 585 |
.set_register("sp", stack.start().value().unwrap() as u32); |
| 586 |
|
| 587 |
let s = f.walk_stack(stack).await; |
| 588 |
assert_eq!(s.frames.len(), 2); |
| 589 |
|
| 590 |
{ |
| 591 |
// Frame 0 |
| 592 |
let frame = &s.frames[0]; |
| 593 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 594 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 595 |
} |
| 596 |
|
| 597 |
{ |
| 598 |
// Frame 2 (Found as Frame 1) |
| 599 |
let frame = &s.frames[1]; |
| 600 |
let valid = &frame.context.valid; |
| 601 |
assert_eq!(frame.trust, FrameTrust::FramePointer); |
| 602 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 603 |
assert_eq!(which.len(), 3); |
| 604 |
} else { |
| 605 |
unreachable!(); |
| 606 |
} |
| 607 |
|
| 608 |
if let MinidumpRawContext::Arm(ctx) = &frame.context.raw { |
| 609 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address2); |
| 610 |
assert_eq!( |
| 611 |
ctx.get_register("sp", valid).unwrap(), |
| 612 |
frame2_sp.value().unwrap() as u32 |
| 613 |
); |
| 614 |
assert_eq!( |
| 615 |
ctx.get_register("fp", valid).unwrap(), |
| 616 |
frame2_fp.value().unwrap() as u32 |
| 617 |
); |
| 618 |
} else { |
| 619 |
unreachable!(); |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
#[tokio::test] |
| 625 |
async fn test_frame_pointer_infinite_equality() { |
| 626 |
// Leaf functions on Arm are allowed to not update the stack pointer, so |
| 627 |
// it's valid for the frame pointer analysis to conclude that the stack |
| 628 |
// pointer doesn't change. However we must only provide this allowance |
| 629 |
// to the first stack frame, or else we're vulnerable to infinite loops. |
| 630 |
// |
| 631 |
// One of the CFI tests already checks that we allow the leaf case to work, |
| 632 |
// so here we test that we don't get stuck in an infinite loop for the |
| 633 |
// non-leaf case. |
| 634 |
// |
| 635 |
// This is just a copy-paste of test_frame_pointer except for the line |
| 636 |
// "EVIL INFINITE FRAME POINTER" has been changed from frame2_fp to frame1_fp. |
| 637 |
let mut f = TestFixture::new(); |
| 638 |
let mut stack = Section::new(); |
| 639 |
stack.start().set_const(0x80000000); |
| 640 |
|
| 641 |
let return_address1 = 0x50000100u32; |
| 642 |
let return_address2 = 0x50000900u32; |
| 643 |
let frame1_sp = Label::new(); |
| 644 |
let frame2_sp = Label::new(); |
| 645 |
let frame0_fp = Label::new(); |
| 646 |
let frame1_fp = Label::new(); |
| 647 |
let frame2_fp = Label::new(); |
| 648 |
|
| 649 |
stack = stack |
| 650 |
// frame 0 |
| 651 |
.append_repeated(0, 32) // space |
| 652 |
.D32(0x0000000D) // junk that's not |
| 653 |
.D32(0xF0000000) // a return address |
| 654 |
.mark(&frame0_fp) // next fp will point to the next value |
| 655 |
.D32(&frame0_fp) // EVIL INFINITE FRAME POINTER |
| 656 |
.D32(return_address1) // save current link register |
| 657 |
.mark(&frame1_sp) |
| 658 |
// frame 1 |
| 659 |
.append_repeated(0, 32) // space |
| 660 |
.D32(0x0000000D) // junk that's not |
| 661 |
.D32(0xF0000000) // a return address |
| 662 |
.mark(&frame1_fp) |
| 663 |
.D32(&frame2_fp) |
| 664 |
.D32(return_address2) |
| 665 |
.mark(&frame2_sp) |
| 666 |
// frame 2 |
| 667 |
.append_repeated(0, 32) // Whatever values on the stack. |
| 668 |
.D32(0x0000000D) // junk that's not |
| 669 |
.D32(0xF0000000) // a return address. |
| 670 |
.mark(&frame2_fp) |
| 671 |
.D32(0) |
| 672 |
.D32(0); |
| 673 |
|
| 674 |
f.raw.set_register("pc", 0x40005510); |
| 675 |
f.raw.set_register("lr", return_address1); |
| 676 |
f.raw.set_register("fp", frame0_fp.value().unwrap() as u32); |
| 677 |
f.raw |
| 678 |
.set_register("sp", stack.start().value().unwrap() as u32); |
| 679 |
|
| 680 |
let s = f.walk_stack(stack).await; |
| 681 |
assert_eq!(s.frames.len(), 2); |
| 682 |
|
| 683 |
{ |
| 684 |
// Frame 0 |
| 685 |
let frame = &s.frames[0]; |
| 686 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 687 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 688 |
} |
| 689 |
|
| 690 |
{ |
| 691 |
// Frame 1 (a messed up combination of frame 0 and 1) |
| 692 |
let frame = &s.frames[1]; |
| 693 |
let valid = &frame.context.valid; |
| 694 |
assert_eq!(frame.trust, FrameTrust::FramePointer); |
| 695 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 696 |
assert_eq!(which.len(), 3); |
| 697 |
} else { |
| 698 |
unreachable!(); |
| 699 |
} |
| 700 |
|
| 701 |
if let MinidumpRawContext::Arm(ctx) = &frame.context.raw { |
| 702 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address1); |
| 703 |
assert_eq!( |
| 704 |
ctx.get_register("sp", valid).unwrap(), |
| 705 |
frame1_sp.value().unwrap() as u32 |
| 706 |
); |
| 707 |
assert_eq!( |
| 708 |
ctx.get_register("fp", valid).unwrap(), |
| 709 |
frame0_fp.value().unwrap() as u32 |
| 710 |
); |
| 711 |
} else { |
| 712 |
unreachable!(); |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
// Never get to frame 2, alas! |
| 717 |
} |
| 718 |
|
| 719 |
const CALLEE_SAVE_REGS: &[&str] = &["pc", "sp", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "fp"]; |
| 720 |
|
| 721 |
fn init_cfi_state() -> (TestFixture, Section, CONTEXT_ARM, MinidumpContextValidity) { |
| 722 |
let mut f = TestFixture::new(); |
| 723 |
let symbols = [ |
| 724 |
// The youngest frame's function. |
| 725 |
"FUNC 4000 1000 10 enchiridion\n", |
| 726 |
// Initially, nothing has been pushed on the stack, |
| 727 |
// and the return address is still in the link register. |
| 728 |
"STACK CFI INIT 4000 100 .cfa: sp .ra: lr\n", |
| 729 |
// Push r4, the frame pointer, and the link register. |
| 730 |
"STACK CFI 4001 .cfa: sp 12 + r4: .cfa 12 - ^", |
| 731 |
" r11: .cfa 8 - ^ .ra: .cfa 4 - ^\n", |
| 732 |
// Save r4..r7 in r0..r3: verify that we populate |
| 733 |
// the youngest frame with all the values we have. |
| 734 |
"STACK CFI 4002 r4: r0 r5: r1 r6: r2 r7: r3\n", |
| 735 |
// Restore r4..r7. Save the non-callee-saves register r1. |
| 736 |
"STACK CFI 4003 .cfa: sp 16 + r1: .cfa 16 - ^", |
| 737 |
" r4: r4 r5: r5 r6: r6 r7: r7\n", |
| 738 |
// Move the .cfa back four bytes, to point at the return |
| 739 |
// address, and restore the sp explicitly. |
| 740 |
"STACK CFI 4005 .cfa: sp 12 + r1: .cfa 12 - ^", |
| 741 |
" r11: .cfa 4 - ^ .ra: .cfa ^ sp: .cfa 4 +\n", |
| 742 |
// Recover the PC explicitly from a new stack slot; |
| 743 |
// provide garbage for the .ra. |
| 744 |
"STACK CFI 4006 .cfa: sp 16 + pc: .cfa 16 - ^\n", |
| 745 |
// The calling function. |
| 746 |
"FUNC 5000 1000 10 epictetus\n", |
| 747 |
// Mark it as end of stack. |
| 748 |
"STACK CFI INIT 5000 1000 .cfa: 0 .ra: 0\n", |
| 749 |
// A function whose CFI makes the stack pointer |
| 750 |
// go backwards. |
| 751 |
"FUNC 6000 1000 20 palinal\n", |
| 752 |
"STACK CFI INIT 6000 1000 .cfa: sp 4 - .ra: lr\n", |
| 753 |
// A function with CFI expressions that can't be |
| 754 |
// evaluated. |
| 755 |
"FUNC 7000 1000 20 rhetorical\n", |
| 756 |
"STACK CFI INIT 7000 1000 .cfa: moot .ra: ambiguous\n", |
| 757 |
]; |
| 758 |
f.add_symbols(String::from("module1"), symbols.concat()); |
| 759 |
|
| 760 |
f.raw.set_register("pc", 0x40005510); |
| 761 |
f.raw.set_register("sp", 0x80000000); |
| 762 |
f.raw.set_register("fp", 0x8112e110); |
| 763 |
f.raw.iregs[4] = 0xb5d55e68; |
| 764 |
f.raw.iregs[5] = 0xebd134f3; |
| 765 |
f.raw.iregs[6] = 0xa31e74bc; |
| 766 |
f.raw.iregs[7] = 0x2dcb16b3; |
| 767 |
f.raw.iregs[8] = 0x2ada2137; |
| 768 |
f.raw.iregs[9] = 0xbbbb557d; |
| 769 |
f.raw.iregs[10] = 0x48bf8ca7; |
| 770 |
|
| 771 |
let raw_valid = MinidumpContextValidity::All; |
| 772 |
|
| 773 |
let expected = f.raw.clone(); |
| 774 |
let expected_regs = CALLEE_SAVE_REGS; |
| 775 |
let expected_valid = MinidumpContextValidity::Some(expected_regs.iter().copied().collect()); |
| 776 |
|
| 777 |
let stack = Section::new(); |
| 778 |
stack |
| 779 |
.start() |
| 780 |
.set_const(f.raw.get_register("sp", &raw_valid).unwrap() as u64); |
| 781 |
|
| 782 |
(f, stack, expected, expected_valid) |
| 783 |
} |
| 784 |
|
| 785 |
async fn check_cfi( |
| 786 |
f: TestFixture, |
| 787 |
stack: Section, |
| 788 |
expected: CONTEXT_ARM, |
| 789 |
expected_valid: MinidumpContextValidity, |
| 790 |
) { |
| 791 |
let s = f.walk_stack(stack).await; |
| 792 |
assert_eq!(s.frames.len(), 2); |
| 793 |
|
| 794 |
{ |
| 795 |
// Frame 0 |
| 796 |
let frame = &s.frames[0]; |
| 797 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 798 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 799 |
} |
| 800 |
|
| 801 |
{ |
| 802 |
// Frame 1 |
| 803 |
if let MinidumpContextValidity::Some(ref expected_regs) = expected_valid { |
| 804 |
let frame = &s.frames[1]; |
| 805 |
let valid = &frame.context.valid; |
| 806 |
assert_eq!(frame.trust, FrameTrust::CallFrameInfo); |
| 807 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 808 |
assert_eq!(which.len(), expected_regs.len()); |
| 809 |
} else { |
| 810 |
unreachable!(); |
| 811 |
} |
| 812 |
|
| 813 |
if let MinidumpRawContext::Arm(ctx) = &frame.context.raw { |
| 814 |
for reg in expected_regs { |
| 815 |
assert_eq!( |
| 816 |
ctx.get_register(reg, valid), |
| 817 |
expected.get_register(reg, &expected_valid), |
| 818 |
"{reg} registers didn't match!" |
| 819 |
); |
| 820 |
} |
| 821 |
return; |
| 822 |
} |
| 823 |
} |
| 824 |
} |
| 825 |
unreachable!(); |
| 826 |
} |
| 827 |
|
| 828 |
#[tokio::test] |
| 829 |
async fn test_cfi_at_4000() { |
| 830 |
let (mut f, mut stack, expected, expected_valid) = init_cfi_state(); |
| 831 |
|
| 832 |
stack = stack.append_repeated(0, 120); |
| 833 |
|
| 834 |
f.raw.set_register("pc", 0x40004000); |
| 835 |
f.raw.set_register("lr", 0x40005510); |
| 836 |
|
| 837 |
check_cfi(f, stack, expected, expected_valid).await; |
| 838 |
} |
| 839 |
|
| 840 |
#[tokio::test] |
| 841 |
async fn test_cfi_at_4001() { |
| 842 |
let (mut f, mut stack, mut expected, expected_valid) = init_cfi_state(); |
| 843 |
|
| 844 |
let frame1_sp = Label::new(); |
| 845 |
stack = stack |
| 846 |
.D32(0xb5d55e68) // saved r4 |
| 847 |
.D32(0x8112e110) // saved fp |
| 848 |
.D32(0x40005510) // return address |
| 849 |
.mark(&frame1_sp) |
| 850 |
.append_repeated(0, 120); |
| 851 |
|
| 852 |
expected.set_register("sp", frame1_sp.value().unwrap() as u32); |
| 853 |
f.raw.set_register("pc", 0x40004001); |
| 854 |
f.raw.iregs[4] = 0x635adc9f; |
| 855 |
f.raw.set_register("fp", 0xbe145fc4); |
| 856 |
|
| 857 |
check_cfi(f, stack, expected, expected_valid).await; |
| 858 |
} |
| 859 |
|
| 860 |
#[tokio::test] |
| 861 |
async fn test_cfi_at_4002() { |
| 862 |
let (mut f, mut stack, mut expected, expected_valid) = init_cfi_state(); |
| 863 |
|
| 864 |
let frame1_sp = Label::new(); |
| 865 |
stack = stack |
| 866 |
.D32(0xfb81ff3d) // no longer saved r4 |
| 867 |
.D32(0x8112e110) // saved fp |
| 868 |
.D32(0x40005510) // return address |
| 869 |
.mark(&frame1_sp) |
| 870 |
.append_repeated(0, 120); |
| 871 |
|
| 872 |
expected.set_register("sp", frame1_sp.value().unwrap() as u32); |
| 873 |
f.raw.set_register("pc", 0x40004002); |
| 874 |
f.raw.iregs[0] = 0xb5d55e68; // saved r4 |
| 875 |
f.raw.iregs[1] = 0xebd134f3; // saved r5 |
| 876 |
f.raw.iregs[2] = 0xa31e74bc; // saved r6 |
| 877 |
f.raw.iregs[3] = 0x2dcb16b3; // saved r7 |
| 878 |
f.raw.iregs[4] = 0xfdd35466; // distinct callee r4 |
| 879 |
f.raw.iregs[5] = 0xf18c946c; // distinct callee r5 |
| 880 |
f.raw.iregs[6] = 0xac2079e8; // distinct callee r6 |
| 881 |
f.raw.iregs[7] = 0xa449829f; // distinct callee r7 |
| 882 |
f.raw.set_register("fp", 0xbe145fc4); |
| 883 |
|
| 884 |
check_cfi(f, stack, expected, expected_valid).await; |
| 885 |
} |
| 886 |
|
| 887 |
#[tokio::test] |
| 888 |
async fn test_cfi_at_4003() { |
| 889 |
let (mut f, mut stack, mut expected, mut expected_valid) = init_cfi_state(); |
| 890 |
|
| 891 |
let frame1_sp = Label::new(); |
| 892 |
stack = stack |
| 893 |
.D32(0x48c8dd5a) // saved r1 (even though it's not callee-saves) |
| 894 |
.D32(0xcb78040e) // no longer saved r4 |
| 895 |
.D32(0x8112e110) // saved fp |
| 896 |
.D32(0x40005510) // return address |
| 897 |
.mark(&frame1_sp) |
| 898 |
.append_repeated(0, 120); |
| 899 |
|
| 900 |
expected.set_register("sp", frame1_sp.value().unwrap() as u32); |
| 901 |
expected.iregs[1] = 0x48c8dd5a; |
| 902 |
if let MinidumpContextValidity::Some(ref mut which) = expected_valid { |
| 903 |
which.insert("r1"); |
| 904 |
} else { |
| 905 |
unreachable!(); |
| 906 |
} |
| 907 |
|
| 908 |
f.raw.set_register("pc", 0x40004003); |
| 909 |
f.raw.iregs[1] = 0xfb756319; |
| 910 |
|
| 911 |
check_cfi(f, stack, expected, expected_valid).await; |
| 912 |
} |
| 913 |
|
| 914 |
#[tokio::test] |
| 915 |
async fn test_cfi_at_4004() { |
| 916 |
// Should be the same as 4003 |
| 917 |
let (mut f, mut stack, mut expected, mut expected_valid) = init_cfi_state(); |
| 918 |
|
| 919 |
let frame1_sp = Label::new(); |
| 920 |
stack = stack |
| 921 |
.D32(0x48c8dd5a) // saved r1 (even though it's not callee-saves) |
| 922 |
.D32(0xcb78040e) // no longer saved r4 |
| 923 |
.D32(0x8112e110) // saved fp |
| 924 |
.D32(0x40005510) // return address |
| 925 |
.mark(&frame1_sp) |
| 926 |
.append_repeated(0, 120); |
| 927 |
|
| 928 |
expected.set_register("sp", frame1_sp.value().unwrap() as u32); |
| 929 |
expected.iregs[1] = 0x48c8dd5a; |
| 930 |
if let MinidumpContextValidity::Some(ref mut which) = expected_valid { |
| 931 |
which.insert("r1"); |
| 932 |
} else { |
| 933 |
unreachable!(); |
| 934 |
} |
| 935 |
|
| 936 |
f.raw.set_register("pc", 0x40004004); |
| 937 |
f.raw.iregs[1] = 0xfb756319; |
| 938 |
|
| 939 |
check_cfi(f, stack, expected, expected_valid).await; |
| 940 |
} |
| 941 |
|
| 942 |
#[tokio::test] |
| 943 |
async fn test_cfi_at_4005() { |
| 944 |
let (mut f, mut stack, mut expected, mut expected_valid) = init_cfi_state(); |
| 945 |
|
| 946 |
let frame1_sp = Label::new(); |
| 947 |
stack = stack |
| 948 |
.D32(0x48c8dd5a) // saved r1 (even though it's not callee-saves) |
| 949 |
.D32(0xf013f841) // no longer saved r4 |
| 950 |
.D32(0x8112e110) // saved fp |
| 951 |
.D32(0x40005510) // return address |
| 952 |
.mark(&frame1_sp) |
| 953 |
.append_repeated(0, 120); |
| 954 |
|
| 955 |
expected.set_register("sp", frame1_sp.value().unwrap() as u32); |
| 956 |
expected.iregs[1] = 0x48c8dd5a; |
| 957 |
if let MinidumpContextValidity::Some(ref mut which) = expected_valid { |
| 958 |
which.insert("r1"); |
| 959 |
} else { |
| 960 |
unreachable!(); |
| 961 |
} |
| 962 |
|
| 963 |
f.raw.set_register("pc", 0x40004005); |
| 964 |
f.raw.iregs[1] = 0xfb756319; |
| 965 |
|
| 966 |
check_cfi(f, stack, expected, expected_valid).await; |
| 967 |
} |
| 968 |
|
| 969 |
#[tokio::test] |
| 970 |
async fn test_cfi_at_4006() { |
| 971 |
// Here we provide an explicit rule for the PC, and have the saved .ra be |
| 972 |
// bogus. |
| 973 |
|
| 974 |
let (mut f, mut stack, mut expected, mut expected_valid) = init_cfi_state(); |
| 975 |
|
| 976 |
let frame1_sp = Label::new(); |
| 977 |
stack = stack |
| 978 |
.D32(0x40005510) // saved pc |
| 979 |
.D32(0x48c8dd5a) // saved r1 (even though it's not callee-saves) |
| 980 |
.D32(0xf013f841) // no longer saved r4 |
| 981 |
.D32(0x8112e110) // saved fp |
| 982 |
.D32(0xf8d15783) // .ra rule recovers this, which is garbage |
| 983 |
.mark(&frame1_sp) |
| 984 |
.append_repeated(0, 120); |
| 985 |
|
| 986 |
expected.set_register("sp", frame1_sp.value().unwrap() as u32); |
| 987 |
expected.iregs[1] = 0x48c8dd5a; |
| 988 |
if let MinidumpContextValidity::Some(ref mut which) = expected_valid { |
| 989 |
which.insert("r1"); |
| 990 |
} else { |
| 991 |
unreachable!(); |
| 992 |
} |
| 993 |
|
| 994 |
f.raw.set_register("pc", 0x40004006); |
| 995 |
f.raw.iregs[1] = 0xfb756319; |
| 996 |
|
| 997 |
check_cfi(f, stack, expected, expected_valid).await; |
| 998 |
} |
| 999 |
|
| 1000 |
#[tokio::test] |
| 1001 |
async fn test_cfi_reject_backwards() { |
| 1002 |
// Check that we reject rules that would cause the stack pointer to |
| 1003 |
// move in the wrong direction. |
| 1004 |
|
| 1005 |
let (mut f, mut stack, _expected, _expected_valid) = init_cfi_state(); |
| 1006 |
|
| 1007 |
stack = stack.append_repeated(0, 120); |
| 1008 |
|
| 1009 |
f.raw.set_register("pc", 0x40006000); |
| 1010 |
f.raw.set_register("sp", 0x80000000); |
| 1011 |
f.raw.set_register("lr", 0x40005510); |
| 1012 |
|
| 1013 |
let s = f.walk_stack(stack).await; |
| 1014 |
assert_eq!(s.frames.len(), 1); |
| 1015 |
} |
| 1016 |
|
| 1017 |
#[tokio::test] |
| 1018 |
async fn test_cfi_reject_bad_exprs() { |
| 1019 |
// Check that we reject rules whose expressions' evaluation fails. |
| 1020 |
|
| 1021 |
let (mut f, mut stack, _expected, _expected_valid) = init_cfi_state(); |
| 1022 |
|
| 1023 |
stack = stack.append_repeated(0, 120); |
| 1024 |
|
| 1025 |
f.raw.set_register("pc", 0x40007000); |
| 1026 |
f.raw.set_register("sp", 0x80000000); |
| 1027 |
|
| 1028 |
let s = f.walk_stack(stack).await; |
| 1029 |
assert_eq!(s.frames.len(), 1); |
| 1030 |
} |
| 1031 |
|
| 1032 |
#[tokio::test] |
| 1033 |
async fn test_frame_pointer_overflow() { |
| 1034 |
// Make sure we don't explode when trying frame pointer analysis on a value |
| 1035 |
// that will overflow. |
| 1036 |
|
| 1037 |
type Pointer = u32; |
| 1038 |
let stack_max: Pointer = Pointer::MAX; |
| 1039 |
let stack_size: Pointer = 1000; |
| 1040 |
let bad_frame_ptr: Pointer = stack_max; |
| 1041 |
|
| 1042 |
let mut f = TestFixture::new(); |
| 1043 |
let mut stack = Section::new(); |
| 1044 |
let stack_start: Pointer = stack_max - stack_size; |
| 1045 |
stack.start().set_const(stack_start as u64); |
| 1046 |
|
| 1047 |
stack = stack |
| 1048 |
// frame 0 |
| 1049 |
.append_repeated(0, stack_size as usize); // junk, not important to the test |
| 1050 |
|
| 1051 |
f.raw.set_register("pc", 0x7a100000); |
| 1052 |
f.raw.set_register("fp", bad_frame_ptr); |
| 1053 |
f.raw |
| 1054 |
.set_register("sp", stack.start().value().unwrap() as Pointer); |
| 1055 |
f.raw.set_register("lr", 0x7b302000); |
| 1056 |
|
| 1057 |
let s = f.walk_stack(stack).await; |
| 1058 |
assert_eq!(s.frames.len(), 1); |
| 1059 |
|
| 1060 |
// As long as we don't panic, we're good! |
| 1061 |
} |
| 1062 |
|
| 1063 |
#[tokio::test] |
| 1064 |
async fn test_frame_pointer_overflow_nonsense_32bit_stack() { |
| 1065 |
// same as test_frame_pointer_overflow, but we're going to abuse the fact |
| 1066 |
// that rust-minidump prefers representing things in 64-bit to create |
| 1067 |
// impossible stack addresses that overflow 32-bit integers but appear |
| 1068 |
// valid in 64-bit. By doing this memory reads will "succeed" but |
| 1069 |
// pointer math done in the native pointer width will overflow and |
| 1070 |
// everything will be sad. |
| 1071 |
|
| 1072 |
type Pointer = u32; |
| 1073 |
let pointer_size: u64 = std::mem::size_of::<Pointer>() as u64; |
| 1074 |
let stack_max: u64 = Pointer::MAX as u64 + pointer_size * 2; |
| 1075 |
let stack_size: u64 = 1000; |
| 1076 |
let bad_frame_ptr: u64 = Pointer::MAX as u64 - pointer_size; |
| 1077 |
|
| 1078 |
let mut f = TestFixture::new(); |
| 1079 |
let mut stack = Section::new(); |
| 1080 |
let stack_start: u64 = stack_max - stack_size; |
| 1081 |
stack.start().set_const(stack_start); |
| 1082 |
|
| 1083 |
stack = stack |
| 1084 |
// frame 0 |
| 1085 |
.append_repeated(0, 1000); // junk, not important to the test |
| 1086 |
|
| 1087 |
f.raw.set_register("pc", 0x7a100000); |
| 1088 |
f.raw.set_register("fp", bad_frame_ptr as u32); |
| 1089 |
f.raw |
| 1090 |
.set_register("sp", stack.start().value().unwrap() as Pointer); |
| 1091 |
f.raw.set_register("lr", 0x7b302000); |
| 1092 |
|
| 1093 |
let s = f.walk_stack(stack).await; |
| 1094 |
assert_eq!(s.frames.len(), 1); |
| 1095 |
|
| 1096 |
// As long as we don't panic, we're good! |
| 1097 |
} |
| 1098 |
|
| 1099 |
#[tokio::test] |
| 1100 |
async fn test_frame_pointer_barely_no_overflow() { |
| 1101 |
// This is a simple frame pointer test but with the all the values pushed |
| 1102 |
// as close to the upper memory boundary as possible, to confirm that |
| 1103 |
// our code doesn't randomly overflow *AND* isn't overzealous in |
| 1104 |
// its overflow guards. |
| 1105 |
|
| 1106 |
let mut f = TestFixture::new(); |
| 1107 |
let mut stack = Section::new(); |
| 1108 |
|
| 1109 |
type Pointer = u32; |
| 1110 |
let stack_max: Pointer = Pointer::MAX; |
| 1111 |
let pointer_size: Pointer = std::mem::size_of::<Pointer>() as Pointer; |
| 1112 |
let stack_size: Pointer = pointer_size * 3; |
| 1113 |
|
| 1114 |
let stack_start: Pointer = stack_max - stack_size; |
| 1115 |
let return_address: Pointer = 0x7b302000; |
| 1116 |
stack.start().set_const(stack_start as u64); |
| 1117 |
|
| 1118 |
let frame0_fp = Label::new(); |
| 1119 |
let frame1_sp = Label::new(); |
| 1120 |
let frame1_fp = Label::new(); |
| 1121 |
|
| 1122 |
stack = stack |
| 1123 |
// frame 0 |
| 1124 |
.mark(&frame0_fp) |
| 1125 |
.D32(&frame1_fp) // caller-pushed %rbp |
| 1126 |
.D32(return_address) // actual return address |
| 1127 |
// frame 1 |
| 1128 |
.mark(&frame1_sp) |
| 1129 |
.mark(&frame1_fp) // end of stack |
| 1130 |
.D32(0); |
| 1131 |
|
| 1132 |
f.raw.set_register("pc", 0x7a100000); |
| 1133 |
f.raw |
| 1134 |
.set_register("fp", frame0_fp.value().unwrap() as Pointer); |
| 1135 |
f.raw |
| 1136 |
.set_register("sp", stack.start().value().unwrap() as Pointer); |
| 1137 |
f.raw.set_register("lr", return_address); |
| 1138 |
|
| 1139 |
let s = f.walk_stack(stack).await; |
| 1140 |
assert_eq!(s.frames.len(), 2); |
| 1141 |
|
| 1142 |
{ |
| 1143 |
// Frame 0 |
| 1144 |
let frame = &s.frames[0]; |
| 1145 |
let valid = &frame.context.valid; |
| 1146 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 1147 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 1148 |
|
| 1149 |
if let MinidumpRawContext::Arm(ctx) = &frame.context.raw { |
| 1150 |
assert_eq!( |
| 1151 |
ctx.get_register("fp", valid).unwrap(), |
| 1152 |
frame0_fp.value().unwrap() as Pointer |
| 1153 |
); |
| 1154 |
} else { |
| 1155 |
unreachable!(); |
| 1156 |
} |
| 1157 |
} |
| 1158 |
|
| 1159 |
{ |
| 1160 |
// Frame 1 |
| 1161 |
let frame = &s.frames[1]; |
| 1162 |
let valid = &frame.context.valid; |
| 1163 |
assert_eq!(frame.trust, FrameTrust::FramePointer); |
| 1164 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 1165 |
assert_eq!(which.len(), 3); |
| 1166 |
} else { |
| 1167 |
unreachable!(); |
| 1168 |
} |
| 1169 |
|
| 1170 |
if let MinidumpRawContext::Arm(ctx) = &frame.context.raw { |
| 1171 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address); |
| 1172 |
assert_eq!( |
| 1173 |
ctx.get_register("sp", valid).unwrap(), |
| 1174 |
frame1_sp.value().unwrap() as Pointer |
| 1175 |
); |
| 1176 |
assert_eq!( |
| 1177 |
ctx.get_register("fp", valid).unwrap(), |
| 1178 |
frame1_fp.value().unwrap() as Pointer |
| 1179 |
); |
| 1180 |
} else { |
| 1181 |
unreachable!(); |
| 1182 |
} |
| 1183 |
} |
| 1184 |
} |