summaryrefslogtreecommitdiff
path: root/rust/examples
diff options
context:
space:
mode:
authorMichael Krasnitski <michael.krasnitski@gmail.com>2024-04-16 19:32:04 -0400
committerKyle Martin <krm504@nyu.edu>2024-05-09 13:11:41 -0400
commit29b62677dad48aa4d55ad2dae5d176d9880216bd (patch)
treef205a5f4ebb7e71ea132fe06a7076c70616d3c62 /rust/examples
parent608f261e6bca5869e748d4509da92a5717dce75d (diff)
Fix clippy warnings and run rustfmt
Diffstat (limited to 'rust/examples')
-rw-r--r--rust/examples/decompile/src/main.rs2
-rw-r--r--rust/examples/dwarf/dwarf_export/src/lib.rs12
-rw-r--r--rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs22
-rw-r--r--rust/examples/dwarf/dwarf_import/src/lib.rs45
-rw-r--r--rust/examples/dwarf/dwarfdump/src/lib.rs4
-rw-r--r--rust/examples/hlil_visitor/src/main.rs2
-rw-r--r--rust/examples/minidump/src/command.rs5
-rw-r--r--rust/examples/minidump/src/view.rs32
-rw-r--r--rust/examples/mlil_visitor/src/main.rs2
9 files changed, 44 insertions, 82 deletions
diff --git a/rust/examples/decompile/src/main.rs b/rust/examples/decompile/src/main.rs
index 0865538d..cac5ae06 100644
--- a/rust/examples/decompile/src/main.rs
+++ b/rust/examples/decompile/src/main.rs
@@ -26,7 +26,7 @@ fn decompile_to_c(view: &BinaryView, func: &Function) {
let last = view.get_next_linear_disassembly_lines(&mut cursor.duplicate());
let first = view.get_previous_linear_disassembly_lines(&mut cursor);
- let lines = first.into_iter().chain(last.into_iter());
+ let lines = first.into_iter().chain(&last);
for line in lines {
println!("{}", line.as_ref());
diff --git a/rust/examples/dwarf/dwarf_export/src/lib.rs b/rust/examples/dwarf/dwarf_export/src/lib.rs
index ef71f1ae..057abe27 100644
--- a/rust/examples/dwarf/dwarf_export/src/lib.rs
+++ b/rust/examples/dwarf/dwarf_export/src/lib.rs
@@ -522,13 +522,11 @@ fn export_data_vars(
for data_variable in &bv.data_variables() {
if let Some(symbol) = data_variable.symbol(bv) {
- if symbol.sym_type() == SymbolType::External {
- continue;
- } else if symbol.sym_type() == SymbolType::Function {
- continue;
- } else if symbol.sym_type() == SymbolType::ImportedFunction {
- continue;
- } else if symbol.sym_type() == SymbolType::LibraryFunction {
+ if let SymbolType::External
+ | SymbolType::Function
+ | SymbolType::ImportedFunction
+ | SymbolType::LibraryFunction = symbol.sym_type()
+ {
continue;
}
}
diff --git a/rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs b/rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs
index b1b91c1d..537051c8 100644
--- a/rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs
+++ b/rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs
@@ -28,6 +28,7 @@ use gimli::{DebuggingInformationEntry, Dwarf, Reader, Unit};
use log::{error, warn};
use std::{
+ cmp::Ordering,
collections::{hash_map::Values, HashMap},
hash::Hash,
};
@@ -222,13 +223,7 @@ 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,
@@ -379,8 +374,7 @@ impl DebugInfoBuilder {
if simplify_str_to_fqn(func_full_name, true).len()
< simplify_str_to_fqn(symbol_full_name.clone(), true).len()
{
- func.full_name =
- Some(symbol_full_name.to_string());
+ func.full_name = Some(symbol_full_name.to_string());
}
}
}
@@ -388,10 +382,12 @@ impl DebugInfoBuilder {
if let Some(address) = func.address {
let existing_functions = bv.functions_at(address);
- if existing_functions.len() > 1 {
- warn!("Multiple existing functions at address {address:08x}. One or more functions at this address may have the wrong platform information. Please report this binary.");
- } else if existing_functions.len() == 1 {
- func.platform = Some(existing_functions.get(0).platform());
+ match existing_functions.len().cmp(&1) {
+ Ordering::Greater => {
+ warn!("Multiple existing functions at address {address:08x}. One or more functions at this address may have the wrong platform information. Please report this binary.");
+ }
+ Ordering::Equal => func.platform = Some(existing_functions.get(0).platform()),
+ Ordering::Less => {}
}
}
}
diff --git a/rust/examples/dwarf/dwarf_import/src/lib.rs b/rust/examples/dwarf/dwarf_import/src/lib.rs
index 06809428..8aeac658 100644
--- a/rust/examples/dwarf/dwarf_import/src/lib.rs
+++ b/rust/examples/dwarf/dwarf_import/src/lib.rs
@@ -106,8 +106,7 @@ fn recover_names<R: Reader<Offset = usize>>(
}
}
} else {
- namespace_qualifiers
- .push((depth, "anonymous_namespace".to_string()));
+ namespace_qualifiers.push((depth, "anonymous_namespace".to_string()));
}
}
@@ -129,22 +128,24 @@ fn recover_names<R: Reader<Offset = usize>>(
depth,
match entry.tag() {
constants::DW_TAG_class_type => "anonymous_class".to_string(),
- constants::DW_TAG_structure_type => "anonymous_structure".to_string(),
+ constants::DW_TAG_structure_type => {
+ "anonymous_structure".to_string()
+ }
constants::DW_TAG_union_type => "anonymous_union".to_string(),
_ => unreachable!(),
- }
+ },
))
}
debug_info_builder_context.set_name(
get_uid(&unit, entry),
- simplify_str_to_str(
- namespace_qualifiers
- .iter()
- .map(|(_, namespace)| namespace.to_owned())
- .collect::<Vec<String>>()
- .join("::"),
- )
- .to_string(),
+ simplify_str_to_str(
+ namespace_qualifiers
+ .iter()
+ .map(|(_, namespace)| namespace.to_owned())
+ .collect::<Vec<String>>()
+ .join("::"),
+ )
+ .to_string(),
);
}
constants::DW_TAG_typedef
@@ -153,17 +154,15 @@ 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),
- simplify_str_to_str(
- namespace_qualifiers
- .iter()
- .chain(vec![&(-1, name)].into_iter())
- .map(|(_, namespace)| {
- namespace.to_owned()
- })
- .collect::<Vec<String>>()
- .join("::"),
- )
- .to_string(),
+ simplify_str_to_str(
+ namespace_qualifiers
+ .iter()
+ .chain(vec![&(-1, name)].into_iter())
+ .map(|(_, namespace)| namespace.to_owned())
+ .collect::<Vec<String>>()
+ .join("::"),
+ )
+ .to_string(),
);
}
}
diff --git a/rust/examples/dwarf/dwarfdump/src/lib.rs b/rust/examples/dwarf/dwarfdump/src/lib.rs
index c062ac63..2cfbbbce 100644
--- a/rust/examples/dwarf/dwarfdump/src/lib.rs
+++ b/rust/examples/dwarf/dwarfdump/src/lib.rs
@@ -33,7 +33,7 @@ use gimli::{
UnitSectionOffset,
};
-static PADDING: [&'static str; 23] = [
+static PADDING: [&str; 23] = [
"",
" ",
" ",
@@ -189,7 +189,7 @@ fn get_info_string<R: Reader>(
let value_string = format!("{}", value);
attr_line.push(InstructionTextToken::new(
&value_string,
- InstructionTextTokenContents::Integer(value.into()),
+ InstructionTextTokenContents::Integer(value),
));
} else if let Some(value) = attr.sdata_value() {
let value_string = format!("{}", value);
diff --git a/rust/examples/hlil_visitor/src/main.rs b/rust/examples/hlil_visitor/src/main.rs
index 71c283a8..e1bdefb0 100644
--- a/rust/examples/hlil_visitor/src/main.rs
+++ b/rust/examples/hlil_visitor/src/main.rs
@@ -20,7 +20,7 @@ fn print_variable(func: &HighLevelILFunction, var: &Variable) {
fn print_il_expr(instr: &HighLevelILLiftedInstruction, mut indent: usize) {
print_indent(indent);
print_operation(instr);
- println!("");
+ println!();
indent += 1;
diff --git a/rust/examples/minidump/src/command.rs b/rust/examples/minidump/src/command.rs
index 0b10c65a..d33a8d0b 100644
--- a/rust/examples/minidump/src/command.rs
+++ b/rust/examples/minidump/src/command.rs
@@ -5,14 +5,11 @@ use minidump::{Minidump, MinidumpMemoryInfoList};
use binaryninja::binaryview::{BinaryView, BinaryViewBase, BinaryViewExt};
-use crate::view::DataBufferWrapper;
-
pub fn print_memory_information(bv: &BinaryView) {
debug!("Printing memory information");
if let Ok(minidump_bv) = bv.parent_view() {
if let Ok(read_buffer) = minidump_bv.read_buffer(0, minidump_bv.len()) {
- let read_buffer = DataBufferWrapper::new(read_buffer);
- if let Ok(minidump_obj) = Minidump::read(read_buffer) {
+ if let Ok(minidump_obj) = Minidump::read(read_buffer.get_data()) {
if let Ok(memory_info_list) = minidump_obj.get_stream::<MinidumpMemoryInfoList>() {
let mut memory_info_list_writer = Vec::new();
match memory_info_list.print(&mut memory_info_list_writer) {
diff --git a/rust/examples/minidump/src/view.rs b/rust/examples/minidump/src/view.rs
index 9eee6aa6..bbbe0bd5 100644
--- a/rust/examples/minidump/src/view.rs
+++ b/rust/examples/minidump/src/view.rs
@@ -1,6 +1,5 @@
use std::collections::HashMap;
-use std::ops::{Deref, Range};
-use std::sync::Arc;
+use std::ops::Range;
use binaryninja::section::Section;
use binaryninja::segment::Segment;
@@ -16,37 +15,11 @@ use binaryninja::custombinaryview::{
BinaryViewType, BinaryViewTypeBase, CustomBinaryView, CustomBinaryViewType, CustomView,
CustomViewBuilder,
};
-use binaryninja::databuffer::DataBuffer;
use binaryninja::platform::Platform;
use binaryninja::Endianness;
type BinaryViewResult<R> = binaryninja::binaryview::Result<R>;
-/// A wrapper around a `binaryninja::databuffer::DataBuffer`, from which a `[u8]` buffer can be obtained
-/// to pass to `minidump::Minidump::read`.
-///
-/// This code is taken from [`dwarfdump`](https://github.com/Vector35/binaryninja-api/blob/9d8bc846bd213407fb1a7a19af2a96f17501ac3b/rust/examples/dwarfdump/src/lib.rs#L81)
-/// in the Rust API examples.
-#[derive(Clone)]
-pub struct DataBufferWrapper {
- inner: Arc<DataBuffer>,
-}
-
-impl DataBufferWrapper {
- pub fn new(buf: DataBuffer) -> Self {
- DataBufferWrapper {
- inner: Arc::new(buf),
- }
- }
-}
-
-impl Deref for DataBufferWrapper {
- type Target = [u8];
- fn deref(&self) -> &Self::Target {
- self.inner.get_data()
- }
-}
-
/// The _Minidump_ binary view type, which the Rust plugin registers with the Binary Ninja core
/// (via `binaryninja::custombinaryview::register_view_type`) as a possible binary view
/// that can be applied to opened binaries.
@@ -141,9 +114,8 @@ impl MinidumpBinaryView {
fn init(&self) -> BinaryViewResult<()> {
let parent_view = self.parent_view()?;
let read_buffer = parent_view.read_buffer(0, parent_view.len())?;
- let read_buffer = DataBufferWrapper::new(read_buffer);
- if let Ok(minidump_obj) = Minidump::read(read_buffer) {
+ if let Ok(minidump_obj) = Minidump::read(read_buffer.get_data()) {
// Architecture, platform information
if let Ok(minidump_system_info) = minidump_obj.get_stream::<MinidumpSystemInfo>() {
if let Some(platform) = MinidumpBinaryView::translate_minidump_platform(
diff --git a/rust/examples/mlil_visitor/src/main.rs b/rust/examples/mlil_visitor/src/main.rs
index c38d59f4..a0cb8f0d 100644
--- a/rust/examples/mlil_visitor/src/main.rs
+++ b/rust/examples/mlil_visitor/src/main.rs
@@ -20,7 +20,7 @@ fn print_variable(func: &MediumLevelILFunction, var: &Variable) {
fn print_il_expr(instr: &MediumLevelILLiftedInstruction, mut indent: usize) {
print_indent(indent);
print_operation(instr);
- println!("");
+ println!();
indent += 1;