Repository navigation
Description
Why
A contract harness passes even when the function writes to bytes that a live &T argument points to. That write is UB under Rust's aliasing rules, because a reference argument is protected for the whole call, and Kani doesn't model that:
#[kani::modifies(dst)]
pub unsafe fn bump(src: &u8, dst: *mut u8) {
*dst = *src + 1;
}
#[kani::proof_for_contract(bump)]
fn check_bump() {
let mut x: u8 = kani::any_where(|v: &u8| *v < 255);
let p = &raw mut x;
unsafe { bump(&*p, p) }; // dst aliases the live `&u8` argument
}- Kani (
6ce55b9):VERIFICATION:- SUCCESSFUL. The write is insidemodifies(dst), so the write-set check passes. - Miri, same call:
Undefined Behavior: not granting access to tag ... because that would remove [SharedReadOnly ...] which is strongly protected.
This came up verifying <CStr as CloneToUninit>::clone_to_uninit for model-checking/verify-rust-std#638: its contract allows a dest that overlaps self. Kani catches that case only because copy_nonoverlapping checks for overlap. With ptr::copy in the body, the same contract would verify.
Proposal
In contract check mode, treat the bytes behind each &T argument (outside UnsafeCell) as read-only for the duration of the call, and assert that every write lands outside them, next to the existing assigns checks. That matches Rust's rule for protected reference arguments, makes check_bump fail, and catches the clone_to_uninit case without the author having to state non-overlap.
This is much smaller than a full aliasing model (#314, #3475), because it only covers the call being checked. It doesn't help with obligations that outlive the call, such as "the caller must not mutate the memory behind the &'a CStr that CStr::from_ptr returns".
Where input is wanted
- Should this be on by default in
proof_for_contract, or behind a flag first? - How should it treat
&Targuments whose pointee containsUnsafeCellin only some fields?
Activity