summaryrefslogtreecommitdiff
path: root/rust/src
diff options
context:
space:
mode:
authorrose <47357290+rose4096@users.noreply.github.com>2022-06-22 13:37:21 -0400
committerrose <47357290+rose4096@users.noreply.github.com>2022-06-23 12:00:25 -0400
commita651dd81ff72d5155b122ce99e9252d6e714cc1e (patch)
treec343b19b77d286ff51356b1d11001a524682ac89 /rust/src
parent6c6dc60787427789e4bc9e3a20cca304a8210e8d (diff)
Rust API: Change instruction_text to use Vec<>
Diffstat (limited to 'rust/src')
-rw-r--r--rust/src/architecture.rs20
-rw-r--r--rust/src/custombinaryview.rs4
-rw-r--r--rust/src/disassembly.rs30
-rw-r--r--rust/src/lib.rs24
-rw-r--r--rust/src/types.rs28
5 files changed, 59 insertions, 47 deletions
diff --git a/rust/src/architecture.rs b/rust/src/architecture.rs
index b8e40c5b..0f364b86 100644
--- a/rust/src/architecture.rs
+++ b/rust/src/architecture.rs
@@ -288,7 +288,7 @@ pub trait Architecture: 'static + Sized + AsRef<CoreArchitecture> {
&self,
data: &[u8],
addr: u64,
- ) -> Option<(usize, Array<InstructionTextToken>)>;
+ ) -> Option<(usize, Vec<InstructionTextToken>)>;
fn instruction_llil(
&self,
data: &[u8],
@@ -752,7 +752,7 @@ impl Architecture for CoreArchitecture {
&self,
data: &[u8],
addr: u64,
- ) -> Option<(usize, Array<InstructionTextToken>)> {
+ ) -> Option<(usize, Vec<InstructionTextToken>)> {
let mut consumed = data.len();
let mut count: usize = 0;
let mut result: *mut BNInstructionTextToken = ptr::null_mut();
@@ -766,7 +766,11 @@ impl Architecture for CoreArchitecture {
&mut result as *mut _,
&mut count as *mut _,
) {
- Some((consumed, Array::new(result, count, ())))
+ let vec = Vec::<BNInstructionTextToken>::from_raw_parts(result, count, count)
+ .iter()
+ .map(|x| InstructionTextToken::from_raw(x))
+ .collect();
+ Some((consumed, vec))
} else {
None
}
@@ -1233,10 +1237,14 @@ where
let result = unsafe { &mut *result };
match custom_arch.instruction_text(data, addr) {
- Some((res_size, res_tokens)) => {
+ Some((res_size, mut res_tokens)) => {
unsafe {
- let (r_ptr, r_count) = res_tokens.into_raw_parts();
- *result = r_ptr;
+ // TODO: Can't use into_raw_parts as it's unstable so we do this instead...
+ let r_ptr = res_tokens.as_mut_ptr();
+ let r_count = res_tokens.len();
+ mem::forget(res_tokens);
+
+ *result = &mut (*r_ptr).0;
*count = r_count;
*len = res_size;
}
diff --git a/rust/src/custombinaryview.rs b/rust/src/custombinaryview.rs
index d547c388..35e4b0fe 100644
--- a/rust/src/custombinaryview.rs
+++ b/rust/src/custombinaryview.rs
@@ -56,8 +56,8 @@ where
}
extern "C" fn cb_deprecated<T>(ctxt: *mut c_void) -> bool
- where
- T: CustomBinaryViewType,
+ where
+ T: CustomBinaryViewType,
{
ffi_wrap!("BinaryViewTypeBase::is_deprecated", unsafe {
let view_type = &*(ctxt as *mut T);
diff --git a/rust/src/disassembly.rs b/rust/src/disassembly.rs
index ff00e8d0..ab8f91f9 100644
--- a/rust/src/disassembly.rs
+++ b/rust/src/disassembly.rs
@@ -192,25 +192,6 @@ impl Clone for InstructionTextToken {
// }
// }
-impl CoreArrayProvider for InstructionTextToken {
- type Raw = BNInstructionTextToken;
- type Context = ();
-}
-
-unsafe impl CoreOwnedArrayProvider for InstructionTextToken {
- unsafe fn free(raw: *mut BNInstructionTextToken, count: usize, _context: &()) {
- BNFreeInstructionText(raw, count);
- }
-}
-
-unsafe impl<'a> CoreArrayWrapper<'a> for InstructionTextToken {
- type Wrapped = Guard<'a, InstructionTextToken>;
-
- unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
- Guard::new(InstructionTextToken::from_raw(raw), _context)
- }
-}
-
pub struct DisassemblyTextLine(pub(crate) BNDisassemblyTextLine);
impl DisassemblyTextLine {
@@ -235,8 +216,13 @@ impl DisassemblyTextLine {
self.0.tagCount
}
- pub fn tokens(&self) -> ArrayGuard<InstructionTextToken> {
- unsafe { ArrayGuard::new(self.0.tokens, self.0.count, ()) }
+ pub fn tokens(&self) -> Vec<InstructionTextToken> {
+ unsafe {
+ Vec::<BNInstructionTextToken>::from_raw_parts(self.0.tokens, self.0.count, self.0.count)
+ .iter()
+ .map(|x| InstructionTextToken::from_raw(x))
+ .collect()
+ }
}
}
@@ -267,7 +253,7 @@ impl From<Vec<InstructionTextToken>> for DisassemblyTextLine {
tokens.shrink_to_fit();
assert!(tokens.len() == tokens.capacity());
- // let (tokens_pointer, tokens_len, _) = unsafe { tokens.into_raw_parts() }; // Can't use for now...still a rust nighly feature
+ // TODO: let (tokens_pointer, tokens_len, _) = unsafe { tokens.into_raw_parts() }; // Can't use for now...still a rust nightly feature
let tokens_pointer = tokens.as_mut_ptr();
let tokens_len = tokens.len();
mem::forget(tokens);
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index 59e68447..21198051 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -193,13 +193,25 @@ pub mod logger {
LogGuard { ctxt: raw }
}
- extern "C" fn cb_log<L>(ctxt: *mut c_void, session: usize, level: Level, msg: *const c_char, logger_name: *const c_char, tid: usize)
- where
+ extern "C" fn cb_log<L>(
+ ctxt: *mut c_void,
+ session: usize,
+ level: Level,
+ msg: *const c_char,
+ logger_name: *const c_char,
+ tid: usize,
+ ) where
L: LogListener,
{
ffi_wrap!("LogListener::log", unsafe {
let listener = &*(ctxt as *const L);
- listener.log(session, level, BnStr::from_raw(msg), BnStr::from_raw(logger_name), tid);
+ listener.log(
+ session,
+ level,
+ BnStr::from_raw(msg),
+ BnStr::from_raw(logger_name),
+ tid,
+ );
})
}
@@ -232,7 +244,11 @@ pub fn open_view<F: AsRef<Path>>(filename: F) -> Result<rc::Ref<binaryview::Bina
let mut metadata = filemetadata::FileMetadata::with_filename(filename.to_str().unwrap());
- let (is_bndb, view) = if filename.extension().map(|ext| ext == "bndb").unwrap_or(false) {
+ let (is_bndb, view) = if filename
+ .extension()
+ .map(|ext| ext == "bndb")
+ .unwrap_or(false)
+ {
let mut file = File::open(filename).or(Err("Could not open file".to_string()))?;
let mut buf = [0; 15];
diff --git a/rust/src/types.rs b/rust/src/types.rs
index 01767853..62c4f909 100644
--- a/rust/src/types.rs
+++ b/rust/src/types.rs
@@ -813,12 +813,11 @@ impl Type {
let reg_stack_adjust_regs = ptr::null_mut();
let reg_stack_adjust_values = ptr::null_mut();
- let mut return_regs: BNRegisterSetWithConfidence =
- BNRegisterSetWithConfidence{
- regs: ptr::null_mut(),
- count: 0,
- confidence: 0,
- };
+ let mut return_regs: BNRegisterSetWithConfidence = BNRegisterSetWithConfidence {
+ regs: ptr::null_mut(),
+ count: 0,
+ confidence: 0,
+ };
unsafe {
Self::ref_from_raw(BNCreateFunctionType(
@@ -880,12 +879,11 @@ impl Type {
let reg_stack_adjust_regs = ptr::null_mut();
let reg_stack_adjust_values = ptr::null_mut();
- let mut return_regs: BNRegisterSetWithConfidence =
- BNRegisterSetWithConfidence{
- regs: ptr::null_mut(),
- count: 0,
- confidence: 0,
- };
+ let mut return_regs: BNRegisterSetWithConfidence = BNRegisterSetWithConfidence {
+ regs: ptr::null_mut(),
+ count: 0,
+ confidence: 0,
+ };
unsafe {
Self::ref_from_raw(BNCreateFunctionType(
@@ -991,7 +989,11 @@ impl From<&TypeBuilder> for Ref<Type> {
impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", unsafe {
- BnString::from_raw(BNGetTypeString(self.handle, ptr::null_mut(), BNTokenEscapingType::NoTokenEscapingType))
+ BnString::from_raw(BNGetTypeString(
+ self.handle,
+ ptr::null_mut(),
+ BNTokenEscapingType::NoTokenEscapingType,
+ ))
})
}
}