From d50d190f297afcb9621b39eb1d351e9eae96271b Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Thu, 20 Apr 2017 23:25:31 -0400 Subject: Renaming and adding variable identifiers --- python/function.py | 65 +++++++++++++++++++++++++++++-------------------- python/mediumlevelil.py | 38 ++++++++++++++--------------- 2 files changed, 57 insertions(+), 46 deletions(-) (limited to 'python') diff --git a/python/function.py b/python/function.py index 74f4b0f8..bca67a44 100644 --- a/python/function.py +++ b/python/function.py @@ -146,25 +146,6 @@ class PossibleValueSet(object): return "" -class VariableNameAndType(object): - def __init__(self, var, name, t): - self.var = var - self.name = name - self.type = t - - def __repr__(self): - if self.var.type == VariableSourceType.StackVariableSourceType: - return "" % (self.var.identifier, self.type, self.name) - elif self.var.type == VariableSourceType.RegisterVariableSourceType: - return "" % (self.var.function.arch.get_reg_name(self.var.identifier), self.type, self.name) - elif self.var.type == VariableSourceType.FlagVariableSourceType: - return "" % (self.var.function.arch.get_flag_name(self.var.identifier), self.type, self.name) - return "" % (self.var, self.type, self.name) - - def __str__(self): - return self.name - - class StackVariableReference(object): def __init__(self, src_operand, t, name, start_ofs, ref_ofs): self.source_operand = src_operand @@ -186,11 +167,40 @@ class StackVariableReference(object): class Variable(object): - def __init__(self, func, var_type, index, identifier): + def __init__(self, func, source_type, index, storage, name = None, var_type = None): self.function = func - self.type = var_type + self.source_type = source_type self.index = index - self.identifier = identifier + self.storage = storage + + var = core.BNVariable() + var.type = source_type + var.index = index + var.storage = storage + self.identifier = core.BNToVariableIdentifier(var) + + if name is None: + name = core.BNGetVariableName(func.handle, var) + if var_type is None: + var_type = core.BNGetVariableType(func.handle, var) + if var_type: + var_type = types.Type(var_type) + + self.name = name + self.type = var_type + + @classmethod + def from_identifier(self, func, identifier): + var = core.BNFromVariableIdentifier(identifier) + return Variable(func, VariableSourceType(var.type), var.index, var.storage) + + def __repr__(self): + if self.type is None: + return "" % self.name + return "" % (self.type.get_string_before_name(), self.name, self.type.get_string_after_name()) + + def __str__(self): + return self.name class ConstantReference(object): @@ -374,9 +384,9 @@ class Function(object): v = core.BNGetStackLayout(self.handle, count) result = [] for i in xrange(0, count.value): - var = Variable(self, v[i].var.type, v[i].var.index, v[i].var.identifier) - result.append(VariableNameAndType(var, v[i].name, types.Type(handle = core.BNNewTypeReference(v[i].type)))) - result.sort(key = lambda x: x.var.identifier) + result.append(Variable(self, v[i].var.type, v[i].var.index, v[i].var.storage, v[i].name, + types.Type(handle = core.BNNewTypeReference(v[i].type)))) + result.sort(key = lambda x: x.identifier) core.BNFreeVariableList(v, count.value) return result @@ -387,8 +397,9 @@ class Function(object): v = core.BNGetFunctionVariables(self.handle, count) result = [] for i in xrange(0, count.value): - var = Variable(self, v[i].var.type, v[i].var.index, v[i].var.identifier) - result.append(VariableNameAndType(var, v[i].name, types.Type(handle = core.BNNewTypeReference(v[i].type)))) + result.append(Variable(self, v[i].var.type, v[i].var.index, v[i].var.storage, v[i].name, + types.Type(handle = core.BNNewTypeReference(v[i].type)))) + result.sort(key = lambda x: x.identifier) core.BNFreeVariableList(v, count.value) return result diff --git a/python/mediumlevelil.py b/python/mediumlevelil.py index f827819f..e959e480 100644 --- a/python/mediumlevelil.py +++ b/python/mediumlevelil.py @@ -159,9 +159,9 @@ class MediumLevelILInstruction(object): elif operand_type == "var": var_type = VariableSourceType(instr.operands[i] >> 32) index = instr.operands[i] & 0xffffffff - identifier = instr.operands[i + 1] + storage = instr.operands[i + 1] i += 1 - value = function.Variable(self.function, var_type, index, identifier) + value = function.Variable(self.function.source_function, var_type, index, storage) elif operand_type == "int_list": count = ctypes.c_ulonglong() operand_list = core.BNMediumLevelILGetOperandList(func.handle, self.expr_index, i, count) @@ -177,8 +177,8 @@ class MediumLevelILInstruction(object): for j in xrange(count.value / 2): var_type = VariableSourceType(operand_list[j * 2] >> 32) index = operand_list[j * 2] & 0xffffffff - identifier = operand_list[(j * 2) + 1] - value.append(function.Variable(self.function, var_type, index, identifier)) + storage = operand_list[(j * 2) + 1] + value.append(function.Variable(self.function.source_function, var_type, index, storage)) core.BNMediumLevelILFreeOperandList(operand_list) elif operand_type == "var_ssa_list": count = ctypes.c_ulonglong() @@ -188,9 +188,9 @@ class MediumLevelILInstruction(object): for j in xrange(count.value / 3): var_type = VariableSourceType(operand_list[j * 3] >> 32) index = operand_list[j * 3] & 0xffffffff - identifier = operand_list[(j * 3) + 1] + storage = operand_list[(j * 3) + 1] var_index = operand_list[(j * 3) + 2] - value.append((function.Variable(self.function, var_type, index, identifier), var_index)) + value.append((function.Variable(self.function.source_function, var_type, index, storage), var_index)) core.BNMediumLevelILFreeOperandList(operand_list) elif operand_type == "expr_list": count = ctypes.c_ulonglong() @@ -296,35 +296,35 @@ class MediumLevelILInstruction(object): def get_ssa_var_possible_values(self, var, index): var_data = core.BNVariable() - var_data.type = var.type + var_data.type = var.source_type var_data.index = var.index - var_data.identifier = var.identifier + var_data.storage = var.storage value = core.BNGetMediumLevelILPossibleSSAVarValues(self.function.handle, var_data, index, self.instr_index) result = function.RegisterValue(self.function.arch, value) return result def get_ssa_var_index(self, var): var_data = core.BNVariable() - var_data.type = var.type + var_data.type = var.source_type var_data.index = var.index - var_data.identifier = var.identifier + var_data.storage = var.storage return core.BNGetMediumLevelILSSAVarIndexAtILInstruction(self.function.handle, var_data, self.instr_index) def get_var_for_reg(self, reg): if isinstance(reg, str): reg = self.function.arch.regs[reg].index result = core.BNGetMediumLevelILVariableForRegisterAtInstruction(self.function.handle, reg, self.instr_index) - return function.Variable(self.function.source_function, result.type, result.index, result.identifier) + return function.Variable(self.function.source_function, result.type, result.index, result.storage) def get_var_for_flag(self, flag): if isinstance(flag, str): flag = self.function.arch.regs[flag].index result = core.BNGetMediumLevelILVariableForFlagAtInstruction(self.function.handle, flag, self.instr_index) - return function.Variable(self.function.source_function, result.type, result.index, result.identifier) + return function.Variable(self.function.source_function, result.type, result.index, result.storage) def get_var_for_stack_location(self, offset): result = core.BNGetMediumLevelILVariableForStackLocationAtInstruction(self.function.handle, offset, self.instr_index) - return function.Variable(self.function.source_function, result.type, result.index, result.identifier) + return function.Variable(self.function.source_function, result.type, result.index, result.storage) def get_reg_value(self, reg): if isinstance(reg, str): @@ -655,9 +655,9 @@ class MediumLevelILFunction(object): def get_ssa_var_definition(self, var, index): var_data = core.BNVariable() - var_data.type = var.type + var_data.type = var.source_type var_data.index = var.index - var_data.identifier = var.identifier + var_data.storage = var.storage result = core.BNGetMediumLevelILSSAVarDefinition(self.handle, var_data, index) if result >= core.BNGetMediumLevelILInstructionCount(self.handle): return None @@ -672,9 +672,9 @@ class MediumLevelILFunction(object): def get_ssa_var_uses(self, var, index): count = ctypes.c_ulonglong() var_data = core.BNVariable() - var_data.type = var.type + var_data.type = var.source_type var_data.index = var.index - var_data.identifier = var.identifier + var_data.storage = var.storage instrs = core.BNGetMediumLevelILSSAVarUses(self.handle, var_data, index, count) result = [] for i in xrange(0, count.value): @@ -693,9 +693,9 @@ class MediumLevelILFunction(object): def get_ssa_var_value(self, var, index): var_data = core.BNVariable() - var_data.type = var.type + var_data.type = var.source_type var_data.index = var.index - var_data.identifier = var.identifier + var_data.storage = var.storage value = core.BNGetMediumLevelILSSAVarValue(self.handle, var_data, index) result = function.RegisterValue(self.arch, value) return result -- cgit v1.3.1