-
Notifications
You must be signed in to change notification settings - Fork 2
/
viper.sil
177 lines (147 loc) · 4.44 KB
/
viper.sil
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
// Models the logic of one process i
method Proc(x: IArray, y: IArray, done: IArray, i: Int)
// Arrays are all the same length and i is in bounds, and
// 1/2 access to slot i of x, y, and done
requires ppre(i, x, y, done)
// done bit is set for i, and
// 1/2 access to slot i of x, y, and done
ensures ppost(i, x, y, done)
{
// acquire the lock:
// 1/2 access to all slots of x, y, and done
// also gives the inductive invariant of the algorithm
acquire(x, y, done)
// x[i] := 1
slot(x, i).val := 1
// release and then re-acquire the lock:
// exhales and then inhales 1/2 permissions to every slot
// also exhales and inhales the inductive invariant
yield(x, y, done)
// y[i] := x[(i-1) mod N]
slot(y, i).val := slot(x, (i-1) % len(x)).val
// mark the process as done
slot(done, i).val := 1
// release the lock:
// give back the 1/2 permissions and
// exhale the inductive invariant
release(x, y, done)
}
define paccess(i, x, y, done)
(access(x, i, 1/2) &&
access(y, i, 1/2) &&
access(done, i, 1/2))
define ppre(i, x, y, done)
(0 <= i && i < len(x) && len(y) == len(x) && len(done) == len(x) &&
paccess(i, x, y, done))
define ppost(i, x, y, done)
(paccess(i, x, y, done) && is_done(done, i))
define control_inv(x, y, done)
(access_all(x, 1/2) &&
access_all(y, 1/2) &&
access_all(done, 1/2) &&
ind_inv(x, y, done))
define acquire(x, y, done) {
inhale control_inv(x, y, done)
}
define release(x, y, done) {
exhale control_inv(x, y, done)
}
method yield(x: IArray, y: IArray, done: IArray)
requires len(y) == len(x) && len(done) == len(x)
requires control_inv(x, y, done)
ensures control_inv(x, y, done)
define is_done(done, i) slot(done, i).val == 1
define all_done(done)
forall j: Int :: 0 <= j && j < len(done) ==> is_done(done, j)
define safety(y, done)
all_done(done) ==>
exists j: Int :: 0 <= j && j < len(y) && slot(y, j).val == 1
define ind_inv(x, y, done)
safety(y, done) &&
(forall j: Int :: 0 <= j && j < len(done) && is_done(done, j) ==> slot(x, j).val == 1)
// The start of a main method that forks N processes and
// then joins them. The goal is to assert that some y[i] == 1
// after joining all processes.
method Main()
{
var x: IArray
var y: IArray
var done: IArray
inhale access_all(x, write) && access_all(y, write) && access_all(done, write)
inhale len(x) == len(y) && len(x) == len(done)
inhale forall j: Int :: 0 <= j && j < len(done) ==> !is_done(done, j)
inhale len(done) > 0
assert !is_done(done, 0)
acquire(x, y, done)
fork_all(x, y, done)
join_all(x, y, done)
assert exists j: Int :: 0 <= j && j < len(y) && slot(y, j).val == 1
release(x, y, done)
}
method fork_all(x: IArray, y: IArray, done: IArray)
requires len(x) == len(y) && len(x) == len(done)
requires access_all(x, 1/2)
requires access_all(y, 1/2)
requires access_all(done, 1/2)
requires control_inv(x, y, done)
ensures control_inv(x, y, done)
{
var i: Int := 0
while (i < len(x))
invariant 0 <= i
invariant access_range(x, i, len(x), 1/2)
invariant access_range(y, i, len(y), 1/2)
invariant access_range(done, i, len(done), 1/2)
invariant control_inv(x, y, done)
{
fork(i, x, y, done)
i := i + 1
}
}
method join_all(x: IArray, y: IArray, done: IArray)
requires len(x) == len(y) && len(x) == len(done)
requires control_inv(x, y, done)
ensures control_inv(x, y, done)
ensures all_done(done)
{
var i: Int := 0
while (i < len(x))
invariant control_inv(x, y, done)
invariant i <= len(done)
invariant forall j: Int :: 0 <= j && j < i ==> is_done(done, j)
invariant access_range(done, 0, i, 1/2)
{
join(i, x, y, done)
i := i + 1
}
}
define fork(i, x, y, done) {
exhale ppre(i, x, y, done)
}
define join(i, x, y, done) {
yield(x, y, done)
inhale ppost(i, x, y, done)
}
/* Encoding of arrays */
field val: Int
// Acccess to one slot
define access(a, i, f) acc(slot(a,i).val, f)
// Access to a range of slots
define access_range(a, start, end, f)
(forall j: Int :: start <= j && j < end ==> acc(slot(a,j).val, f))
// Access to all slots
define access_all(a, f) access_range(a, 0, len(a), f)
domain IArray {
function slot(a: IArray, i: Int): Ref
function len(a: IArray): Int
function first(r: Ref): IArray
function second(r: Ref): Int
axiom all_diff {
forall a: IArray, i: Int :: { slot(a,i) }
first(slot(a,i)) == a && second(slot(a,i)) == i
}
axiom len_nonneg {
forall a: IArray :: { len(a) }
len(a) >= 0
}
}