Skip to content

Commit

Permalink
add HighLevelILFunction::ssa_variables method
Browse files Browse the repository at this point in the history
  • Loading branch information
rbran committed May 31, 2024
1 parent 10a8b3c commit 00d7d42
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
13 changes: 12 additions & 1 deletion rust/src/hlil/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::architecture::CoreArchitecture;
use crate::basicblock::BasicBlock;
use crate::function::Function;
use crate::rc::{Array, Ref, RefCountable};
use crate::types::{SSAVariable, Variable};
use crate::types::{HighLevelILSSAVariable, SSAVariable, Variable};

use super::{HighLevelILBlock, HighLevelILInstruction, HighLevelILLiftedInstruction};

Expand Down Expand Up @@ -229,6 +229,17 @@ impl HighLevelILFunction {
assert!(!variables.is_null());
unsafe { Array::new(variables, count, ()) }
}

/// This gets just the HLIL SSA variables - you may be interested in the union
/// of [crate::function::Function::parameter_variables] and
/// [crate::mlil::function::MediumLevelILFunction::aliased_variables] as well for all the
/// variables used in the function
pub fn ssa_variables(&self) -> Array<HighLevelILSSAVariable> {
let mut count = 0;
let variables = unsafe { BNGetHighLevelILVariables(self.handle, &mut count) };
assert!(!variables.is_null());
unsafe { Array::new(variables, count, self.to_owned()) }
}
}

impl ToOwned for HighLevelILFunction {
Expand Down
5 changes: 3 additions & 2 deletions rust/src/mlil/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use crate::function::{Function, Location};
use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Ref, RefCountable};
use crate::string::BnStrCompatible;
use crate::types::{
Conf, PossibleValueSet, RegisterValue, SSAVariable, Type, UserVariableValues, Variable,
Conf, MediumLevelILSSAVariable, PossibleValueSet, RegisterValue, SSAVariable, Type,
UserVariableValues, Variable,
};

use super::{MediumLevelILBlock, MediumLevelILInstruction, MediumLevelILLiftedInstruction};
Expand Down Expand Up @@ -571,7 +572,7 @@ impl MediumLevelILFunction {
/// union of [MediumLevelILFunction::aliased_variables] and
/// [crate::function::Function::parameter_variables] for all the
/// variables used in the function.
pub fn ssa_variables(&self) -> Array<Array<SSAVariable>> {
pub fn ssa_variables(&self) -> Array<MediumLevelILSSAVariable> {
let mut count = 0;
let vars = unsafe { BNGetMediumLevelILVariables(self.handle, &mut count) };
unsafe { Array::new(vars, count, self.to_owned()) }
Expand Down
37 changes: 25 additions & 12 deletions rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,7 @@
use binaryninjacore_sys::*;

use crate::{
architecture::{Architecture, CoreArchitecture},
binaryview::{BinaryView, BinaryViewExt},
callingconvention::CallingConvention,
filemetadata::FileMetadata,
function::Function,
mlil::MediumLevelILFunction,
rc::*,
string::{raw_to_string, BnStrCompatible, BnString},
symbol::Symbol,
architecture::{Architecture, CoreArchitecture}, binaryview::{BinaryView, BinaryViewExt}, callingconvention::CallingConvention, filemetadata::FileMetadata, function::Function, hlil::HighLevelILFunction, mlil::MediumLevelILFunction, rc::*, string::{raw_to_string, BnStrCompatible, BnString}, symbol::Symbol
};

use lazy_static::lazy_static;
Expand Down Expand Up @@ -1461,13 +1453,14 @@ unsafe impl CoreArrayProviderInner for SSAVariable {
}
}

impl CoreArrayProvider for Array<SSAVariable> {
pub struct MediumLevelILSSAVariable;
impl CoreArrayProvider for MediumLevelILSSAVariable {
type Raw = BNVariable;
type Context = Ref<MediumLevelILFunction>;
type Wrapped<'a> = Self;
type Wrapped<'a> = Array<SSAVariable>;
}

unsafe impl CoreArrayProviderInner for Array<SSAVariable> {
unsafe impl CoreArrayProviderInner for MediumLevelILSSAVariable {
unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {
BNFreeVariableList(raw)
}
Expand All @@ -1480,6 +1473,26 @@ unsafe impl CoreArrayProviderInner for Array<SSAVariable> {
}
}

pub struct HighLevelILSSAVariable;
impl CoreArrayProvider for HighLevelILSSAVariable {
type Raw = BNVariable;
type Context = Ref<HighLevelILFunction>;
type Wrapped<'a> = Array<SSAVariable>;
}

unsafe impl CoreArrayProviderInner for HighLevelILSSAVariable {
unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {
BNFreeVariableList(raw)
}

unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
let mut count = 0;
let versions =
unsafe { BNGetHighLevelILVariableSSAVersions(context.handle, raw, &mut count) };
Array::new(versions, count, Variable::from_raw(*raw))
}
}

///////////////
// NamedVariable

Expand Down

0 comments on commit 00d7d42

Please sign in to comment.