From 3d403cfae9d5a366f112c8a5936a371a01cfd230 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Mon, 10 Jul 2017 21:40:51 -0400 Subject: Add confidence levels to type objects --- python/basicblock.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'python/basicblock.py') diff --git a/python/basicblock.py b/python/basicblock.py index 7858cc60..c92336c5 100644 --- a/python/basicblock.py +++ b/python/basicblock.py @@ -298,8 +298,9 @@ class BasicBlock(object): size = lines[i].tokens[j].size operand = lines[i].tokens[j].operand context = lines[i].tokens[j].context + confidence = lines[i].tokens[j].confidence address = lines[i].tokens[j].address - tokens.append(function.InstructionTextToken(token_type, text, value, size, operand, context, address)) + tokens.append(function.InstructionTextToken(token_type, text, value, size, operand, context, address, confidence)) result.append(function.DisassemblyTextLine(addr, tokens)) core.BNFreeDisassemblyTextLines(lines, count.value) return result -- cgit v1.3.1 From ecfc609d7941694033971ae6b3f96830e7debd70 Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Fri, 21 Jul 2017 14:34:39 -0400 Subject: Adding optimization for accessing arch and function from basic block --- python/basicblock.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'python/basicblock.py') diff --git a/python/basicblock.py b/python/basicblock.py index 7858cc60..178e473a 100644 --- a/python/basicblock.py +++ b/python/basicblock.py @@ -48,6 +48,8 @@ class BasicBlock(object): def __init__(self, view, handle): self.view = view self.handle = core.handle_of_type(handle, core.BNBasicBlock) + self._arch = None + self._func = None def __del__(self): core.BNFreeBasicBlock(self.handle) @@ -65,18 +67,26 @@ class BasicBlock(object): @property def function(self): """Basic block function (read-only)""" + if self._func is not None: + return self._func func = core.BNGetBasicBlockFunction(self.handle) if func is None: return None - return function.Function(self.view, func) + self._func = function.Function(self.view, func) + return self._func @property def arch(self): """Basic block architecture (read-only)""" + # The arch for a BasicBlock isn't going to change so just cache + # it the first time we need it + if self._arch is not None: + return self._arch arch = core.BNGetBasicBlockArchitecture(self.handle) if arch is None: return None - return architecture.Architecture(arch) + self._arch = architecture.Architecture(arch) + return self._arch @property def start(self): -- cgit v1.3.1 From 92ef6481d84d3289ba86a9b9ffe6553bb5842c5d Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Fri, 4 Aug 2017 16:22:25 -0400 Subject: Use the basic block's arch not the view's arch --- python/basicblock.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'python/basicblock.py') diff --git a/python/basicblock.py b/python/basicblock.py index 4a5caa14..fc7a870e 100644 --- a/python/basicblock.py +++ b/python/basicblock.py @@ -274,8 +274,8 @@ class BasicBlock(object): idx = start while idx < end: data = self.view.read(idx, 16) - inst_info = self.view.arch.get_instruction_info(data, idx) - inst_text = self.view.arch.get_instruction_text(data, idx) + inst_info = self.arch.get_instruction_info(data, idx) + inst_text = self.arch.get_instruction_text(data, idx) yield inst_text idx += inst_info.length -- cgit v1.3.1 From 22edfd701bff068067b43a8e6a682202d19ce122 Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Tue, 8 Aug 2017 23:55:48 -0400 Subject: Fixing llil and mlil incoming/outgoing edges, and dominator apis --- python/basicblock.py | 18 +++++++++++------- python/lowlevelil.py | 3 +++ python/mediumlevelil.py | 4 ++++ 3 files changed, 18 insertions(+), 7 deletions(-) (limited to 'python/basicblock.py') diff --git a/python/basicblock.py b/python/basicblock.py index fc7a870e..9ecf90d0 100644 --- a/python/basicblock.py +++ b/python/basicblock.py @@ -64,6 +64,10 @@ class BasicBlock(object): return True return ctypes.addressof(self.handle.contents) != ctypes.addressof(value.handle.contents) + def _create_instance(self, view, handle): + """Internal method used to instantiante child instances""" + return BasicBlock(view, handle) + @property def function(self): """Basic block function (read-only)""" @@ -117,7 +121,7 @@ class BasicBlock(object): for i in xrange(0, count.value): branch_type = BranchType(edges[i].type) if edges[i].target: - target = BasicBlock(self.view, core.BNNewBasicBlockReference(edges[i].target)) + target = self._create_instance(self.view, core.BNNewBasicBlockReference(edges[i].target)) else: target = None result.append(BasicBlockEdge(branch_type, self, target, edges[i].backEdge)) @@ -133,7 +137,7 @@ class BasicBlock(object): for i in xrange(0, count.value): branch_type = BranchType(edges[i].type) if edges[i].target: - target = BasicBlock(self.view, core.BNNewBasicBlockReference(edges[i].target)) + target = self._create_instance(self.view, core.BNNewBasicBlockReference(edges[i].target)) else: target = None result.append(BasicBlockEdge(branch_type, self, target, edges[i].backEdge)) @@ -152,7 +156,7 @@ class BasicBlock(object): blocks = core.BNGetBasicBlockDominators(self.handle, count) result = [] for i in xrange(0, count.value): - result.append(BasicBlock(self.view, core.BNNewBasicBlockReference(blocks[i]))) + result.append(self._create_instance(self.view, core.BNNewBasicBlockReference(blocks[i]))) core.BNFreeBasicBlockList(blocks, count.value) return result @@ -163,7 +167,7 @@ class BasicBlock(object): blocks = core.BNGetBasicBlockStrictDominators(self.handle, count) result = [] for i in xrange(0, count.value): - result.append(BasicBlock(self.view, core.BNNewBasicBlockReference(blocks[i]))) + result.append(self._create_instance(self.view, core.BNNewBasicBlockReference(blocks[i]))) core.BNFreeBasicBlockList(blocks, count.value) return result @@ -173,7 +177,7 @@ class BasicBlock(object): result = core.BNGetBasicBlockImmediateDominator(self.handle) if not result: return None - return BasicBlock(self.view, result) + return self._create_instance(self.view, result) @property def dominator_tree_children(self): @@ -182,7 +186,7 @@ class BasicBlock(object): blocks = core.BNGetBasicBlockDominatorTreeChildren(self.handle, count) result = [] for i in xrange(0, count.value): - result.append(BasicBlock(self.view, core.BNNewBasicBlockReference(blocks[i]))) + result.append(self._create_instance(self.view, core.BNNewBasicBlockReference(blocks[i]))) core.BNFreeBasicBlockList(blocks, count.value) return result @@ -193,7 +197,7 @@ class BasicBlock(object): blocks = core.BNGetBasicBlockDominanceFrontier(self.handle, count) result = [] for i in xrange(0, count.value): - result.append(BasicBlock(self.view, core.BNNewBasicBlockReference(blocks[i]))) + result.append(self._create_instance(self.view, core.BNNewBasicBlockReference(blocks[i]))) core.BNFreeBasicBlockList(blocks, count.value) return result diff --git a/python/lowlevelil.py b/python/lowlevelil.py index 9f532e6f..e50f80c6 100644 --- a/python/lowlevelil.py +++ b/python/lowlevelil.py @@ -1716,6 +1716,9 @@ class LowLevelILBasicBlock(basicblock.BasicBlock): else: return self.il_function[self.end + idx] + def _create_instance(self, view, handle): + """Internal method by super to instantiante child instances""" + return LowLevelILBasicBlock(view, handle, self.il_function) def LLIL_TEMP(n): return n | 0x80000000 diff --git a/python/mediumlevelil.py b/python/mediumlevelil.py index feca0937..c837aa42 100644 --- a/python/mediumlevelil.py +++ b/python/mediumlevelil.py @@ -885,3 +885,7 @@ class MediumLevelILBasicBlock(basicblock.BasicBlock): return self.il_function[idx + self.start] else: return self.il_function[self.end + idx] + + def _create_instance(self, view, handle): + """Internal method by super to instantiante child instances""" + return MediumLevelILBasicBlock(view, handle, self.il_function) -- cgit v1.3.1 From ca2aaa21523b210157fb71c066acfbebf9dadc78 Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Wed, 9 Aug 2017 21:04:13 -0400 Subject: Fixing some broken documentation --- python/__init__.py | 2 +- python/architecture.py | 4 ++-- python/basicblock.py | 6 ++++-- python/function.py | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) (limited to 'python/basicblock.py') diff --git a/python/__init__.py b/python/__init__.py index 7f5206ff..45b115b9 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -70,7 +70,7 @@ def get_install_directory(): """ ``get_install_directory`` returns a string pointing to the installed binary currently running - .warning:: ONLY for use within the Binary Ninja UI, behavior is undefined and unreliable if run headlessly + ..warning:: ONLY for use within the Binary Ninja UI, behavior is undefined and unreliable if run headlessly """ return core.BNGetInstallDirectory() diff --git a/python/architecture.py b/python/architecture.py index fa235985..b5f56767 100644 --- a/python/architecture.py +++ b/python/architecture.py @@ -1658,7 +1658,7 @@ class Architecture(object): :param str filename: optional source filename :param list(str) include_dirs: optional list of string filename include directories :param str auto_type_source: optional source of types if used for automatically generated types - :return: py:class:`TypeParserResult` (a SyntaxError is thrown on parse error) + :return: :py:class:`TypeParserResult` (a SyntaxError is thrown on parse error) :rtype: TypeParserResult :Example: @@ -1704,7 +1704,7 @@ class Architecture(object): :param str filename: filename of file to be parsed :param list(str) include_dirs: optional list of string filename include directories :param str auto_type_source: optional source of types if used for automatically generated types - :return: py:class:`TypeParserResult` (a SyntaxError is thrown on parse error) + :return: :py:class:`TypeParserResult` (a SyntaxError is thrown on parse error) :rtype: TypeParserResult :Example: diff --git a/python/basicblock.py b/python/basicblock.py index 9ecf90d0..623067f5 100644 --- a/python/basicblock.py +++ b/python/basicblock.py @@ -290,9 +290,11 @@ class BasicBlock(object): def get_disassembly_text(self, settings=None): """ ``get_disassembly_text`` returns a list of function.DisassemblyTextLine objects for the current basic block. + + :param DisassemblySettings settings: (optional) DisassemblySettings object :Example: - >>>current_basic_block.get_disassembly_text() + >>> current_basic_block.get_disassembly_text() [<0x100000f30: _main:>, <0x100000f30: push rbp>, ... ] """ settings_obj = None @@ -323,7 +325,7 @@ class BasicBlock(object): """ ``set_auto_highlight`` highlights the current BasicBlock with the supplied color. - .warning:: Use only in analysis plugins. Do not use in regular plugins, as colors won't be saved to the database. + ..warning:: Use only in analysis plugins. Do not use in regular plugins, as colors won't be saved to the database. :param HighlightStandardColor or highlight.HighlightColor color: Color value to use for highlighting """ diff --git a/python/function.py b/python/function.py index f0b6faee..b14ba53e 100644 --- a/python/function.py +++ b/python/function.py @@ -835,7 +835,7 @@ class Function(object): """ ``set_auto_instr_highlight`` highlights the instruction at the specified address with the supplied color - .warning:: Use only in analysis plugins. Do not use in regular plugins, as colors won't be saved to the database. + ..warning:: Use only in analysis plugins. Do not use in regular plugins, as colors won't be saved to the database. :param int addr: virtual address of the instruction to be highlighted :param HighlightStandardColor or highlight.HighlightColor color: Color value to use for highlighting -- cgit v1.3.1 From 7f3efa01f053e19549c480a770728085900c2131 Mon Sep 17 00:00:00 2001 From: Brian Potchik Date: Tue, 22 Aug 2017 12:02:56 -0400 Subject: Add BasicBlock CanExit Accessor. --- basicblock.cpp | 6 ++++++ binaryninjaapi.h | 1 + binaryninjacore.h | 1 + python/basicblock.py | 5 +++++ 4 files changed, 13 insertions(+) (limited to 'python/basicblock.py') diff --git a/basicblock.cpp b/basicblock.cpp index 721f1ca6..89ace134 100644 --- a/basicblock.cpp +++ b/basicblock.cpp @@ -160,6 +160,12 @@ bool BasicBlock::HasUndeterminedOutgoingEdges() const } +bool BasicBlock::CanExit() const +{ + return BNBasicBlockCanExit(m_object); +} + + set> BasicBlock::GetDominators() const { size_t count; diff --git a/binaryninjaapi.h b/binaryninjaapi.h index a1b31089..e16679cc 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -2036,6 +2036,7 @@ namespace BinaryNinja std::vector GetOutgoingEdges() const; std::vector GetIncomingEdges() const; bool HasUndeterminedOutgoingEdges() const; + bool CanExit() const; std::set> GetDominators() const; std::set> GetStrictDominators() const; diff --git a/binaryninjacore.h b/binaryninjacore.h index 5c6619b2..0ee5c4d7 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -2102,6 +2102,7 @@ extern "C" BINARYNINJACOREAPI BNBasicBlockEdge* BNGetBasicBlockIncomingEdges(BNBasicBlock* block, size_t* count); BINARYNINJACOREAPI void BNFreeBasicBlockEdgeList(BNBasicBlockEdge* edges, size_t count); BINARYNINJACOREAPI bool BNBasicBlockHasUndeterminedOutgoingEdges(BNBasicBlock* block); + BINARYNINJACOREAPI bool BNBasicBlockCanExit(BNBasicBlock* block); BINARYNINJACOREAPI size_t BNGetBasicBlockIndex(BNBasicBlock* block); BINARYNINJACOREAPI BNBasicBlock** BNGetBasicBlockDominators(BNBasicBlock* block, size_t* count); BINARYNINJACOREAPI BNBasicBlock** BNGetBasicBlockStrictDominators(BNBasicBlock* block, size_t* count); diff --git a/python/basicblock.py b/python/basicblock.py index 623067f5..72f31876 100644 --- a/python/basicblock.py +++ b/python/basicblock.py @@ -149,6 +149,11 @@ class BasicBlock(object): """Whether basic block has undetermined outgoing edges (read-only)""" return core.BNBasicBlockHasUndeterminedOutgoingEdges(self.handle) + @property + def can_exit(self): + """Whether basic block can return or is tagged as 'No Return' (read-only)""" + return core.BNBasicBlockCanExit(self.handle) + @property def dominators(self): """List of dominators for this basic block (read-only)""" -- cgit v1.3.1