Skip to content

Commit bafb400

Browse files
committed
[WIP] Update TransmuteFrom safety proofs
gherrit-pr-id: I32ffeea758b53073aa461ab41c217e5b8f0bc4e4
1 parent baf0a15 commit bafb400

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/pointer/transmute.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,9 @@ where
275275
/// ## By-value transmutation
276276
///
277277
/// If `Src: Sized` and `Self: Sized`, then it must be sound to transmute an
278-
/// `SV`-valid `Src` into a `DV`-valid `Dst` by value via union transmute. In
279-
/// particular:
280-
/// - If `size_of::<Src>() > size_of::<Dst>()`, then the first
281-
/// `size_of::<Dst>()` bytes of any `SV`-valid `Src` must be a `DV`-valid
282-
/// `Dst`.
283-
/// - If `size_of::<Src>() < size_of::<Dst>()`, then any `SV`-valid `Src`
284-
/// followed by `size_of::<Dst>() - size_of::<Src>()` uninitialized bytes must
285-
/// be a `DV`-valid `Dst`.
278+
/// `SV`-valid `Src` into a `DV`-valid `Dst` by value via size-preserving or
279+
/// size-shrinking transmute. In particular, the first `size_of::<Dst>()` bytes
280+
/// of any `SV`-valid `Src` must be a `DV`-valid `Dst`.
286281
///
287282
/// If either `Src: !Sized` or `Self: !Sized`, then this condition does not need
288283
/// to hold.
@@ -348,12 +343,18 @@ unsafe impl<T: ?Sized> SizeCompat<T> for T {
348343
}
349344
}
350345

351-
// TODO: Update all `TransmuteFrom` safety proofs.
352-
353346
/// `Valid<Src: IntoBytes> → Initialized<Dst>`
354-
// SAFETY: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
355-
// initialized bit patterns, which is exactly the set allowed in the referent of
356-
// any `Initialized` `Ptr`.
347+
// SAFETY:
348+
// - By-value: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
349+
// initialized bit patterns, which is exactly the set allowed in the referent
350+
// of any `Initialized` `Ptr`. This holds for both size-preserving and
351+
// size-shrinking transmutes.
352+
// - By-reference:
353+
// - Shrinking: See above.
354+
// - Tearing: Let `src` be a `Valid` `Src` and `dst` be an `Initialized`
355+
// `Dst`. The trailing bytes of `dst` have bit validity `[u8; N]`. `src` has
356+
// bit validity `[u8; M]`. Thus, `dst' = src + trailing_bytes_of(dst)` has
357+
// bit validity `[u8; N + M]`, which is a valid `Initialized` value.
357358
unsafe impl<Src, Dst> TransmuteFrom<Src, Valid, Initialized> for Dst
358359
where
359360
Src: IntoBytes + ?Sized,
@@ -365,6 +366,8 @@ where
365366
// SAFETY: Since `Dst: FromBytes`, any initialized bit pattern may appear in the
366367
// referent of a `Ptr<Dst, (_, _, Valid)>`. This is exactly equal to the set of
367368
// bit patterns which may appear in the referent of any `Initialized` `Ptr`.
369+
//
370+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
368371
unsafe impl<Src, Dst> TransmuteFrom<Src, Initialized, Valid> for Dst
369372
where
370373
Src: ?Sized,
@@ -379,6 +382,8 @@ where
379382
/// `Initialized<Src> → Initialized<Dst>`
380383
// SAFETY: The set of allowed bit patterns in the referent of any `Initialized`
381384
// `Ptr` is the same regardless of referent type.
385+
//
386+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
382387
unsafe impl<Src, Dst> TransmuteFrom<Src, Initialized, Initialized> for Dst
383388
where
384389
Src: ?Sized,
@@ -393,6 +398,8 @@ where
393398
/// `V<Src> → Uninit<Dst>`
394399
// SAFETY: A `Dst` with validity `Uninit` permits any byte sequence, and
395400
// therefore can be transmuted from any value.
401+
//
402+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
396403
unsafe impl<Src, Dst, V> TransmuteFrom<Src, V, Uninit> for Dst
397404
where
398405
Src: ?Sized,
@@ -496,6 +503,8 @@ impl_transitive_transmute_from!(T: ?Sized => UnsafeCell<T> => T => Cell<T>);
496503
// explicitly guaranteed, but it's obvious from `MaybeUninit`'s documentation
497504
// that this is the intention:
498505
// https://doc.rust-lang.org/1.85.0/core/mem/union.MaybeUninit.html
506+
//
507+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
499508
unsafe impl<Src, Dst> TransmuteFrom<Src, Uninit, Valid> for MaybeUninit<Dst> {}
500509

501510
// SAFETY: `MaybeUninit<T>` has the same size as `T` [1]. Thus, a pointer cast

0 commit comments

Comments
 (0)