Skip to content

Commit 2890b20

Browse files
committed
[WIP] Update TransmuteFrom safety proofs
gherrit-pr-id: I32ffeea758b53073aa461ab41c217e5b8f0bc4e4
1 parent 85b4437 commit 2890b20

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
@@ -272,14 +272,9 @@ where
272272
/// ## By-value transmutation
273273
///
274274
/// If `Src: Sized` and `Self: Sized`, then it must be sound to transmute an
275-
/// `SV`-valid `Src` into a `DV`-valid `Dst` by value via union transmute. In
276-
/// particular:
277-
/// - If `size_of::<Src>() > size_of::<Dst>()`, then the first
278-
/// `size_of::<Dst>()` bytes of any `SV`-valid `Src` must be a `DV`-valid
279-
/// `Dst`.
280-
/// - If `size_of::<Src>() < size_of::<Dst>()`, then any `SV`-valid `Src`
281-
/// followed by `size_of::<Dst>() - size_of::<Src>()` uninitialized bytes must
282-
/// be a `DV`-valid `Dst`.
275+
/// `SV`-valid `Src` into a `DV`-valid `Dst` by value via size-preserving or
276+
/// size-shrinking transmute. In particular, the first `size_of::<Dst>()` bytes
277+
/// of any `SV`-valid `Src` must be a `DV`-valid `Dst`.
283278
///
284279
/// If either `Src: !Sized` or `Self: !Sized`, then this condition does not need
285280
/// to hold.
@@ -345,12 +340,18 @@ unsafe impl<T: ?Sized> SizeCompat<T> for T {
345340
}
346341
}
347342

348-
// TODO: Update all `TransmuteFrom` safety proofs.
349-
350343
/// `Valid<Src: IntoBytes> → Initialized<Dst>`
351-
// SAFETY: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
352-
// initialized bit patterns, which is exactly the set allowed in the referent of
353-
// any `Initialized` `Ptr`.
344+
// SAFETY:
345+
// - By-value: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
346+
// initialized bit patterns, which is exactly the set allowed in the referent
347+
// of any `Initialized` `Ptr`. This holds for both size-preserving and
348+
// size-shrinking transmutes.
349+
// - By-reference:
350+
// - Shrinking: See above.
351+
// - Tearing: Let `src` be a `Valid` `Src` and `dst` be an `Initialized`
352+
// `Dst`. The trailing bytes of `dst` have bit validity `[u8; N]`. `src` has
353+
// bit validity `[u8; M]`. Thus, `dst' = src + trailing_bytes_of(dst)` has
354+
// bit validity `[u8; N + M]`, which is a valid `Initialized` value.
354355
unsafe impl<Src, Dst> TransmuteFrom<Src, Valid, Initialized> for Dst
355356
where
356357
Src: IntoBytes + ?Sized,
@@ -362,6 +363,8 @@ where
362363
// SAFETY: Since `Dst: FromBytes`, any initialized bit pattern may appear in the
363364
// referent of a `Ptr<Dst, (_, _, Valid)>`. This is exactly equal to the set of
364365
// bit patterns which may appear in the referent of any `Initialized` `Ptr`.
366+
//
367+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
365368
unsafe impl<Src, Dst> TransmuteFrom<Src, Initialized, Valid> for Dst
366369
where
367370
Src: ?Sized,
@@ -376,6 +379,8 @@ where
376379
/// `Initialized<Src> → Initialized<Dst>`
377380
// SAFETY: The set of allowed bit patterns in the referent of any `Initialized`
378381
// `Ptr` is the same regardless of referent type.
382+
//
383+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
379384
unsafe impl<Src, Dst> TransmuteFrom<Src, Initialized, Initialized> for Dst
380385
where
381386
Src: ?Sized,
@@ -390,6 +395,8 @@ where
390395
/// `V<Src> → Uninit<Dst>`
391396
// SAFETY: A `Dst` with validity `Uninit` permits any byte sequence, and
392397
// therefore can be transmuted from any value.
398+
//
399+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
393400
unsafe impl<Src, Dst, V> TransmuteFrom<Src, V, Uninit> for Dst
394401
where
395402
Src: ?Sized,
@@ -499,6 +506,8 @@ impl_transitive_transmute_from!(T: ?Sized => UnsafeCell<T> => T => Cell<T>);
499506
// explicitly guaranteed, but it's obvious from `MaybeUninit`'s documentation
500507
// that this is the intention:
501508
// https://doc.rust-lang.org/1.85.0/core/mem/union.MaybeUninit.html
509+
//
510+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
502511
unsafe impl<Src, Dst> TransmuteFrom<Src, Uninit, Valid> for MaybeUninit<Dst> {}
503512

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

0 commit comments

Comments
 (0)