Skip to content

Add loop_invariant and harness for array reverse #430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,13 @@ impl<T> [T] {
let (b, _) = b.split_at_mut(n);

let mut i = 0;
#[safety::loop_invariant(i <= n)]
#[cfg_attr(kani,
kani::loop_modifies(
unsafe {slice::from_raw_parts_mut(a.as_mut_ptr(), n)},
unsafe {slice::from_raw_parts_mut(b.as_mut_ptr(), n)},
&i)
)]
Comment on lines +1015 to +1020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having this cfg_attr, can you define loop_modifies in the safety module like we've done fo r the other attributes?

while i < n {
mem::swap(&mut a[i], &mut b[n - 1 - i]);
i += 1;
Expand Down Expand Up @@ -5495,4 +5502,10 @@ mod verify {
gen_align_to_mut_harnesses!(align_to_mut_from_bool, bool);
gen_align_to_mut_harnesses!(align_to_mut_from_char, char);
gen_align_to_mut_harnesses!(align_to_mut_from_unit, ());

#[kani::proof]
fn check_reverse() {
let mut a: [u8; 100] = kani::any();
a.reverse();
}
}
Loading