Skip to content

Commit 369f949

Browse files
authored
Merge pull request #1562 from chorman0773/spec-add-identifiers-behaviour-considered-undefined
Add spec identifiers to behaviour-considered-undefined.md
2 parents cf88463 + a8b8c49 commit 369f949

File tree

1 file changed

+92
-12
lines changed

1 file changed

+92
-12
lines changed

src/behavior-considered-undefined.md

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,49 @@
11
## Behavior considered undefined
22

3+
r[undefined]
4+
5+
r[undefined.general]
36
Rust code is incorrect if it exhibits any of the behaviors in the following
47
list. This includes code within `unsafe` blocks and `unsafe` functions.
58
`unsafe` only means that avoiding undefined behavior is on the programmer; it
69
does not change anything about the fact that Rust programs must never cause
710
undefined behavior.
811

12+
r[undefined.soundness]
913
It is the programmer's responsibility when writing `unsafe` code to ensure that
1014
any safe code interacting with the `unsafe` code cannot trigger these
1115
behaviors. `unsafe` code that satisfies this property for any safe client is
1216
called *sound*; if `unsafe` code can be misused by safe code to exhibit
1317
undefined behavior, it is *unsound*.
1418

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+
<div class="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.
1930

31+
</div>
32+
33+
r[undefined.race]
2034
* Data races.
35+
36+
r[undefined.pointer-access]
2137
* Accessing (loading from or storing to) a place that is [dangling] or [based on
2238
a misaligned pointer].
39+
40+
r[undefined.place-projection]
2341
* Performing a place projection that violates the requirements of [in-bounds
2442
pointer arithmetic](pointer#method.offset). A place projection is a [field
2543
expression][project-field], a [tuple index expression][project-tuple], or an
2644
[array/slice index expression][project-slice].
45+
46+
r[undefined.alias]
2747
* Breaking the [pointer aliasing rules]. `Box<T>`, `&mut T` and `&T` follow
2848
LLVM’s scoped [noalias] model, except if the `&T` contains an
2949
[`UnsafeCell<U>`]. References and boxes must not be [dangling] while they are
@@ -40,23 +60,37 @@ undefined behavior, it is *unsound*.
4060
All this also applies when values of these
4161
types are passed in a (nested) field of a compound type, but not behind
4262
pointer indirections.
63+
64+
r[undefined.immutable]
4365
* Mutating immutable bytes.
4466
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`.
4567
The bytes owned by an immutable binding or immutable `static` are immutable, unless those bytes are part of an [`UnsafeCell<U>`].
4668

4769
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.
4870

4971
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]
5074
* Invoking undefined behavior via compiler intrinsics.
75+
76+
r[undefined.target-feature]
5177
* Executing code compiled with platform features that the current platform
5278
does not support (see [`target_feature`]), *except* if the platform explicitly documents this to be safe.
79+
80+
r[undefined.call]
5381
* Calling a function with the wrong call ABI or unwinding from a function with the wrong unwind ABI.
82+
83+
r[undefined.invalid]
5484
* Producing an [invalid value][invalid-values]. "Producing" a
5585
value happens any time a value is assigned to or read from a place, passed to
5686
a function/primitive operation or returned from a function/primitive
5787
operation.
88+
89+
r[undefined.asm]
5890
* Incorrect use of inline assembly. For more details, refer to the [rules] to
5991
follow when writing code that uses inline assembly.
92+
93+
r[undefined.const-transmute-ptr2int]
6094
* **In [const context](const_eval.md#const-context)**: transmuting or otherwise
6195
reinterpreting a pointer (reference, raw pointer, or function pointer) into
6296
some allocated object as a non-pointer type (such as integers).
@@ -71,11 +105,15 @@ undefined behavior, it is *unsound*.
71105
72106
### Pointed-to bytes
73107

108+
r[undefined.pointed-to]
74109
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`).
75110

76111
### Places based on misaligned pointers
77112
[based on a misaligned pointer]: #places-based-on-misaligned-pointers
78113

114+
r[undefined.misaligned]
115+
116+
r[undefined.misaligned.general]
79117
A place is said to be "based on a misaligned pointer" if the last `*` projection
80118
during place computation was performed on a pointer that was not aligned for its
81119
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
93131
the pointer that was dereferenced, *not* the type of the field that is being
94132
accessed.
95133

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
99143
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
102148
more aligned than the type that contains it, i.e., `repr(packed)`. This means
103149
that being based on an aligned pointer is always sufficient to ensure that the
104150
new reference is aligned, but it is not always necessary.
105151

106152
### Dangling pointers
107153
[dangling]: #dangling-pointers
108154

155+
r[undefined.dangling]
156+
157+
r[undefined.dangling.general]
109158
A reference/pointer is "dangling" if not all of the bytes it
110159
[points to] are part of the same live allocation (so in particular they all have to be
111160
part of *some* allocation).
112161

162+
r[undefined.dangling.zero-size]
113163
If the size is 0, then the pointer is trivially never "dangling"
114164
(even if it is a null pointer).
115165

166+
r[undefined.dangling.dynamic-size]
116167
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`)
119172
must never exceed `isize::MAX`, since it is impossible for a single allocation
120173
to be larger than `isize::MAX`.
121174

122175
### Invalid values
123176
[invalid-values]: #invalid-values
124177

178+
r[undefined.validity]
179+
180+
r[undefined.validity.general]
125181
The Rust compiler assumes that all values produced during program execution are
126182
"valid", and producing an invalid value is hence immediate UB.
127183

128184
Whether a value is valid depends on the type:
185+
186+
r[undefined.validity.bool]
129187
* A [`bool`] value must be `false` (`0`) or `true` (`1`).
188+
189+
r[undefined.validity.fn-pointer]
130190
* A `fn` pointer value must be non-null.
191+
192+
r[undefined.validity.char]
131193
* 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]
132196
* A `!` value must never exist.
197+
198+
r[undefined.validity.scalar]
133199
* An integer (`i*`/`u*`), floating point value (`f*`), or raw pointer must be
134200
initialized, i.e., must not be obtained from [uninitialized memory][undef].
201+
202+
r[undefined.validity.str]
135203
* A `str` value is treated like `[u8]`, i.e. it must be initialized.
204+
205+
r[undefined.validity.enum]
136206
* 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]
137209
* A `struct`, tuple, and array requires all fields/elements to be valid at their respective type.
210+
211+
r[undefined.validity.union]
138212
* For a `union`, the exact validity requirements are not decided yet.
139213
Obviously, all values that can be created entirely in safe code are valid.
140214
If the union has a zero-sized field, then every possible value is valid.
141215
Further details are [still being debated](https://github.com/rust-lang/unsafe-code-guidelines/issues/438).
216+
217+
r[undefined.validity.reference-box]
142218
* A reference or [`Box<T>`] must be aligned, it cannot be [dangling], and it must point to a valid value
143219
(in case of dynamically sized types, using the actual dynamic type of the
144220
pointee as determined by the metadata).
145221
Note that the last point (about pointing to a valid value) remains a subject of some debate.
222+
223+
r[undefined.validity.wide]
146224
* The metadata of a wide reference, [`Box<T>`], or raw pointer must match
147225
the type of the unsized tail:
148226
* `dyn Trait` metadata must be a pointer to a compiler-generated vtable for `Trait`.
149227
(For raw pointers, this requirement remains a subject of some debate.)
150228
* Slice (`[T]`) metadata must be a valid `usize`.
151229
Furthermore, for wide references and [`Box<T>`], slice metadata is invalid
152230
if it makes the total size of the pointed-to value bigger than `isize::MAX`.
231+
232+
r[undefined.validity.valid-range]
153233
* If a type has a custom range of a valid values, then a valid value must be in that range.
154234
In the standard library, this affects [`NonNull<T>`] and [`NonZero<T>`].
155235

156236
> **Note**: `rustc` achieves this with the unstable
157237
> `rustc_layout_scalar_valid_range_*` attributes.
158238
239+
r[undefined.validity.undef]
159240
**Note:** Uninitialized memory is also implicitly invalid for any type that has
160241
a restricted set of valid values. In other words, the only cases in which
161242
reading uninitialized memory is permitted are inside `union`s and in "padding"
162243
(the gaps between the fields of a type).
163244

164-
165245
[`bool`]: types/boolean.md
166246
[`const`]: items/constant-items.md
167247
[noalias]: http://llvm.org/docs/LangRef.html#noalias

0 commit comments

Comments
 (0)