diff options
| author | KyleMiles <krm504@nyu.edu> | 2024-01-16 15:34:55 -0500 |
|---|---|---|
| committer | KyleMiles <krm504@nyu.edu> | 2024-01-19 15:45:24 -0500 |
| commit | e9b7c410bd50582d2a6ecd49cf02ea8f54ec1f1d (patch) | |
| tree | 78ad6272077d976a69ef76698d658991faf129d0 /rust/examples | |
| parent | fb0fcd199680032932784b07b074738999779d58 (diff) | |
DWARF Import : Remove a bunch of CString overhead
Diffstat (limited to 'rust/examples')
| -rw-r--r-- | rust/examples/dwarf/dwarf_import/src/die_handlers.rs | 6 | ||||
| -rw-r--r-- | rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs | 43 | ||||
| -rw-r--r-- | rust/examples/dwarf/dwarf_import/src/functions.rs | 4 | ||||
| -rw-r--r-- | rust/examples/dwarf/dwarf_import/src/helpers.rs | 11 | ||||
| -rw-r--r-- | rust/examples/dwarf/dwarf_import/src/lib.rs | 32 | ||||
| -rw-r--r-- | rust/examples/dwarf/dwarf_import/src/types.rs | 9 |
6 files changed, 45 insertions, 60 deletions
diff --git a/rust/examples/dwarf/dwarf_import/src/die_handlers.rs b/rust/examples/dwarf/dwarf_import/src/die_handlers.rs index 7f4b1000..125038dd 100644 --- a/rust/examples/dwarf/dwarf_import/src/die_handlers.rs +++ b/rust/examples/dwarf/dwarf_import/src/die_handlers.rs @@ -23,8 +23,6 @@ use binaryninja::{ use gimli::{constants, AttributeValue::Encoding, DebuggingInformationEntry, Reader, Unit}; -use std::ffi::CString; - pub(crate) fn handle_base_type<R: Reader<Offset = usize>>( unit: &Unit<R>, entry: &DebuggingInformationEntry<R>, @@ -133,7 +131,7 @@ pub(crate) fn handle_enum<R: Reader<Offset = usize>>( pub(crate) fn handle_typedef( debug_info_builder: &mut DebugInfoBuilder, entry_type: Option<TypeUID>, - typedef_name: CString, + typedef_name: String, ) -> (Option<Ref<Type>>, bool) { // All base types have: // DW_AT_name @@ -313,7 +311,7 @@ pub(crate) fn handle_function<R: Reader<Offset = usize>>( ); } - let mut parameters: Vec<FunctionParameter<CString>> = vec![]; + let mut parameters: Vec<FunctionParameter<String>> = vec![]; let mut variable_arguments = false; // Get all the children and populate diff --git a/rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs b/rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs index cefc8672..4b0f9350 100644 --- a/rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs +++ b/rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs @@ -29,7 +29,6 @@ use gimli::{DebuggingInformationEntry, Dwarf, Reader, Unit}; use log::{error, warn}; use std::{ collections::{hash_map::Values, HashMap}, - ffi::CString, hash::Hash, }; @@ -41,22 +40,22 @@ pub(crate) type TypeUID = usize; // TODO : Function local variables #[derive(PartialEq, Eq, Hash)] pub(crate) struct FunctionInfoBuilder { - pub(crate) full_name: Option<CString>, - pub(crate) raw_name: Option<CString>, + pub(crate) full_name: Option<String>, + pub(crate) raw_name: Option<String>, pub(crate) return_type: Option<TypeUID>, pub(crate) address: Option<u64>, - pub(crate) parameters: Vec<Option<(CString, TypeUID)>>, + pub(crate) parameters: Vec<Option<(String, TypeUID)>>, pub(crate) platform: Option<Ref<Platform>>, } impl FunctionInfoBuilder { pub(crate) fn update( &mut self, - full_name: Option<CString>, - raw_name: Option<CString>, + full_name: Option<String>, + raw_name: Option<String>, return_type: Option<TypeUID>, address: Option<u64>, - parameters: Vec<Option<(CString, TypeUID)>>, + parameters: Vec<Option<(String, TypeUID)>>, ) { if full_name.is_some() { self.full_name = full_name; @@ -93,7 +92,7 @@ impl FunctionInfoBuilder { // TODO : Don't make this pub...fix the value thing pub(crate) struct DebugType { - name: CString, + name: String, t: Ref<Type>, commit: bool, } @@ -101,7 +100,7 @@ pub(crate) struct DebugType { pub(crate) struct DebugInfoBuilderContext<R: Reader<Offset = usize>> { dwarf: Dwarf<R>, units: Vec<Unit<R>>, - names: HashMap<TypeUID, CString>, + names: HashMap<TypeUID, String>, default_address_size: usize, pub(crate) total_die_count: usize, } @@ -140,7 +139,7 @@ impl<R: Reader<Offset = usize>> DebugInfoBuilderContext<R> { self.default_address_size } - pub(crate) fn set_name(&mut self, die_uid: TypeUID, name: CString) { + pub(crate) fn set_name(&mut self, die_uid: TypeUID, name: String) { assert!(self.names.insert(die_uid, name).is_none()); } @@ -148,7 +147,7 @@ impl<R: Reader<Offset = usize>> DebugInfoBuilderContext<R> { &self, unit: &Unit<R>, entry: &DebuggingInformationEntry<R>, - ) -> Option<CString> { + ) -> Option<String> { match resolve_specification(unit, entry, self) { DieReference::UnitAndOffset((entry_unit, entry_offset)) => self .names @@ -168,7 +167,7 @@ impl<R: Reader<Offset = usize>> DebugInfoBuilderContext<R> { pub(crate) struct DebugInfoBuilder { functions: Vec<FunctionInfoBuilder>, types: HashMap<TypeUID, DebugType>, - data_variables: HashMap<u64, (Option<CString>, TypeUID)>, + data_variables: HashMap<u64, (Option<String>, TypeUID)>, } impl DebugInfoBuilder { @@ -183,11 +182,11 @@ impl DebugInfoBuilder { #[allow(clippy::too_many_arguments)] pub(crate) fn insert_function( &mut self, - full_name: Option<CString>, - raw_name: Option<CString>, + full_name: Option<String>, + raw_name: Option<String>, return_type: Option<TypeUID>, address: Option<u64>, - parameters: Vec<Option<(CString, TypeUID)>>, + parameters: Vec<Option<(String, TypeUID)>>, ) { // Raw names should be the primary key, but if they don't exist, use the full name // TODO : Consider further falling back on address/architecture @@ -226,7 +225,7 @@ impl DebugInfoBuilder { pub(crate) fn add_type( &mut self, type_uid: TypeUID, - name: CString, + name: String, t: Ref<Type>, commit: bool, ) { @@ -258,7 +257,7 @@ impl DebugInfoBuilder { } // TODO : Non-copy? - pub(crate) fn get_type(&self, type_uid: TypeUID) -> Option<(CString, Ref<Type>)> { + pub(crate) fn get_type(&self, type_uid: TypeUID) -> Option<(String, Ref<Type>)> { self.types .get(&type_uid) .map(|type_ref_ref| (type_ref_ref.name.clone(), type_ref_ref.t.clone())) @@ -271,7 +270,7 @@ impl DebugInfoBuilder { pub(crate) fn add_data_variable( &mut self, address: u64, - name: Option<CString>, + name: Option<String>, type_uid: TypeUID, ) { if let Some((_existing_name, existing_type_uid)) = @@ -317,7 +316,7 @@ impl DebugInfoBuilder { _ => Conf::new(binaryninja::types::Type::void(), 0), }; - let parameters: Vec<FunctionParameter<CString>> = function + let parameters: Vec<FunctionParameter<String>> = function .parameters .iter() .filter_map(|parameter| match parameter { @@ -363,7 +362,7 @@ impl DebugInfoBuilder { for func in &mut self.functions { // If the function's raw name already exists in the binary... if let Some(raw_name) = &func.raw_name { - if let Ok(symbol) = bv.symbol_by_raw_name(raw_name.as_c_str()) { + if let Ok(symbol) = bv.symbol_by_raw_name(raw_name) { // Link mangled names without addresses to existing symbols in the binary if func.address.is_none() && func.raw_name.is_some() { // DWARF doesn't contain GOT info, so remove any entries there...they will be wrong (relying on Binja's mechanisms for the GOT is good ) @@ -373,7 +372,7 @@ impl DebugInfoBuilder { } if let Some(full_name) = &func.full_name { - let func_full_name = full_name.to_str().unwrap(); + let func_full_name = full_name; let symbol_full_name = symbol.full_name(); // If our name has fewer namespaces than the existing name, assume we lost the namespace info @@ -381,7 +380,7 @@ impl DebugInfoBuilder { < simplify_str_to_fqn(symbol_full_name.clone(), true).len() { func.full_name = - Some(CString::new(symbol_full_name.to_string()).unwrap()); + Some(symbol_full_name.to_string()); } } } diff --git a/rust/examples/dwarf/dwarf_import/src/functions.rs b/rust/examples/dwarf/dwarf_import/src/functions.rs index e031071b..1d8879a0 100644 --- a/rust/examples/dwarf/dwarf_import/src/functions.rs +++ b/rust/examples/dwarf/dwarf_import/src/functions.rs @@ -18,14 +18,12 @@ use crate::types::get_type; use gimli::{constants, DebuggingInformationEntry, Reader, Unit}; -use std::ffi::CString; - fn get_parameters<R: Reader<Offset = usize>>( unit: &Unit<R>, entry: &DebuggingInformationEntry<R>, debug_info_builder_context: &DebugInfoBuilderContext<R>, debug_info_builder: &mut DebugInfoBuilder, -) -> Vec<Option<(CString, TypeUID)>> { +) -> Vec<Option<(String, TypeUID)>> { if !entry.has_children() { vec![] } else { diff --git a/rust/examples/dwarf/dwarf_import/src/helpers.rs b/rust/examples/dwarf/dwarf_import/src/helpers.rs index da1f0d29..4788df76 100644 --- a/rust/examples/dwarf/dwarf_import/src/helpers.rs +++ b/rust/examples/dwarf/dwarf_import/src/helpers.rs @@ -21,7 +21,6 @@ use gimli::{ }; use log::warn; -use std::ffi::CString; pub(crate) fn get_uid<R: Reader<Offset = usize>>( unit: &Unit<R>, @@ -112,7 +111,7 @@ pub(crate) fn get_name<R: Reader<Offset = usize>>( unit: &Unit<R>, entry: &DebuggingInformationEntry<R>, debug_info_builder_context: &DebugInfoBuilderContext<R>, -) -> Option<CString> { +) -> Option<String> { match resolve_specification(unit, entry, debug_info_builder_context) { DieReference::UnitAndOffset((entry_unit, entry_offset)) => { if let Ok(Some(attr_val)) = entry_unit @@ -125,7 +124,7 @@ pub(crate) fn get_name<R: Reader<Offset = usize>>( .attr_string(entry_unit, attr_val) { if let Ok(attr_string) = attr_string.to_string() { - return Some(CString::new(attr_string.to_string()).unwrap()); + return Some(attr_string.to_string()); } } } @@ -133,7 +132,7 @@ pub(crate) fn get_name<R: Reader<Offset = usize>>( // if let Some(raw_name) = get_raw_name(unit, entry, debug_info_builder_context) { // if let Some(arch) = debug_info_builder_context.default_architecture() { // if let Ok((_, names)) = demangle_gnu3(&arch, raw_name, true) { - // return Some(CString::new(names.join("::")).unwrap()); + // return Some(names.join("::")); // } // } // } @@ -148,14 +147,14 @@ pub(crate) fn get_raw_name<R: Reader<Offset = usize>>( unit: &Unit<R>, entry: &DebuggingInformationEntry<R>, debug_info_builder_context: &DebugInfoBuilderContext<R>, -) -> Option<CString> { +) -> Option<String> { if let Ok(Some(attr_val)) = entry.attr_value(constants::DW_AT_linkage_name) { if let Ok(attr_string) = debug_info_builder_context .dwarf() .attr_string(unit, attr_val) { if let Ok(attr_string) = attr_string.to_string() { - return Some(CString::new(attr_string.to_string()).unwrap()); + return Some(attr_string.to_string()); } } } diff --git a/rust/examples/dwarf/dwarf_import/src/lib.rs b/rust/examples/dwarf/dwarf_import/src/lib.rs index d46be392..06809428 100644 --- a/rust/examples/dwarf/dwarf_import/src/lib.rs +++ b/rust/examples/dwarf/dwarf_import/src/lib.rs @@ -36,7 +36,6 @@ use dwarfreader::{ use gimli::{constants, DebuggingInformationEntry, Dwarf, DwarfFileType, Reader, SectionId, Unit}; use log::{error, warn, LevelFilter}; -use std::ffi::CString; fn recover_names<R: Reader<Offset = usize>>( debug_info_builder_context: &mut DebugInfoBuilderContext<R>, @@ -45,7 +44,7 @@ fn recover_names<R: Reader<Offset = usize>>( let mut iter = debug_info_builder_context.dwarf().units(); while let Ok(Some(header)) = iter.next() { let unit = debug_info_builder_context.dwarf().unit(header).unwrap(); - let mut namespace_qualifiers: Vec<(isize, CString)> = vec![]; + let mut namespace_qualifiers: Vec<(isize, String)> = vec![]; let mut entries = unit.entries(); let mut depth = 0; @@ -77,7 +76,7 @@ fn recover_names<R: Reader<Offset = usize>>( unit: &Unit<R>, entry: &DebuggingInformationEntry<R>, debug_info_builder_context: &DebugInfoBuilderContext<R>, - namespace_qualifiers: &mut Vec<(isize, CString)>, + namespace_qualifiers: &mut Vec<(isize, String)>, depth: isize, ) { if let Some(namespace_qualifier) = @@ -108,7 +107,7 @@ fn recover_names<R: Reader<Offset = usize>>( } } else { namespace_qualifiers - .push((depth, CString::new("anonymous_namespace").unwrap())); + .push((depth, "anonymous_namespace".to_string())); } } @@ -128,28 +127,24 @@ fn recover_names<R: Reader<Offset = usize>>( } else { namespace_qualifiers.push(( depth, - CString::new(match entry.tag() { - constants::DW_TAG_class_type => "anonymous_class", - constants::DW_TAG_structure_type => "anonymous_structure", - constants::DW_TAG_union_type => "anonymous_union", + match entry.tag() { + constants::DW_TAG_class_type => "anonymous_class".to_string(), + constants::DW_TAG_structure_type => "anonymous_structure".to_string(), + constants::DW_TAG_union_type => "anonymous_union".to_string(), _ => unreachable!(), - }) - .unwrap(), + } )) } debug_info_builder_context.set_name( get_uid(&unit, entry), - CString::new( simplify_str_to_str( namespace_qualifiers .iter() - .map(|(_, namespace)| namespace.to_string_lossy().to_string()) + .map(|(_, namespace)| namespace.to_owned()) .collect::<Vec<String>>() .join("::"), ) - .as_str(), - ) - .unwrap(), + .to_string(), ); } constants::DW_TAG_typedef @@ -158,20 +153,17 @@ fn recover_names<R: Reader<Offset = usize>>( if let Some(name) = get_name(&unit, entry, debug_info_builder_context) { debug_info_builder_context.set_name( get_uid(&unit, entry), - CString::new( simplify_str_to_str( namespace_qualifiers .iter() .chain(vec![&(-1, name)].into_iter()) .map(|(_, namespace)| { - namespace.to_string_lossy().to_string() + namespace.to_owned() }) .collect::<Vec<String>>() .join("::"), ) - .as_str(), - ) - .unwrap(), + .to_string(), ); } } diff --git a/rust/examples/dwarf/dwarf_import/src/types.rs b/rust/examples/dwarf/dwarf_import/src/types.rs index 0c2435f1..38bd36ea 100644 --- a/rust/examples/dwarf/dwarf_import/src/types.rs +++ b/rust/examples/dwarf/dwarf_import/src/types.rs @@ -26,7 +26,6 @@ use binaryninja::{ use gimli::{constants, DebuggingInformationEntry, Reader, Unit}; use log::warn; -use std::ffi::CString; pub(crate) fn parse_data_variable<R: Reader<Offset = usize>>( unit: &Unit<R>, @@ -125,7 +124,7 @@ fn do_structure_parse<R: Reader<Offset = usize>>( let full_name = format!("anonymous_structure_{:x}", get_uid(unit, entry)); debug_info_builder.add_type( get_uid(unit, entry), - CString::new(full_name.clone()).unwrap(), + full_name.clone(), Type::named_type_from_type(full_name, &Type::structure(&structure_builder.finalize())), false, ); @@ -147,7 +146,7 @@ fn do_structure_parse<R: Reader<Offset = usize>>( .get_name(unit, child.entry()) .map_or( if child_type.type_class() == TypeClass::StructureTypeClass { - Some(CString::new("").unwrap()) + Some("".to_string()) } else { None }, @@ -197,7 +196,7 @@ fn do_structure_parse<R: Reader<Offset = usize>>( } else { debug_info_builder.add_type( get_uid(unit, entry), - CString::new(format!("{}", finalized_structure)).unwrap(), + format!("{}", finalized_structure), finalized_structure, false, // Don't commit anonymous unions (because I think it'll break things) ); @@ -383,7 +382,7 @@ pub(crate) fn get_type<R: Reader<Offset = usize>>( } .unwrap_or_else(|| { commit = false; - CString::new(format!("{}", type_def)).unwrap() + format!("{}", type_def) }); debug_info_builder.add_type(get_uid(unit, entry), name, type_def, commit); |
