Skip to content

Support malloc and free in loops with loop contracts #3587

Description

@qinheping

Requested feature: The loop contracts support #3151 is based on CBMC's loop contracts, which doesn't support malloc and free in loops with loop contracts. We want to support it to enable more harness.
Use case:
Link to relevant documentation (Rust reference, Nomicon, RFC): #3167

Test case: Box::new calls malloc and free implicitly. The checks for new allocated objects cannot be correctly handled in loop contract transformation, and hence Kani will report failure on the following example.

// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

// kani-flags: -Z loop-contracts

//! Check if loop contracts is correctly applied.

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

type Data = u8;
#[kani::proof]
fn main() {
    let mut i: u8 = 0;

    let mut data: Box<Data> = Box::new(0);

    #[kani::loop_invariant(*data == i)]
    while  i < 10 {
        i += 1;
        data = Box::new(i);
    }
}

Activity

  1. added
    [C] Feature / Enhancement
    on Oct 9, 2024
  2. self-assigned this
    on Oct 9, 2024
  3. added
    Z-Contracts
    on Mar 27, 2026
  4. added
    [F] Spurious Failure
    on Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Z-Contracts[C] Feature / Enhancement[F] Spurious Failure

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions