Search in sources :

Example 26 with Opcode

use of org.jf.dexlib2.Opcode 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 27 with Opcode

use of org.jf.dexlib2.Opcode in project smali by JesusFreke.

the class SmaliUsageTypeProvider method findClassUsageType.

@Nullable
private UsageType findClassUsageType(@NotNull PsiElement element) {
    PsiElement originalElement = element;
    while (element != null) {
        if (element instanceof SmaliFieldReference) {
            PsiElement prev = originalElement.getPrevSibling();
            while (prev != null) {
                // the declaring class
                if (prev.getNode().getElementType() == SmaliTokens.COLON) {
                    return FIELD_TYPE_REFERENCE;
                }
                prev = prev.getPrevSibling();
            }
            return FIELD_DECLARING_TYPE_REFERENCE;
        } else if (element instanceof SmaliMethodReferenceParamList) {
            return METHOD_PARAM_REFERENCE;
        } else if (element instanceof SmaliMethodReference) {
            PsiElement prev = originalElement.getPrevSibling();
            while (prev != null) {
                IElementType elementType = prev.getNode().getElementType();
                // "if" for SmaliMethodReferenceParamList
                if (elementType == SmaliTokens.CLOSE_PAREN) {
                    return METHOD_RETURN_TYPE_REFERENCE;
                }
                prev = prev.getPrevSibling();
            }
            return METHOD_DECLARING_TYPE_REFERENCE;
        } else if (element instanceof SmaliInstruction) {
            Opcode opcode = ((SmaliInstruction) element).getOpcode();
            if (opcode == Opcode.INSTANCE_OF) {
                return UsageType.CLASS_INSTANCE_OF;
            } else if (opcode == Opcode.CHECK_CAST) {
                return UsageType.CLASS_CAST_TO;
            } else if (newArrayInstructions.contains(opcode)) {
                return UsageType.CLASS_NEW_ARRAY;
            } else if (opcode == Opcode.NEW_INSTANCE) {
                return UsageType.CLASS_NEW_OPERATOR;
            } else if (opcode == Opcode.CONST_CLASS) {
                return UsageType.CLASS_CLASS_OBJECT_ACCESS;
            } else if (opcode == Opcode.THROW_VERIFICATION_ERROR) {
                return VERIFICATION_ERROR;
            }
        } else if (element instanceof SmaliSuperStatement || element instanceof SmaliImplementsStatement) {
            return UsageType.CLASS_EXTENDS_IMPLEMENTS_LIST;
        } else if (element instanceof SmaliClassStatement) {
            return CLASS_DECLARATION;
        } else if (element instanceof SmaliMethodParamList) {
            return UsageType.CLASS_METHOD_PARAMETER_DECLARATION;
        } else if (element instanceof SmaliMethodPrototype) {
            return UsageType.CLASS_METHOD_RETURN_TYPE;
        } else if (element instanceof SmaliField) {
            return UsageType.CLASS_FIELD_DECLARATION;
        } else if (element instanceof SmaliCatchStatement) {
            return UsageType.CLASS_CATCH_CLAUSE_PARAMETER_DECLARATION;
        } else if (element instanceof SmaliLocalDebugStatement) {
            return UsageType.CLASS_LOCAL_VAR_DECLARATION;
        } else if (element instanceof SmaliAnnotation) {
            return UsageType.ANNOTATION;
        } else if (element instanceof SmaliLiteral) {
            return LITERAL;
        }
        element = element.getParent();
    }
    return UsageType.UNCLASSIFIED;
}
Also used : Opcode(org.jf.dexlib2.Opcode) IElementType(com.intellij.psi.tree.IElementType) Nullable(org.jetbrains.annotations.Nullable)

Example 28 with Opcode

use of org.jf.dexlib2.Opcode 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

Opcode (org.jf.dexlib2.Opcode)17 Instruction (org.jf.dexlib2.iface.instruction.Instruction)15 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)11 OffsetInstruction (org.jf.dexlib2.iface.instruction.OffsetInstruction)10 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)7 AnalyzedInstruction (org.jf.dexlib2.analysis.AnalyzedInstruction)6 Test (org.junit.Test)6 InvalidItemIndex (org.jf.dexlib2.dexbacked.DexBackedDexFile.InvalidItemIndex)4 Instruction31t (org.jf.dexlib2.iface.instruction.formats.Instruction31t)4 FieldReference (org.jf.dexlib2.iface.reference.FieldReference)4 TypeReference (org.jf.dexlib2.iface.reference.TypeReference)4 InvalidInstructionOffset (org.jf.dexlib2.util.InstructionOffsetMap.InvalidInstructionOffset)4 ExceptionWithContext (org.jf.util.ExceptionWithContext)4 Nonnull (javax.annotation.Nonnull)3 Instruction20bc (org.jf.dexlib2.iface.instruction.formats.Instruction20bc)3 Instruction22c (org.jf.dexlib2.iface.instruction.formats.Instruction22c)3 Reference (org.jf.dexlib2.iface.reference.Reference)3 ImmutableFieldReference (org.jf.dexlib2.immutable.reference.ImmutableFieldReference)3 IOException (java.io.IOException)2 ReferenceType (org.jf.dexlib2.ReferenceType)2