def write_method(self): acc = [] access = self.method.access self.constructor = 0x10000 in access for i in self.method.access: if i == 0x10000: continue acc.append(ACCESS_FLAGS_METHODS.get(i)) if self.constructor: name = get_type(self.method.cls_name).split('.')[-1] proto = '%s %s(' % (' '.join(acc), name) else: name = self.method.name proto = '%s %s %s(' % (' '.join(acc), self.method.type, name) self.write('%s%s' % (self.space(), proto)) if 0x8 in self.method.access: params = self.method.lparams else: params = self.method.lparams[1:] proto = '' if self.method.params_type: proto = ', '.join(['%s p%s' % (get_type(p_type), param) for p_type, param in zip(self.method.params_type, params)]) self.write('%s)' % proto) if self.graph is None: return self.write(';') self.write('\n%s{\n' % self.space()) self.inc_ind() # for v, var in self.method.var_to_name.iteritems(): # var.visit_decl(self) self.visit_node(self.graph.get_entry()) self.dec_ind() self.write('%s}\n' % self.space())
def visit_move_exception(self, var, data=None): var.declared = True var_type = var.get_type() or 'unknownType' self.write('%s v%s' % (get_type(var_type), var.name)) self.write_ext(('EXCEPTION_TYPE', '%s' % get_type(var_type), data.type)) self.write_ext(('SPACE', ' ')) self.write_ext(('NAME_CLASS_EXCEPTION', 'v%s' % var.value(), data.type, data))
def write_method(self): acc = [] access = self.method.access self.constructor = False for modifier in access: if modifier == 'constructor': self.constructor = True continue acc.append(modifier) self.write('\n%s' % self.space()) if acc: self.write('%s ' % ' '.join(acc)) if self.constructor: self.write(get_type(self.method.cls_name).split('.')[-1]) else: self.write('%s %s' % (get_type(self.method.type), self.method.name)) params = self.method.lparams if 'static' not in access: params = params[1:] proto = '' if self.method.params_type: proto = ', '.join(['%s p%s' % (get_type(p_type), param) for p_type, param in zip(self.method.params_type, params)]) self.write('(%s)' % proto) if self.graph is None: return self.write(';\n') self.write('\n%s{\n' % self.space()) self.inc_ind() self.visit_node(self.graph.entry) self.dec_ind() self.write('%s}\n' % self.space())
def write_method(self): acc = [] access = self.method.access self.constructor = "constructor" in access for modifier in self.method.access: if modifier == "constructor": continue acc.append(modifier) if self.constructor: name = get_type(self.method.cls_name).split(".")[-1] proto = "%s %s(" % (" ".join(acc), name) else: name = self.method.name proto = "%s %s %s(" % (" ".join(acc), self.method.type, name) self.write("%s%s" % (self.space(), proto)) params = self.method.lparams if "static" not in self.method.access: params = params[1:] proto = "" if self.method.params_type: proto = ", ".join( ["%s p%s" % (get_type(p_type), param) for p_type, param in zip(self.method.params_type, params)] ) self.write("%s)" % proto) if self.graph is None: return self.write(";") self.write("\n%s{\n" % self.space()) self.inc_ind() # for v, var in self.method.var_to_name.iteritems(): # var.visit_decl(self) self.visit_node(self.graph.entry) self.dec_ind() self.write("%s}\n" % self.space())
def write_method(self): acc = [] access = self.method.access self.constructor = 'constructor' in access for modifier in self.method.access: if modifier == 'constructor': continue acc.append(modifier) if self.constructor: name = get_type(self.method.cls_name).split('.')[-1] proto = '%s %s(' % (' '.join(acc), name) else: name = self.method.name proto = '%s %s %s(' % (' '.join(acc), self.method.type, name) self.write('%s%s' % (self.space(), proto)) params = self.method.lparams if 'static' not in self.method.access: params = params[1:] proto = '' if self.method.params_type: proto = ', '.join(['%s p%s' % (get_type(p_type), param) for p_type, param in zip(self.method.params_type, params)]) self.write('%s)' % proto) if self.graph is None: return self.write(';') self.write('\n%s{\n' % self.space()) self.inc_ind() # for v, var in self.method.var_to_name.iteritems(): # var.visit_decl(self) self.visit_node(self.graph.get_entry()) self.dec_ind() self.write('%s}\n' % self.space())
def visit_move_exception(self, var, data=None): var.declared = True var_type = var.get_type() or "unknownType" self.write("%s v%s" % (get_type(var_type), var.name)) self.write_ext(("EXCEPTION_TYPE", "%s" % get_type(var_type), data.type)) self.write_ext(("SPACE", " ")) self.write_ext(("NAME_CLASS_EXCEPTION", "v%s" % var.value(), data.type, data))
def write_method(self): acc = [] access = self.method.access self.constructor = False for modifier in access: if modifier == 'constructor': self.constructor = True continue acc.append(modifier) if self.constructor: name = get_type(self.method.cls_name).split('.')[-1] proto = '%s %s' % (' '.join(acc), name) else: name = self.method.name proto = '%s %s %s' % (' '.join(acc), get_type( self.method.type), name) self.write('\n%s%s' % (self.space(), proto)) params = self.method.lparams if 'static' not in access: params = params[1:] proto = '' if self.method.params_type: proto = ', '.join([ '%s p%s' % (get_type(p_type), param) for p_type, param in zip(self.method.params_type, params) ]) self.write('(%s)' % proto) if self.graph is None: return self.write(';\n') self.write('\n%s{\n' % self.space()) self.inc_ind() self.visit_node(self.graph.entry) self.dec_ind() self.write('%s}\n' % self.space())
def write_method(self): acc = [] access = self.method.access self.constructor = False for modifier in access: if modifier == 'constructor': self.constructor = True continue acc.append(modifier) self.write('\n%s' % self.space()) self.write_ext(('NEWLINE', '\n%s' % (self.space()))) if acc: self.write('%s ' % ' '.join(acc)) self.write_ext(('PROTOTYPE_ACCESS', '%s ' % ' '.join(acc))) if self.constructor: name = get_type(self.method.cls_name).split('.')[-1] self.write(name) self.write_ext(('NAME_METHOD_PROTOTYPE', '%s' % name, self.method)) else: self.write('%s %s' % (get_type(self.method.type), self.method.name)) self.write_ext( ('PROTOTYPE_TYPE', '%s' % get_type(self.method.type))) self.write_ext(('SPACE', ' ')) self.write_ext(('NAME_METHOD_PROTOTYPE', '%s' % self.method.name, self.method)) params = self.method.lparams if 'static' not in access: params = params[1:] proto = '' self.write_ext(('PARENTHESIS_START', '(')) if self.method.params_type: proto = ', '.join([ '%s p%s' % (get_type(p_type), param) for p_type, param in zip(self.method.params_type, params) ]) first = True for p_type, param in zip(self.method.params_type, params): if not first: self.write_ext(('COMMA', ', ')) else: first = False self.write_ext(('ARG_TYPE', '%s' % get_type(p_type))) self.write_ext(('SPACE', ' ')) self.write_ext( ('NAME_ARG', 'p%s' % param, p_type, self.method)) self.write_ext(('PARENTHESIS_END', ')')) self.write('(%s)' % proto) if self.graph is None: self.write(';\n') self.write_ext(('METHOD_END_NO_CONTENT', ';\n')) return self.write('\n%s{\n' % self.space()) self.write_ext(('METHOD_START', '\n%s{\n' % self.space())) self.inc_ind() self.visit_node(self.graph.entry) self.dec_ind() self.write('%s}\n' % self.space()) self.write_ext(('METHOD_END', '%s}\n' % self.space()))
def visit_move_exception(self, var, data=None): var.declared = True var_type = var.get_type() or 'unknownType' self.write('%s v%s' % (get_type(var_type), var.name)) self.write_ext(('EXCEPTION_TYPE', '%s' % get_type(var_type), data.type)) self.write_ext(('SPACE', ' ')) self.write_ext( ('NAME_CLASS_EXCEPTION', 'v%s' % var.value(), data.type, data))
def write_method(self): acc = [] access = self.method.access self.constructor = False for modifier in access: if modifier == 'constructor': self.constructor = True continue acc.append(modifier) self.write('\n%s' % self.space()) self.write_ext(('NEWLINE', '\n%s' % (self.space()))) if acc: self.write('%s ' % ' '.join(acc)) self.write_ext(('PROTOTYPE_ACCESS', '%s ' % ' '.join(acc))) if self.constructor: name = get_type(self.method.cls_name).split('.')[-1] self.write(name) self.write_ext(('NAME_METHOD_PROTOTYPE', '%s' % name, self.method)) else: self.write( '%s %s' % (get_type(self.method.type), self.method.name)) self.write_ext( ('PROTOTYPE_TYPE', '%s' % get_type(self.method.type))) self.write_ext(('SPACE', ' ')) self.write_ext( ('NAME_METHOD_PROTOTYPE', '%s' % self.method.name, self.method)) params = self.method.lparams if 'static' not in access: params = params[1:] proto = '' self.write_ext(('PARENTHESIS_START', '(')) if self.method.params_type: proto = ', '.join(['%s p%s' % (get_type(p_type), param) for p_type, param in zip(self.method.params_type, params)]) first = True for p_type, param in zip(self.method.params_type, params): if not first: self.write_ext(('COMMA', ', ')) else: first = False self.write_ext(('ARG_TYPE', '%s' % get_type(p_type))) self.write_ext(('SPACE', ' ')) self.write_ext( ('NAME_ARG', 'p%s' % param, p_type, self.method)) self.write_ext(('PARENTHESIS_END', ')')) self.write('(%s)' % proto) if self.graph is None: self.write(';\n') self.write_ext(('METHOD_END_NO_CONTENT', ';\n')) return self.write('\n%s{\n' % self.space()) self.write_ext(('METHOD_START', '\n%s{\n' % self.space())) self.inc_ind() self.visit_node(self.graph.entry) self.dec_ind() self.write('%s}\n' % self.space()) self.write_ext(('METHOD_END', '%s}\n' % self.space()))
def visit_variable(self, var): var_type = var.get_type() or 'unknownType' if not var.declared: self.write('%s ' % get_type(var_type)) self.write_ext(('VARIABLE_TYPE', '%s' % get_type(var_type), var_type)) self.write_ext(('SPACE', ' ')) var.declared = True self.write('v%s' % var.name) self.write_ext(('NAME_VARIABLE', 'v%s' % var.name, var, var_type))
def visit_variable(self, var): var_type = var.get_type() or "unknownType" if not var.declared: self.write("%s " % get_type(var_type)) self.write_ext(("VARIABLE_TYPE", "%s" % get_type(var_type), var_type)) self.write_ext(("SPACE", " ")) var.declared = True self.write("v%s" % var.name) self.write_ext(("NAME_VARIABLE", "v%s" % var.name, var, var_type))
def visit_variable(self, var): var_type = var.get_type() or 'unknownType' if not var.declared: self.write('%s ' % get_type(var_type)) self.write_ext( ('VARIABLE_TYPE', '%s' % get_type(var_type), var_type)) self.write_ext(('SPACE', ' ')) var.declared = True self.write('v%s' % var.name) self.write_ext(('NAME_VARIABLE', 'v%s' % var.name, var, var_type))
def write_method(self): acc = collections.deque() access = self.method.access self.constructor = False for modifier in access: if modifier == "constructor": self.constructor = True continue acc.append(modifier) self.write("\n%s" % self.space()) self.write_ext(("NEWLINE", "\n%s" % (self.space()))) if acc: self.write("%s " % " ".join(acc)) self.write_ext(("PROTOTYPE_ACCESS", "%s " % " ".join(acc))) if self.constructor: name = get_type(self.method.cls_name).split(".")[-1] self.write(name) self.write_ext(("NAME_METHOD_PROTOTYPE", "%s" % name, self.method)) else: self.write("%s %s" % (get_type(self.method.type), self.method.name)) self.write_ext(("PROTOTYPE_TYPE", "%s" % get_type(self.method.type))) self.write_ext(("SPACE", " ")) self.write_ext(("NAME_METHOD_PROTOTYPE", "%s" % self.method.name, self.method)) params = self.method.lparams if "static" not in access: params = params[1:] proto = "" self.write_ext(("PARENTHESIS_START", "(")) if self.method.params_type: proto = ", ".join( ["%s p%s" % (get_type(p_type), param) for p_type, param in zip(self.method.params_type, params)] ) first = True for p_type, param in zip(self.method.params_type, params): if not first: self.write_ext(("COMMA", ", ")) else: first = False self.write_ext(("ARG_TYPE", "%s" % get_type(p_type))) self.write_ext(("SPACE", " ")) self.write_ext(("NAME_ARG", "p%s" % param, p_type, self.method)) self.write_ext(("PARENTHESIS_END", ")")) self.write("(%s)" % proto) if self.graph is None: self.write(";\n") self.write_ext(("METHOD_END_NO_CONTENT", ";\n")) return self.write("\n%s{\n" % self.space()) self.write_ext(("METHOD_START", "\n%s{\n" % self.space())) self.inc_ind() self.visit_node(self.graph.entry) self.dec_ind() self.write("%s}\n" % self.space()) self.write_ext(("METHOD_END", "%s}\n" % self.space()))
def visit_filled_new_array(self, atype, size, args): self.write('new %s {' % get_type(atype)) for idx, arg in enumerate(args): arg.visit(self) if idx + 1 < len(args): self.write(', ') self.write('})')
def __init__(self, cls_name, field_type, field_name): super().__init__() self.cls = util.get_type(cls_name) self.ftype = field_type self.name = field_name self.clsdesc = cls_name
def visit_filled_new_array(self, atype, size, args): self.write('new %s {' % get_type(atype), data="NEW_ARRAY_FILLED") for idx, arg in enumerate(args): arg.visit(self) if idx + 1 < len(args): self.write(', ', data="COMMA") self.write('})', data="NEW_ARRAY_FILLED_END")
def visit_decl(self, var): if not var.declared: var_type = var.get_type() or 'unknownType' self.write('%s%s v%s' % ( self.space(), get_type(var_type), var.name), data="DECLARATION") self.end_ins()
def show_source(self): if not self.inner and self.package: print 'package %s;\n' % self.package if self.superclass is not None: self.superclass = self.superclass[1:-1].replace('/', '.') if self.superclass.split('.')[-1] == 'Object': self.superclass = None if self.superclass is not None: self.prototype += ' extends %s' % self.superclass if self.interfaces is not None: interfaces = self.interfaces[1:-1].split(' ') self.prototype += ' implements %s' % ', '.join( [n[1:-1].replace('/', '.') for n in interfaces]) print '%s {\n' % self.prototype for field in self.fields.values(): access = [util.ACCESS_FLAGS_FIELDS.get(flag) for flag in util.ACCESS_FLAGS_FIELDS if flag & field.get_access_flags()] f_type = util.get_type(field.get_descriptor()) name = field.get_name() print ' %s %s %s;\n' % (' '.join(access), f_type, name) for klass in self.subclasses.values(): klass.show_source() for _, method in self.methods.iteritems(): if isinstance(method, DvMethod): method.show_source() print '}\n'
def visit_filled_new_array(self, atype, size, args): self.write("new %s {" % get_type(atype), data="NEW_ARRAY_FILLED") for idx, arg in enumerate(args): arg.visit(self) if idx + 1 < len(args): self.write(", ", data="COMMA") self.write("})", data="NEW_ARRAY_FILLED_END")
def __init__(self, cls_name, field_type, field_name): super(StaticExpression, self).__init__() self.cls = util.get_type(cls_name) self.ftype = field_type self.name = field_name self.clsdesc = cls_name
def get_source(self): source = [] if not self.inner and self.package: source.append('package %s;\n' % self.package) if self.superclass is not None: self.superclass = self.superclass[1:-1].replace('/', '.') if self.superclass.split('.')[-1] == 'Object': self.superclass = None if self.superclass is not None: self.prototype += ' extends %s' % self.superclass if self.interfaces is not None: interfaces = self.interfaces[1:-1].split(' ') self.prototype += ' implements %s' % ', '.join( [n[1:-1].replace('/', '.') for n in interfaces]) source.append('%s {\n' % self.prototype) for field in self.fields.values(): field_access_flags = field.get_access_flags() access = [util.ACCESS_FLAGS_FIELDS[flag] for flag in util.ACCESS_FLAGS_FIELDS if flag & field_access_flags] f_type = util.get_type(field.get_descriptor()) name = field.get_name() source.append(' %s %s %s;\n' % (' '.join(access), f_type, name)) for klass in self.subclasses.values(): source.append(klass.get_source()) for _, method in self.methods.iteritems(): if isinstance(method, DvMethod): source.append(method.get_source()) source.append('}\n') return ''.join(source)
def show_source(self): if not self.inner and self.package: print 'package %s;\n' % self.package if self.superclass is not None: self.superclass = self.superclass[1:-1].replace('/', '.') if self.superclass.split('.')[-1] == 'Object': self.superclass = None if self.superclass is not None: self.prototype += ' extends %s' % self.superclass if self.interfaces is not None: interfaces = self.interfaces[1:-1].split(' ') self.prototype += ' implements %s' % ', '.join( [n[1:-1].replace('/', '.') for n in interfaces]) print '%s {\n' % self.prototype for field in self.fields.values(): access = [ util.ACCESS_FLAGS_FIELDS.get(flag) for flag in util.ACCESS_FLAGS_FIELDS if flag & field.get_access_flags() ] f_type = util.get_type(field.get_descriptor()) name = field.get_name() print ' %s %s %s;\n' % (' '.join(access), f_type, name) for klass in self.subclasses.values(): klass.show_source() for _, method in self.methods.iteritems(): if isinstance(method, DvMethod): method.show_source() print '}\n'
def visit_decl(self, var): if not var.declared: var_type = var.get_type() or 'unknownType' self.write('%s%s v%s' % (self.space(), get_type(var_type), var.name), data="DECLARATION") self.end_ins()
def get_source_ext(self): source = [] if not self.inner and self.package: source.append( ( "PACKAGE", [("PACKAGE_START", "package "), ("NAME_PACKAGE", "%s" % self.package), ("PACKAGE_END", ";\n")], ) ) list_proto = [] list_proto.append(("PROTOTYPE_ACCESS", "%s class " % " ".join(self.access))) list_proto.append(("NAME_PROTOTYPE", "%s" % self.name, self.package)) superclass = self.superclass if superclass is not None and superclass != "Ljava/lang/Object;": superclass = superclass[1:-1].replace("/", ".") list_proto.append(("EXTEND", " extends ")) list_proto.append(("NAME_SUPERCLASS", "%s" % superclass)) if len(self.interfaces) > 0: list_proto.append(("IMPLEMENTS", " implements ")) for i, interface in enumerate(self.interfaces): if i != 0: list_proto.append(("COMMA", ", ")) list_proto.append(("NAME_INTERFACE", interface[1:-1].replace("/", "."))) list_proto.append(("PROTOTYPE_END", " {\n")) source.append(("PROTOTYPE", list_proto)) for field in self.fields: field_access_flags = field.get_access_flags() access = [util.ACCESS_FLAGS_FIELDS[flag] for flag in util.ACCESS_FLAGS_FIELDS if flag & field_access_flags] f_type = util.get_type(field.get_descriptor()) name = field.get_name() if access: access_str = " %s " % " ".join(access) else: access_str = " " source.append( ( "FIELD", [ ("FIELD_ACCESS", access_str), ("FIELD_TYPE", "%s" % f_type), ("SPACE", " "), ("NAME_FIELD", "%s" % name, f_type, field), ("FIELD_END", ";\n"), ], ) ) # TODO: call get_source_ext for each subclass? for klass in self.subclasses.values(): source.append((klass, klass.get_source())) for method in self.methods: if isinstance(method, DvMethod): source.append(("METHOD", method.get_source_ext())) source.append(("CLASS_END", [("CLASS_END", "}\n")])) return source
def __init__(self, arg, klass, ftype, name): super(InstanceExpression, self).__init__() self.arg = arg.v self.cls = util.get_type(klass) self.ftype = ftype self.name = name self.var_map[arg.v] = arg self.clsdesc = klass
def __init__(self, rhs, klass, ftype, name): super(StaticInstruction, self).__init__() self.rhs = rhs.v self.cls = util.get_type(klass) self.ftype = ftype self.name = name self.var_map[rhs.v] = rhs self.clsdesc = klass
def __init__(self, rhs, klass, ftype, name): super().__init__() self.rhs = rhs.v self.cls = util.get_type(klass) self.ftype = ftype self.name = name self.var_map[rhs.v] = rhs self.clsdesc = klass
def __init__(self, arg, klass, ftype, name): super().__init__() self.arg = arg.v self.cls = util.get_type(klass) self.ftype = ftype self.name = name self.var_map[arg.v] = arg self.clsdesc = klass
def __init__(self, rhs, lhs, klass, atype, name): super().__init__() self.lhs = lhs.v self.rhs = rhs.v self.atype = atype self.cls = util.get_type(klass) self.name = name self.var_map.update([(lhs.v, lhs), (rhs.v, rhs)]) self.clsdesc = klass
def __init__(self, rhs, lhs, klass, atype, name): super(InstanceInstruction, self).__init__() self.lhs = lhs.v self.rhs = rhs.v self.atype = atype self.cls = util.get_type(klass) self.name = name self.var_map.update([(lhs.v, lhs), (rhs.v, rhs)]) self.clsdesc = klass
def get_source_ext(self): source = [] if not self.inner and self.package: source.append( ('PACKAGE', [('PACKAGE_START', 'package '), ('NAME_PACKAGE', '%s' % self.package), ('PACKAGE_END', ';\n')])) list_proto = [] list_proto.append( ('PROTOTYPE_ACCESS', '%s class ' % ' '.join(self.access))) list_proto.append(('NAME_PROTOTYPE', '%s' % self.name, self.package)) if self.superclass is not None: self.superclass = self.superclass[1:-1].replace('/', '.') if self.superclass.split('.')[-1] == 'Object': self.superclass = None if self.superclass is not None: list_proto.append(('EXTEND', ' extends ')) list_proto.append(('NAME_SUPERCLASS', '%s' % self.superclass)) if self.interfaces is not None: interfaces = self.interfaces[1:-1].split(' ') list_proto.append(('IMPLEMENTS', ' implements ')) for i in range(len(interfaces)): if i != 0: list_proto.append(('COMMA', ', ')) list_proto.append( ('NAME_INTERFACE', interfaces[i][1:-1].replace('/', '.'))) list_proto.append(('PROTOTYPE_END', ' {\n')) source.append(("PROTOTYPE", list_proto)) for field in self.fields.values(): field_access_flags = field.get_access_flags() access = [util.ACCESS_FLAGS_FIELDS[flag] for flag in util.ACCESS_FLAGS_FIELDS if flag & field_access_flags] f_type = util.get_type(field.get_descriptor()) name = field.get_name() if access: access_str = ' %s ' % ' '.join(access) else: access_str = ' ' source.append( ('FIELD', [('FIELD_ACCESS', access_str), ('FIELD_TYPE', '%s' % f_type), ('SPACE', ' '), ('NAME_FIELD', '%s' % name, f_type, field), ('FIELD_END', ';\n')])) #TODO: call get_source_ext for each subclass? for klass in self.subclasses.values(): source.append((klass, klass.get_source())) for _, method in self.methods.iteritems(): if isinstance(method, DvMethod): source.append(("METHOD", method.get_source_ext())) source.append(("CLASS_END", [('CLASS_END', '}\n')])) return source
def get_source_ext(self): source = [] if not self.inner and self.package: source.append(('PACKAGE', [('PACKAGE_START', 'package '), ('NAME_PACKAGE', '%s' % self.package), ('PACKAGE_END', ';\n')])) list_proto = [] list_proto.append( ('PROTOTYPE_ACCESS', '%s class ' % ' '.join(self.access))) list_proto.append(('NAME_PROTOTYPE', '%s' % self.name, self.package)) superclass = self.superclass if superclass is not None and superclass != 'Ljava/lang/Object;': superclass = superclass[1:-1].replace('/', '.') list_proto.append(('EXTEND', ' extends ')) list_proto.append(('NAME_SUPERCLASS', '%s' % superclass)) if len(self.interfaces) > 0: list_proto.append(('IMPLEMENTS', ' implements ')) for i, interface in enumerate(self.interfaces): if i != 0: list_proto.append(('COMMA', ', ')) list_proto.append( ('NAME_INTERFACE', interface[1:-1].replace('/', '.'))) list_proto.append(('PROTOTYPE_END', ' {\n')) source.append(("PROTOTYPE", list_proto)) for field in self.fields: field_access_flags = field.get_access_flags() access = [ util.ACCESS_FLAGS_FIELDS[flag] for flag in util.ACCESS_FLAGS_FIELDS if flag & field_access_flags ] f_type = util.get_type(field.get_descriptor()) name = field.get_name() if access: access_str = ' %s ' % ' '.join(access) else: access_str = ' ' source.append( ('FIELD', [('FIELD_ACCESS', access_str), ('FIELD_TYPE', '%s' % f_type), ('SPACE', ' '), ('NAME_FIELD', '%s' % name, f_type, field), ('FIELD_END', ';\n')])) #TODO: call get_source_ext for each subclass? for klass in list(self.subclasses.values()): source.append((klass, klass.get_source())) for method in self.methods: if isinstance(method, DvMethod): source.append(("METHOD", method.get_source_ext())) source.append(("CLASS_END", [('CLASS_END', '}\n')])) return source
def get_source_ext(self): source = [] if not self.inner and self.package: source.append( ('PACKAGE', [('PACKAGE_START', 'package '), ( 'NAME_PACKAGE', '%s' % self.package), ('PACKAGE_END', ';\n') ])) list_proto = [] list_proto.append( ('PROTOTYPE_ACCESS', '%s class ' % ' '.join(self.access))) list_proto.append(('NAME_PROTOTYPE', '%s' % self.name, self.package)) superclass = self.superclass if superclass is not None and superclass != 'Ljava/lang/Object;': superclass = superclass[1:-1].replace('/', '.') list_proto.append(('EXTEND', ' extends ')) list_proto.append(('NAME_SUPERCLASS', '%s' % superclass)) if len(self.interfaces) > 0: list_proto.append(('IMPLEMENTS', ' implements ')) for i, interface in enumerate(self.interfaces): if i != 0: list_proto.append(('COMMA', ', ')) list_proto.append( ('NAME_INTERFACE', interface[1:-1].replace('/', '.'))) list_proto.append(('PROTOTYPE_END', ' {\n')) source.append(("PROTOTYPE", list_proto)) for field in self.fields: field_access_flags = field.get_access_flags() access = [util.ACCESS_FLAGS_FIELDS[flag] for flag in util.ACCESS_FLAGS_FIELDS if flag & field_access_flags] f_type = util.get_type(field.get_descriptor()) name = field.get_name() if access: access_str = ' %s ' % ' '.join(access) else: access_str = ' ' source.append( ('FIELD', [('FIELD_ACCESS', access_str), ( 'FIELD_TYPE', '%s' % f_type), ('SPACE', ' '), ( 'NAME_FIELD', '%s' % name, f_type, field), ('FIELD_END', ';\n')])) for method in self.methods: if isinstance(method, DvMethod): source.append(("METHOD", method.get_source_ext())) source.append(("CLASS_END", [('CLASS_END', '}\n')])) return source
def get_source(self): source = [] if not self.inner and self.package: source.append('package %s;\n' % self.package) superclass, prototype = self.superclass, self.prototype if superclass is not None and superclass != 'Ljava/lang/Object;': superclass = superclass[1:-1].replace('/', '.') prototype += ' extends %s' % superclass if len(self.interfaces) > 0: prototype += ' implements %s' % ', '.join( [n[1:-1].replace('/', '.') for n in self.interfaces]) source.append('%s {\n' % prototype) for field in self.fields: name = field.get_name() access = util.get_access_field(field.get_access_flags()) f_type = util.get_type(field.get_descriptor()) source.append(' ') if access: source.append(' '.join(access)) source.append(' ') init_value = field.get_init_value() if init_value: value = init_value.value if f_type == 'String': if value: value = '"%s"' % value.encode("unicode-escape").decode( "ascii") else: # FIXME we can not check if this value here is null or "" # In both cases we end up here... value = '""' elif field.proto == 'B': # byte value: convert from unsiged int to signed and print as hex # as bytes are signed in Java value = hex(struct.unpack("b", struct.pack("B", value))[0]) source.append('%s %s = %s;\n' % (f_type, name, value)) else: source.append('%s %s;\n' % (f_type, name)) for method in self.methods: if isinstance(method, DvMethod): source.append(method.get_source()) source.append('}\n') return ''.join(source)
def get_source(self): source = [] if not self.inner and self.package: source.append('package %s;\n' % self.package) superclass, prototype = self.superclass, self.prototype if superclass is not None and superclass != 'Ljava/lang/Object;': superclass = superclass[1:-1].replace('/', '.') prototype += ' extends %s' % superclass if len(self.interfaces) > 0: prototype += ' implements %s' % ', '.join( [n[1:-1].replace('/', '.') for n in self.interfaces]) source.append('%s {\n' % prototype) for field in self.fields: name = field.get_name() access = util.get_access_field(field.get_access_flags()) f_type = util.get_type(field.get_descriptor()) source.append(' ') if access: source.append(' '.join(access)) source.append(' ') init_value = field.get_init_value() if init_value: value = init_value.value if f_type == 'String': if value: value = '"%s"' % value.encode("unicode-escape").decode("ascii") else: # FIXME we can not check if this value here is null or "" # In both cases we end up here... value = '""' elif field.proto == 'B': # byte value: convert from unsiged int to signed and print as hex # as bytes are signed in Java value = hex(struct.unpack("b", struct.pack("B", value))[0]) source.append('%s %s = %s;\n' % (f_type, name, value)) else: source.append('%s %s;\n' % (f_type, name)) for method in self.methods: if isinstance(method, DvMethod): source.append(method.get_source()) source.append('}\n') return ''.join(source)
def get_source(self): source = [] if not self.inner and self.package: source.append('package %s;\n' % self.package) superclass, prototype = self.superclass, self.prototype if superclass is not None and superclass != 'Ljava/lang/Object;': superclass = superclass[1:-1].replace('/', '.') prototype += ' extends %s' % superclass if len(self.interfaces) > 0: prototype += ' implements %s' % ', '.join( [n[1:-1].replace('/', '.') for n in self.interfaces]) source.append('%s {\n' % prototype) for field in self.fields: name = field.get_name() access = util.get_access_field(field.get_access_flags()) f_type = util.get_type(field.get_descriptor()) source.append(' ') if access: source.append(' '.join(access)) source.append(' ') if field.init_value: value = field.init_value.value if f_type == 'String': value = '"%s"' % value elif field.proto == 'B': value = '0x%x' % struct.unpack('b', value)[0] source.append('%s %s = %s;\n' % (f_type, name, value)) else: source.append('%s %s;\n' % (f_type, name)) for klass in self.subclasses.values(): source.append(klass.get_source()) for method in self.methods: if isinstance(method, DvMethod): source.append(method.get_source()) else: source.append( DvMethod(self.vma.get_method(method)).get_source()) source.append('}\n') return ''.join(source)
def get_source(self): source = [] if not self.inner and self.package: source.append('package %s;\n' % self.package) superclass, prototype = self.superclass, self.prototype if superclass is not None and superclass != 'Ljava/lang/Object;': superclass = superclass[1:-1].replace('/', '.') prototype += ' extends %s' % superclass if len(self.interfaces) > 0: prototype += ' implements %s' % ', '.join( [n[1:-1].replace('/', '.') for n in self.interfaces]) source.append('%s {\n' % prototype) for field in self.fields: name = field.get_name() access = util.get_access_field(field.get_access_flags()) f_type = util.get_type(field.get_descriptor()) source.append(' ') if access: source.append(' '.join(access)) source.append(' ') if field.init_value: value = field.init_value.value if f_type == 'String': value = '"%s"' % value elif field.proto == 'B': value = '0x%x' % struct.unpack('b', value)[0] source.append('%s %s = %s;\n' % (f_type, name, value)) else: source.append('%s %s;\n' % (f_type, name)) for klass in self.subclasses.values(): source.append(klass.get_source()) for method in self.methods: if isinstance(method, DvMethod): source.append(method.get_source()) else: source.append(DvMethod(self.vma.get_method(method)).get_source( )) source.append('}\n') return ''.join(source)
def make_node(graph, block, block_to_node, vmap, gen_ret): node = block_to_node.get(block) if node is None: node = build_node_from_block(block, vmap, gen_ret) block_to_node[block] = node if block.exception_analysis: for _type, _, exception_target in block.exception_analysis.exceptions: exception_node = block_to_node.get(exception_target) if exception_node is None: exception_node = build_node_from_block(exception_target, vmap, gen_ret, _type) exception_node.set_catch_type(get_type(_type)) exception_node.in_catch = True block_to_node[exception_target] = exception_node graph.add_catch_edge(node, exception_node) for _, _, child_block in block.childs: child_node = block_to_node.get(child_block) if child_node is None: child_node = build_node_from_block(child_block, vmap, gen_ret) block_to_node[child_block] = child_node graph.add_edge(node, child_node) if node.type.is_switch: node.add_case(child_node) if node.type.is_cond: if_target = ((block.end / 2) - (block.last_length / 2) + node.off_last_ins) child_addr = child_block.start / 2 if if_target == child_addr: node.true = child_node else: node.false = child_node # Check that both branch of the if point to something # It may happen that both branch point to the same node, in this case # the false branch will be None. So we set it to the right node. # TODO: In this situation, we should transform the condition node into # a statement node if node.type.is_cond and node.false is None: node.false = node.true return node
def __init__(self, methanalysis): method = methanalysis.get_method() self.start_block = next(methanalysis.get_basic_blocks().get(), None) self.cls_name = method.get_class_name() self.name = method.get_name() self.lparams = [] self.var_to_name = {} self.writer = None self.graph = None access = method.get_access_flags() self.access = [ name for flag, name in util.ACCESS_FLAGS_METHODS.iteritems() if flag & access ] desc = method.get_descriptor() self.type = util.get_type(desc.split(')')[-1]) self.params_type = util.get_params_type(desc) self.exceptions = methanalysis.exceptions.exceptions code = method.get_code() if code is None: logger.debug('No code : %s %s', self.name, self.cls_name) else: start = code.registers_size - code.ins_size if 'static' not in self.access: self.var_to_name[start] = ThisParam(start, self.name) self.lparams.append(start) start += 1 num_param = 0 for ptype in self.params_type: param = start + num_param self.lparams.append(param) self.var_to_name.setdefault(param, Param(param, ptype)) num_param += util.get_type_size(ptype) if 0: from androguard.core import bytecode bytecode.method2png('/tmp/dad/graphs/%s#%s.png' % \ (self.cls_name.split('/')[-1][:-1], self.name), methanalysis)
def get_source(self): source = [] if not self.inner and self.package: source.append('package %s;\n' % self.package) if self.superclass is not None: self.superclass = self.superclass[1:-1].replace('/', '.') if self.superclass.split('.')[-1] == 'Object': self.superclass = None if self.superclass is not None: self.prototype += ' extends %s' % self.superclass if self.interfaces is not None: interfaces = self.interfaces[1:-1].split(' ') self.prototype += ' implements %s' % ', '.join( [n[1:-1].replace('/', '.') for n in interfaces]) source.append('%s {\n' % self.prototype) for name, field in sorted(self.fields.iteritems()): access = util.get_access_field(field.get_access_flags()) f_type = util.get_type(field.get_descriptor()) source.append(' ') if access: source.append(' '.join(access)) source.append(' ') if field.init_value: value = field.init_value.value if f_type == 'String': value = '"%s"' % value source.append('%s %s = %s;\n' % (f_type, name, value)) else: source.append('%s %s;\n' % (f_type, name)) for klass in self.subclasses.values(): source.append(klass.get_source()) for _, method in self.methods.iteritems(): if isinstance(method, DvMethod): source.append(method.get_source()) source.append('}\n') return ''.join(source)
def get_source(self): source = [] if not self.inner and self.package: source.append("package %s;\n" % self.package) superclass, prototype = self.superclass, self.prototype if superclass is not None and superclass != "Ljava/lang/Object;": superclass = superclass[1:-1].replace("/", ".") prototype += " extends %s" % superclass if len(self.interfaces) > 0: prototype += " implements %s" % ", ".join([n[1:-1].replace("/", ".") for n in self.interfaces]) source.append("%s {\n" % prototype) for field in self.fields: name = field.get_name() access = util.get_access_field(field.get_access_flags()) f_type = util.get_type(field.get_descriptor()) source.append(" ") if access: source.append(" ".join(access)) source.append(" ") if field.init_value: value = field.init_value.value if f_type == "String": value = '"%s"' % value elif field.proto == "B": value = "0x%x" % struct.unpack("b", value)[0] source.append("%s %s = %s;\n" % (f_type, name, value)) else: source.append("%s %s;\n" % (f_type, name)) for klass in self.subclasses.values(): source.append(klass.get_source()) for method in self.methods: if isinstance(method, DvMethod): source.append(method.get_source()) source.append("}\n") return "".join(source)
def __init__(self, methanalysis): method = methanalysis.get_method() self.start_block = next(methanalysis.get_basic_blocks().get(), None) self.cls_name = method.get_class_name() self.name = method.get_name() self.lparams = [] self.var_to_name = {} self.writer = None self.graph = None access = method.get_access_flags() self.access = [flag for flag in util.ACCESS_FLAGS_METHODS if flag & access] desc = method.get_descriptor() self.type = util.get_type(desc.split(')')[-1]) self.params_type = util.get_params_type(desc) self.exceptions = methanalysis.exceptions.exceptions code = method.get_code() if code is None: logger.debug('No code : %s %s', self.name, self.cls_name) else: start = code.registers_size - code.ins_size if 0x8 not in self.access: self.var_to_name[start] = ThisParam(start, self.name) self.lparams.append(start) start += 1 num_param = 0 for ptype in self.params_type: param = start + num_param self.lparams.append(param) self.var_to_name.setdefault(param, Param(param, ptype)) num_param += util.get_type_size(ptype) if 0: from androguard.core import bytecode bytecode.method2png('/tmp/dad/graphs/%s#%s.png' % \ (self.cls_name.split('/')[-1][:-1], self.name), methanalysis)
def get_source(self): source = [] if not self.inner and self.package: source.append('package %s;\n' % self.package) if self.superclass is not None: self.superclass = self.superclass[1:-1].replace('/', '.') if self.superclass.split('.')[-1] == 'Object': self.superclass = None if self.superclass is not None: self.prototype += ' extends %s' % self.superclass if self.interfaces is not None: interfaces = self.interfaces[1:-1].split(' ') self.prototype += ' implements %s' % ', '.join( [n[1:-1].replace('/', '.') for n in interfaces]) source.append('%s {\n' % self.prototype) for field in self.fields.values(): field_access_flags = field.get_access_flags() access = [ util.ACCESS_FLAGS_FIELDS[flag] for flag in util.ACCESS_FLAGS_FIELDS if flag & field_access_flags ] f_type = util.get_type(field.get_descriptor()) name = field.get_name() source.append(' %s %s %s;\n' % (' '.join(access), f_type, name)) for klass in self.subclasses.values(): source.append(klass.get_source()) for _, method in self.methods.iteritems(): if isinstance(method, DvMethod): source.append(method.get_source()) source.append('}\n') return ''.join(source)
def get_source_ext(self): source = [] if not self.inner and self.package: source.append(('PACKAGE', [('PACKAGE_START', 'package '), ('NAME_PACKAGE', '%s' % self.package), ('PACKAGE_END', ';\n')])) list_proto = [('PROTOTYPE_ACCESS', '%s class ' % ' '.join(self.access)), ('NAME_PROTOTYPE', '%s' % self.name, self.package)] superclass = self.superclass if superclass is not None and superclass != 'Ljava/lang/Object;': superclass = superclass[1:-1].replace('/', '.') list_proto.append(('EXTEND', ' extends ')) list_proto.append(('NAME_SUPERCLASS', '%s' % superclass)) if len(self.interfaces) > 0: list_proto.append(('IMPLEMENTS', ' implements ')) for i, interface in enumerate(self.interfaces): if i != 0: list_proto.append(('COMMA', ', ')) list_proto.append( ('NAME_INTERFACE', interface[1:-1].replace('/', '.'))) list_proto.append(('PROTOTYPE_END', ' {\n')) source.append(("PROTOTYPE", list_proto)) for field in self.fields: field_access_flags = field.get_access_flags() access = [ util.ACCESS_FLAGS_FIELDS[flag] for flag in util.ACCESS_FLAGS_FIELDS if flag & field_access_flags ] f_type = util.get_type(field.get_descriptor()) name = field.get_name() if access: access_str = ' %s ' % ' '.join(access) else: access_str = ' ' value = None init_value = field.get_init_value() if init_value: value = init_value.value if f_type == 'String': if value: value = ' = "%s"' % value.encode( "unicode-escape").decode("ascii") else: # FIXME we can not check if this value here is null or "" # In both cases we end up here... value = ' = ""' elif field.proto == 'B': # a byte value = ' = %s' % hex( struct.unpack("b", struct.pack("B", value))[0]) else: value = ' = %s' % str(value) if value: source.append( ('FIELD', [('FIELD_ACCESS', access_str), ('FIELD_TYPE', '%s' % f_type), ('SPACE', ' '), ('NAME_FIELD', '%s' % name, f_type, field), ('FIELD_VALUE', value), ('FIELD_END', ';\n')])) else: source.append( ('FIELD', [('FIELD_ACCESS', access_str), ('FIELD_TYPE', '%s' % f_type), ('SPACE', ' '), ('NAME_FIELD', '%s' % name, f_type, field), ('FIELD_END', ';\n')])) for method in self.methods: if isinstance(method, DvMethod): source.append(("METHOD", method.get_source_ext())) source.append(("CLASS_END", [('CLASS_END', '}\n')])) return source
def visit_move_exception(self, var): var.declared = True var_type = var.get_type() or 'unknownType' self.write('%s v%s' % (get_type(var_type), var.value()))
def visit_new_array(self, atype, size): self.write('new %s[' % get_type(atype[1:])) size.visit(self) self.write(']')
def visit_new(self, atype, data=None): self.write('new %s' % get_type(atype)) self.write_ext(('NEW', 'new ')) self.write_ext( ('NAME_CLASS_NEW', '%s' % get_type(atype), data.type, data))
def visit_new(self, atype): self.write('new %s' % get_type(atype))
def visit_variable(self, var): if not var.declared: var_type = var.get_type() or 'unknownType' self.write('%s ' % get_type(var_type)) var.declared = True self.write('v%s' % var.value())
def visit_decl(self, var): if not var.declared: var_type = var.get_type() or 'unknownType' self.write('%s%s v%s' % (self.space(), get_type(var_type), var.value())) self.end_ins()
def visit_new_array(self, atype, size): self.write('new %s[' % get_type(atype[1:]), data="NEW_ARRAY") size.visit(self) self.write(']', data="NEW_ARRAY_END")
def visit(self, visitor): return visitor.visit_check_cast(self.var_map[self.arg], util.get_type(self.type))
def visit_exception(self, visitor): if self.exception_ins: visitor.visit_ins(self.exception_ins) else: visitor.write(get_type(self.catch_type))