From 0ff1794278a96d0b5c23a0fc0280d1faf5687a94 Mon Sep 17 00:00:00 2001 From: Brian Potchik Date: Sun, 27 Sep 2020 23:20:17 -0400 Subject: Add High Level IL Mappings. --- binaryninjaapi.h | 3 +++ binaryninjacore.h | 4 ++++ mediumlevelil.cpp | 21 +++++++++++++++++++++ mediumlevelilinstruction.cpp | 12 ++++++++++++ mediumlevelilinstruction.h | 2 ++ python/highlevelil.py | 10 ++++++++++ python/lowlevelil.py | 27 +++++++++++++++++++++++++++ python/mediumlevelil.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 122 insertions(+) diff --git a/binaryninjaapi.h b/binaryninjaapi.h index ddfa6340..0e281baa 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -4074,6 +4074,9 @@ __attribute__ ((format (printf, 1, 2))) Ref GetLowLevelIL() const; size_t GetLowLevelILInstructionIndex(size_t instr) const; size_t GetLowLevelILExprIndex(size_t expr) const; + Ref GetHighLevelIL() const; + size_t GetHighLevelILInstructionIndex(size_t instr) const; + size_t GetHighLevelILExprIndex(size_t expr) const; Confidence> GetExprType(size_t expr); Confidence> GetExprType(const MediumLevelILInstruction& expr); diff --git a/binaryninjacore.h b/binaryninjacore.h index c71d67fa..2dfea2c8 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -3933,6 +3933,10 @@ __attribute__ ((format (printf, 1, 2))) BINARYNINJACOREAPI size_t BNGetLowLevelILInstructionIndex(BNMediumLevelILFunction* func, size_t instr); BINARYNINJACOREAPI size_t BNGetLowLevelILExprIndex(BNMediumLevelILFunction* func, size_t expr); + BINARYNINJACOREAPI BNHighLevelILFunction* BNGetHighLevelILForMediumLevelIL(BNMediumLevelILFunction* func); + BINARYNINJACOREAPI size_t BNGetHighLevelILInstructionIndex(BNMediumLevelILFunction* func, size_t instr); + BINARYNINJACOREAPI size_t BNGetHighLevelILExprIndex(BNMediumLevelILFunction* func, size_t expr); + BINARYNINJACOREAPI BNTypeWithConfidence BNGetMediumLevelILExprType(BNMediumLevelILFunction* func, size_t expr); // High-level IL diff --git a/mediumlevelil.cpp b/mediumlevelil.cpp index 2bbaa90e..6fb43d22 100644 --- a/mediumlevelil.cpp +++ b/mediumlevelil.cpp @@ -776,6 +776,27 @@ size_t MediumLevelILFunction::GetLowLevelILExprIndex(size_t expr) const } +Ref MediumLevelILFunction::GetHighLevelIL() const +{ + BNHighLevelILFunction* func = BNGetHighLevelILForMediumLevelIL(m_object); + if (!func) + return nullptr; + return new HighLevelILFunction(func); +} + + +size_t MediumLevelILFunction::GetHighLevelILInstructionIndex(size_t instr) const +{ + return BNGetHighLevelILInstructionIndex(m_object, instr); +} + + +size_t MediumLevelILFunction::GetHighLevelILExprIndex(size_t expr) const +{ + return BNGetHighLevelILExprIndex(m_object, expr); +} + + Confidence> MediumLevelILFunction::GetExprType(size_t expr) { BNTypeWithConfidence result = BNGetMediumLevelILExprType(m_object, expr); diff --git a/mediumlevelilinstruction.cpp b/mediumlevelilinstruction.cpp index 3779eb90..3d90d7d2 100644 --- a/mediumlevelilinstruction.cpp +++ b/mediumlevelilinstruction.cpp @@ -1268,6 +1268,18 @@ size_t MediumLevelILInstructionBase::GetLowLevelILExprIndex() const } +size_t MediumLevelILInstructionBase::GetHighLevelILInstructionIndex() const +{ + return function->GetHighLevelILInstructionIndex(instructionIndex); +} + + +size_t MediumLevelILInstructionBase::GetHighLevelILExprIndex() const +{ + return function->GetHighLevelILExprIndex(exprIndex); +} + + bool MediumLevelILInstructionBase::HasLowLevelIL() const { Ref func = function->GetLowLevelIL(); diff --git a/mediumlevelilinstruction.h b/mediumlevelilinstruction.h index a8c9b8ec..42567834 100644 --- a/mediumlevelilinstruction.h +++ b/mediumlevelilinstruction.h @@ -444,6 +444,8 @@ namespace BinaryNinja size_t GetLowLevelILInstructionIndex() const; size_t GetLowLevelILExprIndex() const; + size_t GetHighLevelILInstructionIndex() const; + size_t GetHighLevelILExprIndex() const; bool HasLowLevelIL() const; LowLevelILInstruction GetLowLevelIL() const; diff --git a/python/highlevelil.py b/python/highlevelil.py index 589d6505..56619f05 100644 --- a/python/highlevelil.py +++ b/python/highlevelil.py @@ -529,6 +529,16 @@ class HighLevelILInstruction(object): """Alias for medium_level_il""" return self.medium_level_il + @property + def low_level_il(self): + """Low level IL form of this expression""" + return self.mlil.llil + + @property + def llil(self): + """Alias for low_level_il""" + return self.low_level_il + @property def il_basic_block(self): """IL basic block object containing this expression (read-only) (only available on finalized functions)""" diff --git a/python/lowlevelil.py b/python/lowlevelil.py index 9435f07b..64a862d6 100644 --- a/python/lowlevelil.py +++ b/python/lowlevelil.py @@ -1025,6 +1025,15 @@ class LowLevelILInstruction(object): def mmlil(self): return self.mapped_medium_level_il + @property + def high_level_il(self): + """Gets the high level IL expression corresponding to this expression (may be None for eliminated instructions)""" + return self.mlil.hlil + + @property + def hlil(self): + return self.high_level_il + @property def value(self): """Value of expression if constant or a known value (read-only)""" @@ -2999,6 +3008,24 @@ class LowLevelILFunction(object): return None return result + def get_high_level_il_instruction_index(self, instr): + med_il = self.medium_level_il + if med_il is None: + return None + mlil_instr = self.get_medium_level_il_instruction_index(instr) + if mlil_instr is None: + return None + return med_il.get_high_level_il_instruction_index(mlil_instr) + + def get_high_level_il_expr_index(self, expr): + med_il = self.medium_level_il + if med_il is None: + return None + mlil_expr = self.get_medium_level_il_expr_index(expr) + if mlil_expr is None: + return None + return med_il.get_high_level_il_expr_index(mlil_expr) + def create_graph(self, settings = None): if settings is not None: settings_obj = settings.handle diff --git a/python/mediumlevelil.py b/python/mediumlevelil.py index 857d8cff..68da31f3 100644 --- a/python/mediumlevelil.py +++ b/python/mediumlevelil.py @@ -469,6 +469,19 @@ class MediumLevelILInstruction(object): """Alias for low_level_il""" return self.low_level_il + @property + def high_level_il(self): + """High level IL form of this expression""" + expr = self._function.get_high_level_il_expr_index(self._expr_index) + if expr is None: + return None + return binaryninja.highlevelil.HighLevelILInstruction(self._function.high_level_il, expr) + + @property + def hlil(self): + """Alias for high_level_il""" + return self.high_level_il + @property def ssa_memory_version(self): """Version of active memory contents in SSA form for this instruction""" @@ -927,6 +940,18 @@ class MediumLevelILFunction(object): """Alias for low_level_il""" return self.low_level_il + @property + def high_level_il(self): + """High level IL for this medium level IL.""" + result = core.BNGetHighLevelILForMediumLevelIL(self.handle) + if not result: + return None + return binaryninja.highlevelil.HighLevelILFunction(self._arch, result, self._source_function) + + @property + def hlil(self): + return self.high_level_il + def get_instruction_start(self, addr, arch = None): if arch is None: arch = self._arch @@ -1137,6 +1162,24 @@ class MediumLevelILFunction(object): return None return result + def get_high_level_il_instruction_index(self, instr): + high_il = self.high_level_il + if high_il is None: + return None + result = core.BNGetHighLevelILInstructionIndex(self.handle, instr) + if result >= core.BNGetHighLevelILInstructionCount(high_il.handle): + return None + return result + + def get_high_level_il_expr_index(self, expr): + high_il = self.high_level_il + if high_il is None: + return None + result = core.BNGetHighLevelILExprIndex(self.handle, expr) + if result >= core.BNGetHighLevelILExprCount(high_il.handle): + return None + return result + def create_graph(self, settings = None): if settings is not None: settings_obj = settings.handle -- cgit v1.3.1