Skip to content

Commit

Permalink
add Function::{indirect_branches, indirect_branches_at} methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rbran committed May 9, 2024
1 parent cd8714f commit 5cdf27d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
24 changes: 24 additions & 0 deletions rust/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::disassembly::InstructionTextToken;
use crate::rc::*;
use crate::string::*;
use crate::tags::{Tag, TagType};
use crate::types::IndirectBranchInfo;
use crate::types::{ConstantReference, QualifiedName, RegisterValueType, Variable};
use crate::{
architecture::CoreArchitecture,
Expand Down Expand Up @@ -713,6 +714,29 @@ impl Function {

unsafe { Array::new(tags, count, ()) }
}

/// List of indirect branches
pub fn indirect_branches(
&self,
) -> Array<IndirectBranchInfo> {
let mut count = 0;
let branches = unsafe { BNGetIndirectBranches(self.handle, &mut count) };
assert!(!branches.is_null());
unsafe { Array::new(branches, count, ()) }
}

/// List of indirect branches at this address
pub fn indirect_branches_at(
&self,
addr: u64,
arch: Option<CoreArchitecture>,
) -> Array<IndirectBranchInfo> {
let arch = arch.unwrap_or_else(|| self.arch());
let mut count = 0;
let branches = unsafe { BNGetIndirectBranchesAt(self.handle, arch.0, addr, &mut count) };
assert!(!branches.is_null());
unsafe { Array::new(branches, count, ()) }
}
}

impl fmt::Debug for Function {
Expand Down
56 changes: 53 additions & 3 deletions rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3203,15 +3203,15 @@ impl ConstantReference {
value: value.value,
size: value.size,
pointer: value.pointer,
intermediate: value.intermediate
intermediate: value.intermediate,
}
}
pub fn into_raw(self) -> BNConstantReference{
pub fn into_raw(self) -> BNConstantReference {
BNConstantReference {
value: self.value,
size: self.size,
pointer: self.pointer,
intermediate: self.intermediate
intermediate: self.intermediate,
}
}
}
Expand All @@ -3233,3 +3233,53 @@ unsafe impl CoreArrayWrapper for ConstantReference {
Self::from_raw(*raw)
}
}

/////////////////////////
// IndirectBranchInfo

pub struct IndirectBranchInfo {
pub source_arch: CoreArchitecture,
pub source_addr: u64,
pub dest_arch: CoreArchitecture,
pub dest_addr: u64,
pub auto_defined: bool,
}

impl IndirectBranchInfo {
pub fn from_raw(value: BNIndirectBranchInfo) -> Self {
Self {
source_arch: unsafe { CoreArchitecture::from_raw(value.sourceArch) },
source_addr: value.sourceAddr,
dest_arch: unsafe { CoreArchitecture::from_raw(value.destArch) },
dest_addr: value.destAddr,
auto_defined: value.autoDefined,
}
}
pub fn into_raw(self) -> BNIndirectBranchInfo {
BNIndirectBranchInfo {
sourceArch: self.source_arch.0,
sourceAddr: self.source_addr,
destArch: self.dest_arch.0,
destAddr: self.dest_addr,
autoDefined: self.auto_defined,
}
}
}

impl CoreArrayProvider for IndirectBranchInfo {
type Raw = BNIndirectBranchInfo;
type Context = ();
}

unsafe impl CoreOwnedArrayProvider for IndirectBranchInfo {
unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {
BNFreeIndirectBranchList(raw)
}
}

unsafe impl CoreArrayWrapper for IndirectBranchInfo {
type Wrapped<'a> = Self;
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
Self::from_raw(*raw)
}
}

0 comments on commit 5cdf27d

Please sign in to comment.