public static void addPredicatesToBoogieFile(Program inprog, string outfile, string candidateFile) { // delete {:template} and {:trainingPredicates} procedure DeleteNonFreeTemplates(inprog); BoogieUtil.PrintProgram(inprog, outfile); #region ZL: read summaries from dababase. //var database = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), // "candidates.db"); var database = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), candidateFile); if (!System.IO.File.Exists(database)) { throw new Exception("File does not exist: " + database); } StubAnnotatedSummaryDictionary summaries = null; while (true) { try { using (var fs = new System.IO.FileStream(database, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) { var serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); summaries = StubAnnotatedSummaryDictionary.GetDictionary(serializer.Deserialize(fs)); break; } } catch (System.IO.IOException ex) { Thread.Sleep(300); Console.WriteLine("-- {0}: {1}", ex.GetType().Name, ex.ToString()); } } summaries.DeMarshall(z3exe); var numPreds = 0; string s = summaries.DumpPredicates(out numPreds); // TODO: 1. Add "free requires yogi_error == 0; free ensures alloc >= old(alloc);" // 2. Add the two long candidates for irql rules. // 3. Track Mem_T.int4. // 4. Add free ensures and free requires for type state variables. // 5. Add inline depth argument #endregion using (var file = new System.IO.StreamWriter(outfile, true)) { file.WriteLine("procedure {:template} summaryTemplates();"); file.WriteLine(s); if (debugging) { Console.WriteLine("Written summary tempaltes from cubes.db"); } } }
public static void addAtomsToBoogieFile(Program inprog, string outfile) { DeleteNonFreeTemplates(inprog); BoogieUtil.PrintProgram(inprog, outfile); #region ZL: read atoms from atoms.db. var database = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "atoms.db"); if (!System.IO.File.Exists(database)) { throw new Exception("File does not exist: " + database); } AtomDictionary summaries = null; while (true) { try { using (var fs = new System.IO.FileStream(database, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) { var serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); summaries = serializer.Deserialize(fs) as AtomDictionary; break; } } catch (System.IO.IOException ex) { Thread.Sleep(300); Console.WriteLine("-- {0}: {1}", ex.GetType().Name, ex.ToString()); } } var numPreds = 0; string s = @"procedure {:trainingPredicates} summaryTrainingtemplate();"; //string s = @"procedure {:XXX} YYY();"; s += summaries.DumpPredicates(out numPreds); #endregion // Add procedure summaryTrainingtemplate() by reading summary.db. using (var file = new System.IO.StreamWriter(outfile, true)) { file.WriteLine(s); if (debugging) { Console.WriteLine("Read atoms from atoms.db"); } } }
static void DeleteNonFreeTemplates(Program prog) { // Delete non-free templates prog.TopLevelDeclarations.RemoveAll(decl => BoogieUtil.checkAttrExists("trainingPredicates", decl.Attributes)); var template = prog.TopLevelDeclarations.OfType <Procedure>() .Where(p => BoogieUtil.checkAttrExists("template", p.Attributes)) .FirstOrDefault(); if (template != null) { template.Ensures.RemoveAll(e => !e.Free); template.Requires.RemoveAll(e => !e.Free); } }
private static void addAtomsFromDualityOutput(string filename) { var pwd = Environment.CurrentDirectory; var dualityOutputFiles = System.IO.Directory.GetFiles(pwd, "cp.*").ToList(); dualityOutputFiles.RemoveAll(fname => fname == System.IO.Path.Combine(pwd, "cp.tmp")); if (System.IO.File.Exists("fp.bpl")) { dualityOutputFiles.Add("fp.bpl"); } //foreach (var file in dualityOutputFiles) // Console.WriteLine("file: {0}", file); if (!dualityOutputFiles.Any()) { return; } // Get the set of variables with annotation {:environment}. var slicVars = new HashSet <string>(); var testBpl = BoogieUtil.ParseProgram(filename); testBpl.TopLevelDeclarations.OfType <Variable>() .Where(g => QKeyValue.FindBoolAttribute(g.Attributes, "environment")) .Iter(g => slicVars.Add(g.Name)); var outatoms = new HashSet <string>(); var summaries = new AtomDictionary(); foreach (var file in dualityOutputFiles) { // Parse the procedure summary file fp.bpl generated by Duality. var program = BoogieUtil.ParseProgram(file); // Collect atomic propositions that talk about slic variables. var literalVisitor = new LiteralVisitor(); foreach (var proc in program.TopLevelDeclarations.OfType <Procedure>()) { foreach (var ens in proc.Ensures) { literalVisitor.VisitEnsures(ens); } } // Filter literals that talk about SLIC variables only. var literals = new HashSet <Expr>(); foreach (var lit in literalVisitor.literals) { var fv = new VarsInExpr(); fv.VisitExpr(lit); bool allVarsAreSlicVars = true; foreach (var v in fv.Vars) { if (!slicVars.Contains(v)) { allVarsAreSlicVars = false; } } if (allVarsAreSlicVars) { literals.Add(lit); } } // Get pre/post annotation var GetAtomFromLiteral = new Func <Expr, Tuple <string, Expr> >(e => { if (HasNonOldExpr.Process(e)) { return(Tuple.Create("{:post}", e)); } else { return(Tuple.Create("{:pre}", e)); } }); literals.Select(l => GetAtomFromLiteral(l)) .Iter(t => { summaries.Add(t.Item1, t.Item2); outatoms.Add(t.Item1 + " " + t.Item2); }); } var atomDatabasePath = CommonLib.GlobalConfig.util_result_file; Debug.Assert(!System.IO.File.Exists(atomDatabasePath)); TrainerDB.Driver.WriteDB(summaries, atomDatabasePath); if (debugging) { foreach (var atom in outatoms) { Console.WriteLine("Dumped atom: {0}", atom); } Console.WriteLine("Dumped atoms to db."); } }
static void runGenerator() { var filename = ""; var corralflags = flags.Concat(" "); if (mode == MODE.GENERATE_CANDIDATES) { var prog = BoogieUtil.ParseProgram(bplfile); addAtomsToBoogieFile(prog, "train.bpl"); corralflags += " /trainSummaries /runHoudini:2"; filename = "train.bpl"; } else if (mode == MODE.RUN_CANDIDATES) { var prog = BoogieUtil.ParseProgram(bplfile); addPredicatesToBoogieFile(prog, "test.bpl"); filename = "test.bpl"; corralflags += @" /useDuality "; corralflags += " /bopt:z3exe:" + iz3exe + " "; } else if (mode == MODE.GENERATE_ATOMS) { var prog = BoogieUtil.ParseProgram(bplfile); DeleteNonFreeTemplates(prog); BoogieUtil.PrintProgram(prog, "basic.bpl"); filename = "basic.bpl"; corralflags += @" /useDuality /z3opt:Fixedpoint.no_conj=true /z3opt:Fixedpoint.mbqi=false /z3opt:SMT.QI.max_instances=10000 /z3opt:SMT.QI.EAGER_THRESHOLD=10 /bopt:printFixedPoint:fp.bpl /bopt:printConjectures:cp /recursionBound:100 "; corralflags += " /bopt:z3exe:" + iz3exe + " "; } if (debugging) { Console.WriteLine("Running corral with flags: {0}", corralflags); } var corralout = CommonLib.Util.run(Environment.CurrentDirectory, wlimitexe, string.Format("/w {0} {1} {2}", timeout, corralexe, filename + " " + corralflags)); if (mode == MODE.GENERATE_ATOMS) { addAtomsFromDualityOutput(filename); Console.WriteLine("Done"); } else if (mode == MODE.GENERATE_CANDIDATES) { addSummaryPredicate(); Console.WriteLine("Done"); } else { File.WriteAllLines(CommonLib.GlobalConfig.util_result_file, corralout); string output1 = CommonLib.Util.parseOutput(corralout, false); // Run vanilla Corral without Houdini inference string corralFlagsNoHoudini = ""; corralflags.Split(' ').Iter <string>(n => { if (!n.StartsWith("/runHoudini")) { corralFlagsNoHoudini += n + " "; } }); if (debugging) { Console.WriteLine("Running corral with flags: {0}", corralflags); } var corralout2 = CommonLib.Util.run(Environment.CurrentDirectory, wlimitexe, string.Format("/w {0} {1} {2}", timeout, corralexe, filename + " " + corralFlagsNoHoudini)); File.AppendAllLines(CommonLib.GlobalConfig.util_result_file, corralout2); string output2 = CommonLib.Util.parseOutput(corralout2, false); Console.WriteLine("{0} {1}", output1, output2); } }
static void runGenerator() { var filename = ""; var corralflags = flags.Concat(" "); if (mode == MODE.GENERATE_CANDIDATES) { var prog = BoogieUtil.ParseProgram(bplfile); loadTemplates(prog, "train.bpl"); //corralflags += " /trainSummaries /runHoudini:2"; // "trainSummaries" runs AbstractHoudini and generates "candidates.txt" filename = "train.bpl"; // Corral just needs to be run to generate the final bpl file //corralflags += @" /useDuality /z3opt:Fixedpoint.no_conj=true /z3opt:Fixedpoint.mbqi=false /z3opt:SMT.QI.max_instances=10000 /z3opt:SMT.QI.EAGER_THRESHOLD=10 /bopt:printFixedPoint:fp.bpl /bopt:printConjectures:cp /recursionBound:100 "; corralflags += @" /useDuality /z3opt:Fixedpoint.no_conj=true /z3opt:Fixedpoint.mbqi=false /z3opt:SMT.QI.max_instances=10000 /z3opt:SMT.QI.EAGER_THRESHOLD=10 /recursionBound:100 "; //corralflags += @" /useDuality /bopt:printFixedPoint:fp.bpl /bopt:printConjectures:cp /recursionBound:100 "; corralflags += " /bopt:z3exe:" + iz3exe + " "; corralflags += " /printFinalProg:f.bpl "; } else if (mode == MODE.RUN_CANDIDATES) { var prog = BoogieUtil.ParseProgram(bplfile); addPredicatesToBoogieFile(prog, "test0.bpl", "candidates0.db"); addPredicatesToBoogieFile(prog, "test1.bpl", "candidates1.db"); addPredicatesToBoogieFile(prog, "test2.bpl", "candidates2.db"); addPredicatesToBoogieFile(prog, "test3.bpl", "candidates3.db"); //filename = "test.bpl"; filename = null; //corralflags += " /timeLimit:3000 /memLimit:4096 "; } else if (mode == MODE.GENERATE_TEMPLATES) { var prog = BoogieUtil.ParseProgram(bplfile); DeleteNonFreeTemplates(prog); BoogieUtil.PrintProgram(prog, "basic.bpl"); filename = "basic.bpl"; corralflags += @" /useDuality /z3opt:Fixedpoint.no_conj=true /z3opt:Fixedpoint.mbqi=false /z3opt:SMT.QI.max_instances=10000 /z3opt:SMT.QI.EAGER_THRESHOLD=10 /bopt:printFixedPoint:fp.bpl /bopt:printConjectures:cp /recursionBound:100 "; corralflags += " /bopt:z3exe:" + iz3exe + " "; } if (mode == MODE.GENERATE_TEMPLATES) { if (debugging) { Console.WriteLine("Running corral with flags: {0}", corralflags); } var corralout = CommonLib.Util.run(Environment.CurrentDirectory, wlimitexe, string.Format("/w {0} {1} {2}", timeout, corralexe, filename + " " + corralflags)); if (!System.IO.File.Exists("fp.bpl")) { Console.WriteLine("Summary file is not found!"); return; } CommandLineOptions.Clo.ApplyDefaultOptions(); // needed for creating prover CommandLineOptions.Clo.StratifiedInlining = 1; addTemplatesFromDualityOutput(filename); Console.WriteLine("Done"); } else if (mode == MODE.GENERATE_CANDIDATES) { if (debugging) { Console.WriteLine("Running corral with flags: {0}", corralflags); } var corralout = CommonLib.Util.run(Environment.CurrentDirectory, wlimitexe, string.Format("/w {0} {1} {2}", timeout, corralexe, filename + " " + corralflags)); CommandLineOptions.Clo.ApplyDefaultOptions(); // needed for creating prover CommandLineOptions.Clo.StratifiedInlining = 1; addSummaryTemplate("train.bpl"); Console.WriteLine("Done"); } else { File.WriteAllText(CommonLib.GlobalConfig.util_result_file, "Starting Run"); // clear out the file #if false // (1) Run vanilla Corral+SI WITH manual summaries string output1 = runCorral(bplfile, corralflags, false); // (2) Run vanilla Corral+SI WITHOUT manual summaries string corralFlagsNoHoudini = ""; corralflags.Split(' ').Iter <string>(n => { if (!n.StartsWith("/runHoudini")) { corralFlagsNoHoudini += n + " "; } }); string output2 = runCorral(bplfile, corralFlagsNoHoudini, false); // (3) Run vanilla Corral+Duality WITH manual summaries string output3 = runCorral(bplfile, corralflags, true); // (4) Run vanilla Corral+Duality WITHOUT manual summaries string output4 = runCorral(bplfile, corralFlagsNoHoudini, true); #endif string output = ""; if (runCandidatesMode == 0) { // (5) Run Corral+SI WITH predicate summaries output = runCorral("test0.bpl", corralflags, false); } if (runCandidatesMode == 1) { // (6) Run Corral+SI WITH template(Clauses) summaries output = runCorral("test1.bpl", corralflags, false); } if (runCandidatesMode == 2) { // (7) Run Corral+SI WITH template(BoundedTail) summaries output = runCorral("test2.bpl", corralflags, false); } if (runCandidatesMode == 3) { // (8) Run Corral+SI WITH template(FreeTail) summaries output = runCorral("test3.bpl", corralflags, false); } Console.WriteLine("{0}", output); } }