Skip to content

Commit

Permalink
More rust cleanup
Browse files Browse the repository at this point in the history
- Updated README to clarify offline documentation
- Refactored settings api
- Added settings example to show dumping settings value and specific properties
- Use the workspace to depend on binaryninja and binaryninjacore-sys
- Remove binaryninjacore-sys as a workspace member (its not really required)
  • Loading branch information
emesare committed Jan 19, 2025
1 parent 72c98dc commit 05b7a18
Show file tree
Hide file tree
Showing 32 changed files with 475 additions and 288 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
resolver = "2"
members = [
"rust",
"rust/binaryninjacore-sys",
"arch/riscv",
"arch/msp430",
"view/bintxt",
Expand All @@ -20,6 +19,10 @@ members = [
"plugins/warp"
]

[workspace.dependencies]
binaryninja = { path = "rust" }
binaryninjacore-sys = { path = "rust/binaryninjacore-sys" }

[profile.release]
lto = true
debug = "full"
4 changes: 2 additions & 2 deletions arch/msp430/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ authors = ["jrozner"]
edition = "2021"

[dependencies]
binaryninja = { path = "../../rust" }
binaryninjacore-sys = { path = "../../rust/binaryninjacore-sys" }
binaryninja.workspace = true
binaryninjacore-sys.workspace = true
log = "0.4"
msp430-asm = "^0.2"

Expand Down
4 changes: 2 additions & 2 deletions arch/riscv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ authors = ["Ryan Snyder <[email protected]>"]
edition = "2021"

[dependencies]
binaryninja = { path = "../../rust" }
binaryninjacore-sys = { path = "../../rust/binaryninjacore-sys" }
binaryninja.workspace = true
binaryninjacore-sys.workspace = true
riscv-dis = { path = "disasm" }
log = "0.4"
rayon = { version = "1.0", optional = true }
Expand Down
6 changes: 3 additions & 3 deletions plugins/dwarf/dwarf_export/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
binaryninja = { path = "../../../rust" }
binaryninjacore-sys = { path = "../../../rust/binaryninjacore-sys" }
binaryninja.workspace = true
binaryninjacore-sys.workspace = true
gimli = "^0.31"
log = "^0.4"
log = "0.4"
object = { version = "0.32.1", features = ["write"] }
6 changes: 3 additions & 3 deletions plugins/dwarf/dwarf_import/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ crate-type = ["cdylib"]

[dependencies]
dwarfreader = { path = "../shared/" }
binaryninja = { path = "../../../rust" }
binaryninjacore-sys = { path = "../../../rust/binaryninjacore-sys" }
binaryninja.workspace = true
binaryninjacore-sys.workspace = true
gimli = "0.31"
log = "0.4.20"
log = "0.4"
iset = "0.2.2"
cpp_demangle = "0.4.3"
regex = "1"
Expand Down
35 changes: 22 additions & 13 deletions plugins/dwarf/dwarf_import/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use gimli::{
DebuggingInformationEntry, Operation, Unit, UnitOffset, UnitSectionOffset,
};

use binaryninja::settings::QueryOptions;
use log::warn;

pub(crate) fn get_uid<R: ReaderType>(
Expand Down Expand Up @@ -414,9 +415,10 @@ pub(crate) fn download_debug_info(
build_id: &str,
view: &BinaryView,
) -> Result<Ref<BinaryView>, String> {
let settings = Settings::new("");

let debug_server_urls = settings.get_string_list("network.debuginfodServers", Some(view), None);
let mut settings_query_opts = QueryOptions::new_with_view(view);
let settings = Settings::new();
let debug_server_urls =
settings.get_string_list_with_opts("network.debuginfodServers", &mut settings_query_opts);

for debug_server_url in debug_server_urls.iter() {
let artifact_url = format!("{}/buildid/{}/debuginfo", debug_server_url, build_id);
Expand Down Expand Up @@ -487,19 +489,21 @@ pub(crate) fn find_local_debug_file_for_build_id(
build_id: &str,
view: &BinaryView,
) -> Option<String> {
let settings = Settings::new("");
let debug_dirs_enabled = settings.get_bool(
let mut settings_query_opts = QueryOptions::new_with_view(view);
let settings = Settings::new();
let debug_dirs_enabled = settings.get_bool_with_opts(
"analysis.debugInfo.enableDebugDirectories",
Some(view),
None,
&mut settings_query_opts,
);

if !debug_dirs_enabled {
return None;
}

let debug_info_paths =
settings.get_string_list("analysis.debugInfo.debugDirectories", Some(view), None);
let debug_info_paths = settings.get_string_list_with_opts(
"analysis.debugInfo.debugDirectories",
&mut settings_query_opts,
);

if debug_info_paths.is_empty() {
return None;
Expand Down Expand Up @@ -530,6 +534,8 @@ pub(crate) fn load_debug_info_for_build_id(
build_id: &str,
view: &BinaryView,
) -> (Option<Ref<BinaryView>>, bool) {
let mut settings_query_opts = QueryOptions::new_with_view(view);
let settings = Settings::new();
if let Some(debug_file_path) = find_local_debug_file_for_build_id(build_id, view) {
return (
binaryninja::load_with_options(
Expand All @@ -539,16 +545,19 @@ pub(crate) fn load_debug_info_for_build_id(
),
false,
);
} else if Settings::new("").get_bool("network.enableDebuginfod", Some(view), None) {
} else if settings.get_bool_with_opts("network.enableDebuginfod", &mut settings_query_opts) {
return (download_debug_info(build_id, view).ok(), true);
}
(None, false)
}

pub(crate) fn find_sibling_debug_file(view: &BinaryView) -> Option<String> {
let settings = Settings::new("");
let load_sibling_debug =
settings.get_bool("analysis.debugInfo.loadSiblingDebugFiles", Some(view), None);
let mut settings_query_opts = QueryOptions::new_with_view(view);
let settings = Settings::new();
let load_sibling_debug = settings.get_bool_with_opts(
"analysis.debugInfo.loadSiblingDebugFiles",
&mut settings_query_opts,
);

if !load_sibling_debug {
return None;
Expand Down
2 changes: 1 addition & 1 deletion plugins/dwarf/dwarf_import/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ impl CustomDebugInfoParser for DWARFParser {
pub extern "C" fn CorePluginInit() -> bool {
Logger::new("DWARF").init();

let settings = Settings::new("");
let settings = Settings::new();

settings.register_setting_json(
"network.enableDebuginfod",
Expand Down
4 changes: 2 additions & 2 deletions plugins/dwarf/dwarfdump/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ crate-type = ["cdylib"]

[dependencies]
dwarfreader = { path = "../shared/" }
binaryninja = { path = "../../../rust" }
binaryninjacore-sys = { path = "../../../rust/binaryninjacore-sys" }
binaryninja.workspace = true
binaryninjacore-sys.workspace = true
gimli = "0.31"
File renamed without changes.
4 changes: 2 additions & 2 deletions plugins/dwarf/shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ authors = ["Kyle Martin <[email protected]>"]
edition = "2021"

[dependencies]
binaryninja = { path = "../../../rust" }
binaryninjacore-sys = { path = "../../../rust/binaryninjacore-sys" }
binaryninja.workspace = true
binaryninjacore-sys.workspace = true
gimli = "0.31"
zstd = "0.13.2"
thiserror = "1.0"
5 changes: 3 additions & 2 deletions plugins/dwarf/shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use binaryninja::{
Endianness,
};

use binaryninja::settings::QueryOptions;
use std::rc::Rc;

//////////////////////
// Dwarf Validation

Expand Down Expand Up @@ -63,8 +63,9 @@ pub fn is_raw_dwo_dwarf(view: &BinaryView) -> bool {
}

pub fn can_use_debuginfod(view: &BinaryView) -> bool {
let mut query_options = QueryOptions::new_with_view(view);
has_build_id_section(view)
&& Settings::new("").get_bool("network.enableDebuginfod", Some(view), None)
&& Settings::new().get_bool_with_opts("network.enableDebuginfod", &mut query_options)
}

pub fn has_build_id_section(view: &BinaryView) -> bool {
Expand Down
6 changes: 3 additions & 3 deletions plugins/idb_import/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ crate-type = ["cdylib"]

[dependencies]
anyhow = { version = "1.0.86", features = ["backtrace"] }
binaryninja = { path = "../../rust" }
binaryninjacore-sys = { path = "../../rust/binaryninjacore-sys" }
binaryninja.workspace = true
binaryninjacore-sys.workspace = true
idb-rs = { git = "https://github.com/Vector35/idb-rs", tag = "0.1.6" }
log = "0.4.20"
log = "0.4"
6 changes: 3 additions & 3 deletions plugins/pdb-ng/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ crate-type = ["cdylib"]

[dependencies]
anyhow = "^1.0"
binaryninja = { path = "../../rust" }
binaryninjacore-sys = { path = "../../rust/binaryninjacore-sys" }
binaryninja.workspace = true
binaryninjacore-sys.workspace = true
itertools = "^0.11"
log = "^0.4"
log = "0.4"
pdb = { git = "https://github.com/Vector35/pdb-rs", rev = "6016177" }
regex = "1"

Expand Down
2 changes: 1 addition & 1 deletion plugins/pdb-ng/demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ anyhow = "^1.0"
binaryninja = {path = "../../../"}
home = "^0.5.5"
itertools = "^0.11"
log = "^0.4"
log = "0.4"
pdb = { git = "https://github.com/Vector35/pdb-rs", branch = "master" }
cab = "^0.4"
regex = "1"
Expand Down
38 changes: 24 additions & 14 deletions plugins/pdb-ng/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use binaryninja::debuginfo::{CustomDebugInfoParser, DebugInfo, DebugInfoParser};
use binaryninja::download_provider::{DownloadInstanceInputOutputCallbacks, DownloadProvider};
use binaryninja::interaction::{MessageBoxButtonResult, MessageBoxButtonSet};
use binaryninja::logger::Logger;
use binaryninja::settings::Settings;
use binaryninja::settings::{QueryOptions, Settings};
use binaryninja::string::BnString;
use binaryninja::{interaction, user_directory};
use parser::PDBParserInstance;
Expand Down Expand Up @@ -87,12 +87,14 @@ fn default_local_cache() -> Result<String> {

fn active_local_cache(view: Option<&BinaryView>) -> Result<String> {
// Check the local symbol store
let mut local_store_path = Settings::new("")
.get_string("pdb.files.localStoreAbsolute", view, None)
let mut settings_query_options = view.map(QueryOptions::new_with_view).unwrap_or_default();
let settings = Settings::new();
let mut local_store_path = settings
.get_string_with_opts("pdb.files.localStoreAbsolute", &mut settings_query_options)
.to_string();
if local_store_path.is_empty() {
let relative_local_store = Settings::new("")
.get_string("pdb.files.localStoreRelative", view, None)
let relative_local_store = settings
.get_string_with_opts("pdb.files.localStoreRelative", &mut settings_query_options)
.to_string();
local_store_path = user_directory()
.join(relative_local_store)
Expand Down Expand Up @@ -168,7 +170,8 @@ fn read_from_sym_store(bv: &BinaryView, path: &str) -> Result<(bool, Vec<u8>)> {
return Ok((false, conts));
}

if !Settings::new("").get_bool("network.pdbAutoDownload", Some(bv), None) {
let mut query_options = QueryOptions::new_with_view(bv);
if !Settings::new().get_bool_with_opts("network.pdbAutoDownload", &mut query_options) {
return Err(anyhow!("Auto download disabled"));
}

Expand Down Expand Up @@ -359,16 +362,19 @@ impl PDBParser {
) -> Result<()> {
let mut pdb = PDB::open(Cursor::new(&conts))?;

let settings = Settings::new("");
let settings = Settings::new();
let mut settings_query_opts = QueryOptions::new_with_view(view);

if let Some(info) = parse_pdb_info(view) {
let pdb_info = &pdb.pdb_information()?;
if info.guid.as_slice() != pdb_info.guid.as_bytes() {
if check_guid {
return Err(anyhow!("PDB GUID does not match"));
} else {
let ask =
settings.get_string("pdb.features.loadMismatchedPDB", Some(view), None);
let ask = settings.get_string_with_opts(
"pdb.features.loadMismatchedPDB",
&mut settings_query_opts,
);

match ask.as_str() {
"true" => {},
Expand Down Expand Up @@ -400,7 +406,7 @@ impl PDBParser {
}
}

if did_download && settings.get_bool("pdb.files.localStoreCache", None, None) {
if did_download && settings.get_bool("pdb.files.localStoreCache") {
match active_local_cache(Some(view)) {
Ok(cache) => {
let mut cab_path = PathBuf::from(&cache);
Expand Down Expand Up @@ -479,7 +485,10 @@ impl PDBParser {
if check_guid {
return Err(anyhow!("File not compiled with PDB information"));
} else {
let ask = settings.get_string("pdb.features.loadMismatchedPDB", Some(view), None);
let ask = settings.get_string_with_opts(
"pdb.features.loadMismatchedPDB",
&mut settings_query_opts,
);

match ask.as_str() {
"true" => {},
Expand Down Expand Up @@ -644,8 +653,9 @@ impl CustomDebugInfoParser for PDBParser {
}

// Next, try downloading from all symbol servers in the server list
let server_list =
Settings::new("").get_string_list("pdb.files.symbolServerList", Some(view), None);
let mut query_options = QueryOptions::new_with_view(view);
let server_list = Settings::new()
.get_string_list_with_opts("pdb.files.symbolServerList", &mut query_options);

for server in server_list.iter() {
match search_sym_store(view, server.to_string(), &info) {
Expand Down Expand Up @@ -688,7 +698,7 @@ fn init_plugin() -> bool {
Logger::new("PDB").init();
DebugInfoParser::register("PDB", PDBParser {});

let settings = Settings::new("");
let settings = Settings::new();
settings.register_group("pdb", "PDB Loader");
settings.register_setting_json(
"pdb.files.localStoreAbsolute",
Expand Down
Loading

0 comments on commit 05b7a18

Please sign in to comment.