Search in sources :

Example 41 with DexFile

use of org.jf.dexlib2.iface.DexFile in project smali by JesusFreke.

the class Baksmali method disassembleDexFile.

public static boolean disassembleDexFile(DexFile dexFile, File outputDir, int jobs, final BaksmaliOptions options, @Nullable List<String> classes) {
    //sort the classes, so that if we're on a case-insensitive file system and need to handle classes with file
    //name collisions, then we'll use the same name for each class, if the dex file goes through multiple
    //baksmali/smali cycles for some reason. If a class with a colliding name is added or removed, the filenames
    //may still change of course
    List<? extends ClassDef> classDefs = Ordering.natural().sortedCopy(dexFile.getClasses());
    final ClassFileNameHandler fileNameHandler = new ClassFileNameHandler(outputDir, ".smali");
    ExecutorService executor = Executors.newFixedThreadPool(jobs);
    List<Future<Boolean>> tasks = Lists.newArrayList();
    Set<String> classSet = null;
    if (classes != null) {
        classSet = new HashSet<String>(classes);
    }
    for (final ClassDef classDef : classDefs) {
        if (classSet != null && !classSet.contains(classDef.getType())) {
            continue;
        }
        tasks.add(executor.submit(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                return disassembleClass(classDef, fileNameHandler, options);
            }
        }));
    }
    boolean errorOccurred = false;
    try {
        for (Future<Boolean> task : tasks) {
            while (true) {
                try {
                    if (!task.get()) {
                        errorOccurred = true;
                    }
                } catch (InterruptedException ex) {
                    continue;
                } catch (ExecutionException ex) {
                    throw new RuntimeException(ex);
                }
                break;
            }
        }
    } finally {
        executor.shutdown();
    }
    return !errorOccurred;
}
Also used : ClassFileNameHandler(org.jf.util.ClassFileNameHandler) ClassDef(org.jf.dexlib2.iface.ClassDef)

Example 42 with DexFile

use of org.jf.dexlib2.iface.DexFile in project smali by JesusFreke.

the class DeodexCommand method getOptions.

@Override
protected BaksmaliOptions getOptions() {
    BaksmaliOptions options = super.getOptions();
    options.deodex = true;
    if (dexFile instanceof DexBackedOdexFile) {
        if (inlineTable == null) {
            options.inlineResolver = InlineMethodResolver.createInlineMethodResolver(((DexBackedOdexFile) dexFile).getOdexVersion());
        } else {
            File inlineTableFile = new File(inlineTable);
            if (!inlineTableFile.exists()) {
                System.err.println(String.format("Could not find file: %s", inlineTable));
                System.exit(-1);
            }
            try {
                options.inlineResolver = new CustomInlineMethodResolver(options.classPath, inlineTableFile);
            } catch (IOException ex) {
                System.err.println(String.format("Error while reading file: %s", inlineTableFile));
                ex.printStackTrace(System.err);
                System.exit(-1);
            }
        }
    }
    return options;
}
Also used : CustomInlineMethodResolver(org.jf.dexlib2.analysis.CustomInlineMethodResolver) DexBackedOdexFile(org.jf.dexlib2.dexbacked.DexBackedOdexFile) IOException(java.io.IOException) DexBackedOdexFile(org.jf.dexlib2.dexbacked.DexBackedOdexFile) File(java.io.File)

Example 43 with DexFile

use of org.jf.dexlib2.iface.DexFile in project smali by JesusFreke.

the class DisassembleCommand method getOptions.

protected BaksmaliOptions getOptions() {
    if (dexFile == null) {
        throw new IllegalStateException("You must call loadDexFile first");
    }
    final BaksmaliOptions options = new BaksmaliOptions();
    if (needsClassPath()) {
        try {
            options.classPath = analysisArguments.loadClassPathForDexFile(inputFile.getAbsoluteFile().getParentFile(), dexFile, shouldCheckPackagePrivateAccess());
        } catch (Exception ex) {
            System.err.println("\n\nError occurred while loading class path files. Aborting.");
            ex.printStackTrace(System.err);
            System.exit(-1);
        }
    }
    if (!resourceIdFiles.isEmpty()) {
        Map<String, File> resourceFiles = Maps.newHashMap();
        assert (resourceIdFiles.size() % 2) == 0;
        for (int i = 0; i < resourceIdFiles.size(); i += 2) {
            String resourcePrefix = resourceIdFiles.get(i);
            String publicXml = resourceIdFiles.get(i + 1);
            File publicXmlFile = new File(publicXml);
            if (!publicXmlFile.exists()) {
                System.err.println(String.format("Can't find file: %s", publicXmlFile));
                System.exit(-1);
            }
            resourceFiles.put(resourcePrefix, publicXmlFile);
        }
        try {
            options.loadResourceIds(resourceFiles);
        } catch (IOException ex) {
            System.err.println("Error while loading resource files:");
            ex.printStackTrace(System.err);
            System.exit(-1);
        } catch (SAXException ex) {
            System.err.println("Error while loading resource files:");
            ex.printStackTrace(System.err);
            System.exit(-1);
        }
    }
    options.parameterRegisters = parameterRegisters;
    options.localsDirective = localsDirective;
    options.sequentialLabels = sequentialLabels;
    options.debugInfo = debugInfo;
    options.codeOffsets = codeOffsets;
    options.accessorComments = accessorComments;
    options.implicitReferences = implicitReferences;
    options.normalizeVirtualMethods = normalizeVirtualMethods;
    options.registerInfo = 0;
    for (String registerInfoType : registerInfoTypes) {
        if (registerInfoType.equalsIgnoreCase("ALL")) {
            options.registerInfo |= BaksmaliOptions.ALL;
        } else if (registerInfoType.equalsIgnoreCase("ALLPRE")) {
            options.registerInfo |= BaksmaliOptions.ALLPRE;
        } else if (registerInfoType.equalsIgnoreCase("ALLPOST")) {
            options.registerInfo |= BaksmaliOptions.ALLPOST;
        } else if (registerInfoType.equalsIgnoreCase("ARGS")) {
            options.registerInfo |= BaksmaliOptions.ARGS;
        } else if (registerInfoType.equalsIgnoreCase("DEST")) {
            options.registerInfo |= BaksmaliOptions.DEST;
        } else if (registerInfoType.equalsIgnoreCase("MERGE")) {
            options.registerInfo |= BaksmaliOptions.MERGE;
        } else if (registerInfoType.equalsIgnoreCase("FULLMERGE")) {
            options.registerInfo |= BaksmaliOptions.FULLMERGE;
        } else {
            System.err.println(String.format("Invalid register info type: %s", registerInfoType));
            usage();
            System.exit(-1);
        }
        if ((options.registerInfo & BaksmaliOptions.FULLMERGE) != 0) {
            options.registerInfo &= ~BaksmaliOptions.MERGE;
        }
    }
    if (accessorComments) {
        options.syntheticAccessorResolver = new SyntheticAccessorResolver(dexFile.getOpcodes(), dexFile.getClasses());
    }
    return options;
}
Also used : IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) SyntheticAccessorResolver(org.jf.dexlib2.util.SyntheticAccessorResolver)

Example 44 with DexFile

use of org.jf.dexlib2.iface.DexFile in project smali by JesusFreke.

the class AnalysisTest method runTest.

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(TextUtils.normalizeWhitespace(smaliContents), TextUtils.normalizeWhitespace((stringWriter.toString())));
    }
}
Also used : ClassPath(org.jf.dexlib2.analysis.ClassPath) ClassDef(org.jf.dexlib2.iface.ClassDef) StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) IndentingWriter(org.jf.util.IndentingWriter) ClassDefinition(org.jf.baksmali.Adaptors.ClassDefinition) DexFile(org.jf.dexlib2.iface.DexFile)

Example 45 with DexFile

use of org.jf.dexlib2.iface.DexFile in project smali by JesusFreke.

the class FieldGapOrderTest method testNewOrder.

@Test
public void testNewOrder() {
    DexFile dexFile = getInputDexFile("FieldGapOrder", new BaksmaliOptions());
    Assert.assertEquals(3, dexFile.getClasses().size());
    ClassPath classPath = new ClassPath(Lists.newArrayList(new DexClassProvider(dexFile)), false, 67);
    ClassProto classProto = (ClassProto) classPath.getClass("LGapOrder;");
    Assert.assertEquals("s", classProto.getFieldByOffset(10).getName());
    Assert.assertEquals("r1", classProto.getFieldByOffset(12).getName());
    Assert.assertEquals("r2", classProto.getFieldByOffset(16).getName());
    Assert.assertEquals("i", classProto.getFieldByOffset(20).getName());
    Assert.assertEquals("d", classProto.getFieldByOffset(24).getName());
}
Also used : ClassProto(org.jf.dexlib2.analysis.ClassProto) ClassPath(org.jf.dexlib2.analysis.ClassPath) DexClassProvider(org.jf.dexlib2.analysis.DexClassProvider) DexFile(org.jf.dexlib2.iface.DexFile) Test(org.junit.Test)

Aggregations

DexFile (org.jf.dexlib2.iface.DexFile)28 ClassDef (org.jf.dexlib2.iface.ClassDef)26 Test (org.junit.Test)17 Nonnull (javax.annotation.Nonnull)15 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)15 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)13 File (java.io.File)12 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)12 IOException (java.io.IOException)10 Method (org.jf.dexlib2.iface.Method)10 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)10 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)8 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)8 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)7 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)7 BuilderInstruction21t (org.jf.dexlib2.builder.instruction.BuilderInstruction21t)6 BuilderInstruction22c (org.jf.dexlib2.builder.instruction.BuilderInstruction22c)6 Instruction (org.jf.dexlib2.iface.instruction.Instruction)6 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)6 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)5