Skip to content

Commit 2b3bdfd

Browse files
author
Remi Delmas
committed
Propagate changes from rust-lang/rust#137483
Rename `sub_ptr` to `offset_from_unsigned`. The feature gate is still `ptr_sub_ptr`.
1 parent 06113dd commit 2b3bdfd

File tree

6 files changed

+32
-30
lines changed

6 files changed

+32
-30
lines changed

kani-compiler/src/kani_middle/kani_functions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ pub enum KaniModel {
8787
Offset,
8888
#[strum(serialize = "PtrOffsetFromModel")]
8989
PtrOffsetFrom,
90-
#[strum(serialize = "PtrSubPtrModel")]
91-
PtrSubPtr,
90+
#[strum(serialize = "PtrOffsetFromUnsignedModel")]
91+
PtrOffsetFromUnsigned,
9292
#[strum(serialize = "RunContractModel")]
9393
RunContract,
9494
#[strum(serialize = "RunLoopContractModel")]

kani-compiler/src/kani_middle/transform/rustc_intrinsics.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ impl MutMirVisitor for ReplaceIntrinsicCallVisitor<'_> {
168168
Intrinsic::SizeOfVal => self.models[&KaniModel::SizeOfVal],
169169
Intrinsic::MinAlignOfVal => self.models[&KaniModel::AlignOfVal],
170170
Intrinsic::PtrOffsetFrom => self.models[&KaniModel::PtrOffsetFrom],
171-
Intrinsic::PtrOffsetFromUnsigned => self.models[&KaniModel::PtrSubPtr],
171+
Intrinsic::PtrOffsetFromUnsigned => {
172+
self.models[&KaniModel::PtrOffsetFromUnsigned]
173+
}
172174
// The rest is handled in codegen.
173175
_ => {
174176
return self.super_terminator(term);

library/kani_core/src/models.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ macro_rules! generate_models {
8989
}
9090
}
9191

92-
#[kanitool::fn_marker = "PtrSubPtrModel"]
93-
pub unsafe fn ptr_sub_ptr<T>(ptr1: *const T, ptr2: *const T) -> usize {
92+
#[kanitool::fn_marker = "PtrOffsetFromUnsignedModel"]
93+
pub unsafe fn ptr_offset_from_unsigned<T>(ptr1: *const T, ptr2: *const T) -> usize {
9494
let offset = ptr_offset_from(ptr1, ptr2);
9595
kani::safety_check(offset >= 0, "Expected non-negative distance between pointers");
9696
offset as usize
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
Checking harness check_sub_ptr_same_dangling...
1+
Checking harness check_offset_from_unsigned_same_dangling...
22
VERIFICATION:- SUCCESSFUL
33

4-
Checking harness check_sub_ptr_unit_panic...
4+
Checking harness check_offset_from_unsigned_unit_panic...
55
Failed Checks: assertion failed: 0 < pointee_size && pointee_size <= isize::MAX as usize
66
VERIFICATION:- SUCCESSFUL (encountered one or more panics as expected)
77

8-
Checking harness check_sub_ptr_negative_result...
8+
Checking harness check_offset_from_unsigned_negative_result...
99
Failed Checks: Expected non-negative distance between pointers
1010
VERIFICATION:- FAILED
1111

12-
Checking harness check_sub_ptr_diff_alloc...
12+
Checking harness check_offset_from_unsigned_diff_alloc...
1313
Failed Checks: Offset result and original pointer should point to the same allocation
1414
VERIFICATION:- FAILED
1515

16-
Checking harness check_sub_ptr_oob_ptr...
16+
Checking harness check_offset_from_unsigned_oob_ptr...
1717
Failed Checks: Offset result and original pointer should point to the same allocation
1818
VERIFICATION:- FAILED
1919

20-
Checking harness check_sub_ptr_self_oob...
20+
Checking harness check_offset_from_unsigned_self_oob...
2121
Failed Checks: Offset result and original pointer should point to the same allocation
2222
VERIFICATION:- FAILED
2323

2424
Summary:
25-
Verification failed for - check_sub_ptr_negative_result
26-
Verification failed for - check_sub_ptr_diff_alloc
27-
Verification failed for - check_sub_ptr_oob_ptr
28-
Verification failed for - check_sub_ptr_self_oob
25+
Verification failed for - check_offset_from_unsigned_negative_result
26+
Verification failed for - check_offset_from_unsigned_diff_alloc
27+
Verification failed for - check_offset_from_unsigned_oob_ptr
28+
Verification failed for - check_offset_from_unsigned_self_oob
2929
Complete - 2 successfully verified harnesses, 4 failures, 6 total.
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,70 @@
11
// Copyright Kani Contributors
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
3-
//! Check that Kani can detect UB due to `sub_ptr` with out-of-bounds pointer or wrong order.
3+
//! Check that Kani can detect UB due to `offset_from_unsigned` with out-of-bounds pointer or wrong order.
44
55
#![feature(ptr_sub_ptr)]
66

77
#[kani::proof]
8-
fn check_sub_ptr_self_oob() {
8+
fn check_offset_from_unsigned_self_oob() {
99
let val = 10u128;
1010
let ptr: *const u128 = &val;
1111
let ptr_oob: *const u128 = ptr.wrapping_add(10);
1212
// SAFETY: This is not safe!
13-
let _offset = unsafe { ptr_oob.sub_ptr(ptr) };
13+
let _offset = unsafe { ptr_oob.offset_from_unsigned(ptr) };
1414
}
1515

1616
#[kani::proof]
17-
fn check_sub_ptr_oob_ptr() {
17+
fn check_offset_from_unsigned_oob_ptr() {
1818
let val = 10u128;
1919
let ptr: *const u128 = &val;
2020
let ptr_oob: *const u128 = ptr.wrapping_sub(10);
2121
// SAFETY: This is not safe!
22-
let _offset = unsafe { ptr.sub_ptr(ptr_oob) };
22+
let _offset = unsafe { ptr.offset_from_unsigned(ptr_oob) };
2323
}
2424

2525
#[kani::proof]
26-
fn check_sub_ptr_diff_alloc() {
26+
fn check_offset_from_unsigned_diff_alloc() {
2727
let val1 = kani::any();
2828
let val2 = kani::any();
2929
let ptr1: *const u128 = &val1;
3030
let ptr2: *const u128 = &val2;
3131
// SAFETY: This is not safe!
32-
let _offset = unsafe { ptr1.sub_ptr(ptr2) };
32+
let _offset = unsafe { ptr1.offset_from_unsigned(ptr2) };
3333
}
3434

3535
#[kani::proof]
36-
fn check_sub_ptr_negative_result() {
36+
fn check_offset_from_unsigned_negative_result() {
3737
let val: [u8; 10] = kani::any();
3838
let ptr_first: *const _ = &val[0];
3939
let ptr_last: *const _ = &val[9];
4040
// SAFETY: This is safe!
41-
let offset_ok = unsafe { ptr_last.sub_ptr(ptr_first) };
41+
let offset_ok = unsafe { ptr_last.offset_from_unsigned(ptr_first) };
4242

4343
// SAFETY: This is not safe!
44-
let offset_not_ok = unsafe { ptr_first.sub_ptr(ptr_last) };
44+
let offset_not_ok = unsafe { ptr_first.offset_from_unsigned(ptr_last) };
4545

4646
// Just use the result.
4747
assert!(offset_ok != offset_not_ok);
4848
}
4949

5050
#[kani::proof]
5151
#[kani::should_panic]
52-
fn check_sub_ptr_unit_panic() {
52+
fn check_offset_from_unsigned_unit_panic() {
5353
let val1 = kani::any();
5454
let val2 = kani::any();
5555
let ptr1: *const () = &val1 as *const _ as *const ();
5656
let ptr2: *const () = &val2 as *const _ as *const ();
5757
// SAFETY: This is safe but will panic...
58-
let _offset = unsafe { ptr1.sub_ptr(ptr2) };
58+
let _offset = unsafe { ptr1.offset_from_unsigned(ptr2) };
5959
}
6060

6161
#[kani::proof]
62-
fn check_sub_ptr_same_dangling() {
62+
fn check_offset_from_unsigned_same_dangling() {
6363
let val = 10u128;
6464
let ptr: *const u128 = &val;
6565
let ptr_oob_1: *const u128 = ptr.wrapping_add(10);
6666
let ptr_oob_2: *const u128 = ptr.wrapping_add(5).wrapping_add(5);
6767
// SAFETY: This is safe since the pointer is the same!
68-
let offset = unsafe { ptr_oob_1.sub_ptr(ptr_oob_2) };
68+
let offset = unsafe { ptr_oob_1.offset_from_unsigned(ptr_oob_2) };
6969
assert_eq!(offset, 0);
7070
}

tests/kani/PointerOffset/offset_from_vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ fn offset_non_power_two() {
1818
let offset = kani::any_where(|o: &usize| *o <= v.len());
1919
let begin = v.as_mut_ptr();
2020
let end = begin.add(offset);
21-
assert_eq!(end.sub_ptr(begin), offset);
21+
assert_eq!(end.offset_from_unsigned(begin), offset);
2222
}
2323
}

0 commit comments

Comments
 (0)