Search in sources :

Example 71 with Instruction

use of org.jf.dexlib2.iface.instruction.Instruction in project smali by JesusFreke.

the class AccessorTest method testAccessors.

@Test
public void testAccessors() throws IOException {
    URL url = AccessorTest.class.getClassLoader().getResource("accessorTest.dex");
    Assert.assertNotNull(url);
    DexFile f = DexFileFactory.loadDexFile(url.getFile(), Opcodes.getDefault());
    SyntheticAccessorResolver sar = new SyntheticAccessorResolver(f.getOpcodes(), f.getClasses());
    ClassDef accessorTypesClass = null;
    ClassDef accessorsClass = null;
    for (ClassDef classDef : f.getClasses()) {
        String className = classDef.getType();
        if (className.equals("Lorg/jf/dexlib2/AccessorTypes;")) {
            accessorTypesClass = classDef;
        } else if (className.equals("Lorg/jf/dexlib2/AccessorTypes$Accessors;")) {
            accessorsClass = classDef;
        }
    }
    Assert.assertNotNull(accessorTypesClass);
    Assert.assertNotNull(accessorsClass);
    for (Method method : accessorsClass.getMethods()) {
        Matcher m = accessorMethodPattern.matcher(method.getName());
        if (!m.matches()) {
            continue;
        }
        String type = m.group(1);
        String operation = m.group(2);
        MethodImplementation methodImpl = method.getImplementation();
        Assert.assertNotNull(methodImpl);
        for (Instruction instruction : methodImpl.getInstructions()) {
            Opcode opcode = instruction.getOpcode();
            if (opcode == Opcode.INVOKE_STATIC || opcode == Opcode.INVOKE_STATIC_RANGE) {
                MethodReference accessorMethod = (MethodReference) ((ReferenceInstruction) instruction).getReference();
                SyntheticAccessorResolver.AccessedMember accessedMember = sar.getAccessedMember(accessorMethod);
                Assert.assertNotNull(String.format("Could not resolve accessor for %s_%s", type, operation), accessedMember);
                int operationType = operationTypes.get(operation);
                Assert.assertEquals(operationType, accessedMember.accessedMemberType);
                Assert.assertEquals(String.format("%s_val", type), ((FieldReference) accessedMember.accessedMember).getName());
            }
        }
    }
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) Matcher(java.util.regex.Matcher) Method(org.jf.dexlib2.iface.Method) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) URL(java.net.URL) DexFile(org.jf.dexlib2.iface.DexFile) SyntheticAccessorResolver(org.jf.dexlib2.util.SyntheticAccessorResolver) ClassDef(org.jf.dexlib2.iface.ClassDef) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) Test(org.junit.Test)

Example 72 with Instruction

use of org.jf.dexlib2.iface.instruction.Instruction 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 73 with Instruction

use of org.jf.dexlib2.iface.instruction.Instruction 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 74 with Instruction

use of org.jf.dexlib2.iface.instruction.Instruction in project smali by JesusFreke.

the class SmalideaMethodTest method testArrayData.

public void testArrayData() {
    String text = ".class public LFormat31t;\n" + ".super Ljava/lang/Object;\n" + ".source \"Format31t.smali\"" + "\n" + ".method public test_fill-array-data()V\n" + "    .registers 3\n" + "    .annotation runtime Lorg/junit/Test;\n" + "    .end annotation\n" + "\n" + "    const v0, 6\n" + "    new-array v0, v0, [I\n" + "    fill-array-data v0, :ArrayData\n" + "\n" + "    const v1, 0\n" + "    aget v2, v0, v1\n" + "    const v1, 1\n" + "    invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + "    const v1, 1\n" + "    aget v2, v0, v1\n" + "    const v1, 2\n" + "    invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + "    const v1, 2\n" + "    aget v2, v0, v1\n" + "    const v1, 3\n" + "    invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + "    const v1, 3\n" + "    aget v2, v0, v1\n" + "    const v1, 4\n" + "    invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + "    const v1, 4\n" + "    aget v2, v0, v1\n" + "    const v1, 5\n" + "    invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + "    const v1, 5\n" + "    aget v2, v0, v1\n" + "    const v1, 6\n" + "    invoke-static {v1, v2}, LAssert;->assertEquals(II)V\n" + "\n" + "    return-void\n" + "\n" + ":ArrayData\n" + "    .array-data 4\n" + "        1 2 128 -256 65536 0x7fffffff\n" + "    .end array-data\n" + ".end method";
    SmaliFile file = (SmaliFile) myFixture.addFileToProject("my/pkg/blah.smali", text);
    SmaliClass smaliClass = file.getPsiClass();
    SmaliMethod smaliMethod = smaliClass.getMethods()[0];
    SmalideaMethod method = new SmalideaMethod(smaliMethod);
    MethodImplementation impl = method.getImplementation();
    Assert.assertNotNull(impl);
    List<Instruction> instructions = Lists.newArrayList(impl.getInstructions());
    ArrayPayload arrayPayload = (ArrayPayload) instructions.get(28);
    Assert.assertEquals(4, arrayPayload.getElementWidth());
    List<Number> elements = arrayPayload.getArrayElements();
    Assert.assertEquals(6, elements.size());
    Assert.assertEquals(1L, elements.get(0).longValue());
    Assert.assertEquals(2L, elements.get(1).longValue());
    Assert.assertEquals(128L, elements.get(2));
    Assert.assertEquals(-256L, elements.get(3));
    Assert.assertEquals(65536L, elements.get(4));
    Assert.assertEquals(0x7fffffffL, elements.get(5));
}
Also used : SmaliFile(org.jf.smalidea.psi.impl.SmaliFile) MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) SmaliClass(org.jf.smalidea.psi.impl.SmaliClass) SmaliMethod(org.jf.smalidea.psi.impl.SmaliMethod) Instruction(org.jf.dexlib2.iface.instruction.Instruction)

Example 75 with Instruction

use of org.jf.dexlib2.iface.instruction.Instruction in project smali by JesusFreke.

the class SmalideaMethodTest method testPackedSwitch.

public void testPackedSwitch() {
    String text = ".class public LFormat31t;\n" + ".super Ljava/lang/Object;\n" + ".source \"Format31t.smali\"" + "\n" + ".method public test_packed-switch()V\n" + "    .registers 1\n" + "    .annotation runtime Lorg/junit/Test;\n" + "    .end annotation\n" + "\n" + "    const v0, 12\n" + "\n" + ":switch\n" + "    packed-switch v0, :PackedSwitch\n" + "\n" + ":Label10\n" + "    invoke-static {}, Lorg/junit/Assert;->fail()V\n" + "    return-void\n" + "\n" + ":Label11\n" + "    invoke-static {}, Lorg/junit/Assert;->fail()V\n" + "    return-void\n" + "\n" + ":Label12\n" + "    return-void\n" + "\n" + ":Label13\n" + "    invoke-static {}, Lorg/junit/Assert;->fail()V\n" + "    return-void\n" + "\n" + ":PackedSwitch\n" + "    .packed-switch 10\n" + "        :Label10\n" + "        :Label11\n" + "        :Label12\n" + "        :Label13\n" + "    .end packed-switch\n" + ".end method";
    SmaliFile file = (SmaliFile) myFixture.addFileToProject("my/pkg/blah.smali", text);
    SmaliClass smaliClass = file.getPsiClass();
    SmaliMethod smaliMethod = smaliClass.getMethods()[0];
    SmalideaMethod method = new SmalideaMethod(smaliMethod);
    MethodImplementation impl = method.getImplementation();
    Assert.assertNotNull(impl);
    List<Instruction> instructions = Lists.newArrayList(impl.getInstructions());
    PackedSwitchPayload packedSwitchPayload = (PackedSwitchPayload) instructions.get(9);
    List<? extends SwitchElement> switchElements = packedSwitchPayload.getSwitchElements();
    Assert.assertEquals(4, switchElements.size());
    checkSwitchElement(switchElements.get(0), 10, 6);
    checkSwitchElement(switchElements.get(1), 11, 14);
    checkSwitchElement(switchElements.get(2), 12, 22);
    checkSwitchElement(switchElements.get(3), 13, 24);
}
Also used : SmaliFile(org.jf.smalidea.psi.impl.SmaliFile) MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) SmaliClass(org.jf.smalidea.psi.impl.SmaliClass) SmaliMethod(org.jf.smalidea.psi.impl.SmaliMethod) Instruction(org.jf.dexlib2.iface.instruction.Instruction)

Aggregations

Instruction (org.jf.dexlib2.iface.instruction.Instruction)35 Test (org.junit.Test)20 Opcode (org.jf.dexlib2.Opcode)16 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)16 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)15 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)13 OffsetInstruction (org.jf.dexlib2.iface.instruction.OffsetInstruction)12 AnalyzedInstruction (org.jf.dexlib2.analysis.AnalyzedInstruction)11 ExceptionWithContext (org.jf.util.ExceptionWithContext)11 TypeReference (org.jf.dexlib2.iface.reference.TypeReference)10 Nonnull (javax.annotation.Nonnull)7 ClassDef (org.jf.dexlib2.iface.ClassDef)7 FieldReference (org.jf.dexlib2.iface.reference.FieldReference)7 IOException (java.io.IOException)6 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)6 Reference (org.jf.dexlib2.iface.reference.Reference)6 ImmutableFieldReference (org.jf.dexlib2.immutable.reference.ImmutableFieldReference)5 BuilderInstruction10t (org.jf.dexlib2.builder.instruction.BuilderInstruction10t)4 InvalidItemIndex (org.jf.dexlib2.dexbacked.DexBackedDexFile.InvalidItemIndex)4 DexFile (org.jf.dexlib2.iface.DexFile)4