Skip to content

Verify generic code once for an abstract T (symbolic size, alignment and trait behaviour) #5041

Description

@feliperodri

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_of and align_of are 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 most isize::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 "Drop writes no memory outside self".

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 T depend on it. Option<T> is the same size as T when T has a niche. For an abstract T, 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_of for 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

  1. added
    [C] Feature / Enhancement
    on Nov 6, 2026
  2. self-assigned this
    on Nov 6, 2026
  3. added
    T-RFC
    on Nov 6, 2026
  4. added theissue type on Nov 6, 2026
  5. changed the issue type fromtoon Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

T-RFC[C] Feature / Enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions