Search in sources :

Example 6 with Dependencies

use of soot.javaToJimple.IInitialResolver.Dependencies in project soot by Sable.

the class DexResolver method resolveFromFile.

/**
 * Resolve the class contained in file into the passed soot class.
 *
 * @param file
 *            the path to the dex/apk file to resolve
 * @param className
 *            the name of the class to resolve
 * @param sc
 *            the soot class that will represent the class
 * @return the dependencies of this class.
 */
public Dependencies resolveFromFile(File file, String className, SootClass sc) {
    DexlibWrapper wrapper = initializeDexFile(file);
    Dependencies deps = wrapper.makeSootClass(sc, className);
    addSourceFileTag(sc, "dalvik_source_" + file.getName());
    return deps;
}
Also used : Dependencies(soot.javaToJimple.IInitialResolver.Dependencies)

Example 7 with Dependencies

use of soot.javaToJimple.IInitialResolver.Dependencies 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)

Aggregations

Dependencies (soot.javaToJimple.IInitialResolver.Dependencies)7 IInitialResolver (soot.javaToJimple.IInitialResolver)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Field (org.jf.dexlib2.iface.Field)1 Method (org.jf.dexlib2.iface.Method)1 ClassReader (org.objectweb.asm.ClassReader)1 Node (polyglot.ast.Node)1 SootClass (soot.SootClass)1 SootField (soot.SootField)1 SootMethod (soot.SootMethod)1 soot.jimple (soot.jimple)1 LexerException (soot.jimple.parser.lexer.LexerException)1 ParserException (soot.jimple.parser.parser.ParserException)1 InnerClassAttribute (soot.tagkit.InnerClassAttribute)1 InnerClassTag (soot.tagkit.InnerClassTag)1 SourceFileTag (soot.tagkit.SourceFileTag)1 Tag (soot.tagkit.Tag)1 ComputeASTMetrics (soot.toolkits.astmetrics.ComputeASTMetrics)1