| 1 |
// Copyright 2015 Ted Mielczarek. See the COPYRIGHT |
| 2 |
// file at the top-level directory of this distribution. |
| 3 |
|
| 4 |
// NOTE: we don't bother testing arm64_old, it should have identical code at |
| 5 |
// all times! |
| 6 |
|
| 7 |
use crate::*; |
| 8 |
use minidump::system_info::{Cpu, Os}; |
| 9 |
use std::collections::HashMap; |
| 10 |
use test_assembler::*; |
| 11 |
|
| 12 |
type Context = minidump::format::CONTEXT_ARM64; |
| 13 |
|
| 14 |
struct TestFixture { |
| 15 |
pub raw: Context, |
| 16 |
pub modules: MinidumpModuleList, |
| 17 |
pub symbols: HashMap<String, String>, |
| 18 |
} |
| 19 |
|
| 20 |
impl TestFixture { |
| 21 |
pub fn new() -> TestFixture { |
| 22 |
TestFixture { |
| 23 |
raw: Context::default(), |
| 24 |
// Give the two modules reasonable standard locations and names |
| 25 |
// for tests to play with. |
| 26 |
modules: MinidumpModuleList::from_modules(vec![ |
| 27 |
MinidumpModule::new(0x40000000, 0x10000, "module1"), |
| 28 |
MinidumpModule::new(0x50000000, 0x10000, "module2"), |
| 29 |
]), |
| 30 |
symbols: HashMap::new(), |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
pub fn high_module() -> TestFixture { |
| 35 |
TestFixture { |
| 36 |
raw: Context::default(), |
| 37 |
// Same as new but with a really high module to stretch ptr auth stripping |
| 38 |
modules: MinidumpModuleList::from_modules(vec![ |
| 39 |
MinidumpModule::new(0x40000000, 0x10000, "module1"), |
| 40 |
MinidumpModule::new(0x50000000, 0x10000, "module2"), |
| 41 |
MinidumpModule::new(0x10000000000000, 0x10000, "high-module"), |
| 42 |
]), |
| 43 |
symbols: HashMap::new(), |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
pub fn highest_module() -> TestFixture { |
| 48 |
TestFixture { |
| 49 |
raw: Context::default(), |
| 50 |
// Same as new but with a module so high it sets the maximum address bit |
| 51 |
// effectively disabling stripping |
| 52 |
modules: MinidumpModuleList::from_modules(vec![ |
| 53 |
MinidumpModule::new(0x40000000, 0x10000, "module1"), |
| 54 |
MinidumpModule::new(0x50000000, 0x10000, "module2"), |
| 55 |
MinidumpModule::new(0xa000_0000_0000_0000, 0x10000, "highest-module"), |
| 56 |
]), |
| 57 |
symbols: HashMap::new(), |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
pub async fn walk_stack(&self, stack: Section) -> CallStack { |
| 62 |
let context = MinidumpContext { |
| 63 |
raw: MinidumpRawContext::Arm64(self.raw.clone()), |
| 64 |
valid: MinidumpContextValidity::All, |
| 65 |
}; |
| 66 |
let base = stack.start().value().unwrap(); |
| 67 |
let size = stack.size(); |
| 68 |
let stack = stack.get_contents().unwrap(); |
| 69 |
let stack_memory = MinidumpMemory { |
| 70 |
desc: Default::default(), |
| 71 |
base_address: base, |
| 72 |
size, |
| 73 |
bytes: &stack, |
| 74 |
endian: scroll::LE, |
| 75 |
}; |
| 76 |
let system_info = SystemInfo { |
| 77 |
os: Os::Windows, |
| 78 |
os_version: None, |
| 79 |
os_build: None, |
| 80 |
cpu: Cpu::Arm64, |
| 81 |
cpu_info: None, |
| 82 |
cpu_microcode_version: None, |
| 83 |
cpu_count: 1, |
| 84 |
}; |
| 85 |
let symbolizer = Symbolizer::new(string_symbol_supplier(self.symbols.clone())); |
| 86 |
let mut stack = CallStack::with_context(context); |
| 87 |
|
| 88 |
walk_stack( |
| 89 |
0, |
| 90 |
(), |
| 91 |
&mut stack, |
| 92 |
Some(UnifiedMemory::Memory(&stack_memory)), |
| 93 |
&self.modules, |
| 94 |
&system_info, |
| 95 |
&symbolizer, |
| 96 |
) |
| 97 |
.await; |
| 98 |
|
| 99 |
stack |
| 100 |
} |
| 101 |
|
| 102 |
pub fn add_symbols(&mut self, name: String, symbols: String) { |
| 103 |
self.symbols.insert(name, symbols); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
#[tokio::test] |
| 108 |
async fn test_simple() { |
| 109 |
let mut f = TestFixture::new(); |
| 110 |
let stack = Section::new(); |
| 111 |
stack.start().set_const(0x80000000); |
| 112 |
// There should be no references to the stack in this walk: we don't |
| 113 |
// provide any call frame information, so trying to reconstruct the |
| 114 |
// context frame's caller should fail. So there's no need for us to |
| 115 |
// provide stack contents. |
| 116 |
f.raw.set_register("pc", 0x4000c020); |
| 117 |
f.raw.set_register("fp", 0x80000000); |
| 118 |
|
| 119 |
let s = f.walk_stack(stack).await; |
| 120 |
assert_eq!(s.frames.len(), 1); |
| 121 |
let f = &s.frames[0]; |
| 122 |
let m = f.module.as_ref().unwrap(); |
| 123 |
assert_eq!(m.code_file(), "module1"); |
| 124 |
} |
| 125 |
|
| 126 |
#[tokio::test] |
| 127 |
async fn test_scan_without_symbols() { |
| 128 |
// Scanning should work without any symbols |
| 129 |
let mut f = TestFixture::new(); |
| 130 |
let mut stack = Section::new(); |
| 131 |
stack.start().set_const(0x80000000); |
| 132 |
|
| 133 |
let return_address1 = 0x50000100u64; |
| 134 |
let return_address2 = 0x50000900u64; |
| 135 |
let frame1_sp = Label::new(); |
| 136 |
let frame2_sp = Label::new(); |
| 137 |
|
| 138 |
stack = stack |
| 139 |
// frame 0 |
| 140 |
.append_repeated(0, 16) // space |
| 141 |
.D64(0x40090000) // junk that's not |
| 142 |
.D64(0x60000000) // a return address |
| 143 |
.D64(return_address1) // actual return address |
| 144 |
// frame 1 |
| 145 |
.mark(&frame1_sp) |
| 146 |
.append_repeated(0, 16) // space |
| 147 |
.D64(0xF0000000) // more junk |
| 148 |
.D64(0x0000000D) |
| 149 |
.D64(return_address2) // actual return address |
| 150 |
// frame 2 |
| 151 |
.mark(&frame2_sp) |
| 152 |
.append_repeated(0, 64); // end of stack |
| 153 |
|
| 154 |
f.raw.set_register("pc", 0x40005510); |
| 155 |
f.raw.set_register("sp", stack.start().value().unwrap()); |
| 156 |
|
| 157 |
let s = f.walk_stack(stack).await; |
| 158 |
assert_eq!(s.frames.len(), 3); |
| 159 |
|
| 160 |
{ |
| 161 |
// Frame 0 |
| 162 |
let frame = &s.frames[0]; |
| 163 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 164 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 165 |
} |
| 166 |
|
| 167 |
{ |
| 168 |
// Frame 1 |
| 169 |
let frame = &s.frames[1]; |
| 170 |
let valid = &frame.context.valid; |
| 171 |
assert_eq!(frame.trust, FrameTrust::Scan); |
| 172 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 173 |
assert_eq!(which.len(), 2); |
| 174 |
} else { |
| 175 |
unreachable!(); |
| 176 |
} |
| 177 |
|
| 178 |
if let MinidumpRawContext::Arm64(ctx) = &frame.context.raw { |
| 179 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address1); |
| 180 |
assert_eq!( |
| 181 |
ctx.get_register("sp", valid).unwrap(), |
| 182 |
frame1_sp.value().unwrap() |
| 183 |
); |
| 184 |
} else { |
| 185 |
unreachable!(); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
{ |
| 190 |
// Frame 2 |
| 191 |
let frame = &s.frames[2]; |
| 192 |
let valid = &frame.context.valid; |
| 193 |
assert_eq!(frame.trust, FrameTrust::Scan); |
| 194 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 195 |
assert_eq!(which.len(), 2); |
| 196 |
} else { |
| 197 |
unreachable!(); |
| 198 |
} |
| 199 |
|
| 200 |
if let MinidumpRawContext::Arm64(ctx) = &frame.context.raw { |
| 201 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address2); |
| 202 |
assert_eq!( |
| 203 |
ctx.get_register("sp", valid).unwrap(), |
| 204 |
frame2_sp.value().unwrap() |
| 205 |
); |
| 206 |
} else { |
| 207 |
unreachable!(); |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
#[tokio::test] |
| 213 |
async fn test_scan_with_symbols() { |
| 214 |
// Test that we can refine our scanning using symbols. Specifically we |
| 215 |
// should be able to reject pointers that are in modules but don't map to |
| 216 |
// any FUNC/PUBLIC record. |
| 217 |
let mut f = TestFixture::new(); |
| 218 |
let mut stack = Section::new(); |
| 219 |
let stack_start = 0x80000000; |
| 220 |
stack.start().set_const(stack_start); |
| 221 |
|
| 222 |
let return_address = 0x50000200; |
| 223 |
|
| 224 |
let frame1_sp = Label::new(); |
| 225 |
stack = stack |
| 226 |
// frame 0 |
| 227 |
.append_repeated(0, 16) // space |
| 228 |
.D64(0x40090000) // junk that's not |
| 229 |
.D64(0x60000000) // a return address |
| 230 |
.D64(0x40001000) // a couple of plausible addresses |
| 231 |
.D64(0x5000F000) // that are not within functions |
| 232 |
.D64(return_address) // actual return address |
| 233 |
// frame 1 |
| 234 |
.mark(&frame1_sp) |
| 235 |
.append_repeated(0, 64); // end of stack |
| 236 |
|
| 237 |
f.raw.set_register("pc", 0x40000200); |
| 238 |
f.raw.set_register("sp", stack.start().value().unwrap()); |
| 239 |
|
| 240 |
f.add_symbols( |
| 241 |
String::from("module1"), |
| 242 |
// The youngest frame's function. |
| 243 |
String::from("FUNC 100 400 10 monotreme\n"), |
| 244 |
); |
| 245 |
f.add_symbols( |
| 246 |
String::from("module2"), |
| 247 |
// The calling frame's function. |
| 248 |
String::from("FUNC 100 400 10 marsupial\n"), |
| 249 |
); |
| 250 |
|
| 251 |
let s = f.walk_stack(stack).await; |
| 252 |
assert_eq!(s.frames.len(), 2); |
| 253 |
|
| 254 |
{ |
| 255 |
// Frame 0 |
| 256 |
let frame = &s.frames[0]; |
| 257 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 258 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 259 |
} |
| 260 |
|
| 261 |
{ |
| 262 |
// Frame 1 |
| 263 |
let frame = &s.frames[1]; |
| 264 |
let valid = &frame.context.valid; |
| 265 |
assert_eq!(frame.trust, FrameTrust::Scan); |
| 266 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 267 |
assert_eq!(which.len(), 2); |
| 268 |
} else { |
| 269 |
unreachable!(); |
| 270 |
} |
| 271 |
|
| 272 |
if let MinidumpRawContext::Arm64(ctx) = &frame.context.raw { |
| 273 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address); |
| 274 |
assert_eq!( |
| 275 |
ctx.get_register("sp", valid).unwrap(), |
| 276 |
frame1_sp.value().unwrap() |
| 277 |
); |
| 278 |
} else { |
| 279 |
unreachable!(); |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
#[tokio::test] |
| 285 |
async fn test_scan_first_frame() { |
| 286 |
// The first (context) frame gets extra long scans, this test checks that. |
| 287 |
let mut f = TestFixture::new(); |
| 288 |
let mut stack = Section::new(); |
| 289 |
stack.start().set_const(0x80000000); |
| 290 |
|
| 291 |
let return_address1 = 0x50000100u64; |
| 292 |
let return_address2 = 0x50000900u64; |
| 293 |
let frame1_sp = Label::new(); |
| 294 |
let frame2_sp = Label::new(); |
| 295 |
|
| 296 |
stack = stack |
| 297 |
// frame 0 |
| 298 |
.append_repeated(0, 16) // space |
| 299 |
.D64(0x40090000) // junk that's not |
| 300 |
.D64(0x60000000) // a return address |
| 301 |
.append_repeated(0, 96) // more space |
| 302 |
.D64(return_address1) // actual return address |
| 303 |
// frame 1 |
| 304 |
.mark(&frame1_sp) |
| 305 |
.append_repeated(0, 32) // space |
| 306 |
.D64(0xF0000000) // more junk |
| 307 |
.D64(0x0000000D) |
| 308 |
.append_repeated(0, 336) // more space |
| 309 |
.D64(return_address2) // actual return address (won't be found) |
| 310 |
// frame 2 |
| 311 |
.mark(&frame2_sp) |
| 312 |
.append_repeated(0, 64); // end of stack |
| 313 |
|
| 314 |
f.raw.set_register("pc", 0x40005510); |
| 315 |
f.raw.set_register("sp", stack.start().value().unwrap()); |
| 316 |
|
| 317 |
let s = f.walk_stack(stack).await; |
| 318 |
assert_eq!(s.frames.len(), 2); |
| 319 |
|
| 320 |
{ |
| 321 |
// Frame 0 |
| 322 |
let frame = &s.frames[0]; |
| 323 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 324 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 325 |
} |
| 326 |
|
| 327 |
{ |
| 328 |
// Frame 1 |
| 329 |
let frame = &s.frames[1]; |
| 330 |
let valid = &frame.context.valid; |
| 331 |
assert_eq!(frame.trust, FrameTrust::Scan); |
| 332 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 333 |
assert_eq!(which.len(), 2); |
| 334 |
} else { |
| 335 |
unreachable!(); |
| 336 |
} |
| 337 |
|
| 338 |
if let MinidumpRawContext::Arm64(ctx) = &frame.context.raw { |
| 339 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address1); |
| 340 |
assert_eq!( |
| 341 |
ctx.get_register("sp", valid).unwrap(), |
| 342 |
frame1_sp.value().unwrap() |
| 343 |
); |
| 344 |
} else { |
| 345 |
unreachable!(); |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
#[tokio::test] |
| 351 |
async fn test_frame_pointer() { |
| 352 |
// Frame-pointer-based unwinding |
| 353 |
let mut f = TestFixture::new(); |
| 354 |
let mut stack = Section::new(); |
| 355 |
stack.start().set_const(0x80000000); |
| 356 |
|
| 357 |
let return_address1 = 0x50000100u64; |
| 358 |
let return_address2 = 0x50000900u64; |
| 359 |
let frame1_sp = Label::new(); |
| 360 |
let frame2_sp = Label::new(); |
| 361 |
let frame0_fp = Label::new(); |
| 362 |
let frame1_fp = Label::new(); |
| 363 |
let frame2_fp = Label::new(); |
| 364 |
|
| 365 |
stack = stack |
| 366 |
// frame 0 |
| 367 |
.append_repeated(0, 64) // space |
| 368 |
.D64(0x0000000D) // junk that's not |
| 369 |
.D64(0xF0000000) // a return address |
| 370 |
.mark(&frame0_fp) // next fp will point to the next value |
| 371 |
.D64(&frame1_fp) // save current frame pointer |
| 372 |
.D64(return_address1) // save current link register |
| 373 |
.mark(&frame1_sp) |
| 374 |
// frame 1 |
| 375 |
.append_repeated(0, 64) // space |
| 376 |
.D64(0x0000000D) // junk that's not |
| 377 |
.D64(0xF0000000) // a return address |
| 378 |
.mark(&frame1_fp) |
| 379 |
.D64(&frame2_fp) |
| 380 |
.D64(return_address2) |
| 381 |
.mark(&frame2_sp) |
| 382 |
// frame 2 |
| 383 |
.append_repeated(0, 64) // Whatever values on the stack. |
| 384 |
.D64(0x0000000D) // junk that's not |
| 385 |
.D64(0xF0000000) // a return address. |
| 386 |
.mark(&frame2_fp) // next fp will point to the next value |
| 387 |
.D64(0) |
| 388 |
.D64(0); |
| 389 |
|
| 390 |
f.raw.set_register("pc", 0x40005510); |
| 391 |
f.raw.set_register("lr", 0x1fe0fe10); |
| 392 |
f.raw.set_register("fp", frame0_fp.value().unwrap()); |
| 393 |
f.raw.set_register("sp", stack.start().value().unwrap()); |
| 394 |
|
| 395 |
let s = f.walk_stack(stack).await; |
| 396 |
assert_eq!(s.frames.len(), 3); |
| 397 |
|
| 398 |
{ |
| 399 |
// Frame 0 |
| 400 |
let frame = &s.frames[0]; |
| 401 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 402 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 403 |
} |
| 404 |
|
| 405 |
{ |
| 406 |
// Frame 1 |
| 407 |
let frame = &s.frames[1]; |
| 408 |
let valid = &frame.context.valid; |
| 409 |
assert_eq!(frame.trust, FrameTrust::FramePointer); |
| 410 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 411 |
assert_eq!(which.len(), 3); |
| 412 |
} else { |
| 413 |
unreachable!(); |
| 414 |
} |
| 415 |
|
| 416 |
if let MinidumpRawContext::Arm64(ctx) = &frame.context.raw { |
| 417 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address1); |
| 418 |
assert_eq!( |
| 419 |
ctx.get_register("sp", valid).unwrap(), |
| 420 |
frame1_sp.value().unwrap() |
| 421 |
); |
| 422 |
assert_eq!( |
| 423 |
ctx.get_register("fp", valid).unwrap(), |
| 424 |
frame1_fp.value().unwrap() |
| 425 |
); |
| 426 |
} else { |
| 427 |
unreachable!(); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
{ |
| 432 |
// Frame 2 |
| 433 |
let frame = &s.frames[2]; |
| 434 |
let valid = &frame.context.valid; |
| 435 |
assert_eq!(frame.trust, FrameTrust::FramePointer); |
| 436 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 437 |
assert_eq!(which.len(), 3); |
| 438 |
} else { |
| 439 |
unreachable!(); |
| 440 |
} |
| 441 |
|
| 442 |
if let MinidumpRawContext::Arm64(ctx) = &frame.context.raw { |
| 443 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address2); |
| 444 |
assert_eq!( |
| 445 |
ctx.get_register("sp", valid).unwrap(), |
| 446 |
frame2_sp.value().unwrap() |
| 447 |
); |
| 448 |
assert_eq!( |
| 449 |
ctx.get_register("fp", valid).unwrap(), |
| 450 |
frame2_fp.value().unwrap() |
| 451 |
); |
| 452 |
} else { |
| 453 |
unreachable!(); |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
#[tokio::test] |
| 459 |
async fn test_frame_pointer_stackless_leaf() { |
| 460 |
// Same as test_frame_pointer but frame0 is a stackless leaf. |
| 461 |
// |
| 462 |
// In the current implementation we will misunderstand this slightly |
| 463 |
// and basically "lose" frame 1, but still properly recover frame 2. |
| 464 |
// THIS TEST BREAKING MIGHT MEAN YOU'VE MADE THINGS WORK BETTER! |
| 465 |
let mut f = TestFixture::new(); |
| 466 |
let mut stack = Section::new(); |
| 467 |
stack.start().set_const(0x80000000); |
| 468 |
|
| 469 |
let return_address1 = 0x50000100u64; |
| 470 |
let return_address2 = 0x50000900u64; |
| 471 |
let frame1_sp = Label::new(); |
| 472 |
let frame2_sp = Label::new(); |
| 473 |
let frame1_fp = Label::new(); |
| 474 |
let frame2_fp = Label::new(); |
| 475 |
|
| 476 |
stack = stack |
| 477 |
// frame 0 (all junk!) |
| 478 |
.append_repeated(0, 64) // space |
| 479 |
.D64(0x0000000D) // junk that's not |
| 480 |
.D64(0xF0000000) // a return address |
| 481 |
.mark(&frame1_sp) |
| 482 |
// frame 1 (this is sadly dropped) |
| 483 |
.append_repeated(0, 64) // space |
| 484 |
.D64(0x0000000D) // junk that's not |
| 485 |
.D64(0xF0000000) // a return address |
| 486 |
.mark(&frame1_fp) |
| 487 |
.D64(&frame2_fp) |
| 488 |
.D64(return_address2) |
| 489 |
.mark(&frame2_sp) |
| 490 |
// frame 2 |
| 491 |
.append_repeated(0, 64) // Whatever values on the stack. |
| 492 |
.D64(0x0000000D) // junk that's not |
| 493 |
.D64(0xF0000000) // a return address. |
| 494 |
.mark(&frame2_fp) // next fp will point to the next value |
| 495 |
.D64(0) |
| 496 |
.D64(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()); |
| 501 |
f.raw.set_register("sp", stack.start().value().unwrap()); |
| 502 |
|
| 503 |
let s = f.walk_stack(stack).await; |
| 504 |
assert_eq!(s.frames.len(), 2); |
| 505 |
|
| 506 |
{ |
| 507 |
// Frame 0 |
| 508 |
let frame = &s.frames[0]; |
| 509 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 510 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 511 |
} |
| 512 |
|
| 513 |
{ |
| 514 |
// Frame 2 (found as Frame 1) |
| 515 |
let frame = &s.frames[1]; |
| 516 |
let valid = &frame.context.valid; |
| 517 |
assert_eq!(frame.trust, FrameTrust::FramePointer); |
| 518 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 519 |
assert_eq!(which.len(), 3); |
| 520 |
} else { |
| 521 |
unreachable!(); |
| 522 |
} |
| 523 |
|
| 524 |
if let MinidumpRawContext::Arm64(ctx) = &frame.context.raw { |
| 525 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address2); |
| 526 |
assert_eq!( |
| 527 |
ctx.get_register("sp", valid).unwrap(), |
| 528 |
frame2_sp.value().unwrap() |
| 529 |
); |
| 530 |
assert_eq!( |
| 531 |
ctx.get_register("fp", valid).unwrap(), |
| 532 |
frame2_fp.value().unwrap() |
| 533 |
); |
| 534 |
} else { |
| 535 |
unreachable!(); |
| 536 |
} |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
#[tokio::test] |
| 541 |
async fn test_frame_pointer_stackful_leaf() { |
| 542 |
// Same as test_frame_pointer but frame0 is a stackful leaf. |
| 543 |
// |
| 544 |
// In the current implementation we will misunderstand this slightly |
| 545 |
// and basically "lose" frame 1, but still properly recover frame 2. |
| 546 |
// THIS TEST BREAKING MIGHT MEAN YOU'VE MADE THINGS WORK BETTER! |
| 547 |
let mut f = TestFixture::new(); |
| 548 |
let mut stack = Section::new(); |
| 549 |
stack.start().set_const(0x80000000); |
| 550 |
|
| 551 |
let return_address1 = 0x50000100u64; |
| 552 |
let return_address2 = 0x50000900u64; |
| 553 |
let frame1_sp = Label::new(); |
| 554 |
let frame2_sp = Label::new(); |
| 555 |
let frame1_fp = Label::new(); |
| 556 |
let frame2_fp = Label::new(); |
| 557 |
|
| 558 |
stack = stack |
| 559 |
// frame 0 (literally nothing!) |
| 560 |
.mark(&frame1_sp) |
| 561 |
// frame 1 (this is sadly dropped) |
| 562 |
.append_repeated(0, 64) // space |
| 563 |
.D64(0x0000000D) // junk that's not |
| 564 |
.D64(0xF0000000) // a return address |
| 565 |
.mark(&frame1_fp) |
| 566 |
.D64(&frame2_fp) |
| 567 |
.D64(return_address2) |
| 568 |
.mark(&frame2_sp) |
| 569 |
// frame 2 |
| 570 |
.append_repeated(0, 64) // Whatever values on the stack. |
| 571 |
.D64(0x0000000D) // junk that's not |
| 572 |
.D64(0xF0000000) // a return address. |
| 573 |
.mark(&frame2_fp) // next fp will point to the next value |
| 574 |
.D64(0) |
| 575 |
.D64(0); |
| 576 |
|
| 577 |
f.raw.set_register("pc", 0x40005510); |
| 578 |
f.raw.set_register("lr", return_address1); // we will sadly ignore this |
| 579 |
f.raw.set_register("fp", frame1_fp.value().unwrap()); |
| 580 |
f.raw.set_register("sp", stack.start().value().unwrap()); |
| 581 |
|
| 582 |
let s = f.walk_stack(stack).await; |
| 583 |
assert_eq!(s.frames.len(), 2); |
| 584 |
|
| 585 |
{ |
| 586 |
// Frame 0 |
| 587 |
let frame = &s.frames[0]; |
| 588 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 589 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 590 |
} |
| 591 |
|
| 592 |
{ |
| 593 |
// Frame 2 (found as Frame 1) |
| 594 |
let frame = &s.frames[1]; |
| 595 |
let valid = &frame.context.valid; |
| 596 |
assert_eq!(frame.trust, FrameTrust::FramePointer); |
| 597 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 598 |
assert_eq!(which.len(), 3); |
| 599 |
} else { |
| 600 |
unreachable!(); |
| 601 |
} |
| 602 |
|
| 603 |
if let MinidumpRawContext::Arm64(ctx) = &frame.context.raw { |
| 604 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address2); |
| 605 |
assert_eq!( |
| 606 |
ctx.get_register("sp", valid).unwrap(), |
| 607 |
frame2_sp.value().unwrap() |
| 608 |
); |
| 609 |
assert_eq!( |
| 610 |
ctx.get_register("fp", valid).unwrap(), |
| 611 |
frame2_fp.value().unwrap() |
| 612 |
); |
| 613 |
} else { |
| 614 |
unreachable!(); |
| 615 |
} |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
#[tokio::test] |
| 620 |
async fn test_frame_pointer_ptr_auth_strip() { |
| 621 |
// Same as the basic frame pointer test but extra high bits have been set which |
| 622 |
// must be masked out. This is vaguely emulating Arm Pointer Authentication, |
| 623 |
// although very synthetically. This might break if we implement more accurate |
| 624 |
// stripping. But at that point we should have a better understanding of how |
| 625 |
// to make an "accurate" test! |
| 626 |
let mut f = TestFixture::new(); |
| 627 |
let mut stack = Section::new(); |
| 628 |
stack.start().set_const(0x80000000); |
| 629 |
|
| 630 |
let return_address1 = 0x50000100u64; |
| 631 |
let return_address2 = 0x50000900u64; |
| 632 |
let authenticated_return_address1 = return_address1 | 0x0013_8000_0000_0000; |
| 633 |
let authenticated_return_address2 = return_address2 | 0x1110_0000_0000_0000; |
| 634 |
|
| 635 |
let frame1_sp = Label::new(); |
| 636 |
let frame2_sp = Label::new(); |
| 637 |
let frame0_fp = Label::new(); |
| 638 |
let frame1_fp = Label::new(); |
| 639 |
let frame2_fp = Label::new(); |
| 640 |
let authenticated_frame1_fp = Label::new(); |
| 641 |
let authenticated_frame2_fp = Label::new(); |
| 642 |
|
| 643 |
stack = stack |
| 644 |
// frame 0 |
| 645 |
.append_repeated(0, 64) // space |
| 646 |
.D64(0x0000000D) // junk that's not |
| 647 |
.D64(0xF0000000) // a return address |
| 648 |
.mark(&frame0_fp) // next fp will point to the next value |
| 649 |
.D64(&authenticated_frame1_fp) // save current frame pointer |
| 650 |
.D64(authenticated_return_address1) // save current link register |
| 651 |
.mark(&frame1_sp) |
| 652 |
// frame 1 |
| 653 |
.append_repeated(0, 64) // space |
| 654 |
.D64(0x0000000D) // junk that's not |
| 655 |
.D64(0xF0000000) // a return address |
| 656 |
.mark(&frame1_fp) |
| 657 |
.D64(&authenticated_frame2_fp) |
| 658 |
.D64(authenticated_return_address2) |
| 659 |
.mark(&frame2_sp) |
| 660 |
// frame 2 |
| 661 |
.append_repeated(0, 64) // Whatever values on the stack. |
| 662 |
.D64(0x0000000D) // junk that's not |
| 663 |
.D64(0xF0000000) // a return address. |
| 664 |
.mark(&frame2_fp) // next fp will point to the next value |
| 665 |
.D64(0) |
| 666 |
.D64(0); |
| 667 |
|
| 668 |
authenticated_frame1_fp.set_const(frame1_fp.value().unwrap() | 0xa310_0000_0000_0000); |
| 669 |
authenticated_frame2_fp.set_const(frame2_fp.value().unwrap() | 0xf31e_8000_0000_0000); |
| 670 |
|
| 671 |
f.raw.set_register("pc", 0x40005510); |
| 672 |
f.raw.set_register("lr", 0x1fe0fe10); |
| 673 |
f.raw.set_register("fp", frame0_fp.value().unwrap()); |
| 674 |
f.raw.set_register("sp", stack.start().value().unwrap()); |
| 675 |
|
| 676 |
let s = f.walk_stack(stack).await; |
| 677 |
assert_eq!(s.frames.len(), 3); |
| 678 |
|
| 679 |
{ |
| 680 |
// Frame 0 |
| 681 |
let frame = &s.frames[0]; |
| 682 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 683 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 684 |
} |
| 685 |
|
| 686 |
{ |
| 687 |
// Frame 1 |
| 688 |
let frame = &s.frames[1]; |
| 689 |
let valid = &frame.context.valid; |
| 690 |
assert_eq!(frame.trust, FrameTrust::FramePointer); |
| 691 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 692 |
assert_eq!(which.len(), 3); |
| 693 |
} else { |
| 694 |
unreachable!(); |
| 695 |
} |
| 696 |
|
| 697 |
if let MinidumpRawContext::Arm64(ctx) = &frame.context.raw { |
| 698 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address1); |
| 699 |
assert_eq!( |
| 700 |
ctx.get_register("sp", valid).unwrap(), |
| 701 |
frame1_sp.value().unwrap() |
| 702 |
); |
| 703 |
assert_eq!( |
| 704 |
ctx.get_register("fp", valid).unwrap(), |
| 705 |
frame1_fp.value().unwrap() |
| 706 |
); |
| 707 |
} else { |
| 708 |
unreachable!(); |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
{ |
| 713 |
// Frame 2 |
| 714 |
let frame = &s.frames[2]; |
| 715 |
let valid = &frame.context.valid; |
| 716 |
assert_eq!(frame.trust, FrameTrust::FramePointer); |
| 717 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 718 |
assert_eq!(which.len(), 3); |
| 719 |
} else { |
| 720 |
unreachable!(); |
| 721 |
} |
| 722 |
|
| 723 |
if let MinidumpRawContext::Arm64(ctx) = &frame.context.raw { |
| 724 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address2); |
| 725 |
assert_eq!( |
| 726 |
ctx.get_register("sp", valid).unwrap(), |
| 727 |
frame2_sp.value().unwrap() |
| 728 |
); |
| 729 |
assert_eq!( |
| 730 |
ctx.get_register("fp", valid).unwrap(), |
| 731 |
frame2_fp.value().unwrap() |
| 732 |
); |
| 733 |
} else { |
| 734 |
unreachable!(); |
| 735 |
} |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
const CALLEE_SAVE_REGS: &[&str] = &[ |
| 740 |
"pc", "sp", "fp", "x19", "x20", "x21", "x22", "x23", "x24", "x25", "x26", "x27", "x28", |
| 741 |
]; |
| 742 |
|
| 743 |
fn init_cfi_state_high_module() -> (TestFixture, Section, Context, MinidumpContextValidity) { |
| 744 |
init_cfi_state_common(TestFixture::high_module()) |
| 745 |
} |
| 746 |
|
| 747 |
fn init_cfi_state() -> (TestFixture, Section, Context, MinidumpContextValidity) { |
| 748 |
init_cfi_state_common(TestFixture::new()) |
| 749 |
} |
| 750 |
|
| 751 |
fn init_cfi_state_common( |
| 752 |
mut f: TestFixture, |
| 753 |
) -> (TestFixture, Section, Context, MinidumpContextValidity) { |
| 754 |
let symbols = [ |
| 755 |
// The youngest frame's function. |
| 756 |
"FUNC 4000 1000 10 enchiridion\n", |
| 757 |
// Initially, nothing has been pushed on the stack, |
| 758 |
// and the return address is still in the link |
| 759 |
// register (x30). |
| 760 |
"STACK CFI INIT 4000 100 .cfa: sp 0 + .ra: x30\n", |
| 761 |
// Push x19, x20, the frame pointer and the link register. |
| 762 |
"STACK CFI 4001 .cfa: sp 32 + .ra: .cfa -8 + ^", |
| 763 |
" x19: .cfa -32 + ^ x20: .cfa -24 + ^ ", |
| 764 |
" x29: .cfa -16 + ^\n", |
| 765 |
// Save x19..x22 in x0..x3: verify that we populate |
| 766 |
// the youngest frame with all the values we have. |
| 767 |
"STACK CFI 4002 x19: x0 x20: x1 x21: x2 x22: x3\n", |
| 768 |
// Restore x19..x22. Save the non-callee-saves register x1. |
| 769 |
"STACK CFI 4003 .cfa: sp 40 + x1: .cfa 40 - ^", |
| 770 |
" x19: x19 x20: x20 x21: x21 x22: x22\n", |
| 771 |
// Move the .cfa back eight bytes, to point at the return |
| 772 |
// address, and restore the sp explicitly. |
| 773 |
"STACK CFI 4005 .cfa: sp 32 + x1: .cfa 32 - ^", |
| 774 |
" x29: .cfa 8 - ^ .ra: .cfa ^ sp: .cfa 8 +\n", |
| 775 |
// Recover the PC explicitly from a new stack slot; |
| 776 |
// provide garbage for the .ra. |
| 777 |
"STACK CFI 4006 .cfa: sp 40 + pc: .cfa 40 - ^\n", |
| 778 |
// The calling function. |
| 779 |
"FUNC 5000 1000 10 epictetus\n", |
| 780 |
// Mark it as end of stack. |
| 781 |
"STACK CFI INIT 5000 1000 .cfa: 0 .ra: 0\n", |
| 782 |
// A function whose CFI makes the stack pointer |
| 783 |
// go backwards. |
| 784 |
"FUNC 6000 1000 20 palinal\n", |
| 785 |
"STACK CFI INIT 6000 1000 .cfa: sp 8 - .ra: x30\n", |
| 786 |
// A function with CFI expressions that can't be |
| 787 |
// evaluated. |
| 788 |
"FUNC 7000 1000 20 rhetorical\n", |
| 789 |
"STACK CFI INIT 7000 1000 .cfa: moot .ra: ambiguous\n", |
| 790 |
]; |
| 791 |
f.add_symbols(String::from("module1"), symbols.concat()); |
| 792 |
|
| 793 |
f.raw.set_register("pc", 0x0000_0000_4000_5510); |
| 794 |
f.raw.set_register("sp", 0x0000_0000_8000_0000); |
| 795 |
f.raw.set_register("fp", 0x0000_00a2_8112_e110); |
| 796 |
f.raw.set_register("x19", 0x5e68b5d5b5d55e68); |
| 797 |
f.raw.set_register("x20", 0x34f3ebd1ebd134f3); |
| 798 |
f.raw.set_register("x21", 0x74bca31ea31e74bc); |
| 799 |
f.raw.set_register("x22", 0x16b32dcb2dcb16b3); |
| 800 |
f.raw.set_register("x23", 0x21372ada2ada2137); |
| 801 |
f.raw.set_register("x24", 0x557dbbbbbbbb557d); |
| 802 |
f.raw.set_register("x25", 0x8ca748bf48bf8ca7); |
| 803 |
f.raw.set_register("x26", 0x21f0ab46ab4621f0); |
| 804 |
f.raw.set_register("x27", 0x146732b732b71467); |
| 805 |
f.raw.set_register("x28", 0xa673645fa673645f); |
| 806 |
|
| 807 |
let raw_valid = MinidumpContextValidity::All; |
| 808 |
|
| 809 |
let expected = f.raw.clone(); |
| 810 |
let expected_regs = CALLEE_SAVE_REGS; |
| 811 |
let expected_valid = MinidumpContextValidity::Some(expected_regs.iter().copied().collect()); |
| 812 |
|
| 813 |
let stack = Section::new(); |
| 814 |
stack |
| 815 |
.start() |
| 816 |
.set_const(f.raw.get_register("sp", &raw_valid).unwrap()); |
| 817 |
|
| 818 |
(f, stack, expected, expected_valid) |
| 819 |
} |
| 820 |
|
| 821 |
async fn check_cfi( |
| 822 |
f: TestFixture, |
| 823 |
stack: Section, |
| 824 |
expected: Context, |
| 825 |
expected_valid: MinidumpContextValidity, |
| 826 |
) { |
| 827 |
let s = f.walk_stack(stack).await; |
| 828 |
assert_eq!(s.frames.len(), 2); |
| 829 |
|
| 830 |
{ |
| 831 |
// Frame 0 |
| 832 |
let frame = &s.frames[0]; |
| 833 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 834 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 835 |
} |
| 836 |
|
| 837 |
{ |
| 838 |
// Frame 1 |
| 839 |
if let MinidumpContextValidity::Some(ref expected_regs) = expected_valid { |
| 840 |
let frame = &s.frames[1]; |
| 841 |
let valid = &frame.context.valid; |
| 842 |
assert_eq!(frame.trust, FrameTrust::CallFrameInfo); |
| 843 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 844 |
assert_eq!(which.len(), expected_regs.len()); |
| 845 |
} else { |
| 846 |
unreachable!(); |
| 847 |
} |
| 848 |
|
| 849 |
if let MinidumpRawContext::Arm64(ctx) = &frame.context.raw { |
| 850 |
for reg in expected_regs { |
| 851 |
assert_eq!( |
| 852 |
ctx.get_register(reg, valid), |
| 853 |
expected.get_register(reg, &expected_valid), |
| 854 |
"{reg} registers didn't match!" |
| 855 |
); |
| 856 |
} |
| 857 |
return; |
| 858 |
} else { |
| 859 |
unreachable!() |
| 860 |
} |
| 861 |
} |
| 862 |
} |
| 863 |
unreachable!(); |
| 864 |
} |
| 865 |
|
| 866 |
#[tokio::test] |
| 867 |
async fn test_cfi_at_4000() { |
| 868 |
let (mut f, mut stack, expected, expected_valid) = init_cfi_state(); |
| 869 |
|
| 870 |
stack = stack.append_repeated(0, 120); |
| 871 |
|
| 872 |
f.raw.set_register("pc", 0x0000000040004000); |
| 873 |
f.raw.set_register("lr", 0x0000000040005510); |
| 874 |
|
| 875 |
check_cfi(f, stack, expected, expected_valid).await; |
| 876 |
} |
| 877 |
|
| 878 |
#[tokio::test] |
| 879 |
async fn test_cfi_at_4001() { |
| 880 |
let (mut f, mut stack, mut expected, expected_valid) = init_cfi_state(); |
| 881 |
|
| 882 |
let frame1_sp = Label::new(); |
| 883 |
stack = stack |
| 884 |
.D64(0x5e68b5d5b5d55e68) // saved x19 |
| 885 |
.D64(0x34f3ebd1ebd134f3) // saved x20 |
| 886 |
.D64(0x0000_00a2_8112_e110) // saved fp |
| 887 |
.D64(0x0000_0000_4000_5510) // return address |
| 888 |
.mark(&frame1_sp) |
| 889 |
.append_repeated(0, 120); |
| 890 |
|
| 891 |
expected.set_register("sp", frame1_sp.value().unwrap()); |
| 892 |
f.raw.set_register("pc", 0x0000000040004001); |
| 893 |
f.raw.set_register("x19", 0xadc9f635a635adc9); |
| 894 |
f.raw.set_register("x20", 0x623135ac35ac6231); |
| 895 |
f.raw.set_register("fp", 0x5fc4be14be145fc4); |
| 896 |
|
| 897 |
check_cfi(f, stack, expected, expected_valid).await; |
| 898 |
} |
| 899 |
|
| 900 |
#[tokio::test] |
| 901 |
async fn test_cfi_at_4002() { |
| 902 |
let (mut f, mut stack, mut expected, expected_valid) = init_cfi_state(); |
| 903 |
|
| 904 |
let frame1_sp = Label::new(); |
| 905 |
stack = stack |
| 906 |
.D64(0xff3dfb81fb81ff3d) // no longer saved x19 |
| 907 |
.D64(0x34f3ebd1ebd134f3) // no longer saved x20 |
| 908 |
.D64(0x0000_00a2_8112_e110) // saved fp |
| 909 |
.D64(0x0000_0000_4000_5510) // return address |
| 910 |
.mark(&frame1_sp) |
| 911 |
.append_repeated(0, 120); |
| 912 |
|
| 913 |
expected.set_register("sp", frame1_sp.value().unwrap()); |
| 914 |
f.raw.set_register("pc", 0x0000000040004002); |
| 915 |
f.raw.iregs[0] = 0x5e68b5d5b5d55e68; // saved x19 |
| 916 |
f.raw.iregs[1] = 0x34f3ebd1ebd134f3; // saved x20 |
| 917 |
f.raw.iregs[2] = 0x74bca31ea31e74bc; // saved x21 |
| 918 |
f.raw.iregs[3] = 0x16b32dcb2dcb16b3; // saved x22 |
| 919 |
f.raw.iregs[19] = 0xadc9f635a635adc9; // distinct callee x19 |
| 920 |
f.raw.iregs[20] = 0x623135ac35ac6231; // distinct callee x20 |
| 921 |
f.raw.iregs[21] = 0xac4543564356ac45; // distinct callee x21 |
| 922 |
f.raw.iregs[22] = 0x2561562f562f2561; // distinct callee x22 |
| 923 |
f.raw.set_register("fp", 0x5fc4be14be145fc4); |
| 924 |
|
| 925 |
check_cfi(f, stack, expected, expected_valid).await; |
| 926 |
} |
| 927 |
|
| 928 |
#[tokio::test] |
| 929 |
async fn test_cfi_at_4003() { |
| 930 |
let (mut f, mut stack, mut expected, mut expected_valid) = init_cfi_state(); |
| 931 |
|
| 932 |
let frame1_sp = Label::new(); |
| 933 |
stack = stack |
| 934 |
.D64(0xdd5a48c848c8dd5a) // saved x1 (even though it's not callee-saves) |
| 935 |
.D64(0xff3dfb81fb81ff3d) // no longer saved x19 |
| 936 |
.D64(0x34f3ebd1ebd134f3) // no longer saved x20 |
| 937 |
.D64(0x0000_00a2_8112_e110) // saved fp |
| 938 |
.D64(0x0000_0000_4000_5510) // return address |
| 939 |
.mark(&frame1_sp) |
| 940 |
.append_repeated(0, 120); |
| 941 |
|
| 942 |
expected.set_register("sp", frame1_sp.value().unwrap()); |
| 943 |
expected.iregs[1] = 0xdd5a48c848c8dd5a; |
| 944 |
if let MinidumpContextValidity::Some(ref mut which) = expected_valid { |
| 945 |
which.insert("x1"); |
| 946 |
} else { |
| 947 |
unreachable!(); |
| 948 |
} |
| 949 |
|
| 950 |
f.raw.set_register("pc", 0x0000000040004003); |
| 951 |
f.raw.iregs[1] = 0xfb756319fb756319; |
| 952 |
f.raw.set_register("fp", 0x5fc4be14be145fc4); |
| 953 |
|
| 954 |
check_cfi(f, stack, expected, expected_valid).await; |
| 955 |
} |
| 956 |
|
| 957 |
#[tokio::test] |
| 958 |
async fn test_cfi_at_4004() { |
| 959 |
// Should just be the same as 4003 |
| 960 |
|
| 961 |
let (mut f, mut stack, mut expected, mut expected_valid) = init_cfi_state(); |
| 962 |
|
| 963 |
let frame1_sp = Label::new(); |
| 964 |
stack = stack |
| 965 |
.D64(0xdd5a48c848c8dd5a) // saved x1 (even though it's not callee-saves) |
| 966 |
.D64(0xff3dfb81fb81ff3d) // no longer saved x19 |
| 967 |
.D64(0x34f3ebd1ebd134f3) // no longer saved x20 |
| 968 |
.D64(0x0000_00a2_8112_e110) // saved fp |
| 969 |
.D64(0x0000_0000_4000_5510) // return address |
| 970 |
.mark(&frame1_sp) |
| 971 |
.append_repeated(0, 120); |
| 972 |
|
| 973 |
expected.set_register("sp", frame1_sp.value().unwrap()); |
| 974 |
expected.iregs[1] = 0xdd5a48c848c8dd5a; |
| 975 |
if let MinidumpContextValidity::Some(ref mut which) = expected_valid { |
| 976 |
which.insert("x1"); |
| 977 |
} else { |
| 978 |
unreachable!(); |
| 979 |
} |
| 980 |
|
| 981 |
f.raw.set_register("pc", 0x0000000040004004); |
| 982 |
f.raw.iregs[1] = 0xfb756319fb756319; |
| 983 |
f.raw.set_register("fp", 0x5fc4be14be145fc4); |
| 984 |
|
| 985 |
check_cfi(f, stack, expected, expected_valid).await; |
| 986 |
} |
| 987 |
|
| 988 |
#[tokio::test] |
| 989 |
async fn test_cfi_at_4005_ptr_auth_strip_apple() { |
| 990 |
// This is the same as the normal 4005 test but with extra garabage (auth) bits |
| 991 |
// set in the high 24 bits. This emulates what apple platforms looks like. |
| 992 |
|
| 993 |
let (mut f, mut stack, mut expected, mut expected_valid) = init_cfi_state(); |
| 994 |
|
| 995 |
let frame1_sp = Label::new(); |
| 996 |
stack = stack |
| 997 |
.D64(0xdd5a48c848c8dd5a) // saved x1 (even though it's not callee-saves) |
| 998 |
.D64(0xff3dfb81fb81ff3d) // no longer saved x19 |
| 999 |
.D64(0x34f3ebd1ebd134f3) // no longer saved x20 |
| 1000 |
.D64(0xae23_80a2_8112_e110) // saved fp WITH AUTH |
| 1001 |
.D64(0xae1d_0000_4000_5510) // return address WITH AUTH |
| 1002 |
.mark(&frame1_sp) |
| 1003 |
.append_repeated(0, 120); |
| 1004 |
|
| 1005 |
expected.set_register("sp", frame1_sp.value().unwrap()); |
| 1006 |
expected.iregs[1] = 0xdd5a48c848c8dd5a; |
| 1007 |
if let MinidumpContextValidity::Some(ref mut which) = expected_valid { |
| 1008 |
which.insert("x1"); |
| 1009 |
} else { |
| 1010 |
unreachable!(); |
| 1011 |
} |
| 1012 |
|
| 1013 |
f.raw.set_register("pc", 0x0000000040004005); |
| 1014 |
f.raw.iregs[1] = 0xfb756319fb756319; |
| 1015 |
|
| 1016 |
check_cfi(f, stack, expected, expected_valid).await; |
| 1017 |
} |
| 1018 |
|
| 1019 |
#[tokio::test] |
| 1020 |
async fn test_cfi_at_4005_ptr_auth_strip_high() { |
| 1021 |
// This is the same as the normal 4005 test but with extra garabage (auth) bits |
| 1022 |
// set in the **extra** high bits. This emulates what android platforms look like. |
| 1023 |
|
| 1024 |
let (mut f, mut stack, mut expected, mut expected_valid) = init_cfi_state_high_module(); |
| 1025 |
|
| 1026 |
let frame1_sp = Label::new(); |
| 1027 |
stack = stack |
| 1028 |
.D64(0xdd5a48c848c8dd5a) // saved x1 (even though it's not callee-saves) |
| 1029 |
.D64(0xff3dfb81fb81ff3d) // no longer saved x19 |
| 1030 |
.D64(0x34f3ebd1ebd134f3) // no longer saved x20 |
| 1031 |
.D64(0x1003_45a2_8112_e110) // saved fp WITH AUTH |
| 1032 |
.D64(0x100d_f700_4000_5510) // return address WITH AUTH |
| 1033 |
.mark(&frame1_sp) |
| 1034 |
.append_repeated(0, 120); |
| 1035 |
|
| 1036 |
expected.set_register("sp", frame1_sp.value().unwrap()); |
| 1037 |
expected.set_register("fp", 0x0003_45a2_8112_e110); |
| 1038 |
expected.set_register("pc", 0x000d_f700_4000_5510); |
| 1039 |
expected.iregs[1] = 0xdd5a48c848c8dd5a; |
| 1040 |
if let MinidumpContextValidity::Some(ref mut which) = expected_valid { |
| 1041 |
which.insert("x1"); |
| 1042 |
} else { |
| 1043 |
unreachable!(); |
| 1044 |
} |
| 1045 |
|
| 1046 |
f.raw.set_register("pc", 0x0000000040004005); |
| 1047 |
f.raw.iregs[1] = 0xfb756319fb756319; |
| 1048 |
|
| 1049 |
check_cfi(f, stack, expected, expected_valid).await; |
| 1050 |
} |
| 1051 |
|
| 1052 |
#[tokio::test] |
| 1053 |
async fn test_cfi_at_4005() { |
| 1054 |
// Here we move the .cfa, but provide an explicit rule to recover the SP, |
| 1055 |
// so again there should be no change in the registers recovered. |
| 1056 |
|
| 1057 |
let (mut f, mut stack, mut expected, mut expected_valid) = init_cfi_state(); |
| 1058 |
|
| 1059 |
let frame1_sp = Label::new(); |
| 1060 |
stack = stack |
| 1061 |
.D64(0xdd5a48c848c8dd5a) // saved x1 (even though it's not callee-saves) |
| 1062 |
.D64(0xff3dfb81fb81ff3d) // no longer saved x19 |
| 1063 |
.D64(0x34f3ebd1ebd134f3) // no longer saved x20 |
| 1064 |
.D64(0x0000_00a2_8112_e110) // saved fp |
| 1065 |
.D64(0x0000_0000_4000_5510) // return address |
| 1066 |
.mark(&frame1_sp) |
| 1067 |
.append_repeated(0, 120); |
| 1068 |
|
| 1069 |
expected.set_register("sp", frame1_sp.value().unwrap()); |
| 1070 |
expected.iregs[1] = 0xdd5a48c848c8dd5a; |
| 1071 |
if let MinidumpContextValidity::Some(ref mut which) = expected_valid { |
| 1072 |
which.insert("x1"); |
| 1073 |
} else { |
| 1074 |
unreachable!(); |
| 1075 |
} |
| 1076 |
|
| 1077 |
f.raw.set_register("pc", 0x0000000040004005); |
| 1078 |
f.raw.iregs[1] = 0xfb756319fb756319; |
| 1079 |
|
| 1080 |
check_cfi(f, stack, expected, expected_valid).await; |
| 1081 |
} |
| 1082 |
|
| 1083 |
#[tokio::test] |
| 1084 |
async fn test_cfi_at_4006() { |
| 1085 |
// Here we provide an explicit rule for the PC, and have the saved .ra be |
| 1086 |
// bogus. |
| 1087 |
|
| 1088 |
let (mut f, mut stack, mut expected, mut expected_valid) = init_cfi_state(); |
| 1089 |
|
| 1090 |
let frame1_sp = Label::new(); |
| 1091 |
stack = stack |
| 1092 |
.D64(0x0000000040005510) // saved pc |
| 1093 |
.D64(0xdd5a48c848c8dd5a) // saved x1 (even though it's not callee-saves) |
| 1094 |
.D64(0xff3dfb81fb81ff3d) // no longer saved x19 |
| 1095 |
.D64(0x34f3ebd1ebd134f3) // no longer saved x20 |
| 1096 |
.D64(0x0000_00a2_8112_e110) // saved fp |
| 1097 |
.D64(0xf8d157835783f8d1) // .ra rule recovers this, which is garbage |
| 1098 |
.mark(&frame1_sp) |
| 1099 |
.append_repeated(0, 120); |
| 1100 |
|
| 1101 |
expected.set_register("sp", frame1_sp.value().unwrap()); |
| 1102 |
expected.iregs[1] = 0xdd5a48c848c8dd5a; |
| 1103 |
if let MinidumpContextValidity::Some(ref mut which) = expected_valid { |
| 1104 |
which.insert("x1"); |
| 1105 |
} else { |
| 1106 |
unreachable!(); |
| 1107 |
} |
| 1108 |
|
| 1109 |
f.raw.set_register("pc", 0x0000000040004006); |
| 1110 |
f.raw.iregs[1] = 0xfb756319fb756319; |
| 1111 |
|
| 1112 |
check_cfi(f, stack, expected, expected_valid).await; |
| 1113 |
} |
| 1114 |
|
| 1115 |
#[tokio::test] |
| 1116 |
async fn test_cfi_reject_backwards() { |
| 1117 |
// Check that we reject rules that would cause the stack pointer to |
| 1118 |
// move in the wrong direction. |
| 1119 |
|
| 1120 |
let (mut f, mut stack, _expected, _expected_valid) = init_cfi_state(); |
| 1121 |
|
| 1122 |
stack = stack.append_repeated(0, 120); |
| 1123 |
|
| 1124 |
f.raw.set_register("pc", 0x0000000040006000); |
| 1125 |
f.raw.set_register("sp", 0x0000000080000000); |
| 1126 |
f.raw.set_register("lr", 0x0000000040005510); |
| 1127 |
|
| 1128 |
let s = f.walk_stack(stack).await; |
| 1129 |
assert_eq!(s.frames.len(), 1); |
| 1130 |
} |
| 1131 |
|
| 1132 |
#[tokio::test] |
| 1133 |
async fn test_cfi_reject_bad_exprs() { |
| 1134 |
// Check that we reject rules whose expressions' evaluation fails. |
| 1135 |
|
| 1136 |
let (mut f, mut stack, _expected, _expected_valid) = init_cfi_state(); |
| 1137 |
|
| 1138 |
stack = stack.append_repeated(0, 120); |
| 1139 |
|
| 1140 |
f.raw.set_register("pc", 0x0000000040007000); |
| 1141 |
f.raw.set_register("sp", 0x0000000080000000); |
| 1142 |
|
| 1143 |
let s = f.walk_stack(stack).await; |
| 1144 |
assert_eq!(s.frames.len(), 1); |
| 1145 |
} |
| 1146 |
|
| 1147 |
#[tokio::test] |
| 1148 |
async fn test_frame_pointer_overflow() { |
| 1149 |
// Make sure we don't explode when trying frame pointer analysis on a value |
| 1150 |
// that will overflow. |
| 1151 |
|
| 1152 |
type Pointer = u64; |
| 1153 |
let stack_max: Pointer = Pointer::MAX; |
| 1154 |
let stack_size: Pointer = 1000; |
| 1155 |
let bad_frame_ptr: Pointer = stack_max; |
| 1156 |
|
| 1157 |
let mut f = TestFixture::new(); |
| 1158 |
let mut stack = Section::new(); |
| 1159 |
let stack_start: Pointer = stack_max - stack_size; |
| 1160 |
stack.start().set_const(stack_start); |
| 1161 |
|
| 1162 |
stack = stack |
| 1163 |
// frame 0 |
| 1164 |
.append_repeated(0, stack_size as usize); // junk, not important to the test |
| 1165 |
|
| 1166 |
f.raw.set_register("pc", 0x00007400c0000200); |
| 1167 |
f.raw.set_register("fp", bad_frame_ptr); |
| 1168 |
f.raw |
| 1169 |
.set_register("sp", stack.start().value().unwrap() as Pointer); |
| 1170 |
f.raw.set_register("lr", 0x00007500b0000110); |
| 1171 |
|
| 1172 |
let s = f.walk_stack(stack).await; |
| 1173 |
assert_eq!(s.frames.len(), 1); |
| 1174 |
|
| 1175 |
// As long as we don't panic, we're good! |
| 1176 |
} |
| 1177 |
|
| 1178 |
#[tokio::test] |
| 1179 |
async fn test_frame_pointer_barely_no_overflow() { |
| 1180 |
// This is a simple frame pointer test but with the all the values pushed |
| 1181 |
// as close to the upper memory boundary as possible, to confirm that |
| 1182 |
// our code doesn't randomly overflow *AND* isn't overzealous in |
| 1183 |
// its overflow guards. |
| 1184 |
|
| 1185 |
// We set the highest module here to bypass ptr auth stripping entirely and stress overflows |
| 1186 |
let mut f = TestFixture::highest_module(); |
| 1187 |
|
| 1188 |
let mut stack = Section::new(); |
| 1189 |
|
| 1190 |
type Pointer = u64; |
| 1191 |
let stack_max: Pointer = Pointer::MAX; |
| 1192 |
let pointer_size: Pointer = std::mem::size_of::<Pointer>() as Pointer; |
| 1193 |
let stack_size: Pointer = pointer_size * 3; |
| 1194 |
|
| 1195 |
let stack_start: Pointer = stack_max - stack_size; |
| 1196 |
let return_address: Pointer = 0x00007500b0000110; |
| 1197 |
stack.start().set_const(stack_start); |
| 1198 |
|
| 1199 |
let frame0_fp = Label::new(); |
| 1200 |
let frame1_sp = Label::new(); |
| 1201 |
let frame1_fp = Label::new(); |
| 1202 |
|
| 1203 |
stack = stack |
| 1204 |
// frame 0 |
| 1205 |
.mark(&frame0_fp) |
| 1206 |
.D64(&frame1_fp) // |
| 1207 |
.D64(return_address) // actual return address |
| 1208 |
// frame 1 |
| 1209 |
.mark(&frame1_sp) |
| 1210 |
.mark(&frame1_fp) // end of stack |
| 1211 |
.D64(0); |
| 1212 |
|
| 1213 |
f.raw.set_register("pc", 0x00007400c0000200); |
| 1214 |
f.raw |
| 1215 |
.set_register("fp", frame0_fp.value().unwrap() as Pointer); |
| 1216 |
f.raw |
| 1217 |
.set_register("sp", stack.start().value().unwrap() as Pointer); |
| 1218 |
f.raw.set_register("lr", return_address); |
| 1219 |
|
| 1220 |
let s = f.walk_stack(stack).await; |
| 1221 |
assert_eq!(s.frames.len(), 2); |
| 1222 |
|
| 1223 |
{ |
| 1224 |
// Frame 0 |
| 1225 |
let frame = &s.frames[0]; |
| 1226 |
let valid = &frame.context.valid; |
| 1227 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 1228 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 1229 |
|
| 1230 |
if let MinidumpRawContext::Arm64(ctx) = &frame.context.raw { |
| 1231 |
assert_eq!( |
| 1232 |
ctx.get_register("fp", valid).unwrap(), |
| 1233 |
frame0_fp.value().unwrap() as Pointer |
| 1234 |
); |
| 1235 |
} else { |
| 1236 |
unreachable!(); |
| 1237 |
} |
| 1238 |
} |
| 1239 |
|
| 1240 |
{ |
| 1241 |
// Frame 1 |
| 1242 |
let frame = &s.frames[1]; |
| 1243 |
let valid = &frame.context.valid; |
| 1244 |
assert_eq!(frame.trust, FrameTrust::FramePointer); |
| 1245 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 1246 |
assert_eq!(which.len(), 3); |
| 1247 |
} else { |
| 1248 |
unreachable!(); |
| 1249 |
} |
| 1250 |
|
| 1251 |
if let MinidumpRawContext::Arm64(ctx) = &frame.context.raw { |
| 1252 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address); |
| 1253 |
assert_eq!( |
| 1254 |
ctx.get_register("sp", valid).unwrap(), |
| 1255 |
frame1_sp.value().unwrap() as Pointer |
| 1256 |
); |
| 1257 |
assert_eq!( |
| 1258 |
ctx.get_register("fp", valid).unwrap(), |
| 1259 |
frame1_fp.value().unwrap() as Pointer |
| 1260 |
); |
| 1261 |
} else { |
| 1262 |
unreachable!(); |
| 1263 |
} |
| 1264 |
} |
| 1265 |
} |
| 1266 |
|
| 1267 |
#[tokio::test] |
| 1268 |
async fn test_frame_pointer_infinite_equality() { |
| 1269 |
// Leaf functions on Arm are allowed to not update the stack pointer, so |
| 1270 |
// it's valid for the frame pointer analysis to conclude that the stack |
| 1271 |
// pointer doesn't change. However we must only provide this allowance |
| 1272 |
// to the first stack frame, or else we're vulnerable to infinite loops. |
| 1273 |
// |
| 1274 |
// One of the CFI tests already checks that we allow the leaf case to work, |
| 1275 |
// so here we test that we don't get stuck in an infinite loop for the |
| 1276 |
// non-leaf case. |
| 1277 |
// |
| 1278 |
// This is just a copy-paste of test_frame_pointer except for the line |
| 1279 |
// "EVIL INFINITE FRAME POINTER" has been changed from frame2_fp to frame1_fp. |
| 1280 |
let mut f = TestFixture::new(); |
| 1281 |
let mut stack = Section::new(); |
| 1282 |
stack.start().set_const(0x80000000); |
| 1283 |
|
| 1284 |
let return_address1 = 0x50000100u64; |
| 1285 |
let return_address2 = 0x50000900u64; |
| 1286 |
let frame1_sp = Label::new(); |
| 1287 |
let frame2_sp = Label::new(); |
| 1288 |
let frame0_fp = Label::new(); |
| 1289 |
let frame1_fp = Label::new(); |
| 1290 |
let frame2_fp = Label::new(); |
| 1291 |
|
| 1292 |
stack = stack |
| 1293 |
// frame 0 |
| 1294 |
.append_repeated(0, 64) // space |
| 1295 |
.D64(0x0000000D) // junk that's not |
| 1296 |
.D64(0xF0000000) // a return address |
| 1297 |
.mark(&frame0_fp) // next fp will point to the next value |
| 1298 |
.D64(&frame0_fp) // EVIL INFINITE FRAME POINTER |
| 1299 |
.D64(return_address1) // save current link register |
| 1300 |
.mark(&frame1_sp) |
| 1301 |
// frame 1 |
| 1302 |
.append_repeated(0, 64) // space |
| 1303 |
.D64(0x0000000D) // junk that's not |
| 1304 |
.D64(0xF0000000) // a return address |
| 1305 |
.mark(&frame1_fp) |
| 1306 |
.D64(&frame2_fp) |
| 1307 |
.D64(return_address2) |
| 1308 |
.mark(&frame2_sp) |
| 1309 |
// frame 2 |
| 1310 |
.append_repeated(0, 64) // Whatever values on the stack. |
| 1311 |
.D64(0x0000000D) // junk that's not |
| 1312 |
.D64(0xF0000000) // a return address. |
| 1313 |
.mark(&frame2_fp) // next fp will point to the next value |
| 1314 |
.D64(0) |
| 1315 |
.D64(0); |
| 1316 |
|
| 1317 |
f.raw.set_register("pc", 0x40005510); |
| 1318 |
f.raw.set_register("lr", 0x1fe0fe10); |
| 1319 |
f.raw.set_register("fp", frame0_fp.value().unwrap()); |
| 1320 |
f.raw.set_register("sp", stack.start().value().unwrap()); |
| 1321 |
|
| 1322 |
let s = f.walk_stack(stack).await; |
| 1323 |
assert_eq!(s.frames.len(), 2); |
| 1324 |
|
| 1325 |
{ |
| 1326 |
// Frame 0 |
| 1327 |
let frame = &s.frames[0]; |
| 1328 |
assert_eq!(frame.trust, FrameTrust::Context); |
| 1329 |
assert_eq!(frame.context.valid, MinidumpContextValidity::All); |
| 1330 |
} |
| 1331 |
|
| 1332 |
{ |
| 1333 |
// Frame 1 (a messed up hybrid of frame0 and frame1) |
| 1334 |
let frame = &s.frames[1]; |
| 1335 |
let valid = &frame.context.valid; |
| 1336 |
assert_eq!(frame.trust, FrameTrust::FramePointer); |
| 1337 |
if let MinidumpContextValidity::Some(ref which) = valid { |
| 1338 |
assert_eq!(which.len(), 3); |
| 1339 |
} else { |
| 1340 |
unreachable!(); |
| 1341 |
} |
| 1342 |
|
| 1343 |
if let MinidumpRawContext::Arm64(ctx) = &frame.context.raw { |
| 1344 |
assert_eq!(ctx.get_register("pc", valid).unwrap(), return_address1); |
| 1345 |
assert_eq!( |
| 1346 |
ctx.get_register("sp", valid).unwrap(), |
| 1347 |
frame1_sp.value().unwrap() |
| 1348 |
); |
| 1349 |
assert_eq!( |
| 1350 |
ctx.get_register("fp", valid).unwrap(), |
| 1351 |
frame0_fp.value().unwrap() |
| 1352 |
); |
| 1353 |
} else { |
| 1354 |
unreachable!(); |
| 1355 |
} |
| 1356 |
} |
| 1357 |
|
| 1358 |
// Never get to frame 2, alas! |
| 1359 |
} |