vstd_extra/external/
ptr.rs1use core::marker::PointeeSized;
2use vstd::{
3 prelude::*,
4 raw_ptr::{MemContents, PointsTo, PtrData, ptr_mut_from_data},
5};
6
7verus! {
8
9pub assume_specification<T: PointeeSized>[ <*const T>::map_addr ](
10 ptr: *const T,
11 f: impl FnOnce(usize) -> usize,
12) -> (ret: *const T)
13 requires
14 f.requires((ptr@.addr,)),
15 ensures
16 ret@.metadata == ptr@.metadata,
17 ret@.provenance == ptr@.provenance,
18 f.ensures((ptr@.addr,), ret@.addr),
19;
20
21pub assume_specification<T: PointeeSized>[ <*mut T>::map_addr ](
22 ptr: *mut T,
23 f: impl FnOnce(usize) -> usize,
24) -> (ret: *mut T)
25 requires
26 f.requires((ptr@.addr,)),
27 ensures
28 ret@.metadata == ptr@.metadata,
29 ret@.provenance == ptr@.provenance,
30 f.ensures((ptr@.addr,), ret@.addr),
31;
32
33#[verifier::inline]
34pub open spec fn ptr_cast_spec<T: PointeeSized, U>(ptr: *const T) -> *const U {
35 ptr as *const U
36}
37
38#[verifier::inline]
39pub open spec fn ptr_mut_cast_spec<T: PointeeSized, U>(ptr: *mut T) -> *mut U {
40 ptr as *mut U
41}
42
43#[verifier::inline]
44pub open spec fn ptr_mut_cast_const_spec<T: PointeeSized>(ptr: *mut T) -> *const T {
45 ptr as *const T
46}
47
48#[verifier::inline]
49pub open spec fn ptr_cast_mut_spec<T: PointeeSized>(ptr: *const T) -> *mut T {
50 ptr as *mut T
51}
52
53#[verifier::inline]
54pub open spec fn ptr_is_null_spec<T: PointeeSized>(ptr: *const T) -> bool {
55 ptr.addr() == 0
56}
57
58#[verifier::inline]
59pub open spec fn ptr_mut_is_null_spec<T: PointeeSized>(ptr: *mut T) -> bool {
60 ptr.addr() == 0
61}
62
63#[verifier::when_used_as_spec(ptr_cast_spec)]
64pub assume_specification<T: PointeeSized, U>[ <*const T>::cast::<U> ](ptr: *const T) -> *const U
65 returns
66 ptr as *const U,
67 opens_invariants none
68 no_unwind
69;
70
71#[verifier::when_used_as_spec(ptr_mut_cast_spec)]
72pub assume_specification<T: PointeeSized, U>[ <*mut T>::cast::<U> ](ptr: *mut T) -> *mut U
73 returns
74 ptr as *mut U,
75 opens_invariants none
76 no_unwind
77;
78
79#[verifier::when_used_as_spec(ptr_mut_cast_const_spec)]
80pub assume_specification<T: PointeeSized>[ <*mut T>::cast_const ](ptr: *mut T) -> *const T
81 returns
82 ptr as *const T,
83 opens_invariants none
84 no_unwind
85;
86
87#[verifier::when_used_as_spec(ptr_cast_mut_spec)]
88pub assume_specification<T: PointeeSized>[ <*const T>::cast_mut ](ptr: *const T) -> *mut T
89 returns
90 ptr as *mut T,
91 opens_invariants none
92 no_unwind
93;
94
95#[verifier::when_used_as_spec(ptr_is_null_spec)]
97pub assume_specification<T: PointeeSized>[ <*const T>::is_null ](ptr: *const T) -> bool
98 returns
99 ptr.addr() == 0,
100 opens_invariants none
101 no_unwind
102;
103
104#[verifier::when_used_as_spec(ptr_mut_is_null_spec)]
106pub assume_specification<T: PointeeSized>[ <*mut T>::is_null ](ptr: *mut T) -> bool
107 returns
108 ptr.addr() == 0,
109 opens_invariants none
110 no_unwind
111;
112
113pub assume_specification<T>[ <*mut T>::is_aligned ](ptr: *mut T) -> bool
115 returns
116 ptr.addr() % core::mem::align_of::<T>() == 0,
117 opens_invariants none
118 no_unwind
119;
120
121#[verifier::inline]
123pub open spec fn ptr_mut_add_spec<T: PointeeSized + Sized>(p: *mut T, count: usize) -> *mut T {
124 ptr_mut_from_data(
125 PtrData::<T> {
126 addr: ((p.addr() + count * size_of::<T>()) % (usize::MAX as int + 1)) as usize,
127 ..p@
128 },
129 )
130}
131
132} #[verus_verify(external_body)]
141#[verus_spec(ret =>
142 with
143 Ghost(is_rust_allocation): Ghost<bool>,
144 Tracked(perm): Tracked<Option<&mut PointsTo<T>>>,
145 requires
146 ptr.addr() % align_of::<T>() == 0,
147 is_rust_allocation ==> (perm matches Some(p) && {
148 &&& p.ptr() == ptr
149 &&& p.is_init()
150 }),
151 !is_rust_allocation ==> perm is None,
152 ensures
153 is_rust_allocation ==> (perm matches Some(p) && {
154 &&& final(p).ptr() == ptr
155 &&& final(p).is_uninit()
156 &&& ret == p.value()
157 }),
158 opens_invariants none
159 no_unwind when ptr.addr() % align_of::<T>() == 0
160)]
161pub unsafe fn read_volatile_wrapper<T>(ptr: *const T) -> T {
162 unsafe { core::ptr::read_volatile(ptr) }
164}
165
166#[verus_verify(external_body)]
171#[verus_spec(
172 with
173 Ghost(is_rust_allocation): Ghost<bool>,
174 Tracked(perm): Tracked<Option<&mut PointsTo<T>>>,
175 requires
176 ptr.addr() % align_of::<T>() == 0,
177 is_rust_allocation ==> (perm matches Some(p) && p.ptr() == ptr),
178 !is_rust_allocation ==> perm is None,
179 ensures
180 is_rust_allocation ==> (perm matches Some(p) && {
181 &&& final(p).ptr() == ptr
182 &&& final(p).opt_value() == MemContents::Init(value)
183 }),
184 opens_invariants none
185 no_unwind when ptr.addr() % align_of::<T>() == 0
186)]
187pub unsafe fn write_volatile_wrapper<T>(ptr: *mut T, value: T) {
188 unsafe { core::ptr::write_volatile(ptr, value) }
190}
191
192#[verus_verify(external_body)]
197#[verus_spec(
198 requires
199 p.addr() + count * size_of::<T>() <= usize::MAX,
200 count * size_of::<T>() <= isize::MAX,
201 returns ptr_mut_add_spec(p, count),
202 opens_invariants none
203 no_unwind when p.addr() + count * size_of::<T>() <= usize::MAX
204 && count * size_of::<T>() <= isize::MAX
205)]
206pub unsafe fn ptr_mut_add<T>(p: *mut T, count: usize) -> *mut T {
207 unsafe { p.add(count) }
209}