generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 129
Add regular & fixme tests for function contracts #3371
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9813172
Test contract support to parameter patterns
celinval aac359f
Add fixme tests for contracts for different issues
celinval 01fa123
Merge branch 'main' into issue-3206-tests
celinval eaf3c68
Merge branch 'main' into issue-3206-tests
feliperodri ea91888
Apply suggestions from code review
celinval 2fd0fe4
update tests
62f2855
remove charon submodule
f742cd2
remove test copy
d231bcc
Merge branch 'main' into issue-3206-tests
celinval 44b282d
Apply suggestions from code review
celinval File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
148 changes: 148 additions & 0 deletions
148
tests/kani/FunctionContracts/fixme_receiver_contracts.rs
This file contains hidden or 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,148 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| //! Checks that function contracts work with different types of receivers. I.e.: | ||
| //! - &Self (i.e. &self) | ||
| //! - &mut Self (i.e &mut self) | ||
| //! - Box<Self> | ||
| //! - Rc<Self> | ||
| //! - Arc<Self> | ||
| //! - Pin<P> where P is one of the types above | ||
| //! Source: <https://doc.rust-lang.org/reference/items/traits.html?highlight=receiver#object-safety> | ||
| // compile-flags: --edition 2021 | ||
| // kani-flags: -Zfunction-contracts | ||
|
|
||
| #![feature(rustc_attrs)] | ||
|
|
||
| extern crate kani; | ||
|
|
||
| use std::boxed::Box; | ||
| use std::pin::Pin; | ||
| use std::rc::Rc; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Type representing a valid ASCII value going from `0..128`. | ||
| #[derive(Copy, Clone, PartialEq, Eq)] | ||
| #[rustc_layout_scalar_valid_range_start(0)] | ||
| #[rustc_layout_scalar_valid_range_end(128)] | ||
| struct CharASCII(u8); | ||
|
|
||
| impl kani::Arbitrary for CharASCII { | ||
| fn any() -> CharASCII { | ||
| let val = kani::any_where(|inner: &u8| *inner < 128); | ||
| unsafe { CharASCII(val) } | ||
| } | ||
| } | ||
|
|
||
| /// This type contains unsafe setter functions with the same contract but different type of | ||
| /// receivers. | ||
| impl CharASCII { | ||
| #[kani::modifies(&self.0)] | ||
| #[kani::requires(new_val < 128)] | ||
| #[kani::ensures(|_| self.0 == new_val)] | ||
| unsafe fn set_val(&mut self, new_val: u8) { | ||
| self.0 = new_val | ||
| } | ||
|
|
||
| #[kani::modifies(&self.0)] | ||
| #[kani::requires(new_val < 128)] | ||
| #[kani::ensures(|_| self.0 == new_val)] | ||
| unsafe fn set_mut_ref(self: &mut Self, new_val: u8) { | ||
| self.0 = new_val | ||
| } | ||
|
|
||
| #[kani::modifies(&self.as_ref().0)] | ||
| #[kani::requires(new_val < 128)] | ||
| #[kani::ensures(|_| self.as_ref().0 == new_val)] | ||
| unsafe fn set_box(mut self: Box<Self>, new_val: u8) { | ||
| self.as_mut().0 = new_val | ||
| } | ||
|
|
||
| #[kani::modifies(&self.as_ref().0)] | ||
| #[kani::requires(new_val < 128)] | ||
| #[kani::ensures(|_| self.as_ref().0 == new_val)] | ||
| unsafe fn set_rc(mut self: Rc<Self>, new_val: u8) { | ||
| Rc::<_>::get_mut(&mut self).unwrap().0 = new_val | ||
| } | ||
|
|
||
| #[kani::modifies(&self.as_ref().0)] | ||
| #[kani::requires(new_val < 128)] | ||
| #[kani::ensures(|_| self.as_ref().0 == new_val)] | ||
| unsafe fn set_arc(mut self: Arc<Self>, new_val: u8) { | ||
| Arc::<_>::get_mut(&mut self).unwrap().0 = new_val; | ||
| } | ||
|
|
||
| #[kani::modifies(&self.0)] | ||
| #[kani::requires(new_val < 128)] | ||
| #[kani::ensures(|_| self.0 == new_val)] | ||
| unsafe fn set_pin(mut self: Pin<&mut Self>, new_val: u8) { | ||
| self.0 = new_val | ||
| } | ||
|
|
||
| #[kani::modifies(&self.0)] | ||
| #[kani::requires(new_val < 128)] | ||
| #[kani::ensures(|_| self.0 == new_val)] | ||
| unsafe fn set_pin_box(mut self: Pin<Box<Self>>, new_val: u8) { | ||
| self.0 = new_val | ||
| } | ||
| } | ||
|
|
||
| mod verify { | ||
| use super::*; | ||
| use kani::Arbitrary; | ||
|
|
||
| #[kani::proof_for_contract(CharASCII::set_val)] | ||
| fn check_set_val() { | ||
| let mut obj = CharASCII::any(); | ||
| let original = obj.0; | ||
| let new_val: u8 = kani::any(); | ||
| unsafe { obj.set_val(new_val) }; | ||
| } | ||
|
|
||
| #[kani::proof_for_contract(CharASCII::set_mut_ref)] | ||
| fn check_mut_ref() { | ||
| let mut obj = CharASCII::any(); | ||
| let original = obj.0; | ||
celinval marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let new_val: u8 = kani::any(); | ||
| unsafe { obj.set_mut_ref(new_val) }; | ||
| } | ||
|
|
||
| #[kani::proof_for_contract(CharASCII::set_box)] | ||
| fn check_box() { | ||
| let obj = CharASCII::any(); | ||
| let original = obj.0; | ||
| let new_val: u8 = kani::any(); | ||
| unsafe { Box::new(obj).set_box(new_val) }; | ||
| } | ||
|
|
||
| #[kani::proof_for_contract(CharASCII::set_rc)] | ||
| fn check_rc() { | ||
| let obj = CharASCII::any(); | ||
| let original = obj.0; | ||
| let new_val: u8 = kani::any(); | ||
| unsafe { Rc::new(obj).set_rc(new_val) }; | ||
| } | ||
|
|
||
| #[kani::proof_for_contract(CharASCII::set_arc)] | ||
| fn check_arc() { | ||
| let obj = CharASCII::any(); | ||
| let original = obj.0; | ||
| let new_val: u8 = kani::any(); | ||
| unsafe { Arc::new(obj).set_arc(new_val) }; | ||
| } | ||
|
|
||
| #[kani::proof_for_contract(CharASCII::set_pin)] | ||
| fn check_pin() { | ||
| let mut obj = CharASCII::any(); | ||
| let original = obj.0; | ||
| let new_val: u8 = kani::any(); | ||
| unsafe { Pin::new(&mut obj).set_pin(new_val) }; | ||
| } | ||
|
|
||
| #[kani::proof_for_contract(CharASCII::set_pin_box)] | ||
| fn check_pin_box() { | ||
| let obj = CharASCII::any(); | ||
| let original = obj.0; | ||
| let new_val: u8 = kani::any(); | ||
| unsafe { Pin::new(Box::new(obj)).set_pin_box(new_val) }; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,25 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| //! Check that Kani correctly verify the contract that modifies slices. | ||
celinval marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! | ||
| //! Note that this test used to crash while parsing the annotations. | ||
| // kani-flags: -Zfunction-contracts | ||
| extern crate kani; | ||
|
|
||
| #[kani::requires(idx < slice.len())] | ||
| #[kani::modifies(slice.as_ptr().wrapping_add(idx))] | ||
| #[kani::ensures(|_| slice[idx] == new_val)] | ||
| fn modify_slice(slice: &mut [u32], idx: usize, new_val: u32) { | ||
| *slice.get_mut(idx).unwrap() = new_val; | ||
| } | ||
|
|
||
| #[cfg(kani)] | ||
| mod verify { | ||
| use super::modify_slice; | ||
|
|
||
| #[kani::proof_for_contract(modify_slice)] | ||
| fn check_modify_slice() { | ||
| let mut data = kani::vec::any_vec::<u32, 5>(); | ||
| modify_slice(&mut data, kani::any(), kani::any()) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.