-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fn that checks pointers point to same allocation (#3583)
This changes adds a new function that returns whether two pointers points to the same allocated object. Since we cannot reason about addresses to objects that are dead or haven't been allocated, we panic if the user provides dangling pointers. ## Call-out: - Creating this as draft for now for early feedback By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
- Loading branch information
Showing
3 changed files
with
95 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// kani-flags: -Z mem-predicates | ||
//! Check same allocation predicate. | ||
|
||
extern crate kani; | ||
|
||
use kani::mem::same_allocation; | ||
use kani::{AllocationStatus, ArbitraryPointer, PointerGenerator}; | ||
|
||
#[kani::proof] | ||
fn check_inbounds() { | ||
let mut generator = PointerGenerator::<100>::new(); | ||
let ArbitraryPointer { ptr: ptr1, .. } = generator.any_in_bounds::<u8>(); | ||
let ArbitraryPointer { ptr: ptr2, .. } = generator.any_in_bounds::<u8>(); | ||
assert!(same_allocation(ptr1, ptr2)); | ||
} | ||
|
||
#[kani::proof] | ||
fn check_inbounds_other_alloc() { | ||
let mut generator1 = PointerGenerator::<100>::new(); | ||
let mut generator2 = PointerGenerator::<100>::new(); | ||
let ArbitraryPointer { ptr: ptr1, .. } = generator1.any_in_bounds::<u8>(); | ||
let ArbitraryPointer { ptr: ptr2, .. } = generator2.any_in_bounds::<u8>(); | ||
assert!(!same_allocation(ptr1, ptr2)); | ||
} | ||
|
||
#[kani::proof] | ||
fn check_dangling() { | ||
let mut generator = PointerGenerator::<100>::new(); | ||
let ArbitraryPointer { ptr: ptr1, status: status1, .. } = generator.any_alloc_status::<u8>(); | ||
let ArbitraryPointer { ptr: ptr2, status: status2, .. } = generator.any_alloc_status::<u8>(); | ||
kani::assume(status1 == AllocationStatus::Dangling && status2 == AllocationStatus::InBounds); | ||
assert!(!same_allocation(ptr1, ptr2)); | ||
} | ||
|
||
#[kani::proof] | ||
fn check_one_dead() { | ||
let mut generator = PointerGenerator::<100>::new(); | ||
let ArbitraryPointer { ptr: ptr1, status: status1, .. } = generator.any_alloc_status::<u8>(); | ||
let ArbitraryPointer { ptr: ptr2, status: status2, .. } = generator.any_alloc_status::<u8>(); | ||
kani::assume(status1 == AllocationStatus::DeadObject && status2 == AllocationStatus::InBounds); | ||
assert!(!same_allocation(ptr1, ptr2)); | ||
} | ||
|
||
#[kani::proof] | ||
fn check_dyn_alloc() { | ||
let mut generator1 = Box::new(PointerGenerator::<100>::new()); | ||
let mut generator2 = Box::new(PointerGenerator::<100>::new()); | ||
let ArbitraryPointer { ptr: ptr1a, .. } = generator1.any_in_bounds::<u8>(); | ||
let ArbitraryPointer { ptr: ptr1b, .. } = generator1.any_in_bounds::<u8>(); | ||
assert!(same_allocation(ptr1a, ptr1b)); | ||
|
||
let ArbitraryPointer { ptr: ptr2, .. } = generator2.any_in_bounds::<u8>(); | ||
assert!(!same_allocation(ptr1a, ptr2)); | ||
} |