summaryrefslogtreecommitdiff
path: root/rust/src/types.rs
diff options
context:
space:
mode:
authorKyleMiles <krm504@nyu.edu>2021-05-19 20:40:24 -0400
committerKyleMiles <krm504@nyu.edu>2021-07-07 23:27:29 -0400
commitd19f5e85ee26c3d6604798d30ab821d15731cada (patch)
tree9851b7f6bf5589df69508d4489c11148515cb550 /rust/src/types.rs
parent0f58b0742e3010eee6bbd2e15f4dd4930ec0d822 (diff)
DebugInfo - plugable debug information importers - C++, Rust, and Python APIs
Diffstat (limited to 'rust/src/types.rs')
-rw-r--r--rust/src/types.rs157
1 files changed, 142 insertions, 15 deletions
diff --git a/rust/src/types.rs b/rust/src/types.rs
index 91bf6972..0884d9ac 100644
--- a/rust/src/types.rs
+++ b/rust/src/types.rs
@@ -21,7 +21,7 @@ use std::{fmt, mem, ptr, result, slice};
use crate::architecture::{Architecture, CoreArchitecture};
use crate::callingconvention::CallingConvention;
-use crate::string::{BnStr, BnStrCompatible, BnString};
+use crate::string::{raw_to_string, BnStr, BnStrCompatible, BnString};
use crate::rc::*;
@@ -774,7 +774,6 @@ impl Type {
let mut stack_adjust = Conf::<i64>::new(0, min_confidence()).into();
let mut raw_parameters = Vec::<BNFunctionParameter>::with_capacity(parameters.len());
for parameter in parameters {
- // TODO : The core side is fine, but test this function with named parameters to ensure rust doesn't delete the reference
let raw_name = parameter.name.as_bytes_with_nul();
let location = match &parameter.location {
@@ -823,7 +822,6 @@ impl Type {
let mut raw_parameters = Vec::<BNFunctionParameter>::with_capacity(parameters.len());
for parameter in parameters {
- // TODO : The core side is fine, but test this function with named parameters to ensure rust doesn't delete the reference
let raw_name = parameter.name.as_bytes_with_nul();
let location = match &parameter.location {
@@ -1105,10 +1103,9 @@ impl EnumerationBuilder {
let members: &[*mut BNEnumerationMember] =
slice::from_raw_parts(members_raw as *mut _, count);
- let mut result = vec![];
- for i in 0..count {
- result.push(EnumerationMember::from_raw(members[i]));
- }
+ let result = (0..count)
+ .map(|i| EnumerationMember::from_raw(members[i]))
+ .collect();
BNFreeEnumerationMemberList(members_raw, count);
@@ -1166,10 +1163,9 @@ impl Enumeration {
let members: &[*mut BNEnumerationMember] =
slice::from_raw_parts(members_raw as *mut _, count);
- let mut result = vec![];
- for i in 0..count {
- result.push(EnumerationMember::from_raw(members[i]));
- }
+ let result = (0..count)
+ .map(|i| EnumerationMember::from_raw(members[i]))
+ .collect();
BNFreeEnumerationMemberList(members_raw, count);
@@ -1217,10 +1213,7 @@ impl StructureBuilder {
//! use binaryninja::types::{Structure, Type};
//! // Define struct, set size (in bytes)
- //! let mut my_custom_struct = Structure::new();
- //! my_custom_struct.set_width(17);
-
- //! // Create some fields for the struct
+ //! let mut my_custom_struct = Structure::new();BnStr::from_raw(raw.name)
//! let field_1 = Self::named_int(5, false, "my_weird_int_type");
//! let field_2 = Self::int(4, false);
//! let field_3 = Self::int(8, false);
@@ -1537,3 +1530,137 @@ unsafe impl<'a> CoreOwnedArrayWrapper<'a> for QualifiedNameAndType {
mem::transmute(raw)
}
}
+
+//////////////////////////
+// QualifiedNameAndType
+
+pub struct NameAndType<S: BnStrCompatible> {
+ pub name: S,
+ t: Conf<Ref<Type>>,
+}
+
+impl NameAndType<String> {
+ pub(crate) fn from_raw(raw: &BNNameAndType) -> Self {
+ Self::new(
+ raw_to_string(raw.name).unwrap(),
+ unsafe { &Type::ref_from_raw(raw.type_) },
+ raw.typeConfidence,
+ )
+ }
+}
+
+impl<S: BnStrCompatible> NameAndType<S> {
+ pub fn new(name: S, t: &Ref<Type>, confidence: u8) -> Self {
+ Self {
+ name: name,
+ t: Conf::new(t.clone(), confidence),
+ }
+ }
+
+ pub fn type_with_confidence(&self) -> Conf<Ref<Type>> {
+ self.t.clone()
+ }
+}
+
+unsafe impl<S: BnStrCompatible> CoreOwnedArrayProvider for NameAndType<S> {
+ type Raw = BNNameAndType;
+ type Context = ();
+
+ unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
+ BNFreeNameAndTypeList(raw, count);
+ }
+}
+
+unsafe impl<'a, S: 'a + BnStrCompatible> CoreOwnedArrayWrapper<'a> for NameAndType<S> {
+ type Wrapped = &'a NameAndType<S>;
+
+ unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
+ mem::transmute(raw)
+ }
+}
+
+//////////////////
+// DataVariable
+
+pub struct DataVariable {
+ pub address: u64,
+ pub t: Conf<Ref<Type>>,
+ pub auto_discovered: bool,
+}
+
+// impl DataVariable {
+// pub(crate) fn from_raw(var: &BNDataVariable) -> Self {
+// Self {
+// address: var.address,
+// t: Conf::new(unsafe { Type::ref_from_raw(var.type_) }, var.typeConfidence),
+// auto_discovered: var.autoDiscovered,
+// }
+// }
+// }
+
+impl DataVariable {
+ pub fn type_with_confidence(&self) -> Conf<Ref<Type>> {
+ Conf::new(self.t.contents.clone(), self.t.confidence)
+ }
+}
+
+// unsafe impl CoreOwnedArrayProvider for DataVariable {
+// type Raw = BNDataVariable;
+// type Context = ();
+
+// unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
+// BNFreeDataVariables(raw, count);
+// }
+// }
+
+// unsafe impl<'a> CoreOwnedArrayWrapper<'a> for DataVariable {
+// type Wrapped = &'a DataVariable;
+
+// unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
+// mem::transmute(raw)
+// }
+// }
+
+/////////////////////////
+// DataVariableAndName
+
+pub struct DataVariableAndName<S: BnStrCompatible> {
+ pub address: u64,
+ pub t: Conf<Ref<Type>>,
+ pub auto_discovered: bool,
+ pub name: S,
+}
+
+impl DataVariableAndName<String> {
+ pub(crate) fn from_raw(var: &BNDataVariableAndName) -> Self {
+ Self {
+ address: var.address,
+ t: Conf::new(unsafe { Type::ref_from_raw(var.type_) }, var.typeConfidence),
+ auto_discovered: var.autoDiscovered,
+ name: raw_to_string(var.name).unwrap(),
+ }
+ }
+}
+
+impl<S: BnStrCompatible> DataVariableAndName<S> {
+ pub fn type_with_confidence(&self) -> Conf<Ref<Type>> {
+ Conf::new(self.t.contents.clone(), self.t.confidence)
+ }
+}
+
+// unsafe impl<S: BnStrCompatible> CoreOwnedArrayProvider for DataVariableAndName<S> {
+// type Raw = BNDataVariableAndName;
+// type Context = ();
+
+// unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
+// BNFreeDataVariablesAndName(raw, count);
+// }
+// }
+
+// unsafe impl<'a, S: 'a + BnStrCompatible> CoreOwnedArrayWrapper<'a> for DataVariableAndName<S> {
+// type Wrapped = &'a DataVariableAndName<S>;
+
+// unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
+// mem::transmute(raw)
+// }
+// }