Skip to content

Commit 5b36220

Browse files
committed
Improve initialization in rust headless scripts
Provide sensible errors and validation to rust headless scripts, solves #5796
1 parent 3d1cfff commit 5b36220

27 files changed

+198
-121
lines changed

plugins/pdb-ng/src/lib.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,13 @@ fn active_local_cache(view: Option<&BinaryView>) -> Result<String> {
9191
.get_string("pdb.files.localStoreAbsolute", view, None)
9292
.to_string();
9393
if local_store_path.is_empty() {
94-
local_store_path = match user_directory() {
95-
Ok(mut dir) => {
96-
dir.push(
97-
Settings::new("")
98-
.get_string("pdb.files.localStoreRelative", view, None)
99-
.to_string(),
100-
);
101-
match dir.to_str() {
102-
Some(s) => s.to_string(),
103-
_ => "".to_string(),
104-
}
105-
}
106-
_ => "".to_string(),
107-
};
94+
let relative_local_store = Settings::new("")
95+
.get_string("pdb.files.localStoreRelative", view, None)
96+
.to_string();
97+
local_store_path = user_directory()
98+
.join(relative_local_store)
99+
.to_string_lossy()
100+
.to_string();
108101
}
109102
if !local_store_path.is_empty() {
110103
Ok(local_store_path)

plugins/warp/benches/convert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::path::PathBuf;
55
use warp_ninja::convert::from_bn_type;
66

77
pub fn type_conversion_benchmark(c: &mut Criterion) {
8-
let session = Session::new();
8+
let session = Session::new().expect("Failed to initialize session");
99
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
1010
for entry in std::fs::read_dir(out_dir).expect("Failed to read OUT_DIR") {
1111
let entry = entry.expect("Failed to read directory entry");

plugins/warp/benches/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use warp_ninja::build_function;
66
use warp_ninja::cache::FunctionCache;
77

88
pub fn function_benchmark(c: &mut Criterion) {
9-
let session = Session::new();
9+
let session = Session::new().expect("Failed to initialize session");
1010
let bv = session.load(env!("TEST_BIN_LIBRARY_OBJ")).unwrap();
1111
let functions = bv.functions();
1212
assert_eq!(functions.len(), 6);

plugins/warp/benches/guid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use criterion::{criterion_group, criterion_main, Criterion};
44
use warp_ninja::function_guid;
55

66
pub fn guid_benchmark(c: &mut Criterion) {
7-
let session = Session::new();
7+
let session = Session::new().expect("Failed to initialize session");
88
let bv = session.load(env!("TEST_BIN_LIBRARY_OBJ")).unwrap();
99
let functions = bv.functions();
1010
assert_eq!(functions.len(), 6);

plugins/warp/src/bin/sigem.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ fn main() {
9292
}
9393

9494
log::debug!("Starting Binary Ninja session...");
95-
let _headless_session = binaryninja::headless::Session::new();
95+
let _headless_session =
96+
binaryninja::headless::Session::new().expect("Failed to initialize session");
9697

9798
// Adjust the amount of worker threads so that we can actually free BinaryViews.
9899
let bn_settings = Settings::new("");
@@ -258,7 +259,8 @@ mod tests {
258259
env_logger::init();
259260
// TODO: Store oracles here to get more out of this test.
260261
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
261-
let _headless_session = binaryninja::headless::Session::new();
262+
let _headless_session =
263+
binaryninja::headless::Session::new().expect("Failed to initialize session");
262264
let bn_settings = Settings::new("");
263265
let settings = default_settings(&bn_settings);
264266
for entry in std::fs::read_dir(out_dir).expect("Failed to read OUT_DIR") {

plugins/warp/src/convert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ mod tests {
582582
static INIT: OnceLock<Session> = OnceLock::new();
583583

584584
fn get_session<'a>() -> &'a Session {
585-
INIT.get_or_init(|| Session::new())
585+
INIT.get_or_init(|| Session::new().expect("Failed to initialize session"))
586586
}
587587

588588
#[test]

plugins/warp/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ mod plugin;
2828

2929
pub fn core_signature_dir() -> PathBuf {
3030
// Get core signatures for the given platform
31-
let install_dir = binaryninja::install_directory().unwrap();
31+
let install_dir = binaryninja::install_directory();
3232
// macOS core dir is separate from the install dir.
3333
#[cfg(target_os = "macos")]
3434
let core_dir = install_dir.parent().unwrap().join("Resources");
@@ -38,7 +38,7 @@ pub fn core_signature_dir() -> PathBuf {
3838
}
3939

4040
pub fn user_signature_dir() -> PathBuf {
41-
binaryninja::user_directory().unwrap().join("signatures/")
41+
binaryninja::user_directory().join("signatures/")
4242
}
4343

4444
pub fn build_function<A: Architecture, M: FunctionMutability, V: NonSSAVariant>(
@@ -185,7 +185,7 @@ mod tests {
185185

186186
fn get_session<'a>() -> &'a Session {
187187
// TODO: This is not shared between other test modules, should still be fine (mutex in core now).
188-
INIT.get_or_init(|| Session::new())
188+
INIT.get_or_init(|| Session::new().expect("Failed to initialize session"))
189189
}
190190

191191
#[test]

rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ no_exports = []
1414
log = { version = "0.4", features = ["std"] }
1515
rayon = { version = "1.8", optional = true }
1616
binaryninjacore-sys = { path = "binaryninjacore-sys" }
17+
thiserror = "2.0"
1718

1819
[dev-dependencies]
1920
rstest = "0.24.0"

rust/examples/decompile.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ pub fn main() {
3131

3232
println!("Starting session...");
3333
// This loads all the core architecture, platform, etc plugins
34-
let headless_session = binaryninja::headless::Session::new();
34+
let headless_session =
35+
binaryninja::headless::Session::new().expect("Failed to initialize session");
3536

3637
println!("Loading binary...");
3738
let bv = headless_session

rust/examples/demangler.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ impl CustomDemangler for TestDemangler {
2828
fn main() {
2929
println!("Starting session...");
3030
// This loads all the core architecture, platform, etc plugins
31-
let _headless_session = binaryninja::headless::Session::new();
31+
let _headless_session =
32+
binaryninja::headless::Session::new().expect("Failed to initialize session");
3233

3334
println!("Registering demangler...");
3435
Demangler::register("Test", TestDemangler);

0 commit comments

Comments
 (0)