Skip to content
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

Verify String::remove in alloc::string #22

Merged
merged 41 commits into from
Dec 6, 2024
Merged
Changes from 38 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
9a8993d
added common codes for all proof of contracts
xsxszab Oct 7, 2024
55950bb
implemented integer type proof for contract for fn add, sub and offset
xsxszab Oct 7, 2024
ce13e8f
Merge branch 'main' into verify/ptr_mut
stogaru Oct 9, 2024
3682858
Adds proofs for tuple types
stogaru Oct 9, 2024
a6d4d62
Renames harnesses
stogaru Oct 9, 2024
dec8c30
Added unit type proofs for mut ptr
MayureshJoshi25 Oct 9, 2024
2be7639
Merge pull request #8 from stogaru/verify/ptr_mut_unit_types
stogaru Oct 11, 2024
75179dc
Merge branch 'verify/ptr_mut' into verify/ptr_mut_integer_types
xsxszab Oct 11, 2024
34c670e
Merge pull request #6 from stogaru/verify/ptr_mut_integer_types
xsxszab Oct 11, 2024
66c956e
Merge branch 'verify/ptr_mut' into verify/ptr_mut_composite
stogaru Oct 11, 2024
d9b8c65
Merge pull request #7 from stogaru/verify/ptr_mut_composite
stogaru Oct 11, 2024
0faac46
Combined macros
stogaru Oct 11, 2024
82345de
Fixes a typo
stogaru Oct 12, 2024
656209b
Removes an unnecessary attribute
stogaru Oct 12, 2024
46e839c
Merge pull request #9 from stogaru/verify/ptr_mut_combined
stogaru Oct 12, 2024
96a8a2e
refactored function contracts for add, sub and offset using the same_…
xsxszab Oct 23, 2024
852e96f
merged macros for add, sub and offset
xsxszab Oct 23, 2024
04bd61e
updated function contracts and proof for contracts for add(), sub() a…
xsxszab Oct 24, 2024
7557fc5
added support for unit type pointers
xsxszab Oct 31, 2024
76b2311
updated function contracts, removed unnecessary kani::assmue
xsxszab Nov 6, 2024
cb6a177
added comments to function contracts
xsxszab Nov 6, 2024
6385d4a
Merge pull request #12 from stogaru/verify/ptr_mut_refactor_harness
xsxszab Nov 7, 2024
a079a7a
added comments for magic numbers
xsxszab Nov 7, 2024
214f84d
Merge branch 'main' into verify/ptr_mut
stogaru Nov 7, 2024
51ded41
updated proofs using pointer generator
xsxszab Nov 15, 2024
fb6215e
fixed typos in comments
xsxszab Nov 15, 2024
f229fd9
rustfmt formatting
xsxszab Nov 15, 2024
20fffa3
reverse unnecessary rustfmt formatting
xsxszab Nov 15, 2024
b9054a8
temporary disabled check_mut_add_u8 test
xsxszab Nov 16, 2024
b528b0e
Clarified preconditions with additional notes
xsxszab Nov 27, 2024
9b90b23
added tracking issue info for the pointer generator bug
xsxszab Nov 27, 2024
b3b0620
Merge branch 'main' into verify/ptr_mut
tautschnig Nov 27, 2024
afe1b16
Add proof for String:remove
szlee118 Dec 2, 2024
80b3f91
Use any_array at creation
szlee118 Dec 3, 2024
6e97438
align mut_ptr with usage_proofs branch
szlee118 Dec 5, 2024
f581759
Copied file from usage_proofs to verify/string_remove
szlee118 Dec 5, 2024
5de632a
Merge branch 'usage_proofs' into verify/string_remove
szlee118 Dec 5, 2024
081474e
Fix of any_where usage, hardcode constant
szlee118 Dec 5, 2024
51e903d
Add extra assertion and remove redundant assume
szlee118 Dec 6, 2024
8ec384e
Add description
szlee118 Dec 6, 2024
ee06313
Remove redundant white line
szlee118 Dec 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ use crate::str::{self, Chars, Utf8Error, from_utf8_unchecked_mut};
use crate::str::{FromStr, from_boxed_utf8_unchecked};
use crate::vec::Vec;


/// A UTF-8–encoded, growable string.
///
/// `String` is the most common string type. It has ownership over the contents
Expand Down Expand Up @@ -3217,3 +3218,43 @@ 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]
#[kani::unwind(9)]
stogaru marked this conversation as resolved.
Show resolved Hide resolved
fn check_remove() {
// array size is chosen because it is small enough to be feasible to check exhaustively
const ARRAY_SIZE: usize = 8;
let arr: [u8; ARRAY_SIZE] = kani::Arbitrary::any_array();
for &byte in &arr {
kani::assume(byte.is_ascii()); // Constrain to ASCII characters
}

// Convert byte array to a String directly (safe since all are ASCII)
let mut s = unsafe { String::from_utf8_unchecked(arr.to_vec()) };

// Ensure the string is not empty
kani::assume(!s.is_empty());

// Generate a valid index within the bounds of the string
let idx: usize = kani::any_where(|&x| x < s.len());
kani::assume(idx < s.len());
stogaru marked this conversation as resolved.
Show resolved Hide resolved

// Call the `remove` function
let removed_char = s.remove(idx);

// Verify the string length decreases correctly
assert_eq!(s.len(), arr.len() - 1);

// Verify the removed character is a valid ASCII character
assert!(removed_char.is_ascii());
stogaru marked this conversation as resolved.
Show resolved Hide resolved
}
}