Java Examples for org.jf.dexlib.Code.Analysis.AnalyzedInstruction

The following java examples will help you to understand the usage of org.jf.dexlib.Code.Analysis.AnalyzedInstruction. These source code samples are taken from different open source projects.

Example 1
Project: brut.apktool.smali-master  File: MethodDefinition.java View source code
/**
     * @param instructions The instructions array for this method
     * @param instruction The instruction
     * @return true if the specified instruction is a NOP, and the next instruction is one of the variable sized
     * switch/array data structures
     */
private boolean isInstructionPaddingNop(List<AnalyzedInstruction> instructions, AnalyzedInstruction instruction) {
    if (instruction.getInstruction().opcode != Opcode.NOP || instruction.getInstruction().getFormat().variableSizeFormat) {
        return false;
    }
    if (instruction.getInstructionIndex() == instructions.size() - 1) {
        return false;
    }
    AnalyzedInstruction nextInstruction = instructions.get(instruction.getInstructionIndex() + 1);
    if (nextInstruction.getInstruction().getFormat().variableSizeFormat) {
        return true;
    }
    return false;
}
Example 2
Project: smali-analysis-master  File: MethodDefinition.java View source code
/**
     * @param instructions The instructions array for this method
     * @param instruction The instruction
     * @return true if the specified instruction is a NOP, and the next instruction is one of the variable sized
     * switch/array data structures
     */
private boolean isInstructionPaddingNop(List<AnalyzedInstruction> instructions, AnalyzedInstruction instruction) {
    if (instruction.getInstruction().opcode != Opcode.NOP || instruction.getInstruction().getFormat().variableSizeFormat) {
        return false;
    }
    if (instruction.getInstructionIndex() == instructions.size() - 1) {
        return false;
    }
    AnalyzedInstruction nextInstruction = instructions.get(instruction.getInstructionIndex() + 1);
    if (nextInstruction.getInstruction().getFormat().variableSizeFormat) {
        return true;
    }
    return false;
}
Example 3
Project: ApkAnalyser-master  File: DexReader.java View source code
protected static DexMethod readMethodInfo(DexClass clazz, ClassDataItem.EncodedMethod method, boolean isodex) {
    DexMethod res = null;
    res = new DexMethod(clazz, method);
    int access_dex = method.accessFlags;
    int accessflag = (access_dex & AccessFlags.PUBLIC.getValue()) != 0 ? DexMethod.ACC_PUBLIC : 0;
    accessflag |= (access_dex & AccessFlags.FINAL.getValue()) != 0 ? DexMethod.ACC_FINAL : 0;
    accessflag |= (access_dex & AccessFlags.PRIVATE.getValue()) != 0 ? DexMethod.ACC_PRIVATE : 0;
    accessflag |= (access_dex & AccessFlags.PROTECTED.getValue()) != 0 ? DexMethod.ACC_PROTECTED : 0;
    accessflag |= (access_dex & AccessFlags.STATIC.getValue()) != 0 ? DexMethod.ACC_STATIC : 0;
    accessflag |= (access_dex & AccessFlags.NATIVE.getValue()) != 0 ? DexMethod.ACC_NATIVE : 0;
    accessflag |= (access_dex & AccessFlags.ABSTRACT.getValue()) != 0 ? DexMethod.ACC_ABSTRACT : 0;
    accessflag |= (access_dex & AccessFlags.STRICTFP.getValue()) != 0 ? DexMethod.ACC_STRICT : 0;
    accessflag |= (access_dex & (AccessFlags.SYNCHRONIZED.getValue() | AccessFlags.DECLARED_SYNCHRONIZED.getValue())) != 0 ? DexMethod.ACC_SYNCHRONIZED : 0;
    res.setAccessFlags(accessflag);
    //System.out.println("[DexClassReader] method="+ method.method.getMethodString()+" acc="+accessflag + " oriacc="+ access_dex);
    //ignored
    res.setNameIndex(0);
    //ignored
    res.setDescriptorIndex(0);
    //ignored
    res.setAttributes(null);
    res.m_dexInvokations = new ArrayList<MEMethod.Invokation>();
    res.setExceptions(new MEClass[0]);
    DexReferenceCache desRefCache = ((ApkClassContext) clazz.getResource().getContext()).getDexReferenceCache();
    if ((accessflag & (DexMethod.ACC_ABSTRACT | DexMethod.ACC_NATIVE)) == 0 && method.codeItem != null && clazz.getResource().getContext().isMidlet() == true) {
        try {
            method.codeItem.registerOriginalCount = method.codeItem.getRegisterCount();
            Instruction[] instructions = method.codeItem.getInstructions();
            int currentCodeAddress = 0;
            List<AnalyzedInstruction> analysedInstructions = null;
            if (isodex) {
                prepareClassPath(clazz);
                //TODO: do not support customlized inline table now, could be read from device later.
                MethodAnalyzer methodAnalyser = new MethodAnalyzer(method, isodex, null);
                methodAnalyser.analyze();
                analysedInstructions = methodAnalyser.getInstructions();
            }
            for (int i = 0; i < instructions.length; i++) {
                Instruction instruction = instructions[i];
                instruction.codeAddress = currentCodeAddress;
                instruction.line = -1;
                instruction.deodexedInstruction = analysedInstructions != null ? analysedInstructions.get(i).getInstruction() : instruction;
                //add pc
                currentCodeAddress += instruction.getSize(currentCodeAddress);
                //method invocations
                boolean isVirtual = false;
                boolean isInterface = false;
                boolean isInvoke = false;
                //boolean isOdexInvoke = false;
                switch(instruction.deodexedInstruction.opcode) {
                    /*
                    case INVOKE_VIRTUAL_QUICK:
                    case INVOKE_VIRTUAL_QUICK_RANGE:
                    	isOdexInvoke = true;
                     */
                    case INVOKE_VIRTUAL:
                    case INVOKE_VIRTUAL_RANGE:
                        isVirtual = true;
                        isInvoke = true;
                        break;
                    case INVOKE_INTERFACE:
                    case INVOKE_INTERFACE_RANGE:
                        isInterface = true;
                        isInvoke = true;
                        break;
                    /*
                    case EXECUTE_INLINE:
                    case EXECUTE_INLINE_RANGE:
                    case INVOKE_DIRECT_EMPTY:
                    case INVOKE_SUPER_QUICK:
                    case INVOKE_SUPER_QUICK_RANGE:
                    isOdexInvoke = true;
                     */
                    case INVOKE_SUPER:
                    case INVOKE_DIRECT:
                    case INVOKE_STATIC:
                    case INVOKE_DIRECT_RANGE:
                    case INVOKE_SUPER_RANGE:
                    case INVOKE_STATIC_RANGE:
                        isInvoke = true;
                        break;
                    default:
                        break;
                }
                InstructionWithReference instructionRef = null;
                if (isInvoke == true && instruction instanceof InstructionWithReference) {
                    instructionRef = (InstructionWithReference) instruction;
                }
                if (instructionRef != null) {
                    Item<?> item = instructionRef.getReferencedItem();
                    if (item.getItemType() == ItemType.TYPE_METHOD_ID_ITEM) {
                        MethodIdItem methodIdItem = (MethodIdItem) item;
                        TypeIdItem methodClass = methodIdItem.getContainingClass();
                        MEMethod.Invokation inv = new MEMethod.Invokation(Util.getClassName(methodClass.getTypeDescriptor()), methodIdItem.getMethodName().getStringValue(), methodIdItem.getPrototype().getPrototypeString(), instruction.codeAddress, i, isVirtual, isInterface, instruction);
                        //System.out.println("[DexClassReader] invoke:" + Util.getClassName(methodClass.getTypeDescriptor()) + "->" +methodIdItem.getMethodName().getStringValue() + "->" +methodIdItem.getPrototype().getPrototypeString());
                        res.m_dexInvokations.add(inv);
                        continue;
                    }
                }
                //field accesses
                boolean read = true;
                Item<?> item = null;
                switch(instruction.deodexedInstruction.opcode) {
                    case IPUT:
                    case IPUT_WIDE:
                    case IPUT_OBJECT:
                    case IPUT_BOOLEAN:
                    case IPUT_BYTE:
                    case IPUT_CHAR:
                    case IPUT_SHORT:
                    case SPUT:
                    case SPUT_WIDE:
                    case SPUT_OBJECT:
                    case SPUT_BOOLEAN:
                    case SPUT_BYTE:
                    case SPUT_CHAR:
                    case SPUT_SHORT:
                        read = false;
                        item = ((InstructionWithReference) instruction).getReferencedItem();
                        break;
                    /*
                    case IPUT_QUICK:
                    case IPUT_WIDE_QUICK:
                    case IPUT_OBJECT_QUICK:
                    //for gingerbread
                    case IPUT_VOLATILE:
                    case IPUT_WIDE_VOLATILE:
                    case IPUT_OBJECT_VOLATILE:
                    case SPUT_VOLATILE:
                    case SPUT_WIDE_VOLATILE:
                    case SPUT_OBJECT_VOLATILE:
                    {
                    Instruction deodexedIns = analysedInstructions.get(i).getInstruction();
                    if (deodexedIns instanceof InstructionWithReference){
                    	read = false;
                    	item = ((InstructionWithReference)deodexedIns).getReferencedItem();
                    }
                    break;
                    }
                     */
                    case IGET:
                    case IGET_WIDE:
                    case IGET_OBJECT:
                    case IGET_BOOLEAN:
                    case IGET_BYTE:
                    case IGET_CHAR:
                    case IGET_SHORT:
                    case SGET:
                    case SGET_WIDE:
                    case SGET_OBJECT:
                    case SGET_BOOLEAN:
                    case SGET_BYTE:
                    case SGET_CHAR:
                    case SGET_SHORT:
                        read = true;
                        item = ((InstructionWithReference) instruction).getReferencedItem();
                        break;
                    /*
                    case IGET_QUICK:
                    case IGET_WIDE_QUICK:
                    case IGET_OBJECT_QUICK:
                    //for gingerbread
                    case IGET_VOLATILE:
                    case IGET_WIDE_VOLATILE:
                    case IGET_OBJECT_VOLATILE:
                    case SGET_VOLATILE:
                    case SGET_WIDE_VOLATILE:
                    case SGET_OBJECT_VOLATILE:
                    {
                    Instruction deodexedIns = analysedInstructions.get(i).getInstruction();
                    if (deodexedIns instanceof InstructionWithReference){
                    	read = true;
                    	item = ((InstructionWithReference)deodexedIns).getReferencedItem();
                    }
                    break;
                    }
                     */
                    default:
                        break;
                }
                if (item != null) {
                    desRefCache.addFieldAccessReference(new DexReferenceCache.FieldAccess((FieldIdItem) item, res, instruction, instruction.codeAddress, read));
                    continue;
                }
                //const access for resources
                int literal = 0;
                switch(instruction.deodexedInstruction.opcode) {
                    case CONST:
                        literal = (int) ((Instruction31i) instruction).getLiteral();
                        break;
                    case CONST_HIGH16:
                        literal = (int) ((Instruction21h) instruction).getLiteral() << 16;
                        break;
                    default:
                        break;
                }
                if (literal != 0) {
                    desRefCache.addCodeReference(new DexReferenceCache.LoadConstRes(literal, res, instruction, instruction.codeAddress));
                    continue;
                }
                MethodItem methodItem = InstructionMethodItemFactory.makeInstructionFormatMethodItem(res.getDefinition(), method.codeItem, instruction.codeAddress, instruction);
                if (methodItem instanceof ArrayDataMethodItem) {
                    ArrayDataMethodItem arrayDataItem = (ArrayDataMethodItem) methodItem;
                    Iterator<ArrayDataPseudoInstruction.ArrayElement> iterator = arrayDataItem.instruction.getElements();
                    while (iterator.hasNext()) {
                        ArrayDataPseudoInstruction.ArrayElement element = iterator.next();
                        if (element.elementWidth == 4) {
                            int id = 0;
                            for (int j = 0; j < 4; j++) {
                                id |= (element.buffer[element.bufferIndex + j] & 0xFF) << (j * 8);
                            }
                            //FIXME cannot locate array item correctly
                            desRefCache.addCodeReference(new DexReferenceCache.LoadConstRes(id, res, instructions[0], instructions[0].codeAddress));
                        }
                    }
                }
                String constSting = null;
                switch(instruction.deodexedInstruction.opcode) {
                    case CONST_STRING:
                        constSting = ((StringIdItem) ((Instruction21c) instruction).getReferencedItem()).getStringDataItem().getStringValue();
                        break;
                    case CONST_STRING_JUMBO:
                        constSting = ((StringIdItem) ((Instruction31c) instruction).getReferencedItem()).getStringDataItem().getStringValue();
                        break;
                    default:
                        break;
                }
                if (constSting != null) {
                    desRefCache.addCodeConstString(new DexReferenceCache.LoadConstString(constSting, res, instruction, instruction.codeAddress));
                }
            }
            res.getDefinition().prepareLineNumbers();
            int exceptionSize = 0;
            ArrayList<MEClass> exceptions = new ArrayList<MEClass>();
            DexClassDefinition def = clazz.getDefinition();
            AnnotationSetItem annotationSet = def.getAnnotationSetItem(method);
            if (annotationSet != null) {
                for (AnnotationItem annotationItem : annotationSet.getAnnotations()) {
                    if (annotationItem.getEncodedAnnotation().annotationType.getTypeDescriptor().equals("Ldalvik/annotation/Throws;")) {
                        AnnotationEncodedSubValue encodedAnnotation = annotationItem.getEncodedAnnotation();
                        for (int m = 0; m < encodedAnnotation.names.length; m++) {
                            //System.out.println("[DexClassReader] annType:"+ annotationItem.getEncodedAnnotation().annotationType.getTypeDescriptor());
                            EncodedValue v = encodedAnnotation.values[m];
                            if (v.getValueType() == ValueType.VALUE_ARRAY) {
                                ArrayEncodedValue array = (ArrayEncodedValue) v;
                                EncodedValue[] values = array.values;
                                for (EncodedValue encodedValue : values) {
                                    String name = null;
                                    if (encodedValue.getValueType() == ValueType.VALUE_TYPE) {
                                        name = ((TypeEncodedValue) encodedValue).value.getTypeDescriptor();
                                        MEClass exceptionClass;
                                        try {
                                            //bugfix, if a new exception class throw itself in some method, it will overflow.
                                            if (Util.getClassName(name).equals(clazz.getName())) {
                                                exceptionClass = clazz;
                                            } else {
                                                exceptionClass = clazz.getResource().getContext().getMEClass(Util.getClassName(name));
                                            }
                                        } catch (ClassNotFoundException cnfe) {
                                            exceptionClass = new UnknownClass(Util.getClassName(name), clazz.getResource());
                                        }
                                        exceptions.add(exceptionClass);
                                        exceptionSize++;
                                    }
                                }
                            }
                        }
                    }
                }
                res.setExceptions(exceptions.toArray(new MEClass[exceptionSize]));
            }
        } catch (IllegalArgumentException e) {
            if (!e.getMessage().equals("The method has no code")) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return res;
}
Example 4
Project: MalwareDetector-master  File: MethodDefinition.java View source code
/**
     * @param instructions The instructions array for this method
     * @param instruction The instruction
     * @return true if the specified instruction is a NOP, and the next instruction is one of the variable sized
     * switch/array data structures
     */
private boolean isInstructionPaddingNop(List<AnalyzedInstruction> instructions, AnalyzedInstruction instruction) {
    if (instruction.getInstruction().opcode != Opcode.NOP || instruction.getInstruction().getFormat().variableSizeFormat) {
        return false;
    }
    if (instruction.getInstructionIndex() == instructions.size() - 1) {
        return false;
    }
    AnalyzedInstruction nextInstruction = instructions.get(instruction.getInstructionIndex() + 1);
    if (nextInstruction.getInstruction().getFormat().variableSizeFormat) {
        return true;
    }
    return false;
}
Example 5
Project: brut.apktool-master  File: MethodDefinition.java View source code
/**
     * @param instructions The instructions array for this method
     * @param instruction The instruction
     * @return true if the specified instruction is a NOP, and the next instruction is one of the variable sized
     * switch/array data structures
     */
private boolean isInstructionPaddingNop(List<AnalyzedInstruction> instructions, AnalyzedInstruction instruction) {
    if (instruction.getInstruction().opcode != Opcode.NOP || instruction.getInstruction().getFormat().variableSizeFormat) {
        return false;
    }
    if (instruction.getInstructionIndex() == instructions.size() - 1) {
        return false;
    }
    AnalyzedInstruction nextInstruction = instructions.get(instruction.getInstructionIndex() + 1);
    if (nextInstruction.getInstruction().getFormat().variableSizeFormat) {
        return true;
    }
    return false;
}
Example 6
Project: RsApktool-master  File: MethodDefinition.java View source code
/**
     * @param instructions The instructions array for this method
     * @param instruction The instruction
     * @return true if the specified instruction is a NOP, and the next instruction is one of the variable sized
     * switch/array data structures
     */
private boolean isInstructionPaddingNop(List<AnalyzedInstruction> instructions, AnalyzedInstruction instruction) {
    if (instruction.getInstruction().opcode != Opcode.NOP || instruction.getInstruction().getFormat().variableSizeFormat) {
        return false;
    }
    if (instruction.getInstructionIndex() == instructions.size() - 1) {
        return false;
    }
    AnalyzedInstruction nextInstruction = instructions.get(instruction.getInstructionIndex() + 1);
    if (nextInstruction.getInstruction().getFormat().variableSizeFormat) {
        return true;
    }
    return false;
}
Example 7
Project: apktool4android-master  File: MethodDefinition.java View source code
/**
     * @param instructions The instructions array for this method
     * @param instruction The instruction
     * @return true if the specified instruction is a NOP, and the next instruction is one of the variable sized
     * switch/array data structures
     */
private boolean isInstructionPaddingNop(List<AnalyzedInstruction> instructions, AnalyzedInstruction instruction) {
    if (instruction.getInstruction().opcode != Opcode.NOP || instruction.getInstruction().getFormat().variableSizeFormat) {
        return false;
    }
    if (instruction.getInstructionIndex() == instructions.size() - 1) {
        return false;
    }
    AnalyzedInstruction nextInstruction = instructions.get(instruction.getInstructionIndex() + 1);
    if (nextInstruction.getInstruction().getFormat().variableSizeFormat) {
        return true;
    }
    return false;
}