diff options
| author | Mason Reed <mason@vector35.com> | 2025-02-13 16:03:31 -0500 |
|---|---|---|
| committer | Mason Reed <mason@vector35.com> | 2025-02-13 16:03:35 -0500 |
| commit | 6d267ec0604ef5036f173761d67d0fb4adc8a8fa (patch) | |
| tree | a5d4a8dfdf468065fdab74613d013532b4830d72 | |
| parent | b366ebd28bcb44492719b487f9dd34b6da56d23f (diff) | |
Fix base detection in rust API failing on auto arch detection
Also added actual test for base address detection
| -rw-r--r-- | rust/src/base_detection.rs | 13 | ||||
| -rw-r--r-- | rust/tests/base_detection.rs | 17 |
2 files changed, 21 insertions, 9 deletions
diff --git a/rust/src/base_detection.rs b/rust/src/base_detection.rs index 46a56842..1fd45ef6 100644 --- a/rust/src/base_detection.rs +++ b/rust/src/base_detection.rs @@ -1,9 +1,8 @@ use binaryninjacore_sys::*; -use std::ffi::CStr; +use std::ffi::{c_char, CStr}; use crate::architecture::CoreArchitecture; use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner}; -use crate::string::BnString; use std::num::NonZeroU32; use std::ptr::NonNull; @@ -11,6 +10,9 @@ pub type BaseAddressDetectionPOISetting = BNBaseAddressDetectionPOISetting; pub type BaseAddressDetectionConfidence = BNBaseAddressDetectionConfidence; pub type BaseAddressDetectionPOIType = BNBaseAddressDetectionPOIType; +/// This is the architecture name used to use the architecture auto-detection feature. +const BASE_ADDRESS_AUTO_DETECTION_ARCH: &CStr = c"auto detect"; + pub enum BaseAddressDetectionAnalysis { Basic, ControlFlow, @@ -169,9 +171,12 @@ pub struct BaseAddressDetectionSettings { impl BaseAddressDetectionSettings { pub(crate) fn into_raw(value: &Self) -> BNBaseAddressDetectionSettings { - let arch_name = value.arch.map(|a| a.name()).unwrap_or(BnString::new("")); + let arch_name = value + .arch + .map(|a| a.name().as_ptr()) + .unwrap_or(BASE_ADDRESS_AUTO_DETECTION_ARCH.as_ptr() as *const c_char); BNBaseAddressDetectionSettings { - Architecture: arch_name.as_ptr(), + Architecture: arch_name, Analysis: value.analysis.as_raw().as_ptr(), MinStrlen: value.min_string_len, Alignment: value.alignment.get(), diff --git a/rust/tests/base_detection.rs b/rust/tests/base_detection.rs index b5497c3c..e4594b8e 100644 --- a/rust/tests/base_detection.rs +++ b/rust/tests/base_detection.rs @@ -1,4 +1,4 @@ -use binaryninja::base_detection::BaseAddressDetectionSettings; +use binaryninja::base_detection::{BaseAddressDetectionConfidence, BaseAddressDetectionSettings}; use binaryninja::binary_view::BinaryViewExt; use binaryninja::headless::Session; use rstest::{fixture, rstest}; @@ -11,14 +11,21 @@ fn session() -> Session { } #[rstest] -fn test_failed_base_detection(_session: &Session) { +fn test_base_detection(_session: &Session) { let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap(); - let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view"); + let view = binaryninja::load(out_dir.join("raw_base_detection_aarch64")) + .expect("Failed to create view"); let bad = view .base_address_detection() .expect("Failed to create base address detection"); assert!( - !bad.detect(&BaseAddressDetectionSettings::default()), - "Detection should fail on this view" + bad.detect(&BaseAddressDetectionSettings::default()), + "Detection should succeed on this view" + ); + let result = bad.scores(10); + assert_eq!(result.scores.len(), 3); + assert_eq!( + result.confidence, + BaseAddressDetectionConfidence::HighConfidence ); } |
