-
Notifications
You must be signed in to change notification settings - Fork 65
Challenge 23: Verify safety of Vec functions part 1 #569
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -4317,37 +4317,322 @@ mod verify { | |||||||
|
|
||||||||
| #[kani::proof] | ||||||||
| pub fn verify_swap_remove() { | ||||||||
| // Creating a vector directly from a fixed length arbitrary array | ||||||||
| let mut arr: [i32; ARRAY_LEN] = kani::Arbitrary::any_array(); | ||||||||
| let mut vect = Vec::from(&arr); | ||||||||
| let arr: [i32; ARRAY_LEN] = kani::Arbitrary::any_array(); | ||||||||
| let mut v = Vec::from(&arr); | ||||||||
| let i: usize = kani::any_where(|x| *x < v.len()); | ||||||||
| let _ = v.swap_remove(i); | ||||||||
| assert!(v.len() == ARRAY_LEN - 1); | ||||||||
| } | ||||||||
|
|
||||||||
| // Recording the original length and a copy of the vector for validation | ||||||||
| let original_len = vect.len(); | ||||||||
| let original_vec = vect.clone(); | ||||||||
| #[kani::proof] | ||||||||
| pub fn verify_push() { | ||||||||
| let arr: [i32; ARRAY_LEN] = kani::Arbitrary::any_array(); | ||||||||
| let mut v = Vec::from(&arr); | ||||||||
| v.push(kani::any()); | ||||||||
| assert!(v.len() == ARRAY_LEN + 1); | ||||||||
| } | ||||||||
|
|
||||||||
| // Generating a nondeterministic index which is guaranteed to be within bounds | ||||||||
| let index: usize = kani::any_where(|x| *x < original_len); | ||||||||
| #[kani::proof] | ||||||||
| pub fn verify_pop() { | ||||||||
| let arr: [i32; ARRAY_LEN] = kani::Arbitrary::any_array(); | ||||||||
| let mut v = Vec::from(&arr); | ||||||||
| assert!(v.pop().is_some()); | ||||||||
| assert!(v.len() == ARRAY_LEN - 1); | ||||||||
| } | ||||||||
|
|
||||||||
| let removed = vect.swap_remove(index); | ||||||||
| #[kani::proof] | ||||||||
| pub fn verify_push_within_capacity() { | ||||||||
| let arr: [i32; ARRAY_LEN] = kani::Arbitrary::any_array(); | ||||||||
| let mut v = Vec::from(&arr); | ||||||||
| v.reserve(1); | ||||||||
| assert!(v.push_within_capacity(42).is_ok()); | ||||||||
| } | ||||||||
|
|
||||||||
| // Verifying that the length of the vector decreases by one after the operation is performed | ||||||||
| assert!(vect.len() == original_len - 1, "Length should decrease by 1"); | ||||||||
| #[kani::proof] | ||||||||
| pub fn verify_insert() { | ||||||||
| let arr: [i32; ARRAY_LEN] = kani::Arbitrary::any_array(); | ||||||||
| let mut v = Vec::from(&arr); | ||||||||
| let i: usize = kani::any_where(|&x: &usize| x <= ARRAY_LEN); | ||||||||
| v.insert(i, 42); | ||||||||
| assert!(v.len() == ARRAY_LEN + 1); | ||||||||
| assert!(v[i] == 42); | ||||||||
| } | ||||||||
|
|
||||||||
| // Verifying that the removed element matches the original element at the index | ||||||||
| assert!(removed == original_vec[index], "Removed element should match original"); | ||||||||
| #[kani::proof] | ||||||||
| pub fn verify_remove() { | ||||||||
| let arr: [i32; ARRAY_LEN] = kani::Arbitrary::any_array(); | ||||||||
| let mut v = Vec::from(&arr); | ||||||||
| let i: usize = kani::any_where(|&x: &usize| x < ARRAY_LEN); | ||||||||
| let _ = v.remove(i); | ||||||||
| assert!(v.len() == ARRAY_LEN - 1); | ||||||||
| } | ||||||||
|
|
||||||||
| // Verifying that the removed index now contains the element originally at the vector's last index if applicable | ||||||||
| if index < original_len - 1 { | ||||||||
| assert!( | ||||||||
| vect[index] == original_vec[original_len - 1], | ||||||||
| "Index should contain last element" | ||||||||
| ); | ||||||||
| #[kani::proof] | ||||||||
| pub fn verify_clear() { | ||||||||
| let arr: [i32; ARRAY_LEN] = kani::Arbitrary::any_array(); | ||||||||
| let mut v = Vec::from(&arr); | ||||||||
| v.clear(); | ||||||||
| assert!(v.is_empty()); | ||||||||
| } | ||||||||
|
|
||||||||
| #[kani::proof] | ||||||||
| pub fn verify_truncate() { | ||||||||
| let arr: [i32; ARRAY_LEN] = kani::Arbitrary::any_array(); | ||||||||
| let mut v = Vec::from(&arr); | ||||||||
| let n: usize = kani::any_where(|&x: &usize| x <= ARRAY_LEN); | ||||||||
| v.truncate(n); | ||||||||
| assert!(v.len() == n); | ||||||||
| } | ||||||||
|
|
||||||||
| #[kani::proof] | ||||||||
| pub fn verify_deref() { | ||||||||
| let arr: [i32; ARRAY_LEN] = kani::Arbitrary::any_array(); | ||||||||
| let v = Vec::from(&arr); | ||||||||
| let s: &[i32] = &*v; | ||||||||
| assert!(s.len() == ARRAY_LEN); | ||||||||
| } | ||||||||
|
|
||||||||
| #[kani::proof] | ||||||||
| pub fn verify_deref_mut() { | ||||||||
| let arr: [i32; ARRAY_LEN] = kani::Arbitrary::any_array(); | ||||||||
| let mut v = Vec::from(&arr); | ||||||||
| let s: &mut [i32] = &mut *v; | ||||||||
| s[0] = 42; | ||||||||
| assert!(s[0] == 42); | ||||||||
| } | ||||||||
|
|
||||||||
| #[kani::proof] | ||||||||
| pub fn verify_leak() { | ||||||||
| let v = Vec::from(&[1i32, 2, 3]); | ||||||||
| let leaked = v.leak(); | ||||||||
| assert!(leaked.len() == 3); | ||||||||
| unsafe { | ||||||||
| drop(crate::boxed::Box::from_raw(leaked as *mut [i32])); | ||||||||
| } | ||||||||
|
Comment on lines
+4409
to
4411
|
||||||||
| unsafe { | |
| drop(crate::boxed::Box::from_raw(leaked as *mut [i32])); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Several harnesses in this
#[cfg(kani)]module callVecAPIs that are themselves#[cfg(not(no_global_oom_handling))](e.g.,push,reserve,insert,into_boxed_slice,extend_from_within,append). Under a build that enablesno_global_oom_handlingtogether withkani, these harnesses won’t compile because those methods are not available. Consider adding matching#[cfg(not(no_global_oom_handling))]gates to the affected proof functions (or otherwise gating the whole verify module) so cfg combinations remain buildable.