Skip to content

Commit f9e4d83

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

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
@@ -288,14 +288,9 @@ where
288288
/// ## By-value transmutation
289289
///
290290
/// If `Src: Sized` and `Dst: Sized`, then it must be sound to transmute an
291-
/// `SV`-valid `Src` into a `DV`-valid `Dst` by value via union transmute. In
292-
/// particular:
293-
/// - If `size_of::<Src>() >= size_of::<Dst>()`, then the first
294-
/// `size_of::<Dst>()` bytes of any `SV`-valid `Src` must be a `DV`-valid
295-
/// `Dst`.
296-
/// - If `size_of::<Src>() < size_of::<Dst>()`, then any `SV`-valid `Src`
297-
/// followed by `size_of::<Dst>() - size_of::<Src>()` uninitialized bytes must
298-
/// be a `DV`-valid `Dst`.
291+
/// `SV`-valid `Src` into a `DV`-valid `Dst` by value via size-preserving or
292+
/// size-shrinking transmute. In particular, the first `size_of::<Dst>()` bytes
293+
/// of any `SV`-valid `Src` must be a `DV`-valid `Dst`.
299294
///
300295
/// If either `Src: !Sized` or `Dst: !Sized`, then this condition does not need
301296
/// to hold.
@@ -364,12 +359,18 @@ unsafe impl<T: ?Sized> SizeCompat<T> for T {
364359
}
365360
}
366361

367-
// TODO: Update all `TransmuteFrom` safety proofs.
368-
369362
/// `Valid<Src: IntoBytes> → Initialized<Dst>`
370-
// SAFETY: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
371-
// initialized bit patterns, which is exactly the set allowed in the referent of
372-
// any `Initialized` `Ptr`.
363+
// SAFETY:
364+
// - By-value: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
365+
// initialized bit patterns, which is exactly the set allowed in the referent
366+
// of any `Initialized` `Ptr`. This holds for both size-preserving and
367+
// size-shrinking transmutes.
368+
// - By-reference:
369+
// - Shrinking: See above.
370+
// - Tearing: Let `src` be a `Valid` `Src` and `dst` be an `Initialized`
371+
// `Dst`. The trailing bytes of `dst` have bit validity `[u8; N]`. `src` has
372+
// bit validity `[u8; M]`. Thus, `dst' = src + trailing_bytes_of(dst)` has
373+
// bit validity `[u8; N + M]`, which is a valid `Initialized` value.
373374
unsafe impl<Src, Dst> TransmuteFrom<Src, Valid, Initialized> for Dst
374375
where
375376
Src: IntoBytes + ?Sized,
@@ -381,6 +382,8 @@ where
381382
// SAFETY: Since `Dst: FromBytes`, any initialized bit pattern may appear in the
382383
// referent of a `Ptr<Dst, (_, _, Valid)>`. This is exactly equal to the set of
383384
// bit patterns which may appear in the referent of any `Initialized` `Ptr`.
385+
//
386+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
384387
unsafe impl<Src, Dst> TransmuteFrom<Src, Initialized, Valid> for Dst
385388
where
386389
Src: ?Sized,
@@ -395,6 +398,8 @@ where
395398
/// `Initialized<Src> → Initialized<Dst>`
396399
// SAFETY: The set of allowed bit patterns in the referent of any `Initialized`
397400
// `Ptr` is the same regardless of referent type.
401+
//
402+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
398403
unsafe impl<Src, Dst> TransmuteFrom<Src, Initialized, Initialized> for Dst
399404
where
400405
Src: ?Sized,
@@ -409,6 +414,8 @@ where
409414
/// `V<Src> → Uninit<Dst>`
410415
// SAFETY: A `Dst` with validity `Uninit` permits any byte sequence, and
411416
// therefore can be transmuted from any value.
417+
//
418+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
412419
unsafe impl<Src, Dst, V> TransmuteFrom<Src, V, Uninit> for Dst
413420
where
414421
Src: ?Sized,
@@ -512,6 +519,8 @@ impl_transitive_transmute_from!(T: ?Sized => UnsafeCell<T> => T => Cell<T>);
512519
// explicitly guaranteed, but it's obvious from `MaybeUninit`'s documentation
513520
// that this is the intention:
514521
// https://doc.rust-lang.org/1.85.0/core/mem/union.MaybeUninit.html
522+
//
523+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
515524
unsafe impl<Src, Dst> TransmuteFrom<Src, Uninit, Valid> for MaybeUninit<Dst> {}
516525

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

0 commit comments

Comments
 (0)