-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propagate changes from rust-lang/rust#137483
Rename `sub_ptr` to `offset_from_unsigned`. The feature gate is still `ptr_sub_ptr`.
- Loading branch information
Remi Delmas
committed
Feb 27, 2025
1 parent
06113dd
commit 2b3bdfd
Showing
6 changed files
with
32 additions
and
30 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
20 changes: 10 additions & 10 deletions
20
...cted/offset-bounds-check/sub_ptr.expected → ...ounds-check/offset_from_unsigned.expected
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 |
---|---|---|
@@ -1,29 +1,29 @@ | ||
Checking harness check_sub_ptr_same_dangling... | ||
Checking harness check_offset_from_unsigned_same_dangling... | ||
VERIFICATION:- SUCCESSFUL | ||
|
||
Checking harness check_sub_ptr_unit_panic... | ||
Checking harness check_offset_from_unsigned_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... | ||
Checking harness check_offset_from_unsigned_negative_result... | ||
Failed Checks: Expected non-negative distance between pointers | ||
VERIFICATION:- FAILED | ||
|
||
Checking harness check_sub_ptr_diff_alloc... | ||
Checking harness check_offset_from_unsigned_diff_alloc... | ||
Failed Checks: Offset result and original pointer should point to the same allocation | ||
VERIFICATION:- FAILED | ||
|
||
Checking harness check_sub_ptr_oob_ptr... | ||
Checking harness check_offset_from_unsigned_oob_ptr... | ||
Failed Checks: Offset result and original pointer should point to the same allocation | ||
VERIFICATION:- FAILED | ||
|
||
Checking harness check_sub_ptr_self_oob... | ||
Checking harness check_offset_from_unsigned_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 | ||
Verification failed for - check_offset_from_unsigned_negative_result | ||
Verification failed for - check_offset_from_unsigned_diff_alloc | ||
Verification failed for - check_offset_from_unsigned_oob_ptr | ||
Verification failed for - check_offset_from_unsigned_self_oob | ||
Complete - 2 successfully verified harnesses, 4 failures, 6 total. |
28 changes: 14 additions & 14 deletions
28
...s/expected/offset-bounds-check/sub_ptr.rs → ...fset-bounds-check/offset_from_unsigned.rs
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 |
---|---|---|
@@ -1,70 +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. | ||
//! Check that Kani can detect UB due to `offset_from_unsigned` with out-of-bounds pointer or wrong order. | ||
#![feature(ptr_sub_ptr)] | ||
|
||
#[kani::proof] | ||
fn check_sub_ptr_self_oob() { | ||
fn check_offset_from_unsigned_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) }; | ||
let _offset = unsafe { ptr_oob.offset_from_unsigned(ptr) }; | ||
} | ||
|
||
#[kani::proof] | ||
fn check_sub_ptr_oob_ptr() { | ||
fn check_offset_from_unsigned_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) }; | ||
let _offset = unsafe { ptr.offset_from_unsigned(ptr_oob) }; | ||
} | ||
|
||
#[kani::proof] | ||
fn check_sub_ptr_diff_alloc() { | ||
fn check_offset_from_unsigned_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) }; | ||
let _offset = unsafe { ptr1.offset_from_unsigned(ptr2) }; | ||
} | ||
|
||
#[kani::proof] | ||
fn check_sub_ptr_negative_result() { | ||
fn check_offset_from_unsigned_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) }; | ||
let offset_ok = unsafe { ptr_last.offset_from_unsigned(ptr_first) }; | ||
|
||
// SAFETY: This is not safe! | ||
let offset_not_ok = unsafe { ptr_first.sub_ptr(ptr_last) }; | ||
let offset_not_ok = unsafe { ptr_first.offset_from_unsigned(ptr_last) }; | ||
|
||
// Just use the result. | ||
assert!(offset_ok != offset_not_ok); | ||
} | ||
|
||
#[kani::proof] | ||
#[kani::should_panic] | ||
fn check_sub_ptr_unit_panic() { | ||
fn check_offset_from_unsigned_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) }; | ||
let _offset = unsafe { ptr1.offset_from_unsigned(ptr2) }; | ||
} | ||
|
||
#[kani::proof] | ||
fn check_sub_ptr_same_dangling() { | ||
fn check_offset_from_unsigned_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) }; | ||
let offset = unsafe { ptr_oob_1.offset_from_unsigned(ptr_oob_2) }; | ||
assert_eq!(offset, 0); | ||
} |
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