Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f89fb8a
challenge 22 proof
v3risec Oct 27, 2026
e667451
Merge branch 'main' into challenge-22-str-iter
v3risec Oct 27, 2026
fddac32
Merge branch 'main' into challenge-22-str-iter
v3risec Nov 7, 2026
1ccb130
Merge remote-tracking branch 'upstream/main' into challenge-22-str-iter
v3risec Nov 8, 2026
554b9da
Merge branch 'main' into challenge-22-str-iter
v3risec Nov 10, 2026
1e31276
Merge remote-tracking branch 'origin/challenge-22-str-iter' into chal…
v3risec Nov 10, 2026
0753634
Merge branch 'main' into challenge-22-str-iter
v3risec Nov 16, 2026
8166f4a
Strengthen Challenge 22 verification with unbounded symbolic-length …
v3risec Nov 16, 2026
b377ac9
Merge remote-tracking branch 'origin/challenge-22-str-iter' into chal…
v3risec Nov 16, 2026
81ead6a
fix upstream test error
v3risec Nov 16, 2026
0e79f5b
Run Challenge 22 proofs on pinned Kani
v3risec Nov 18, 2026
4f6b418
Remove Challenge 22 unwind annotations and as_slice stub
v3risec Nov 18, 2026
8ef46db
Merge branch 'main' into challenge-22-str-iter
v3risec Nov 19, 2026
b0c8a69
Verify Challenge 22 split transitions and chunk counts
v3risec Nov 19, 2026
1865765
Merge branch 'main' into challenge-22-str-iter
v3risec Nov 21, 2026
1ef00e2
Adapt Challenge 22 ModelPattern to unsafe Pattern
v3risec Nov 21, 2026
6c9a3bb
Merge branch 'main' into challenge-22-str-iter
v3risec Nov 22, 2026
1d30613
Merge branch 'main' into challenge-22-str-iter
v3risec Nov 22, 2026
238a686
Strengthen Challenge 22 string iterator proofs
v3risec Nov 24, 2026
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
98 changes: 98 additions & 0 deletions library/core/src/array/iter/iter_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,62 @@ impl<T: TrivialClone, const N: usize> SpecPolymorphicIterClone<N> for T {
}
}

// Fail closed if the harness-wide fold stub reaches an unproved element
// instantiation. The actual advance_by pipeline and refinement proofs use bool.
#[cfg(kani)]
trait Challenge22FoldElement {
fn admitted() -> bool;
}

#[cfg(kani)]
impl<T> Challenge22FoldElement for T {
default fn admitted() -> bool {
false
}
}

#[cfg(kani)]
impl Challenge22FoldElement for bool {
fn admitted() -> bool {
true
}
}

impl<T> PolymorphicIter<[MaybeUninit<T>]> {
/// Loop-free replacement used only by the Challenge 22 harnesses.
///
/// Advance `alive` before each callback, as `try_fold` does, so drop sees
/// the same remaining elements even if the callback panics.
#[cfg(kani)]
pub(crate) fn kani_fold_at_most_32<B>(&mut self, init: B, mut f: impl FnMut(B, T) -> B) -> B {
assert!(<T as Challenge22FoldElement>::admitted());
assert!(self.alive.start() <= self.alive.end());
assert!(self.alive.end() <= self.data.len());
assert!(self.data.len() == 32);
let start = self.alive.start();
let end = self.alive.end();
let data = self.data.as_ptr().cast::<T>();
let mut accum = init;
macro_rules! step {
($($k:literal),+ $(,)?) => {
$(
if start + $k < end {
let idx = start + $k;
self.alive = IndexRange::kani_from_bounds(idx + 1, end);
// SAFETY: This step consumes an initialized live element.
let elem = unsafe { crate::intrinsics::read_via_copy(data.wrapping_add(idx)) };
accum = f(accum, elem);
}
)+
};
}
step!(
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
);
accum
}

#[inline]
pub(super) fn as_slice(&self) -> &[T] {
// SAFETY: We know that all elements within `alive` are properly initialized.
Expand Down Expand Up @@ -324,3 +379,46 @@ impl<T> PolymorphicIter<[MaybeUninit<T>]> {
})
}
}

#[cfg(kani)]
#[unstable(feature = "kani", issue = "none")]
mod verify {
use super::*;
use crate::kani;

// In addition to the sum used by advance_by, record the entire sequence
// (at most 32 bits) and the callback count. Compare the actual implementation
// directly with the model; neither implementation is stubbed in this proof.
#[kani::proof]
fn check_consuming_fold_model() {
let flags: [bool; 32] = kani::any();
let start: usize = kani::any();
let end: usize = kani::any();
kani::assume(start <= end && end <= 32);
kani::cover(start < end, "nonempty live range admitted");
// Same representation conversion as IntoIterator for arrays, without
// array::map's unrelated construction and draining machinery.
let data: [MaybeUninit<bool>; 32] = unsafe { crate::mem::transmute(flags) };
let mut actual =
unsafe { PolymorphicIter::new_unchecked(IndexRange::new_unchecked(start, end), data) };
let mut model =
unsafe { PolymorphicIter::new_unchecked(IndexRange::new_unchecked(start, end), data) };
let actual: &mut PolymorphicIter<[MaybeUninit<bool>]> = &mut actual;
let model: &mut PolymorphicIter<[MaybeUninit<bool>]> = &mut model;
let observe = |(sum, trace, calls): (u8, u64, u8), flag: bool| {
assert!(calls < 32, "fold called the callback more than 32 times");
(sum + flag as u8, (trace << 1) | flag as u64, calls + 1)
};
let expected = actual.fold((0, 0, 0), observe);
let result = model.kani_fold_at_most_32((0, 0, 0), observe);
assert!(result == expected);
assert!(actual.alive == model.alive);
assert!(model.alive.start() == end && model.alive.end() == end);
assert!(result.2 as usize == end - start);
assert!(model.next().is_none() && actual.next().is_none());
kani::cover(start == 0 && end == 32, "all 32 independent flags consumed");
kani::cover(start == end, "empty alive range");
kani::cover(0 < start && start < end && end < 32, "interior alive range");
// Both ordinary destructors run; bool has no destructor of its own.
}
}
9 changes: 9 additions & 0 deletions library/core/src/ops/index_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ impl FusedIterator for IndexRange {}
#[cfg(kani)]
mod verify {
use super::*;

impl IndexRange {
// A direct range assignment keeps the fold model's write set explicit.
pub(crate) fn kani_from_bounds(start: usize, end: usize) -> Self {
assert!(start <= end);
Self { start, end }
}
}

#[kani::proof_for_contract(IndexRange::new_unchecked)]
fn proof_for_index_range_new_unchecked() {
let start = kani::any::<usize>();
Expand Down
24 changes: 24 additions & 0 deletions library/core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,30 @@ impl<T> AsRef<[T]> for Iter<'_, T> {
}
}

#[cfg(kani)]
impl<'a, T> Iter<'a, T> {
/// Expose the updated cursor as a raw pointer for Kani's loop write set.
pub(crate) fn kani_cursor_slot(&mut self) -> *mut *const T {
// NonNull<T> is transparent over its pointer.
(&raw mut self.ptr).cast()
}

/// Clause-only copy: CBMC adds a write-set parameter to ordinary calls,
/// whereas calls from loop clauses have a different calling convention.
///
/// # Safety
/// `this` must point to readable iterator storage. The pointer fields are
/// read as raw pointer representations, so a havoc candidate need not yet
/// satisfy the NonNull or iterator range invariants.
pub(crate) unsafe fn kani_loop_raw_parts(this: *const Self) -> (*const T, *const T) {
// Read the representation before requiring NonNull's validity. Havoc
// can produce invalid candidates, which the invariant must exclude.
let ptr = unsafe { &raw const (*this).ptr } as *const *const T;
let end = unsafe { &raw const (*this).end_or_len };
unsafe { (crate::intrinsics::read_via_copy(ptr), crate::intrinsics::read_via_copy(end)) }
}
}

#[unstable(feature = "ub_checks", issue = "none")]
impl<T> Invariant for Iter<'_, T> {
/// An iterator can be safely used if its pointer can be read for its current length.
Expand Down
Loading
Loading