From 08d88d805407ad45210380e1b9b79f239760cc47 Mon Sep 17 00:00:00 2001 From: Mark Rowe Date: Tue, 27 May 2025 11:47:19 -0700 Subject: [PATCH] [Rust] Allow mapping between SSA and non-SSA forms of instructions / expressions --- rust/src/low_level_il/expression.rs | 30 ++++++++++++++++++++ rust/src/low_level_il/instruction.rs | 42 ++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/rust/src/low_level_il/expression.rs b/rust/src/low_level_il/expression.rs index 8df8dfe0cf..8d715c4588 100644 --- a/rust/src/low_level_il/expression.rs +++ b/rust/src/low_level_il/expression.rs @@ -104,6 +104,36 @@ where } } +impl LowLevelILExpression<'_, M, SSA, R> +where + M: FunctionMutability, + R: ExpressionResultType, +{ + pub fn non_ssa_form<'func>( + &self, + non_ssa: &'func LowLevelILFunction, + ) -> LowLevelILExpression<'func, M, NonSSA, R> { + use binaryninjacore_sys::BNGetLowLevelILNonSSAExprIndex; + let idx = unsafe { BNGetLowLevelILNonSSAExprIndex(self.function.handle, self.index.0) }; + LowLevelILExpression::new(non_ssa, LowLevelExpressionIndex(idx)) + } +} + +impl LowLevelILExpression<'_, M, NonSSA, R> +where + M: FunctionMutability, + R: ExpressionResultType, +{ + pub fn ssa_form<'func>( + &self, + ssa: &'func LowLevelILFunction, + ) -> LowLevelILExpression<'func, M, SSA, R> { + use binaryninjacore_sys::BNGetLowLevelILSSAExprIndex; + let idx = unsafe { BNGetLowLevelILSSAExprIndex(self.function.handle, self.index.0) }; + LowLevelILExpression::new(ssa, LowLevelExpressionIndex(idx)) + } +} + impl<'func, M> ExpressionHandler<'func, M, SSA> for LowLevelILExpression<'func, M, SSA, ValueExpr> where M: FunctionMutability, diff --git a/rust/src/low_level_il/instruction.rs b/rust/src/low_level_il/instruction.rs index ef546412ed..a216d82efd 100644 --- a/rust/src/low_level_il/instruction.rs +++ b/rust/src/low_level_il/instruction.rs @@ -100,6 +100,48 @@ where } } +impl<'func, M> LowLevelILInstruction<'func, M, NonSSA> +where + M: FunctionMutability, +{ + pub fn ssa_form( + &self, + ssa: &'func LowLevelILFunction, + ) -> LowLevelILInstruction<'func, M, SSA> { + use binaryninjacore_sys::BNGetLowLevelILSSAInstructionIndex; + let idx = unsafe { BNGetLowLevelILSSAInstructionIndex(self.function.handle, self.index.0) }; + LowLevelILInstruction::new(ssa, LowLevelInstructionIndex(idx)) + } +} + +impl<'func, M> LowLevelILInstruction<'func, M, SSA> +where + M: FunctionMutability, +{ + pub fn non_ssa_form( + &self, + non_ssa: &'func LowLevelILFunction, + ) -> LowLevelILInstruction<'func, M, NonSSA> { + use binaryninjacore_sys::BNGetLowLevelILNonSSAInstructionIndex; + let idx = + unsafe { BNGetLowLevelILNonSSAInstructionIndex(self.function.handle, self.index.0) }; + LowLevelILInstruction::new(non_ssa, LowLevelInstructionIndex(idx)) + } +} + +impl Clone for LowLevelILInstruction<'_, M, F> +where + M: FunctionMutability, + F: FunctionForm, +{ + fn clone(&self) -> Self { + Self { + function: self.function, + index: self.index, + } + } +} + impl Debug for LowLevelILInstruction<'_, M, F> where M: FunctionMutability,