Repository navigation
Open
Labels
T-High PriorityTag issues that have high priority[C] BugThis is a bug. Something isn't working.[E] PerformanceTrack performance improvement (Time / Memory / CPU)[F] CrashKani crashed
Description
I tried this code:
fn identity(x: u32) -> u32 {
// The following two lines cause kani to loop infinitely
let helpful_log_message = format!("Called with: {}", x);
println!("{}", helpful_log_message);
// Directly calling println (without format) does not cause infinite looping
// println!("Called with: {}", x);
x
}
#[cfg(kani)]
#[kani::proof]
fn check_identity() {
let x: u32 = kani::any();
let y = identity(x);
assert_eq!(x, y);
}using the following command line invocation:
cargo-kani
with Kani version: kani was built on commit 054780c64a30192a49bc35c4f428323f07b0e85e on Amazon Linux 2, x86 In the following way:
git clone https://github.com/model-checking/kani.git
cd kani
git submodule update --init
./scripts/setup/al2/install_deps.sh
cargo build-dev -- --release
I expected to see this happen: Proof to terminate with "Verification SUCCESSFUL"
Instead, this happened:
Kani loops idenfinitely. Some logs [truncated]
aborting path on assume(false) at file <RUST_TOOLCHAIN>/lib/rustlib/src/rust/library/core/src/result.rs line 966 column 15 function std::result::Result::<std::ptr::NonNull<[u8]>, std::alloc::AllocError>::map_err::<std::collections::TryReserveError, {closure@alloc::raw_vec::RawVecInner::finish_grow::{closure#0}}> thread 0
aborting path on assume(false) at file <RUST_TOOLCHAIN>/lib/rustlib/src/rust/library/core/src/result.rs line 2173 column 15 function <std::result::Result<std::ptr::NonNull<[u8]>, std::collections::TryReserveError> as std::ops::Try>::branch thread 0
aborting path on assume(false) at file src/lib.rs line 0 column 0 function alloc::raw_vec::RawVecInner::grow_amortized thread 0
aborting path on assume(false) at file <RUST_TOOLCHAIN>/lib/rustlib/src/rust/library/alloc/src/raw_vec/mod.rs line 550 column 9 function alloc::raw_vec::RawVecInner::<A>::reserve::do_reserve_and_handle::<std::alloc::Global> thread 0
aborting path on assume(false) at file <RUST_TOOLCHAIN>/lib/rustlib/src/rust/library/alloc/src/collections/mod.rs line 93 column 10 function <std::collections::TryReserveErrorKind as std::clone::Clone>::clone thread 0
aborting path on assume(false) at file <RUST_TOOLCHAIN>/lib/rustlib/src/rust/library/alloc/src/raw_vec/mod.rs line 845 column 11 function alloc::raw_vec::handle_error thread 0
aborting path on assume(false) at file <RUST_TOOLCHAIN>/lib/rustlib/src/rust/library/alloc/src/alloc.rs line 366 column 5 function std::alloc::handle_alloc_error::rt_error thread 0
aborting path on assume(false) at file <RUST_TOOLCHAIN>/lib/rustlib/src/rust/library/alloc/src/alloc.rs line 407 column 13 function std::alloc::handle_alloc_error::rt_error thread 0
aborting path on assume(false) at file <RUST_TOOLCHAIN>/lib/rustlib/src/rust/library/alloc/src/alloc.rs line 413 column 9 function std::alloc::handle_alloc_error thread 0
aborting path on assume(false) at file <RUST_TOOLCHAIN>/lib/rustlib/src/rust/library/alloc/src/raw_vec/mod.rs line 847 column 38 function alloc::raw_vec::handle_error thread 0
aborting path on assume(false) at file <RUST_TOOLCHAIN>/lib/rustlib/src/rust/library/alloc/src/raw_vec/mod.rs line 28 column 5 function alloc::raw_vec::capacity_overflow thread 0
aborting path on assume(false) at file <RUST_TOOLCHAIN>/lib/rustlib/src/rust/library/alloc/src/raw_vec/mod.rs line 846 column 29 function alloc::raw_vec::handle_error thread 0
aborting path on assume(false) at file <RUST_TOOLCHAIN>/lib/rustlib/src/rust/library/alloc/src/raw_vec/mod.rs line 558 column 17 function alloc::raw_vec::RawVecInner::<A>::reserve::do_reserve_and_handle::<std::alloc::Global
Metadata
Metadata
Assignees
Labels
T-High PriorityTag issues that have high priority[C] BugThis is a bug. Something isn't working.[E] PerformanceTrack performance improvement (Time / Memory / CPU)[F] CrashKani crashed
Activity
gshaurya18 commentedon Dec 31, 2025
On Amazon Linux 2023, the same proof succeeds
Kani was installed as follows:
zhassan-aws commentedon Jan 5, 2026
This is the culprit commit: f4d4a94.
Before this commit, verification completes in a few seconds.
zhassan-aws commentedon Jan 5, 2026
Somehow, the upstream change (rust-lang/rust#148789) is causing the formatting code to fall on the verification path, even though the result of the
format!call is not relevant to verification in this example.zhassan-aws commentedon Jan 12, 2026
This is the MIR for the
identityfunction, before and after the change:zhassan-aws commentedon Jan 12, 2026
I'm not sure there's a way to address this on the Kani side.
@gshaurya18 one workaround is to guard the formatting/printing code under
cfg!(not(kani)), e.g.:With this change, the code verifies quickly:
RESULTS: Check 1: main.assertion.1 - Status: SUCCESS - Description: "assertion failed: x == y" - Location: src/main.rs:17:5 in function main SUMMARY: ** 0 of 1 failed VERIFICATION:- SUCCESSFUL Verification Time: 0.028873827sSay why Kani does not finish on format!, and how to shape a harness t…
jaschadub commentedon Nov 24, 2026
I ran into this on real code and found a workaround that keeps
format!in place, plus what seems to trigger the blow-up.Setup: Kani 0.68.0, CBMC 6.11.0, toolchain nightly-2026-08-21, x86_64 Linux.
Workaround: stub the capacity hint
Run with
-Z stubbing. Thewhere 'a: 'ais needed; without it Kani rejects the stub with "mismatch in the number of generic parameters".On the reproducer from this issue:
memchr_naiveand a recursion inPadAdapter::write_str)VERIFICATION:- SUCCESSFULin 4 s, 38,120 program stepsWhat triggers it
alloc::fmt::formatdoesString::with_capacity(args.estimated_capacity())and thenwrite_fmt. Since rust-lang/rust#148789 that capacity comes from a walk over the template bytes. Probes withaas any valid 1-byte&str:format!("[{a}")write!(&mut String::new(), "[{a}")let cap = args.estimated_capacity(); String::with_capacity(cap), thenwrite_fmt(args)String::with_capacity(2)(hint still computed, not used)format!("{a}]")(placeholder first)format!("{a}{a}")assert!(cap == 2)passes in the third probe, so Kani can prove the value. It just does not seem to treat it as a constant during symbolic execution.The slow cases all format a placeholder after something was already written to the String. In those runs CBMC logs
Unwinding recursion <&dyn std::fmt::Debug as std::fmt::Debug>::fmtup to the unwind bound, although the argument is a&strwithDisplay. So it looks as if the call through the argument's formatter function pointer is no longer resolved to one target once the String has a capacity that symex does not know. I did not find out why the unknown capacity has that effect.Since
estimated_capacityonly sizes the first allocation, a built-in stub or model for it in Kani might be a cheap fix.