Skip to main content

vstd_extra/
raw_ptr.rs

1//! Raw-pointer permissions retaining exposed provenance for address round trips.
2use vstd::{
3    prelude::*,
4    raw_ptr::{IsExposed, PointsTo},
5};
6
7verus! {
8
9/// Typed memory access paired with a proof that its provenance has been exposed.
10/// This permission carries no allocation or deallocation ownership.
11pub tracked struct ExposedPointsTo<T> {
12    points_to: PointsTo<T>,
13    exposed: IsExposed,
14}
15
16impl<T> ExposedPointsTo<T> {
17    #[verifier::type_invariant]
18    closed spec fn type_inv(self) -> bool {
19        self.points_to.ptr()@.provenance == self.exposed.provenance()
20    }
21
22    pub closed spec fn ptr(self) -> *const T {
23        self.points_to.ptr() as *const T
24    }
25
26    pub open spec fn ptr_mut(self) -> *mut T {
27        self.ptr() as *mut T
28    }
29
30    pub open spec fn addr(self) -> usize {
31        self.ptr().addr()
32    }
33
34    pub closed spec fn is_init(self) -> bool {
35        self.points_to.is_init()
36    }
37
38    pub closed spec fn value(self) -> T {
39        self.points_to.value()
40    }
41
42    /// Packages an existing access permission with its provenance exposure proof.
43    pub proof fn new(tracked points_to: PointsTo<T>, tracked exposed: IsExposed) -> (tracked perm:
44        Self)
45        requires
46            points_to.ptr()@.provenance == exposed.provenance(),
47        ensures
48            perm.ptr() == points_to.ptr(),
49            perm.is_init() == points_to.is_init(),
50            perm.value() == points_to.value(),
51    {
52        Self { points_to, exposed }
53    }
54}
55
56/// Reconstructs a pointer at the address recorded by its exposed permission.
57pub fn ptr_from_addr<T>(addr: usize, Tracked(perm): Tracked<&ExposedPointsTo<T>>) -> *const T
58    requires
59        perm.addr() == addr,
60    returns
61        perm.ptr(),
62    opens_invariants none
63    no_unwind
64{
65    proof {
66        use_type_invariant(perm);
67    }
68    /* vstd requires an erased witness for the exposed provenance of this cast.
69     * Origin Rust: addr as *const T
70     */
71    vstd::raw_ptr::with_exposed_provenance(addr, Tracked(perm.exposed)) as *const T
72}
73
74/// Borrows initialized memory through its exposed pointer permission.
75pub fn ptr_ref<'a, T>(ptr: *const T, Tracked(perm): Tracked<&'a ExposedPointsTo<T>>) -> &'a T
76    requires
77        ptr == perm.ptr(),
78        perm.is_init(),
79    returns
80        perm.value(),
81    opens_invariants none
82    no_unwind
83{
84    /* vstd checks raw dereferences through an explicit permission argument.
85     * Origin Rust: unsafe { &*ptr }
86     */
87    vstd::raw_ptr::ptr_ref(ptr, Tracked(&perm.points_to))
88}
89
90/// Reconstructs a mutable pointer at the address recorded by its exposed permission.
91pub fn ptr_mut_from_addr<T>(addr: usize, Tracked(perm): Tracked<&ExposedPointsTo<T>>) -> *mut T
92    requires
93        perm.addr() == addr,
94    returns
95        perm.ptr_mut(),
96    opens_invariants none
97    no_unwind
98{
99    /* vstd requires an erased witness for the exposed provenance of this cast.
100     * Origin Rust: addr as *mut T
101     */
102    ptr_from_addr::<T>(addr, Tracked(perm)) as *mut T
103}
104
105/// Exclusively borrows initialized memory, reflecting mutations in its permission.
106pub fn ptr_mut_ref<'a, T>(
107    ptr: *mut T,
108    Tracked(perm): Tracked<&'a mut ExposedPointsTo<T>>,
109) -> (value: &'a mut T)
110    requires
111        ptr == old(perm).ptr_mut(),
112        old(perm).is_init(),
113    ensures
114        final(perm).ptr_mut() == ptr,
115        final(perm).is_init(),
116        *value == old(perm).value(),
117        *final(value) == final(perm).value(),
118    opens_invariants none
119    no_unwind
120{
121    proof {
122        use_type_invariant(&*perm);
123    }
124    /* vstd checks raw mutable dereferences through an exclusive permission borrow.
125     * Origin Rust: unsafe { &mut *ptr }
126     */
127    vstd::raw_ptr::ptr_mut_ref(ptr, Tracked(&mut perm.points_to))
128}
129
130} // verus!