Skip to content

Commit 6d267ec

Browse files
committed
Fix base detection in rust API failing on auto arch detection
Also added actual test for base address detection
1 parent b366ebd commit 6d267ec

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

rust/src/base_detection.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
use binaryninjacore_sys::*;
2-
use std::ffi::CStr;
2+
use std::ffi::{c_char, CStr};
33

44
use crate::architecture::CoreArchitecture;
55
use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner};
6-
use crate::string::BnString;
76
use std::num::NonZeroU32;
87
use std::ptr::NonNull;
98

109
pub type BaseAddressDetectionPOISetting = BNBaseAddressDetectionPOISetting;
1110
pub type BaseAddressDetectionConfidence = BNBaseAddressDetectionConfidence;
1211
pub type BaseAddressDetectionPOIType = BNBaseAddressDetectionPOIType;
1312

13+
/// This is the architecture name used to use the architecture auto-detection feature.
14+
const BASE_ADDRESS_AUTO_DETECTION_ARCH: &CStr = c"auto detect";
15+
1416
pub enum BaseAddressDetectionAnalysis {
1517
Basic,
1618
ControlFlow,
@@ -169,9 +171,12 @@ pub struct BaseAddressDetectionSettings {
169171

170172
impl BaseAddressDetectionSettings {
171173
pub(crate) fn into_raw(value: &Self) -> BNBaseAddressDetectionSettings {
172-
let arch_name = value.arch.map(|a| a.name()).unwrap_or(BnString::new(""));
174+
let arch_name = value
175+
.arch
176+
.map(|a| a.name().as_ptr())
177+
.unwrap_or(BASE_ADDRESS_AUTO_DETECTION_ARCH.as_ptr() as *const c_char);
173178
BNBaseAddressDetectionSettings {
174-
Architecture: arch_name.as_ptr(),
179+
Architecture: arch_name,
175180
Analysis: value.analysis.as_raw().as_ptr(),
176181
MinStrlen: value.min_string_len,
177182
Alignment: value.alignment.get(),

rust/tests/base_detection.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use binaryninja::base_detection::BaseAddressDetectionSettings;
1+
use binaryninja::base_detection::{BaseAddressDetectionConfidence, BaseAddressDetectionSettings};
22
use binaryninja::binary_view::BinaryViewExt;
33
use binaryninja::headless::Session;
44
use rstest::{fixture, rstest};
@@ -11,14 +11,21 @@ fn session() -> Session {
1111
}
1212

1313
#[rstest]
14-
fn test_failed_base_detection(_session: &Session) {
14+
fn test_base_detection(_session: &Session) {
1515
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
16-
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
16+
let view = binaryninja::load(out_dir.join("raw_base_detection_aarch64"))
17+
.expect("Failed to create view");
1718
let bad = view
1819
.base_address_detection()
1920
.expect("Failed to create base address detection");
2021
assert!(
21-
!bad.detect(&BaseAddressDetectionSettings::default()),
22-
"Detection should fail on this view"
22+
bad.detect(&BaseAddressDetectionSettings::default()),
23+
"Detection should succeed on this view"
24+
);
25+
let result = bad.scores(10);
26+
assert_eq!(result.scores.len(), 3);
27+
assert_eq!(
28+
result.confidence,
29+
BaseAddressDetectionConfidence::HighConfidence
2330
);
2431
}

0 commit comments

Comments
 (0)