Package org.jf.dexlib2.dexbacked

Examples of org.jf.dexlib2.dexbacked.DexReader


        builder.addInstruction(new BuilderInstruction10t(Opcode.GOTO, firstGotoTarget));

        builder.addLineNumber(1);

        for (int i=0; i<250; i++) {
            builder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
        }

        builder.addLabel("tryStart");

        builder.addLineNumber(2);

        for (int i=0; i<250; i++) {
            builder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
        }

        builder.addLineNumber(3);

        Label secondGotoTarget = builder.getLabel("secondGotoTarget");
        builder.addInstruction(new BuilderInstruction10t(Opcode.GOTO, secondGotoTarget));


        builder.addLineNumber(4);
        builder.addLabel("handler");

        for (int i=0; i<500; i++) {
            builder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
        }

        builder.addLineNumber(5);

        builder.addLabel("tryEnd");

        builder.addLabel("firstGotoTarget");
        builder.addLabel("secondGotoTarget");
        builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));

        Label tryStart = builder.getLabel("tryStart");
        Label tryEnd = builder.getLabel("tryEnd");
        Label handler = builder.getLabel("handler");
View Full Code Here


        Label gotoTarget = builder.getLabel("gotoTarget");
        builder.addInstruction(new BuilderInstruction10t(Opcode.GOTO, gotoTarget));

        for (int i=0; i<500; i++) {
            builder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
        }

        builder.addLabel("gotoTarget");
        builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));

        MethodImplementation impl = builder.getMethodImplementation();

        List<? extends Instruction> instructions = Lists.newArrayList(impl.getInstructions());
        Assert.assertEquals(502, instructions.size());
View Full Code Here

        Label gotoTarget = builder.getLabel("gotoTarget");
        builder.addInstruction(new BuilderInstruction10t(Opcode.GOTO, gotoTarget));

        for (int i=0; i<70000; i++) {
            builder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
        }

        builder.addLabel("gotoTarget");
        builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));

        MethodImplementation impl = builder.getMethodImplementation();

        List<? extends Instruction> instructions = Lists.newArrayList(impl.getInstructions());
        Assert.assertEquals(70002, instructions.size());
View Full Code Here

        Label gotoTarget = builder.getLabel("gotoTarget");
        builder.addInstruction(new BuilderInstruction20t(Opcode.GOTO_16, gotoTarget));

        for (int i=0; i<70000; i++) {
            builder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
        }

        builder.addLabel("gotoTarget");
        builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));

        MethodImplementation impl = builder.getMethodImplementation();

        List<? extends Instruction> instructions = Lists.newArrayList(impl.getInstructions());
        Assert.assertEquals(70002, instructions.size());
View Full Code Here

            Instruction instruction = instructions.get(i);

            if (instruction.getOpcode() == Opcode.CONST_STRING) {
                if (stringSection.getItemIndex(
                        (StringRef)((ReferenceInstruction)instruction).getReference()) >= 65536) {
                    methodImplementation.replaceInstruction(i, new BuilderInstruction31c(Opcode.CONST_STRING_JUMBO,
                            ((OneRegisterInstruction)instruction).getRegisterA(),
                            ((ReferenceInstruction)instruction).getReference()));
                }
            }
        }
View Full Code Here

            return new FixedSizeSet<DexBackedAnnotation>() {
                @Nonnull
                @Override
                public DexBackedAnnotation readItem(int index) {
                    int annotationOffset = dexFile.readSmallUint(annotationSetOffset + 4 + (4*index));
                    return new DexBackedAnnotation(dexFile, annotationOffset);
                }

                @Override public int size() { return size; }
            };
        }
View Full Code Here

            } else if (fileLength > Integer.MAX_VALUE) {
                throw new ExceptionWithContext("The " + dexEntry + " file in %s is too large to read in", dexFile.getName());
            }
            byte[] dexBytes = new byte[(int)fileLength];
            ByteStreams.readFully(zipFile.getInputStream(zipEntry), dexBytes);
            return new DexBackedDexFile(opcodes, dexBytes);
        } catch (IOException ex) {
            // don't continue on if we know it's a zip file
            if (isZipFile) {
                throw ex;
            }
View Full Code Here

            //TODO: does dalvik allow references to invalid registers?
            final LocalInfo[] locals = new LocalInfo[registerCount];
            Arrays.fill(locals, EMPTY_LOCAL_INFO);

            DexBackedMethod method = methodImpl.method;

            // Create a MethodParameter iterator that uses our DexReader instance to read the parameter names.
            // After we have finished iterating over the parameters, reader will "point to" the beginning of the
            // debug instructions
            final Iterator<? extends MethodParameter> parameterIterator =
                    new ParameterIterator(method.getParameterTypes(),
                            method.getParameterAnnotations(),
                            getParameterNames(reader));

            // first, we grab all the parameters and temporarily store them at the beginning of locals,
            // disregarding any wide types
            int parameterIndex = 0;
View Full Code Here

        };

        @Nonnull
        @Override
        public Iterator<DebugItem> iterator() {
            DexReader reader = dexFile.readerAt(debugInfoOffset);
            final int lineNumberStart = reader.readBigUleb128();
            int registerCount = methodImpl.getRegisterCount();

            //TODO: does dalvik allow references to invalid registers?
            final LocalInfo[] locals = new LocalInfo[registerCount];
            Arrays.fill(locals, EMPTY_LOCAL_INFO);

            DexBackedMethod method = methodImpl.method;

            // Create a MethodParameter iterator that uses our DexReader instance to read the parameter names.
            // After we have finished iterating over the parameters, reader will "point to" the beginning of the
            // debug instructions
            final Iterator<? extends MethodParameter> parameterIterator =
                    new ParameterIterator(method.getParameterTypes(),
                            method.getParameterAnnotations(),
                            getParameterNames(reader));

            // first, we grab all the parameters and temporarily store them at the beginning of locals,
            // disregarding any wide types
            int parameterIndex = 0;
            if (!AccessFlags.STATIC.isSet(methodImpl.method.getAccessFlags())) {
                // add the local info for the "this" parameter
                locals[parameterIndex++] = new LocalInfo() {
                    @Override public String getName() { return "this"; }
                    @Override public String getType() { return methodImpl.method.getDefiningClass(); }
                    @Override public String getSignature() { return null; }
                };
            }
            while (parameterIterator.hasNext()) {
                locals[parameterIndex++] = parameterIterator.next();
            }

            if (parameterIndex < registerCount) {
                // now, we push the parameter locals back to their appropriate register, starting from the end
                int localIndex = registerCount-1;
                while(--parameterIndex > -1) {
                    LocalInfo currentLocal = locals[parameterIndex];
                    String type = currentLocal.getType();
                    if (type != null && (type.equals("J") || type.equals("D"))) {
                        localIndex--;
                        if (localIndex == parameterIndex) {
                            // there's no more room to push, the remaining registers are already in the correct place
                            break;
                        }
                    }
                    locals[localIndex] = currentLocal;
                    locals[parameterIndex] = EMPTY_LOCAL_INFO;
                    localIndex--;
                }
            }

            return new VariableSizeLookaheadIterator<DebugItem>(dexFile, reader.getOffset()) {
                private int codeAddress = 0;
                private int lineNumber = lineNumberStart;

                @Nullable
                protected DebugItem readNextItem(@Nonnull DexReader reader) {
                    while (true) {
                        int next = reader.readUbyte();
                        switch (next) {
                            case DebugItemType.END_SEQUENCE: {
                                return null;
                            }
                            case DebugItemType.ADVANCE_PC: {
                                int addressDiff = reader.readSmallUleb128();
                                codeAddress += addressDiff;
                                continue;
                            }
                            case DebugItemType.ADVANCE_LINE: {
                                int lineDiff = reader.readSleb128();
                                lineNumber += lineDiff;
                                continue;
                            }
                            case DebugItemType.START_LOCAL: {
                                int register = reader.readSmallUleb128();
                                String name = dexFile.getOptionalString(reader.readSmallUleb128() - 1);
                                String type = dexFile.getOptionalType(reader.readSmallUleb128() - 1);
                                ImmutableStartLocal startLocal =
                                        new ImmutableStartLocal(codeAddress, register, name, type, null);
                                locals[register] = startLocal;
                                return startLocal;
                            }
                            case DebugItemType.START_LOCAL_EXTENDED: {
                                int register = reader.readSmallUleb128();
                                String name = dexFile.getOptionalString(reader.readSmallUleb128() - 1);
                                String type = dexFile.getOptionalType(reader.readSmallUleb128() - 1);
                                String signature = dexFile.getOptionalString(reader.readSmallUleb128() - 1);
                                ImmutableStartLocal startLocal =
                                        new ImmutableStartLocal(codeAddress, register, name, type, signature);
                                locals[register] = startLocal;
                                return startLocal;
                            }
                            case DebugItemType.END_LOCAL: {
                                int register = reader.readSmallUleb128();
                                LocalInfo localInfo = locals[register];
                                boolean replaceLocalInTable = true;
                                if (localInfo instanceof EndLocal) {
                                    localInfo = EMPTY_LOCAL_INFO;
                                    // don't replace the local info in locals. The new EndLocal won't have any info at all,
                                    // and we dont want to wipe out what's there, so that it is available for a subsequent
                                    // RestartLocal
                                    replaceLocalInTable = false;
                                }
                                ImmutableEndLocal endLocal =
                                        new ImmutableEndLocal(codeAddress, register, localInfo.getName(),
                                                localInfo.getType(), localInfo.getSignature());
                                if (replaceLocalInTable) {
                                    locals[register] = endLocal;
                                }
                                return endLocal;
                            }
                            case DebugItemType.RESTART_LOCAL: {
                                int register = reader.readSmallUleb128();
                                LocalInfo localInfo = locals[register];
                                ImmutableRestartLocal restartLocal =
                                        new ImmutableRestartLocal(codeAddress, register, localInfo.getName(),
                                                localInfo.getType(), localInfo.getSignature());
                                locals[register] = restartLocal;
                                return restartLocal;
                            }
                            case DebugItemType.PROLOGUE_END: {
                                return new ImmutablePrologueEnd(codeAddress);
                            }
                            case DebugItemType.EPILOGUE_BEGIN: {
                                return new ImmutableEpilogueBegin(codeAddress);
                            }
                            case DebugItemType.SET_SOURCE_FILE: {
                                String sourceFile = dexFile.getOptionalString(reader.readSmallUleb128() - 1);
                                return new ImmutableSetSourceFile(codeAddress, sourceFile);
                            }
                            default: {
                                int adjusted = next - 0x0A;
                                codeAddress += adjusted / 15;
View Full Code Here

            }

            @Override
            public void annotateItem(@Nonnull AnnotatedBytes out, int itemIndex, @Nullable String itemIdentity) {
                try {
                    DexReader reader = dexFile.readerAt(out.getCursor());

                    int registers = reader.readUshort();
                    out.annotate(2, "registers_size = %d", registers);

                    int inSize = reader.readUshort();
                    out.annotate(2, "ins_size = %d", inSize);

                    int outSize = reader.readUshort();
                    out.annotate(2, "outs_size = %d", outSize);

                    int triesCount = reader.readUshort();
                    out.annotate(2, "tries_size = %d", triesCount);

                    int debugInfoOffset = reader.readSmallUint();
                    out.annotate(4, "debug_info_off = 0x%x", debugInfoOffset);

                    if (debugInfoOffset != 0) {
                        addDebugInfoIdentity(debugInfoOffset, itemIdentity);
                    }

                    int instructionSize = reader.readSmallUint();
                    out.annotate(4, "insns_size = 0x%x", instructionSize);

                    out.annotate(0, "instructions:");
                    out.indent();

                    out.setLimit(out.getCursor(), out.getCursor() + instructionSize * 2);

                    int end = reader.getOffset() + instructionSize*2;
                    try {
                        while (reader.getOffset() < end) {
                            Instruction instruction = DexBackedInstruction.readFrom(reader);

                            // if we read past the end of the instruction list
                            if (reader.getOffset() > end) {
                                out.annotateTo(end, "truncated instruction");
                                reader.setOffset(end);
                            } else {
                                switch (instruction.getOpcode().format) {
                                    case Format10x:
                                        annotateInstruction10x(out, instruction);
                                        break;
                                    case Format35c:
                                        annotateInstruction35c(out, (Instruction35c)instruction);
                                        break;
                                    case Format3rc:
                                        annotateInstruction3rc(out, (Instruction3rc)instruction);
                                        break;
                                    case ArrayPayload:
                                        annotateArrayPayload(out, (ArrayPayload)instruction);
                                        break;
                                    case PackedSwitchPayload:
                                        annotatePackedSwitchPayload(out, (PackedSwitchPayload)instruction);
                                        break;
                                    case SparseSwitchPayload:
                                        annotateSparseSwitchPayload(out, (SparseSwitchPayload)instruction);
                                        break;
                                    default:
                                        annotateDefaultInstruction(out, instruction);
                                        break;
                                }
                            }

                            assert reader.getOffset() == out.getCursor();
                        }
                    } catch (ExceptionWithContext ex) {
                        ex.printStackTrace(System.err);
                        out.annotate(0, "annotation error: %s", ex.getMessage());
                        out.moveTo(end);
                        reader.setOffset(end);
                    } finally {
                        out.clearLimit();
                        out.deindent();
                    }

                    if (triesCount > 0) {
                        if ((reader.getOffset() % 4) != 0) {
                            reader.readUshort();
                            out.annotate(2, "padding");
                        }

                        out.annotate(0, "try_items:");
                        out.indent();
                        try {
                            for (int i=0; i<triesCount; i++) {
                                out.annotate(0, "try_item[%d]:", i);
                                out.indent();
                                try {
                                    int startAddr = reader.readSmallUint();
                                    out.annotate(4, "start_addr = 0x%x", startAddr);

                                    int instructionCount = reader.readUshort();
                                    out.annotate(2, "insn_count = 0x%x", instructionCount);

                                    int handlerOffset = reader.readUshort();
                                    out.annotate(2, "handler_off = 0x%x", handlerOffset);
                                } finally {
                                    out.deindent();
                                }
                            }
                        } finally {
                            out.deindent();
                        }

                        int handlerListCount = reader.readSmallUleb128();
                        out.annotate(0, "encoded_catch_handler_list:");
                        out.annotateTo(reader.getOffset(), "size = %d", handlerListCount);
                        out.indent();
                        try {
                            for (int i=0; i<handlerListCount; i++) {
                                out.annotate(0, "encoded_catch_handler[%d]", i);
                                out.indent();
                                try {
                                    int handlerCount = reader.readSleb128();
                                    out.annotateTo(reader.getOffset(), "size = %d", handlerCount);
                                    boolean hasCatchAll = handlerCount <= 0;
                                    handlerCount = Math.abs(handlerCount);
                                    if (handlerCount != 0) {
                                        out.annotate(0, "handlers:");
                                        out.indent();
                                        try {
                                            for (int j=0; j<handlerCount; j++) {
                                                out.annotate(0, "encoded_type_addr_pair[%d]", i);
                                                out.indent();
                                                try {
                                                    int typeIndex = reader.readSmallUleb128();
                                                    out.annotateTo(reader.getOffset(), TypeIdItem.getReferenceAnnotation(dexFile, typeIndex));

                                                    int handlerAddress = reader.readSmallUleb128();
                                                    out.annotateTo(reader.getOffset(), "addr = 0x%x", handlerAddress);
                                                } finally {
                                                    out.deindent();
                                                }
                                            }
                                        } finally {
                                            out.deindent();
                                        }
                                    }
                                    if (hasCatchAll) {
                                        int catchAllAddress = reader.readSmallUleb128();
                                        out.annotateTo(reader.getOffset(), "catch_all_addr = 0x%x", catchAllAddress);
                                    }
                                } finally {
                                    out.deindent();
                                }
                            }
View Full Code Here

TOP

Related Classes of org.jf.dexlib2.dexbacked.DexReader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.