generated from amazon-archives/__template_Apache-2.0
-
Couldn't load subscription status.
- Fork 129
Add UB checks for ptr_offset_from* intrinsics #3757
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
celinval
merged 16 commits into
model-checking:main
from
celinval:issue-3756-offset-from
Jan 9, 2025
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ed7e698
Implement bounds check on offset
celinval 1632b67
Fix tests after enabling offset bound checks
celinval 0f027cf
Add UB checks for `ptr_offset_from*` intrinsics
celinval e825aee
Revert "Add UB checks for `ptr_offset_from*` intrinsics"
tautschnig dab2d65
Revert "Fix tests after enabling offset bound checks"
tautschnig 0f31a8d
Revert "Implement bounds check on offset"
tautschnig b58ec79
Merge remote-tracking branch 'origin/main' into issue-3756-offset-from
tautschnig 072303a
Add UB checks for `ptr_offset_from*` intrinsics
celinval e321b71
fix tests
049c1da
add test for distance offset_from UB
b3beba3
Add test for pointers to ZSTs
86aa2f9
Change offset_from with ZST to panic
celinval d1940aa
Fix ptr_offset_from model
celinval 67ebc69
Update pointer_inbounds.rs
celinval a7917c1
Merge branch 'main' into issue-3756-offset-from
celinval 509c6af
Apply suggestions from code review
celinval 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
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 |
|---|---|---|
| @@ -1,2 +1 @@ | ||
| FAILURE\ | ||
| "same object violation" | ||
| Failed Checks: Offset result and original pointer should point to the same allocation |
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| Failed Checks: attempt to compute unsigned offset with negative distance | ||
| Failed Checks: Expected non-negative distance between pointers |
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,6 @@ | ||
| Failed Checks: assertion failed: 0 < pointee_size && pointee_size <= isize::MAX as usize | ||
celinval marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Failed Checks: Offset result and original pointer should point to the same allocation | ||
|
|
||
| Verification failed for - check_offset_from_diff_alloc | ||
| Verification failed for - check_offset_from_oob_ptr | ||
| Complete - 2 successfully verified harnesses, 2 failures, 4 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,47 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| //! Check that Kani offset operations correctly detect out-of-bound access. | ||
|
|
||
| /// Verification should fail because safety violation is not a regular panic. | ||
| #[kani::proof] | ||
| #[kani::should_panic] | ||
| fn check_offset_from_oob_ptr() { | ||
| let val = 10u128; | ||
| let ptr: *const u128 = &val; | ||
| let ptr_oob: *const u128 = ptr.wrapping_add(10); | ||
| // SAFETY: This is not safe! | ||
| let _offset = unsafe { ptr_oob.offset_from(ptr) }; | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn check_offset_from_diff_alloc() { | ||
| let val1 = 10u128; | ||
| let val2 = 0u128; | ||
| let ptr1: *const u128 = &val1; | ||
| let ptr2: *const u128 = &val2; | ||
| // SAFETY: This is not safe! | ||
| let offset = unsafe { ptr1.offset_from(ptr2) }; | ||
| assert!(offset != 0); | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| #[kani::should_panic] | ||
| fn check_offset_from_unit_panic() { | ||
| let val1 = kani::any(); | ||
| let val2 = kani::any(); | ||
| let ptr1: *const () = &val1 as *const _ as *const (); | ||
| let ptr2: *const () = &val2 as *const _ as *const (); | ||
| // SAFETY: This is safe but will panic... | ||
| let _offset = unsafe { ptr1.offset_from(ptr2) }; | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn check_offset_from_same_dangling() { | ||
| let val = 10u128; | ||
| let ptr: *const u128 = &val; | ||
| let ptr_oob_1: *const u128 = ptr.wrapping_add(10); | ||
| let ptr_oob_2: *const u128 = ptr.wrapping_add(5).wrapping_add(5); | ||
| // SAFETY: This is safe since the pointer is the same! | ||
| let offset = unsafe { ptr_oob_1.offset_from(ptr_oob_2) }; | ||
| assert_eq!(offset, 0); | ||
| } |
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,29 @@ | ||
| Checking harness check_sub_ptr_same_dangling... | ||
| VERIFICATION:- SUCCESSFUL | ||
|
|
||
| Checking harness check_sub_ptr_unit_panic... | ||
| Failed Checks: assertion failed: 0 < pointee_size && pointee_size <= isize::MAX as usize | ||
| VERIFICATION:- SUCCESSFUL (encountered one or more panics as expected) | ||
|
|
||
| Checking harness check_sub_ptr_negative_result... | ||
| Failed Checks: Expected non-negative distance between pointers | ||
| VERIFICATION:- FAILED | ||
|
|
||
| Checking harness check_sub_ptr_diff_alloc... | ||
| Failed Checks: Offset result and original pointer should point to the same allocation | ||
| VERIFICATION:- FAILED | ||
|
|
||
| Checking harness check_sub_ptr_oob_ptr... | ||
| Failed Checks: Offset result and original pointer should point to the same allocation | ||
| VERIFICATION:- FAILED | ||
|
|
||
| Checking harness check_sub_ptr_self_oob... | ||
| Failed Checks: Offset result and original pointer should point to the same allocation | ||
| VERIFICATION:- FAILED | ||
|
|
||
| Summary: | ||
| Verification failed for - check_sub_ptr_negative_result | ||
| Verification failed for - check_sub_ptr_diff_alloc | ||
| Verification failed for - check_sub_ptr_oob_ptr | ||
| Verification failed for - check_sub_ptr_self_oob | ||
| Complete - 2 successfully verified harnesses, 4 failures, 6 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,70 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| //! Check that Kani can detect UB due to `sub_ptr` with out-of-bounds pointer or wrong order. | ||
|
|
||
| #![feature(ptr_sub_ptr)] | ||
|
|
||
| #[kani::proof] | ||
| fn check_sub_ptr_self_oob() { | ||
| let val = 10u128; | ||
| let ptr: *const u128 = &val; | ||
| let ptr_oob: *const u128 = ptr.wrapping_add(10); | ||
| // SAFETY: This is not safe! | ||
| let _offset = unsafe { ptr_oob.sub_ptr(ptr) }; | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn check_sub_ptr_oob_ptr() { | ||
| let val = 10u128; | ||
| let ptr: *const u128 = &val; | ||
| let ptr_oob: *const u128 = ptr.wrapping_sub(10); | ||
| // SAFETY: This is not safe! | ||
| let _offset = unsafe { ptr.sub_ptr(ptr_oob) }; | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn check_sub_ptr_diff_alloc() { | ||
| let val1 = kani::any(); | ||
| let val2 = kani::any(); | ||
| let ptr1: *const u128 = &val1; | ||
| let ptr2: *const u128 = &val2; | ||
| // SAFETY: This is not safe! | ||
| let _offset = unsafe { ptr1.sub_ptr(ptr2) }; | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn check_sub_ptr_negative_result() { | ||
| let val: [u8; 10] = kani::any(); | ||
| let ptr_first: *const _ = &val[0]; | ||
| let ptr_last: *const _ = &val[9]; | ||
| // SAFETY: This is safe! | ||
| let offset_ok = unsafe { ptr_last.sub_ptr(ptr_first) }; | ||
|
|
||
| // SAFETY: This is not safe! | ||
| let offset_not_ok = unsafe { ptr_first.sub_ptr(ptr_last) }; | ||
|
|
||
| // Just use the result. | ||
| assert!(offset_ok != offset_not_ok); | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| #[kani::should_panic] | ||
| fn check_sub_ptr_unit_panic() { | ||
| let val1 = kani::any(); | ||
| let val2 = kani::any(); | ||
| let ptr1: *const () = &val1 as *const _ as *const (); | ||
| let ptr2: *const () = &val2 as *const _ as *const (); | ||
| // SAFETY: This is safe but will panic... | ||
| let _offset = unsafe { ptr1.sub_ptr(ptr2) }; | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn check_sub_ptr_same_dangling() { | ||
| let val = 10u128; | ||
| let ptr: *const u128 = &val; | ||
| let ptr_oob_1: *const u128 = ptr.wrapping_add(10); | ||
| let ptr_oob_2: *const u128 = ptr.wrapping_add(5).wrapping_add(5); | ||
| // SAFETY: This is safe since the pointer is the same! | ||
| let offset = unsafe { ptr_oob_1.sub_ptr(ptr_oob_2) }; | ||
| assert_eq!(offset, 0); | ||
| } |
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,3 @@ | ||
| Failed Checks: Expected the distance between the pointers, in bytes, to be a | ||
| multiple of the size of `T` | ||
| VERIFICATION:- FAILED |
Oops, something went wrong.
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.