|
13 | 13 | :scope: system |
14 | 14 | :tags: undefined-behavior, unsafe |
15 | 15 |
|
16 | | - Do not read uninitialized memory of any non-union type as a typed value :cite:`gui_xK9pLm2nQR4t:RUSTNOMICON-UNINIT`. |
| 16 | + Do not read uninitialized memory of any non-union type as a typed value :cite:`gui_uyp3mCj77FS8:RUSTNOMICON-UNINIT`. |
17 | 17 | This is sometimes referred to as *transmuting* or *read-at-type*. |
18 | 18 | Memory can remain uninitialized if it is not read as a type. |
19 | 19 |
|
20 | 20 | Reading from a union is covered by `Do not read from union fields that may contain uninitialized bytes |
21 | 21 | <https://coding-guidelines.arewesafetycriticalyet.org/coding-guidelines/types-and-traits/index.html#gui_UnionPartialInit>`_. |
22 | 22 |
|
23 | | - Calling `assume_init <https://doc.rust-lang.org/stable/std/mem/union.MaybeUninit.html#method.assume_init>`_ |
24 | | - :cite:`gui_aE3fNc6dWX1v:MAYBEUNINIT-DOC` or any of the following related functions is treated in the same manner as a typed read: |
| 23 | + Calling :std:`std::mem::MaybeUninit::assume_init` |
| 24 | + :cite:`gui_uyp3mCj77FS8:MAYBEUNINIT-DOC` or any of the following related functions is treated in the same manner as a typed read: |
25 | 25 |
|
26 | 26 | * ``assume_init_drop`` |
27 | 27 | * ``assume_init_mut`` |
28 | 28 | * ``assume_init_read`` |
29 | 29 | * ``assume_init_ref`` |
30 | 30 | * ``array_assume_init`` |
31 | 31 |
|
32 | | - Calling any of these function on memory that is not yet fully initialized is undefined behavior :cite:`gui_vB7sHj5wYZ8u:RUST-REF-BEHAVIOR`. |
33 | | - The memory must be properly initialized according to the requirements of the variable's type :cite:`gui_uZ0zDq8rST1b:UCG-VALIDITY`. |
| 32 | + Calling any of these function on memory that is not yet fully initialized is undefined behavior :cite:`gui_uyp3mCj77FS8:RUST-REF-BEHAVIOR`. |
| 33 | + The memory must be properly initialized according to the requirements of the variable's type :cite:`gui_uyp3mCj77FS8:UCG-VALIDITY`. |
34 | 34 | For example, a variable of reference type must be aligned, non-null, and point to valid memory. |
35 | 35 | Similarly, entirely uninitialized memory may have any content, while a ``bool`` must always be ``true`` or ``false``. |
36 | 36 | Consequently, reading an uninitialized ``bool`` is undefined behavior. |
|
40 | 40 | :status: draft |
41 | 41 |
|
42 | 42 | Rust's memory model requires that all bytes must be initialized before being read as a typed value |
43 | | - :cite:`gui_xK9pLm2nQR4t:RUSTNOMICON-UNINIT` :cite:`gui_kP8qTg4hUS2w:FERROCENE-SPEC`. |
44 | | - Reading uninitialized memory as a typed value is undefined behavior :cite:`gui_vB7sHj5wYZ8u:RUST-REF-BEHAVIOR`. |
45 | | - This guideline aligns with functional safety standards :cite:`gui_oT4tXk2lMN5y:ISO-26262` |
46 | | - :cite:`gui_qV6vZm4nOP7z:IEC-61508` and secure coding practices. |
| 43 | + :cite:`gui_uyp3mCj77FS8:RUSTNOMICON-UNINIT` :cite:`gui_uyp3mCj77FS8:FERROCENE-SPEC`. |
| 44 | + Reading uninitialized memory as a typed value is undefined behavior :cite:`gui_uyp3mCj77FS8:RUST-REF-BEHAVIOR`. |
| 45 | + This guideline aligns with functional safety standards :cite:`gui_uyp3mCj77FS8:ISO-26262` |
| 46 | + :cite:`gui_uyp3mCj77FS8:IEC-61508` and secure coding practices. |
47 | 47 |
|
48 | 48 | .. non_compliant_example:: |
49 | 49 | :id: non_compl_ex_Qb5GqYTP6db1 |
|
53 | 53 | which is undefined behavior. |
54 | 54 |
|
55 | 55 | .. rust-example:: |
| 56 | + :miri: expect_ub |
56 | 57 |
|
57 | 58 | use std::mem::MaybeUninit; |
58 | 59 |
|
|
72 | 73 | This is the canonical safe pattern for using ``MaybeUninit``. |
73 | 74 |
|
74 | 75 | .. rust-example:: |
| 76 | + :miri: |
75 | 77 |
|
76 | 78 | use std::mem::MaybeUninit; |
77 | 79 |
|
|
87 | 89 | :status: draft |
88 | 90 |
|
89 | 91 | This noncompliant example creates a pointer from uninitialized memory. |
90 | | - Not all bit patterns are valid pointers for all operations (e.g., provenance rules) :cite:`gui_uZ0zDq8rST1b:UCG-VALIDITY`. |
| 92 | + Not all bit patterns are valid pointers for all operations (e.g., provenance rules) :cite:`gui_uyp3mCj77FS8:UCG-VALIDITY`. |
91 | 93 | You cannot create a pointer from unspecified bytes. |
92 | | - Even a raw pointer type (e.g., ``*const T``) has validity rules `gui_vB7sHj5wYZ8u:RUST-REF-BEHAVIOR`. |
| 94 | + Even a raw pointer type (e.g., ``*const T``) has validity rules :cite:`gui_uyp3mCj77FS8:RUST-REF-BEHAVIOR`. |
93 | 95 |
|
94 | 96 | .. rust-example:: |
| 97 | + :miri: expect_ub |
95 | 98 |
|
96 | 99 | use std::mem::MaybeUninit; |
97 | 100 |
|
|
104 | 107 | :status: draft |
105 | 108 |
|
106 | 109 | This noncompliant example creates a reference from uninitialized memory. |
107 | | - Creating a reference from arbitrary or uninitialized bytes is undefined behavior :cite:`gui_vB7sHj5wYZ8u:RUST-REF-BEHAVIOR`. |
108 | | - References must be valid, aligned, dereferenceable, and non-null :cite:`gui_uZ0zDq8rST1b:UCG-VALIDITY`. |
| 110 | + Creating a reference from arbitrary or uninitialized bytes is undefined behavior :cite:`gui_uyp3mCj77FS8:RUST-REF-BEHAVIOR`. |
| 111 | + References must be valid, aligned, dereferenceable, and non-null :cite:`gui_uyp3mCj77FS8:UCG-VALIDITY`. |
109 | 112 | Uninitialized memory cannot satisfy these requirements. |
110 | 113 |
|
111 | 114 | .. rust-example:: |
| 115 | + :miri: expect_ub |
112 | 116 |
|
113 | 117 | use std::mem::MaybeUninit; |
114 | 118 |
|
|
124 | 128 | This noncompliant example creates a reference from uninitialized memory. |
125 | 129 |
|
126 | 130 | .. rust-example:: |
| 131 | + :miri: expect_ub |
127 | 132 |
|
128 | 133 | use std::mem::MaybeUninit; |
129 | 134 | use std::ptr; |
|
173 | 178 | Reading uninitialized bytes as a concrete type is always undefined behavior. |
174 | 179 |
|
175 | 180 | .. rust-example:: |
| 181 | + :miri: expect_ub |
176 | 182 |
|
177 | 183 | use std::mem::MaybeUninit; |
178 | 184 |
|
|
207 | 213 | * ``read_unaligned`` handles the alignment issue |
208 | 214 |
|
209 | 215 | .. rust-example:: |
| 216 | + :miri: |
210 | 217 |
|
211 | 218 | #[repr(C)] |
212 | 219 | #[derive(Debug)] |
|
234 | 241 | :widths: auto |
235 | 242 | :class: bibliography-table |
236 | 243 |
|
237 | | - * - :bibentry:`gui_wB2bFs0tUV3c:DO-178C` |
| 244 | + * - :bibentry:`gui_uyp3mCj77FS8:DO-178C` |
238 | 245 | - RTCA, Inc. "DO-178C: Software Considerations in Airborne Systems and Equipment Certification." https://store.accuristech.com/standards/rtca-do-178c?product_id=2200105. |
239 | 246 |
|
240 | | - * - :bibentry:`gui_xK9pLm2nQR4t:RUSTNOMICON-UNINIT` |
| 247 | + * - :bibentry:`gui_uyp3mCj77FS8:RUSTNOMICON-UNINIT` |
241 | 248 | - The Rust Project Developers. "The Rustonomicon - Uninitialized Memory." https://doc.rust-lang.org/nomicon/uninitialized.html. |
242 | 249 |
|
243 | | - * - :bibentry:`gui_vB7sHj5wYZ8u:RUST-REF-BEHAVIOR` |
| 250 | + * - :bibentry:`gui_uyp3mCj77FS8:RUST-REF-BEHAVIOR` |
244 | 251 | - The Rust Project Developers. "The Rust Reference - Behavior considered undefined." https://doc.rust-lang.org/reference/behavior-considered-undefined.html. |
245 | 252 |
|
246 | | - * - :bibentry:`gui_aE3fNc6dWX1v:MAYBEUNINIT-DOC` |
| 253 | + * - :bibentry:`gui_uyp3mCj77FS8:MAYBEUNINIT-DOC` |
247 | 254 | - The Rust Project Developers. "std::mem::MaybeUninit - Rust Standard Library Documentation." https://doc.rust-lang.org/stable/std/mem/union.MaybeUninit.html. |
248 | 255 |
|
249 | | - * - :bibentry:`gui_kP8qTg4hUS2w:FERROCENE-SPEC` |
| 256 | + * - :bibentry:`gui_uyp3mCj77FS8:FERROCENE-SPEC` |
250 | 257 | - Ferrocene GmbH. "Ferrocene Language Specification." https://spec.ferrocene.dev/. |
251 | 258 |
|
252 | | - * - :bibentry:`gui_mR1rVi9jKL3x:MISRA-RUST` |
| 259 | + * - :bibentry:`gui_uyp3mCj77FS8:MISRA-RUST` |
253 | 260 | - MISRA Consortium Limited. "MISRA Rust Guidelines (Draft)." https://misra.org.uk/. |
254 | 261 |
|
255 | | - * - :bibentry:`gui_oT4tXk2lMN5y:ISO-26262` |
| 262 | + * - :bibentry:`gui_uyp3mCj77FS8:ISO-26262` |
256 | 263 | - International Organization for Standardization. "ISO 26262 - Road vehicles - Functional safety." https://www.iso.org/standard/68383.html. |
257 | 264 |
|
258 | | - * - :bibentry:`gui_qV6vZm4nOP7z:IEC-61508` |
| 265 | + * - :bibentry:`gui_uyp3mCj77FS8:IEC-61508` |
259 | 266 | - International Electrotechnical Commission. "IEC 61508 - Functional Safety of Electrical/Electronic/Programmable Electronic Safety-related Systems." https://www.iec.ch/functional-safety. |
260 | 267 |
|
261 | | - * - :bibentry:`gui_sX8xBo6pQR9a:RUST-SAFETY-CRITICAL-WG` |
| 268 | + * - :bibentry:`gui_uyp3mCj77FS8:RUST-SAFETY-CRITICAL-WG` |
262 | 269 | - Rust Foundation. "Rust Safety-Critical Consortium." https://github.com/rust-lang/safety-critical-consortium. |
263 | 270 |
|
264 | | - * - :bibentry:`gui_uZ0zDq8rST1b:UCG-VALIDITY` |
265 | | - - The Rust Project Developers. "Unsafe Code Guidelines - Validity Invariants." https://rust-lang.github.io/unsafe-code-guidelines/glossary.html#validity-and-safety-invariant. |
| 271 | + * - :bibentry:`gui_uyp3mCj77FS8:UCG-VALIDITY` |
| 272 | + - Rust Unsafe Code Guidelines. "Validity and Safety Invariant." https://rust-lang.github.io/unsafe-code-guidelines/glossary.html#validity-and-safety-invariant. |
266 | 273 |
|
267 | | - |
268 | | - * - .. [CERT_RUST] |
269 | | - - | Carnegie Mellon University Software Engineering Institute |
270 | | - | "SEI CERT Rust Coding Standard" |
271 | | - | https://wiki.sei.cmu.edu/confluence/display/rust |
| 274 | + * - :bibentry:`gui_uyp3mCj77FS8:CERT-RUST` |
| 275 | + - Carnegie Mellon University Software Engineering Institute. "SEI CERT Rust Coding Standard." https://wiki.sei.cmu.edu/confluence/display/rust |
0 commit comments