Search in sources :

Example 1 with BuilderInstruction12x

use of org.jf.dexlib2.builder.instruction.BuilderInstruction12x 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 2 with BuilderInstruction12x

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

the class PayloadAlignmentTest method testPayloadAlignmentAddNop.

@Test
public void testPayloadAlignmentAddNop() {
    MethodImplementationBuilder implBuilder = new MethodImplementationBuilder(10);
    implBuilder.addInstruction(new BuilderInstruction12x(Opcode.MOVE, 0, 0));
    implBuilder.addInstruction(new BuilderArrayPayload(4, null));
    List<? extends Instruction> instructions = Lists.newArrayList(implBuilder.getMethodImplementation().getInstructions());
    Assert.assertEquals(instructions.size(), 3);
    Instruction instruction = instructions.get(0);
    Assert.assertEquals(instruction.getOpcode(), Opcode.MOVE);
    instruction = instructions.get(1);
    Assert.assertEquals(instruction.getOpcode(), Opcode.NOP);
    instruction = instructions.get(2);
    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 3 with BuilderInstruction12x

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

the class MethodAnalyzerTest method testInstanceOfNarrowingAfterMove_art.

@Test
public void testInstanceOfNarrowingAfterMove_art() 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(forArtVersion(56), Collections.singletonList(classDef));
    ClassPath classPath = new ClassPath(Lists.newArrayList(new DexClassProvider(dexFile)), true, 56);
    MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, null, false);
    List<AnalyzedInstruction> analyzedInstructions = methodAnalyzer.getAnalyzedInstructions();
    Assert.assertEquals("Lmain;", analyzedInstructions.get(3).getPreInstructionRegisterType(1).type.getType());
    Assert.assertEquals("Lmain;", 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 4 with BuilderInstruction12x

use of org.jf.dexlib2.builder.instruction.BuilderInstruction12x 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 5 with BuilderInstruction12x

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

Aggregations

Test (org.junit.Test)5 Instruction (org.jf.dexlib2.iface.instruction.Instruction)3 OffsetInstruction (org.jf.dexlib2.iface.instruction.OffsetInstruction)3 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)2 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)2 BuilderInstruction12x (org.jf.dexlib2.builder.instruction.BuilderInstruction12x)2 BuilderInstruction21t (org.jf.dexlib2.builder.instruction.BuilderInstruction21t)2 BuilderInstruction22c (org.jf.dexlib2.builder.instruction.BuilderInstruction22c)2 ClassDef (org.jf.dexlib2.iface.ClassDef)2 DexFile (org.jf.dexlib2.iface.DexFile)2 Method (org.jf.dexlib2.iface.Method)2 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)2 Instruction31t (org.jf.dexlib2.iface.instruction.formats.Instruction31t)2 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)2 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)2 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)2 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)2 ImmutableTypeReference (org.jf.dexlib2.immutable.reference.ImmutableTypeReference)2