Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clippy warnings and run rustfmt #5764

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions rust/examples/dwarf/dwarf_import/src/die_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// limitations under the License.

use crate::dwarfdebuginfo::{DebugInfoBuilder, DebugInfoBuilderContext, TypeUID};
use crate::{helpers::*, ReaderType};
use crate::types::get_type;
use crate::{helpers::*, ReaderType};

use binaryninja::{
rc::*,
Expand Down Expand Up @@ -172,7 +172,10 @@ pub(crate) fn handle_pointer<R: ReaderType>(

if let Some(pointer_size) = get_size_as_usize(entry) {
if let Some(entry_type_offset) = entry_type {
let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().get_type();
let parent_type = debug_info_builder
.get_type(entry_type_offset)
.unwrap()
.get_type();
Some(Type::pointer_of_width(
parent_type.as_ref(),
pointer_size,
Expand All @@ -190,7 +193,10 @@ pub(crate) fn handle_pointer<R: ReaderType>(
))
}
} else if let Some(entry_type_offset) = entry_type {
let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().get_type();
let parent_type = debug_info_builder
.get_type(entry_type_offset)
.unwrap()
.get_type();
Some(Type::pointer_of_width(
parent_type.as_ref(),
debug_info_builder_context.default_address_size(),
Expand Down Expand Up @@ -228,7 +234,10 @@ pub(crate) fn handle_array<R: ReaderType>(
// For multidimensional arrays, DW_TAG_subrange_type or DW_TAG_enumeration_type

if let Some(entry_type_offset) = entry_type {
let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().get_type();
let parent_type = debug_info_builder
.get_type(entry_type_offset)
.unwrap()
.get_type();

let mut tree = unit.entries_tree(Some(entry.offset())).unwrap();
let mut children = tree.root().unwrap().children();
Expand Down Expand Up @@ -286,12 +295,10 @@ pub(crate) fn handle_function<R: ReaderType>(
// or is otherwise DW_TAG_unspecified_parameters

let return_type = match entry_type {
Some(entry_type_offset) => {
debug_info_builder
.get_type(entry_type_offset)
.expect("Subroutine return type was not processed")
.get_type()
}
Some(entry_type_offset) => debug_info_builder
.get_type(entry_type_offset)
.expect("Subroutine return type was not processed")
.get_type(),
None => Type::void(),
};

Expand Down Expand Up @@ -336,7 +343,10 @@ pub(crate) fn handle_function<R: ReaderType>(
}
}

if debug_info_builder_context.get_name(dwarf, unit, entry).is_some() {
if debug_info_builder_context
.get_name(dwarf, unit, entry)
.is_some()
{
debug_info_builder.remove_type(get_uid(dwarf, unit, entry));
}

Expand All @@ -360,7 +370,10 @@ pub(crate) fn handle_const(
// ?DW_AT_type

if let Some(entry_type_offset) = entry_type {
let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().get_type();
let parent_type = debug_info_builder
.get_type(entry_type_offset)
.unwrap()
.get_type();
Some((*parent_type).to_builder().set_const(true).finalize())
} else {
Some(TypeBuilder::void().set_const(true).finalize())
Expand All @@ -380,7 +393,10 @@ pub(crate) fn handle_volatile(
// ?DW_AT_type

if let Some(entry_type_offset) = entry_type {
let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().get_type();
let parent_type = debug_info_builder
.get_type(entry_type_offset)
.unwrap()
.get_type();
Some((*parent_type).to_builder().set_volatile(true).finalize())
} else {
Some(TypeBuilder::void().set_volatile(true).finalize())
Expand Down
87 changes: 54 additions & 33 deletions rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{helpers::{get_uid, resolve_specification, DieReference}, ReaderType};
use crate::{
helpers::{get_uid, resolve_specification, DieReference},
ReaderType,
};

use binaryninja::{
binaryninjacore_sys::BNVariableSourceType,
binaryview::{BinaryView, BinaryViewBase, BinaryViewExt},
debuginfo::{DebugFunctionInfo, DebugInfo},
platform::Platform,
Expand Down Expand Up @@ -120,7 +124,6 @@ pub(crate) struct DebugInfoBuilderContext<R: ReaderType> {

impl<R: ReaderType> DebugInfoBuilderContext<R> {
pub(crate) fn new(view: &BinaryView, dwarf: &Dwarf<R>) -> Option<Self> {

let mut units = vec![];
let mut iter = dwarf.units();
while let Ok(Some(header)) = iter.next() {
Expand Down Expand Up @@ -200,7 +203,7 @@ pub(crate) struct DebugInfoBuilder {
full_function_name_indices: HashMap<String, usize>,
types: HashMap<TypeUID, DebugType>,
data_variables: HashMap<u64, (Option<String>, TypeUID)>,
range_data_offsets: iset::IntervalMap<u64, i64>
range_data_offsets: iset::IntervalMap<u64, i64>,
}

impl DebugInfoBuilder {
Expand Down Expand Up @@ -234,10 +237,10 @@ impl DebugInfoBuilder {
// TODO : Consider further falling back on address/architecture

/*
If it has a raw_name and we know it, update it and return
Else if it has a full_name and we know it, update it and return
Else Add a new entry if we don't know the full_name or raw_name
*/
If it has a raw_name and we know it, update it and return
Else if it has a full_name and we know it, update it and return
Else Add a new entry if we don't know the full_name or raw_name
*/

if let Some(ident) = &raw_name {
// check if we already know about this raw name's index
Expand All @@ -248,20 +251,20 @@ impl DebugInfoBuilder {
let function = self.functions.get_mut(*idx).unwrap();

if function.full_name.is_some() && function.full_name != full_name {
self.full_function_name_indices.remove(function.full_name.as_ref().unwrap());
self.full_function_name_indices
.remove(function.full_name.as_ref().unwrap());
}

function.update(full_name, raw_name, return_type, address, parameters);

if function.full_name.is_some() {
self.full_function_name_indices.insert(function.full_name.clone().unwrap(), *idx);
if function.full_name.is_some() {
self.full_function_name_indices
.insert(function.full_name.clone().unwrap(), *idx);
}

return Some(*idx);
}
}

else if let Some(ident) = &full_name {
} else if let Some(ident) = &full_name {
// check if we already know about this full name's index
// if we do, and the raw name will change, remove the known raw index if it exists
// update the function
Expand All @@ -270,13 +273,15 @@ impl DebugInfoBuilder {
let function = self.functions.get_mut(*idx).unwrap();

if function.raw_name.is_some() && function.raw_name != raw_name {
self.raw_function_name_indices.remove(function.raw_name.as_ref().unwrap());
self.raw_function_name_indices
.remove(function.raw_name.as_ref().unwrap());
}

function.update(full_name, raw_name, return_type, address, parameters);

if function.raw_name.is_some() {
self.raw_function_name_indices.insert(function.raw_name.clone().unwrap(), *idx);
if function.raw_name.is_some() {
self.raw_function_name_indices
.insert(function.raw_name.clone().unwrap(), *idx);
}

return Some(*idx);
Expand All @@ -300,15 +305,17 @@ impl DebugInfoBuilder {
};

if let Some(n) = &function.full_name {
self.full_function_name_indices.insert(n.clone(), self.functions.len());
self.full_function_name_indices
.insert(n.clone(), self.functions.len());
}

if let Some(n) = &function.raw_name {
self.raw_function_name_indices.insert(n.clone(), self.functions.len());
self.raw_function_name_indices
.insert(n.clone(), self.functions.len());
}

self.functions.push(function);
Some(self.functions.len()-1)
Some(self.functions.len() - 1)
}

pub(crate) fn functions(&self) -> &[FunctionInfoBuilder] {
Expand All @@ -319,7 +326,13 @@ impl DebugInfoBuilder {
self.types.values()
}

pub(crate) fn add_type(&mut self, type_uid: TypeUID, name: &String, t: Ref<Type>, commit: bool) {
pub(crate) fn add_type(
&mut self,
type_uid: TypeUID,
name: &String,
t: Ref<Type>,
commit: bool,
) {
if let Some(DebugType {
name: existing_name,
t: existing_type,
Expand Down Expand Up @@ -355,7 +368,6 @@ impl DebugInfoBuilder {
self.types.contains_key(&type_uid)
}


pub(crate) fn add_stack_variable(
&mut self,
fn_idx: Option<usize>,
Expand All @@ -368,11 +380,10 @@ impl DebugInfoBuilder {
if x.len() == 1 && x.chars().next() == Some('\x00') {
// Anonymous variable, generate name
format!("debug_var_{}", offset)
}
else {
} else {
x
}
},
}
None => {
// Anonymous variable, generate name
format!("debug_var_{}", offset)
Expand All @@ -381,14 +392,16 @@ impl DebugInfoBuilder {

let Some(function_index) = fn_idx else {
// If we somehow lost track of what subprogram we're in or we're not actually in a subprogram
error!("Trying to add a local variable outside of a subprogram. Please report this issue.");
error!(
"Trying to add a local variable outside of a subprogram. Please report this issue."
);
return;
};

// Either get the known type or use a 0 confidence void type so we at least get the name applied
let t = match type_uid {
Some(uid) => Conf::new(self.get_type(uid).unwrap().get_type(), 128),
None => Conf::new(Type::void(), 0)
None => Conf::new(Type::void(), 0),
};
let function = &mut self.functions[function_index];

Expand All @@ -400,7 +413,8 @@ impl DebugInfoBuilder {
return;
};

let Some(offset_adjustment) = self.range_data_offsets.values_overlap(func_addr).next() else {
let Some(offset_adjustment) = self.range_data_offsets.values_overlap(func_addr).next()
else {
// Unknown why, but this is happening with MachO + external dSYM
debug!("Refusing to add a local variable ({}@{}) to function at {} without a known CIE offset.", name, offset, func_addr);
return;
Expand All @@ -414,9 +428,14 @@ impl DebugInfoBuilder {
return;
}

let var = Variable::new(VariableSourceType::StackVariableSourceType, 0, adjusted_offset);
function.stack_variables.push(NamedTypedVariable::new(var, name, t, false));

let var = Variable::new(
VariableSourceType::StackVariableSourceType,
0,
adjusted_offset,
);
function
.stack_variables
.push(NamedTypedVariable::new(var, name, t, false));
}

pub(crate) fn add_data_variable(
Expand Down Expand Up @@ -464,7 +483,9 @@ impl DebugInfoBuilder {

fn get_function_type(&self, function: &FunctionInfoBuilder) -> Ref<Type> {
let return_type = match function.return_type {
Some(return_type_id) => Conf::new(self.get_type(return_type_id).unwrap().get_type(), 128),
Some(return_type_id) => {
Conf::new(self.get_type(return_type_id).unwrap().get_type(), 128)
}
_ => Conf::new(binaryninja::types::Type::void(), 0),
};

Expand Down Expand Up @@ -496,7 +517,7 @@ impl DebugInfoBuilder {
Some(self.get_function_type(function)),
function.address,
function.platform.clone(),
vec![], // TODO : Components
vec![], // TODO : Components
function.stack_variables.clone(), // TODO: local non-stack variables
));
}
Expand Down Expand Up @@ -536,7 +557,7 @@ impl DebugInfoBuilder {

if let Some(address) = func.address.as_mut() {
let diff = bv.start() - bv.original_image_base();
*address += diff; // rebase the address
*address += diff; // rebase the address
let existing_functions = bv.functions_at(*address);
match existing_functions.len().cmp(&1) {
Ordering::Greater => {
Expand Down
31 changes: 24 additions & 7 deletions rust/examples/dwarf/dwarf_import/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use std::sync::OnceLock;

use crate::dwarfdebuginfo::{DebugInfoBuilder, DebugInfoBuilderContext, TypeUID};
use crate::{helpers::*, ReaderType};
use crate::types::get_type;
use crate::{helpers::*, ReaderType};

use binaryninja::templatesimplifier::simplify_str_to_str;
use cpp_demangle::DemangleOptions;
Expand Down Expand Up @@ -81,9 +81,21 @@ pub(crate) fn parse_function_entry<R: ReaderType>(
) -> Option<usize> {
// Collect function properties (if they exist in this DIE)
let raw_name = get_raw_name(dwarf, unit, entry);
let return_type = get_type(dwarf, unit, entry, debug_info_builder_context, debug_info_builder);
let return_type = get_type(
dwarf,
unit,
entry,
debug_info_builder_context,
debug_info_builder,
);
let address = get_start_address(dwarf, unit, entry);
let (parameters, variable_arguments) = get_parameters(dwarf, unit, entry, debug_info_builder_context, debug_info_builder);
let (parameters, variable_arguments) = get_parameters(
dwarf,
unit,
entry,
debug_info_builder_context,
debug_info_builder,
);

// If we have a raw name, it might be mangled, see if we can demangle it into full_name
// raw_name should contain a superset of the info we have in full_name
Expand All @@ -99,9 +111,7 @@ pub(crate) fn parse_function_entry<R: ReaderType>(
});

static ABI_REGEX_MEM: OnceLock<Regex> = OnceLock::new();
let abi_regex = ABI_REGEX_MEM.get_or_init(|| {
Regex::new(r"\[abi:v\d+\]").unwrap()
});
let abi_regex = ABI_REGEX_MEM.get_or_init(|| Regex::new(r"\[abi:v\d+\]").unwrap());
if let Ok(sym) = cpp_demangle::Symbol::new(possibly_mangled_name) {
if let Ok(demangled) = sym.demangle(demangle_options) {
let cleaned = abi_regex.replace_all(&demangled, "");
Expand All @@ -117,5 +127,12 @@ pub(crate) fn parse_function_entry<R: ReaderType>(
full_name = debug_info_builder_context.get_name(dwarf, unit, entry)
}

debug_info_builder.insert_function(full_name, raw_name, return_type, address, &parameters, variable_arguments)
debug_info_builder.insert_function(
full_name,
raw_name,
return_type,
address,
&parameters,
variable_arguments,
)
}
Loading
Loading