-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathMoreCompleteExhaleSupporter.scala
More file actions
396 lines (333 loc) · 16.4 KB
/
Copy pathMoreCompleteExhaleSupporter.scala
File metadata and controls
396 lines (333 loc) · 16.4 KB
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2011-2019 ETH Zurich.
package viper.silicon.rules
import scala.collection.mutable.ListBuffer
import viper.silicon.{MList, MMap}
import viper.silicon.interfaces.state._
import viper.silicon.interfaces.{Success, VerificationResult}
import viper.silicon.resources.{FieldID, NonQuantifiedPropertyInterpreter, Resources}
import viper.silicon.rules.chunkSupporter.findChunksWithID
import viper.silicon.state._
import viper.silicon.state.terms._
import viper.silicon.state.terms.perms.{IsNonPositive, IsPositive}
import viper.silicon.supporters.functions.NoopFunctionRecorder
import viper.silicon.verifier.Verifier
import viper.silver.ast
import viper.silver.verifier.VerificationError
object moreCompleteExhaleSupporter extends SymbolicExecutionRules {
sealed trait TaggedSummarisingSnapshot {
def snapshot: Term
}
final case class FreshSummarisingSnapshot(snapshot: Term) extends TaggedSummarisingSnapshot
final case class ReusedSummarisingSnapshot(snapshot: Term) extends TaggedSummarisingSnapshot
private def summariseOnly(s: State,
relevantChunks: Seq[NonQuantifiedChunk],
resource: ast.Resource,
args: Seq[Term],
v: Verifier)
: (State, TaggedSummarisingSnapshot, Seq[Term], Term) = {
// TODO: Since relevantChunks is a sequence, the order of the chunks affects caching, but shouldn't.
// An order-agnostic way of caching, would be better. A simple benchmark should reveal how
// many cache misses are due to order changes.
// TODO: Caching would be more effective if the summary were created independently of the provided
// args. E.g. the summary could be created with free arguments ?a1, ?a2, ...; this summary
// could be cached, and ?a1 etc. would be replaced before returning the summary to the caller.
Verifier.config.mapCache(s.ssCache.get((resource, relevantChunks, args))) match {
case Some((_taggedSummarisingSnapshot, _summarisingSnapshotDefinitions, _permissionSum)) =>
return (s, _taggedSummarisingSnapshot, _summarisingSnapshotDefinitions, _permissionSum)
case _ =>
/* Cache miss */
}
val sort: Sort =
resource match {
case f: ast.Field => v.symbolConverter.toSort(f.typ)
case _: ast.Predicate => sorts.Snap
case _: ast.MagicWand => sorts.Snap
}
val `?s` = Var(Identifier("?s"), sort)
var summarisingSnapshotDefinitions: Seq[Term] = Vector.empty
var permissionSum: Term = NoPerm
relevantChunks.foreach(ch => {
val argumentEqualities =
And(ch.args.zip(args).map { case (t1, t2) => t1 === t2 })
summarisingSnapshotDefinitions :+=
Implies(And(argumentEqualities, IsPositive(ch.perm)), `?s` === ch.snap)
permissionSum =
PermPlus(permissionSum, Ite(argumentEqualities, ch.perm, NoPerm))
})
val taggedSummarisingSnapshot =
summarisingSnapshotDefinitions
.collectFirst {
case Equals(`?s`, snap) => ReusedSummarisingSnapshot(snap)
}.getOrElse({
// val ss = v.decider.appliedFresh("ss", sort, s.relevantQuantifiedVariables)
val ss = v.decider.appliedFresh("ss", sort, s.functionRecorderQuantifiedVariables())
FreshSummarisingSnapshot(ss)
})
val summarisingSnapshot = taggedSummarisingSnapshot.snapshot
summarisingSnapshotDefinitions =
summarisingSnapshotDefinitions map (_.replace(`?s`, summarisingSnapshot))
val ssc1 = s.ssCache + ((resource, relevantChunks, args) -> (taggedSummarisingSnapshot, summarisingSnapshotDefinitions, permissionSum))
val s1 = s.copy(ssCache = ssc1)
(s1, taggedSummarisingSnapshot, summarisingSnapshotDefinitions, permissionSum)
}
private def summarise(s: State,
relevantChunks: Seq[NonQuantifiedChunk],
resource: ast.Resource,
args: Seq[Term],
v: Verifier)
(Q: (State, Term, Seq[Term], Term, Verifier) => VerificationResult)
: VerificationResult = {
// Don't use the shortcut if we want a counterexample; in that case, we need the decider to perform a single
// query to check if the permission amount we have is sufficient to get the correct counterexample. If we perform
// the query in two parts (one part here, one part in our caller to see if the permission amount is sufficient),
// the counterexample might be wrong.
if (relevantChunks.size == 1 && !Verifier.config.counterexample.isDefined) {
val chunk = relevantChunks.head
if (v.decider.check(And(chunk.args.zip(args).map { case (t1, t2) => t1 === t2 }), Verifier.config.checkTimeout())) {
return Q(s, chunk.snap, Seq(), chunk.perm, v)
} else {
return Q(s, chunk.snap, Seq(), NoPerm, v)
}
}
val (s1, taggedSnap, snapDefs, permSum) = summariseOnly(s, relevantChunks, resource, args, v)
v.decider.assumeDefinition(And(snapDefs))
// v.decider.assume(PermAtMost(permSum, FullPerm())) /* Done in StateConsolidator instead */
val s2 =
taggedSnap match {
case _: FreshSummarisingSnapshot =>
val smd = SnapshotMapDefinition(resource, taggedSnap.snapshot, snapDefs, Seq.empty)
val fr2 = s1.functionRecorder.recordFvfAndDomain(smd)
s1.copy(functionRecorder = fr2)
case _ =>
s1
}
Q(s2, taggedSnap.snapshot, snapDefs, permSum, v)
}
def lookupComplete(s: State,
h: Heap,
resource: ast.Resource,
args: Seq[Term],
ve: VerificationError,
v: Verifier)
(Q: (State, Term, Verifier) => VerificationResult)
: VerificationResult = {
val id = ChunkIdentifier(resource, s.program)
val relevantChunks = findChunksWithID[NonQuantifiedChunk](h.values, id).toSeq
if (relevantChunks.isEmpty) {
if (v.decider.checkSmoke(true)) {
Success() // TODO: Mark branch as dead?
} else {
createFailure(ve, v, s)
}
} else {
summarise(s, relevantChunks, resource, args, v)((s1, snap, _, permSum, v1) =>
v.decider.assert(IsPositive(permSum)) {
case true =>
Q(s1, snap, v1)
case false =>
createFailure(ve, v, s1)
})
}
}
def consumeComplete(s: State,
h: Heap,
resource: ast.Resource,
args: Seq[Term],
perms: Term,
ve: VerificationError,
v: Verifier)
(Q: (State, Heap, Option[Term], Verifier) => VerificationResult)
: VerificationResult = {
if (!s.hackIssue387DisablePermissionConsumption)
actualConsumeComplete(s, h, resource, args, perms, ve, v)(Q)
else
summariseHeapAndAssertReadAccess(s, h, resource, args, ve, v)(Q)
}
private def summariseHeapAndAssertReadAccess(s: State,
h: Heap,
resource: ast.Resource,
args: Seq[Term],
ve: VerificationError,
v: Verifier)
(Q: (State, Heap, Option[Term], Verifier) => VerificationResult)
: VerificationResult = {
val id = ChunkIdentifier(resource, s.program)
val relevantChunks = findChunksWithID[NonQuantifiedChunk](h.values, id).toSeq
summarise(s, relevantChunks, resource, args, v)((s1, snap, _, permSum, v1) =>
v.decider.assert(IsPositive(permSum)) {
case true =>
Q(s1, h, Some(snap), v1)
case false =>
createFailure(ve, v, s1)
})
}
private def actualConsumeComplete(s: State,
h: Heap,
resource: ast.Resource,
args: Seq[Term],
perms: Term,
ve: VerificationError,
v: Verifier)
(Q: (State, Heap, Option[Term], Verifier) => VerificationResult)
: VerificationResult = {
val id = ChunkIdentifier(resource, s.program)
val relevantChunks = ListBuffer[NonQuantifiedChunk]()
val otherChunks = ListBuffer[Chunk]()
h.values foreach {
case c: NonQuantifiedChunk if id == c.id => relevantChunks.append(c)
case ch => otherChunks.append(ch)
}
if (relevantChunks.isEmpty) {
// if no permission is exhaled, return none
v.decider.assert(perms === NoPerm) {
case true => Q(s, h, None, v)
case false => createFailure(ve, v, s)
}
} else {
if (!terms.utils.consumeExactRead(perms, s.constrainableARPs)) {
actualConsumeCompleteConstrainable(s, relevantChunks, resource, args, perms, ve, v)((s1, updatedChunks, optSnap, v2) => {
Q(s1, Heap(updatedChunks ++ otherChunks), optSnap, v2)})
} else {
var pNeeded = perms
var pSum: Term = NoPerm
val newChunks = ListBuffer[NonQuantifiedChunk]()
var moreNeeded = true
val definiteAlias = chunkSupporter.findChunk[NonQuantifiedChunk](relevantChunks, id, args, v)
val sortFunction: (NonQuantifiedChunk, NonQuantifiedChunk) => Boolean = (ch1, ch2) => {
// The definitive alias and syntactic aliases should get priority, since it is always
// possible to consume from them
definiteAlias.contains(ch1) || !definiteAlias.contains(ch2) && ch1.args == args
}
val additionalArgs = s.relevantQuantifiedVariables
var currentFunctionRecorder = s.functionRecorder
relevantChunks.sortWith(sortFunction) foreach { ch =>
if (moreNeeded) {
val eq = And(ch.args.zip(args).map { case (t1, t2) => t1 === t2 })
val pTaken = if (s.functionRecorder != NoopFunctionRecorder || Verifier.config.useFlyweight) {
// ME: When using Z3 via API, it is beneficial to not use macros, since macro-terms will *always* be different
// (leading to new terms that have to be translated), whereas without macros, we can usually use a term
// that already exists.
// During function verification, we should not define macros, since they could contain resullt, which is not
// defined elsewhere.
Ite(eq, PermMin(ch.perm, pNeeded), NoPerm)
} else {
val pTakenBody = Ite(eq, PermMin(ch.perm, pNeeded), NoPerm)
val pTakenArgs = additionalArgs
val pTakenDecl = v.decider.freshMacro("mce_pTaken", pTakenArgs, pTakenBody)
val pTakenMacro = Macro(pTakenDecl.id, pTakenDecl.args.map(_.sort), pTakenDecl.body.sort)
currentFunctionRecorder = currentFunctionRecorder.recordFreshMacro(pTakenDecl)
val pTakenApp = App(pTakenMacro, pTakenArgs)
v.symbExLog.addMacro(pTakenApp, pTakenBody)
pTakenApp
}
pSum = PermPlus(pSum, Ite(eq, ch.perm, NoPerm))
val newChunk = ch.withPerm(PermMinus(ch.perm, pTaken))
pNeeded = PermMinus(pNeeded, pTaken)
if (!v.decider.check(IsNonPositive(newChunk.perm), Verifier.config.splitTimeout())) {
newChunks.append(newChunk)
}
moreNeeded = !v.decider.check(pNeeded === NoPerm, Verifier.config.splitTimeout())
} else {
newChunks.append(ch)
}
}
val allChunks = otherChunks ++ newChunks
// TODO: Since no permissions were gained, I don't see why the PropertyInterpreter would yield any new assumptions.
// See if it can be removed here.
val interpreter = new NonQuantifiedPropertyInterpreter(allChunks, v)
newChunks foreach { ch =>
val resource = Resources.resourceDescriptions(ch.resourceID)
v.decider.assume(interpreter.buildPathConditionsForChunk(ch, resource.instanceProperties))
}
val newHeap = Heap(allChunks)
val s0 = s.copy(functionRecorder = currentFunctionRecorder)
summarise(s0, relevantChunks.toSeq, resource, args, v)((s1, snap, _, _, v1) => {
val condSnap = if (v1.decider.check(IsPositive(perms), Verifier.config.checkTimeout())) {
snap
} else {
Ite(IsPositive(perms), snap.convert(sorts.Snap), Unit)
}
if (!moreNeeded) {
Q(s1, newHeap, Some(condSnap), v1)
} else {
v1.decider.assert(pNeeded === NoPerm) {
case true =>
Q(s1, newHeap, Some(condSnap), v1)
case false =>
createFailure(ve, v1, s1)
}
}})
}
}
}
private def actualConsumeCompleteConstrainable(s: State,
relevantChunks: ListBuffer[NonQuantifiedChunk],
resource: ast.Resource,
args: Seq[Term],
perms: Term, // Expected to be constrainable. Will be assumed to equal the consumed permission amount.
ve: VerificationError,
v: Verifier)
(Q: (State, ListBuffer[NonQuantifiedChunk], Option[Term], Verifier) => VerificationResult)
: VerificationResult = {
var totalPermSum: Term = NoPerm
var totalPermTaken: Term = NoPerm
var newFr = s.functionRecorder
val updatedChunks =
relevantChunks map (ch => {
val eq = And(ch.args.zip(args).map { case (t1, t2) => t1 === t2 })
val permTaken = v.decider.fresh("p", sorts.Perm)
totalPermSum = PermPlus(totalPermSum, Ite(eq, ch.perm, NoPerm))
totalPermTaken = PermPlus(totalPermTaken, permTaken)
val constraint = And(IsValidPermVar(permTaken),
PermAtMost(permTaken, ch.perm),
Implies(Not(eq), permTaken === NoPerm)
)
v.decider.assume(constraint)
newFr = newFr.recordArp(permTaken, constraint)
ch.withPerm(PermMinus(ch.perm, permTaken))
})
v.decider.assume(
Implies(
totalPermSum !== NoPerm,
And(
PermLess(NoPerm, totalPermTaken),
PermLess(totalPermTaken, totalPermSum))))
val s1 = s.copy(functionRecorder = newFr)
v.decider.assert(totalPermTaken !== NoPerm) {
case true =>
v.decider.assume(perms === totalPermTaken)
summarise(s1, relevantChunks.toSeq, resource, args, v)((s2, snap, _, _, v1) =>
Q(s2, updatedChunks, Some(snap), v1))
case false =>
createFailure(ve, v, s)
}
}
private val freeReceiver = Var(Identifier("?rcvr"), sorts.Ref)
def assumeFieldPermissionUpperBounds(h: Heap, v: Verifier): Unit = {
// TODO: Instead of "manually" assuming such upper bounds, appropriate PropertyInterpreters
// should be used, see StateConsolidator
val relevantChunksPerField = MMap.empty[String, MList[BasicChunk]]
// TODO: Consider caching results, e.g. as mapping from relevant chunks to permission sum
h.values foreach {
case ch: BasicChunk if ch.resourceID == FieldID =>
val relevantChunks = relevantChunksPerField.getOrElseUpdate(ch.id.name, MList.empty)
relevantChunks += ch
case _ => /* Ignore */
}
relevantChunksPerField foreach { case (_, relevantChunks) =>
val permissionSum =
relevantChunks.foldLeft(NoPerm: Term) { case (permSum, chunk) =>
val eq = freeReceiver === chunk.args.head /* For field chunks, the receiver is the only argument */
PermPlus(permSum, Ite(eq, chunk.perm, NoPerm))
}
relevantChunks foreach (chunk => {
val instantiatedPermSum = permissionSum.replace(freeReceiver, chunk.args.head)
v.decider.assume(PermAtMost(instantiatedPermSum, FullPerm))
})
}
}
}