Search in sources :

Example 56 with ClassDef

use of org.jf.dexlib2.iface.ClassDef 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 57 with ClassDef

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

the class ListClassesCommand method run.

@Override
public void run() {
    if (help || inputList == null || inputList.isEmpty()) {
        usage();
        return;
    }
    if (inputList.size() > 1) {
        System.err.println("Too many files specified");
        usage();
        return;
    }
    String input = inputList.get(0);
    loadDexFile(input);
    for (ClassDef classDef : dexFile.getClasses()) {
        System.out.println(classDef.getType());
    }
}
Also used : ClassDef(org.jf.dexlib2.iface.ClassDef)

Example 58 with ClassDef

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

the class ListFieldOffsetsCommand method run.

@Override
public void run() {
    if (help || inputList == null || inputList.isEmpty()) {
        usage();
        return;
    }
    if (inputList.size() > 1) {
        System.err.println("Too many files specified");
        usage();
        return;
    }
    String input = inputList.get(0);
    loadDexFile(input);
    BaksmaliOptions options = getOptions();
    try {
        for (ClassDef classDef : dexFile.getClasses()) {
            ClassProto classProto = (ClassProto) options.classPath.getClass(classDef);
            SparseArray<FieldReference> fields = classProto.getInstanceFields();
            String className = "Class " + classDef.getType() + " : " + fields.size() + " instance fields\n";
            System.out.write(className.getBytes());
            for (int i = 0; i < fields.size(); i++) {
                String field = fields.keyAt(i) + ":" + fields.valueAt(i).getType() + " " + fields.valueAt(i).getName() + "\n";
                System.out.write(field.getBytes());
            }
            System.out.write("\n".getBytes());
        }
        System.out.close();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : ClassProto(org.jf.dexlib2.analysis.ClassProto) ClassDef(org.jf.dexlib2.iface.ClassDef) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) IOException(java.io.IOException)

Example 59 with ClassDef

use of org.jf.dexlib2.iface.ClassDef 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 60 with ClassDef

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

the class ClassDefinition method writeStaticFields.

private Set<String> writeStaticFields(IndentingWriter writer) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();
    Iterable<? extends Field> staticFields;
    if (classDef instanceof DexBackedClassDef) {
        staticFields = ((DexBackedClassDef) classDef).getStaticFields(false);
    } else {
        staticFields = classDef.getStaticFields();
    }
    for (Field field : staticFields) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# static fields");
            wroteHeader = true;
        }
        writer.write('\n');
        boolean setInStaticConstructor;
        IndentingWriter fieldWriter = writer;
        String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
        if (!writtenFields.add(fieldString)) {
            writer.write("# duplicate field ignored\n");
            fieldWriter = new CommentingIndentingWriter(writer);
            System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
            setInStaticConstructor = false;
        } else {
            setInStaticConstructor = fieldsSetInStaticConstructor.contains(fieldString);
        }
        FieldDefinition.writeTo(options, fieldWriter, field, setInStaticConstructor);
    }
    return writtenFields;
}
Also used : IndentingWriter(org.jf.util.IndentingWriter) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef)

Aggregations

ClassDef (org.jf.dexlib2.iface.ClassDef)47 DexFile (org.jf.dexlib2.iface.DexFile)23 Test (org.junit.Test)21 Method (org.jf.dexlib2.iface.Method)18 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)15 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)14 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)14 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)13 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)12 IndentingWriter (org.jf.util.IndentingWriter)11 File (java.io.File)10 IOException (java.io.IOException)10 HashSet (java.util.HashSet)8 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)8 Instruction (org.jf.dexlib2.iface.instruction.Instruction)8 Nonnull (javax.annotation.Nonnull)7 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)7 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)7 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)7 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)6