Skip to content

Commit ccc172b

Browse files
authored
Update test small_slice_eq (#3618)
Update the harness functions with `kani::any` of vectors, and use memory predicates `smae_allocation` in the loop invariants. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
1 parent 0896568 commit ccc172b

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

tests/expected/loop-contract/small_slice_eq.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,25 @@
66
// Modifications Copyright Kani Contributors
77
// See GitHub history for details.
88

9-
// kani-flags: -Z loop-contracts --enable-unstable --cbmc-args --arrays-uf-always --no-standard-checks --object-bits 8
9+
// kani-flags: -Z loop-contracts -Z mem-predicates --enable-unstable --cbmc-args --object-bits 8
1010

11-
//! Check if loop contracts are correctly applied. The flag --no-standard-checks should be
12-
//! removed once same_object predicate is supported in loop contracts.
11+
//! Check if loop contracts are correctly applied.
1312
1413
#![feature(stmt_expr_attributes)]
1514
#![feature(proc_macro_hygiene)]
1615
#![feature(ptr_sub_ptr)]
16+
17+
extern crate kani;
18+
19+
use kani::mem::same_allocation;
20+
1721
unsafe fn small_slice_eq(x: &[u8], y: &[u8]) -> bool {
1822
debug_assert_eq!(x.len(), y.len());
1923
unsafe {
2024
let (mut px, mut py) = (x.as_ptr(), y.as_ptr());
2125
let (pxend, pyend) = (px.add(x.len() - 4), py.add(y.len() - 4));
22-
#[kani::loop_invariant( px as isize >= x.as_ptr() as isize
26+
#[kani::loop_invariant( same_allocation(x.as_ptr(), px) && same_allocation(y.as_ptr(), py)
27+
&& px as isize >= x.as_ptr() as isize
2328
&& py as isize >= y.as_ptr() as isize
2429
&& px as isize - x.as_ptr() as isize == (py as isize - y.as_ptr() as isize))]
2530
while px < pxend {
@@ -43,9 +48,10 @@ fn small_slice_eq_harness() {
4348
// because DFCC contract enforcement assumes that a definition for `free`
4449
// exists.
4550
let _ = Box::new(10);
46-
let mut a = [1; 2000];
47-
let mut b = [1; 2000];
51+
const ARR_SIZE: usize = 2000;
52+
let x: [u8; ARR_SIZE] = kani::any();
53+
let y: [u8; ARR_SIZE] = kani::any();
4854
unsafe {
49-
small_slice_eq(&mut a, &mut b);
55+
small_slice_eq(&x, &y);
5056
}
5157
}

0 commit comments

Comments
 (0)