forked from model-checking/kani
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instrument validity checks for pointer to reference casts for slices …
…and str's (model-checking#3513) As pointed out in model-checking#3498, validity checks for pointer to reference casts (added in model-checking#3221) were not instrumented in the case of fat pointers (e.g. array and string slices). This PR extends the instrumentation of validity checks to handle those cases. Resolves model-checking#3498 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses. --------- Co-authored-by: Celina G. Val <[email protected]>
- Loading branch information
1 parent
dba8f39
commit f888913
Showing
12 changed files
with
216 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
check_misaligned_ptr_cast_fail.safety_check\ | ||
Status: FAILURE\ | ||
Description: "misaligned pointer to reference cast: address must be a multiple of its type's alignment"\ | ||
in function check_misaligned_ptr_cast_fail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
//! This test checks that Kani detects UB resulting from converting a raw | ||
//! pointer to a reference when the pointer is not properly aligned. | ||
#[repr(align(4))] | ||
#[derive(Clone, Copy)] | ||
struct AlignedI32(i32); | ||
|
||
#[kani::proof] | ||
fn check_misaligned_ptr_cast_fail() { | ||
let data = AlignedI32(42); | ||
let ptr = &data as *const AlignedI32; | ||
|
||
unsafe { | ||
let misaligned = ptr.byte_add(1); | ||
let x = unsafe { &*misaligned }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
MyStr::new.safety_check\ | ||
Status: FAILURE\ | ||
Description: "dereference failure: pointer invalid"\ | ||
in function MyStr::new | ||
|
||
Verification failed for - check_size_of_val | ||
Verification failed for - check_slice_my_str | ||
Complete - 1 successfully verified harnesses, 2 failures, 3 total. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
//! This test case checks the usage of slices of slices (&[&[T]]). | ||
use std::mem::size_of_val; | ||
|
||
/// Structure with a raw string (i.e.: [char]). | ||
struct MyStr { | ||
header: u16, | ||
data: str, | ||
} | ||
|
||
impl MyStr { | ||
/// This creates a MyStr from a byte slice. | ||
fn new(original: &mut String) -> &mut Self { | ||
let buf = original.get_mut(..).unwrap(); | ||
assert!(size_of_val(buf) > 2, "This requires at least 2 bytes"); | ||
let unsized_len = buf.len() - 2; | ||
let ptr = std::ptr::slice_from_raw_parts_mut(buf.as_mut_ptr(), unsized_len); | ||
unsafe { &mut *(ptr as *mut Self) } | ||
} | ||
} | ||
|
||
#[kani::proof] | ||
fn sanity_check_my_str() { | ||
let mut buf = String::from("123456"); | ||
let my_str = MyStr::new(&mut buf); | ||
my_str.header = 0; | ||
|
||
assert_eq!(size_of_val(my_str), 6); | ||
assert_eq!(my_str.data.len(), 4); | ||
assert_eq!(my_str.data.chars().nth(0), Some('3')); | ||
assert_eq!(my_str.data.chars().nth(3), Some('6')); | ||
} | ||
|
||
#[kani::proof] | ||
fn check_slice_my_str() { | ||
let mut buf_0 = String::from("000"); | ||
let mut buf_1 = String::from("001"); | ||
let my_slice = &[MyStr::new(&mut buf_0), MyStr::new(&mut buf_1)]; | ||
assert_eq!(my_slice.len(), 2); | ||
|
||
assert_eq!(my_slice[0].data.len(), 1); | ||
assert_eq!(my_slice[1].data.len(), 1); | ||
|
||
assert_eq!(my_slice[0].data.chars().nth(0), Some('0')); | ||
assert_eq!(my_slice[1].data.chars().nth(0), Some('1')); | ||
} | ||
|
||
#[kani::proof] | ||
fn check_size_of_val() { | ||
let mut buf_0 = String::from("000"); | ||
let mut buf_1 = String::from("001"); | ||
let my_slice = &[MyStr::new(&mut buf_0), MyStr::new(&mut buf_1)]; | ||
assert_eq!(size_of_val(my_slice), 32); // Slice of 2 fat pointers. | ||
assert_eq!(size_of_val(my_slice[0]), 4); // Size of a fat pointer. | ||
assert_eq!(size_of_val(&my_slice[0].data), 1); // Size of str. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Status: FAILURE\ | ||
Description: "dereference failure: pointer invalid"\ | ||
|
||
Verification failed for - check_with_byte_add_fail | ||
Verification failed for - check_with_metadata_fail | ||
Complete - 1 successfully verified harnesses, 2 failures, 3 total. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
#![feature(set_ptr_value)] | ||
|
||
//! This test checks that Kani detects UB resulting from converting a raw | ||
//! pointer to a reference when the metadata is not valid. | ||
// Generate invalid fat pointer by combining the metadata. | ||
#[kani::proof] | ||
fn check_with_metadata_fail() { | ||
let short = [0u32; 2]; | ||
let long = [0u32; 10]; | ||
let ptr = &short as *const [u32]; | ||
// This should trigger UB since the slice is not valid for the new length. | ||
let fake_long = unsafe { &*ptr.with_metadata_of(&long) }; | ||
assert_eq!(fake_long.len(), long.len()); | ||
} | ||
|
||
#[kani::proof] | ||
fn check_with_byte_add_fail() { | ||
let data = [5u8; 5]; | ||
let ptr = &data as *const [u8]; | ||
// This should trigger UB since the metadata does not get adjusted. | ||
let val = unsafe { &*ptr.byte_add(1) }; | ||
assert_eq!(val.len(), data.len()); | ||
} | ||
|
||
#[kani::proof] | ||
fn check_with_byte_add_sub_pass() { | ||
let data = [5u8; 5]; | ||
let ptr = &data as *const [u8]; | ||
let offset = kani::any_where(|i| *i < 100); | ||
// This should pass since the resulting metadata is valid | ||
let val = unsafe { &*ptr.byte_add(offset).byte_sub(offset) }; | ||
assert_eq!(val.len(), data.len()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Status: FAILURE\ | ||
Description: "dereference failure: pointer invalid"\ | ||
|
||
VERIFICATION:- FAILED | ||
Verification failed for - check_with_metadata_fail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
#![feature(set_ptr_value)] | ||
|
||
//! This test checks that Kani detects UB resulting from converting a raw | ||
//! pointer to a reference when the metadata is not valid. | ||
#[kani::proof] | ||
fn check_with_metadata_fail() { | ||
let short = "sh"; | ||
let long = "longer"; | ||
let ptr = short as *const str; | ||
// This should trigger UB since the slice is not valid for the new length. | ||
let fake_long = unsafe { &*ptr.with_metadata_of(long) }; | ||
assert_eq!(fake_long.len(), long.len()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters