#[cfg(creusot)]
use crate::std::mem::{align_of_logic, size_of_logic, size_of_val_logic};
use crate::{
ghost::perm::{Container, Perm},
prelude::*,
};
use core::marker::PhantomData;
#[cfg(creusot)]
use core::ptr::Pointee;
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
#[logic(opaque)]
pub fn metadata_logic<T: ?Sized>(_: *const T) -> <T as Pointee>::Metadata {
dead
}
#[logic(open, inline)]
#[intrinsic("metadata_matches")]
pub fn metadata_matches<T: ?Sized>(_value: T, _metadata: <T as Pointee>::Metadata) -> bool {
dead
}
#[allow(unused)]
#[logic]
#[intrinsic("metadata_matches_slice")]
fn metadata_matches_slice<T>(value: [T], len: usize) -> bool {
pearlite! { value@.len() == len@ }
}
#[allow(unused)]
#[logic]
#[intrinsic("metadata_matches_str")]
fn metadata_matches_str(value: str, len: usize) -> bool {
pearlite! { value@.to_bytes().len() == len@ }
}
#[allow(unused_variables)]
#[logic(open, inline)]
#[intrinsic("is_aligned_logic")]
pub fn is_aligned_logic<T: ?Sized>(ptr: *const T) -> bool {
dead
}
#[allow(unused)]
#[logic]
#[intrinsic("is_aligned_logic_sized")]
fn is_aligned_logic_sized<T>(ptr: *const T) -> bool {
ptr.is_aligned_to_logic(align_of_logic::<T>())
}
#[allow(unused)]
#[logic]
#[intrinsic("is_aligned_logic_slice")]
fn is_aligned_logic_slice<T>(ptr: *const [T]) -> bool {
ptr.is_aligned_to_logic(align_of_logic::<T>())
}
#[allow(dead_code)]
pub struct PtrDeepModel {
pub addr: usize,
runtime_metadata: usize,
}
impl<T: ?Sized> DeepModel for *mut T {
type DeepModelTy = PtrDeepModel;
#[trusted]
#[logic(opaque)]
#[ensures(result.addr == self.addr_logic())]
fn deep_model(self) -> Self::DeepModelTy {
dead
}
}
impl<T: ?Sized> DeepModel for *const T {
type DeepModelTy = PtrDeepModel;
#[trusted]
#[logic(opaque)]
#[ensures(result.addr == self.addr_logic())]
fn deep_model(self) -> Self::DeepModelTy {
dead
}
}
pub trait PointerExt<T: ?Sized>: Sized {
#[logic]
fn addr_logic(self) -> usize;
#[logic(open, sealed)]
fn is_null_logic(self) -> bool {
self.addr_logic() == 0usize
}
#[logic(open, sealed)]
fn is_aligned_to_logic(self, align: usize) -> bool {
pearlite! { self.addr_logic() & (align - 1usize) == 0usize }
}
#[logic]
fn is_aligned_logic(self) -> bool;
}
impl<T: ?Sized> PointerExt<T> for *const T {
#[logic]
#[cfg_attr(target_pointer_width = "16", builtin("creusot.prelude.Ptr$BW$.addr_logic_u16"))]
#[cfg_attr(target_pointer_width = "32", builtin("creusot.prelude.Ptr$BW$.addr_logic_u32"))]
#[cfg_attr(target_pointer_width = "64", builtin("creusot.prelude.Ptr$BW$.addr_logic_u64"))]
fn addr_logic(self) -> usize {
dead
}
#[logic(open, inline)]
fn is_aligned_logic(self) -> bool {
is_aligned_logic(self)
}
}
impl<T: ?Sized> PointerExt<T> for *mut T {
#[logic]
#[cfg_attr(target_pointer_width = "16", builtin("creusot.prelude.Ptr$BW$.addr_logic_u16"))]
#[cfg_attr(target_pointer_width = "32", builtin("creusot.prelude.Ptr$BW$.addr_logic_u32"))]
#[cfg_attr(target_pointer_width = "64", builtin("creusot.prelude.Ptr$BW$.addr_logic_u64"))]
fn addr_logic(self) -> usize {
dead
}
#[logic(open, inline)]
fn is_aligned_logic(self) -> bool {
is_aligned_logic(self)
}
}
pub trait SizedPointerExt<T>: PointerExt<T> {
#[logic]
#[requires(0 <= self.addr_logic()@ + offset * size_of_logic::<T>())]
#[requires(self.addr_logic()@ + offset * size_of_logic::<T>() <= usize::MAX@)]
#[ensures(result.addr_logic()@ == self.addr_logic()@ + offset * size_of_logic::<T>())]
fn offset_logic(self, offset: Int) -> Self;
#[logic(law)]
#[ensures(self.offset_logic(0) == self)]
fn offset_logic_zero(self);
#[logic(law)]
#[ensures(self.offset_logic(offset1).offset_logic(offset2) == self.offset_logic(offset1 + offset2))]
fn offset_logic_assoc(self, offset1: Int, offset2: Int);
#[logic]
fn sub_logic(self, rhs: Self) -> Int;
#[logic(law)]
#[ensures(self.sub_logic(self) == 0)]
fn sub_logic_refl(self);
#[logic(law)]
#[ensures(self.offset_logic(offset).sub_logic(self) == offset)]
#[ensures(self.sub_logic(self.offset_logic(offset)) == - offset)]
fn sub_offset_logic(self, offset: Int);
}
impl<T> SizedPointerExt<T> for *const T {
#[trusted]
#[logic(opaque)]
#[requires(0 <= self.addr_logic()@ + offset * size_of_logic::<T>())]
#[requires(self.addr_logic()@ + offset * size_of_logic::<T>() <= usize::MAX@)]
#[ensures(result.addr_logic()@ == self.addr_logic()@ + offset * size_of_logic::<T>())]
fn offset_logic(self, offset: Int) -> Self {
dead
}
#[trusted]
#[logic(law)]
#[ensures(self.offset_logic(0) == self)]
fn offset_logic_zero(self) {}
#[trusted]
#[logic(law)]
#[ensures(self.offset_logic(offset1).offset_logic(offset2) == self.offset_logic(offset1 + offset2))]
fn offset_logic_assoc(self, offset1: Int, offset2: Int) {}
#[allow(unused)]
#[trusted]
#[logic(opaque)]
fn sub_logic(self, rhs: Self) -> Int {
dead
}
#[trusted]
#[logic(law)]
#[ensures(self.sub_logic(self) == 0)]
fn sub_logic_refl(self) {}
#[trusted]
#[logic(law)]
#[ensures(self.offset_logic(offset).sub_logic(self) == offset)]
#[ensures(self.sub_logic(self.offset_logic(offset)) == - offset)]
fn sub_offset_logic(self, offset: Int) {}
}
impl<T> SizedPointerExt<T> for *mut T {
#[logic(open, inline)]
#[requires(0 <= self.addr_logic()@ + offset * size_of_logic::<T>())]
#[requires(self.addr_logic()@ + offset * size_of_logic::<T>() <= usize::MAX@)]
#[ensures(result.addr_logic()@ == self.addr_logic()@ + offset * size_of_logic::<T>())]
fn offset_logic(self, offset: Int) -> Self {
pearlite! { (self as *const T).offset_logic(offset) as *mut T }
}
#[logic(law)]
#[ensures(self.offset_logic(0) == self)]
fn offset_logic_zero(self) {}
#[logic(law)]
#[ensures(self.offset_logic(offset1).offset_logic(offset2) == self.offset_logic(offset1 + offset2))]
fn offset_logic_assoc(self, offset1: Int, offset2: Int) {}
#[logic(open, inline)]
fn sub_logic(self, rhs: Self) -> Int {
pearlite! { (self as *const T).sub_logic(rhs as *const T) }
}
#[logic(law)]
#[ensures(self.sub_logic(self) == 0)]
fn sub_logic_refl(self) {}
#[logic(law)]
#[ensures(self.offset_logic(offset).sub_logic(self) == offset)]
#[ensures(self.sub_logic(self.offset_logic(offset)) == - offset)]
fn sub_offset_logic(self, offset: Int) {}
}
pub trait SlicePointerExt<T>: PointerExt<[T]> {
#[logic]
fn thin(self) -> *const T;
#[logic]
fn len_logic(self) -> usize;
#[logic(law)]
#[ensures(self.thin() == other.thin() && self.len_logic() == other.len_logic() ==> self == other)]
fn slice_ptr_ext(self, other: Self);
}
impl<T> SlicePointerExt<T> for *const [T] {
#[logic(open, inline)]
fn thin(self) -> *const T {
self as *const T
}
#[logic(open, inline)]
fn len_logic(self) -> usize {
pearlite! { metadata_logic(self) }
}
#[trusted]
#[logic(law)]
#[ensures(self.thin() == other.thin() && self.len_logic() == other.len_logic() ==> self == other)]
fn slice_ptr_ext(self, other: Self) {}
}
impl<T> SlicePointerExt<T> for *mut [T] {
#[logic(open, inline)]
fn thin(self) -> *const T {
self as *const T
}
#[logic(open, inline)]
fn len_logic(self) -> usize {
pearlite! { metadata_logic(self as *const [T]) }
}
#[logic(law)]
#[ensures(self.thin() == other.thin() && self.len_logic() == other.len_logic() ==> self == other)]
fn slice_ptr_ext(self, other: Self) {
(self as *const [T]).slice_ptr_ext(other as *const [T])
}
}
extern_spec! {
impl<T: ?Sized> *const T {
#[check(ghost)]
#[ensures(result == self.addr_logic())]
fn addr(self) -> usize;
#[check(ghost)]
#[ensures(result == self.is_null_logic())]
fn is_null(self) -> bool;
#[check(ghost)]
#[erasure]
#[ensures(result == self as _)]
fn cast<U>(self) -> *const U {
self as _
}
#[check(ghost)]
#[erasure]
#[ensures(result == self as _)]
const fn cast_mut(self) -> *mut T {
self as _
}
#[check(terminates)]
#[erasure]
#[ensures(result == self.is_aligned_logic())]
fn is_aligned(self) -> bool
where T: Sized,
{
self.is_aligned_to(core::mem::align_of::<T>())
}
#[check(ghost)]
#[erasure]
#[bitwise_proof]
#[requires(align != 0usize && align & (align - 1usize) == 0usize)]
#[ensures(result == self.is_aligned_to_logic(align))]
fn is_aligned_to(self, align: usize) -> bool
{
if !align.is_power_of_two() {
::core::panic::panic_2021!("is_aligned_to: align is not a power-of-two");
}
self.addr() & (align - 1) == 0
}
}
impl<T: ?Sized> *mut T {
#[check(ghost)]
#[ensures(result == self.addr_logic())]
fn addr(self) -> usize;
#[check(ghost)]
#[ensures(result == self.is_null_logic())]
fn is_null(self) -> bool;
#[check(ghost)]
#[erasure]
#[ensures(result == self as _)]
fn cast<U>(self) -> *mut U {
self as _
}
#[check(ghost)]
#[erasure]
#[ensures(result == self as _)]
fn cast_const(self) -> *const T {
self as _
}
#[check(terminates)]
#[erasure]
#[ensures(result == self.is_aligned_logic())]
fn is_aligned(self) -> bool
where T: Sized,
{
self.is_aligned_to(core::mem::align_of::<T>())
}
#[check(ghost)]
#[erasure]
#[bitwise_proof]
#[requires(align != 0usize && align & (align - 1usize) == 0usize)]
#[ensures(result == self.is_aligned_to_logic(align))]
fn is_aligned_to(self, align: usize) -> bool
{
if !align.is_power_of_two() {
::core::panic::panic_2021!("is_aligned_to: align is not a power-of-two");
}
self.addr() & (align - 1) == 0
}
}
impl<T> *const [T] {
#[ensures(result == metadata_logic(self))]
fn len(self) -> usize;
}
impl<T> *mut [T] {
#[ensures(result == metadata_logic(self))]
fn len(self) -> usize;
}
mod core {
mod ptr {
#[check(ghost)]
#[ensures(result.is_null_logic())]
fn null<T>() -> *const T
where
T: core::ptr::Thin + ?Sized;
#[check(ghost)]
#[ensures(result.is_null_logic())]
fn null_mut<T>() -> *mut T
where
T: core::ptr::Thin + ?Sized;
#[check(ghost)]
#[ensures(result == (p.addr_logic() == q.addr_logic()))]
fn addr_eq<T, U>(p: *const T, q: *const U) -> bool
where
T: ?Sized, U: ?Sized;
#[check(ghost)]
#[ensures(result == metadata_logic(ptr))]
fn metadata<T: ?Sized>(ptr: *const T) -> <T as Pointee>::Metadata;
#[check(ghost)]
#[ensures(false)]
unsafe fn read_volatile<T>(src: *const T) -> T;
#[ensures(result as *const T == data && result.len_logic() == len)]
fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T];
#[ensures(result as *mut T == data && result.len_logic() == len)]
fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T];
}
}
impl<T> Clone for *mut T {
#[check(ghost)]
#[ensures(result == *self)]
fn clone(&self) -> *mut T {
*self
}
}
impl<T> Clone for *const T {
#[check(ghost)]
#[ensures(result == *self)]
fn clone(&self) -> *const T {
*self
}
}
}
impl<T: ?Sized> Container for *const T {
type Value = T;
#[logic(open, inline)]
fn is_disjoint(&self, self_val: &T, other: &Self, other_val: &T) -> bool {
pearlite! {
size_of_val_logic(*self_val) != 0 && size_of_val_logic(*other_val) != 0 ==>
self.addr_logic() != other.addr_logic()
}
}
}
impl<T: ?Sized> Invariant for Perm<*const T> {
#[logic(open, prophetic)]
fn invariant(self) -> bool {
pearlite! {
!self.ward().is_null_logic()
&& metadata_matches(*self.val(), metadata_logic(*self.ward()))
&& inv(self.val())
}
}
}
impl<T: ?Sized> Perm<*const T> {
#[check(terminates)] #[ensures(*result.1.ward() == result.0 && *result.1.val() == v)]
pub fn new(v: T) -> (*mut T, Ghost<Box<Perm<*const T>>>)
where
T: Sized,
{
Self::from_box(Box::new(v))
}
#[trusted]
#[check(terminates)] #[ensures(*result.1.ward() == result.0 && *result.1.val() == *val)]
#[erasure(Box::into_raw)]
pub fn from_box(val: Box<T>) -> (*mut T, Ghost<Box<Perm<*const T>>>) {
(Box::into_raw(val), Ghost::conjure())
}
#[trusted]
#[check(terminates)] #[ensures(*result.1.ward() == result.0)]
#[ensures(*result.1.val() == *r)]
#[intrinsic("perm_from_ref")]
pub fn from_ref(r: &T) -> (*const T, Ghost<&Perm<*const T>>) {
(r, Ghost::conjure())
}
#[trusted]
#[check(terminates)] #[ensures(*result.1.ward() == result.0)]
#[ensures(*result.1.val() == *r)]
#[ensures(*(^result.1.inner_logic()).val() == ^r)]
#[intrinsic("perm_from_mut")]
pub fn from_mut(r: &mut T) -> (*mut T, Ghost<&mut Perm<*const T>>) {
(r, Ghost::conjure())
}
#[trusted]
#[check(terminates)]
#[requires(ptr == *own.ward())]
#[ensures(*result == *own.val())]
#[allow(unused_variables)]
#[intrinsic("perm_as_ref")]
pub unsafe fn as_ref(ptr: *const T, own: Ghost<&Perm<*const T>>) -> &T {
unsafe { &*ptr }
}
#[trusted]
#[check(terminates)]
#[allow(unused_variables)]
#[requires(ptr as *const T == *own.ward())]
#[ensures(*result == *own.val())]
#[ensures((^own).ward() == own.ward())]
#[ensures(*(^own).val() == ^result)]
#[intrinsic("perm_as_mut")]
pub unsafe fn as_mut(ptr: *mut T, own: Ghost<&mut Perm<*const T>>) -> &mut T {
unsafe { &mut *ptr }
}
#[trusted]
#[check(terminates)]
#[requires(ptr as *const T == *own.ward())]
#[ensures(*result == *own.val())]
#[allow(unused_variables)]
#[erasure(Box::from_raw)]
pub unsafe fn to_box(ptr: *mut T, own: Ghost<Box<Perm<*const T>>>) -> Box<T> {
unsafe { Box::from_raw(ptr) }
}
#[check(terminates)]
#[requires(ptr as *const T == *own.ward())]
pub unsafe fn drop(ptr: *mut T, own: Ghost<Box<Perm<*const T>>>) {
let _ = unsafe { Self::to_box(ptr, own) };
}
}
impl<T> Perm<*const [T]> {
#[logic(open, inline)]
pub fn len(self) -> Int {
pearlite! { self.val()@.len() }
}
#[trusted]
#[check(ghost)]
#[requires(0 <= index && index <= self.len())]
#[ensures(self.ward().thin() == result.0.ward().thin())]
#[ensures(self.ward().thin().offset_logic(index) == result.1.ward().thin())]
#[ensures(self.val()@[..index] == result.0.val()@)]
#[ensures(self.val()@[index..] == result.1.val()@)]
pub fn split_at(&self, index: Int) -> (&Self, &Self) {
let _ = index;
panic!("called ghost function in normal code")
}
#[trusted]
#[check(ghost)]
#[requires(0 <= index && index <= self.len())]
#[ensures(self.ward().thin() == result.0.ward().thin())]
#[ensures(self.ward().thin().offset_logic(index) == result.1.ward().thin())]
#[ensures(self.val()@[..index] == result.0.val()@)]
#[ensures(self.val()@[index..] == result.1.val()@)]
#[ensures((^self).ward() == self.ward())]
#[ensures((^result.0).val()@.len() == index)]
#[ensures((^self).val()@ == (^result.0).val()@.concat((^result.1).val()@))]
pub fn split_at_mut(&mut self, index: Int) -> (&mut Perm<*const [T]>, &mut Perm<*const [T]>) {
let _ = index;
panic!("called ghost function in normal code")
}
#[trusted]
#[check(ghost)]
#[ensures(result.len() == self.len())]
#[ensures(forall<i> 0 <= i && i < self.len()
==> *result[i].ward() == self.ward().thin().offset_logic(i)
&& *result[i].val() == self.val()@[i])]
pub fn elements(&self) -> Seq<&Perm<*const T>> {
panic!("called ghost function in normal code")
}
#[trusted]
#[check(ghost)]
#[ensures(result.len() == self.len())]
#[ensures(forall<i> 0 <= i && i < self.len()
==> *result[i].ward() == self.ward().thin().offset_logic(i)
&& *result[i].val() == self.val()@[i])]
#[ensures((^self).ward() == self.ward())]
#[ensures(forall<i> 0 <= i && i < self.len() ==> *(^result[i]).val() == (^self).val()@[i])]
pub fn elements_mut(&mut self) -> Seq<&mut Perm<*const T>> {
panic!("called ghost function in normal code")
}
#[check(ghost)]
#[requires(0 <= index && index < self.len())]
#[ensures(*result.ward() == self.ward().thin().offset_logic(index))]
#[ensures(*result.val() == self.val()@[index])]
pub fn index(&self, index: Int) -> &Perm<*const T> {
let mut r = self.elements();
r.split_off_ghost(index).pop_front_ghost().unwrap()
}
#[check(ghost)]
#[requires(0 <= index && index < self.len())]
#[ensures(*result.ward() == self.ward().thin().offset_logic(index))]
#[ensures(*result.val() == self.val()@[index])]
#[ensures((^self).ward() == self.ward())]
#[ensures(*(^result).val() == (^self).val()@[index])]
#[ensures(forall<k: Int> 0 <= k && k < self.len() && k != index ==> (^self).val()@[k] == self.val()@[k])]
pub fn index_mut(&mut self, index: Int) -> &mut Perm<*const T> {
let mut r = self.elements_mut();
proof_assert! { forall<k> index < k && k < r.len() ==> r[k].val() == r[index..].tail()[k-index-1].val() };
let _r = snapshot! { r };
let result = r.split_off_ghost(index).pop_front_ghost().unwrap();
proof_assert! { forall<i> 0 <= i && i < index ==> r[i] == _r[i] }; result
}
#[trusted]
#[check(ghost)]
#[ensures(result.ward() == self.ward().thin())]
#[ensures(result.len()@ == self.len())]
pub fn live(&self) -> PtrLive<'_, T> {
panic!("called ghost function in normal code")
}
#[trusted]
#[check(ghost)]
#[ensures(result.ward() == self.ward().thin())]
#[ensures(result.len()@ == self.len())]
pub fn live_mut<'a, 'b>(self: &'b &'a mut Self) -> PtrLive<'a, T> {
panic!("called ghost function in normal code")
}
}
#[opaque]
pub struct PtrLive<'a, T>(PhantomData<&'a T>);
impl<T> Clone for PtrLive<'_, T> {
#[trusted]
#[check(ghost)]
#[ensures(result == *self)]
fn clone(&self) -> Self {
panic!("called ghost function in normal code")
}
}
impl<T> Copy for PtrLive<'_, T> {}
impl<T> Invariant for PtrLive<'_, T> {
#[logic(open, prophetic)]
fn invariant(self) -> bool {
pearlite! {
self.len()@ * size_of_logic::<T>() <= isize::MAX@
&& self.ward().addr_logic()@ + self.len()@ * size_of_logic::<T>() <= usize::MAX@
&& self.ward().is_aligned_logic()
}
}
}
impl<T> PtrLive<'_, T> {
#[trusted]
#[logic(opaque)]
pub fn ward(self) -> *const T {
dead
}
#[trusted]
#[logic(opaque)]
pub fn len(self) -> usize {
dead
}
#[logic(open, inline)]
pub fn contains_range(self, ptr: *const T, len: Int) -> bool {
pearlite! {
let offset = ptr.sub_logic(self.ward());
ptr == self.ward().offset_logic(offset)
&& 0 <= offset && offset <= self.len()@
&& 0 <= offset + len && offset + len <= self.len()@
}
}
}
pub trait PtrAddExt<'a, T> {
#[requires(false)]
unsafe fn add_live(self, offset: usize, live: Ghost<PtrLive<'a, T>>) -> Self;
#[requires(false)]
unsafe fn offset_live(self, offset: isize, live: Ghost<PtrLive<'a, T>>) -> Self;
}
impl<'a, T> PtrAddExt<'a, T> for *const T {
#[trusted]
#[erasure(<*const T>::add)]
#[requires(live.contains_range(self, offset@))]
#[ensures(result == self.offset_logic(offset@))]
unsafe fn add_live(self, offset: usize, live: Ghost<PtrLive<'a, T>>) -> Self {
let _ = live;
unsafe { self.add(offset) }
}
#[trusted]
#[erasure(<*const T>::offset)]
#[requires(live.contains_range(self, offset@))]
#[ensures(result == self.offset_logic(offset@))]
unsafe fn offset_live(self, offset: isize, live: Ghost<PtrLive<'a, T>>) -> Self {
let _ = live;
unsafe { self.offset(offset) }
}
}
impl<'a, T> PtrAddExt<'a, T> for *mut T {
#[trusted]
#[erasure(<*mut T>::add)]
#[requires(live.contains_range(self, offset@))]
#[ensures(result == self.offset_logic(offset@))]
unsafe fn add_live(self, offset: usize, live: Ghost<PtrLive<'a, T>>) -> Self {
let _ = live;
unsafe { self.add(offset) }
}
#[trusted]
#[erasure(<*mut T>::offset)]
#[requires(live.contains_range(self, offset@))]
#[ensures(result == self.offset_logic(offset@))]
unsafe fn offset_live(self, offset: isize, live: Ghost<PtrLive<'a, T>>) -> Self {
let _ = live;
unsafe { self.offset(offset) }
}
}