Search in sources :

Example 46 with AllocNode

use of soot.jimple.spark.pag.AllocNode in project soot by Sable.

the class EvalResults method estimateHeapDefuseGraph.

/**
 * Estimate the size of the def-use graph for the heap memory. The heap
 * graph is estimated without context information.
 */
public void estimateHeapDefuseGraph() {
    final Map<IVarAbstraction, int[]> defUseCounterForGeom = new HashMap<IVarAbstraction, int[]>();
    final Map<AllocDotField, int[]> defUseCounterForSpark = new HashMap<AllocDotField, int[]>();
    Date begin = new Date();
    for (SootMethod sm : ptsProvider.getAllReachableMethods()) {
        if (sm.isJavaLibraryMethod())
            continue;
        if (!sm.isConcrete())
            continue;
        if (!sm.hasActiveBody()) {
            sm.retrieveActiveBody();
        }
        if (!ptsProvider.isValidMethod(sm))
            continue;
        // We first gather all the memory access expressions
        for (Iterator<Unit> stmts = sm.getActiveBody().getUnits().iterator(); stmts.hasNext(); ) {
            Stmt st = (Stmt) stmts.next();
            if (!(st instanceof AssignStmt))
                continue;
            AssignStmt a = (AssignStmt) st;
            final Value lValue = a.getLeftOp();
            final Value rValue = a.getRightOp();
            InstanceFieldRef ifr = null;
            if (lValue instanceof InstanceFieldRef) {
                // Def statement
                ifr = (InstanceFieldRef) lValue;
            } else if (rValue instanceof InstanceFieldRef) {
                // Use statement
                ifr = (InstanceFieldRef) rValue;
            }
            if (ifr != null) {
                final SootField field = ifr.getField();
                LocalVarNode vn = ptsProvider.findLocalVarNode((Local) ifr.getBase());
                if (vn == null)
                    continue;
                IVarAbstraction pn = ptsProvider.findInternalNode(vn);
                if (pn == null)
                    continue;
                pn = pn.getRepresentative();
                if (!pn.hasPTResult())
                    continue;
                // Spark
                vn.getP2Set().forall(new P2SetVisitor() {

                    @Override
                    public void visit(Node n) {
                        IVarAbstraction padf = ptsProvider.findAndInsertInstanceField((AllocNode) n, field);
                        AllocDotField adf = (AllocDotField) padf.getWrappedNode();
                        int[] defUseUnit = defUseCounterForSpark.get(adf);
                        if (defUseUnit == null) {
                            defUseUnit = new int[2];
                            defUseCounterForSpark.put(adf, defUseUnit);
                        }
                        if (lValue instanceof InstanceFieldRef) {
                            defUseUnit[0]++;
                        } else {
                            defUseUnit[1]++;
                        }
                    }
                });
                // Geom
                Set<AllocNode> objsSet = pn.get_all_points_to_objects();
                for (AllocNode obj : objsSet) {
                    /*
						 * We will create a lot of instance fields. Because in
						 * points-to analysis, we concern only the reference
						 * type fields. But here, we concern all the fields read
						 * write including the primitive type fields.
						 */
                    IVarAbstraction padf = ptsProvider.findAndInsertInstanceField(obj, field);
                    int[] defUseUnit = defUseCounterForGeom.get(padf);
                    if (defUseUnit == null) {
                        defUseUnit = new int[2];
                        defUseCounterForGeom.put(padf, defUseUnit);
                    }
                    if (lValue instanceof InstanceFieldRef) {
                        defUseUnit[0]++;
                    } else {
                        defUseUnit[1]++;
                    }
                }
            }
        }
    }
    for (int[] defUseUnit : defUseCounterForSpark.values()) {
        evalRes.n_spark_du_pairs += ((long) defUseUnit[0]) * defUseUnit[1];
    }
    for (int[] defUseUnit : defUseCounterForGeom.values()) {
        evalRes.n_geom_du_pairs += ((long) defUseUnit[0]) * defUseUnit[1];
    }
    Date end = new Date();
    ptsProvider.ps.println();
    ptsProvider.ps.println("-----------> Heap Def Use Graph Evaluation <------------");
    ptsProvider.ps.println("The edges in the heap def-use graph is (by Geom): " + evalRes.n_geom_du_pairs);
    ptsProvider.ps.println("The edges in the heap def-use graph is (by Spark): " + evalRes.n_spark_du_pairs);
    ptsProvider.ps.printf("Using time: %dms \n", end.getTime() - begin.getTime());
    ptsProvider.ps.println();
}
Also used : AllocDotField(soot.jimple.spark.pag.AllocDotField) HashMap(java.util.HashMap) AssignStmt(soot.jimple.AssignStmt) LocalVarNode(soot.jimple.spark.pag.LocalVarNode) Node(soot.jimple.spark.pag.Node) VarNode(soot.jimple.spark.pag.VarNode) AllocNode(soot.jimple.spark.pag.AllocNode) Unit(soot.Unit) Date(java.util.Date) Stmt(soot.jimple.Stmt) AssignStmt(soot.jimple.AssignStmt) AllocNode(soot.jimple.spark.pag.AllocNode) IVarAbstraction(soot.jimple.spark.geom.geomPA.IVarAbstraction) Value(soot.Value) InstanceFieldRef(soot.jimple.InstanceFieldRef) SootMethod(soot.SootMethod) SootField(soot.SootField) LocalVarNode(soot.jimple.spark.pag.LocalVarNode) P2SetVisitor(soot.jimple.spark.sets.P2SetVisitor)

Example 47 with AllocNode

use of soot.jimple.spark.pag.AllocNode in project soot by Sable.

the class EvalResults method test_1cfa_call_graph.

/**
 * We assess the quality of building the 1-cfa call graph with the geometric
 * points-to result.
 */
private void test_1cfa_call_graph(LocalVarNode vn, SootMethod caller, SootMethod callee_signature, Histogram ce_range) {
    long l, r;
    IVarAbstraction pn = ptsProvider.findInternalNode(vn);
    if (pn == null)
        return;
    pn = pn.getRepresentative();
    Set<SootMethod> tgts = new HashSet<SootMethod>();
    Set<AllocNode> set = pn.get_all_points_to_objects();
    LinkedList<CgEdge> list = ptsProvider.getCallEdgesInto(ptsProvider.getIDFromSootMethod(caller));
    FastHierarchy hierarchy = Scene.v().getOrMakeFastHierarchy();
    for (Iterator<CgEdge> it = list.iterator(); it.hasNext(); ) {
        CgEdge p = it.next();
        l = p.map_offset;
        r = l + ptsProvider.max_context_size_block[p.s];
        tgts.clear();
        for (AllocNode obj : set) {
            if (!pn.pointer_interval_points_to(l, r, obj))
                continue;
            Type t = obj.getType();
            if (t == null)
                continue;
            else if (t instanceof AnySubType)
                t = ((AnySubType) t).getBase();
            else if (t instanceof ArrayType)
                t = RefType.v("java.lang.Object");
            try {
                tgts.add(hierarchy.resolveConcreteDispatch(((RefType) t).getSootClass(), callee_signature));
            } catch (Exception e) {
            }
        }
        tgts.remove(null);
        ce_range.addNumber(tgts.size());
    }
}
Also used : CgEdge(soot.jimple.spark.geom.dataRep.CgEdge) ArrayType(soot.ArrayType) RefType(soot.RefType) FastHierarchy(soot.FastHierarchy) RefType(soot.RefType) AnySubType(soot.AnySubType) RefLikeType(soot.RefLikeType) ArrayType(soot.ArrayType) Type(soot.Type) AllocNode(soot.jimple.spark.pag.AllocNode) IVarAbstraction(soot.jimple.spark.geom.geomPA.IVarAbstraction) SootMethod(soot.SootMethod) AnySubType(soot.AnySubType) HashSet(java.util.HashSet)

Example 48 with AllocNode

use of soot.jimple.spark.pag.AllocNode in project soot by Sable.

the class EvalResults method checkCastsSafety.

/**
 * Count how many static casts can be determined safe.
 */
public void checkCastsSafety() {
    for (SootMethod sm : ptsProvider.getAllReachableMethods()) {
        if (sm.isJavaLibraryMethod())
            continue;
        if (!sm.isConcrete())
            continue;
        if (!sm.hasActiveBody()) {
            sm.retrieveActiveBody();
        }
        if (!ptsProvider.isValidMethod(sm))
            continue;
        // All the statements in the method
        for (Iterator<Unit> stmts = sm.getActiveBody().getUnits().iterator(); stmts.hasNext(); ) {
            Stmt st = (Stmt) stmts.next();
            if (st instanceof AssignStmt) {
                Value rhs = ((AssignStmt) st).getRightOp();
                Value lhs = ((AssignStmt) st).getLeftOp();
                if (rhs instanceof CastExpr && lhs.getType() instanceof RefLikeType) {
                    Value v = ((CastExpr) rhs).getOp();
                    VarNode node = ptsProvider.findLocalVarNode(v);
                    if (node == null)
                        continue;
                    IVarAbstraction pn = ptsProvider.findInternalNode(node);
                    if (pn == null)
                        continue;
                    pn = pn.getRepresentative();
                    if (!pn.hasPTResult())
                        continue;
                    evalRes.total_casts++;
                    final Type targetType = (RefLikeType) ((CastExpr) rhs).getCastType();
                    // We first use the geometric points-to result to
                    // evaluate
                    solved = true;
                    Set<AllocNode> set = pn.get_all_points_to_objects();
                    for (AllocNode obj : set) {
                        solved = ptsProvider.castNeverFails(obj.getType(), targetType);
                        if (solved == false)
                            break;
                    }
                    if (solved)
                        evalRes.geom_solved_casts++;
                    // Second is the SPARK result
                    solved = true;
                    node.getP2Set().forall(new P2SetVisitor() {

                        public void visit(Node arg0) {
                            if (solved == false)
                                return;
                            solved = ptsProvider.castNeverFails(arg0.getType(), targetType);
                        }
                    });
                    if (solved)
                        evalRes.spark_solved_casts++;
                }
            }
        }
    }
    ptsProvider.ps.println();
    ptsProvider.ps.println("-----------> Static Casts Safety Evaluation <------------");
    ptsProvider.ps.println("Total casts (app code): " + evalRes.total_casts);
    ptsProvider.ps.println("Safe casts: Geom = " + evalRes.geom_solved_casts + ", SPARK = " + evalRes.spark_solved_casts);
}
Also used : LocalVarNode(soot.jimple.spark.pag.LocalVarNode) VarNode(soot.jimple.spark.pag.VarNode) AssignStmt(soot.jimple.AssignStmt) LocalVarNode(soot.jimple.spark.pag.LocalVarNode) Node(soot.jimple.spark.pag.Node) VarNode(soot.jimple.spark.pag.VarNode) AllocNode(soot.jimple.spark.pag.AllocNode) Unit(soot.Unit) Stmt(soot.jimple.Stmt) AssignStmt(soot.jimple.AssignStmt) RefLikeType(soot.RefLikeType) RefType(soot.RefType) AnySubType(soot.AnySubType) RefLikeType(soot.RefLikeType) ArrayType(soot.ArrayType) Type(soot.Type) AllocNode(soot.jimple.spark.pag.AllocNode) IVarAbstraction(soot.jimple.spark.geom.geomPA.IVarAbstraction) Value(soot.Value) CastExpr(soot.jimple.CastExpr) SootMethod(soot.SootMethod) P2SetVisitor(soot.jimple.spark.sets.P2SetVisitor)

Example 49 with AllocNode

use of soot.jimple.spark.pag.AllocNode in project soot by Sable.

the class MethodNodeFactory method caseClassConstant.

@Override
public final void caseClassConstant(ClassConstant cc) {
    AllocNode classConstant = pag.makeClassConstantNode(cc);
    VarNode classConstantLocal = pag.makeGlobalVarNode(classConstant, RefType.v("java.lang.Class"));
    pag.addEdge(classConstant, classConstantLocal);
    setResult(classConstantLocal);
}
Also used : VarNode(soot.jimple.spark.pag.VarNode) AllocNode(soot.jimple.spark.pag.AllocNode)

Example 50 with AllocNode

use of soot.jimple.spark.pag.AllocNode in project soot by Sable.

the class MethodNodeFactory method caseNewMultiArrayExpr.

@Override
public final void caseNewMultiArrayExpr(NewMultiArrayExpr nmae) {
    ArrayType type = (ArrayType) nmae.getType();
    AllocNode prevAn = pag.makeAllocNode(new Pair<Expr, Integer>(nmae, new Integer(type.numDimensions)), type, method);
    VarNode prevVn = pag.makeLocalVarNode(prevAn, prevAn.getType(), method);
    mpag.addInternalEdge(prevAn, prevVn);
    setResult(prevAn);
    while (true) {
        Type t = type.getElementType();
        if (!(t instanceof ArrayType))
            break;
        type = (ArrayType) t;
        AllocNode an = pag.makeAllocNode(new Pair<Expr, Integer>(nmae, new Integer(type.numDimensions)), type, method);
        VarNode vn = pag.makeLocalVarNode(an, an.getType(), method);
        mpag.addInternalEdge(an, vn);
        mpag.addInternalEdge(vn, pag.makeFieldRefNode(prevVn, ArrayElement.v()));
        prevAn = an;
        prevVn = vn;
    }
}
Also used : ArrayType(soot.ArrayType) VarNode(soot.jimple.spark.pag.VarNode) RefType(soot.RefType) Type(soot.Type) RefLikeType(soot.RefLikeType) ArrayType(soot.ArrayType) NewArrayExpr(soot.jimple.NewArrayExpr) VirtualInvokeExpr(soot.jimple.VirtualInvokeExpr) PhiExpr(soot.shimple.PhiExpr) NewMultiArrayExpr(soot.jimple.NewMultiArrayExpr) CastExpr(soot.jimple.CastExpr) InvokeExpr(soot.jimple.InvokeExpr) NewExpr(soot.jimple.NewExpr) Expr(soot.jimple.Expr) StaticInvokeExpr(soot.jimple.StaticInvokeExpr) AllocNode(soot.jimple.spark.pag.AllocNode)

Aggregations

AllocNode (soot.jimple.spark.pag.AllocNode)67 Node (soot.jimple.spark.pag.Node)37 VarNode (soot.jimple.spark.pag.VarNode)36 LocalVarNode (soot.jimple.spark.pag.LocalVarNode)28 PointsToSetInternal (soot.jimple.spark.sets.PointsToSetInternal)25 FieldRefNode (soot.jimple.spark.pag.FieldRefNode)22 P2SetVisitor (soot.jimple.spark.sets.P2SetVisitor)19 PlainConstraint (soot.jimple.spark.geom.dataRep.PlainConstraint)18 SegmentNode (soot.jimple.spark.geom.dataRep.SegmentNode)18 SootMethod (soot.SootMethod)17 ClassConstantNode (soot.jimple.spark.pag.ClassConstantNode)17 SparkField (soot.jimple.spark.pag.SparkField)16 RefType (soot.RefType)14 Type (soot.Type)13 AllocDotField (soot.jimple.spark.pag.AllocDotField)13 NewInstanceNode (soot.jimple.spark.pag.NewInstanceNode)11 HashSet (java.util.HashSet)10 GlobalVarNode (soot.jimple.spark.pag.GlobalVarNode)10 StringConstantNode (soot.jimple.spark.pag.StringConstantNode)10 SootClass (soot.SootClass)9