Skip to main content

vstd_extra/
cast_ptr.rs

1use vstd::prelude::*;
2
3use vstd::raw_ptr::MemContents;
4use vstd::simple_pptr::{self, PPtr};
5use vstd::std_specs::convert::{FromSpec, FromSpecImpl};
6
7use core::marker::PhantomData;
8
9verus! {
10
11/// A trait for types that have a concrete representation type `R`.
12pub trait Repr<R: Sized>: Sized {
13    spec fn wf(r: R) -> bool;
14
15    spec fn from_repr_spec(r: R) -> Self;
16
17    fn to_repr(self) -> (res: R)
18        ensures
19            Self::wf(res),
20            Self::from_repr_spec(res) == self,
21    ;
22
23    fn from_borrowed<'a>(r: &'a R) -> (res: &'a Self)
24        requires
25            Self::wf(*r),
26        ensures
27            *res == Self::from_repr_spec(*r),
28    ;
29
30    /// Mutable projection of the representation. Implementations must keep
31    /// the representation consistent with mutations through the returned reference.
32    fn from_borrowed_mut<'a>(r: &'a mut R) -> (res: &'a mut Self)
33        requires
34            Self::wf(*old(r)),
35        ensures
36            *res == Self::from_repr_spec(*old(r)),
37            Self::wf(*final(r)),
38            *final(res) == Self::from_repr_spec(*final(r)),
39    ;
40}
41
42/// Concrete representation of a pointer to an object of type T with representation type R
43/// The length of the array is not stored in the pointer
44#[verifier::accept_recursive_types(T)]
45pub struct ReprPtr<R, T> {
46    pub ptr: PPtr<R>,
47    pub _T: PhantomData<T>,
48}
49
50impl<R, T> Clone for ReprPtr<R, T> {
51    fn clone(&self) -> Self
52        returns
53            self,
54    {
55        Self { ptr: self.ptr, _T: PhantomData }
56    }
57}
58
59impl<R, T> Copy for ReprPtr<R, T> {
60
61}
62
63impl<R, T> FromSpecImpl<PPtr<R>> for ReprPtr<R, T> {
64    open spec fn obeys_from_spec() -> bool {
65        true
66    }
67
68    open spec fn from_spec(ptr: PPtr<R>) -> Self {
69        Self { ptr, _T: PhantomData }
70    }
71}
72
73impl<R, T> From<PPtr<R>> for ReprPtr<R, T> {
74    fn from(ptr: PPtr<R>) -> Self {
75        Self { ptr, _T: PhantomData }
76    }
77}
78
79impl<R, T> FromSpecImpl<ReprPtr<R, T>> for PPtr<R> {
80    open spec fn obeys_from_spec() -> bool {
81        true
82    }
83
84    open spec fn from_spec(ptr: ReprPtr<R, T>) -> Self {
85        ptr.ptr
86    }
87}
88
89impl<R, T> From<ReprPtr<R, T>> for PPtr<R> {
90    fn from(ptr: ReprPtr<R, T>) -> Self {
91        ptr.ptr
92    }
93}
94
95impl<R, T> ReprPtr<R, T> {
96    pub open spec fn new_spec(ptr: PPtr<R>) -> Self {
97        Self { ptr, _T: PhantomData }
98    }
99
100    pub fn from_pptr(ptr: PPtr<R>) -> (res: Self)
101        ensures
102            res == Self::new_spec(ptr),
103            res.addr() == ptr.addr(),
104            res.ptr == ptr,
105    {
106        Self { ptr, _T: PhantomData }
107    }
108
109    pub open spec fn to_pptr(self) -> PPtr<R> {
110        self.ptr
111    }
112
113    pub open spec fn addr_spec(self) -> usize {
114        self.ptr.addr()
115    }
116
117    #[verifier::when_used_as_spec(addr_spec)]
118    pub fn addr(self) -> usize
119        returns
120            self.addr(),
121    {
122        self.ptr.addr()
123    }
124}
125
126impl<R, T: Repr<R>> ReprPtr<R, T> {
127    pub fn put(self, Tracked(perm): Tracked<&mut PointsTo<R, T>>, v: T)
128        requires
129            old(perm).pptr() == self,
130            old(perm).mem_contents() == MemContents::Uninit::<T>,
131        ensures
132            final(perm).pptr() == old(perm).pptr(),
133            final(perm).mem_contents() == MemContents::Init(v),
134            final(perm).wf(),
135            final(perm).points_to.is_init(),
136    {
137        self.ptr.put(Tracked(&mut perm.points_to), v.to_repr())
138    }
139
140    pub fn borrow<'a>(self, Tracked(perm): Tracked<&'a PointsTo<R, T>>) -> (v: &'a T)
141        requires
142            perm.pptr() == self,
143            perm.is_init(),
144            perm.wf(),
145        ensures
146            *v == perm.value(),
147    {
148        T::from_borrowed(self.ptr.borrow(Tracked(&perm.points_to)))
149    }
150
151    /// Borrows the pointed-to `T` mutably for the lifetime of `perm`.
152    ///
153    /// While the returned borrow is live, `perm` is exclusively held and
154    /// cannot be used. The returned borrow is tied to the tracked permission:
155    /// after the borrow ends, the permission's value matches the final `*v`. Callers must
156    /// preserve any invariants beyond the final initialised/well-formed state
157    /// themselves.
158    pub fn borrow_mut<'a>(self, Tracked(perm): Tracked<&'a mut PointsTo<R, T>>) -> (v: &'a mut T)
159        requires
160            old(perm).pptr() == self,
161            old(perm).is_init(),
162            old(perm).wf(),
163        ensures
164            *v == old(perm).value(),
165            final(perm).pptr() == old(perm).pptr(),
166            final(perm).is_init(),
167            final(perm).wf(),
168            final(perm).value() == *final(v),
169    {
170        /* `Repr` conversions need not preserve layout; project through the representation.
171         * Origin Rust: unsafe { &mut *(self.ptr.addr() as *mut T) }
172         */
173        T::from_borrowed_mut(self.ptr.borrow_mut(Tracked(&mut perm.points_to)))
174    }
175}
176
177#[verifier::accept_recursive_types(T)]
178pub tracked struct PointsTo<R, T: Repr<R>> {
179    pub points_to: simple_pptr::PointsTo<R>,
180    pub _T: PhantomData<T>,
181}
182
183impl<R, T: Repr<R>> PointsTo<R, T> {
184    pub open spec fn new_spec(points_to: simple_pptr::PointsTo<R>) -> Self {
185        Self { points_to, _T: PhantomData }
186    }
187
188    pub proof fn new(tracked points_to: simple_pptr::PointsTo<R>) -> (tracked res: Self)
189        returns
190            Self::new_spec(points_to),
191    {
192        Self { points_to, _T: PhantomData }
193    }
194
195    pub open spec fn wf(self) -> bool {
196        &&& T::wf(self.points_to.value())
197    }
198
199    pub open spec fn addr(self) -> usize {
200        self.points_to.addr()
201    }
202
203    pub open spec fn mem_contents(self) -> MemContents<T> {
204        match self.points_to.mem_contents() {
205            MemContents::<R>::Uninit => MemContents::<T>::Uninit,
206            MemContents::<R>::Init(r) => MemContents::<T>::Init(T::from_repr_spec(r)),
207        }
208    }
209
210    pub open spec fn is_init(self) -> bool {
211        self.mem_contents().is_init()
212    }
213
214    pub open spec fn is_uninit(self) -> bool {
215        self.mem_contents().is_uninit()
216    }
217
218    pub open spec fn value(self) -> T
219        recommends
220            self.is_init(),
221    {
222        self.mem_contents().value()
223    }
224
225    pub open spec fn pptr(self) -> ReprPtr<R, T> {
226        ReprPtr { ptr: self.points_to.pptr(), _T: PhantomData }
227    }
228}
229
230} // verus!