-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathparser.rs
More file actions
528 lines (487 loc) · 21 KB
/
parser.rs
File metadata and controls
528 lines (487 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// Copyright 2022-2026 Vector 35 Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::{BTreeMap, HashMap, HashSet};
use std::env;
use std::fmt::Display;
use std::sync::OnceLock;
use anyhow::{anyhow, Result};
use pdb::*;
use crate::symbol_parser::{ParsedDataSymbol, ParsedProcedure, ParsedSymbol};
use crate::type_parser::ParsedType;
use binaryninja::architecture::{Architecture, CoreArchitecture};
use binaryninja::binary_view::BinaryView;
use binaryninja::calling_convention::CoreCallingConvention;
use binaryninja::confidence::{Conf, MIN_CONFIDENCE};
use binaryninja::debuginfo::{DebugFunctionInfo, DebugInfo};
use binaryninja::platform::Platform;
use binaryninja::rc::Ref;
use binaryninja::settings::{QueryOptions, Settings};
use binaryninja::types::{
EnumerationBuilder, NamedTypeReference, NamedTypeReferenceClass, StructureBuilder,
StructureType, Type, TypeClass,
};
use binaryninja::variable::{NamedDataVariableWithType, NamedVariableWithType};
/// Megastruct for all the parsing
/// Certain fields are only used by specific files, as marked below.
/// Why not make new structs for them? Because vvvv this garbage
pub struct PDBParserInstance<'a, S: Source<'a> + 'a> {
/// DebugInfo where types/functions will be stored eventually
pub(crate) debug_info: &'a mut DebugInfo,
/// Parent binary view (usually during BinaryView::Finalize)
pub(crate) bv: &'a BinaryView,
/// Default arch of self.bv
pub(crate) arch: CoreArchitecture,
/// Default calling convention for self.arch
pub(crate) default_cc: Ref<CoreCallingConvention>,
/// Thiscall calling convention for self.bv, or default_cc if we can't find one
pub(crate) thiscall_cc: Ref<CoreCallingConvention>,
/// Cdecl calling convention for self.bv, or default_cc if we can't find one
pub(crate) cdecl_cc: Ref<CoreCallingConvention>,
/// Default platform of self.bv
pub(crate) platform: Ref<Platform>,
/// pdb-rs structure for making lifetime hell a real place
pub(crate) pdb: PDB<'a, S>,
/// pdb-rs Mapping of modules to addresses for resolving RVAs
pub(crate) address_map: AddressMap<'a>,
/// Binja Settings instance (for optimization)
pub(crate) settings: Ref<Settings>,
/// Binja Settings query instance (for optimization)
pub(crate) settings_query_opts: QueryOptions<'a>,
/// type_parser.rs
/// TypeIndex -> ParsedType enum used during parsing
pub(crate) indexed_types: BTreeMap<TypeIndex, ParsedType>,
/// QName -> Binja Type for finished types
pub(crate) named_types: BTreeMap<String, Ref<Type>>,
/// Raw (mangled) name -> TypeIndex for resolving forward references
pub(crate) full_type_indices: BTreeMap<String, TypeIndex>,
/// Stack of types we're currently parsing
pub(crate) type_stack: Vec<TypeIndex>,
/// Stack of parent types we're parsing nested types inside of
pub(crate) namespace_stack: Vec<String>,
/// Type Index -> Does it return on the stack
pub(crate) type_default_returnable: BTreeMap<TypeIndex, bool>,
/// symbol_parser.rs
/// List of fully parsed symbols from all modules
pub(crate) parsed_symbols: Vec<ParsedSymbol>,
/// Raw name -> index in parsed_symbols
pub(crate) parsed_symbols_by_name: BTreeMap<String, usize>,
/// Raw name -> Symbol index for looking up symbols for the currently parsing module (mostly for thunks)
pub(crate) named_symbols: BTreeMap<String, SymbolIndex>,
/// Parent -> Children symbol index tree for the currently parsing module
pub(crate) symbol_tree: BTreeMap<SymbolIndex, Vec<SymbolIndex>>,
/// Child -> Parent symbol index mapping, inverse of symbol_tree
pub(crate) symbol_parents: BTreeMap<SymbolIndex, SymbolIndex>,
/// Stack of (start, end) indices for the current symbols being parsed while constructing the tree
pub(crate) symbol_stack: Vec<(SymbolIndex, SymbolIndex)>,
/// Index -> parsed symbol for the currently parsing module
pub(crate) indexed_symbols: BTreeMap<SymbolIndex, ParsedSymbol>,
/// Symbol address -> Symbol for looking up by address
pub(crate) addressed_symbols: BTreeMap<u64, Vec<ParsedSymbol>>,
/// CPU type of the currently parsing module
pub(crate) module_cpu_type: Option<CPUType>,
}
impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
/// Try to create a new parser instance from a given bv/pdb
pub fn new(
debug_info: &'a mut DebugInfo,
bv: &'a BinaryView,
mut pdb: PDB<'a, S>,
) -> Result<Self> {
let arch = if let Some(arch) = bv.default_arch() {
arch
} else {
return Err(anyhow!("Cannot parse to view with no architecture"));
};
let platform = bv
.default_platform()
.expect("Expected bv to have a platform");
let address_map = pdb.address_map()?;
let default_cc = platform
.get_default_calling_convention()
.expect("Expected default calling convention");
let thiscall_cc = Self::find_calling_convention(platform.as_ref(), "thiscall")
.unwrap_or(default_cc.clone());
let cdecl_cc = platform
.get_cdecl_calling_convention()
.unwrap_or(default_cc.clone());
Ok(Self {
debug_info,
bv,
arch,
default_cc,
thiscall_cc,
cdecl_cc,
platform,
pdb,
address_map,
settings: Settings::new(),
settings_query_opts: QueryOptions::new_with_view(bv),
indexed_types: Default::default(),
named_types: Default::default(),
full_type_indices: Default::default(),
type_stack: Default::default(),
namespace_stack: Default::default(),
type_default_returnable: Default::default(),
parsed_symbols: Default::default(),
parsed_symbols_by_name: Default::default(),
named_symbols: Default::default(),
symbol_tree: Default::default(),
symbol_parents: Default::default(),
symbol_stack: Default::default(),
indexed_symbols: Default::default(),
addressed_symbols: Default::default(),
module_cpu_type: None,
})
}
/// Try to parse the pdb into the DebugInfo
pub fn try_parse_info(
&mut self,
progress: Box<dyn Fn(usize, usize) -> Result<()> + 'a>,
) -> Result<()> {
self.parse_types(Self::split_progress(&progress, 0, &[1.0, 3.0, 0.5, 0.5]))?;
for (name, ty) in self.named_types.iter() {
self.debug_info
.add_type(&name.to_string(), ty.as_ref(), &[]); // TODO : Components
}
tracing::info!(
"PDB found {} types (before resolving NTRs)",
self.named_types.len()
);
if self
.settings
.get_bool_with_opts("pdb.features.parseSymbols", &mut self.settings_query_opts)
{
let (symbols, functions) =
self.parse_symbols(Self::split_progress(&progress, 1, &[1.0, 3.0, 0.5, 0.5]))?;
if self.settings.get_bool_with_opts(
"pdb.features.createMissingNamedTypes",
&mut self.settings_query_opts,
) {
self.resolve_missing_ntrs(
&symbols,
Self::split_progress(&progress, 2, &[1.0, 3.0, 0.5, 0.5]),
)?;
self.resolve_missing_ntrs(
&functions,
Self::split_progress(&progress, 3, &[1.0, 3.0, 0.5, 0.5]),
)?;
}
tracing::info!("PDB found {} types", self.named_types.len());
tracing::info!("PDB found {} data variables", symbols.len());
tracing::info!("PDB found {} functions", functions.len());
let allow_void = self.settings.get_bool_with_opts(
"pdb.features.allowVoidGlobals",
&mut self.settings_query_opts,
);
let min_confidence_type = Conf::new(Type::void(), MIN_CONFIDENCE);
for sym in symbols {
match sym {
ParsedSymbol::Data(ParsedDataSymbol {
address,
name,
type_,
..
}) => {
let real_type = type_.as_ref().unwrap_or(&min_confidence_type);
if real_type.contents.type_class() == TypeClass::VoidTypeClass {
if !allow_void {
self.log(|| {
format!("Not adding void-typed symbol {:?}@{:x}", name, address)
});
continue;
}
}
self.log(|| {
format!(
"Adding data variable: 0x{:x}: {} {:?}",
address, &name.raw_name, real_type
)
});
self.debug_info
.add_data_variable_info(NamedDataVariableWithType::new(
address,
real_type.clone(),
name.full_name.unwrap_or(name.raw_name),
true,
));
}
s => {
self.log(|| format!("Not adding non-data symbol {:?}", s));
}
}
}
for sym in functions {
match sym {
ParsedSymbol::Procedure(ParsedProcedure {
address,
name,
type_,
locals,
..
}) => {
self.log(|| {
format!(
"Adding function: 0x{:x}: {} {:?}",
address, &name.raw_name, type_
)
});
self.debug_info.add_function(&DebugFunctionInfo::new(
Some(name.short_name.unwrap_or(name.raw_name.clone())),
Some(name.full_name.unwrap_or(name.raw_name.clone())),
Some(name.raw_name),
type_.clone().and_then(|conf| {
// TODO: When DebugInfo support confidence on function types, remove this
if conf.confidence == 0 {
None
} else {
Some(conf.contents)
}
}),
Some(address),
Some(self.platform.clone()),
vec![], // TODO : Components
locals
.iter()
.filter_map(|v| {
let Some(var_type) = &v.type_ else {
return None;
};
if v.storage.len() != 1 {
// TODO: how should we handle variables with multiple storage locations?
return None;
}
let mut var_loc = v.storage[0].location;
if v.storage[0].base_relative {
var_loc.storage -= self.arch.address_size() as i64;
}
Some(NamedVariableWithType {
variable: var_loc,
ty: var_type.clone(),
name: v.name.clone(),
auto_defined: false,
})
})
.collect::<Vec<_>>(),
));
}
_ => {}
}
}
}
Ok(())
}
fn collect_name(
&self,
name: &NamedTypeReference,
unknown_names: &mut HashMap<String, NamedTypeReferenceClass>,
) {
let used_name = name.name().to_string();
if let Some(&found) = unknown_names.get(&used_name) {
if found != name.class() {
// Interesting case, not sure we care
self.log(|| {
format!(
"Mismatch unknown NTR class for {}: {} ?",
&used_name,
name.class() as u32
)
});
}
} else {
self.log(|| format!("Found new unused name: {}", &used_name));
unknown_names.insert(used_name, name.class());
}
}
fn collect_names(
&self,
ty: &Type,
unknown_names: &mut HashMap<String, NamedTypeReferenceClass>,
) {
match ty.type_class() {
TypeClass::StructureTypeClass => {
if let Some(structure) = ty.get_structure() {
for member in structure.members() {
self.collect_names(member.ty.contents.as_ref(), unknown_names);
}
for base in structure.base_structures() {
self.collect_name(base.ty.as_ref(), unknown_names);
}
}
}
TypeClass::PointerTypeClass => {
if let Some(target) = ty.target() {
self.collect_names(target.contents.as_ref(), unknown_names);
}
}
TypeClass::ArrayTypeClass => {
if let Some(element_type) = ty.element_type() {
self.collect_names(element_type.contents.as_ref(), unknown_names);
}
}
TypeClass::FunctionTypeClass => {
if let Some(return_value) = ty.return_value() {
self.collect_names(return_value.contents.as_ref(), unknown_names);
}
if let Some(params) = ty.parameters() {
for param in params {
self.collect_names(param.ty.contents.as_ref(), unknown_names);
}
}
}
TypeClass::NamedTypeReferenceClass => {
if let Some(ntr) = ty.get_named_type_reference() {
self.collect_name(ntr.as_ref(), unknown_names);
}
}
_ => {}
}
}
fn resolve_missing_ntrs(
&mut self,
symbols: &Vec<ParsedSymbol>,
progress: Box<dyn Fn(usize, usize) -> Result<()> + '_>,
) -> Result<()> {
let mut unknown_names = HashMap::new();
let mut known_names = self
.bv
.types()
.iter()
.map(|qnat| qnat.name.to_string())
.collect::<HashSet<_>>();
for ty in &self.named_types {
known_names.insert(ty.0.clone());
}
let count = symbols.len();
for (i, sym) in symbols.iter().enumerate() {
match sym {
ParsedSymbol::Data(ParsedDataSymbol {
type_: Some(type_), ..
}) => {
self.collect_names(type_.contents.as_ref(), &mut unknown_names);
}
ParsedSymbol::Procedure(ParsedProcedure {
type_: Some(type_),
locals,
..
}) => {
self.collect_names(type_.contents.as_ref(), &mut unknown_names);
for l in locals {
if let Some(ltype) = &l.type_ {
self.collect_names(ltype.contents.as_ref(), &mut unknown_names);
}
}
}
_ => {}
}
progress(i, count)?;
}
for (name, class) in unknown_names.into_iter() {
if known_names.contains(&name) {
self.log(|| format!("Found referenced name and ignoring: {}", &name));
continue;
}
self.log(|| format!("Adding referenced but unknown type {} (likely due to demangled name and stripped type)", &name));
match class {
NamedTypeReferenceClass::UnknownNamedTypeClass
| NamedTypeReferenceClass::TypedefNamedTypeClass => {
self.debug_info
.add_type(&name.to_string(), Type::void().as_ref(), &[]);
// TODO : Components
}
NamedTypeReferenceClass::ClassNamedTypeClass
| NamedTypeReferenceClass::StructNamedTypeClass
| NamedTypeReferenceClass::UnionNamedTypeClass => {
let mut structure = StructureBuilder::new();
match class {
NamedTypeReferenceClass::ClassNamedTypeClass => {
structure.structure_type(StructureType::ClassStructureType);
}
NamedTypeReferenceClass::StructNamedTypeClass => {
structure.structure_type(StructureType::StructStructureType);
}
NamedTypeReferenceClass::UnionNamedTypeClass => {
structure.structure_type(StructureType::UnionStructureType);
}
_ => {}
}
structure.width(1);
structure.alignment(1);
self.debug_info.add_type(
&name.to_string(),
Type::structure(structure.finalize().as_ref()).as_ref(),
&[], // TODO : Components
);
}
NamedTypeReferenceClass::EnumNamedTypeClass => {
let enumeration = EnumerationBuilder::new();
self.debug_info.add_type(
&name.to_string(),
Type::enumeration(
enumeration.finalize().as_ref(),
self.arch.default_integer_size().try_into()?,
false,
)
.as_ref(),
&[], // TODO : Components
);
}
}
}
Ok(())
}
/// Lazy logging function that prints like 20MB of messages
pub(crate) fn log<F: FnOnce() -> D, D: Display>(&self, msg: F) {
static MEM: OnceLock<bool> = OnceLock::new();
let debug_pdb = MEM.get_or_init(|| env::var("BN_DEBUG_PDB").is_ok());
if *debug_pdb {
let space = "\t".repeat(self.type_stack.len()) + &"\t".repeat(self.symbol_stack.len());
let msg = format!("{}", msg());
tracing::debug!(
"{}{}",
space,
msg.replace("\n", &("\n".to_string() + &space))
);
}
}
pub(crate) fn split_progress<'b, F: Fn(usize, usize) -> Result<()> + 'b>(
original_fn: F,
subpart: usize,
subpart_weights: &[f64],
) -> Box<dyn Fn(usize, usize) -> Result<()> + 'b> {
// Normalize weights
let weight_sum: f64 = subpart_weights.iter().sum();
if weight_sum < 0.0001 {
return Box::new(|_, _| Ok(()));
}
// Keep a running count of weights for the start
let mut subpart_starts = vec![];
let mut start = 0f64;
for w in subpart_weights {
subpart_starts.push(start);
start += *w;
}
let subpart_start = subpart_starts[subpart] / weight_sum;
let weight = subpart_weights[subpart] / weight_sum;
Box::new(move |cur: usize, max: usize| {
// Just use a large number for easy divisibility
let steps = 1000000f64;
let subpart_size = steps * weight;
let subpart_progress = ((cur as f64) / (max as f64)) * subpart_size;
original_fn(
(subpart_start * steps + subpart_progress) as usize,
steps as usize,
)
})
}
}