-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathoverflow.rs
332 lines (277 loc) · 8.31 KB
/
overflow.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
#![feature(rustc_private)]
#[macro_use]
mod common;
use common::*;
test_verify_one_file! {
#[test] test_overflow_spec_pass verus_code! {
use vstd::*;
fn test(a: u64) {
let ghost mut j: u64 = a;
proof { j = add(j, 2); }
assert(j == add(a, 2));
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_overflow_spec_fails_1 verus_code! {
proof fn test(a: u64) {
let mut j = a;
j = add(j, 2);
assert(j == a as nat + 2); // FAILS
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_overflow_fails_1 verus_code! {
fn test(a: u64) {
let mut j = a;
j = j + 2; // FAILS
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_overflow_spec_fails_2 verus_code! {
use vstd::*;
fn test(a: u64) {
let ghost mut j: u64 = a;
proof {
j = add(j, 2);
j = add(j, 2);
}
assert(j == a + 4); // FAILS
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_overflow_fails_2 verus_code! {
fn test(a: u64) {
let mut j = a;
j = j + 2; // FAILS
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_divide_by_zero verus_code! {
fn ok(a: u8, b: u8)
requires b != 0
{
let x = a / b;
let y = a % b;
}
fn fail1(a: u8, b: u8) {
let x = a / b; // FAILS
}
fn fail2(a: u8, b: u8) {
let y = a % b; // FAILS
}
} => Err(e) => assert_fails(e, 2)
}
test_verify_one_file! {
#[test] test_const_ok verus_code! {
const C: u8 = 254 + 1;
} => Ok(())
}
test_verify_one_file! {
#[test] test_const_fail verus_code! {
const C: u8 = 255 + 1 /* FAILS */;
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_static_fail verus_code! {
exec static C: u8 = 255 + 1 /* FAILS */;
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_literal_out_of_range verus_code! {
const C: u8 = 256 - 1;
} => Err(err) => assert_rust_error_msg(err, "evaluation of constant value failed")
}
test_verify_one_file! {
#[test] test_overflow_fails_usize verus_code! {
fn test(a: usize) -> usize {
let b = a + 1; // FAILS
b
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_overflow_ensures_pass verus_code! {
fn test(a: usize) -> (r: usize)
requires a < 30
ensures r == a + 1
{
let b = a + 1;
b
}
} => Ok(())
}
test_verify_one_file! {
#[test] underflow verus_code! {
fn underflow() {
let mut a: u64 = 0;
a = a - 1; // FAILS
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] bit_shift_overflow verus_code! {
fn test_overflow_right_shift() {
let x: u16 = 0;
let y: u16 = 16;
let z = x >> y; // FAILS
}
fn test_overflow_right_shift2() {
let x: u16 = 0;
let y: u16 = 17;
let z = x >> y; // FAILS
}
fn test_overflow_right_shift3() {
let x: u16 = 0;
let y: u16 = 15;
// this one is ok
let z = x >> y;
}
fn test_overflow_left_arg_doesnt_matter() {
let x: u16 = 16;
let y: u16 = 5;
// this one is ok
let z = x >> y;
}
fn test_overflow_left_shift() {
let x: u16 = 0;
let y: u16 = 16;
let z = x << y; // FAILS
}
fn test_usize_overflow() {
let x: usize = 0;
let y: usize = 32;
let z = x << y; // FAILS
}
fn test_usize_overflow2() {
let x: usize = 0;
let y: usize = usize::BITS as usize;
let z = x << y; // FAILS
}
fn test_usize_overflow3() {
let x: usize = 0;
let y: usize = (usize::BITS - 1) as usize;
// this is ok
let z = x << y;
}
} => Err(e) => assert_fails(e, 5)
}
test_verify_one_file! {
#[test] bit_shift_width_mismatch verus_code! {
fn test_underflow() {
// This type mismatch is unsupported; however, if it is ever supported,
// it should be an overflow error.
let x: u16 = 0;
let y: u32 = 40;
let z = x << y; // FAILS
}
} => Err(e) => assert_fails(e, 1)
}
test_verify_one_file! {
#[test] bit_shift_width_mismatch_signed verus_code! {
fn test_underflow() {
// This type mismatch is unsupported; however, if it is ever supported,
// it should be an underflow error.
let x: u16 = 0;
let y: i32 = -1;
let z = x << y; // FAILS
}
} => Err(e) => assert_fails(e, 1)
}
test_verify_one_file! {
#[test] bit_shift_unsigned_shift_signed verus_code! {
fn test_underflow() {
let x: u16 = 0;
let y: i16 = -1;
let z = x << y; // FAILS
}
} => Err(e) => assert_vir_error_msg(e, "possible bit shift underflow/overflow")
}
test_verify_one_file! {
#[test] bit_shift_underflow verus_code! {
fn test_underflow() {
let x: i16 = 0;
let y: i16 = -1;
let z = x << y; // FAILS
}
} => Err(e) => assert_vir_error_msg(e, "possible bit shift underflow/overflow")
}
test_verify_one_file_with_options! {
#[test] bit_shift_overflow_arch32 ["vstd"] => verus_code! {
global size_of usize == 4;
fn test_usize_overflow() {
let x: usize = 0;
let y: usize = 32;
let z = x << y; // FAILS
}
fn test_usize_overflow2() {
let x: usize = 0;
let y: usize = 31;
let z = x << y;
}
} => Err(e) => assert_fails(e, 1)
}
test_verify_one_file_with_options! {
#[test] bit_shift_overflow_arch64 ["vstd"] => verus_code! {
global size_of usize == 8;
fn test_usize_overflow() {
let x: usize = 0;
let y: usize = 64;
let z = x << y; // FAILS
}
fn test_usize_overflow2() {
let x: usize = 0;
let y: usize = 63;
let z = x << y;
}
} => Err(e) => assert_fails(e, 1)
}
test_verify_one_file! {
#[test] spec_bitshift_defined_for_larger_ints verus_code! {
fn test() {
// Unlike Rust, Z3's bitvector reasoning allows the right-hand side to
// be out-of-range
assert(1u8 >> 20u8 == 0u8) by(bit_vector);
}
} => Ok(())
}
test_verify_one_file! {
#[test] spec_bitshift_negative verus_code! {
// TODO bit_vector solver currently doesn't support bit-shifts by signed types.
// However, if it ever does, then this test case should test that Verus
// checks for underflow (maybe as a recommends check?)
// If our solver interprets (-1i8) as 255 then the following assert would pass,
// but it may be preferable to leave shifts-by-negatives underspecified?
// That can be decided later, though.
fn test() {
assert(1i8 >> (-1i8) == 0i8) by(bit_vector); // FAILS
}
} => Err(e) => assert_vir_error_msg(e, "bit-shift with possibly negative shift")
}
test_verify_one_file! {
#[test] nonlinear_ops_dont_overflow_unsigned verus_code!{
fn test_mul(x: u16, y: u16) {
assert(((x as nat) * (y as nat)) as int == (x as int) * (y as int));
}
fn test_div(a: u32, b: u32)
requires b != 0
{
let x = a / b;
assert(x as int == a as int / b as int);
}
fn test_mod(a: u32, b: u32)
requires b != 0
{
let x = a % b;
assert(x as int == a as int % b as int);
}
// Make sure axiom about % properly accounts for 0:
proof fn test_mod_by_0(a: u32, b: u32) {
assert((a as int % 0 as int) < 0); // FAILS
}
} => Err(e) => assert_one_fails(e)
}