summaryrefslogtreecommitdiff
path: root/Chalice/tests/permission-model/permission_arithmetic.chalice
blob: 0cdf8aaebeecab55add81cb3d2c276ea67473f06 (plain)
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
class Cell {
  var x: int;
  var i: int;
  var y: Cell;
  var f: int;
  var g: int;
  
  invariant rd(x);
  
  predicate valid { rd(x) }
  
  method a1(n: int) // test various arithmetic operations on permissions
    requires acc(x,1+1) && acc(x,1) && acc(x,3) && acc(x,1-rd(5-7)+rd(3)) && rd(x) && rd(this.y);
    ensures acc(x,100-97);
  {
  }
  
  method a2(n: int)
    requires acc(x,1-rd(1)-2);
  {
    assert false; // this should verify, as the precondition contains an invalid permission
  }
  
  method a3(n: int)
  {
    assert acc(x,1-rd(1)-2); // ERROR: invalid (negative) permission
  }
  
  method a4(n: int)
    requires acc(x,rd(n));
  {
  }
  
  method a5(n: int)
    requires acc(x,rd(n)-rd(2));
  {
  }
  
  method a6()
    requires acc(x);
  {
    call a5(1); // ERROR: n=1 makes the permission in the precondition negative
  }
  
  method a7(c: Cell)
    requires acc(c.x,100-rd(c));
    requires c != null && acc(c.mu) && waitlevel << c;
    ensures acc(c.x);
  {
    acquire(c);
    unshare(c);
  }
  
  method a8()
    requires acc(x,100-rd(valid)) && valid;
    ensures acc(x);
  {
    unfold valid;
  }
  
  method a9()
    requires acc(x,rd(valid));
    ensures valid;
  {
    fold valid;
  }
  
  method a10()
    requires valid;
    ensures acc(x,rd(valid));
  {
    unfold valid;
  }
  
  method a11() // ERROR: postcondition does not hold (not enough permission)
    requires valid;
    ensures acc(x);
  {
    unfold valid;
  }
  
  method a12()
    requires rd(this.i) && this.i > 0 && acc(x,rd(this.i));
    ensures rd(this.i) && this.i > 0 && acc(x,rd(i));
  {
  }
  
  method a13(i: int) // ERROR: postcondition does not hold
    requires rd(this.i) && this.i > 0 && i > 0 && acc(x,rd(this.i));
    ensures i > 0 && acc(x,rd(i));
  {
  }
  
  method a14()
    requires acc(y) && this.y == this; // test aliasing
    requires acc(x,100-rd(y));
    requires y != null && acc(this.mu) && waitlevel << this;
    ensures acc(x);
    lockchange this;
  {
    acquire this;
  }
  
  method a15()
    requires acc(x,rd(this.i)); // ERROR: this.i is not readable
    ensures acc(x,rd(this.i));
  {
  }
  
  method a16()
    requires acc(x,rd(this.y)); // ERROR: this.y is not readable
    ensures acc(x,rd(this.y));
  {
  }
  
  method a17(tk: token<Cell.void>)
    requires acc(x,100-rd(tk)) && acc(tk.joinable) && tk.joinable;
    requires eval(tk.fork this.void(),true);
    ensures acc(x);
  {
    join tk;
  }
  
  method a18()
    requires acc(x,rd+rd-rd+10*rd-rd*(5+5))
    ensures rd(x)
  {
    call void();
  }
  
  method a19()
    requires acc(x)
    requires acc(this.mu) && lockbottom == this.mu
    ensures acc(x)
    lockchange this;
  {
    share this;
    acquire this;
    unshare this;
  }
  
  method a20()
    requires rd(x)
    requires acc(this.mu) && lockbottom == this.mu
    lockchange this;
  {
    share this; // ERROR: not enough permission
  }
  
  method a21()
    requires acc(x,rd*2)
    ensures rd(x) && rd(x)
  {
    assert acc(x,rd+rd)
    assert acc(x,(1+1)*rd)
  }
  
  method a22()
    requires acc(x,1*2*5)
    ensures acc(x,10)
  {
  }
  
  method a23(c: Cell) // ERROR: permission in postcondition not positive
    requires acc(x,rd-rd(c))
    ensures acc(x,rd(c)-rd)
  {
  }
  
  method a24()
    requires rd*(x)
    requires rd*(x)
  {
  }
  
  method a25() // ERROR: postcondition does not hold, possibly not enough permission
    requires rd*(x)
    ensures acc(x,rd)
  {
  }
  
  // interaction of monitors and predicates
  method a26()
    requires acc(x,rd(this))
    requires acc(mu) && lockbottom == this.mu
    ensures valid
    lockchange this
  {
    share this
    acquire this
    fold valid
  }
  
  method a27()
    requires acc(f,100-rd) && acc(f,rd)
  {
    assert acc(f) // ok, we have full access
  }
  
  method a28()
    requires acc(f)
  {
    call a27();
    var x: int
    x := f // ERROR: no permission left
  }
  
  method a27b()
    requires acc(f,100-rd)
    requires acc(f,rd)
  {
    assert acc(f) // ok, we have full access
  }
  
  method a28b()
    requires acc(f)
  {
    call a27b();
    var x: int
    x := f // ERROR: no permission left
  }
  
  method a29()
    requires acc(f, 100-rd) && acc(g, rd)
  {  }

  method a30()
    requires acc(f, 100) && acc(g, rd)
  {
    call a29();
    var tmp: int := this.g;
  }
  
  method a31(c: Cell)
    requires acc(f, 100-rd) && acc(c.f, rd)
  {  }

  method a32(c: Cell)
    requires acc(f, 100) && acc(c.f, rd)
  {
    call a31(c);
    var tmp: int := this.f;
  }
  
  method void() requires rd(x); ensures rd(x); {}
}