Skip to content

Call to format! causes an infinite loop #4507

Open

Description

@gshaurya18

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

Activity

  1. added
    [C] Bug
    on Dec 31, 2025
  2. gshaurya18 commented on Dec 31, 2025

    @gshaurya18
    Author

    On Amazon Linux 2023, the same proof succeeds

    VERIFICATION:- SUCCESSFUL
    Verification Time: 3.8319087s
    

    Kani was installed as follows:

    cargo install --locked kani-verifier
    cargo kani setup
    cargo-kani --version
    cargo-kani 0.66.0
    
  3. zhassan-aws commented on Jan 5, 2026

    @zhassan-aws
    Contributor

    This is the culprit commit: f4d4a94.

    Before this commit, verification completes in a few seconds.

  4. added
    [E] Performance
    on Jan 5, 2026
  5. zhassan-aws commented on Jan 5, 2026

    @zhassan-aws
    Contributor

    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.

  6. zhassan-aws commented on Jan 12, 2026

    @zhassan-aws
    Contributor

    This is the MIR for the identity function, before and after the change:

    Image
  7. zhassan-aws commented on Jan 12, 2026

    @zhassan-aws
    Contributor

    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.:

    fn identity(x: u32) -> u32 {
        if cfg!(not(kani)) {
            // 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
    }

    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.028873827s
  8. added theissue type on Apr 1, 2026
  9. added
    T-High Priority
    on Nov 5, 2026
  10. self-assigned this
    on Nov 5, 2026
  11. jaschadub commented on Nov 24, 2026

    @jaschadub

    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

    // Only a hint for the first allocation of the String, so a constant changes no result.
    fn no_hint<'a>(_args: &core::fmt::Arguments<'a>) -> usize where 'a: 'a { 0 }
    
    #[kani::proof]
    #[kani::stub(std::fmt::Arguments::estimated_capacity, no_hint)]
    fn check_identity() { /* the harness from this issue, unchanged */ }

    Run with -Z stubbing. The where 'a: 'a is needed; without it Kani rejects the stub with "mismatch in the number of generic parameters".

    On the reproducer from this issue:

    Result
    unchanged no result in 240 s (unwinding memchr_naive and a recursion in PadAdapter::write_str)
    with the stub VERIFICATION:- SUCCESSFUL in 4 s, 38,120 program steps

    What triggers it

    alloc::fmt::format does String::with_capacity(args.estimated_capacity()) and then write_fmt. Since rust-lang/rust#148789 that capacity comes from a walk over the template bytes. Probes with a as any valid 1-byte &str:

    Probe Program steps Time
    format!("[{a}") 458,326 28 s
    write!(&mut String::new(), "[{a}") 36,062 2 s
    let cap = args.estimated_capacity(); String::with_capacity(cap), then write_fmt(args) 439,127 28 s
    same, but String::with_capacity(2) (hint still computed, not used) 39,180 2 s
    format!("{a}]") (placeholder first) 66,972 4 s
    format!("{a}{a}") 459,118 28 s

    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>::fmt up to the unwind bound, although the argument is a &str with Display. 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_capacity only sizes the first allocation, a built-in stub or model for it in Kani might be a cheap fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

T-High Priority[C] Bug[E] Performance[F] Crash

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions