Skip to content

Commit 6ce3ddb

Browse files
committed
fix(guidelines): annotate unsafe examples
Add miri directives and normalize citations/bibliography entries so the guideline builds cleanly.
1 parent cebdd28 commit 6ce3ddb

1 file changed

Lines changed: 33 additions & 29 deletions

File tree

src/coding-guidelines/values/gui_uyp3mCj77FS8.rst.inc

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@
1313
:scope: system
1414
:tags: undefined-behavior, unsafe
1515

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`.
1717
This is sometimes referred to as *transmuting* or *read-at-type*.
1818
Memory can remain uninitialized if it is not read as a type.
1919

2020
Reading from a union is covered by `Do not read from union fields that may contain uninitialized bytes
2121
<https://coding-guidelines.arewesafetycriticalyet.org/coding-guidelines/types-and-traits/index.html#gui_UnionPartialInit>`_.
2222

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:
2525

2626
* ``assume_init_drop``
2727
* ``assume_init_mut``
2828
* ``assume_init_read``
2929
* ``assume_init_ref``
3030
* ``array_assume_init``
3131

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`.
3434
For example, a variable of reference type must be aligned, non-null, and point to valid memory.
3535
Similarly, entirely uninitialized memory may have any content, while a ``bool`` must always be ``true`` or ``false``.
3636
Consequently, reading an uninitialized ``bool`` is undefined behavior.
@@ -40,10 +40,10 @@
4040
:status: draft
4141
4242
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.
4747

4848
.. non_compliant_example::
4949
:id: non_compl_ex_Qb5GqYTP6db1
@@ -53,6 +53,7 @@
5353
which is undefined behavior.
5454

5555
.. rust-example::
56+
:miri: expect_ub
5657

5758
use std::mem::MaybeUninit;
5859

@@ -72,6 +73,7 @@
7273
This is the canonical safe pattern for using ``MaybeUninit``.
7374

7475
.. rust-example::
76+
:miri:
7577

7678
use std::mem::MaybeUninit;
7779

@@ -87,11 +89,12 @@
8789
:status: draft
8890

8991
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`.
9193
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`.
9395

9496
.. rust-example::
97+
:miri: expect_ub
9598

9699
use std::mem::MaybeUninit;
97100

@@ -104,11 +107,12 @@
104107
:status: draft
105108

106109
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`.
109112
Uninitialized memory cannot satisfy these requirements.
110113

111114
.. rust-example::
115+
:miri: expect_ub
112116

113117
use std::mem::MaybeUninit;
114118

@@ -124,6 +128,7 @@
124128
This noncompliant example creates a reference from uninitialized memory.
125129

126130
.. rust-example::
131+
:miri: expect_ub
127132

128133
use std::mem::MaybeUninit;
129134
use std::ptr;
@@ -173,6 +178,7 @@
173178
Reading uninitialized bytes as a concrete type is always undefined behavior.
174179

175180
.. rust-example::
181+
:miri: expect_ub
176182

177183
use std::mem::MaybeUninit;
178184

@@ -207,6 +213,7 @@
207213
* ``read_unaligned`` handles the alignment issue
208214

209215
.. rust-example::
216+
:miri:
210217

211218
#[repr(C)]
212219
#[derive(Debug)]
@@ -234,38 +241,35 @@
234241
:widths: auto
235242
:class: bibliography-table
236243

237-
* - :bibentry:`gui_wB2bFs0tUV3c:DO-178C`
244+
* - :bibentry:`gui_uyp3mCj77FS8:DO-178C`
238245
- RTCA, Inc. "DO-178C: Software Considerations in Airborne Systems and Equipment Certification." https://store.accuristech.com/standards/rtca-do-178c?product_id=2200105.
239246

240-
* - :bibentry:`gui_xK9pLm2nQR4t:RUSTNOMICON-UNINIT`
247+
* - :bibentry:`gui_uyp3mCj77FS8:RUSTNOMICON-UNINIT`
241248
- The Rust Project Developers. "The Rustonomicon - Uninitialized Memory." https://doc.rust-lang.org/nomicon/uninitialized.html.
242249

243-
* - :bibentry:`gui_vB7sHj5wYZ8u:RUST-REF-BEHAVIOR`
250+
* - :bibentry:`gui_uyp3mCj77FS8:RUST-REF-BEHAVIOR`
244251
- The Rust Project Developers. "The Rust Reference - Behavior considered undefined." https://doc.rust-lang.org/reference/behavior-considered-undefined.html.
245252

246-
* - :bibentry:`gui_aE3fNc6dWX1v:MAYBEUNINIT-DOC`
253+
* - :bibentry:`gui_uyp3mCj77FS8:MAYBEUNINIT-DOC`
247254
- The Rust Project Developers. "std::mem::MaybeUninit - Rust Standard Library Documentation." https://doc.rust-lang.org/stable/std/mem/union.MaybeUninit.html.
248255

249-
* - :bibentry:`gui_kP8qTg4hUS2w:FERROCENE-SPEC`
256+
* - :bibentry:`gui_uyp3mCj77FS8:FERROCENE-SPEC`
250257
- Ferrocene GmbH. "Ferrocene Language Specification." https://spec.ferrocene.dev/.
251258

252-
* - :bibentry:`gui_mR1rVi9jKL3x:MISRA-RUST`
259+
* - :bibentry:`gui_uyp3mCj77FS8:MISRA-RUST`
253260
- MISRA Consortium Limited. "MISRA Rust Guidelines (Draft)." https://misra.org.uk/.
254261

255-
* - :bibentry:`gui_oT4tXk2lMN5y:ISO-26262`
262+
* - :bibentry:`gui_uyp3mCj77FS8:ISO-26262`
256263
- International Organization for Standardization. "ISO 26262 - Road vehicles - Functional safety." https://www.iso.org/standard/68383.html.
257264

258-
* - :bibentry:`gui_qV6vZm4nOP7z:IEC-61508`
265+
* - :bibentry:`gui_uyp3mCj77FS8:IEC-61508`
259266
- International Electrotechnical Commission. "IEC 61508 - Functional Safety of Electrical/Electronic/Programmable Electronic Safety-related Systems." https://www.iec.ch/functional-safety.
260267

261-
* - :bibentry:`gui_sX8xBo6pQR9a:RUST-SAFETY-CRITICAL-WG`
268+
* - :bibentry:`gui_uyp3mCj77FS8:RUST-SAFETY-CRITICAL-WG`
262269
- Rust Foundation. "Rust Safety-Critical Consortium." https://github.com/rust-lang/safety-critical-consortium.
263270

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.
266273

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

Comments
 (0)