summaryrefslogtreecommitdiff
path: root/Source/ExecutionEngine/VerificationResultCache.cs
blob: 798411c8ce5f140abeb9aac4f5cca32aa8de6362 (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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using VC;

namespace Microsoft.Boogie
{

  class DependencyCollector : StandardVisitor
  {
    private HashSet<DeclWithFormals> dependencies;

    public DependencyCollector()
    {
      dependencies = new HashSet<DeclWithFormals>();
    }

    public static void Collect(Absy node, out List<DeclWithFormals> dependencies)
    {
      var dc = new DependencyCollector();
      dc.Visit(node);
      dependencies = dc.dependencies.ToList();
    }

    public static string DependenciesChecksum(Implementation impl)
    {
      List<DeclWithFormals> deps;
      DependencyCollector.Collect(impl, out deps);
      if (deps.Any(dep => dep.Checksum == null))
      {
        return null;
      }

      var md5 = System.Security.Cryptography.MD5.Create();
      var data = Encoding.UTF8.GetBytes(deps.MapConcat(dep => dep.Checksum, ""));
      var hashedData = md5.ComputeHash(data);
      return BitConverter.ToString(hashedData);
    }

    public override Procedure VisitProcedure(Procedure node)
    {
      dependencies.Add(node);

      return base.VisitProcedure(node);
    }

    public override Function VisitFunction(Function node)
    {
      dependencies.Add(node);

      return base.VisitFunction(node);
    }

    public override Cmd VisitCallCmd(CallCmd node)
    {
      var result = base.VisitCallCmd(node);

      var visited = dependencies.Contains(node.Proc);
      if (!visited)
      {
        VisitProcedure(node.Proc);
      }

      return result;
    }

    public override Expr VisitNAryExpr(NAryExpr node)
    {
      var result = base.VisitNAryExpr(node);

      var funCall = node.Fun as FunctionCall;
      if (funCall != null)
      {
        var visited = dependencies.Contains(funCall.Func);
        if (!visited)
        {
          VisitFunction(funCall.Func);
          if (funCall.Func.DefinitionAxiom != null)
          {
            VisitAxiom(funCall.Func.DefinitionAxiom);
          }
        }
      }

      return result;
    }
  }


  public class VerificationResult
  {
    public readonly string Checksum;
    public readonly string DependeciesChecksum;
    public readonly string RequestId;
    public readonly ConditionGeneration.Outcome Outcome;
    public readonly List<Counterexample> Errors;

    public VerificationResult(string requestId, string checksum, string depsChecksum, ConditionGeneration.Outcome outcome, List<Counterexample> errors)
    {
      Checksum = checksum;
      DependeciesChecksum = depsChecksum;
      Outcome = outcome;
      Errors = errors;
      RequestId = requestId;
    }
  }


  public class VerificationResultCache
  {
    private readonly ConcurrentDictionary<string, VerificationResult> Cache = new ConcurrentDictionary<string, VerificationResult>();


    public void Insert(string key, VerificationResult result)
    {
      Contract.Requires(key != null);
      Contract.Requires(result != null);

      Cache[key] = result;
    }


    public VerificationResult Lookup(string key)
    {
      VerificationResult result;
      var success = Cache.TryGetValue(key, out result);
      return success ? result : null;
    }


    public void Clear()
    {
      Cache.Clear();
    }


    public void RemoveMatchingKeys(Regex keyRegexp)
    {
      foreach (var kv in Cache)
      {
        if (keyRegexp.IsMatch(kv.Key))
        {
          VerificationResult res;
          Cache.TryRemove(kv.Key, out res);
        }
      }
    }


    public bool NeedsToBeVerified(Implementation impl, string depsChecksumOfImpl)
    {
      return 0 < VerificationPriority(impl, depsChecksumOfImpl);
    }


    public int VerificationPriority(Implementation impl, string depsChecksumOfImpl)
    {
      if (!Cache.ContainsKey(impl.Id))
      {
        return 3;  // high priority (has been never verified before)
      }
      else if (Cache[impl.Id].Checksum != impl.Checksum)
      {
        return 2;  // medium priority (old snapshot has been verified before)
      }
      else if (depsChecksumOfImpl == null || Cache[impl.Id].DependeciesChecksum != depsChecksumOfImpl)
      {
        return 1;  // low priority (the same snapshot has been verified before, but a callee has changed)
      }
      else
      {
        return 0;  // skip verification
      }
    }

  }

}