forked from model-checking/kani
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move any_slice_from_array to kani_core (model-checking#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.
- Loading branch information
Showing
4 changed files
with
48 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
//! This macro generates the logic required to generate slice with arbitrary contents and length. | ||
#[allow(clippy::crate_in_macro_def)] | ||
#[macro_export] | ||
macro_rules! slice_generator { | ||
() => { | ||
use crate::kani; | ||
|
||
/// Given an array `arr` of length `LENGTH`, this function returns a **valid** | ||
/// slice of `arr` with non-deterministic start and end points. This is useful | ||
/// in situations where one wants to verify that all possible slices of a given | ||
/// array satisfy some property. | ||
/// | ||
/// # Example: | ||
/// | ||
/// ```no_run | ||
/// # fn foo(_: &[i32]) {} | ||
/// let arr = [1, 2, 3]; | ||
/// let slice = kani::slice::any_slice_of_array(&arr); | ||
/// foo(slice); // where foo is a function that takes a slice and verifies a property about it | ||
/// ``` | ||
pub fn any_slice_of_array<T, const LENGTH: usize>(arr: &[T; LENGTH]) -> &[T] { | ||
let (from, to) = any_range::<LENGTH>(); | ||
&arr[from..to] | ||
} | ||
|
||
/// A mutable version of the previous function | ||
pub fn any_slice_of_array_mut<T, const LENGTH: usize>(arr: &mut [T; LENGTH]) -> &mut [T] { | ||
let (from, to) = any_range::<LENGTH>(); | ||
&mut arr[from..to] | ||
} | ||
|
||
fn any_range<const LENGTH: usize>() -> (usize, usize) { | ||
let from: usize = kani::any(); | ||
let to: usize = kani::any(); | ||
kani::assume(to <= LENGTH); | ||
kani::assume(from <= to); | ||
(from, to) | ||
} | ||
}; | ||
} |