Search in sources :

Example 36 with MethodImplementation

use of org.jf.dexlib2.iface.MethodImplementation 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 37 with MethodImplementation

use of org.jf.dexlib2.iface.MethodImplementation 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 38 with MethodImplementation

use of org.jf.dexlib2.iface.MethodImplementation 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 39 with MethodImplementation

use of org.jf.dexlib2.iface.MethodImplementation 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)

Example 40 with MethodImplementation

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

the class ImplicitReferenceTest method testImplicitMethodReference.

@Test
public void testImplicitMethodReference() throws RecognitionException, IOException {
    ClassDef classDef = SmaliTestUtils.compileSmali("" + ".class public LHelloWorld;\n" + ".super Ljava/lang/Object;\n" + ".method public static main([Ljava/lang/String;)V\n" + "    .registers 1\n" + "    invoke-static {p0}, toString()V\n" + "    invoke-static {p0}, V()V\n" + "    invoke-static {p0}, I()V\n" + "    return-void\n" + ".end method");
    Method mainMethod = null;
    for (Method method : classDef.getMethods()) {
        if (method.getName().equals("main")) {
            mainMethod = method;
        }
    }
    Assert.assertNotNull(mainMethod);
    MethodImplementation methodImpl = mainMethod.getImplementation();
    Assert.assertNotNull(methodImpl);
    List<Instruction> instructions = Lists.newArrayList(methodImpl.getInstructions());
    Instruction35c instruction = (Instruction35c) instructions.get(0);
    Assert.assertNotNull(instruction);
    Assert.assertEquals(Opcode.INVOKE_STATIC, instruction.getOpcode());
    MethodReference method = (MethodReference) instruction.getReference();
    Assert.assertEquals(classDef.getType(), method.getDefiningClass());
    Assert.assertEquals("toString", method.getName());
    instruction = (Instruction35c) instructions.get(1);
    Assert.assertNotNull(instruction);
    Assert.assertEquals(Opcode.INVOKE_STATIC, instruction.getOpcode());
    method = (MethodReference) instruction.getReference();
    Assert.assertEquals(classDef.getType(), method.getDefiningClass());
    Assert.assertEquals("V", method.getName());
    instruction = (Instruction35c) instructions.get(2);
    Assert.assertNotNull(instruction);
    Assert.assertEquals(Opcode.INVOKE_STATIC, instruction.getOpcode());
    method = (MethodReference) instruction.getReference();
    Assert.assertEquals(classDef.getType(), method.getDefiningClass());
    Assert.assertEquals("I", method.getName());
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) ClassDef(org.jf.dexlib2.iface.ClassDef) Instruction35c(org.jf.dexlib2.iface.instruction.formats.Instruction35c) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) Method(org.jf.dexlib2.iface.Method) Instruction(org.jf.dexlib2.iface.instruction.Instruction) Test(org.junit.Test)

Aggregations

MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)29 Test (org.junit.Test)18 Method (org.jf.dexlib2.iface.Method)17 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)13 ClassDef (org.jf.dexlib2.iface.ClassDef)12 Instruction (org.jf.dexlib2.iface.instruction.Instruction)12 DexFile (org.jf.dexlib2.iface.DexFile)11 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)11 MutableMethodImplementation (org.jf.dexlib2.builder.MutableMethodImplementation)9 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)8 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)8 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)8 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)7 HashSet (java.util.HashSet)6 BuilderInstruction21c (org.jf.dexlib2.builder.instruction.BuilderInstruction21c)6 BuilderInstruction21t (org.jf.dexlib2.builder.instruction.BuilderInstruction21t)6 BuilderInstruction22c (org.jf.dexlib2.builder.instruction.BuilderInstruction22c)6 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)6 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)6 ImmutableTypeReference (org.jf.dexlib2.immutable.reference.ImmutableTypeReference)6