def visit_cast_exp(self, exp, *args, **kwargs): operand, op_name = get_inner_node(exp) if type(operand) in (Reg, IndirectOffset): feature = 'UNOP[{}][{}]'.format(op_name, '{}') add_unary_feature(feature, operand) self.visit(operand) else: self.visit(exp.e)
def visit_binop_exp(self, exp, *args, **kwargs): a, a_op = get_inner_node(exp.e1) b, b_op = get_inner_node(exp.e2) if type(a) in (Reg, IndirectOffset) \ or type(b) in (Reg, IndirectOffset): feature = 'UNOP[{}][{}]'.format(a_op, '{}') add_unary_feature(feature, a) feature = 'UNOP[{}][{}]'.format(b_op, '{}') add_unary_feature(feature, b) feature = '{}[{}][{}]'.format(exp.op, '{}', '{}') add_binary_feature(feature, a, b) self.visit(a) self.visit(b) else: self.visit(exp.e1) self.visit(exp.e2)
def visit_jmp(self, stmt, *args, **kwargs): function = kwargs['function'] binary = function.binary if stmt.cond is not None: cond, cond_op = get_inner_node(stmt.cond) if type(cond) in (Reg, IndirectOffset): feature = 'UNOP[{}][{}]'.format(cond_op, '{}') add_unary_feature(feature, cond) elif isinstance(cond, Flag): key = (cond.base_flag, cond.index) if key in function.flag_defs: flag_def, flag_op = get_inner_node(function.flag_defs[key]) if type(flag_def) in (Reg, IndirectOffset): feature = 'COND[{}{}{}][{}]'.format( cond_op, flag_op, cond.base_flag, '{}') add_unary_feature(feature, flag_def) elif isinstance(flag_def, BinOpExp): e1, e1_op = get_inner_node(flag_def.e1) e2, e2_op = get_inner_node(flag_def.e2) if type(e1) in NODES \ and type(e2) in NODES \ and (type(e1) in (Reg, IndirectOffset) or type(e2) in (Reg, IndirectOffset)): feature = 'COND_{}[{}{}{}][{}{}][{}{}]'.format( flag_def.op, cond_op, flag_op, cond.base_flag, e1_op, '{}', e2_op, '{}') add_binary_feature(feature, e1, e2) elif isinstance(e1, BinOpExp) and isinstance( e2, IntConst): if e2.value == 0: a, a_op = get_inner_node(e1.e1) b, b_op = get_inner_node(e1.e2) if type(a) in NODES \ and type(b) in NODES \ and (type(a) in (Reg, IndirectOffset) or type(b) in (Reg, IndirectOffset)): feature = 'COND_{}[{}{}{}][{}{}][{}{}]'.format( e1.op, cond_op, flag_op, cond.base_flag, a_op, '{}', b_op, '{}') add_binary_feature(feature, e1, e2) else: pass else: pass elif isinstance(flag_def, IntConst): pass else: pass elif isinstance(cond, BinOpExp): e1, e1_op = get_inner_node(cond.e1) e2, e2_op = get_inner_node(cond.e2) if type(e1) in NODES \ and type(e2) in NODES \ and (type(e1) in (Reg, IndirectOffset) or type(e2) in (Reg, IndirectOffset)): feature = 'COND_{}[{}{}][{}{}]'.format( cond.op, e1_op, '{}', e2_op, '{}') add_binary_feature(feature, e1, e2) elif isinstance(e1, BinOpExp) \ and isinstance(e2, IntConst): a, a_op = get_inner_node(e1.e1) b, b_op = get_inner_node(e1.e2) if e2.value == 0: if type(a) in NODES \ and type(b) in NODES \ and (type(a) in (Reg, IndirectOffset) or type(b) in (Reg, IndirectOffset)): feature = 'COND_{}[{}{}][{}{}][{}{}]'.format( e1.op, cond.op, e1_op, a_op, '{}', b_op, '{}') add_binary_feature(feature, a, b) elif type(a) in (Reg, IndirectOffset) \ and isinstance(b, IntConst): feature = 'COND_{}[{}{}][{}_{}][{}{}][{}{}]'.format( e1.op, cond_op, e1_op, e2.width, e2.value, a_op, '{}', b_op, '{}') add_binary_feature(feature, a, b) else: pass else: pass else: pass if isinstance(stmt.kind, CallKind) \ and stmt.kind.target is not None \ and isinstance(stmt.kind.target, Function): function = stmt.kind.target for key in stmt.kind.args: arg = stmt.kind.args[key] func_name = function.name if function.is_name_given else 'FUNC' f2 = key if binary.config.MACHINE_ARCH == 'x86': f2 = '{}+{}'.format(key[0], key[1]) elif binary.config.MACHINE_ARCH == 'x64': f2 = key elif binary.config.MACHINE_ARCH == 'ARM': f2 = key feature = 'ARG[{}][{}][{}]'.format(func_name, f2, '{}') add_unary_feature(feature, arg)