Skip to content

Commit 20f2aa4

Browse files
committed
Add identifiers to behaviour-considered-undefined.md
1 parent 0b18ee6 commit 20f2aa4

File tree

1 file changed

+80
-6
lines changed

1 file changed

+80
-6
lines changed

src/behavior-considered-undefined.md

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
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
@@ -26,13 +30,20 @@ Please read the [Rustonomicon] before writing unsafe code.
2630

2731
</div>
2832

33+
r[undefined.race]
2934
* Data races.
35+
36+
r[undefined.pointer-access]
3037
* Accessing (loading from or storing to) a place that is [dangling] or [based on
3138
a misaligned pointer].
39+
40+
r[undefined.place-projection]
3241
* Performing a place projection that violates the requirements of [in-bounds
3342
pointer arithmetic][offset]. A place projection is a [field
3443
expression][project-field], a [tuple index expression][project-tuple], or an
3544
[array/slice index expression][project-slice].
45+
46+
r[undefined.alias]
3647
* Breaking the [pointer aliasing rules]. `Box<T>`, `&mut T` and `&T` follow
3748
LLVM’s scoped [noalias] model, except if the `&T` contains an
3849
[`UnsafeCell<U>`]. References and boxes must not be [dangling] while they are
@@ -49,22 +60,36 @@ Please read the [Rustonomicon] before writing unsafe code.
4960
All this also applies when values of these
5061
types are passed in a (nested) field of a compound type, but not behind
5162
pointer indirections.
63+
64+
r[undefined.immutable]
5265
* Mutating immutable bytes. All bytes inside a [`const`] item are immutable.
5366
The bytes owned by an immutable binding or immutable `static` are immutable, unless those bytes are part of an [`UnsafeCell<U>`].
5467

5568
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.
5669

5770
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]
5873
* Invoking undefined behavior via compiler intrinsics.
74+
75+
r[undefined.target-feature]
5976
* Executing code compiled with platform features that the current platform
6077
does not support (see [`target_feature`]), *except* if the platform explicitly documents this to be safe.
78+
79+
r[undefined.call]
6180
* Calling a function with the wrong call ABI or unwinding from a function with the wrong unwind ABI.
81+
82+
r[undefined.invalid]
6283
* Producing an [invalid value][invalid-values]. "Producing" a
6384
value happens any time a value is assigned to or read from a place, passed to
6485
a function/primitive operation or returned from a function/primitive
6586
operation.
87+
88+
r[undefined.asm]
6689
* Incorrect use of inline assembly. For more details, refer to the [rules] to
6790
follow when writing code that uses inline assembly.
91+
92+
r[undefined.const-transmute-ptr2int]
6893
* **In [const context](const_eval.md#const-context)**: transmuting or otherwise
6994
reinterpreting a pointer (reference, raw pointer, or function pointer) into
7095
some allocated object as a non-pointer type (such as integers).
@@ -79,11 +104,15 @@ Please read the [Rustonomicon] before writing unsafe code.
79104
80105
### Pointed-to bytes
81106

107+
r[undefined.pointed-to]
82108
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`).
83109

84110
### Places based on misaligned pointers
85111
[based on a misaligned pointer]: #places-based-on-misaligned-pointers
86112

113+
r[undefined.misaligned]
114+
115+
r[undefined.misaligned.general]
87116
A place is said to be "based on a misaligned pointer" if the last `*` projection
88117
during place computation was performed on a pointer that was not aligned for its
89118
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
101130
the pointer that was dereferenced, *not* the type of the field that is being
102131
accessed.
103132

133+
r[undefined.misaligned.load-store]
104134
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
107142
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
110147
more aligned than the type that contains it, i.e., `repr(packed)`. This means
111148
that being based on an aligned pointer is always sufficient to ensure that the
112149
new reference is aligned, but it is not always necessary.
113150

114151
### Dangling pointers
115152
[dangling]: #dangling-pointers
116153

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

161+
r[undefined.dangling.zero-size]
121162
If the size is 0, then the pointer is trivially never "dangling"
122163
(even if it is a null pointer).
123164

165+
r[undefined.dangling.dynamic-size]
124166
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`)
127171
must never exceed `isize::MAX`, since it is impossible for a single allocation
128172
to be larger than `isize::MAX`.
129173

130174
### Invalid values
131175
[invalid-values]: #invalid-values
132176

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

136184
Whether a value is valid depends on the type:
185+
186+
r[undefined.validity.bool]
137187
* A [`bool`] value must be `false` (`0`) or `true` (`1`).
188+
189+
r[undefined.validity.fn-pointer]
138190
* A `fn` pointer value must be non-null.
191+
192+
r[undefined.validity.char]
139193
* 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]
140196
* A `!` value must never exist.
197+
198+
r[undefined.validity.scalar]
141199
* An integer (`i*`/`u*`), floating point value (`f*`), or raw pointer must be
142200
initialized, i.e., must not be obtained from [uninitialized memory][undef].
201+
202+
203+
r[undefined.validity.str]
143204
* A `str` value is treated like `[u8]`, i.e. it must be initialized.
205+
206+
r[undefined.validity.enum]
144207
* 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]
145210
* A `struct`, tuple, and array requires all fields/elements to be valid at their respective type.
211+
212+
r[undefined.validity.union]
146213
* For a `union`, the exact validity requirements are not decided yet.
147214
Obviously, all values that can be created entirely in safe code are valid.
148215
If the union has a zero-sized field, then every possible value is valid.
149216
Further details are [still being debated](https://github.com/rust-lang/unsafe-code-guidelines/issues/438).
217+
218+
r[undefined.validity.reference-box]
150219
* A reference or [`Box<T>`] must be aligned, it cannot be [dangling], and it must point to a valid value
151220
(in case of dynamically sized types, using the actual dynamic type of the
152221
pointee as determined by the metadata).
153222
Note that the last point (about pointing to a valid value) remains a subject of some debate.
223+
224+
r[undefined.validity.wide]
154225
* The metadata of a wide reference, [`Box<T>`], or raw pointer must match
155226
the type of the unsized tail:
156227
* `dyn Trait` metadata must be a pointer to a compiler-generated vtable for `Trait`.
157228
(For raw pointers, this requirement remains a subject of some debate.)
158229
* Slice (`[T]`) metadata must be a valid `usize`.
159230
Furthermore, for wide references and [`Box<T>`], slice metadata is invalid
160231
if it makes the total size of the pointed-to value bigger than `isize::MAX`.
232+
233+
r[undefined.validity.valid-range]
161234
* If a type has a custom range of a valid values, then a valid value must be in that range.
162235
In the standard library, this affects [`NonNull<T>`] and [`NonZero<T>`].
163236

164237
> **Note**: `rustc` achieves this with the unstable
165238
> `rustc_layout_scalar_valid_range_*` attributes.
166239
240+
r[undefined.validity.undef]
167241
**Note:** Uninitialized memory is also implicitly invalid for any type that has
168242
a restricted set of valid values. In other words, the only cases in which
169243
reading uninitialized memory is permitted are inside `union`s and in "padding"

0 commit comments

Comments
 (0)