-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_diskspec.py
328 lines (232 loc) · 8.18 KB
/
test_diskspec.py
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
from z3 import *
import disk
import unittest
from yggdrasil.util import *
from yggdrasil.diskspec import *
from yggdrasil import test
class AsyncDiskTest(test.DiskTest):
def setUp(self):
self._array = FreshDiskArray('array')
def test_prefix(self):
mach = Machine()
d = AsyncDisk(mach, self._array)
a = FreshBlock('block')
b = FreshBlock('block')
c = FreshBlock('block')
d.write(5, a)
d.write(6, b)
d.write(7, c)
d = d.crash(Machine())
self.show(Implies(mach.assumption,
And(d.read(5) == a, d.read(6) != b), d.read(7) != c))
def test_reorder(self):
mach = Machine()
d = AsyncDisk(mach, self._array)
a = FreshBlock('block')
b = FreshBlock('block')
c = FreshBlock('block')
d.write(5, a)
d.write(6, b)
d.write(7, c)
d = d.crash(Machine())
self.show(Implies(mach.assumption,
And(d.read(6) == a, d.read(5) != b)))
def test_nocrash(self):
mach = Machine()
d = AsyncDisk(mach, self._array)
a = FreshBlock('block')
b = FreshBlock('block')
c = FreshBlock('block')
d.write(5, a)
d.write(6, b)
d.write(7, c)
d = d.crash(Machine())
self.show(Implies(mach.assumption,
And(d.read(5) == a, d.read(6) == b, d.read(7) == c)))
def test_write_two(self):
mach = Machine()
d = AsyncDisk(mach, self._array)
i0 = FreshSize('i')
i1 = FreshSize('i')
x0 = FreshBlock('x')
x1 = FreshBlock('x')
assumption = i0 != i1
oldv0 = d.read(i0)
oldv1 = d.read(i1)
d.write(i0, x0)
d.write(i1, x1)
newv0 = d.read(i0)
newv1 = d.read(i1)
self.prove(Implies(assumption, newv0 == x0))
self.prove(Implies(assumption, newv1 == x1))
d = d.crash(Machine())
anyv0 = d.read(i0)
anyv1 = d.read(i1)
self.prove(Implies(assumption, And(
Or(anyv0 == oldv0, anyv0 == x0),
Or(anyv1 == oldv1, anyv1 == x1))))
def test_flush(self):
mach = Machine()
d = AsyncDisk(mach, self._array)
i0 = FreshSize('i')
i1 = FreshSize('i')
x0 = FreshBlock('x')
x1 = FreshBlock('x')
oldv0 = d.read(i0)
oldv1 = d.read(i1)
d.write(i0, x0)
d.flush()
d.write(i1, x1)
d = d.crash(Machine())
anyv0 = d.read(i0)
anyv1 = d.read(i1)
assumption = And(i0 != i1, x1 != oldv1, mach.assumption)
self.prove(Implies(assumption, Implies(anyv1 == x1, anyv0 == x0)))
# implement SyncDisk on top of AsyncDisk
class FlushingDisk:
def __init__(self, disk):
self._disk = disk
def write(self, bid, data):
self._disk.write(bid, data)
self._disk.flush()
def flush(self):
pass
def read(self, bid):
return self._disk.read(bid)
def crash(self, mach):
return self.__class__(self._disk.crash(mach))
class SyncDiskTest(test.DiskTest):
# SyncDisk is an AsyncDisk
def test_syncdisk_is_async(self):
array = DiskSort(fresh_name('array'))
bid = FreshSize('bid')
data = FreshBlock('data')
spec_mach = Machine()
impl_mach = Machine()
ad = AsyncDisk(spec_mach, array)
ad.write(bid, data)
sd = SyncDisk(impl_mach, array)
sd.write(bid, data)
self.prove(ForAll([array] + impl_mach.control,
Exists(spec_mach.control,
ForAll([bid], sd.read(bid) == ad.read(bid)))))
# AsyncDisk+flush is a SyncDisk
def test_flushingdisk_is_sync(self):
array = DiskSort(fresh_name('array'))
bid = FreshSize('bid')
data = FreshBlock('data')
spec_mach = Machine()
impl_mach = Machine()
sd = SyncDisk(spec_mach, array)
sd.write(bid, data)
fd = FlushingDisk(AsyncDisk(impl_mach, array))
fd.write(bid, data)
self.prove(ForAll([array] + impl_mach.control,
Exists(spec_mach.control,
ForAll([bid], sd.read(bid) == fd.read(bid)))))
# implement SyncDisk on top of AsyncDisk
class FlushingDisk:
def __init__(self, disk):
self._disk = disk
def write(self, bid, data):
self._disk.write(bid, data)
self._disk.flush()
def flush(self):
pass
def read(self, bid):
return self._disk.read(bid)
def crash(self, mach):
return self.__class__(self._disk.crash(mach))
class SyncDiskTest(test.DiskTest):
# SyncDisk is an AsyncDisk
def test_syncdisk_is_async(self):
array = FreshDiskArray('array')
bid = FreshSize('bid')
data = FreshBlock('data')
spec_mach = Machine()
impl_mach = Machine()
ad = AsyncDisk(spec_mach, array)
ad.write(bid, data)
sd = SyncDisk(impl_mach, array)
sd.write(bid, data)
self.solve(ForAll(spec_mach.control, Not(
ForAll([bid], sd.read(bid) == ad.read(bid)))))
# AsyncDisk+flush is a SyncDisk
def test_flushingdisk_is_sync(self):
array = FreshDiskArray('array')
bid = FreshSize('bid')
data = FreshBlock('data')
spec_mach = Machine()
impl_mach = Machine()
sd = SyncDisk(spec_mach, array)
sd.write(bid, data)
fd = FlushingDisk(AsyncDisk(impl_mach, array))
fd.write(bid, data)
self.solve(ForAll(spec_mach.control, Not(
ForAll([bid], sd.read(bid) == fd.read(bid)))))
class VirtualAsyncDiskTest(test.RefinementTest):
def create_spec(self, mach):
array = FreshDiskArray('array')
return AsyncDisk(mach, array)
def create_impl(self, mach):
array = FreshDiskArray('array')
mapped = FreshDiskArray('mapped')
return VirtualAsyncDisk(mach, mapped, array)
def equivalence(self, spec, impl, **kwargs):
vbn = Const(fresh_name('i'), spec.domain())
return ForAll([vbn],
Implies(impl.is_mapped(vbn),
impl.read(vbn) == spec.read(vbn)))
def match_write(self, spec=None, impl=None):
i0 = FreshSize('i')
x0 = FreshBlock('x')
yield (i0, x0)
class InodeSpecTest(test.DiskTest):
def setUp(self):
disk.native = False
disk.assertion.assertions = []
self._array = FreshDiskArray('array')
self._attr_array = FreshDiskArray('attrs')
self._mapped = FreshDiskArray('mapped')
def test_compare_to_async(self):
d1_mach = Machine()
d1 = AsyncDisk(d1_mach, self._array)
d2_mach = Machine()
d2 = InodeSpec(d2_mach, [self._mapped, self._attr_array, self._array])
vbn = Const(fresh_name('i'), d1.domain())
i0 = Const(fresh_name('i'), d1.domain())
x0 = FreshBlock('x')
pre = ForAll([vbn], Implies(d2.is_mapped(vbn), d1.read(vbn) == d2.read(vbn)))
d1.write(i0, x0)
d2.begin_tx()
d2.write(i0, x0)
d2.commit_tx()
d1 = d1.crash(Machine())
d2 = d2.crash(Machine())
assumption = And(d1_mach.assumption, d2_mach.assumption)
post = ForAll([vbn], Implies(d2.is_mapped(vbn), d1.read(vbn) == d2.read(vbn)))
C1 = d1_mach.control
self.solve(
ForAll(C1, Not(
Implies(assumption, Implies(pre, post)))))
def test_bmap(self):
self.enable_symbolic_execution()
mach = Machine()
d2 = InodeSpec(mach, [self._mapped, self._attr_array, self._array])
vbn = FreshSize('vbn')
mapped = d2.is_mapped(vbn)
d2.begin_tx()
d2.bmap(vbn)
d2.commit_tx()
self.prove(Implies(And(*disk.assertion.assertions), Implies(Not(mapped), d2.is_mapped(vbn))))
def test_bunmap(self):
mach = Machine()
d2 = InodeSpec(mach, [self._mapped, self._attr_array, self._array])
vbn = FreshSize('vbn')
mapped = d2.is_mapped(vbn)
d2.begin_tx()
d2.bunmap(vbn)
d2.commit_tx()
self.prove(Implies(mapped, Not(d2.is_mapped(vbn))))
if __name__ == '__main__':
test.main()