-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathProducer.scala
More file actions
445 lines (403 loc) · 19.5 KB
/
Copy pathProducer.scala
File metadata and controls
445 lines (403 loc) · 19.5 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
// 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.silicon.interfaces.{Unreachable, VerificationResult}
import viper.silicon.logger.records.data.{CondExpRecord, ImpliesRecord, ProduceRecord}
import viper.silicon.resources.{FieldID, PredicateID}
import viper.silicon.state.terms.predef.`?r`
import viper.silicon.state.terms._
import viper.silicon.state._
import viper.silicon.supporters.functions.NoopFunctionRecorder
import viper.silicon.verifier.Verifier
import viper.silver.verifier.reasons.{NegativePermission, QPAssertionNotInjective}
trait ProductionRules extends SymbolicExecutionRules {
/** Produce assertion `a` into state `s`.
*
* @param s The state to produce the assertion into.
* @param sf The heap snapshot determining the values of the produced partial heap.
* @param a The assertion to produce.
* @param pve The error to report in case the production fails.
* @param v The verifier to use.
* @param Q The continuation to invoke if the production succeeded, with the state and
* the verifier resulting from the production as arguments.
* @return The result of the continuation.
*/
def produce(s: State,
sf: (Sort, Verifier) => Term,
a: ast.Exp,
pve: PartialVerificationError,
v: Verifier)
(Q: (State, Verifier) => VerificationResult)
: VerificationResult
/** Subsequently produces assertions `as` into state `s`.
*
* `produces(s, sf, as, _ => pve, v)` should (not yet tested ...) be equivalent to
* `produce(s, sf, BigAnd(as), pve, v)`, expect that the former allows a more-fine-grained
* error messages.
*
* @param s The state to produce the assertions into.
* @param sf The heap snapshots determining the values of the produced partial heaps.
* @param as The assertions to produce.
* @param pvef The error to report in case the production fails. Given assertions `as`, an error
* `pvef(as_i)` will be reported if producing assertion `as_i` fails.
* @param v @see [[produce]]
* @param Q @see [[produce]]
* @return @see [[produce]]
*/
def produces(s: State,
sf: (Sort, Verifier) => Term,
as: Seq[ast.Exp],
pvef: ast.Exp => PartialVerificationError,
v: Verifier)
(Q: (State, Verifier) => VerificationResult)
: VerificationResult
}
object producer extends ProductionRules {
import brancher._
import evaluator._
/* Overview of and interaction between the different available produce-methods:
* - `produce` and `produces` are the entry methods and intended to be called by *clients*
* (e.g. from the executor), but *not* by the implementation of the producer itself
* (e.g. recursively).
* - Produce methods suffixed with `tlc` (or `tlcs`) expect top-level conjuncts as assertions.
* The other produce methods therefore split the given assertion(s) into top-level
* conjuncts and forward these to `produceTlcs`.
* - `produceTlc` implements the actual symbolic execution rules for producing an assertion,
* and `produceTlcs` is basically `produceTlc` lifted to a sequence of assertions
* (a continuation-aware fold, if you will).
* - Certain operations such as logging need to be performed per produced top-level conjunct.
* This is implemented by `wrappedProduceTlc`: a wrapper around (or decorator for)
* `produceTlc` that performs additional operations before/after invoking `produceTlc`.
* `produceTlcs` therefore repeatedly invokes `wrappedProduceTlc` (and not `produceTlc`
* directly)
* - `produceR` is intended for recursive calls: it takes an assertion, splits it into
* top-level conjuncts and uses `produceTlcs` to produce each of them (hence, each assertion
* to produce passes `wrappedProduceTlc` before finally reaching `produceTlc`).
* - Note that the splitting into top-level conjuncts performed by `produceR` is not redundant,
* although the entry methods such as `produce` split assertions as well: if a client needs
* to produce `a1 && (b ==> a2 && a3) && a4`, then the entry method will split the assertion
* into the sequence `[a1, b ==> a2 && a3, a4]`, and the recursively produced assertion
* `a2 && a3` (after having branched over `b`) needs to be split again.
*/
/** @inheritdoc */
def produce(s: State,
sf: (Sort, Verifier) => Term,
a: ast.Exp,
pve: PartialVerificationError,
v: Verifier)
(Q: (State, Verifier) => VerificationResult)
: VerificationResult =
produceR(s, sf, a.whenInhaling, pve, v)(Q)
/** @inheritdoc */
def produces(s: State,
sf: (Sort, Verifier) => Term,
as: Seq[ast.Exp],
pvef: ast.Exp => PartialVerificationError,
v: Verifier)
(Q: (State, Verifier) => VerificationResult)
: VerificationResult = {
val allTlcs = mutable.ListBuffer[ast.Exp]()
val allPves = mutable.ListBuffer[PartialVerificationError]()
as.foreach(a => {
val tlcs = a.whenInhaling.topLevelConjuncts
val pves = Seq.fill(tlcs.length)(pvef(a))
allTlcs ++= tlcs
allPves ++= pves
})
produceTlcs(s, sf, allTlcs.result(), allPves.result(), v)(Q)
}
private def produceTlcs(s: State,
sf: (Sort, Verifier) => Term,
as: Seq[ast.Exp],
pves: Seq[PartialVerificationError],
v: Verifier)
(Q: (State, Verifier) => VerificationResult)
: VerificationResult = {
if (as.isEmpty)
Q(s, v)
else {
val a = as.head.whenInhaling
val pve = pves.head
if (as.tail.isEmpty)
wrappedProduceTlc(s, sf, a, pve, v)(Q)
else {
try {
val (sf0, sf1) =
v.snapshotSupporter.createSnapshotPair(s, sf, a, viper.silicon.utils.ast.BigAnd(as.tail), v)
/* TODO: Refactor createSnapshotPair s.t. it can be used with Seq[Exp],
* then remove use of BigAnd; for one it is not efficient since
* the tail of the (decreasing list parameter as) is BigAnd-ed
* over and over again.
*/
wrappedProduceTlc(s, sf0, a, pve, v)((s1, v1) =>
produceTlcs(s1, sf1, as.tail, pves.tail, v1)(Q))
} catch {
// We will get an IllegalArgumentException from createSnapshotPair if sf(...) returns Unit.
// This should never happen if we're in a reachable state, so here we check for that
// (without timeout, since there is no fallback) and stop verifying the current branch.
case _: IllegalArgumentException if v.decider.check(False, Verifier.config.assertTimeout.getOrElse(0)) =>
Unreachable()
}
}
}
}
private def produceR(s: State,
sf: (Sort, Verifier) => Term,
a: ast.Exp,
pve: PartialVerificationError,
v: Verifier)
(Q: (State, Verifier) => VerificationResult)
: VerificationResult = {
val tlcs = a.topLevelConjuncts
val pves = Seq.fill(tlcs.length)(pve)
produceTlcs(s, sf, tlcs, pves, v)(Q)
}
/** Wrapper/decorator for consume that injects the following operations:
* - Logging, see Executor.scala for an explanation
*/
private def wrappedProduceTlc(s: State,
sf: (Sort, Verifier) => Term,
a: ast.Exp,
pve: PartialVerificationError,
v: Verifier)
(Q: (State, Verifier) => VerificationResult)
: VerificationResult = {
val sepIdentifier = v.symbExLog.openScope(new ProduceRecord(a, s, v.decider.pcs))
produceTlc(s, sf, a, pve, v)((s1, v1) => {
v1.symbExLog.closeScope(sepIdentifier)
Q(s1, v1)})
}
private def produceTlc(s: State,
sf: (Sort, Verifier) => Term,
a: ast.Exp,
pve: PartialVerificationError,
v: Verifier)
(continuation: (State, Verifier) => VerificationResult)
: VerificationResult = {
v.logger.debug(s"\nPRODUCE ${viper.silicon.utils.ast.sourceLineColumn(a)}: $a")
v.logger.debug(v.stateFormatter.format(s, v.decider.pcs))
val Q: (State, Verifier) => VerificationResult = (state, verifier) =>
continuation(if (state.exhaleExt) state.copy(reserveHeaps = state.h +: state.reserveHeaps.drop(1)) else state, verifier)
val produced = a match {
case imp @ ast.Implies(e0, a0) if !a.isPure =>
val impliesRecord = new ImpliesRecord(imp, s, v.decider.pcs, "produce")
val uidImplies = v.symbExLog.openScope(impliesRecord)
eval(s, e0, pve, v)((s1, t0, v1) =>
branch(s1, t0, Some(e0), v1)(
(s2, v2) => produceR(s2, sf, a0, pve, v2)((s3, v3) => {
v3.symbExLog.closeScope(uidImplies)
Q(s3, v3)
}),
(s2, v2) => {
v2.decider.assume(sf(sorts.Snap, v2) === Unit)
/* TODO: Avoid creating a fresh var (by invoking) `sf` that is not used
* otherwise. In order words, only make this assumption if `sf` has
* already been used, e.g. in a snapshot equality such as `s0 == (s1, s2)`.
*/
v2.symbExLog.closeScope(uidImplies)
Q(s2, v2)
}))
case ite @ ast.CondExp(e0, a1, a2) if !a.isPure =>
val condExpRecord = new CondExpRecord(ite, s, v.decider.pcs, "produce")
val uidCondExp = v.symbExLog.openScope(condExpRecord)
eval(s, e0, pve, v)((s1, t0, v1) =>
branch(s1, t0, Some(e0), v1)(
(s2, v2) => produceR(s2, sf, a1, pve, v2)((s3, v3) => {
v3.symbExLog.closeScope(uidCondExp)
Q(s3, v3)
}),
(s2, v2) => produceR(s2, sf, a2, pve, v2)((s3, v3) => {
v3.symbExLog.closeScope(uidCondExp)
Q(s3, v3)
})))
case let: ast.Let if !let.isPure =>
letSupporter.handle[ast.Exp](s, let, pve, v)((s1, g1, body, v1) =>
produceR(s1.copy(g = s1.g + g1), sf, body, pve, v1)(Q))
case ast.FieldAccessPredicate(ast.FieldAccess(eRcvr, field), perm) =>
eval(s, eRcvr, pve, v)((s1, tRcvr, v1) =>
eval(s1, perm, pve, v1)((s2, tPerm, v2) =>
permissionSupporter.assertNotNegative(s2, tPerm, perm, pve, v2)((s3, v3) => {
val snap = sf(v3.symbolConverter.toSort(field.typ), v3)
val gain = PermTimes(tPerm, s3.permissionScalingFactor)
if (s3.qpFields.contains(field)) {
val trigger = (sm: Term) => FieldTrigger(field.name, sm, tRcvr)
quantifiedChunkSupporter.produceSingleLocation(s3, field, Seq(`?r`), Seq(tRcvr), snap, gain, trigger, v3)(Q)
} else {
val ch = BasicChunk(FieldID, BasicChunkIdentifier(field.name), Seq(tRcvr), snap, gain)
chunkSupporter.produce(s3, s3.h, ch, v3)((s4, h4, v4) =>
Q(s4.copy(h = h4), v4))
}})))
case ast.PredicateAccessPredicate(ast.PredicateAccess(eArgs, predicateName), perm) =>
val predicate = s.program.findPredicate(predicateName)
evals(s, eArgs, _ => pve, v)((s1, tArgs, v1) =>
eval(s1, perm, pve, v1)((s1a, tPerm, v1a) =>
permissionSupporter.assertNotNegative(s1a, tPerm, perm, pve, v1a)((s2, v2) => {
val snap = sf(
predicate.body.map(v2.snapshotSupporter.optimalSnapshotSort(_, s.program)._1)
.getOrElse(sorts.Snap), v2)
val gain = PermTimes(tPerm, s2.permissionScalingFactor)
if (s2.qpPredicates.contains(predicate)) {
val formalArgs = s2.predicateFormalVarMap(predicate)
val trigger = (sm: Term) => PredicateTrigger(predicate.name, sm, tArgs)
quantifiedChunkSupporter.produceSingleLocation(
s2, predicate, formalArgs, tArgs, snap, gain, trigger, v2)(Q)
} else {
val snap1 = snap.convert(sorts.Snap)
val ch = BasicChunk(PredicateID, BasicChunkIdentifier(predicate.name), tArgs, snap1, gain)
chunkSupporter.produce(s2, s2.h, ch, v2)((s3, h3, v3) => {
if (Verifier.config.enablePredicateTriggersOnInhale() && s3.functionRecorder == NoopFunctionRecorder
&& !Verifier.config.disableFunctionUnfoldTrigger()) {
v3.decider.assume(App(s3.predicateData(predicate).triggerFunction, snap1 +: tArgs))
}
Q(s3.copy(h = h3), v3)})
}})))
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, args,v1) => {
val (sm, smValueDef) =
quantifiedChunkSupporter.singletonSnapshotMap(s1, wand, args, sf(sorts.Snap, v1), v1)
v1.decider.prover.comment("Definitional axioms for singleton-SM's value")
val definitionalAxiomMark = v1.decider.setPathConditionMark()
v1.decider.assumeDefinition(smValueDef)
val conservedPcs =
if (s1.recordPcs) (s1.conservedPcs.head :+ v1.decider.pcs.after(definitionalAxiomMark)) +: s1.conservedPcs.tail
else s1.conservedPcs
val ch =
quantifiedChunkSupporter.createSingletonQuantifiedChunk(formalVars, wand, args, FullPerm, sm, s.program)
val h2 = s1.h + ch
val smCache1 = if(s1.heapDependentTriggers.contains(MagicWandIdentifier(wand, s1.program))){
val (relevantChunks, _) =
quantifiedChunkSupporter.splitHeap[QuantifiedMagicWandChunk](h2, ch.id)
val (smDef1, smCache1) =
quantifiedChunkSupporter.summarisingSnapshotMap(
s1, wand, formalVars, relevantChunks, v1)
v1.decider.assume(PredicateTrigger(ch.id.toString, smDef1.sm, args))
smCache1
} else {
s1.smCache
}
val smDef = SnapshotMapDefinition(wand, sm, Seq(smValueDef), Seq())
val s2 =
s1.copy(h = h2,
functionRecorder = s1.functionRecorder.recordFvfAndDomain(smDef),
smCache = smCache1,
conservedPcs = conservedPcs)
Q(s2, v1)})
case wand: ast.MagicWand =>
val snap = sf(sorts.Snap, v)
magicWandSupporter.createChunk(s, wand, MagicWandSnapshot(snap), pve, v)((s1, chWand, v1) =>
chunkSupporter.produce(s1, s1.h, chWand, v1)((s2, h2, v2) =>
Q(s2.copy(h = h2), v2)))
/* 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 qid = acc.loc.field.name
val optTrigger =
if (forall.triggers.isEmpty) None
else Some(forall.triggers)
evalQuantified(s, Forall, forall.variables, Seq(cond), Seq(acc.loc.rcv, acc.perm), optTrigger, qid, pve, v) {
case (s1, qvars, Seq(tCond), Seq(tRcvr, tPerm), tTriggers, (auxGlobals, auxNonGlobals), v1) =>
val tSnap = sf(sorts.FieldValueFunction(v1.symbolConverter.toSort(acc.loc.field.typ), acc.loc.field.name), v1)
// v.decider.assume(PermAtMost(tPerm, FullPerm()))
quantifiedChunkSupporter.produce(
s1,
forall,
acc.loc.field,
qvars, Seq(`?r`),
qid, optTrigger,
tTriggers,
auxGlobals,
auxNonGlobals,
tCond,
Seq(tRcvr),
tSnap,
tPerm,
pve,
NegativePermission(acc.perm),
QPAssertionNotInjective(acc.loc),
v1
)(Q)
}
case QuantifiedPermissionAssertion(forall, cond, acc: ast.PredicateAccessPredicate) =>
val predicate = s.program.findPredicate(acc.loc.predicateName)
val formalVars = s.predicateFormalVarMap(predicate)
val qid = 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, pve, v) {
case (s1, qvars, Seq(tCond), Seq(tPerm, tArgs @ _*), tTriggers, (auxGlobals, auxNonGlobals), v1) =>
val tSnap = sf(sorts.PredicateSnapFunction(s1.predicateSnapMap(predicate), predicate.name), v1)
quantifiedChunkSupporter.produce(
s1,
forall,
predicate,
qvars,
formalVars,
qid,
optTrigger,
tTriggers,
auxGlobals,
auxNonGlobals,
tCond,
tArgs,
tSnap,
tPerm,
pve,
NegativePermission(acc.perm),
QPAssertionNotInjective(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 optTrigger =
if (forall.triggers.isEmpty) None
else Some(forall.triggers)
val qid = MagicWandIdentifier(wand, s.program).toString
evalQuantified(s, Forall, forall.variables, Seq(cond), bodyVars, optTrigger, qid, pve, v) {
case (s1, qvars, Seq(tCond), tArgs, tTriggers, (auxGlobals, auxNonGlobals), v1) =>
val tSnap = sf(sorts.PredicateSnapFunction(sorts.Snap, qid), v1)
quantifiedChunkSupporter.produce(
s1,
forall,
wand,
qvars,
formalVars,
qid,
optTrigger,
tTriggers,
auxGlobals,
auxNonGlobals,
tCond,
tArgs,
tSnap,
FullPerm,
pve,
NegativePermission(ast.FullPerm()()),
QPAssertionNotInjective(wand),
v1
)(Q)
}
case _: ast.InhaleExhaleExp =>
createFailure(viper.silicon.utils.consistency.createUnexpectedInhaleExhaleExpressionError(a), v, s)
/* Any regular expressions, i.e. boolean and arithmetic. */
case _ =>
v.decider.assume(sf(sorts.Snap, v) === Unit) /* TODO: See comment for case ast.Implies above */
eval(s, a, pve, v)((s1, t, v1) => {
v1.decider.assume(t)
Q(s1, v1)})
}
produced
}
}