Search in sources :

Example 21 with MethodImplementationBuilder

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

the class MethodAnalyzerTest method testInstanceOfNarrowingAfterMove_dalvik.

@Test
public void testInstanceOfNarrowingAfterMove_dalvik() throws IOException {
    MethodImplementationBuilder builder = new MethodImplementationBuilder(3);
    builder.addInstruction(new BuilderInstruction12x(Opcode.MOVE_OBJECT, 1, 2));
    builder.addInstruction(new BuilderInstruction22c(Opcode.INSTANCE_OF, 0, 1, new ImmutableTypeReference("Lmain;")));
    builder.addInstruction(new BuilderInstruction21t(Opcode.IF_EQZ, 0, builder.getLabel("not_instance_of")));
    builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
    builder.addLabel("not_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(3).getPreInstructionRegisterType(1).type.getType());
    Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(3).getPreInstructionRegisterType(2).type.getType());
    Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(4).getPreInstructionRegisterType(1).type.getType());
    Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(4).getPreInstructionRegisterType(2).type.getType());
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) BuilderInstruction10x(org.jf.dexlib2.builder.instruction.BuilderInstruction10x) BuilderInstruction12x(org.jf.dexlib2.builder.instruction.BuilderInstruction12x) ImmutableMethod(org.jf.dexlib2.immutable.ImmutableMethod) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) ImmutableMethodParameter(org.jf.dexlib2.immutable.ImmutableMethodParameter) BuilderInstruction21t(org.jf.dexlib2.builder.instruction.BuilderInstruction21t) ImmutableMethod(org.jf.dexlib2.immutable.ImmutableMethod) Method(org.jf.dexlib2.iface.Method) DexFile(org.jf.dexlib2.iface.DexFile) ImmutableDexFile(org.jf.dexlib2.immutable.ImmutableDexFile) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) ClassDef(org.jf.dexlib2.iface.ClassDef) BuilderInstruction22c(org.jf.dexlib2.builder.instruction.BuilderInstruction22c) ImmutableTypeReference(org.jf.dexlib2.immutable.reference.ImmutableTypeReference) ImmutableDexFile(org.jf.dexlib2.immutable.ImmutableDexFile) MethodImplementationBuilder(org.jf.dexlib2.builder.MethodImplementationBuilder) Test(org.junit.Test)

Example 22 with MethodImplementationBuilder

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

the class PayloadAlignmentTest method testPayloadAlignmentRemoveNopWithReferent.

@Test
public void testPayloadAlignmentRemoveNopWithReferent() {
    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 BuilderInstruction10x(Opcode.NOP));
    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.ARRAY_PAYLOAD });
    Instruction31t referent = (Instruction31t) instructions.get(0);
    Assert.assertEquals(6, 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 23 with MethodImplementationBuilder

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

the class PayloadAlignmentTest method testPayloadAlignmentRemoveNop.

@Test
public void testPayloadAlignmentRemoveNop() {
    MethodImplementationBuilder implBuilder = new MethodImplementationBuilder(10);
    implBuilder.addInstruction(new BuilderInstruction10x(Opcode.NOP));
    implBuilder.addInstruction(new BuilderArrayPayload(4, null));
    List<? extends Instruction> instructions = Lists.newArrayList(implBuilder.getMethodImplementation().getInstructions());
    Assert.assertEquals(instructions.size(), 1);
    Instruction instruction = instructions.get(0);
    Assert.assertEquals(instruction.getOpcode(), Opcode.ARRAY_PAYLOAD);
}
Also used : OffsetInstruction(org.jf.dexlib2.iface.instruction.OffsetInstruction) Instruction(org.jf.dexlib2.iface.instruction.Instruction) Test(org.junit.Test)

Example 24 with MethodImplementationBuilder

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

the class MutableMethodImplementationTest method testNewLabelByAddress.

@Test
public void testNewLabelByAddress() {
    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.newLabelForAddress(0), mutableMethodImplementation.newLabelForAddress(8), mutableMethodImplementation.newLabelForAddress(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());
}
Also used : BuilderInstruction10x(org.jf.dexlib2.builder.instruction.BuilderInstruction10x) BuilderInstruction32x(org.jf.dexlib2.builder.instruction.BuilderInstruction32x) Test(org.junit.Test)

Example 25 with MethodImplementationBuilder

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

the class MutableMethodImplementationTest method testTryEndAtEndOfMethod.

@Test
public void testTryEndAtEndOfMethod() {
    MethodImplementationBuilder builder = new MethodImplementationBuilder(10);
    Label startLabel = builder.addLabel("start");
    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));
    Label endLabel = builder.addLabel("end");
    builder.addCatch(startLabel, endLabel, startLabel);
    MethodImplementation methodImplementation = builder.getMethodImplementation();
    Assert.assertEquals(0, methodImplementation.getTryBlocks().get(0).getStartCodeAddress());
    Assert.assertEquals(8, methodImplementation.getTryBlocks().get(0).getCodeUnitCount());
    methodImplementation = new MutableMethodImplementation(methodImplementation);
    Assert.assertEquals(0, methodImplementation.getTryBlocks().get(0).getStartCodeAddress());
    Assert.assertEquals(8, methodImplementation.getTryBlocks().get(0).getCodeUnitCount());
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) BuilderInstruction10x(org.jf.dexlib2.builder.instruction.BuilderInstruction10x) BuilderInstruction32x(org.jf.dexlib2.builder.instruction.BuilderInstruction32x) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)22 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)15 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)12 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)9 ImmutableTypeReference (org.jf.dexlib2.immutable.reference.ImmutableTypeReference)8 DexFile (org.jf.dexlib2.iface.DexFile)7 Instruction (org.jf.dexlib2.iface.instruction.Instruction)7 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)7 BuilderInstruction21t (org.jf.dexlib2.builder.instruction.BuilderInstruction21t)6 BuilderInstruction22c (org.jf.dexlib2.builder.instruction.BuilderInstruction22c)6 ClassDef (org.jf.dexlib2.iface.ClassDef)6 Method (org.jf.dexlib2.iface.Method)6 OffsetInstruction (org.jf.dexlib2.iface.instruction.OffsetInstruction)6 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)6 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)6 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)6 BuilderInstruction10t (org.jf.dexlib2.builder.instruction.BuilderInstruction10t)4 BuilderInstruction32x (org.jf.dexlib2.builder.instruction.BuilderInstruction32x)3 LinkedHashMap (java.util.LinkedHashMap)2 BuilderInstruction12x (org.jf.dexlib2.builder.instruction.BuilderInstruction12x)2