Search in sources :

Example 16 with AnalyzedInstruction

use of org.jf.dexlib2.analysis.AnalyzedInstruction in project atlas by alibaba.

the class PreInstructionRegisterInfoMethodItem method writeFullMerge.

private void writeFullMerge(IndentingWriter writer, int registerNum) throws IOException {
    registerFormatter.writeTo(writer, registerNum);
    writer.write('=');
    analyzedInstruction.getPreInstructionRegisterType(registerNum).writeTo(writer);
    writer.write(":merge{");
    boolean first = true;
    for (AnalyzedInstruction predecessor : analyzedInstruction.getPredecessors()) {
        RegisterType predecessorRegisterType = predecessor.getPostInstructionRegisterType(registerNum);
        if (!first) {
            writer.write(',');
        }
        if (predecessor.getInstructionIndex() == -1) {
            //the fake "StartOfMethod" instruction
            writer.write("Start:");
        } else {
            writer.write("0x");
            writer.printUnsignedLongAsHex(methodAnalyzer.getInstructionAddress(predecessor));
            writer.write(':');
        }
        predecessorRegisterType.writeTo(writer);
        first = false;
    }
    writer.write('}');
}
Also used : RegisterType(org.jf.dexlib2.analysis.RegisterType) AnalyzedInstruction(org.jf.dexlib2.analysis.AnalyzedInstruction)

Example 17 with AnalyzedInstruction

use of org.jf.dexlib2.analysis.AnalyzedInstruction in project smali by JesusFreke.

the class MethodAnalyzer method analyzeIputIgetQuick.

private boolean analyzeIputIgetQuick(@Nonnull AnalyzedInstruction analyzedInstruction) {
    Instruction22cs instruction = (Instruction22cs) analyzedInstruction.instruction;
    int fieldOffset = instruction.getFieldOffset();
    RegisterType objectRegisterType = getAndCheckSourceRegister(analyzedInstruction, instruction.getRegisterB(), ReferenceOrUninitCategories);
    if (objectRegisterType.category == RegisterType.NULL) {
        return false;
    }
    TypeProto objectRegisterTypeProto = objectRegisterType.type;
    assert objectRegisterTypeProto != null;
    TypeProto classTypeProto = classPath.getClass(objectRegisterTypeProto.getType());
    FieldReference resolvedField = classTypeProto.getFieldByOffset(fieldOffset);
    if (resolvedField == null) {
        throw new AnalysisException("Could not resolve the field in class %s at offset %d", objectRegisterType.type.getType(), fieldOffset);
    }
    ClassDef thisClass = classPath.getClassDef(method.getDefiningClass());
    if (!TypeUtils.canAccessClass(thisClass.getType(), classPath.getClassDef(resolvedField.getDefiningClass()))) {
        // the class is not accessible. So we start looking at objectRegisterTypeProto (which may be different
        // than resolvedField.getDefiningClass()), and walk up the class hierarchy.
        ClassDef fieldClass = classPath.getClassDef(objectRegisterTypeProto.getType());
        while (!TypeUtils.canAccessClass(thisClass.getType(), fieldClass)) {
            String superclass = fieldClass.getSuperclass();
            if (superclass == null) {
                throw new ExceptionWithContext("Couldn't find accessible class while resolving field %s", ReferenceUtil.getShortFieldDescriptor(resolvedField));
            }
            fieldClass = classPath.getClassDef(superclass);
        }
        // fieldClass is now the first accessible class found. Now. we need to make sure that the field is
        // actually valid for this class
        FieldReference newResolvedField = classPath.getClass(fieldClass.getType()).getFieldByOffset(fieldOffset);
        if (newResolvedField == null) {
            throw new ExceptionWithContext("Couldn't find accessible class while resolving field %s", ReferenceUtil.getShortFieldDescriptor(resolvedField));
        }
        resolvedField = new ImmutableFieldReference(fieldClass.getType(), newResolvedField.getName(), newResolvedField.getType());
    }
    String fieldType = resolvedField.getType();
    Opcode opcode = classPath.getFieldInstructionMapper().getAndCheckDeodexedOpcode(fieldType, instruction.getOpcode());
    Instruction22c deodexedInstruction = new ImmutableInstruction22c(opcode, (byte) instruction.getRegisterA(), (byte) instruction.getRegisterB(), resolvedField);
    analyzedInstruction.setDeodexedInstruction(deodexedInstruction);
    analyzeInstruction(analyzedInstruction);
    return true;
}
Also used : FieldReference(org.jf.dexlib2.iface.reference.FieldReference) ImmutableFieldReference(org.jf.dexlib2.immutable.reference.ImmutableFieldReference) ImmutableFieldReference(org.jf.dexlib2.immutable.reference.ImmutableFieldReference) Opcode(org.jf.dexlib2.Opcode) ExceptionWithContext(org.jf.util.ExceptionWithContext)

Example 18 with AnalyzedInstruction

use of org.jf.dexlib2.analysis.AnalyzedInstruction in project smali by JesusFreke.

the class MethodAnalyzer method analyzeExecuteInlineRange.

private void analyzeExecuteInlineRange(@Nonnull AnalyzedInstruction analyzedInstruction) {
    if (inlineResolver == null) {
        throw new AnalysisException("Cannot analyze an odexed instruction unless we are deodexing");
    }
    Instruction3rmi instruction = (Instruction3rmi) analyzedInstruction.instruction;
    Method resolvedMethod = inlineResolver.resolveExecuteInline(analyzedInstruction);
    Opcode deodexedOpcode;
    int acccessFlags = resolvedMethod.getAccessFlags();
    if (AccessFlags.STATIC.isSet(acccessFlags)) {
        deodexedOpcode = Opcode.INVOKE_STATIC_RANGE;
    } else if (AccessFlags.PRIVATE.isSet(acccessFlags)) {
        deodexedOpcode = Opcode.INVOKE_DIRECT_RANGE;
    } else {
        deodexedOpcode = Opcode.INVOKE_VIRTUAL_RANGE;
    }
    Instruction3rc deodexedInstruction = new ImmutableInstruction3rc(deodexedOpcode, instruction.getStartRegister(), instruction.getRegisterCount(), resolvedMethod);
    analyzedInstruction.setDeodexedInstruction(deodexedInstruction);
    analyzeInstruction(analyzedInstruction);
}
Also used : Opcode(org.jf.dexlib2.Opcode)

Example 19 with AnalyzedInstruction

use of org.jf.dexlib2.analysis.AnalyzedInstruction in project smali by JesusFreke.

the class MethodAnalyzer method analyzeExecuteInline.

private void analyzeExecuteInline(@Nonnull AnalyzedInstruction analyzedInstruction) {
    if (inlineResolver == null) {
        throw new AnalysisException("Cannot analyze an odexed instruction unless we are deodexing");
    }
    Instruction35mi instruction = (Instruction35mi) analyzedInstruction.instruction;
    Method resolvedMethod = inlineResolver.resolveExecuteInline(analyzedInstruction);
    Opcode deodexedOpcode;
    int acccessFlags = resolvedMethod.getAccessFlags();
    if (AccessFlags.STATIC.isSet(acccessFlags)) {
        deodexedOpcode = Opcode.INVOKE_STATIC;
    } else if (AccessFlags.PRIVATE.isSet(acccessFlags)) {
        deodexedOpcode = Opcode.INVOKE_DIRECT;
    } else {
        deodexedOpcode = Opcode.INVOKE_VIRTUAL;
    }
    Instruction35c deodexedInstruction = new ImmutableInstruction35c(deodexedOpcode, instruction.getRegisterCount(), instruction.getRegisterC(), instruction.getRegisterD(), instruction.getRegisterE(), instruction.getRegisterF(), instruction.getRegisterG(), resolvedMethod);
    analyzedInstruction.setDeodexedInstruction(deodexedInstruction);
    analyzeInstruction(analyzedInstruction);
}
Also used : Opcode(org.jf.dexlib2.Opcode)

Example 20 with AnalyzedInstruction

use of org.jf.dexlib2.analysis.AnalyzedInstruction in project smali by JesusFreke.

the class MethodAnalyzer method analyzeInvokeVirtual.

private boolean analyzeInvokeVirtual(@Nonnull AnalyzedInstruction analyzedInstruction, boolean isRange) {
    MethodReference targetMethod;
    if (!normalizeVirtualMethods) {
        return true;
    }
    if (isRange) {
        Instruction3rc instruction = (Instruction3rc) analyzedInstruction.instruction;
        targetMethod = (MethodReference) instruction.getReference();
    } else {
        Instruction35c instruction = (Instruction35c) analyzedInstruction.instruction;
        targetMethod = (MethodReference) instruction.getReference();
    }
    MethodReference replacementMethod = normalizeMethodReference(targetMethod);
    if (replacementMethod == null || replacementMethod.equals(targetMethod)) {
        return true;
    }
    Instruction deodexedInstruction;
    if (isRange) {
        Instruction3rc instruction = (Instruction3rc) analyzedInstruction.instruction;
        deodexedInstruction = new ImmutableInstruction3rc(instruction.getOpcode(), instruction.getStartRegister(), instruction.getRegisterCount(), replacementMethod);
    } else {
        Instruction35c instruction = (Instruction35c) analyzedInstruction.instruction;
        deodexedInstruction = new ImmutableInstruction35c(instruction.getOpcode(), instruction.getRegisterCount(), instruction.getRegisterC(), instruction.getRegisterD(), instruction.getRegisterE(), instruction.getRegisterF(), instruction.getRegisterG(), replacementMethod);
    }
    analyzedInstruction.setDeodexedInstruction(deodexedInstruction);
    return true;
}
Also used : BaseMethodReference(org.jf.dexlib2.base.reference.BaseMethodReference) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) MethodReference(org.jf.dexlib2.iface.reference.MethodReference)

Aggregations

Opcode (org.jf.dexlib2.Opcode)8 TypeReference (org.jf.dexlib2.iface.reference.TypeReference)7 AnalyzedInstruction (org.jf.dexlib2.analysis.AnalyzedInstruction)6 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)6 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)6 BuilderInstruction21t (org.jf.dexlib2.builder.instruction.BuilderInstruction21t)6 BuilderInstruction22c (org.jf.dexlib2.builder.instruction.BuilderInstruction22c)6 ClassDef (org.jf.dexlib2.iface.ClassDef)6 DexFile (org.jf.dexlib2.iface.DexFile)6 Method (org.jf.dexlib2.iface.Method)6 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)6 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)6 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)6 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)6 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)6 ImmutableTypeReference (org.jf.dexlib2.immutable.reference.ImmutableTypeReference)6 Test (org.junit.Test)6 FieldReference (org.jf.dexlib2.iface.reference.FieldReference)5 ImmutableFieldReference (org.jf.dexlib2.immutable.reference.ImmutableFieldReference)5 BaseMethodReference (org.jf.dexlib2.base.reference.BaseMethodReference)4