1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//! Generic permissions for accessing memory pointed to by pointers or within an interior mutable
//! type.
use crate*;
use cratestructural_resolve;
/// Trait for the types that can be used in a [`Perm`].
/// Token that represents the ownership of the contents of a container object. The container is
/// either an interior mutable type (e.g., `Perm` or atomic types) or a raw pointer.
///
/// A `Perm` only exists in the ghost world, and it must be used in conjunction with its container
/// in order to read or write the value.
///
/// Permissions are made unsized to guarantee that they cannot be replaced in a mutable reference.
/// This would allow the permission to outlive the reference it has been placed in. This makes it
/// easier to specify splitting a mutable reference of a permission to a slice, and makes it
/// possible to specify functions such as [`Perm::from_mut`].
///
/// # Pointer permissions
///
/// A particular case of permissions is the case of permissions for raw pointers (i.e., `C` is
/// `*const T`). In this case, the permission represents the ownership of the memory cell.
///
/// A warning regarding memory leaks: dropping a `Perm<*const T>` cannot deallocate the memory
/// corresponding to the pointer because it is a ghost value. One must thus remember to explicitly
/// call [`drop`] in order to free the memory tracked by a `Perm<*const T>` token.
///
/// ## Safety
///
/// When using Creusot to verify the code, all methods should be safe to call. Indeed,
/// Creusot ensures that every operation on the inner value uses the right [`Perm`] object
/// created by [`Perm::new`], ensuring safety in a manner similar to [ghost_cell](https://docs.rs/ghost-cell/latest/ghost_cell/).
///
/// ## `#[check(terminates)]`
///
/// `Perm<*const T>` methods, particularly constructors (`new`, `from_box`, `from_ref`, `from_mut`),
/// are marked `check(terminates)` rather than `check(ghost)` to prevent two things from happening
/// in ghost code:
/// 1. running out of pointer addresses;
/// 2. allocating too large objects.
///
/// Note that we already can't guard against these issues in program code.
/// But preventing them in ghost code is even more imperative to ensure soundness.
///
/// Specifically, creating too many pointers contradicts the [`Perm::disjoint_lemma`],
/// and allocating too large objects contradicts the [`Perm::invariant`] that
/// allocations have size at most `isize::MAX`.
///
/// ## Layout facts
///
/// Certain facts about the layout and alignment of pointers can be made available
/// through the type invariant of [`crate::std::ptr::PtrLive`] by calling [`Perm::live`].
] PermPayload);