Description
Function contract stubs fail compilation when using slice references as function arguments. This is true for &mut [u8] as well as &[u8].
This is particularly relevant for types which implement
std::io::Readandstd::io::Writein order to reduce loop unwinding complexity. It would be nice to replace complex write implementation by their stubs so that other methods which call them can be verified without blowing up proving time. A concrete example of this can be found here. Without stubbing, an end-to-end test using a custom std::io::Write implementor ends up taking 1h40 and 72GB of RAM to validate, even on relatively small buffer sizes.
The following code provides a minimal reproducible example:
//! src/lib.rs
// Bogus implementation which will end up using the verified stub during validation
pub struct StackBuffer<'data> {
buf: &'data mut [u8],
}
impl<'data> StackBuffer<'data> {
// `is_clear` is replaced here by `stub_verified`
pub fn new(buf: &'data mut [u8]) -> Self {
assert!(util::is_clear(buf));
Self { buf }
}
// The function we want to validate with the stub.
pub fn len(&self) -> usize {
self.buf.len()
}
}
mod util {
#[cfg_attr(kani, kani::modifies(buf))]
#[cfg_attr(kani, kani::ensures(|res| *res == buf.iter().all(|n| *n == 0)))]
pub fn is_clear(buf: &[u8]) -> bool {
for i in 0..buf.len() {
if buf[i] != 0 {
return false;
}
}
true
}
}
#[cfg(all(test, kani))]
mod validate {
use super::*;
const MAX_SIZE: usize = 16;
const _: () = assert!(MAX_SIZE > 0);
#[kani::proof]
#[kani::unwind(17)]
fn is_clear_harness() {
let data: [u8; MAX_SIZE] = kani::any();
let oracle = data;
let size = kani::any();
kani::assume(size < MAX_SIZE);
assert_eq!(
util::is_clear(&data[..size]),
oracle[..size].iter().all(|n| *n == 0)
);
}
#[kani::proof_for_contract(util::is_clear)]
#[kani::unwind(17)]
fn is_clear_contract() {
let data: [u8; MAX_SIZE] = std::array::from_fn(|i| i as u8);
let size = kani::any();
kani::assume(size < MAX_SIZE);
util::is_clear(&data[..size]);
}
#[kani::proof]
#[kani::unwind(17)]
#[kani::stub_verified(util::is_clear)] // Removing this line fixes the compilation error
fn buffer_len() {
let mut data = [0; MAX_SIZE];
let size = kani::any();
kani::assume(size < MAX_SIZE);
let buffer = StackBuffer::new(&mut data[..size]);
assert_eq!(buffer.len(), size);
}
}using the following command line invocation:
kani src/lib.rs --tests --harness validate::buffer_len -Z function-contracts -Z stubbing
with Kani version: kani 0.67.0, commit 2bf550f.
I expected to see this happen:
is_clear should have been replaced by its verified stub in validate::buffer_len.
Instead, this happened:
thread 'rustc' (162965) panicked at kani-compiler/src/kani_middle/reachability.rs:420:69:
called `Result::unwrap()` on an `Err` value: Error("Failed to resolve `FnDef(DefId { id: 402, name: \"std::iter::IntoIterator::
into_iter\" })` with `GenericArgs([Type(Ty { id: 125, kind: RigidTy(Ref(Region { kind: ReErased }, Ty { id: 126, kind: RigidTy(
Slice(Ty { id: 31, kind: RigidTy(Slice(Ty { id: 22, kind: RigidTy(Uint(U8)) })) })) }, Mut)) })])`")
stack backtrace:
0: __rustc::rust_begin_unwind
1: core::panicking::panic_fmt
2: core::result::unwrap_failed
3: <kani_compiler::kani_middle::reachability::MonoItemsCollector>::reachable_items
4: kani_compiler::kani_middle::reachability::collect_reachable_items
5: <kani_compiler::codegen_cprover_gotoc::compiler_interface::GotocCodegenBackend>::codegen_items
6: <scoped_tls::ScopedKey<core::cell::Cell<*const ()>>>::set::<rustc_public::compiler_interface::run<rustc_public::rustc_int
ernal::run<<kani_compiler::codegen_cprover_gotoc::compiler_interface::GotocCodegenBackend as rustc_codegen_ssa::traits::backend
::CodegenBackend>::codegen_crate::{closure#0}, alloc::boxed::Box<dyn core::any::Any>>::{closure#0}, alloc::boxed::Box<dyn core:
:any::Any>>::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_public::error::Error>>
7: rustc_public::rustc_internal::run::<<kani_compiler::codegen_cprover_gotoc::compiler_interface::GotocCodegenBackend as rus
tc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate::{closure#0}, alloc::boxed::Box<dyn core::any::Any>>
8: <kani_compiler::codegen_cprover_gotoc::compiler_interface::GotocCodegenBackend as rustc_codegen_ssa::traits::backend::Cod
egenBackend>::codegen_crate
9: <rustc_interface::queries::Linker>::codegen_and_build_linker
10: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Kani unexpectedly panicked during compilation.
Please file an issue here: https://github.com/model-checking/kani/issues/new?labels=bug&template=bug_report.md
[Kani] no current codegen item.
[Kani] no current codegen location.
Activity
Fix write_any_slice resolution for slice modifies stubs (model-checki…
Fix compiler crash on slice-modifies verified stubs (model-checking#4748