1 // SPDX-License-Identifier: GPL-2.0
3 //! Traits for transmuting types.
5 /// Types for which any bit pattern is valid.
7 /// Not all types are valid for all values. For example, a `bool` must be either zero or one, so
8 /// reading arbitrary bytes into something that contains a `bool` is not okay.
10 /// It's okay for the type to have padding, as initializing those bytes has no effect.
14 /// All bit-patterns must be valid for this type. This type must not have interior mutability.
15 pub unsafe trait FromBytes {}
17 macro_rules! impl_frombytes {
18 ($($({$($generics:tt)*})? $t:ty, )*) => {
19 // SAFETY: Safety comments written in the macro invocation.
20 $(unsafe impl$($($generics)*)? FromBytes for $t {})*
25 // SAFETY: All bit patterns are acceptable values of the types below.
26 u8, u16, u32, u64, usize,
27 i8, i16, i32, i64, isize,
29 // SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit
30 // patterns are also acceptable for arrays of that type.
32 {<T: FromBytes, const N: usize>} [T; N],
35 /// Types that can be viewed as an immutable slice of initialized bytes.
37 /// If a struct implements this trait, then it is okay to copy it byte-for-byte to userspace. This
38 /// means that it should not have any padding, as padding bytes are uninitialized. Reading
39 /// uninitialized memory is not just undefined behavior, it may even lead to leaking sensitive
40 /// information on the stack to userspace.
42 /// The struct should also not hold kernel pointers, as kernel pointer addresses are also considered
43 /// sensitive. However, leaking kernel pointers is not considered undefined behavior by Rust, so
44 /// this is a correctness requirement, but not a safety requirement.
48 /// Values of this type may not contain any uninitialized bytes. This type must not have interior
50 pub unsafe trait AsBytes {}
52 macro_rules! impl_asbytes {
53 ($($({$($generics:tt)*})? $t:ty, )*) => {
54 // SAFETY: Safety comments written in the macro invocation.
55 $(unsafe impl$($($generics)*)? AsBytes for $t {})*
60 // SAFETY: Instances of the following types have no uninitialized portions.
61 u8, u16, u32, u64, usize,
62 i8, i16, i32, i64, isize,
67 // SAFETY: If individual values in an array have no uninitialized portions, then the array
68 // itself does not have any uninitialized portions either.
70 {<T: AsBytes, const N: usize>} [T; N],