Skip to content

Commit 4c4e831

Browse files
committed
note about uninit
1 parent 7920f3f commit 4c4e831

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/advanced_unsafety/invalid_values.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ There are a lot of other reasons that a pointer type may not be valid, but these
3737
#### "shallow" vs "deep" validity
3838

3939

40-
An open question in Rust's model is whether references and reference-like types have "shallow" validity (roughly, the rules above), or "deep" validity (where a reference is valid only when the pointed-to data is valid, and that applies transitively). This issue is tracked upstream as [UGC #77](https://github.com/rust-lang/unsafe-code-guidelines/issues/77). The current discussion seems to skew towards shallow validity as opposed to deep validity, but this code change.
40+
An open question in Rust's model is whether references and reference-like types have "shallow" validity (roughly, the rules above), or "deep" validity (where a reference is valid only when the pointed-to data is valid, and that applies transitively). This issue is tracked upstream as [UGC #77](https://github.com/rust-lang/unsafe-code-guidelines/issues/77). The current discussion seems to skew towards shallow validity as opposed to deep validity, but this may change.
4141

4242
For the purposes of _writing_ unsafe code, it is convenient to imagine the boundary as being such that `&`/`&mut` references should never point to invalid memory. However, when auditing existing unsafe code it may be okay to allow scenarios that assume only shallow validity is required, depending on your risk appetite.
4343

@@ -98,7 +98,9 @@ As a library user you may not assume anything about the representation of a libr
9898

9999
Invalid values have a chance to crop up when you're reinterpreting a chunk of memory as a value of a different type. This can happen when calling [`mem::transmute()`], [`mem::transmute_copy()`], or [`mem::zeroed()`], when casting a reference to a region of memory into one of a different type, or when accessing the wrong variant of a `union`. The value need not be on the stack to be considered invalid: if you gin up an `&bool` that points to a bit pattern that is not a valid `bool`, that can instantly be UB (in a "deep validity" world) even if you don't read from the reference.
100100

101-
They can also happen when receiving values over FFI where either the signature of the function is incorrect (e.g. saying an FFI function accepts `bool` when the other side thinks it accepts a `u8`), or where there are differences in notions of validity across languages.
101+
Note that since [uninitialized memory][uninit-chapter] is a type of invalid value, any way to produce uninitialized memory (including [`mem::uninitialized()`]) is also a way of producing invalid values.
102+
103+
Invalid values can also be created when receiving values over FFI where either the signature of the function is incorrect (e.g. saying an FFI function accepts `bool` when the other side thinks it accepts a `u8`), or where there are differences in notions of validity across languages.
102104

103105
A subtle case of this comes up occasionally in FFI code due to differences in expectations between how enums are used in Rust and C.
104106

@@ -137,6 +139,7 @@ This is not an exhaustive list: ultimately, having an invalid value is UB and it
137139
[unaligned]: ../core_unsafety/dangling_and_unaligned_pointers.md
138140
[uninit-chapter]: ../undef_memory.md
139141
[`mem::transmute()`]: https://doc.rust-lang.org/stable/std/mem/fn.transmute.html
142+
[`mem::uninitialized()`]: https://doc.rust-lang.org/stable/std/mem/fn.uninitialized.html
140143
[`mem::transmute_copy()`]: https://doc.rust-lang.org/stable/std/mem/fn.transmute_copy.html
141144
[`mem::zeroed()`]: https://doc.rust-lang.org/stable/std/mem/fn.zeroed.html
142145
[`NonNull<T>`]: https://doc.rust-lang.org/stable/std/ptr/struct.NonNull.html

0 commit comments

Comments
 (0)