Skip to content

Commit 058866b

Browse files
committed
add Kani harnesses for ; refine the slice harness
1 parent dc80500 commit 058866b

1 file changed

Lines changed: 83 additions & 7 deletions

File tree

library/alloc/src/rc.rs

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4564,15 +4564,25 @@ mod verify_1239 {
45644564
// impl<T, A> Rc<[MaybeUninit<T>], A> (same issue as verify_1198).
45654565
// Uses #[kani::proof]; requires is checked as assertion at call site.
45664566

4567-
// Kani cannot express the full "every element is initialized" precondition for
4568-
// arbitrary slices today. Keep this harness instantiated only for zero-valid types.
4567+
// Kani cannot express the full "all elements are initialized" predicate for
4568+
// arbitrary `T`. This harness models the caller-side safety requirement of
4569+
// `assume_init` with a byte-level witness: it explicitly writes the backing
4570+
// bytes before converting the allocation to `Rc<[MaybeUninit<T>]>`.
4571+
45694572
fn exercise_assume_init_slice<T>() {
4570-
let uninit = verifier_nondet_zeroed_uninit_rc_slice::<T>();
4573+
let len = kani::any_where(|l: &usize| rc_slice_layout_ok::<T>(*l));
4574+
let mut initialized = Vec::<mem::MaybeUninit<T>, Global>::with_capacity(len);
4575+
unsafe {
4576+
initialized.set_len(len);
4577+
ptr::write_bytes(
4578+
initialized.as_mut_ptr().cast::<u8>(),
4579+
kani::any::<u8>(),
4580+
mem::size_of::<T>() * len,
4581+
);
4582+
}
4583+
let uninit: Rc<[mem::MaybeUninit<T>], Global> = Rc::from(initialized);
45714584
let expected_data = (&*uninit).as_ptr() as *const T;
45724585
let expected_len = uninit.len();
4573-
4574-
// SAFETY: All harness instantiations below use types for which a zeroed
4575-
// allocation is already a valid initialized `[T]`.
45764586
let result: Rc<[T], Global> = unsafe { uninit.assume_init() };
45774587

45784588
assert_eq!((&*result).as_ptr(), expected_data);
@@ -4599,7 +4609,6 @@ mod verify_1239 {
45994609
gen_assume_init_slice_harness!(harness_assume_init_slice_u32, u32);
46004610
gen_assume_init_slice_harness!(harness_assume_init_slice_u64, u64);
46014611
gen_assume_init_slice_harness!(harness_assume_init_slice_u128, u128);
4602-
gen_assume_init_slice_harness!(harness_assume_init_slice_bool, bool);
46034612
gen_assume_init_slice_harness!(harness_assume_init_slice_unit, ());
46044613
gen_assume_init_slice_harness!(harness_assume_init_slice_array, [u8; 4]);
46054614
}
@@ -8787,3 +8796,70 @@ mod verify_3107 {
87878796
gen_from_rc_str_to_rc_u8_slice_harness!(harness_from_rc_str_to_rc_u8_slice_empty, "");
87888797
gen_from_rc_str_to_rc_u8_slice_harness!(harness_from_rc_str_to_rc_u8_slice_nonempty, "test");
87898798
}
8799+
8800+
#[cfg(kani)]
8801+
#[unstable(feature = "kani", issue = "none")]
8802+
mod verify_1528 {
8803+
use core::any::Any;
8804+
use super::kani_rc_harness_helpers::*;
8805+
use super::*;
8806+
8807+
macro_rules! gen_into_raw_with_allocator_sized_harness {
8808+
($name:ident, dyn Any) => {
8809+
#[kani::proof]
8810+
pub fn $name() {
8811+
let rc_i32: Rc<i32, Global> = Rc::new_in(kani::any::<i32>(), Global);
8812+
let rc: Rc<dyn Any, Global> = rc_i32;
8813+
let (ptr, alloc): (*const dyn Any, Global) =
8814+
Rc::<dyn Any, Global>::into_raw_with_allocator(rc);
8815+
let _recovered: Rc<dyn Any, Global> =
8816+
unsafe { Rc::<dyn Any, Global>::from_raw_in(ptr, alloc) };
8817+
}
8818+
};
8819+
($name:ident, $ty:ty) => {
8820+
#[kani::proof]
8821+
pub fn $name() {
8822+
let rc: Rc<$ty, Global> = Rc::new_in(kani::any::<$ty>(), Global);
8823+
let (ptr, alloc): (*const $ty, Global) =
8824+
Rc::<$ty, Global>::into_raw_with_allocator(rc);
8825+
let _recovered: Rc<$ty, Global> =
8826+
unsafe { Rc::<$ty, Global>::from_raw_in(ptr, alloc) };
8827+
}
8828+
};
8829+
}
8830+
8831+
macro_rules! gen_into_raw_with_allocator_unsized_harness {
8832+
($name:ident, $elem:ty) => {
8833+
#[kani::proof]
8834+
pub fn $name() {
8835+
let vec = verifier_nondet_vec_rc::<$elem>();
8836+
let rc: Rc<[$elem], Global> = Rc::from(vec);
8837+
let (ptr, alloc): (*const [$elem], Global) =
8838+
Rc::<[$elem], Global>::into_raw_with_allocator(rc);
8839+
let _recovered: Rc<[$elem], Global> =
8840+
unsafe { Rc::<[$elem], Global>::from_raw_in(ptr, alloc) };
8841+
}
8842+
};
8843+
}
8844+
8845+
gen_into_raw_with_allocator_sized_harness!(harness_into_raw_with_allocator_i8, i8);
8846+
gen_into_raw_with_allocator_sized_harness!(harness_into_raw_with_allocator_i16, i16);
8847+
gen_into_raw_with_allocator_sized_harness!(harness_into_raw_with_allocator_i32, i32);
8848+
gen_into_raw_with_allocator_sized_harness!(harness_into_raw_with_allocator_i64, i64);
8849+
gen_into_raw_with_allocator_sized_harness!(harness_into_raw_with_allocator_i128, i128);
8850+
gen_into_raw_with_allocator_sized_harness!(harness_into_raw_with_allocator_u8, u8);
8851+
gen_into_raw_with_allocator_sized_harness!(harness_into_raw_with_allocator_u16, u16);
8852+
gen_into_raw_with_allocator_sized_harness!(harness_into_raw_with_allocator_u32, u32);
8853+
gen_into_raw_with_allocator_sized_harness!(harness_into_raw_with_allocator_u64, u64);
8854+
gen_into_raw_with_allocator_sized_harness!(harness_into_raw_with_allocator_u128, u128);
8855+
gen_into_raw_with_allocator_sized_harness!(harness_into_raw_with_allocator_unit, ());
8856+
gen_into_raw_with_allocator_sized_harness!(harness_into_raw_with_allocator_bool, bool);
8857+
gen_into_raw_with_allocator_sized_harness!(harness_into_raw_with_allocator_array, [u8; 4]);
8858+
gen_into_raw_with_allocator_sized_harness!(harness_into_raw_with_allocator_dyn_any, dyn Any);
8859+
8860+
gen_into_raw_with_allocator_unsized_harness!(harness_into_raw_with_allocator_vec_u8, u8);
8861+
gen_into_raw_with_allocator_unsized_harness!(harness_into_raw_with_allocator_vec_u16, u16);
8862+
gen_into_raw_with_allocator_unsized_harness!(harness_into_raw_with_allocator_vec_u32, u32);
8863+
gen_into_raw_with_allocator_unsized_harness!(harness_into_raw_with_allocator_vec_u64, u64);
8864+
gen_into_raw_with_allocator_unsized_harness!(harness_into_raw_with_allocator_vec_u128, u128);
8865+
}

0 commit comments

Comments
 (0)