-
Notifications
You must be signed in to change notification settings - Fork 66
Verify CStr CloneToUninit and Index<RangeFrom> safety (Challenge 13) #543
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
10cfee8
a0b5f8f
f0e5049
ac63dd9
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 |
|---|---|---|
|
|
@@ -875,6 +875,7 @@ impl FusedIterator for Bytes<'_> {} | |
| #[unstable(feature = "kani", issue = "none")] | ||
| mod verify { | ||
| use super::*; | ||
| use crate::clone::CloneToUninit; | ||
|
|
||
| // Helper function | ||
| fn arbitrary_cstr(slice: &[u8]) -> &CStr { | ||
|
|
@@ -1096,4 +1097,59 @@ mod verify { | |
| assert_eq!(expected_is_empty, c_str.is_empty()); | ||
| assert!(c_str.is_safe()); | ||
| } | ||
|
|
||
| // ops::Index<ops::RangeFrom<usize>> for CStr | ||
| #[kani::proof] | ||
| #[kani::unwind(33)] | ||
| fn check_index_from() { | ||
| const MAX_SIZE: usize = 32; | ||
| let string: [u8; MAX_SIZE] = kani::any(); | ||
| let slice = kani::slice::any_slice_of_array(&string); | ||
| let c_str = arbitrary_cstr(slice); | ||
|
|
||
| let bytes_with_nul = c_str.to_bytes_with_nul(); | ||
| let idx: usize = kani::any(); | ||
| kani::assume(idx < bytes_with_nul.len()); | ||
|
|
||
| let indexed = &c_str[idx..]; | ||
| assert!(indexed.is_safe()); | ||
| // The indexed result should correspond to the tail of the original bytes | ||
| assert_eq!(indexed.to_bytes_with_nul(), &bytes_with_nul[idx..]); | ||
| } | ||
|
|
||
| // CloneToUninit for CStr | ||
| // MAX_SIZE is kept small to avoid CBMC timeout: the symbolic pointer | ||
| // arithmetic in clone_to_uninit is expensive; 8 bytes is sufficient to | ||
| // cover empty, single-char, and multi-char C strings. | ||
| #[kani::proof] | ||
| #[kani::unwind(9)] | ||
| fn check_clone_to_uninit() { | ||
| const MAX_SIZE: usize = 8; | ||
| let string: [u8; MAX_SIZE] = kani::any(); | ||
| let slice = kani::slice::any_slice_of_array(&string); | ||
| let c_str = arbitrary_cstr(slice); | ||
|
|
||
| let len = c_str.to_bytes_with_nul().len(); | ||
|
|
||
| // Use an initialized buffer to avoid UB from reading uninitialized | ||
| // memory if clone_to_uninit were buggy and failed to write all bytes. | ||
| let mut buf = [0u8; MAX_SIZE]; | ||
| let dest = buf.as_mut_ptr(); | ||
|
|
||
|
Comment on lines
+1132
to
+1138
|
||
| // Safety: dest is non-null (stack allocation), valid for len writes, | ||
| // properly aligned (u8 has alignment 1) | ||
| unsafe { | ||
| c_str.clone_to_uninit(dest); | ||
| } | ||
|
|
||
| // Verify the cloned bytes form a valid CStr | ||
| let cloned_slice = unsafe { core::slice::from_raw_parts(dest, len) }; | ||
| let cloned_cstr = CStr::from_bytes_with_nul(cloned_slice); | ||
| assert!(cloned_cstr.is_ok()); | ||
| let cloned = cloned_cstr.unwrap(); | ||
| assert!(cloned.is_safe()); | ||
|
|
||
| // Verify exact byte-for-byte match with source (including NUL terminator) | ||
| assert_eq!(cloned.to_bytes_with_nul(), c_str.to_bytes_with_nul()); | ||
| } | ||
| } | ||
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.
The new
#[requires(!dest.is_null())]precondition is weaker than the documented safety requirements forCloneToUninit::clone_to_uninit(destination must be valid forsize_of_val(self)writes and properly aligned). For consistency with other contracts in core (e.g. pointer APIs usingub_checks::can_write), consider strengthening this contract to reflect the full safety requirements, not just non-nullness.