Skip to content

Commit

Permalink
Add HLIL APIs to fetch the root or by instruction index, and expose t…
Browse files Browse the repository at this point in the history
…he operation size in the Rust API
  • Loading branch information
D0ntPanic committed Apr 30, 2024
1 parent f193132 commit bec6531
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
41 changes: 41 additions & 0 deletions rust/src/hlil/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) }
}
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions rust/src/hlil/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct HighLevelILInstruction {
pub function: Ref<HighLevelILFunction>,
pub address: u64,
pub index: usize,
pub size: usize,
pub kind: HighLevelILInstructionKind,
}

Expand Down Expand Up @@ -629,6 +630,7 @@ impl HighLevelILInstruction {
function,
address: op.address,
index,
size: op.size,
kind,
}
}
Expand Down Expand Up @@ -878,6 +880,7 @@ impl HighLevelILInstruction {
function: self.function.clone(),
address: self.address,
index: self.index,
size: self.size,
kind,
}
}
Expand Down
1 change: 1 addition & 0 deletions rust/src/hlil/lift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct HighLevelILLiftedInstruction {
pub function: Ref<HighLevelILFunction>,
pub address: u64,
pub index: usize,
pub size: usize,
pub kind: HighLevelILLiftedInstructionKind,
}

Expand Down
2 changes: 1 addition & 1 deletion rust/src/hlil/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit bec6531

Please sign in to comment.