Skip to content

Commit a7bb504

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

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
@@ -296,14 +296,9 @@ where
296296
/// ## By-value transmutation
297297
///
298298
/// If `Src: Sized` and `Dst: Sized`, then it must be sound to transmute an
299-
/// `SV`-valid `Src` into a `DV`-valid `Dst` by value via union transmute. In
300-
/// particular:
301-
/// - If `size_of::<Src>() >= size_of::<Dst>()`, then the first
302-
/// `size_of::<Dst>()` bytes of any `SV`-valid `Src` must be a `DV`-valid
303-
/// `Dst`.
304-
/// - If `size_of::<Src>() < size_of::<Dst>()`, then any `SV`-valid `Src`
305-
/// followed by `size_of::<Dst>() - size_of::<Src>()` uninitialized bytes must
306-
/// be a `DV`-valid `Dst`.
299+
/// `SV`-valid `Src` into a `DV`-valid `Dst` by value via size-preserving or
300+
/// size-shrinking transmute. In particular, the first `size_of::<Dst>()` bytes
301+
/// of any `SV`-valid `Src` must be a `DV`-valid `Dst`.
307302
///
308303
/// If either `Src: !Sized` or `Dst: !Sized`, then this condition does not need
309304
/// to hold.
@@ -372,12 +367,18 @@ unsafe impl<T: ?Sized> SizeCompat<T> for T {
372367
}
373368
}
374369

375-
// TODO: Update all `TransmuteFrom` safety proofs.
376-
377370
/// `Valid<Src: IntoBytes> → Initialized<Dst>`
378-
// SAFETY: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
379-
// initialized bit patterns, which is exactly the set allowed in the referent of
380-
// any `Initialized` `Ptr`.
371+
// SAFETY:
372+
// - By-value: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
373+
// initialized bit patterns, which is exactly the set allowed in the referent
374+
// of any `Initialized` `Ptr`. This holds for both size-preserving and
375+
// size-shrinking transmutes.
376+
// - By-reference:
377+
// - Shrinking: See above.
378+
// - Tearing: Let `src` be a `Valid` `Src` and `dst` be an `Initialized`
379+
// `Dst`. The trailing bytes of `dst` have bit validity `[u8; N]`. `src` has
380+
// bit validity `[u8; M]`. Thus, `dst' = src + trailing_bytes_of(dst)` has
381+
// bit validity `[u8; N + M]`, which is a valid `Initialized` value.
381382
unsafe impl<Src, Dst> TransmuteFrom<Src, Valid, Initialized> for Dst
382383
where
383384
Src: IntoBytes + ?Sized,
@@ -389,6 +390,8 @@ where
389390
// SAFETY: Since `Dst: FromBytes`, any initialized bit pattern may appear in the
390391
// referent of a `Ptr<Dst, (_, _, Valid)>`. This is exactly equal to the set of
391392
// bit patterns which may appear in the referent of any `Initialized` `Ptr`.
393+
//
394+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
392395
unsafe impl<Src, Dst> TransmuteFrom<Src, Initialized, Valid> for Dst
393396
where
394397
Src: ?Sized,
@@ -403,6 +406,8 @@ where
403406
/// `Initialized<Src> → Initialized<Dst>`
404407
// SAFETY: The set of allowed bit patterns in the referent of any `Initialized`
405408
// `Ptr` is the same regardless of referent type.
409+
//
410+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
406411
unsafe impl<Src, Dst> TransmuteFrom<Src, Initialized, Initialized> for Dst
407412
where
408413
Src: ?Sized,
@@ -417,6 +422,8 @@ where
417422
/// `V<Src> → Uninit<Dst>`
418423
// SAFETY: A `Dst` with validity `Uninit` permits any byte sequence, and
419424
// therefore can be transmuted from any value.
425+
//
426+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
420427
unsafe impl<Src, Dst, V> TransmuteFrom<Src, V, Uninit> for Dst
421428
where
422429
Src: ?Sized,
@@ -520,6 +527,8 @@ impl_transitive_transmute_from!(T: ?Sized => UnsafeCell<T> => T => Cell<T>);
520527
// explicitly guaranteed, but it's obvious from `MaybeUninit`'s documentation
521528
// that this is the intention:
522529
// https://doc.rust-lang.org/1.85.0/core/mem/union.MaybeUninit.html
530+
//
531+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
523532
unsafe impl<Src, Dst> TransmuteFrom<Src, Uninit, Valid> for MaybeUninit<Dst> {}
524533

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

0 commit comments

Comments
 (0)