From 75242aecc30c565ee56d3e74ec2e2dd502a6f073 Mon Sep 17 00:00:00 2001 From: Josh Watson Date: Sun, 29 Oct 2017 12:51:23 -0400 Subject: Fixed possible IndexError in Function.get_*_at methods (#775) `Function.get_low_level_il_at` and `Function.get_lifted_il_at` methods could raise an IndexError because proper checking was not performed on the index returned by the core method that retrieves the IL instruction index of the requested instruction. The methods now check this value to see if it is equal to the length of the IL function, and if so, returns `None` instead. The onus of checking for `None` will be on the user, but at least they shouldn't have to wrap this in a try/except block anymore. --- python/function.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'python/function.py') diff --git a/python/function.py b/python/function.py index 5daa7b2a..9aafdf52 100644 --- a/python/function.py +++ b/python/function.py @@ -727,7 +727,13 @@ class Function(object): """ if arch is None: arch = self.arch - return self.low_level_il[core.BNGetLowLevelILForInstruction(self.handle, arch.handle, addr)] + + idx = core.BNGetLowLevelILForInstruction(self.handle, arch.handle, addr) + + if idx == len(self.low_level_il): + return None + + return self.low_level_il[idx] def get_low_level_il_exits_at(self, addr, arch=None): if arch is None: @@ -878,7 +884,13 @@ class Function(object): def get_lifted_il_at(self, addr, arch=None): if arch is None: arch = self.arch - return self.lifted_il[core.BNGetLiftedILForInstruction(self.handle, arch.handle, addr)] + + idx = core.BNGetLiftedILForInstruction(self.handle, arch.handle, addr) + + if idx == len(self.lifted_il): + return None + + return self.lifted_il[idx] def get_lifted_il_flag_uses_for_definition(self, i, flag): flag = self.arch.get_flag_index(flag) -- cgit v1.3.1 From 0ca722550555a1a0fc82766686bade6d669af3dc Mon Sep 17 00:00:00 2001 From: Cory Duplantis Date: Sun, 29 Oct 2017 13:23:33 -0500 Subject: Add convenience API's for accessing blocks and instructions in Function (#792) * Update export-svg.py path splitting Changed the path splitting part to the way python's documentation recommends it. I got some errors trying to use that function on windows, because it didn't split correctly. I stumbled upon this in the python documentation: https://docs.python.org/2/library/os.html#os.sep I changed the relevant line using their recommendation. * Rename data to session_data, as this API is per-session and not stored in the db * get_basic_blocks_starting_at incorrectly in the docs * Update troubleshooting.md * Zoom * get_instruction_low_level_il tweak made api docs less ambiguous * documenting Type.int and Type.function * Update __init__.py * Add convenience API's for accessing blocks and instructions in Function --- python/function.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'python/function.py') diff --git a/python/function.py b/python/function.py index 9aafdf52..cbbdf9bd 100644 --- a/python/function.py +++ b/python/function.py @@ -666,6 +666,41 @@ class Function(object): """Sets a comment for the current function""" return core.BNSetFunctionComment(self.handle, comment) + @property + def llil_basic_blocks(self): + """A generator of all LowLevelILBasicBlock objects in the current function""" + for block in self.low_level_il: + yield block + + @property + def mlil_basic_blocks(self): + """A generator of all MediumLevelILBasicBlock objects in the current function""" + for block in self.medium_level_il: + yield block + + @property + def instructions(self): + """A generator of instruction tokens and their start addresses for the current function""" + for block in self.basic_blocks: + start = block.start + for i in block: + yield (i[0], start) + start += i[1] + + @property + def llil_instructions(self): + """A generator of llil instructions of the current function""" + for block in self.llil_basic_blocks: + for i in block: + yield i + + @property + def mlil_instructions(self): + """A generator of mlil instructions of the current function""" + for block in self.mlil_basic_blocks: + for i in block: + yield i + def __iter__(self): count = ctypes.c_ulonglong() blocks = core.BNGetFunctionBasicBlockList(self.handle, count) -- cgit v1.3.1 From 704ceff1223485e9dc12947a36b0d56a3ff9d3ae Mon Sep 17 00:00:00 2001 From: David Barksdale Date: Thu, 2 Nov 2017 11:38:11 -0500 Subject: Move the function discrimination from SSAVariable to Variable (#856) --- python/function.py | 4 ++-- python/mediumlevelil.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'python/function.py') diff --git a/python/function.py b/python/function.py index cbbdf9bd..f8aaae44 100644 --- a/python/function.py +++ b/python/function.py @@ -241,10 +241,10 @@ class Variable(object): return self.name def __eq__(self, other): - return self.identifier == other.identifier + return (self.identifier, self.function) == (other.identifier, other.function) def __hash__(self): - return hash(self.identifier) + return hash((self.identifier, self.function)) class ConstantReference(object): diff --git a/python/mediumlevelil.py b/python/mediumlevelil.py index 333a6c4d..6f5515bb 100644 --- a/python/mediumlevelil.py +++ b/python/mediumlevelil.py @@ -39,12 +39,12 @@ class SSAVariable(object): def __eq__(self, other): return ( - (self.var.identifier, self.var.function, self.version) == - (other.var.identifier, other.var.function, other.version) + (self.var, self.version) == + (other.var, other.version) ) def __hash__(self): - return hash((self.var.identifier, self.var.function, self.version)) + return hash((self.var, self.version)) class MediumLevelILLabel(object): -- cgit v1.3.1