Skip to content

Define the alloc models in kani_core for the verify-std flow - #4863

Closed
srivatsansamraj wants to merge 2 commits into
model-checking:mainfrom
srivatsansamraj:alloc-models
Closed

srivatsansamraj wants to merge 2 commits into
model-checking:mainfrom
srivatsansamraj:alloc-models

Conversation

@srivatsansamraj

Copy link
Copy Markdown
Contributor

The heap models autoharness relies on now exist in the verify-std flow. That flow compiles core, alloc and std against kani_core. The Arbitrary implementations for Box, Rc and Arc, the BoundedArbitrary implementations for Box<[T]>, Vec, String, BTreeMap and BTreeSet, and their models (any_box, any_rc, any_arc, the unbounded slice and vector models, the slice_validity_assume hook) lived only in the kani crate. kani_lib!(core) cannot define them, because core cannot name alloc's types.

Changes:

  • kani_core gains generate_alloc_arbitrary!, holding those items, moved from library/kani unchanged apart from paths and formatting. The kani crate expands it as before. Its hidden models move from kani::arbitrary:: to the crate root, beside the other models; nothing refers to them by path.
  • kani_lib! gains an (alloc) arm that expands it in a kani module inside alloc.
  • Without a kani crate, find_kani_functions collects the marked functions from both core and alloc and merges them, since the two arms define disjoint sets. The is_optional docs now say when each optional model is missing.

The new test verify_std_alloc injects kani_lib!(alloc) into a copy of alloc and runs autoharness on first(p: Box<Pair>) and len(v: Vec<u8>). Both get harnesses and verify. Without the compiler change, first is skipped for a missing Arbitrary implementation and len asks for --bounded-arguments.

The test runs autoharness --std, which fails on main with #4839 until #4848 lands, so this branch includes #4848's commit. Only the last commit is new here.

verify-rust-std turns this on with one line in library/alloc/src/lib.rs, kani_core::kani_lib!(alloc);, beside the existing one for core. Autoharness there then treats Box, Rc, Arc and Vec arguments as it already does in an ordinary crate.

Resolves #4807

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

feliperodri and others added 2 commits September 24, 2026 15:02
`kani autoharness` aborted on any crate declaring a `#[rustc_comptime]`
function -- `core` has 27 of them, so the whole standard library run died
during candidate collection:

    do not use `optimized_mir` for constants: Const { allow_const_fn_promotion: false }

The const-block precondition search added in model-checking#4820 guarded its fetch with
`is_mir_available` and then called `optimized_mir`. That guard is not enough: a
`#[rustc_comptime]` function's body is a const context, which rustc's
`optimized_mir` provider refuses to serve even though the MIR is available.
`instance_mir` is rustc's own dispatcher between `optimized_mir` and
`mir_for_ctfe`, so use it rather than duplicating the choice here.

The three shapes `core` has are now covered by a test: a body-less
`#[rustc_intrinsic] #[rustc_comptime]` declaration (`intrinsics::size_of`), a
`#[rustc_comptime]` function with a body (`TypeId::trait_info_of`), and one
whose body carries a const-block precondition on a const generic. The last
matters most: switching providers must not lose the detection model-checking#4820 exists for,
and the test also pins the plain `const fn` shape that `core::escape` uses.

Resolves model-checking#4839
The `Arbitrary` implementations for `Box`, `Rc` and `Arc`, the
`BoundedArbitrary` implementations for `Box<[T]>`, `Vec`, `String`,
`BTreeMap` and `BTreeSet`, and the models autoharness uses for them
(`any_box`, `any_rc`, `any_arc`, the unbounded slice and vector models,
the `slice_validity_assume` hook) lived only in the `kani` crate.
`verify-std` compiles `core`, `alloc` and `std` against `kani_core`, so
none of them existed there.

They move, unchanged apart from paths and formatting, into
`generate_alloc_arbitrary!` in `kani_core`. The `kani` crate expands it as
before, and a new `kani_lib!(alloc)` arm expands it inside `alloc`.
Without a `kani` crate, `find_kani_functions` now collects the marked
functions from `core` and `alloc` and merges them; the two arms define
disjoint sets.
@srivatsansamraj
srivatsansamraj requested review from a team as code owners September 24, 2026 22:17
@github-actions github-actions Bot added Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-CompilerBenchCI Tag a PR to run benchmark CI labels Sep 24, 2026
@srivatsansamraj

Copy link
Copy Markdown
Contributor Author

Closing in favour of #4871.

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

Labels

Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Autoharness: models for alloc types in the verify-std flow

2 participants