Skip to content

Commit ed98af3

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

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
@@ -279,14 +279,9 @@ where
279279
/// ## By-value transmutation
280280
///
281281
/// If `Src: Sized` and `Dst: Sized`, then it must be sound to transmute an
282-
/// `SV`-valid `Src` into a `DV`-valid `Dst` by value via union transmute. In
283-
/// particular:
284-
/// - If `size_of::<Src>() >= size_of::<Dst>()`, then the first
285-
/// `size_of::<Dst>()` bytes of any `SV`-valid `Src` must be a `DV`-valid
286-
/// `Dst`.
287-
/// - If `size_of::<Src>() < size_of::<Dst>()`, then any `SV`-valid `Src`
288-
/// followed by `size_of::<Dst>() - size_of::<Src>()` uninitialized bytes must
289-
/// be a `DV`-valid `Dst`.
282+
/// `SV`-valid `Src` into a `DV`-valid `Dst` by value via size-preserving or
283+
/// size-shrinking transmute. In particular, the first `size_of::<Dst>()` bytes
284+
/// of any `SV`-valid `Src` must be a `DV`-valid `Dst`.
290285
///
291286
/// If either `Src: !Sized` or `Dst: !Sized`, then this condition does not need
292287
/// to hold.
@@ -355,12 +350,18 @@ unsafe impl<T: ?Sized> SizeCompat<T> for T {
355350
}
356351
}
357352

358-
// TODO: Update all `TransmuteFrom` safety proofs.
359-
360353
/// `Valid<Src: IntoBytes> → Initialized<Dst>`
361-
// SAFETY: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
362-
// initialized bit patterns, which is exactly the set allowed in the referent of
363-
// any `Initialized` `Ptr`.
354+
// SAFETY:
355+
// - By-value: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
356+
// initialized bit patterns, which is exactly the set allowed in the referent
357+
// of any `Initialized` `Ptr`. This holds for both size-preserving and
358+
// size-shrinking transmutes.
359+
// - By-reference:
360+
// - Shrinking: See above.
361+
// - Tearing: Let `src` be a `Valid` `Src` and `dst` be an `Initialized`
362+
// `Dst`. The trailing bytes of `dst` have bit validity `[u8; N]`. `src` has
363+
// bit validity `[u8; M]`. Thus, `dst' = src + trailing_bytes_of(dst)` has
364+
// bit validity `[u8; N + M]`, which is a valid `Initialized` value.
364365
unsafe impl<Src, Dst> TransmuteFrom<Src, Valid, Initialized> for Dst
365366
where
366367
Src: IntoBytes + ?Sized,
@@ -372,6 +373,8 @@ where
372373
// SAFETY: Since `Dst: FromBytes`, any initialized bit pattern may appear in the
373374
// referent of a `Ptr<Dst, (_, _, Valid)>`. This is exactly equal to the set of
374375
// bit patterns which may appear in the referent of any `Initialized` `Ptr`.
376+
//
377+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
375378
unsafe impl<Src, Dst> TransmuteFrom<Src, Initialized, Valid> for Dst
376379
where
377380
Src: ?Sized,
@@ -386,6 +389,8 @@ where
386389
/// `Initialized<Src> → Initialized<Dst>`
387390
// SAFETY: The set of allowed bit patterns in the referent of any `Initialized`
388391
// `Ptr` is the same regardless of referent type.
392+
//
393+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
389394
unsafe impl<Src, Dst> TransmuteFrom<Src, Initialized, Initialized> for Dst
390395
where
391396
Src: ?Sized,
@@ -400,6 +405,8 @@ where
400405
/// `V<Src> → Uninit<Dst>`
401406
// SAFETY: A `Dst` with validity `Uninit` permits any byte sequence, and
402407
// therefore can be transmuted from any value.
408+
//
409+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
403410
unsafe impl<Src, Dst, V> TransmuteFrom<Src, V, Uninit> for Dst
404411
where
405412
Src: ?Sized,
@@ -503,6 +510,8 @@ impl_transitive_transmute_from!(T: ?Sized => UnsafeCell<T> => T => Cell<T>);
503510
// explicitly guaranteed, but it's obvious from `MaybeUninit`'s documentation
504511
// that this is the intention:
505512
// https://doc.rust-lang.org/1.85.0/core/mem/union.MaybeUninit.html
513+
//
514+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
506515
unsafe impl<Src, Dst> TransmuteFrom<Src, Uninit, Valid> for MaybeUninit<Dst> {}
507516

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

0 commit comments

Comments
 (0)