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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
|
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.IO;
using Microsoft.Boogie;
using Graphing;
using AI = Microsoft.AbstractInterpretationFramework;
using System.Diagnostics.Contracts;
using Microsoft.Basetypes;
using Microsoft.Boogie.VCExprAST;
namespace VC
{
#region SuperClass for different doomed code detection strategies
abstract internal class DoomDetectionStrategy
{
public int __DEBUG_BlocksChecked = 0;
public int __DEBUG_BlocksTotal = 0;
public int __DEBUG_InfeasibleTraces = 0;
public int __DEBUG_TracesChecked = 0;
public int __DEBUG_TracesTotal = 0;
public int __DEBUG_EQCTotal = 0;
public int __DEBUG_EQCLeaf = 0;
public int __DEBUG_EQCChecked = 0;
//Please use this one to toggle your Debug output
protected bool __DEBUGOUT = CommandLineOptions.Clo.DoomStrategy != -1;
protected Implementation impl;
protected BlockHierachy m_BlockH = null;
protected int m_MaxBranchingDepth = 0;
protected int m_MaxK = 0;
protected Stopwatch sw = new Stopwatch();
// This is the List with all detected doomed program points. This List is used by VCDoomed.cs to
// create an error message
public List<List<Block/*!*/>/*!*/>/*!*/ DetectedBlock = new List<List<Block/*!*/>/*!*/>();
private List<Block> __DEBUG_minelements = new List<Block>();
// There is no default constructor, because these parameters are needed for most subclasses
public DoomDetectionStrategy(Implementation imp, Block unifiedexit, List<Block> unreach)
{
m_BlockH = new BlockHierachy(imp, unifiedexit);
__DEBUG_EQCLeaf = m_BlockH.Leaves.Count;
//foreach (BlockHierachyNode bhn in m_BlockH.Leaves)
//{
// if (bhn.Content.Count > 0) __DEBUG_minelements.Add(bhn.Content[0]);
//}
//if (imp.Blocks.Count>0) m_GatherInfo(imp.Blocks[0], 0, 0,0);
if (__DEBUGOUT)
{
Console.WriteLine("MaBranchingDepth {0} MaxMinPP {1} ", m_MaxBranchingDepth, m_MaxK);
Console.WriteLine("AvgLeaverPerPath {0} AvgPLen {1}", 0, 0);
}
MaxBlocks = imp.Blocks.Count;
MinBlocks = imp.Blocks.Count;
HACK_NewCheck = false;
__DEBUG_BlocksTotal = imp.Blocks.Count;
}
public int MaxBlocks, MinBlocks;
public bool HACK_NewCheck;
// This method is called by the prover while it returns true. The prover checks for each
// List lb if
// |= !lb_1 /\ ... /\ !lb_n => wlp(Program, false)
// and passes the result to SetCurrentResult
abstract public bool GetNextBlock(out List<Block> passBlock);
// This method is called to inform about the prover outcome for the previous GetNextBlock call.
abstract public bool SetCurrentResult(List<Variable> reachvar, ProverInterface.Outcome outcome, DoomErrorHandler cb);
protected List<Block> m_GetErrorTraceFromCE(DoomErrorHandler cb)
{
BlockHierachyNode tn=null;
List<Block> errortrace = new List<Block>();
foreach (Block b in cb.TraceNodes)
{
if (errortrace.Contains(b)) continue;
if (m_BlockH.BlockToHierachyMap.TryGetValue(b, out tn))
{
foreach (Block b_ in tn.Unavoidable)
{
if (!errortrace.Contains(b_)) errortrace.Add(b_);
}
foreach (Block b_ in tn.Content)
{
if (!errortrace.Contains(b_)) errortrace.Add(b_);
}
}
}
return errortrace;
}
private List<int> __pathLength = new List<int>();
private List<int> __leavespp = new List<int>();
protected void m_GatherInfo(Block b, int branchingdepth, int leavespp, int plen)
{
if (b.Predecessors.Length > 1) branchingdepth--;
GotoCmd gc = b.TransferCmd as GotoCmd;
if (__DEBUG_minelements.Contains(b)) leavespp++;
plen++;
if (gc != null && gc.labelTargets.Length>0)
{
if (gc.labelTargets.Length > 1) branchingdepth++;
m_MaxBranchingDepth = (branchingdepth > m_MaxBranchingDepth) ? branchingdepth : m_MaxBranchingDepth;
foreach (Block s in gc.labelTargets)
{
m_GatherInfo(s, branchingdepth, leavespp,plen);
}
}
else
{
__pathLength.Add(plen);
__leavespp.Add(leavespp);
m_MaxK = (m_MaxK > leavespp) ? m_MaxK : leavespp;
}
}
}
#endregion
#region BruteForce Strategy
internal class NoStrategy : DoomDetectionStrategy
{
private List<Block> m_Blocks = new List<Block>();
private int m_Current = 0;
public NoStrategy(Implementation imp, Block unifiedexit, List<Block> unreach)
: base(imp, unifiedexit, unreach)
{
m_Blocks = imp.Blocks;
}
override public bool GetNextBlock(out List<Block> lb)
{
if (m_Current < m_Blocks.Count)
{
lb = new List<Block>();
lb.Add(m_Blocks[m_Current]);
m_Current++;
return true;
}
lb = null;
return false;
}
// This method is called to inform about the prover outcome for the previous GetNextBlock call.
override public bool SetCurrentResult(List<Variable> reachvar, ProverInterface.Outcome outcome, DoomErrorHandler cb)
{
this.__DEBUG_BlocksChecked++;
// outcome==Valid means that there is no feasible execution for the current block/path (i.e., might be doomed)
if (outcome == ProverInterface.Outcome.Valid && m_Current <= m_Blocks.Count)
{
List<Block> lb = new List<Block>();
lb.Add(m_Blocks[m_Current - 1]);
DetectedBlock.Add(lb);
}
return true;
}
}
#endregion
#region Only check the minimal elements of the Hasse diagram
internal class HierachyStrategy : DoomDetectionStrategy
{
private List<Block> m_Blocks = new List<Block>();
private List<Block> m_doomedBlocks = new List<Block>();
private int m_Current = 0;
public HierachyStrategy(Implementation imp, Block unifiedexit, List<Block> unreach)
: base(imp, unifiedexit, unreach)
{
foreach (BlockHierachyNode bhn in m_BlockH.Leaves)
{
if (bhn.Content.Count > 0)
{
m_Blocks.Add(bhn.Content[0]);
}
}
}
override public bool GetNextBlock(out List<Block> lb)
{
sw.Start();
if (m_Current < m_Blocks.Count)
{
lb = new List<Block>();
lb.Add(m_Blocks[m_Current]);
m_Current++;
return true;
}
else
{
DetectedBlock.Add(m_BlockH.GetOtherDoomedBlocks(m_doomedBlocks));
}
lb = null;
return false;
}
// This method is called to inform about the prover outcome for the previous GetNextBlock call.
override public bool SetCurrentResult(List<Variable> reachvar, ProverInterface.Outcome outcome, DoomErrorHandler cb)
{
this.__DEBUG_BlocksChecked++;
// outcome==Valid means that there is no feasible execution for the current block/path (i.e., might be doomed)
if (outcome == ProverInterface.Outcome.Valid && m_Current <= m_Blocks.Count)
{
m_doomedBlocks.Add(m_Blocks[m_Current - 1]);
}
if (__DEBUGOUT) Console.WriteLine("K := {0,3} , out {1,8}, time {2,12}", MaxBlocks, outcome, sw.ElapsedTicks);
sw.Stop();
sw.Reset();
return true;
}
}
#endregion
#region Only check the minimal elements of the Hasse diagram and use CEs
internal class HierachyCEStrategy : DoomDetectionStrategy
{
private List<Block> m_Blocks = new List<Block>();
private List<Block> m_doomedBlocks = new List<Block>();
private Block m_Current = null;
public HierachyCEStrategy(Implementation imp, Block unifiedexit, List<Block> unreach)
: base(imp, unifiedexit, unreach)
{
foreach (BlockHierachyNode bhn in m_BlockH.Leaves)
{
if (bhn.Content.Count > 0)
{
m_Blocks.Add(bhn.Content[0]);
}
}
}
override public bool GetNextBlock(out List<Block> lb)
{
m_Current = null;
if (m_Blocks.Count > 0)
{
m_Current = m_Blocks[0];
m_Blocks.Remove(m_Current);
lb = new List<Block>();
lb.Add(m_Current);
return true;
}
else
{
DetectedBlock.Add(m_BlockH.GetOtherDoomedBlocks(m_doomedBlocks));
}
lb = null;
return false;
}
// This method is called to inform about the prover outcome for the previous GetNextBlock call.
override public bool SetCurrentResult(List<Variable> reachvar, ProverInterface.Outcome outcome, DoomErrorHandler cb)
{
this.__DEBUG_BlocksChecked++;
// outcome==Valid means that there is no feasible execution for the current block/path (i.e., might be doomed)
if (outcome == ProverInterface.Outcome.Valid && m_Current != null)
{
m_doomedBlocks.Add(m_Current);
}
else if (outcome == ProverInterface.Outcome.Invalid)
{
List<Block> errortrace = m_GetErrorTraceFromCE(cb);
foreach (Block b in errortrace)
{
if (m_Blocks.Contains(b))
{
m_Blocks.Remove(b);
}
}
cb.TraceNodes.Clear();
}
return true;
}
}
#endregion
#region Path Cover Optimization with L
internal class PathCoverStrategy : DoomDetectionStrategy
{
List<Block> m_Uncheckedlocks = new List<Block>();
List<Block> m_Ignore = new List<Block>();
Random m_Random = new Random();
bool m_NoMoreMoves = false;
private List<Block> m_foundBlock = new List<Block>();
public PathCoverStrategy(Implementation imp, Block unifiedexit, List<Block> unreach)
: base(imp, unifiedexit, unreach)
{
m_Ignore = unreach;
HACK_NewCheck = true;
impl = imp;
foreach (BlockHierachyNode bhn in m_BlockH.Leaves)
{
if (bhn.Content.Count > 0)
{
m_Uncheckedlocks.Add(bhn.Content[0]);
}
}
m_MaxK = m_BlockH.GetMaxK(m_Uncheckedlocks);
MinBlocks = m_MaxK / 2 + (m_MaxK % 2 > 0 ? 1 : 0);
MaxBlocks = -1;
}
override public bool GetNextBlock(out List<Block> lb)
{
sw.Start();
lb = new List<Block>();
if (m_Uncheckedlocks.Count == 0 || m_NoMoreMoves)
{
if (m_Uncheckedlocks.Count > 0)
{
DetectedBlock.Add(m_BlockH.GetOtherDoomedBlocks(m_Uncheckedlocks));
}
return false;
}
lb.AddRange(m_Uncheckedlocks);
return true;
}
override public bool SetCurrentResult(List<Variable> reachvar, ProverInterface.Outcome outcome, DoomErrorHandler cb)
{
this.__DEBUG_BlocksChecked++;
// Valid means infeasible...
int oldl = MinBlocks;
int oldsize = m_Uncheckedlocks.Count;
if (outcome == ProverInterface.Outcome.Valid)
{
this.__DEBUG_InfeasibleTraces++;
if (MinBlocks == 1)
{
m_NoMoreMoves = true;
}
else
{
MinBlocks = 1;
}
}
else if (outcome == ProverInterface.Outcome.Invalid)
{
this.__DEBUG_TracesChecked++;
List<Block> errortrace = m_GetErrorTraceFromCE(cb);
foreach (Block b in errortrace)
{
if (m_Uncheckedlocks.Contains(b))
{
m_Uncheckedlocks.Remove(b);
}
}
cb.TraceNodes.Clear();
m_MaxK = m_BlockH.GetMaxK(m_Uncheckedlocks);
if (m_MaxK < 1)
{
m_NoMoreMoves = true; m_Uncheckedlocks.Clear();
}
MinBlocks = m_MaxK / 2 + (m_MaxK % 2 > 0 ? 1 : 0);
//if (MinBlocks > m_MaxK) MinBlocks = m_MaxK;
}
else
{
m_NoMoreMoves = true; m_Uncheckedlocks.Clear();
}
if (__DEBUGOUT)
Console.WriteLine("K := {0,3}, L := {1,3}, deltaSp {2,3}, out {3,8}, time {4,8}", MaxBlocks, oldl, (oldsize - m_Uncheckedlocks.Count), outcome, sw.ElapsedTicks);
sw.Stop();
sw.Reset();
return true;
}
}
#endregion
#region Path Cover Optimization with K
internal class PathCoverStrategyK : DoomDetectionStrategy
{
List<Block> m_Uncheckedlocks = new List<Block>();
List<Block> m_Ignore = new List<Block>();
Random m_Random = new Random();
bool m_NoMoreMoves = false;
private List<Block> m_foundBlock = new List<Block>();
public PathCoverStrategyK(Implementation imp, Block unifiedexit, List<Block> unreach)
: base(imp, unifiedexit, unreach)
{
m_Ignore = unreach;
HACK_NewCheck = true;
impl = imp;
foreach (BlockHierachyNode bhn in m_BlockH.Leaves)
{
if (bhn.Content.Count > 0)
{
m_Uncheckedlocks.Add(bhn.Content[0]);
}
}
m_MaxK = m_BlockH.GetMaxK(m_Uncheckedlocks);
MaxBlocks = m_Uncheckedlocks.Count;
if (m_MaxK < m_Uncheckedlocks.Count && m_MaxK > 0)
{
MaxBlocks = m_MaxK;
}
else if (m_MaxK >= m_Uncheckedlocks.Count)
{
MaxBlocks = m_Uncheckedlocks.Count;
}
else
{
MaxBlocks = 1;
}
//Console.WriteLine("InitK {0}, Max {1}", m_MaxK, MaxBlocks);
}
override public bool GetNextBlock(out List<Block> lb)
{
sw.Start();
lb = new List<Block>();
if (m_Uncheckedlocks.Count == 0 || m_NoMoreMoves)
{
if (m_Uncheckedlocks.Count > 0)
{
DetectedBlock.Add(m_BlockH.GetOtherDoomedBlocks(m_Uncheckedlocks));
}
return false;
}
lb.AddRange(m_Uncheckedlocks);
MaxBlocks = MaxBlocks > m_Uncheckedlocks.Count ? m_Uncheckedlocks.Count : MaxBlocks;
MinBlocks = MaxBlocks / 2 + (MaxBlocks % 2 > 0 ? 1 : 0);
return true;
}
override public bool SetCurrentResult(List<Variable> reachvar, ProverInterface.Outcome outcome, DoomErrorHandler cb)
{
this.__DEBUG_BlocksChecked++;
// Valid means infeasible...
int oldk = MaxBlocks;
int oldl = MinBlocks;
int oldsize = m_Uncheckedlocks.Count;
if (outcome == ProverInterface.Outcome.Valid)
{
this.__DEBUG_InfeasibleTraces++;
if (MaxBlocks == 1)
{
m_NoMoreMoves = true;
}
else
{
MaxBlocks /= 2;
}
}
else if (outcome == ProverInterface.Outcome.Invalid)
{
this.__DEBUG_TracesChecked++;
List<Block> errortrace = m_GetErrorTraceFromCE(cb);
foreach (Block b in errortrace)
{
if (m_Uncheckedlocks.Contains(b))
{
m_Uncheckedlocks.Remove(b);
}
}
cb.TraceNodes.Clear();
int k = m_BlockH.GetMaxK(m_Uncheckedlocks);
MaxBlocks = (k > MaxBlocks) ? MaxBlocks : k;
}
else
{
m_NoMoreMoves = true; m_Uncheckedlocks.Clear();
}
if (__DEBUGOUT)
Console.WriteLine("K := {0,3}, L := {1,3}, deltaSp {2,3}, out {3,8}, time {4,8}", oldk, oldl, (oldsize - m_Uncheckedlocks.Count), outcome, sw.ElapsedTicks);
sw.Stop();
sw.Reset();
return true;
}
}
#endregion
}
|