lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <DFK1KT28KH9S.QV7VCG9O9VGV@garyguo.net>
Date: Fri, 09 Jan 2026 12:02:57 +0000
From: "Gary Guo" <gary@...yguo.net>
To: "Benno Lossin" <lossin@...nel.org>, "Gary Guo" <gary@...yguo.net>,
 "Miguel Ojeda" <ojeda@...nel.org>, "Boqun Feng" <boqun.feng@...il.com>,
 Björn Roy Baron <bjorn3_gh@...tonmail.com>, "Andreas
 Hindborg" <a.hindborg@...nel.org>, "Alice Ryhl" <aliceryhl@...gle.com>,
 "Trevor Gross" <tmgross@...ch.edu>, "Danilo Krummrich" <dakr@...nel.org>,
 "Fiona Behrens" <me@...enk.dev>, "Christian Schrefl"
 <chrisi.schrefl@...il.com>, "Alban Kurti" <kurti@...icto.ai>
Cc: <rust-for-linux@...r.kernel.org>, <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH 04/12] rust: pin-init: rewrite `derive(Zeroable)` and
 `derive(MaybeZeroable)` using `syn`

On Thu Jan 8, 2026 at 1:50 PM GMT, Benno Lossin wrote:
> Rewrite the two derive macros for `Zeroable` using `syn`. One positive
> side effect of this change is that tuple structs are now supported by
> them. Additionally, syntax errors and the error emitted when trying to
> use one of the derive macros on an `enum` are improved. Otherwise no
> functional changes intended.
>
> For example:
>
>     #[derive(Zeroable)]
>     enum Num {
>         A(u32),
>         B(i32),
>     }
>
> Produced this error before this commit:
>
>     error: no rules expected keyword `enum`
>      --> tests/ui/compile-fail/zeroable/enum.rs:5:1
>       |
>     5 | enum Num {
>       | ^^^^ no rules expected this token in macro call
>       |
>     note: while trying to match keyword `struct`
>      --> src/macros.rs
>       |
>       |             $vis:vis struct $name:ident
>       |                      ^^^^^^
>
> Now the error is:
>
>     error: cannot derive `Zeroable` for an enum
>      --> tests/ui/compile-fail/zeroable/enum.rs:5:1
>       |
>     5 | enum Num {
>       | ^^^^
>
>     error: cannot derive `Zeroable` for an enum
>
> Signed-off-by: Benno Lossin <lossin@...nel.org>
> ---
>  rust/pin-init/internal/src/lib.rs      |   5 +-
>  rust/pin-init/internal/src/zeroable.rs | 149 ++++++++++---------------
>  rust/pin-init/src/macros.rs            | 124 --------------------
>  3 files changed, 65 insertions(+), 213 deletions(-)

Yay! the updated code is much more readable.

>
> diff --git a/rust/pin-init/internal/src/lib.rs b/rust/pin-init/internal/src/lib.rs
> index 4c4dc639ce82..ec593362c5ac 100644
> --- a/rust/pin-init/internal/src/lib.rs
> +++ b/rust/pin-init/internal/src/lib.rs
> @@ -11,6 +11,7 @@
>  #![allow(missing_docs)]
>  
>  use proc_macro::TokenStream;
> +use syn::parse_macro_input;
>  
>  mod helpers;
>  mod pin_data;
> @@ -29,10 +30,10 @@ pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream {
>  
>  #[proc_macro_derive(Zeroable)]
>  pub fn derive_zeroable(input: TokenStream) -> TokenStream {
> -    zeroable::derive(input.into()).into()
> +    zeroable::derive(parse_macro_input!(input as _)).into()

This can just be

    zeroable::derive(parse_macro_input!(input)).into()

same for the below.

>  }
>  
>  #[proc_macro_derive(MaybeZeroable)]
>  pub fn maybe_derive_zeroable(input: TokenStream) -> TokenStream {
> -    zeroable::maybe_derive(input.into()).into()
> +    zeroable::maybe_derive(parse_macro_input!(input as _)).into()
>  }
> diff --git a/rust/pin-init/internal/src/zeroable.rs b/rust/pin-init/internal/src/zeroable.rs
> index d8a5ef3883f4..0328c3bdfceb 100644
> --- a/rust/pin-init/internal/src/zeroable.rs
> +++ b/rust/pin-init/internal/src/zeroable.rs
> @@ -1,99 +1,74 @@
>  // SPDX-License-Identifier: GPL-2.0
>  
> -use crate::helpers::{parse_generics, Generics};
> -use proc_macro2::{TokenStream, TokenTree};
> -use quote::quote;
> +use proc_macro2::TokenStream;
> +use quote::{quote, quote_spanned};
> +use syn::{parse_quote, Data, DeriveInput, Field, Fields};
>  
> -pub(crate) fn parse_zeroable_derive_input(
> -    input: TokenStream,
> -) -> (
> -    Vec<TokenTree>,
> -    Vec<TokenTree>,
> -    Vec<TokenTree>,
> -    Option<TokenTree>,
> -) {
> -    let (
> -        Generics {
> -            impl_generics,
> -            decl_generics: _,
> -            ty_generics,
> -        },
> -        mut rest,
> -    ) = parse_generics(input);
> -    // This should be the body of the struct `{...}`.
> -    let last = rest.pop();
> -    // Now we insert `Zeroable` as a bound for every generic parameter in `impl_generics`.
> -    let mut new_impl_generics = Vec::with_capacity(impl_generics.len());
> -    // Are we inside of a generic where we want to add `Zeroable`?
> -    let mut in_generic = !impl_generics.is_empty();
> -    // Have we already inserted `Zeroable`?
> -    let mut inserted = false;
> -    // Level of `<>` nestings.
> -    let mut nested = 0;
> -    for tt in impl_generics {
> -        match &tt {
> -            // If we find a `,`, then we have finished a generic/constant/lifetime parameter.
> -            TokenTree::Punct(p) if nested == 0 && p.as_char() == ',' => {
> -                if in_generic && !inserted {
> -                    new_impl_generics.extend(quote! { : ::pin_init::Zeroable });
> -                }
> -                in_generic = true;
> -                inserted = false;
> -                new_impl_generics.push(tt);
> -            }
> -            // If we find `'`, then we are entering a lifetime.
> -            TokenTree::Punct(p) if nested == 0 && p.as_char() == '\'' => {
> -                in_generic = false;
> -                new_impl_generics.push(tt);
> -            }
> -            TokenTree::Punct(p) if nested == 0 && p.as_char() == ':' => {
> -                new_impl_generics.push(tt);
> -                if in_generic {
> -                    new_impl_generics.extend(quote! { ::pin_init::Zeroable + });
> -                    inserted = true;
> -                }
> -            }
> -            TokenTree::Punct(p) if p.as_char() == '<' => {
> -                nested += 1;
> -                new_impl_generics.push(tt);
> -            }
> -            TokenTree::Punct(p) if p.as_char() == '>' => {
> -                assert!(nested > 0);
> -                nested -= 1;
> -                new_impl_generics.push(tt);
> -            }
> -            _ => new_impl_generics.push(tt),
> +pub(crate) fn derive(input: DeriveInput) -> TokenStream {
> +    let fields = match input.data {
> +        Data::Struct(data_struct) => data_struct.fields,
> +        Data::Union(data_union) => Fields::Named(data_union.fields),
> +        Data::Enum(data_enum) => {
> +            return quote_spanned! {data_enum.enum_token.span=>
> +                ::core::compile_error!("cannot derive `Zeroable` for an enum");
> +            };

Even if it's currently just one place that could error, I would still probably
use `syn`'s Error type as it's easier to extend in the future.

Best,
Gary

>          }
> +    };
> +    let name = input.ident;
> +    let mut generics = input.generics;
> +    for param in generics.type_params_mut() {
> +        param.bounds.insert(0, parse_quote!(::pin_init::Zeroable));
>      }
> -    assert_eq!(nested, 0);
> -    if in_generic && !inserted {
> -        new_impl_generics.extend(quote! { : ::pin_init::Zeroable });
> -    }
> -    (rest, new_impl_generics, ty_generics, last)
> -}
> -


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ