Skip to content

Modifies clauses: an empty slice with a dangling data pointer fails ptr NULL or writable up to size #4931

Closed

Description

@Talha-Dmr

I tried this code:

#![feature(proc_macro_hygiene)]
#![feature(stmt_expr_attributes)]

#[kani::proof]
fn loop_modifies_empty_vec() {
    let len: usize = kani::any_where(|l: &usize| *l <= 2);
    let mut v: Vec<u8> = vec![1; len];
    let s = v.as_mut_slice();
    let n = len;
    let mut i: usize = 0;
    #[kani::loop_invariant(i <= n)]
    #[kani::loop_modifies(&i, &mut s[..])]
    while i < n {
        s[i] = 0;
        i += 1;
    }
}

#[kani::modifies(x)]
fn zero(x: &mut [u8]) {
    x.fill(0)
}

#[kani::proof_for_contract(zero)]
fn modifies_empty_vec() {
    let mut v: Vec<u8> = Vec::new();
    zero(v.as_mut_slice());
}

using the following command line invocation:

kani empty_slice.rs -Z loop-contracts -Z function-contracts

with Kani version: 0.68.0, built from source at 7207ac1 (current main); CBMC 6.11.0; Arch Linux x86_64.

I expected to see this happen: both harnesses verify. An empty slice covers no memory, so naming it in a modifies clause should be harmless.

Instead, this happened: both fail when the slice is empty:

Checking harness modifies_empty_vec...
Failed Checks: ptr NULL or writable up to size
VERIFICATION:- FAILED
Checking harness loop_modifies_empty_vec...
Failed Checks: ptr NULL or writable up to size
VERIFICATION:- FAILED

The same happens for a slice over the buffer of a String without capacity (slice_from_raw_parts_mut(st.as_mut_ptr(), st.capacity()), which also fails assertion havoc_slice W_OK(car.lb, car.size) in a loop) and for &mut a[..] with a: [u8; 0] (which also fails no offset bits overflow on CAR upper bound computation). An empty slice at the end of a live array (&mut a[4..] with a: [u8; 4]) works.

Cause: a slice target is generated as __CPROVER_object_upto(data, len * size_of::<T>()) (ty_to_assign_target in statement.rs for loops, codegen_modifies_contract in contract.rs for functions). CBMC's __CPROVER_contracts_car_create / __CPROVER_contracts_car_set_insert assert ptr == NULL || __CPROVER_rw_ok(ptr, size), and __CPROVER_rw_ok is false for a dangling pointer even when size is 0. The data pointer of an empty Vec, of a String without capacity or of an empty array is dangling.

This is similar to #3181 and #4786, which were about zero-sized types rather than zero-length slices.

Activity

  1. added a commit that references this issue on Oct 2, 2026
    5e70300
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions