Skip to content

Commit 4b5775e

Browse files
committed
Challenge 22: Verify the safety of str iter functions with Kani
1 parent 1feeadd commit 4b5775e

3 files changed

Lines changed: 1217 additions & 0 deletions

File tree

‎library/core/src/array/iter/iter_inner.rs‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,84 @@ impl<T> PolymorphicIter<[MaybeUninit<T>]> {
324324
})
325325
}
326326
}
327+
328+
#[cfg(kani)]
329+
use crate::kani;
330+
331+
// Loop-free sound over-approximation of `PolymorphicIter::fold` on `bool/u8` sum (total <= 32).
332+
// Implemented as an inherent helper rather than an attached `PolymorphicIter::fold` contract
333+
// (avoiding generic-method contract limits, cf. kani#4084), and paired with
334+
// `check_kani_fold_bounded32_soundness` proving real `PolymorphicIter::fold` satisfies
335+
// `res <= total && len == 0` for all 2^32 Boolean valuations.
336+
#[cfg(kani)]
337+
impl<T> PolymorphicIter<[MaybeUninit<T>]> {
338+
pub(super) fn kani_fold_bounded32<B, F>(&mut self, mut accum: B, f: F) -> B
339+
where
340+
F: FnMut(B, T) -> B,
341+
{
342+
assert!(crate::mem::size_of::<B>() == 1);
343+
assert!(crate::mem::size_of::<F>() == 0);
344+
assert!(kani::mem::can_dereference((&raw const accum).cast::<u8>()));
345+
assert!(unsafe { *(&raw const accum).cast::<u8>() } == 0);
346+
let total = self.len();
347+
assert!(total <= 32);
348+
let _ = f;
349+
let res_u8: u8 = kani::any();
350+
kani::assume(res_u8 <= total as u8);
351+
self.alive = crate::ops::IndexRange::zero_to(0);
352+
assert!(self.len() == 0, "bounded fold did not consume the iterator");
353+
kani::cover(total == 32, "bounded32 helper reached");
354+
unsafe { *(&raw mut accum).cast::<u8>() = res_u8 };
355+
accum
356+
}
357+
}
358+
359+
#[cfg(kani)]
360+
#[kani::proof]
361+
pub fn check_kani_fold_bounded32_soundness() {
362+
let values: [bool; 32] = [
363+
kani::any::<bool>(),
364+
kani::any::<bool>(),
365+
kani::any::<bool>(),
366+
kani::any::<bool>(),
367+
kani::any::<bool>(),
368+
kani::any::<bool>(),
369+
kani::any::<bool>(),
370+
kani::any::<bool>(),
371+
kani::any::<bool>(),
372+
kani::any::<bool>(),
373+
kani::any::<bool>(),
374+
kani::any::<bool>(),
375+
kani::any::<bool>(),
376+
kani::any::<bool>(),
377+
kani::any::<bool>(),
378+
kani::any::<bool>(),
379+
kani::any::<bool>(),
380+
kani::any::<bool>(),
381+
kani::any::<bool>(),
382+
kani::any::<bool>(),
383+
kani::any::<bool>(),
384+
kani::any::<bool>(),
385+
kani::any::<bool>(),
386+
kani::any::<bool>(),
387+
kani::any::<bool>(),
388+
kani::any::<bool>(),
389+
kani::any::<bool>(),
390+
kani::any::<bool>(),
391+
kani::any::<bool>(),
392+
kani::any::<bool>(),
393+
kani::any::<bool>(),
394+
kani::any::<bool>(),
395+
];
396+
let mut iter = values.into_iter();
397+
let total = iter.len();
398+
assert_eq!(total, 32);
399+
let res: u8 = iter.unsize_mut().fold(0u8, |accum, value| accum + value as u8);
400+
assert!((res as usize) <= total);
401+
assert_eq!(iter.len(), 0);
402+
let sum_res: u8 = values.into_iter().map(|v| v as u8).sum();
403+
assert_eq!(sum_res, res);
404+
kani::cover(res == 0, "all values false");
405+
kani::cover(res == 32, "all values true");
406+
kani::cover(res > 0 && res < 32, "mixed values");
407+
}

‎library/core/src/slice/iter.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3402,3 +3402,13 @@ mod verify {
34023402
check_iter_with_ty!(verify_char, char, 50);
34033403
check_iter_with_ty!(verify_tup, (char, u8), 50);
34043404
}
3405+
3406+
#[cfg(kani)]
3407+
impl<'a, T> Iter<'a, T> {
3408+
/// Exposes the raw cursor and end pointers for sibling `core::str::iter` Kani invariants.
3409+
pub(crate) fn kani_parts_for_ch22(&self) -> (*const T, *const T) {
3410+
// NonNull<T> is transparent over its pointer; T is Sized here.
3411+
let cursor = unsafe { crate::mem::transmute::<NonNull<T>, *const T>(self.ptr) };
3412+
(cursor, self.end_or_len)
3413+
}
3414+
}

0 commit comments

Comments
 (0)