private static String buildFrameworkCache(String resPath) throws Exception { List<File> resFiles = new LinkedList<>(); try { URL url = FrameworkJarBuilder.class.getResource(resPath); File file = new File(url.toURI()); resFiles.addAll(Utils.getFilesWithSmaliExtension(file)); } catch (URISyntaxException e) { e.printStackTrace(); } DexBuilder dexBuilder = new DexBuilder(Opcodes.getDefault()); StringBuilder sb = new StringBuilder(); for (File resFile : resFiles) { String absPath = resFile.getAbsolutePath(); String path = absPath.substring(absPath.lastIndexOf(FRAMEWORK_ROOT)); InputStream is = FrameworkJarBuilder.class.getResourceAsStream(path); BuilderClassDef classDef = SmaliParser.parse(path, is, dexBuilder); sb.append(ReferenceUtil.getReferenceString(classDef)); sb.append(':').append(path.substring(1)).append('\n'); } return sb.toString(); }
/** * Create a ClassManager with no input Smali classes. It will only contain parse library * classes. */ public ClassManager build() throws IOException { DexBuilder dexBuilder = new DexBuilder(Opcodes.getDefault()); return build(dexBuilder); }
/** * @param smaliPath Path to Smali file or folder */ public ClassManager build(String smaliPath) throws IOException { return build(smaliPath, new DexBuilder(Opcodes.getDefault())); }
private void build() throws AndrolibException { try { DexBuilder dexBuilder; if (mApiLevel > 0) { dexBuilder = new DexBuilder(Opcodes.forApi(mApiLevel)); } else { dexBuilder = new DexBuilder(Opcodes.getDefault()); } for (String fileName : mSmaliDir.getDirectory().getFiles(true)) { buildFile(fileName, dexBuilder); } dexBuilder.writeTo(new FileDataStore( new File(mDexFile.getAbsolutePath()))); } catch (IOException | DirectoryException ex) { throw new AndrolibException(ex); } }
private static ClassProvider getBasicClasses() { // fallbacks for some special classes that we assume are present return new DexClassProvider(new ImmutableDexFile(Opcodes.getDefault(), ImmutableSet.of( new ReflectionClassDef(Class.class), new ReflectionClassDef(Cloneable.class), new ReflectionClassDef(Object.class), new ReflectionClassDef(Serializable.class), new ReflectionClassDef(String.class), new ReflectionClassDef(Throwable.class)))); }
@Nonnull public static Opcodes getDefaultOpCodes() { if (DEFAULT_OPCODES == null) { DEFAULT_OPCODES = Opcodes.getDefault(); } return DEFAULT_OPCODES; }
public <F extends DexFile> MultiDex(@Nonnull List<F> dexFiles) { Opcodes firstOpcodes = null; if (!dexFiles.isEmpty()) { DexFile dexFile = dexFiles.get(0); if (dexFile instanceof DexBackedDexFile) { DexBackedDexFile dexBackedDexFile = (DexBackedDexFile) dexFile; firstOpcodes = dexBackedDexFile.getOpcodes(); } } opcodes = firstOpcodes == null ? Opcodes.getDefault() : firstOpcodes; dexFiles.forEach(this::addFile); }
try { MultiDexContainer<? extends DexBackedDexFile> container = DexFileFactory.loadDexContainer(file, Opcodes.getDefault()); entries = container.getDexEntryNames(); } catch (IOException ex) {
@Test public void testAnnotationElementOrder() { // Elements are out of order wrt to the element name ImmutableSet<ImmutableAnnotationElement> elements = ImmutableSet.of(new ImmutableAnnotationElement("zabaglione", ImmutableNullEncodedValue.INSTANCE), new ImmutableAnnotationElement("blah", ImmutableNullEncodedValue.INSTANCE)); ImmutableAnnotation annotation = new ImmutableAnnotation(AnnotationVisibility.RUNTIME, "Lorg/test/anno;", elements); ImmutableClassDef classDef = new ImmutableClassDef("Lorg/test/blah;", 0, "Ljava/lang/Object;", null, null, ImmutableSet.of(annotation), null, null); MemoryDataStore dataStore = new MemoryDataStore(); try { DexPool.writeTo(dataStore, new ImmutableDexFile(Opcodes.getDefault(), ImmutableSet.of(classDef))); } catch (IOException ex) { throw new RuntimeException(ex); } DexBackedDexFile dexFile = new DexBackedDexFile(Opcodes.getDefault(), dataStore.getBuffer()); ClassDef dbClassDef = Iterables.getFirst(dexFile.getClasses(), null); Assert.assertNotNull(dbClassDef); Annotation dbAnnotation = Iterables.getFirst(dbClassDef.getAnnotations(), null); Assert.assertNotNull(dbAnnotation); List<AnnotationElement> dbElements = Lists.newArrayList(dbAnnotation.getElements()); // Ensure that the elements were written out in sorted order Assert.assertEquals(2, dbElements.size()); Assert.assertEquals("blah", dbElements.get(0).getName()); Assert.assertEquals("zabaglione", dbElements.get(1).getName()); }
public void runTest(String test, boolean registerInfo, boolean isArt) throws IOException, URISyntaxException { String dexFilePath = String.format("%s%sclasses.dex", test, File.separatorChar); DexFile dexFile = DexFileFactory.loadDexFile(findResource(dexFilePath), Opcodes.getDefault()); BaksmaliOptions options = new BaksmaliOptions(); if (registerInfo) { options.registerInfo = BaksmaliOptions.ALL | BaksmaliOptions.FULLMERGE; if (isArt) { options.classPath = new ClassPath(new ArrayList<ClassProvider>(), true, 56); } else { options.classPath = new ClassPath(); } } options.implicitReferences = false; for (ClassDef classDef: dexFile.getClasses()) { StringWriter stringWriter = new StringWriter(); IndentingWriter writer = new IndentingWriter(stringWriter); ClassDefinition classDefinition = new ClassDefinition(options, classDef); classDefinition.writeTo(writer); writer.close(); String className = classDef.getType(); String smaliPath = String.format("%s%s%s.smali", test, File.separatorChar, className.substring(1, className.length() - 1)); String smaliContents = readResource(smaliPath); Assert.assertEquals(BaksmaliTestUtils.normalizeWhitespace(smaliContents), BaksmaliTestUtils.normalizeWhitespace((stringWriter.toString()))); } }
@Test public void testGetSuperclassChain() throws IOException { ClassDef objectClassDef = TestUtils.makeClassDef("Ljava/lang/Object;", null); ClassDef oneClassDef = TestUtils.makeClassDef("Ltest/one;", "Ljava/lang/Object;"); ClassDef twoClassDef = TestUtils.makeClassDef("Ltest/two;", "Ltest/one;"); ClassDef threeClassDef = TestUtils.makeClassDef("Ltest/three;", "Ltest/two;"); ImmutableSet<ClassDef> classes = ImmutableSet.<ClassDef>of( objectClassDef, oneClassDef, twoClassDef, threeClassDef); ClassPath classPath = new ClassPath(new DexClassProvider(new ImmutableDexFile(Opcodes.getDefault(), classes))); TypeProto objectClassProto = classPath.getClass("Ljava/lang/Object;"); TypeProto oneClassProto = classPath.getClass("Ltest/one;"); TypeProto twoClassProto = classPath.getClass("Ltest/two;"); TypeProto threeClassProto = classPath.getClass("Ltest/three;"); Assert.assertEquals( ImmutableList.<TypeProto>of(), ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(objectClassProto))); Assert.assertEquals( ImmutableList.<TypeProto>of(objectClassProto), ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(oneClassProto))); Assert.assertEquals( ImmutableList.<TypeProto>of(oneClassProto, objectClassProto), ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(twoClassProto))); Assert.assertEquals( ImmutableList.<TypeProto>of(twoClassProto, oneClassProto, objectClassProto), ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(threeClassProto))); }
@Test public void testGetSuperclassChain_Unresolved() throws IOException { // Ltest/one; isn't defined ClassDef twoClassDef = TestUtils.makeClassDef("Ltest/two;", "Ltest/one;"); ClassDef threeClassDef = TestUtils.makeClassDef("Ltest/three;", "Ltest/two;"); ImmutableSet<ClassDef> classes = ImmutableSet.<ClassDef>of(twoClassDef, threeClassDef); ClassPath classPath = new ClassPath(new DexClassProvider(new ImmutableDexFile(Opcodes.getDefault(), classes))); TypeProto unknownClassProto = classPath.getUnknownClass(); TypeProto oneClassProto = classPath.getClass("Ltest/one;"); TypeProto twoClassProto = classPath.getClass("Ltest/two;"); TypeProto threeClassProto = classPath.getClass("Ltest/three;"); Assert.assertEquals( ImmutableList.<TypeProto>of(oneClassProto, unknownClassProto), ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(twoClassProto))); Assert.assertEquals( ImmutableList.<TypeProto>of(twoClassProto, oneClassProto, unknownClassProto), ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(threeClassProto))); } }
@Test public void testCustomMethodInlineTable_Direct() throws IOException { List<ImmutableInstruction> instructions = Lists.newArrayList( new ImmutableInstruction35mi(Opcode.EXECUTE_INLINE, 1, 0, 0, 0, 0, 0, 0), new ImmutableInstruction10x(Opcode.RETURN_VOID)); ImmutableMethodImplementation methodImpl = new ImmutableMethodImplementation(1, instructions, null, null); ImmutableMethod method = new ImmutableMethod("Lblah;", "blah", null, "V", AccessFlags.PRIVATE.getValue(), null, methodImpl); ClassDef classDef = new ImmutableClassDef("Lblah;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, null, ImmutableList.of(method), null); DexFile dexFile = new ImmutableDexFile(Opcodes.getDefault(), ImmutableList.of(classDef)); ClassPathResolver resolver = new ClassPathResolver(ImmutableList.<String>of(), ImmutableList.<String>of(), ImmutableList.<String>of(), dexFile); ClassPath classPath = new ClassPath(resolver.getResolvedClassProviders(), false, ClassPath.NOT_ART); InlineMethodResolver inlineMethodResolver = new CustomInlineMethodResolver(classPath, "Lblah;->blah()V"); MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, inlineMethodResolver, false); Instruction deodexedInstruction = methodAnalyzer.getInstructions().get(0); Assert.assertEquals(Opcode.INVOKE_DIRECT, deodexedInstruction.getOpcode()); MethodReference methodReference = (MethodReference)((Instruction35c)deodexedInstruction).getReference(); Assert.assertEquals(method, methodReference); } }
@Test public void testCustomMethodInlineTable_Virtual() throws IOException { List<ImmutableInstruction> instructions = Lists.newArrayList( new ImmutableInstruction35mi(Opcode.EXECUTE_INLINE, 1, 0, 0, 0, 0, 0, 0), new ImmutableInstruction10x(Opcode.RETURN_VOID)); ImmutableMethodImplementation methodImpl = new ImmutableMethodImplementation(1, instructions, null, null); ImmutableMethod method = new ImmutableMethod("Lblah;", "blah", null, "V", AccessFlags.PUBLIC.getValue(), null, methodImpl); ClassDef classDef = new ImmutableClassDef("Lblah;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, null, null, ImmutableList.of(method)); DexFile dexFile = new ImmutableDexFile(Opcodes.getDefault(), ImmutableList.of(classDef)); ClassPathResolver resolver = new ClassPathResolver(ImmutableList.<String>of(), ImmutableList.<String>of(), ImmutableList.<String>of(), dexFile); ClassPath classPath = new ClassPath(resolver.getResolvedClassProviders(), false, ClassPath.NOT_ART); InlineMethodResolver inlineMethodResolver = new CustomInlineMethodResolver(classPath, "Lblah;->blah()V"); MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, inlineMethodResolver, false); Instruction deodexedInstruction = methodAnalyzer.getInstructions().get(0); Assert.assertEquals(Opcode.INVOKE_VIRTUAL, deodexedInstruction.getOpcode()); MethodReference methodReference = (MethodReference)((Instruction35c)deodexedInstruction).getReference(); Assert.assertEquals(method, methodReference); }
@Test public void testCustomMethodInlineTable_Static() throws IOException { List<ImmutableInstruction> instructions = Lists.newArrayList( new ImmutableInstruction35mi(Opcode.EXECUTE_INLINE, 1, 0, 0, 0, 0, 0, 0), new ImmutableInstruction10x(Opcode.RETURN_VOID)); ImmutableMethodImplementation methodImpl = new ImmutableMethodImplementation(1, instructions, null, null); ImmutableMethod method = new ImmutableMethod("Lblah;", "blah", null, "V", AccessFlags.STATIC.getValue(), null, methodImpl); ClassDef classDef = new ImmutableClassDef("Lblah;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, null, ImmutableList.of(method), null); DexFile dexFile = new ImmutableDexFile(Opcodes.getDefault(), ImmutableList.of(classDef)); ClassPathResolver resolver = new ClassPathResolver(ImmutableList.<String>of(), ImmutableList.<String>of(), ImmutableList.<String>of(), dexFile); ClassPath classPath = new ClassPath(resolver.getResolvedClassProviders(), false, ClassPath.NOT_ART); InlineMethodResolver inlineMethodResolver = new CustomInlineMethodResolver(classPath, "Lblah;->blah()V"); MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, inlineMethodResolver, false); Instruction deodexedInstruction = methodAnalyzer.getInstructions().get(0); Assert.assertEquals(Opcode.INVOKE_STATIC, deodexedInstruction.getOpcode()); MethodReference methodReference = (MethodReference)((Instruction35c)deodexedInstruction).getReference(); Assert.assertEquals(method, methodReference); }
private static ClassProvider getBasicClasses() { // fallbacks for some special classes that we assume are present return new DexClassProvider(new ImmutableDexFile(Opcodes.getDefault(), ImmutableSet.of( new ReflectionClassDef(Class.class), new ReflectionClassDef(Cloneable.class), new ReflectionClassDef(Object.class), new ReflectionClassDef(Serializable.class), new ReflectionClassDef(String.class), new ReflectionClassDef(Throwable.class)))); }
@Test public void should_remove_final_modifier_from_the_given_method() throws IOException { ImmutableMethod method = new ImmutableMethod("Lfoo/Bar;", "f", null, "V", AccessFlags.FINAL.getValue(), null, null); ImmutableClassDef def = new ImmutableClassDef("Lfoo/Bar;", 0, "Ljava/lang/Object;", null, null, null, null, Collections.singleton(method)); byte[] bytecode = DexPoolUtils.toBytecode(new ImmutableDexFile(Opcodes.getDefault(), Collections.singleton(def))); DexRewriter rewriter = new DexRewriter(new FinalModifierRemoverModule()); Method out = rewriter.getMethodRewriter() .rewrite(new DexBackedDexFile(null, bytecode) .getClasses() .iterator() .next() .getVirtualMethods() .iterator() .next()); assertFalse(AccessFlags.FINAL.isSet(out.getAccessFlags())); }
@Test public void should_remove_final_modifier_from_the_given_class() throws IOException { ImmutableClassDef def = new ImmutableClassDef("Lfoo/Bar;", AccessFlags.FINAL.getValue(), "Ljava/lang/Object;", null, null, null, null, null); byte[] bytecode = DexPoolUtils.toBytecode(new ImmutableDexFile(Opcodes.getDefault(), Collections.singleton(def))); DexRewriter rewriter = new DexRewriter(new FinalModifierRemoverModule()); ClassDef out = rewriter.getClassDefRewriter() .rewrite(new DexBackedDexFile(null, bytecode) .getClasses() .iterator() .next()); assertFalse(AccessFlags.FINAL.isSet(out.getAccessFlags())); }
@Test public void testInstanceOfNarrowingNez_dalvik() throws IOException { MethodImplementationBuilder builder = new MethodImplementationBuilder(2); builder.addInstruction(new BuilderInstruction22c(Opcode.INSTANCE_OF, 0, 1, new ImmutableTypeReference("Lmain;"))); builder.addInstruction(new BuilderInstruction21t(Opcode.IF_NEZ, 0, builder.getLabel("instance_of"))); builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID)); builder.addLabel("instance_of"); builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID)); MethodImplementation methodImplementation = builder.getMethodImplementation(); Method method = new ImmutableMethod("Lmain;", "narrowing", Collections.singletonList(new ImmutableMethodParameter("Ljava/lang/Object;", null, null)), "V", AccessFlags.PUBLIC.getValue(), null, methodImplementation); ClassDef classDef = new ImmutableClassDef("Lmain;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, Collections.singletonList(method)); DexFile dexFile = new ImmutableDexFile(Opcodes.getDefault(), Collections.singletonList(classDef)); ClassPath classPath = new ClassPath(new DexClassProvider(dexFile)); MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, null, false); List<AnalyzedInstruction> analyzedInstructions = methodAnalyzer.getAnalyzedInstructions(); Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(2).getPreInstructionRegisterType(1).type.getType()); Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(3).getPreInstructionRegisterType(1).type.getType()); }
ClassDef classDef = new ImmutableClassDef("Lmain;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, Collections.singletonList(method)); DexFile dexFile = new ImmutableDexFile(Opcodes.getDefault(), Collections.singletonList(classDef));