-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathraw_ptrs.rs
288 lines (228 loc) · 8.35 KB
/
raw_ptrs.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
#![feature(rustc_private)]
#[macro_use]
mod common;
use common::*;
test_verify_one_file! {
#[test] basics verus_code! {
use vstd::prelude::*;
use vstd::raw_ptr::*;
fn test_mut_const_casts(x: *mut u8) {
let y = x as *const u8;
let z = y as *const u8;
assert(z == x);
assert(z == y);
}
fn test_addr_doesnt_imply_eq(x: *mut u8, y: *mut u8) {
assume([email protected] == [email protected]);
assert(x == y); // FAILS
}
fn test_view_does_imply_eq(x: *mut u8, y: *mut u8) {
assume(x@ == y@);
assert(x == y);
}
fn test_view_does_imply_eq_const(x: *const u8, y: *const u8) {
assume(x@ == y@);
assert(x == y);
}
fn test_null() {
let x: *const u8 = core::ptr::null();
let y: *mut u8 = core::ptr::null_mut();
assert(x == y);
assert([email protected] == 0);
}
fn test_manipulating(x: *mut u8, Tracked(pt): Tracked<PointsTo<u8>>) {
let tracked mut pt = pt;
assume(x == pt.ptr());
assume(pt.is_uninit());
ptr_mut_write(x, Tracked(&mut pt), 20);
assert(x == pt.ptr());
assert(pt.is_init());
assert(pt.value() == 20);
let val = ptr_ref(x, Tracked(&pt));
assert(val == 20);
let val = ptr_mut_read(x, Tracked(&mut pt));
assert(val == 20);
assert(x == pt.ptr());
assert(pt.is_uninit());
}
fn test_manipulating2(x: *mut u8, Tracked(pt): Tracked<PointsTo<u8>>) {
let tracked mut pt = pt;
assume(x == pt.ptr());
assume(pt.is_uninit());
ptr_mut_write(x, Tracked(&mut pt), 20);
assert(x == pt.ptr());
assert(pt.is_init());
assert(pt.value() == 20);
let val = ptr_ref(x, Tracked(&pt));
assert(val == 20);
let val = ptr_mut_read(x, Tracked(&mut pt));
assert(val == 20);
assert(x == pt.ptr());
assert(pt.is_uninit());
assert(false); // FAILS
}
fn test_ptr_mut_write_different(x: *mut u8, Tracked(pt): Tracked<PointsTo<u8>>) {
let tracked mut pt = pt;
assume(pt.is_uninit());
ptr_mut_write(x, Tracked(&mut pt), 20); // FAILS
}
fn test_ptr_mut_read_different(x: *mut u8, Tracked(pt): Tracked<PointsTo<u8>>) {
let tracked mut pt = pt;
assume(pt.is_init());
let _ = ptr_mut_read(x, Tracked(&mut pt)); // FAILS
}
fn test_ptr_ref_different(x: *mut u8, Tracked(pt): Tracked<PointsTo<u8>>) {
assume(pt.is_init());
let _ = ptr_ref(x, Tracked(&pt)); // FAILS
}
fn test_ptr_mut_read_uninit(x: *mut u8, Tracked(pt): Tracked<PointsTo<u8>>) {
let tracked mut pt = pt;
assume(pt.ptr() == x);
let _ = ptr_mut_read(x, Tracked(&mut pt)); // FAILS
}
fn test_ptr_ref_uninit(x: *mut u8, Tracked(pt): Tracked<PointsTo<u8>>) {
assume(pt.ptr() == x);
let _ = ptr_ref(x, Tracked(&pt)); // FAILS
}
fn cast_test(x: *mut u8) {
let y = x as *mut u16;
assert([email protected] == [email protected]);
assert([email protected] == [email protected]);
assert([email protected] == vstd::raw_ptr::Metadata::Thin);
}
fn cast_test2(x: *mut [u8]) {
let y = x as *mut u16;
assert([email protected] == [email protected]);
assert([email protected] == [email protected]);
assert([email protected] == vstd::raw_ptr::Metadata::Thin);
}
fn cast_test3(x: *mut [u64; 16]) {
let y = x as *mut [u64];
assert([email protected] == [email protected]);
assert([email protected] == [email protected]);
assert([email protected] == vstd::raw_ptr::Metadata::Length(16));
}
proof fn cast_proof_test(x: *mut u8) {
let y = x as *mut u16;
assert([email protected] == [email protected]);
assert([email protected] == [email protected]);
assert([email protected] == vstd::raw_ptr::Metadata::Thin);
}
proof fn cast_proof_test2(x: *mut [u8]) {
let y = x as *mut u16;
assert([email protected] == [email protected]);
assert([email protected] == [email protected]);
assert([email protected] == vstd::raw_ptr::Metadata::Thin);
}
proof fn cast_proof_test3(x: *mut [u64; 16]) {
let y = x as *mut [u64];
assert([email protected] == [email protected]);
assert([email protected] == [email protected]);
assert([email protected] == vstd::raw_ptr::Metadata::Length(16));
}
fn test_strict_provenance(a: *mut u64) {
let ad = a.addr();
assert(ad == [email protected]);
let b = a.with_addr(7);
assert([email protected] == 7);
assert([email protected] == [email protected]);
assert([email protected] == [email protected]);
assert(a == b); // FAILS
}
fn test_strict_provenance_const(a: *const u64) {
let ad = a.addr();
assert(ad == [email protected]);
let b = a.with_addr(7);
assert([email protected] == 7);
assert([email protected] == [email protected]);
assert([email protected] == [email protected]);
}
} => Err(err) => assert_fails(err, 8)
}
test_verify_one_file! {
#[test] pointer_cast_to_ints verus_code! {
use vstd::prelude::*;
use vstd::raw_ptr::*;
proof fn test<T>(x: *mut T) {
assert([email protected] == x as int);
assert([email protected] == x as nat);
assert([email protected] == x as usize);
assert([email protected] as u16 == x as u16);
}
proof fn test_fails<T>(x: *mut T) {
assert([email protected] == x as u16); // FAILS
}
proof fn test_fails2<T>(x: *mut T) {
assert([email protected] == x as isize); // FAILS
}
fn exec_test<T>(x: *mut T) {
let x1 = x as usize;
assert([email protected] == x1);
let x2 = x as u16;
assert([email protected] as u16 == x2);
}
fn exec_test_fails<T>(x: *mut T) {
let x1 = x as u16;
assert([email protected] == x1); // FAILS
}
fn exec_test_fails2<T>(x: *mut T) {
let x1 = x as isize;
assert([email protected] == x1); // FAILS
}
} => Err(err) => assert_fails(err, 4)
}
test_verify_one_file! {
#[test] pointer_exec_eq_is_not_spec_eq verus_code! {
fn test_const_eq(x: *const u8, y: *const u8) {
if x == y {
assert(x == y); // FAILS
}
}
fn test_mut_eq(x: *mut u8, y: *mut u8) {
if x == y {
assert(x == y); // FAILS
}
}
} => Err(err) => assert_vir_error_msg(err, "The verifier does not yet support the following Rust feature: ==/!= for non smt equality types")
}
test_verify_one_file! {
#[test] points_to_move_error verus_code! {
use vstd::prelude::*;
use vstd::raw_ptr::*;
fn test(Tracked(pt): Tracked<PointsTo<u8>>) {
}
fn test2(Tracked(pt): Tracked<PointsTo<u8>>) {
test(Tracked(pt));
test(Tracked(pt));
}
} => Err(err) => assert_vir_error_msg(err, "use of moved value: `pt`")
}
test_verify_one_file! {
#[test] ptr_ref_lifetime_error verus_code! {
use vstd::prelude::*;
use vstd::raw_ptr::*;
fn test(Tracked(pt): Tracked<PointsTo<u8>>) {
}
fn test2(x: *mut u8, Tracked(pt): Tracked<PointsTo<u8>>) {
assume(pt.is_init());
assume(pt.ptr() == x);
let y = ptr_ref(x, Tracked(&pt));
test(Tracked(pt));
let z = *y;
}
} => Err(err) => assert_vir_error_msg(err, "cannot move out of `pt` because it is borrowed")
}
test_verify_one_file! {
#[test] not_supported_int_to_ptr_cast verus_code! {
fn test(x: usize) {
let y = x as *mut u8;
}
} => Err(err) => assert_vir_error_msg(err, "Verus does not support this cast")
}
test_verify_one_file! {
#[test] not_supported_deref_ptr verus_code! {
pub fn run(x: *mut u8) {
unsafe { let y = *x; }
}
} => Err(err) => assert_vir_error_msg(err, "The verifier does not yet support the following Rust feature: dereferencing a raw pointer")
}