summaryrefslogtreecommitdiff
path: root/Source/ExecutionEngine/VerificationResultCache.cs
blob: 044d84fe207d769a74a34c48cc635c59f826d7c1 (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
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 : ReadOnlyVisitor
  {
    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);

      foreach (var param in node.InParams)
      {
        if (param.TypedIdent != null && param.TypedIdent.WhereExpr != null)
        {
          VisitExpr(param.TypedIdent.WhereExpr);
        }
      }

      return base.VisitProcedure(node);
    }

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

      if (node.DefinitionAxiom != null)
      {
        VisitAxiom(node.DefinitionAxiom);
      }

      return base.VisitFunction(node);
    }

    public override Cmd VisitCallCmd(CallCmd node)
    {
      var visited = dependencies.Contains(node.Proc);
      if (!visited)
      {
        VisitProcedure(node.Proc);
      }

      return base.VisitCallCmd(node);
    }

    public override Expr VisitNAryExpr(NAryExpr node)
    {
      var funCall = node.Fun as FunctionCall;
      if (funCall != null)
      {
        var visited = dependencies.Contains(funCall.Func);
        if (!visited)
        {
          VisitFunction(funCall.Func);
        }
      }

      return base.VisitNAryExpr(node);
    }
  }


  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 VerificationResult Lookup(Implementation impl)
    {
      if (!NeedsToBeVerified(impl))
      {
        return Lookup(impl.Id);
      }
      else
      {
        return 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)
    {
      return VerificationPriority(impl) < int.MaxValue;
    }


    public int VerificationPriority(Implementation impl)
    {
      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 (impl.DependenciesChecksum == null || Cache[impl.Id].DependeciesChecksum != impl.DependenciesChecksum)
      {
        return 1;  // low priority (the same snapshot has been verified before, but a callee has changed)
      }
      else
      {
        return int.MaxValue;  // skip verification (highest priority to get them done as soon as possible)
      }
    }
  }

}