Repository navigation
Description
Why
A Kani proof of generic code holds only for the types the harness names. Code that branches on a property of T that none of those types has goes unchecked. A 3-line example, shaped like RawVec's min_non_zero_cap (which branches on element size 1, up to 1 KiB, or larger):
unsafe fn first<T>(p: *const u8) -> u8 {
let q = if core::mem::size_of::<T>() <= 1024 { p } else { p.add(1) };
*q // reads past the buffer when T is larger than 1 KiB
}
fn check<T>() { let b: u8 = kani::any(); let _ = unsafe { first::<T>(&b) }; }Instantiated over (), u8, bool, [u8; 3], u64, a 16-byte-aligned type and a type with Drop, all seven harnesses pass on Kani 6ce55b9. Only a 2 KiB type fails (pointer outside object bounds). VeriFast, which proves the generic function once, reports the error without any instantiation.
This blocks Kani solutions to seven verify-rust-std challenges (16–19, 23–25), which require proofs to "hold for generic type T (no monomorphization)". See model-checking/verify-rust-std#681 for the open ruling request.
Proposal
Let a harness use an abstract stand-in for T:
size_ofandalign_ofare symbolic, constrained only by what holds for every Rust type: the alignment is a power of two, the size is a multiple of the alignment, and the size is at mostisize::MAX. Users can narrow these bounds.- Values of the type are byte blobs of that size. CBMC already supports objects of symbolic size.
- Its trait methods (
Clone,Drop,PartialEq, ...) are stubs that may do anything a user-supplied contract allows, for example "Dropwrites no memory outsideself".
A passing proof then holds for every concrete T that fits the bounds and whose impls satisfy the contracts, instead of for a list of sample types.
Hard parts
- Layouts are fixed at monomorphization, and
size_of::<T>()is often const-folded before Kani sees it. The abstract type would need its own handling before const evaluation. - Layouts of types built from
Tdepend on it.Option<T>is the same size asTwhenThas a niche. For an abstractT, both layouts have to be covered. - Drop glue and trait dispatch for the abstract type need the stub-plus-contract treatment.
Code that treats T as opaque bytes (RawVec, Vec, VecDeque, slices) looks like the natural first target. Most of the blocked challenges are about these.
Where input is wanted
- Is intercepting
size_of/align_offor a marker type before const evaluation feasible in kani-compiler? - Would a first version that rejects code whose layout depends on
T's niche be useful enough?
Activity