Search in sources :

Example 6 with Label

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

the class FixGotoTest method testFixGotoToGoto32.

@Test
public void testFixGotoToGoto32() {
    MethodImplementationBuilder builder = new MethodImplementationBuilder(1);
    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());
    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) BuilderInstruction10t(org.jf.dexlib2.builder.instruction.BuilderInstruction10t) Test(org.junit.Test)

Example 7 with Label

use of org.jf.dexlib2.builder.Label 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 8 with Label

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

the class FixOffsetsTest method testFixOffsets.

@Test
public void testFixOffsets() {
    MethodImplementationBuilder builder = new MethodImplementationBuilder(1);
    Label firstGotoTarget = builder.getLabel("firstGotoTarget");
    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");
    builder.addCatch(tryStart, tryEnd, handler);
    MethodImplementation impl = builder.getMethodImplementation();
    List<? extends Instruction> instructions = Lists.newArrayList(impl.getInstructions());
    Assert.assertEquals(1003, instructions.size());
    Assert.assertEquals(Opcode.GOTO_16, instructions.get(0).getOpcode());
    Assert.assertEquals(1004, ((OffsetInstruction) instructions.get(0)).getCodeOffset());
    Assert.assertEquals(Opcode.GOTO_16, instructions.get(501).getOpcode());
    Assert.assertEquals(502, ((OffsetInstruction) instructions.get(501)).getCodeOffset());
    List<? extends TryBlock<? extends ExceptionHandler>> exceptionHandlers = impl.getTryBlocks();
    Assert.assertEquals(1, exceptionHandlers.size());
    Assert.assertEquals(252, exceptionHandlers.get(0).getStartCodeAddress());
    Assert.assertEquals(752, exceptionHandlers.get(0).getCodeUnitCount());
    Assert.assertEquals(1, exceptionHandlers.get(0).getExceptionHandlers().size());
    ExceptionHandler exceptionHandler = exceptionHandlers.get(0).getExceptionHandlers().get(0);
    Assert.assertEquals(504, exceptionHandler.getHandlerCodeAddress());
    List<DebugItem> debugItems = Lists.newArrayList(impl.getDebugItems());
    Assert.assertEquals(5, debugItems.size());
    Assert.assertEquals(1, ((LineNumber) debugItems.get(0)).getLineNumber());
    Assert.assertEquals(2, debugItems.get(0).getCodeAddress());
    Assert.assertEquals(2, ((LineNumber) debugItems.get(1)).getLineNumber());
    Assert.assertEquals(252, debugItems.get(1).getCodeAddress());
    Assert.assertEquals(3, ((LineNumber) debugItems.get(2)).getLineNumber());
    Assert.assertEquals(502, debugItems.get(2).getCodeAddress());
    Assert.assertEquals(4, ((LineNumber) debugItems.get(3)).getLineNumber());
    Assert.assertEquals(504, debugItems.get(3).getCodeAddress());
    Assert.assertEquals(5, ((LineNumber) debugItems.get(4)).getLineNumber());
    Assert.assertEquals(1004, debugItems.get(4).getCodeAddress());
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) ExceptionHandler(org.jf.dexlib2.iface.ExceptionHandler) BuilderInstruction10x(org.jf.dexlib2.builder.instruction.BuilderInstruction10x) BuilderInstruction10t(org.jf.dexlib2.builder.instruction.BuilderInstruction10t) DebugItem(org.jf.dexlib2.iface.debug.DebugItem) Test(org.junit.Test)

Example 9 with Label

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

the class PayloadAlignmentTest method testPayloadAlignmentAddNopWithReferent.

@Test
public void testPayloadAlignmentAddNopWithReferent() {
    MethodImplementationBuilder implBuilder = new MethodImplementationBuilder(10);
    Label label = implBuilder.getLabel("array_payload");
    implBuilder.addInstruction(new BuilderInstruction31t(Opcode.FILL_ARRAY_DATA, 0, label));
    implBuilder.addInstruction(new BuilderInstruction12x(Opcode.MOVE, 0, 0));
    implBuilder.addInstruction(new BuilderInstruction12x(Opcode.MOVE, 0, 0));
    implBuilder.addInstruction(new BuilderInstruction12x(Opcode.MOVE, 0, 0));
    implBuilder.addInstruction(new BuilderInstruction12x(Opcode.MOVE, 0, 0));
    implBuilder.addLabel("array_payload");
    implBuilder.addInstruction(new BuilderArrayPayload(4, null));
    List<Instruction> instructions = Lists.newArrayList(implBuilder.getMethodImplementation().getInstructions());
    checkInstructions(instructions, new Opcode[] { Opcode.FILL_ARRAY_DATA, Opcode.MOVE, Opcode.MOVE, Opcode.MOVE, Opcode.MOVE, Opcode.NOP, Opcode.ARRAY_PAYLOAD });
    Instruction31t referent = (Instruction31t) instructions.get(0);
    Assert.assertEquals(8, referent.getCodeOffset());
}
Also used : OffsetInstruction(org.jf.dexlib2.iface.instruction.OffsetInstruction) Instruction(org.jf.dexlib2.iface.instruction.Instruction) Instruction31t(org.jf.dexlib2.iface.instruction.formats.Instruction31t) Test(org.junit.Test)

Example 10 with Label

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

the class MutableMethodImplementation method newBuilderPackedSwitchPayload.

@Nonnull
private BuilderPackedSwitchPayload newBuilderPackedSwitchPayload(@Nonnull MethodLocation location, @Nonnull int[] codeAddressToIndex, @Nonnull PackedSwitchPayload instruction) {
    List<? extends SwitchElement> switchElements = instruction.getSwitchElements();
    if (switchElements.size() == 0) {
        return new BuilderPackedSwitchPayload(0, null);
    }
    MethodLocation switchLocation = findSwitchForPayload(location);
    int baseAddress;
    if (switchLocation == null) {
        baseAddress = 0;
    } else {
        baseAddress = switchLocation.codeAddress;
    }
    List<Label> labels = Lists.newArrayList();
    for (SwitchElement element : switchElements) {
        labels.add(newLabel(codeAddressToIndex, element.getOffset() + baseAddress));
    }
    return new BuilderPackedSwitchPayload(switchElements.get(0).getKey(), labels);
}
Also used : SwitchElement(org.jf.dexlib2.iface.instruction.SwitchElement) Nonnull(javax.annotation.Nonnull)

Aggregations

Test (org.junit.Test)8 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)6 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)6 Label (org.jf.dexlib2.builder.Label)4 BuilderInstruction10t (org.jf.dexlib2.builder.instruction.BuilderInstruction10t)4 Instruction (org.jf.dexlib2.iface.instruction.Instruction)4 Nonnull (javax.annotation.Nonnull)3 BuilderInstruction (org.jf.dexlib2.builder.BuilderInstruction)3 SwitchElement (org.jf.dexlib2.iface.instruction.SwitchElement)3 IdentityStmt (soot.jimple.IdentityStmt)3 MonitorStmt (soot.jimple.MonitorStmt)3 NopStmt (soot.jimple.NopStmt)3 Stmt (soot.jimple.Stmt)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 BuilderOffsetInstruction (org.jf.dexlib2.builder.BuilderOffsetInstruction)2 BuilderInstruction20t (org.jf.dexlib2.builder.instruction.BuilderInstruction20t)2 ExceptionHandler (org.jf.dexlib2.iface.ExceptionHandler)2 OffsetInstruction (org.jf.dexlib2.iface.instruction.OffsetInstruction)2 Instruction31t (org.jf.dexlib2.iface.instruction.formats.Instruction31t)2