summaryrefslogtreecommitdiff
path: root/rust/src
diff options
context:
space:
mode:
authorKyleMiles <krm504@nyu.edu>2023-02-14 00:09:13 -0500
committerKyleMiles <krm504@nyu.edu>2023-02-14 00:15:57 -0500
commit9e91eaf63aed340a2c0a61ad8f70e9a7883e3aba (patch)
treea77dc4618b1f2848b09694a4fb81b4fa193a1566 /rust/src
parent60f49f32d989355696b1eb78aee98140f417952c (diff)
Rust API: Misc changes and improvements
Diffstat (limited to 'rust/src')
-rw-r--r--rust/src/binaryview.rs6
-rw-r--r--rust/src/function.rs39
-rw-r--r--rust/src/interaction.rs6
-rw-r--r--rust/src/rc.rs1
-rw-r--r--rust/src/symbol.rs11
-rw-r--r--rust/src/types.rs74
6 files changed, 98 insertions, 39 deletions
diff --git a/rust/src/binaryview.rs b/rust/src/binaryview.rs
index aab920d2..f83076cb 100644
--- a/rust/src/binaryview.rs
+++ b/rust/src/binaryview.rs
@@ -305,7 +305,7 @@ pub trait BinaryViewExt: BinaryViewBase {
return Err(());
}
- Ok(Ref::new(Symbol::from_raw(raw_sym)))
+ Ok(Symbol::ref_from_raw(raw_sym))
}
}
@@ -323,7 +323,7 @@ pub trait BinaryViewExt: BinaryViewBase {
return Err(());
}
- Ok(Ref::new(Symbol::from_raw(raw_sym)))
+ Ok(Symbol::ref_from_raw(raw_sym))
}
}
@@ -425,7 +425,7 @@ pub trait BinaryViewExt: BinaryViewBase {
return Err(());
}
- Ok(Ref::new(Symbol::from_raw(raw_sym)))
+ Ok(Symbol::ref_from_raw(raw_sym))
}
}
diff --git a/rust/src/function.rs b/rust/src/function.rs
index 2eed562e..426f936b 100644
--- a/rust/src/function.rs
+++ b/rust/src/function.rs
@@ -12,21 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-use std::{fmt, mem};
-
use binaryninjacore_sys::*;
-use crate::architecture::CoreArchitecture;
-use crate::basicblock::{BasicBlock, BlockContext};
-use crate::binaryview::{BinaryView, BinaryViewExt};
-use crate::platform::Platform;
-use crate::symbol::Symbol;
-use crate::types::Type;
-
-use crate::llil;
-
use crate::rc::*;
use crate::string::*;
+use crate::{
+ architecture::CoreArchitecture,
+ basicblock::{BasicBlock, BlockContext},
+ binaryview::{BinaryView, BinaryViewExt},
+ llil,
+ platform::Platform,
+ symbol::Symbol,
+ types::{Conf, Type},
+};
+
+use std::{fmt, mem};
pub struct Location {
pub arch: Option<CoreArchitecture>,
@@ -144,7 +144,7 @@ impl Function {
pub fn symbol(&self) -> Ref<Symbol> {
unsafe {
let sym = BNGetFunctionSymbol(self.handle);
- Ref::new(Symbol::from_raw(sym))
+ Symbol::ref_from_raw(sym)
}
}
@@ -240,6 +240,19 @@ impl Function {
}
}
+ pub fn return_type(&self) -> Conf<Ref<Type>> {
+ let result = unsafe { BNGetFunctionReturnType(self.handle) };
+
+ Conf::new(
+ unsafe { Type::ref_from_raw(result.type_) },
+ result.confidence,
+ )
+ }
+
+ pub fn function_type(&self) -> Ref<Type> {
+ unsafe { Type::ref_from_raw(BNGetFunctionType(self.handle)) }
+ }
+
pub fn set_user_type(&self, t: Type) {
unsafe {
BNSetFunctionUserType(self.handle, t.handle);
@@ -330,4 +343,4 @@ unsafe impl<'a> CoreArrayWrapper<'a> for AddressRange {
unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
mem::transmute(raw)
}
-} \ No newline at end of file
+}
diff --git a/rust/src/interaction.rs b/rust/src/interaction.rs
index c6aada61..20f84348 100644
--- a/rust/src/interaction.rs
+++ b/rust/src/interaction.rs
@@ -466,8 +466,6 @@ impl FormInputBuilder {
///
/// println!("{} {} likes {}", &first_name, &last_name, food);
/// ```
-
-
pub fn get_form_input(&mut self, title: &str) -> Vec<FormResponses> {
if unsafe {
BNGetFormInput(
@@ -488,7 +486,9 @@ impl FormInputBuilder {
| BNFormInputFieldType::OpenFileNameFormField
| BNFormInputFieldType::SaveFileNameFormField
| BNFormInputFieldType::DirectoryNameFormField => {
- FormResponses::String(unsafe { BnStr::from_raw(form_field.stringResult).to_string() })
+ FormResponses::String(unsafe {
+ BnStr::from_raw(form_field.stringResult).to_string()
+ })
}
BNFormInputFieldType::IntegerFormField => {
diff --git a/rust/src/rc.rs b/rust/src/rc.rs
index 4ffecb3f..2e4086e3 100644
--- a/rust/src/rc.rs
+++ b/rust/src/rc.rs
@@ -48,6 +48,7 @@ pub struct Ref<T: RefCountable> {
}
impl<T: RefCountable> Ref<T> {
+ /// Safety: You need to make sure wherever you got the contents from incremented the ref count already. Anywhere the core passes out an object to the API does this.
pub(crate) unsafe fn new(contents: T) -> Self {
Self { contents }
}
diff --git a/rust/src/symbol.rs b/rust/src/symbol.rs
index 22c2b051..59080814 100644
--- a/rust/src/symbol.rs
+++ b/rust/src/symbol.rs
@@ -166,7 +166,7 @@ impl<S: BnStrCompatible> SymbolBuilder<S> {
self.ordinal,
);
- Ref::new(Symbol::from_raw(res))
+ Symbol::ref_from_raw(res)
}
}
}
@@ -177,6 +177,10 @@ pub struct Symbol {
}
impl Symbol {
+ pub(crate) unsafe fn ref_from_raw(raw: *mut BNSymbol) -> Ref<Self> {
+ Ref::new(Self { handle: raw })
+ }
+
pub(crate) unsafe fn from_raw(raw: *mut BNSymbol) -> Self {
Self { handle: raw }
}
@@ -226,6 +230,11 @@ impl Symbol {
pub fn auto_defined(&self) -> bool {
unsafe { BNIsSymbolAutoDefined(self.handle) }
}
+
+ /// Wether this symbol has external linkage
+ pub fn external(&self) -> bool {
+ self.binding() == Binding::Weak || self.binding() == Binding::Global
+ }
}
unsafe impl Send for Symbol {}
diff --git a/rust/src/types.rs b/rust/src/types.rs
index 81265a92..6bd85d0e 100644
--- a/rust/src/types.rs
+++ b/rust/src/types.rs
@@ -17,23 +17,31 @@
// TODO : Test the get_enumeration and get_structure methods
use binaryninjacore_sys::*;
-use lazy_static::lazy_static;
-use std::borrow::Cow;
-use std::ffi::CStr;
-use std::fmt::{Debug, Display, Formatter};
-use std::hash::{Hash, Hasher};
-use std::iter::{zip, IntoIterator};
-use std::os::raw::c_char;
-use std::sync::Mutex;
-use std::{fmt, mem, ptr, result, slice};
-use crate::architecture::{Architecture, CoreArchitecture};
-use crate::binaryview::BinaryView;
-use crate::callingconvention::CallingConvention;
-use crate::filemetadata::FileMetadata;
-use crate::string::{raw_to_string, BnStr, BnStrCompatible, BnString};
+use crate::{
+ architecture::{Architecture, CoreArchitecture},
+ binaryview::{BinaryView, BinaryViewExt},
+ callingconvention::CallingConvention,
+ filemetadata::FileMetadata,
+ rc::*,
+ string::{raw_to_string, BnStr, BnStrCompatible, BnString},
+ symbol::Symbol,
+};
-use crate::rc::*;
+use lazy_static::lazy_static;
+use std::{
+ borrow::Cow,
+ collections::HashSet,
+ ffi::CStr,
+ fmt,
+ fmt::{Debug, Display, Formatter},
+ hash::{Hash, Hasher},
+ iter::{zip, IntoIterator},
+ mem,
+ os::raw::c_char,
+ ptr, result, slice,
+ sync::Mutex,
+};
pub type Result<R> = result::Result<R, ()>;
@@ -1419,8 +1427,7 @@ impl EnumerationBuilder {
unsafe {
let mut count: usize = mem::zeroed();
let members_raw = BNGetEnumerationBuilderMembers(self.handle, &mut count);
- let members: &[BNEnumerationMember] =
- slice::from_raw_parts(members_raw, count);
+ let members: &[BNEnumerationMember] = slice::from_raw_parts(members_raw, count);
let result = (0..count)
.map(|i| EnumerationMember::from_raw(members[i]))
@@ -1477,8 +1484,7 @@ impl Enumeration {
unsafe {
let mut count: usize = mem::zeroed();
let members_raw = BNGetEnumerationMembers(self.handle, &mut count);
- let members: &[BNEnumerationMember] =
- slice::from_raw_parts(members_raw, count);
+ let members: &[BNEnumerationMember] = slice::from_raw_parts(members_raw, count);
let result = (0..count)
.map(|i| EnumerationMember::from_raw(members[i]))
@@ -1873,6 +1879,32 @@ impl NamedTypeReference {
pub fn class(&self) -> NamedTypeReferenceClass {
unsafe { BNGetTypeReferenceClass(self.handle) }
}
+
+ fn target_helper(&self, bv: &BinaryView, visited: &mut HashSet<BnString>) -> Option<Ref<Type>> {
+ // TODO : This is a clippy bug (#10088, I think); remove after we upgrade past 2022-12-12
+ #[allow(clippy::manual_filter)]
+ if let Some(t) = bv.get_type_by_id(self.id()) {
+ if t.type_class() != TypeClass::NamedTypeReferenceClass {
+ Some(t)
+ } else {
+ let t = t.get_named_type_reference().unwrap();
+ if visited.contains(&t.id()) {
+ error!("Can't get target for recursively defined type!");
+ None
+ } else {
+ visited.insert(t.id());
+ t.target_helper(bv, visited)
+ }
+ }
+ } else {
+ None
+ }
+ }
+
+ pub fn target(&self, bv: &BinaryView) -> Option<Ref<Type>> {
+ //! Returns the type referenced by this named type reference
+ self.target_helper(bv, &mut HashSet::new())
+ }
}
///////////////////
@@ -2101,6 +2133,10 @@ impl DataVariable {
pub fn type_with_confidence(&self) -> Conf<Ref<Type>> {
Conf::new(self.t.contents.clone(), self.t.confidence)
}
+
+ pub fn symbol(&self, bv: &BinaryView) -> Option<Ref<Symbol>> {
+ bv.symbol_by_address(self.address).ok()
+ }
}
impl CoreArrayProvider for DataVariable {