Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions library/kani_core/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,9 @@ macro_rules! kani_mem {

/// Get the object offset of the given pointer.
#[doc(hidden)]
#[crate::kani::unstable_feature(
feature = "ghost-state",
issue = 3184,
reason = "experimental ghost state/shadow memory API"
)]
#[kanitool::fn_marker = "PointerOffsetHook"]
#[inline(never)]
pub fn pointer_offset<T: PointeeSized>(_ptr: *const T) -> usize {
pub(crate) fn pointer_offset<T: PointeeSized>(_ptr: *const T) -> usize {
kani_intrinsic()
}
};
Expand Down
4 changes: 3 additions & 1 deletion library/kani_core/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ macro_rules! generate_models {
"Offset result and original pointer should point to the same allocation",
);
// The offset must fit in isize since this represents the same allocation.
let offset_bytes = ptr1.addr().wrapping_sub(ptr2.addr()) as isize;
let offset_bytes = kani::mem::pointer_offset(ptr1)
.wrapping_sub(kani::mem::pointer_offset(ptr2))
as isize;
let t_size = size_of::<T>() as isize;
kani::safety_check(
offset_bytes % t_size == 0,
Expand Down
7 changes: 6 additions & 1 deletion tests/expected/shadow/unsupported_object_size/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
// This test checks the maximum object size supported by Kani's shadow
// memory model (currently 64)

#![feature(core_intrinsics)]
use std::intrinsics::ptr_offset_from_unsigned;

static mut SM: kani::shadow::ShadowMem<bool> = kani::shadow::ShadowMem::new(false);

fn check_max_objects<const N: usize>() {
let arr: [u8; N] = [0; N];
let last = &arr[N - 1];
assert_eq!(kani::mem::pointer_offset(last as *const u8), N - 1);
unsafe {
assert_eq!(ptr_offset_from_unsigned(last as *const u8, &arr[0] as *const u8), N - 1);
}
// the following call to `set_init` would fail if the object offset for
// `last` exceeds the maximum allowed by Kani's shadow memory model
unsafe {
Expand Down
3 changes: 3 additions & 0 deletions tests/expected/string-repeat/2235.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Failed Checks: called `Option::unwrap()` on a `None` value
Verification failed for - repeat_panic
Complete - 1 successfully verified harnesses, 1 failures, 2 total.
14 changes: 14 additions & 0 deletions tests/expected/string-repeat/2235.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

#[kani::proof]
fn repeat_const() {
let s = String::from("a").repeat(1);
assert_eq!(s.chars().nth(0).unwrap(), 'a');
}

#[kani::proof]
fn repeat_panic() {
let x = String::new().repeat(1);
let z = x.chars().nth(1).unwrap();
}
Loading