示例#1
0
 public Constraint(Constraint cons, bool copy)
 {
     if (!copy)
     {
         Conjuncts = new List <Expr>(cons.Conjuncts);
     }
     else
     {
         var duper = new Duplicator();
         Conjuncts = new List <Expr>(cons.Conjuncts.Count);
         foreach (var e in cons.Conjuncts)
         {
             Conjuncts.Add(duper.VisitExpr(e));
         }
     }
 }
示例#2
0
        public static Expr Resolve(Variable var, SymbolicStore store)
        {
            if (var == null || store == null)
            {
                return(null);
            }

            var resolver = new ExprResolver(store);
            var duper    = new Duplicator();
            var ret      = store.Get(new StoreLhs(var));

            if (ret == null)
            {
                Log.Out(Log.Error, "Could not find " + var.Name + " in symbolic store!");
                return(null);
            }

            ret = resolver.VisitExpr(ret);
            ret = duper.VisitExpr(ret);
            return(ret);
        }
示例#3
0
        public static SymbolicBlock ExecBlockDown(Block b, SymbolicStore gammaIntoBlock, out SymbolicStore gammaOutOfBlock)
        {
            var duper = new Duplicator();
            var sb    = new SymbolicBlock(b);

            SymbolicStore gammaIntoCmd = gammaIntoBlock;

            foreach (Cmd c in b.Cmds)
            {
                var sc = new SymbolicCmd(c);
                if (c is AssumeCmd)
                {
                    if (!((AssumeCmd)c).Expr.Equals(Expr.True))
                    {
                        var tmp = ((AssumeCmd)c).Expr;
                        sc.AddCons(((AssumeCmd)c).Expr);
                    }
                }
                else if (c is AssignCmd)
                {
                    SymbolicStore newGamma; //= new SymbolicStore(gammaIntoCmd);

                    AssignCmd assgn = c as AssignCmd;
                    if (assgn.Lhss.Count != assgn.Rhss.Count)
                    {
                        Console.WriteLine("Bizzare: uneven assignment command in ExecDown");
                        continue;
                    }
                    //each AssignCmd contains a list of assignments
                    for (int i = 0; i < assgn.Lhss.Count; i++)
                    {
                        var lhsrhs = StoreLhs.Make(assgn.Lhss[i], assgn.Rhss[i]);
                        var lhs    = lhsrhs.fst;
                        var rhs    = lhsrhs.snd;

                        Expr oldrhs;
                        gammaIntoCmd.TryGetValue(lhs, out oldrhs);

                        //if lhs was uninitialized, we just add the new binding to the old gamma
                        if (oldrhs == null)
                        {
                            newGamma = new SymbolicStore(gammaIntoCmd);
                            newGamma.Add(lhs, rhs);
                        }
                        //otherwise we have to do substitution over everything
                        else
                        {
                            newGamma = new SymbolicStore();
                            newGamma.Add(lhs, Substituter.Apply(x => (x == lhs.Var) ? duper.VisitExpr(oldrhs) : null, rhs));

                            //otherwise, we need to substitute the old value for lhs into Gamma
                            foreach (var entry in gammaIntoCmd)
                            {
                                if (!entry.Key.Equals(lhs))
                                {
                                    newGamma.Add(entry.Key, Substituter.Apply(x => (x == lhs.Var) ? duper.VisitExpr(oldrhs) : null, entry.Value));
                                }
                            }
                        }
                        gammaIntoCmd = newGamma;
                        sc.AddGamma(newGamma);
                    }
                }
                else if (c is HavocCmd)
                {
                    Log.Out(Log.Warning, "Help! HavocCmd in ExecBlockDown!");
                }

                // if(sc.Gammas.Count != 0)
                sb.SCmds.Add(sc);
            }

            gammaOutOfBlock = gammaIntoCmd;
            return(sb);
        }
示例#4
0
        public static void ProcessCounterexamples(SDiffCounterexamples errors, List <Variable> globals, List <Variable> outputVars, Program prog, Implementation vtLeftProcImpl, Implementation vtRightProcImpl)
        {
            List <Expr> constraintExprs = new List <Expr>();

            for (int i = 0; i < errors.Count; i++)
            {
                var impl  = errors[i].Impl;
                var trace = errors[i].Trace;

                Log.Out(Log.Normal, "Attempting symbolic execution of [" + (i + 1) + "] $ " + impl.Name);


                if (Options.GenerateCTrace)
                {
                    Log.Out(Log.Normal, "Constructing metatrace from location annotations");
                    var extractor = new SDiff.SymEx.TraceExtractor();
                    extractor.VisitTrace(trace);
                    //Log.Out(Log.CTrace, CTrace.Make(extractor.metaTrace));
                    var cTrace = CTrace.Make(extractor.metaTrace, null, null, "", "");
                    //var cTrace = "";
                    if (cTrace.Trim() != "")
                    {
                        var fname  = impl.Name + "_cex_" + (i + 1) + "_out.c";
                        var cexOut = new TokenTextWriter(impl.Name + "_cex_" + (i + 1) + "_out.c", true);
                        cexOut.WriteLine(cTrace);
                        cexOut.Close();
                        Log.Out(Log.CTrace, "n:" + (i + 1) + ":" + fname);
                        Log.Out(Log.CTrace, "w:" + fname);
                    }
                }

                Log.Out(Log.Normal, "Desugaring calls");
                //HACK!!!: changes havoc x; assume (x= uf(..)); --> x := uf(...)
                var deCall = new SDiff.SymEx.DesugarCallCmds();
                trace = deCall.VisitTrace(trace);

                //symbolic constants
                var symInParams = new List <Variable>(impl.InParams);
                foreach (var g in globals)
                {
                    symInParams.Add(g);
                }

                Log.Out(Log.Normal, "Executing trace downward");
                //symbolic stores (look at symex.cs for the datastructure)
                var sTrace = SDiff.SymEx.Cexecutor.ExecDown(trace, symInParams);
                Log.Out(Log.Normal, "Executing trace upward");
                //looks at each assume to create the path constants
                //clean up the duplication of assignments
                SDiff.SymEx.Cexecutor.ExecUp(sTrace);

                if (Options.DumpSymTrace)
                {
                    SDiff.SymEx.CexDumper.PrintSymbolicTrace(sTrace);
                }

                //printing the symbolic stores at exit
                //still in SSA form
                Log.Out(Log.Normal, "Grabbing final execution values");
                var lastStore = sTrace.LastSCmd().Gammas.Last();
                var firstCons = sTrace.FirstSCmd().Cons;

                //resolves the outputs to be expressions over uif and symbolic constants only
                Log.Out(Log.Normal, "Resolving result expressions");
                var outSymExprs = new List <Expr>();
                foreach (var v in outputVars)
                {
                    outSymExprs.Add(SymEx.ResolveSymExpr.Resolve(v, lastStore));
                }


                Log.Out(Log.SymEx, "SymEx Results===================================");

                Log.Out(Log.SymEx, "Input constraint");
                if (Options.EraseUFArgumentsOnPrintout) // f(..)
                {
                    var eraser   = new ArgumentParameterEraser();
                    var duper    = new Duplicator();
                    var dupConst = new SDiff.SymEx.Constraint(firstCons, true);
                    foreach (var e in dupConst.Conjuncts)
                    {
                        eraser.VisitExpr(e);
                    }
                    Log.LogEmit(Log.SymEx, 1, dupConst.Emit);
                }
                else
                {
                    Log.LogEmit(Log.SymEx, 1, firstCons.Emit);
                }

                constraintExprs.Add(firstCons.Conjoin());

                Console.WriteLine();


                Log.Out(Log.SymEx, "Symbolic output values");

                var outSymExprsPrintable = outSymExprs;
                if (Options.EraseUFArgumentsOnPrintout) // f(..)
                {
                    outSymExprsPrintable = new List <Expr>();
                    var eraser = new ArgumentParameterEraser();
                    var duper  = new Duplicator();
                    foreach (Expr e in outSymExprs)
                    {
                        var ex = duper.VisitExpr(e);
                        eraser.VisitExpr(ex);
                        outSymExprsPrintable.Add(ex);
                    }
                }

                //if the output variables aren't paired, iterate through normally
                if (outputVars.Count % 2 != 0)
                {
                    for (int j = 0; j < outputVars.Count; j++)
                    {
                        Log.Out(Log.SymEx, "\t" + outputVars[j].ToString() + ": ");
                        Log.LogEmit(Log.SymEx, 2, outSymExprsPrintable[j].Emit);
                        Log.Out(Log.Normal, "");
                    }
                }
                //otherwise don't print a pair if it's syntactically equal
                else
                {
                    for (int j = 0; j < outputVars.Count; j += 2)
                    {
                        if (!outSymExprs[j].StringEquals(outSymExprs[j + 1]))
                        {
                            Log.Out(Log.SymEx, "\t" + outputVars[j].ToString() + ": ");
                            Log.LogEmit(Log.SymEx, 2, outSymExprsPrintable[j].Emit);
                            Log.Out(Log.Normal, "");
                            Log.Out(Log.SymEx, "\t" + outputVars[j + 1].ToString() + ": ");
                            Log.LogEmit(Log.SymEx, 2, outSymExprsPrintable[j + 1].Emit);
                            Log.Out(Log.Normal, "");
                        }
                    }
                }
            }
            Expr disjunctionDiffCond = Expr.False;

            if (Options.PropagateSingleDifference && errors.Count > 1)
            {
                var t = constraintExprs.First();
                disjunctionDiffCond = Expr.Or(disjunctionDiffCond, constraintExprs.First());
            }
            else
            {
                foreach (Expr currentExpr in constraintExprs)
                {
                    disjunctionDiffCond = Expr.Or(disjunctionDiffCond, currentExpr);
                }
            }

            ProcessCounterexampleForDiffInline(errors, vtLeftProcImpl, vtRightProcImpl, disjunctionDiffCond);
        }