-
Notifications
You must be signed in to change notification settings - Fork 48
CStr Safety invariant & Harnesses for from_bytes_until_nul
#180
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
CStr Safety invariant & Harnesses for from_bytes_until_nul
#180
Conversation
Questions // Positive check
#[kani::proof]
#[kani::unwind(32)]
fn check_from_bytes_until_nul() {
const ARR_LEN: usize = 32;
let mut string: [u8; ARR_LEN] = kani::any();
// ensure that there is at least one null byte
let idx: usize = kani::any_where(|x: &usize| *x >= 0 && *x < ARR_LEN);
string[idx] = 0;
let c_str = CStr::from_bytes_until_nul(&string).unwrap();
assert!(c_str.is_safe());
}
// Negative check
#[kani::proof]
#[kani::unwind(5)]
#[kani::should_panic]
fn check_from_bytes_until_nul_panic() {
const ARR_LEN: usize = 4;
// let mut string: [u8; ARR_LEN] = [64, 65, 66];
// The array does not contain any null bytes. Calling
// from_bytes_until_nul will return an error.
let mut string: [u8; ARR_LEN] = kani::any_where(|x: &[u8; ARR_LEN]| !x.contains(&0));
let c_str = CStr::from_bytes_until_nul(&string).unwrap();
assert!(c_str.is_safe());
} Verification result:
|
Hi @Yenyun035, that's a great question. I think in the case of Thus, it should be enough to create an arbitrary slice of |
@celinval Thank you very much for your advice! I modified the harness and the PR description accordingly, and now it should be good to go. |
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.
Thanks!
@celinval Thank you for your comments. I resolved the conversations. Please let me know if anything else to be modified :) |
@zhassan-aws Thank you for your comments. I just fixed them. |
Resolves / Towards #150
Changes
CStr
Safety Invariantfrom_bytes_until_nul
, the harness covers:Discussion
Verification Result
./scripts/run-kani.sh --kani-args --harness ffi::c_str::verify
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.