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/coding-guidelines/values.rst
+79-10Lines changed: 79 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,43 +14,92 @@ Values
14
14
:fls: fls_6lg0oaaopc26
15
15
:decidability: undecidable
16
16
:scope: system
17
-
:tags: undefined-behavior
17
+
:tags: undefined-behavior, unsafe
18
18
19
-
A program shall not create a value of any type from uninitialized memory, except when accessing a field of a union type, where such reads are explicitly defined to be permitted even if the bytes of that field are uninitialized.
20
-
It is prohibited to interpret uninitialized memory as a value of any Rust type (primitive, aggregate, reference, pointer, struct, enum, array, tuple, etc.)
19
+
A program shall not create a value of any type from uninitialized memory,
20
+
except when accessing a field of a union type,
21
+
where such reads are explicitly defined to be permitted even if the bytes of that field are uninitialized.
22
+
It is prohibited to interpret uninitialized memory as a value of any Rust type such as a
23
+
primitive, aggregate, reference, pointer, struct, enum, array, or tuple.
21
24
22
25
**Exception:** You can access a field of a union even when the backing bytes of that field are uninitialized provided that:
23
26
24
27
- The resulting value has an unspecified but well-defined bit pattern.
25
-
- Interpreting that value must still comply with the requirements of the accessed type (e.g., no invalid enum discriminants, no invalid pointer values, etc.).
28
+
- Interpreting that value must still comply with the requirements of the accessed type
29
+
(e.g., no invalid enum discriminants, no invalid pointer values, etc.).
26
30
27
-
For example, reading an uninitialized u32 field of a union is allowed; reading an uninitialized bool field is disallowed because not all bit patterns are valid.
31
+
For example, reading an uninitialized u32 field of a union is allowed;
32
+
reading an uninitialized bool field is disallowed because not all bit patterns are valid.
28
33
29
34
.. rationale::
30
35
:id: rat_kjFRrhpS8Wu6
31
36
:status: draft
32
37
33
-
Rust’s memory model treats all types except unions as having an invariant that all bytes must be initialized before a value may be constructed. Reading uninitialized memory:
38
+
Rust’s memory model treats all types except unions as having an invariant that all bytes must be initialized before a value may be constructed.
39
+
Reading uninitialized memory:
34
40
35
41
- creates undefined behavior for most types,
36
42
- may violate niche or discriminant validity,
37
-
- may create invalid pointer values,
38
-
- or may produce values that violate type invariants.
43
+
- may create invalid pointer values, or
44
+
- may produce values that violate type invariants.
39
45
40
-
The sole exception is that unions work like C unions: any union field may be read, even if it was never written. The resulting bytes must, however, form a valid representation for the field’s type, which is not guaranteed if the union contains arbitrary data.
46
+
The sole exception is that unions work like C unions: any union field may be read, even if it was never written.
47
+
The resulting bytes must, however, form a valid representation for the field’s type,
48
+
which is not guaranteed if the union contains arbitrary data.
41
49
42
50
.. non_compliant_example::
43
51
:id: non_compl_ex_Qb5GqYTP6db1
44
52
:status: draft
45
53
46
-
The following code creates a value from uninitialized memory via assume_init:
54
+
This noncompliant example creates a value from uninitialized memory via ``assume_init``:
47
55
48
56
.. code-block:: rust
49
57
50
58
use std::mem::MaybeUninit;
51
59
52
60
let x: u32 = unsafe { MaybeUninit::uninit().assume_init() }; // UB
53
61
62
+
.. non_compliant_example::
63
+
:id: non_compl_ex_Qb5GqYTP6db2
64
+
:status: draft
65
+
66
+
Creating a reference from arbitrary or uninitialized bytes is always undefined behavior.
67
+
References must be valid, aligned, properly dereferenceable, and non-null.
68
+
Uninitialized memory cannot satisfy these invariants.
0 commit comments