|
| 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 | +} |
0 commit comments