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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using Microsoft.Contracts;
using System.Collections.Generic;
using Microsoft.Boogie;
using Microsoft.Boogie.Simplify.AST;
using VC;
using Microsoft.Boogie.Z3;
using Microsoft.AbstractInterpretationFramework;
using Microsoft.Boogie.AbstractInterpretation;
using System.Collections;
using System.Compiler;
using System.IO;
using System.Threading;
namespace Microsoft.Boogie.Houdini {
public abstract class HdnCheckerFactory {
public static HdnChecker! BuildHdnChecker(Checker! checker) {
if (checker.TheoremProver is Z3ThreadTheoremProver)
return new HoudiniZ3ApiChecker(checker);
else if (checker.TheoremProver is Z3ProcessTheoremProver) {
if (CommandLineOptions.Clo.houdiniFlags.incremental) {
return new HoudiniZ3CheckerIncr(checker);
} else {
return new HoudiniZ3Checker(checker);
}
} else
throw new Exception("HdnChecker only works with z3");
}
}
public abstract class HdnChecker {
public abstract void PrepareCheck(string! _descriptiveName,
VCExpr! _vc, ProverInterface.ErrorHandler! _handler);
public abstract void PushAxiom(VCExpr! vc);
public abstract void Pop();
public abstract void Check();
public abstract ProverInterface.Outcome ReadOutcome()
throws UnexpectedProverOutputException;
}
public class HoudiniZ3ApiChecker:HdnChecker {
private Z3ThreadTheoremProver! z3prover;
private ProverInterface.ErrorHandler handler;
private ProverInterface.Outcome outcome;
private UnexpectedProverOutputException outputExn;
internal HoudiniZ3ApiChecker(Checker! checker)
requires checker.TheoremProver!=null;
requires checker.TheoremProver is Z3ThreadTheoremProver;
{
this.z3prover = (Z3ThreadTheoremProver)checker.TheoremProver;
}
public override void PrepareCheck(string! _descriptiveName, VCExpr! _vc, ProverInterface.ErrorHandler! _handler) {
handler = _handler;
z3prover.LogComment(_descriptiveName);
z3prover.PrepareCheck(_descriptiveName,_vc);
}
public override void PushAxiom(VCExpr! vc) {
z3prover.PushAxiom(vc);
}
public override void Pop() {
z3prover.Pop();
}
public override void Check() {
z3prover.BeginPreparedCheck();
try {
outcome = z3prover.CheckOutcome((!)handler);
} catch (UnexpectedProverOutputException e) {
outputExn = e;
}
}
public override ProverInterface.Outcome ReadOutcome()
throws UnexpectedProverOutputException;
{
if (outputExn != null) {
throw outputExn;
}
return outcome;
}
}
// version of Z3PTP exposing more of the prover process functionality
public class Z3ProcessTPExposed : Z3ProcessTheoremProver {
[NotDelayed]
public Z3ProcessTPExposed (TextWriter/*?*/ logFileWriter, VCExpressionGenerator! gen, DeclFreeProverContext! ctx, int timeout, bool typed)
throws UnexpectedProverOutputException;
{
base(logFileWriter, gen, ctx, timeout, typed, "z3.exe");
}
//MOVED Directly Z3 ProverInterface for now...
//To introduce this cleanly, need to add a new subproject under Provers
//and must use the commandline option for that, instead of saying /prover:z3
//That way, the class is constructed in class Checker the same way
//all the other provers are constructed
}
public class Z3ExposedFactory : Z3.Factory {
}
//Old version
public class HoudiniZ3Checker:HdnChecker {
private Stack<VCExpr!>! axioms = new Stack<VCExpr!>();
private Checker! checker;
private string descriptiveName;
private VCExpr conjecture;
private ProverInterface.ErrorHandler handler;
internal HoudiniZ3Checker(Checker! checker)
requires checker.TheoremProver!=null;
requires checker.TheoremProver is Z3ProcessTheoremProver;
{
this.checker = checker;
}
public override void PrepareCheck(string! _descriptiveName, VCExpr! _vc, ProverInterface.ErrorHandler! _handler)
{
this.descriptiveName = _descriptiveName;
this.conjecture = _vc;
this.handler = _handler;
}
public override void PushAxiom(VCExpr! vc) {
axioms.Push(vc);
}
public override void Pop() {
axioms.Pop();
}
private VCExpr! BuildVCAxioms(Stack<VCExpr!>! axioms)
requires axioms.Count>0;
{
VCExpr vc_axioms = null;
foreach (VCExpr! axiom in axioms) {
if (vc_axioms==null)
vc_axioms=axiom;
else
vc_axioms = checker.VCExprGen.And(vc_axioms,axiom);
}
return (!)vc_axioms;
}
public override void Check()
{
assert (descriptiveName!=null);
assert (conjecture!=null);
assert (handler!=null);
outcome = ProverInterface.Outcome.Undetermined;
VCExpr! vc;
if (axioms.Count>0) {
VCExpr vc_axioms = BuildVCAxioms(axioms);
vc = checker.VCExprGen.Implies(vc_axioms,conjecture);
} else
vc = conjecture;
checker.BeginCheck(descriptiveName,vc,handler);
WaitHandle.WaitAny(new WaitHandle[] {checker.ProverDone});
}
private ProverInterface.Outcome outcome;
private UnexpectedProverOutputException outputExn;
public override ProverInterface.Outcome ReadOutcome()
throws UnexpectedProverOutputException;
{
try {
outcome = checker.ReadOutcome();
} catch (UnexpectedProverOutputException e) {
throw e;
}
return outcome;
}
}
//new version
public class HoudiniZ3CheckerIncr:HdnChecker {
private Checker! checker;
private string descriptiveName;
private VCExpr conjecture;
private ProverInterface.ErrorHandler handler;
// Hack to delay the exception throwing (from Push/Pop...) for now
private UnexpectedProverOutputException delayedExc;
private Z3ProcessTheoremProver! thmProver;
internal HoudiniZ3CheckerIncr(Checker! checker)
requires checker.TheoremProver!=null;
requires checker.TheoremProver is Z3ProcessTheoremProver;
{
this.checker = checker;
this.thmProver = (Z3ProcessTheoremProver!)checker.TheoremProver;
}
private void delayException(UnexpectedProverOutputException exc) {
if (delayedExc == null) { delayedExc = exc; }
}
public override void PrepareCheck(string! _descriptiveName, VCExpr! _vc,
ProverInterface.ErrorHandler! _handler)
{
this.descriptiveName = _descriptiveName;
this.conjecture = _vc;
this.handler = _handler;
this.thmProver.LogComment(_descriptiveName);
try {
this.thmProver.PrepareCheck(_descriptiveName,_vc);
} catch (UnexpectedProverOutputException exc) {
delayException(exc);
}
}
public override void PushAxiom(VCExpr! vc) {
string! vcStr = vc.ToString();
try {
thmProver.PushAxioms(vcStr);
} catch (UnexpectedProverOutputException exc) {
delayException(exc);
}
}
public override void Pop() {
try {
thmProver.PopAxioms();
} catch (UnexpectedProverOutputException exc) {
delayException(exc);
}
}
public override void Check()
{
assert (descriptiveName!=null);
assert (conjecture!=null);
assert (handler!=null);
outcome = ProverInterface.Outcome.Undetermined;
VCExpr! vc = checker.VCExprGen.False;
checker.BeginCheck(descriptiveName,vc,handler);
WaitHandle.WaitAny(new WaitHandle[] {checker.ProverDone});
}
private ProverInterface.Outcome outcome;
private UnexpectedProverOutputException outputExn;
public override ProverInterface.Outcome ReadOutcome()
throws UnexpectedProverOutputException;
{
// HACK
if (delayedExc != null) {
UnexpectedProverOutputException temp = delayedExc;
delayedExc = null;
throw temp;
}
// Normal stuff
try {
outcome = checker.ReadOutcome();
} catch (UnexpectedProverOutputException e) {
throw e;
}
return outcome;
}
}
public abstract class HdnVCGenFactory {
public abstract HdnVCGen! BuildVCGen(Program! program, string logFilePath, bool appendLogFile);
}
public class Z3APIHdnVCGenFactory:HdnVCGenFactory {
public override HdnVCGen! BuildVCGen(Program! program, string logFilePath, bool appendLogFile) {
return new Z3ApiHdnVCGen(program, logFilePath, appendLogFile);
}
}
public class Z3HdnVCGenFactory:HdnVCGenFactory {
public override HdnVCGen! BuildVCGen(Program! program, string logFilePath, bool appendLogFile) {
return new Z3HdnVCGen(program, logFilePath, appendLogFile);
}
}
public abstract class HdnVCGen {
public abstract void PrepareVerification(Implementation! impl, Program! program);
public abstract void PushAxiom(Axiom! axiom);
public abstract void Pop();
public abstract VC.VCGen.Outcome Verify(out List<Counterexample!>? errors)
throws UnexpectedProverOutputException;
}
public class Z3HdnVCGen: HdnVCGen {
private Program! program;
private string logFilePath;
private bool appendLogFile;
private Implementation implementation;
private Axiom axiom;
private ExtVCGen vcgen;
public Z3HdnVCGen(Program! program, string logFilePath, bool appendLogFile) {
this.program = program;
this.logFilePath = logFilePath;
this.appendLogFile= appendLogFile;
}
public override void PrepareVerification(Implementation! impl, Program! program) {
this.implementation = impl;
this.program = program;
this.vcgen = new ExtVCGen(program,logFilePath,appendLogFile);
this.vcgen.PrepareVerification(this.implementation,this.program);
//watch out for > 1 call -- don't want CFG conversion to happen too many times!!!
}
public override void PushAxiom(Axiom! axiom) {
assert this.vcgen != null;
this.axiom=axiom;
this.vcgen.PushAxiom(this.axiom);
}
public override void Pop() {
assert this.vcgen != null;
this.vcgen.Pop();
this.axiom = null;
}
public override VC.VCGen.Outcome Verify(out List<Counterexample!>? errors)
throws UnexpectedProverOutputException; {
assert(this.vcgen!=null);
return vcgen.Verify(out errors);
}
}
public class Z3ApiHdnVCGen: HdnVCGen {
private ExtVCGen! vcgen;
public Z3ApiHdnVCGen(Program! program, string logFilePath, bool appendLogFile) {
this.vcgen = new ExtVCGen(program,logFilePath,appendLogFile);
}
public override void PrepareVerification(Implementation! impl, Program! program) {
vcgen.PrepareVerification(impl,program);
}
public override void PushAxiom(Axiom! axiom) {
vcgen.PushAxiom(axiom);
}
public override void Pop() {
vcgen.Pop();
}
public override VC.VCGen.Outcome Verify(out List<Counterexample!>? errors)
throws UnexpectedProverOutputException; {
return vcgen.Verify(out errors);
}
}
public class ExtVCGen : VCGen {
private HdnChecker hdnChecker;
private VCExpressionGenerator gen;
CounterexampleCollector! collector = new CounterexampleCollector();
public ExtVCGen(Program! program, string logFilePath, bool appendLogFile) {
base(program,logFilePath,appendLogFile);
}
public void PrepareVerification(Implementation! impl, Program! program) {
collector.OnProgress("HdnVCGen", 0, 0, 0.0);
if (CommandLineOptions.Clo.SoundnessSmokeTest) {
throw new Exception("HoudiniVCGen does not support Soundness smoke test.");
}
ConvertCFG2DAG(impl, program);
Hashtable/*TransferCmd->ReturnCmd*/ gotoCmdOrigins = PassifyImpl(impl, program);
Hashtable/*<int, Absy!>*/! label2absy;
Checker! checker = new Checker(program, this.logFilePath, this.appendLogFile, impl, CommandLineOptions.Clo.ProverKillTime);
VCExpr! vc = GenerateVC(impl, out label2absy, checker);
ErrorReporter reporter;
if (CommandLineOptions.Clo.vcVariety == CommandLineOptions.VCVariety.Local) {
reporter = new ErrorReporterLocal(gotoCmdOrigins, label2absy, impl.Blocks, incarnationOriginMap, collector);
} else {
reporter = new ErrorReporter(gotoCmdOrigins, label2absy, impl.Blocks, incarnationOriginMap, collector);
}
this.gen = checker.VCExprGen;
this.hdnChecker = HdnCheckerFactory.BuildHdnChecker(checker);
this.hdnChecker.PrepareCheck((!) impl.Name, vc, reporter);
}
public void PushAxiom(Axiom! axiom)
{
assume (hdnChecker!=null);
assume (gen!=null);
VCExpr vc = axiom.Expr.VCView(gen);
this.hdnChecker.PushAxiom(vc);
}
public void Pop() {
assume (hdnChecker!=null);
hdnChecker.Pop();
}
public Outcome Verify(out List<Counterexample!>? errors)
throws UnexpectedProverOutputException; {
assume (hdnChecker!=null);
collector.examples.Clear();
hdnChecker.Check();
ProverInterface.Outcome proverOutcome;
proverOutcome = hdnChecker.ReadOutcome();
Outcome verifyOutcome = ReadOutcome(proverOutcome);
if (verifyOutcome == Outcome.Errors) {
assume (collector.examples!=null);
if (collector.examples.Count == 0) {
string memStr = System.Convert.ToString(System.GC.GetTotalMemory(false));
if (memStr != null)
memStr = "?";
throw new UnexpectedProverOutputException("Outcome.Errors w/ 0 counter examples. " + memStr + " memory used");
}
errors = collector.examples;
} else {
errors = null;
}
return verifyOutcome;
}
private Outcome ReadOutcome(ProverInterface.Outcome proverOutcome) {
switch (proverOutcome) {
case ProverInterface.Outcome.Valid:
return Outcome.Correct;
case ProverInterface.Outcome.Invalid:
return Outcome.Errors;
case ProverInterface.Outcome.TimeOut:
return Outcome.TimedOut;
case ProverInterface.Outcome.Undetermined:
return Outcome.Inconclusive;
default:
throw new Exception("Unknown Prover Interface outcome while reading outcome.");
}
}
}
}
|