Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
#![feature(min_specialization)]
#![feature(multiple_supertrait_upcastable)]
#![feature(negative_impls)]
#![feature(proc_macro_hygiene)]
#![feature(rustc_attrs)]
#![feature(slice_internals)]
#![feature(staged_api)]
Expand Down
4 changes: 4 additions & 0 deletions library/alloc/src/vec/extract_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,7 @@ where
.finish_non_exhaustive()
}
}

#[cfg(all(kani, not(no_global_oom_handling)))]
#[path = "verify_extract_if.rs"]
mod verify;
165 changes: 164 additions & 1 deletion library/alloc/src/vec/into_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use core::panic::UnwindSafe;
use core::ptr::{self, NonNull};
use core::{array, fmt, slice};

use safety::requires;
use safety::{loop_invariant, requires};

#[cfg(not(no_global_oom_handling))]
use super::AsVecIntoIter;
Expand Down Expand Up @@ -265,6 +265,129 @@ unsafe impl<T: Send, A: Allocator + Send> Send for IntoIter<T, A> {}
#[stable(feature = "rust1", since = "1.0.0")]
unsafe impl<T: Sync, A: Allocator + Sync> Sync for IntoIter<T, A> {}

// Loop-invariant predicates for `IntoIter::fold` and `IntoIter::try_fold`.
// Marked `#[inline(always)]` and written with pure intrinsics and `const { ... }`
// blocks rather than method calls (e.g. `NonNull::as_ptr` / `pointer::addr` /
// non-const `mem::size_of`) so Kani's loop-contract lowering evaluates them
// inline as side-effect-free CBMC expressions without emitting nondeterministic
// helper calls (https://github.com/model-checking/kani/issues/4796).
// `self.alloc` is untouched by either loop and is preserved across loop
// contracts (verified with both `TrackAlloc` and non-ZST `StatefulAlloc`).
#[cfg(kani)]
#[inline(always)]
fn kani_into_iter_zst_inv<T>(
ptr: NonNull<T>,
buf: NonNull<T>,
cap: usize,
end: *const T,
entry_ptr: NonNull<T>,
entry_buf: NonNull<T>,
entry_cap: usize,
entry_end: *const T,
) -> bool {
// SAFETY: `NonNull<T>`, `*const T`, and `usize` have identical layout (thin pointer width);
// these casts are side-effect-free re-interpretations for CBMC's loop-invariant evaluator.
let (ptr_p, eptr_p, buf_p, ebuf_p, ptr_a, eptr_a, end_a, eend_a): (
*const T,
*const T,
*const T,
*const T,
usize,
usize,
usize,
usize,
) = unsafe {
(
core::intrinsics::transmute_unchecked(ptr),
core::intrinsics::transmute_unchecked(entry_ptr),
core::intrinsics::transmute_unchecked(buf),
core::intrinsics::transmute_unchecked(entry_buf),
core::intrinsics::transmute_unchecked(ptr),
core::intrinsics::transmute_unchecked(entry_ptr),
core::intrinsics::transmute_unchecked(end),
core::intrinsics::transmute_unchecked(entry_end),
)
};
ptr_p == eptr_p
&& buf_p == ebuf_p
&& cap == entry_cap
&& core::intrinsics::wrapping_sub(end_a, ptr_a)
<= core::intrinsics::wrapping_sub(eend_a, eptr_a)
}

#[cfg(kani)]
#[inline(always)]
fn kani_into_iter_non_zst_inv<T>(
ptr: NonNull<T>,
buf: NonNull<T>,
cap: usize,
end: *const T,
entry_ptr: NonNull<T>,
entry_buf: NonNull<T>,
entry_cap: usize,
entry_end: *const T,
) -> bool {
// SAFETY: `NonNull<T>`, `*const T`, `*const u8`, and `usize` have identical thin-pointer
// layout; used to avoid method calls in CBMC loop-contract predicates (kani#4796).
let (buf_p, ebuf_p, ptr_p, eptr_u8, ptr_a, eptr_a, end_a): (
*const T,
*const T,
*const T,
*const u8,
usize,
usize,
usize,
) = unsafe {
(
core::intrinsics::transmute_unchecked(buf),
core::intrinsics::transmute_unchecked(entry_buf),
core::intrinsics::transmute_unchecked(ptr),
core::intrinsics::transmute_unchecked(entry_ptr),
core::intrinsics::transmute_unchecked(ptr),
core::intrinsics::transmute_unchecked(entry_ptr),
core::intrinsics::transmute_unchecked(end),
)
};
let diff = core::intrinsics::wrapping_sub(ptr_a, eptr_a);
let span = core::intrinsics::wrapping_sub(end_a, eptr_a);
let sz = const { core::mem::size_of::<T>() };
let aligned = if const { core::mem::size_of::<T>().is_power_of_two() } {
(diff & core::intrinsics::wrapping_sub(sz, 1)) == 0
} else {
// Exact divisibility `diff % sz == 0` for any non-power-of-two `sz > 0` without a
// 64-bit integer division circuit: write `sz = odd * 2^tz` and compute the modular
// multiplicative inverse `odd_inv` (`odd * odd_inv == 1 mod 2^BITS`) at const time.
// Then `sz | diff` iff `2^tz | diff` and `(diff >> tz) * odd_inv <= usize::MAX / sz`.
let (tz, odd_inv, max_q) = const {
let s = core::mem::size_of::<T>();
if s == 0 {
(0u32, 1usize, 0usize)
} else {
let tz = s.trailing_zeros();
let odd = s >> tz;
let mut inv: usize = 1;
let mut i = 0;
while i < 6 {
inv = inv.wrapping_mul(2usize.wrapping_sub(odd.wrapping_mul(inv)));
i += 1;
}
assert!(odd.wrapping_mul(inv) == 1);
(tz, inv, usize::MAX / s)
}
};
let low_mask = core::intrinsics::wrapping_sub(1usize << tz, 1);
(diff & low_mask) == 0 && core::intrinsics::wrapping_mul(diff >> tz, odd_inv) <= max_q
};
// SAFETY: `arith_offset` is wrapping (non-UB) pointer arithmetic; short-circuiting after
// `diff <= span && aligned` additionally guarantees `diff` stays within `[entry_ptr, end]`.
buf_p == ebuf_p
&& cap == entry_cap
&& end == entry_end
&& diff <= span
&& aligned
&& ptr_p == unsafe { core::intrinsics::arith_offset(eptr_u8, diff as isize) as *const T }
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, A: Allocator> Iterator for IntoIter<T, A> {
type Item = T;
Expand Down Expand Up @@ -374,6 +497,16 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
F: FnMut(B, Self::Item) -> B,
{
if T::IS_ZST {
#[loop_invariant(kani_into_iter_zst_inv(
self.ptr,
self.buf,
self.cap,
self.end,
on_entry(self.ptr),
on_entry(self.buf),
on_entry(self.cap),
on_entry(self.end),
))]
while self.ptr.as_ptr() != self.end.cast_mut() {
// SAFETY: we just checked that `self.ptr` is in bounds.
let tmp = unsafe { self.ptr.read() };
Expand All @@ -383,6 +516,16 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
}
} else {
// SAFETY: `self.end` can only be null if `T` is a ZST.
#[loop_invariant(kani_into_iter_non_zst_inv(
self.ptr,
self.buf,
self.cap,
self.end,
on_entry(self.ptr),
on_entry(self.buf),
on_entry(self.cap),
on_entry(self.end),
))]
while self.ptr != non_null!(self.end, T) {
// SAFETY: we just checked that `self.ptr` is in bounds.
let tmp = unsafe { self.ptr.read() };
Expand All @@ -409,6 +552,16 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
R: core::ops::Try<Output = B>,
{
if T::IS_ZST {
#[loop_invariant(kani_into_iter_zst_inv(
self.ptr,
self.buf,
self.cap,
self.end,
on_entry(self.ptr),
on_entry(self.buf),
on_entry(self.cap),
on_entry(self.end),
))]
while self.ptr.as_ptr() != self.end.cast_mut() {
// SAFETY: we just checked that `self.ptr` is in bounds.
let tmp = unsafe { self.ptr.read() };
Expand All @@ -418,6 +571,16 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
}
} else {
// SAFETY: `self.end` can only be null if `T` is a ZST.
#[loop_invariant(kani_into_iter_non_zst_inv(
self.ptr,
self.buf,
self.cap,
self.end,
on_entry(self.ptr),
on_entry(self.buf),
on_entry(self.cap),
on_entry(self.end),
))]
while self.ptr != non_null!(self.end, T) {
// SAFETY: we just checked that `self.ptr` is in bounds.
let tmp = unsafe { self.ptr.read() };
Expand Down
3 changes: 3 additions & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ use self::spec_from_elem::SpecFromElem;
#[cfg(not(no_global_oom_handling))]
mod spec_from_elem;

#[cfg(all(kani, not(no_global_oom_handling)))]
mod verify_iter_spec;

#[cfg(not(no_global_oom_handling))]
use self::set_len_on_drop::SetLenOnDrop;

Expand Down
Loading
Loading