Skip to content

Commit

Permalink
issue #27 : [WIP] Move _get_asm_address to helper
Browse files Browse the repository at this point in the history
  • Loading branch information
igogo-x86 committed Jul 10, 2019
1 parent bfb0e78 commit 0849ab3
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 25 deletions.
2 changes: 0 additions & 2 deletions HexRaysPyTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import HexRaysPyTools.core.cache as cache
import HexRaysPyTools.core.const as const
import HexRaysPyTools.core.helper as helper
import HexRaysPyTools.settings as settings
from HexRaysPyTools.callbacks import hx_callback_manager, action_manager
from HexRaysPyTools.core.struct_xrefs import XrefStorage
Expand Down Expand Up @@ -48,5 +47,4 @@ def PLUGIN_ENTRY():
logging.basicConfig(format='[%(levelname)s] %(message)s\t(%(module)s:%(funcName)s)')
logging.root.setLevel(settings.DEBUG_MESSAGE_LEVEL)
idaapi.notify_when(idaapi.NW_OPENIDB, cache.initialize_cache)
helper.extend_ida()
return MyPlugin()
11 changes: 7 additions & 4 deletions HexRaysPyTools/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ def _manipulate(self, cexpr, obj):
self.__manipulate(cexpr, obj)

def __manipulate(self, cexpr, obj):
logger.debug("Expression {} at {} Id - {}".format(cexpr.opname, to_hex(self._find_asm_address(cexpr)), obj.id))
logger.debug("Expression {} at {} Id - {}".format(
cexpr.opname,
to_hex(helper.find_asm_address(cexpr, self.parents)),
obj.id))


class ObjectDownwardsVisitor(ObjectVisitor):
Expand Down Expand Up @@ -272,7 +275,7 @@ def visit_expr(self, cexpr):
if obj.is_target(x_cexpr):
if self.__is_object_overwritten(x_cexpr, obj, y_cexpr):
logger.info("Removed object {} from scanning at {}".format(
obj, to_hex(self._find_asm_address(x_cexpr))))
obj, to_hex(helper.find_asm_address(x_cexpr, self.parents))))
self._objects.remove(obj)
return 0
elif obj.is_target(y_cexpr):
Expand All @@ -297,7 +300,7 @@ def _is_initial_object(self, cexpr):
cexpr = cexpr.y
if cexpr.op == idaapi.cot_cast:
cexpr = cexpr.x
return self._init_obj.is_target(cexpr) and self._find_asm_address(cexpr) == self._start_ea
return self._init_obj.is_target(cexpr) and helper.find_asm_address(cexpr, self.parents) == self._start_ea

def __is_object_overwritten(self, x_cexpr, obj, y_cexpr):
if len(self._objects) < 2:
Expand Down Expand Up @@ -379,7 +382,7 @@ def process(self):
super(ObjectUpwardsVisitor, self).process()

def _is_initial_object(self, cexpr):
return self._init_obj.is_target(cexpr) and self._find_asm_address(cexpr) == self._start_ea
return self._init_obj.is_target(cexpr) and helper.find_asm_address(cexpr, self.parents) == self._start_ea

def __add_object_assignment(self, from_obj, to_obj):
if from_obj in self._tree:
Expand Down
4 changes: 2 additions & 2 deletions HexRaysPyTools/callbacks/guess_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def _manipulate(self, cexpr, obj):
if alloc_obj:
self._data.append([alloc_obj.ea, obj.name, self._get_line(), "HEAP"])
elif self.parent_expr().op == idaapi.cot_ref:
self._data.append([self._find_asm_address(cexpr), obj.name, self._get_line(), "STACK"])
self._data.append([helper.find_asm_address(cexpr, self.parents), obj.name, self._get_line(), "STACK"])
elif obj.id == api.SO_GLOBAL_OBJECT:
self._data.append([self._find_asm_address(cexpr), obj.name, self._get_line(), "GLOBAL"])
self._data.append([helper.find_asm_address(cexpr, self.parents), obj.name, self._get_line(), "GLOBAL"])

def _finish(self):
chooser = _StructAllocChoose(self._data)
Expand Down
5 changes: 3 additions & 2 deletions HexRaysPyTools/callbacks/renames.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ def visit_expr(self, expr):
if expr.op == idaapi.cot_call and expr.x.op == idaapi.cot_obj and expr.x.obj_ea == self.__func_addr:
arg_expr = expr.a[self.__arg_idx]
if arg_expr.op != idaapi.cot_obj:
logger.error("Argument is a not string at {}".format(helper.to_hex(self._find_asm_address(expr))))
cexpr_ea = helper.find_asm_address(expr, self.parents)
logger.error("Argument is a not string at {}".format(helper.to_hex(cexpr_ea)))
return 1
self.__add_func_name(arg_expr)
return 0
Expand All @@ -193,7 +194,7 @@ def __add_func_name(self, arg_expr):
new_name = idc.get_strlit_contents(arg_expr.obj_ea)
if not idaapi.is_valid_typename(new_name):
logger.warn("Argument has a weird name `{}` at {}".format(
new_name, helper.to_hex(self._find_asm_address(arg_expr))))
new_name, helper.to_hex(helper.find_asm_address(arg_expr, self.parents))))
return

self.__possible_names.add(new_name)
Expand Down
13 changes: 2 additions & 11 deletions HexRaysPyTools/core/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,19 +373,14 @@ def decompile_function(address):
logger.warn("IDA failed to decompile function at 0x{address:08X}".format(address=address))


# ======================================================================
# Functions that extends IDA Pro capabilities
# ======================================================================


def _find_asm_address(self, cexpr):
def find_asm_address(cexpr, parents):
""" Returns most close virtual address corresponding to cexpr """

ea = cexpr.ea
if ea != idaapi.BADADDR:
return ea

for p in reversed(self.parents):
for p in reversed(parents):
if p.ea != idaapi.BADADDR:
return p.ea

Expand Down Expand Up @@ -414,7 +409,3 @@ def my_cexpr_t(*args, **kwargs):
if 'z' in kwargs:
cexpr._set_z(kwargs['z'])
return cexpr


def extend_ida():
idaapi.ctree_parentee_t._find_asm_address = _find_asm_address
9 changes: 5 additions & 4 deletions HexRaysPyTools/core/variable_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def _manipulate(self, cexpr, obj):
super(SearchVisitor, self)._manipulate(cexpr, obj)

if obj.tinfo and not helper.is_legal_type(obj.tinfo):
logger.warn("Variable obj.name has weird type at {}".format(helper.to_hex(self._find_asm_address(cexpr))))
cexpr_ea = helper.find_asm_address(cexpr, self.parents)
logger.warn("Variable obj.name has weird type at {}".format(helper.to_hex(cexpr_ea)))
return
if cexpr.type.is_ptr():
member = self.__extract_member_from_pointer(cexpr, obj)
Expand All @@ -136,13 +137,13 @@ def _manipulate(self, cexpr, obj):
self.__temporary_structure.add_row(member)

def _get_member(self, offset, cexpr, obj, tinfo=None, obj_ea=None):
cexpr_ea = helper.find_asm_address(cexpr, self.parents)
if offset < 0:
logger.error("Considered to be imposible: offset - {}, obj - {}".format(
offset, helper.to_hex(self._find_asm_address(cexpr))))
logger.error("Considered to be impossible: offset - {}, obj - {}".format(
offset, helper.to_hex(cexpr_ea)))
raise AssertionError

applicable = not self.crippled
cexpr_ea = self._find_asm_address(cexpr)
scan_obj = ScannedObject.create(obj, cexpr_ea, self.__origin, applicable)
if obj_ea:
if temporary_structure.VirtualTable.check_address(obj_ea):
Expand Down

0 comments on commit 0849ab3

Please sign in to comment.