summaryrefslogtreecommitdiff
path: root/python/highlevelil.py
diff options
context:
space:
mode:
authorKyleMiles <krm504@nyu.edu>2021-07-20 16:28:33 -0400
committerKyleMiles <krm504@nyu.edu>2021-07-26 16:09:18 -0400
commitd999ff47c7c81030e5c592f93d8a1415de058882 (patch)
tree1f8fa0df54d4a3324ebc713179e128b0a662e9d8 /python/highlevelil.py
parent661d7c953e7a81e82f3eb6a70c03041354d73bfd (diff)
Python API : Fix copyright year, add .vars to IL functions, remove BNGetFunctionILVariables, prevent variables without types (from Mapped MLIL) from erroring out in function.Variable's __repr__, added BNGetLowLevel-Registers, RegisterStacks, Flags, MemoryVersions, and their respective SSA versions, added `LowLevelILFunction`- `.vars`, `.ssa_vars`, `.registers`, `.register_stacks`, `.flags`, `.memory_versions` and ssa versions.
Diffstat (limited to 'python/highlevelil.py')
-rw-r--r--python/highlevelil.py57
1 files changed, 55 insertions, 2 deletions
diff --git a/python/highlevelil.py b/python/highlevelil.py
index 55d979f8..45f6def4 100644
--- a/python/highlevelil.py
+++ b/python/highlevelil.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2019 Vector 35 Inc
+# Copyright (c) 2019-2021 Vector 35 Inc
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
@@ -20,11 +20,12 @@
import ctypes
import struct
+from typing import List
# Binary Ninja components
import binaryninja
from binaryninja import _binaryninjacore as core
-from binaryninja.enums import HighLevelILOperation, InstructionTextTokenType
+from binaryninja.enums import HighLevelILOperation, FunctionGraphType
from binaryninja import function
from binaryninja import lowlevelil
from binaryninja import mediumlevelil
@@ -661,6 +662,13 @@ class HighLevelILFunction(object):
if self.handle is not None:
core.BNFreeHighLevelILFunction(self.handle)
+ def __repr__(self):
+ arch = self.source_function.arch
+ if arch:
+ return "<hlil func: %s@%#x>" % (arch.name, self.source_function.start)
+ else:
+ return "<hlil func: %#x>" % self.source_function.start
+
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
@@ -916,6 +924,51 @@ class HighLevelILFunction(object):
self._source_function = value
@property
+ def il_form(self) -> "binaryninja.enums.FunctionGraphType":
+ if len(self.basic_blocks) < 1:
+ return FunctionGraphType.InvalidILViewType
+ return FunctionGraphType(core.BNGetBasicBlockFunctionGraphType(self.basic_blocks[0].handle))
+
+ @property
+ def vars(self) -> List["binaryninja.function.Variable"]:
+ """This gets just the HLIL variables - you may be interested in the union of `HighLevelIlFunction.source_function.param_vars` for all the variables used in the function"""
+ if self.source_function is None:
+ return []
+
+ if self.il_form == FunctionGraphType.HighLevelILFunctionGraph or self.il_form == FunctionGraphType.HighLevelILSSAFormFunctionGraph:
+ count = ctypes.c_ulonglong()
+ core_variables = core.BNGetHighLevelILVariables(self.handle, count)
+ result = []
+ for var_i in range(count.value):
+ result.append(function.Variable(self.source_function, core_variables[var_i].type, core_variables[var_i].index, core_variables[var_i].storage))
+ core.BNFreeVariableList(core_variables)
+ return result
+ return []
+
+ @property
+ def ssa_vars(self) -> List["binaryninja.mediumlevelil.SSAVariable"]:
+ """This gets just the HLIL SSA variables - you may be interested in the union of `HighLevelIlFunction.source_function.param_vars` for all the variables used in the function"""
+ if self.source_function is None:
+ return []
+
+ if self.il_form == FunctionGraphType.HighLevelILSSAFormFunctionGraph:
+ variable_count = ctypes.c_ulonglong()
+ core_variables = core.BNGetHighLevelILVariables(self.handle, variable_count)
+ result = []
+ for var_i in range(variable_count.value):
+ version_count = ctypes.c_ulonglong()
+ versions = core.BNGetHighLevelILVariableSSAVersions(self.handle, core_variables[var_i], version_count)
+
+ for version_i in range(version_count.value):
+ result.append(mediumlevelil.SSAVariable(function.Variable(self.source_function, core_variables[var_i].type, core_variables[var_i].index, core_variables[var_i].storage), versions[version_i]))
+ core.BNFreeILInstructionList(versions)
+
+ core.BNFreeVariableList(core_variables)
+ return result
+
+ return []
+
+ @property
def medium_level_il(self):
"""Medium level IL for this function"""
result = core.BNGetMediumLevelILForHighLevelILFunction(self.handle)