Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: derive `Zeroable` for all structs & unions generated by bindgen where possible

Using the `--with-derive-custom-{struct,union}` option of bindgen, add
`#[derive(MaybeZeroable)]` to every struct & union. This makes those
types implement `Zeroable` if all their fields implement it.

Sadly bindgen doesn't add custom derives to the `__BindgenBitfieldUnit`
struct. So manually implement `Zeroable` for that.

Signed-off-by: Benno Lossin <lossin@kernel.org>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
[ Formatted comment. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

authored by

Benno Lossin and committed by
Miguel Ojeda
4846300b 3c847e17

+14
+4
rust/bindgen_parameters
··· 35 35 # recognized, block generation of the non-helper constants. 36 36 --blocklist-item ARCH_SLAB_MINALIGN 37 37 --blocklist-item ARCH_KMALLOC_MINALIGN 38 + 39 + # Structs should implement `Zeroable` when all of their fields do. 40 + --with-derive-custom-struct .*=MaybeZeroable 41 + --with-derive-custom-union .*=MaybeZeroable
+8
rust/bindings/lib.rs
··· 31 31 #[allow(clippy::undocumented_unsafe_blocks)] 32 32 #[cfg_attr(CONFIG_RUSTC_HAS_UNNECESSARY_TRANSMUTES, allow(unnecessary_transmutes))] 33 33 mod bindings_raw { 34 + use pin_init::{MaybeZeroable, Zeroable}; 35 + 34 36 // Manual definition for blocklisted types. 35 37 type __kernel_size_t = usize; 36 38 type __kernel_ssize_t = isize; 37 39 type __kernel_ptrdiff_t = isize; 40 + 41 + // `bindgen` doesn't automatically do this, see 42 + // <https://github.com/rust-lang/rust-bindgen/issues/3196> 43 + // 44 + // SAFETY: `__BindgenBitfieldUnit<Storage>` is a newtype around `Storage`. 45 + unsafe impl<Storage> Zeroable for __BindgenBitfieldUnit<Storage> where Storage: Zeroable {} 38 46 39 47 // Use glob import here to expose all helpers. 40 48 // Symbols defined within the module will take precedence to the glob import.
+2
rust/uapi/lib.rs
··· 34 34 type __kernel_ssize_t = isize; 35 35 type __kernel_ptrdiff_t = isize; 36 36 37 + use pin_init::MaybeZeroable; 38 + 37 39 include!(concat!(env!("OBJTREE"), "/rust/uapi/uapi_generated.rs"));