Skip to content

Commit ff094cf

Browse files
committed
More rust cleanup
- Use GroupId instead of u64 - Use ProgressCallback in place of ProgressExecutor - Misc cleanup of FileMetadata - Add `save_to_path` and `save_to_accessor` to save modified binaries - Added binary_view unit tests - Added collaboration unit tests - Fixed a few issues with the collaboration apis - Renamed Command registration functions so that there is no import ambiguity - Split out RemoteUndoEntry - Collaboration apis now have a explicit `_with_progress` set of apis - Misc clippy lint fixes
1 parent 2264e23 commit ff094cf

34 files changed

+1944
-1179
lines changed

plugins/dwarf/dwarf_export/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::fs;
1313
use binaryninja::logger::Logger;
1414
use binaryninja::{
1515
binary_view::{BinaryView, BinaryViewBase, BinaryViewExt},
16-
command::{register, Command},
16+
command::{register_command, Command},
1717
confidence::Conf,
1818
interaction,
1919
interaction::{FormResponses, FormResponses::Index},
@@ -781,7 +781,7 @@ pub extern "C" fn CorePluginInit() -> bool {
781781
.with_level(LevelFilter::Debug)
782782
.init();
783783

784-
register(
784+
register_command(
785785
"Export as DWARF",
786786
"Export current analysis state and annotations as DWARF for import into other tools",
787787
MyCommand {},

plugins/dwarf/dwarfdump/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use binaryninja::{
1616
binary_view::{BinaryView, BinaryViewExt},
17-
command::{register, Command},
17+
command::{register_command, Command},
1818
disassembly::{DisassemblyTextLine, InstructionTextToken, InstructionTextTokenKind},
1919
flowgraph::{BranchType, EdgeStyle, FlowGraph, FlowGraphNode, FlowGraphOption},
2020
};
@@ -325,7 +325,7 @@ impl Command for DWARFDump {
325325

326326
#[no_mangle]
327327
pub extern "C" fn UIPluginInit() -> bool {
328-
register(
328+
register_command(
329329
"DWARF Dump",
330330
"Show embedded DWARF info as a tree structure for you to navigate",
331331
DWARFDump {},

plugins/warp/src/plugin.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,67 +186,67 @@ pub extern "C" fn CorePluginInit() -> bool {
186186

187187
workflow::insert_workflow();
188188

189-
binaryninja::command::register(
189+
binaryninja::command::register_command(
190190
"WARP\\Run Matcher",
191191
"Run the matcher manually",
192192
workflow::RunMatcher {},
193193
);
194194

195-
binaryninja::command::register(
195+
binaryninja::command::register_command(
196196
"WARP\\Debug\\Cache",
197197
"Debug cache sizes... because...",
198198
DebugCache {},
199199
);
200200

201-
binaryninja::command::register(
201+
binaryninja::command::register_command(
202202
"WARP\\Debug\\Invalidate Caches",
203203
"Invalidate all WARP caches",
204204
DebugInvalidateCache {},
205205
);
206206

207-
binaryninja::command::register_for_function(
207+
binaryninja::command::register_command_for_function(
208208
"WARP\\Debug\\Function Signature",
209209
"Print the entire signature for the function",
210210
DebugFunction {},
211211
);
212212

213-
binaryninja::command::register_for_function(
213+
binaryninja::command::register_command_for_function(
214214
"WARP\\Debug\\Function Matcher",
215215
"Print all possible matches for the function",
216216
DebugMatcher {},
217217
);
218218

219-
binaryninja::command::register(
219+
binaryninja::command::register_command(
220220
"WARP\\Debug\\Apply Signature File Types",
221221
"Load all types from a signature file and ignore functions",
222222
types::LoadTypes {},
223223
);
224224

225-
binaryninja::command::register(
225+
binaryninja::command::register_command(
226226
"WARP\\Load Signature File",
227227
"Load file into the matcher, this does NOT kick off matcher analysis",
228228
load::LoadSignatureFile {},
229229
);
230230

231-
binaryninja::command::register_for_function(
231+
binaryninja::command::register_command_for_function(
232232
"WARP\\Copy Function GUID",
233233
"Copy the computed GUID for the function",
234234
copy::CopyFunctionGUID {},
235235
);
236236

237-
binaryninja::command::register(
237+
binaryninja::command::register_command(
238238
"WARP\\Find Function From GUID",
239239
"Locate the function in the view using a GUID",
240240
find::FindFunctionFromGUID {},
241241
);
242242

243-
binaryninja::command::register(
243+
binaryninja::command::register_command(
244244
"WARP\\Generate Signature File",
245245
"Generates a signature file containing all binary view functions",
246246
create::CreateSignatureFile {},
247247
);
248248

249-
binaryninja::command::register_for_function(
249+
binaryninja::command::register_command_for_function(
250250
"WARP\\Add Function Signature to File",
251251
"Stores the signature for the function in the signature file",
252252
add::AddFunctionSignature {},

rust/src/binary_view.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::function::{Function, NativeBlock};
3737
use crate::linear_view::{LinearDisassemblyLine, LinearViewCursor};
3838
use crate::metadata::Metadata;
3939
use crate::platform::Platform;
40-
use crate::progress::ProgressExecutor;
40+
use crate::progress::{NoProgressCallback, ProgressCallback};
4141
use crate::project::file::ProjectFile;
4242
use crate::rc::*;
4343
use crate::references::{CodeReference, DataReference};
@@ -145,11 +145,10 @@ pub trait BinaryViewBase: AsRef<BinaryView> {
145145
fn default_endianness(&self) -> Endianness;
146146
fn address_size(&self) -> usize;
147147

148-
// TODO saving fileaccessor
149148
fn save(&self) -> bool {
150149
self.as_ref()
151150
.parent_view()
152-
.map(|bv| bv.save())
151+
.map(|view| view.save())
153152
.unwrap_or(false)
154153
}
155154
}
@@ -627,38 +626,37 @@ pub trait BinaryViewExt: BinaryViewBase {
627626
T: Iterator<Item = I>,
628627
I: Into<QualifiedNameTypeAndId>,
629628
{
630-
self.define_auto_types_with_progress(names_sources_and_types, ProgressExecutor::default())
629+
self.define_auto_types_with_progress(names_sources_and_types, NoProgressCallback)
631630
}
632631

633-
fn define_auto_types_with_progress<T, I>(
632+
fn define_auto_types_with_progress<T, I, P>(
634633
&self,
635634
names_sources_and_types: T,
636-
progress: impl Into<ProgressExecutor>,
635+
mut progress: P,
637636
) -> HashMap<String, QualifiedName>
638637
where
639638
T: Iterator<Item = I>,
640639
I: Into<QualifiedNameTypeAndId>,
640+
P: ProgressCallback,
641641
{
642642
let mut types: Vec<BNQualifiedNameTypeAndId> = names_sources_and_types
643643
.map(Into::into)
644644
.map(QualifiedNameTypeAndId::into_raw)
645645
.collect();
646646
let mut result_ids: *mut *mut c_char = std::ptr::null_mut();
647647
let mut result_names: *mut BNQualifiedName = std::ptr::null_mut();
648-
let boxed_progress = Box::new(progress.into());
649-
let leaked_boxed_progress = Box::into_raw(boxed_progress);
648+
650649
let result_count = unsafe {
651650
BNDefineAnalysisTypes(
652651
self.as_ref().handle,
653652
types.as_mut_ptr(),
654653
types.len(),
655-
Some(ProgressExecutor::cb_execute),
656-
leaked_boxed_progress as *mut c_void,
654+
Some(P::cb_progress_callback),
655+
&mut progress as *mut P as *mut c_void,
657656
&mut result_ids as *mut _,
658657
&mut result_names as *mut _,
659658
)
660659
};
661-
let _ = unsafe { Box::from_raw(leaked_boxed_progress) };
662660

663661
for ty in types {
664662
QualifiedNameTypeAndId::free_raw(ty);
@@ -678,33 +676,30 @@ pub trait BinaryViewExt: BinaryViewBase {
678676
T: Iterator<Item = I>,
679677
I: Into<QualifiedNameAndType>,
680678
{
681-
self.define_user_types_with_progress(names_and_types, ProgressExecutor::default());
679+
self.define_user_types_with_progress(names_and_types, NoProgressCallback);
682680
}
683681

684-
fn define_user_types_with_progress<T, I>(
685-
&self,
686-
names_and_types: T,
687-
progress: impl Into<ProgressExecutor>,
688-
) where
682+
fn define_user_types_with_progress<T, I, P>(&self, names_and_types: T, mut progress: P)
683+
where
689684
T: Iterator<Item = I>,
690685
I: Into<QualifiedNameAndType>,
686+
P: ProgressCallback,
691687
{
692688
let mut types: Vec<BNQualifiedNameAndType> = names_and_types
693689
.map(Into::into)
694690
.map(QualifiedNameAndType::into_raw)
695691
.collect();
696-
let boxed_progress = Box::new(progress.into());
697-
let leaked_boxed_progress = Box::into_raw(boxed_progress);
692+
698693
unsafe {
699694
BNDefineUserAnalysisTypes(
700695
self.as_ref().handle,
701696
types.as_mut_ptr(),
702697
types.len(),
703-
Some(ProgressExecutor::cb_execute),
704-
leaked_boxed_progress as *mut c_void,
698+
Some(P::cb_progress_callback),
699+
&mut progress as *mut P as *mut c_void,
705700
)
706701
};
707-
let _ = unsafe { Box::from_raw(leaked_boxed_progress) };
702+
708703
for ty in types {
709704
QualifiedNameAndType::free_raw(ty);
710705
}
@@ -1865,8 +1860,7 @@ impl BinaryView {
18651860
}
18661861

18671862
pub fn from_accessor(meta: &FileMetadata, file: &mut FileAccessor) -> Result<Ref<Self>> {
1868-
let handle =
1869-
unsafe { BNCreateBinaryDataViewFromFile(meta.handle, &mut file.api_object as *mut _) };
1863+
let handle = unsafe { BNCreateBinaryDataViewFromFile(meta.handle, &mut file.api_object) };
18701864

18711865
if handle.is_null() {
18721866
return Err(());
@@ -1886,6 +1880,17 @@ impl BinaryView {
18861880

18871881
unsafe { Ok(Ref::new(Self { handle })) }
18881882
}
1883+
1884+
/// Save the original binary file to the provided `file_path` along with any modifications.
1885+
pub fn save_to_path(&self, file_path: impl AsRef<Path>) -> bool {
1886+
let file = file_path.as_ref().into_bytes_with_nul();
1887+
unsafe { BNSaveToFilename(self.handle, file.as_ptr() as *mut _) }
1888+
}
1889+
1890+
/// Save the original binary file to the provided [`FileAccessor`] along with any modifications.
1891+
pub fn save_to_accessor(&self, file: &mut FileAccessor) -> bool {
1892+
unsafe { BNSaveToFile(self.handle, &mut file.api_object) }
1893+
}
18891894
}
18901895

18911896
impl BinaryViewBase for BinaryView {

0 commit comments

Comments
 (0)