forked from verus-lang/verus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogatom.rs
More file actions
693 lines (624 loc) · 20 KB
/
Copy pathlogatom.rs
File metadata and controls
693 lines (624 loc) · 20 KB
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#![feature(rustc_private)]
#[macro_use]
mod common;
use std::sync::LazyLock;
use common::*;
const TOKEN_LIB: &'static str = verus_code_str! {
use vstd::*;
use vstd::prelude::*;
use vstd::atomic::*;
pub tracked struct Token {
state: u16,
}
impl Token {
pub closed spec fn is_valid(self) -> bool {
self.state <= 1000
}
proof fn with_state(tracked state: u16) -> (tracked out: Self)
ensures out.state == state,
{
Self { state }
}
pub proof fn new() -> (tracked out: Self)
ensures out.is_valid(),
{
Self::with_state(250)
}
pub proof fn invalid() -> (tracked out: Self)
ensures !out.is_valid(),
{
Self::with_state(2000)
}
pub proof fn visit(tracked &self)
requires self.is_valid(),
ensures self.is_valid(),
{}
pub proof fn modify(tracked &mut self)
requires old(self).is_valid(),
ensures final(self).is_valid(),
{
self.state = (1000 - self.state) as u16;
}
pub proof fn consume(tracked self) {
let _ = self;
}
}
};
test_verify_one_file! {
#[test] token_lib_def TOKEN_LIB.to_owned() => Ok(())
}
test_verify_one_file! {
#[test] atomic_function_args
TOKEN_LIB.to_owned() + verus_code_str! {
pub fn atomic_function(x: u8, y: i32)
atomically (atomic_update) {}
{
assert(atomic_update.pred().args(x, y));
assume(false);
}
} => Ok(())
}
test_verify_one_file! {
#[test] atomic_function_commit_only
TOKEN_LIB.to_owned() + verus_code_str! {
pub fn atomic_function()
atomically (atomic_update) {
(x: Token) -> (y: Result<Token, Token>),
requires x.is_valid(),
ensures match y {
Ok(t) => t.is_valid(),
Err(t) => t == x,
},
},
{
let res = try_open_atomic_update!(atomic_update, token => {
Tracked(Ok(token))
});
assert(res@ is Ok);
assert(res@->Ok_0 == ());
assert(atomic_update.resolves());
}
} => Ok(())
}
test_verify_one_file! {
#[test] atomic_function_commit_fail_atomic_post
TOKEN_LIB.to_owned() + verus_code_str! {
pub fn atomic_function()
atomically (atomic_update) {
(x: Token) -> (y: Result<Token, Token>),
requires x.is_valid(),
ensures match y {
Ok(t) => t.is_valid(),
Err(t) => t == x,
},
},
{
try_open_atomic_update!(atomic_update, token => {
Tracked(Ok(Token::invalid()))
});
}
} => Err(err) => assert_vir_error_msg(err, "cannot show atomic postcondition hold at end of block")
}
test_verify_one_file! {
#[test] atomic_function_abort_only
TOKEN_LIB.to_owned() + verus_code_str! {
pub fn atomic_function()
atomically (atomic_update) {
(x: Token) -> (y: Result<Token, Token>),
requires x.is_valid(),
ensures match y {
Ok(t) => t.is_valid(),
Err(t) => t == x,
},
},
{
let tracked au = atomic_update;
let res = try_open_atomic_update!(au, token => {
Tracked(Err(token))
});
assert(res@ is Err);
assert(res@->Err_0 == atomic_update);
assume(false);
}
} => Ok(())
}
test_verify_one_file! {
#[test] atomic_function_abort_fail_atomic_post
TOKEN_LIB.to_owned() + verus_code_str! {
pub fn atomic_function()
atomically (atomic_update) {
(x: Token) -> (y: Result<Token, Token>),
requires x.is_valid(),
ensures match y {
Ok(t) => t.is_valid(),
Err(t) => t == x,
},
},
{
try_open_atomic_update!(atomic_update, token => {
Tracked(Err(Token::invalid()))
});
}
} => Err(err) => assert_any_vir_error_msg(err, "cannot show atomic postcondition hold at end of block")
}
test_verify_one_file! {
#[test] atomic_function_abort_and_commit
TOKEN_LIB.to_owned() + verus_code_str! {
pub fn atomic_function()
atomically (atomic_update) {
(x: Token) -> (y: Result<Token, Token>),
requires x.is_valid(),
ensures match y {
Ok(t) => t.is_valid(),
Err(t) => t == x,
},
},
{
let tracked au = atomic_update;
let res = try_open_atomic_update!(au, token => {
Tracked(Err(token))
});
assert(res@ is Err);
let tracked au = res.get().tracked_unwrap_err();
assert(au == atomic_update);
let res = try_open_atomic_update!(au, mut token => {
proof { token.modify() };
Tracked(Ok(token))
});
assert(res@ is Ok);
assert(atomic_update.resolves());
}
} => Ok(())
}
test_verify_one_file! {
#[test] atomic_function_lifetime_escape
TOKEN_LIB.to_owned() + verus_code_str! {
pub fn atomic_function()
atomically (atomic_update) {
(x: Token) -> (y: Result<Token, Token>),
requires x.is_valid(),
ensures match y {
Ok(t) => t.is_valid(),
Err(t) => t == x,
},
},
{
let tracked dummy = Token::invalid();
let tracked mut escape = &dummy;
let res = try_open_atomic_update!(atomic_update, token => {
proof { escape = &token };
Tracked(Ok(Token::new()))
});
proof { escape.visit() };
}
} => Err(_err)
}
test_verify_one_file! {
#[test] atomic_function_lifetime_escape_ref
TOKEN_LIB.to_owned() + verus_code_str! {
pub fn atomic_function()
atomically (atomic_update) {
(x: &Token) -> (y: Commit<&Token>),
requires x.is_valid(),
ensures true,
},
{
let tracked dummy = Token::invalid();
let tracked mut escape = &dummy;
let res = try_open_atomic_update!(atomic_update, token => {
proof { escape = token };
Tracked(Commit(token))
});
proof { escape.visit() };
}
} => Err(_err)
}
static ATOMIC_FUNCTION: LazyLock<String> = LazyLock::new(|| {
TOKEN_LIB.to_owned()
+ verus_code_str! {
type FunAU = AtomicUpdate<Token, Result<Token, Token>, FunPred>;
pub fn atomic_function()
atomically (atomic_update) {
type FunPred,
(x: Token) -> (y: Result<Token, Token>),
requires x.is_valid(),
ensures match y {
Ok(t) => t.is_valid(),
Err(t) => t == x,
},
},
{
assume(atomic_update.resolves());
}
uninterp spec fn cond<X>(x: X) -> bool;
}
});
test_verify_one_file! {
#[test] atomic_function_def ATOMIC_FUNCTION.to_owned() => Ok(())
}
test_verify_one_file! {
#[test] atomic_function_predicate_type
ATOMIC_FUNCTION.to_owned() + verus_code_str! {
pub proof fn check_predicate_type(
tracked au: FunAU,
tracked x: Token,
tracked y: Result<Token, Token>,
)
ensures
au.req(x) <==> x.is_valid(),
au.ens(x, y) <==> match y {
Ok(t) => t.is_valid(),
Err(t) => t == x,
},
au.outer_mask().is_empty(),
au.inner_mask().is_empty(),
{}
}
=> Ok(())
}
test_verify_one_file! {
#[test] atomic_call_assume_both
ATOMIC_FUNCTION.to_owned() + verus_code_str! {
#[verifier::loop_isolation(false)]
fn atomic_function_call() {
atomic_function() atomically loop |update| {
let tracked res: Result<Token, Token> = update(Token::new());
if cond(res) {
assume(res.branch() == UpdateControlFlow::Commit);
break;
} else {
assume(res.branch() == UpdateControlFlow::Abort);
continue;
}
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] atomic_call_fail_commit
ATOMIC_FUNCTION.to_owned() + verus_code_str! {
#[verifier::loop_isolation(false)]
fn atomic_function_call() {
atomic_function() atomically loop |update| {
let tracked res: Result<Token, Token> = update(Token::new());
if cond(res) {
break;
} else {
assume(res.branch() == UpdateControlFlow::Abort);
continue;
}
}
}
} => Err(err) => assert_any_vir_error_msg(err, "cannot show the atomic update was committed")
}
test_verify_one_file! {
#[test] atomic_call_fail_abort
ATOMIC_FUNCTION.to_owned() + verus_code_str! {
#[verifier::loop_isolation(false)]
fn atomic_function_call() {
atomic_function() atomically loop |update| {
let tracked res: Result<Token, Token> = update(Token::new());
if cond(res) {
assume(res.branch() == UpdateControlFlow::Commit);
break;
} else {
continue;
}
}
}
} => Err(err) => assert_any_vir_error_msg(err, "cannot show the atomic update was aborted")
}
test_verify_one_file! {
#[test] atomic_call_fail_abort_implicit
ATOMIC_FUNCTION.to_owned() + verus_code_str! {
#[verifier::loop_isolation(false)]
fn atomic_function_call() {
atomic_function() atomically loop |update| {
let tracked res: Result<Token, Token> = update(Token::new());
if cond(res) {
assume(res.branch() == UpdateControlFlow::Commit);
break;
}
}
}
} => Err(err) => assert_any_vir_error_msg(err, "cannot show the atomic update was aborted")
}
test_verify_one_file! {
#[test] atomic_call_no_update
ATOMIC_FUNCTION.to_owned() + verus_code_str! {
#[verifier::loop_isolation(false)]
fn atomic_function_call() {
atomic_function() atomically loop |update| {}
}
} => Err(err) => assert_any_vir_error_msg(err, "function must be called in `atomically` block")
}
test_verify_one_file! {
#[test] atomic_call_multiple_updates
ATOMIC_FUNCTION.to_owned() + verus_code_str! {
#[verifier::loop_isolation(false)]
fn atomic_function_call() {
atomic_function() atomically loop |update| {
let tracked _ = update(Token::new());
let tracked _ = update(Token::new());
}
}
} => Err(err) => assert_any_vir_error_msg(err, "function must be called exactly once in `atomically` block")
}
test_verify_one_file! {
#[test] atomic_call_success
ATOMIC_FUNCTION.to_owned() + verus_code_str! {
#[verifier::loop_isolation(false)]
fn atomic_function_call() {
let tracked mut token = Token::new();
atomic_function() atomically loop |update|
invariant token.is_valid(),
{
let tracked res: Result<Token, Token> = update(token);
match res {
Ok(t) => break,
Err(t) => token = t,
}
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] atomic_call_success_loop_isolation
ATOMIC_FUNCTION.to_owned() + verus_code_str! {
#[verifier::loop_isolation(true)]
fn atomic_function_call() {
let tracked mut token = Token::new();
atomic_function() atomically loop |update| -> (au: FunAU)
invariant token.is_valid(),
{
let tracked res: Result<Token, Token> = update(token);
match res {
Ok(t) => {
assert(res.branch() == UpdateControlFlow::Commit);
break;
}
Err(t) => {
assert(res.branch() == UpdateControlFlow::Abort);
token = t;
continue;
}
}
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] atomic_call_with_labels
ATOMIC_FUNCTION.to_owned() + verus_code_str! {
#[verifier::loop_isolation(false)]
fn atomic_function_call() {
let tracked mut token = Token::new();
atomic_function() 'label: atomically loop |update| -> (au: FunAU)
invariant token.is_valid(),
{
let tracked res = update(token);
match res {
Ok(t) => break 'label,
Err(t) => {
token = t;
continue 'label;
}
}
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] atomic_call_fail_atomic_pre
ATOMIC_FUNCTION.to_owned() + verus_code_str! {
#[verifier::loop_isolation(false)]
fn atomic_function_call() {
atomic_function() atomically loop |update| {
let tracked _ = update(Token::invalid());
assume(false);
break;
}
}
} => Err(err) => assert_vir_error_msg(err, "cannot show atomic precondition holds before update function")
}
test_verify_one_file! {
#[test] atomic_call_missing_loop
ATOMIC_FUNCTION.to_owned() + verus_code_str! {
#[verifier::loop_isolation(false)]
fn atomic_function_call() {
let tracked mut token = Token::new();
atomic_function() atomically |update| {
update(token);
}
}
} => Err(err) => assert_any_vir_error_msg(err, "cannot show atomic update was committed")
}
test_verify_one_file! {
#[test] atomic_call_misplaced_invariant
ATOMIC_FUNCTION.to_owned() + verus_code_str! {
#[verifier::loop_isolation(false)]
fn atomic_function_call() {
let tracked mut token = Token::new();
atomic_function() atomically |update|
invariant token.is_valid(),
{
update(token);
assume(false);
}
}
} => Err(err) => assert_any_vir_error_msg(err, "invariants are only effective on `atomically loop` function calls")
}
test_verify_one_file! {
#[test] atomic_spec_empty
verus_code! {
use vstd::atomic::*;
pub exec fn atomic_function()
atomically (au) {},
{
try_open_atomic_update!(au, _unit => {
Tracked(())
});
}
} => Ok(())
}
test_verify_one_file! {
#[test] atomic_spec_check_defaults
verus_code! {
use vstd::atomic::*;
pub exec fn atomic_function()
atomically (au) {
type PredType,
},
{
try_open_atomic_update!(au, _unit => {
Tracked(())
});
}
pub proof fn check_predicate_type(
tracked au: AtomicUpdate<(), (), PredType>,
tracked x: (),
tracked y: (),
)
ensures
au.req(x),
au.ens(x, y),
au.outer_mask().is_empty(),
au.inner_mask().is_empty(),
{}
} => Ok(())
}
test_verify_one_file! {
#[test] atomic_spec_private_ensures_callee
verus_code! {
use vstd::*;
use vstd::prelude::*;
use vstd::atomic::*;
pub exec fn atomic_function() -> (out: u32)
atomically (au) {
type FunctionPred,
(x: u32) -> (y: Commit<u32>),
requires x == 2,
ensures y@ == 3,
},
ensures out == x + y@,
{
try_open_atomic_update!(au, value => {
Tracked(Commit((value + 1_u32) as u32))
});
assert(au.input() == 2);
assert(au.output()@ == 3);
return 5;
}
} => Ok(())
}
test_verify_one_file! {
#[test] atomic_spec_private_ensures_caller
verus_code! {
use vstd::*;
use vstd::prelude::*;
use vstd::atomic::*;
pub exec fn atomic_function() -> (out: u32)
atomically (au) {
type FunctionPred,
(x: u32) -> (y: Commit<u32>),
ensures x < y@,
},
ensures out == y@,
{
assume(false);
unreached()
}
#[verifier::loop_isolation(false)]
pub exec fn other_function() {
let tracked mut value: u32 = 5;
let out = atomic_function() atomically loop |update|
invariant value == 5,
{
let tracked Commit(next) = update(value);
value = next;
break;
};
assert(out > 5);
}
} => Ok(())
}
test_verify_one_file! {
#[test] previously_unsound_1
TOKEN_LIB.to_owned() + verus_code_str! {
pub exec fn atomic_function()
atomically (au) {
type FunctionPred,
(x: Token) -> (y: Commit<Token>),
},
requires au.resolves(), // !!!
{}
#[verifier::loop_isolation(false)]
pub exec fn client() {
let tracked mut token = Token::new();
atomic_function() atomically loop |update| -> (au) {
assert(au.req(token));
let tracked Commit(new_token) = update(token);
assert(au.ens(token, Commit(new_token)));
assert(au.resolves());
break
};
}
} => Err(err) => assert_vir_error_msg(err, "precondition not satisfied")
}
test_verify_one_file! {
#[test] previously_unsound_2
verus_code! {
use vstd::*;
use vstd::prelude::*;
use vstd::atomic::*;
pub exec fn atomic_function()
atomically (_au) {
requires true,
ensures false,
},
requires false,
ensures true,
{}
#[verifier::loop_isolation(false)]
pub exec fn client() {
atomic_function() atomically |update| {
update(())
};
}
} => Err(err) => assert_vir_error_msg(err, "precondition not satisfied")
}
test_verify_one_file! {
#[test] bad_self_type
code! {
use vstd::prelude::*;
use vstd::atomic::*;
struct Stuff;
struct Thing;
impl Thing {
verus! {
fn method(self)
atomically (au) {
type PredType,
(input: Stuff) -> (output: Commit<Stuff>),
}
{}
}
}
} => Err(err) => assert!(err.errors[0].message.contains("failed to resolve `self` type"))
}
test_verify_one_file! {
#[test] args_in_mask
verus_code! {
use vstd::prelude::*;
use vstd::atomic::*;
pub uninterp spec fn ns<X>(x: X) -> int;
fn function(num: i32, text: &str)
atomically (au) {
outer_mask any / [ns(num)],
inner_mask [ns(text)],
},
{
assume(false);
}
} => Ok(())
}