-
Notifications
You must be signed in to change notification settings - Fork 165
Inject pointer validity check when casting raw pointers to references #3221
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
aea2651
Inject pointer validity check when casting raw pointers to references
artemagvanian a7428a3
fix clippy warning
artemagvanian af6bcc6
Catch potentially invalid cast in std-checks
artemagvanian a2f3ad7
Try changing types in ReadOk expression
artemagvanian 129f930
Add an escape hatch for not injecting the check for drops
artemagvanian f8a1164
Refactor check injection into a separate function
artemagvanian 8d01aa5
Disable the problematic test in s2n-quic via overlay
artemagvanian d316adb
Fix copyright statement
artemagvanian ceaf8ef
Merge branch 'main' into main
artemagvanian 7d38bfe
Fix assertion message and test files
artemagvanian 739e6b7
Put the checks behind the flag
artemagvanian 347fc1a
Merge branch 'main' into main
artemagvanian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,75 @@ | ||
| Checking harness check_zst_deref... | ||
|
|
||
| Status: FAILURE | ||
|
artemagvanian marked this conversation as resolved.
Outdated
|
||
| Description: "dereferencing a pointer to invalid memory location" | ||
| Location: tests/expected/ptr_to_ref_cast/ptr_to_ref_cast.rs:131:35 in function zst_deref | ||
|
artemagvanian marked this conversation as resolved.
Outdated
|
||
|
|
||
| VERIFICATION:- FAILED | ||
|
|
||
| Checking harness check_equal_size_deref... | ||
|
|
||
| Status: SUCCESS | ||
| Description: "dereferencing a pointer to invalid memory location" | ||
| Location: tests/expected/ptr_to_ref_cast/ptr_to_ref_cast.rs:106:31 in function equal_size_deref | ||
|
|
||
| Status: SUCCESS | ||
| Description: "dereferencing a pointer to invalid memory location" | ||
| Location: tests/expected/ptr_to_ref_cast/ptr_to_ref_cast.rs:109:38 in function equal_size_deref | ||
|
|
||
| VERIFICATION:- SUCCESSFUL | ||
|
|
||
| Checking harness check_smaller_deref... | ||
|
|
||
| Status: SUCCESS | ||
| Description: "dereferencing a pointer to invalid memory location" | ||
| Location: tests/expected/ptr_to_ref_cast/ptr_to_ref_cast.rs:88:31 in function smaller_deref | ||
|
|
||
| Status: SUCCESS | ||
| Description: "dereferencing a pointer to invalid memory location" | ||
| Location: tests/expected/ptr_to_ref_cast/ptr_to_ref_cast.rs:92:38 in function smaller_deref | ||
|
|
||
| VERIFICATION:- SUCCESSFUL | ||
|
|
||
| Checking harness check_larger_deref_struct... | ||
|
|
||
| Status: FAILURE | ||
| Description: "dereferencing a pointer to invalid memory location" | ||
| Location: tests/expected/ptr_to_ref_cast/ptr_to_ref_cast.rs:74:31 in function larger_deref_struct | ||
|
|
||
| VERIFICATION:- FAILED | ||
|
|
||
| Checking harness check_larger_deref_into_ptr... | ||
|
|
||
| Status: FAILURE | ||
| Description: "dereferencing a pointer to invalid memory location" | ||
| Location: tests/expected/ptr_to_ref_cast/ptr_to_ref_cast.rs:48:38 in function larger_deref_into_ptr | ||
|
|
||
| VERIFICATION:- FAILED | ||
|
|
||
| Checking harness check_larger_deref... | ||
|
|
||
| Status: FAILURE | ||
| Description: "dereferencing a pointer to invalid memory location" | ||
| Location: tests/expected/ptr_to_ref_cast/ptr_to_ref_cast.rs:34:32 in function larger_deref | ||
|
|
||
| VERIFICATION:- FAILED | ||
|
|
||
| Checking harness check_store... | ||
|
|
||
| Status: FAILURE | ||
| Description: "dereferencing a pointer to invalid memory location" | ||
| Location: tests/expected/ptr_to_ref_cast/ptr_to_ref_cast.rs:17:28 in function Store::<'_, 3>::from | ||
|
|
||
| Status: SUCCESS | ||
| Description: "assertion failed: broken.data.len() == 3" | ||
| Location: tests/expected/ptr_to_ref_cast/ptr_to_ref_cast.rs:26:5 in function check_store | ||
|
|
||
| VERIFICATION:- FAILED | ||
|
|
||
| Summary: | ||
| Verification failed for - check_zst_deref | ||
| Verification failed for - check_larger_deref_struct | ||
| Verification failed for - check_larger_deref_into_ptr | ||
| Verification failed for - check_larger_deref | ||
| Verification failed for - check_store | ||
| Complete - 2 successfully verified harnesses, 5 failures, 7 total. | ||
This file contains hidden or 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,139 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| //! This test case checks that raw pointer validity is checked before converting it to a reference, e.g., &(*ptr). | ||
|
|
||
| // 1. Original example. | ||
|
|
||
| struct Store<'a, const LEN: usize> { | ||
| data: [&'a i128; LEN], | ||
| } | ||
|
|
||
| impl<'a, const LEN: usize> Store<'a, LEN> { | ||
| pub fn from(var: &i64) -> Self { | ||
| let ref1: *const i64 = var; | ||
| let ref2: *const i128 = ref1 as *const i128; | ||
| unsafe { | ||
| Store { data: [&*ref2; LEN] } // ---- THIS LINE SHOULD FAIL | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| pub fn check_store() { | ||
| let val = 1; | ||
| let broken = Store::<3>::from(&val); | ||
| assert_eq!(broken.data.len(), 3) | ||
| } | ||
|
|
||
| // 2. Make sure the error is raised when casting to a simple type of a larger size. | ||
|
|
||
| pub fn larger_deref(var: &i64) { | ||
| let ref1: *const i64 = var; | ||
| let ref2: *const i128 = ref1 as *const i128; | ||
| let ref3: &i128 = unsafe { &*ref2 }; // ---- THIS LINE SHOULD FAIL | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| pub fn check_larger_deref() { | ||
| let var: i64 = kani::any(); | ||
| larger_deref(&var); | ||
| } | ||
|
|
||
| // 3. Make sure the error is raised when casting to a simple type of a larger size and storing the result in a pointer. | ||
|
|
||
| pub fn larger_deref_into_ptr(var: &i64) { | ||
| let ref1: *const i64 = var; | ||
| let ref2: *const i128 = ref1 as *const i128; | ||
| let ref3: *const i128 = unsafe { &*ref2 }; // ---- THIS LINE SHOULD FAIL | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| pub fn check_larger_deref_into_ptr() { | ||
| let var: i64 = kani::any(); | ||
| larger_deref_into_ptr(&var); | ||
| } | ||
|
|
||
| // 4. Make sure the error is raised when casting to a struct of a larger size. | ||
|
|
||
| #[derive(kani::Arbitrary)] | ||
| struct Foo { | ||
| a: u8, | ||
| } | ||
|
|
||
| #[derive(kani::Arbitrary)] | ||
| struct Bar { | ||
| a: u8, | ||
| b: u64, | ||
| c: u64, | ||
| } | ||
|
|
||
| pub fn larger_deref_struct(var: &Foo) { | ||
| let ref1: *const Foo = var; | ||
| let ref2: *const Bar = ref1 as *const Bar; | ||
| let ref3: &Bar = unsafe { &*ref2 }; // ---- THIS LINE SHOULD FAIL | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| pub fn check_larger_deref_struct() { | ||
| let var: Foo = kani::any(); | ||
| larger_deref_struct(&var); | ||
| } | ||
|
|
||
| // 5. Make sure the error is not raised if the target size is smaller. | ||
|
|
||
| pub fn smaller_deref(var: &i64, var_struct: &Bar) { | ||
| let ref1: *const i64 = var; | ||
| let ref2: *const i32 = ref1 as *const i32; | ||
| let ref3: &i32 = unsafe { &*ref2 }; | ||
|
|
||
| let ref1_struct: *const Bar = var_struct; | ||
| let ref2_struct: *const Foo = ref1_struct as *const Foo; | ||
| let ref3_struct: &Foo = unsafe { &*ref2_struct }; | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| pub fn check_smaller_deref() { | ||
| let var: i64 = kani::any(); | ||
| let var_struct: Bar = kani::any(); | ||
| smaller_deref(&var, &var_struct); | ||
| } | ||
|
|
||
| // 6. Make sure the error is not raised if the target size is the same. | ||
|
|
||
| pub fn equal_size_deref(var: &i64, var_struct: &Foo) { | ||
| let ref1: *const i64 = var; | ||
| let ref2: &i64 = unsafe { &*ref1 }; | ||
|
|
||
| let ref1_struct: *const Foo = var_struct; | ||
| let ref2_struct: &Foo = unsafe { &*ref1_struct }; | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| pub fn check_equal_size_deref() { | ||
| let var: i64 = kani::any(); | ||
| let var_struct: Foo = kani::any(); | ||
| equal_size_deref(&var, &var_struct); | ||
| } | ||
|
|
||
| // 7. Make sure the check works with ZSTs. | ||
|
|
||
| #[derive(kani::Arbitrary)] | ||
| struct Zero; | ||
|
|
||
| pub fn zst_deref(var_struct: &Foo, var_zst: &Zero) { | ||
| let ref1_struct: *const Foo = var_struct; | ||
| let ref2_struct: *const Zero = ref1_struct as *const Zero; | ||
| let ref3_struct: &Zero = unsafe { &*ref2_struct }; | ||
|
|
||
| let ref1_zst: *const Zero = var_zst; | ||
| let ref2_zst: *const Foo = ref1_zst as *const Foo; | ||
| let ref3_zst: &Foo = unsafe { &*ref2_zst }; // ---- THIS LINE SHOULD FAIL | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| pub fn check_zst_deref() { | ||
| let var_struct: Foo = kani::any(); | ||
| let var_zst: Zero = kani::any(); | ||
| zst_deref(&var_struct, &var_zst); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.