Skip to content

Commit 556918b

Browse files
committed
More rust cleanup
- Improved binary view initialization (see init_with_opts) - Allow floating license to free itself before initialization - Add initialization unit test - Add better Debug impls for some common types - Use Path api for opening binary views, this is not breaking as it uses the AsRef impl - Bump rayon dependency and constrain dependencies to x.x
1 parent 098c507 commit 556918b

File tree

10 files changed

+312
-158
lines changed

10 files changed

+312
-158
lines changed

rust/Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ rust-version = "1.83.0"
99
# This is used when statically linking to prevent exporting CorePluginABIVersion and UiPluginABIVersion.
1010
no_exports = []
1111

12-
1312
[dependencies]
1413
log = { version = "0.4", features = ["std"] }
15-
rayon = { version = "1.8", optional = true }
14+
rayon = { version = "1.10", optional = true }
1615
binaryninjacore-sys = { path = "binaryninjacore-sys" }
1716
thiserror = "2.0"
1817

1918
[dev-dependencies]
20-
rstest = "0.24.0"
21-
tempfile = "3"
19+
rstest = "0.24"
20+
tempfile = "3.15"

rust/src/binaryview.rs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,28 @@ use crate::function::{Function, NativeBlock};
3939
use crate::linearview::{LinearDisassemblyLine, LinearViewCursor};
4040
use crate::metadata::Metadata;
4141
use crate::platform::Platform;
42+
use crate::rc::*;
43+
use crate::references::{CodeReference, DataReference};
4244
use crate::relocation::Relocation;
4345
use crate::section::{Section, SectionBuilder};
4446
use crate::segment::{Segment, SegmentBuilder};
4547
use crate::settings::Settings;
48+
use crate::string::*;
4649
use crate::symbol::{Symbol, SymbolType};
4750
use crate::tags::{Tag, TagType};
51+
use crate::typecontainer::TypeContainer;
4852
use crate::typelibrary::TypeLibrary;
4953
use crate::types::{
5054
NamedTypeReference, QualifiedName, QualifiedNameAndType, QualifiedNameTypeAndId, Type,
5155
};
56+
use crate::variable::DataVariable;
5257
use crate::Endianness;
5358
use std::collections::HashMap;
5459
use std::ffi::{c_char, c_void};
5560
use std::ops::Range;
61+
use std::path::Path;
5662
use std::ptr::NonNull;
5763
use std::{ops, result, slice};
58-
59-
use crate::rc::*;
60-
use crate::references::{CodeReference, DataReference};
61-
use crate::string::*;
62-
use crate::typecontainer::TypeContainer;
63-
use crate::variable::DataVariable;
6464
// TODO : general reorg of modules related to bv
6565

6666
pub type Result<R> = result::Result<R, ()>;
@@ -190,7 +190,6 @@ pub trait BinaryViewExt: BinaryViewBase {
190190
fn file(&self) -> Ref<FileMetadata> {
191191
unsafe {
192192
let raw = BNGetFileForView(self.as_ref().handle);
193-
194193
Ref::new(FileMetadata::from_raw(raw))
195194
}
196195
}
@@ -1774,15 +1773,10 @@ impl BinaryView {
17741773
Ref::new(Self { handle })
17751774
}
17761775

1777-
pub fn from_filename<S: BnStrCompatible>(
1778-
meta: &mut FileMetadata,
1779-
filename: S,
1780-
) -> Result<Ref<Self>> {
1781-
let file = filename.into_bytes_with_nul();
1782-
1783-
let handle = unsafe {
1784-
BNCreateBinaryDataViewFromFilename(meta.handle, file.as_ref().as_ptr() as *mut _)
1785-
};
1776+
pub fn from_path(meta: &mut FileMetadata, file_path: impl AsRef<Path>) -> Result<Ref<Self>> {
1777+
let file = file_path.as_ref().into_bytes_with_nul();
1778+
let handle =
1779+
unsafe { BNCreateBinaryDataViewFromFilename(meta.handle, file.as_ptr() as *mut _) };
17861780

17871781
if handle.is_null() {
17881782
return Err(());
@@ -1920,13 +1914,25 @@ unsafe impl Sync for BinaryView {}
19201914

19211915
impl std::fmt::Debug for BinaryView {
19221916
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1923-
write!(
1924-
f,
1925-
"BinaryView (type: `{}`): '{}', len {:#x}",
1926-
self.view_type(),
1927-
self.file().filename(),
1928-
self.len()
1929-
)
1917+
f.debug_struct("BinaryView")
1918+
.field("type_name", &self.type_name())
1919+
.field("file", &self.file())
1920+
.field("original_image_base", &self.original_image_base())
1921+
.field("start", &self.start())
1922+
.field("end", &self.end())
1923+
.field("len", &self.len())
1924+
.field("default_platform", &self.default_platform())
1925+
.field("default_arch", &self.default_arch())
1926+
.field("default_endianness", &self.default_endianness())
1927+
.field("entry_point", &self.entry_point())
1928+
.field(
1929+
"entry_point_functions",
1930+
&self.entry_point_functions().to_vec(),
1931+
)
1932+
.field("address_size", &self.address_size())
1933+
.field("sections", &self.sections().to_vec())
1934+
.field("segments", &self.segments().to_vec())
1935+
.finish()
19301936
}
19311937
}
19321938

rust/src/component.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ use crate::variable::DataVariable;
1010
use binaryninjacore_sys::*;
1111

1212
pub struct ComponentBuilder {
13+
// TODO: This should be a ref
1314
bv: *mut BNBinaryView,
15+
// TODO: Make owned string
1416
parent: Option<BnString>,
17+
// TODO: Make owned string
1518
name: Option<BnString>,
1619
}
1720

@@ -75,6 +78,7 @@ impl Component {
7578
Self { handle }
7679
}
7780

81+
// TODO: Ref<>
7882
pub(crate) unsafe fn ref_from_raw(handle: &*mut BNComponent) -> &Self {
7983
assert!(!handle.is_null());
8084
mem::transmute(handle)

rust/src/enterprise.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub enum EnterpriseCheckoutError {
1818
RefreshExpiredLicenseFailed,
1919
}
2020

21+
/// Initialize the enterprise server connection to check out a floating license.
2122
pub fn checkout_license(duration: Duration) -> Result<(), EnterpriseCheckoutError> {
2223
if crate::is_ui_enabled() {
2324
// We only need to check out a license if running headlessly.
@@ -70,6 +71,16 @@ pub fn checkout_license(duration: Duration) -> Result<(), EnterpriseCheckoutErro
7071

7172
pub fn release_license() {
7273
if !crate::is_ui_enabled() {
74+
// This might look dumb, why would we want to connect to the server, would that not just mean
75+
// we don't need to release the license? Well no, you could have run a script, acquired a license for 10 hours
76+
// then you WOULD want to call release license, and your expectation is that acquired license
77+
// will now be released. To release that you must have an active connection which is what this does.
78+
if !is_server_initialized() {
79+
initialize_server();
80+
}
81+
if !is_server_connected() {
82+
connect_server();
83+
}
7384
// We should only release the license if we are running headlessly.
7485
release_server_license();
7586
}

rust/src/filemetadata.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use crate::binaryview::BinaryView;
16+
use crate::database::Database;
17+
use crate::project::ProjectFile;
1518
use binaryninjacore_sys::{
1619
BNBeginUndoActions, BNCloseFile, BNCommitUndoActions, BNCreateDatabase, BNCreateFileMetadata,
1720
BNFileMetadata, BNFileMetadataGetSessionId, BNFreeFileMetadata, BNGetCurrentOffset,
@@ -23,10 +26,7 @@ use binaryninjacore_sys::{
2326
};
2427
use binaryninjacore_sys::{BNCreateDatabaseWithProgress, BNOpenExistingDatabaseWithProgress};
2528
use std::ffi::c_void;
26-
27-
use crate::binaryview::BinaryView;
28-
use crate::database::Database;
29-
use crate::project::ProjectFile;
29+
use std::fmt::Debug;
3030

3131
use crate::rc::*;
3232
use crate::string::*;
@@ -38,9 +38,6 @@ pub struct FileMetadata {
3838
pub(crate) handle: *mut BNFileMetadata,
3939
}
4040

41-
unsafe impl Send for FileMetadata {}
42-
unsafe impl Sync for FileMetadata {}
43-
4441
impl FileMetadata {
4542
pub(crate) fn from_raw(handle: *mut BNFileMetadata) -> Self {
4643
Self { handle }
@@ -285,6 +282,22 @@ impl FileMetadata {
285282
}
286283
}
287284

285+
impl Debug for FileMetadata {
286+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
287+
f.debug_struct("FileMetadata")
288+
.field("filename", &self.filename())
289+
.field("session_id", &self.session_id())
290+
.field("modified", &self.modified())
291+
.field("is_analysis_changed", &self.is_analysis_changed())
292+
.field("current_view", &self.current_view())
293+
.field("current_view", &self.current_offset())
294+
.finish()
295+
}
296+
}
297+
298+
unsafe impl Send for FileMetadata {}
299+
unsafe impl Sync for FileMetadata {}
300+
288301
impl ToOwned for FileMetadata {
289302
type Owned = Ref<Self>;
290303

0 commit comments

Comments
 (0)