Search in sources :

Example 1 with BuilderPackedSwitchPayload

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

the class BuilderMutableMethodImplementation 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) BuilderPackedSwitchPayload(org.jf.dexlib2.builder.instruction.BuilderPackedSwitchPayload) Nonnull(javax.annotation.Nonnull)

Example 2 with BuilderPackedSwitchPayload

use of org.jf.dexlib2.builder.instruction.BuilderPackedSwitchPayload 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)

Example 3 with BuilderPackedSwitchPayload

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

the class PayloadAlignmentTest method testPackedSwitchAlignment.

@Test
public void testPackedSwitchAlignment() {
    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 BuilderPackedSwitchPayload(0, Lists.newArrayList(implBuilder.getLabel("switch_target_1"), implBuilder.getLabel("switch_target_2"), 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.PACKED_SWITCH, 0, implBuilder.getLabel("switch_payload")));
    List<Instruction> instructions = Lists.newArrayList(implBuilder.getMethodImplementation().getInstructions());
    checkInstructions(instructions, new Opcode[] { Opcode.GOTO, Opcode.NOP, Opcode.PACKED_SWITCH_PAYLOAD, Opcode.NOP, Opcode.NOP, Opcode.NOP, Opcode.NOP, Opcode.PACKED_SWITCH });
    OffsetInstruction gotoInstruction = (OffsetInstruction) instructions.get(0);
    Assert.assertEquals(12, gotoInstruction.getCodeOffset());
    PackedSwitchPayload payload = (PackedSwitchPayload) instructions.get(2);
    Assert.assertEquals(3, payload.getSwitchElements().size());
    Assert.assertEquals(-16, 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(-14, referent.getCodeOffset());
}
Also used : OffsetInstruction(org.jf.dexlib2.iface.instruction.OffsetInstruction) PackedSwitchPayload(org.jf.dexlib2.iface.instruction.formats.PackedSwitchPayload) OffsetInstruction(org.jf.dexlib2.iface.instruction.OffsetInstruction) Instruction(org.jf.dexlib2.iface.instruction.Instruction) Test(org.junit.Test)

Aggregations

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