Repository navigation
Description
Dropping an Arc around a struct that holds an empty Mutex<Vec<Arc<T>>> makes CBMC unroll the vector's element drop loop, even though the vector has no elements. Whether it happens depends on the struct's other fields. With a usize, two AtomicUsize, and an AtomicU64 beside the vector, the harness times out. With one AtomicU64 beside it, the same harness verifies in a fraction of a second. The vector is Vec::new() in both, and nothing is pushed.
I hit this while verifying the Fictionet SDK, where every harness that drops one of our budget types pays for it, and several of our harnesses time out there.
I tried this code:
Cargo.toml:
[package]
name = "kani-empty-vec-drop"
version = "0.1.0"
edition = "2024"
[lib]
path = "src/lib.rs"
[workspace]src/lib.rs:
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
/// An element whose drop loops over 32 places.
pub struct Heavy([Option<Arc<u8>>; 32]);
/// The shape that times out.
pub struct Shape {
used: AtomicUsize,
limit: usize,
wanted: AtomicUsize,
wakes: AtomicU64,
list: Mutex<Vec<Arc<Heavy>>>,
}
/// A smaller shape that does not.
pub struct Small {
wakes: AtomicU64,
list: Mutex<Vec<Arc<Heavy>>>,
}
#[cfg(kani)]
mod proofs {
use super::*;
#[kani::proof]
fn shape_drops_no_element() {
let shared = Arc::new(Shape {
used: AtomicUsize::new(0),
limit: 0,
wanted: AtomicUsize::new(0),
wakes: AtomicU64::new(0),
list: Mutex::new(Vec::new()),
});
assert!(shared.limit == 0 && shared.used.load(Ordering::Relaxed) == 0);
assert!(shared.wanted.load(Ordering::Relaxed) == 0);
assert!(shared.wakes.load(Ordering::Relaxed) == 0);
drop(shared);
}
#[kani::proof]
fn small_drops_no_element() {
let shared = Arc::new(Small {
wakes: AtomicU64::new(0),
list: Mutex::new(Vec::new()),
});
assert!(shared.wakes.load(Ordering::Relaxed) == 0);
drop(shared);
}
}using the following command line invocation:
cargo kani --exact --harness proofs::small_drops_no_element -Z unstable-options --harness-timeout 60s
cargo kani --exact --harness proofs::shape_drops_no_element -Z unstable-options --harness-timeout 60s
with Kani version: 0.68.0 (cargo plugin), CBMC 6.11.0, toolchain nightly-2026-08-21, x86_64 Linux.
I expected to see this happen: both harnesses verify quickly. The vector is empty, so its drop should not reach Heavy's drop at all.
Instead, this happened:
small_drops_no_element: VERIFICATION SUCCESSFUL in 0.2 s.shape_drops_no_element: CBMC timed out at 60 s. Before the timeout, the log showsUnwinding loopfordrop_glue::<[Arc<Heavy>]>13 times and fordrop_glue::<[Option<Arc<u8>>]>434 times. The counts change a little from run to run, since they depend on how far CBMC gets in 60 s. So CBMC is droppingHeavyvalues that the empty vector does not have, and unrollingHeavy's own 32-place loop for each one.
How I narrowed it down: in our code the struct is the same shape as Shape, with a different element type in the list. The list on its own, the list behind the lock, and the list in an Arc with or without the lock all verified in under half a second. So did the full struct when it was not in an Arc. Swapping our element type for Heavy changed nothing, so the trigger seems to be the struct's layout inside an Arc, not the element type. I don't know why the other fields matter. My guess, which I have not checked, is that the empty vector's dangling pointer gets compared with its end at an offset the layout changes, and the simplifier stops deciding that comparison.
A related effect: dropping a merged enum walks a variant neither path built
This may be the same cause, so I'm including it here. Add to the same crate:
pub enum Event {
Data(Arc<Heavy>),
Written(u64),
Ended,
Reset,
}
pub enum Admit<T> {
Taken,
Partial(T),
Blocked(T),
Closed(T),
}
#[cfg(kani)]
mod merged {
use super::*;
#[kani::proof]
fn merged_admit_drops_no_heavy_value() {
let admit: Admit<Event> = if kani::any() {
Admit::Taken
} else {
Admit::Closed(Event::Written(1))
};
drop(admit);
}
#[kani::proof]
fn one_path_admit_drops_no_heavy_value() {
let admit: Admit<Event> = Admit::Closed(Event::Written(1));
drop(admit);
}
}Neither path builds an Event::Data, so there is no Heavy to drop. merged_admit_drops_no_heavy_value still verifies, but it takes 20.4 s, and the log shows Heavy's 32-place drop loop unrolled 96 times. one_path_admit_drops_no_heavy_value, with the value built on one path only, verifies in 0.13 s with no such unrolling. In an earlier run I also tried a Result<Admit<Event>, Error> merged the same way, with and without #[repr(u8)] on the enums, and saw the same 96 unrollings both ways, so the enums' niche layout doesn't seem to be the cause.
Activity