Skip to content

#[kani::stub] on contracted functions #4591

Closed

Description

@feliperodri

Using #[kani::stub(target, replacement)] where target has a #[kani::ensures] contract produces a compilation error: Failed to find contract closure __kani_recursion_check_<fn_name>.

Minimal reproducer

#[kani::ensures(|result: &i32| *result >= 0)]
fn abs_val(x: i32) -> i32 {
    if x < 0 { -x } else { x }
}

fn stub_abs_val(_x: i32) -> i32 {
    let r: i32 = kani::any();
    kani::assume(r >= 0);
    r
}

fn caller(x: i32) -> i32 {
    abs_val(x) + 1
}

#[kani::proof]
#[kani::stub(abs_val, stub_abs_val)]
fn check_caller() {
    let x: i32 = kani::any();
    let result = caller(x);
    assert!(result >= 1);
}
$ cargo kani -Z function-contracts -Z stubbing
error: Failed to find contract closure `__kani_recursion_check_abs_val` in function `abs_val`

Expected behavior

#[kani::stub] should work on functions that have contracts. The stub replaces the function body entirely, so the contract instrumentation should be irrelevant.

Impact

This prevents compositional verification where a function has been proved correct via proof_for_contract and callers want to use a standard stub (not stub_verified) to abstract it away. The workaround is to remove the ensures contract from the target function, but this breaks the proof_for_contract harness.

Activity

  1. added this to the Contracts milestone on Apr 23, 2026
  2. self-assigned this
    on Apr 23, 2026
  3. added theissue type on Apr 23, 2026
  4. added
    [C] Bug
    Z-Contracts
    on Apr 23, 2026
  5. hiSandog commented on Jul 11, 2026

    @hiSandog

    It would help to clarify whether stubbing a contracted target is intended to bypass the target contract entirely or require the replacement to satisfy it. A regression matrix should cover ensures, requires, and recursion contracts independently, because the missing __kani_recursion_check_* closure may be generated only for one contract-lowering path.

  6. feliperodri commented on Jul 11, 2026

    @feliperodri
    MemberAuthor

    Thanks, @hiSandog! I think this currently reflects a tooling limitation rather than intended semantics. With #[kani::stub(abs_val, stub_abs_val)], the abs_val body should be fully abstracted away for that harness, so requiring contract-generated closure symbols from abs_val (e.g. __kani_recursion_check_abs_val) appears inconsistent.

    In other words, stubbing and contract instrumentation are not composed correctly yet: the pipeline still resolves contract artifacts for the original target even when the call is redirected. I believe expected behavior is that plain stub works regardless of whether the target has #[kani::ensures] (unless we explicitly document otherwise).

    [Note to self] Proposed fix direction: adjust ordering/resolution so contract closure lookup is skipped or redirected when a stub replacement is active, and add this reproducer as a regression test.

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] Bug[F] Crash

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions