Package org.jf.dexlib2

Examples of org.jf.dexlib2.Opcode


        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

            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

    @Nonnull
    public Iterable<? extends DexBackedField> getStaticFields(final boolean skipDuplicates) {
        if (staticFieldCount > 0) {
            DexReader reader = dexFile.readerAt(staticFieldsOffset);

            final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
            final int staticInitialValuesOffset =
                    dexFile.readSmallUint(classDefOffset + ClassDefItem.STATIC_VALUES_OFFSET);
            final int fieldsStartOffset = reader.getOffset();

            return new Iterable<DexBackedField>() {
                @Nonnull
                @Override
                public Iterator<DexBackedField> iterator() {
                    final AnnotationsDirectory.AnnotationIterator annotationIterator =
                            annotationsDirectory.getFieldAnnotationIterator();
                    final StaticInitialValueIterator staticInitialValueIterator =
                            StaticInitialValueIterator.newOrEmpty(dexFile, staticInitialValuesOffset);

                    return new VariableSizeLookaheadIterator<DexBackedField>(dexFile, fieldsStartOffset) {
                        private int count;
View Full Code Here

    @Nonnull
    public Iterable<? extends DexBackedField> getInstanceFields(final boolean skipDuplicates) {
        if (instanceFieldCount > 0) {
            DexReader reader = dexFile.readerAt(getInstanceFieldsOffset());

            final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
            final int fieldsStartOffset = reader.getOffset();

            return new Iterable<DexBackedField>() {
                @Nonnull
                @Override
                public Iterator<DexBackedField> iterator() {
                    final AnnotationsDirectory.AnnotationIterator annotationIterator =
                            annotationsDirectory.getFieldAnnotationIterator();

                    return new VariableSizeLookaheadIterator<DexBackedField>(dexFile, fieldsStartOffset) {
                        private int count;
                        @Nullable private FieldReference previousField;
                        private int previousIndex;
View Full Code Here

TOP

Related Classes of org.jf.dexlib2.Opcode

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.