-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathConsumer.scala
More file actions
480 lines (428 loc) · 20.3 KB
/
Copy pathConsumer.scala
File metadata and controls
480 lines (428 loc) · 20.3 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
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
// 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
import viper.silver.ast
import viper.silver.ast.utility.QuantifiedPermissions.QuantifiedPermissionAssertion
import viper.silver.verifier.PartialVerificationError
import viper.silver.verifier.reasons._
import viper.silicon.interfaces.VerificationResult
import viper.silicon.logger.SymbExLogger
import viper.silicon.logger.records.data.{CondExpRecord, ConsumeRecord, ImpliesRecord}
import viper.silicon.state._
import viper.silicon.state.terms._
import viper.silicon.state.terms.predef.`?r`
import viper.silicon.verifier.Verifier
trait ConsumptionRules extends SymbolicExecutionRules {
/** Consume assertion `a` from state `s`.
*
* @param s The state to consume the assertion from.
* @param a The assertion to consume.
* @param pve The error to report in case the consumption fails.
* @param v The verifier to use.
* @param Q The continuation to invoke if the consumption succeeded, with the following
* arguments: state (1st argument) and verifier (3rd argument) resulting from the
* consumption, and a heap snapshot (2bd argument )representing the values of the
* consumed partial heap.
* @return The result of the continuation.
*/
def consume(s: State, a: ast.Exp, pve: PartialVerificationError, v: Verifier)
(Q: (State, Term, Verifier) => VerificationResult)
: VerificationResult
/** Subsequently consumes the assertions `as` (from head to tail), starting in state `s`.
*
* `consumes(s, as, _ => pve, v)` should (not yet tested ...) be equivalent to
* `consume(s, BigAnd(as), pve, v)`, expect that the former allows a more-fine-grained
* error messages.
*
* @param s The state to consume the assertions from.
* @param as The assertions to consume.
* @param pvef The error to report in case a consumption fails. Given assertions `as`, an error
* `pvef(as_i)` will be reported if consuming assertion `as_i` fails.
* @param v @see [[consume]]
* @param Q @see [[consume]]
* @return @see [[consume]]
*/
def consumes(s: State,
as: Seq[ast.Exp],
pvef: ast.Exp => PartialVerificationError,
v: Verifier)
(Q: (State, Term, Verifier) => VerificationResult)
: VerificationResult
}
object consumer extends ConsumptionRules {
import brancher._
import evaluator._
/* See the comment in Producer.scala for an overview of the different produce methods: the
* different consume methods provided by the consumer work and interact analogously.
*/
/** @inheritdoc */
def consume(s: State, a: ast.Exp, pve: PartialVerificationError, v: Verifier)
(Q: (State, Term, Verifier) => VerificationResult)
: VerificationResult = {
consumeR(s, s.h, a.whenExhaling, pve, v)((s1, h1, snap, v1) => {
val s2 = s1.copy(h = h1,
partiallyConsumedHeap = s.partiallyConsumedHeap)
Q(s2, snap, v1)})
}
/** @inheritdoc */
def consumes(s: State,
as: Seq[ast.Exp],
pvef: ast.Exp => PartialVerificationError,
v: Verifier)
(Q: (State, Term, Verifier) => VerificationResult)
: VerificationResult = {
val allTlcs = mutable.ListBuffer[ast.Exp]()
val allPves = mutable.ListBuffer[PartialVerificationError]()
as.foreach(a => {
val tlcs = a.whenExhaling.topLevelConjuncts
val pves = Seq.fill(tlcs.length)(pvef(a))
allTlcs ++= tlcs
allPves ++= pves
})
consumeTlcs(s, s.h, allTlcs.result(), allPves.result(), v)((s1, h1, snap1, v1) => {
val s2 = s1.copy(h = h1,
partiallyConsumedHeap = s.partiallyConsumedHeap)
Q(s2, snap1, v1)
})
}
private def consumeTlcs(s: State,
h: Heap,
tlcs: Seq[ast.Exp],
pves: Seq[PartialVerificationError],
v: Verifier)
(Q: (State, Heap, Term, Verifier) => VerificationResult)
: VerificationResult = {
if (tlcs.isEmpty)
Q(s, h, Unit, v)
else {
val a = tlcs.head
val pve = pves.head
if (tlcs.tail.isEmpty)
wrappedConsumeTlc(s, h, a, pve, v)(Q)
else
wrappedConsumeTlc(s, h, a, pve, v)((s1, h1, snap1, v1) =>
consumeTlcs(s1, h1, tlcs.tail, pves.tail, v1)((s2, h2, snap2, v2) =>
Q(s2, h2, Combine(snap1, snap2), v2)))
}
}
private def consumeR(s: State, h: Heap, a: ast.Exp, pve: PartialVerificationError, v: Verifier)
(Q: (State, Heap, Term, Verifier) => VerificationResult)
: VerificationResult = {
val tlcs = a.topLevelConjuncts
val pves = Seq.fill(tlcs.length)(pve)
consumeTlcs(s, h, tlcs, pves, v)(Q)
}
/** Wrapper/decorator for consume that injects the following operations:
* - Logging, see Executor.scala for an explanation
* - Failure-driven state consolidation
*/
protected def wrappedConsumeTlc(s: State,
h: Heap,
a: ast.Exp,
pve: PartialVerificationError,
v: Verifier)
(Q: (State, Heap, Term, Verifier) => VerificationResult)
: VerificationResult = {
/* tryOrFail effects the "main" heap s.h, so we temporarily set the consume-heap h to be the
* main heap. Note that the main heap is used for evaluating expressions during an ongoing
* consume.
*/
val sInit = s.copy(h = h)
executionFlowController.tryOrFail2[Heap, Term](sInit, v)((s0, v1, QS) => {
val h0 = s0.h /* h0 is h, but potentially consolidated */
val s1 = s0.copy(h = s.h) /* s1 is s, but the retrying flag might be set */
val sepIdentifier = SymbExLogger.currentLog().openScope(new ConsumeRecord(a, s1, v.decider.pcs))
consumeTlc(s1, h0, a, pve, v1)((s2, h2, snap2, v2) => {
SymbExLogger.currentLog().closeScope(sepIdentifier)
QS(s2, h2, snap2, v2)})
})(Q)
}
private def consumeTlc(s: State, h: Heap, a: ast.Exp, pve: PartialVerificationError, v: Verifier)
(Q: (State, Heap, Term, Verifier) => VerificationResult)
: VerificationResult = {
/* ATTENTION: Expressions such as `perm(...)` must be evaluated in-place,
* i.e. in the partially consumed heap which is denoted by `h` here. The
* evaluator evaluates such expressions in the heap
* `context.partiallyConsumedHeap`. Hence, this field must be updated every
* time permissions have been consumed.
*/
v.logger.debug(s"\nCONSUME ${viper.silicon.utils.ast.sourceLineColumn(a)}: $a")
v.logger.debug(v.stateFormatter.format(s, v.decider.pcs))
v.logger.debug("h = " + v.stateFormatter.format(h))
if (s.reserveHeaps.nonEmpty)
v.logger.debug("hR = " + s.reserveHeaps.map(v.stateFormatter.format).mkString("", ",\n ", ""))
val consumed = a match {
case imp @ ast.Implies(e0, a0) if !a.isPure =>
val impliesRecord = new ImpliesRecord(imp, s, v.decider.pcs, "consume")
val uidImplies = SymbExLogger.currentLog().openScope(impliesRecord)
evaluator.eval(s, e0, pve, v)((s1, t0, v1) =>
branch(s1, t0, Some(e0), v1)(
(s2, v2) => consumeR(s2, h, a0, pve, v2)((s3, h1, t1, v3) => {
SymbExLogger.currentLog().closeScope(uidImplies)
Q(s3, h1, t1, v3)
}),
(s2, v2) => {
SymbExLogger.currentLog().closeScope(uidImplies)
Q(s2, h, Unit, v2)
}))
case ite @ ast.CondExp(e0, a1, a2) if !a.isPure =>
val condExpRecord = new CondExpRecord(ite, s, v.decider.pcs, "consume")
val uidCondExp = SymbExLogger.currentLog().openScope(condExpRecord)
eval(s, e0, pve, v)((s1, t0, v1) =>
branch(s1, t0, Some(e0), v1)(
(s2, v2) => consumeR(s2, h, a1, pve, v2)((s3, h1, t1, v3) => {
SymbExLogger.currentLog().closeScope(uidCondExp)
Q(s3, h1, t1, v3)
}),
(s2, v2) => consumeR(s2, h, a2, pve, v2)((s3, h1, t1, v3) => {
SymbExLogger.currentLog().closeScope(uidCondExp)
Q(s3, h1, t1, v3)
})))
/* TODO: Initial handling of QPs is identical/very similar in consumer
* and producer. Try to unify the code.
*/
case QuantifiedPermissionAssertion(forall, cond, acc: ast.FieldAccessPredicate) =>
val field = acc.loc.field
val qid = BasicChunkIdentifier(acc.loc.field.name)
val optTrigger =
if (forall.triggers.isEmpty) None
else Some(forall.triggers)
evalQuantified(s, Forall, forall.variables, Seq(cond), Seq(acc.perm, acc.loc.rcv), optTrigger, qid.name, pve, v) {
case (s1, qvars, Seq(tCond), Seq(tPerm, tRcvr), tTriggers, (auxGlobals, auxNonGlobals), v1) =>
quantifiedChunkSupporter.consume(
s = s1,
h = h,
resource = field,
qvars = qvars,
formalQVars = Seq(`?r`),
qid = qid.name,
optTrigger = optTrigger,
tTriggers = tTriggers,
auxGlobals = auxGlobals,
auxNonGlobals = auxNonGlobals,
tCond = tCond,
tArgs = Seq(tRcvr),
tPerm = tPerm,
pve = pve,
negativePermissionReason = NegativePermission(acc.perm),
notInjectiveReason = QPAssertionNotInjective(acc.loc),
insufficientPermissionReason = InsufficientPermission(acc.loc),
v1)(Q)
}
case QuantifiedPermissionAssertion(forall, cond, acc: ast.PredicateAccessPredicate) =>
val predicate = s.program.findPredicate(acc.loc.predicateName)
/* TODO: Quantified codomain variables are used in axioms and chunks (analogous to `?r`)
* and need to be instantiated in several places. Hence, they need to be known,
* which is more complicated if fresh identifiers are used.
* At least two options:
* 1. Choose fresh identifiers each time; remember/restore, e.g. by storing these variables in chunks
* 2. Choose fresh identifiers once; store in and take from state (or from object Verifier)
*/
val formalVars = s.predicateFormalVarMap(predicate)
val qid = BasicChunkIdentifier(acc.loc.predicateName)
val optTrigger =
if (forall.triggers.isEmpty) None
else Some(forall.triggers)
evalQuantified(s, Forall, forall.variables, Seq(cond), acc.perm +: acc.loc.args, optTrigger, qid.name, pve, v) {
case (s1, qvars, Seq(tCond), Seq(tPerm, tArgs @ _*), tTriggers, (auxGlobals, auxNonGlobals), v1) =>
quantifiedChunkSupporter.consume(
s = s1,
h = h,
resource = predicate,
qvars = qvars,
formalQVars = formalVars,
qid = qid.name,
optTrigger = optTrigger,
tTriggers = tTriggers,
auxGlobals = auxGlobals,
auxNonGlobals = auxNonGlobals,
tCond = tCond,
tArgs = tArgs,
tPerm = tPerm,
pve = pve,
negativePermissionReason = NegativePermission(acc.perm),
notInjectiveReason = QPAssertionNotInjective(acc.loc),
insufficientPermissionReason = InsufficientPermission(acc.loc),
v1)(Q)
}
case QuantifiedPermissionAssertion(forall, cond, wand: ast.MagicWand) =>
val bodyVars = wand.subexpressionsToEvaluate(s.program)
val formalVars = bodyVars.indices.toList.map(i => Var(Identifier(s"x$i"), v.symbolConverter.toSort(bodyVars(i).typ)))
val qid = MagicWandIdentifier(wand, s.program).toString
val optTrigger =
if (forall.triggers.isEmpty) None
else Some(forall.triggers)
val ePerm = ast.FullPerm()()
val tPerm = FullPerm()
evalQuantified(s, Forall, forall.variables, Seq(cond), bodyVars, optTrigger, qid, pve, v) {
case (s1, qvars, Seq(tCond), tArgs, tTriggers, (auxGlobals, auxNonGlobals), v1) =>
quantifiedChunkSupporter.consume(
s = s1,
h = h,
resource = wand,
qvars = qvars,
formalQVars = formalVars,
qid = qid,
optTrigger = optTrigger,
tTriggers = tTriggers,
auxGlobals = auxGlobals,
auxNonGlobals = auxNonGlobals,
tCond = tCond,
tArgs = tArgs,
tPerm = tPerm,
pve = pve,
negativePermissionReason = NegativePermission(ePerm),
notInjectiveReason = sys.error("Quantified wand not injective"), /*ReceiverNotInjective(...)*/
insufficientPermissionReason = MagicWandChunkNotFound(wand), /*InsufficientPermission(...)*/
v1)(Q)
}
case ast.AccessPredicate(loc @ ast.FieldAccess(eRcvr, field), ePerm)
if s.qpFields.contains(field) =>
eval(s, eRcvr, pve, v)((s1, tRcvr, v1) =>
eval(s1, ePerm, pve, v1)((s2, tPerm, v2) => {
val (relevantChunks, _) =
quantifiedChunkSupporter.splitHeap[QuantifiedFieldChunk](s2.h, BasicChunkIdentifier(field.name))
val (smDef1, smCache1) =
quantifiedChunkSupporter.summarisingSnapshotMap(
s2, field, Seq(`?r`), relevantChunks, v2)
v2.decider.assume(FieldTrigger(field.name, smDef1.sm, tRcvr))
// v2.decider.assume(PermAtMost(tPerm, FullPerm()))
val loss = PermTimes(tPerm, s2.permissionScalingFactor)
quantifiedChunkSupporter.consumeSingleLocation(
s2.copy(smCache = smCache1),
h,
Seq(`?r`),
Seq(tRcvr),
loc,
loss,
None,
pve,
v2
)((s3, h3, snap, v3) => {
val s4 = s3.copy(constrainableARPs = s1.constrainableARPs,
partiallyConsumedHeap = Some(h3))
Q(s4, h3, snap, v3)})}))
case ast.AccessPredicate(loc @ ast.PredicateAccess(eArgs, predname), ePerm)
if s.qpPredicates.contains(s.program.findPredicate(predname)) =>
val predicate = s.program.findPredicate(predname)
val formalVars = s.predicateFormalVarMap(predicate)
evals(s, eArgs, _ => pve, v)((s1, tArgs, v1) =>
eval(s1, ePerm, pve, v1)((s2, tPerm, v2) => {
val (relevantChunks, _) =
quantifiedChunkSupporter.splitHeap[QuantifiedPredicateChunk](s.h, BasicChunkIdentifier(predname))
val (smDef1, smCache1) =
quantifiedChunkSupporter.summarisingSnapshotMap(
s2, predicate, s2.predicateFormalVarMap(predicate), relevantChunks, v2)
v2.decider.assume(PredicateTrigger(predicate.name, smDef1.sm, tArgs))
val loss = PermTimes(tPerm, s2.permissionScalingFactor)
quantifiedChunkSupporter.consumeSingleLocation(
s2.copy(smCache = smCache1),
h,
formalVars,
tArgs,
loc,
loss,
None,
pve,
v2
)((s3, h3, snap, v3) => {
val s4 = s3.copy(constrainableARPs = s1.constrainableARPs,
partiallyConsumedHeap = Some(h3))
Q(s4, h3, snap, v3)})}))
case let: ast.Let if !let.isPure =>
letSupporter.handle[ast.Exp](s, let, pve, v)((s1, g1, body, v1) => {
val s2 = s1.copy(g = s1.g + g1)
consumeR(s2, h, body, pve, v1)(Q)})
case ast.AccessPredicate(locacc: ast.LocationAccess, perm) =>
eval(s, perm, pve, v)((s1, tPerm, v1) =>
evalLocationAccess(s1, locacc, pve, v1)((s2, _, tArgs, v2) =>
permissionSupporter.assertNotNegative(s2, tPerm, perm, pve, v2)((s3, v3) => {
val resource = locacc.res(s.program)
val loss = PermTimes(tPerm, s3.permissionScalingFactor)
val ve = pve dueTo InsufficientPermission(locacc)
val description = s"consume ${a.pos}: $a"
chunkSupporter.consume(s3, h, resource, tArgs, loss, ve, v3, description)((s4, h1, snap1, v4) => {
val s5 = s4.copy(partiallyConsumedHeap = Some(h1),
constrainableARPs = s.constrainableARPs)
Q(s5, h1, snap1, v4)})})))
case _: ast.InhaleExhaleExp =>
createFailure(viper.silicon.utils.consistency.createUnexpectedInhaleExhaleExpressionError(a), v, s)
/* Handle wands */
case wand: ast.MagicWand if s.qpMagicWands.contains(MagicWandIdentifier(wand, s.program)) =>
val bodyVars = wand.subexpressionsToEvaluate(s.program)
val formalVars = bodyVars.indices.toList.map(i => Var(Identifier(s"x$i"), v.symbolConverter.toSort(bodyVars(i).typ)))
evals(s, bodyVars, _ => pve, v)((s1, tArgs, v1) => {
val (relevantChunks, _) =
quantifiedChunkSupporter.splitHeap[QuantifiedMagicWandChunk](s1.h, MagicWandIdentifier(wand, s.program))
val (smDef1, smCache1) =
quantifiedChunkSupporter.summarisingSnapshotMap(
s1, wand, formalVars, relevantChunks, v1)
v1.decider.assume(PredicateTrigger(MagicWandIdentifier(wand, s.program).toString, smDef1.sm, tArgs))
val loss = PermTimes(FullPerm(), s1.permissionScalingFactor)
quantifiedChunkSupporter.consumeSingleLocation(
s1.copy(smCache = smCache1),
h,
formalVars,
tArgs,
wand,
loss,
None,
pve,
v1
)((s3, h3, snap, v3) => {
val s4 = s3.copy(constrainableARPs = s1.constrainableARPs,
partiallyConsumedHeap = Some(h3))
Q(s4, h3, snap, v3)})})
case wand: ast.MagicWand =>
magicWandSupporter.evaluateWandArguments(s, wand, pve, v)((s1, tArgs, v1) => {
val ve = pve dueTo MagicWandChunkNotFound(wand)
val description = s"consume wand $wand"
chunkSupporter.consume(s1, h, wand, tArgs, FullPerm(), ve, v1, description)(Q)
})
case _ =>
evalAndAssert(s, a, pve, v)((s1, t, v1) => {
Q(s1, h, t, v1)
})
}
consumed
}
private def evalAndAssert(s: State, e: ast.Exp, pve: PartialVerificationError, v: Verifier)
(Q: (State, Term, Verifier) => VerificationResult)
: VerificationResult = {
/* It is expected that the partially consumed heap (h in the above implementation of
* `consume`) has already been assigned to `c.partiallyConsumedHeap`.
*
* Switch to the eval heap (σUsed) of magic wand's exhale-ext, if necessary.
* This is done here already (the evaluator would do it as well) to ensure that the eval
* heap is consolidated by tryOrFail if the assertion fails.
* The latter is also the reason for wrapping the assertion check in a tryOrFail block:
* the tryOrFail that wraps the consumption of each top-level conjunct would not consolidate
* the right heap.
*/
val s1 = s.copy(h = magicWandSupporter.getEvalHeap(s),
reserveHeaps = Nil,
exhaleExt = false)
executionFlowController.tryOrFail0(s1, v)((s2, v1, QS) => {
eval(s2, e, pve, v1)((s3, t, v2) => {
v2.decider.assert(t) {
case true =>
v2.decider.assume(t)
QS(s3, v2)
case false =>
val failure = createFailure(pve dueTo AssertionFalse(e), v2, s3)
if (s3.retryLevel == 0 && v2.reportFurtherErrors()){
v2.decider.assume(t)
failure combine QS(s3, v2)
} else failure}})
})((s4, v4) => {
val s5 = s4.copy(h = s.h,
reserveHeaps = s.reserveHeaps,
exhaleExt = s.exhaleExt)
Q(s5, Unit, v4)
})
}
}