forked from viperproject/silicon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunkSupporter.scala
308 lines (278 loc) · 12.4 KB
/
ChunkSupporter.scala
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
// 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 viper.silicon.debugger.DebugExp
import viper.silicon.interfaces.state._
import viper.silicon.interfaces.{Success, VerificationResult}
import viper.silicon.resources.{NonQuantifiedPropertyInterpreter, Resources}
import viper.silicon.state._
import viper.silicon.state.terms._
import viper.silicon.state.terms.perms.IsPositive
import viper.silicon.utils.ast.buildMinExp
import viper.silicon.verifier.Verifier
import viper.silver.ast
import viper.silver.parser.PUnknown
import viper.silver.verifier.VerificationError
import scala.reflect.ClassTag
trait ChunkSupportRules extends SymbolicExecutionRules {
def consume(s: State,
h: Heap,
resource: ast.Resource,
args: Seq[Term],
argsExp: Option[Seq[ast.Exp]],
perms: Term,
permsExp: Option[ast.Exp],
returnSnap: Boolean,
ve: VerificationError,
v: Verifier,
description: String)
(Q: (State, Heap, Option[Term], Verifier) => VerificationResult)
: VerificationResult
def produce(s: State, h: Heap, ch: NonQuantifiedChunk, v: Verifier)
(Q: (State, Heap, Verifier) => VerificationResult)
: VerificationResult
def lookup(s: State,
h: Heap,
resource: ast.Resource,
args: Seq[Term],
argsExp: Option[Seq[ast.Exp]],
ve: VerificationError,
v: Verifier)
(Q: (State, Heap, Term, Verifier) => VerificationResult)
: VerificationResult
def findChunk[CH <: NonQuantifiedChunk: ClassTag]
(chunks: Iterable[Chunk],
id: ChunkIdentifer,
args: Iterable[Term],
v: Verifier)
: Option[CH]
def findChunksWithID[CH <: NonQuantifiedChunk: ClassTag]
(chunks: Iterable[Chunk],
id: ChunkIdentifer)
: Iterable[CH]
}
object chunkSupporter extends ChunkSupportRules {
def consume(s: State,
h: Heap,
resource: ast.Resource,
args: Seq[Term],
argsExp: Option[Seq[ast.Exp]],
perms: Term,
permsExp: Option[ast.Exp],
returnSnap: Boolean,
ve: VerificationError,
v: Verifier,
description: String)
(Q: (State, Heap, Option[Term], Verifier) => VerificationResult)
: VerificationResult = {
consume2(s, h, resource, args, argsExp, perms, permsExp, returnSnap, ve, v)((s2, h2, optSnap, v2) =>
optSnap match {
case Some(snap) =>
Q(s2, h2, Some(snap.convert(sorts.Snap)), v2)
case None if returnSnap =>
/* Not having consumed anything could mean that we are in an infeasible
* branch, or that the permission amount to consume was zero.
*
* [MS 2022-01-28] Previously, a a fresh snapshot was retured, which also had to be
* registered with the function recorder. However, since nothing was consumed,
* returning the unit snapshot seems more appropriate.
*/
val fresh = v2.decider.fresh(sorts.Snap, Option.when(withExp)(PUnknown()))
val s3 = s2.copy(functionRecorder = s2.functionRecorder.recordFreshSnapshot(fresh.applicable))
Q(s3, h2, Some(fresh), v2)
case None => Q(s2, h2, None, v2)
})
}
private def consume2(s: State,
h: Heap,
resource: ast.Resource,
args: Seq[Term],
argsExp: Option[Seq[ast.Exp]],
perms: Term,
permsExp: Option[ast.Exp],
returnSnap: Boolean,
ve: VerificationError,
v: Verifier)
(Q: (State, Heap, Option[Term], Verifier) => VerificationResult)
: VerificationResult = {
val id = ChunkIdentifier(resource, s.program)
if (s.exhaleExt) {
val failure = createFailure(ve, v, s, "chunk consume in package")
magicWandSupporter.transfer(s, perms, permsExp, failure, Seq(), v)(consumeGreedy(_, _, id, args, _, _, _))((s1, optCh, v1) =>
if (returnSnap){
Q(s1, h, optCh.flatMap(ch => Some(ch.snap)), v1)
} else {
Q(s1, h, None, v1)
})
} else {
executionFlowController.tryOrFail2[Heap, Option[Term]](s.copy(h = h), v)((s1, v1, QS) =>
if (s1.moreCompleteExhale) {
moreCompleteExhaleSupporter.consumeComplete(s1, s1.h, resource, args, argsExp, perms, permsExp, returnSnap, ve, v1)((s2, h2, snap2, v2) => {
QS(s2.copy(h = s.h), h2, snap2, v2)
})
} else {
consumeGreedy(s1, s1.h, id, args, perms, permsExp, v1) match {
case (Complete(), s2, h2, optCh2) =>
val snap = optCh2 match {
case Some(ch) if returnSnap =>
if (v1.decider.check(IsPositive(perms), Verifier.config.checkTimeout())) {
Some(ch.snap)
} else {
Some(Ite(IsPositive(perms), ch.snap.convert(sorts.Snap), Unit))
}
case _ => None
}
QS(s2.copy(h = s.h), h2, snap, v1)
case _ if v1.decider.checkSmoke(true) =>
Success() // TODO: Mark branch as dead?
case _ =>
createFailure(ve, v1, s1, "consuming chunk", true)
}
}
)(Q)
}
}
private def consumeGreedy(s: State,
h: Heap,
id: ChunkIdentifer,
args: Seq[Term],
perms: Term,
permsExp: Option[ast.Exp],
v: Verifier)
: (ConsumptionResult, State, Heap, Option[NonQuantifiedChunk]) = {
val consumeExact = terms.utils.consumeExactRead(perms, s.constrainableARPs)
def assumeProperties(chunk: NonQuantifiedChunk, heap: Heap): Unit = {
val interpreter = new NonQuantifiedPropertyInterpreter(heap.values, v)
val resource = Resources.resourceDescriptions(chunk.resourceID)
val pathCond = interpreter.buildPathConditionsForChunk(chunk, resource.instanceProperties)
pathCond.foreach(p => v.decider.assume(p._1, Option.when(withExp)(DebugExp.createInstance(p._2, p._2))))
}
findChunk[NonQuantifiedChunk](h.values, id, args, v) match {
case Some(ch) =>
if (consumeExact) {
val toTake = PermMin(ch.perm, perms)
val toTakeExp = permsExp.map(pe => buildMinExp(Seq(ch.permExp.get, pe), ast.Perm))
val newPermExp = permsExp.map(pe => ast.PermSub(ch.permExp.get, toTakeExp.get)(pe.pos, pe.info, pe.errT))
val newChunk = ch.withPerm(PermMinus(ch.perm, toTake), newPermExp)
val takenChunk = Some(ch.withPerm(toTake, toTakeExp))
var newHeap = h - ch
if (!v.decider.check(newChunk.perm === NoPerm, Verifier.config.checkTimeout())) {
newHeap = newHeap + newChunk
assumeProperties(newChunk, newHeap)
}
val remainingExp = permsExp.map(pe => ast.PermSub(pe, toTakeExp.get)(pe.pos, pe.info, pe.errT))
(ConsumptionResult(PermMinus(perms, toTake), remainingExp, Seq(), v, 0), s, newHeap, takenChunk)
} else {
if (v.decider.check(ch.perm !== NoPerm, Verifier.config.checkTimeout())) {
val constraintExp = permsExp.map(pe => ast.PermLtCmp(pe, ch.permExp.get)(pe.pos, pe.info, pe.errT))
v.decider.assume(PermLess(perms, ch.perm), Option.when(withExp)(DebugExp.createInstance(constraintExp, constraintExp)))
val newPermExp = permsExp.map(pe => ast.PermSub(ch.permExp.get, pe)(pe.pos, pe.info, pe.errT))
val newChunk = ch.withPerm(PermMinus(ch.perm, perms), newPermExp)
val takenChunk = ch.withPerm(perms, permsExp)
val newHeap = h - ch + newChunk
assumeProperties(newChunk, newHeap)
(Complete(), s, newHeap, Some(takenChunk))
} else {
(Incomplete(perms, permsExp), s, h, None)
}
}
case None =>
if (consumeExact && s.retrying && v.decider.check(perms === NoPerm, Verifier.config.checkTimeout())) {
(Complete(), s, h, None)
} else {
(Incomplete(perms, permsExp), s, h, None)
}
}
}
def produce(s: State, h: Heap, ch: NonQuantifiedChunk, v: Verifier)
(Q: (State, Heap, Verifier) => VerificationResult)
: VerificationResult = {
// Try to merge the chunk into the heap by finding an alias.
// In any case, property assumptions are added after the merge step.
val (fr1, h1) = v.stateConsolidator(s).merge(s.functionRecorder, s, h, ch, v)
Q(s.copy(functionRecorder = fr1), h1, v)
}
def lookup(s: State,
h: Heap,
resource: ast.Resource,
args: Seq[Term],
argsExp: Option[Seq[ast.Exp]],
ve: VerificationError,
v: Verifier)
(Q: (State, Heap, Term, Verifier) => VerificationResult)
: VerificationResult = {
executionFlowController.tryOrFail2[Heap, Term](s.copy(h = h), v)((s1, v1, QS) => {
val lookupFunction =
if (s1.moreCompleteExhale) moreCompleteExhaleSupporter.lookupComplete _
else lookupGreedy _
lookupFunction(s1, s1.h, resource, args, argsExp, ve, v1)((s2, tSnap, v2) =>
QS(s2.copy(h = s.h), s2.h, tSnap, v2))
})(Q)
}
private def lookupGreedy(s: State,
h: Heap,
resource: ast.Resource,
args: Seq[Term],
argsExp: Option[Seq[ast.Exp]],
ve: VerificationError,
v: Verifier)
(Q: (State, Term, Verifier) => VerificationResult)
: VerificationResult = {
val id = ChunkIdentifier(resource, s.program)
val findRes = findChunk[NonQuantifiedChunk](h.values, id, args, v)
findRes match {
case Some(ch) if v.decider.check(IsPositive(ch.perm), Verifier.config.checkTimeout()) =>
Q(s, ch.snap, v)
case _ if v.decider.checkSmoke(true) =>
Success() // TODO: Mark branch as dead?
case _ =>
createFailure(ve, v, s, "looking up chunk", true)
}
}
def findChunk[CH <: NonQuantifiedChunk: ClassTag]
(chunks: Iterable[Chunk],
id: ChunkIdentifer,
args: Iterable[Term],
v: Verifier)
: Option[CH] = {
val relevantChunks = findChunksWithID[CH](chunks, id)
findChunkLiterally(relevantChunks, args) orElse findChunkWithProver(relevantChunks, args, v)
}
def findChunksWithID[CH <: NonQuantifiedChunk: ClassTag](chunks: Iterable[Chunk], id: ChunkIdentifer): Iterable[CH] = {
chunks.flatMap {
case c: CH if id == c.id => Some(c)
case _ => None
}
}
/** Extract the chunks with resource matching id.
* Return two sequences of chunks -- one with resource id, and the
* other with the remaining resources.
*/
def splitHeap[CH <: NonQuantifiedChunk : ClassTag](h: Heap, id: ChunkIdentifer)
: (Seq[CH], Seq[Chunk]) = {
var relevantChunks = Seq[CH]()
var otherChunks = Seq[Chunk]()
h.values foreach {
case ch: CH if ch.id == id =>
relevantChunks +:= ch
case ch: QuantifiedChunk if ch.id == id =>
sys.error(
s"I did not expect quantified chunks on the heap for resource $id, "
+ s"but found $ch")
case ch =>
otherChunks +:= ch
}
(relevantChunks, otherChunks)
}
private def findChunkLiterally[CH <: NonQuantifiedChunk](chunks: Iterable[CH], args: Iterable[Term]) = {
chunks find (ch => ch.args == args)
}
private def findChunkWithProver[CH <: NonQuantifiedChunk](chunks: Iterable[CH], args: Iterable[Term], v: Verifier) = {
chunks find (ch =>
args.size == ch.args.size &&
v.decider.check(And(ch.args zip args map (x => x._1 === x._2)), Verifier.config.checkTimeout()))
}
}