Skip to content

Commit 0e678af

Browse files
committed
[WIP] Update TransmuteFrom safety proofs
gherrit-pr-id: I32ffeea758b53073aa461ab41c217e5b8f0bc4e4
1 parent 674049b commit 0e678af

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
@@ -277,14 +277,9 @@ where
277277
/// ## By-value transmutation
278278
///
279279
/// If `Src: Sized` and `Dst: Sized`, then it must be sound to transmute an
280-
/// `SV`-valid `Src` into a `DV`-valid `Dst` by value via union transmute. In
281-
/// particular:
282-
/// - If `size_of::<Src>() > size_of::<Dst>()`, then the first
283-
/// `size_of::<Dst>()` bytes of any `SV`-valid `Src` must be a `DV`-valid
284-
/// `Dst`.
285-
/// - If `size_of::<Src>() < size_of::<Dst>()`, then any `SV`-valid `Src`
286-
/// followed by `size_of::<Dst>() - size_of::<Src>()` uninitialized bytes must
287-
/// be a `DV`-valid `Dst`.
280+
/// `SV`-valid `Src` into a `DV`-valid `Dst` by value via size-preserving or
281+
/// size-shrinking transmute. In particular, the first `size_of::<Dst>()` bytes
282+
/// of any `SV`-valid `Src` must be a `DV`-valid `Dst`.
288283
///
289284
/// If either `Src: !Sized` or `Dst: !Sized`, then this condition does not need
290285
/// to hold.
@@ -350,12 +345,18 @@ unsafe impl<T: ?Sized> SizeCompat<T> for T {
350345
}
351346
}
352347

353-
// TODO: Update all `TransmuteFrom` safety proofs.
354-
355348
/// `Valid<Src: IntoBytes> → Initialized<Dst>`
356-
// SAFETY: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
357-
// initialized bit patterns, which is exactly the set allowed in the referent of
358-
// any `Initialized` `Ptr`.
349+
// SAFETY:
350+
// - By-value: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
351+
// initialized bit patterns, which is exactly the set allowed in the referent
352+
// of any `Initialized` `Ptr`. This holds for both size-preserving and
353+
// size-shrinking transmutes.
354+
// - By-reference:
355+
// - Shrinking: See above.
356+
// - Tearing: Let `src` be a `Valid` `Src` and `dst` be an `Initialized`
357+
// `Dst`. The trailing bytes of `dst` have bit validity `[u8; N]`. `src` has
358+
// bit validity `[u8; M]`. Thus, `dst' = src + trailing_bytes_of(dst)` has
359+
// bit validity `[u8; N + M]`, which is a valid `Initialized` value.
359360
unsafe impl<Src, Dst> TransmuteFrom<Src, Valid, Initialized> for Dst
360361
where
361362
Src: IntoBytes + ?Sized,
@@ -367,6 +368,8 @@ where
367368
// SAFETY: Since `Dst: FromBytes`, any initialized bit pattern may appear in the
368369
// referent of a `Ptr<Dst, (_, _, Valid)>`. This is exactly equal to the set of
369370
// bit patterns which may appear in the referent of any `Initialized` `Ptr`.
371+
//
372+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
370373
unsafe impl<Src, Dst> TransmuteFrom<Src, Initialized, Valid> for Dst
371374
where
372375
Src: ?Sized,
@@ -381,6 +384,8 @@ where
381384
/// `Initialized<Src> → Initialized<Dst>`
382385
// SAFETY: The set of allowed bit patterns in the referent of any `Initialized`
383386
// `Ptr` is the same regardless of referent type.
387+
//
388+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
384389
unsafe impl<Src, Dst> TransmuteFrom<Src, Initialized, Initialized> for Dst
385390
where
386391
Src: ?Sized,
@@ -395,6 +400,8 @@ where
395400
/// `V<Src> → Uninit<Dst>`
396401
// SAFETY: A `Dst` with validity `Uninit` permits any byte sequence, and
397402
// therefore can be transmuted from any value.
403+
//
404+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
398405
unsafe impl<Src, Dst, V> TransmuteFrom<Src, V, Uninit> for Dst
399406
where
400407
Src: ?Sized,
@@ -498,6 +505,8 @@ impl_transitive_transmute_from!(T: ?Sized => UnsafeCell<T> => T => Cell<T>);
498505
// explicitly guaranteed, but it's obvious from `MaybeUninit`'s documentation
499506
// that this is the intention:
500507
// https://doc.rust-lang.org/1.85.0/core/mem/union.MaybeUninit.html
508+
//
509+
// TODO: Prove `TransmuteFrom` reference transmutation conditions.
501510
unsafe impl<Src, Dst> TransmuteFrom<Src, Uninit, Valid> for MaybeUninit<Dst> {}
502511

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

0 commit comments

Comments
 (0)