| 1 |
// Copyright 2015 Ted Mielczarek. See the COPYRIGHT |
| 2 |
// file at the top-level directory of this distribution. |
| 3 |
|
| 4 |
use nom::branch::alt; |
| 5 |
use nom::bytes::complete::{tag, take_while}; |
| 6 |
use nom::character::complete::{hex_digit1, space1}; |
| 7 |
use nom::character::{is_digit, is_hex_digit}; |
| 8 |
use nom::combinator::{cut, map, map_res, opt}; |
| 9 |
use nom::error::{Error, ErrorKind, ParseError}; |
| 10 |
use nom::multi::separated_list1; |
| 11 |
use nom::sequence::{preceded, terminated, tuple}; |
| 12 |
use nom::{Err, IResult}; |
| 13 |
use range_map::{Range, RangeMap}; |
| 14 |
use tracing::warn; |
| 15 |
|
| 16 |
use std::collections::HashMap; |
| 17 |
use std::convert::TryFrom; |
| 18 |
use std::fmt::Debug; |
| 19 |
use std::{mem, str}; |
| 20 |
|
| 21 |
use minidump_common::traits::IntoRangeMapSafe; |
| 22 |
|
| 23 |
use crate::sym_file::types::*; |
| 24 |
use crate::SymbolError; |
| 25 |
|
| 26 |
#[derive(Debug)] |
| 27 |
enum Line { |
| 28 |
Module(String, String, String, String), |
| 29 |
Info(Info), |
| 30 |
File(u32, String), |
| 31 |
InlineOrigin(u32, String), |
| 32 |
Public(PublicSymbol), |
| 33 |
Function(Function, Vec<SourceLine>, Vec<Inlinee>), |
| 34 |
StackWin(WinFrameType), |
| 35 |
StackCfi(StackInfoCfi), |
| 36 |
} |
| 37 |
|
| 38 |
/// Match a hex string, parse it to a u32 or a u64. |
| 39 |
fn hex_str<T: std::ops::Shl<T, Output = T> + std::ops::BitOr<T, Output = T> + From<u8>>( |
| 40 |
input: &[u8], |
| 41 |
) -> IResult<&[u8], T> { |
| 42 |
// Consume up to max_len digits. For u32 that's 8 digits and for u64 that's 16 digits. |
| 43 |
// Two hex digits form one byte. |
| 44 |
let max_len = mem::size_of::<T>() * 2; |
| 45 |
|
| 46 |
let mut res: T = T::from(0); |
| 47 |
let mut k = 0; |
| 48 |
for v in input.iter().take(max_len) { |
| 49 |
let digit = match (*v as char).to_digit(16) { |
| 50 |
Some(v) => v, |
| 51 |
None => break, |
| 52 |
}; |
| 53 |
res = res << T::from(4); |
| 54 |
res = res | T::from(digit as u8); |
| 55 |
k += 1; |
| 56 |
} |
| 57 |
if k == 0 { |
| 58 |
return Err(Err::Error(Error::from_error_kind( |
| 59 |
input, |
| 60 |
ErrorKind::HexDigit, |
| 61 |
))); |
| 62 |
} |
| 63 |
let remaining = &input[k..]; |
| 64 |
Ok((remaining, res)) |
| 65 |
} |
| 66 |
|
| 67 |
/// Match a decimal string, parse it to a u32. |
| 68 |
/// |
| 69 |
/// This is doing everything manually so that we only look at each byte once. |
| 70 |
/// With a naive implementation you might be looking at them three times: First |
| 71 |
/// you might get a slice of acceptable characters from nom, then you might parse |
| 72 |
/// that slice into a str (checking for utf-8 unnecessarily), and then you might |
| 73 |
/// parse that string into a decimal number. |
| 74 |
fn decimal_u32(input: &[u8]) -> IResult<&[u8], u32> { |
| 75 |
const MAX_LEN: usize = 10; // u32::MAX has 10 decimal digits |
| 76 |
let mut res: u64 = 0; |
| 77 |
let mut k = 0; |
| 78 |
for v in input.iter().take(MAX_LEN) { |
| 79 |
let digit = *v as char; |
| 80 |
let digit_value = match digit.to_digit(10) { |
| 81 |
Some(v) => v, |
| 82 |
None => break, |
| 83 |
}; |
| 84 |
res = res * 10 + digit_value as u64; |
| 85 |
k += 1; |
| 86 |
} |
| 87 |
if k == 0 { |
| 88 |
return Err(Err::Error(Error::from_error_kind(input, ErrorKind::Digit))); |
| 89 |
} |
| 90 |
let res = u32::try_from(res) |
| 91 |
.map_err(|_| Err::Error(Error::from_error_kind(input, ErrorKind::TooLarge)))?; |
| 92 |
let remaining = &input[k..]; |
| 93 |
Ok((remaining, res)) |
| 94 |
} |
| 95 |
|
| 96 |
/// Take 0 or more non-space bytes. |
| 97 |
fn non_space(input: &[u8]) -> IResult<&[u8], &[u8]> { |
| 98 |
take_while(|c: u8| c != b' ')(input) |
| 99 |
} |
| 100 |
|
| 101 |
/// Accept `\n` with an arbitrary number of preceding `\r` bytes. |
| 102 |
/// |
| 103 |
/// This is different from `line_ending` which doesn't accept `\r` if it isn't |
| 104 |
/// followed by `\n`. |
| 105 |
fn my_eol(input: &[u8]) -> IResult<&[u8], &[u8]> { |
| 106 |
preceded(take_while(|b| b == b'\r'), tag(b"\n"))(input) |
| 107 |
} |
| 108 |
|
| 109 |
/// Accept everything except `\r` and `\n`. |
| 110 |
/// |
| 111 |
/// This is different from `not_line_ending` which rejects its input if it's |
| 112 |
/// followed by a `\r` which is not immediately followed by a `\n`. |
| 113 |
fn not_my_eol(input: &[u8]) -> IResult<&[u8], &[u8]> { |
| 114 |
take_while(|b| b != b'\r' && b != b'\n')(input) |
| 115 |
} |
| 116 |
|
| 117 |
/// Parse a single byte if it matches the predicate. |
| 118 |
/// |
| 119 |
/// nom has `satisfy`, which is similar. It differs in the argument type of the |
| 120 |
/// predicate: `satisfy`'s predicate takes `char`, whereas `single`'s predicate |
| 121 |
/// takes `u8`. |
| 122 |
fn single(predicate: fn(u8) -> bool) -> impl Fn(&[u8]) -> IResult<&[u8], u8> { |
| 123 |
move |i: &[u8]| match i.split_first() { |
| 124 |
Some((b, rest)) if predicate(*b) => Ok((rest, *b)), |
| 125 |
_ => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Satisfy))), |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
// Matches a MODULE record. |
| 130 |
fn module_line(input: &[u8]) -> IResult<&[u8], (String, String, String, String)> { |
| 131 |
let (input, _) = terminated(tag("MODULE"), space1)(input)?; |
| 132 |
let (input, (os, cpu, debug_id, filename)) = cut(tuple(( |
| 133 |
terminated(map_res(non_space, str::from_utf8), space1), // os |
| 134 |
terminated(map_res(non_space, str::from_utf8), space1), // cpu |
| 135 |
terminated(map_res(hex_digit1, str::from_utf8), space1), // debug id |
| 136 |
terminated(map_res(not_my_eol, str::from_utf8), my_eol), // filename |
| 137 |
)))(input)?; |
| 138 |
Ok(( |
| 139 |
input, |
| 140 |
( |
| 141 |
os.to_string(), |
| 142 |
cpu.to_string(), |
| 143 |
debug_id.to_string(), |
| 144 |
filename.to_string(), |
| 145 |
), |
| 146 |
)) |
| 147 |
} |
| 148 |
|
| 149 |
// Matches an INFO URL record. |
| 150 |
fn info_url(input: &[u8]) -> IResult<&[u8], Info> { |
| 151 |
let (input, _) = terminated(tag("INFO URL"), space1)(input)?; |
| 152 |
let (input, url) = cut(terminated(map_res(not_my_eol, str::from_utf8), my_eol))(input)?; |
| 153 |
Ok((input, Info::Url(url.to_string()))) |
| 154 |
} |
| 155 |
|
| 156 |
// Matches other INFO records. |
| 157 |
fn info_line(input: &[u8]) -> IResult<&[u8], &[u8]> { |
| 158 |
let (input, _) = terminated(tag("INFO"), space1)(input)?; |
| 159 |
cut(terminated(not_my_eol, my_eol))(input) |
| 160 |
} |
| 161 |
|
| 162 |
// Matches a FILE record. |
| 163 |
fn file_line(input: &[u8]) -> IResult<&[u8], (u32, String)> { |
| 164 |
let (input, _) = terminated(tag("FILE"), space1)(input)?; |
| 165 |
let (input, (id, filename)) = cut(tuple(( |
| 166 |
terminated(decimal_u32, space1), |
| 167 |
terminated(map_res(not_my_eol, str::from_utf8), my_eol), |
| 168 |
)))(input)?; |
| 169 |
Ok((input, (id, filename.to_string()))) |
| 170 |
} |
| 171 |
|
| 172 |
// Matches an INLINE_ORIGIN record. |
| 173 |
fn inline_origin_line(input: &[u8]) -> IResult<&[u8], (u32, String)> { |
| 174 |
let (input, _) = terminated(tag("INLINE_ORIGIN"), space1)(input)?; |
| 175 |
let (input, (id, function)) = cut(tuple(( |
| 176 |
terminated(decimal_u32, space1), |
| 177 |
terminated(map_res(not_my_eol, str::from_utf8), my_eol), |
| 178 |
)))(input)?; |
| 179 |
Ok((input, (id, function.to_string()))) |
| 180 |
} |
| 181 |
|
| 182 |
// Matches a PUBLIC record. |
| 183 |
fn public_line(input: &[u8]) -> IResult<&[u8], PublicSymbol> { |
| 184 |
let (input, _) = terminated(tag("PUBLIC"), space1)(input)?; |
| 185 |
let (input, (_multiple, address, parameter_size, name)) = cut(tuple(( |
| 186 |
opt(terminated(tag("m"), space1)), |
| 187 |
terminated(hex_str::<u64>, space1), |
| 188 |
terminated(hex_str::<u32>, space1), |
| 189 |
terminated(map_res(not_my_eol, str::from_utf8), my_eol), |
| 190 |
)))(input)?; |
| 191 |
Ok(( |
| 192 |
input, |
| 193 |
PublicSymbol { |
| 194 |
address, |
| 195 |
parameter_size, |
| 196 |
name: name.to_string(), |
| 197 |
}, |
| 198 |
)) |
| 199 |
} |
| 200 |
|
| 201 |
// Matches line data after a FUNC record. |
| 202 |
fn func_line_data(input: &[u8]) -> IResult<&[u8], SourceLine> { |
| 203 |
let (input, (address, size, line, file)) = tuple(( |
| 204 |
terminated(hex_str::<u64>, space1), |
| 205 |
terminated(hex_str::<u32>, space1), |
| 206 |
terminated(decimal_u32, space1), |
| 207 |
terminated(decimal_u32, my_eol), |
| 208 |
))(input)?; |
| 209 |
Ok(( |
| 210 |
input, |
| 211 |
SourceLine { |
| 212 |
address, |
| 213 |
size, |
| 214 |
file, |
| 215 |
line, |
| 216 |
}, |
| 217 |
)) |
| 218 |
} |
| 219 |
|
| 220 |
// Matches a FUNC record. |
| 221 |
fn func_line(input: &[u8]) -> IResult<&[u8], Function> { |
| 222 |
let (input, _) = terminated(tag("FUNC"), space1)(input)?; |
| 223 |
let (input, (_multiple, address, size, parameter_size, name)) = cut(tuple(( |
| 224 |
opt(terminated(tag("m"), space1)), |
| 225 |
terminated(hex_str::<u64>, space1), |
| 226 |
terminated(hex_str::<u32>, space1), |
| 227 |
terminated(hex_str::<u32>, space1), |
| 228 |
terminated(map_res(not_my_eol, str::from_utf8), my_eol), |
| 229 |
)))(input)?; |
| 230 |
Ok(( |
| 231 |
input, |
| 232 |
Function { |
| 233 |
address, |
| 234 |
size, |
| 235 |
parameter_size, |
| 236 |
name: name.to_string(), |
| 237 |
lines: RangeMap::new(), |
| 238 |
inlinees: Vec::new(), |
| 239 |
}, |
| 240 |
)) |
| 241 |
} |
| 242 |
|
| 243 |
// Matches one entry of the form <address> <size> which is used at the end of an INLINE record |
| 244 |
fn inline_address_range(input: &[u8]) -> IResult<&[u8], (u64, u32)> { |
| 245 |
tuple((terminated(hex_str::<u64>, space1), hex_str::<u32>))(input) |
| 246 |
} |
| 247 |
|
| 248 |
// Matches an INLINE record. |
| 249 |
/// |
| 250 |
/// An INLINE record has the form `INLINE <inline_nest_level> <call_site_line> <call_site_file_id> <origin_id> [<address> <size>]+`. |
| 251 |
fn inline_line(input: &[u8]) -> IResult<&[u8], impl Iterator<Item = Inlinee>> { |
| 252 |
let (input, _) = terminated(tag("INLINE"), space1)(input)?; |
| 253 |
let (input, (depth, call_line, call_file, origin_id)) = cut(tuple(( |
| 254 |
terminated(decimal_u32, space1), |
| 255 |
terminated(decimal_u32, space1), |
| 256 |
terminated(decimal_u32, space1), |
| 257 |
terminated(decimal_u32, space1), |
| 258 |
)))(input)?; |
| 259 |
let (input, address_ranges) = cut(terminated( |
| 260 |
separated_list1(space1, inline_address_range), |
| 261 |
my_eol, |
| 262 |
))(input)?; |
| 263 |
Ok(( |
| 264 |
input, |
| 265 |
address_ranges |
| 266 |
.into_iter() |
| 267 |
.map(move |(address, size)| Inlinee { |
| 268 |
address, |
| 269 |
size, |
| 270 |
call_file, |
| 271 |
call_line, |
| 272 |
depth, |
| 273 |
origin_id, |
| 274 |
}), |
| 275 |
)) |
| 276 |
} |
| 277 |
|
| 278 |
// Matches a STACK WIN record. |
| 279 |
fn stack_win_line(input: &[u8]) -> IResult<&[u8], WinFrameType> { |
| 280 |
let (input, _) = terminated(tag("STACK WIN"), space1)(input)?; |
| 281 |
let ( |
| 282 |
input, |
| 283 |
( |
| 284 |
ty, |
| 285 |
address, |
| 286 |
code_size, |
| 287 |
prologue_size, |
| 288 |
epilogue_size, |
| 289 |
parameter_size, |
| 290 |
saved_register_size, |
| 291 |
local_size, |
| 292 |
max_stack_size, |
| 293 |
has_program_string, |
| 294 |
rest, |
| 295 |
), |
| 296 |
) = cut(tuple(( |
| 297 |
terminated(single(is_hex_digit), space1), // ty |
| 298 |
terminated(hex_str::<u64>, space1), // address |
| 299 |
terminated(hex_str::<u32>, space1), // code_size |
| 300 |
terminated(hex_str::<u32>, space1), // prologue_size |
| 301 |
terminated(hex_str::<u32>, space1), // epilogue_size |
| 302 |
terminated(hex_str::<u32>, space1), // parameter_size |
| 303 |
terminated(hex_str::<u32>, space1), // saved_register_size |
| 304 |
terminated(hex_str::<u32>, space1), // local_size |
| 305 |
terminated(hex_str::<u32>, space1), // max_stack_size |
| 306 |
terminated(map(single(is_digit), |b| b == b'1'), space1), // has_program_string |
| 307 |
terminated(map_res(not_my_eol, str::from_utf8), my_eol), |
| 308 |
)))(input)?; |
| 309 |
|
| 310 |
// Sometimes has_program_string is just wrong. We could try to infer which one is right |
| 311 |
// but this is rare enough that it's better to just play it safe and discard the input. |
| 312 |
let really_has_program_string = ty == b'4'; |
| 313 |
if really_has_program_string != has_program_string { |
| 314 |
let kind = match ty { |
| 315 |
b'4' => "FrameData", |
| 316 |
b'0' => "Fpo", |
| 317 |
_ => "Unknown Type!", |
| 318 |
}; |
| 319 |
warn!("STACK WIN entry had inconsistent values for type and has_program_string, discarding corrupt entry"); |
| 320 |
// warn!(" {}", &line); |
| 321 |
warn!( |
| 322 |
" type: {} ({}), has_program_string: {}, final_arg: {}", |
| 323 |
str::from_utf8(&[ty]).unwrap_or(""), |
| 324 |
kind, |
| 325 |
has_program_string, |
| 326 |
rest |
| 327 |
); |
| 328 |
|
| 329 |
return Ok((input, WinFrameType::Unhandled)); |
| 330 |
} |
| 331 |
|
| 332 |
let program_string_or_base_pointer = if really_has_program_string { |
| 333 |
WinStackThing::ProgramString(rest.to_string()) |
| 334 |
} else { |
| 335 |
WinStackThing::AllocatesBasePointer(rest == "1") |
| 336 |
}; |
| 337 |
let info = StackInfoWin { |
| 338 |
address, |
| 339 |
size: code_size, |
| 340 |
prologue_size, |
| 341 |
epilogue_size, |
| 342 |
parameter_size, |
| 343 |
saved_register_size, |
| 344 |
local_size, |
| 345 |
max_stack_size, |
| 346 |
program_string_or_base_pointer, |
| 347 |
}; |
| 348 |
let frame_type = match ty { |
| 349 |
b'4' => WinFrameType::FrameData(info), |
| 350 |
b'0' => WinFrameType::Fpo(info), |
| 351 |
_ => WinFrameType::Unhandled, |
| 352 |
}; |
| 353 |
Ok((input, frame_type)) |
| 354 |
} |
| 355 |
|
| 356 |
// Matches a STACK CFI record. |
| 357 |
fn stack_cfi(input: &[u8]) -> IResult<&[u8], CfiRules> { |
| 358 |
let (input, _) = terminated(tag("STACK CFI"), space1)(input)?; |
| 359 |
let (input, (address, rules)) = cut(tuple(( |
| 360 |
terminated(hex_str::<u64>, space1), |
| 361 |
terminated(map_res(not_my_eol, str::from_utf8), my_eol), |
| 362 |
)))(input)?; |
| 363 |
Ok(( |
| 364 |
input, |
| 365 |
CfiRules { |
| 366 |
address, |
| 367 |
rules: rules.to_string(), |
| 368 |
}, |
| 369 |
)) |
| 370 |
} |
| 371 |
|
| 372 |
// Matches a STACK CFI INIT record. |
| 373 |
fn stack_cfi_init(input: &[u8]) -> IResult<&[u8], StackInfoCfi> { |
| 374 |
let (input, _) = terminated(tag("STACK CFI INIT"), space1)(input)?; |
| 375 |
let (input, (address, size, rules)) = cut(tuple(( |
| 376 |
terminated(hex_str::<u64>, space1), |
| 377 |
terminated(hex_str::<u32>, space1), |
| 378 |
terminated(map_res(not_my_eol, str::from_utf8), my_eol), |
| 379 |
)))(input)?; |
| 380 |
Ok(( |
| 381 |
input, |
| 382 |
StackInfoCfi { |
| 383 |
init: CfiRules { |
| 384 |
address, |
| 385 |
rules: rules.to_string(), |
| 386 |
}, |
| 387 |
size, |
| 388 |
add_rules: Default::default(), |
| 389 |
}, |
| 390 |
)) |
| 391 |
} |
| 392 |
|
| 393 |
// Parse any of the line data that can occur in the body of a symbol file. |
| 394 |
fn line(input: &[u8]) -> IResult<&[u8], Line> { |
| 395 |
alt(( |
| 396 |
map(info_url, Line::Info), |
| 397 |
map(info_line, |_| Line::Info(Info::Unknown)), |
| 398 |
map(file_line, |(i, f)| Line::File(i, f)), |
| 399 |
map(inline_origin_line, |(i, f)| Line::InlineOrigin(i, f)), |
| 400 |
map(public_line, Line::Public), |
| 401 |
map(func_line, |f| Line::Function(f, Vec::new(), Vec::new())), |
| 402 |
map(stack_win_line, Line::StackWin), |
| 403 |
map(stack_cfi_init, Line::StackCfi), |
| 404 |
map(module_line, |(p, a, i, f)| Line::Module(p, a, i, f)), |
| 405 |
))(input) |
| 406 |
} |
| 407 |
|
| 408 |
/// A parser for SymbolFiles. |
| 409 |
/// |
| 410 |
/// This is basically just a SymbolFile but with some extra state |
| 411 |
/// to handle streaming parsing. |
| 412 |
/// |
| 413 |
/// Use this by repeatedly calling [`parse_more`][] until the |
| 414 |
/// whole input is consumed. Then call [`finish`][]. |
| 415 |
#[derive(Debug, Default)] |
| 416 |
pub struct SymbolParser { |
| 417 |
module_id: String, |
| 418 |
debug_file: String, |
| 419 |
files: HashMap<u32, String>, |
| 420 |
inline_origins: HashMap<u32, String>, |
| 421 |
publics: Vec<PublicSymbol>, |
| 422 |
|
| 423 |
// When building a RangeMap when need to sort an array of this |
| 424 |
// format anyway, so we might as well construct it directly and |
| 425 |
// save a giant allocation+copy. |
| 426 |
functions: Vec<(Range<u64>, Function)>, |
| 427 |
cfi_stack_info: Vec<(Range<u64>, StackInfoCfi)>, |
| 428 |
win_stack_framedata_info: Vec<(Range<u64>, StackInfoWin)>, |
| 429 |
win_stack_fpo_info: Vec<(Range<u64>, StackInfoWin)>, |
| 430 |
url: Option<String>, |
| 431 |
pub lines: u64, |
| 432 |
cur_item: Option<Line>, |
| 433 |
} |
| 434 |
|
| 435 |
impl SymbolParser { |
| 436 |
/// Creates a new SymbolParser. |
| 437 |
pub fn new() -> Self { |
| 438 |
Self::default() |
| 439 |
} |
| 440 |
|
| 441 |
/// Parses as much of the input as it can, and then returns |
| 442 |
/// how many bytes of the input was used. The *unused* portion of the |
| 443 |
/// input must be resubmitted on subsequent calls to parse_more |
| 444 |
/// (along with more data so we can make progress on the parse). |
| 445 |
pub fn parse_more(&mut self, mut input: &[u8]) -> Result<usize, SymbolError> { |
| 446 |
// We parse the input line-by-line, so trim away any part of the input |
| 447 |
// that comes after the last newline (this is necessary for streaming |
| 448 |
// parsing, as it can otherwise be impossible to tell if a line is |
| 449 |
// truncated.) |
| 450 |
input = if let Some(idx) = input.iter().rposition(|&x| x == b'\n') { |
| 451 |
&input[..idx + 1] |
| 452 |
} else { |
| 453 |
// If there's no newline, then we can't process anything! |
| 454 |
return Ok(0); |
| 455 |
}; |
| 456 |
// Remember the (truncated) input so that we can tell how many bytes |
| 457 |
// we've consumed. |
| 458 |
let orig_input = input; |
| 459 |
|
| 460 |
loop { |
| 461 |
// If there's no more input, then we've consumed all of it |
| 462 |
// (except for the partial line we trimmed away). |
| 463 |
if input.is_empty() { |
| 464 |
return Ok(orig_input.len()); |
| 465 |
} |
| 466 |
|
| 467 |
// First check if we're currently processing sublines of a |
| 468 |
// multi-line item like `FUNC` and `STACK CFI INIT`. |
| 469 |
// If we are, parse the next line as its subline format. |
| 470 |
// |
| 471 |
// If we encounter an error, this probably means we've |
| 472 |
// reached a new item (which ends this one). To handle this, |
| 473 |
// we can just finish off the current item and resubmit this |
| 474 |
// line to the top-level parser (below). If the line is |
| 475 |
// genuinely corrupt, then the top-level parser will also |
| 476 |
// fail to read it. |
| 477 |
// |
| 478 |
// We `take` and then reconstitute the item for borrowing/move |
| 479 |
// reasons. |
| 480 |
match self.cur_item.take() { |
| 481 |
Some(Line::Function(cur, mut lines, mut inlinees)) => { |
| 482 |
match self.parse_func_subline(input, &mut lines, &mut inlinees) { |
| 483 |
Ok((new_input, ())) => { |
| 484 |
input = new_input; |
| 485 |
self.cur_item = Some(Line::Function(cur, lines, inlinees)); |
| 486 |
self.lines += 1; |
| 487 |
continue; |
| 488 |
} |
| 489 |
Err(_) => { |
| 490 |
self.finish_item(Line::Function(cur, lines, inlinees)); |
| 491 |
continue; |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
Some(Line::StackCfi(mut cur)) => match stack_cfi(input) { |
| 496 |
Ok((new_input, line)) => { |
| 497 |
cur.add_rules.push(line); |
| 498 |
input = new_input; |
| 499 |
self.cur_item = Some(Line::StackCfi(cur)); |
| 500 |
self.lines += 1; |
| 501 |
continue; |
| 502 |
} |
| 503 |
Err(_) => { |
| 504 |
self.finish_item(Line::StackCfi(cur)); |
| 505 |
continue; |
| 506 |
} |
| 507 |
}, |
| 508 |
_ => { |
| 509 |
// We're not parsing sublines, move on to top level parser! |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
// Ignore empty lines |
| 514 |
if let Ok((new_input, _)) = my_eol(input) { |
| 515 |
input = new_input; |
| 516 |
self.lines += 1; |
| 517 |
continue; |
| 518 |
} |
| 519 |
|
| 520 |
// Parse a top-level item, and first handle the Result |
| 521 |
let line = match line(input) { |
| 522 |
Ok((new_input, line)) => { |
| 523 |
// Success! Advance the input. |
| 524 |
input = new_input; |
| 525 |
line |
| 526 |
} |
| 527 |
Err(_) => { |
| 528 |
// The file has a completely corrupt line, |
| 529 |
// conservatively reject the entire parse. |
| 530 |
return Err(SymbolError::ParseError("failed to parse file", self.lines)); |
| 531 |
} |
| 532 |
}; |
| 533 |
|
| 534 |
// Now store the item in our partial SymbolFile (or make it the cur_item |
| 535 |
// if it has potential sublines we need to parse first). |
| 536 |
match line { |
| 537 |
Line::Module(_platform, _arch, module_id, debug_file) => { |
| 538 |
// We don't use this but it MUST be the first line |
| 539 |
if self.lines != 0 { |
| 540 |
return Err(SymbolError::ParseError( |
| 541 |
"MODULE line found after the start of the file", |
| 542 |
self.lines, |
| 543 |
)); |
| 544 |
} |
| 545 |
self.module_id = module_id; |
| 546 |
self.debug_file = debug_file; |
| 547 |
} |
| 548 |
Line::Info(Info::Url(cached_url)) => { |
| 549 |
self.url = Some(cached_url); |
| 550 |
} |
| 551 |
Line::Info(Info::Unknown) => { |
| 552 |
// Don't care |
| 553 |
} |
| 554 |
Line::File(id, filename) => { |
| 555 |
self.files.insert(id, filename.to_string()); |
| 556 |
} |
| 557 |
Line::InlineOrigin(id, function) => { |
| 558 |
self.inline_origins.insert(id, function.to_string()); |
| 559 |
} |
| 560 |
Line::Public(p) => { |
| 561 |
self.publics.push(p); |
| 562 |
} |
| 563 |
Line::StackWin(frame_type) => { |
| 564 |
// PDB files contain lots of overlapping unwind info, so we have to filter |
| 565 |
// some of it out. |
| 566 |
fn insert_win_stack_info( |
| 567 |
stack_win: &mut Vec<(Range<u64>, StackInfoWin)>, |
| 568 |
info: StackInfoWin, |
| 569 |
) { |
| 570 |
if let Some(memory_range) = info.memory_range() { |
| 571 |
if let Some((last_range, last_info)) = stack_win.last_mut() { |
| 572 |
if last_range.intersects(&memory_range) { |
| 573 |
if info.address > last_info.address { |
| 574 |
// Sometimes we get STACK WIN directives where each line |
| 575 |
// has an accurate starting point, but the length just |
| 576 |
// covers the entire function, like so: |
| 577 |
// |
| 578 |
// addr: 0, len: 10 |
| 579 |
// addr: 1, len: 9 |
| 580 |
// addr: 4, len: 6 |
| 581 |
// |
| 582 |
// In this case, the next instruction is the one that |
| 583 |
// really defines the length of the previous one. So |
| 584 |
// we need to fixup the lengths like so: |
| 585 |
// |
| 586 |
// addr: 0, len: 1 |
| 587 |
// addr: 1, len: 2 |
| 588 |
// addr: 4, len: 6 |
| 589 |
last_info.size = (info.address - last_info.address) as u32; |
| 590 |
*last_range = last_info.memory_range().unwrap(); |
| 591 |
} else if *last_range != memory_range { |
| 592 |
// We silently drop identical ranges because sometimes |
| 593 |
// duplicates happen, but we complain for non-trivial duplicates. |
| 594 |
warn!( |
| 595 |
"STACK WIN entry had bad intersections, dropping it {:?}", |
| 596 |
info |
| 597 |
); |
| 598 |
return; |
| 599 |
} |
| 600 |
} |
| 601 |
} |
| 602 |
stack_win.push((memory_range, info)); |
| 603 |
} else { |
| 604 |
warn!("STACK WIN entry had invalid range, dropping it {:?}", info); |
| 605 |
} |
| 606 |
} |
| 607 |
match frame_type { |
| 608 |
WinFrameType::FrameData(s) => { |
| 609 |
insert_win_stack_info(&mut self.win_stack_framedata_info, s); |
| 610 |
} |
| 611 |
WinFrameType::Fpo(s) => { |
| 612 |
insert_win_stack_info(&mut self.win_stack_fpo_info, s); |
| 613 |
} |
| 614 |
// Just ignore other types. |
| 615 |
_ => {} |
| 616 |
} |
| 617 |
} |
| 618 |
item @ Line::Function(_, _, _) => { |
| 619 |
// More sublines to parse |
| 620 |
self.cur_item = Some(item); |
| 621 |
} |
| 622 |
item @ Line::StackCfi(_) => { |
| 623 |
// More sublines to parse |
| 624 |
self.cur_item = Some(item); |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
// Make note that we've consumed a line of input. |
| 629 |
self.lines += 1; |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
/// Parses a single line which is following a FUNC line. |
| 634 |
fn parse_func_subline<'a>( |
| 635 |
&mut self, |
| 636 |
input: &'a [u8], |
| 637 |
lines: &mut Vec<SourceLine>, |
| 638 |
inlinees: &mut Vec<Inlinee>, |
| 639 |
) -> IResult<&'a [u8], ()> { |
| 640 |
// We can have three different types of sublines: INLINE_ORIGIN, INLINE, or line records. |
| 641 |
// Check them one by one. |
| 642 |
// We're not using nom's `alt()` here because we'd need to find a common return type. |
| 643 |
if input.starts_with(b"INLINE_ORIGIN ") { |
| 644 |
let (input, (id, function)) = inline_origin_line(input)?; |
| 645 |
self.inline_origins.insert(id, function); |
| 646 |
return Ok((input, ())); |
| 647 |
} |
| 648 |
if input.starts_with(b"INLINE ") { |
| 649 |
let (input, new_inlinees) = inline_line(input)?; |
| 650 |
inlinees.extend(new_inlinees); |
| 651 |
return Ok((input, ())); |
| 652 |
} |
| 653 |
let (input, line) = func_line_data(input)?; |
| 654 |
lines.push(line); |
| 655 |
Ok((input, ())) |
| 656 |
} |
| 657 |
|
| 658 |
/// Finish processing an item (cur_item) which had sublines. |
| 659 |
/// We now have all the sublines, so it's complete. |
| 660 |
fn finish_item(&mut self, item: Line) { |
| 661 |
match item { |
| 662 |
Line::Function(mut cur, lines, mut inlinees) => { |
| 663 |
cur.lines = lines |
| 664 |
.into_iter() |
| 665 |
// Line data from PDB files often has a zero-size line entry, so just |
| 666 |
// filter those out. |
| 667 |
.filter(|l| l.size > 0) |
| 668 |
.map(|l| { |
| 669 |
let end_address = l.address.checked_add(l.size as u64 - 1); |
| 670 |
let range = end_address.map(|end| Range::new(l.address, end)); |
| 671 |
(range, l) |
| 672 |
}) |
| 673 |
.into_rangemap_safe(); |
| 674 |
|
| 675 |
inlinees.sort(); |
| 676 |
cur.inlinees = inlinees; |
| 677 |
|
| 678 |
if let Some(range) = cur.memory_range() { |
| 679 |
self.functions.push((range, cur)); |
| 680 |
} |
| 681 |
} |
| 682 |
Line::StackCfi(mut cur) => { |
| 683 |
cur.add_rules.sort(); |
| 684 |
if let Some(range) = cur.memory_range() { |
| 685 |
self.cfi_stack_info.push((range, cur)); |
| 686 |
} |
| 687 |
} |
| 688 |
_ => { |
| 689 |
unreachable!() |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
/// Finish the parse and create the final SymbolFile. |
| 695 |
/// |
| 696 |
/// Call this when the parser has consumed all the input. |
| 697 |
pub fn finish(mut self) -> SymbolFile { |
| 698 |
// If there's a pending multiline item, finish it now. |
| 699 |
if let Some(item) = self.cur_item.take() { |
| 700 |
self.finish_item(item); |
| 701 |
} |
| 702 |
|
| 703 |
// Now sort everything and bundle it up in its final format. |
| 704 |
self.publics.sort(); |
| 705 |
|
| 706 |
SymbolFile { |
| 707 |
module_id: self.module_id, |
| 708 |
debug_file: self.debug_file, |
| 709 |
files: self.files, |
| 710 |
publics: self.publics, |
| 711 |
functions: into_rangemap_safe(self.functions), |
| 712 |
inline_origins: self.inline_origins, |
| 713 |
cfi_stack_info: into_rangemap_safe(self.cfi_stack_info), |
| 714 |
win_stack_framedata_info: into_rangemap_safe(self.win_stack_framedata_info), |
| 715 |
win_stack_fpo_info: into_rangemap_safe(self.win_stack_fpo_info), |
| 716 |
// Will get filled in by the caller |
| 717 |
url: self.url, |
| 718 |
ambiguities_repaired: 0, |
| 719 |
ambiguities_discarded: 0, |
| 720 |
corruptions_discarded: 0, |
| 721 |
cfi_eval_corruptions: 0, |
| 722 |
} |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
// Copied from minidump-common, because we've preconstructed the array to sort. |
| 727 |
fn into_rangemap_safe<V: Clone + Eq + Debug>(mut input: Vec<(Range<u64>, V)>) -> RangeMap<u64, V> { |
| 728 |
input.sort_by_key(|x| x.0); |
| 729 |
let mut vec: Vec<(Range<u64>, V)> = Vec::with_capacity(input.len()); |
| 730 |
for (range, val) in input { |
| 731 |
if let Some((last_range, last_val)) = vec.last_mut() { |
| 732 |
if range.start <= last_range.end && val != *last_val { |
| 733 |
continue; |
| 734 |
} |
| 735 |
|
| 736 |
if range.start <= last_range.end.saturating_add(1) && &val == last_val { |
| 737 |
last_range.end = std::cmp::max(range.end, last_range.end); |
| 738 |
continue; |
| 739 |
} |
| 740 |
} |
| 741 |
vec.push((range, val)); |
| 742 |
} |
| 743 |
RangeMap::try_from_iter(vec).unwrap() |
| 744 |
} |
| 745 |
|
| 746 |
#[cfg(test)] |
| 747 |
fn parse_symbol_bytes(data: &[u8]) -> Result<SymbolFile, SymbolError> { |
| 748 |
SymbolFile::parse(data, |_| ()) |
| 749 |
} |
| 750 |
|
| 751 |
#[test] |
| 752 |
fn test_module_line() { |
| 753 |
let line = b"MODULE Linux x86 D3096ED481217FD4C16B29CD9BC208BA0 firefox-bin\n"; |
| 754 |
let rest = &b""[..]; |
| 755 |
assert_eq!( |
| 756 |
module_line(line), |
| 757 |
Ok(( |
| 758 |
rest, |
| 759 |
( |
| 760 |
"Linux".to_string(), |
| 761 |
"x86".to_string(), |
| 762 |
"D3096ED481217FD4C16B29CD9BC208BA0".to_string(), |
| 763 |
"firefox-bin".to_string() |
| 764 |
) |
| 765 |
)) |
| 766 |
); |
| 767 |
} |
| 768 |
|
| 769 |
#[test] |
| 770 |
fn test_module_line_filename_spaces() { |
| 771 |
let line = b"MODULE Windows x86_64 D3096ED481217FD4C16B29CD9BC208BA0 firefox x y z\n"; |
| 772 |
let rest = &b""[..]; |
| 773 |
assert_eq!( |
| 774 |
module_line(line), |
| 775 |
Ok(( |
| 776 |
rest, |
| 777 |
( |
| 778 |
"Windows".to_string(), |
| 779 |
"x86_64".to_string(), |
| 780 |
"D3096ED481217FD4C16B29CD9BC208BA0".to_string(), |
| 781 |
"firefox x y z".to_string() |
| 782 |
) |
| 783 |
)) |
| 784 |
); |
| 785 |
} |
| 786 |
|
| 787 |
/// Sometimes dump_syms on Windows does weird things and produces multiple carriage returns |
| 788 |
/// before the line feed. |
| 789 |
#[test] |
| 790 |
fn test_module_line_crcrlf() { |
| 791 |
let line = b"MODULE Windows x86_64 D3096ED481217FD4C16B29CD9BC208BA0 firefox\r\r\n"; |
| 792 |
let rest = &b""[..]; |
| 793 |
assert_eq!( |
| 794 |
module_line(line), |
| 795 |
Ok(( |
| 796 |
rest, |
| 797 |
( |
| 798 |
"Windows".to_string(), |
| 799 |
"x86_64".to_string(), |
| 800 |
"D3096ED481217FD4C16B29CD9BC208BA0".to_string(), |
| 801 |
"firefox".to_string() |
| 802 |
) |
| 803 |
)) |
| 804 |
); |
| 805 |
} |
| 806 |
|
| 807 |
#[test] |
| 808 |
fn test_info_line() { |
| 809 |
let line = b"INFO blah blah blah\n"; |
| 810 |
let bits = &b"blah blah blah"[..]; |
| 811 |
let rest = &b""[..]; |
| 812 |
assert_eq!(info_line(line), Ok((rest, bits))); |
| 813 |
} |
| 814 |
|
| 815 |
#[test] |
| 816 |
fn test_info_line2() { |
| 817 |
let line = b"INFO CODE_ID abc xyz\n"; |
| 818 |
let bits = &b"CODE_ID abc xyz"[..]; |
| 819 |
let rest = &b""[..]; |
| 820 |
assert_eq!(info_line(line), Ok((rest, bits))); |
| 821 |
} |
| 822 |
|
| 823 |
#[test] |
| 824 |
fn test_info_url() { |
| 825 |
let line = b"INFO URL https://www.example.com\n"; |
| 826 |
let url = "https://www.example.com".to_string(); |
| 827 |
let rest = &b""[..]; |
| 828 |
assert_eq!(info_url(line), Ok((rest, Info::Url(url)))); |
| 829 |
} |
| 830 |
|
| 831 |
#[test] |
| 832 |
fn test_file_line() { |
| 833 |
let line = b"FILE 1 foo.c\n"; |
| 834 |
let rest = &b""[..]; |
| 835 |
assert_eq!(file_line(line), Ok((rest, (1, String::from("foo.c"))))); |
| 836 |
} |
| 837 |
|
| 838 |
#[test] |
| 839 |
fn test_file_line_spaces() { |
| 840 |
let line = b"FILE 1234 foo bar.xyz\n"; |
| 841 |
let rest = &b""[..]; |
| 842 |
assert_eq!( |
| 843 |
file_line(line), |
| 844 |
Ok((rest, (1234, String::from("foo bar.xyz")))) |
| 845 |
); |
| 846 |
} |
| 847 |
|
| 848 |
#[test] |
| 849 |
fn test_public_line() { |
| 850 |
let line = b"PUBLIC f00d d00d some func\n"; |
| 851 |
let rest = &b""[..]; |
| 852 |
assert_eq!( |
| 853 |
public_line(line), |
| 854 |
Ok(( |
| 855 |
rest, |
| 856 |
PublicSymbol { |
| 857 |
address: 0xf00d, |
| 858 |
parameter_size: 0xd00d, |
| 859 |
name: "some func".to_string(), |
| 860 |
} |
| 861 |
)) |
| 862 |
); |
| 863 |
} |
| 864 |
|
| 865 |
#[test] |
| 866 |
fn test_public_with_m() { |
| 867 |
let line = b"PUBLIC m f00d d00d some func\n"; |
| 868 |
let rest = &b""[..]; |
| 869 |
assert_eq!( |
| 870 |
public_line(line), |
| 871 |
Ok(( |
| 872 |
rest, |
| 873 |
PublicSymbol { |
| 874 |
address: 0xf00d, |
| 875 |
parameter_size: 0xd00d, |
| 876 |
name: "some func".to_string(), |
| 877 |
} |
| 878 |
)) |
| 879 |
); |
| 880 |
} |
| 881 |
|
| 882 |
#[test] |
| 883 |
fn test_func_lines_no_lines() { |
| 884 |
use range_map::RangeMap; |
| 885 |
let line = b"FUNC c184 30 0 nsQueryInterfaceWithError::operator()(nsID const&, void**) const\n"; |
| 886 |
let rest = &b""[..]; |
| 887 |
assert_eq!( |
| 888 |
func_line(line), |
| 889 |
Ok(( |
| 890 |
rest, |
| 891 |
Function { |
| 892 |
address: 0xc184, |
| 893 |
size: 0x30, |
| 894 |
parameter_size: 0, |
| 895 |
name: "nsQueryInterfaceWithError::operator()(nsID const&, void**) const" |
| 896 |
.to_string(), |
| 897 |
lines: RangeMap::new(), |
| 898 |
inlinees: Vec::new(), |
| 899 |
} |
| 900 |
)) |
| 901 |
); |
| 902 |
} |
| 903 |
|
| 904 |
#[test] |
| 905 |
fn test_truncated_func() { |
| 906 |
let line = b"FUNC 1000\n1000 10 42 7\n"; |
| 907 |
assert_eq!( |
| 908 |
func_line(line), |
| 909 |
Err(Err::Failure(Error { |
| 910 |
input: &line[9..], |
| 911 |
code: ErrorKind::Space |
| 912 |
})) |
| 913 |
); |
| 914 |
} |
| 915 |
|
| 916 |
#[test] |
| 917 |
fn test_inline_line_single_range() { |
| 918 |
let line = b"INLINE 0 3082 52 1410 49200 10\n"; |
| 919 |
assert_eq!( |
| 920 |
inline_line(line).unwrap().1.collect::<Vec<_>>(), |
| 921 |
vec![Inlinee { |
| 922 |
depth: 0, |
| 923 |
address: 0x49200, |
| 924 |
size: 0x10, |
| 925 |
call_file: 52, |
| 926 |
call_line: 3082, |
| 927 |
origin_id: 1410 |
| 928 |
}] |
| 929 |
) |
| 930 |
} |
| 931 |
|
| 932 |
#[test] |
| 933 |
fn test_inline_line_multiple_ranges() { |
| 934 |
let line = b"INLINE 6 642 8 207 8b110 18 8b154 18\n"; |
| 935 |
assert_eq!( |
| 936 |
inline_line(line).unwrap().1.collect::<Vec<_>>(), |
| 937 |
vec![ |
| 938 |
Inlinee { |
| 939 |
depth: 6, |
| 940 |
address: 0x8b110, |
| 941 |
size: 0x18, |
| 942 |
call_file: 8, |
| 943 |
call_line: 642, |
| 944 |
origin_id: 207 |
| 945 |
}, |
| 946 |
Inlinee { |
| 947 |
depth: 6, |
| 948 |
address: 0x8b154, |
| 949 |
size: 0x18, |
| 950 |
call_file: 8, |
| 951 |
call_line: 642, |
| 952 |
origin_id: 207 |
| 953 |
} |
| 954 |
] |
| 955 |
) |
| 956 |
} |
| 957 |
|
| 958 |
#[test] |
| 959 |
fn test_func_lines_and_lines() { |
| 960 |
let data = b"FUNC 1000 30 10 some func |
| 961 |
1000 10 42 7 |
| 962 |
INLINE_ORIGIN 16 inlined_function_name() |
| 963 |
1010 10 52 8 |
| 964 |
INLINE 0 23 9 16 1020 10 |
| 965 |
1020 10 62 15 |
| 966 |
"; |
| 967 |
let file = SymbolFile::from_bytes(data).expect("failed to parse!"); |
| 968 |
let (_, f) = file.functions.ranges_values().next().unwrap(); |
| 969 |
assert_eq!(f.address, 0x1000); |
| 970 |
assert_eq!(f.size, 0x30); |
| 971 |
assert_eq!(f.parameter_size, 0x10); |
| 972 |
assert_eq!(f.name, "some func".to_string()); |
| 973 |
assert_eq!( |
| 974 |
f.lines.get(0x1000).unwrap(), |
| 975 |
&SourceLine { |
| 976 |
address: 0x1000, |
| 977 |
size: 0x10, |
| 978 |
file: 7, |
| 979 |
line: 42, |
| 980 |
} |
| 981 |
); |
| 982 |
assert_eq!( |
| 983 |
f.lines.ranges_values().collect::<Vec<_>>(), |
| 984 |
vec![ |
| 985 |
&( |
| 986 |
Range::<u64>::new(0x1000, 0x100F), |
| 987 |
SourceLine { |
| 988 |
address: 0x1000, |
| 989 |
size: 0x10, |
| 990 |
file: 7, |
| 991 |
line: 42, |
| 992 |
}, |
| 993 |
), |
| 994 |
&( |
| 995 |
Range::<u64>::new(0x1010, 0x101F), |
| 996 |
SourceLine { |
| 997 |
address: 0x1010, |
| 998 |
size: 0x10, |
| 999 |
file: 8, |
| 1000 |
line: 52, |
| 1001 |
}, |
| 1002 |
), |
| 1003 |
&( |
| 1004 |
Range::<u64>::new(0x1020, 0x102F), |
| 1005 |
SourceLine { |
| 1006 |
address: 0x1020, |
| 1007 |
size: 0x10, |
| 1008 |
file: 15, |
| 1009 |
line: 62, |
| 1010 |
}, |
| 1011 |
), |
| 1012 |
] |
| 1013 |
); |
| 1014 |
assert_eq!( |
| 1015 |
f.inlinees, |
| 1016 |
vec![Inlinee { |
| 1017 |
depth: 0, |
| 1018 |
address: 0x1020, |
| 1019 |
size: 0x10, |
| 1020 |
call_file: 9, |
| 1021 |
call_line: 23, |
| 1022 |
origin_id: 16 |
| 1023 |
}] |
| 1024 |
); |
| 1025 |
} |
| 1026 |
|
| 1027 |
#[test] |
| 1028 |
fn test_nested_inlines() { |
| 1029 |
// 0x1000: outer_func() @ <file 15>:60 -> mid_func() @ <file 4>:12 -> inner_func1() <file 7>:42 |
| 1030 |
// 0x1010: outer_func() @ <file 15>:60 -> mid_func() @ <file 4>:17 -> inner_func2() <file 8>:52 |
| 1031 |
// 0x1020: outer_func() @ <file 15>:62 |
| 1032 |
let data = b"FUNC 1000 30 10 outer_func() |
| 1033 |
INLINE_ORIGIN 1 inner_func_2() |
| 1034 |
INLINE_ORIGIN 2 mid_func() |
| 1035 |
INLINE_ORIGIN 3 inner_func_1() |
| 1036 |
INLINE 0 60 15 2 1000 20 |
| 1037 |
INLINE 1 12 4 3 1000 10 |
| 1038 |
INLINE 1 17 4 1 1010 10 |
| 1039 |
1000 10 42 7 |
| 1040 |
1010 10 52 8 |
| 1041 |
1020 10 62 15 |
| 1042 |
"; |
| 1043 |
let file = SymbolFile::from_bytes(data).expect("failed to parse!"); |
| 1044 |
let (_, f) = file.functions.ranges_values().next().unwrap(); |
| 1045 |
assert_eq!(f.address, 0x1000); |
| 1046 |
assert_eq!(f.size, 0x30); |
| 1047 |
assert_eq!(f.parameter_size, 0x10); |
| 1048 |
assert_eq!(f.name, "outer_func()".to_string()); |
| 1049 |
|
| 1050 |
// Check the source locations at the "outermost" level, i.e. the line |
| 1051 |
// numbers inside the "outer_func()" function. This function has its |
| 1052 |
// code in file 15, so all source locations at this level should be |
| 1053 |
// in that file. |
| 1054 |
assert_eq!(f.get_outermost_sourceloc(0x0fff), None); |
| 1055 |
assert_eq!( |
| 1056 |
f.get_outermost_sourceloc(0x1000), |
| 1057 |
Some((15, 60, 0x1000, Some(2))) |
| 1058 |
); |
| 1059 |
assert_eq!( |
| 1060 |
f.get_outermost_sourceloc(0x100f), |
| 1061 |
Some((15, 60, 0x1000, Some(2))) |
| 1062 |
); |
| 1063 |
assert_eq!( |
| 1064 |
f.get_outermost_sourceloc(0x1010), |
| 1065 |
Some((15, 60, 0x1000, Some(2))) |
| 1066 |
); |
| 1067 |
assert_eq!( |
| 1068 |
f.get_outermost_sourceloc(0x101f), |
| 1069 |
Some((15, 60, 0x1000, Some(2))) |
| 1070 |
); |
| 1071 |
assert_eq!( |
| 1072 |
f.get_outermost_sourceloc(0x1020), |
| 1073 |
Some((15, 62, 0x1020, None)) |
| 1074 |
); |
| 1075 |
assert_eq!( |
| 1076 |
f.get_outermost_sourceloc(0x102f), |
| 1077 |
Some((15, 62, 0x1020, None)) |
| 1078 |
); |
| 1079 |
assert_eq!(f.get_outermost_sourceloc(0x1030), None); |
| 1080 |
|
| 1081 |
// Check the first level of inlining. There is only one inlined call |
| 1082 |
// at this level, the call from outer_func() to mid_func(), spanning |
| 1083 |
// the range 0x1000..0x1020. |
| 1084 |
assert_eq!(f.get_inlinee_at_depth(0, 0x0fff), None); |
| 1085 |
assert_eq!(f.get_inlinee_at_depth(0, 0x1000), Some((15, 60, 0x1000, 2))); |
| 1086 |
assert_eq!(f.get_inlinee_at_depth(0, 0x100f), Some((15, 60, 0x1000, 2))); |
| 1087 |
assert_eq!(f.get_inlinee_at_depth(0, 0x1010), Some((15, 60, 0x1000, 2))); |
| 1088 |
assert_eq!(f.get_inlinee_at_depth(0, 0x101f), Some((15, 60, 0x1000, 2))); |
| 1089 |
assert_eq!(f.get_inlinee_at_depth(0, 0x1020), None); |
| 1090 |
assert_eq!(f.get_inlinee_at_depth(0, 0x102f), None); |
| 1091 |
assert_eq!(f.get_inlinee_at_depth(0, 0x1030), None); |
| 1092 |
|
| 1093 |
// Check the second level of inlining. Two function calls from mid_func() |
| 1094 |
// have been inlined at this level, the call to inner_func_1() and the |
| 1095 |
// call to inner_func_2(). |
| 1096 |
// The code for mid_func() is in file 4, so the location of the calls to |
| 1097 |
// inner_func_1() and inner_func_2() are in file 4. |
| 1098 |
assert_eq!(f.get_inlinee_at_depth(1, 0x0fff), None); |
| 1099 |
assert_eq!(f.get_inlinee_at_depth(1, 0x1000), Some((4, 12, 0x1000, 3))); |
| 1100 |
assert_eq!(f.get_inlinee_at_depth(1, 0x100f), Some((4, 12, 0x1000, 3))); |
| 1101 |
assert_eq!(f.get_inlinee_at_depth(1, 0x1010), Some((4, 17, 0x1010, 1))); |
| 1102 |
assert_eq!(f.get_inlinee_at_depth(1, 0x101f), Some((4, 17, 0x1010, 1))); |
| 1103 |
assert_eq!(f.get_inlinee_at_depth(1, 0x1020), None); |
| 1104 |
assert_eq!(f.get_inlinee_at_depth(1, 0x102f), None); |
| 1105 |
assert_eq!(f.get_inlinee_at_depth(1, 0x1030), None); |
| 1106 |
|
| 1107 |
// Check that there are no deeper inline calls. |
| 1108 |
assert_eq!(f.get_inlinee_at_depth(2, 0x0fff), None); |
| 1109 |
assert_eq!(f.get_inlinee_at_depth(2, 0x1000), None); |
| 1110 |
assert_eq!(f.get_inlinee_at_depth(2, 0x100f), None); |
| 1111 |
assert_eq!(f.get_inlinee_at_depth(2, 0x1010), None); |
| 1112 |
assert_eq!(f.get_inlinee_at_depth(2, 0x101f), None); |
| 1113 |
assert_eq!(f.get_inlinee_at_depth(2, 0x1020), None); |
| 1114 |
assert_eq!(f.get_inlinee_at_depth(2, 0x102f), None); |
| 1115 |
assert_eq!(f.get_inlinee_at_depth(2, 0x1030), None); |
| 1116 |
|
| 1117 |
// Check the "innermost" source locations. These locations describe the |
| 1118 |
// file and line at the deepest level of inlining at the given address. |
| 1119 |
// We have a location in inner_func_1() (whose code is in file 7), a location |
| 1120 |
// in inner_func_2() (whose code is in file 8), and a location in the outer |
| 1121 |
// function outer_func() (whose code is in file 15). |
| 1122 |
assert_eq!(f.get_innermost_sourceloc(0x0fff), None); |
| 1123 |
assert_eq!(f.get_innermost_sourceloc(0x1000), Some((7, 42, 0x1000))); |
| 1124 |
assert_eq!(f.get_innermost_sourceloc(0x100f), Some((7, 42, 0x1000))); |
| 1125 |
assert_eq!(f.get_innermost_sourceloc(0x1010), Some((8, 52, 0x1010))); |
| 1126 |
assert_eq!(f.get_innermost_sourceloc(0x101f), Some((8, 52, 0x1010))); |
| 1127 |
assert_eq!(f.get_innermost_sourceloc(0x1020), Some((15, 62, 0x1020))); |
| 1128 |
assert_eq!(f.get_innermost_sourceloc(0x102f), Some((15, 62, 0x1020))); |
| 1129 |
assert_eq!(f.get_innermost_sourceloc(0x1030), None); |
| 1130 |
} |
| 1131 |
|
| 1132 |
#[test] |
| 1133 |
fn test_func_with_m() { |
| 1134 |
let data = b"FUNC m 1000 30 10 some func |
| 1135 |
1000 10 42 7 |
| 1136 |
1010 10 52 8 |
| 1137 |
1020 10 62 15 |
| 1138 |
"; |
| 1139 |
let file = SymbolFile::from_bytes(data).expect("failed to parse!"); |
| 1140 |
let (_, _f) = file.functions.ranges_values().next().unwrap(); |
| 1141 |
} |
| 1142 |
|
| 1143 |
#[test] |
| 1144 |
fn test_stack_win_line_program_string() { |
| 1145 |
let line = |
| 1146 |
b"STACK WIN 4 2170 14 a1 b2 c3 d4 e5 f6 1 $eip 4 + ^ = $esp $ebp 8 + = $ebp $ebp ^ =\n"; |
| 1147 |
match stack_win_line(line) { |
| 1148 |
Ok((rest, WinFrameType::FrameData(stack))) => { |
| 1149 |
assert_eq!(rest, &b""[..]); |
| 1150 |
assert_eq!(stack.address, 0x2170); |
| 1151 |
assert_eq!(stack.size, 0x14); |
| 1152 |
assert_eq!(stack.prologue_size, 0xa1); |
| 1153 |
assert_eq!(stack.epilogue_size, 0xb2); |
| 1154 |
assert_eq!(stack.parameter_size, 0xc3); |
| 1155 |
assert_eq!(stack.saved_register_size, 0xd4); |
| 1156 |
assert_eq!(stack.local_size, 0xe5); |
| 1157 |
assert_eq!(stack.max_stack_size, 0xf6); |
| 1158 |
assert_eq!( |
| 1159 |
stack.program_string_or_base_pointer, |
| 1160 |
WinStackThing::ProgramString( |
| 1161 |
"$eip 4 + ^ = $esp $ebp 8 + = $ebp $ebp ^ =".to_string() |
| 1162 |
) |
| 1163 |
); |
| 1164 |
} |
| 1165 |
Err(e) => panic!("{}", format!("Parse error: {e:?}")), |
| 1166 |
_ => panic!("Something bad happened"), |
| 1167 |
} |
| 1168 |
} |
| 1169 |
|
| 1170 |
#[test] |
| 1171 |
fn test_stack_win_line_frame_data() { |
| 1172 |
let line = b"STACK WIN 0 1000 30 a1 b2 c3 d4 e5 f6 0 1\n"; |
| 1173 |
match stack_win_line(line) { |
| 1174 |
Ok((rest, WinFrameType::Fpo(stack))) => { |
| 1175 |
assert_eq!(rest, &b""[..]); |
| 1176 |
assert_eq!(stack.address, 0x1000); |
| 1177 |
assert_eq!(stack.size, 0x30); |
| 1178 |
assert_eq!(stack.prologue_size, 0xa1); |
| 1179 |
assert_eq!(stack.epilogue_size, 0xb2); |
| 1180 |
assert_eq!(stack.parameter_size, 0xc3); |
| 1181 |
assert_eq!(stack.saved_register_size, 0xd4); |
| 1182 |
assert_eq!(stack.local_size, 0xe5); |
| 1183 |
assert_eq!(stack.max_stack_size, 0xf6); |
| 1184 |
assert_eq!( |
| 1185 |
stack.program_string_or_base_pointer, |
| 1186 |
WinStackThing::AllocatesBasePointer(true) |
| 1187 |
); |
| 1188 |
} |
| 1189 |
Err(e) => panic!("{}", format!("Parse error: {e:?}")), |
| 1190 |
_ => panic!("Something bad happened"), |
| 1191 |
} |
| 1192 |
} |
| 1193 |
|
| 1194 |
#[test] |
| 1195 |
fn test_stack_cfi() { |
| 1196 |
let line = b"STACK CFI deadf00d some rules\n"; |
| 1197 |
let rest = &b""[..]; |
| 1198 |
assert_eq!( |
| 1199 |
stack_cfi(line), |
| 1200 |
Ok(( |
| 1201 |
rest, |
| 1202 |
CfiRules { |
| 1203 |
address: 0xdeadf00d, |
| 1204 |
rules: "some rules".to_string(), |
| 1205 |
} |
| 1206 |
)) |
| 1207 |
); |
| 1208 |
} |
| 1209 |
|
| 1210 |
#[test] |
| 1211 |
fn test_stack_cfi_init() { |
| 1212 |
let line = b"STACK CFI INIT badf00d abc init rules\n"; |
| 1213 |
let rest = &b""[..]; |
| 1214 |
assert_eq!( |
| 1215 |
stack_cfi_init(line), |
| 1216 |
Ok(( |
| 1217 |
rest, |
| 1218 |
StackInfoCfi { |
| 1219 |
init: CfiRules { |
| 1220 |
address: 0xbadf00d, |
| 1221 |
rules: "init rules".to_string(), |
| 1222 |
}, |
| 1223 |
size: 0xabc, |
| 1224 |
add_rules: vec![], |
| 1225 |
} |
| 1226 |
)) |
| 1227 |
); |
| 1228 |
} |
| 1229 |
|
| 1230 |
#[test] |
| 1231 |
fn test_stack_cfi_lines() { |
| 1232 |
let data = b"STACK CFI INIT badf00d abc init rules |
| 1233 |
STACK CFI deadf00d some rules |
| 1234 |
STACK CFI deadbeef more rules |
| 1235 |
|
| 1236 |
"; |
| 1237 |
let file = SymbolFile::from_bytes(data).expect("failed to parse!"); |
| 1238 |
let (_, cfi) = file.cfi_stack_info.ranges_values().next().unwrap(); |
| 1239 |
assert_eq!( |
| 1240 |
cfi, |
| 1241 |
&StackInfoCfi { |
| 1242 |
init: CfiRules { |
| 1243 |
address: 0xbadf00d, |
| 1244 |
rules: "init rules".to_string(), |
| 1245 |
}, |
| 1246 |
size: 0xabc, |
| 1247 |
add_rules: vec![ |
| 1248 |
CfiRules { |
| 1249 |
address: 0xdeadbeef, |
| 1250 |
rules: "more rules".to_string(), |
| 1251 |
}, |
| 1252 |
CfiRules { |
| 1253 |
address: 0xdeadf00d, |
| 1254 |
rules: "some rules".to_string(), |
| 1255 |
}, |
| 1256 |
], |
| 1257 |
} |
| 1258 |
); |
| 1259 |
} |
| 1260 |
|
| 1261 |
#[test] |
| 1262 |
fn test_parse_symbol_bytes() { |
| 1263 |
let bytes = &b"MODULE Linux x86 D3096ED481217FD4C16B29CD9BC208BA0 firefox-bin |
| 1264 |
INFO blah blah blah |
| 1265 |
FILE 0 foo.c |
| 1266 |
FILE 100 bar.c |
| 1267 |
PUBLIC abcd 10 func 1 |
| 1268 |
PUBLIC ff00 3 func 2 |
| 1269 |
FUNC 900 30 10 some other func |
| 1270 |
FUNC 1000 30 10 some func |
| 1271 |
1000 10 42 7 |
| 1272 |
1010 10 52 8 |
| 1273 |
1020 10 62 15 |
| 1274 |
FUNC 1100 30 10 a third func |
| 1275 |
STACK WIN 4 900 30 a1 b2 c3 d4 e5 f6 1 prog string |
| 1276 |
STACK WIN 0 1000 30 a1 b2 c3 d4 e5 f6 0 1 |
| 1277 |
STACK CFI INIT badf00d abc init rules |
| 1278 |
STACK CFI deadf00d some rules |
| 1279 |
STACK CFI deadbeef more rules |
| 1280 |
STACK CFI INIT f00f f0 more init rules |
| 1281 |
|
| 1282 |
"[..]; |
| 1283 |
let sym = parse_symbol_bytes(bytes).unwrap(); |
| 1284 |
assert_eq!(sym.files.len(), 2); |
| 1285 |
assert_eq!(sym.files.get(&0).unwrap(), "foo.c"); |
| 1286 |
assert_eq!(sym.files.get(&100).unwrap(), "bar.c"); |
| 1287 |
assert_eq!(sym.publics.len(), 2); |
| 1288 |
{ |
| 1289 |
let p = &sym.publics[0]; |
| 1290 |
assert_eq!(p.address, 0xabcd); |
| 1291 |
assert_eq!(p.parameter_size, 0x10); |
| 1292 |
assert_eq!(p.name, "func 1".to_string()); |
| 1293 |
} |
| 1294 |
{ |
| 1295 |
let p = &sym.publics[1]; |
| 1296 |
assert_eq!(p.address, 0xff00); |
| 1297 |
assert_eq!(p.parameter_size, 0x3); |
| 1298 |
assert_eq!(p.name, "func 2".to_string()); |
| 1299 |
} |
| 1300 |
assert_eq!(sym.functions.ranges_values().count(), 3); |
| 1301 |
let funcs = sym |
| 1302 |
.functions |
| 1303 |
.ranges_values() |
| 1304 |
.map(|(_, f)| f) |
| 1305 |
.collect::<Vec<_>>(); |
| 1306 |
{ |
| 1307 |
let f = &funcs[0]; |
| 1308 |
assert_eq!(f.address, 0x900); |
| 1309 |
assert_eq!(f.size, 0x30); |
| 1310 |
assert_eq!(f.parameter_size, 0x10); |
| 1311 |
assert_eq!(f.name, "some other func".to_string()); |
| 1312 |
assert_eq!(f.lines.ranges_values().count(), 0); |
| 1313 |
} |
| 1314 |
{ |
| 1315 |
let f = &funcs[1]; |
| 1316 |
assert_eq!(f.address, 0x1000); |
| 1317 |
assert_eq!(f.size, 0x30); |
| 1318 |
assert_eq!(f.parameter_size, 0x10); |
| 1319 |
assert_eq!(f.name, "some func".to_string()); |
| 1320 |
assert_eq!( |
| 1321 |
f.lines.ranges_values().collect::<Vec<_>>(), |
| 1322 |
vec![ |
| 1323 |
&( |
| 1324 |
Range::new(0x1000, 0x100F), |
| 1325 |
SourceLine { |
| 1326 |
address: 0x1000, |
| 1327 |
size: 0x10, |
| 1328 |
file: 7, |
| 1329 |
line: 42, |
| 1330 |
}, |
| 1331 |
), |
| 1332 |
&( |
| 1333 |
Range::new(0x1010, 0x101F), |
| 1334 |
SourceLine { |
| 1335 |
address: 0x1010, |
| 1336 |
size: 0x10, |
| 1337 |
file: 8, |
| 1338 |
line: 52, |
| 1339 |
}, |
| 1340 |
), |
| 1341 |
&( |
| 1342 |
Range::new(0x1020, 0x102F), |
| 1343 |
SourceLine { |
| 1344 |
address: 0x1020, |
| 1345 |
size: 0x10, |
| 1346 |
file: 15, |
| 1347 |
line: 62, |
| 1348 |
}, |
| 1349 |
), |
| 1350 |
] |
| 1351 |
); |
| 1352 |
} |
| 1353 |
{ |
| 1354 |
let f = &funcs[2]; |
| 1355 |
assert_eq!(f.address, 0x1100); |
| 1356 |
assert_eq!(f.size, 0x30); |
| 1357 |
assert_eq!(f.parameter_size, 0x10); |
| 1358 |
assert_eq!(f.name, "a third func".to_string()); |
| 1359 |
assert_eq!(f.lines.ranges_values().count(), 0); |
| 1360 |
} |
| 1361 |
assert_eq!(sym.win_stack_framedata_info.ranges_values().count(), 1); |
| 1362 |
let ws = sym |
| 1363 |
.win_stack_framedata_info |
| 1364 |
.ranges_values() |
| 1365 |
.map(|(_, s)| s) |
| 1366 |
.collect::<Vec<_>>(); |
| 1367 |
{ |
| 1368 |
let stack = &ws[0]; |
| 1369 |
assert_eq!(stack.address, 0x900); |
| 1370 |
assert_eq!(stack.size, 0x30); |
| 1371 |
assert_eq!(stack.prologue_size, 0xa1); |
| 1372 |
assert_eq!(stack.epilogue_size, 0xb2); |
| 1373 |
assert_eq!(stack.parameter_size, 0xc3); |
| 1374 |
assert_eq!(stack.saved_register_size, 0xd4); |
| 1375 |
assert_eq!(stack.local_size, 0xe5); |
| 1376 |
assert_eq!(stack.max_stack_size, 0xf6); |
| 1377 |
assert_eq!( |
| 1378 |
stack.program_string_or_base_pointer, |
| 1379 |
WinStackThing::ProgramString("prog string".to_string()) |
| 1380 |
); |
| 1381 |
} |
| 1382 |
assert_eq!(sym.win_stack_fpo_info.ranges_values().count(), 1); |
| 1383 |
let ws = sym |
| 1384 |
.win_stack_fpo_info |
| 1385 |
.ranges_values() |
| 1386 |
.map(|(_, s)| s) |
| 1387 |
.collect::<Vec<_>>(); |
| 1388 |
{ |
| 1389 |
let stack = &ws[0]; |
| 1390 |
assert_eq!(stack.address, 0x1000); |
| 1391 |
assert_eq!(stack.size, 0x30); |
| 1392 |
assert_eq!(stack.prologue_size, 0xa1); |
| 1393 |
assert_eq!(stack.epilogue_size, 0xb2); |
| 1394 |
assert_eq!(stack.parameter_size, 0xc3); |
| 1395 |
assert_eq!(stack.saved_register_size, 0xd4); |
| 1396 |
assert_eq!(stack.local_size, 0xe5); |
| 1397 |
assert_eq!(stack.max_stack_size, 0xf6); |
| 1398 |
assert_eq!( |
| 1399 |
stack.program_string_or_base_pointer, |
| 1400 |
WinStackThing::AllocatesBasePointer(true) |
| 1401 |
); |
| 1402 |
} |
| 1403 |
assert_eq!(sym.cfi_stack_info.ranges_values().count(), 2); |
| 1404 |
let cs = sym |
| 1405 |
.cfi_stack_info |
| 1406 |
.ranges_values() |
| 1407 |
.map(|(_, s)| s.clone()) |
| 1408 |
.collect::<Vec<_>>(); |
| 1409 |
assert_eq!( |
| 1410 |
cs[0], |
| 1411 |
StackInfoCfi { |
| 1412 |
init: CfiRules { |
| 1413 |
address: 0xf00f, |
| 1414 |
rules: "more init rules".to_string(), |
| 1415 |
}, |
| 1416 |
size: 0xf0, |
| 1417 |
add_rules: vec![], |
| 1418 |
} |
| 1419 |
); |
| 1420 |
assert_eq!( |
| 1421 |
cs[1], |
| 1422 |
StackInfoCfi { |
| 1423 |
init: CfiRules { |
| 1424 |
address: 0xbadf00d, |
| 1425 |
rules: "init rules".to_string(), |
| 1426 |
}, |
| 1427 |
size: 0xabc, |
| 1428 |
add_rules: vec![ |
| 1429 |
CfiRules { |
| 1430 |
address: 0xdeadbeef, |
| 1431 |
rules: "more rules".to_string(), |
| 1432 |
}, |
| 1433 |
CfiRules { |
| 1434 |
address: 0xdeadf00d, |
| 1435 |
rules: "some rules".to_string(), |
| 1436 |
}, |
| 1437 |
], |
| 1438 |
} |
| 1439 |
); |
| 1440 |
} |
| 1441 |
|
| 1442 |
/// Test that parsing a symbol file with overlapping FUNC/line data works. |
| 1443 |
#[test] |
| 1444 |
fn test_parse_with_overlap() { |
| 1445 |
//TODO: deal with duplicate PUBLIC records? Not as important since they don't go |
| 1446 |
// into a RangeMap. |
| 1447 |
let bytes = b"MODULE Linux x86 D3096ED481217FD4C16B29CD9BC208BA0 firefox-bin |
| 1448 |
FILE 0 foo.c |
| 1449 |
PUBLIC abcd 10 func 1 |
| 1450 |
PUBLIC ff00 3 func 2 |
| 1451 |
FUNC 1000 30 10 some func |
| 1452 |
1000 10 42 0 |
| 1453 |
1000 10 43 0 |
| 1454 |
1001 10 44 0 |
| 1455 |
1001 5 45 0 |
| 1456 |
1010 10 52 0 |
| 1457 |
FUNC 1000 30 10 some func overlap exact |
| 1458 |
FUNC 1001 30 10 some func overlap end |
| 1459 |
FUNC 1001 10 10 some func overlap contained |
| 1460 |
"; |
| 1461 |
let sym = parse_symbol_bytes(&bytes[..]).unwrap(); |
| 1462 |
assert_eq!(sym.publics.len(), 2); |
| 1463 |
{ |
| 1464 |
let p = &sym.publics[0]; |
| 1465 |
assert_eq!(p.address, 0xabcd); |
| 1466 |
assert_eq!(p.parameter_size, 0x10); |
| 1467 |
assert_eq!(p.name, "func 1".to_string()); |
| 1468 |
} |
| 1469 |
{ |
| 1470 |
let p = &sym.publics[1]; |
| 1471 |
assert_eq!(p.address, 0xff00); |
| 1472 |
assert_eq!(p.parameter_size, 0x3); |
| 1473 |
assert_eq!(p.name, "func 2".to_string()); |
| 1474 |
} |
| 1475 |
assert_eq!(sym.functions.ranges_values().count(), 1); |
| 1476 |
let funcs = sym |
| 1477 |
.functions |
| 1478 |
.ranges_values() |
| 1479 |
.map(|(_, f)| f) |
| 1480 |
.collect::<Vec<_>>(); |
| 1481 |
{ |
| 1482 |
let f = &funcs[0]; |
| 1483 |
assert_eq!(f.address, 0x1000); |
| 1484 |
assert_eq!(f.size, 0x30); |
| 1485 |
assert_eq!(f.parameter_size, 0x10); |
| 1486 |
assert_eq!(f.name, "some func".to_string()); |
| 1487 |
assert_eq!( |
| 1488 |
f.lines.ranges_values().collect::<Vec<_>>(), |
| 1489 |
vec![ |
| 1490 |
&( |
| 1491 |
Range::new(0x1000, 0x100F), |
| 1492 |
SourceLine { |
| 1493 |
address: 0x1000, |
| 1494 |
size: 0x10, |
| 1495 |
file: 0, |
| 1496 |
line: 42, |
| 1497 |
}, |
| 1498 |
), |
| 1499 |
&( |
| 1500 |
Range::new(0x1010, 0x101F), |
| 1501 |
SourceLine { |
| 1502 |
address: 0x1010, |
| 1503 |
size: 0x10, |
| 1504 |
file: 0, |
| 1505 |
line: 52, |
| 1506 |
}, |
| 1507 |
), |
| 1508 |
] |
| 1509 |
); |
| 1510 |
} |
| 1511 |
} |
| 1512 |
|
| 1513 |
#[test] |
| 1514 |
fn test_parse_symbol_bytes_malformed() { |
| 1515 |
assert!( |
| 1516 |
parse_symbol_bytes(&b"this is not a symbol file\n"[..]).is_err(), |
| 1517 |
"Should fail to parse junk" |
| 1518 |
); |
| 1519 |
|
| 1520 |
assert!( |
| 1521 |
parse_symbol_bytes( |
| 1522 |
&b"MODULE Linux x86 xxxxxx |
| 1523 |
FILE 0 foo.c |
| 1524 |
"[..] |
| 1525 |
) |
| 1526 |
.is_err(), |
| 1527 |
"Should fail to parse malformed MODULE line" |
| 1528 |
); |
| 1529 |
|
| 1530 |
assert!( |
| 1531 |
parse_symbol_bytes( |
| 1532 |
&b"MODULE Linux x86 abcd1234 foo |
| 1533 |
FILE x foo.c |
| 1534 |
"[..] |
| 1535 |
) |
| 1536 |
.is_err(), |
| 1537 |
"Should fail to parse malformed FILE line" |
| 1538 |
); |
| 1539 |
|
| 1540 |
assert!( |
| 1541 |
parse_symbol_bytes( |
| 1542 |
&b"MODULE Linux x86 abcd1234 foo |
| 1543 |
FUNC xx 1 2 foo |
| 1544 |
"[..] |
| 1545 |
) |
| 1546 |
.is_err(), |
| 1547 |
"Should fail to parse malformed FUNC line" |
| 1548 |
); |
| 1549 |
|
| 1550 |
assert!( |
| 1551 |
parse_symbol_bytes( |
| 1552 |
&b"MODULE Linux x86 abcd1234 foo |
| 1553 |
this is some junk |
| 1554 |
"[..] |
| 1555 |
) |
| 1556 |
.is_err(), |
| 1557 |
"Should fail to parse malformed file" |
| 1558 |
); |
| 1559 |
|
| 1560 |
assert!( |
| 1561 |
parse_symbol_bytes( |
| 1562 |
&b"MODULE Linux x86 abcd1234 foo |
| 1563 |
FILE 0 foo.c |
| 1564 |
FILE"[..] |
| 1565 |
) |
| 1566 |
.is_err(), |
| 1567 |
"Should fail to parse truncated file" |
| 1568 |
); |
| 1569 |
|
| 1570 |
assert!( |
| 1571 |
parse_symbol_bytes(&b""[..]).is_err(), |
| 1572 |
"Should fail to parse empty file" |
| 1573 |
); |
| 1574 |
} |
| 1575 |
|
| 1576 |
#[test] |
| 1577 |
fn test_parse_stack_win_inconsistent() { |
| 1578 |
// Various cases where the has_program_string value is inconsistent |
| 1579 |
// with the type of the STACK WIN entry. |
| 1580 |
// |
| 1581 |
// type=0 (FPO) should go with has_program_string==0 (false) |
| 1582 |
// type=4 (FrameData) should go with has_program_string==1 (true) |
| 1583 |
// |
| 1584 |
// Only 4d93e and 8d93e are totally valid. |
| 1585 |
// |
| 1586 |
// Current policy is to discard all the other ones, but all the cases |
| 1587 |
// are here in case we decide on a more sophisticated heuristic. |
| 1588 |
|
| 1589 |
let bytes = b"MODULE Windows x86 D3096ED481217FD4C16B29CD9BC208BA0 firefox-bin |
| 1590 |
FILE 0 foo.c |
| 1591 |
STACK WIN 0 1d93e 4 4 0 0 10 0 0 1 1 |
| 1592 |
STACK WIN 0 2d93e 4 4 0 0 10 0 0 1 0 |
| 1593 |
STACK WIN 0 3d93e 4 4 0 0 10 0 0 1 prog string |
| 1594 |
STACK WIN 0 4d93e 4 4 0 0 10 0 0 0 1 |
| 1595 |
STACK WIN 4 5d93e 4 4 0 0 10 0 0 0 1 |
| 1596 |
STACK WIN 4 6d93e 4 4 0 0 10 0 0 0 0 |
| 1597 |
STACK WIN 4 7d93e 4 4 0 0 10 0 0 0 prog string |
| 1598 |
STACK WIN 4 8d93e 4 4 0 0 10 0 0 1 prog string |
| 1599 |
"; |
| 1600 |
let sym = parse_symbol_bytes(&bytes[..]).unwrap(); |
| 1601 |
|
| 1602 |
assert_eq!(sym.win_stack_framedata_info.ranges_values().count(), 1); |
| 1603 |
let ws = sym |
| 1604 |
.win_stack_framedata_info |
| 1605 |
.ranges_values() |
| 1606 |
.map(|(_, s)| s) |
| 1607 |
.collect::<Vec<_>>(); |
| 1608 |
{ |
| 1609 |
let stack = &ws[0]; |
| 1610 |
assert_eq!(stack.address, 0x8d93e); |
| 1611 |
assert_eq!(stack.size, 0x4); |
| 1612 |
assert_eq!(stack.prologue_size, 0x4); |
| 1613 |
assert_eq!(stack.epilogue_size, 0); |
| 1614 |
assert_eq!(stack.parameter_size, 0); |
| 1615 |
assert_eq!(stack.saved_register_size, 0x10); |
| 1616 |
assert_eq!(stack.local_size, 0); |
| 1617 |
assert_eq!(stack.max_stack_size, 0); |
| 1618 |
assert_eq!( |
| 1619 |
stack.program_string_or_base_pointer, |
| 1620 |
WinStackThing::ProgramString("prog string".to_string()) |
| 1621 |
); |
| 1622 |
} |
| 1623 |
assert_eq!(sym.win_stack_fpo_info.ranges_values().count(), 1); |
| 1624 |
let ws = sym |
| 1625 |
.win_stack_fpo_info |
| 1626 |
.ranges_values() |
| 1627 |
.map(|(_, s)| s) |
| 1628 |
.collect::<Vec<_>>(); |
| 1629 |
{ |
| 1630 |
let stack = &ws[0]; |
| 1631 |
assert_eq!(stack.address, 0x4d93e); |
| 1632 |
assert_eq!(stack.size, 0x4); |
| 1633 |
assert_eq!(stack.prologue_size, 0x4); |
| 1634 |
assert_eq!(stack.epilogue_size, 0); |
| 1635 |
assert_eq!(stack.parameter_size, 0); |
| 1636 |
assert_eq!(stack.saved_register_size, 0x10); |
| 1637 |
assert_eq!(stack.local_size, 0); |
| 1638 |
assert_eq!(stack.max_stack_size, 0); |
| 1639 |
assert_eq!( |
| 1640 |
stack.program_string_or_base_pointer, |
| 1641 |
WinStackThing::AllocatesBasePointer(true) |
| 1642 |
); |
| 1643 |
} |
| 1644 |
} |
| 1645 |
|
| 1646 |
#[test] |
| 1647 |
fn address_size_overflow() { |
| 1648 |
let bytes = b"FUNC 1 2 3 x\nffffffffffffffff 2 0 0\n"; |
| 1649 |
let sym = parse_symbol_bytes(bytes.as_slice()).unwrap(); |
| 1650 |
let fun = sym.functions.get(1).unwrap(); |
| 1651 |
assert!(fun.lines.is_empty()); |
| 1652 |
assert!(fun.name == "x"); |
| 1653 |
} |