-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is just an experiment to get early feedback. This currently does not support methods.
- Loading branch information
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
library/kani_macros/src/sysroot/contracts/generate_harness.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
//! Logic to implement gen_proof_for_contract macro | ||
|
||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::parse::{Parse, ParseStream}; | ||
use syn::{Expr, LitInt, parse_macro_input, parse_quote}; | ||
|
||
/// Implementation of [crate::gen_proof_for_contract] when building with `kani_sysroot` enabled. | ||
pub fn gen_proof_for_contract(item: TokenStream) -> TokenStream { | ||
let ProofForContract { harness_name, fn_path, num_args } = parse_macro_input!(item); | ||
let kani_any: Expr = parse_quote! { kani::any() }; | ||
let args = (0..num_args).map(|_i| kani_any.clone()).collect::<Vec<_>>(); | ||
quote! { | ||
#[kani::proof_for_contract(#fn_path)] | ||
fn #harness_name() { | ||
let _ = #fn_path(#(#args),*); | ||
} | ||
} | ||
.into() | ||
} | ||
|
||
#[derive(Debug)] | ||
struct ProofForContract { | ||
harness_name: syn::Ident, | ||
fn_path: syn::Path, | ||
num_args: usize, | ||
} | ||
|
||
impl Parse for ProofForContract { | ||
fn parse(input: ParseStream) -> syn::Result<Self> { | ||
let harness_name = input.parse::<syn::Ident>()?; | ||
let _ = input.parse::<syn::Token![,]>()?; | ||
let fn_path = input.parse::<syn::Path>()?; | ||
let _ = input.parse::<syn::Token![,]>()?; | ||
let num_args = input.parse::<LitInt>()?; | ||
Ok(ProofForContract { harness_name, fn_path, num_args: num_args.base10_parse()? }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// kani-flags: -Z function-contracts | ||
|
||
extern crate kani; | ||
use kani::{Arbitrary, Invariant}; | ||
|
||
#[derive(Arbitrary, Invariant)] | ||
struct Stars { | ||
#[safety_constraint(*value <= 5)] | ||
value: u8, | ||
} | ||
|
||
impl Stars { | ||
#[kani::requires(stars <= 5)] | ||
#[kani::ensures(|res| res.is_safe())] | ||
fn new(stars: u8) -> Stars { | ||
Stars::try_new(stars).unwrap() | ||
} | ||
|
||
#[kani::ensures(|ret| kani::implies!(stars <= 5 => ret.is_ok()))] | ||
#[kani::ensures(|ret| kani::implies!(stars > 5 => ret.is_err()))] | ||
fn try_new(stars: u8) -> Result<Stars, ()> { | ||
if stars > 5 { Err(()) } else { Ok(Stars { value: stars }) } | ||
} | ||
} | ||
|
||
#[cfg(kani)] | ||
mod verify { | ||
use super::*; | ||
kani::gen_proof_for_contract!(check_new_contract, Stars::new, 1); | ||
kani::gen_proof_for_contract!(check_try_new_contract, Stars::try_new, 1); | ||
} |