Search in sources :

Example 1 with BuilderInstruction20t

use of org.jf.dexlib2.builder.instruction.BuilderInstruction20t in project smali by JesusFreke.

the class FixGotoTest method testFixGotoCascading.

@Test
public void testFixGotoCascading() {
    MethodImplementationBuilder builder = new MethodImplementationBuilder(1);
    Label goto16Target = builder.getLabel("goto16Target");
    builder.addInstruction(new BuilderInstruction20t(Opcode.GOTO_16, goto16Target));
    for (int i = 0; i < 1000; i++) {
        builder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
    }
    Label gotoTarget = builder.getLabel("gotoTarget");
    builder.addInstruction(new BuilderInstruction10t(Opcode.GOTO, gotoTarget));
    for (int i = 0; i < 499; i++) {
        builder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
    }
    builder.addLabel("gotoTarget");
    for (int i = 0; i < 31265; i++) {
        builder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
    }
    builder.addLabel("goto16Target");
    builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
    MethodImplementation impl = builder.getMethodImplementation();
    List<? extends Instruction> instructions = Lists.newArrayList(impl.getInstructions());
    Assert.assertEquals(32767, instructions.size());
    Assert.assertEquals(Opcode.GOTO_32, instructions.get(0).getOpcode());
    Assert.assertEquals(32769, ((OffsetInstruction) instructions.get(0)).getCodeOffset());
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) BuilderInstruction10x(org.jf.dexlib2.builder.instruction.BuilderInstruction10x) BuilderInstruction10t(org.jf.dexlib2.builder.instruction.BuilderInstruction10t) BuilderInstruction20t(org.jf.dexlib2.builder.instruction.BuilderInstruction20t) Test(org.junit.Test)

Example 2 with BuilderInstruction20t

use of org.jf.dexlib2.builder.instruction.BuilderInstruction20t in project smali by JesusFreke.

the class FixGotoTest method testFixGoto16ToGoto32.

@Test
public void testFixGoto16ToGoto32() {
    MethodImplementationBuilder builder = new MethodImplementationBuilder(1);
    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());
    Assert.assertEquals(Opcode.GOTO_32, instructions.get(0).getOpcode());
    Assert.assertEquals(70003, ((OffsetInstruction) instructions.get(0)).getCodeOffset());
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) BuilderInstruction10x(org.jf.dexlib2.builder.instruction.BuilderInstruction10x) BuilderInstruction20t(org.jf.dexlib2.builder.instruction.BuilderInstruction20t) Test(org.junit.Test)

Example 3 with BuilderInstruction20t

use of org.jf.dexlib2.builder.instruction.BuilderInstruction20t in project tinker by Tencent.

the class BuilderMutableMethodImplementation method fixInstructions.

private void fixInstructions() {
    HashSet<MethodLocation> payloadLocations = Sets.newHashSet();
    for (MethodLocation location : instructionList) {
        BuilderInstruction instruction = location.instruction;
        if (instruction != null) {
            switch(instruction.getOpcode()) {
                case SPARSE_SWITCH:
                case PACKED_SWITCH:
                    {
                        MethodLocation targetLocation = ((BuilderOffsetInstruction) instruction).getTarget().getLocation();
                        BuilderInstruction targetInstruction = targetLocation.instruction;
                        if (targetInstruction == null) {
                            throw new IllegalStateException(String.format("Switch instruction at address/index " + "0x%x/%d points to the end of the method.", location.codeAddress, location.index));
                        }
                        if (targetInstruction.getOpcode() == Opcode.NOP) {
                            targetInstruction = getFirstNonNop(targetLocation.index + 1);
                        }
                        if (targetInstruction == null || !(targetInstruction instanceof BuilderSwitchPayload)) {
                            throw new IllegalStateException(String.format("Switch instruction at address/index " + "0x%x/%d does not refer to a payload instruction.", location.codeAddress, location.index));
                        }
                        if ((instruction.opcode == Opcode.PACKED_SWITCH && targetInstruction.getOpcode() != Opcode.PACKED_SWITCH_PAYLOAD) || (instruction.opcode == Opcode.SPARSE_SWITCH && targetInstruction.getOpcode() != Opcode.SPARSE_SWITCH_PAYLOAD)) {
                            throw new IllegalStateException(String.format("Switch instruction at address/index " + "0x%x/%d refers to the wrong type of payload instruction.", location.codeAddress, location.index));
                        }
                        if (!payloadLocations.add(targetLocation)) {
                            throw new IllegalStateException("Multiple switch instructions refer to the same payload. " + "This is not currently supported. Please file a bug :)");
                        }
                        ((BuilderSwitchPayload) targetInstruction).referrer = location;
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }
    }
    boolean madeChanges;
    do {
        madeChanges = false;
        for (int index = 0; index < instructionList.size(); index++) {
            MethodLocation location = instructionList.get(index);
            BuilderInstruction instruction = location.instruction;
            if (instruction != null) {
                switch(instruction.getOpcode()) {
                    case GOTO:
                        {
                            int offset = ((BuilderOffsetInstruction) instruction).internalGetCodeOffset();
                            if (offset < Byte.MIN_VALUE || offset > Byte.MAX_VALUE) {
                                BuilderOffsetInstruction replacement;
                                if (offset < Short.MIN_VALUE || offset > Short.MAX_VALUE) {
                                    replacement = new BuilderInstruction30t(Opcode.GOTO_32, ((BuilderOffsetInstruction) instruction).getTarget());
                                } else {
                                    replacement = new BuilderInstruction20t(Opcode.GOTO_16, ((BuilderOffsetInstruction) instruction).getTarget());
                                }
                                replaceInstruction(location.index, replacement);
                                madeChanges = true;
                            }
                            break;
                        }
                    case GOTO_16:
                        {
                            int offset = ((BuilderOffsetInstruction) instruction).internalGetCodeOffset();
                            if (offset < Short.MIN_VALUE || offset > Short.MAX_VALUE) {
                                BuilderOffsetInstruction replacement = new BuilderInstruction30t(Opcode.GOTO_32, ((BuilderOffsetInstruction) instruction).getTarget());
                                replaceInstruction(location.index, replacement);
                                madeChanges = true;
                            }
                            break;
                        }
                    case SPARSE_SWITCH_PAYLOAD:
                    case PACKED_SWITCH_PAYLOAD:
                        if (((BuilderSwitchPayload) instruction).referrer == null) {
                            // if the switch payload isn't referenced, just remove it
                            removeInstruction(index);
                            index--;
                            madeChanges = true;
                            break;
                        }
                    // intentional fall-through
                    case ARRAY_PAYLOAD:
                        {
                            if ((location.codeAddress & 0x01) != 0) {
                                int previousIndex = location.index - 1;
                                MethodLocation previousLocation = instructionList.get(previousIndex);
                                Instruction previousInstruction = previousLocation.instruction;
                                assert previousInstruction != null;
                                if (previousInstruction.getOpcode() == Opcode.NOP) {
                                    removeInstruction(previousIndex);
                                    index--;
                                } else {
                                    addInstruction(location.index, new BuilderInstruction10x(Opcode.NOP));
                                    index++;
                                }
                                madeChanges = true;
                            }
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }
        }
    } while (madeChanges);
    fixInstructions = false;
}
Also used : BuilderInstruction10x(org.jf.dexlib2.builder.instruction.BuilderInstruction10x) BuilderInstruction20t(org.jf.dexlib2.builder.instruction.BuilderInstruction20t) BuilderInstruction30t(org.jf.dexlib2.builder.instruction.BuilderInstruction30t) Instruction(org.jf.dexlib2.iface.instruction.Instruction)

Aggregations

BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)3 BuilderInstruction20t (org.jf.dexlib2.builder.instruction.BuilderInstruction20t)3 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)2 Test (org.junit.Test)2 BuilderInstruction10t (org.jf.dexlib2.builder.instruction.BuilderInstruction10t)1 BuilderInstruction30t (org.jf.dexlib2.builder.instruction.BuilderInstruction30t)1 Instruction (org.jf.dexlib2.iface.instruction.Instruction)1