Search in sources :

Example 1 with Kill

use of heros.flowfunc.Kill in project soot by Sable.

the class IFDSUninitializedVariables method createFlowFunctionsFactory.

@Override
public FlowFunctions<Unit, Local, SootMethod> createFlowFunctionsFactory() {
    return new FlowFunctions<Unit, Local, SootMethod>() {

        @Override
        public FlowFunction<Local> getNormalFlowFunction(Unit curr, Unit succ) {
            final SootMethod m = interproceduralCFG().getMethodOf(curr);
            if (Scene.v().getEntryPoints().contains(m) && interproceduralCFG().isStartPoint(curr)) {
                return new FlowFunction<Local>() {

                    @Override
                    public Set<Local> computeTargets(Local source) {
                        if (source == zeroValue()) {
                            Set<Local> res = new LinkedHashSet<Local>();
                            res.addAll(m.getActiveBody().getLocals());
                            for (int i = 0; i < m.getParameterCount(); i++) res.remove(m.getActiveBody().getParameterLocal(i));
                            return res;
                        }
                        return Collections.emptySet();
                    }
                };
            }
            if (curr instanceof DefinitionStmt) {
                final DefinitionStmt definition = (DefinitionStmt) curr;
                final Value leftOp = definition.getLeftOp();
                if (leftOp instanceof Local) {
                    final Local leftOpLocal = (Local) leftOp;
                    return new FlowFunction<Local>() {

                        @Override
                        public Set<Local> computeTargets(final Local source) {
                            List<ValueBox> useBoxes = definition.getUseBoxes();
                            for (ValueBox valueBox : useBoxes) {
                                if (valueBox.getValue().equivTo(source)) {
                                    LinkedHashSet<Local> res = new LinkedHashSet<Local>();
                                    res.add(source);
                                    res.add(leftOpLocal);
                                    return res;
                                }
                            }
                            if (leftOp.equivTo(source))
                                return Collections.emptySet();
                            return Collections.singleton(source);
                        }
                    };
                }
            }
            return Identity.v();
        }

        @Override
        public FlowFunction<Local> getCallFlowFunction(Unit callStmt, final SootMethod destinationMethod) {
            Stmt stmt = (Stmt) callStmt;
            InvokeExpr invokeExpr = stmt.getInvokeExpr();
            final List<Value> args = invokeExpr.getArgs();
            final List<Local> localArguments = new ArrayList<Local>();
            for (Value value : args) if (value instanceof Local)
                localArguments.add((Local) value);
            return new FlowFunction<Local>() {

                @Override
                public Set<Local> computeTargets(final Local source) {
                    // Do not map parameters for <clinit> edges
                    if (destinationMethod.getName().equals("<clinit>") || destinationMethod.getSubSignature().equals("void run()"))
                        return Collections.emptySet();
                    for (Local localArgument : localArguments) {
                        if (source.equivTo(localArgument)) {
                            return Collections.<Local>singleton(destinationMethod.getActiveBody().getParameterLocal(args.indexOf(localArgument)));
                        }
                    }
                    if (source == zeroValue()) {
                        // gen all locals that are not parameter locals
                        Collection<Local> locals = destinationMethod.getActiveBody().getLocals();
                        LinkedHashSet<Local> uninitializedLocals = new LinkedHashSet<Local>(locals);
                        for (int i = 0; i < destinationMethod.getParameterCount(); i++) {
                            uninitializedLocals.remove(destinationMethod.getActiveBody().getParameterLocal(i));
                        }
                        return uninitializedLocals;
                    }
                    return Collections.emptySet();
                }
            };
        }

        @Override
        public FlowFunction<Local> getReturnFlowFunction(final Unit callSite, SootMethod calleeMethod, final Unit exitStmt, Unit returnSite) {
            if (callSite instanceof DefinitionStmt) {
                final DefinitionStmt definition = (DefinitionStmt) callSite;
                if (definition.getLeftOp() instanceof Local) {
                    final Local leftOpLocal = (Local) definition.getLeftOp();
                    if (exitStmt instanceof ReturnStmt) {
                        final ReturnStmt returnStmt = (ReturnStmt) exitStmt;
                        return new FlowFunction<Local>() {

                            @Override
                            public Set<Local> computeTargets(Local source) {
                                if (returnStmt.getOp().equivTo(source))
                                    return Collections.singleton(leftOpLocal);
                                return Collections.emptySet();
                            }
                        };
                    } else if (exitStmt instanceof ThrowStmt) {
                        // if we throw an exception, LHS of call is undefined
                        return new FlowFunction<Local>() {

                            @Override
                            public Set<Local> computeTargets(final Local source) {
                                if (source == zeroValue())
                                    return Collections.singleton(leftOpLocal);
                                else
                                    return Collections.emptySet();
                            }
                        };
                    }
                }
            }
            return KillAll.v();
        }

        @Override
        public FlowFunction<Local> getCallToReturnFlowFunction(Unit callSite, Unit returnSite) {
            if (callSite instanceof DefinitionStmt) {
                DefinitionStmt definition = (DefinitionStmt) callSite;
                if (definition.getLeftOp() instanceof Local) {
                    final Local leftOpLocal = (Local) definition.getLeftOp();
                    return new Kill<Local>(leftOpLocal);
                }
            }
            return Identity.v();
        }
    };
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) Unit(soot.Unit) FlowFunctions(heros.FlowFunctions) ThrowStmt(soot.jimple.ThrowStmt) ReturnStmt(soot.jimple.ReturnStmt) Stmt(soot.jimple.Stmt) DefinitionStmt(soot.jimple.DefinitionStmt) InvokeExpr(soot.jimple.InvokeExpr) Kill(heros.flowfunc.Kill) FlowFunction(heros.FlowFunction) JimpleLocal(soot.jimple.internal.JimpleLocal) Local(soot.Local) ValueBox(soot.ValueBox) Value(soot.Value) SootMethod(soot.SootMethod) DefinitionStmt(soot.jimple.DefinitionStmt) ReturnStmt(soot.jimple.ReturnStmt) ThrowStmt(soot.jimple.ThrowStmt)

Example 2 with Kill

use of heros.flowfunc.Kill in project soot by Sable.

the class IFDSLocalInfoFlow method createFlowFunctionsFactory.

public FlowFunctions<Unit, Local, SootMethod> createFlowFunctionsFactory() {
    return new FlowFunctions<Unit, Local, SootMethod>() {

        @Override
        public FlowFunction<Local> getNormalFlowFunction(Unit src, Unit dest) {
            if (src instanceof IdentityStmt && interproceduralCFG().getMethodOf(src) == Scene.v().getMainMethod()) {
                IdentityStmt is = (IdentityStmt) src;
                Local leftLocal = (Local) is.getLeftOp();
                Value right = is.getRightOp();
                if (right instanceof ParameterRef) {
                    return new Gen<Local>(leftLocal, zeroValue());
                }
            }
            if (src instanceof AssignStmt) {
                AssignStmt assignStmt = (AssignStmt) src;
                Value right = assignStmt.getRightOp();
                if (assignStmt.getLeftOp() instanceof Local) {
                    final Local leftLocal = (Local) assignStmt.getLeftOp();
                    if (right instanceof Local) {
                        final Local rightLocal = (Local) right;
                        return new Transfer<Local>(leftLocal, rightLocal);
                    } else {
                        return new Kill<Local>(leftLocal);
                    }
                }
            }
            return Identity.v();
        }

        @Override
        public FlowFunction<Local> getCallFlowFunction(Unit src, final SootMethod dest) {
            Stmt s = (Stmt) src;
            InvokeExpr ie = s.getInvokeExpr();
            final List<Value> callArgs = ie.getArgs();
            final List<Local> paramLocals = new ArrayList<Local>();
            for (int i = 0; i < dest.getParameterCount(); i++) {
                paramLocals.add(dest.getActiveBody().getParameterLocal(i));
            }
            return new FlowFunction<Local>() {

                public Set<Local> computeTargets(Local source) {
                    // ignore implicit calls to static initializers
                    if (dest.getName().equals(SootMethod.staticInitializerName) && dest.getParameterCount() == 0)
                        return Collections.emptySet();
                    Set<Local> taintsInCaller = new HashSet<Local>();
                    for (int i = 0; i < callArgs.size(); i++) {
                        if (callArgs.get(i).equivTo(source)) {
                            taintsInCaller.add(paramLocals.get(i));
                        }
                    }
                    return taintsInCaller;
                }
            };
        }

        @Override
        public FlowFunction<Local> getReturnFlowFunction(Unit callSite, SootMethod callee, Unit exitStmt, Unit retSite) {
            if (exitStmt instanceof ReturnStmt) {
                ReturnStmt returnStmt = (ReturnStmt) exitStmt;
                Value op = returnStmt.getOp();
                if (op instanceof Local) {
                    if (callSite instanceof DefinitionStmt) {
                        DefinitionStmt defnStmt = (DefinitionStmt) callSite;
                        Value leftOp = defnStmt.getLeftOp();
                        if (leftOp instanceof Local) {
                            final Local tgtLocal = (Local) leftOp;
                            final Local retLocal = (Local) op;
                            return new FlowFunction<Local>() {

                                public Set<Local> computeTargets(Local source) {
                                    if (source == retLocal)
                                        return Collections.singleton(tgtLocal);
                                    return Collections.emptySet();
                                }
                            };
                        }
                    }
                }
            }
            return KillAll.v();
        }

        @Override
        public FlowFunction<Local> getCallToReturnFlowFunction(Unit call, Unit returnSite) {
            return Identity.v();
        }
    };
}
Also used : AssignStmt(soot.jimple.AssignStmt) FlowFunction(heros.FlowFunction) ArrayList(java.util.ArrayList) JimpleLocal(soot.jimple.internal.JimpleLocal) Local(soot.Local) Unit(soot.Unit) FlowFunctions(heros.FlowFunctions) IdentityStmt(soot.jimple.IdentityStmt) ReturnStmt(soot.jimple.ReturnStmt) Stmt(soot.jimple.Stmt) AssignStmt(soot.jimple.AssignStmt) DefinitionStmt(soot.jimple.DefinitionStmt) Gen(heros.flowfunc.Gen) ParameterRef(soot.jimple.ParameterRef) InvokeExpr(soot.jimple.InvokeExpr) Value(soot.Value) Transfer(heros.flowfunc.Transfer) SootMethod(soot.SootMethod) Kill(heros.flowfunc.Kill) ReturnStmt(soot.jimple.ReturnStmt) DefinitionStmt(soot.jimple.DefinitionStmt) IdentityStmt(soot.jimple.IdentityStmt) HashSet(java.util.HashSet)

Aggregations

FlowFunction (heros.FlowFunction)2 FlowFunctions (heros.FlowFunctions)2 Kill (heros.flowfunc.Kill)2 ArrayList (java.util.ArrayList)2 Local (soot.Local)2 SootMethod (soot.SootMethod)2 Unit (soot.Unit)2 Value (soot.Value)2 DefinitionStmt (soot.jimple.DefinitionStmt)2 InvokeExpr (soot.jimple.InvokeExpr)2 ReturnStmt (soot.jimple.ReturnStmt)2 Stmt (soot.jimple.Stmt)2 JimpleLocal (soot.jimple.internal.JimpleLocal)2 Gen (heros.flowfunc.Gen)1 Transfer (heros.flowfunc.Transfer)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 ValueBox (soot.ValueBox)1 AssignStmt (soot.jimple.AssignStmt)1