-
Notifications
You must be signed in to change notification settings - Fork 66
Challenge 10: Verify memory safety of String functions #571
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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -3488,3 +3488,129 @@ impl From<char> for String { | |||||||
| c.to_string() | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| #[cfg(kani)] | ||||||||
| #[unstable(feature = "kani", issue = "none")] | ||||||||
| mod verify { | ||||||||
| use core::kani; | ||||||||
|
|
||||||||
| use crate::string::String; | ||||||||
|
|
||||||||
| #[kani::proof] | ||||||||
| fn verify_pop() { | ||||||||
| let mut s = String::from("hello"); | ||||||||
| let c = s.pop(); | ||||||||
| assert!(c == Some('o')); | ||||||||
| assert!(s.len() == 4); | ||||||||
| } | ||||||||
|
|
||||||||
| #[kani::proof] | ||||||||
| fn verify_remove() { | ||||||||
| let mut s = String::from("hello"); | ||||||||
| let c = s.remove(0); | ||||||||
| assert!(c == 'h'); | ||||||||
| assert!(s.len() == 4); | ||||||||
| } | ||||||||
|
|
||||||||
| #[kani::proof] | ||||||||
| fn verify_insert() { | ||||||||
| let mut s = String::from("ello"); | ||||||||
| s.insert(0, 'h'); | ||||||||
| assert!(s.len() == 5); | ||||||||
| } | ||||||||
|
|
||||||||
| #[kani::proof] | ||||||||
| fn verify_insert_str() { | ||||||||
| let mut s = String::from("world"); | ||||||||
| s.insert_str(0, "hello "); | ||||||||
| assert!(s.len() == 11); | ||||||||
| } | ||||||||
|
|
||||||||
| #[kani::proof] | ||||||||
| fn verify_split_off() { | ||||||||
| let mut s = String::from("hello"); | ||||||||
| let s2 = s.split_off(2); | ||||||||
| assert!(s.len() == 2); | ||||||||
| assert!(s2.len() == 3); | ||||||||
| } | ||||||||
|
|
||||||||
| #[kani::proof] | ||||||||
| fn verify_drain() { | ||||||||
| let mut s = String::from("hello"); | ||||||||
| let d: String = s.drain(1..3).collect(); | ||||||||
| assert!(d.len() == 2); | ||||||||
| assert!(s.len() == 3); | ||||||||
| } | ||||||||
|
|
||||||||
| #[kani::proof] | ||||||||
| fn verify_replace_range() { | ||||||||
| let mut s = String::from("hello"); | ||||||||
| s.replace_range(1..3, "a"); | ||||||||
| assert!(s.len() == 4); | ||||||||
| } | ||||||||
|
|
||||||||
| #[kani::proof] | ||||||||
| fn verify_into_boxed_str() { | ||||||||
| let s = String::from("hello"); | ||||||||
| let b: crate::boxed::Box<str> = s.into_boxed_str(); | ||||||||
| assert!(b.len() == 5); | ||||||||
| } | ||||||||
|
|
||||||||
| #[kani::proof] | ||||||||
| fn verify_leak() { | ||||||||
| let s = String::from("hello"); | ||||||||
| let leaked: &'static mut str = s.leak(); | ||||||||
| assert!(leaked.len() == 5); | ||||||||
| unsafe { | ||||||||
| drop(crate::boxed::Box::from_raw(leaked as *mut str)); | ||||||||
| } | ||||||||
|
Comment on lines
+3564
to
+3566
|
||||||||
| unsafe { | |
| drop(crate::boxed::Box::from_raw(leaked as *mut str)); | |
| } |
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 harnesses use fixed, hard-coded inputs (e.g., "hello", constant indices/ranges), so Kani only explores a single execution path per method. That doesn’t meaningfully verify memory safety across the input space (e.g., varying lengths, capacities, UTF-8 boundaries, and index/range preconditions). Consider generating nondeterministic inputs with
kani::any/kani::Arbitraryand constraining them withkani::assume/any_where(e.g.,idx < s.len()ands.is_char_boundary(idx)), so the proof actually covers the cases these APIs must handle.