Skip to content

Commit 7ec618e

Browse files
bdalrhmtedinski
authored andcommitted
Improve some regression tests and add some more. (rust-lang#154)
* Improve some regression tests and add some more. * Add more assertions to a test. * Revert changes to trait regressions. * Add comments explaining dereferencing raw address test.
1 parent ea22451 commit 7ec618e

File tree

14 files changed

+101
-59
lines changed

14 files changed

+101
-59
lines changed

rust-tests/cbmc-reg/BitManipulation/Stable/fixme_main_fail.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn main() {
1010
let num_leading = x.leading_zeros();
1111
let rotated_num = x.rotate_left(3);
1212

13-
assert!(num_trailing == 3);
13+
assert!(num_trailing == 3); // fails because of https://github.com/model-checking/rmc/issues/26
1414
assert!(num_leading == 2);
1515
assert!(rotated_num == 0b1100_0001_u8);
1616
}

rust-tests/cbmc-reg/BitManipulation/Unstable/fixme_main_fail.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fn main() {
1414
let num_leading = ctlz(v8);
1515
let num_trailing_nz = unsafe { cttz_nonzero(v8) };
1616

17+
// fail because of https://github.com/model-checking/rmc/issues/26
1718
assert!(nttz8 == 3);
1819
assert!(nttz16 == 11);
1920
assert!(nttz32 == 27);

rust-tests/cbmc-reg/CodegenStatic/main.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
static STATIC: [&str; 1] = ["FOO"];
44
fn main() {
5-
let _x = STATIC[0];
5+
let x = STATIC[0];
6+
let bytes = x.as_bytes();
7+
assert!(bytes.len() == 3);
8+
assert!(bytes[0] == b'F');
9+
assert!(bytes[1] == b'O');
10+
assert!(bytes[2] == b'O');
611
}

rust-tests/cbmc-reg/Enum/result3.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
#[derive(Debug, PartialEq)]
4+
pub enum Unit {
5+
Unit,
6+
}
7+
8+
fn __nondet<T>() -> T {
9+
unimplemented!()
10+
}
11+
12+
fn foo(input: &Result<u32, Unit>) -> u32 {
13+
if let Ok(num) = input { *num } else { 3 }
14+
}
15+
16+
pub fn main() {
17+
let input: Result<u32, Unit> = __nondet();
18+
let x = foo(&input);
19+
assert!(x == 3 || input != Err(Unit::Unit));
20+
let input: Result<u32, Unit> = if __nondet() { Ok(0) } else { Err(Unit::Unit) };
21+
let x = foo(&input);
22+
assert!(x != 3 || input == Err(Unit::Unit));
23+
assert!(x != 0 || input == Ok(0));
24+
}

rust-tests/cbmc-reg/ExactDiv/main.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,5 @@ fn main() {
66
let a: u8 = 8;
77
let b: u8 = 4;
88
let i = unsafe { std::intrinsics::exact_div(a, b) };
9+
assert!(i == 2);
910
}
10-
11-
// let mut iter = "a".split_whitespace();
12-
// iter.next();

rust-tests/cbmc-reg/FatPointers/boxslice1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() {
88
// This vector of bytes is used to initialize a Box<[u8; 4]>
99
let sparkle_heart_vec = vec![240, 159, 146, 150];
1010

11-
// This transformer produces a Box<[u8>]
11+
// This transformer produces a Box<[u8]>
1212
let _sparkle_heart_str = str::from_utf8(&sparkle_heart_vec);
1313

1414
// see boxslice2_fail.rs for an attempt to test sparkle_heart_str

rust-tests/cbmc-reg/FatPointers/boxslice2_fail.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() {
88
// This vector of bytes is used to initialize a Box<[u8; 4]>
99
let sparkle_heart_vec = vec![240, 159, 146, 150];
1010

11-
// This transformer produces a Box<[u8>]
11+
// This transformer produces a Box<[u8]>
1212
let sparkle_heart_str = str::from_utf8(&sparkle_heart_vec);
1313

1414
// This match statement generates failures even though
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
fn __nondet<T>() -> T {
4+
unimplemented!()
5+
}
6+
7+
macro_rules! test_floats {
8+
($ty:ty) => {
9+
let a: $ty = __nondet();
10+
let b = a / 2.0;
11+
12+
if a < 0.0 {
13+
assert!(a <= b);
14+
} else if a >= 0.0 {
15+
assert!(a >= b);
16+
}
17+
18+
let c = b * 2.0;
19+
// general/infinity Close but not exact NAN
20+
assert!(a == c || a - c < 0.00000001 || c - a < 0.00000001 || c * 0.0 != 0.0);
21+
22+
let d: $ty = 0.0;
23+
assert!(d + 1.0 > 0.0);
24+
assert!(d - 1.0 < 0.0);
25+
};
26+
}
27+
28+
fn main() {
29+
assert!(1.1 == 1.1 * 1.0);
30+
assert!(1.1 != 1.11 / 1.0);
31+
32+
test_floats!(f32);
33+
test_floats!(f64);
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
fn main() {
5+
let address = 0x01234usize;
6+
let ptr = address as *mut i32;
7+
// pointers can only be dereferenced inside unsafe blocks
8+
unsafe {
9+
// dereferencing a random address in memory will probably crash the program
10+
*ptr = 1; // rmc verification succeeds without generating any assertions
11+
};
12+
}

rust-tests/cbmc-reg/SaturatingIntrinsics/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn __nondet<T>() -> T {
88
}
99

1010
macro_rules! test_saturating_intrinsics {
11-
($ty:tt) => {
11+
($ty:ty) => {
1212
let v: $ty = __nondet();
1313
let w: $ty = __nondet();
1414
let result = intrinsics::saturating_add(v, w);

rust-tests/cbmc-reg/Slice/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
/// rmc main.rs -- --unwind 6 --unwinding-assertions
34
fn main() {
45
let name: &str = "hello";
6+
assert!(name == "hello");
57
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
use std::slice;
4+
5+
fn __nondet<T>() -> T {
6+
unimplemented!()
7+
}
8+
9+
// From Listing 19-7: Creating a slice from an arbitrary memory location. https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html
10+
fn main() {
11+
let address = 0x01234usize;
12+
let r = address as *mut i32;
13+
let slice: &mut [i32] = unsafe { slice::from_raw_parts_mut(r, 10000) };
14+
// the behavior is undefined when the slice is used
15+
slice[9999] = 0; // verification succeeds
16+
assert!(slice[9999] == 0); // verification succeeds
17+
}

rust-tests/cbmc-reg/f32/main.rs

-27
This file was deleted.

rust-tests/cbmc-reg/f64/main.rs

-24
This file was deleted.

0 commit comments

Comments
 (0)