Skip to content

Commit

Permalink
Remove assigns clause for ZST pointers (#3417)
Browse files Browse the repository at this point in the history
This PR filters out ZST pointee types when generating CMBC assigns
clauses for contracts. This prevents CMBC from complaining that the
pointer doesn't point to a valid allocation.

Resolves #3181

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
  • Loading branch information
carolynzech authored Aug 5, 2024
1 parent b24c01b commit 5424bc5
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 13 deletions.
10 changes: 8 additions & 2 deletions kani-compiler/src/codegen_cprover_gotoc/codegen/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT
use crate::codegen_cprover_gotoc::GotocCtx;
use crate::codegen_cprover_gotoc::{codegen::ty_stable::pointee_type_stable, GotocCtx};
use crate::kani_middle::attributes::KaniAttributes;
use cbmc::goto_program::FunctionContract;
use cbmc::goto_program::{Expr, Lambda, Location, Type};
Expand Down Expand Up @@ -160,11 +160,17 @@ impl<'tcx> GotocCtx<'tcx> {
let TyKind::RigidTy(RigidTy::Tuple(modifies_tys)) = modifies_ty.kind() else {
unreachable!("found {:?}", modifies_ty.kind())
};

for ty in &modifies_tys {
assert!(ty.kind().is_any_ptr(), "Expected pointer, but found {}", ty);
}

let assigns: Vec<_> = modifies_tys
.into_iter()
// do not attempt to dereference (and assign) a ZST
.filter(|ty| !self.is_zst_stable(pointee_type_stable(*ty).unwrap()))
.enumerate()
.map(|(idx, ty)| {
assert!(ty.kind().is_any_ptr(), "Expected pointer, but found {}", ty);
let ptr = modifies_args.clone().member(idx.to_string(), &self.symbol_table);
if self.is_fat_pointer_stable(ty) {
let unref = match ty.kind() {
Expand Down
5 changes: 5 additions & 0 deletions tests/expected/function-contract/modifies/zst_pass.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.assertion\
- Status: SUCCESS\
- Description: "ptr NULL or writable up to size"\

VERIFICATION:- SUCCESSFUL
22 changes: 22 additions & 0 deletions tests/expected/function-contract/modifies/zst_pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT
// kani-flags: -Zfunction-contracts

#[kani::modifies(dst)]
pub unsafe fn replace<T>(dst: *mut T, src: T) -> T {
std::ptr::replace(dst, src)
}

#[kani::proof_for_contract(replace)]
pub fn check_replace_unit() {
check_replace_impl::<()>();
}

fn check_replace_impl<T: kani::Arbitrary + Eq + Clone>() {
let mut dst = T::any();
let orig = dst.clone();
let src = T::any();
let ret = unsafe { replace(&mut dst, src.clone()) };
assert_eq!(ret, orig);
assert_eq!(dst, src);
}
6 changes: 1 addition & 5 deletions tests/std-checks/core/mem.expected
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
Checking harness mem::verify::check_swap_unit...

Failed Checks: ptr NULL or writable up to size

Summary:
Verification failed for - mem::verify::check_swap_unit
Complete - 6 successfully verified harnesses, 1 failures, 7 total.
Complete - 7 successfully verified harnesses, 0 failures, 7 total.
3 changes: 1 addition & 2 deletions tests/std-checks/core/ptr.expected
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Summary:
Verification failed for - ptr::verify::check_replace_unit
Verification failed for - ptr::verify::check_as_ref_dangling
Complete - 4 successfully verified harnesses, 2 failures, 6 total.
Complete - 5 successfully verified harnesses, 1 failures, 6 total.
2 changes: 0 additions & 2 deletions tests/std-checks/core/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ mod verify {
contracts::swap(&mut x, &mut y)
}

/// FIX-ME: Modifies clause fail with pointer to ZST.
/// <https://github.com/model-checking/kani/issues/3181>
#[kani::proof_for_contract(contracts::swap)]
pub fn check_swap_unit() {
let mut x: () = kani::any();
Expand Down
2 changes: 0 additions & 2 deletions tests/std-checks/core/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ mod verify {
let _rf = unsafe { contracts::as_ref(&non_null) };
}

/// FIX-ME: Modifies clause fail with pointer to ZST.
/// <https://github.com/model-checking/kani/issues/3181>
#[kani::proof_for_contract(contracts::replace)]
pub fn check_replace_unit() {
check_replace_impl::<()>();
Expand Down

0 comments on commit 5424bc5

Please sign in to comment.