Beispiel #1
0
 public static void Close()
 {
     if (debugOut != null)
     {
         debugOut.Close();
     }
 }
        // Returns file and line of the failing assert. Dumps
        // error trace to disk.
        public Tuple <string, int> PrintErrorTrace(cba.ErrorTrace trace, string filename, List <Tuple <string, int, string> > eeSlicedSourceLines, string failStatus)
        {
            trace = mapBackTrace(trace);

            if (Driver.printTraceMode == Driver.PRINT_TRACE_MODE.Boogie)
            {
                cba.PrintProgramPath.print(input, trace, filename);
                return(null);
            }
            else
            {
                // relevant lines
                cba.PrintSdvPath.relevantLines = null;
                if (eeSlicedSourceLines != null)
                {
                    cba.PrintSdvPath.relevantLines = new HashSet <Tuple <string, int> >();
                    eeSlicedSourceLines.Iter(tup => cba.PrintSdvPath.relevantLines.Add(Tuple.Create(tup.Item1, tup.Item2)));
                }

                cba.PrintSdvPath.failingLocation = null;
                cba.PrintSdvPath.failStatus      = failStatus;


                cba.PrintSdvPath.Print(input.getProgram(), trace, new HashSet <string>(), "",
                                       filename + ".tt", filename + "stack.txt");

                if (cba.PrintSdvPath.abortMessage != null)
                {
                    var am = new TokenTextWriter(filename + ".txt");
                    am.WriteLine(cba.PrintSdvPath.abortMessage);
                    am.Close();
                }


                if (cba.PrintSdvPath.lastDriverLocation == null)
                {
                    return(null);
                }
                cba.PrintSdvPath.failingLocation = Tuple.Create(cba.PrintSdvPath.lastDriverLocation.Item1, cba.PrintSdvPath.lastDriverLocation.Item3);

                cba.PrintSdvPath.Print(input.getProgram(), trace, new HashSet <string>(), "",
                                       filename + ".tt", filename + "stack.txt");

                if (cba.PrintSdvPath.abortMessage != null)
                {
                    var am = new TokenTextWriter(filename + ".txt");
                    am.WriteLine(cba.PrintSdvPath.abortMessage);
                    am.Close();
                }

                cba.PrintSdvPath.relevantLines   = null;
                cba.PrintSdvPath.failingLocation = null;
                cba.PrintSdvPath.failStatus      = null;

                return(Tuple.Create(cba.PrintSdvPath.lastDriverLocation.Item1, cba.PrintSdvPath.lastDriverLocation.Item3));
            }
        }
Beispiel #3
0
        public static void PrintCindyTrace(List <Block> e, string filename)
        {
            var ttt = new TokenTextWriter(filename, true);

            foreach (Block b in e)
            {
                b.Emit(ttt, 0);
            }
            ttt.Close();
        }
Beispiel #4
0
            public static void DumpExceptionInformation(Exception e)
            {
                if (e.ToString().Contains("An attempt was made to load an assembly from a network location"))
                {
                    Console.Error.WriteLine();
                    Console.Error.WriteLine("GPUVerify has had trouble loading one of its components due to security settings.");
                    Console.Error.WriteLine();
                    Console.Error.WriteLine("In order to run GPUVerify successfully you need to unblock the archive before unzipping it.");
                    Console.Error.WriteLine();
                    Console.Error.WriteLine("To do this:");
                    Console.Error.WriteLine(" - Right click on the .zip file");
                    Console.Error.WriteLine(" - Click \"Properties\"");
                    Console.Error.WriteLine(" - At the bottom of the \"General\" tab you should see:");
                    Console.Error.WriteLine("     Security: This file came from another computer and might be blocked");
                    Console.Error.WriteLine("     to help protect this computer.");
                    Console.Error.WriteLine(" - Click \"Unblock\"");
                    Console.Error.WriteLine(" - Click \"OK\"");
                    Console.Error.WriteLine("Once this is done, unzip GPUVerify afresh and this issue should be resolved.");
                    Environment.Exit((int)ToolExitCodes.INTERNAL_ERROR);
                }

                const string DUMP_FILE = "__gvdump.txt";

                #region Give generic internal error message
                Console.Error.WriteLine("\nGPUVerify: an internal error has occurred, details written to " + DUMP_FILE + ".");
                Console.Error.WriteLine();
                Console.Error.WriteLine("Please consult the troubleshooting guide in the GPUVerify documentation");
                Console.Error.WriteLine("for common problems, and if this does not help, raise an issue via the");
                Console.Error.WriteLine("GPUVerify issue tracker:");
                Console.Error.WriteLine();
                Console.Error.WriteLine("  https://github.com/mc-imperial/gpuverify");
                Console.Error.WriteLine();
                #endregion

                #region Now try to give the user a specific hint if this looks like a common problem
                try {
                    throw e;
                } catch (ProverException) {
                    Console.Error.WriteLine("Hint: It looks like GPUVerify is having trouble invoking its");
                    Console.Error.WriteLine("supporting theorem prover, which by default is Z3.  Have you");
                    Console.Error.WriteLine("installed Z3?");
                } catch (Exception) {
                    // Nothing to say about this
                }
                #endregion

                #region Write details of the exception to the dump file
                using (TokenTextWriter writer = new TokenTextWriter(DUMP_FILE, false)) {
                    writer.Write("Exception ToString:");
                    writer.Write("===================");
                    writer.Write(e.ToString());
                    writer.Close();
                }
                #endregion
            }
Beispiel #5
0
        static void removeTemplates(string file, string outfile)
        {
            var program = ParseProgram(file);

            foreach (var proc in program.TopLevelDeclarations.OfType <Procedure>().Where(p => QKeyValue.FindBoolAttribute(p.Attributes, "template")))
            {
                proc.Ensures.RemoveAll(ens => !ens.Free);
            }

            var sw = new StreamWriter(outfile, false);
            var tw = new TokenTextWriter(sw);

            program.Emit(tw);
            sw.Close();
            tw.Close();
        }
        public static void Run(Program input)
        {
            string          tempFilename = @".temp.bpl";
            TokenTextWriter ttw          = new TokenTextWriter(tempFilename);

            input.Emit(ttw);
            ttw.Close();

            char   delim      = Options.IsLinux() ? '/' : '\\';
            string binaryName = @"." + delim + "references" + delim + "Boogie.exe";
            string z3Path     = @"." + delim + "references" + delim + "z3.4.4.1.exe";
            string arguments  = "/z3exe:" + z3Path + @" /contractInfer /z3opt:smt.RELEVANCY=0 /z3opt:smt.CASE_SPLIT=0 /timeLimit:300 /errorLimit:1 " + tempFilename;

            Utils.Assert(File.Exists(binaryName), "Could not find provided Boogie executable!");

            //Console.WriteLine("\tSTART Executing {0} {1}", binaryName, arguments);
            try
            {
                ProcessStartInfo procInfo = new ProcessStartInfo();
                procInfo.UseShellExecute        = false;
                procInfo.FileName               = binaryName;
                procInfo.Arguments              = arguments;
                procInfo.WindowStyle            = ProcessWindowStyle.Hidden;
                procInfo.RedirectStandardOutput = true;
                Process proc = new Process();
                proc.StartInfo           = procInfo;
                proc.EnableRaisingEvents = false;
                Stopwatch sw = new Stopwatch();
                sw.Start();
                proc.Start();
                string output;
                output = proc.StandardOutput.ReadToEnd();
                proc.WaitForExit();
                sw.Stop();
                //Console.WriteLine("\tEND Executing {0} {1}", binaryName, arguments);
                Console.WriteLine("======================");
                Console.WriteLine(output);
                Console.WriteLine("Time taken: " + sw.ElapsedMilliseconds / 1000 + "s");
                Console.WriteLine("======================");
            }
            catch (Exception e)
            {
                //Console.WriteLine("\tEND Executing {0} {1} with Exception {2}", binaryName, arguments, e);
                Console.WriteLine("Exception: " + e);
            }
        }
        public void PrintInstrumentedProcedures(Program prog)
        {
            if (!Options.splitFiles)
            {
                return;
            }

            int impl_counter = 0;

            foreach (Tuple <string, Cmd, AssertCmd, SlashVerifyCmdType> assertion in assertions)
            {
                string          tag      = Options.tag != "" ? @"." + Options.tag : "";
                var             filename = Options.outputPath + @"/" + Options.splitFilesDir + @"/split_" + impl_counter.ToString() + tag + @".bpl";
                StringWriter    sw       = new StringWriter();
                TokenTextWriter ttw      = new TokenTextWriter(sw);
                Program         new_prog = new Program();
                Implementation  new_impl = null;
                foreach (Declaration d in prog.TopLevelDeclarations)
                {
                    if (!(d is Implementation))
                    {
                        new_prog.AddTopLevelDeclaration(d);
                    }
                    else
                    {
                        new_impl = instrumentAssertion(original_impl, assertion.Item1, assertion.Item2, assertion.Item3);
                        new_prog.AddTopLevelDeclaration(new_impl);
                    }
                }
                new_prog.Emit(ttw);
                Utils.ParseString(sw.ToString(), out new_prog);
                Console.WriteLine("Now analysing split {0}.", impl_counter);
                (new Slicer(new_prog)).Visit(new_prog);
                sw.Close();
                ttw.Close();
                ttw = new TokenTextWriter(filename);
                new_prog.Emit(ttw);
                ttw.Close();
                impl_counter++;
            }
            Console.WriteLine("VCSplitter generated {0} assertions", assertions.Count);
        }
Beispiel #8
0
        public static void Run(Program input)
        {
            StringWriter    sw  = new StringWriter();
            TokenTextWriter ttw = new TokenTextWriter(sw);

            input.Emit(ttw);
            ttw.Close();

            Options.splitMemoryModel = false;
            Options.splitFiles       = true; // HACK
            Program prog;

            Utils.ParseString(sw.ToString(), out prog);
            Dictionary <Tuple <string, Cmd, AssertCmd>, bool> loadAddressRegionDB = DecideAddressRegions(prog, false);

            Utils.ParseString(sw.ToString(), out prog);
            Dictionary <Tuple <string, Cmd, AssertCmd>, bool> storeAddressRegionDB = DecideAddressRegions(prog, true);

            Options.splitMemoryModel = true;
            (new SplitMemoryModeler(storeAddressRegionDB, loadAddressRegionDB, Utils.ProgramIsSplit(input))).Visit(input);
            sw.Close();
        }
        // Print an interleaved trace, using the execution context information present
        // in trace
        public static void print(PersistentCBAProgram program, ErrorTrace trace, string file)
        {
            trace = trace.Copy();
            ErrorTrace.fillInContextSwitchInfo(trace);

            printConsole = false;
            setupPrint(program, trace, file);
            collectAllEvents(trace);
            arrangeEvents();

            // compute LOC
            var stks = new HashSet <string>();

            foreach (var ev in events)
            {
                stks.Add(ev.filename + "::" + ev.lineno.ToString());
            }
            LOC = stks.Count;

            // Print the failing assert
            foreach (var ev in events)
            {
                if (ev.extra.Contains("ASSERTION FAILS"))
                {
                    Console.WriteLine("{0}({1},1): error PF5001: This assertion can fail", sanitizeFileName(ev.filename), ev.lineno);
                }
            }
            Console.WriteLine();

            foreach (var ev in events)
            {
                ev.printEvent();
            }

            if (pathFile != null)
            {
                pathFile.Close();
            }
        }
Beispiel #10
0
        public static int LoopUnroller(string[] args)
        {
            var filename           = args[2];
            int iterationsToUnroll = Int16.Parse(args[1]);
            var outfilename        = args[3];

            string boogieOptions = "/printInstrumented " + Options.BoogieUserOpts;

            //Log.Out(Log.Normal, "Initializing Boogie");
            if (SDiff.Boogie.Process.InitializeBoogie(boogieOptions))
            {
                return(1);
            }

            var program = BoogieUtils.ParseProgram(filename);

            if (program == null)
            {
                Log.Out(Log.Error, "Parse failed.");
                return(1);
            }
            if (BoogieUtils.ResolveAndTypeCheckThrow(program, filename))
            {
                Log.Out(Log.Error, "Check failed.");
                return(1);
            }

            program.UnrollLoops(iterationsToUnroll, false); //setting it to true breaks ex7


            var outchan = new TokenTextWriter(outfilename, true);

            program.Emit(outchan);
            outchan.Close();
            return(0);
        }
Beispiel #11
0
        public static void Main(string[] args)
        {
            if (!CfiVerifier.Options.ParseCommandLine(String.Join(" ", args)))
            {
                return;
            }
            Utils.Assert(args.Length == 3, "Expecting three given arguments (<passes> <inputFile> <outputFile>)!");
            // Arguments: string inputFile, string outputFile
            Program inputProgram;

            Utils.ParseProgram(args[1], out inputProgram);
            Utils.Assert(inputProgram.Implementations.Count() == 1, "Expecting a single implementation in the program");
            string outputName     = args[2];
            string outputBasename = Path.GetFileName(args[2]);

            foreach (string choiceString in args[0].Split(','))
            {
                ProgramChoice choice = (ProgramChoice)Enum.Parse(typeof(ProgramChoice), choiceString);
                Console.WriteLine(choiceString);
                switch (choice)
                {
                case ProgramChoice.SLICE:
                    CLSlicer.Run(inputProgram);
                    break;

                case ProgramChoice.GRAPH_DGML:
                    CL_DGML_GraphEmitter.Run(inputProgram, outputBasename.Split('.')[0]);
                    break;

                case ProgramChoice.GRAPH_DOT:
                    CL_DOT_GraphEmitter.Run(inputProgram, outputBasename.Split('.')[0]);
                    break;

                case ProgramChoice.REMOVE_CODE_BRANCHES:
                    CLRemoveCodeBranches.Run(inputProgram);
                    break;

                case ProgramChoice.SPLIT_MEMORY:
                    CLMemorySplitter.Run(inputProgram);
                    break;

                case ProgramChoice.SLICE_PARTITIONS:
                    CLParitionSlicer.Run(inputProgram);
                    break;

                case ProgramChoice.SIMPLIFY_CONSTANT_EXPRS:
                    CLConstantExpressionSimplifier.Run(inputProgram);
                    break;

                case ProgramChoice.EXTRACT_LOADS:
                    CLLoadExtractor.Run(inputProgram);
                    break;

                case ProgramChoice.VERIFY:
                    CLVerifier.Run(inputProgram);
                    break;

                case ProgramChoice.SLICE_ASSUMES:
                    CLIndiscrimateAssumeSlicer.Run(inputProgram);
                    break;

                case ProgramChoice.ABSTRACT_MEM:
                    CLMemAbstractor.Run(inputProgram);
                    break;

                default:
                    throw new Exception("Not implemented");
                }
            }
            TokenTextWriter ttw = new TokenTextWriter(outputName);

            inputProgram.Emit(ttw);
            ttw.Close();
        }
Beispiel #12
0
        private void ConjecturesToSpecs()
        {

            if (mode != Mode.Corral || CommandLineOptions.Clo.PrintConjectures == null)
                return; // not implemented for other annotation modes yet

            var twr = new TokenTextWriter(CommandLineOptions.Clo.PrintConjectures + "." + ConjectureFileCounter.ToString(), /*pretty=*/ false);
            ConjectureFileCounter++;

            foreach (var c in rpfp.conjectures)
            {
                var name = c.node.Name.GetDeclName();
                if (implName2StratifiedInliningInfo.ContainsKey(name))
                {
                    StratifiedInliningInfo info = implName2StratifiedInliningInfo[c.node.Name.GetDeclName()];
                    Implementation impl = info.impl;
                    Procedure proc = impl.Proc;
                    EmitProcSpec(twr, proc, info, c.bound);
                }
            }

            twr.Close ();     
        }
Beispiel #13
0
		private void FixedPointToSpecs(){
			
			if(mode != Mode.Corral || CommandLineOptions.Clo.PrintFixedPoint == null)
				return; // not implemented for other annotation modes yet
			
            var twr = new TokenTextWriter(CommandLineOptions.Clo.PrintFixedPoint, /*pretty=*/ false);
			Dictionary<string, RPFP.Node> pmap = new Dictionary<string,RPFP.Node> ();

			foreach (var node in rpfp.nodes)
				pmap.Add ((node.Name as VCExprBoogieFunctionOp).Func.Name, node);
			
			foreach (var impl in program.Implementations)
            {
                Contract.Assert(!impl.Name.StartsWith(recordProcName), "Not allowed to have an implementation for this guy");

                Procedure proc = cce.NonNull(impl.Proc);

                {
                    StratifiedInliningInfo info = new StratifiedInliningInfo(impl, program, boogieContext, QuantifierExpr.GetNextSkolemId());
                    implName2StratifiedInliningInfo[impl.Name] = info;
                    // We don't need controlFlowVariable for stratified Inlining
                    //impl.LocVars.Add(info.controlFlowVariable);
                    List<Expr> exprs = new List<Expr>();

                    {
                        if (pmap.ContainsKey(impl.Name))
                        {
                            RPFP.Node node = pmap[impl.Name];
                            var annot = node.Annotation;
                            EmitProcSpec(twr, proc, info, annot);
                        }
                    }
                }
            }
			twr.Close ();
		}
Beispiel #14
0
 private void PrintFunction(Function function)
 {
     var tt = new TokenTextWriter(Console.Out);
     var invars = new List<Expr>(function.InParams.OfType<Variable>().Select(v => Expr.Ident(v)));
     function.Body = function2Value[function.Name].Gamma(invars);
     function.Emit(tt, 0);
     tt.Close();
 }
        // If the impl has multiple calls:
        //   "call foo(args); return;"
        // with the same args, then merge these calls into one
        public static string mergeRecCalls(Implementation impl)
        {
            // find the recursive calls
            var rBlocks = new List <Block>();

            foreach (var blk in impl.Blocks)
            {
                var rc =
                    blk.Cmds
                    .OfType <CallCmd>()
                    .Where(cc => cc.callee == impl.Name);
                if (!rc.Any())
                {
                    continue;
                }
                if (rc.Count() != 1)
                {
                    return(null);
                }

                // make sure recursive call is last in the block
                var rcall = rc.First();
                if (rcall != blk.Cmds.Last())
                {
                    return(null);
                }

                // check return
                if (!(blk.TransferCmd is ReturnCmd))
                {
                    return(null);
                }

                rBlocks.Add(blk);
            }

            if (rBlocks.Count <= 1)
            {
                return(null);
            }

            // grab the rec calls
            var recCalls = new Dictionary <string, CallCmd>();

            rBlocks.Iter(blk => recCalls.Add(blk.Label, blk.Cmds.Last() as CallCmd));

            // prune attributes
            var origAttr = new Dictionary <string, QKeyValue>();

            recCalls.Iter(kvp => origAttr.Add(kvp.Key, kvp.Value.Attributes));

            recCalls.Values
            .Iter(cc => cc.Attributes = BoogieUtil.removeAttrs(new HashSet <string> {
                "si_unique_call", "si_old_unique_call"
            }, cc.Attributes));

            // check that all recursive calls have the same arguments

            // Check 1: ToString
            var callStr = new HashSet <string>();

            recCalls.Values
            .Iter(cc =>
            {
                var str = new System.IO.StringWriter();
                var tt  = new TokenTextWriter(str);
                cc.Emit(tt, 0);
                tt.Close();
                callStr.Add(str.ToString());
                str.Close();
            });

            if (callStr.Count != 1)
            {
                // restore attributes
                recCalls
                .Iter(kvp => kvp.Value.Attributes = origAttr[kvp.Key]);
                return(null);
            }

            // Check 2: AST
            var rc1 = recCalls[recCalls.Keys.First()];

            if (
                recCalls.Values
                .Where(c => !IsSame(rc1, c))
                .Any())
            {
                // restore attributes
                recCalls
                .Iter(kvp => kvp.Value.Attributes = origAttr[kvp.Key]);

                return(null);
            }

            // Merge
            rc1.Attributes = origAttr[recCalls.Keys.First()];
            var nb = BoogieAstFactory.MkBlock(rc1);

            rBlocks.Iter(blk => blk.Cmds.Remove(blk.Cmds.Last()));
            rBlocks.Iter(blk =>
            {
                var gc          = BoogieAstFactory.MkGotoCmd(nb.Label);
                gc.labelTargets = new List <Block>();
                gc.labelTargets.Add(nb);
                blk.TransferCmd = gc;
            });
            impl.Blocks.Add(nb);
            return(nb.Label);
        }
Beispiel #16
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);
        }
        public Dictionary <Tuple <string, Cmd, AssertCmd>, bool> VerifyInstrumentedProcedures(Program prog)
        {
            Dictionary <Tuple <string, Cmd, AssertCmd, SlashVerifyCmdType>, int> intermediate = new Dictionary <Tuple <string, Cmd, AssertCmd, SlashVerifyCmdType>, int>();
            Dictionary <Tuple <string, Cmd, AssertCmd>, bool> result = new Dictionary <Tuple <string, Cmd, AssertCmd>, bool>();
            int numAssertions = 0;

            foreach (Tuple <string, Cmd, AssertCmd, SlashVerifyCmdType> assertion in assertions)
            {
                var filename = Options.outputPath + @"/" + Options.splitFilesDir + @"/intermediate_" + numAssertions.ToString() + ".bpl";

                StringWriter    sw       = new StringWriter();
                TokenTextWriter ttw      = new TokenTextWriter(sw);
                Program         new_prog = new Program();
                Implementation  new_impl = null;
                foreach (Declaration d in prog.TopLevelDeclarations)
                {
                    if (!(d is Implementation))
                    {
                        new_prog.AddTopLevelDeclaration(d);
                    }
                    else
                    {
                        this.splitting = true;
                        new_impl       = instrumentAssertion(original_impl, assertion.Item1, assertion.Item2, assertion.Item3);
                        new_prog.AddTopLevelDeclaration(new_impl);
                    }
                }
                new_prog.Emit(ttw);
                Utils.ParseString(sw.ToString(), out new_prog);
                RemoveCodeBranches.Run(new_impl);
                (new Slicer(new_prog)).Visit(new_prog);
                sw.Close();
                ttw.Close();
                ttw = new TokenTextWriter(filename);
                new_prog.Emit(ttw);
                ttw.Close();
                intermediate[assertion] = numAssertions;
                numAssertions++;
            }

            shared_result_struct = new System.Collections.Concurrent.ConcurrentDictionary <int, bool>();

            //Parallel.For(0, numAssertions, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, i => CheckAssertion(i));
            var delim = Options.IsLinux() ? @"/" : @"\";
            List <Tuple <string, string> > solvers = new List <Tuple <string, string> >();

            AddSolver(solvers, "Z3_441", @"." + delim + "references" + delim + "z3.4.4.1.exe", "/z3opt:smt.RELEVANCY=0 /z3opt:smt.CASE_SPLIT=0");
            AddSolver(solvers, "Z3_441", @"../../../references/z3.4.4.1.exe", "/z3opt:smt.RELEVANCY=0 /z3opt:smt.CASE_SPLIT=0");

            // work stealing parallel implementation
            workItems = new System.Collections.Concurrent.ConcurrentBag <Tuple <string, string, int> >();
            foreach (Tuple <string, string> solver in solvers)
            {
                for (int i = 0; i < numAssertions; i++)
                {
                    workItems.Add(new Tuple <string, string, int>(solver.Item1, solver.Item2, i));
                }
            }


            var threads = new List <Thread>();

            for (int i = 0; i < Environment.ProcessorCount; i++)
            {
                threads.Add(new Thread(new ThreadStart(CheckAssertions)));
            }

            threads.Iter(t => t.Start());
            threads.Iter(t => t.Join());


            foreach (Tuple <string, Cmd, AssertCmd, SlashVerifyCmdType> assertion in assertions)
            {
                result[new Tuple <string, Cmd, AssertCmd>(assertion.Item1, assertion.Item2, assertion.Item3)] =
                    shared_result_struct[intermediate[assertion]];
            }
            return(result);
        }
Beispiel #18
0
        public static void ProcessCounterexamplesWOSymbolicOut(SDiffCounterexamples errors, List <Variable> globals, List <Variable> eqLocVars,
                                                               Implementation vtLeftProcImpl, Implementation vtRightProcImpl, List <Declaration> consts, List <Model> errModelList,
                                                               string v1Name, string v2Name)
        {
            List <Expr>  constraintExprs = new List <Expr>();
            List <Block> listBlocksV1    = new List <Block>();
            List <Block> listBlocksV2    = new List <Block>();

            Model[] errModelArray = errModelList.ToArray();

            var label = new GotoCmd(Token.NoToken, new List <String>()
            {
                "DONE"
            });

            label.labelNames = new List <string>(); // label.labelTargets = new List<Block>();
            label.labelNames.Add("DONE");           //label.labelTargets.Add("DONE");


            // First block
            var labels = new List <String>();

            for (int j = 0; j < errors.Count; j++)
            {
                labels.Add("Cex" + j);
            }

            labels.Add("ELSE");
            var labelEntry = new GotoCmd(Token.NoToken, labels);

            labelEntry.labelTargets = new List <Block>();

            for (int j = 0; j < errors.Count; j++)
            {
                labelEntry.labelNames.Add("Cex" + j);//labelEntry.labelTargets.Add("Cex" + j);
            }

            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 + "] $ " + 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, consts, errModelArray[i], v1Name, v2Name);
                    //var cTrace = "";
                    if (cTrace.Trim() != "")
                    {
                        //note that the index for cex on the output shows 1,2,..., instead of 0,1,2....
                        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);

                Log.Out(Log.Normal, "Grabbing final execution values");
                // var lastStore = sTrace.LastSCmd().Gammas.Last();
                // var firstCons = sTrace.FirstSCmd().Cons;

                // constraintExprs.Add(firstCons.Conjoin());

                //#hemr - generating new strategy right here with inlining counterexamples!

                // Second block
                List <Cmd> cmdsV1_Diff_inline = new List <Cmd>();
                List <Cmd> cmdsV2_Diff_inline = new List <Cmd>();

                Substitution replaceScopeV1 = delegate(Variable x)
                {
                    return(replaceScopeGeneric(vtLeftProcImpl, x));
                };

                Substitution replaceScopeV2 = delegate(Variable x)
                {
                    return(replaceScopeGeneric(vtRightProcImpl, x));
                };

                foreach (Block currentBlock in trace)
                {
                    if (currentBlock.ToString().Contains("inline$" + v1Name))
                    {
                        foreach (Cmd currentCmd in currentBlock.Cmds)
                        {
                            Cmd newCmd = Substituter.Apply(replaceScopeV1, currentCmd);
                            cmdsV1_Diff_inline.Add(newCmd);
                        }
                    }
                    else if (currentBlock.ToString().Contains("inline$" + v2Name))
                    {
                        foreach (Cmd currentCmd in currentBlock.Cmds)
                        {
                            Cmd newCmd = Substituter.Apply(replaceScopeV2, currentCmd);
                            cmdsV2_Diff_inline.Add(newCmd);
                        }
                    }
                }

                Block blockV1 = new Block(Token.NoToken, "Cex" + i, cmdsV1_Diff_inline, label);
                Block blockV2 = new Block(Token.NoToken, "Cex" + i, cmdsV2_Diff_inline, label);

                listBlocksV1.Add(blockV1);
                listBlocksV2.Add(blockV2);

                //#hemr - displaying values related to error model (at console level)

                Model currentModel = errModelArray[i];

                //var keys = currentModel.identifierToPartition.Keys;
                var keys = currentModel.Functions.Where(f => f.Arity == 0).Select(f => f.GetConstant());
            }

            Expr disjunctionDiffCond = Expr.False;

            foreach (Expr currentExpr in constraintExprs)
            {
                disjunctionDiffCond = Expr.Or(disjunctionDiffCond, currentExpr);
            }

            ProcessCounterexampleForDiffInliningWOSymbolicOut(eqLocVars, vtLeftProcImpl, vtRightProcImpl, v1Name, v2Name, listBlocksV1, listBlocksV2, labelEntry);
        }
Beispiel #19
0
        public static int ExtractLoops(string[] args)
        {
            var filename    = args[1];
            var outfilename = args[2];

            string boogieOptions = "/printInstrumented  " + Options.BoogieUserOpts;

            if (args[0] == "-extractLoops") //if -extractLoops:n then don't pass the flag (need to fix a bug 7/1/15)
            {
                boogieOptions += " /deterministicExtractLoops ";
            }

            //Log.Out(Log.Normal, "Initializing Boogie");
            if (SDiff.Boogie.Process.InitializeBoogie(boogieOptions))
            {
                return(1);
            }

            var program = BoogieUtils.ParseProgram(filename);

            if (program == null)
            {
                Log.Out(Log.Error, "Parse failed.");
                return(1);
            }
            if (BoogieUtils.ResolveAndTypeCheckThrow(program, filename))
            {
                Log.Out(Log.Error, "Check failed.");
                return(1);
            }

            Dictionary <string, Dictionary <string, Block> > tmp = program.ExtractLoops();

            // the hacks below should be history now with the deterministic Loop extract in Boogie

            ///*
            // * iterate over each procedure in program
            // * find the extracted procedures by name
            // * Find the exit block in the extracted procedure
            // * The name of the exit block is "exit"
            // * The exit block should have no statements
            // * add "assume false" as the single statement
            // * [FIXME] This is possibly buggy for the simple example {for(i=0..2) l++;} {for(i=0..3) l++;}
            // * [FIX for modular checking]: make the non-det deterministic (see email on Current Loop extract fix for SymDiff, 5/4/12)
            // * This fix may not be ok for the case of inlining, as all instances either execute the body or exit
            // */
            //List<Declaration> impls = program.TopLevelDeclarations.Where(x => x is Implementation);
            //TypedIdent tid = new TypedIdent(new Token(), "__loop_nondet_" /*+ impl.Name*/, new BasicType(SimpleType.Bool));
            //GlobalVariable loopNonDet = new GlobalVariable(new Token(), tid);
            //program.TopLevelDeclarations.Insert(0, loopNonDet);
            //foreach (Implementation impl in impls)
            //    if (impl.Name.Contains("_loop_")) //TODO: this is just too hacky
            //    {
            //        //add a boolean constant
            //        //HACK: We don't add the impl.Name to avoid mapping problems with different loop names
            //        //      This may cause problem if these functions are inlined more than once
            //        string loopHeadLabel = "";
            //        foreach (Block bl in impl.Blocks)
            //        {
            //            if (bl.Label == "entry")
            //                loopHeadLabel = FindLoopHeadLabel(bl);
            //            if (bl.Label == loopHeadLabel) {
            //                AssumeCmd ac = new AssumeCmd(Token.NoToken, Expr.Ident(loopNonDet));
            //                bl.Cmds = new List<Cmd>();
            //                bl.Cmds.Add(ac);
            //            }

            //            if (bl.Label == "exit") {
            //                //insert assume false
            //                //AssumeCmd ac = new AssumeCmd(Token.NoToken, Expr.False);
            //                AssumeCmd ac = new AssumeCmd(Token.NoToken, Expr.Not(Expr.Ident(loopNonDet)));
            //                bl.Cmds = new List<Cmd>();
            //                bl.Cmds.Add(ac);
            //            }
            //        }
            //        if (loopHeadLabel == "") {
            //            throw new Exception("Could not find loophead during loop extraction");
            //        }
            //    }



            var outchan = new TokenTextWriter(outfilename, true);

            program.Emit(outchan);
            outchan.Close();
            return(0);
        }
Beispiel #20
0
        static IEnumerable <string> WriteProgram(Program program, string infile)
        {
            var cd  = System.Environment.CurrentDirectory;
            var ret = new List <string>();

            var result_dir = @"f:\tmp\pmin";

            if (!System.IO.Directory.Exists(result_dir))
            {
                result_dir = @"d:\tmp\pmin";
                if (!System.IO.Directory.Exists(result_dir))
                {
                    ret.Add("Result directory not found");
                    return(ret);
                }
            }

            var trypath = "";
            var cnt     = 0;

            while (true)
            {
                trypath = Path.Combine(result_dir, infile.Replace(".bpl", string.Format("_pm_{0}.bpl", cnt)));
                ret.Add(string.Format("Trying file name {0}", trypath));

                if (System.IO.File.Exists(trypath))
                {
                    cnt++;
                    continue;
                }

                StreamWriter sw;

                try
                {
                    sw = new StreamWriter(trypath, false);
                    var tw = new TokenTextWriter(sw);
                    program.Emit(tw);
                    sw.Close();
                    tw.Close();
                }
                catch (System.IO.IOException)
                {
                    continue;
                }

                break;
            }

            ret.Add(string.Format("File written: {0}", trypath));

            System.IO.StreamWriter mapFile = null;

            while (mapFile == null)
            {
                try
                {
                    mapFile = new System.IO.StreamWriter(result_dir + "\\file_map.txt", true);
                }
                catch (System.IO.IOException)
                {
                    mapFile = null;
                    System.Threading.Thread.Sleep(10);
                }
            }

            mapFile.WriteLine(Path.GetFileName(trypath) + "\t" + infile);
            mapFile.Close();

            ret.Add(string.Format("Written to file_map as well"));
            return(ret);
        }