Skip to content

Commit da0761a

Browse files
add quantifiers to loop invariant
1 parent 9bd7cc2 commit da0761a

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

library/core/src/slice/memchr.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const fn memchr_naive(x: u8, text: &[u8]) -> Option<usize> {
3636
let mut i = 0;
3737

3838
// FIXME(const-hack): Replace with `text.iter().pos(|c| *c == x)`.
39-
#[safety::loop_invariant(true)]
39+
#[safety::loop_invariant(i <= text.len() && kani::forall!(|j in (0,i)| unsafe {*text.as_ptr().wrapping_add(j)} != x))]
4040
while i < text.len() {
4141
if text[i] == x {
4242
return Some(i);
@@ -79,7 +79,8 @@ const fn memchr_aligned(x: u8, text: &[u8]) -> Option<usize> {
7979

8080
// search the body of the text
8181
let repeated_x = usize::repeat_u8(x);
82-
#[safety::loop_invariant(len >= 2 * USIZE_BYTES && offset <= len)]
82+
#[safety::loop_invariant(len >= 2 * USIZE_BYTES && offset <= len &&
83+
kani::forall!(|j in (0,offset)| unsafe {*text.as_ptr().wrapping_add(j)} != x))]
8384
while offset <= len - 2 * USIZE_BYTES {
8485
// SAFETY: the while's predicate guarantees a distance of at least 2 * usize_bytes
8586
// between the offset and the end of the slice.
@@ -170,22 +171,21 @@ pub mod verify {
170171
use crate::kani;
171172

172173
#[kani::proof]
174+
#[kani::solver(cvc5)]
173175
#[cfg(not(all(target_arch = "x86_64", target_feature = "sse2")))]
174176
pub fn check_memchr_naive() {
175-
const ARR_SIZE: usize = 1000;
177+
const ARR_SIZE: usize = 64;
176178
let x: u8 = kani::any();
177-
let a: [u8; ARR_SIZE] = kani::any();
178-
let text = kani::slice::any_slice_of_array(&a);
179-
let _result = memchr_naive(x, text);
179+
let text: [u8; ARR_SIZE] = kani::any();
180+
let _result = memchr_naive(x, &text);
180181
}
181182

182183
#[kani::proof]
183184
#[cfg(not(all(target_arch = "x86_64", target_feature = "sse2")))]
184185
pub fn check_memchr() {
185-
const ARR_SIZE: usize = 1000;
186+
const ARR_SIZE: usize = 64;
186187
let x: u8 = kani::any();
187-
let a: [u8; ARR_SIZE] = kani::any();
188-
let text = kani::slice::any_slice_of_array(&a);
189-
let _result = memrchr(x, text);
188+
let text: [u8; ARR_SIZE] = kani::any();
189+
let _result = memrchr(x, &text);
190190
}
191191
}

0 commit comments

Comments
 (0)