diff --git a/rust/examples/decompile/src/main.rs b/rust/examples/decompile/src/main.rs index 0865538da..cac5ae06b 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 ef71f1ae7..057abe27d 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 b1b91c1dc..537051c89 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, - commit: bool, - ) { + pub(crate) fn add_type(&mut self, type_uid: TypeUID, name: String, t: Ref, 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 068094280..8aeac6587 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>( } } } else { - namespace_qualifiers - .push((depth, "anonymous_namespace".to_string())); + namespace_qualifiers.push((depth, "anonymous_namespace".to_string())); } } @@ -129,22 +128,24 @@ fn recover_names>( 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::>() - .join("::"), - ) - .to_string(), + simplify_str_to_str( + namespace_qualifiers + .iter() + .map(|(_, namespace)| namespace.to_owned()) + .collect::>() + .join("::"), + ) + .to_string(), ); } constants::DW_TAG_typedef @@ -153,17 +154,15 @@ fn recover_names>( 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::>() - .join("::"), - ) - .to_string(), + simplify_str_to_str( + namespace_qualifiers + .iter() + .chain(vec![&(-1, name)].into_iter()) + .map(|(_, namespace)| namespace.to_owned()) + .collect::>() + .join("::"), + ) + .to_string(), ); } } diff --git a/rust/examples/dwarf/dwarfdump/src/lib.rs b/rust/examples/dwarf/dwarfdump/src/lib.rs index c062ac63d..2cfbbbce9 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( 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 71c283a85..e1bdefb0a 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 0b10c65ad..d33a8d0bb 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::() { 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 9eee6aa6d..bbbe0bd5e 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 = binaryninja::binaryview::Result; -/// 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, -} - -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::() { 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 c38d59f48..a0cb8f0d7 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; diff --git a/rust/src/binaryview.rs b/rust/src/binaryview.rs index 6e5106949..70b1cab0e 100644 --- a/rust/src/binaryview.rs +++ b/rust/src/binaryview.rs @@ -1584,11 +1584,9 @@ where ctx: *mut ::std::os::raw::c_void, view: *mut BNBinaryView, ) { - ffi_wrap!("EventHandler::on_event", unsafe { - let mut context = &mut *(ctx as *mut Handler); - - let handle = BinaryView::from_raw(BNNewViewReference(view)); - Handler::on_event(&mut context, handle.as_ref()); + ffi_wrap!("EventHandler::on_event", { + let context = unsafe { &*(ctx as *const Handler) }; + context.on_event(&BinaryView::from_raw(BNNewViewReference(view))); }) } diff --git a/rust/src/flowgraph.rs b/rust/src/flowgraph.rs index c2d4d559e..c8505187a 100644 --- a/rust/src/flowgraph.rs +++ b/rust/src/flowgraph.rs @@ -68,7 +68,7 @@ impl<'a> FlowGraphNode<'a> { unsafe { FlowGraphNode::from_raw(BNCreateFlowGraphNode(graph.handle)) } } - pub fn set_disassembly_lines(&self, lines: &'a Vec) { + pub fn set_disassembly_lines(&self, lines: &'a [DisassemblyTextLine]) { unsafe { BNSetFlowGraphNodeLines(self.handle, lines.as_ptr() as *mut _, lines.len()); // BNFreeDisassemblyTextLines(lines.as_ptr() as *mut _, lines.len()); // Shouldn't need...would be a double free? @@ -79,7 +79,7 @@ impl<'a> FlowGraphNode<'a> { let lines = lines .iter() .map(|&line| DisassemblyTextLine::from(&vec![line])) - .collect(); + .collect::>(); self.set_disassembly_lines(&lines); } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 0d385746e..739bfbf68 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -74,16 +74,12 @@ //! ### `main.rs` //! Standalone binaries need to initialize Binary Ninja before they can work. You can do this through [`headless::Session`], [`headless::script_helper`], or [`headless::init()`] at start and [`headless::shutdown()`] at shutdown. //! ```rust -//! fn main() { -//! // This loads all the core architecture, platform, etc plugins -//! // Standalone executables need to call this, but plugins do not -//! let headless_session = binaryninja::headless::Session::new(); +//! // This loads all the core architecture, platform, etc plugins +//! // Standalone executables need to call this, but plugins do not +//! let headless_session = binaryninja::headless::Session::new(); //! -//! println!("Loading binary..."); -//! let bv = headless_session.load("/bin/cat").expect("Couldn't open `/bin/cat`"); -//! -//! // Your code here... -//! } +//! println!("Loading binary..."); +//! let bv = headless_session.load("/bin/cat").expect("Couldn't open `/bin/cat`"); //! ``` //! //! ### `Cargo.toml` diff --git a/rust/src/llil/instruction.rs b/rust/src/llil/instruction.rs index 469c10fb0..62e50453f 100644 --- a/rust/src/llil/instruction.rs +++ b/rust/src/llil/instruction.rs @@ -99,7 +99,7 @@ where let expr_idx = unsafe { BNGetLowLevelILIndexForInstruction(self.function.handle, self.instr_idx) }; let op = unsafe { BNGetLowLevelILByIndex(self.function.handle, expr_idx) }; - return op.address; + op.address } pub fn info(&self) -> InstrInfo<'func, A, M, NonSSA> { diff --git a/rust/src/string.rs b/rust/src/string.rs index 75942da91..96830ca56 100644 --- a/rust/src/string.rs +++ b/rust/src/string.rs @@ -94,6 +94,10 @@ impl BnString { pub fn len(&self) -> usize { self.as_ref().len() } + + pub fn is_empty(&self) -> bool { + self.as_ref().is_empty() + } } impl Drop for BnString {