Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contracts: History Expressions via "old" monad #3232

Merged
merged 73 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
03b4cf3
rename_result
pi314mm May 28, 2024
6403650
globalize result name
pi314mm May 28, 2024
679c39e
closure syntax
pi314mm May 30, 2024
713f412
attempting to fix tests
May 30, 2024
aa42787
more tests pass
May 30, 2024
d00b229
change rewriter
May 31, 2024
e063b85
more tests pass
May 31, 2024
8e2e610
change to pass by reference
May 31, 2024
f26ff82
Merge branch 'main' into result_rename
pi314mm May 31, 2024
895cd35
fix more tests
May 31, 2024
ddb6f9c
comments
Jun 3, 2024
c70576b
Merge branch 'main' into result_rename
pi314mm Jun 3, 2024
4518865
format
Jun 3, 2024
ea8d809
curried remember
May 31, 2024
b6d2981
build_ensures function
May 31, 2024
9b06ee3
count_remembers
Jun 3, 2024
0d08b9b
counting works
Jun 3, 2024
8ae7251
format
Jun 3, 2024
f4b40ff
merge resolution
Jun 5, 2024
c2654ec
change currying
Jun 5, 2024
ee07387
old
Jun 6, 2024
491d2fa
format
Jun 6, 2024
82f19ee
changed dereference to prevent bug
Jun 6, 2024
b9d98b1
format
Jun 6, 2024
4be3bea
more tests
Jun 6, 2024
e614ad3
more tests
Jun 6, 2024
ed9bb8b
interesting test
Jun 6, 2024
9894f88
more code documentation
Jun 7, 2024
e172179
more documentation
Jun 10, 2024
dd1d8a6
fix formating
Jun 10, 2024
f9414d3
Merge branch 'main' into old
pi314mm Jun 10, 2024
4a857f9
Update library/kani/src/contracts.rs
pi314mm Jun 11, 2024
0b2edec
Update library/kani/src/contracts.rs
pi314mm Jun 11, 2024
2b93e77
more details in tests
Jun 11, 2024
bb6aed8
format
Jun 11, 2024
2251318
rename
Jun 11, 2024
ab330bb
Merge branch 'main' into old
pi314mm Jun 11, 2024
ad7e2c7
changed from vec to hashmap
Jun 11, 2024
e74ac89
Merge branch 'main' into old
pi314mm Jun 11, 2024
dd3d135
rearranged place where renaming happens
Jun 11, 2024
bde7e4a
fixed test
Jun 11, 2024
6c42043
change doc
Jun 11, 2024
3ed6da6
format
Jun 11, 2024
741315d
delete test
Jun 11, 2024
5fc8bdd
Merge branch 'main' into old
pi314mm Jun 12, 2024
8668560
Merge branch 'main' into old
pi314mm Jun 14, 2024
10ee621
newlines
Jun 17, 2024
4742560
change closure
Jun 17, 2024
a44a835
Update library/kani_macros/src/sysroot/contracts/mod.rs
pi314mm Jun 17, 2024
2db0f90
Update library/kani_macros/src/sysroot/contracts/shared.rs
pi314mm Jun 17, 2024
ed9fa7a
Update library/kani_macros/src/sysroot/contracts/shared.rs
pi314mm Jun 17, 2024
e915170
Update library/kani_macros/src/sysroot/contracts/shared.rs
pi314mm Jun 17, 2024
014b6fd
Merge branch 'main' into old
pi314mm Jun 17, 2024
9a54f4f
comments
Jun 17, 2024
9c70a07
stub test
Jun 17, 2024
7453f9e
rfc update
Jun 17, 2024
5070247
Merge branch 'main' into old
pi314mm Jun 17, 2024
80d51e3
Update tests/expected/function-contract/history/clone_pass.rs
pi314mm Jun 17, 2024
008e514
Apply suggestions from code review
pi314mm Jun 17, 2024
9b4e4c7
Update side_effect.rs
pi314mm Jun 17, 2024
801f0df
stub_verified
Jun 17, 2024
d54ce2e
added links
Jun 17, 2024
09c6360
Merge branch 'main' into old
pi314mm Jun 17, 2024
219287e
fix stub
Jun 17, 2024
ef191cc
Merge branch 'old' of github.com:pi314mm/kani into old
Jun 17, 2024
a8a5510
Merge branch 'main' into old
pi314mm Jun 17, 2024
1d3d353
Merge branch 'main' into old
pi314mm Jun 18, 2024
c89feda
Merge branch 'main' into old
pi314mm Jun 18, 2024
95d12c8
more tests
Jun 18, 2024
c088144
hash
Jun 18, 2024
a0ab61b
ignore test
Jun 18, 2024
1a78ea8
format
Jun 18, 2024
ed33b8b
small edits to tests
Jun 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions library/kani/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,27 @@
//! Rust pointer type (`&T`, `&mut T`, `*const T` or `*mut T`). In addition `T`
//! must implement [`Arbitrary`](super::Arbitrary). This is used to assign
//! `kani::any()` to the location when the function is used in a `stub_verified`.
//!
//! ## History Variables
pi314mm marked this conversation as resolved.
Show resolved Hide resolved
//!
//! Additionally, an ensures clause is allowed to refer to history computations
pi314mm marked this conversation as resolved.
Show resolved Hide resolved
//! via an `old` monad. Any instance of `old(computation)` will evaluate the
//! computation before the function is called. It is required that this computation
//! is effect free and closed with respect to the function arguments.
pi314mm marked this conversation as resolved.
Show resolved Hide resolved
//!
//! For example, the following code passes kani tests:
//!
//! ```
//! #[kani::modifies(a)]
//! #[kani::ensures(|result| old(*a).wrapping_add(1) == *a)]
//! #[kani::ensures(|result : &u32| old(*a).wrapping_add(1) == *result)]
//! fn add1(a : &mut u32) -> u32 {
//! *a=a.wrapping_add(1);
//! *a
//! }
//! ```
//!
//! Here, the value stored in `a` is precomputed and remembered after the function
//! is called, even though the contents of `a` changed during the function execution.
pi314mm marked this conversation as resolved.
Show resolved Hide resolved
//!
pub use super::{ensures, modifies, proof_for_contract, requires, stub_verified};
9 changes: 7 additions & 2 deletions library/kani_macros/src/sysroot/contracts/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use syn::{Expr, FnArg, ItemFn, Token};

use super::{
helpers::*,
shared::{make_unsafe_argument_copies, try_as_result_assign_mut},
shared::{
build_ensures, count_remembers, make_unsafe_argument_copies, try_as_result_assign_mut,
},
ContractConditionsData, ContractConditionsHandler, INTERNAL_RESULT_IDENT,
};

Expand All @@ -25,6 +27,7 @@ impl<'a> ContractConditionsHandler<'a> {
pub fn make_check_body(&mut self) -> TokenStream2 {
let mut inner = self.ensure_bootstrapped_check_body();
let Self { attr_copy, .. } = self;
let remember_count: usize = count_remembers(&inner);

match &self.condition_type {
ContractConditionsData::Requires { attr } => {
Expand All @@ -35,11 +38,12 @@ impl<'a> ContractConditionsHandler<'a> {
}
ContractConditionsData::Ensures { argument_names, attr } => {
let (arg_copies, copy_clean) = make_unsafe_argument_copies(&argument_names);
let (remembers_stmts, ensures_clause) = build_ensures(attr, remember_count);

// The code that enforces the postconditions and cleans up the shallow
// argument copies (with `mem::forget`).
let exec_postconditions = quote!(
kani::assert(#attr, stringify!(#attr_copy));
kani::assert(#ensures_clause, stringify!(#attr_copy));
#copy_clean
);

Expand All @@ -52,6 +56,7 @@ impl<'a> ContractConditionsHandler<'a> {
let result = Ident::new(INTERNAL_RESULT_IDENT, Span::call_site());
quote!(
#arg_copies
#remembers_stmts
#(#inner)*
#exec_postconditions
#result
Expand Down
18 changes: 18 additions & 0 deletions library/kani_macros/src/sysroot/contracts/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,21 @@ pub fn short_hash_of_token_stream(stream: &proc_macro::TokenStream) -> u64 {
let long_hash = hasher.finish();
long_hash % SIX_HEX_DIGITS_MASK
}

macro_rules! assert_spanned_err {
($condition:expr, $span_source:expr, $msg:expr, $($args:expr),+) => {
if !$condition {
$span_source.span().unwrap().error(format!($msg, $($args),*)).emit();
assert!(false);
}
};
($condition:expr, $span_source:expr, $msg:expr $(,)?) => {
if !$condition {
$span_source.span().unwrap().error($msg).emit();
assert!(false);
}
};
($condition:expr, $span_source:expr) => {
assert_spanned_err!($condition, $span_source, concat!("Failed assertion ", stringify!($condition)))
};
}
pi314mm marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 3 additions & 6 deletions library/kani_macros/src/sysroot/contracts/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ use std::collections::{HashMap, HashSet};

use proc_macro::{Diagnostic, TokenStream};
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
use quote::quote;
use syn::{spanned::Spanned, visit::Visit, visit_mut::VisitMut, Expr, ExprClosure, ItemFn};
use syn::{spanned::Spanned, visit::Visit, visit_mut::VisitMut, ExprClosure, ItemFn};

use super::{
helpers::{chunks_by, is_token_stream_2_comma, matches_path},
ContractConditionsData, ContractConditionsHandler, ContractConditionsType,
ContractFunctionState, INTERNAL_RESULT_IDENT,
ContractFunctionState,
};

impl<'a> TryFrom<&'a syn::Attribute> for ContractFunctionState {
Expand Down Expand Up @@ -84,9 +83,7 @@ impl<'a> ContractConditionsHandler<'a> {
ContractConditionsType::Ensures => {
let mut data: ExprClosure = syn::parse(attr)?;
let argument_names = rename_argument_occurrences(&annotated_fn.sig, &mut data);
let result: Ident = Ident::new(INTERNAL_RESULT_IDENT, Span::call_site());
let app: Expr = Expr::Verbatim(quote!((#data)(&#result)));
ContractConditionsData::Ensures { argument_names, attr: app }
ContractConditionsData::Ensures { argument_names, attr: data }
}
ContractConditionsType::Modifies => {
ContractConditionsData::new_modifies(attr, &mut output)
Expand Down
70 changes: 68 additions & 2 deletions library/kani_macros/src/sysroot/contracts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,81 @@
//! }
//! }
//! ```
//!
//! Additionally, there is functionality that allows the referencing of
//! history values within the ensures statement. This means we can
//! precompute a value before the function is called and have access to
//! this value in the later ensures statement. This is done via the
//! `old` monad which lets you access the old state within the present
//! state. Each occurance of old is lifted, so is is necessary that
//! each lifted occurance is closed with respect to the function arguments.
pi314mm marked this conversation as resolved.
Show resolved Hide resolved
//! The results of these old computations are placed into
//! `remember_kani_internal_XXX` variables of incrementing index to avoid
//! collisions of variable names. Consider the following example:
//!
//! ```
//! #[kani::modifies(a)]
//! #[kani::ensures(|result| old(*a).wrapping_add(1) == *a)]
//! #[kani::ensures(|result : &u32| old(*a).wrapping_add(1) == *result)]
//! fn add1(a : &mut u32) -> u32 {
//! *a=a.wrapping_add(1);
//! *a
//! }
//!
//! #[kani::proof_for_contract(add1)]
//! fn verify_success() {
//! let a : &mut u32 = &mut kani::any();
//! add1(a);
//! }
//! ```
//!
//! This expands to
//!
//! ```
//! #[allow(dead_code, unused_variables)]
//! #[kanitool :: is_contract_generated(check)]
//! fn add1_check_86e6df(a : & mut u32) -> u32 {
//! let a_renamed = kani::internal::untracked_deref(& a);
//! let remember_kani_internal_1 = *a_renamed;
//! let a_renamed = kani::internal::untracked_deref(& a);
//! let remember_kani_internal_0 = * a_renamed;
//! let _wrapper_arg_1 = unsafe { kani :: internal :: Pointer :: decouple_lifetime(& a) };
//! let result_kani_internal : u32 = add1_wrapper_86e6df(a, _wrapper_arg_1);
//! kani::assert((| result | (remember_kani_internal_0).wrapping_add(1) == *a_renamed) (& result_kani_internal),
//! stringify! (|result| old(*a).wrapping_add(1) == *a));
//! std::mem::forget(a_renamed);
//! kani::assert((| result : & u32 | (remember_kani_internal_1).wrapping_add(1) == *result) (& result_kani_internal),
//! stringify!(|result : &u32| old(*a).wrapping_add(1) == *result));
//! std::mem::forget(a_renamed);
//! result_kani_internal
//! }
//!
//! #[allow(dead_code, unused_variables)]
//! #[kanitool :: is_contract_generated(replace)]
//! fn add1_replace_86e6df(a : & mut u32) -> u32 {
//! let a_renamed = kani::internal::untracked_deref(& a);
//! let remember_kani_internal_1 = *a_renamed;
//! let a_renamed = kani::internal::untracked_deref(& a);
//! let remember_kani_internal_0 = *a_renamed;
//! let result_kani_internal : u32 = kani::any_modifies();
//! *unsafe{ kani :: internal :: Pointer :: assignable(a) } = kani::any_modifies();
//! kani ::assume((| result | (remember_kani_internal_0).wrapping_add(1) == *a_renamed) (& result_kani_internal));
//! std :: mem :: forget(a_renamed);
//! kani ::assume((| result : & u32 | (remember_kani_internal_1).wrapping_add(1) == *result) (& result_kani_internal));
//! std :: mem :: forget(a_renamed);
//! result_kani_internal
//! }
//! ```

use proc_macro::TokenStream;
use proc_macro2::{Ident, TokenStream as TokenStream2};
use quote::{quote, ToTokens};
use std::collections::HashMap;
use syn::{parse_macro_input, Expr, ItemFn};
use syn::{parse_macro_input, Expr, ExprClosure, ItemFn};

mod bootstrap;
mod check;
#[macro_use]
mod helpers;
mod initialize;
mod replace;
Expand Down Expand Up @@ -356,7 +422,7 @@ enum ContractConditionsData {
/// we will be emitting.
argument_names: HashMap<Ident, Ident>,
/// The contents of the attribute.
attr: Expr,
attr: ExprClosure,
},
Modifies {
attr: Vec<Expr>,
Expand Down
7 changes: 5 additions & 2 deletions library/kani_macros/src/sysroot/contracts/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use quote::quote;

use super::{
helpers::*,
shared::{make_unsafe_argument_copies, try_as_result_assign},
shared::{build_ensures, count_remembers, make_unsafe_argument_copies, try_as_result_assign},
ContractConditionsData, ContractConditionsHandler, INTERNAL_RESULT_IDENT,
};

Expand Down Expand Up @@ -67,6 +67,7 @@ impl<'a> ContractConditionsHandler<'a> {
/// generating a replace function.
fn make_replace_body(&self) -> TokenStream2 {
let (before, after) = self.ensure_bootstrapped_replace_body();
let remember_count = count_remembers(&before);

match &self.condition_type {
ContractConditionsData::Requires { attr } => {
Expand All @@ -82,11 +83,13 @@ impl<'a> ContractConditionsHandler<'a> {
ContractConditionsData::Ensures { attr, argument_names } => {
let (arg_copies, copy_clean) = make_unsafe_argument_copies(&argument_names);
let result = Ident::new(INTERNAL_RESULT_IDENT, Span::call_site());
let (remembers_stmts, ensures_clause) = build_ensures(attr, remember_count);
quote!(
#arg_copies
#remembers_stmts
#(#before)*
#(#after)*
kani::assume(#attr);
kani::assume(#ensures_clause);
#copy_clean
#result
)
Expand Down
Loading
Loading