Skip to content

Let alloc define the Kani models for its own types - #4871

Draft
feliperodri wants to merge 1 commit into
model-checking:mainfrom
feliperodri:feat-4807-alloc-models-verify-std
Draft

feliperodri wants to merge 1 commit into
model-checking:mainfrom
feliperodri:feat-4807-alloc-models-verify-std

Conversation

@feliperodri

Copy link
Copy Markdown
Member

Draft — I want feedback on the shape before polishing. Takes the approach @srivatsansamraj proposed in #4807.

Why

When we verify the standard library, autoharness can't make up a Vec or a String, so it skips every function that takes one. The models aren't missing — they're in library/kani — but verify-std builds core/alloc/std against kani_core, and the only hook is kani_lib!(core). core can't name Vec, so there's no way in. That's a blind spot in the containers most std code is actually about, in the one flow where we're trying to verify all of std.

What's here

A kani_lib!(alloc) arm, and the alloc-dependent definitions moved out of library/kani into kani_core so both flows expand the same source: Arbitrary for Box/Rc/Arc, BoundedArbitrary for Vec/String/Box<[T]>/BTreeMap/BTreeSet, and the any_vec_unbounded/any_slice_*_unbounded/any_box/any_rc/any_arc models. Plus one compiler fix: find_kani_functions stopped at the first crate with any marked function, so anything defined in alloc would have been ignored even once it existed.

On verify-rust-std's side this is two lines in alloc/src/lib.rs: the kani feature gate and the macro call.

What I'd like input on

Is the (alloc) arm the right seam? It works — the test injects it into a real std copy and both a Vec and a Box harness verify inside alloc — but it means kani_core now has a macro whose body only makes sense in one specific crate, and kani_lib!(alloc) silently depends on kani_lib!(core) having run. If you'd rather see that dependency expressed some other way, now's the time.

Scope. I deliberately left three things out and want to know if any of them belong here: kani::vec::any_vec (public API, #4027 is deprecating it anyway); the nondet_fn* models (missing from verify-std too, but nothing to do with alloc); and HashMap/HashSet, which need std, i.e. a kani_lib!(std) arm that doesn't exist yet.

Risk

The move is the risky part, not the new arm — library/kani is what regular cargo kani users get, and I've relocated ~350 lines of it. So I checked rather than assumed: autoharness --list on a probe crate taking Vec<i32>, &Vec<i32>, &mut Vec<i32>, String, Box<i32>, Rc<i32>, Arc<i32>, Box<[i32]>, BTreeMap<i32, i32> and &[i32] produces byte-identical output before and after. Everything else is additive.

Two things worth knowing

This unlocks by-value arguments, not &mut Vec<T>. The issue counts ~120 Vec methods, but most take &mut self, and a reference to a BoundedArbitrary type isn't supported today — that's true in the regular flow too, not something this PR introduces. Closing that needs the same treatment slices get (generate the value, hand out a reference to it), which I'd do separately. So think of this as the prerequisite, with the by-value functions (Vec::into_iter, String::into_bytes, and the alloc/std functions taking these by value) as the immediate win.

I couldn't get a full-std autoharness --list to compare against the issue's numbers. Two unrelated ICEs get in the way: the one #4848 fixes, and an assertion in intrinsics.rs:243 while compiling proc_macro (assertion failed: matches!(output.kind(), RigidTy::Int(I32))). Identical with and without this change, so it isn't mine — I'll file the second one. The verify-std test is what actually demonstrates the models resolving in the no-core flow.

Suites: kani 611, expected 478, ui 152, cargo-kani 71, cargo-ui 30, script-based-pre 76 — all passing, plus fmt, clippy and the unit tests.

Towards #4807

`kani verify-std` compiles `core`, `alloc` and `std` against `kani_core`, and the
only way to inject Kani definitions is `kani_lib!(core)`. `core` cannot name
`Vec`, so no amount of work in `core::kani` can give autoharness a way to make
one up, and every std function taking an `alloc` container is skipped for a
missing `Arbitrary`/`BoundedArbitrary` implementation.

Add a `kani_lib!(alloc)` arm that defines the implementations and models needing
`alloc`'s types, and move them out of `library/kani` into `kani_core` so both
flows expand one definition: `Arbitrary` for `Box`/`Rc`/`Arc`,
`BoundedArbitrary` for `Vec`/`String`/`Box<[T]>`/`BTreeMap`/`BTreeSet`, and the
`any_box`/`any_rc`/`any_arc`/`any_vec_unbounded`/`any_slice_*_unbounded` models
with the `slice_validity_assume` hook they rely on.

`find_kani_functions` stopped at the first crate with any marked function, so
markers defined in `alloc` would have been ignored even once they existed; it now
collects from every crate it looks at, keeping the first definition of each.

The `verify-std` test gains a phase that injects `kani_lib!(alloc)` and verifies
a `Vec` and a `Box` harness inside `alloc` itself. The regular `cargo kani` flow
is unchanged: `autoharness --list` reports the same support for `Vec`, `String`,
`Box`, `Rc`, `Arc`, `BTreeMap`, `Box<[T]>` and `&[T]` arguments as before the
move.

Towards model-checking#4807
@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 25, 2026
@srivatsansamraj

srivatsansamraj commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

#4863 does the same thing, so it's closed.

#4863's test runs autoharness --std over two alloc functions whose harnesses need any_box and any_vec_unbounded, so it fails without the find_kani_functions change. The proofs in verify_alloc.rs here call the Arbitrary/BoundedArbitrary impls directly and still pass with that change reverted. We can push the test to this branch or open it on top.

On the dependency: pub use core::kani::* makes a missing kani_lib!(core) a compile error at that line, so the doc comment is enough.

Scope: agree on leaving all three out; we'll send the nondet_fn* move separately.

For &mut Vec<T>: the team has already planned a fix. It covers up to 282 functions in our std listing that take a reference to Vec, String, BTreeMap, BTreeSet or Box<[T]>. If you want, we can put it up once this merges.

@feliperodri feliperodri self-assigned this Oct 2, 2026
@feliperodri feliperodri added [C] Feature / Enhancement A new feature request or enhancement to an existing feature. T-High Priority Tag issues that have high priority Z-Autoharness Issue related to autoharness subcommand labels Nov 6, 2026
@feliperodri feliperodri added this to the Autoharness milestone Nov 6, 2026
@feliperodri feliperodri removed the Z-Autoharness Issue related to autoharness subcommand label Nov 6, 2026
@feliperodri feliperodri removed this from the Autoharness milestone Nov 6, 2026
@feliperodri feliperodri removed their assignment Nov 7, 2026

This branch has not been deployed

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

Labels

[C] Feature / Enhancement A new feature request or enhancement to an existing feature. T-High Priority Tag issues that have high priority 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.

2 participants