Search in sources :

Example 1 with ImmutableInstruction35c

use of org.jf.dexlib2.immutable.instruction.ImmutableInstruction35c in project atlas by alibaba.

the class InsTructionsReIClassDef method reInstructions.

@Override
protected Iterable<? extends Instruction> reInstructions(Iterable<? extends Instruction> instructions) {
    final List<Instruction> reinstructions = new ArrayList<Instruction>();
    for (final Instruction instruction : instructions) {
        if (instruction instanceof ReferenceInstruction) {
            Opcode opcode = instruction.getOpcode();
            if (opcode.referenceType == ReferenceType.METHOD) {
                boolean isBasic = false;
                MethodReference methodReference = null;
                try {
                    methodReference = (MethodReference) ((ReferenceInstruction) instruction).getReference();
                    if (DexObfuscatedTool.aliProguard && methodReference.getName().equals("getName") && methodReference.getDefiningClass().equals("Ljava/lang/Class;") && opcode.equals(Opcode.INVOKE_VIRTUAL)) {
                        opcode = Opcode.INVOKE_STATIC;
                        List<CharSequence> list = new ArrayList<>();
                        list.add("Ljava/lang/Class;");
                        Iterable iterable = new Iterable() {

                            @Override
                            public Iterator iterator() {
                                return list.iterator();
                            }
                        };
                        ImmutableMethodReference immutableMethodReference = new ImmutableMethodReference("Lcom/ali/mobisecenhance/ReflectMap;", "getName", iterable, "Ljava/lang/String;");
                        reinstructions.add(new ImmutableInstruction35c(opcode, ((Instruction35c) instruction).getRegisterCount(), ((Instruction35c) instruction).getRegisterC(), ((Instruction35c) instruction).getRegisterD(), ((Instruction35c) instruction).getRegisterE(), ((Instruction35c) instruction).getRegisterF(), ((Instruction35c) instruction).getRegisterG(), immutableMethodReference));
                        continue;
                    }
                    if (methodReference.getDefiningClass().contains("Ljava/lang") || methodReference.getDefiningClass().startsWith("Ljava/util/") || methodReference.getDefiningClass().startsWith("[Ljava/lang")) {
                        reinstructions.add(ImmutableInstruction.of(instruction));
                        continue;
                    }
                    String returnType = methodReference.getReturnType();
                    boolean isArray = false;
                    if (returnType.startsWith("[")) {
                        isArray = true;
                    }
                    String methodName = methodReference.getName();
                    if (basicType.containsKey(returnType)) {
                        isBasic = true;
                    }
                    List<? extends CharSequence> paramTypes = methodReference.getParameterTypes();
                    List<CharSequence> dalvikParamTypes = new ArrayList<CharSequence>();
                    List<CharSequence> newParamTypes = new ArrayList<CharSequence>();
                    for (CharSequence charSequence : paramTypes) {
                        if (basicType.containsKey(charSequence.toString())) {
                            newParamTypes.add(charSequence);
                            dalvikParamTypes.add(basicType.get(charSequence.toString()));
                            continue;
                        }
                        boolean isArray1 = charSequence.toString().startsWith("[");
                        dalvikParamTypes.add(DefineUtils.getDalvikClassName(charSequence.toString()) + (isArray ? "[]" : ""));
                        newParamTypes.add(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(charSequence.toString())).className, isArray1));
                    }
                    final ImmutableMethodReference immutableReference = new ImmutableMethodReference(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(methodReference.getDefiningClass())).className, methodReference.getDefiningClass().startsWith("[")), classProcessor.methodProcess(DefineUtils.getDalvikClassName(methodReference.getDefiningClass()), methodReference.getName(), isBasic ? basicType.get(methodReference.getReturnType()) : DefineUtils.getDalvikClassName(methodReference.getReturnType()) + (isArray ? "[]" : ""), StringUtils.join(dalvikParamTypes.toArray(), ",")).methodName, newParamTypes, isBasic ? returnType : DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(methodReference.getReturnType())).className, methodReference.getReturnType().startsWith("[")));
                    if (instruction instanceof Instruction3rc) {
                        reinstructions.add(new ImmutableInstruction3rc(instruction.getOpcode(), ((Instruction3rc) instruction).getStartRegister(), ((Instruction3rc) instruction).getRegisterCount(), immutableReference));
                    } else if (instruction instanceof Instruction20bc) {
                        reinstructions.add(new ImmutableInstruction20bc(instruction.getOpcode(), ((Instruction20bc) instruction).getVerificationError(), immutableReference));
                    } else if (instruction instanceof Instruction21c) {
                        reinstructions.add(new ImmutableInstruction21c(instruction.getOpcode(), ((Instruction21c) instruction).getRegisterA(), immutableReference));
                    } else if (instruction instanceof Instruction22c) {
                        reinstructions.add(new ImmutableInstruction22c(instruction.getOpcode(), ((Instruction22c) instruction).getRegisterA(), ((Instruction22c) instruction).getRegisterB(), immutableReference));
                    } else if (instruction instanceof Instruction31c) {
                        reinstructions.add(new ImmutableInstruction31c(instruction.getOpcode(), ((Instruction31c) instruction).getRegisterA(), immutableReference));
                    } else if (instruction instanceof Instruction35c) {
                        reinstructions.add(new ImmutableInstruction35c(instruction.getOpcode(), ((Instruction35c) instruction).getRegisterCount(), ((Instruction35c) instruction).getRegisterC(), ((Instruction35c) instruction).getRegisterD(), ((Instruction35c) instruction).getRegisterE(), ((Instruction35c) instruction).getRegisterF(), ((Instruction35c) instruction).getRegisterG(), immutableReference));
                    }
                } catch (Exception e) {
                }
            } else if (opcode.referenceType == ReferenceType.FIELD) {
                FieldReference fieldReference = null;
                boolean isBasic = false;
                boolean isBasicArray = false;
                fieldReference = (FieldReference) ((ReferenceInstruction) instruction).getReference();
                if (fieldReference.getDefiningClass().startsWith("Ljava/lang/") || fieldReference.getDefiningClass().startsWith("Ljava/util/") || fieldReference.getDefiningClass().startsWith("[Ljava/lang/")) {
                    reinstructions.add(ImmutableInstruction.of(instruction));
                    continue;
                }
                if (basicType.containsKey(fieldReference.getType())) {
                    isBasic = true;
                }
                final ImmutableFieldReference immutableFieldReference = new ImmutableFieldReference(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(fieldReference.getDefiningClass())).className, fieldReference.getDefiningClass().startsWith("[")), classProcessor.filedProcess(DefineUtils.getDalvikClassName(fieldReference.getDefiningClass()), isBasic ? basicType.get(fieldReference.getType()) : DefineUtils.getDalvikClassName(fieldReference.getType()), fieldReference.getName()).fieldName, isBasic ? fieldReference.getType() : DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(fieldReference.getType())).className, fieldReference.getType().startsWith("[")));
                if (instruction instanceof Instruction3rc) {
                    reinstructions.add(new ImmutableInstruction3rc(instruction.getOpcode(), ((Instruction3rc) instruction).getStartRegister(), ((Instruction3rc) instruction).getRegisterCount(), immutableFieldReference));
                } else if (instruction instanceof Instruction20bc) {
                    reinstructions.add(new ImmutableInstruction20bc(instruction.getOpcode(), ((Instruction20bc) instruction).getVerificationError(), immutableFieldReference));
                } else if (instruction instanceof Instruction21c) {
                    reinstructions.add(new ImmutableInstruction21c(instruction.getOpcode(), ((Instruction21c) instruction).getRegisterA(), immutableFieldReference));
                } else if (instruction instanceof Instruction22c) {
                    reinstructions.add(new ImmutableInstruction22c(instruction.getOpcode(), ((Instruction22c) instruction).getRegisterA(), ((Instruction22c) instruction).getRegisterB(), immutableFieldReference));
                } else if (instruction instanceof Instruction31c) {
                    reinstructions.add(new ImmutableInstruction31c(instruction.getOpcode(), ((Instruction31c) instruction).getRegisterA(), immutableFieldReference));
                } else if (instruction instanceof Instruction35c) {
                    reinstructions.add(new ImmutableInstruction35c(instruction.getOpcode(), ((Instruction35c) instruction).getRegisterCount(), ((Instruction35c) instruction).getRegisterC(), ((Instruction35c) instruction).getRegisterD(), ((Instruction35c) instruction).getRegisterE(), ((Instruction35c) instruction).getRegisterF(), ((Instruction35c) instruction).getRegisterG(), immutableFieldReference));
                }
            } else if (opcode.referenceType == ReferenceType.TYPE) {
                TypeReference typeReference = (TypeReference) ((ReferenceInstruction) instruction).getReference();
                String type = typeReference.getType();
                if (!basicType.containsKey(type) && !type.startsWith("Ljava/lang") && !type.startsWith("Ljava/util/") && !type.startsWith("[Ljava/lang")) {
                    type = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(type)).className, type.startsWith("["));
                }
                ImmutableTypeReference immutableTypeReference = new ImmutableTypeReference(type);
                if (instruction instanceof Instruction3rc) {
                    reinstructions.add(new ImmutableInstruction3rc(instruction.getOpcode(), ((Instruction3rc) instruction).getStartRegister(), ((Instruction3rc) instruction).getRegisterCount(), immutableTypeReference));
                } else if (instruction instanceof Instruction20bc) {
                    reinstructions.add(new ImmutableInstruction20bc(instruction.getOpcode(), ((Instruction20bc) instruction).getVerificationError(), immutableTypeReference));
                } else if (instruction instanceof Instruction21c) {
                    reinstructions.add(new ImmutableInstruction21c(instruction.getOpcode(), ((Instruction21c) instruction).getRegisterA(), immutableTypeReference));
                } else if (instruction instanceof Instruction22c) {
                    reinstructions.add(new ImmutableInstruction22c(instruction.getOpcode(), ((Instruction22c) instruction).getRegisterA(), ((Instruction22c) instruction).getRegisterB(), immutableTypeReference));
                } else if (instruction instanceof Instruction31c) {
                    reinstructions.add(new ImmutableInstruction31c(instruction.getOpcode(), ((Instruction31c) instruction).getRegisterA(), immutableTypeReference));
                } else if (instruction instanceof Instruction35c) {
                    reinstructions.add(new ImmutableInstruction35c(instruction.getOpcode(), ((Instruction35c) instruction).getRegisterCount(), ((Instruction35c) instruction).getRegisterC(), ((Instruction35c) instruction).getRegisterD(), ((Instruction35c) instruction).getRegisterE(), ((Instruction35c) instruction).getRegisterF(), ((Instruction35c) instruction).getRegisterG(), immutableTypeReference));
                }
            } else if (opcode.referenceType == ReferenceType.STRING) {
                StringReference stringReference = (StringReference) ((ReferenceInstruction) instruction).getReference();
                String type = stringReference.getString();
                // if (!basicType.contains(type) && !type.startsWith("[Ljava/lang")) {
                // type = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(type)).className);
                // }
                ImmutableStringReference immutableStringReference = new ImmutableStringReference(type);
                if (instruction instanceof Instruction3rc) {
                    reinstructions.add(new ImmutableInstruction3rc(instruction.getOpcode(), ((Instruction3rc) instruction).getStartRegister(), ((Instruction3rc) instruction).getRegisterCount(), immutableStringReference));
                } else if (instruction instanceof Instruction20bc) {
                    reinstructions.add(new ImmutableInstruction20bc(instruction.getOpcode(), ((Instruction20bc) instruction).getVerificationError(), immutableStringReference));
                } else if (instruction instanceof Instruction21c) {
                    reinstructions.add(new ImmutableInstruction21c(instruction.getOpcode(), ((Instruction21c) instruction).getRegisterA(), immutableStringReference));
                } else if (instruction instanceof Instruction22c) {
                    reinstructions.add(new ImmutableInstruction22c(instruction.getOpcode(), ((Instruction22c) instruction).getRegisterA(), ((Instruction22c) instruction).getRegisterB(), immutableStringReference));
                } else if (instruction instanceof Instruction31c) {
                    reinstructions.add(new ImmutableInstruction31c(instruction.getOpcode(), ((Instruction31c) instruction).getRegisterA(), immutableStringReference));
                } else if (instruction instanceof Instruction35c) {
                    reinstructions.add(new ImmutableInstruction35c(instruction.getOpcode(), ((Instruction35c) instruction).getRegisterCount(), ((Instruction35c) instruction).getRegisterC(), ((Instruction35c) instruction).getRegisterD(), ((Instruction35c) instruction).getRegisterE(), ((Instruction35c) instruction).getRegisterF(), ((Instruction35c) instruction).getRegisterG(), immutableStringReference));
                }
            } else {
                reinstructions.add(ImmutableInstruction.of(instruction));
            }
        } else {
            reinstructions.add(ImmutableInstruction.of(instruction));
        }
    }
    return new Iterable<Instruction>() {

        @Override
        public Iterator<Instruction> iterator() {
            return reinstructions.iterator();
        }
    };
}
Also used : ArrayList(java.util.ArrayList) ImmutableFieldReference(org.jf.dexlib2.immutable.reference.ImmutableFieldReference) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) Opcode(org.jf.dexlib2.Opcode) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) TypeReference(org.jf.dexlib2.iface.reference.TypeReference) ImmutableTypeReference(org.jf.dexlib2.immutable.reference.ImmutableTypeReference) ImmutableTypeReference(org.jf.dexlib2.immutable.reference.ImmutableTypeReference) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) ImmutableFieldReference(org.jf.dexlib2.immutable.reference.ImmutableFieldReference) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) ImmutableStringReference(org.jf.dexlib2.immutable.reference.ImmutableStringReference) ImmutableStringReference(org.jf.dexlib2.immutable.reference.ImmutableStringReference) StringReference(org.jf.dexlib2.iface.reference.StringReference) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) MethodReference(org.jf.dexlib2.iface.reference.MethodReference)

Example 2 with ImmutableInstruction35c

use of org.jf.dexlib2.immutable.instruction.ImmutableInstruction35c in project smali by JesusFreke.

the class InstructionOffsetMapTest method testInstructionOffsetMap.

@Test
public void testInstructionOffsetMap() {
    ImmutableList<ImmutableInstruction> instructions = ImmutableList.of(/*00: 0x00*/
    new ImmutableInstruction10t(Opcode.GOTO, 1), /*01: 0x01*/
    new ImmutableInstruction10x(Opcode.NOP), /*02: 0x02*/
    new ImmutableInstruction11n(Opcode.CONST_4, 2, 3), /*03: 0x03*/
    new ImmutableInstruction11x(Opcode.RETURN, 4), /*04: 0x04*/
    new ImmutableInstruction12x(Opcode.ARRAY_LENGTH, 5, 6), /*05: 0x05*/
    new ImmutableInstruction20t(Opcode.GOTO_16, 7), /*06: 0x07*/
    new ImmutableInstruction21c(Opcode.CONST_STRING, 8, new ImmutableStringReference("blah")), /*07: 0x09*/
    new ImmutableInstruction21ih(Opcode.CONST_HIGH16, 9, 0x10000), /*08: 0x0b*/
    new ImmutableInstruction21lh(Opcode.CONST_WIDE_HIGH16, 10, 0x1000000000000L), /*09: 0x0d*/
    new ImmutableInstruction21s(Opcode.CONST_16, 11, 12), /*10: 0x0f*/
    new ImmutableInstruction21t(Opcode.IF_EQZ, 12, 13), /*11: 0x11*/
    new ImmutableInstruction22b(Opcode.ADD_INT_LIT8, 14, 15, 16), /*12: 0x13*/
    new ImmutableInstruction22c(Opcode.INSTANCE_OF, 0, 1, new ImmutableTypeReference("Ltype;")), /*13: 0x15*/
    new ImmutableInstruction22s(Opcode.ADD_INT_LIT16, 2, 3, 17), /*14: 0x17*/
    new ImmutableInstruction22t(Opcode.IF_EQ, 4, 5, 18), /*15: 0x19*/
    new ImmutableInstruction22x(Opcode.MOVE_FROM16, 19, 20), /*16: 0x1b*/
    new ImmutableInstruction23x(Opcode.AGET, 21, 22, 23), /*17: 0x1d*/
    new ImmutableInstruction30t(Opcode.GOTO_32, 24), /*18: 0x20*/
    new ImmutableInstruction31c(Opcode.CONST_STRING_JUMBO, 25, new ImmutableStringReference("this is a string")), /*19: 0x23*/
    new ImmutableInstruction31i(Opcode.CONST, 26, 27), /*20: 0x26*/
    new ImmutableInstruction31t(Opcode.FILL_ARRAY_DATA, 28, 29), /*21: 0x29*/
    new ImmutableInstruction32x(Opcode.MOVE_16, 30, 31), /*22: 0x2c*/
    new ImmutableInstruction35c(Opcode.FILLED_NEW_ARRAY, 0, 0, 0, 0, 0, 0, new ImmutableTypeReference("Ltype;")), /*23: 0x2f*/
    new ImmutableInstruction3rc(Opcode.FILLED_NEW_ARRAY_RANGE, 0, 0, new ImmutableTypeReference("Ltype;")), /*24: 0x32*/
    new ImmutableInstruction51l(Opcode.CONST_WIDE, 32, 33), /*25: 0x37*/
    new ImmutableInstruction10t(Opcode.GOTO, 1));
    ImmutableMethodImplementation impl = new ImmutableMethodImplementation(33, instructions, null, null);
    InstructionOffsetMap instructionOffsetMap = new InstructionOffsetMap(instructions);
    int[] expectedOffsets = new int[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, 0x09, 0x0b, 0x0d, 0x0f, 0x11, 0x13, 0x15, 0x17, 0x19, 0x1b, 0x1d, 0x20, 0x23, 0x26, 0x29, 0x2c, 0x2f, 0x32, 0x37 };
    for (int i = 0; i < instructions.size(); i++) {
        Assert.assertEquals(expectedOffsets[i], instructionOffsetMap.getInstructionCodeOffset(i));
        Assert.assertEquals(i, instructionOffsetMap.getInstructionIndexAtCodeOffset(expectedOffsets[i], true));
        Assert.assertEquals(i, instructionOffsetMap.getInstructionIndexAtCodeOffset(expectedOffsets[i], false));
    }
    int instructionIndex = -1;
    for (int codeOffset = 0; codeOffset <= expectedOffsets[expectedOffsets.length - 1]; codeOffset++) {
        if (codeOffset == expectedOffsets[instructionIndex + 1]) {
            // this offset is at the beginning of an instruction
            instructionIndex++;
        } else {
            // this offset is in the middle of an instruction
            Assert.assertEquals(instructionIndex, instructionOffsetMap.getInstructionIndexAtCodeOffset(codeOffset, false));
            try {
                instructionOffsetMap.getInstructionIndexAtCodeOffset(codeOffset, true);
                Assert.fail(String.format("Exception exception didn't occur for code offset 0x%x", codeOffset));
            } catch (ExceptionWithContext ex) {
            // expected exception
            }
        }
    }
    Assert.assertEquals(expectedOffsets.length - 1, instructionOffsetMap.getInstructionIndexAtCodeOffset(expectedOffsets[expectedOffsets.length - 1] + 1, false));
    Assert.assertEquals(expectedOffsets.length - 1, instructionOffsetMap.getInstructionIndexAtCodeOffset(expectedOffsets[expectedOffsets.length - 1] + 10, false));
}
Also used : ExceptionWithContext(org.jf.util.ExceptionWithContext) ImmutableMethodImplementation(org.jf.dexlib2.immutable.ImmutableMethodImplementation) ImmutableTypeReference(org.jf.dexlib2.immutable.reference.ImmutableTypeReference) ImmutableStringReference(org.jf.dexlib2.immutable.reference.ImmutableStringReference) Test(org.junit.Test)

Example 3 with ImmutableInstruction35c

use of org.jf.dexlib2.immutable.instruction.ImmutableInstruction35c 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 4 with ImmutableInstruction35c

use of org.jf.dexlib2.immutable.instruction.ImmutableInstruction35c 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)

Example 5 with ImmutableInstruction35c

use of org.jf.dexlib2.immutable.instruction.ImmutableInstruction35c in project smali by JesusFreke.

the class MethodAnalyzer method analyzeInvokeVirtualQuick.

private boolean analyzeInvokeVirtualQuick(@Nonnull AnalyzedInstruction analyzedInstruction, boolean isSuper, boolean isRange) {
    int methodIndex;
    int objectRegister;
    if (isRange) {
        Instruction3rms instruction = (Instruction3rms) analyzedInstruction.instruction;
        methodIndex = instruction.getVtableIndex();
        objectRegister = instruction.getStartRegister();
    } else {
        Instruction35ms instruction = (Instruction35ms) analyzedInstruction.instruction;
        methodIndex = instruction.getVtableIndex();
        objectRegister = instruction.getRegisterC();
    }
    RegisterType objectRegisterType = getAndCheckSourceRegister(analyzedInstruction, objectRegister, ReferenceOrUninitCategories);
    TypeProto objectRegisterTypeProto = objectRegisterType.type;
    if (objectRegisterType.category == RegisterType.NULL) {
        return false;
    }
    assert objectRegisterTypeProto != null;
    MethodReference resolvedMethod;
    if (isSuper) {
        // invoke-super is only used for the same class that we're currently in
        TypeProto typeProto = classPath.getClass(method.getDefiningClass());
        TypeProto superType;
        String superclassType = typeProto.getSuperclass();
        if (superclassType != null) {
            superType = classPath.getClass(superclassType);
        } else {
            // This is either java.lang.Object, or an UnknownClassProto
            superType = typeProto;
        }
        resolvedMethod = superType.getMethodByVtableIndex(methodIndex);
    } else {
        resolvedMethod = objectRegisterTypeProto.getMethodByVtableIndex(methodIndex);
    }
    if (resolvedMethod == null) {
        throw new AnalysisException("Could not resolve the method in class %s at index %d", objectRegisterType.type.getType(), methodIndex);
    }
    // no need to check class access for invoke-super. A class can obviously access its superclass.
    ClassDef thisClass = classPath.getClassDef(method.getDefiningClass());
    if (classPath.getClass(resolvedMethod.getDefiningClass()).isInterface()) {
        resolvedMethod = new ReparentedMethodReference(resolvedMethod, objectRegisterTypeProto.getType());
    } else if (!isSuper && !TypeUtils.canAccessClass(thisClass.getType(), classPath.getClassDef(resolvedMethod.getDefiningClass()))) {
        // the class is not accessible. So we start looking at objectRegisterTypeProto (which may be different
        // than resolvedMethod.getDefiningClass()), and walk up the class hierarchy.
        ClassDef methodClass = classPath.getClassDef(objectRegisterTypeProto.getType());
        while (!TypeUtils.canAccessClass(thisClass.getType(), methodClass)) {
            String superclass = methodClass.getSuperclass();
            if (superclass == null) {
                throw new ExceptionWithContext("Couldn't find accessible class while resolving method %s", resolvedMethod);
            }
            methodClass = classPath.getClassDef(superclass);
        }
        // methodClass is now the first accessible class found. Now. we need to make sure that the method is
        // actually valid for this class
        MethodReference newResolvedMethod = classPath.getClass(methodClass.getType()).getMethodByVtableIndex(methodIndex);
        if (newResolvedMethod == null) {
            throw new ExceptionWithContext("Couldn't find accessible class while resolving method %s", resolvedMethod);
        }
        resolvedMethod = newResolvedMethod;
        resolvedMethod = new ImmutableMethodReference(methodClass.getType(), resolvedMethod.getName(), resolvedMethod.getParameterTypes(), resolvedMethod.getReturnType());
    }
    if (normalizeVirtualMethods) {
        MethodReference replacementMethod = normalizeMethodReference(resolvedMethod);
        if (replacementMethod != null) {
            resolvedMethod = replacementMethod;
        }
    }
    Instruction deodexedInstruction;
    if (isRange) {
        Instruction3rms instruction = (Instruction3rms) analyzedInstruction.instruction;
        Opcode opcode;
        if (isSuper) {
            opcode = Opcode.INVOKE_SUPER_RANGE;
        } else {
            opcode = Opcode.INVOKE_VIRTUAL_RANGE;
        }
        deodexedInstruction = new ImmutableInstruction3rc(opcode, instruction.getStartRegister(), instruction.getRegisterCount(), resolvedMethod);
    } else {
        Instruction35ms instruction = (Instruction35ms) analyzedInstruction.instruction;
        Opcode opcode;
        if (isSuper) {
            opcode = Opcode.INVOKE_SUPER;
        } else {
            opcode = Opcode.INVOKE_VIRTUAL;
        }
        deodexedInstruction = new ImmutableInstruction35c(opcode, instruction.getRegisterCount(), instruction.getRegisterC(), instruction.getRegisterD(), instruction.getRegisterE(), instruction.getRegisterF(), instruction.getRegisterG(), resolvedMethod);
    }
    analyzedInstruction.setDeodexedInstruction(deodexedInstruction);
    analyzeInstruction(analyzedInstruction);
    return true;
}
Also used : ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) Opcode(org.jf.dexlib2.Opcode) ExceptionWithContext(org.jf.util.ExceptionWithContext) BaseMethodReference(org.jf.dexlib2.base.reference.BaseMethodReference) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) MethodReference(org.jf.dexlib2.iface.reference.MethodReference)

Aggregations

ImmutableMethodReference (org.jf.dexlib2.immutable.reference.ImmutableMethodReference)4 Opcode (org.jf.dexlib2.Opcode)3 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)3 BaseMethodReference (org.jf.dexlib2.base.reference.BaseMethodReference)2 ImmutableMethodImplementation (org.jf.dexlib2.immutable.ImmutableMethodImplementation)2 ImmutableStringReference (org.jf.dexlib2.immutable.reference.ImmutableStringReference)2 ImmutableTypeReference (org.jf.dexlib2.immutable.reference.ImmutableTypeReference)2 ExceptionWithContext (org.jf.util.ExceptionWithContext)2 Test (org.junit.Test)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 ClassDef (org.jf.dexlib2.iface.ClassDef)1 DexFile (org.jf.dexlib2.iface.DexFile)1 Instruction (org.jf.dexlib2.iface.instruction.Instruction)1 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)1 FieldReference (org.jf.dexlib2.iface.reference.FieldReference)1 StringReference (org.jf.dexlib2.iface.reference.StringReference)1 TypeReference (org.jf.dexlib2.iface.reference.TypeReference)1 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)1 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)1