Skip to content

Writes through an integer-cast pointer that equals a live object's address are dropped (false VERIFICATION SUCCESSFUL); this makes stub_verified unsound for pointer-holding modifies targets and results #5143

Open

Description

@c-grellois

Note -- this is part of a project of the reporter's, mixing a theoretical framework with AI agentic work for the implementation and benchmarking.

The following was outputted by Claude Opus 5.5, after months of interactions to co-create and develop a tool from the original theoretical methodology and results that are all "human-produced".

Disclosure: this report was prepared with the help of an AI assistant (Anthropic's Claude). Every command below was run locally on my machine and its output pasted verbatim. I reviewed the analysis, but please treat the root-cause section as a hypothesis to check.

Summary. Kani can produce a pointer from an integer (kani::any::<usize>() as *mut T, or ptr::with_exposed_provenance_mut). If that pointer is constrained to equal the address of a live object, a write through it does not reach the object, every pointer check passes, and Kani reports VERIFICATION:- SUCCESSFUL for a false assertion.

In a hand-written harness, this needs an explicit kani::assume. #[kani::stub_verified] adds that constraint on its own:

So a verified contract, used as a stub, silently drops writes made through the pointers it returns.

I tried this code (library crate, src/lib.rs; Cargo.toml has no dependencies):

pub struct Cursor {
    pub p: *mut u8,
}

// Kani has no `Arbitrary` for raw pointers, so a `modifies` target that holds one
// needs a hand-written impl. This one has the shape of verify-rust-std's
// `impl<T> kani::Arbitrary for NonNull<T>` (library/core/src/ptr/non_null.rs).
impl kani::Arbitrary for Cursor {
    fn any() -> Self {
        Cursor { p: kani::any::<usize>() as *mut u8 }
    }
}

#[kani::modifies(c)]
#[kani::ensures(|_r: &()| c.p == old(c.p).wrapping_add(1))]
pub fn step(c: &mut Cursor) {
    c.p = c.p.wrapping_add(1);
}

#[kani::proof_for_contract(step)]
fn check_step() {
    let mut a = [0u8; 2];
    let mut c = Cursor { p: a.as_mut_ptr() };
    step(&mut c);
}

#[kani::proof]
#[kani::stub_verified(step)]
fn use_step() {
    let mut a = [0u8; 2];
    let mut c = Cursor { p: a.as_mut_ptr() };
    step(&mut c); // by the contract, c.p == &a[1]
    kani::cover!(c.p == a.as_mut_ptr().wrapping_add(1), "c.p == &a[1] after the call");
    unsafe { *c.p = 7 };
    kani::cover!(a[1] == 7, "the write reached a[1]");
    assert!(a[1] == 0, "a[1] == 0 after writing 7 through c.p (false)");
}

#[kani::proof]
fn use_step_inline() {
    let mut a = [0u8; 2];
    let mut c = Cursor { p: a.as_mut_ptr() };
    step(&mut c);
    unsafe { *c.p = 7 };
    assert!(a[1] == 0, "a[1] == 0 after writing 7 through c.p (false)");
}

// Without contracts: a pointer made from an integer, pinned to &a[1] with an assume.
// a's provenance is exposed first, so the write is defined behaviour in Rust.
#[kani::proof]
fn plain_exposed() {
    let mut a = [0u8; 2];
    let addr = a.as_mut_ptr().expose_provenance();
    let p: *mut u8 = core::ptr::with_exposed_provenance_mut(kani::any::<usize>());
    kani::assume(p as usize == addr.wrapping_add(1));
    unsafe { *p = 7 };
    kani::cover!(a[1] == 7, "the write reached a[1]");
    assert!(a[1] == 0, "a[1] == 0 after writing 7 through p (false)");
}

// Control: the same pin on a pointer that is not made from an integer.
#[kani::proof]
fn plain_control() {
    let mut a = [0u8; 2];
    let addr = a.as_mut_ptr().expose_provenance();
    let p: *mut u8 = if kani::any() { a.as_mut_ptr() } else { a.as_mut_ptr().wrapping_add(1) };
    kani::assume(p as usize == addr.wrapping_add(1));
    unsafe { *p = 7 };
    kani::cover!(a[1] == 7, "the write reached a[1]");
    assert!(a[1] == 0, "a[1] == 0 after writing 7 through p (false)");
}

using the following command line invocation:

cargo kani -Z function-contracts -Z stubbing --harness <name> --exact

with Kani version: 0.68.0 (Kani Rust Verifier 0.68.0 (cargo plugin), CBMC 6.11.0), x86_64 Linux (Ubuntu 22.04, WSL2). Kani 0.67.0 (CBMC 6.8.0) gives the same results for use_step and use_step_inline.

I expected to see this happen: check_step verifies, and the other four harnesses all fail. The contract replacement must include the real call's behaviour, and in the real call c.p ends up equal to &a[1], so the write sets a[1] to 7. In plain_exposed, a's provenance was exposed, so p may legitimately point to a[1], and the write is defined behaviour.

Instead, this happened:

Harness Result
check_step ** 0 of 125 failed, VERIFICATION:- SUCCESSFUL (the contract holds)
use_step VERIFICATION:- SUCCESSFUL (** 0 of 89 failed, ** 1 of 2 cover properties satisfied)
use_step_inline assertion FAILURE, VERIFICATION:- FAILED
plain_exposed VERIFICATION:- SUCCESSFUL (** 0 of 12 failed, ** 0 of 1 cover properties satisfied)
plain_control cover SATISFIED, assertion FAILURE, VERIFICATION:- FAILED

use_step, excerpt:

Check 2: use_step.cover.1
	 - Status: SATISFIED
	 - Description: "c.p == &a[1] after the call"
	 - Location: src/lib.rs:33:5 in function use_step

Check 5: use_step.cover.2
	 - Status: UNSATISFIABLE
	 - Description: "the write reached a[1]"
	 - Location: src/lib.rs:35:5 in function use_step

Check 7: use_step.assertion.3
	 - Status: SUCCESS
	 - Description: ""a[1] == 0 after writing 7 through c.p (false)""
	 - Location: src/lib.rs:36:5 in function use_step
...
Check 60: use_step.pointer_dereference.1
	 - Status: SUCCESS
	 - Description: "dereference failure: pointer NULL"
	 - Location: src/lib.rs:34:14 in function use_step

The other five pointer_dereference checks on the write (pointer invalid, deallocated dynamic object, dead object, pointer outside object bounds, invalid integer address) also pass, both in use_step (line 34) and in plain_exposed (line 56).

Some variants of use_step I tried:

  • assert!(false) placed after the write fails, so the path is reachable;
  • assert!(c.p == a.as_mut_ptr().wrapping_add(1)) after the call succeeds;
  • the true assert!(a[1] == 7) fails spuriously.

The false SUCCESS also appears with:

  • a NonNull<u8> field (the same integer cast plus kani::assume(!ptr.is_null()));
  • a transmute::<usize, _>(kani::any()) impl;
  • an integer pin in the ensures (c.p as usize == old(c.p as usize) + 1);
  • a heap buffer;
  • the pointer in the result (struct Ret(*mut u8), or a struct around Option<NonNull<u8>>);
  • a's provenance exposed before the call.

A pointer-free cursor (struct { i: usize }, derived Arbitrary, write a[c.i] = 7) behaves correctly. Nothing I tried made the false SUCCESS visible: -Z uninit-checks, -Z valid-value-checks, -Z unstable-options --extra-pointer-checks, --cbmc-args --pointer-primitive-check. With -Z mem-predicates, kani::mem::can_write(c.p) and same_allocation(c.p, a.as_ptr()) both hold.

It also happens with no user-written impl. I appended a small probe to library/core/src/ptr/non_null.rs in a local copy of model-checking/verify-rust-std and used its pinned Kani (1640445, with the flags of scripts/run-kani.sh). The probe has a contracted fn next(p: *mut u8) -> NonNull<u8> with ensures(|r| r.as_ptr() == p.wrapping_add(1)). stub_verified havocs its result with verify-rust-std's own impl<T> kani::Arbitrary for NonNull<T>, and the same false SUCCESS appears (0 of 79 failed, the cover after the write UNSATISFIABLE). It also appears for a modifies(q) target with q: &mut NonNull<u8>.

What I think happens (please verify).

  • CBMC gives a pointer made from an integer the points-to set {integer_address}; for transmuted bytes it is {unknown}. The assumption makes the pointer's value equal &a[1], so the pointer checks pass, but the dereference follows the points-to set, so the write goes to __CPROVER_memory or to a failure object. With --keep-temps and cbmc --program-only on the generated GOTO, the write in use_step is __CPROVER_memory#1 == __CPROVER_memory#0 WITH [(long int)__CPROVER_POINTER_OFFSET(var_27!0@1#2):=7], and that pointer's points-to set is [ "object_descriptor(integer_address, unknown)" ]. In use_step_inline the write is a!0@1#3[[1]] == 7.
  • CBMC does refine the points-to set for a syntactic __CPROVER_assume(p == &x). Kani lowers p == q into a temporary first, so CBMC never sees that form, and even a direct kani::assume(c.p == &a[1]) in the harness does not help. A C program of the same shape, where p = (char *)nondet_ulong(); is followed by __CPROVER_assume(is_next(p)); with _Bool is_next(char *q) { return q == buf + 1; }, gives the same false SUCCESS on CBMC 6.11.0. With __CPROVER_assume(p == buf + 1); it fails correctly.
  • stub_verified makes the pattern systematic. The replacement is kani::any_modifies() for the result and kani::internal::write_any(...) for each modifies target, which AnyModifiesPass turns into kani::any::<T>() / write_any_slim::<T>. The ensures is the pin.

PointerGenerator's documentation says that kani::any::<usize>() as *const u8 "represents any address, and it may point to anything in memory, allocated or not" (library/kani_core/src/arbitrary/pointer.rs). The observed behaviour does not match that: such a pointer may equal the address of a live object, but writes through it never reach that object.

A PointerGenerator-based impl is not a way around this. It cannot alias the caller's memory, so with an ensures that pins the result to the caller's buffer, the post-call state becomes unreachable instead.

Possible directions:

  • lower a non-constant integer-to-pointer cast (and with_exposed_provenance{,_mut}) to a pointer-typed nondet constrained by p as usize == n. CBMC's pointer-typed nondets may alias existing objects. This would also cover the contract-free case.
  • in stub_verified, havoc pointer-holding modifies targets and results with a CBMC-level nondet of the type instead of Arbitrary::any(). This would need Arbitrary's validity assumptions (non-null, enum discriminants, ...) added back. For struct-typed targets it also interacts with a separate CBMC issue about pointer members of struct-typed nondets: diffblue/cbmc#NNNN.
  • at least, warn when stub_verified is applied to a function whose modifies targets or result contain raw pointers or NonNull.

Related:

Activity

  1. added this to the Soundness milestone on Nov 9, 2026
  2. added theissue type on Nov 9, 2026
  3. self-assigned this
    on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

T-CBMCIssue related to an existing CBMC issue[C] BugThis is a bug. Something isn't working.[F] SoundnessKani failed to detect an issue

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions