-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathcell_lib.rs
331 lines (283 loc) · 11.7 KB
/
cell_lib.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
#![feature(rustc_private)]
#[macro_use]
mod common;
use common::*;
const IMPORTS: &str = code_str! {
#[allow(unused_imports)] use vstd::*;
#[allow(unused_imports)] use vstd::pervasive::*;
#[allow(unused_imports)] use vstd::{cell::*};
#[allow(unused_imports)] use vstd::{raw_ptr::*};
#[allow(unused_imports)] use vstd::layout::*;
#[allow(unused_imports)] use vstd::{modes::*};
#[allow(unused_imports)] use vstd::prelude::*;
};
/// With contradiction_smoke_test, add a final `assert(false)` that is expected to fail at the end
/// of the test, as a cheap way to check that the trusted specs aren't contradictory
fn test_body(tests: &str, contradiction_smoke_test: bool) -> String {
IMPORTS.to_string()
+ " verus!{ fn test() {"
+ tests
+ if contradiction_smoke_test { "assert(false); // FAILS\n" } else { "" }
+ " } }"
}
const CELL_TEST: &str = code_str! {
let (cell, Tracked(mut token)) = PCell::<u32>::empty();
assert(equal(token.view().pcell, cell.id()));
assert(equal(token.view().value, Option::None));
cell.put(Tracked(&mut token), 5);
assert(equal(token.view().pcell, cell.id()));
assert(equal(token.view().value, Option::Some(5)));
let x = cell.replace(Tracked(&mut token), 7);
assert(equal(token.view().pcell, cell.id()));
assert(equal(token.view().value, Option::Some(7)));
assert(equal(x, 5));
let t = cell.borrow(Tracked(&token));
assert(equal(*t, 7));
let x = cell.take(Tracked(&mut token));
assert(equal(token.view().pcell, cell.id()));
assert(equal(token.view().value, Option::None));
assert(equal(x, 7));
};
test_verify_one_file! {
#[test] test_cell_pass test_body(CELL_TEST, false) => Ok(())
}
test_verify_one_file! {
#[test] test_cell_smoke test_body(CELL_TEST, true) => Err(e) => assert_one_fails(e)
}
const PTR_TEST: &str = code_str! {
assume(size_of::<u32>() == 4);
assume(align_of::<u32>() == 4);
layout_for_type_is_valid::<u32>();
let (ptr, Tracked(token), Tracked(dealloc)) = allocate(4, 4);
let tracked mut token = token.into_typed::<u32>(ptr as usize);
let ptr = ptr as *mut u32;
assert(equal(token.ptr(), ptr));
assert(equal(token.opt_value(), MemContents::Uninit));
ptr_mut_write(ptr, Tracked(&mut token), 5);
assert(equal(token.ptr(), ptr));
assert(equal(token.opt_value(), MemContents::Init(5)));
let t = ptr_ref(ptr, Tracked(&token));
assert(equal(*t, 5));
let x = ptr_mut_read(ptr, Tracked(&mut token));
assert(equal(token.ptr(), ptr));
assert(equal(token.opt_value(), MemContents::Uninit));
assert(equal(x, 5));
let tracked token = token.into_raw();
deallocate(ptr as *mut u8, 4, 4, Tracked(token), Tracked(dealloc));
};
test_verify_one_file! {
#[test] test_ptr_pass test_body(PTR_TEST, false) => Ok(())
}
test_verify_one_file! {
#[test] test_ptr_smoke test_body(PTR_TEST, true) => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] cell_mismatch_put IMPORTS.to_string() + verus_code_str! {
pub fn f() {
let (cell1, Tracked(mut token1)) = PCell::<u32>::empty();
let (cell2, Tracked(mut token2)) = PCell::<u32>::empty();
cell1.put(Tracked(&mut token2), 5); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] cell_mismatch_take IMPORTS.to_string() + verus_code_str! {
pub fn f() {
let (cell1, Tracked(mut token1)) = PCell::<u32>::empty();
let (cell2, Tracked(mut token2)) = PCell::<u32>::empty();
cell1.put(Tracked(&mut token1), 5);
cell2.put(Tracked(&mut token2), 5);
let x = cell1.take(Tracked(&mut token2)); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] cell_mismatch_replace IMPORTS.to_string() + verus_code_str! {
pub fn f() {
let (cell1, Tracked(mut token1)) = PCell::<u32>::empty();
let (cell2, Tracked(mut token2)) = PCell::<u32>::empty();
cell1.put(Tracked(&mut token1), 5);
cell2.put(Tracked(&mut token2), 5);
let x = cell1.replace(Tracked(&mut token2), 7); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] cell_mismatch_borrow IMPORTS.to_string() + verus_code_str! {
pub fn f() {
let (cell1, Tracked(mut token1)) = PCell::<u32>::empty();
let (cell2, Tracked(mut token2)) = PCell::<u32>::empty();
cell1.put(Tracked(&mut token1), 5);
cell2.put(Tracked(&mut token2), 5);
let x = cell1.borrow(Tracked(&token2)); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] cell_some_put IMPORTS.to_string() + verus_code_str! {
pub fn f() {
let (cell1, Tracked(mut token1)) = PCell::<u32>::empty();
cell1.put(Tracked(&mut token1), 7);
cell1.put(Tracked(&mut token1), 5); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] cell_none_take IMPORTS.to_string() + verus_code_str! {
pub fn f() {
let (cell1, Tracked(mut token1)) = PCell::<u32>::empty();
let x = cell1.take(Tracked(&mut token1)); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] cell_none_replace IMPORTS.to_string() + verus_code_str! {
pub fn f() {
let (cell1, Tracked(mut token1)) = PCell::<u32>::empty();
let x = cell1.replace(Tracked(&mut token1), 7); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] cell_none_borrow IMPORTS.to_string() + verus_code_str! {
pub fn f() {
let (cell1, Tracked(mut token1)) = PCell::<u32>::empty();
let x = cell1.borrow(Tracked(&token1)); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] ptr_mismatch_put IMPORTS.to_string() + verus_code_str! {
global layout u32 is size == 4, align == 4;
pub fn f() {
layout_for_type_is_valid::<u32>();
let (ptr1, Tracked(token1), Tracked(dealloc)) = allocate(4, 4);
let (ptr2, Tracked(token2), Tracked(dealloc)) = allocate(4, 4);
let tracked mut token1 = token1.into_typed::<u32>(ptr1 as usize);
let tracked mut token2 = token2.into_typed::<u32>(ptr2 as usize);
let ptr1 = ptr1 as *mut u32;
let ptr2 = ptr2 as *mut u32;
ptr_mut_write(ptr1, Tracked(&mut token2), 5); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] ptr_mismatch_take IMPORTS.to_string() + verus_code_str! {
global layout u32 is size == 4, align == 4;
pub fn f() {
layout_for_type_is_valid::<u32>();
let (ptr1, Tracked(token1), Tracked(dealloc)) = allocate(4, 4);
let (ptr2, Tracked(token2), Tracked(dealloc)) = allocate(4, 4);
let tracked mut token1 = token1.into_typed::<u32>(ptr1 as usize);
let tracked mut token2 = token2.into_typed::<u32>(ptr2 as usize);
let ptr1 = ptr1 as *mut u32;
let ptr2 = ptr2 as *mut u32;
ptr_mut_write(ptr1, Tracked(&mut token1), 5);
ptr_mut_write(ptr2, Tracked(&mut token2), 5);
let x = ptr_mut_read(ptr1, Tracked(&mut token2)); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] ptr_mismatch_borrow IMPORTS.to_string() + verus_code_str! {
global layout u32 is size == 4, align == 4;
pub fn f() {
layout_for_type_is_valid::<u32>();
let (ptr1, Tracked(token1), Tracked(dealloc)) = allocate(4, 4);
let (ptr2, Tracked(token2), Tracked(dealloc)) = allocate(4, 4);
let tracked mut token1 = token1.into_typed::<u32>(ptr1 as usize);
let tracked mut token2 = token2.into_typed::<u32>(ptr2 as usize);
let ptr1 = ptr1 as *mut u32;
let ptr2 = ptr2 as *mut u32;
ptr_mut_write(ptr1, Tracked(&mut token1), 5);
ptr_mut_write(ptr2, Tracked(&mut token2), 5);
let x = ptr_ref(ptr1, Tracked(&token2)); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] ptr_mismatch_dispose IMPORTS.to_string() + verus_code_str! {
global layout u32 is size == 4, align == 4;
pub fn f() {
layout_for_type_is_valid::<u32>();
let (ptr1, Tracked(mut token1), Tracked(dealloc1)) = allocate(4, 4);
let (ptr2, Tracked(mut token2), Tracked(dealloc2)) = allocate(4, 4);
deallocate(ptr1, 4, 4, Tracked(token2), Tracked(dealloc1)); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] ptr_mismatch_dispose2 IMPORTS.to_string() + verus_code_str! {
global layout u32 is size == 4, align == 4;
pub fn f() {
layout_for_type_is_valid::<u32>();
let (ptr1, Tracked(mut token1), Tracked(dealloc1)) = allocate(4, 4);
let (ptr2, Tracked(mut token2), Tracked(dealloc2)) = allocate(4, 4);
deallocate(ptr1, 4, 4, Tracked(token1), Tracked(dealloc2)); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] ptr_some_put IMPORTS.to_string() + verus_code_str! {
global layout u32 is size == 4, align == 4;
pub fn f() {
layout_for_type_is_valid::<u32>();
let (ptr1, Tracked(token1), Tracked(dealloc)) = allocate(4, 4);
let tracked mut token1 = token1.into_typed::<u32>(ptr1 as usize);
let ptr1 = ptr1 as *mut u32;
ptr_mut_write(ptr1, Tracked(&mut token1), 7);
ptr_mut_write(ptr1, Tracked(&mut token1), 5); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] ptr_none_take IMPORTS.to_string() + verus_code_str! {
global layout u32 is size == 4, align == 4;
pub fn f() {
layout_for_type_is_valid::<u32>();
let (ptr1, Tracked(token1), Tracked(dealloc)) = allocate(4, 4);
let tracked mut token1 = token1.into_typed::<u32>(ptr1 as usize);
let ptr1 = ptr1 as *mut u32;
let x = ptr_mut_read(ptr1, Tracked(&mut token1)); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] ptr_none_borrow IMPORTS.to_string() + verus_code_str! {
global layout u32 is size == 4, align == 4;
pub fn f() {
layout_for_type_is_valid::<u32>();
let (ptr1, Tracked(token1), Tracked(dealloc)) = allocate(4, 4);
let tracked mut token1 = token1.into_typed::<u32>(ptr1 as usize);
let ptr1 = ptr1 as *mut u32;
let x = ptr_ref(ptr1, Tracked(&token1)); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
// Test that cell::PointsTo<T> correctly inherits the Send and Sync properties of T
test_verify_one_file! {
#[test] permission_inherits_sync IMPORTS.to_string() + code_str! {
struct Foo {
i: u8,
}
impl !Sync for Foo { }
pub fn f<T: Sync>(t: T) {
}
pub fn foo(r: cell::PointsTo<Foo>) {
f(r);
}
} => Err(e) => assert_rust_error_msg(e, "`Foo` cannot be shared between threads safely")
}
test_verify_one_file! {
#[test] permission_inherits_send IMPORTS.to_string() + code_str! {
struct Foo {
i: u8,
}
impl !Send for Foo { }
pub fn f<T: Send>(t: T) {
}
pub fn foo(r: cell::PointsTo<Foo>) {
f(r);
}
} => Err(e) => assert_rust_error_msg(e, "`Foo` cannot be sent between threads safely")
}