summaryrefslogtreecommitdiff
path: root/rust/src
diff options
context:
space:
mode:
Diffstat (limited to 'rust/src')
-rw-r--r--rust/src/architecture.rs23
-rw-r--r--rust/src/backgroundtask.rs8
-rw-r--r--rust/src/basicblock.rs12
-rw-r--r--rust/src/callingconvention.rs42
-rw-r--r--rust/src/custombinaryview.rs23
-rw-r--r--rust/src/debuginfo.rs23
-rw-r--r--rust/src/demangle.rs8
-rw-r--r--rust/src/disassembly.rs21
-rw-r--r--rust/src/downloadprovider.rs10
-rw-r--r--rust/src/function.rs29
-rw-r--r--rust/src/hlil/function.rs41
-rw-r--r--rust/src/hlil/instruction.rs3
-rw-r--r--rust/src/hlil/lift.rs1
-rw-r--r--rust/src/hlil/operation.rs2
-rw-r--r--rust/src/interaction.rs4
-rw-r--r--rust/src/linearview.rs6
-rw-r--r--rust/src/llil/operation.rs20
-rw-r--r--rust/src/metadata.rs85
-rw-r--r--rust/src/mlil/function.rs14
-rw-r--r--rust/src/mlil/instruction.rs10
-rw-r--r--rust/src/mlil/lift.rs1
-rw-r--r--rust/src/platform.rs6
-rw-r--r--rust/src/rc.rs76
-rw-r--r--rust/src/references.rs16
-rw-r--r--rust/src/relocation.rs26
-rw-r--r--rust/src/section.rs6
-rw-r--r--rust/src/segment.rs6
-rw-r--r--rust/src/string.rs6
-rw-r--r--rust/src/symbol.rs6
-rw-r--r--rust/src/types.rs46
30 files changed, 337 insertions, 243 deletions
diff --git a/rust/src/architecture.rs b/rust/src/architecture.rs
index 9b5b36b4..b2413d39 100644
--- a/rust/src/architecture.rs
+++ b/rust/src/architecture.rs
@@ -23,7 +23,7 @@ use std::{
collections::HashMap,
ffi::{c_char, c_int, CStr, CString},
hash::Hash,
- mem::zeroed,
+ mem::{zeroed, MaybeUninit},
ops, ptr, slice,
};
@@ -1689,8 +1689,8 @@ where
A: 'static + Architecture<Handle = CustomArchitectureHandle<A>> + Send + Sync,
F: FnOnce(CustomArchitectureHandle<A>, CoreArchitecture) -> A,
{
- arch: A,
- func: F,
+ arch: MaybeUninit<A>,
+ func: Option<F>,
}
extern "C" fn cb_init<A, F>(ctxt: *mut c_void, obj: *mut BNArchitecture)
@@ -1704,11 +1704,10 @@ where
handle: ctxt as *mut A,
};
- let create = ptr::read(&custom_arch.func);
- ptr::write(
- &mut custom_arch.arch,
- create(custom_arch_handle, CoreArchitecture(obj)),
- );
+ let create = custom_arch.func.take().unwrap();
+ custom_arch
+ .arch
+ .write(create(custom_arch_handle, CoreArchitecture(obj)));
}
}
@@ -2685,13 +2684,13 @@ where
let name = name.into_bytes_with_nul();
let uninit_arch = ArchitectureBuilder {
- arch: unsafe { zeroed() },
- func,
+ arch: MaybeUninit::zeroed(),
+ func: Some(func),
};
let raw = Box::into_raw(Box::new(uninit_arch));
let mut custom_arch = BNCustomArchitecture {
- context: raw as *mut _,
+ context: raw as *mut ArchitectureBuilder<_, _> as *mut _,
init: Some(cb_init::<A, F>),
getEndianness: Some(cb_endianness::<A>),
getAddressSize: Some(cb_address_size::<A>),
@@ -2776,7 +2775,7 @@ where
assert!(!res.is_null());
- &(*raw).arch
+ (*raw).arch.assume_init_mut()
}
}
diff --git a/rust/src/backgroundtask.rs b/rust/src/backgroundtask.rs
index 1eb090d7..e62cfbcb 100644
--- a/rust/src/backgroundtask.rs
+++ b/rust/src/backgroundtask.rs
@@ -112,13 +112,13 @@ unsafe impl CoreOwnedArrayProvider for BackgroundTask {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for BackgroundTask {
- type Wrapped = Guard<'a, BackgroundTask>;
+unsafe impl CoreArrayWrapper for BackgroundTask {
+ type Wrapped<'a> = Guard<'a, BackgroundTask>;
- unsafe fn wrap_raw(
+ unsafe fn wrap_raw<'a>(
raw: &'a *mut BNBackgroundTask,
context: &'a (),
- ) -> Guard<'a, BackgroundTask> {
+ ) -> Self::Wrapped<'a> {
Guard::new(BackgroundTask::from_raw(*raw), context)
}
}
diff --git a/rust/src/basicblock.rs b/rust/src/basicblock.rs
index 73ad9362..f28e596f 100644
--- a/rust/src/basicblock.rs
+++ b/rust/src/basicblock.rs
@@ -76,10 +76,10 @@ unsafe impl<'a, C: 'a + BlockContext> CoreOwnedArrayProvider for Edge<'a, C> {
}
}
-unsafe impl<'a, C: 'a + BlockContext> CoreArrayWrapper<'a> for Edge<'a, C> {
- type Wrapped = Edge<'a, C>;
+unsafe impl<'a, C: BlockContext> CoreArrayWrapper for Edge<'a, C> {
+ type Wrapped<'b> = Edge<'b, C> where 'a: 'b;
- unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Edge<'a, C> {
+ unsafe fn wrap_raw<'b>(raw: &'b Self::Raw, context: &'b Self::Context) -> Self::Wrapped<'b> {
let edge_target = Guard::new(
BasicBlock::from_raw(raw.target, context.orig_block.context.clone()),
raw,
@@ -309,10 +309,10 @@ unsafe impl<C: BlockContext> CoreOwnedArrayProvider for BasicBlock<C> {
}
}
-unsafe impl<'a, C: 'a + BlockContext> CoreArrayWrapper<'a> for BasicBlock<C> {
- type Wrapped = Guard<'a, BasicBlock<C>>;
+unsafe impl<C: BlockContext> CoreArrayWrapper for BasicBlock<C> {
+ type Wrapped<'a> = Guard<'a, BasicBlock<C>> where C: 'a;
- unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped {
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(BasicBlock::from_raw(*raw, context.clone()), context)
}
}
diff --git a/rust/src/callingconvention.rs b/rust/src/callingconvention.rs
index 815f4d42..ef2725e8 100644
--- a/rust/src/callingconvention.rs
+++ b/rust/src/callingconvention.rs
@@ -569,11 +569,43 @@ impl<A: Architecture> CallingConventionBase for CallingConvention<A> {
}
fn int_arg_registers(&self) -> Vec<A::Register> {
- Vec::new()
+ unsafe {
+ let mut count = 0;
+ let regs = BNGetIntegerArgumentRegisters(self.handle, &mut count);
+ let arch = self.arch_handle.borrow();
+
+ let res = slice::from_raw_parts(regs, count)
+ .iter()
+ .map(|&r| {
+ arch.register_from_id(r)
+ .expect("bad reg id from CallingConvention")
+ })
+ .collect();
+
+ BNFreeRegisterList(regs);
+
+ res
+ }
}
fn float_arg_registers(&self) -> Vec<A::Register> {
- Vec::new()
+ unsafe {
+ let mut count = 0;
+ let regs = BNGetFloatArgumentRegisters(self.handle, &mut count);
+ let arch = self.arch_handle.borrow();
+
+ let res = slice::from_raw_parts(regs, count)
+ .iter()
+ .map(|&r| {
+ arch.register_from_id(r)
+ .expect("bad reg id from CallingConvention")
+ })
+ .collect();
+
+ BNFreeRegisterList(regs);
+
+ res
+ }
}
fn arg_registers_shared_index(&self) -> bool {
@@ -662,10 +694,10 @@ unsafe impl<A: Architecture> CoreOwnedArrayProvider for CallingConvention<A> {
}
}
-unsafe impl<'a, A: Architecture> CoreArrayWrapper<'a> for CallingConvention<A> {
- type Wrapped = Guard<'a, CallingConvention<A>>;
+unsafe impl<A: Architecture> CoreArrayWrapper for CallingConvention<A> {
+ type Wrapped<'a> = Guard<'a, CallingConvention<A>>;
- unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped {
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(
CallingConvention {
handle: *raw,
diff --git a/rust/src/custombinaryview.rs b/rust/src/custombinaryview.rs
index 956be9bd..4c645ffd 100644
--- a/rust/src/custombinaryview.rs
+++ b/rust/src/custombinaryview.rs
@@ -20,6 +20,7 @@ pub use binaryninjacore_sys::BNModificationStatus as ModificationStatus;
use std::marker::PhantomData;
use std::mem;
+use std::mem::MaybeUninit;
use std::os::raw::c_void;
use std::ptr;
use std::slice;
@@ -122,11 +123,10 @@ where
let long_name = long_name.into_bytes_with_nul();
let long_name_ptr = long_name.as_ref().as_ptr() as *mut _;
- let ctxt = Box::new(unsafe { mem::zeroed() });
- let ctxt = Box::into_raw(ctxt);
+ let ctxt = Box::leak(Box::new(MaybeUninit::zeroed()));
let mut bn_obj = BNCustomBinaryViewType {
- context: ctxt as *mut _,
+ context: ctxt.as_mut_ptr() as *mut _,
create: Some(cb_create::<T>),
parse: Some(cb_parse::<T>),
isValidForData: Some(cb_valid::<T>),
@@ -140,15 +140,16 @@ where
if res.is_null() {
// avoid leaking the space allocated for the type, but also
// avoid running its Drop impl (if any -- not that there should
- // be one since view types live for the life of the process)
- mem::forget(*Box::from_raw(ctxt));
+ // be one since view types live for the life of the process) as
+ // MaybeUninit suppress the Drop implementation of it's inner type
+ drop(Box::from_raw(ctxt));
panic!("bvt registration failed");
}
- ptr::write(ctxt, constructor(BinaryViewType(res)));
+ ctxt.write(constructor(BinaryViewType(res)));
- &*ctxt
+ ctxt.assume_init_mut()
}
}
@@ -297,11 +298,11 @@ unsafe impl CoreOwnedArrayProvider for BinaryViewType {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for BinaryViewType {
- type Wrapped = BinaryViewType;
+unsafe impl CoreArrayWrapper for BinaryViewType {
+ type Wrapped<'a> = Guard<'a, BinaryViewType>;
- unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
- BinaryViewType(*raw)
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
+ Guard::new(BinaryViewType(*raw), &())
}
}
diff --git a/rust/src/debuginfo.rs b/rust/src/debuginfo.rs
index 32db7ebb..ac398334 100644
--- a/rust/src/debuginfo.rs
+++ b/rust/src/debuginfo.rs
@@ -74,7 +74,7 @@ use crate::{
types::{DataVariableAndName, NameAndType, Type},
};
-use std::{hash::Hash, mem, os::raw::c_void, ptr, slice};
+use std::{hash::Hash, os::raw::c_void, ptr, slice};
struct ProgressContext(Option<Box<dyn Fn(usize, usize) -> Result<(), ()>>>);
@@ -109,14 +109,14 @@ impl DebugInfoParser {
/// List all debug-info parsers
pub fn list() -> Array<DebugInfoParser> {
- let mut count: usize = unsafe { mem::zeroed() };
+ let mut count = 0;
let raw_parsers = unsafe { BNGetDebugInfoParsers(&mut count as *mut _) };
unsafe { Array::new(raw_parsers, count, ()) }
}
/// Returns a list of debug-info parsers that are valid for the provided binary view
pub fn parsers_for_view(bv: &BinaryView) -> Array<DebugInfoParser> {
- let mut count: usize = unsafe { mem::zeroed() };
+ let mut count = 0;
let raw_parsers = unsafe { BNGetDebugInfoParsersForView(bv.handle, &mut count as *mut _) };
unsafe { Array::new(raw_parsers, count, ()) }
}
@@ -414,10 +414,7 @@ impl DebugInfo {
}
/// Returns a generator of all functions provided by a named DebugInfoParser
- pub fn functions_by_name<S: BnStrCompatible>(
- &self,
- parser_name: S,
- ) -> Vec<DebugFunctionInfo> {
+ pub fn functions_by_name<S: BnStrCompatible>(&self, parser_name: S) -> Vec<DebugFunctionInfo> {
let parser_name = parser_name.into_bytes_with_nul();
let mut count: usize = 0;
@@ -758,21 +755,15 @@ impl DebugInfo {
let short_name_bytes = new_func.short_name.map(|name| name.into_bytes_with_nul());
let short_name = short_name_bytes
.as_ref()
- .map_or(ptr::null_mut() as *mut _, |name| {
- name.as_ptr() as _
- });
+ .map_or(ptr::null_mut() as *mut _, |name| name.as_ptr() as _);
let full_name_bytes = new_func.full_name.map(|name| name.into_bytes_with_nul());
let full_name = full_name_bytes
.as_ref()
- .map_or(ptr::null_mut() as *mut _, |name| {
- name.as_ptr() as _
- });
+ .map_or(ptr::null_mut() as *mut _, |name| name.as_ptr() as _);
let raw_name_bytes = new_func.raw_name.map(|name| name.into_bytes_with_nul());
let raw_name = raw_name_bytes
.as_ref()
- .map_or(ptr::null_mut() as *mut _, |name| {
- name.as_ptr() as _
- });
+ .map_or(ptr::null_mut() as *mut _, |name| name.as_ptr() as _);
let mut components_array: Vec<*const ::std::os::raw::c_char> =
Vec::with_capacity(new_func.components.len());
diff --git a/rust/src/demangle.rs b/rust/src/demangle.rs
index 19eb085c..3756ea06 100644
--- a/rust/src/demangle.rs
+++ b/rust/src/demangle.rs
@@ -33,8 +33,8 @@ pub fn demangle_gnu3<S: BnStrCompatible>(
) -> Result<(Option<Ref<Type>>, Vec<String>)> {
let mangled_name_bwn = mangled_name.into_bytes_with_nul();
let mangled_name_ptr = mangled_name_bwn.as_ref();
- let mut out_type: *mut BNType = unsafe { std::mem::zeroed() };
- let mut out_name: *mut *mut std::os::raw::c_char = unsafe { std::mem::zeroed() };
+ let mut out_type: *mut BNType = std::ptr::null_mut();
+ let mut out_name: *mut *mut std::os::raw::c_char = std::ptr::null_mut();
let mut out_size: usize = 0;
let res = unsafe {
BNDemangleGNU3(
@@ -89,8 +89,8 @@ pub fn demangle_ms<S: BnStrCompatible>(
let mangled_name_bwn = mangled_name.into_bytes_with_nul();
let mangled_name_ptr = mangled_name_bwn.as_ref();
- let mut out_type: *mut BNType = unsafe { std::mem::zeroed() };
- let mut out_name: *mut *mut std::os::raw::c_char = unsafe { std::mem::zeroed() };
+ let mut out_type: *mut BNType = std::ptr::null_mut();
+ let mut out_name: *mut *mut std::os::raw::c_char = std::ptr::null_mut();
let mut out_size: usize = 0;
let res = unsafe {
BNDemangleMS(
diff --git a/rust/src/disassembly.rs b/rust/src/disassembly.rs
index f213fa0e..8b3a4cb5 100644
--- a/rust/src/disassembly.rs
+++ b/rust/src/disassembly.rs
@@ -307,10 +307,9 @@ impl std::fmt::Display for DisassemblyTextLine {
}
impl From<Vec<InstructionTextToken>> for DisassemblyTextLine {
- fn from(mut tokens: Vec<InstructionTextToken>) -> Self {
- tokens.shrink_to_fit();
+ fn from(tokens: Vec<InstructionTextToken>) -> Self {
+ let mut tokens: Box<[_]> = tokens.into();
- assert!(tokens.len() == tokens.capacity());
// 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();
@@ -345,14 +344,11 @@ impl From<Vec<InstructionTextToken>> for DisassemblyTextLine {
impl From<&Vec<&str>> for DisassemblyTextLine {
fn from(string_tokens: &Vec<&str>) -> Self {
- let mut tokens: Vec<BNInstructionTextToken> = Vec::with_capacity(string_tokens.len());
- tokens.extend(
- string_tokens.iter().map(|&token| {
- InstructionTextToken::new(token, InstructionTextTokenContents::Text).0
- }),
- );
+ let mut tokens: Box<[BNInstructionTextToken]> = string_tokens
+ .iter()
+ .map(|&token| InstructionTextToken::new(token, InstructionTextTokenContents::Text).0)
+ .collect();
- 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
let tokens_pointer = tokens.as_mut_ptr();
let tokens_len = tokens.len();
@@ -416,8 +412,9 @@ impl Default for DisassemblyTextLine {
impl Drop for DisassemblyTextLine {
fn drop(&mut self) {
- unsafe {
- Vec::from_raw_parts(self.0.tokens, self.0.count, self.0.count);
+ if !self.0.tokens.is_null() {
+ let ptr = core::ptr::slice_from_raw_parts_mut(self.0.tokens, self.0.count);
+ let _ = unsafe { Box::from_raw(ptr) };
}
}
}
diff --git a/rust/src/downloadprovider.rs b/rust/src/downloadprovider.rs
index 97ebbdbc..8334e0ce 100644
--- a/rust/src/downloadprovider.rs
+++ b/rust/src/downloadprovider.rs
@@ -1,5 +1,5 @@
use crate::rc::{
- Array, CoreArrayProvider, CoreArrayWrapper, CoreOwnedArrayProvider, Ref, RefCountable,
+ Array, CoreArrayProvider, CoreArrayWrapper, CoreOwnedArrayProvider, Guard, Ref, RefCountable,
};
use crate::settings::Settings;
use crate::string::{BnStrCompatible, BnString};
@@ -71,11 +71,11 @@ unsafe impl CoreOwnedArrayProvider for DownloadProvider {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for DownloadProvider {
- type Wrapped = DownloadProvider;
+unsafe impl CoreArrayWrapper for DownloadProvider {
+ type Wrapped<'a> = Guard<'a, DownloadProvider>;
- unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
- DownloadProvider::from_raw(*raw)
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
+ Guard::new(DownloadProvider::from_raw(*raw), &())
}
}
diff --git a/rust/src/function.rs b/rust/src/function.rs
index 273a0861..74e05ea5 100644
--- a/rust/src/function.rs
+++ b/rust/src/function.rs
@@ -30,7 +30,6 @@ pub use binaryninjacore_sys::BNAnalysisSkipReason as AnalysisSkipReason;
pub use binaryninjacore_sys::BNFunctionAnalysisSkipOverride as FunctionAnalysisSkipOverride;
pub use binaryninjacore_sys::BNFunctionUpdateType as FunctionUpdateType;
-
use std::hash::Hash;
use std::{fmt, mem};
@@ -315,6 +314,22 @@ impl Function {
}
}
+ pub fn parameter_variables(&self) -> Conf<Vec<Variable>> {
+ unsafe {
+ let mut variables = BNGetFunctionParameterVariables(self.handle);
+ let mut result = Vec::with_capacity(variables.count);
+ let confidence = variables.confidence;
+ let vars = std::slice::from_raw_parts(variables.vars, variables.count);
+
+ for i in 0..variables.count {
+ result.push(Variable::from_raw(vars[i]));
+ }
+
+ BNFreeParameterVariables(&mut variables);
+ Conf::new(result, confidence)
+ }
+ }
+
pub fn apply_imported_types(&self, sym: &Symbol, t: Option<&Type>) {
unsafe {
BNApplyImportedTypes(
@@ -407,10 +422,10 @@ unsafe impl CoreOwnedArrayProvider for Function {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for Function {
- type Wrapped = Guard<'a, Function>;
+unsafe impl CoreArrayWrapper for Function {
+ type Wrapped<'a> = Guard<'a, Function>;
- unsafe fn wrap_raw(raw: &'a *mut BNFunction, context: &'a ()) -> Guard<'a, Function> {
+ unsafe fn wrap_raw<'a>(raw: &'a *mut BNFunction, context: &'a ()) -> Self::Wrapped<'a> {
Guard::new(Function { handle: *raw }, context)
}
}
@@ -461,10 +476,10 @@ unsafe impl CoreOwnedArrayProvider for AddressRange {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for AddressRange {
- type Wrapped = &'a AddressRange;
+unsafe impl CoreArrayWrapper for AddressRange {
+ type Wrapped<'a> = &'a AddressRange;
- unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
mem::transmute(raw)
}
}
diff --git a/rust/src/hlil/function.rs b/rust/src/hlil/function.rs
index 4bad7f0f..25608d71 100644
--- a/rust/src/hlil/function.rs
+++ b/rust/src/hlil/function.rs
@@ -2,8 +2,10 @@ use std::hash::{Hash, Hasher};
use binaryninjacore_sys::BNFreeHighLevelILFunction;
use binaryninjacore_sys::BNGetHighLevelILBasicBlockList;
+use binaryninjacore_sys::BNGetHighLevelILIndexForInstruction;
use binaryninjacore_sys::BNGetHighLevelILInstructionCount;
use binaryninjacore_sys::BNGetHighLevelILOwnerFunction;
+use binaryninjacore_sys::BNGetHighLevelILRootExpr;
use binaryninjacore_sys::BNGetHighLevelILSSAForm;
use binaryninjacore_sys::BNHighLevelILFunction;
use binaryninjacore_sys::BNNewHighLevelILFunctionReference;
@@ -52,6 +54,29 @@ impl HighLevelILFunction {
self.instruction_from_idx(expr_idx).lift()
}
+ pub fn instruction_from_instruction_idx(&self, instr_idx: usize) -> HighLevelILInstruction {
+ HighLevelILInstruction::new(self.as_non_ast(), unsafe {
+ BNGetHighLevelILIndexForInstruction(self.handle, instr_idx)
+ })
+ }
+
+ pub fn lifted_instruction_from_instruction_idx(
+ &self,
+ instr_idx: usize,
+ ) -> HighLevelILLiftedInstruction {
+ self.instruction_from_instruction_idx(instr_idx).lift()
+ }
+
+ pub fn root(&self) -> HighLevelILInstruction {
+ HighLevelILInstruction::new(self.as_ast(), unsafe {
+ BNGetHighLevelILRootExpr(self.handle)
+ })
+ }
+
+ pub fn lifted_root(&self) -> HighLevelILLiftedInstruction {
+ self.root().lift()
+ }
+
pub fn instruction_count(&self) -> usize {
unsafe { BNGetHighLevelILInstructionCount(self.handle) }
}
@@ -81,6 +106,22 @@ impl HighLevelILFunction {
unsafe { Array::new(blocks, count, context) }
}
+
+ pub fn as_ast(&self) -> Ref<HighLevelILFunction> {
+ Self {
+ handle: self.handle,
+ full_ast: true,
+ }
+ .to_owned()
+ }
+
+ pub fn as_non_ast(&self) -> Ref<HighLevelILFunction> {
+ Self {
+ handle: self.handle,
+ full_ast: false,
+ }
+ .to_owned()
+ }
}
impl ToOwned for HighLevelILFunction {
diff --git a/rust/src/hlil/instruction.rs b/rust/src/hlil/instruction.rs
index 9bffadaf..7e77e379 100644
--- a/rust/src/hlil/instruction.rs
+++ b/rust/src/hlil/instruction.rs
@@ -16,6 +16,7 @@ pub struct HighLevelILInstruction {
pub function: Ref<HighLevelILFunction>,
pub address: u64,
pub index: usize,
+ pub size: usize,
pub kind: HighLevelILInstructionKind,
}
@@ -629,6 +630,7 @@ impl HighLevelILInstruction {
function,
address: op.address,
index,
+ size: op.size,
kind,
}
}
@@ -878,6 +880,7 @@ impl HighLevelILInstruction {
function: self.function.clone(),
address: self.address,
index: self.index,
+ size: self.size,
kind,
}
}
diff --git a/rust/src/hlil/lift.rs b/rust/src/hlil/lift.rs
index 74ae0c64..731a785c 100644
--- a/rust/src/hlil/lift.rs
+++ b/rust/src/hlil/lift.rs
@@ -24,6 +24,7 @@ pub struct HighLevelILLiftedInstruction {
pub function: Ref<HighLevelILFunction>,
pub address: u64,
pub index: usize,
+ pub size: usize,
pub kind: HighLevelILLiftedInstructionKind,
}
diff --git a/rust/src/hlil/operation.rs b/rust/src/hlil/operation.rs
index 965d9517..ee0d437b 100644
--- a/rust/src/hlil/operation.rs
+++ b/rust/src/hlil/operation.rs
@@ -9,7 +9,7 @@ use super::HighLevelILLiftedInstruction;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GotoLabel {
pub(crate) function: Ref<Function>,
- pub(crate) target: u64,
+ pub target: u64,
}
impl GotoLabel {
diff --git a/rust/src/interaction.rs b/rust/src/interaction.rs
index ccb5c81a..76fc9275 100644
--- a/rust/src/interaction.rs
+++ b/rust/src/interaction.rs
@@ -296,7 +296,9 @@ impl FormInputBuilder {
result.type_ = BNFormInputFieldType::AddressFormField;
result.prompt = prompt.as_ref().as_ptr() as *const c_char;
if let Some(view) = view {
- result.view = view.handle;
+ // the view is being moved into result, there is no need to clone
+ // and drop is intentionally being avoided with `Ref::into_raw`
+ result.view = unsafe { Ref::into_raw(view) }.handle;
}
result.currentAddress = current_address.unwrap_or(0);
result.hasDefault = default.is_some();
diff --git a/rust/src/linearview.rs b/rust/src/linearview.rs
index 09968fc6..31b05ad2 100644
--- a/rust/src/linearview.rs
+++ b/rust/src/linearview.rs
@@ -423,10 +423,10 @@ unsafe impl CoreOwnedArrayProvider for LinearDisassemblyLine {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for LinearDisassemblyLine {
- type Wrapped = Guard<'a, LinearDisassemblyLine>;
+unsafe impl CoreArrayWrapper for LinearDisassemblyLine {
+ type Wrapped<'a> = Guard<'a, LinearDisassemblyLine>;
- unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(LinearDisassemblyLine::from_raw(raw), _context)
}
}
diff --git a/rust/src/llil/operation.rs b/rust/src/llil/operation.rs
index 3c40f207..3ba4fa78 100644
--- a/rust/src/llil/operation.rs
+++ b/rust/src/llil/operation.rs
@@ -89,10 +89,10 @@ pub struct Syscall;
pub struct Intrinsic;
impl<'func, A, M, V> Operation<'func, A, M, NonSSA<V>, Intrinsic>
- where
- A: 'func + Architecture,
- M: FunctionMutability,
- V: NonSSAVariant,
+where
+ A: 'func + Architecture,
+ M: FunctionMutability,
+ V: NonSSAVariant,
{
// TODO: Support register and expression lists
pub fn intrinsic(&self) -> Option<A::Intrinsic> {
@@ -382,12 +382,20 @@ where
}
}
+ pub fn true_target_idx(&self) -> usize {
+ self.op.operands[1] as usize
+ }
+
pub fn false_target(&self) -> Instruction<'func, A, M, F> {
Instruction {
function: self.function,
instr_idx: self.op.operands[2] as usize,
}
}
+
+ pub fn false_target_idx(&self) -> usize {
+ self.op.operands[2] as usize
+ }
}
// LLIL_GOTO
@@ -405,6 +413,10 @@ where
instr_idx: self.op.operands[0] as usize,
}
}
+
+ pub fn target_idx(&self) -> usize {
+ self.op.operands[0] as usize
+ }
}
// LLIL_FLAG_COND
diff --git a/rust/src/metadata.rs b/rust/src/metadata.rs
index e29789e5..6f026a04 100644
--- a/rust/src/metadata.rs
+++ b/rust/src/metadata.rs
@@ -343,10 +343,10 @@ unsafe impl CoreOwnedArrayProvider for Metadata {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for Metadata {
- type Wrapped = Guard<'a, Metadata>;
+unsafe impl CoreArrayWrapper for Metadata {
+ type Wrapped<'a> = Guard<'a, Metadata>;
- unsafe fn wrap_raw(raw: &'a *mut BNMetadata, context: &'a ()) -> Guard<'a, Metadata> {
+ unsafe fn wrap_raw<'a>(raw: &'a *mut BNMetadata, context: &'a ()) -> Self::Wrapped<'a> {
Guard::new(Metadata::from_raw(*raw), context)
}
}
@@ -403,12 +403,6 @@ impl From<&str> for Ref<Metadata> {
}
}
-impl<T: Into<Ref<Metadata>>> From<&T> for Ref<Metadata> {
- fn from(value: &T) -> Self {
- value.into()
- }
-}
-
impl From<&Vec<u8>> for Ref<Metadata> {
fn from(value: &Vec<u8>) -> Self {
unsafe { Metadata::ref_from_raw(BNCreateMetadataRawData(value.as_ptr(), value.len())) }
@@ -441,16 +435,15 @@ impl From<&Array<Metadata>> for Ref<Metadata> {
impl<S: BnStrCompatible> From<HashMap<S, Ref<Metadata>>> for Ref<Metadata> {
fn from(value: HashMap<S, Ref<Metadata>>) -> Self {
- let mut key_refs: Vec<S::Result> = vec![];
- let mut keys: Vec<*const c_char> = vec![];
- let mut values: Vec<*mut BNMetadata> = vec![];
- for (k, v) in value.into_iter() {
- key_refs.push(k.into_bytes_with_nul());
- values.push(v.as_ref().handle);
- }
- for k in &key_refs {
- keys.push(k.as_ref().as_ptr() as *const c_char);
- }
+ let data: Vec<(S::Result, Ref<Metadata>)> = value
+ .into_iter()
+ .map(|(k, v)| (k.into_bytes_with_nul(), v))
+ .collect();
+ let mut keys: Vec<*const c_char> = data
+ .iter()
+ .map(|(k, _)| k.as_ref().as_ptr() as *const c_char)
+ .collect();
+ let mut values: Vec<*mut BNMetadata> = data.iter().map(|(_, v)| v.handle).collect();
unsafe {
Metadata::ref_from_raw(BNCreateMetadataValueStore(
@@ -462,19 +455,21 @@ impl<S: BnStrCompatible> From<HashMap<S, Ref<Metadata>>> for Ref<Metadata> {
}
}
-impl<S: BnStrCompatible + Copy, T: Into<Ref<Metadata>>> From<&[(S, T)]> for Ref<Metadata> {
+impl<S, T> From<&[(S, T)]> for Ref<Metadata>
+where
+ S: BnStrCompatible + Copy,
+ for<'a> &'a T: Into<Ref<Metadata>>,
+{
fn from(value: &[(S, T)]) -> Self {
- let mut key_refs: Vec<S::Result> = vec![];
- let mut keys: Vec<*const c_char> = vec![];
- let mut values: Vec<*mut BNMetadata> = vec![];
- for (k, v) in value.iter() {
- key_refs.push(k.into_bytes_with_nul());
- let value_metadata: Ref<Metadata> = v.into();
- values.push(value_metadata.handle);
- }
- for k in &key_refs {
- keys.push(k.as_ref().as_ptr() as *const c_char);
- }
+ let data: Vec<(S::Result, Ref<Metadata>)> = value
+ .into_iter()
+ .map(|(k, v)| (k.into_bytes_with_nul(), v.into()))
+ .collect();
+ let mut keys: Vec<*const c_char> = data
+ .iter()
+ .map(|(k, _)| k.as_ref().as_ptr() as *const c_char)
+ .collect();
+ let mut values: Vec<*mut BNMetadata> = data.iter().map(|(_, v)| v.handle).collect();
unsafe {
Metadata::ref_from_raw(BNCreateMetadataValueStore(
@@ -486,29 +481,15 @@ impl<S: BnStrCompatible + Copy, T: Into<Ref<Metadata>>> From<&[(S, T)]> for Ref<
}
}
-impl<S: BnStrCompatible + Copy, T: Into<Ref<Metadata>>, const N: usize> From<[(S, T); N]>
- for Ref<Metadata>
+impl<S, T, const N: usize> From<[(S, T); N]> for Ref<Metadata>
+where
+ S: BnStrCompatible + Copy,
+ for<'a> &'a T: Into<Ref<Metadata>>,
{
fn from(value: [(S, T); N]) -> Self {
- let mut key_refs: Vec<S::Result> = vec![];
- let mut keys: Vec<*const c_char> = vec![];
- let mut values: Vec<*mut BNMetadata> = vec![];
- for (k, v) in value.into_iter() {
- key_refs.push(k.into_bytes_with_nul());
- let value_metadata: Ref<Metadata> = v.into();
- values.push(value_metadata.handle);
- }
- for k in &key_refs {
- keys.push(k.as_ref().as_ptr() as *const c_char);
- }
-
- unsafe {
- Metadata::ref_from_raw(BNCreateMetadataValueStore(
- keys.as_mut_ptr(),
- values.as_mut_ptr(),
- keys.len(),
- ))
- }
+ let slice = &value[..];
+ // use the `impl From<&[(S, T)]>`
+ slice.into()
}
}
diff --git a/rust/src/mlil/function.rs b/rust/src/mlil/function.rs
index 16cc5102..0b662578 100644
--- a/rust/src/mlil/function.rs
+++ b/rust/src/mlil/function.rs
@@ -2,6 +2,7 @@ use core::hash::{Hash, Hasher};
use binaryninjacore_sys::BNFreeMediumLevelILFunction;
use binaryninjacore_sys::BNGetMediumLevelILBasicBlockList;
+use binaryninjacore_sys::BNGetMediumLevelILIndexForInstruction;
use binaryninjacore_sys::BNGetMediumLevelILInstructionCount;
use binaryninjacore_sys::BNGetMediumLevelILOwnerFunction;
use binaryninjacore_sys::BNGetMediumLevelILSSAForm;
@@ -65,6 +66,19 @@ impl MediumLevelILFunction {
self.instruction_from_idx(expr_idx).lift()
}
+ pub fn instruction_from_instruction_idx(&self, instr_idx: usize) -> MediumLevelILInstruction {
+ MediumLevelILInstruction::new(self.to_owned(), unsafe {
+ BNGetMediumLevelILIndexForInstruction(self.handle, instr_idx)
+ })
+ }
+
+ pub fn lifted_instruction_from_instruction_idx(
+ &self,
+ instr_idx: usize,
+ ) -> MediumLevelILLiftedInstruction {
+ self.instruction_from_instruction_idx(instr_idx).lift()
+ }
+
pub fn instruction_count(&self) -> usize {
unsafe { BNGetMediumLevelILInstructionCount(self.handle) }
}
diff --git a/rust/src/mlil/instruction.rs b/rust/src/mlil/instruction.rs
index 3b5dcfb4..bd2cc717 100644
--- a/rust/src/mlil/instruction.rs
+++ b/rust/src/mlil/instruction.rs
@@ -18,6 +18,7 @@ pub struct MediumLevelILInstruction {
pub function: Ref<MediumLevelILFunction>,
pub address: u64,
pub index: usize,
+ pub size: usize,
pub kind: MediumLevelILInstructionKind,
}
@@ -704,7 +705,12 @@ impl MediumLevelILInstruction {
}),
// translated directly into a list for Expression or Variables
// TODO MLIL_MEMORY_INTRINSIC_SSA needs to be handled properly
- MLIL_CALL_OUTPUT | MLIL_CALL_PARAM | MLIL_CALL_PARAM_SSA | MLIL_CALL_OUTPUT_SSA | MLIL_MEMORY_INTRINSIC_OUTPUT_SSA | MLIL_MEMORY_INTRINSIC_SSA => {
+ MLIL_CALL_OUTPUT
+ | MLIL_CALL_PARAM
+ | MLIL_CALL_PARAM_SSA
+ | MLIL_CALL_OUTPUT_SSA
+ | MLIL_MEMORY_INTRINSIC_OUTPUT_SSA
+ | MLIL_MEMORY_INTRINSIC_SSA => {
unreachable!()
}
};
@@ -713,6 +719,7 @@ impl MediumLevelILInstruction {
function,
address: op.address,
index,
+ size: op.size,
kind,
}
}
@@ -1022,6 +1029,7 @@ impl MediumLevelILInstruction {
function: self.function.clone(),
address: self.address,
index: self.index,
+ size: self.size,
kind,
}
}
diff --git a/rust/src/mlil/lift.rs b/rust/src/mlil/lift.rs
index 39e8e983..e8548b06 100644
--- a/rust/src/mlil/lift.rs
+++ b/rust/src/mlil/lift.rs
@@ -27,6 +27,7 @@ pub struct MediumLevelILLiftedInstruction {
pub function: Ref<MediumLevelILFunction>,
pub address: u64,
pub index: usize,
+ pub size: usize,
pub kind: MediumLevelILLiftedInstructionKind,
}
diff --git a/rust/src/platform.rs b/rust/src/platform.rs
index 3df5e7c4..42e2f80c 100644
--- a/rust/src/platform.rs
+++ b/rust/src/platform.rs
@@ -373,10 +373,10 @@ unsafe impl CoreOwnedArrayProvider for Platform {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for Platform {
- type Wrapped = Guard<'a, Platform>;
+unsafe impl CoreArrayWrapper for Platform {
+ type Wrapped<'a> = Guard<'a, Platform>;
- unsafe fn wrap_raw(raw: &'a *mut BNPlatform, context: &'a ()) -> Guard<'a, Platform> {
+ unsafe fn wrap_raw<'a>(raw: &'a *mut BNPlatform, context: &'a ()) -> Self::Wrapped<'a> {
debug_assert!(!raw.is_null());
Guard::new(Platform { handle: *raw }, context)
}
diff --git a/rust/src/rc.rs b/rust/src/rc.rs
index cdcae179..eaf8e5d2 100644
--- a/rust/src/rc.rs
+++ b/rust/src/rc.rs
@@ -196,14 +196,12 @@ pub unsafe trait CoreOwnedArrayProvider: CoreArrayProvider {
unsafe fn free(raw: *mut Self::Raw, count: usize, context: &Self::Context);
}
-pub unsafe trait CoreArrayWrapper<'a>: CoreArrayProvider
-where
- Self::Raw: 'a,
- Self::Context: 'a,
-{
- type Wrapped: 'a;
+pub unsafe trait CoreArrayWrapper: CoreArrayProvider {
+ type Wrapped<'a>
+ where
+ Self: 'a;
- unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped;
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a>;
}
pub struct Array<P: CoreOwnedArrayProvider> {
@@ -250,16 +248,16 @@ impl<P: CoreOwnedArrayProvider> Array<P> {
}
}
-impl<'a, P: 'a + CoreArrayWrapper<'a> + CoreOwnedArrayProvider> Array<P> {
+impl<P: CoreArrayWrapper + CoreOwnedArrayProvider> Array<P> {
#[inline]
- pub fn get(&'a self, index: usize) -> P::Wrapped {
+ pub fn get(&self, index: usize) -> P::Wrapped<'_> {
unsafe {
let backing = slice::from_raw_parts(self.contents, self.count);
P::wrap_raw(&backing[index], &self.context)
}
}
- pub fn iter(&'a self) -> ArrayIter<'a, P> {
+ pub fn iter(&self) -> ArrayIter<P> {
ArrayIter {
it: unsafe { slice::from_raw_parts(self.contents, self.count).iter() },
context: &self.context,
@@ -267,8 +265,8 @@ impl<'a, P: 'a + CoreArrayWrapper<'a> + CoreOwnedArrayProvider> Array<P> {
}
}
-impl<'a, P: 'a + CoreArrayWrapper<'a> + CoreOwnedArrayProvider> IntoIterator for &'a Array<P> {
- type Item = P::Wrapped;
+impl<'a, P: CoreArrayWrapper + CoreOwnedArrayProvider> IntoIterator for &'a Array<P> {
+ type Item = P::Wrapped<'a>;
type IntoIter = ArrayIter<'a, P>;
fn into_iter(self) -> Self::IntoIter {
@@ -323,16 +321,16 @@ impl<P: CoreArrayProvider> ArrayGuard<P> {
}
}
-impl<'a, P: 'a + CoreArrayWrapper<'a> + CoreArrayProvider> ArrayGuard<P> {
+impl<P: CoreArrayWrapper + CoreArrayProvider> ArrayGuard<P> {
#[inline]
- pub fn get(&'a self, index: usize) -> P::Wrapped {
+ pub fn get(&self, index: usize) -> P::Wrapped<'_> {
unsafe {
let backing = slice::from_raw_parts(self.contents, self.count);
P::wrap_raw(&backing[index], &self.context)
}
}
- pub fn iter(&'a self) -> ArrayIter<'a, P> {
+ pub fn iter(&self) -> ArrayIter<P> {
ArrayIter {
it: unsafe { slice::from_raw_parts(self.contents, self.count).iter() },
context: &self.context,
@@ -340,8 +338,8 @@ impl<'a, P: 'a + CoreArrayWrapper<'a> + CoreArrayProvider> ArrayGuard<P> {
}
}
-impl<'a, P: 'a + CoreArrayWrapper<'a> + CoreArrayProvider> IntoIterator for &'a ArrayGuard<P> {
- type Item = P::Wrapped;
+impl<'a, P: CoreArrayWrapper + CoreArrayProvider> IntoIterator for &'a ArrayGuard<P> {
+ type Item = P::Wrapped<'a>;
type IntoIter = ArrayIter<'a, P>;
fn into_iter(self) -> Self::IntoIter {
@@ -351,27 +349,27 @@ impl<'a, P: 'a + CoreArrayWrapper<'a> + CoreArrayProvider> IntoIterator for &'a
pub struct ArrayIter<'a, P>
where
- P: 'a + CoreArrayWrapper<'a>,
+ P: CoreArrayWrapper,
{
it: slice::Iter<'a, P::Raw>,
context: &'a P::Context,
}
-unsafe impl<'a, P> Send for ArrayIter<'a, P>
+unsafe impl<P> Send for ArrayIter<'_, P>
where
- P: CoreArrayWrapper<'a>,
+ P: CoreArrayWrapper,
P::Context: Sync,
{
}
impl<'a, P> Iterator for ArrayIter<'a, P>
where
- P: 'a + CoreArrayWrapper<'a>,
+ P: 'a + CoreArrayWrapper,
{
- type Item = P::Wrapped;
+ type Item = P::Wrapped<'a>;
#[inline]
- fn next(&mut self) -> Option<P::Wrapped> {
+ fn next(&mut self) -> Option<Self::Item> {
self.it
.next()
.map(|r| unsafe { P::wrap_raw(r, self.context) })
@@ -385,7 +383,7 @@ where
impl<'a, P> ExactSizeIterator for ArrayIter<'a, P>
where
- P: 'a + CoreArrayWrapper<'a>,
+ P: 'a + CoreArrayWrapper,
{
#[inline]
fn len(&self) -> usize {
@@ -395,10 +393,10 @@ where
impl<'a, P> DoubleEndedIterator for ArrayIter<'a, P>
where
- P: 'a + CoreArrayWrapper<'a>,
+ P: 'a + CoreArrayWrapper,
{
#[inline]
- fn next_back(&mut self) -> Option<P::Wrapped> {
+ fn next_back(&mut self) -> Option<P::Wrapped<'a>> {
self.it
.next_back()
.map(|r| unsafe { P::wrap_raw(r, self.context) })
@@ -412,20 +410,20 @@ use rayon::prelude::*;
use rayon::iter::plumbing::*;
#[cfg(feature = "rayon")]
-impl<'a, P> Array<P>
+impl<P> Array<P>
where
- P: 'a + CoreArrayWrapper<'a> + CoreOwnedArrayProvider,
+ P: CoreArrayWrapper + CoreOwnedArrayProvider,
P::Context: Sync,
- P::Wrapped: Send,
+ for<'a> P::Wrapped<'a>: Send,
{
- pub fn par_iter(&'a self) -> ParArrayIter<'a, P> {
+ pub fn par_iter(&self) -> ParArrayIter<'_, P> {
ParArrayIter { it: self.iter() }
}
}
#[cfg(feature = "rayon")]
pub struct ParArrayIter<'a, P>
where
- P: 'a + CoreArrayWrapper<'a>,
+ P: 'a + CoreArrayWrapper,
ArrayIter<'a, P>: Send,
{
it: ArrayIter<'a, P>,
@@ -434,11 +432,11 @@ where
#[cfg(feature = "rayon")]
impl<'a, P> ParallelIterator for ParArrayIter<'a, P>
where
- P: 'a + CoreArrayWrapper<'a>,
- P::Wrapped: Send,
+ P: 'a + CoreArrayWrapper,
+ P::Wrapped<'a>: Send,
ArrayIter<'a, P>: Send,
{
- type Item = P::Wrapped;
+ type Item = P::Wrapped<'a>;
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
@@ -455,8 +453,8 @@ where
#[cfg(feature = "rayon")]
impl<'a, P> IndexedParallelIterator for ParArrayIter<'a, P>
where
- P: 'a + CoreArrayWrapper<'a>,
- P::Wrapped: Send,
+ P: 'a + CoreArrayWrapper,
+ P::Wrapped<'a>: Send,
ArrayIter<'a, P>: Send,
{
fn drive<C>(self, consumer: C) -> C::Result
@@ -481,7 +479,7 @@ where
#[cfg(feature = "rayon")]
struct ArrayIterProducer<'a, P>
where
- P: 'a + CoreArrayWrapper<'a>,
+ P: 'a + CoreArrayWrapper,
ArrayIter<'a, P>: Send,
{
it: ArrayIter<'a, P>,
@@ -490,10 +488,10 @@ where
#[cfg(feature = "rayon")]
impl<'a, P> Producer for ArrayIterProducer<'a, P>
where
- P: 'a + CoreArrayWrapper<'a>,
+ P: 'a + CoreArrayWrapper,
ArrayIter<'a, P>: Send,
{
- type Item = P::Wrapped;
+ type Item = P::Wrapped<'a>;
type IntoIter = ArrayIter<'a, P>;
fn into_iter(self) -> ArrayIter<'a, P> {
diff --git a/rust/src/references.rs b/rust/src/references.rs
index 76ac4493..8b4ebb87 100644
--- a/rust/src/references.rs
+++ b/rust/src/references.rs
@@ -1,6 +1,6 @@
use crate::architecture::CoreArchitecture;
use crate::function::Function;
-use crate::rc::{CoreArrayProvider, CoreArrayWrapper, CoreOwnedArrayProvider, Ref};
+use crate::rc::{CoreArrayProvider, CoreArrayWrapper, CoreOwnedArrayProvider, Guard, Ref};
use binaryninjacore_sys::{BNFreeCodeReferences, BNFreeDataReferences, BNReferenceSource};
use std::mem::ManuallyDrop;
@@ -64,11 +64,11 @@ unsafe impl CoreOwnedArrayProvider for CodeReference {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for CodeReference {
- type Wrapped = CodeReference;
+unsafe impl CoreArrayWrapper for CodeReference {
+ type Wrapped<'a> = Guard<'a, CodeReference>;
- unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
- CodeReference::new(raw)
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
+ Guard::new(CodeReference::new(raw), &())
}
}
@@ -85,10 +85,10 @@ unsafe impl CoreOwnedArrayProvider for DataReference {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for DataReference {
- type Wrapped = DataReference;
+unsafe impl CoreArrayWrapper for DataReference {
+ type Wrapped<'a> = DataReference;
- unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
DataReference { address: *raw }
}
}
diff --git a/rust/src/relocation.rs b/rust/src/relocation.rs
index f9cbb3c5..17fd5958 100644
--- a/rust/src/relocation.rs
+++ b/rust/src/relocation.rs
@@ -1,3 +1,4 @@
+use crate::rc::Guard;
use crate::string::BnStrCompatible;
use crate::{
architecture::{Architecture, CoreArchitecture},
@@ -8,6 +9,7 @@ use crate::{
};
use binaryninjacore_sys::*;
use std::borrow::Borrow;
+use std::mem::MaybeUninit;
use std::os::raw::c_void;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -227,10 +229,10 @@ unsafe impl CoreOwnedArrayProvider for Relocation {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for Relocation {
- type Wrapped = Relocation;
- unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
- Relocation(*raw)
+unsafe impl CoreArrayWrapper for Relocation {
+ type Wrapped<'a> = Guard<'a, Relocation>;
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
+ Guard::new(Relocation(*raw), &())
}
}
@@ -501,12 +503,9 @@ where
let name = name.into_bytes_with_nul();
- let uninit_handler = RelocationHandlerBuilder {
- handler: unsafe { std::mem::zeroed() },
- };
- let raw = Box::into_raw(Box::new(uninit_handler));
+ let raw = Box::leak(Box::new(MaybeUninit::<RelocationHandlerBuilder<_>>::zeroed()));
let mut custom_handler = BNCustomRelocationHandler {
- context: raw as *mut _,
+ context: raw.as_mut_ptr() as *mut _,
freeObject: Some(cb_free::<R>),
getRelocationInfo: Some(cb_get_relocation_info::<R>),
applyRelocation: Some(cb_apply_relocation::<R>),
@@ -517,13 +516,12 @@ where
assert!(!handle_raw.is_null());
let handle = CoreRelocationHandler(handle_raw);
let custom_handle = CustomRelocationHandlerHandle {
- handle: raw as *mut R,
+ handle: raw.as_mut_ptr() as *mut R,
};
unsafe {
- core::ptr::write(
- &mut raw.as_mut().unwrap().handler,
- func(custom_handle, CoreRelocationHandler(handle.0)),
- );
+ raw.write(RelocationHandlerBuilder {
+ handler: func(custom_handle, CoreRelocationHandler(handle.0)),
+ });
BNArchitectureRegisterRelocationHandler(
arch.handle().as_ref().0,
diff --git a/rust/src/section.rs b/rust/src/section.rs
index 25e8ea5d..06d09fca 100644
--- a/rust/src/section.rs
+++ b/rust/src/section.rs
@@ -179,10 +179,10 @@ unsafe impl CoreOwnedArrayProvider for Section {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for Section {
- type Wrapped = Guard<'a, Section>;
+unsafe impl CoreArrayWrapper for Section {
+ type Wrapped<'a> = Guard<'a, Section>;
- unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped {
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(Section::from_raw(*raw), context)
}
}
diff --git a/rust/src/segment.rs b/rust/src/segment.rs
index 2de785c5..32f9db3a 100644
--- a/rust/src/segment.rs
+++ b/rust/src/segment.rs
@@ -209,10 +209,10 @@ unsafe impl CoreOwnedArrayProvider for Segment {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for Segment {
- type Wrapped = Guard<'a, Segment>;
+unsafe impl CoreArrayWrapper for Segment {
+ type Wrapped<'a> = Guard<'a, Segment>;
- unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped {
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(Segment::from_raw(*raw), context)
}
}
diff --git a/rust/src/string.rs b/rust/src/string.rs
index 1011ca49..75942da9 100644
--- a/rust/src/string.rs
+++ b/rust/src/string.rs
@@ -169,10 +169,10 @@ unsafe impl CoreOwnedArrayProvider for BnString {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for BnString {
- type Wrapped = &'a str;
+unsafe impl CoreArrayWrapper for BnString {
+ type Wrapped<'a> = &'a str;
- unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
CStr::from_ptr(*raw).to_str().unwrap()
}
}
diff --git a/rust/src/symbol.rs b/rust/src/symbol.rs
index cf4c3b10..bdf0d855 100644
--- a/rust/src/symbol.rs
+++ b/rust/src/symbol.rs
@@ -335,10 +335,10 @@ unsafe impl CoreOwnedArrayProvider for Symbol {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for Symbol {
- type Wrapped = Guard<'a, Symbol>;
+unsafe impl CoreArrayWrapper for Symbol {
+ type Wrapped<'a> = Guard<'a, Symbol>;
- unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped {
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(Symbol::from_raw(*raw), context)
}
}
diff --git a/rust/src/types.rs b/rust/src/types.rs
index 9fcd9d45..f886228e 100644
--- a/rust/src/types.rs
+++ b/rust/src/types.rs
@@ -423,7 +423,7 @@ impl TypeBuilder {
pub fn parameters(&self) -> Result<Vec<FunctionParameter>> {
unsafe {
- let mut count: usize = mem::zeroed();
+ let mut count = 0;
let parameters_raw = BNGetTypeBuilderParameters(self.handle, &mut count);
if parameters_raw.is_null() {
Err(())
@@ -795,7 +795,7 @@ impl Type {
pub fn parameters(&self) -> Result<Vec<FunctionParameter>> {
unsafe {
- let mut count: usize = mem::zeroed();
+ let mut count = 0;
let parameters_raw: *mut BNFunctionParameter =
BNGetTypeParameters(self.handle, &mut count);
if parameters_raw.is_null() {
@@ -1455,10 +1455,10 @@ unsafe impl CoreOwnedArrayProvider for NamedTypedVariable {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for NamedTypedVariable {
- type Wrapped = ManuallyDrop<NamedTypedVariable>;
+unsafe impl CoreArrayWrapper for NamedTypedVariable {
+ type Wrapped<'a> = ManuallyDrop<NamedTypedVariable>;
- unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
ManuallyDrop::new(NamedTypedVariable {
var: raw.var,
ty: raw.type_,
@@ -1551,7 +1551,7 @@ impl EnumerationBuilder {
pub fn members(&self) -> Vec<EnumerationMember> {
unsafe {
- let mut count: usize = mem::zeroed();
+ let mut count = 0;
let members_raw = BNGetEnumerationBuilderMembers(self.handle, &mut count);
let members: &[BNEnumerationMember] = slice::from_raw_parts(members_raw, count);
@@ -1608,7 +1608,7 @@ impl Enumeration {
pub fn members(&self) -> Vec<EnumerationMember> {
unsafe {
- let mut count: usize = mem::zeroed();
+ let mut count = 0;
let members_raw = BNGetEnumerationMembers(self.handle, &mut count);
let members: &[BNEnumerationMember] = slice::from_raw_parts(members_raw, count);
@@ -1939,7 +1939,7 @@ impl Structure {
pub fn members(&self) -> Result<Vec<StructureMember>> {
unsafe {
- let mut count: usize = mem::zeroed();
+ let mut count = 0;
let members_raw: *mut BNStructureMember =
BNGetStructureMembers(self.handle, &mut count);
if members_raw.is_null() {
@@ -2350,10 +2350,10 @@ unsafe impl CoreOwnedArrayProvider for QualifiedName {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for QualifiedName {
- type Wrapped = &'a QualifiedName;
+unsafe impl CoreArrayWrapper for QualifiedName {
+ type Wrapped<'a> = &'a QualifiedName;
- unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
mem::transmute(raw)
}
}
@@ -2392,10 +2392,10 @@ unsafe impl CoreOwnedArrayProvider for QualifiedNameAndType {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for QualifiedNameAndType {
- type Wrapped = &'a QualifiedNameAndType;
+unsafe impl CoreArrayWrapper for QualifiedNameAndType {
+ type Wrapped<'a> = &'a QualifiedNameAndType;
- unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
mem::transmute(raw)
}
}
@@ -2438,10 +2438,10 @@ unsafe impl CoreOwnedArrayProvider for QualifiedNameTypeAndId {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for QualifiedNameTypeAndId {
- type Wrapped = &'a QualifiedNameTypeAndId;
+unsafe impl CoreArrayWrapper for QualifiedNameTypeAndId {
+ type Wrapped<'a> = &'a QualifiedNameTypeAndId;
- unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
mem::transmute(raw)
}
}
@@ -2527,10 +2527,10 @@ unsafe impl CoreOwnedArrayProvider for NameAndType {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for NameAndType {
- type Wrapped = &'a NameAndType;
+unsafe impl CoreArrayWrapper for NameAndType {
+ type Wrapped<'a> = &'a NameAndType;
- unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
mem::transmute(raw)
}
}
@@ -2606,10 +2606,10 @@ unsafe impl CoreOwnedArrayProvider for DataVariable {
}
}
-unsafe impl<'a> CoreArrayWrapper<'a> for DataVariable {
- type Wrapped = &'a DataVariable;
+unsafe impl CoreArrayWrapper for DataVariable {
+ type Wrapped<'a> = &'a DataVariable;
- unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
mem::transmute(raw)
}
}