From cecb2af5e8ec590500379e4b103531649de4e5a2 Mon Sep 17 00:00:00 2001 From: Ryan Snyder Date: Mon, 20 Aug 2018 18:58:00 -0400 Subject: api: add methods to get basic block objects from IL instrs --- binaryninjaapi.h | 2 ++ binaryninjacore.h | 2 ++ lowlevelil.cpp | 9 +++++++++ mediumlevelil.cpp | 9 +++++++++ python/lowlevelil.py | 5 +++++ python/mediumlevelil.py | 5 +++++ 6 files changed, 32 insertions(+) diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 620c0472..b00689e0 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -2937,6 +2937,7 @@ namespace BinaryNinja uint32_t GetTemporaryFlagCount(); std::vector> GetBasicBlocks() const; + Ref GetBasicBlockForInstruction(size_t i) const; Ref GetSSAForm() const; Ref GetNonSSAForm() const; @@ -3256,6 +3257,7 @@ namespace BinaryNinja void VisitAllExprs(const std::function& func); std::vector> GetBasicBlocks() const; + Ref GetBasicBlockForInstruction(size_t i) const; Ref GetSSAForm() const; Ref GetNonSSAForm() const; diff --git a/binaryninjacore.h b/binaryninjacore.h index e5949066..c2d9f9f7 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -2768,6 +2768,7 @@ extern "C" BINARYNINJACOREAPI uint32_t BNGetLowLevelILTemporaryFlagCount(BNLowLevelILFunction* func); BINARYNINJACOREAPI BNBasicBlock** BNGetLowLevelILBasicBlockList(BNLowLevelILFunction* func, size_t* count); + BINARYNINJACOREAPI BNBasicBlock* BNGetLowLevelILBasicBlockForInstruction(BNLowLevelILFunction* func, size_t i); BINARYNINJACOREAPI BNLowLevelILFunction* BNGetLowLevelILSSAForm(BNLowLevelILFunction* func); BINARYNINJACOREAPI BNLowLevelILFunction* BNGetLowLevelILNonSSAForm(BNLowLevelILFunction* func); @@ -2889,6 +2890,7 @@ extern "C" BNArchitecture* arch, size_t i, BNInstructionTextToken** tokens, size_t* count); BINARYNINJACOREAPI BNBasicBlock** BNGetMediumLevelILBasicBlockList(BNMediumLevelILFunction* func, size_t* count); + BINARYNINJACOREAPI BNBasicBlock* BNGetMediumLevelILBasicBlockForInstruction(BNMediumLevelILFunction* func, size_t i); BINARYNINJACOREAPI BNMediumLevelILFunction* BNGetMediumLevelILSSAForm(BNMediumLevelILFunction* func); BINARYNINJACOREAPI BNMediumLevelILFunction* BNGetMediumLevelILNonSSAForm(BNMediumLevelILFunction* func); diff --git a/lowlevelil.cpp b/lowlevelil.cpp index 79138159..40748327 100644 --- a/lowlevelil.cpp +++ b/lowlevelil.cpp @@ -491,6 +491,15 @@ vector> LowLevelILFunction::GetBasicBlocks() const } +Ref LowLevelILFunction::GetBasicBlockForInstruction(size_t i) const +{ + BNBasicBlock* block = BNGetLowLevelILBasicBlockForInstruction(m_object, i); + if (!block) + return nullptr; + return new BasicBlock(block); +} + + Ref LowLevelILFunction::GetSSAForm() const { BNLowLevelILFunction* func = BNGetLowLevelILSSAForm(m_object); diff --git a/mediumlevelil.cpp b/mediumlevelil.cpp index 1795a54d..cac45dd6 100644 --- a/mediumlevelil.cpp +++ b/mediumlevelil.cpp @@ -404,6 +404,15 @@ vector> MediumLevelILFunction::GetBasicBlocks() const } +Ref MediumLevelILFunction::GetBasicBlockForInstruction(size_t i) const +{ + BNBasicBlock* block = BNGetMediumLevelILBasicBlockForInstruction(m_object, i); + if (!block) + return nullptr; + return new BasicBlock(block); +} + + Ref MediumLevelILFunction::GetSSAForm() const { BNMediumLevelILFunction* func = BNGetMediumLevelILSSAForm(m_object); diff --git a/python/lowlevelil.py b/python/lowlevelil.py index e714eb48..41579719 100644 --- a/python/lowlevelil.py +++ b/python/lowlevelil.py @@ -536,6 +536,11 @@ class LowLevelILInstruction(object): core.BNFreeInstructionText(tokens, count.value) return result + @property + def il_basic_block(self): + """IL basic block object containing this expression (read-only) (only available on finalized functions)""" + return LowLevelILBasicBlock(self.function.source_function.view, core.BNGetLowLevelILBasicBlockForInstruction(self.function.handle, self.instr_index), self.function) + @property def ssa_form(self): """SSA form of expression (read-only)""" diff --git a/python/mediumlevelil.py b/python/mediumlevelil.py index cfa8f900..c024d287 100644 --- a/python/mediumlevelil.py +++ b/python/mediumlevelil.py @@ -333,6 +333,11 @@ class MediumLevelILInstruction(object): core.BNFreeInstructionText(tokens, count.value) return result + @property + def il_basic_block(self): + """IL basic block object containing this expression (read-only) (only available on finalized functions)""" + return MediumLevelILBasicBlock(self.function.source_function.view, core.BNGetMediumLevelILBasicBlockForInstruction(self.function.handle, self.instr_index), self.function) + @property def ssa_form(self): """SSA form of expression (read-only)""" -- cgit v1.3.1