Search in sources :

Example 16 with Reference

use of org.jf.dexlib2.iface.reference.Reference in project soot by Sable.

the class DexClassLoader method makeSootClass.

public Dependencies makeSootClass(SootClass sc, ClassDef defItem, DexFile dexFile) {
    String superClass = defItem.getSuperclass();
    Dependencies deps = new Dependencies();
    // source file
    String sourceFile = defItem.getSourceFile();
    if (sourceFile != null) {
        sc.addTag(new SourceFileTag(sourceFile));
    }
    // super class for hierarchy level
    if (superClass != null) {
        String superClassName = Util.dottedClassName(superClass);
        SootClass sootSuperClass = SootResolver.v().makeClassRef(superClassName);
        sc.setSuperclass(sootSuperClass);
        deps.typesToHierarchy.add(sootSuperClass.getType());
    }
    // access flags
    int accessFlags = defItem.getAccessFlags();
    sc.setModifiers(accessFlags);
    // Retrieve interface names
    if (defItem.getInterfaces() != null) {
        for (String interfaceName : defItem.getInterfaces()) {
            String interfaceClassName = Util.dottedClassName(interfaceName);
            if (sc.implementsInterface(interfaceClassName))
                continue;
            SootClass interfaceClass = SootResolver.v().makeClassRef(interfaceClassName);
            interfaceClass.setModifiers(interfaceClass.getModifiers() | Modifier.INTERFACE);
            sc.addInterface(interfaceClass);
            deps.typesToHierarchy.add(interfaceClass.getType());
        }
    }
    if (Options.v().oaat() && sc.resolvingLevel() <= SootClass.HIERARCHY) {
        return deps;
    }
    DexAnnotation da = new DexAnnotation(sc, deps);
    // get the fields of the class
    for (Field sf : defItem.getStaticFields()) {
        loadField(sc, da, sf);
    }
    for (Field f : defItem.getInstanceFields()) {
        loadField(sc, da, f);
    }
    // get the methods of the class
    DexMethod dexMethod = createDexMethodFactory(dexFile, sc);
    for (Method method : defItem.getDirectMethods()) {
        loadMethod(method, sc, da, dexMethod);
    }
    for (Method method : defItem.getVirtualMethods()) {
        loadMethod(method, sc, da, dexMethod);
    }
    da.handleClassAnnotation(defItem);
    // In contrast to Java, Dalvik associates the InnerClassAttribute
    // with the inner class, not the outer one. We need to copy the
    // tags over to correspond to the Soot semantics.
    InnerClassAttribute ica = (InnerClassAttribute) sc.getTag("InnerClassAttribute");
    if (ica != null) {
        Iterator<InnerClassTag> innerTagIt = ica.getSpecs().iterator();
        while (innerTagIt.hasNext()) {
            Tag t = innerTagIt.next();
            if (t instanceof InnerClassTag) {
                InnerClassTag ict = (InnerClassTag) t;
                // Get the outer class name
                String outer = DexInnerClassParser.getOuterClassNameFromTag(ict);
                if (outer == null) {
                    // If we don't have any clue what the outer class is, we
                    // just remove
                    // the reference entirely
                    innerTagIt.remove();
                    continue;
                }
                // we leave it as it is
                if (outer.equals(sc.getName()))
                    continue;
                // Check the inner class to make sure that this tag actually
                // refers to the current class as the inner class
                String inner = ict.getInnerClass().replaceAll("/", ".");
                if (!inner.equals(sc.getName())) {
                    innerTagIt.remove();
                    continue;
                }
                SootClass osc = SootResolver.v().makeClassRef(outer);
                if (osc == sc) {
                    if (!sc.hasOuterClass())
                        continue;
                    osc = sc.getOuterClass();
                } else
                    deps.typesToHierarchy.add(osc.getType());
                // Get the InnerClassAttribute of the outer class
                InnerClassAttribute icat = (InnerClassAttribute) osc.getTag("InnerClassAttribute");
                if (icat == null) {
                    icat = new InnerClassAttribute();
                    osc.addTag(icat);
                }
                // Transfer the tag from the inner class to the outer class
                InnerClassTag newt = new InnerClassTag(ict.getInnerClass(), ict.getOuterClass(), ict.getShortName(), ict.getAccessFlags());
                icat.add(newt);
                // Remove the tag from the inner class as inner classes do
                // not have these tags in the Java / Soot semantics. The
                // DexPrinter will copy it back if we do dex->dex.
                innerTagIt.remove();
                // within the PackManager in method handleInnerClasses().
                if (!sc.hasTag("InnerClassTag")) {
                    if (((InnerClassTag) t).getInnerClass().replaceAll("/", ".").equals(sc.toString())) {
                        sc.addTag(t);
                    }
                }
            }
        }
        // remove tag if empty
        if (ica.getSpecs().isEmpty()) {
            sc.getTags().remove(ica);
        }
    }
    return deps;
}
Also used : InnerClassAttribute(soot.tagkit.InnerClassAttribute) SourceFileTag(soot.tagkit.SourceFileTag) SootMethod(soot.SootMethod) Method(org.jf.dexlib2.iface.Method) SootClass(soot.SootClass) SootField(soot.SootField) Field(org.jf.dexlib2.iface.Field) InnerClassTag(soot.tagkit.InnerClassTag) Dependencies(soot.javaToJimple.IInitialResolver.Dependencies) Tag(soot.tagkit.Tag) SourceFileTag(soot.tagkit.SourceFileTag) InnerClassTag(soot.tagkit.InnerClassTag)

Example 17 with Reference

use of org.jf.dexlib2.iface.reference.Reference in project atlas by alibaba.

the class InstructionMethodItem method writeTo.

@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
    Opcode opcode = instruction.getOpcode();
    String verificationErrorName = null;
    String referenceString = null;
    String referenceString2 = null;
    boolean commentOutInstruction = false;
    if (instruction instanceof Instruction20bc) {
        int verificationError = ((Instruction20bc) instruction).getVerificationError();
        verificationErrorName = VerificationError.getVerificationErrorName(verificationError);
        if (verificationErrorName == null) {
            writer.write("#was invalid verification error type: ");
            writer.printSignedIntAsDec(verificationError);
            writer.write("\n");
            verificationErrorName = "generic-error";
        }
    }
    if (instruction instanceof ReferenceInstruction) {
        ReferenceInstruction referenceInstruction = (ReferenceInstruction) instruction;
        String classContext = null;
        if (methodDef.classDef.options.implicitReferences) {
            classContext = methodDef.method.getDefiningClass();
        }
        try {
            Reference reference = referenceInstruction.getReference();
            referenceString = org.jf.dexlib2.util.ReferenceUtil.getReferenceString(reference, classContext);
            assert referenceString != null;
        } catch (InvalidItemIndex ex) {
            commentOutInstruction = true;
            referenceString = writeInvalidItemIndex(ex, referenceInstruction.getReferenceType(), writer);
        } catch (ReferenceType.InvalidReferenceTypeException ex) {
            writer.write("#invalid reference type: ");
            writer.printSignedIntAsDec(ex.getReferenceType());
            commentOutInstruction = true;
            referenceString = "invalid_reference";
        }
        if (instruction instanceof DualReferenceInstruction) {
            DualReferenceInstruction dualReferenceInstruction = (DualReferenceInstruction) instruction;
            try {
                Reference reference2 = dualReferenceInstruction.getReference2();
                referenceString2 = org.jf.dexlib2.util.ReferenceUtil.getReferenceString(reference2, classContext);
            } catch (InvalidItemIndex ex) {
                commentOutInstruction = true;
                referenceString2 = writeInvalidItemIndex(ex, dualReferenceInstruction.getReferenceType2(), writer);
            } catch (ReferenceType.InvalidReferenceTypeException ex) {
                writer.write("#invalid reference type: ");
                writer.printSignedIntAsDec(ex.getReferenceType());
                commentOutInstruction = true;
                referenceString2 = "invalid_reference";
            }
        }
    }
    if (instruction instanceof Instruction31t) {
        boolean validPayload = true;
        switch(instruction.getOpcode()) {
            case PACKED_SWITCH:
                int baseAddress = methodDef.getPackedSwitchBaseAddress(this.codeAddress + ((Instruction31t) instruction).getCodeOffset());
                if (baseAddress == -1) {
                    validPayload = false;
                }
                break;
            case SPARSE_SWITCH:
                baseAddress = methodDef.getSparseSwitchBaseAddress(this.codeAddress + ((Instruction31t) instruction).getCodeOffset());
                if (baseAddress == -1) {
                    validPayload = false;
                }
                break;
            case FILL_ARRAY_DATA:
                try {
                    methodDef.findPayloadOffset(this.codeAddress + ((Instruction31t) instruction).getCodeOffset(), Opcode.ARRAY_PAYLOAD);
                } catch (org.jf.baksmali.Adaptors.MethodDefinition.InvalidSwitchPayload ex) {
                    validPayload = false;
                }
                break;
            default:
                throw new ExceptionWithContext("Invalid 31t opcode: %s", instruction.getOpcode());
        }
        if (!validPayload) {
            writer.write("#invalid payload reference\n");
            commentOutInstruction = true;
        }
    }
    if (opcode.odexOnly()) {
        if (!isAllowedOdex(opcode)) {
            writer.write("#disallowed odex opcode\n");
            commentOutInstruction = true;
        }
    }
    if (commentOutInstruction) {
        writer.write("#");
    }
    switch(instruction.getOpcode().format) {
        case Format10t:
            writeOpcode(writer);
            writer.write(' ');
            writeTargetLabel(writer);
            break;
        case Format10x:
            if (instruction instanceof UnknownInstruction) {
                writer.write("#unknown opcode: 0x");
                writer.printUnsignedLongAsHex(((UnknownInstruction) instruction).getOriginalOpcode());
                writer.write('\n');
            }
            writeOpcode(writer);
            break;
        case Format11n:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeLiteral(writer);
            break;
        case Format11x:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            break;
        case Format12x:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeSecondRegister(writer);
            break;
        case Format20bc:
            writeOpcode(writer);
            writer.write(' ');
            writer.write(verificationErrorName);
            writer.write(", ");
            writer.write(referenceString);
            break;
        case Format20t:
        case Format30t:
            writeOpcode(writer);
            writer.write(' ');
            writeTargetLabel(writer);
            break;
        case Format21c:
        case Format31c:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writer.write(referenceString);
            break;
        case Format21ih:
        case Format21lh:
        case Format21s:
        case Format31i:
        case Format51l:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeLiteral(writer);
            if (instruction.getOpcode().setsWideRegister()) {
                writeCommentIfLikelyDouble(writer);
            } else {
                boolean isResourceId = writeCommentIfResourceId(writer);
                if (!isResourceId)
                    writeCommentIfLikelyFloat(writer);
            }
            break;
        case Format21t:
        case Format31t:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeTargetLabel(writer);
            break;
        case Format22b:
        case Format22s:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeSecondRegister(writer);
            writer.write(", ");
            writeLiteral(writer);
            break;
        case Format22c:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeSecondRegister(writer);
            writer.write(", ");
            writer.write(referenceString);
            break;
        case Format22cs:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeSecondRegister(writer);
            writer.write(", ");
            writeFieldOffset(writer);
            break;
        case Format22t:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeSecondRegister(writer);
            writer.write(", ");
            writeTargetLabel(writer);
            break;
        case Format22x:
        case Format32x:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeSecondRegister(writer);
            break;
        case Format23x:
            writeOpcode(writer);
            writer.write(' ');
            writeFirstRegister(writer);
            writer.write(", ");
            writeSecondRegister(writer);
            writer.write(", ");
            writeThirdRegister(writer);
            break;
        case Format35c:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRegisters(writer);
            writer.write(", ");
            writer.write(referenceString);
            break;
        case Format35mi:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRegisters(writer);
            writer.write(", ");
            writeInlineIndex(writer);
            break;
        case Format35ms:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRegisters(writer);
            writer.write(", ");
            writeVtableIndex(writer);
            break;
        case Format3rc:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRangeRegisters(writer);
            writer.write(", ");
            writer.write(referenceString);
            break;
        case Format3rmi:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRangeRegisters(writer);
            writer.write(", ");
            writeInlineIndex(writer);
            break;
        case Format3rms:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRangeRegisters(writer);
            writer.write(", ");
            writeVtableIndex(writer);
            break;
        case Format45cc:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRegisters(writer);
            writer.write(", ");
            writer.write(referenceString);
            writer.write(", ");
            writer.write(referenceString2);
            break;
        case Format4rcc:
            writeOpcode(writer);
            writer.write(' ');
            writeInvokeRangeRegisters(writer);
            writer.write(", ");
            writer.write(referenceString);
            writer.write(", ");
            writer.write(referenceString2);
            break;
        default:
            assert false;
            return false;
    }
    if (commentOutInstruction) {
        writer.write("\nnop");
    }
    return true;
}
Also used : InvalidItemIndex(org.jf.dexlib2.dexbacked.DexBackedDexFile.InvalidItemIndex) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) TypeReference(org.jf.dexlib2.iface.reference.TypeReference) Reference(org.jf.dexlib2.iface.reference.Reference) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) Opcode(org.jf.dexlib2.Opcode) ReferenceType(org.jf.dexlib2.ReferenceType) UnknownInstruction(org.jf.dexlib2.iface.instruction.formats.UnknownInstruction) Instruction20bc(org.jf.dexlib2.iface.instruction.formats.Instruction20bc) ExceptionWithContext(org.jf.util.ExceptionWithContext) MethodDefinition(com.taobao.android.baksmali.adaptors.MethodDefinition) Instruction31t(org.jf.dexlib2.iface.instruction.formats.Instruction31t)

Example 18 with Reference

use of org.jf.dexlib2.iface.reference.Reference in project atlas by alibaba.

the class ReferenceUtil method getReferenceString.

@Nullable
public static String getReferenceString(@Nonnull Reference reference, @Nullable String containingClass) {
    if (reference instanceof StringReference) {
        return String.format("\"%s\"", StringUtils.escapeString(((StringReference) reference).getString()));
    }
    if (reference instanceof TypeReference) {
        // TypeGenUtil.newType(((TypeReference)reference).getType());
        String clazz = ((TypeReference) reference).getType();
        return clazz;
    }
    if (reference instanceof FieldReference) {
        FieldReference fieldReference = (FieldReference) reference;
        boolean useImplicitReference = fieldReference.getDefiningClass().equals(containingClass);
        return getFieldDescriptor((FieldReference) reference, useImplicitReference);
    }
    if (reference instanceof MethodReference) {
        MethodReference methodReference = (MethodReference) reference;
        boolean useImplicitReference = methodReference.getDefiningClass().equals(containingClass);
        return getMethodDescriptor((MethodReference) reference, useImplicitReference);
    }
    return null;
}
Also used : FieldReference(org.jf.dexlib2.iface.reference.FieldReference) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) TypeReference(org.jf.dexlib2.iface.reference.TypeReference) StringReference(org.jf.dexlib2.iface.reference.StringReference) Nullable(javax.annotation.Nullable)

Example 19 with Reference

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

the class ClassPool method internCode.

private void internCode(@Nonnull Method method) {
    // this also handles parameter names, which aren't directly tied to the MethodImplementation, even though the debug items are
    boolean hasInstruction = false;
    MethodImplementation methodImpl = method.getImplementation();
    if (methodImpl != null) {
        for (Instruction instruction : methodImpl.getInstructions()) {
            hasInstruction = true;
            if (instruction instanceof ReferenceInstruction) {
                Reference reference = ((ReferenceInstruction) instruction).getReference();
                switch(instruction.getOpcode().referenceType) {
                    case ReferenceType.STRING:
                        dexPool.stringSection.intern((StringReference) reference);
                        break;
                    case ReferenceType.TYPE:
                        dexPool.typeSection.intern(((TypeReference) reference).getType());
                        break;
                    case ReferenceType.FIELD:
                        dexPool.fieldSection.intern((FieldReference) reference);
                        break;
                    case ReferenceType.METHOD:
                        dexPool.methodSection.intern((MethodReference) reference);
                        break;
                    case ReferenceType.CALL_SITE:
                        dexPool.callSiteSection.intern((CallSiteReference) reference);
                        break;
                    default:
                        throw new ExceptionWithContext("Unrecognized reference type: %d", instruction.getOpcode().referenceType);
                }
            }
        }
        List<? extends TryBlock> tryBlocks = methodImpl.getTryBlocks();
        if (!hasInstruction && tryBlocks.size() > 0) {
            throw new ExceptionWithContext("Method %s has no instructions, but has try blocks.", method);
        }
        for (TryBlock<? extends ExceptionHandler> tryBlock : methodImpl.getTryBlocks()) {
            for (ExceptionHandler handler : tryBlock.getExceptionHandlers()) {
                dexPool.typeSection.internNullable(handler.getExceptionType());
            }
        }
    }
}
Also used : MutableMethodImplementation(org.jf.dexlib2.builder.MutableMethodImplementation) ExceptionWithContext(org.jf.util.ExceptionWithContext) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction)

Example 20 with Reference

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

the class MethodAnalyzer method analyzeMoveResult.

private void analyzeMoveResult(@Nonnull AnalyzedInstruction analyzedInstruction) {
    AnalyzedInstruction previousInstruction = null;
    if (analyzedInstruction.instructionIndex > 0) {
        previousInstruction = analyzedInstructions.valueAt(analyzedInstruction.instructionIndex - 1);
    }
    if (previousInstruction == null || !previousInstruction.instruction.getOpcode().setsResult()) {
        throw new AnalysisException(analyzedInstruction.instruction.getOpcode().name + " must occur after an " + "invoke-*/fill-new-array instruction");
    }
    RegisterType resultRegisterType;
    ReferenceInstruction invokeInstruction = (ReferenceInstruction) previousInstruction.instruction;
    Reference reference = invokeInstruction.getReference();
    if (reference instanceof MethodReference) {
        resultRegisterType = RegisterType.getRegisterType(classPath, ((MethodReference) reference).getReturnType());
    } else {
        resultRegisterType = RegisterType.getRegisterType(classPath, (TypeReference) reference);
    }
    setDestinationRegisterTypeAndPropagateChanges(analyzedInstruction, resultRegisterType);
}
Also used : BaseMethodReference(org.jf.dexlib2.base.reference.BaseMethodReference) TypeReference(org.jf.dexlib2.iface.reference.TypeReference) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) Reference(org.jf.dexlib2.iface.reference.Reference) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) ImmutableFieldReference(org.jf.dexlib2.immutable.reference.ImmutableFieldReference) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) BaseMethodReference(org.jf.dexlib2.base.reference.BaseMethodReference) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) TypeReference(org.jf.dexlib2.iface.reference.TypeReference)

Aggregations

Reference (org.jf.dexlib2.iface.reference.Reference)12 TypeReference (org.jf.dexlib2.iface.reference.TypeReference)11 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)10 FieldReference (org.jf.dexlib2.iface.reference.FieldReference)9 Opcode (org.jf.dexlib2.Opcode)7 ExceptionWithContext (org.jf.util.ExceptionWithContext)5 DexBackedFieldReference (org.jf.dexlib2.dexbacked.reference.DexBackedFieldReference)3 DexBackedMethodReference (org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference)3 Instruction (org.jf.dexlib2.iface.instruction.Instruction)3 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)3 Instruction20bc (org.jf.dexlib2.iface.instruction.formats.Instruction20bc)3 Instruction31t (org.jf.dexlib2.iface.instruction.formats.Instruction31t)3 UnknownInstruction (org.jf.dexlib2.iface.instruction.formats.UnknownInstruction)3 ImmutableMethodReference (org.jf.dexlib2.immutable.reference.ImmutableMethodReference)3 Nonnull (javax.annotation.Nonnull)2 Nullable (javax.annotation.Nullable)2 InvalidSwitchPayload (org.jf.baksmali.Adaptors.MethodDefinition.InvalidSwitchPayload)2 ReferenceType (org.jf.dexlib2.ReferenceType)2 BaseMethodReference (org.jf.dexlib2.base.reference.BaseMethodReference)2 InvalidItemIndex (org.jf.dexlib2.dexbacked.DexBackedDexFile.InvalidItemIndex)2