-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hyper.dfy
212 lines (182 loc) · 6.83 KB
/
Hyper.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
// RUN: /compile:0
type Pid = int
type FD = int
type RefCount = nat
type FileTableEntry = int
datatype File = File(in_use: bool, refcount: RefCount)
class State {
var file_table: array<File>
var proc_fd_tables: map<Pid, array<FileTableEntry>>
ghost var repr: set<object>
function References(f: int): set<(Pid, FD)>
reads this, repr
requires ReprValid()
requires forall proc | proc in proc_fd_tables :: proc_fd_tables[proc] != null
{
set proc, fd |
&& proc in proc_fd_tables
&& 0 <= fd < proc_fd_tables[proc].Length
&& proc_fd_tables[proc][fd] == f
:: (proc, fd)
}
predicate ReprValid()
reads this, repr
{
&& this in repr
&& null !in repr
&& file_table in repr
&& (forall proc | proc in proc_fd_tables :: proc_fd_tables[proc] in repr)
}
predicate Valid()
reads this, repr
{
&& ReprValid()
&& (forall proc, fd | proc in proc_fd_tables && 0 <= fd < proc_fd_tables[proc].Length ::
var f := proc_fd_tables[proc][fd];
f == -1 || (0 <= f < file_table.Length && file_table[f].in_use))
&& (forall p1, p2 | p1 in proc_fd_tables && p2 in proc_fd_tables && p1 != p2 ::
proc_fd_tables[p1] != proc_fd_tables[p2])
&& (forall f | 0 <= f < file_table.Length ::
&& file_table[f].refcount == |References(f)|
&& (file_table[f].in_use <==> file_table[f].refcount > 0))
}
}
method Dup(s: State, proc: Pid, oldfd: FD) returns (ok: bool, newfd: FD)
requires s != null && s.Valid() && proc in s.proc_fd_tables
modifies s.repr
ensures s.Valid()
ensures s.proc_fd_tables == old(s.proc_fd_tables)
ensures forall proc' | proc' != proc && proc' in s.proc_fd_tables ::
s.proc_fd_tables[proc'][..] == old(s.proc_fd_tables[proc'][..])
ensures ok ==>
&& 0 <= oldfd < s.proc_fd_tables[proc].Length
&& 0 <= newfd < s.proc_fd_tables[proc].Length
&& s.proc_fd_tables[proc][oldfd] == s.proc_fd_tables[proc][newfd]
&& (forall fd | 0 <= fd < newfd :: s.proc_fd_tables[proc][fd] != -1)
&& (forall fd | 0 <= fd < s.proc_fd_tables[proc].Length && fd != newfd ::
s.proc_fd_tables[proc][fd] == old(s.proc_fd_tables[proc][fd]))
{
var fd_table := s.proc_fd_tables[proc];
if oldfd < 0 || oldfd >= fd_table.Length || fd_table[oldfd] == -1 { ok := false; return; }
newfd := 0;
ok := false;
while newfd < fd_table.Length
modifies {}
invariant 0 <= newfd <= fd_table.Length
invariant (forall fd | 0 <= fd < newfd :: s.proc_fd_tables[proc][fd] != -1)
{
if fd_table[newfd] == -1 { ok := true; break; }
newfd := newfd + 1;
}
if !ok { return; }
assert 0 <= newfd < fd_table.Length && fd_table[newfd] == -1;
assert oldfd != newfd;
var f := fd_table[oldfd];
fd_table[newfd] := f;
s.file_table[f] := s.file_table[f].(refcount := s.file_table[f].refcount + 1);
forall f' | 0 <= f' < s.file_table.Length && s.file_table[f'].in_use
ensures s.file_table[f'].refcount == |s.References(f')|
{
if f == f' {
assert s.References(f) == old(s.References(f)) + {(proc, newfd)};
} else {
assert s.References(f') == old(s.References(f'));
}
}
return;
}
method DupFinite(s: State, proc: Pid, oldfd: FD, newfd: FD) returns (ok: bool)
requires s != null && s.Valid() && proc in s.proc_fd_tables
modifies s.repr
ensures s.Valid()
ensures s.proc_fd_tables == old(s.proc_fd_tables)
ensures forall proc' | proc' != proc && proc' in s.proc_fd_tables ::
s.proc_fd_tables[proc'][..] == old(s.proc_fd_tables[proc'][..])
ensures ok ==>
&& 0 <= oldfd < s.proc_fd_tables[proc].Length
&& 0 <= newfd < s.proc_fd_tables[proc].Length
&& s.proc_fd_tables[proc][oldfd] == s.proc_fd_tables[proc][newfd]
&& (forall fd | 0 <= fd < s.proc_fd_tables[proc].Length && fd != newfd ::
s.proc_fd_tables[proc][fd] == old(s.proc_fd_tables[proc][fd]))
{
var fd_table := s.proc_fd_tables[proc];
if oldfd < 0 || oldfd >= fd_table.Length || fd_table[oldfd] == -1 { return false; }
if newfd < 0 || newfd >= fd_table.Length || fd_table[newfd] != -1 { return false; }
assert oldfd != newfd;
var f := fd_table[oldfd];
fd_table[newfd] := f;
s.file_table[f] := s.file_table[f].(refcount := s.file_table[f].refcount + 1);
forall f' | 0 <= f' < s.file_table.Length && s.file_table[f'].in_use
ensures s.file_table[f'].refcount == |s.References(f')|
{
if f == f' {
assert s.References(f) == old(s.References(f)) + {(proc, newfd)};
} else {
assert s.References(f') == old(s.References(f'));
}
}
return true;
}
lemma CardinalitySubset<T>(A: set<T>, B: set<T>)
requires A <= B
ensures |A| <= |B|
{
if A == B {
return;
} else {
assert A < B;
var x :| x in B && x !in A;
CardinalitySubset(A, B - {x});
}
}
lemma CardinalityOne<T>(S: set<T>, x: T)
requires |S| == 1 && x in S
ensures S == {x}
{
forall y | y in S
ensures y == x
{
if y != x {
CardinalitySubset({x, y}, S);
}
}
}
lemma ValidReferences(s: State)
requires s != null && s.Valid()
{}
method Close(s: State, proc: Pid, fd: FD) returns (ok: bool)
requires s != null && s.Valid() && proc in s.proc_fd_tables
modifies s.repr
ensures s.Valid()
ensures s.proc_fd_tables == old(s.proc_fd_tables)
ensures forall proc' | proc' != proc && proc' in s.proc_fd_tables ::
s.proc_fd_tables[proc'][..] == old(s.proc_fd_tables[proc'][..])
{
var fd_table := s.proc_fd_tables[proc];
if fd < 0 || fd >= fd_table.Length || fd_table[fd] == -1 { return false; }
var file := fd_table[fd];
if s.file_table[file].refcount == 1 {
assert forall proc', fd' |
&& proc' in s.proc_fd_tables
&& 0 <= fd' < s.proc_fd_tables[proc'].Length
:: (proc', fd') in s.References(file) || s.proc_fd_tables[proc'][fd'] != file;
assert s.References(file) == {(proc, fd)} by {
CardinalityOne(s.References(file), (proc, fd));
}
}
fd_table[fd] := -1;
s.file_table[file] := s.file_table[file].(refcount := s.file_table[file].refcount - 1);
if s.file_table[file].refcount == 0 {
s.file_table[file] := s.file_table[file].(in_use := false);
}
forall f | 0 <= f < s.file_table.Length
ensures s.file_table[f].refcount == |s.References(f)|
ensures s.file_table[f].in_use <==> s.file_table[f].refcount > 0
{
if file == f {
assert s.References(file) == old(s.References(file)) - {(proc, fd)};
} else {
assert s.References(f) == old(s.References(f));
}
}
}