Skip to content

Commit 49ceae6

Browse files
pthariensflameojeda
authored andcommitted
rust: init: remove impl Zeroable for Infallible
In Rust, producing an invalid value of any type is immediate undefined behavior (UB); this includes via zeroing memory. Therefore, since an uninhabited type has no valid values, producing any values at all for it is UB. The Rust standard library type `core::convert::Infallible` is uninhabited, by virtue of having been declared as an enum with no cases, which always produces uninhabited types in Rust. The current kernel code allows this UB to be triggered, for example by code like `Box::<core::convert::Infallible>::init(kernel::init::zeroed())`. Thus, remove the implementation of `Zeroable` for `Infallible`, thereby avoiding the unsoundness (potential for future UB). Cc: [email protected] Fixes: 38cde0b ("rust: init: add `Zeroable` trait and `init::zeroed` function") Closes: Rust-for-Linux/pin-init#13 Signed-off-by: Laine Taffin Altman <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: Boqun Feng <[email protected]> Reviewed-by: Benno Lossin <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Reformatted the comment slightly. ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 01848ee commit 49ceae6

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

rust/kernel/init.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1292,8 +1292,15 @@ impl_zeroable! {
12921292
i8, i16, i32, i64, i128, isize,
12931293
f32, f64,
12941294

1295-
// SAFETY: These are ZSTs, there is nothing to zero.
1296-
{<T: ?Sized>} PhantomData<T>, core::marker::PhantomPinned, Infallible, (),
1295+
// Note: do not add uninhabited types (such as `!` or `core::convert::Infallible`) to this list;
1296+
// creating an instance of an uninhabited type is immediate undefined behavior. For more on
1297+
// uninhabited/empty types, consult The Rustonomicon:
1298+
// <https://doc.rust-lang.org/stable/nomicon/exotic-sizes.html#empty-types>. The Rust Reference
1299+
// also has information on undefined behavior:
1300+
// <https://doc.rust-lang.org/stable/reference/behavior-considered-undefined.html>.
1301+
//
1302+
// SAFETY: These are inhabited ZSTs; there is nothing to zero and a valid value exists.
1303+
{<T: ?Sized>} PhantomData<T>, core::marker::PhantomPinned, (),
12971304

12981305
// SAFETY: Type is allowed to take any value, including all zeros.
12991306
{<T>} MaybeUninit<T>,

0 commit comments

Comments
 (0)