Repository navigation
Description
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:
- the replacement havocs
modifiestargets and the result withkani::any(); - Kani has no
Arbitraryfor raw pointers orNonNull(Compilation Error When Stubbing Functions That Return Pointers Using Function Contracts #3732,stub_verifiedpanics the compiler when amodifiestarget's type does not implementkani::Arbitrary#5005), so a pointer-holding target or result needs a hand-written impl; - an impl that can return a pointer into memory the caller already owns, which a pinning
ensuresrequires, has to build it from an integer or from bytes; - the proven
ensuresthen pins it.
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 pluskani::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 aroundOption<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_memoryor to a failure object. With--keep-tempsandcbmc --program-onlyon the generated GOTO, the write inuse_stepis__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)" ]. Inuse_step_inlinethe write isa!0@1#3[[1]] == 7. - CBMC does refine the points-to set for a syntactic
__CPROVER_assume(p == &x). Kani lowersp == qinto a temporary first, so CBMC never sees that form, and even a directkani::assume(c.p == &a[1])in the harness does not help. A C program of the same shape, wherep = (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_verifiedmakes the pattern systematic. The replacement iskani::any_modifies()for the result andkani::internal::write_any(...)for eachmodifiestarget, whichAnyModifiesPassturns intokani::any::<T>()/write_any_slim::<T>. Theensuresis 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 byp 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-holdingmodifiestargets and results with a CBMC-level nondet of the type instead ofArbitrary::any(). This would needArbitrary'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_verifiedis applied to a function whosemodifiestargets or result contain raw pointers orNonNull.
Related:
- Compilation Error When Stubbing Functions That Return Pointers Using Function Contracts #3732 and
stub_verifiedpanics the compiler when amodifiestarget's type does not implementkani::Arbitrary#5005:Arbitraryfor pointer-typed targets and results. - Autoharness: do not derive
Arbitrarythrough aNonNullfield #4864: it describes verify-rust-std'sNonNullimpl as an arbitrary integer that points to no allocation, and covers the read side. - Generate non-deterministic pointer variables #3696: an example of writing through
kani::any::<usize>() as *mut T. - Integer to pointer casting behaviour? diffblue/cbmc#8103: integer-to-pointer casts and value sets. That issue shows a spurious failure on a read; here the write side gives a false SUCCESS.
Activity