use of org.jf.dexlib2.builder.instruction.BuilderInstruction10x 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());
}
use of org.jf.dexlib2.builder.instruction.BuilderInstruction10x 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());
}
use of org.jf.dexlib2.builder.instruction.BuilderInstruction10x 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());
}
use of org.jf.dexlib2.builder.instruction.BuilderInstruction10x in project smali by JesusFreke.
the class MutableMethodImplementationTest method testNewLabelByIndex.
@Test
public void testNewLabelByIndex() {
MethodImplementationBuilder builder = new MethodImplementationBuilder(10);
builder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
builder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
builder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
builder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
builder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
builder.addInstruction(new BuilderInstruction32x(Opcode.MOVE_16, 0, 0));
MutableMethodImplementation mutableMethodImplementation = new MutableMethodImplementation(builder.getMethodImplementation());
mutableMethodImplementation.addCatch(mutableMethodImplementation.newLabelForIndex(0), mutableMethodImplementation.newLabelForIndex(6), mutableMethodImplementation.newLabelForIndex(1));
Assert.assertEquals(0, mutableMethodImplementation.getTryBlocks().get(0).getStartCodeAddress());
Assert.assertEquals(8, mutableMethodImplementation.getTryBlocks().get(0).getCodeUnitCount());
Assert.assertEquals(1, mutableMethodImplementation.getTryBlocks().get(0).getExceptionHandlers().get(0).getHandlerCodeAddress());
}
use of org.jf.dexlib2.builder.instruction.BuilderInstruction10x in project smali by JesusFreke.
the class MethodAnalyzerTest method testInstanceOfNarrowingNez_dalvik.
@Test
public void testInstanceOfNarrowingNez_dalvik() throws IOException {
MethodImplementationBuilder builder = new MethodImplementationBuilder(2);
builder.addInstruction(new BuilderInstruction22c(Opcode.INSTANCE_OF, 0, 1, new ImmutableTypeReference("Lmain;")));
builder.addInstruction(new BuilderInstruction21t(Opcode.IF_NEZ, 0, builder.getLabel("instance_of")));
builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
builder.addLabel("instance_of");
builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
MethodImplementation methodImplementation = builder.getMethodImplementation();
Method method = new ImmutableMethod("Lmain;", "narrowing", Collections.singletonList(new ImmutableMethodParameter("Ljava/lang/Object;", null, null)), "V", AccessFlags.PUBLIC.getValue(), null, null, methodImplementation);
ClassDef classDef = new ImmutableClassDef("Lmain;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, Collections.singletonList(method));
DexFile dexFile = new ImmutableDexFile(Opcodes.getDefault(), Collections.singletonList(classDef));
ClassPath classPath = new ClassPath(new DexClassProvider(dexFile));
MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, null, false);
List<AnalyzedInstruction> analyzedInstructions = methodAnalyzer.getAnalyzedInstructions();
Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(2).getPreInstructionRegisterType(1).type.getType());
Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(3).getPreInstructionRegisterType(1).type.getType());
}
Aggregations