From 5841af2db8e8dcf4e0da0c438ac040c5fa90038b Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Tue, 21 Aug 2018 15:47:34 -0400 Subject: Add return hint MLIL instruction (used in intermediate stages, not emitted in final forms) --- python/mediumlevelil.py | 1 + 1 file changed, 1 insertion(+) (limited to 'python/mediumlevelil.py') diff --git a/python/mediumlevelil.py b/python/mediumlevelil.py index cfa8f900..774231ab 100644 --- a/python/mediumlevelil.py +++ b/python/mediumlevelil.py @@ -128,6 +128,7 @@ class MediumLevelILInstruction(object): MediumLevelILOperation.MLIL_LOW_PART: [("src", "expr")], MediumLevelILOperation.MLIL_JUMP: [("dest", "expr")], MediumLevelILOperation.MLIL_JUMP_TO: [("dest", "expr"), ("targets", "int_list")], + MediumLevelILOperation.MLIL_RET_HINT: [("dest", "expr")], MediumLevelILOperation.MLIL_CALL: [("output", "var_list"), ("dest", "expr"), ("params", "expr_list")], MediumLevelILOperation.MLIL_CALL_UNTYPED: [("output", "expr"), ("dest", "expr"), ("params", "expr"), ("stack", "expr")], MediumLevelILOperation.MLIL_CALL_OUTPUT: [("dest", "var_list")], -- cgit v1.3.1 From ef04e9ae0483574531542223aed3eb1768a01db9 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Thu, 30 Aug 2018 22:13:33 -0400 Subject: Fix signed constants in Python IL --- python/lowlevelil.py | 1 + python/mediumlevelil.py | 1 + 2 files changed, 2 insertions(+) (limited to 'python/mediumlevelil.py') diff --git a/python/lowlevelil.py b/python/lowlevelil.py index 41579719..ec7c37fd 100644 --- a/python/lowlevelil.py +++ b/python/lowlevelil.py @@ -365,6 +365,7 @@ class LowLevelILInstruction(object): name, operand_type = operand if operand_type == "int": value = instr.operands[i] + value = (value & ((1 << 63) - 1)) - (value & (1 << 63)) elif operand_type == "float": if instr.size == 4: value = struct.unpack("f", struct.pack("I", instr.operands[i] & 0xffffffff))[0] diff --git a/python/mediumlevelil.py b/python/mediumlevelil.py index c7b21aa9..d8347ebf 100644 --- a/python/mediumlevelil.py +++ b/python/mediumlevelil.py @@ -230,6 +230,7 @@ class MediumLevelILInstruction(object): name, operand_type = operand if operand_type == "int": value = instr.operands[i] + value = (value & ((1 << 63) - 1)) - (value & (1 << 63)) elif operand_type == "float": if instr.size == 4: value = struct.unpack("f", struct.pack("I", instr.operands[i] & 0xffffffff))[0] -- cgit v1.3.1 From e51031010b107089dd7b5b69039ac42b856a0769 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Tue, 4 Sep 2018 20:15:18 -0400 Subject: Add API to create graphs of IL functions --- binaryninjaapi.h | 4 ++++ binaryninjacore.h | 4 ++++ lowlevelil.cpp | 7 +++++++ mediumlevelil.cpp | 7 +++++++ python/lowlevelil.py | 13 ++++++++++--- python/mediumlevelil.py | 14 +++++++++++--- 6 files changed, 43 insertions(+), 6 deletions(-) (limited to 'python/mediumlevelil.py') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 722edcd3..901ea65d 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -3051,6 +3051,8 @@ namespace BinaryNinja size_t GetMediumLevelILExprIndex(size_t expr) const; size_t GetMappedMediumLevelILInstructionIndex(size_t instr) const; size_t GetMappedMediumLevelILExprIndex(size_t expr) const; + + Ref CreateFunctionGraph(DisassemblySettings* settings = nullptr); }; struct MediumLevelILLabel: public BNMediumLevelILLabel @@ -3382,6 +3384,8 @@ namespace BinaryNinja Confidence> GetExprType(size_t expr); Confidence> GetExprType(const MediumLevelILInstruction& expr); + + Ref CreateFunctionGraph(DisassemblySettings* settings = nullptr); }; class FunctionRecognizer diff --git a/binaryninjacore.h b/binaryninjacore.h index b15ceabd..0916112f 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -2663,6 +2663,10 @@ extern "C" BINARYNINJACOREAPI BNFlowGraph* BNCreateFlowGraph(); BINARYNINJACOREAPI BNFlowGraph* BNCreateFunctionGraph(BNFunction* func, BNFunctionGraphType type, BNDisassemblySettings* settings); + BINARYNINJACOREAPI BNFlowGraph* BNCreateLowLevelILFunctionGraph(BNLowLevelILFunction* func, + BNDisassemblySettings* settings); + BINARYNINJACOREAPI BNFlowGraph* BNCreateMediumLevelILFunctionGraph(BNMediumLevelILFunction* func, + BNDisassemblySettings* settings); BINARYNINJACOREAPI BNFlowGraph* BNCreateCustomFlowGraph(BNCustomFlowGraph* callbacks); BINARYNINJACOREAPI BNFlowGraph* BNNewFlowGraphReference(BNFlowGraph* graph); BINARYNINJACOREAPI void BNFreeFlowGraph(BNFlowGraph* graph); diff --git a/lowlevelil.cpp b/lowlevelil.cpp index 40748327..3e09e085 100644 --- a/lowlevelil.cpp +++ b/lowlevelil.cpp @@ -766,3 +766,10 @@ size_t LowLevelILFunction::GetMappedMediumLevelILExprIndex(size_t expr) const { return BNGetMappedMediumLevelILExprIndex(m_object, expr); } + + +Ref LowLevelILFunction::CreateFunctionGraph(DisassemblySettings* settings) +{ + BNFlowGraph* graph = BNCreateLowLevelILFunctionGraph(m_object, settings ? settings->GetObject() : nullptr); + return new CoreFlowGraph(graph); +} diff --git a/mediumlevelil.cpp b/mediumlevelil.cpp index cac45dd6..2d1fe904 100644 --- a/mediumlevelil.cpp +++ b/mediumlevelil.cpp @@ -738,3 +738,10 @@ Confidence> MediumLevelILFunction::GetExprType(const MediumLevelILInst { return GetExprType(expr.exprIndex); } + + +Ref MediumLevelILFunction::CreateFunctionGraph(DisassemblySettings* settings) +{ + BNFlowGraph* graph = BNCreateMediumLevelILFunctionGraph(m_object, settings ? settings->GetObject() : nullptr); + return new CoreFlowGraph(graph); +} diff --git a/python/lowlevelil.py b/python/lowlevelil.py index ec7c37fd..b2a28d6e 100644 --- a/python/lowlevelil.py +++ b/python/lowlevelil.py @@ -734,9 +734,9 @@ class LowLevelILFunction(object): if handle is not None: self.handle = core.handle_of_type(handle, core.BNLowLevelILFunction) else: - func_handle = None - if self.source_function is not None: - func_handle = self.source_function.handle + if self.source_function is None: + raise ValueError("IL functions must be created with an associated function") + func_handle = self.source_function.handle self.handle = core.BNCreateLowLevelILFunction(arch.handle, func_handle) def __del__(self): @@ -2354,6 +2354,13 @@ class LowLevelILFunction(object): return None return result + def create_graph(self, settings = None): + if settings is not None: + settings_obj = settings.handle + else: + settings_obj = None + return binaryninja.flowgraph.CoreFlowGraph(core.BNCreateLowLevelILFunctionGraph(self.handle, settings_obj)) + class LowLevelILBasicBlock(basicblock.BasicBlock): def __init__(self, view, handle, owner): diff --git a/python/mediumlevelil.py b/python/mediumlevelil.py index d8347ebf..a862adca 100644 --- a/python/mediumlevelil.py +++ b/python/mediumlevelil.py @@ -22,6 +22,7 @@ import ctypes import struct # Binary Ninja components +import binaryninja from binaryninja import _binaryninjacore as core from binaryninja.enums import MediumLevelILOperation, InstructionTextTokenType, ILBranchDependence from binaryninja import basicblock #required for MediumLevelILBasicBlock argument @@ -616,9 +617,9 @@ class MediumLevelILFunction(object): if handle is not None: self.handle = core.handle_of_type(handle, core.BNMediumLevelILFunction) else: - func_handle = None - if self.source_function is not None: - func_handle = self.source_function.handle + if self.source_function is None: + raise ValueError("IL functions must be created with an associated function") + func_handle = self.source_function.handle self.handle = core.BNCreateMediumLevelILFunction(arch.handle, func_handle) def __del__(self): @@ -939,6 +940,13 @@ class MediumLevelILFunction(object): return None return result + def create_graph(self, settings = None): + if settings is not None: + settings_obj = settings.handle + else: + settings_obj = None + return binaryninja.flowgraph.CoreFlowGraph(core.BNCreateMediumLevelILFunctionGraph(self.handle, settings_obj)) + class MediumLevelILBasicBlock(basicblock.BasicBlock): def __init__(self, view, handle, owner): -- cgit v1.3.1