Search in sources :

Example 1 with FieldRef

use of soot.jimple.FieldRef in project soot by Sable.

the class ConstantFieldValueFinder method computeFieldToValuesAssignedList.

/*
	 * Go through all the methods in the application and make a mapping of className+methodName ---> values assigned
	 * There can obviously be more than one value assigned to each field
	 */
private void computeFieldToValuesAssignedList() {
    // go through all the classes
    Iterator classIt = appClasses.iterator();
    while (classIt.hasNext()) {
        SootClass s = (SootClass) classIt.next();
        debug("\ncomputeMethodSummaries", "Processing class " + s.getName());
        // go though all the methods
        Iterator methodIt = s.methodIterator();
        while (methodIt.hasNext()) {
            SootMethod m = (SootMethod) methodIt.next();
            DavaBody body = null;
            if (m.hasActiveBody()) {
                /*
					 * Added to try to fix the no active body found exception
					 */
                body = (DavaBody) m.getActiveBody();
            } else {
                continue;
            }
            ASTNode AST = (ASTNode) body.getUnits().getFirst();
            // find all definitions in the program
            AllDefinitionsFinder defFinder = new AllDefinitionsFinder();
            AST.apply(defFinder);
            Iterator<DefinitionStmt> allDefIt = defFinder.getAllDefs().iterator();
            // go through each definition
            while (allDefIt.hasNext()) {
                DefinitionStmt stmt = allDefIt.next();
                // debug("DefinitionStmt")
                Value left = stmt.getLeftOp();
                /*
					 * Only care if we have fieldRef on the left
					 */
                if (!(left instanceof FieldRef)) {
                    continue;
                }
                // we know definition is to a field
                debug("computeMethodSummaries method: " + m.getName(), "Field ref is: " + left);
                // Information we want to store is class of field and name of field and the right op
                FieldRef ref = (FieldRef) left;
                SootField field = ref.getField();
                /*
					 * Only care about fields with primtype
					 */
                if (!(field.getType() instanceof PrimType))
                    continue;
                String fieldName = field.getName();
                String declaringClass = field.getDeclaringClass().getName();
                debug("\tField Name: " + fieldName);
                debug("\tField DeclaringClass: " + declaringClass);
                // get the valueList for this class+field combo
                String combined = declaringClass + combiner + fieldName;
                Object temp = fieldToValues.get(combined);
                ArrayList valueList;
                if (temp == null) {
                    // no value of this field was yet assigned
                    valueList = new ArrayList();
                    fieldToValues.put(combined, valueList);
                } else {
                    valueList = (ArrayList) temp;
                }
                valueList.add(stmt.getRightOp());
            }
        // going through all the definitions
        }
    // going through methods of class s
    }
// going through  classes
}
Also used : AllDefinitionsFinder(soot.dava.toolkits.base.AST.traversals.AllDefinitionsFinder) FieldRef(soot.jimple.FieldRef) ArrayList(java.util.ArrayList) SootClass(soot.SootClass) DavaBody(soot.dava.DavaBody) Iterator(java.util.Iterator) ASTNode(soot.dava.internal.AST.ASTNode) Value(soot.Value) SootMethod(soot.SootMethod) PrimType(soot.PrimType) SootField(soot.SootField) DefinitionStmt(soot.jimple.DefinitionStmt)

Example 2 with FieldRef

use of soot.jimple.FieldRef in project soot by Sable.

the class CPApplication method substituteUses.

public void substituteUses(List useBoxes, CPFlowSet beforeSet) {
    Iterator useIt = useBoxes.iterator();
    while (useIt.hasNext()) {
        Object useObj = useIt.next();
        Value use = ((ValueBox) useObj).getValue();
        if (use instanceof Local) {
            Local useLocal = (Local) use;
            // System.out.println("local is: "+useLocal);
            Object value = beforeSet.contains(className, useLocal.toString());
            if (value != null) {
                // System.out.println("Local "+useLocal+"is present in before set with value"+value);
                // create constant value for the value and replace this
                // local use with the constant value use
                Value newValue = CPHelper.createConstant(value);
                if (newValue != null) {
                    // System.out.println("Substituted the local use with the constant value"+newValue);
                    ((ValueBox) useObj).setValue(newValue);
                } else {
                // System.out.println("FAILED TO Substitute the local use with the constant value");
                }
            }
        } else if (use instanceof FieldRef) {
            FieldRef useField = (FieldRef) use;
            // System.out.println("FieldRef is: "+useField);
            SootField usedSootField = useField.getField();
            Object value = beforeSet.contains(usedSootField.getDeclaringClass().getName(), usedSootField.getName().toString());
            if (value != null) {
                // System.out.println("FieldRef "+usedSootField+"is present in before set with value"+value);
                // create constant value for the value and replace this
                // local use with the constant value use
                Value newValue = CPHelper.createConstant(value);
                if (newValue != null) {
                    // System.out.println("Substituted the constant field ref use with the constant value"+newValue);
                    ((ValueBox) useObj).setValue(newValue);
                } else {
                // System.out.println("FAILED TO Substitute the constant field ref use with the constant value");
                }
            }
        }
    }
}
Also used : FieldRef(soot.jimple.FieldRef) ValueBox(soot.ValueBox) Iterator(java.util.Iterator) Value(soot.Value) Local(soot.Local) SootField(soot.SootField)

Example 3 with FieldRef

use of soot.jimple.FieldRef in project soot by Sable.

the class TypeAssigner method replaceNullType.

/**
 * Replace statements using locals with null_type type and that would
 * throw a NullPointerException at runtime by a set of instructions
 * throwing a NullPointerException.
 *
 * This is done to remove locals with null_type type.
 *
 * @param b
 */
private void replaceNullType(Body b) {
    List<Local> localsToRemove = new ArrayList<Local>();
    boolean hasNullType = false;
    // check if any local has null_type
    for (Local l : b.getLocals()) {
        if (l.getType() instanceof NullType) {
            localsToRemove.add(l);
            hasNullType = true;
        }
    }
    // No local with null_type
    if (!hasNullType)
        return;
    // force to propagate null constants
    Map<String, String> opts = PhaseOptions.v().getPhaseOptions("jop.cpf");
    if (!opts.containsKey("enabled") || !opts.get("enabled").equals("true")) {
        logger.warn("Cannot run TypeAssigner.replaceNullType(Body). Try to enable jop.cfg.");
        return;
    }
    ConstantPropagatorAndFolder.v().transform(b);
    List<Unit> unitToReplaceByException = new ArrayList<Unit>();
    for (Unit u : b.getUnits()) {
        for (ValueBox vb : u.getUseBoxes()) {
            if (vb.getValue() instanceof Local && ((Local) vb.getValue()).getType() instanceof NullType) {
                Local l = (Local) vb.getValue();
                Stmt s = (Stmt) u;
                boolean replace = false;
                if (s.containsArrayRef()) {
                    ArrayRef r = s.getArrayRef();
                    if (r.getBase() == l) {
                        replace = true;
                    }
                } else if (s.containsFieldRef()) {
                    FieldRef r = s.getFieldRef();
                    if (r instanceof InstanceFieldRef) {
                        InstanceFieldRef ir = (InstanceFieldRef) r;
                        if (ir.getBase() == l) {
                            replace = true;
                        }
                    }
                } else if (s.containsInvokeExpr()) {
                    InvokeExpr ie = s.getInvokeExpr();
                    if (ie instanceof InstanceInvokeExpr) {
                        InstanceInvokeExpr iie = (InstanceInvokeExpr) ie;
                        if (iie.getBase() == l) {
                            replace = true;
                        }
                    }
                }
                if (replace) {
                    unitToReplaceByException.add(u);
                }
            }
        }
    }
    for (Unit u : unitToReplaceByException) {
        soot.dexpler.Util.addExceptionAfterUnit(b, "java.lang.NullPointerException", u, "This statement would have triggered an Exception: " + u);
        b.getUnits().remove(u);
    }
    // should be done on a separate phase
    DeadAssignmentEliminator.v().transform(b);
    UnusedLocalEliminator.v().transform(b);
}
Also used : FieldRef(soot.jimple.FieldRef) InstanceFieldRef(soot.jimple.InstanceFieldRef) ArrayList(java.util.ArrayList) Local(soot.Local) InstanceInvokeExpr(soot.jimple.InstanceInvokeExpr) Unit(soot.Unit) Stmt(soot.jimple.Stmt) ArrayRef(soot.jimple.ArrayRef) InstanceInvokeExpr(soot.jimple.InstanceInvokeExpr) InvokeExpr(soot.jimple.InvokeExpr) ValueBox(soot.ValueBox) InstanceFieldRef(soot.jimple.InstanceFieldRef) NullType(soot.NullType)

Example 4 with FieldRef

use of soot.jimple.FieldRef in project soot by Sable.

the class SparkTransformer method addTags.

protected void addTags(PAG pag) {
    final Tag unknown = new StringTag("Untagged Spark node");
    final Map<Node, Tag> nodeToTag = pag.getNodeTags();
    for (final SootClass c : Scene.v().getClasses()) {
        for (final SootMethod m : c.getMethods()) {
            if (!m.isConcrete())
                continue;
            if (!m.hasActiveBody())
                continue;
            for (final Unit u : m.getActiveBody().getUnits()) {
                final Stmt s = (Stmt) u;
                if (s instanceof DefinitionStmt) {
                    Value lhs = ((DefinitionStmt) s).getLeftOp();
                    VarNode v = null;
                    if (lhs instanceof Local) {
                        v = pag.findLocalVarNode(lhs);
                    } else if (lhs instanceof FieldRef) {
                        v = pag.findGlobalVarNode(((FieldRef) lhs).getField());
                    }
                    if (v != null) {
                        PointsToSetInternal p2set = v.getP2Set();
                        p2set.forall(new P2SetVisitor() {

                            public final void visit(Node n) {
                                addTag(s, n, nodeToTag, unknown);
                            }
                        });
                        Node[] simpleSources = pag.simpleInvLookup(v);
                        for (Node element : simpleSources) {
                            addTag(s, element, nodeToTag, unknown);
                        }
                        simpleSources = pag.allocInvLookup(v);
                        for (Node element : simpleSources) {
                            addTag(s, element, nodeToTag, unknown);
                        }
                        simpleSources = pag.loadInvLookup(v);
                        for (Node element : simpleSources) {
                            addTag(s, element, nodeToTag, unknown);
                        }
                    }
                }
            }
        }
    }
}
Also used : StringTag(soot.tagkit.StringTag) VarNode(soot.jimple.spark.pag.VarNode) FieldRef(soot.jimple.FieldRef) PointsToSetInternal(soot.jimple.spark.sets.PointsToSetInternal) Node(soot.jimple.spark.pag.Node) VarNode(soot.jimple.spark.pag.VarNode) AllocNode(soot.jimple.spark.pag.AllocNode) Local(soot.Local) SootClass(soot.SootClass) Unit(soot.Unit) Stmt(soot.jimple.Stmt) DefinitionStmt(soot.jimple.DefinitionStmt) Value(soot.Value) SootMethod(soot.SootMethod) Tag(soot.tagkit.Tag) StringTag(soot.tagkit.StringTag) DefinitionStmt(soot.jimple.DefinitionStmt) P2SetVisitor(soot.jimple.spark.sets.P2SetVisitor)

Example 5 with FieldRef

use of soot.jimple.FieldRef in project soot by Sable.

the class ArrayIndexLivenessAnalysis method getGenAndKillSetForDefnStmt.

private void getGenAndKillSetForDefnStmt(DefinitionStmt asstmt, HashMap<Stmt, HashSet<Value>> absgen, HashSet<Object> genset, HashSet<Value> absgenset, HashSet<Value> killset, HashSet<Value> condset) {
    /* kill left hand side */
    Value lhs = asstmt.getLeftOp();
    Value rhs = asstmt.getRightOp();
    boolean killarrayrelated = false;
    boolean killallarrayref = false;
    if (fieldin) {
        if (lhs instanceof Local) {
            HashSet<Value> related = localToFieldRef.get(lhs);
            if (related != null)
                killset.addAll(related);
        } else if (lhs instanceof StaticFieldRef) {
            killset.add(lhs);
            condset.add(lhs);
        } else if (lhs instanceof InstanceFieldRef) {
            SootField field = ((InstanceFieldRef) lhs).getField();
            HashSet<Value> related = fieldToFieldRef.get(field);
            if (related != null)
                killset.addAll(related);
            condset.add(lhs);
        }
        if (asstmt.containsInvokeExpr()) {
            /*
                Value expr = asstmt.getInvokeExpr();
                List parameters = ((InvokeExpr)expr).getArgs();

                // add the method invocation
                boolean killall = false;
                if (expr instanceof InstanceInvokeExpr)
                    killall = true;
                else
                {
                    for (int i=0; i<parameters.size(); i++)
                    {
                    Value para = (Value)parameters.get(i);
                    if (para.getType() instanceof RefType)
                    {
                        killall = true;
                        break;
                    }
                    }
                }
    
                if (killall)
                {
                    killset.addAll(allInstFieldRefs);
                }   
                */
            killset.addAll(allFieldRefs);
        }
    }
    if (arrayin) {
        // a = ... or i = ...
        if (lhs instanceof Local) {
            killarrayrelated = true;
        } else // a[i] = ...
        if (lhs instanceof ArrayRef) {
            killallarrayref = true;
            condset.add(lhs);
        }
        // invokeexpr kills all array references.
        if (asstmt.containsInvokeExpr()) {
            killallarrayref = true;
        }
    }
    if (csin) {
        HashSet<Value> exprs = localToExpr.get(lhs);
        if (exprs != null)
            killset.addAll(exprs);
        if (rhs instanceof BinopExpr) {
            Value op1 = ((BinopExpr) rhs).getOp1();
            Value op2 = ((BinopExpr) rhs).getOp2();
            if (rhs instanceof AddExpr) {
                if ((op1 instanceof Local) && (op2 instanceof Local))
                    genset.add(rhs);
            } else if (rhs instanceof MulExpr) {
                if ((op1 instanceof Local) || (op2 instanceof Local))
                    genset.add(rhs);
            } else if (rhs instanceof SubExpr) {
                if (op2 instanceof Local)
                    genset.add(rhs);
            }
        }
    }
    if ((lhs instanceof Local) && (fullSet.contains(lhs))) {
        killset.add(lhs);
        /* speculatively add lhs as live condition. */
        condset.add(lhs);
    } else if (lhs instanceof ArrayRef) {
        /* a[i] generate a and i. */
        Value base = ((ArrayRef) lhs).getBase();
        Value index = ((ArrayRef) lhs).getIndex();
        absgenset.add(base);
        if (index instanceof Local) {
            absgenset.add(index);
        }
    }
    if (rhs instanceof Local) {
        /*
              if (lhs instanceof Local && fullSet.contains(rhs))
              genset.add(rhs);
            */
        if (fullSet.contains(rhs))
            genset.add(rhs);
    /*
              if (fieldin && (lhs instanceof FieldRef))
              genset.add(rhs);
            */
    } else if (rhs instanceof FieldRef) {
        if (fieldin)
            genset.add(rhs);
    } else if (rhs instanceof ArrayRef) {
        /* lhs=a[i]. */
        Value base = ((ArrayRef) rhs).getBase();
        Value index = ((ArrayRef) rhs).getIndex();
        absgenset.add(base);
        if (index instanceof Local) {
            absgenset.add(index);
        }
        if (arrayin) {
            genset.add(rhs);
            if (rectarray)
                genset.add(Array2ndDimensionSymbol.v(base));
        }
    } else if (rhs instanceof NewArrayExpr) {
        /* a = new A[i]; */
        Value size = ((NewArrayExpr) rhs).getSize();
        if (size instanceof Local)
            genset.add(size);
    } else if (rhs instanceof NewMultiArrayExpr) {
        /* a = new A[i][]...;*/
        /* More precisely, we should track other dimensions. */
        List sizes = ((NewMultiArrayExpr) rhs).getSizes();
        Iterator sizeIt = sizes.iterator();
        while (sizeIt.hasNext()) {
            Value size = (Value) sizeIt.next();
            if (size instanceof Local)
                genset.add(size);
        }
    } else if (rhs instanceof LengthExpr) {
        /* lhs = lengthof rhs */
        Value op = ((LengthExpr) rhs).getOp();
        genset.add(op);
    } else if (rhs instanceof JAddExpr) {
        /* lhs = rhs+c, lhs=c+rhs */
        Value op1 = ((JAddExpr) rhs).getOp1();
        Value op2 = ((JAddExpr) rhs).getOp2();
        if ((op1 instanceof IntConstant) && (op2 instanceof Local)) {
            genset.add(op2);
        } else if ((op2 instanceof IntConstant) && (op1 instanceof Local)) {
            genset.add(op1);
        }
    } else if (rhs instanceof JSubExpr) {
        Value op1 = ((JSubExpr) rhs).getOp1();
        Value op2 = ((JSubExpr) rhs).getOp2();
        if ((op1 instanceof Local) && (op2 instanceof IntConstant)) {
            genset.add(op1);
        }
    }
    if (arrayin) {
        if (killarrayrelated)
            killArrayRelated.put(asstmt, lhs);
        if (killallarrayref)
            killAllArrayRef.put(asstmt, new Boolean(true));
    }
}
Also used : MulExpr(soot.jimple.MulExpr) JSubExpr(soot.jimple.internal.JSubExpr) FieldRef(soot.jimple.FieldRef) InstanceFieldRef(soot.jimple.InstanceFieldRef) StaticFieldRef(soot.jimple.StaticFieldRef) NewMultiArrayExpr(soot.jimple.NewMultiArrayExpr) LengthExpr(soot.jimple.LengthExpr) JSubExpr(soot.jimple.internal.JSubExpr) SubExpr(soot.jimple.SubExpr) Local(soot.Local) JAddExpr(soot.jimple.internal.JAddExpr) AddExpr(soot.jimple.AddExpr) StaticFieldRef(soot.jimple.StaticFieldRef) ArrayRef(soot.jimple.ArrayRef) NewArrayExpr(soot.jimple.NewArrayExpr) Value(soot.Value) InstanceFieldRef(soot.jimple.InstanceFieldRef) Iterator(java.util.Iterator) IntConstant(soot.jimple.IntConstant) SootField(soot.SootField) ArrayList(java.util.ArrayList) List(java.util.List) JAddExpr(soot.jimple.internal.JAddExpr) BinopExpr(soot.jimple.BinopExpr)

Aggregations

FieldRef (soot.jimple.FieldRef)31 Value (soot.Value)25 Local (soot.Local)21 Unit (soot.Unit)20 Stmt (soot.jimple.Stmt)17 ArrayRef (soot.jimple.ArrayRef)14 InvokeExpr (soot.jimple.InvokeExpr)14 AssignStmt (soot.jimple.AssignStmt)13 SootField (soot.SootField)12 Type (soot.Type)12 DefinitionStmt (soot.jimple.DefinitionStmt)12 InstanceFieldRef (soot.jimple.InstanceFieldRef)12 SootMethod (soot.SootMethod)10 ValueBox (soot.ValueBox)10 ArrayList (java.util.ArrayList)9 NewArrayExpr (soot.jimple.NewArrayExpr)9 CastExpr (soot.jimple.CastExpr)8 StaticFieldRef (soot.jimple.StaticFieldRef)8 HashSet (java.util.HashSet)7 Iterator (java.util.Iterator)7