-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcursor.dfy
214 lines (190 loc) · 5.16 KB
/
cursor.dfy
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
include "typed_fs.dfy"
module FileCursor {
import opened Machine
import opened ByteSlice
import opened FsKinds
import opened JrnlSpec
import opened TypedFs
import opened MemInodes
class Cursor
{
const ino: Ino
const i: MemInode
const fs: TypedFilesys
var off: uint64
var bs: Bytes
var valid?: bool
// this doesn't include fs.Repr and i.Repr; we list those explicitly
ghost function Repr(): set<object>
reads this
{
{this, bs}
}
ghost const ReprFs: set<object> := fs.Repr + i.Repr
ghost predicate {:opaque} ValidFs()
reads this, ReprFs
{
&& fs.ValidIno(ino, i)
&& |fs.data[ino]| % 4096 == 0
&& off % 4096 == 0
&& bs !in i.Repr
&& (valid? ==> off as nat + 4096 <= |fs.data[ino]|)
}
ghost predicate {:opaque} ValidBytes()
reads this, bs, fs
requires fs.ValidDomains()
{
&& |bs.data| == 4096
&& off as nat + 4096 <= |fs.data[ino]|
&& bs.data == fs.data[ino][off as nat .. off as nat + 4096]
}
ghost predicate Valid()
reads Repr(), fs.Repr, i.Repr
{
&& fs.ValidDomains()
&& ValidFs()
&& (valid? ==> ValidBytes())
}
// convenience function since this is a core concept
ghost function contents(): seq<byte>
reads fs
requires fs.ValidDomains()
{
fs.data[ino]
}
ghost predicate has_data(data: seq<byte>)
reads this, bs
{
valid? && bs.data == data
}
constructor(fs: TypedFilesys, ino: Ino, i: MemInode)
requires fs.ValidIno(ino, i)
requires |fs.data[ino]| % 4096 == 0
ensures Valid()
ensures this.fs == fs
ensures this.i == i
ensures this.ino == ino
ensures fresh(Repr())
ensures !valid?
{
this.ino := ino;
this.i := i;
this.fs := fs;
this.off := 0;
var bs := NewBytes(0);
this.bs := bs;
this.valid? := false;
new;
reveal ValidFs();
reveal ValidBytes();
}
lemma data_ok()
requires Valid()
requires valid?
ensures off as nat + 4096 <= |fs.data[ino]|
ensures bs.data == fs.data[ino][off as nat .. off as nat + 4096]
{
reveal ValidFs();
reveal ValidBytes();
}
method size() returns (sz: uint64)
requires Valid()
ensures sz as nat == |fs.data[ino]|
ensures sz % 4096 == 0
{
reveal ValidFs();
fs.inode_metadata(ino, i);
return i.sz;
}
twostate predicate buffer_fresh()
reads this
{
fresh(Repr() - old(Repr()))
}
method advanceTo(txn: Txn, off': uint64)
modifies this
requires fs.has_jrnl(txn)
requires Valid()
requires off' % 4096 == 0
requires off' as nat < |fs.data[ino]|
ensures this.off == off'
ensures valid?
ensures buffer_fresh()
ensures Valid()
{
if off' == off && valid? {
return;
}
reveal ValidFs();
assert off' as nat + 4096 <= |fs.data[ino]| by {
Arith.divisible_bound(off' as nat, |fs.data[ino]|, 4096);
}
this.off := off';
var blk := fs.readUnsafe(txn, ino, i, off, 4096);
bs := blk;
valid? := true;
reveal ValidBytes();
}
method writeback(txn: Txn)
returns (ok: bool)
modifies Repr(), ReprFs
// note that ValidFs is preserved just by not modifying this directly and modifying bs
requires ValidFs()
requires fs.has_jrnl(txn)
requires valid? && |bs.data| == 4096
ensures fs.types_unchanged()
ensures buffer_fresh()
ensures ok ==>
(reveal ValidFs();
&& Valid()
&& fs.data == old(fs.data[ino := C.splice(fs.data[ino], off as nat, bs.data)]))
{
reveal ValidFs();
ok := fs.writeBlock(txn, ino, i, off, bs);
valid? := false;
reveal ValidBytes();
}
method grow(txn: Txn)
returns (ok: bool)
modifies Repr(), ReprFs
requires Valid()
requires fs.has_jrnl(txn)
requires |fs.data[ino]| + 4096 <= Inode.MAX_SZ
ensures fs.types_unchanged()
ensures buffer_fresh()
ensures ok ==>
&& Valid()
&& off == old(off)
&& fs.data == old(fs.data[ino := fs.data[ino] + JrnlTypes.block0])
{
reveal ValidFs();
var blk := NewBytes(4096);
assert blk.data == JrnlTypes.block0;
fs.inode_metadata(ino, i);
ByteFs.write_data_append(fs.data[ino], i.sz as nat, blk.data);
ok := fs.write(txn, ino, i, i.sz, blk);
reveal ValidBytes();
}
method getAttrs() returns (attrs: Inode.Attrs)
requires Valid()
ensures attrs == fs.types[ino]
{
reveal ValidFs();
attrs := i.attrs;
fs.inode_metadata(ino, i);
}
method setAttrs(attrs': Inode.Attrs)
modifies ReprFs
requires Valid() ensures Valid()
requires attrs'.ty == fs.types[ino].ty
ensures fs.types == old(fs.types[ino := attrs'])
ensures fs.data == old(fs.data)
ensures buffer_fresh()
ensures valid? == old(valid?)
{
reveal ValidFs();
fs.setAttrs(ino, i, attrs');
assert Valid() by { reveal ValidBytes(); }
}
}
}