Java Code Examples for org.jf.dexlib2.builder.MutableMethodImplementation#getInstructions()

The following examples show how to use org.jf.dexlib2.builder.MutableMethodImplementation#getInstructions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DexWriter.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
private void fixInstructions(@Nonnull MutableMethodImplementation methodImplementation) {
    List<? extends Instruction> instructions = methodImplementation.getInstructions();

    for (int i=0; i<instructions.size(); i++) {
        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()));
            }
        }
    }
}
 
Example 2
Source File: DexWriter.java    From zjdroid with Apache License 2.0 6 votes vote down vote up
private void fixInstructions(@Nonnull MutableMethodImplementation methodImplementation) {
    List<? extends Instruction> instructions = methodImplementation.getInstructions();

    for (int i=0; i<instructions.size(); i++) {
        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()));
            }
        }
    }
}
 
Example 3
Source File: DexWriter.java    From HeyGirl with Apache License 2.0 6 votes vote down vote up
private void fixInstructions(@Nonnull MutableMethodImplementation methodImplementation) {
    List<? extends Instruction> instructions = methodImplementation.getInstructions();

    for (int i=0; i<instructions.size(); i++) {
        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()));
            }
        }
    }
}
 
Example 4
Source File: DexWriter.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
private void fixInstructions(@Nonnull MutableMethodImplementation methodImplementation) {
    List<? extends Instruction> instructions = methodImplementation.getInstructions();

    for (int i=0; i<instructions.size(); i++) {
        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()));
            }
        }
    }
}
 
Example 5
Source File: PatchMethodTool.java    From atlas with Apache License 2.0 5 votes vote down vote up
private static MethodImplementation modifyMethodAndFix(@Nonnull MethodImplementation implementation, Method method) {
    MutableMethodImplementation mutableImplementation = new MutableMethodImplementation(implementation);
    System.out.println(mutableImplementation.getRegisterCount());
    List<BuilderInstruction> instructions = mutableImplementation.getInstructions();
    mutableImplementation.addInstruction(0,
            new BuilderInstruction21c(Opcode.CONST_STRING, 0,
                    new ImmutableStringReference("AndFix:" + method.getDefiningClass().replace("/", "."))));
    mutableImplementation.addInstruction(1,
            new BuilderInstruction35c(Opcode.INVOKE_STATIC, 1,
                    0, 0, 0, 0, 0,
                    new ImmutableMethodReference("Landroid/util/Log;", "e",
                            Lists.newArrayList("Ljava/lang/String;", "Ljava/lang/String;"), "I")));

    return mutableImplementation;
}
 
Example 6
Source File: PatchMethodTool.java    From atlas with Apache License 2.0 5 votes vote down vote up
private static MethodImplementation modifyMethodTpatch(@Nonnull MethodImplementation implementation, Method method) {
        MutableMethodImplementation mutableImplementation = new MutableMethodImplementation(implementation);
        System.out.println(mutableImplementation.getRegisterCount());
        List<BuilderInstruction> instructions = mutableImplementation.getInstructions();
        boolean isModified = false;
        for (int i = 0; i < instructions.size(); i++) {
            isModified = false;
            if (instructions.get(i).getOpcode() == Opcode.INVOKE_DIRECT) {
                if (!isModified) {
                    mutableImplementation.addInstruction(i++,
                            new BuilderInstruction21c(Opcode.CONST_STRING, 0,
                                    new ImmutableStringReference("tpatch:" + method.getDefiningClass().replace("/", "."))));
                    mutableImplementation.addInstruction(i++,
                            new BuilderInstruction35c(Opcode.INVOKE_STATIC, 1,
                                    0, 0, 0, 0, 0,
                                    new ImmutableMethodReference("Landroid/util/Log;", "e",
                                            Lists.newArrayList("Ljava/lang/String;", "Ljava/lang/String;"), "I")));
                    isModified = true;
                    break;

                }

            }
//            mutableImplementation.addInstruction(instructions.get(i));
        }

        return mutableImplementation;
    }
 
Example 7
Source File: DexWriter.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
private void writeDebugAndCodeItems(@Nonnull DexDataWriter offsetWriter,
                                    @Nonnull DeferredOutputStream temp) throws IOException {
    ByteArrayOutputStream ehBuf = new ByteArrayOutputStream();
    debugSectionOffset = offsetWriter.getPosition();
    DebugWriter<StringKey, TypeKey> debugWriter =
            new DebugWriter<StringKey, TypeKey>(stringSection, typeSection, offsetWriter);

    DexDataWriter codeWriter = new DexDataWriter(temp, 0);

    List<CodeItemOffset<MethodKey>> codeOffsets = Lists.newArrayList();

    for (ClassKey classKey: classSection.getSortedClasses()) {
        Collection<? extends MethodKey> directMethods = classSection.getSortedDirectMethods(classKey);
        Collection<? extends MethodKey> virtualMethods = classSection.getSortedVirtualMethods(classKey);

        Iterable<MethodKey> methods = Iterables.concat(directMethods, virtualMethods);

        for (MethodKey methodKey: methods) {
            List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks =
                    classSection.getTryBlocks(methodKey);
            Iterable<? extends Instruction> instructions = classSection.getInstructions(methodKey);
            Iterable<? extends DebugItem> debugItems = classSection.getDebugItems(methodKey);

            if (instructions != null && stringSection.hasJumboIndexes()) {
                boolean needsFix = false;
                for (Instruction instruction: instructions) {
                    if (instruction.getOpcode() == Opcode.CONST_STRING) {
                        if (stringSection.getItemIndex(
                                (StringRef)((ReferenceInstruction)instruction).getReference()) >= 65536) {
                            needsFix = true;
                            break;
                        }
                    }
                }

                if (needsFix) {
                    MutableMethodImplementation mutableMethodImplementation =
                            classSection.makeMutableMethodImplementation(methodKey);
                    fixInstructions(mutableMethodImplementation);

                    instructions = mutableMethodImplementation.getInstructions();
                    tryBlocks = mutableMethodImplementation.getTryBlocks();
                    debugItems = mutableMethodImplementation.getDebugItems();
                }
            }

            int debugItemOffset = writeDebugItem(offsetWriter, debugWriter,
                    classSection.getParameterNames(methodKey), debugItems);
            int codeItemOffset = writeCodeItem(codeWriter, ehBuf, methodKey, tryBlocks, instructions, debugItemOffset);

            if (codeItemOffset != -1) {
                codeOffsets.add(new CodeItemOffset<MethodKey>(methodKey, codeItemOffset));
            }
        }
    }

    offsetWriter.align();
    codeSectionOffset = offsetWriter.getPosition();

    codeWriter.close();
    temp.writeTo(offsetWriter);
    temp.close();

    for (CodeItemOffset<MethodKey> codeOffset: codeOffsets) {
        classSection.setCodeItemOffset(codeOffset.method, codeSectionOffset + codeOffset.codeOffset);
    }
}
 
Example 8
Source File: DexWriter.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
private void writeDebugAndCodeItems(@Nonnull DexDataWriter offsetWriter,
                                    @Nonnull DeferredOutputStream temp) throws IOException {
    ByteArrayOutputStream ehBuf = new ByteArrayOutputStream();
    debugSectionOffset = offsetWriter.getPosition();
    DebugWriter<StringKey, TypeKey> debugWriter =
            new DebugWriter<StringKey, TypeKey>(stringSection, typeSection, offsetWriter);

    DexDataWriter codeWriter = new DexDataWriter(temp, 0);

    List<CodeItemOffset<MethodKey>> codeOffsets = Lists.newArrayList();

    for (ClassKey classKey: classSection.getSortedClasses()) {
        Collection<? extends MethodKey> directMethods = classSection.getSortedDirectMethods(classKey);
        Collection<? extends MethodKey> virtualMethods = classSection.getSortedVirtualMethods(classKey);

        Iterable<MethodKey> methods = Iterables.concat(directMethods, virtualMethods);

        for (MethodKey methodKey: methods) {
            List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks =
                    classSection.getTryBlocks(methodKey);
            Iterable<? extends Instruction> instructions = classSection.getInstructions(methodKey);
            Iterable<? extends DebugItem> debugItems = classSection.getDebugItems(methodKey);

            if (instructions != null && stringSection.hasJumboIndexes()) {
                boolean needsFix = false;
                for (Instruction instruction: instructions) {
                    if (instruction.getOpcode() == Opcode.CONST_STRING) {
                        if (stringSection.getItemIndex(
                                (StringRef)((ReferenceInstruction)instruction).getReference()) >= 65536) {
                            needsFix = true;
                            break;
                        }
                    }
                }

                if (needsFix) {
                    MutableMethodImplementation mutableMethodImplementation =
                            classSection.makeMutableMethodImplementation(methodKey);
                    fixInstructions(mutableMethodImplementation);

                    instructions = mutableMethodImplementation.getInstructions();
                    tryBlocks = mutableMethodImplementation.getTryBlocks();
                    debugItems = mutableMethodImplementation.getDebugItems();
                }
            }

            int debugItemOffset = writeDebugItem(offsetWriter, debugWriter,
                    classSection.getParameterNames(methodKey), debugItems);
            int codeItemOffset = writeCodeItem(codeWriter, ehBuf, methodKey, tryBlocks, instructions, debugItemOffset);

            if (codeItemOffset != -1) {
                codeOffsets.add(new CodeItemOffset<MethodKey>(methodKey, codeItemOffset));
            }
        }
    }

    offsetWriter.align();
    codeSectionOffset = offsetWriter.getPosition();

    codeWriter.close();
    temp.writeTo(offsetWriter);
    temp.close();

    for (CodeItemOffset<MethodKey> codeOffset: codeOffsets) {
        classSection.setCodeItemOffset(codeOffset.method, codeSectionOffset + codeOffset.codeOffset);
    }
}
 
Example 9
Source File: DexWriter.java    From HeyGirl with Apache License 2.0 4 votes vote down vote up
private void writeDebugAndCodeItems(@Nonnull DexDataWriter offsetWriter,
                                    @Nonnull DeferredOutputStream temp) throws IOException {
    ByteArrayOutputStream ehBuf = new ByteArrayOutputStream();
    debugSectionOffset = offsetWriter.getPosition();
    DebugWriter<StringKey, TypeKey> debugWriter =
            new DebugWriter<StringKey, TypeKey>(stringSection, typeSection, offsetWriter);

    DexDataWriter codeWriter = new DexDataWriter(temp, 0);

    List<CodeItemOffset<MethodKey>> codeOffsets = Lists.newArrayList();

    for (ClassKey classKey: classSection.getSortedClasses()) {
        Collection<? extends MethodKey> directMethods = classSection.getSortedDirectMethods(classKey);
        Collection<? extends MethodKey> virtualMethods = classSection.getSortedVirtualMethods(classKey);

        Iterable<MethodKey> methods = Iterables.concat(directMethods, virtualMethods);

        for (MethodKey methodKey: methods) {
            List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks =
                    classSection.getTryBlocks(methodKey);
            Iterable<? extends Instruction> instructions = classSection.getInstructions(methodKey);
            Iterable<? extends DebugItem> debugItems = classSection.getDebugItems(methodKey);

            if (instructions != null && stringSection.hasJumboIndexes()) {
                boolean needsFix = false;
                for (Instruction instruction: instructions) {
                    if (instruction.getOpcode() == Opcode.CONST_STRING) {
                        if (stringSection.getItemIndex(
                                (StringRef)((ReferenceInstruction)instruction).getReference()) >= 65536) {
                            needsFix = true;
                            break;
                        }
                    }
                }

                if (needsFix) {
                    MutableMethodImplementation mutableMethodImplementation =
                            classSection.makeMutableMethodImplementation(methodKey);
                    fixInstructions(mutableMethodImplementation);

                    instructions = mutableMethodImplementation.getInstructions();
                    tryBlocks = mutableMethodImplementation.getTryBlocks();
                    debugItems = mutableMethodImplementation.getDebugItems();
                }
            }

            int debugItemOffset = writeDebugItem(offsetWriter, debugWriter,
                    classSection.getParameterNames(methodKey), debugItems);
            int codeItemOffset = writeCodeItem(codeWriter, ehBuf, methodKey, tryBlocks, instructions, debugItemOffset);

            if (codeItemOffset != -1) {
                codeOffsets.add(new CodeItemOffset<MethodKey>(methodKey, codeItemOffset));
            }
        }
    }

    offsetWriter.align();
    codeSectionOffset = offsetWriter.getPosition();

    codeWriter.close();
    temp.writeTo(offsetWriter);
    temp.close();

    for (CodeItemOffset<MethodKey> codeOffset: codeOffsets) {
        classSection.setCodeItemOffset(codeOffset.method, codeSectionOffset + codeOffset.codeOffset);
    }
}
 
Example 10
Source File: DexWriter.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
private void writeDebugAndCodeItems(@Nonnull DexDataWriter offsetWriter,
                                    @Nonnull DeferredOutputStream temp) throws IOException {
    ByteArrayOutputStream ehBuf = new ByteArrayOutputStream();
    debugSectionOffset = offsetWriter.getPosition();
    DebugWriter<StringKey, TypeKey> debugWriter =
            new DebugWriter<StringKey, TypeKey>(stringSection, typeSection, offsetWriter);

    DexDataWriter codeWriter = new DexDataWriter(temp, 0);

    List<CodeItemOffset<MethodKey>> codeOffsets = Lists.newArrayList();

    for (ClassKey classKey: classSection.getSortedClasses()) {
        Collection<? extends MethodKey> directMethods = classSection.getSortedDirectMethods(classKey);
        Collection<? extends MethodKey> virtualMethods = classSection.getSortedVirtualMethods(classKey);

        Iterable<MethodKey> methods = Iterables.concat(directMethods, virtualMethods);

        for (MethodKey methodKey: methods) {
            List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks =
                    classSection.getTryBlocks(methodKey);
            Iterable<? extends Instruction> instructions = classSection.getInstructions(methodKey);
            Iterable<? extends DebugItem> debugItems = classSection.getDebugItems(methodKey);

            if (instructions != null && stringSection.hasJumboIndexes()) {
                boolean needsFix = false;
                for (Instruction instruction: instructions) {
                    if (instruction.getOpcode() == Opcode.CONST_STRING) {
                        if (stringSection.getItemIndex(
                                (StringRef)((ReferenceInstruction)instruction).getReference()) >= 65536) {
                            needsFix = true;
                            break;
                        }
                    }
                }

                if (needsFix) {
                    MutableMethodImplementation mutableMethodImplementation =
                            classSection.makeMutableMethodImplementation(methodKey);
                    fixInstructions(mutableMethodImplementation);

                    instructions = mutableMethodImplementation.getInstructions();
                    tryBlocks = mutableMethodImplementation.getTryBlocks();
                    debugItems = mutableMethodImplementation.getDebugItems();
                }
            }

            int debugItemOffset = writeDebugItem(offsetWriter, debugWriter,
                    classSection.getParameterNames(methodKey), debugItems);
            int codeItemOffset = writeCodeItem(codeWriter, ehBuf, methodKey, tryBlocks, instructions, debugItemOffset);

            if (codeItemOffset != -1) {
                codeOffsets.add(new CodeItemOffset<MethodKey>(methodKey, codeItemOffset));
            }
        }
    }

    offsetWriter.align();
    codeSectionOffset = offsetWriter.getPosition();

    codeWriter.close();
    temp.writeTo(offsetWriter);
    temp.close();

    for (CodeItemOffset<MethodKey> codeOffset: codeOffsets) {
        classSection.setCodeItemOffset(codeOffset.method, codeSectionOffset + codeOffset.codeOffset);
    }
}