Search in sources :

Example 81 with SootClass

use of soot.SootClass in project soot by Sable.

the class ReflectionTraceInfo method inferSource.

private Set<SootMethod> inferSource(String source, int lineNumber) {
    String className = source.substring(0, source.lastIndexOf("."));
    String methodName = source.substring(source.lastIndexOf(".") + 1);
    if (!Scene.v().containsClass(className)) {
        Scene.v().addBasicClass(className, SootClass.BODIES);
        Scene.v().loadBasicClasses();
        if (!Scene.v().containsClass(className)) {
            throw new RuntimeException("Trace file refers to unknown class: " + className);
        }
    }
    SootClass sootClass = Scene.v().getSootClass(className);
    Set<SootMethod> methodsWithRightName = new LinkedHashSet<SootMethod>();
    for (SootMethod m : sootClass.getMethods()) {
        if (m.isConcrete() && m.getName().equals(methodName)) {
            methodsWithRightName.add(m);
        }
    }
    if (methodsWithRightName.isEmpty()) {
        throw new RuntimeException("Trace file refers to unknown method with name " + methodName + " in Class " + className);
    } else if (methodsWithRightName.size() == 1) {
        return Collections.singleton(methodsWithRightName.iterator().next());
    } else {
        // more than one method with that name
        for (SootMethod sootMethod : methodsWithRightName) {
            if (coversLineNumber(lineNumber, sootMethod)) {
                return Collections.singleton(sootMethod);
            }
            if (sootMethod.isConcrete()) {
                if (!sootMethod.hasActiveBody())
                    sootMethod.retrieveActiveBody();
                Body body = sootMethod.getActiveBody();
                if (coversLineNumber(lineNumber, body)) {
                    return Collections.singleton(sootMethod);
                }
                for (Unit u : body.getUnits()) {
                    if (coversLineNumber(lineNumber, u)) {
                        return Collections.singleton(sootMethod);
                    }
                }
            }
        }
        // be conservative and return all method that we found
        return methodsWithRightName;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SootMethod(soot.SootMethod) SootClass(soot.SootClass) Unit(soot.Unit) Body(soot.Body)

Example 82 with SootClass

use of soot.SootClass in project soot by Sable.

the class MethodPAG method addMiscEdges.

protected void addMiscEdges() {
    // Add node for parameter (String[]) in main method
    final String signature = method.getSignature();
    if (method.getSubSignature().equals(mainSubSignature)) {
        addInEdge(pag().nodeFactory().caseArgv(), nodeFactory.caseParm(0));
    } else if (signature.equals("<java.lang.Thread: void <init>(java.lang.ThreadGroup,java.lang.String)>")) {
        addInEdge(pag().nodeFactory().caseMainThread(), nodeFactory.caseThis());
        addInEdge(pag().nodeFactory().caseMainThreadGroup(), nodeFactory.caseParm(0));
    } else if (signature.equals("<java.lang.ref.Finalizer: void <init>(java.lang.Object)>")) {
        addInEdge(nodeFactory.caseThis(), pag().nodeFactory().caseFinalizeQueue());
    } else if (signature.equals("<java.lang.ref.Finalizer: void runFinalizer()>")) {
        addInEdge(pag.nodeFactory().caseFinalizeQueue(), nodeFactory.caseThis());
    } else if (signature.equals("<java.lang.ref.Finalizer: void access$100(java.lang.Object)>")) {
        addInEdge(pag.nodeFactory().caseFinalizeQueue(), nodeFactory.caseParm(0));
    } else if (signature.equals("<java.lang.ClassLoader: void <init>()>")) {
        addInEdge(pag.nodeFactory().caseDefaultClassLoader(), nodeFactory.caseThis());
    } else if (signature.equals("<java.lang.Thread: void exit()>")) {
        addInEdge(pag.nodeFactory().caseMainThread(), nodeFactory.caseThis());
    } else if (signature.equals("<java.security.PrivilegedActionException: void <init>(java.lang.Exception)>")) {
        addInEdge(pag.nodeFactory().caseThrow(), nodeFactory.caseParm(0));
        addInEdge(pag.nodeFactory().casePrivilegedActionException(), nodeFactory.caseThis());
    }
    if (method.getNumberedSubSignature().equals(sigCanonicalize)) {
        SootClass cl = method.getDeclaringClass();
        while (cl != null) {
            if (cl.equals(Scene.v().getSootClass("java.io.FileSystem"))) {
                addInEdge(pag.nodeFactory().caseCanonicalPath(), nodeFactory.caseRet());
            }
            cl = cl.getSuperclassUnsafe();
        }
    }
    boolean isImplicit = false;
    for (SootMethod implicitMethod : EntryPoints.v().implicit()) {
        if (implicitMethod.getNumberedSubSignature().equals(method.getNumberedSubSignature())) {
            isImplicit = true;
            break;
        }
    }
    if (isImplicit) {
        SootClass c = method.getDeclaringClass();
        outer: do {
            while (!c.getName().equals("java.lang.ClassLoader")) {
                if (!c.hasSuperclass()) {
                    break outer;
                }
                c = c.getSuperclass();
            }
            if (method.getName().equals("<init>"))
                continue;
            addInEdge(pag().nodeFactory().caseDefaultClassLoader(), nodeFactory.caseThis());
            addInEdge(pag().nodeFactory().caseMainClassNameString(), nodeFactory.caseParm(0));
        } while (false);
    }
}
Also used : SootMethod(soot.SootMethod) NumberedString(soot.util.NumberedString) SootClass(soot.SootClass)

Example 83 with SootClass

use of soot.SootClass in project soot by Sable.

the class CollectConstants method isSuitableClassToAddFieldConstant.

private boolean isSuitableClassToAddFieldConstant(SootClass sc, Constant constant) {
    if (sc.isInterface()) {
        return false;
    }
    if (constant instanceof ClassConstant) {
        ClassConstant classConstant = (ClassConstant) constant;
        RefType type = (RefType) classConstant.toSootType();
        SootClass classFromConstant = type.getSootClass();
        Hierarchy hierarchy = Scene.v().getActiveHierarchy();
        return hierarchy.isVisible(sc, classFromConstant);
    }
    return true;
}
Also used : RefType(soot.RefType) Hierarchy(soot.Hierarchy) SootClass(soot.SootClass) ClassConstant(soot.jimple.ClassConstant)

Example 84 with SootClass

use of soot.SootClass in project soot by Sable.

the class LibraryMethodWrappersBuilder method getVisibleApplicationClasses.

private static List<SootClass> getVisibleApplicationClasses(SootMethod visibleBy) {
    final List<SootClass> result = new ArrayList<>();
    final Iterator<SootClass> applicationClassesIterator = Scene.v().getApplicationClasses().snapshotIterator();
    while (applicationClassesIterator.hasNext()) {
        final SootClass applicationClass = applicationClassesIterator.next();
        if (applicationClass.isConcrete() && !applicationClass.isInterface() && applicationClass.isPublic() && Scene.v().getActiveHierarchy().isVisible(applicationClass, visibleBy)) {
            result.add(applicationClass);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) SootClass(soot.SootClass)

Example 85 with SootClass

use of soot.SootClass in project soot by Sable.

the class LibraryMethodWrappersBuilder method getMethodSafely.

private static SootMethod getMethodSafely(InvokeExpr invokeExpr) {
    try {
        final SootMethod invokedMethod = invokeExpr.getMethod();
        if (invokedMethod == null) {
            return null;
        }
        if (SootMethod.constructorName.equals(invokedMethod.getName()) || SootMethod.staticInitializerName.equals(invokedMethod.getName())) {
            logger.debug("Skipping wrapping method {} as it is constructor/initializer.", invokedMethod);
            return null;
        }
        final SootClass invokedMethodClass = invokedMethod.getDeclaringClass();
        if (!invokedMethodClass.isLibraryClass()) {
            logger.debug("Skipping wrapping method {} as it is not library one.", invokedMethod);
            return null;
        }
        if (invokeExpr.getMethodRef().declaringClass().isInterface() && !invokedMethodClass.isInterface()) {
            logger.debug("Skipping wrapping method {} as original code suppose to execute it on interface {}" + " but resolved code trying to execute it on class {}", invokedMethod, invokeExpr.getMethodRef().declaringClass(), invokedMethodClass);
            return null;
        }
        return invokedMethod;
    } catch (RuntimeException exception) {
        logger.debug("Cannot resolve method of InvokeExpr: " + invokeExpr.toString(), exception);
        return null;
    }
}
Also used : SootMethod(soot.SootMethod) SootClass(soot.SootClass)

Aggregations

SootClass (soot.SootClass)194 SootMethod (soot.SootMethod)99 RefType (soot.RefType)69 ArrayList (java.util.ArrayList)60 Type (soot.Type)57 VoidType (soot.VoidType)33 ArrayType (soot.ArrayType)32 Iterator (java.util.Iterator)29 BooleanType (soot.BooleanType)29 DoubleType (soot.DoubleType)29 LongType (soot.LongType)29 Value (soot.Value)29 FloatType (soot.FloatType)28 Local (soot.Local)27 SootField (soot.SootField)27 List (java.util.List)26 CharType (soot.CharType)26 IntType (soot.IntType)26 ByteType (soot.ByteType)25 PrimType (soot.PrimType)23