summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorPeter LaFosse <peter@vector35.com>2021-08-26 09:02:51 -0400
committerPeter LaFosse <peter@vector35.com>2021-09-06 11:46:38 -0400
commitfb6ebd8167bc3b5dfce911fdf0491b77a5f81757 (patch)
tree1aa5658383094b5f41b156f68c206216beeb1c0a /python
parente0187520be6b4ece01cc875049cf43fd68244246 (diff)
Improve include style in basicblock.py
Improve include style of debuginfo.py
Diffstat (limited to 'python')
-rw-r--r--python/basicblock.py63
-rw-r--r--python/debuginfo.py72
2 files changed, 68 insertions, 67 deletions
diff --git a/python/basicblock.py b/python/basicblock.py
index 3e54b867..c79b5d1a 100644
--- a/python/basicblock.py
+++ b/python/basicblock.py
@@ -23,13 +23,12 @@ from dataclasses import dataclass
from typing import Generator, Optional, List, Tuple
# Binary Ninja components
-import binaryninja
from . import _binaryninjacore as core
from .enums import BranchType, HighlightStandardColor
-# from . import highlight
-# from . import function as function_module
-# from . import binaryview
-# from . import architecture
+from . import binaryview
+from . import architecture
+from . import highlight as _highlight
+from . import function as _function
@dataclass
class BasicBlockEdge:
@@ -49,7 +48,7 @@ class BasicBlockEdge:
class BasicBlock:
- def __init__(self, handle:core.BNBasicBlockHandle, view:Optional['binaryninja.binaryview.BinaryView']=None):
+ def __init__(self, handle:core.BNBasicBlockHandle, view:Optional['binaryview.BinaryView']=None):
self._view = view
_handle = core.BNBasicBlockHandle
self.handle:core.BNBasicBlockHandle = ctypes.cast(handle, _handle)
@@ -91,7 +90,7 @@ class BasicBlock:
except AttributeError:
raise AttributeError("attribute '%s' is read only" % name)
- def __iter__(self) -> Generator[Tuple[List['binaryninja.function.InstructionTextToken'], int], None, None]:
+ def __iter__(self) -> Generator[Tuple[List['_function.InstructionTextToken'], int], None, None]:
if self.arch is None:
raise Exception("Attempting to iterate a BasicBlock object with no Architecture set")
if self.view is None:
@@ -146,7 +145,7 @@ class BasicBlock:
self._instStarts.append(start)
start += length
- def _create_instance(self, handle:core.BNBasicBlockHandle, view:'binaryninja.binaryview.BinaryView') -> 'BasicBlock':
+ def _create_instance(self, handle:core.BNBasicBlockHandle, view:'binaryview.BinaryView') -> 'BasicBlock':
"""Internal method used to instantiate child instances"""
return BasicBlock(handle, view)
@@ -157,18 +156,18 @@ class BasicBlock:
return len(self._instStarts)
@property
- def function(self) -> Optional['binaryninja.function.Function']:
+ def function(self) -> Optional['_function.Function']:
"""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
- self._func = binaryninja.function.Function(self._view, func)
+ self._func = _function.Function(self._view, func)
return self._func
@property
- def view(self) -> Optional['binaryninja.binaryview.BinaryView']:
+ def view(self) -> Optional['binaryview.BinaryView']:
"""BinaryView that contains the basic block (read-only)"""
if self._view is not None:
return self._view
@@ -178,7 +177,7 @@ class BasicBlock:
return self._view
@property
- def arch(self) -> Optional['binaryninja.architecture.Architecture']:
+ def arch(self) -> Optional['architecture.Architecture']:
"""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
@@ -187,7 +186,7 @@ class BasicBlock:
arch = core.BNGetBasicBlockArchitecture(self.handle)
if arch is None:
return None
- self._arch = binaryninja.architecture.CoreArchitecture._from_cache(arch)
+ self._arch = architecture.CoreArchitecture._from_cache(arch)
return self._arch
@property
@@ -270,7 +269,7 @@ class BasicBlock:
return core.BNBasicBlockCanExit(self.handle)
@can_exit.setter
- def can_exit(self, value):
+ def can_exit(self, value:bool) -> None:
"""Sets whether basic block can return or is tagged as 'No Return'"""
core.BNBasicBlockSetCanExit(self.handle, value)
@@ -431,7 +430,7 @@ class BasicBlock:
core.BNFreeBasicBlockList(blocks, count.value)
@property
- def annotations(self) -> List[List['binaryninja.function.InstructionTextToken']]:
+ def annotations(self) -> List[List['_function.InstructionTextToken']]:
"""List of automatic annotations for the start of this block (read-only)"""
assert self.arch is not None, "attempting to get annotation from BasicBlock without architecture"
if self.function is None:
@@ -440,7 +439,7 @@ class BasicBlock:
return self.function.get_block_annotations(self.start, self.arch)
@property
- def disassembly_text(self) -> List['binaryninja.function.DisassemblyTextLine']:
+ def disassembly_text(self) -> List['_function.DisassemblyTextLine']:
"""
``disassembly_text`` property which returns a list of function.DisassemblyTextLine objects for the current basic block.
:Example:
@@ -451,7 +450,7 @@ class BasicBlock:
return self.get_disassembly_text()
@property
- def highlight(self) -> 'binaryninja.highlight.HighlightColor':
+ def highlight(self) -> '_highlight.HighlightColor':
"""Gets or sets the highlight color for basic block
:Example:
@@ -460,10 +459,10 @@ class BasicBlock:
>>> current_basic_block.highlight
<color: blue>
"""
- return binaryninja.highlight.HighlightColor._from_core_struct(core.BNGetBasicBlockHighlight(self.handle))
+ return _highlight.HighlightColor._from_core_struct(core.BNGetBasicBlockHighlight(self.handle))
@highlight.setter
- def highlight(self, value:'binaryninja.highlight.HighlightColor') -> None:
+ def highlight(self, value:'_highlight.HighlightColor') -> None:
self.set_user_highlight(value)
@property
@@ -492,17 +491,19 @@ class BasicBlock:
out_blocks = core.BNGetBasicBlockIteratedDominanceFrontier(block_set, len(blocks), count)
assert out_blocks is not None, "core.BNGetBasicBlockIteratedDominanceFrontier returned None"
result = []
- for i in range(0, count.value):
- handle = core.BNNewBasicBlockReference(out_blocks[i])
- assert handle is not None
- result.append(BasicBlock(handle, blocks[0].view))
- core.BNFreeBasicBlockList(out_blocks, count.value)
- return result
+ try:
+ for i in range(0, count.value):
+ handle = core.BNNewBasicBlockReference(out_blocks[i])
+ assert handle is not None
+ result.append(BasicBlock(handle, blocks[0].view))
+ return result
+ finally:
+ core.BNFreeBasicBlockList(out_blocks, count.value)
def mark_recent_use(self) -> None:
core.BNMarkBasicBlockAsRecentlyUsed(self.handle)
- def get_disassembly_text(self, settings:'binaryninja.function.DisassemblySettings'=None) -> List['binaryninja.function.DisassemblyTextLine']:
+ def get_disassembly_text(self, settings:'_function.DisassemblySettings'=None) -> List['_function.DisassemblyTextLine']:
"""
``get_disassembly_text`` returns a list of DisassemblyTextLine objects for the current basic block.
@@ -542,13 +543,13 @@ class BasicBlock:
:param HighlightStandardColor or HighlightColor color: Color value to use for highlighting
"""
- if not isinstance(color, HighlightStandardColor) and not isinstance(color, binaryninja.highlight.HighlightColor):
+ if not isinstance(color, HighlightStandardColor) and not isinstance(color, _highlight.HighlightColor):
raise ValueError("Specified color is not one of HighlightStandardColor, HighlightColor")
if isinstance(color, HighlightStandardColor):
- color = binaryninja.highlight.HighlightColor(color)
+ color = _highlight.HighlightColor(color)
core.BNSetAutoBasicBlockHighlight(self.handle, color._get_core_struct())
- def set_user_highlight(self, color:'binaryninja.highlight.HighlightColor') -> None:
+ def set_user_highlight(self, color:'_highlight.HighlightColor') -> None:
"""
``set_user_highlight`` highlights the current BasicBlock with the supplied color
@@ -558,10 +559,10 @@ class BasicBlock:
>>> current_basic_block.set_user_highlight(HighlightColor(red=0xff, blue=0xff, green=0))
>>> current_basic_block.set_user_highlight(HighlightStandardColor.BlueHighlightColor)
"""
- if not isinstance(color, HighlightStandardColor) and not isinstance(color, binaryninja.highlight.HighlightColor):
+ if not isinstance(color, HighlightStandardColor) and not isinstance(color, _highlight.HighlightColor):
raise ValueError("Specified color is not one of HighlightStandardColor, HighlightColor")
if isinstance(color, HighlightStandardColor):
- color = binaryninja.highlight.HighlightColor(color)
+ color = _highlight.HighlightColor(color)
core.BNSetUserBasicBlockHighlight(self.handle, color._get_core_struct())
def get_instruction_containing_address(self, addr:int) -> Tuple[bool, int]:
diff --git a/python/debuginfo.py b/python/debuginfo.py
index b3b80614..bafc977c 100644
--- a/python/debuginfo.py
+++ b/python/debuginfo.py
@@ -28,8 +28,10 @@ import binaryninja
from . import _binaryninjacore as core
from . import callingconvention
from . import platform
-from . import types
+from . import types as _types
from . import log
+from . import binaryview
+from . import filemetadata
_debug_info_parsers = {}
@@ -65,7 +67,7 @@ class _DebugInfoParserMetaClass(type):
finally:
core.BNFreeDebugInfoParserList(parsers, count.value)
- def __getitem__(cls, value: str) -> "DebugInfoParser":
+ def __getitem__(cls, value: str) -> 'DebugInfoParser':
"""Returns debug info parser of the given name, if it exists"""
binaryninja._init_plugins()
parser = core.BNGetDebugInfoParserByName(str(value))
@@ -76,7 +78,7 @@ class _DebugInfoParserMetaClass(type):
return DebugInfoParser(parser_ref)
@staticmethod
- def get_parsers_for_view(view: binaryninja.binaryview.BinaryView) -> List["DebugInfoParser"]:
+ def get_parsers_for_view(view: 'binaryview.BinaryView') -> List['DebugInfoParser']:
"""Returns a list of debug-info parsers that are valid for the provided binary view"""
binaryninja._init_plugins()
@@ -94,20 +96,20 @@ class _DebugInfoParserMetaClass(type):
return result
@staticmethod
- def _is_valid(view: core.BNBinaryView, callback: Callable[[binaryninja.binaryview.BinaryView], bool]) -> bool:
+ def _is_valid(view: core.BNBinaryView, callback: Callable[['binaryview.BinaryView'], bool]) -> bool:
try:
- file_metadata = binaryninja.filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
- view_obj = binaryninja.binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
+ file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
+ view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
return callback(view_obj)
except:
log.log_error(traceback.format_exc())
return False
@staticmethod
- def _parse_info(debug_info: core.BNDebugInfo, view: core.BNBinaryView, callback: Callable[["DebugInfo", binaryninja.binaryview.BinaryView], None]) -> None:
+ def _parse_info(debug_info: core.BNDebugInfo, view: core.BNBinaryView, callback: Callable[["DebugInfo", 'binaryview.BinaryView'], None]) -> None:
try:
- file_metadata = binaryninja.filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
- view_obj = binaryninja.binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
+ file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
+ view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
parser_ref = core.BNNewDebugInfoReference(debug_info)
assert parser_ref is not None, "core.BNNewDebugInfoReference returned None"
callback(DebugInfo(parser_ref), view_obj)
@@ -115,8 +117,8 @@ class _DebugInfoParserMetaClass(type):
log.log_error(traceback.format_exc())
@classmethod
- def register(cls, name: str, is_valid: Callable[[binaryninja.binaryview.BinaryView], bool], parse_info: Callable[["DebugInfo", binaryninja.binaryview.BinaryView], None]) -> "DebugInfoParser":
- """Registers a DebugInfoParser. See ``binaryninja.debuginfo.DebugInfoParser`` for more details."""
+ def register(cls, name: str, is_valid: Callable[['binaryview.BinaryView'], bool], parse_info: Callable[["DebugInfo", 'binaryview.BinaryView'], None]) -> "DebugInfoParser":
+ """Registers a DebugInfoParser. See ``debuginfo.DebugInfoParser`` for more details."""
binaryninja._init_plugins()
is_valid_cb = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView))(lambda ctxt, view: cls._is_valid(view, is_valid))
@@ -173,7 +175,7 @@ class DebugInfoParser(object, metaclass=_DebugInfoParserMetaClass):
Multiple debug-info parsers can manually contribute debug info for a binary view by simply calling ``parse_debug_info`` with the
``DebugInfo`` object just returned. This is automatic when opening a binary view with multiple valid debug info parsers. If you
- wish to set the debug info for a binary view without applying it as well, you can call ``binaryninja.binaryview.BinaryView.set_debug_info``.
+ wish to set the debug info for a binary view without applying it as well, you can call ``'binaryview.BinaryView'.set_debug_info``.
"""
def __init__(self, handle: core.BNDebugInfoParser) -> None:
self.handle = core.handle_of_type(handle, core.BNDebugInfoParser)
@@ -202,11 +204,11 @@ class DebugInfoParser(object, metaclass=_DebugInfoParserMetaClass):
"""Debug-info parser's name (read-only)"""
return core.BNGetDebugInfoParserName(self.handle)
- def is_valid_for_view(self, view: binaryninja.binaryview.BinaryView) -> bool:
+ def is_valid_for_view(self, view: 'binaryview.BinaryView') -> bool:
"""Returns whether this debug-info parser is valid for the provided binary view"""
return core.BNIsDebugInfoParserValidForView(self.handle, view.handle)
- def parse_debug_info(self, view: binaryninja.binaryview.BinaryView, debug_info: Optional["DebugInfo"] = None) -> "DebugInfo":
+ def parse_debug_info(self, view: 'binaryview.BinaryView', debug_info: Optional["DebugInfo"] = None) -> "DebugInfo":
"""Returns a ``DebugInfo`` object populated with debug info by this debug-info parser. Only provide a ``DebugInfo`` object if you wish to append to the existing debug info"""
if isinstance(debug_info, DebugInfo):
parser = core.BNParseDebugInfo(self.handle, view.handle, debug_info.handle)
@@ -233,8 +235,8 @@ class DebugFunctionInfo(object):
full_name:Optional[str]
raw_name:Optional[str]
address:Optional[int]
- return_type:Optional[types.Type]
- parameters:Optional[List[Tuple[str, types.Type]]]
+ return_type:Optional[_types.Type]
+ parameters:Optional[List[Tuple[str, _types.Type]]]
variable_parameters:Optional[bool]
calling_convention:Optional[callingconvention.CallingConvention]
platform:Optional[platform.Platform]
@@ -262,8 +264,8 @@ class DebugInfo(object):
DebugInfoParsers. This makes it possible to gather debug info that may be distributed across several different
formats and files.
- DebugInfo cannot be instantiated by the user, instead get it from either the binary view (see ``binaryninja.binaryview.BinaryView.debug_info``)
- or a debug-info parser (see ``binaryninja.debuginfo.DebugInfoParser.parse_debug_info``).
+ DebugInfo cannot be instantiated by the user, instead get it from either the binary view (see ``'binaryview.BinaryView'.debug_info``)
+ or a debug-info parser (see ``debuginfo.DebugInfoParser.parse_debug_info``).
.. note:: Please note that calling one of ``add_*`` functions will not work outside of a debuginfo plugin.
"""
@@ -273,19 +275,19 @@ class DebugInfo(object):
def __del__(self) -> None:
core.BNFreeDebugInfoReference(self.handle)
- def types_from_parser(self, name: Optional[str] = None) -> Iterator[Tuple[str, types.Type]]:
+ def types_from_parser(self, name: Optional[str] = None) -> Iterator[Tuple[str, _types.Type]]:
"""Returns a generator of all types provided by a named DebugInfoParser"""
count = ctypes.c_ulonglong(0)
name_and_types = core.BNGetDebugTypes(self.handle, name, count)
assert name_and_types is not None, "core.BNGetDebugTypes returned None"
try:
for i in range(0, count.value):
- yield (name_and_types[i].name, types.Type(core.BNNewTypeReference(name_and_types[i].type)))
+ yield (name_and_types[i].name, _types.Type(core.BNNewTypeReference(name_and_types[i].type)))
finally:
core.BNFreeDebugTypes(name_and_types, count.value)
@property
- def types(self) -> Iterator[Tuple[str, types.Type]]:
+ def types(self) -> Iterator[Tuple[str, _types.Type]]:
"""A generator of all types provided by DebugInfoParsers"""
return self.types_from_parser()
@@ -297,12 +299,12 @@ class DebugInfo(object):
try:
for i in range(0, count.value):
- parameters: List[Tuple[str, binaryninja.types.Type]] = []
+ parameters: List[Tuple[str, _types.Type]] = []
for j in range(functions[i].parameterCount):
- parameters.append((functions[i].parameterNames[j], binaryninja.types.Type(core.BNNewTypeReference(functions[i].parameterTypes[j]))))
+ parameters.append((functions[i].parameterNames[j], _types.Type(core.BNNewTypeReference(functions[i].parameterTypes[j]))))
if functions[i].returnType:
- return_type = binaryninja.types.Type(core.BNNewTypeReference(functions[i].returnType))
+ return_type = _types.Type(core.BNNewTypeReference(functions[i].returnType))
else:
return_type = None
@@ -335,31 +337,29 @@ class DebugInfo(object):
"""A generator of all functions provided by DebugInfoParsers"""
return self.functions_from_parser()
- def data_variables_from_parser(self, name: Optional[str] = None) -> Iterator[binaryninja.binaryview.DataVariableAndName]:
+ def data_variables_from_parser(self, name: Optional[str] = None) -> Iterator['binaryview.DataVariableAndName']:
"""Returns a generator of all data variables provided by a named DebugInfoParser"""
count = ctypes.c_ulonglong(0)
data_variables = core.BNGetDebugDataVariables(self.handle, name, count)
assert data_variables is not None, "core.BNGetDebugDataVariables returned None"
try:
for i in range(0, count.value):
- yield binaryninja.binaryview.DataVariableAndName(
+ yield binaryview.DataVariableAndName(
data_variables[i].address,
- binaryninja.types.Type(core.BNNewTypeReference(data_variables[i].type)),
+ _types.Type(core.BNNewTypeReference(data_variables[i].type), confidence=data_variables[i].typeConfidence),
data_variables[i].name,
- data_variables[i].autoDiscovered,
- data_variables[i].typeConfidence
- )
+ data_variables[i].autoDiscovered)
finally:
core.BNFreeDataVariablesAndName(data_variables, count.value)
@property
- def data_variables(self) -> Iterator[binaryninja.binaryview.DataVariableAndName]:
+ def data_variables(self) -> Iterator['binaryview.DataVariableAndName']:
"""A generator of all data variables provided by DebugInfoParsers"""
return self.data_variables_from_parser()
- def add_type(self, name: str, new_type: binaryninja.types.Type) -> bool:
+ def add_type(self, name: str, new_type:'_types.Type') -> bool:
"""Adds a type scoped under the current parser's name to the debug info"""
- if isinstance(new_type, binaryninja.types.Type):
+ if isinstance(new_type, _types.Type):
return core.BNAddDebugType(self.handle, name, new_type.handle)
return NotImplemented
@@ -376,7 +376,7 @@ class DebugInfo(object):
if new_func.return_type is None:
func_info.returnType = None
- elif isinstance(new_func.return_type, binaryninja.types.Type):
+ elif isinstance(new_func.return_type, _types.Type):
func_info.returnType = new_func.return_type.handle
else:
return NotImplemented
@@ -412,8 +412,8 @@ class DebugInfo(object):
return core.BNAddDebugFunction(self.handle, func_info)
- def add_data_variable(self, address: int, new_type: binaryninja.types.Type, name: Optional[str] = None) -> bool:
+ def add_data_variable(self, address: int, new_type: '_types.Type', name: Optional[str] = None) -> bool:
"""Adds a data variable scoped under the current parser's name to the debug info"""
- if isinstance(address, int) and isinstance(new_type, binaryninja.types.Type):
+ if isinstance(address, int) and isinstance(new_type, _types.Type):
return core.BNAddDebugDataVariable(self.handle, address, new_type.handle, name)
return NotImplemented