Skip to content

Contracts: flag writes to memory behind a live &T argument #5040

Description

@feliperodri

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 inside modifies(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 &T arguments whose pointee contains UnsafeCell in only some fields?

Activity

  1. added this to the Contracts milestone on Nov 6, 2026
  2. self-assigned this
    on Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

T-High PriorityTag issues that have high priorityZ-ContractsIssue related to code contracts[C] Feature / EnhancementA new feature request or enhancement to an existing feature.[E] Unsupported UBUndefined behavior that Kani does not detect

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions