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