Skip to content

Commit 8c098d2

Browse files
committed
More rust cleanup
- Fixup usage of QualifiedName in plugins - Make QualifiedName more usable
1 parent e3d59cc commit 8c098d2

File tree

15 files changed

+307
-215
lines changed

15 files changed

+307
-215
lines changed

plugins/dwarf/dwarf_export/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fn export_type(
131131
AttributeValue::Data2(t.width() as u16),
132132
);
133133

134-
for struct_member in t.get_structure().unwrap().members().unwrap() {
134+
for struct_member in t.get_structure().unwrap().members() {
135135
let struct_member_die_uid =
136136
dwarf.unit.add(structure_die_uid, constants::DW_TAG_member);
137137
dwarf.unit.get_mut(struct_member_die_uid).set(
@@ -378,8 +378,8 @@ fn export_types(
378378
) {
379379
for t in &bv.types() {
380380
export_type(
381-
t.name().to_string(),
382-
&t.type_object(),
381+
t.name.to_string(),
382+
&t.ty,
383383
bv,
384384
defined_types,
385385
dwarf,

plugins/dwarf/dwarf_import/src/die_handlers.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub(crate) fn handle_enum<R: ReaderType>(
142142
pub(crate) fn handle_typedef(
143143
debug_info_builder: &mut DebugInfoBuilder,
144144
entry_type: Option<TypeUID>,
145-
typedef_name: &String,
145+
typedef_name: &str,
146146
) -> (Option<Ref<Type>>, bool) {
147147
// All base types have:
148148
// DW_AT_name
@@ -152,7 +152,7 @@ pub(crate) fn handle_typedef(
152152
// This will fail in the case where we have a typedef to a type that doesn't exist (failed to parse, incomplete, etc)
153153
if let Some(entry_type_offset) = entry_type {
154154
if let Some(t) = debug_info_builder.get_type(entry_type_offset) {
155-
return (Some(t.get_type()), typedef_name != t.get_name());
155+
return (Some(t.get_type()), typedef_name != t.name);
156156
}
157157
}
158158

@@ -304,13 +304,14 @@ pub(crate) fn handle_function<R: ReaderType>(
304304

305305
// Alias function type in the case that it contains itself
306306
if let Some(name) = debug_info_builder_context.get_name(dwarf, unit, entry) {
307+
let ntr = Type::named_type_from_type(
308+
&name,
309+
&Type::function(return_type.as_ref(), &[], false),
310+
);
307311
debug_info_builder.add_type(
308312
get_uid(dwarf, unit, entry),
309-
&name,
310-
Type::named_type_from_type(
311-
&name,
312-
&Type::function::<&binaryninja::types::Type>(return_type.as_ref(), &[], false),
313-
),
313+
name,
314+
ntr,
314315
false,
315316
);
316317
}

plugins/dwarf/dwarf_import/src/dwarfdebuginfo.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,12 @@ impl FunctionInfoBuilder {
100100

101101
// TODO : Don't make this pub...fix the value thing
102102
pub(crate) struct DebugType {
103-
name: String,
104-
ty: Ref<Type>,
105-
commit: bool,
103+
pub name: String,
104+
pub ty: Ref<Type>,
105+
pub commit: bool,
106106
}
107107

108108
impl DebugType {
109-
pub fn get_name(&self) -> &String {
110-
&self.name
111-
}
112-
113109
pub fn get_type(&self) -> Ref<Type> {
114110
self.ty.clone()
115111
}
@@ -327,7 +323,7 @@ impl DebugInfoBuilder {
327323
self.types.values()
328324
}
329325

330-
pub(crate) fn add_type(&mut self, type_uid: TypeUID, name: &String, t: Ref<Type>, commit: bool) {
326+
pub(crate) fn add_type(&mut self, type_uid: TypeUID, name: String, t: Ref<Type>, commit: bool) {
331327
if let Some(DebugType {
332328
name: existing_name,
333329
ty: existing_type,
@@ -396,7 +392,7 @@ impl DebugInfoBuilder {
396392

397393
// Either get the known type or use a 0 confidence void type so we at least get the name applied
398394
let ty = match type_uid {
399-
Some(uid) => Conf::new(self.get_type(uid).unwrap().get_type(), 128),
395+
Some(uid) => Conf::new(self.get_type(uid).unwrap().ty.clone(), 128),
400396
None => Conf::new(Type::void(), 0)
401397
};
402398
let function = &mut self.functions[function_index];
@@ -465,8 +461,8 @@ impl DebugInfoBuilder {
465461
if let Some((_existing_name, existing_type_uid)) =
466462
self.data_variables.insert(address, (name, type_uid))
467463
{
468-
let existing_type = self.get_type(existing_type_uid).unwrap().get_type();
469-
let new_type = self.get_type(type_uid).unwrap().get_type();
464+
let existing_type = self.get_type(existing_type_uid).unwrap().ty.as_ref();
465+
let new_type = self.get_type(type_uid).unwrap().ty.as_ref();
470466

471467
if existing_type_uid != type_uid || existing_type != new_type {
472468
warn!("DWARF info contains duplicate data variable definition. Overwriting data variable at 0x{:08x} (`{}`) with `{}`",
@@ -549,7 +545,7 @@ impl DebugInfoBuilder {
549545

550546
fn get_function_type(&self, function: &FunctionInfoBuilder) -> Ref<Type> {
551547
let return_type = match function.return_type {
552-
Some(return_type_id) => Conf::new(self.get_type(return_type_id).unwrap().get_type(), 128),
548+
Some(return_type_id) => Conf::new(self.get_type(return_type_id).unwrap().ty.clone(), 128),
553549
_ => Conf::new(Type::void(), 0),
554550
};
555551

@@ -559,7 +555,7 @@ impl DebugInfoBuilder {
559555
.filter_map(|parameter| match parameter {
560556
Some((name, 0)) => Some(FunctionParameter::new(Type::void(), name.clone(), None)),
561557
Some((name, uid)) => Some(FunctionParameter::new(
562-
self.get_type(*uid).unwrap().get_type(),
558+
self.get_type(*uid).unwrap().ty.clone(),
563559
name.clone(),
564560
None,
565561
)),
@@ -610,8 +606,8 @@ impl DebugInfoBuilder {
610606
let symbol_full_name = symbol.full_name();
611607

612608
// If our name has fewer namespaces than the existing name, assume we lost the namespace info
613-
if simplify_str_to_fqn(func_full_name, true).len()
614-
< simplify_str_to_fqn(symbol_full_name.clone(), true).len()
609+
if simplify_str_to_fqn(func_full_name, true).items.len()
610+
< simplify_str_to_fqn(symbol_full_name.clone(), true).items.len()
615611
{
616612
func.full_name = Some(symbol_full_name.to_string());
617613
}

plugins/dwarf/dwarf_import/src/helpers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ pub(crate) fn get_build_id(view: &BinaryView) -> Result<String, String> {
408408
}
409409

410410

411-
pub(crate) fn download_debug_info(build_id: &String, view: &BinaryView) -> Result<Ref<BinaryView>, String> {
411+
pub(crate) fn download_debug_info(build_id: &str, view: &BinaryView) -> Result<Ref<BinaryView>, String> {
412412
let settings = Settings::new("");
413413

414414
let debug_server_urls = settings.get_string_list("network.debuginfodServers", Some(view), None);
@@ -479,7 +479,7 @@ pub(crate) fn download_debug_info(build_id: &String, view: &BinaryView) -> Resul
479479
}
480480

481481

482-
pub(crate) fn find_local_debug_file_for_build_id(build_id: &String, view: &BinaryView) -> Option<String> {
482+
pub(crate) fn find_local_debug_file_for_build_id(build_id: &str, view: &BinaryView) -> Option<String> {
483483
let settings = Settings::new("");
484484
let debug_dirs_enabled = settings.get_bool("analysis.debugInfo.enableDebugDirectories", Some(view), None);
485485

@@ -522,7 +522,7 @@ pub(crate) fn find_local_debug_file_for_build_id(build_id: &String, view: &Binar
522522
}
523523

524524

525-
pub(crate) fn load_debug_info_for_build_id(build_id: &String, view: &BinaryView) -> (Option<Ref<BinaryView>>, bool) {
525+
pub(crate) fn load_debug_info_for_build_id(build_id: &str, view: &BinaryView) -> (Option<Ref<BinaryView>>, bool) {
526526
if let Some(debug_file_path) = find_local_debug_file_for_build_id(build_id, view) {
527527
return
528528
(

plugins/dwarf/dwarf_import/src/types.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,24 +141,29 @@ fn do_structure_parse<R: ReaderType>(
141141
// This reference type will be used by any children to grab while we're still building this type
142142
// it will also be how any other types refer to this struct
143143
if let Some(full_name) = &full_name {
144+
let ntr = Type::named_type_from_type(
145+
full_name,
146+
&Type::structure(&structure_builder.finalize()),
147+
);
144148
debug_info_builder.add_type(
145149
get_uid(dwarf, unit, entry),
146-
&full_name,
147-
Type::named_type_from_type(
148-
full_name.clone(),
149-
&Type::structure(&structure_builder.finalize()),
150-
),
150+
full_name.to_owned(),
151+
ntr,
151152
false,
152153
);
153154
} else {
154155
// We _need_ to have initial typedefs or else we can enter infinite parsing loops
155156
// These get overwritten in the last step with the actual type, however, so this
156157
// is either perfectly fine or breaking a bunch of NTRs
157158
let full_name = format!("anonymous_structure_{:x}", get_uid(dwarf, unit, entry));
159+
let ntr = Type::named_type_from_type(
160+
&full_name,
161+
&Type::structure(&structure_builder.finalize()),
162+
);
158163
debug_info_builder.add_type(
159164
get_uid(dwarf, unit, entry),
160-
&full_name,
161-
Type::named_type_from_type(&full_name, &Type::structure(&structure_builder.finalize())),
165+
full_name,
166+
ntr,
162167
false,
163168
);
164169
}
@@ -224,14 +229,14 @@ fn do_structure_parse<R: ReaderType>(
224229
if let Some(full_name) = full_name {
225230
debug_info_builder.add_type(
226231
get_uid(dwarf, unit, entry) + 1, // TODO : This is super broke (uid + 1 is not guaranteed to be unique)
227-
&full_name,
232+
full_name,
228233
finalized_structure,
229234
true,
230235
);
231236
} else {
232237
debug_info_builder.add_type(
233238
get_uid(dwarf, unit, entry),
234-
&format!("{}", finalized_structure),
239+
finalized_structure.to_string(),
235240
finalized_structure,
236241
false, // Don't commit anonymous unions (because I think it'll break things)
237242
);
@@ -451,10 +456,10 @@ pub(crate) fn get_type<R: ReaderType>(
451456
}
452457
.unwrap_or_else(|| {
453458
commit = false;
454-
format!("{}", type_def)
459+
type_def.to_string()
455460
});
456461

457-
debug_info_builder.add_type(entry_uid, &name, type_def, commit);
462+
debug_info_builder.add_type(entry_uid, name, type_def, commit);
458463
Some(entry_uid)
459464
} else {
460465
None

plugins/idb_import/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,15 @@ impl<F: Fn(usize, usize) -> Result<(), ()>> TranslateIDBTypes<'_, F> {
162162
}
163163
TranslateTypeResult::PartiallyTranslated(og_ty, error) => {
164164
TranslateTypeResult::PartiallyTranslated(
165-
Type::named_type_from_type(&String::from_utf8_lossy(&ty.name), og_ty),
165+
Type::named_type_from_type(String::from_utf8_lossy(&ty.name), og_ty),
166166
error
167167
.as_ref()
168168
.map(|x| BnTypeError::Typedef(Box::new(x.clone())))
169169
.clone(),
170170
)
171171
}
172172
TranslateTypeResult::Translated(og_ty) => TranslateTypeResult::Translated(
173-
Type::named_type_from_type(&String::from_utf8_lossy(&ty.name), og_ty),
173+
Type::named_type_from_type(String::from_utf8_lossy(&ty.name), og_ty),
174174
),
175175
}
176176
}

plugins/pdb-ng/src/parser.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ use binaryninja::debuginfo::{DebugFunctionInfo, DebugInfo};
2929
use binaryninja::platform::Platform;
3030
use binaryninja::rc::Ref;
3131
use binaryninja::settings::Settings;
32-
use binaryninja::types::{
33-
EnumerationBuilder, NamedTypeReference,
34-
NamedTypeReferenceClass, StructureBuilder, StructureType, Type, TypeClass,
35-
};
32+
use binaryninja::types::{EnumerationBuilder, NamedTypeReference, NamedTypeReferenceClass, QualifiedName, StructureBuilder, StructureType, Type, TypeClass};
3633
use binaryninja::variable::NamedDataVariableWithType;
3734
use crate::symbol_parser::{ParsedDataSymbol, ParsedProcedure, ParsedSymbol};
3835
use crate::type_parser::ParsedType;
@@ -67,13 +64,13 @@ pub struct PDBParserInstance<'a, S: Source<'a> + 'a> {
6764
/// TypeIndex -> ParsedType enum used during parsing
6865
pub(crate) indexed_types: BTreeMap<TypeIndex, ParsedType>,
6966
/// QName -> Binja Type for finished types
70-
pub(crate) named_types: BTreeMap<String, Ref<Type>>,
67+
pub(crate) named_types: BTreeMap<QualifiedName, Ref<Type>>,
7168
/// Raw (mangled) name -> TypeIndex for resolving forward references
7269
pub(crate) full_type_indices: BTreeMap<String, TypeIndex>,
7370
/// Stack of types we're currently parsing
7471
pub(crate) type_stack: Vec<TypeIndex>,
7572
/// Stack of parent types we're parsing nested types inside of
76-
pub(crate) namespace_stack: Vec<String>,
73+
pub(crate) namespace_stack: QualifiedName,
7774
/// Type Index -> Does it return on the stack
7875
pub(crate) type_default_returnable: BTreeMap<TypeIndex, bool>,
7976

@@ -287,9 +284,9 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
287284
fn collect_name(
288285
&self,
289286
name: &NamedTypeReference,
290-
unknown_names: &mut HashMap<String, NamedTypeReferenceClass>,
287+
unknown_names: &mut HashMap<QualifiedName, NamedTypeReferenceClass>,
291288
) {
292-
let used_name = name.name().to_string();
289+
let used_name = name.name();
293290
if let Some(&found) =
294291
unknown_names.get(&used_name)
295292
{
@@ -312,20 +309,16 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
312309
fn collect_names(
313310
&self,
314311
ty: &Type,
315-
unknown_names: &mut HashMap<String, NamedTypeReferenceClass>,
312+
unknown_names: &mut HashMap<QualifiedName, NamedTypeReferenceClass>,
316313
) {
317314
match ty.type_class() {
318315
TypeClass::StructureTypeClass => {
319316
if let Some(structure) = ty.get_structure() {
320-
if let Some(members) = structure.members() {
321-
for member in members {
322-
self.collect_names(member.ty.contents.as_ref(), unknown_names);
323-
}
317+
for member in structure.members() {
318+
self.collect_names(member.ty.contents.as_ref(), unknown_names);
324319
}
325-
if let Some(bases) = structure.base_structures() {
326-
for base in bases {
327-
self.collect_name(base.ty.as_ref(), unknown_names);
328-
}
320+
for base in structure.base_structures() {
321+
self.collect_name(base.ty.as_ref(), unknown_names);
329322
}
330323
}
331324
}
@@ -368,7 +361,7 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
368361
.bv
369362
.types()
370363
.iter()
371-
.map(|qnat| qnat.name().string())
364+
.map(|qnat| qnat.name)
372365
.collect::<HashSet<_>>();
373366

374367
for ty in &self.named_types {
@@ -409,7 +402,7 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
409402
match class {
410403
NamedTypeReferenceClass::UnknownNamedTypeClass
411404
| NamedTypeReferenceClass::TypedefNamedTypeClass => {
412-
self.debug_info.add_type(name, Type::void().as_ref(), &[]); // TODO : Components
405+
self.debug_info.add_type(&name, Type::void().as_ref(), &[]); // TODO : Components
413406
}
414407
NamedTypeReferenceClass::ClassNamedTypeClass
415408
| NamedTypeReferenceClass::StructNamedTypeClass
@@ -431,15 +424,15 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
431424
structure.set_alignment(1);
432425

433426
self.debug_info.add_type(
434-
name,
427+
&name,
435428
Type::structure(structure.finalize().as_ref()).as_ref(),
436429
&[], // TODO : Components
437430
);
438431
}
439432
NamedTypeReferenceClass::EnumNamedTypeClass => {
440433
let enumeration = EnumerationBuilder::new();
441434
self.debug_info.add_type(
442-
name,
435+
&name,
443436
Type::enumeration(
444437
enumeration.finalize().as_ref(),
445438
self.arch.default_integer_size().try_into()?,

0 commit comments

Comments
 (0)