Skip to content

Commit

Permalink
Add loop contracts and harness for
Browse files Browse the repository at this point in the history
  • Loading branch information
qinheping committed Oct 19, 2024
1 parent 3a967e3 commit e8a7fca
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions library/core/src/str/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ use crate::convert::TryInto as _;
use crate::slice::memchr;
use crate::{cmp, fmt};

#[cfg(kani)]
use crate::kani;

// Pattern

/// A string pattern.
Expand Down Expand Up @@ -1926,6 +1929,10 @@ unsafe fn small_slice_eq(x: &[u8], y: &[u8]) -> bool {
unsafe {
let (mut px, mut py) = (x.as_ptr(), y.as_ptr());
let (pxend, pyend) = (px.add(x.len() - 4), py.add(y.len() - 4));
#[cfg_attr(kani, kani::loop_invariant(same_allocation(x.as_ptr(), px) && same_allocation(y.as_ptr(), py)
&& px as isize >= x.as_ptr() as isize
&& py as isize >= y.as_ptr() as isize
&& px as isize - x.as_ptr() as isize == (py as isize - y.as_ptr() as isize)))]
while px < pxend {
let vx = (px as *const u32).read_unaligned();
let vy = (py as *const u32).read_unaligned();
Expand All @@ -1940,3 +1947,17 @@ unsafe fn small_slice_eq(x: &[u8], y: &[u8]) -> bool {
vx == vy
}
}

#[cfg(kani)]
#[unstable(feature = "kani", issue = "none")]
pub mod verify {
#[cfg(all(target_arch = "x86_64", target_feature = "sse2"))] // only called on x86
#[kani::proof]
pub fn check_small_slice_eq() {
let _ = Box::new(0);
const ARR_SIZE: usize = 1000;
let x: [i32; ARR_SIZE] = kani::any();
let y: [i32; ARR_SIZE] = kani::any();
small_slice_eq(x, y);
}
}

0 comments on commit e8a7fca

Please sign in to comment.