Skip to content

Commit 63ac0a8

Browse files
BennoLossinjannau
authored andcommitted
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>
1 parent ddcf7a5 commit 63ac0a8

3 files changed

Lines changed: 14 additions & 0 deletions

File tree

rust/bindgen_parameters

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@
3434
# We use const helpers to aid bindgen, to avoid conflicts when constants are
3535
# recognized, block generation of the non-helper constants.
3636
--blocklist-item ARCH_SLAB_MINALIGN
37+
38+
# Structs should implement Zeroable when all of their fields do.
39+
--with-derive-custom-struct .*=MaybeZeroable
40+
--with-derive-custom-union .*=MaybeZeroable

rust/bindings/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,19 @@
2828
#[allow(clippy::undocumented_unsafe_blocks)]
2929
#[cfg_attr(CONFIG_RUSTC_HAS_UNNECESSARY_TRANSMUTES, allow(unnecessary_transmutes))]
3030
mod bindings_raw {
31+
use pin_init::{MaybeZeroable, Zeroable};
32+
3133
// Manual definition for blocklisted types.
3234
type __kernel_size_t = usize;
3335
type __kernel_ssize_t = isize;
3436
type __kernel_ptrdiff_t = isize;
3537

38+
// `bindgen` doesn't automatically do this, see
39+
// <https://github.com/rust-lang/rust-bindgen/issues/3196>
40+
//
41+
// SAFETY: `__BindgenBitfieldUnit<Storage>` is a newtype around `Storage`.
42+
unsafe impl<Storage> Zeroable for __BindgenBitfieldUnit<Storage> where Storage: Zeroable {}
43+
3644
// Use glob import here to expose all helpers.
3745
// Symbols defined within the module will take precedence to the glob import.
3846
pub use super::bindings_helper::*;

rust/uapi/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ type __kernel_size_t = usize;
3131
type __kernel_ssize_t = isize;
3232
type __kernel_ptrdiff_t = isize;
3333

34+
use pin_init::MaybeZeroable;
35+
3436
include!(concat!(env!("OBJTREE"), "/rust/uapi/uapi_generated.rs"));

0 commit comments

Comments
 (0)