-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathconsts.rs
435 lines (365 loc) · 10.8 KB
/
consts.rs
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#![feature(rustc_private)]
#[macro_use]
mod common;
use common::*;
test_verify_one_file! {
#[test] test_const_eval1 verus_code!{
#[repr(u8)]
enum TestConst {
Zero = 0,
One = 1,
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_generic_const_eval2 verus_code!{
const S: usize = 10;
struct TestConst {
a: [u8; S],
}
} => Ok(())
}
test_verify_one_file! {
#[test] test1 verus_code! {
spec fn f() -> int { 1 }
const C: u64 = 3 + 5;
spec const S: int = C as int + f();
const fn e() -> (u: u64) ensures u == 1 { 1 }
exec const E: u64 ensures E == 2 { 1 + e() }
fn test1() {
let x = C;
assert(x == 8);
assert(S == 9);
let y = E;
assert(y == 2);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test1_fails1 verus_code! {
spec fn f() -> int { 1 }
const C: u64 = 3 + 5;
spec const S: int = C + f();
fn test1() {
let x = C;
assert(x == 8);
assert(S == 10); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test1_fails2 verus_code! {
const C: u64 = S;
const S: u64 = C;
} => Err(err) => assert_rust_error_msg(err, "cycle detected when simplifying constant for the type system `C`")
}
test_verify_one_file! {
#[test] test1_fails_const_fn verus_code! {
fn x() {
}
const fn y() {
x()
}
} => Err(err) => assert_rust_error_msg(err, "cannot call non-const fn `x` in constant functions")
}
test_verify_one_file! {
#[test] test1_fails3 verus_code! {
spec const C: u64 = S;
spec const S: u64 = C;
} => Err(err) => assert_vir_error_msg(err, "recursive function must have a decreases clause")
}
test_verify_one_file! {
#[test] test1_fails4 verus_code! {
spec const C: u64 = add(3, 5);
const S: int = C + 1;
} => Err(err) => assert_rust_error_msg(err, "mismatched types")
}
test_verify_one_file! {
#[test] test1_fails5 verus_code! {
const fn f() -> u64 { 1 }
const S: u64 = 1 + f();
} => Err(err) => assert_vir_error_msg(err, "cannot call function `crate::f` with mode exec")
}
test_verify_one_file! {
#[test] test1_fails6 verus_code! {
const fn e() -> (u: u64) ensures u >= 1 { 1 }
exec const E: u64 = 1 + e(); // FAILS
} => Err(e) => assert_vir_error_msg(e, "possible arithmetic underflow/overflow")
}
test_verify_one_file! {
#[test] test1_fails7 verus_code! {
exec const E: u64 ensures true { 1 }
fn test1() {
proof { let x = E; }
}
} => Err(e) => assert_vir_error_msg(e, "cannot read const with mode exec")
}
test_verify_one_file! {
#[test] test1_fails8 verus_code! {
exec const E: u64 ensures true { 1 }
proof fn test1() {
let x = E;
}
} => Err(e) => assert_vir_error_msg(e, "cannot read const with mode exec")
}
test_verify_one_file! {
#[test] test1_fails9 verus_code! {
spec const S: u64 = 1;
fn test1() {
let x = S;
}
} => Err(e) => assert_vir_error_msg(e, "cannot read const with mode spec")
}
test_verify_one_file! {
#[test] test_used_as_spec verus_code! {
spec const SPEC_E: u64 = 7;
#[verifier::when_used_as_spec(SPEC_E)]
exec const E: u64 ensures E == SPEC_E { 7 }
fn test1() {
let y = E;
assert(y == E);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_use_const_twice verus_code! {
// This behavior is important if X represents a Cell, for example
#[verifier::external_body]
struct X { }
#[verifier::external_body]
const fn x() -> X { X{} }
exec const E: X = x();
fn test1() {
// think of 'E' as equivalent to 'x()'. Different calls might
// return different values. (At least if x contains ghost content)
let a = E;
let b = E;
assert(a == b); // FAILS
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_use_static_twice verus_code! {
#[verifier::external_body]
struct X { }
#[verifier::external_body]
const fn x() -> X { X{} }
exec static E: X = x();
fn test1() {
// These are 'the same' E
let a = &E;
let b = &E;
assert(a == b);
}
} => Ok(())
}
test_verify_one_file! {
#[test] spec_dual_mode_unsupported verus_code! {
static E: u64 = 0;
} => Err(e) => assert_vir_error_msg(e, "explicitly mark the static as `exec`")
}
test_verify_one_file! {
#[test] reference_static_from_proof_unsupported verus_code! {
exec static E: u64 = 0;
proof fn stuff() {
let x = E;
}
} => Err(e) => assert_vir_error_msg(e, "cannot read static with mode exec")
}
test_verify_one_file! {
#[test] reference_static_from_spec_unsupported verus_code! {
exec static E: u64 = 0;
// It might be feasible to support this, because although the value of `E`
// is the result of an exec computation, it *is* fixed.
// However, it would require us to be careful about cyclicity checking.
spec fn stuff() -> u64 {
E
}
} => Err(e) => assert_vir_error_msg(e, "cannot read static with mode exec")
}
test_verify_one_file! {
#[test] reference_static_from_dual_mode_const_unsupported verus_code! {
exec static E: u64 = 0;
const s: u64 = E;
} => Err(e) => assert_vir_error_msg(e, "cannot read static with mode exec")
}
test_verify_one_file! {
#[test] reference_static_from_proof_block_double_move verus_code! {
struct X { }
exec static E: X = X{};
fn stuff() {
proof {
let tracked x = E;
let tracked y = E;
}
}
} => Err(e) => assert_vir_error_msg(e, "cannot read static with mode exec")
}
test_verify_one_file! {
#[test] statics_get_type_invariants verus_code! {
exec static E: u64 = 0;
fn stuff() {
let j = E;
assert(j >= 0);
assert(j <= 0xffff_ffff_ffff_ffff);
}
} => Ok(())
}
test_verify_one_file! {
#[test] statics_recurse2 verus_code! {
exec static E: u64 ensures false {
proof { let x = F; }
0
}
exec static F: u64 ensures false {
proof { let x = E; }
0
}
} => Err(err) => assert_vir_error_msg(err, "cannot read static with mode exec")
}
test_verify_one_file! {
#[test] const_recurse2 verus_code! {
exec const D: u64 = 1;
exec const E: u64 ensures false {
proof { let x = D; }
0
}
exec const F: u64 ensures false {
proof { let x = E; }
0
}
} => Err(err) => assert_vir_error_msg(err, "cannot read const with mode exec")
}
test_verify_one_file! {
#[test] return_static_lifetime verus_code! {
exec static x: u8 = 0;
fn stuff() -> &'static u8 {
&x
}
} => Ok(())
}
test_verify_one_file! {
#[test] pub_exec_const verus_code! {
// https://github.com/verus-lang/verus/issues/858
const fn foo() -> u64 {
0
}
pub exec const FOO: u64 = foo();
} => Ok(())
}
test_verify_one_file! {
#[test] statics_atomics verus_code! {
use vstd::*;
pub fn heap_init() {
increment_thread_count();
}
exec static THREAD_COUNT: core::sync::atomic::AtomicUsize = core::sync::atomic::AtomicUsize::new(0);
#[inline]
fn increment_thread_count() {
THREAD_COUNT.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
}
#[inline]
pub fn current_thread_count() -> usize {
THREAD_COUNT.load(core::sync::atomic::Ordering::Relaxed)
}
} => Ok(())
}
test_verify_one_file! {
#[test] statics_ens_ordering verus_code! {
pub fn test() {
test2();
}
exec static X: u32 ensures 2 <= X <= 4 { 3 }
#[inline]
fn test2() {
let y = X;
assert(2 <= y <= 4);
}
} => Ok(())
}
test_verify_one_file! {
#[test] arrays verus_code! {
use vstd::prelude::*;
const MyArray: [u32; 3] = [1, 2, 3];
proof fn test() {
assert(MyArray[2] == 3);
}
fn exec_test() {
let x = MyArray[1];
assert(x == 2);
}
} => Ok(())
}
test_verify_one_file! {
#[test] array_out_of_bounds verus_code! {
use vstd::prelude::*;
const MyArray: [u32; 3] = [1, 2, 3];
proof fn test() {
assert(MyArray[5] == 3); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] array_incorrect_value verus_code! {
use vstd::prelude::*;
const MyArray: [u32; 3] = [1, 2, 3];
proof fn test() {
assert(MyArray[1] == 42); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] exec_const_body_with_proof_assert verus_code! {
spec fn f() -> int { 1 }
const fn e() -> (u: u64) ensures u == 1 { 1 }
exec const E: u64 ensures E == 2 {
assert(f() == 1);
1 + e()
}
} => Ok(())
}
test_verify_one_file! {
#[test] exec_const_body_with_proof_call verus_code! {
#[verifier::opaque]
spec fn f() -> int { 1 }
proof fn e() -> (u: u64)
ensures
u == f(),
u == 1
{
reveal(f);
1
}
exec const E: u64 ensures E == f() {
proof {
let x = e();
assert(x == f());
assert(x == 1);
}
assert(1 == f());
1
}
} => Ok(())
}
test_verify_one_file! {
#[test] allow_external_body_const_regression_1322 verus_code! {
#[verifier(external_body)]
const A: usize = unimplemented!();
} => Err(err) => assert_rust_error_msg(err, "evaluation of constant value failed")
}
test_verify_one_file! {
#[test] allow_external_body_const_regression_1322_1 verus_code! {
#[verifier::external]
const fn stuff() -> usize { 0 }
#[verifier(external_body)]
const A: usize = stuff();
} => Ok(())
}
test_verify_one_file! {
#[ignore] #[test] allow_external_body_const_regression_1322_2 verus_code! {
#[verifier::external]
const fn stuff() -> usize { 0 }
#[verifier(external_body)]
const A: usize ensures 32 <= A <= 52 { stuff() }
} => Ok(())
}