Skip to content

Commit 1dbaed7

Browse files
committed
Revert the changes to use intermediate usize
1 parent e1c1549 commit 1dbaed7

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

library/kani_core/src/models.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ macro_rules! generate_models {
5959
let orig_ptr = ptr.to_const_ptr();
6060
// NOTE: For CBMC, using the pointer addition can have unexpected behavior
6161
// when the offset is higher than the object bits since it will wrap around.
62-
// TODO: Use `wrapping_byte_offset` once we fix:
63-
// https://github.com/model-checking/kani/issues/1150
64-
let new_ptr = orig_ptr.addr().wrapping_add_signed(byte_offset) as *const T;
62+
// See for more details: https://github.com/model-checking/kani/issues/1150
63+
//
64+
// However, when I tried implementing this using usize operation, we got some
65+
// unexpected failures that still require further debugging.
66+
// let new_ptr = orig_ptr.addr().wrapping_add_signed(byte_offset) as *const T;
67+
let new_ptr = orig_ptr.wrapping_byte_offset(byte_offset);
6568
kani::safety_check(
6669
kani::mem::same_allocation_internal(orig_ptr, new_ptr),
6770
"Offset result and original pointer must point to the same allocation",

tests/expected/offset-bounds-check/offset_usize.expected renamed to tests/expected/offset-bounds-check/fixme_offset_usize.expected

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
SUMMARY:\
2-
** 5 of
2+
** 6 of
3+
Failed Checks: "Expected 0 and 1 to be the only safe values for offset"
34
Failed Checks: Offset in bytes overflows isize
45
Failed Checks: Offset result and original pointer must point to the same allocation
56
Failed Checks: Offset value overflows isize
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
//! Check different violations that can be triggered when providing an usize offset
4+
//! with a type that has size > 1.
5+
6+
#![feature(core_intrinsics)]
7+
use std::intrinsics::offset;
8+
use std::ptr::addr_of;
9+
10+
/// This harness exercises different scenarios when providing unconstrained offset counter.
11+
///
12+
/// We expect the following UB to be detected:
13+
/// 1. The offset value, `delta`, itself is greater than `isize::MAX`.
14+
/// 2. The offset in bytes, `delta * size_of::<u32>()`, is greater than `isize::MAX`.
15+
/// 3. Offset result does not point to the same allocation as the original pointer.
16+
///
17+
/// The offset operation should only succeed for delta values:
18+
/// - `0`: The new pointer is the same as the base of the array.
19+
/// - `1`: The new pointer points to the end of the allocation.
20+
///
21+
/// FIXME: Because of CBMC wrapping behavior with pointer arithmetic, the assertion that checks
22+
/// that `delta <= 1` currently fails. See <https://github.com/model-checking/kani/issues/1150>.
23+
#[kani::proof]
24+
fn check_intrinsic_args() {
25+
let array = [0u32];
26+
let delta: usize = kani::any();
27+
let new = unsafe { offset(addr_of!(array), delta) };
28+
assert!(delta <= 1, "Expected 0 and 1 to be the only safe values for offset");
29+
assert_eq!(new, &array, "This should fail for delta `1`");
30+
assert_ne!(new, &array, "This should fail for delta `0`");
31+
}

tests/expected/offset-bounds-check/offset_usize.rs

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)