From abc355d98467bc1fce4368d4a7e1aff0e12cc3c3 Mon Sep 17 00:00:00 2001 From: Mark Rowe Date: Tue, 27 May 2025 13:35:46 -0700 Subject: [Rust] Support retrieving use / def of SSA registers in LLIL --- rust/src/low_level_il/function.rs | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'rust/src/low_level_il/function.rs') diff --git a/rust/src/low_level_il/function.rs b/rust/src/low_level_il/function.rs index 6bd1847a..8af89652 100644 --- a/rust/src/low_level_il/function.rs +++ b/rust/src/low_level_il/function.rs @@ -224,6 +224,57 @@ impl Ref> { } } +impl Ref> { + /// Return a vector of all instructions that use the given SSA register. + #[must_use] + pub fn get_ssa_register_uses( + &self, + reg: LowLevelILSSARegisterKind, + ) -> Vec> { + use binaryninjacore_sys::BNGetLowLevelILSSARegisterUses; + let register_id = match reg { + LowLevelILSSARegisterKind::Full { kind, .. } => kind.id(), + LowLevelILSSARegisterKind::Partial { partial_reg, .. } => partial_reg.id(), + }; + let mut count = 0; + let instrs = unsafe { + BNGetLowLevelILSSARegisterUses( + self.handle, + register_id.into(), + reg.version() as usize, + &mut count, + ) + }; + let result = unsafe { std::slice::from_raw_parts(instrs, count) } + .iter() + .map(|idx| LowLevelILInstruction::new(self, LowLevelInstructionIndex(*idx))) + .collect(); + unsafe { BNFreeILInstructionList(instrs) }; + result + } + + /// Returns the instruction that defines the given SSA register. + #[must_use] + pub fn get_ssa_register_definition( + &self, + reg: &LowLevelILSSARegisterKind, + ) -> Option> { + use binaryninjacore_sys::BNGetLowLevelILSSARegisterDefinition; + let register_id = match reg { + LowLevelILSSARegisterKind::Full { kind, .. } => kind.id(), + LowLevelILSSARegisterKind::Partial { partial_reg, .. } => partial_reg.id(), + }; + let instr_idx = unsafe { + BNGetLowLevelILSSARegisterDefinition( + self.handle, + register_id.into(), + reg.version() as usize, + ) + }; + self.instruction_from_index(LowLevelInstructionIndex(instr_idx)) + } +} + impl ToOwned for LowLevelILFunction where M: FunctionMutability, -- cgit v1.3.1