Search in sources :

Example 1 with RawDexFile

use of org.jf.dexlib2.dexbacked.raw.RawDexFile in project smali by JesusFreke.

the class RollbackTest method testRollback.

@Test
public void testRollback() throws IOException {
    ClassDef class1 = new ImmutableClassDef("Lcls1;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, Lists.newArrayList(new ImmutableAnnotation(AnnotationVisibility.RUNTIME, "Lannotation;", null)), Lists.<Field>newArrayList(new ImmutableField("Lcls1;", "field1", "I", AccessFlags.PUBLIC.getValue(), null, null)), Lists.<Method>newArrayList(new ImmutableMethod("Lcls1", "method1", Lists.<MethodParameter>newArrayList(new ImmutableMethodParameter("L", null, null)), "V", AccessFlags.PUBLIC.getValue(), null, null)));
    ClassDef class2 = new ImmutableClassDef("Lcls2;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, Lists.newArrayList(new ImmutableAnnotation(AnnotationVisibility.RUNTIME, "Lannotation2;", null)), Lists.<Field>newArrayList(new ImmutableField("Lcls2;", "field2", "D", AccessFlags.PUBLIC.getValue(), null, null)), Lists.<Method>newArrayList(new ImmutableMethod("Lcls2;", "method2", Lists.<MethodParameter>newArrayList(new ImmutableMethodParameter("D", null, null)), "V", AccessFlags.PUBLIC.getValue(), null, null)));
    RawDexFile dexFile1;
    {
        MemoryDataStore dataStore = new MemoryDataStore();
        DexPool dexPool = new DexPool(Opcodes.getDefault());
        dexPool.internClass(class1);
        dexPool.mark();
        dexPool.internClass(class2);
        dexPool.reset();
        dexPool.writeTo(dataStore);
        dexFile1 = new RawDexFile(Opcodes.getDefault(), dataStore.getData());
    }
    RawDexFile dexFile2;
    {
        MemoryDataStore dataStore = new MemoryDataStore();
        DexPool dexPool = new DexPool(Opcodes.getDefault());
        dexPool.internClass(class1);
        dexPool.writeTo(dataStore);
        dexFile2 = new RawDexFile(Opcodes.getDefault(), dataStore.getData());
    }
    List<MapItem> mapItems1 = dexFile1.getMapItems();
    List<MapItem> mapItems2 = dexFile2.getMapItems();
    for (int i = 0; i < mapItems1.size(); i++) {
        Assert.assertEquals(mapItems1.get(i).getType(), mapItems2.get(i).getType());
        Assert.assertEquals(mapItems1.get(i).getItemCount(), mapItems2.get(i).getItemCount());
    }
}
Also used : DexPool(org.jf.dexlib2.writer.pool.DexPool) ClassDef(org.jf.dexlib2.iface.ClassDef) RawDexFile(org.jf.dexlib2.dexbacked.raw.RawDexFile) MemoryDataStore(org.jf.dexlib2.writer.io.MemoryDataStore) MapItem(org.jf.dexlib2.dexbacked.raw.MapItem) Test(org.junit.Test)

Example 2 with RawDexFile

use of org.jf.dexlib2.dexbacked.raw.RawDexFile in project smali by JesusFreke.

the class DumpCommand method dump.

/**
     * Writes an annotated hex dump of the given dex file to output.
     *
     * @param dexFile The dex file to dump
     * @param output An OutputStream to write the annotated hex dump to. The caller is responsible for closing this
     *               when needed.
     *
     * @throws IOException
     */
public static void dump(@Nonnull DexBackedDexFile dexFile, @Nonnull OutputStream output) throws IOException {
    Writer writer = new BufferedWriter(new OutputStreamWriter(output));
    int consoleWidth = ConsoleUtil.getConsoleWidth();
    if (consoleWidth <= 0) {
        consoleWidth = 120;
    }
    RawDexFile rawDexFile = new RawDexFile(dexFile.getOpcodes(), dexFile);
    DexAnnotator annotator = new DexAnnotator(rawDexFile, consoleWidth);
    annotator.writeAnnotations(writer);
}
Also used : DexAnnotator(org.jf.dexlib2.dexbacked.raw.util.DexAnnotator) RawDexFile(org.jf.dexlib2.dexbacked.raw.RawDexFile)

Example 3 with RawDexFile

use of org.jf.dexlib2.dexbacked.raw.RawDexFile in project smali by JesusFreke.

the class ClassDataItem method makeAnnotator.

@Nonnull
public static SectionAnnotator makeAnnotator(@Nonnull DexAnnotator annotator, @Nonnull MapItem mapItem) {
    return new SectionAnnotator(annotator, mapItem) {

        private SectionAnnotator codeItemAnnotator = null;

        @Override
        public void annotateSection(@Nonnull AnnotatedBytes out) {
            codeItemAnnotator = annotator.getAnnotator(ItemType.CODE_ITEM);
            super.annotateSection(out);
        }

        @Nonnull
        @Override
        public String getItemName() {
            return "class_data_item";
        }

        @Override
        protected void annotateItem(@Nonnull AnnotatedBytes out, int itemIndex, @Nullable String itemIdentity) {
            DexReader reader = dexFile.readerAt(out.getCursor());
            int staticFieldsSize = reader.readSmallUleb128();
            out.annotateTo(reader.getOffset(), "static_fields_size = %d", staticFieldsSize);
            int instanceFieldsSize = reader.readSmallUleb128();
            out.annotateTo(reader.getOffset(), "instance_fields_size = %d", instanceFieldsSize);
            int directMethodsSize = reader.readSmallUleb128();
            out.annotateTo(reader.getOffset(), "direct_methods_size = %d", directMethodsSize);
            int virtualMethodsSize = reader.readSmallUleb128();
            out.annotateTo(reader.getOffset(), "virtual_methods_size = %d", virtualMethodsSize);
            int previousIndex = 0;
            if (staticFieldsSize > 0) {
                out.annotate(0, "static_fields:");
                out.indent();
                for (int i = 0; i < staticFieldsSize; i++) {
                    out.annotate(0, "static_field[%d]", i);
                    out.indent();
                    previousIndex = annotateEncodedField(out, dexFile, reader, previousIndex);
                    out.deindent();
                }
                out.deindent();
            }
            if (instanceFieldsSize > 0) {
                out.annotate(0, "instance_fields:");
                out.indent();
                previousIndex = 0;
                for (int i = 0; i < instanceFieldsSize; i++) {
                    out.annotate(0, "instance_field[%d]", i);
                    out.indent();
                    previousIndex = annotateEncodedField(out, dexFile, reader, previousIndex);
                    out.deindent();
                }
                out.deindent();
            }
            if (directMethodsSize > 0) {
                out.annotate(0, "direct_methods:");
                out.indent();
                previousIndex = 0;
                for (int i = 0; i < directMethodsSize; i++) {
                    out.annotate(0, "direct_method[%d]", i);
                    out.indent();
                    previousIndex = annotateEncodedMethod(out, dexFile, reader, previousIndex);
                    out.deindent();
                }
                out.deindent();
            }
            if (virtualMethodsSize > 0) {
                out.annotate(0, "virtual_methods:");
                out.indent();
                previousIndex = 0;
                for (int i = 0; i < virtualMethodsSize; i++) {
                    out.annotate(0, "virtual_method[%d]", i);
                    out.indent();
                    previousIndex = annotateEncodedMethod(out, dexFile, reader, previousIndex);
                    out.deindent();
                }
                out.deindent();
            }
        }

        private int annotateEncodedField(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, @Nonnull DexReader reader, int previousIndex) {
            // large values may be used for the index delta, which cause the cumulative index to overflow upon
            // addition, effectively allowing out of order entries.
            int indexDelta = reader.readLargeUleb128();
            int fieldIndex = previousIndex + indexDelta;
            out.annotateTo(reader.getOffset(), "field_idx_diff = %d: %s", indexDelta, FieldIdItem.getReferenceAnnotation(dexFile, fieldIndex));
            int accessFlags = reader.readSmallUleb128();
            out.annotateTo(reader.getOffset(), "access_flags = 0x%x: %s", accessFlags, Joiner.on('|').join(AccessFlags.getAccessFlagsForField(accessFlags)));
            return fieldIndex;
        }

        private int annotateEncodedMethod(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, @Nonnull DexReader reader, int previousIndex) {
            // large values may be used for the index delta, which cause the cumulative index to overflow upon
            // addition, effectively allowing out of order entries.
            int indexDelta = reader.readLargeUleb128();
            int methodIndex = previousIndex + indexDelta;
            out.annotateTo(reader.getOffset(), "method_idx_diff = %d: %s", indexDelta, MethodIdItem.getReferenceAnnotation(dexFile, methodIndex));
            int accessFlags = reader.readSmallUleb128();
            out.annotateTo(reader.getOffset(), "access_flags = 0x%x: %s", accessFlags, Joiner.on('|').join(AccessFlags.getAccessFlagsForMethod(accessFlags)));
            int codeOffset = reader.readSmallUleb128();
            if (codeOffset == 0) {
                out.annotateTo(reader.getOffset(), "code_off = code_item[NO_OFFSET]");
            } else {
                out.annotateTo(reader.getOffset(), "code_off = code_item[0x%x]", codeOffset);
                addCodeItemIdentity(codeOffset, MethodIdItem.asString(dexFile, methodIndex));
            }
            return methodIndex;
        }

        private void addCodeItemIdentity(int codeItemOffset, String methodString) {
            if (codeItemAnnotator != null) {
                codeItemAnnotator.setItemIdentity(codeItemOffset, methodString);
            }
        }
    };
}
Also used : DexReader(org.jf.dexlib2.dexbacked.DexReader) Nonnull(javax.annotation.Nonnull) AnnotatedBytes(org.jf.dexlib2.util.AnnotatedBytes) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull)

Aggregations

RawDexFile (org.jf.dexlib2.dexbacked.raw.RawDexFile)2 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 DexReader (org.jf.dexlib2.dexbacked.DexReader)1 MapItem (org.jf.dexlib2.dexbacked.raw.MapItem)1 DexAnnotator (org.jf.dexlib2.dexbacked.raw.util.DexAnnotator)1 ClassDef (org.jf.dexlib2.iface.ClassDef)1 AnnotatedBytes (org.jf.dexlib2.util.AnnotatedBytes)1 MemoryDataStore (org.jf.dexlib2.writer.io.MemoryDataStore)1 DexPool (org.jf.dexlib2.writer.pool.DexPool)1 Test (org.junit.Test)1