-
Notifications
You must be signed in to change notification settings - Fork 65
Challenge 29: Verify safety of Box functions #573
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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2160,3 +2160,335 @@ impl<E: Error> Error for Box<E> { | |||||||||||||
| Error::provide(&**self, request); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[cfg(kani)] | ||||||||||||||
| #[unstable(feature = "kani", issue = "none")] | ||||||||||||||
| mod verify { | ||||||||||||||
| use core::any::Any; | ||||||||||||||
| use core::kani; | ||||||||||||||
| use core::mem::MaybeUninit; | ||||||||||||||
|
|
||||||||||||||
| use crate::alloc::Global; | ||||||||||||||
| use crate::boxed::Box; | ||||||||||||||
|
|
||||||||||||||
| // === UNSAFE FUNCTIONS (9 — all required) === | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_assume_init_single() { | ||||||||||||||
| let b: Box<MaybeUninit<i32>> = Box::new(MaybeUninit::new(42)); | ||||||||||||||
| let b = unsafe { b.assume_init() }; | ||||||||||||||
| assert!(*b == 42); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_assume_init_slice() { | ||||||||||||||
| let mut b: Box<[MaybeUninit<i32>]> = Box::new_uninit_slice(3); | ||||||||||||||
| for i in 0..3 { | ||||||||||||||
| b[i].write(i as i32); | ||||||||||||||
| } | ||||||||||||||
| let b = unsafe { b.assume_init() }; | ||||||||||||||
| assert!(b.len() == 3); | ||||||||||||||
| assert!(b[0] == 0); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_from_raw() { | ||||||||||||||
| let b = Box::new(42i32); | ||||||||||||||
| let raw = Box::into_raw(b); | ||||||||||||||
| let b = unsafe { Box::from_raw(raw) }; | ||||||||||||||
| assert!(*b == 42); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_from_non_null() { | ||||||||||||||
| let b = Box::new(42i32); | ||||||||||||||
| let nn = Box::into_non_null(b); | ||||||||||||||
| let b = unsafe { Box::from_non_null(nn) }; | ||||||||||||||
| assert!(*b == 42); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_from_raw_in() { | ||||||||||||||
| let b = Box::new_in(42i32, Global); | ||||||||||||||
| let (raw, alloc) = Box::into_raw_with_allocator(b); | ||||||||||||||
| let b = unsafe { Box::from_raw_in(raw, alloc) }; | ||||||||||||||
| assert!(*b == 42); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_from_non_null_in() { | ||||||||||||||
| let b = Box::new_in(42i32, Global); | ||||||||||||||
| let (nn, alloc) = Box::into_non_null_with_allocator(b); | ||||||||||||||
| let b = unsafe { Box::from_non_null_in(nn, alloc) }; | ||||||||||||||
| assert!(*b == 42); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_downcast_unchecked_any() { | ||||||||||||||
| let b: Box<dyn Any> = Box::new(42i32); | ||||||||||||||
| let d = unsafe { b.downcast_unchecked::<i32>() }; | ||||||||||||||
| assert!(*d == 42); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_downcast_unchecked_any_send() { | ||||||||||||||
| let b: Box<dyn Any + Send> = Box::new(42i32); | ||||||||||||||
| let d = unsafe { b.downcast_unchecked::<i32>() }; | ||||||||||||||
| assert!(*d == 42); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_downcast_unchecked_any_send_sync() { | ||||||||||||||
| let b: Box<dyn Any + Send + Sync> = Box::new(42i32); | ||||||||||||||
| let d = unsafe { b.downcast_unchecked::<i32>() }; | ||||||||||||||
| assert!(*d == 42); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // === SAFE FUNCTIONS (34 — need 33 of 43) === | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_new_in() { | ||||||||||||||
| let b = Box::new_in(42i32, Global); | ||||||||||||||
| assert!(*b == 42); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_try_new_in() { | ||||||||||||||
| let r = Box::try_new_in(42i32, Global); | ||||||||||||||
| assert!(r.is_ok()); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_try_new_uninit_in() { | ||||||||||||||
| let r = Box::<i32>::try_new_uninit_in(Global); | ||||||||||||||
| assert!(r.is_ok()); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_try_new_zeroed_in() { | ||||||||||||||
| let r = Box::<i32>::try_new_zeroed_in(Global); | ||||||||||||||
| assert!(r.is_ok()); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_into_boxed_slice() { | ||||||||||||||
| let b = Box::new(42i32); | ||||||||||||||
| let s: Box<[i32]> = Box::into_boxed_slice(b); | ||||||||||||||
| assert!(s.len() == 1); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_new_uninit_slice() { | ||||||||||||||
| let b: Box<[MaybeUninit<i32>]> = Box::new_uninit_slice(3); | ||||||||||||||
| assert!(b.len() == 3); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_new_zeroed_slice() { | ||||||||||||||
| let b: Box<[MaybeUninit<i32>]> = Box::new_zeroed_slice(3); | ||||||||||||||
| let b = unsafe { b.assume_init() }; | ||||||||||||||
| assert!(b[0] == 0); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_try_new_uninit_slice() { | ||||||||||||||
| let r = Box::<[i32]>::try_new_uninit_slice(3); | ||||||||||||||
| assert!(r.is_ok()); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_try_new_zeroed_slice() { | ||||||||||||||
| let r = Box::<[i32]>::try_new_zeroed_slice(3); | ||||||||||||||
| assert!(r.is_ok()); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[kani::proof] | ||||||||||||||
| fn verify_into_array() { | ||||||||||||||
| let b: Box<[i32]> = Box::from([1, 2, 3]); | ||||||||||||||
| let r: Result<Box<[i32; 3]>, _> = b.try_into(); | ||||||||||||||
| assert!(r.is_ok()); | ||||||||||||||
|
Comment on lines
+2308
to
+2309
|
||||||||||||||
| let r: Result<Box<[i32; 3]>, _> = b.try_into(); | |
| assert!(r.is_ok()); | |
| let r = b.into_array::<3>(); | |
| assert!(r.is_some()); | |
| let r = r.unwrap(); | |
| assert!(r[0] == 1 && r[1] == 2 && r[2] == 3); |
Copilot
AI
Mar 31, 2026
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.
MyError (and its Display/Error impls) is duplicated across the three verify_downcast_error* proofs. To reduce repetition and keep these harnesses easier to maintain, consider defining MyError once in the module (or a small helper) and reusing it in all three proofs.
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.
This
#[cfg(kani)]verification module calls APIs likeBox::new,Box::new_uninit, andBox::new_uninit_slice, which are all#[cfg(not(no_global_oom_handling))]in this file. As written, enablingcfg(kani)alongsideno_global_oom_handlingwill fail to compile. Consider gating the module (or the affected proofs) with#[cfg(not(no_global_oom_handling))], or rewriting the harnesses to only use fallible/allocator-based constructors that are available underno_global_oom_handling.