Search in sources :

Example 6 with BaksmaliWriter

use of org.jf.baksmali.formatter.BaksmaliWriter in project smali by JesusFreke.

the class BaksmaliTestUtils method getNormalizedSmali.

@Nonnull
public static String getNormalizedSmali(@Nonnull ClassDef classDef, @Nonnull BaksmaliOptions options, boolean stripComments) throws IOException {
    StringWriter stringWriter = new StringWriter();
    BaksmaliWriter writer = new BaksmaliWriter(stringWriter, options.implicitReferences ? classDef.getType() : null);
    ClassDefinition classDefinition = new ClassDefinition(options, classDef);
    classDefinition.writeTo(writer);
    writer.close();
    return normalizeSmali(stringWriter.toString(), stripComments);
}
Also used : StringWriter(java.io.StringWriter) BaksmaliWriter(org.jf.baksmali.formatter.BaksmaliWriter) ClassDefinition(org.jf.baksmali.Adaptors.ClassDefinition) Nonnull(javax.annotation.Nonnull)

Example 7 with BaksmaliWriter

use of org.jf.baksmali.formatter.BaksmaliWriter in project smali by JesusFreke.

the class InstructionMethodItemTest method testInvalidReference.

@Test
public void testInvalidReference() throws IOException {
    Instruction21c instruction = new Instruction21c() {

        @Override
        public int getRegisterA() {
            return 0;
        }

        @Nonnull
        @Override
        public Reference getReference() {
            return new BaseStringReference() {

                @Override
                public void validateReference() throws InvalidReferenceException {
                    throw new InvalidReferenceException("blahblahblah");
                }

                @Nonnull
                @Override
                public String getString() {
                    throw new RuntimeException("invalid reference");
                }
            };
        }

        @Override
        public int getReferenceType() {
            return ReferenceType.STRING;
        }

        @Override
        public Opcode getOpcode() {
            return Opcode.CONST_STRING;
        }

        @Override
        public int getCodeUnits() {
            return Format.Format21c.size / 2;
        }
    };
    MethodImplementation methodImplementation = new MethodImplementation() {

        @Override
        public int getRegisterCount() {
            return 1;
        }

        @Nonnull
        @Override
        public Iterable<? extends Instruction> getInstructions() {
            return ImmutableList.of(instruction);
        }

        @Nonnull
        @Override
        public List<? extends TryBlock<? extends ExceptionHandler>> getTryBlocks() {
            return ImmutableList.of();
        }

        @Nonnull
        @Override
        public Iterable<? extends DebugItem> getDebugItems() {
            return ImmutableList.of();
        }
    };
    Method method = new TestMethod(methodImplementation);
    ClassDefinition classDefinition = new ClassDefinition(new BaksmaliOptions(), new TestClassDef());
    MethodDefinition methodDefinition = new MethodDefinition(classDefinition, method, methodImplementation);
    methodDefinition.registerFormatter = new RegisterFormatter(new BaksmaliOptions(), 1, 0);
    InstructionMethodItem methodItem = new InstructionMethodItem<Instruction21c>(methodDefinition, 0, instruction);
    StringWriter stringWriter = new StringWriter();
    BaksmaliWriter writer = new BaksmaliWriter(stringWriter);
    methodItem.writeTo(writer);
    Assert.assertEquals("#Invalid reference\n#const-string v0, blahblahblah\nnop", stringWriter.toString());
}
Also used : Instruction21c(org.jf.dexlib2.iface.instruction.formats.Instruction21c) ClassDefinition(org.jf.baksmali.Adaptors.ClassDefinition) RegisterFormatter(org.jf.baksmali.Adaptors.RegisterFormatter) BaseStringReference(org.jf.dexlib2.base.reference.BaseStringReference) InstructionMethodItem(org.jf.baksmali.Adaptors.Format.InstructionMethodItem) StringWriter(java.io.StringWriter) MethodDefinition(org.jf.baksmali.Adaptors.MethodDefinition) BaksmaliWriter(org.jf.baksmali.formatter.BaksmaliWriter) Test(org.junit.Test)

Example 8 with BaksmaliWriter

use of org.jf.baksmali.formatter.BaksmaliWriter 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();
        BaksmaliWriter writer = new BaksmaliWriter(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())));
    }
}
Also used : ClassPath(org.jf.dexlib2.analysis.ClassPath) ClassDef(org.jf.dexlib2.iface.ClassDef) StringWriter(java.io.StringWriter) BaksmaliWriter(org.jf.baksmali.formatter.BaksmaliWriter) ArrayList(java.util.ArrayList) ClassDefinition(org.jf.baksmali.Adaptors.ClassDefinition) DexFile(org.jf.dexlib2.iface.DexFile)

Example 9 with BaksmaliWriter

use of org.jf.baksmali.formatter.BaksmaliWriter in project smali by JesusFreke.

the class ClassDefinition method writeDirectMethods.

private Set<String> writeDirectMethods(BaksmaliWriter writer) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenMethods = new HashSet<String>();
    Iterable<? extends Method> directMethods;
    if (classDef instanceof DexBackedClassDef) {
        directMethods = ((DexBackedClassDef) classDef).getDirectMethods(false);
    } else {
        directMethods = classDef.getDirectMethods();
    }
    for (Method method : directMethods) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# direct methods");
            wroteHeader = true;
        }
        writer.write('\n');
        // TODO: check for method validation errors
        String methodString = formatter.getShortMethodDescriptor(method);
        BaksmaliWriter methodWriter = writer;
        if (!writtenMethods.add(methodString)) {
            writer.write("# duplicate method ignored\n");
            methodWriter = getCommentingWriter(writer);
        }
        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl == null) {
            MethodDefinition.writeEmptyMethodTo(methodWriter, method, this);
        } else {
            MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
            methodDefinition.writeTo(methodWriter);
        }
    }
    return writtenMethods;
}
Also used : BaksmaliWriter(org.jf.baksmali.formatter.BaksmaliWriter) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) HashSet(java.util.HashSet)

Example 10 with BaksmaliWriter

use of org.jf.baksmali.formatter.BaksmaliWriter in project smali by JesusFreke.

the class ClassDefinition method writeStaticFields.

private Set<String> writeStaticFields(BaksmaliWriter 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;
        BaksmaliWriter fieldWriter = writer;
        String fieldString = formatter.getShortFieldDescriptor(field);
        if (!writtenFields.add(fieldString)) {
            writer.write("# duplicate field ignored\n");
            fieldWriter = getCommentingWriter(writer);
            System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
            setInStaticConstructor = false;
        } else {
            setInStaticConstructor = fieldsSetInStaticConstructor.contains(fieldString);
        }
        FieldDefinition.writeTo(fieldWriter, field, setInStaticConstructor);
    }
    return writtenFields;
}
Also used : BaksmaliWriter(org.jf.baksmali.formatter.BaksmaliWriter) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) HashSet(java.util.HashSet)

Aggregations

BaksmaliWriter (org.jf.baksmali.formatter.BaksmaliWriter)11 ClassDefinition (org.jf.baksmali.Adaptors.ClassDefinition)5 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)5 StringWriter (java.io.StringWriter)4 HashSet (java.util.HashSet)4 IOException (java.io.IOException)2 DebugMethodItem (org.jf.baksmali.Adaptors.Debug.DebugMethodItem)2 AnalyzedInstruction (org.jf.dexlib2.analysis.AnalyzedInstruction)2 DexFile (org.jf.dexlib2.iface.DexFile)2 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 Nonnull (javax.annotation.Nonnull)1 InstructionMethodItem (org.jf.baksmali.Adaptors.Format.InstructionMethodItem)1 MethodDefinition (org.jf.baksmali.Adaptors.MethodDefinition)1 RegisterFormatter (org.jf.baksmali.Adaptors.RegisterFormatter)1 BaksmaliOptions (org.jf.baksmali.BaksmaliOptions)1 AnalysisException (org.jf.dexlib2.analysis.AnalysisException)1 ClassPath (org.jf.dexlib2.analysis.ClassPath)1 MethodAnalyzer (org.jf.dexlib2.analysis.MethodAnalyzer)1 BaseStringReference (org.jf.dexlib2.base.reference.BaseStringReference)1