Skip to content

Commit ff983b4

Browse files
authored
Guarantee soundness of pointer-to-int transmutes
Resolves rust-lang/unsafe-code-guidelines#286
1 parent 6288200 commit ff983b4

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/types/pointer.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,12 @@ r[type.pointer.validity]
7979
## Bit validity
8080

8181
r[type.pointer.validity.pointer-fragment]
82-
Despite pointers and references being similar to `usize`s in the machine code emitted on most platforms,
83-
the semantics of transmuting a reference or pointer type to a non-pointer type is currently undecided.
84-
Thus, it may not be valid to transmute a pointer or reference type, `P`, to a `[u8; size_of::<P>()]`.
82+
A pointer or reference type, `P`, is guaranteed to have all of its bytes initialized. Thus, it is always
83+
sound to transmute `p0: P` to `bytes: [u8; size_of::<P>()]`. However, this operation may not preserve
84+
provenance, and so transmuting `bytes` back to `p1: P` may result in a pointer or reference without
85+
valid provenance. If `P` is a raw pointer type, then it may be the case that dereferencing `p1` is undefined
86+
behavior. If `P` is a reference type, then it may be the case that the act of transmuting to `p1` is
87+
undefined behavior even if `p1` is never used.
8588

8689
r[type.pointer.validity.raw]
8790
For thin raw pointers (i.e., for `P = *const T` or `P = *mut T` for `T: Sized`),

0 commit comments

Comments
 (0)