Search in sources :

Example 1 with BuilderSparseSwitchPayload

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

the class MutableMethodImplementation method newBuilderSparseSwitchPayload.

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

Example 2 with BuilderSparseSwitchPayload

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

the class PayloadAlignmentTest method testSparseSwitchAlignment.

@Test
public void testSparseSwitchAlignment() {
    MethodImplementationBuilder implBuilder = new MethodImplementationBuilder(10);
    implBuilder.addLabel("switch_target_1");
    implBuilder.addInstruction(new BuilderInstruction10t(Opcode.GOTO, implBuilder.getLabel("goto_target")));
    implBuilder.addLabel("switch_payload");
    implBuilder.addInstruction(new BuilderSparseSwitchPayload(Lists.newArrayList(new SwitchLabelElement(0, implBuilder.getLabel("switch_target_1")), new SwitchLabelElement(5, implBuilder.getLabel("switch_target_2")), new SwitchLabelElement(10, implBuilder.getLabel("switch_target_3")))));
    implBuilder.addLabel("goto_target");
    implBuilder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
    implBuilder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
    implBuilder.addLabel("switch_target_2");
    implBuilder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
    implBuilder.addLabel("switch_target_3");
    implBuilder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
    implBuilder.addInstruction(new BuilderInstruction31t(Opcode.SPARSE_SWITCH, 0, implBuilder.getLabel("switch_payload")));
    List<Instruction> instructions = Lists.newArrayList(implBuilder.getMethodImplementation().getInstructions());
    checkInstructions(instructions, new Opcode[] { Opcode.GOTO, Opcode.NOP, Opcode.SPARSE_SWITCH_PAYLOAD, Opcode.NOP, Opcode.NOP, Opcode.NOP, Opcode.NOP, Opcode.SPARSE_SWITCH });
    OffsetInstruction gotoInstruction = (OffsetInstruction) instructions.get(0);
    Assert.assertEquals(16, gotoInstruction.getCodeOffset());
    SparseSwitchPayload payload = (SparseSwitchPayload) instructions.get(2);
    Assert.assertEquals(3, payload.getSwitchElements().size());
    Assert.assertEquals(-20, payload.getSwitchElements().get(0).getOffset());
    Assert.assertEquals(-2, payload.getSwitchElements().get(1).getOffset());
    Assert.assertEquals(-1, payload.getSwitchElements().get(2).getOffset());
    OffsetInstruction referent = (OffsetInstruction) instructions.get(7);
    Assert.assertEquals(-18, referent.getCodeOffset());
}
Also used : OffsetInstruction(org.jf.dexlib2.iface.instruction.OffsetInstruction) SparseSwitchPayload(org.jf.dexlib2.iface.instruction.formats.SparseSwitchPayload) OffsetInstruction(org.jf.dexlib2.iface.instruction.OffsetInstruction) Instruction(org.jf.dexlib2.iface.instruction.Instruction) Test(org.junit.Test)

Example 3 with BuilderSparseSwitchPayload

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

the class BuilderMutableMethodImplementation method newBuilderSparseSwitchPayload.

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

Aggregations

Nonnull (javax.annotation.Nonnull)2 SwitchElement (org.jf.dexlib2.iface.instruction.SwitchElement)2 BuilderSparseSwitchPayload (org.jf.dexlib2.builder.instruction.BuilderSparseSwitchPayload)1 Instruction (org.jf.dexlib2.iface.instruction.Instruction)1 OffsetInstruction (org.jf.dexlib2.iface.instruction.OffsetInstruction)1 SparseSwitchPayload (org.jf.dexlib2.iface.instruction.formats.SparseSwitchPayload)1 Test (org.junit.Test)1