|
1 | 1 | use std::hash::{Hash, Hasher}; |
2 | 2 |
|
3 | | -use binaryninjacore_sys::BNFreeHighLevelILFunction; |
4 | | -use binaryninjacore_sys::BNGetHighLevelILBasicBlockList; |
5 | | -use binaryninjacore_sys::BNGetHighLevelILIndexForInstruction; |
6 | | -use binaryninjacore_sys::BNGetHighLevelILInstructionCount; |
7 | | -use binaryninjacore_sys::BNGetHighLevelILOwnerFunction; |
8 | | -use binaryninjacore_sys::BNGetHighLevelILRootExpr; |
9 | | -use binaryninjacore_sys::BNGetHighLevelILSSAForm; |
10 | | -use binaryninjacore_sys::BNHighLevelILFunction; |
11 | | -use binaryninjacore_sys::BNNewHighLevelILFunctionReference; |
| 3 | +use binaryninjacore_sys::*; |
12 | 4 |
|
| 5 | +use crate::architecture::CoreArchitecture; |
13 | 6 | use crate::basicblock::BasicBlock; |
14 | 7 | use crate::function::Function; |
15 | 8 | use crate::rc::{Array, Ref, RefCountable}; |
| 9 | +use crate::types::{HighLevelILSSAVariable, SSAVariable, Variable}; |
16 | 10 |
|
17 | 11 | use super::{HighLevelILBlock, HighLevelILInstruction, HighLevelILLiftedInstruction}; |
18 | 12 |
|
@@ -73,6 +67,10 @@ impl HighLevelILFunction { |
73 | 67 | }) |
74 | 68 | } |
75 | 69 |
|
| 70 | + pub fn set_root(&self, new_root: &HighLevelILInstruction) { |
| 71 | + unsafe { BNSetHighLevelILRootExpr(self.handle, new_root.index) } |
| 72 | + } |
| 73 | + |
76 | 74 | pub fn lifted_root(&self) -> HighLevelILLiftedInstruction { |
77 | 75 | self.root().lift() |
78 | 76 | } |
@@ -122,6 +120,136 @@ impl HighLevelILFunction { |
122 | 120 | } |
123 | 121 | .to_owned() |
124 | 122 | } |
| 123 | + |
| 124 | + pub fn current_address(&self) -> u64 { |
| 125 | + unsafe { BNHighLevelILGetCurrentAddress(self.handle) } |
| 126 | + } |
| 127 | + |
| 128 | + pub fn set_current_address(&self, address: u64, arch: Option<CoreArchitecture>) { |
| 129 | + let arch = arch.unwrap_or_else(|| self.get_function().arch()).0; |
| 130 | + unsafe { BNHighLevelILSetCurrentAddress(self.handle, arch, address) } |
| 131 | + } |
| 132 | + |
| 133 | + /// Gets the instruction that contains the given SSA variable's definition. |
| 134 | + /// |
| 135 | + /// Since SSA variables can only be defined once, this will return the single instruction where that occurs. |
| 136 | + /// For SSA variable version 0s, which don't have definitions, this will return None instead. |
| 137 | + pub fn ssa_variable_definition(&self, variable: SSAVariable) -> Option<HighLevelILInstruction> { |
| 138 | + let index = unsafe { |
| 139 | + BNGetHighLevelILSSAVarDefinition( |
| 140 | + self.handle, |
| 141 | + &variable.variable.raw(), |
| 142 | + variable.version, |
| 143 | + ) |
| 144 | + }; |
| 145 | + (index < self.instruction_count()) |
| 146 | + .then(|| HighLevelILInstruction::new(self.to_owned(), index)) |
| 147 | + } |
| 148 | + |
| 149 | + pub fn ssa_memory_definition(&self, version: usize) -> Option<HighLevelILInstruction> { |
| 150 | + let index = unsafe { BNGetHighLevelILSSAMemoryDefinition(self.handle, version) }; |
| 151 | + (index < self.instruction_count()) |
| 152 | + .then(|| HighLevelILInstruction::new(self.to_owned(), index)) |
| 153 | + } |
| 154 | + |
| 155 | + /// Gets all the instructions that use the given SSA variable. |
| 156 | + pub fn ssa_variable_uses(&self, variable: SSAVariable) -> Array<HighLevelILInstruction> { |
| 157 | + let mut count = 0; |
| 158 | + let instrs = unsafe { |
| 159 | + BNGetHighLevelILSSAVarUses( |
| 160 | + self.handle, |
| 161 | + &variable.variable.raw(), |
| 162 | + variable.version, |
| 163 | + &mut count, |
| 164 | + ) |
| 165 | + }; |
| 166 | + assert!(!instrs.is_null()); |
| 167 | + unsafe { Array::new(instrs, count, self.to_owned()) } |
| 168 | + } |
| 169 | + |
| 170 | + pub fn ssa_memory_uses(&self, version: usize) -> Array<HighLevelILInstruction> { |
| 171 | + let mut count = 0; |
| 172 | + let instrs = unsafe { BNGetHighLevelILSSAMemoryUses(self.handle, version, &mut count) }; |
| 173 | + assert!(!instrs.is_null()); |
| 174 | + unsafe { Array::new(instrs, count, self.to_owned()) } |
| 175 | + } |
| 176 | + |
| 177 | + /// Determines if `variable` is live at any point in the function |
| 178 | + pub fn is_ssa_variable_live(&self, variable: SSAVariable) -> bool { |
| 179 | + unsafe { |
| 180 | + BNIsHighLevelILSSAVarLive(self.handle, &variable.variable.raw(), variable.version) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + /// Determines if `variable` is live at a given point in the function |
| 185 | + pub fn is_ssa_variable_live_at( |
| 186 | + &self, |
| 187 | + variable: SSAVariable, |
| 188 | + instr: &HighLevelILInstruction, |
| 189 | + ) -> bool { |
| 190 | + unsafe { |
| 191 | + BNIsHighLevelILSSAVarLiveAt( |
| 192 | + self.handle, |
| 193 | + &variable.variable.raw(), |
| 194 | + variable.version, |
| 195 | + instr.index, |
| 196 | + ) |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + pub fn variable_definitions(&self, variable: Variable) -> Array<HighLevelILInstruction> { |
| 201 | + let mut count = 0; |
| 202 | + let defs = unsafe { |
| 203 | + BNGetHighLevelILVariableDefinitions(self.handle, &variable.raw(), &mut count) |
| 204 | + }; |
| 205 | + assert!(!defs.is_null()); |
| 206 | + unsafe { Array::new(defs, count, self.to_owned()) } |
| 207 | + } |
| 208 | + |
| 209 | + pub fn variable_uses(&self, variable: Variable) -> Array<HighLevelILInstruction> { |
| 210 | + let mut count = 0; |
| 211 | + let instrs = |
| 212 | + unsafe { BNGetHighLevelILVariableUses(self.handle, &variable.raw(), &mut count) }; |
| 213 | + assert!(!instrs.is_null()); |
| 214 | + unsafe { Array::new(instrs, count, self.to_owned()) } |
| 215 | + } |
| 216 | + |
| 217 | + /// Determines if `variable` is live at a given point in the function |
| 218 | + pub fn is_variable_live_at(&self, variable: Variable, instr: &HighLevelILInstruction) -> bool { |
| 219 | + unsafe { BNIsHighLevelILVarLiveAt(self.handle, &variable.raw(), instr.index) } |
| 220 | + } |
| 221 | + |
| 222 | + /// This gets just the HLIL variables - you may be interested in the union |
| 223 | + /// of [crate::function::Function::parameter_variables] and |
| 224 | + /// [crate::mlil::function::MediumLevelILFunction::aliased_variables] as well for all the |
| 225 | + /// variables used in the function |
| 226 | + pub fn variables(&self) -> Array<Variable> { |
| 227 | + let mut count = 0; |
| 228 | + let variables = unsafe { BNGetHighLevelILVariables(self.handle, &mut count) }; |
| 229 | + assert!(!variables.is_null()); |
| 230 | + unsafe { Array::new(variables, count, ()) } |
| 231 | + } |
| 232 | + |
| 233 | + /// This returns a list of Variables that are taken reference to and used |
| 234 | + /// elsewhere. You may also wish to consider [HighLevelILFunction::variables] |
| 235 | + /// and [crate::function::Function::parameter_variables] |
| 236 | + pub fn aliased_variables(&self) -> Array<Variable> { |
| 237 | + let mut count = 0; |
| 238 | + let variables = unsafe { BNGetHighLevelILAliasedVariables(self.handle, &mut count) }; |
| 239 | + assert!(!variables.is_null()); |
| 240 | + unsafe { Array::new(variables, count, ()) } |
| 241 | + } |
| 242 | + |
| 243 | + /// This gets just the HLIL SSA variables - you may be interested in the union |
| 244 | + /// of [crate::function::Function::parameter_variables] and |
| 245 | + /// [crate::mlil::function::MediumLevelILFunction::aliased_variables] as well for all the |
| 246 | + /// variables used in the function |
| 247 | + pub fn ssa_variables(&self) -> Array<HighLevelILSSAVariable> { |
| 248 | + let mut count = 0; |
| 249 | + let variables = unsafe { BNGetHighLevelILVariables(self.handle, &mut count) }; |
| 250 | + assert!(!variables.is_null()); |
| 251 | + unsafe { Array::new(variables, count, self.to_owned()) } |
| 252 | + } |
125 | 253 | } |
126 | 254 |
|
127 | 255 | impl ToOwned for HighLevelILFunction { |
|
0 commit comments