You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/behavior-considered-undefined.md
+92-12Lines changed: 92 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -1,29 +1,49 @@
1
1
## Behavior considered undefined
2
2
3
+
r[undefined]
4
+
5
+
r[undefined.general]
3
6
Rust code is incorrect if it exhibits any of the behaviors in the following
4
7
list. This includes code within `unsafe` blocks and `unsafe` functions.
5
8
`unsafe` only means that avoiding undefined behavior is on the programmer; it
6
9
does not change anything about the fact that Rust programs must never cause
7
10
undefined behavior.
8
11
12
+
r[undefined.soundness]
9
13
It is the programmer's responsibility when writing `unsafe` code to ensure that
10
14
any safe code interacting with the `unsafe` code cannot trigger these
11
15
behaviors. `unsafe` code that satisfies this property for any safe client is
12
16
called *sound*; if `unsafe` code can be misused by safe code to exhibit
13
17
undefined behavior, it is *unsound*.
14
18
15
-
> [!WARNING]
16
-
> The following list is not exhaustive; it may grow or shrink. There is no formal model of Rust's semantics for what is and is not allowed in unsafe code, so there may be more behavior considered unsafe. We also reserve the right to make some of the behavior in that list defined in the future. In other words, this list does not say that anything will *definitely* always be undefined in all future Rust version (but we might make such commitments for some list items in the future).
17
-
>
18
-
> Please read the [Rustonomicon] before writing unsafe code.
19
+
<divclass="warning">
20
+
21
+
***Warning:*** The following list is not exhaustive; it may grow or shrink.
22
+
There is no formal model of Rust's semantics for what is and is not allowed in
23
+
unsafe code, so there may be more behavior considered unsafe. We also reserve
24
+
the right to make some of the behavior in that list defined in the future. In
25
+
other words, this list does not say that anything will *definitely* always be
26
+
undefined in all future Rust version (but we might make such commitments for
27
+
some list items in the future).
28
+
29
+
Please read the [Rustonomicon] before writing unsafe code.
19
30
31
+
</div>
32
+
33
+
r[undefined.race]
20
34
* Data races.
35
+
36
+
r[undefined.pointer-access]
21
37
* Accessing (loading from or storing to) a place that is [dangling] or [based on
22
38
a misaligned pointer].
39
+
40
+
r[undefined.place-projection]
23
41
* Performing a place projection that violates the requirements of [in-bounds
24
42
pointer arithmetic](pointer#method.offset). A place projection is a [field
25
43
expression][project-field], a [tuple index expression][project-tuple], or an
26
44
[array/slice index expression][project-slice].
45
+
46
+
r[undefined.alias]
27
47
* Breaking the [pointer aliasing rules]. `Box<T>`, `&mut T` and `&T` follow
28
48
LLVM’s scoped [noalias] model, except if the `&T` contains an
29
49
[`UnsafeCell<U>`]. References and boxes must not be [dangling] while they are
@@ -40,23 +60,37 @@ undefined behavior, it is *unsound*.
40
60
All this also applies when values of these
41
61
types are passed in a (nested) field of a compound type, but not behind
42
62
pointer indirections.
63
+
64
+
r[undefined.immutable]
43
65
* Mutating immutable bytes.
44
66
All bytes reachable through a [const-promoted] expression are immutable, as well as bytes reachable through borrows in `static` and `const` initializers that have been [lifetime-extended] to `'static`.
45
67
The bytes owned by an immutable binding or immutable `static` are immutable, unless those bytes are part of an [`UnsafeCell<U>`].
46
68
47
69
Moreover, the bytes [pointed to] by a shared reference, including transitively through other references (both shared and mutable) and `Box`es, are immutable; transitivity includes those references stored in fields of compound types.
48
70
49
71
A mutation is any write of more than 0 bytes which overlaps with any of the relevant bytes (even if that write does not change the memory contents).
72
+
73
+
r[undefined.intrinsic]
50
74
* Invoking undefined behavior via compiler intrinsics.
75
+
76
+
r[undefined.target-feature]
51
77
* Executing code compiled with platform features that the current platform
52
78
does not support (see [`target_feature`]), *except* if the platform explicitly documents this to be safe.
79
+
80
+
r[undefined.call]
53
81
* Calling a function with the wrong call ABI or unwinding from a function with the wrong unwind ABI.
82
+
83
+
r[undefined.invalid]
54
84
* Producing an [invalid value][invalid-values]. "Producing" a
55
85
value happens any time a value is assigned to or read from a place, passed to
56
86
a function/primitive operation or returned from a function/primitive
57
87
operation.
88
+
89
+
r[undefined.asm]
58
90
* Incorrect use of inline assembly. For more details, refer to the [rules] to
59
91
follow when writing code that uses inline assembly.
92
+
93
+
r[undefined.const-transmute-ptr2int]
60
94
***In [const context](const_eval.md#const-context)**: transmuting or otherwise
61
95
reinterpreting a pointer (reference, raw pointer, or function pointer) into
62
96
some allocated object as a non-pointer type (such as integers).
@@ -71,11 +105,15 @@ undefined behavior, it is *unsound*.
71
105
72
106
### Pointed-to bytes
73
107
108
+
r[undefined.pointed-to]
74
109
The span of bytes a pointer or reference "points to" is determined by the pointer value and the size of the pointee type (using `size_of_val`).
75
110
76
111
### Places based on misaligned pointers
77
112
[based on a misaligned pointer]: #places-based-on-misaligned-pointers
78
113
114
+
r[undefined.misaligned]
115
+
116
+
r[undefined.misaligned.general]
79
117
A place is said to be "based on a misaligned pointer" if the last `*` projection
80
118
during place computation was performed on a pointer that was not aligned for its
81
119
type. (If there is no `*` projection in the place expression, then this is
@@ -93,75 +131,117 @@ alignment 1). In other words, the alignment requirement derives from the type of
93
131
the pointer that was dereferenced, *not* the type of the field that is being
94
132
accessed.
95
133
96
-
Note that a place based on a misaligned pointer only leads to undefined behavior
97
-
when it is loaded from or stored to. `&raw const`/`&raw mut` on such a place
98
-
is allowed. `&`/`&mut` on a place requires the alignment of the field type (or
134
+
r[undefined.misaligned.load-store]
135
+
Note that a place based on a misaligned pointer only leads to Undefined Behavior
136
+
when it is loaded from or stored to.
137
+
138
+
r[undefined.misaligned.raw]
139
+
`&raw const`/`&raw mut` on such a place is allowed.
140
+
141
+
r[undefined.misaligned.reference]
142
+
`&`/`&mut` on a place requires the alignment of the field type (or
99
143
else the program would be "producing an invalid value"), which generally is a
100
-
less restrictive requirement than being based on an aligned pointer. Taking a
101
-
reference will lead to a compiler error in cases where the field type might be
144
+
less restrictive requirement than being based on an aligned pointer.
145
+
146
+
r[undefined.misaligned.packed]
147
+
Taking a reference will lead to a compiler error in cases where the field type might be
102
148
more aligned than the type that contains it, i.e., `repr(packed)`. This means
103
149
that being based on an aligned pointer is always sufficient to ensure that the
104
150
new reference is aligned, but it is not always necessary.
105
151
106
152
### Dangling pointers
107
153
[dangling]: #dangling-pointers
108
154
155
+
r[undefined.dangling]
156
+
157
+
r[undefined.dangling.general]
109
158
A reference/pointer is "dangling" if not all of the bytes it
110
159
[points to] are part of the same live allocation (so in particular they all have to be
111
160
part of *some* allocation).
112
161
162
+
r[undefined.dangling.zero-size]
113
163
If the size is 0, then the pointer is trivially never "dangling"
114
164
(even if it is a null pointer).
115
165
166
+
r[undefined.dangling.dynamic-size]
116
167
Note that dynamically sized types (such as slices and strings) point to their
117
-
entire range, so it is important that the length metadata is never too large. In
118
-
particular, the dynamic size of a Rust value (as determined by `size_of_val`)
168
+
entire range, so it is important that the length metadata is never too large.
169
+
170
+
r[undefined.dangling.alloc-limit]
171
+
In particular, the dynamic size of a Rust value (as determined by `size_of_val`)
119
172
must never exceed `isize::MAX`, since it is impossible for a single allocation
120
173
to be larger than `isize::MAX`.
121
174
122
175
### Invalid values
123
176
[invalid-values]: #invalid-values
124
177
178
+
r[undefined.validity]
179
+
180
+
r[undefined.validity.general]
125
181
The Rust compiler assumes that all values produced during program execution are
126
182
"valid", and producing an invalid value is hence immediate UB.
127
183
128
184
Whether a value is valid depends on the type:
185
+
186
+
r[undefined.validity.bool]
129
187
* A [`bool`] value must be `false` (`0`) or `true` (`1`).
188
+
189
+
r[undefined.validity.fn-pointer]
130
190
* A `fn` pointer value must be non-null.
191
+
192
+
r[undefined.validity.char]
131
193
* A `char` value must not be a surrogate (i.e., must not be in the range `0xD800..=0xDFFF`) and must be equal to or less than `char::MAX`.
194
+
195
+
r[undefined.validity.never]
132
196
* A `!` value must never exist.
197
+
198
+
r[undefined.validity.scalar]
133
199
* An integer (`i*`/`u*`), floating point value (`f*`), or raw pointer must be
134
200
initialized, i.e., must not be obtained from [uninitialized memory][undef].
201
+
202
+
r[undefined.validity.str]
135
203
* A `str` value is treated like `[u8]`, i.e. it must be initialized.
204
+
205
+
r[undefined.validity.enum]
136
206
* An `enum` must have a valid discriminant, and all fields of the variant indicated by that discriminant must be valid at their respective type.
207
+
208
+
r[undefined.validity.struct]
137
209
* A `struct`, tuple, and array requires all fields/elements to be valid at their respective type.
210
+
211
+
r[undefined.validity.union]
138
212
* For a `union`, the exact validity requirements are not decided yet.
139
213
Obviously, all values that can be created entirely in safe code are valid.
140
214
If the union has a zero-sized field, then every possible value is valid.
141
215
Further details are [still being debated](https://github.com/rust-lang/unsafe-code-guidelines/issues/438).
216
+
217
+
r[undefined.validity.reference-box]
142
218
* A reference or [`Box<T>`] must be aligned, it cannot be [dangling], and it must point to a valid value
143
219
(in case of dynamically sized types, using the actual dynamic type of the
144
220
pointee as determined by the metadata).
145
221
Note that the last point (about pointing to a valid value) remains a subject of some debate.
222
+
223
+
r[undefined.validity.wide]
146
224
* The metadata of a wide reference, [`Box<T>`], or raw pointer must match
147
225
the type of the unsized tail:
148
226
*`dyn Trait` metadata must be a pointer to a compiler-generated vtable for `Trait`.
149
227
(For raw pointers, this requirement remains a subject of some debate.)
150
228
* Slice (`[T]`) metadata must be a valid `usize`.
151
229
Furthermore, for wide references and [`Box<T>`], slice metadata is invalid
152
230
if it makes the total size of the pointed-to value bigger than `isize::MAX`.
231
+
232
+
r[undefined.validity.valid-range]
153
233
* If a type has a custom range of a valid values, then a valid value must be in that range.
154
234
In the standard library, this affects [`NonNull<T>`] and [`NonZero<T>`].
155
235
156
236
> **Note**: `rustc` achieves this with the unstable
157
237
> `rustc_layout_scalar_valid_range_*` attributes.
158
238
239
+
r[undefined.validity.undef]
159
240
**Note:** Uninitialized memory is also implicitly invalid for any type that has
160
241
a restricted set of valid values. In other words, the only cases in which
161
242
reading uninitialized memory is permitted are inside `union`s and in "padding"
0 commit comments