1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.Diagnostics.Contracts;
using System.Collections.Generic;
using Microsoft.Boogie;
using Microsoft.Boogie.VCExprAST;
using Microsoft.Basetypes;
using System.Collections;
using System.IO;
using System.Threading;
using VC;
namespace Microsoft.Boogie.Houdini {
public class ExistentialConstantCollector : StandardVisitor {
public static HashSet<Variable> CollectHoudiniConstants(Houdini houdini, Implementation impl) {
ExistentialConstantCollector collector = new ExistentialConstantCollector(houdini);
collector.VisitImplementation(impl);
return collector.houdiniConstants;
}
private ExistentialConstantCollector(Houdini houdini) {
this.houdini = houdini;
this.houdiniConstants = new HashSet<Variable>();
}
private Houdini houdini;
private HashSet<Variable> houdiniConstants;
public override Cmd VisitAssertRequiresCmd(AssertRequiresCmd node) {
AddHoudiniConstant(node);
return base.VisitAssertRequiresCmd(node);
}
public override Cmd VisitAssertEnsuresCmd(AssertEnsuresCmd node) {
AddHoudiniConstant(node);
return base.VisitAssertEnsuresCmd(node);
}
public override Cmd VisitAssertCmd(AssertCmd node) {
AddHoudiniConstant(node);
return base.VisitAssertCmd(node);
}
public override Cmd VisitAssumeCmd(AssumeCmd node) {
AddHoudiniConstant(node);
return base.VisitAssumeCmd(node);
}
private void AddHoudiniConstant(PredicateCmd node) {
Variable houdiniConstant;
if (houdini.MatchCandidate(node.Expr, out houdiniConstant))
houdiniConstants.Add(houdiniConstant);
}
}
public class HoudiniSession {
public static double proverTime = 0;
public static int numProverQueries = 0;
public static double unsatCoreProverTime = 0;
public static int numUnsatCoreProverQueries = 0;
public static int numUnsatCorePrunings = 0;
public string descriptiveName;
private VCExpr conjecture;
private ProverInterface.ErrorHandler handler;
ConditionGeneration.CounterexampleCollector collector;
HashSet<Variable> unsatCoreSet;
HashSet<Variable> houdiniConstants;
Houdini houdini;
Implementation implementation;
public bool InUnsatCore(Variable constant) {
if (unsatCoreSet == null)
return true;
if (unsatCoreSet.Contains(constant))
return true;
numUnsatCorePrunings++;
return false;
}
public HoudiniSession(Houdini houdini, VCGen vcgen, ProverInterface proverInterface, Program program, Implementation impl) {
descriptiveName = impl.Name;
collector = new ConditionGeneration.CounterexampleCollector();
collector.OnProgress("HdnVCGen", 0, 0, 0.0);
vcgen.ConvertCFG2DAG(impl, program);
ModelViewInfo mvInfo;
Hashtable/*TransferCmd->ReturnCmd*/ gotoCmdOrigins = vcgen.PassifyImpl(impl, program, out mvInfo);
houdiniConstants = ExistentialConstantCollector.CollectHoudiniConstants(houdini, impl);
var exprGen = proverInterface.Context.ExprGen;
VCExpr controlFlowVariableExpr = CommandLineOptions.Clo.UseLabels ? null : exprGen.Integer(BigNum.ZERO);
Hashtable/*<int, Absy!>*/ label2absy;
conjecture = vcgen.GenerateVC(impl, controlFlowVariableExpr, out label2absy, proverInterface.Context);
if (!CommandLineOptions.Clo.UseLabels) {
VCExpr controlFlowFunctionAppl = exprGen.ControlFlowFunctionApplication(exprGen.Integer(BigNum.ZERO), exprGen.Integer(BigNum.ZERO));
VCExpr eqExpr = exprGen.Eq(controlFlowFunctionAppl, exprGen.Integer(BigNum.FromInt(impl.Blocks[0].UniqueId)));
conjecture = exprGen.Implies(eqExpr, conjecture);
}
Macro macro = new Macro(Token.NoToken, descriptiveName, new VariableSeq(), new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "", Type.Bool), false));
proverInterface.DefineMacro(macro, conjecture);
conjecture = exprGen.Function(macro);
if (CommandLineOptions.Clo.vcVariety == CommandLineOptions.VCVariety.Local) {
handler = new VCGen.ErrorReporterLocal(gotoCmdOrigins, label2absy, impl.Blocks, vcgen.incarnationOriginMap, collector, mvInfo, vcgen.implName2LazyInliningInfo, proverInterface.Context, program);
}
else {
handler = new VCGen.ErrorReporter(gotoCmdOrigins, label2absy, impl.Blocks, vcgen.incarnationOriginMap, collector, mvInfo, vcgen.implName2LazyInliningInfo, proverInterface.Context, program);
}
this.houdini = houdini;
this.implementation = impl;
}
private VCExpr BuildAxiom(ProverInterface proverInterface, Dictionary<Variable, bool> currentAssignment) {
ProverContext proverContext = proverInterface.Context;
Boogie2VCExprTranslator exprTranslator = proverContext.BoogieExprTranslator;
VCExpressionGenerator exprGen = proverInterface.VCExprGen;
VCExpr expr = VCExpressionGenerator.True;
foreach (KeyValuePair<Variable, bool> kv in currentAssignment) {
Variable constant = kv.Key;
VCExprVar exprVar = exprTranslator.LookupVariable(constant);
if (kv.Value) {
expr = exprGen.And(expr, exprVar);
}
else {
expr = exprGen.And(expr, exprGen.Not(exprVar));
}
}
/*
foreach (Variable constant in this.houdiniConstants) {
VCExprVar exprVar = exprTranslator.LookupVariable(constant);
if (currentAssignment[constant]) {
expr = exprGen.And(expr, exprVar);
}
else {
expr = exprGen.And(expr, exprGen.Not(exprVar));
}
}
*/
return expr;
}
public ProverInterface.Outcome Verify(ProverInterface proverInterface, Dictionary<Variable, bool> assignment, out List<Counterexample> errors) {
collector.examples.Clear();
if (CommandLineOptions.Clo.Trace) {
Console.WriteLine("Verifying " + descriptiveName);
}
DateTime now = DateTime.UtcNow;
VCExpr vc = proverInterface.VCExprGen.Implies(BuildAxiom(proverInterface, assignment), conjecture);
proverInterface.BeginCheck(descriptiveName, vc, handler);
ProverInterface.Outcome proverOutcome = proverInterface.CheckOutcome(handler);
double queryTime = (DateTime.UtcNow - now).TotalSeconds;
proverTime += queryTime;
numProverQueries++;
if (CommandLineOptions.Clo.Trace) {
Console.WriteLine("Time taken = " + queryTime);
}
errors = collector.examples;
return proverOutcome;
}
public void UpdateUnsatCore(ProverInterface proverInterface, Dictionary<Variable, bool> assignment) {
DateTime now = DateTime.UtcNow;
Boogie2VCExprTranslator exprTranslator = proverInterface.Context.BoogieExprTranslator;
proverInterface.Push();
proverInterface.Assert(conjecture, false);
foreach (var v in assignment.Keys) {
if (assignment[v]) continue;
proverInterface.Assert(exprTranslator.LookupVariable(v), false);
}
List<Variable> assumptionVars = new List<Variable>();
List<VCExpr> assumptionExprs = new List<VCExpr>();
foreach (var v in assignment.Keys) {
if (!assignment[v]) continue;
assumptionVars.Add(v);
assumptionExprs.Add(exprTranslator.LookupVariable(v));
}
List<int> unsatCore;
ProverInterface.Outcome tmp = proverInterface.CheckAssumptions(assumptionExprs, out unsatCore, handler);
System.Diagnostics.Debug.Assert(tmp == ProverInterface.Outcome.Valid);
unsatCoreSet = new HashSet<Variable>();
foreach (int i in unsatCore)
unsatCoreSet.Add(assumptionVars[i]);
proverInterface.Pop();
double unsatCoreQueryTime = (DateTime.UtcNow - now).TotalSeconds;
unsatCoreProverTime += unsatCoreQueryTime;
numUnsatCoreProverQueries++;
}
}
}
|