Skip to content

Commit 9e9e6e7

Browse files
committed
Fix clippy warnings and run rustfmt
1 parent 7628536 commit 9e9e6e7

33 files changed

+406
-359
lines changed

rust/examples/dwarf/dwarf_import/src/die_handlers.rs

+29-13
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
// limitations under the License.
1414

1515
use crate::dwarfdebuginfo::{DebugInfoBuilder, DebugInfoBuilderContext, TypeUID};
16-
use crate::{helpers::*, ReaderType};
1716
use crate::types::get_type;
17+
use crate::{helpers::*, ReaderType};
1818

1919
use binaryninja::{
2020
rc::*,
@@ -172,7 +172,10 @@ pub(crate) fn handle_pointer<R: ReaderType>(
172172

173173
if let Some(pointer_size) = get_size_as_usize(entry) {
174174
if let Some(entry_type_offset) = entry_type {
175-
let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().get_type();
175+
let parent_type = debug_info_builder
176+
.get_type(entry_type_offset)
177+
.unwrap()
178+
.get_type();
176179
Some(Type::pointer_of_width(
177180
parent_type.as_ref(),
178181
pointer_size,
@@ -190,7 +193,10 @@ pub(crate) fn handle_pointer<R: ReaderType>(
190193
))
191194
}
192195
} else if let Some(entry_type_offset) = entry_type {
193-
let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().get_type();
196+
let parent_type = debug_info_builder
197+
.get_type(entry_type_offset)
198+
.unwrap()
199+
.get_type();
194200
Some(Type::pointer_of_width(
195201
parent_type.as_ref(),
196202
debug_info_builder_context.default_address_size(),
@@ -228,7 +234,10 @@ pub(crate) fn handle_array<R: ReaderType>(
228234
// For multidimensional arrays, DW_TAG_subrange_type or DW_TAG_enumeration_type
229235

230236
if let Some(entry_type_offset) = entry_type {
231-
let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().get_type();
237+
let parent_type = debug_info_builder
238+
.get_type(entry_type_offset)
239+
.unwrap()
240+
.get_type();
232241

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

288297
let return_type = match entry_type {
289-
Some(entry_type_offset) => {
290-
debug_info_builder
291-
.get_type(entry_type_offset)
292-
.expect("Subroutine return type was not processed")
293-
.get_type()
294-
}
298+
Some(entry_type_offset) => debug_info_builder
299+
.get_type(entry_type_offset)
300+
.expect("Subroutine return type was not processed")
301+
.get_type(),
295302
None => Type::void(),
296303
};
297304

@@ -336,7 +343,10 @@ pub(crate) fn handle_function<R: ReaderType>(
336343
}
337344
}
338345

339-
if debug_info_builder_context.get_name(dwarf, unit, entry).is_some() {
346+
if debug_info_builder_context
347+
.get_name(dwarf, unit, entry)
348+
.is_some()
349+
{
340350
debug_info_builder.remove_type(get_uid(dwarf, unit, entry));
341351
}
342352

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

362372
if let Some(entry_type_offset) = entry_type {
363-
let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().get_type();
373+
let parent_type = debug_info_builder
374+
.get_type(entry_type_offset)
375+
.unwrap()
376+
.get_type();
364377
Some((*parent_type).to_builder().set_const(true).finalize())
365378
} else {
366379
Some(TypeBuilder::void().set_const(true).finalize())
@@ -380,7 +393,10 @@ pub(crate) fn handle_volatile(
380393
// ?DW_AT_type
381394

382395
if let Some(entry_type_offset) = entry_type {
383-
let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().get_type();
396+
let parent_type = debug_info_builder
397+
.get_type(entry_type_offset)
398+
.unwrap()
399+
.get_type();
384400
Some((*parent_type).to_builder().set_volatile(true).finalize())
385401
} else {
386402
Some(TypeBuilder::void().set_volatile(true).finalize())

rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs

+54-33
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::{helpers::{get_uid, resolve_specification, DieReference}, ReaderType};
15+
use crate::{
16+
helpers::{get_uid, resolve_specification, DieReference},
17+
ReaderType,
18+
};
1619

1720
use binaryninja::{
21+
binaryninjacore_sys::BNVariableSourceType,
1822
binaryview::{BinaryView, BinaryViewBase, BinaryViewExt},
1923
debuginfo::{DebugFunctionInfo, DebugInfo},
2024
platform::Platform,
@@ -120,7 +124,6 @@ pub(crate) struct DebugInfoBuilderContext<R: ReaderType> {
120124

121125
impl<R: ReaderType> DebugInfoBuilderContext<R> {
122126
pub(crate) fn new(view: &BinaryView, dwarf: &Dwarf<R>) -> Option<Self> {
123-
124127
let mut units = vec![];
125128
let mut iter = dwarf.units();
126129
while let Ok(Some(header)) = iter.next() {
@@ -200,7 +203,7 @@ pub(crate) struct DebugInfoBuilder {
200203
full_function_name_indices: HashMap<String, usize>,
201204
types: HashMap<TypeUID, DebugType>,
202205
data_variables: HashMap<u64, (Option<String>, TypeUID)>,
203-
range_data_offsets: iset::IntervalMap<u64, i64>
206+
range_data_offsets: iset::IntervalMap<u64, i64>,
204207
}
205208

206209
impl DebugInfoBuilder {
@@ -234,10 +237,10 @@ impl DebugInfoBuilder {
234237
// TODO : Consider further falling back on address/architecture
235238

236239
/*
237-
If it has a raw_name and we know it, update it and return
238-
Else if it has a full_name and we know it, update it and return
239-
Else Add a new entry if we don't know the full_name or raw_name
240-
*/
240+
If it has a raw_name and we know it, update it and return
241+
Else if it has a full_name and we know it, update it and return
242+
Else Add a new entry if we don't know the full_name or raw_name
243+
*/
241244

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

250253
if function.full_name.is_some() && function.full_name != full_name {
251-
self.full_function_name_indices.remove(function.full_name.as_ref().unwrap());
254+
self.full_function_name_indices
255+
.remove(function.full_name.as_ref().unwrap());
252256
}
253257

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

256-
if function.full_name.is_some() {
257-
self.full_function_name_indices.insert(function.full_name.clone().unwrap(), *idx);
260+
if function.full_name.is_some() {
261+
self.full_function_name_indices
262+
.insert(function.full_name.clone().unwrap(), *idx);
258263
}
259264

260265
return Some(*idx);
261266
}
262-
}
263-
264-
else if let Some(ident) = &full_name {
267+
} else if let Some(ident) = &full_name {
265268
// check if we already know about this full name's index
266269
// if we do, and the raw name will change, remove the known raw index if it exists
267270
// update the function
@@ -270,13 +273,15 @@ impl DebugInfoBuilder {
270273
let function = self.functions.get_mut(*idx).unwrap();
271274

272275
if function.raw_name.is_some() && function.raw_name != raw_name {
273-
self.raw_function_name_indices.remove(function.raw_name.as_ref().unwrap());
276+
self.raw_function_name_indices
277+
.remove(function.raw_name.as_ref().unwrap());
274278
}
275279

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

278-
if function.raw_name.is_some() {
279-
self.raw_function_name_indices.insert(function.raw_name.clone().unwrap(), *idx);
282+
if function.raw_name.is_some() {
283+
self.raw_function_name_indices
284+
.insert(function.raw_name.clone().unwrap(), *idx);
280285
}
281286

282287
return Some(*idx);
@@ -300,15 +305,17 @@ impl DebugInfoBuilder {
300305
};
301306

302307
if let Some(n) = &function.full_name {
303-
self.full_function_name_indices.insert(n.clone(), self.functions.len());
308+
self.full_function_name_indices
309+
.insert(n.clone(), self.functions.len());
304310
}
305311

306312
if let Some(n) = &function.raw_name {
307-
self.raw_function_name_indices.insert(n.clone(), self.functions.len());
313+
self.raw_function_name_indices
314+
.insert(n.clone(), self.functions.len());
308315
}
309316

310317
self.functions.push(function);
311-
Some(self.functions.len()-1)
318+
Some(self.functions.len() - 1)
312319
}
313320

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

322-
pub(crate) fn add_type(&mut self, type_uid: TypeUID, name: &String, t: Ref<Type>, commit: bool) {
329+
pub(crate) fn add_type(
330+
&mut self,
331+
type_uid: TypeUID,
332+
name: &String,
333+
t: Ref<Type>,
334+
commit: bool,
335+
) {
323336
if let Some(DebugType {
324337
name: existing_name,
325338
t: existing_type,
@@ -355,7 +368,6 @@ impl DebugInfoBuilder {
355368
self.types.contains_key(&type_uid)
356369
}
357370

358-
359371
pub(crate) fn add_stack_variable(
360372
&mut self,
361373
fn_idx: Option<usize>,
@@ -368,11 +380,10 @@ impl DebugInfoBuilder {
368380
if x.len() == 1 && x.chars().next() == Some('\x00') {
369381
// Anonymous variable, generate name
370382
format!("debug_var_{}", offset)
371-
}
372-
else {
383+
} else {
373384
x
374385
}
375-
},
386+
}
376387
None => {
377388
// Anonymous variable, generate name
378389
format!("debug_var_{}", offset)
@@ -381,14 +392,16 @@ impl DebugInfoBuilder {
381392

382393
let Some(function_index) = fn_idx else {
383394
// If we somehow lost track of what subprogram we're in or we're not actually in a subprogram
384-
error!("Trying to add a local variable outside of a subprogram. Please report this issue.");
395+
error!(
396+
"Trying to add a local variable outside of a subprogram. Please report this issue."
397+
);
385398
return;
386399
};
387400

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

@@ -400,7 +413,8 @@ impl DebugInfoBuilder {
400413
return;
401414
};
402415

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

417-
let var = Variable::new(VariableSourceType::StackVariableSourceType, 0, adjusted_offset);
418-
function.stack_variables.push(NamedTypedVariable::new(var, name, t, false));
419-
431+
let var = Variable::new(
432+
VariableSourceType::StackVariableSourceType,
433+
0,
434+
adjusted_offset,
435+
);
436+
function
437+
.stack_variables
438+
.push(NamedTypedVariable::new(var, name, t, false));
420439
}
421440

422441
pub(crate) fn add_data_variable(
@@ -464,7 +483,9 @@ impl DebugInfoBuilder {
464483

465484
fn get_function_type(&self, function: &FunctionInfoBuilder) -> Ref<Type> {
466485
let return_type = match function.return_type {
467-
Some(return_type_id) => Conf::new(self.get_type(return_type_id).unwrap().get_type(), 128),
486+
Some(return_type_id) => {
487+
Conf::new(self.get_type(return_type_id).unwrap().get_type(), 128)
488+
}
468489
_ => Conf::new(binaryninja::types::Type::void(), 0),
469490
};
470491

@@ -496,7 +517,7 @@ impl DebugInfoBuilder {
496517
Some(self.get_function_type(function)),
497518
function.address,
498519
function.platform.clone(),
499-
vec![], // TODO : Components
520+
vec![], // TODO : Components
500521
function.stack_variables.clone(), // TODO: local non-stack variables
501522
));
502523
}
@@ -536,7 +557,7 @@ impl DebugInfoBuilder {
536557

537558
if let Some(address) = func.address.as_mut() {
538559
let diff = bv.start() - bv.original_image_base();
539-
*address += diff; // rebase the address
560+
*address += diff; // rebase the address
540561
let existing_functions = bv.functions_at(*address);
541562
match existing_functions.len().cmp(&1) {
542563
Ordering::Greater => {

rust/examples/dwarf/dwarf_import/src/functions.rs

+24-7
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
use std::sync::OnceLock;
1616

1717
use crate::dwarfdebuginfo::{DebugInfoBuilder, DebugInfoBuilderContext, TypeUID};
18-
use crate::{helpers::*, ReaderType};
1918
use crate::types::get_type;
19+
use crate::{helpers::*, ReaderType};
2020

2121
use binaryninja::templatesimplifier::simplify_str_to_str;
2222
use cpp_demangle::DemangleOptions;
@@ -81,9 +81,21 @@ pub(crate) fn parse_function_entry<R: ReaderType>(
8181
) -> Option<usize> {
8282
// Collect function properties (if they exist in this DIE)
8383
let raw_name = get_raw_name(dwarf, unit, entry);
84-
let return_type = get_type(dwarf, unit, entry, debug_info_builder_context, debug_info_builder);
84+
let return_type = get_type(
85+
dwarf,
86+
unit,
87+
entry,
88+
debug_info_builder_context,
89+
debug_info_builder,
90+
);
8591
let address = get_start_address(dwarf, unit, entry);
86-
let (parameters, variable_arguments) = get_parameters(dwarf, unit, entry, debug_info_builder_context, debug_info_builder);
92+
let (parameters, variable_arguments) = get_parameters(
93+
dwarf,
94+
unit,
95+
entry,
96+
debug_info_builder_context,
97+
debug_info_builder,
98+
);
8799

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

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

120-
debug_info_builder.insert_function(full_name, raw_name, return_type, address, &parameters, variable_arguments)
130+
debug_info_builder.insert_function(
131+
full_name,
132+
raw_name,
133+
return_type,
134+
address,
135+
&parameters,
136+
variable_arguments,
137+
)
121138
}

0 commit comments

Comments
 (0)