Skip to content

Commit 52fcc6c

Browse files
authored
Move any_slice_from_array to kani_core (#3646)
Move `any_slice_from_array` to `kani_core` so that we can call them in verify-rust-std (an example harness in model-checking/verify-rust-std#122). 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 b375b6b commit 52fcc6c

File tree

4 files changed

+48
-36
lines changed

4 files changed

+48
-36
lines changed

library/kani/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ mod concrete_playback;
2929
pub mod futures;
3030
pub mod invariant;
3131
pub mod shadow;
32-
pub mod slice;
3332
pub mod vec;
3433

3534
mod models;

library/kani/src/slice.rs

Lines changed: 0 additions & 35 deletions
This file was deleted.

library/kani_core/src/arbitrary.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//! TODO: Use this inside kani library so that we dont have to maintain two copies of the same proc macro for arbitrary.
1010
1111
mod pointer;
12+
mod slice;
1213

1314
#[macro_export]
1415
#[allow(clippy::crate_in_macro_def)]
@@ -188,6 +189,10 @@ macro_rules! generate_arbitrary {
188189
mod arbitrary_ptr {
189190
kani_core::ptr_generator!();
190191
}
192+
193+
pub mod slice {
194+
kani_core::slice_generator!();
195+
}
191196
};
192197
}
193198

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
//! This macro generates the logic required to generate slice with arbitrary contents and length.
5+
#[allow(clippy::crate_in_macro_def)]
6+
#[macro_export]
7+
macro_rules! slice_generator {
8+
() => {
9+
use crate::kani;
10+
11+
/// Given an array `arr` of length `LENGTH`, this function returns a **valid**
12+
/// slice of `arr` with non-deterministic start and end points. This is useful
13+
/// in situations where one wants to verify that all possible slices of a given
14+
/// array satisfy some property.
15+
///
16+
/// # Example:
17+
///
18+
/// ```no_run
19+
/// # fn foo(_: &[i32]) {}
20+
/// let arr = [1, 2, 3];
21+
/// let slice = kani::slice::any_slice_of_array(&arr);
22+
/// foo(slice); // where foo is a function that takes a slice and verifies a property about it
23+
/// ```
24+
pub fn any_slice_of_array<T, const LENGTH: usize>(arr: &[T; LENGTH]) -> &[T] {
25+
let (from, to) = any_range::<LENGTH>();
26+
&arr[from..to]
27+
}
28+
29+
/// A mutable version of the previous function
30+
pub fn any_slice_of_array_mut<T, const LENGTH: usize>(arr: &mut [T; LENGTH]) -> &mut [T] {
31+
let (from, to) = any_range::<LENGTH>();
32+
&mut arr[from..to]
33+
}
34+
35+
fn any_range<const LENGTH: usize>() -> (usize, usize) {
36+
let from: usize = kani::any();
37+
let to: usize = kani::any();
38+
kani::assume(to <= LENGTH);
39+
kani::assume(from <= to);
40+
(from, to)
41+
}
42+
};
43+
}

0 commit comments

Comments
 (0)