Skip to content

Commit 564408d

Browse files
committed
Add function_by_name to Rust API for applying platform types
1 parent 00a7175 commit 564408d

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

rust/src/platform.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use crate::{
2020
rc::*,
2121
string::*,
2222
types::{
23-
QualifiedNameAndType, TypeContainer, TypeLibrary, TypeParserError, TypeParserErrorSeverity,
24-
TypeParserResult,
23+
QualifiedName, QualifiedNameAndType, Type, TypeContainer, TypeLibrary, TypeParserError,
24+
TypeParserErrorSeverity, TypeParserResult,
2525
},
2626
};
2727
use binaryninjacore_sys::*;
@@ -279,6 +279,22 @@ impl Platform {
279279
}
280280
}
281281

282+
pub fn function_by_name<T: Into<QualifiedName>>(
283+
&self,
284+
name: T,
285+
exact_match: bool,
286+
) -> Option<Ref<Type>> {
287+
let mut raw_name = QualifiedName::into_raw(name.into());
288+
unsafe {
289+
let type_handle = BNGetPlatformFunctionByName(self.handle, &mut raw_name, exact_match);
290+
QualifiedName::free_raw(raw_name);
291+
if type_handle.is_null() {
292+
return None;
293+
}
294+
Some(Type::ref_from_raw(type_handle))
295+
}
296+
}
297+
282298
// TODO: system_calls
283299
// TODO: add a helper function to define a system call (platform function with a specific type)
284300

rust/tests/platform.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ fn test_platform_types() {
2121
assert_ne!(platform_types.len(), 0);
2222
}
2323

24+
#[test]
25+
fn test_platform_function_by_name() {
26+
let _session = Session::new().expect("Failed to initialize session");
27+
let platform = Platform::by_name("windows-x86_64").expect("windows-x86_64 exists");
28+
let platform_functions = platform.functions();
29+
assert_ne!(platform_functions.len(), 0);
30+
31+
let function = platform_functions.get(0);
32+
assert!(platform.function_by_name(function.name, true).is_some());
33+
}
34+
2435
#[test]
2536
fn test_platform_calling_conventions() {
2637
let _session = Session::new().expect("Failed to initialize session");

0 commit comments

Comments
 (0)