summaryrefslogtreecommitdiff
path: root/Source/Doomed/DoomCheck.cs
blob: c1d6736fde71655d909430a5d2d9066858d23277 (plain)
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
//-----------------------------------------------------------------------------
//
// 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 Microsoft.Boogie.GraphUtil;
using System.Diagnostics.Contracts;
using Microsoft.Basetypes;
using Microsoft.Boogie.VCExprAST;

namespace VC
{
    internal class Evc {
        
        public DoomErrorHandler ErrorHandler {
            set {
                m_ErrorHandler = value;
            }
        }
        
      [ContractInvariantMethod]
void ObjectInvariant() 
{
    Contract.Invariant(m_Checker!=null);
}

        private Checker m_Checker;
        private DoomErrorHandler m_ErrorHandler;
        
        [NotDelayed]
        public Evc(Checker check) {
          Contract.Requires(check != null);
            m_Checker = check;
                   
        }
        
        public void Initialize(VCExpr evc) {
          Contract.Requires(evc != null);
            m_Checker.PushVCExpr(evc);
        }
        
    
        public bool CheckReachvar(List<Variable> lv,Dictionary<Expr, int> finalreachvars, 
            int k, int l, bool usenew , out ProverInterface.Outcome outcome) {
          Contract.Requires(lv != null);

          VCExpr vc = VCExpressionGenerator.False;
          if (usenew )
          {
              foreach (Variable v in lv)
              {

                  vc = m_Checker.VCExprGen.Or(
                      m_Checker.VCExprGen.Neq(
                          m_Checker.VCExprGen.Integer(BigNum.ZERO),
                          m_Checker.TheoremProver.Context.BoogieExprTranslator.LookupVariable(v)),
                      vc);
              }
              //Console.WriteLine("TPQuery k={0}, l={1}, |Sp|={2}", k, l, finalreachvars.Count);

              VCExpr vc21 = m_Checker.VCExprGen.Integer(BigNum.ZERO); // Ask: is the necessary or can we use the same instance term in two inequalities?
              VCExpr vc22 = m_Checker.VCExprGen.Integer(BigNum.ZERO);

                  foreach (KeyValuePair<Expr, int> kvp in finalreachvars)
                  {

                      vc21 = m_Checker.VCExprGen.Add(vc21, m_Checker.TheoremProver.Context.BoogieExprTranslator.Translate(kvp.Key));
                      vc22 = m_Checker.VCExprGen.Add(vc22, m_Checker.TheoremProver.Context.BoogieExprTranslator.Translate(kvp.Key));
                  }                  

              VCExpr post = m_Checker.VCExprGen.Gt(m_Checker.VCExprGen.Integer(BigNum.FromInt(l)), vc21);

              if (k != -1)
              {
                  post = m_Checker.VCExprGen.Or(
                        post, m_Checker.VCExprGen.Gt(vc22, m_Checker.VCExprGen.Integer(BigNum.FromInt(k)))
                        );
              }
              vc = (m_Checker.VCExprGen.Or(vc, (post) ));

          }
          else
          {
              
              foreach (Variable v in lv)
              {

                  vc = m_Checker.VCExprGen.Or(
                      m_Checker.VCExprGen.Eq(
                          m_Checker.VCExprGen.Integer(BigNum.ONE),
                          m_Checker.TheoremProver.Context.BoogieExprTranslator.LookupVariable(v)),
                      vc);
              }
              Contract.Assert(vc != null);

              // Add the desired outcome of the reachability variables
              foreach (KeyValuePair<Expr, int> kvp in finalreachvars)
              {
                  vc = m_Checker.VCExprGen.Or(
                      m_Checker.VCExprGen.Neq(
                          m_Checker.VCExprGen.Integer(BigNum.FromInt(kvp.Value)),
                          m_Checker.TheoremProver.Context.BoogieExprTranslator.Translate(kvp.Key)),
                      vc);
              }

          }

            // Todo: Check if vc is trivial true or false            
            outcome = ProverInterface.Outcome.Undetermined;
            Contract.Assert(m_ErrorHandler != null);
            try
            {
              m_Checker.BeginCheck(lv[0].Name, vc, m_ErrorHandler);
              m_Checker.ProverTask.Wait();
              outcome = m_Checker.ReadOutcome();
            }
            catch (UnexpectedProverOutputException e)
            {
              if (CommandLineOptions.Clo.TraceVerify)
              {
                Console.WriteLine("Prover is unable to check {0}! Reason:", lv[0].Name);
                Console.WriteLine(e.ToString());
              }
              return false;
            }
            finally
            {
              m_Checker.GoBackToIdle();
            }
            return true;
        }
    }

    internal class DoomCheck {
        
      [ContractInvariantMethod]
        void ObjectInvariant() 
        {
            Contract.Invariant(Label2Absy!=null);
            Contract.Invariant(m_Check != null);
            Contract.Invariant(m_Evc != null);
            Contract.Invariant(m_Order != null);
        }

        #region Attributes
        public Dictionary<int, Absy> Label2Absy;
        public DoomErrorHandler ErrorHandler {
            set {
                m_ErrHandler = value;
                m_Evc.ErrorHandler = value;
            }
            
            get {
                return m_ErrHandler;
            }
        }
        
        private DoomErrorHandler m_ErrHandler;
        private Checker m_Check;
        private DoomDetectionStrategy m_Order;
        private Evc m_Evc;
        #endregion
        
        public void __DEBUG_PrintStatistics()
        {
            Console.WriteLine("Checked/Total: Bl {0} / {1} EQ {2} / {3} {4} Tr {5} {6} / {7}", m_Order.__DEBUG_BlocksChecked, m_Order.__DEBUG_BlocksTotal, m_Order.__DEBUG_EQCChecked, m_Order.__DEBUG_EQCTotal, m_Order.__DEBUG_EQCLeaf,  m_Order.__DEBUG_TracesChecked, m_Order.__DEBUG_InfeasibleTraces, m_Order.__DEBUG_TracesTotal);
        }

        [NotDelayed]
        public DoomCheck (Implementation passive_impl, Block unifiedExit, Checker check, List<Block> uncheckable, out int assertionCount) {
          Contract.Requires(passive_impl != null);
          Contract.Requires(check != null);
          Contract.Requires(uncheckable != null);
            m_Check = check;            

            int replaceThisByCmdLineOption = CommandLineOptions.Clo.DoomStrategy ;
            if (CommandLineOptions.Clo.DoomStrategy!=-1) Console.Write("Running experiments using {0} /", replaceThisByCmdLineOption);
            switch (replaceThisByCmdLineOption)
            {
                default:
                    {
                        if (CommandLineOptions.Clo.DoomStrategy != -1) Console.WriteLine("Path Cover specialK Strategy");
                        m_Order = new PathCoverStrategyK(passive_impl, unifiedExit, uncheckable);
                        break;
                    }
                case 1:
                    {
                        if (CommandLineOptions.Clo.DoomStrategy != -1) Console.WriteLine("Path Cover L Strategy");
                        m_Order = new PathCoverStrategy(passive_impl, unifiedExit, uncheckable);
                        break;
                    }
                case 2:
                    {
                        if (CommandLineOptions.Clo.DoomStrategy != -1) Console.WriteLine("hasse strategy");
                        m_Order = new HierachyStrategy(passive_impl, unifiedExit, uncheckable);

                        break;
                    }
                case 3:
                    {
                        if (CommandLineOptions.Clo.DoomStrategy != -1) Console.WriteLine("hasse+ce strategy");
                        m_Order = new HierachyCEStrategy(passive_impl, unifiedExit, uncheckable);
                        break;
                    }
                case 4:
                    {
                        if (CommandLineOptions.Clo.DoomStrategy != -1) Console.WriteLine("no strategy");
                        m_Order = new NoStrategy(passive_impl, unifiedExit, uncheckable);
                        break;
                    }
                    
            }

            Label2Absy = new Dictionary<int, Absy>(); // This is only a dummy
            m_Evc = new Evc(check);
            Dictionary<int, Absy> l2a = null;
            VCExpr vce = this.GenerateEVC(passive_impl, out l2a, check, out assertionCount);
            Contract.Assert(vce != null);
            Contract.Assert( l2a!=null);
            Label2Absy = l2a;
          
            m_Evc.Initialize(vce);
        }


        public void RespawnChecker(Implementation passive_impl, Checker check)
        {
            Contract.Requires(check != null);
            m_Check = check;
            Label2Absy = new Dictionary<int, Absy>(); // This is only a dummy
            m_Evc = new Evc(check);
            Dictionary<int, Absy> l2a = null;
            int assertionCount;  // compute and then ignore
            VCExpr vce = this.GenerateEVC(passive_impl, out l2a, check, out assertionCount);
            Contract.Assert(vce != null);
            Contract.Assert(l2a != null);
            Label2Absy = l2a;

            m_Evc.Initialize(vce);            
        }

        /* - Set b to the next block that needs to be checked.
           - Returns false and set b to null if all blocks are checked.           
           - Has to be alternated with CheckLabel; might crash otherwise
        */
        public bool GetNextBlock(out List<Block> lb)
        {
            return m_Order.GetNextBlock(out lb);
        }

        public Stopwatch DEBUG_ProverTime = new Stopwatch();

        /*  - Checking a label means to ask the prover if |= ( rvar=false -> vc ) holds.            
            - outcome is set to Outcome.Invalid if the Block denoted by reachvar is doomed.            
            - returns false if the theorem prover throws an exception, otherwise true.            
        */
        public bool CheckLabel(List<Variable> lv,Dictionary<Expr, int> finalreachvars, out ProverInterface.Outcome outcome) {
           Contract.Requires(lv != null);
            outcome = ProverInterface.Outcome.Undetermined;
            DEBUG_ProverTime.Reset();
            DEBUG_ProverTime.Start();
            if (m_Evc.CheckReachvar(lv,finalreachvars,m_Order.MaxBlocks,m_Order.MinBlocks,m_Order.HACK_NewCheck,  out outcome) ) {
                DEBUG_ProverTime.Stop();
                if (!m_Order.SetCurrentResult(lv, outcome, m_ErrHandler)) {
                    outcome = ProverInterface.Outcome.Undetermined;
                }
                return true;
            } else {
                DEBUG_ProverTime.Stop();
                Console.WriteLine(outcome);
                m_Order.SetCurrentResult(lv, ProverInterface.Outcome.Undetermined, m_ErrHandler);
                return false;
            }
        }

        public List<List<Block/*!>!>!*/>> DoomedSequences { 
            get {
              Contract.Ensures(Contract.ForAll(Contract.Result<List<List<Block>>>(), i=> cce.NonNullElements(i)));

                return m_Order.DetectedBlock;
            }
        }


        #region Error Verification Condition Generation
       /*
                   #region _TESTING_NEW_STUFF_
            CommandLineOptions.Clo.vcVariety = CommandLineOptions.VCVariety.Block;
            //VCExpr wp = Wlp.Block(block, SuccCorrect, context); // Computes wp.S.true
            
            CommandLineOptions.Clo.vcVariety = CommandLineOptions.VCVariety.Doomed;
            #endregion

       */

        VCExpr GenerateEVC(Implementation impl, out Dictionary<int, Absy> label2absy, Checker ch, out int assertionCount) {
          Contract.Requires(impl != null);
          Contract.Requires(ch != null);
          Contract.Ensures(Contract.Result<VCExpr>() != null);

          TypecheckingContext tc = new TypecheckingContext(null);
          impl.Typecheck(tc);
          label2absy = new Dictionary<int, Absy>();
          VCExpr vc;
          switch (CommandLineOptions.Clo.vcVariety) {
            case CommandLineOptions.VCVariety.Doomed:
              vc = LetVC(cce.NonNull(impl.Blocks[0]), label2absy, ch.TheoremProver.Context, out assertionCount);
              break;

            default:
              Contract.Assert(false); throw new cce.UnreachableException();  // unexpected enumeration value
          }
          return vc;
        }

        public VCExpr LetVC(Block startBlock,
                            Dictionary<int, Absy> label2absy,
                            ProverContext proverCtxt,
                            out int assertionCount)
        {
          Contract.Requires(startBlock != null);
          Contract.Requires(label2absy != null);
          Contract.Requires(proverCtxt != null);
          Contract.Ensures(Contract.Result<VCExpr>() != null);

          Hashtable/*<Block, LetVariable!>*/ blockVariables = new Hashtable/*<Block, LetVariable!!>*/();
          List<VCExprLetBinding> bindings = new List<VCExprLetBinding>();
          VCExpr startCorrect = LetVC(startBlock, label2absy, blockVariables, bindings, proverCtxt, out assertionCount);
          if (CommandLineOptions.Clo.vcVariety == CommandLineOptions.VCVariety.Doomed) {
            return proverCtxt.ExprGen.Let(bindings, proverCtxt.ExprGen.Not(startCorrect) );
          } else {
            return proverCtxt.ExprGen.Let(bindings, startCorrect );
          }
        }

        VCExpr LetVC(Block block,
                     Dictionary<int, Absy> label2absy,
                     Hashtable/*<Block, VCExprVar!>*/ blockVariables,
                     List<VCExprLetBinding> bindings,
                     ProverContext proverCtxt,
                     out int assertionCount)
        {
          Contract.Requires(label2absy != null);
          Contract.Requires(blockVariables != null);
          Contract.Requires(proverCtxt != null);
          Contract.Requires(cce.NonNullElements(bindings));
          Contract.Ensures(Contract.Result<VCExpr>() != null);

          assertionCount = 0;
          VCExpressionGenerator gen = proverCtxt.ExprGen;
          Contract.Assert(gen != null);
          VCExprVar v = (VCExprVar)blockVariables[block];
          if (v == null) {
            /*
             * For block A (= block), generate:
             *   LET_binding A_correct = wp(A_body, (/\ S \in Successors(A) :: S_correct))
             * with the side effect of adding the let bindings to "bindings" for any
             * successor not yet visited.
             */
            VCExpr SuccCorrect;
            GotoCmd gotocmd = block.TransferCmd as GotoCmd;
            if (gotocmd == null) {
                if (CommandLineOptions.Clo.vcVariety == CommandLineOptions.VCVariety.Doomed) {
                    SuccCorrect = VCExpressionGenerator.False;
                } else {
                    SuccCorrect = VCExpressionGenerator.True;
                }
            } else {
              Contract.Assert( gotocmd.labelTargets != null);
              List<VCExpr> SuccCorrectVars = new List<VCExpr>(gotocmd.labelTargets.Count);
              foreach (Block successor in gotocmd.labelTargets) {
                Contract.Assert(successor != null);
                int ac;
                VCExpr s = LetVC(successor, label2absy, blockVariables, bindings, proverCtxt, out ac);
                assertionCount += ac;
                SuccCorrectVars.Add(s);
              }
              SuccCorrect = gen.NAry(VCExpressionGenerator.AndOp, SuccCorrectVars);
            }

            VCContext context = new VCContext(label2absy, proverCtxt);
            //        m_Context = context;

            VCExpr vc = Wlp.Block(block, SuccCorrect, context);
            assertionCount += context.AssertionCount;
            v = gen.Variable(block.Label + "_correct", Microsoft.Boogie.Type.Bool);
            
            bindings.Add(gen.LetBinding(v, vc));
            blockVariables.Add(block, v);
          }
          return v;
        }

        
        #endregion 
                       
    }

}