From nobody Thu Dec 18 19:43:32 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A56226E708 for ; Tue, 16 Dec 2025 00:44:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765845897; cv=none; b=qbOWUOKFiXRM4p+DripXW0bVwEc1FTkY11qVGJ2W+mojx+xuEtqTpqQtj53A6DLkYiMNnnAuqEjXicAPzQ7UsmZTDPWG18DGJS9i0S6ggbsTapFh1jYWv8T7m38AqZfY6V2/AXKqC11ewSSL7Nz8q0Ov4F1X5PZfIXlaBr0h97Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765845897; c=relaxed/simple; bh=6wygOt1D76qaraHZDNszRyQ5DIBdZPIQXleSdYwh9us=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=F6LLyPDsO9xkImgfLRl5wxu8J/6tvYp3P/KpuBhWZ6/qcGQ58p6IyaMv84w2ie6GPZH7NmTbMPzSaNXgq0GhhRyx5DkgWzZE/28efjRRGHEIyDb9PU6MXg6zJ6OL5mjFyEyiloYJvfARmW7mZp63dRSi0/ewKioSALPqkfFjNpM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=r/8spQtu; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="r/8spQtu" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b993eb2701bso3945712a12.0 for ; Mon, 15 Dec 2025 16:44:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1765845895; x=1766450695; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ZfIR7uowxPfrBGm0h5/0Sac13z+XC1w8PXgcnbB/c+4=; b=r/8spQtu5LkJZaTKq9IiDotDFMPuUvucKRBk4/uHhAgJKknNNP3egqIQtU9ekCE4yo aQMiBR0m2y2FjkiCaP5w8lVv6eIC+l50+miNYv32zZmU5ku/Ai2R4UR8qmSCTZftJbxV KtxNX6flzbtOINHwfBL4GReHquZJxSQJbwngdGDFoZdWxNln/Z3HfG4OwPq9n/CoC5zv 9coBS5p2wp20gYNjc6qjZb+MxgPZ18DILNVn9dJAoEYeOS+LOGYHZYHBesDs8HmRa2fM 5kDLxQu1zgKubqi+xhNg8ajbgQ2B1f3wq/NCX1q7ELERYBiLnAWE4ZSAjDnyb2/QEy0t y35A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765845895; x=1766450695; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZfIR7uowxPfrBGm0h5/0Sac13z+XC1w8PXgcnbB/c+4=; b=obPDPMheMstOWz5luzqE9EjRe9oz3yV+1Qgr6GWwIi0BGfNUm1B740x5hx3FJaq/Fp 7AFcpUxmA1f1Sx6Oyy6ztjwaQhUEpUeCZ+bC5e5PCl3j4aJMEED7IyYqzjQZ8yO/OfVH b7ZvKON3VrhvdQIn+/cbQmVpsWj6GkMW/Jrfi6388ptUHKvTL3p0EFLB1foYTGSrWk4c gg1mMJGqJ8zgu5EwDJB5rPJcXkza4IH2QejfHtaI2f6NVAgC3qo8gOrmMlaBM/IA0RNN XonPXhhAVUkH2efZdx1Qeg9Ter8gD7nq+A62WOqf0fsflvTrdsMCQlakY50ziRfrbg9h lXhA== X-Forwarded-Encrypted: i=1; AJvYcCUjvrYJNVERbtZRv+xnB052CDgbbyWgNYIS+sXCw9yeXtLWmhhF8A0IZi3wHzz9a+UNjGlwKD1wu0AFruE=@vger.kernel.org X-Gm-Message-State: AOJu0Yy0AdY01jc46jmjZN6MzgF/c0Oo1QuZobaxAgH2t73rnoY2NQ2p lxg1982Bhw8ZOjprYnNn2g1rJcHn8FBrpgGe8+N3oE9zJyYB740J9BNjENjmC0BPfKMJHaS6nux YcAn7DhLSMw== X-Google-Smtp-Source: AGHT+IFqSf3q193P4LBHoVV6viSmK6DVC79Cs1RUnidxl/IY+cL+SDg3bXJP2FIPBC25IfMmPtxP8JpuNH8b X-Received: from dlkk32.prod.google.com ([2002:a05:7022:60a0:b0:11d:cf7a:5407]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:6982:b0:11f:2c69:2d with SMTP id a92af1059eb24-11f34c2f2fbmr9887359c88.45.1765845894728; Mon, 15 Dec 2025 16:44:54 -0800 (PST) Date: Tue, 16 Dec 2025 00:44:42 +0000 In-Reply-To: <20251216-transmute-v2-0-b23e5277ad02@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251216-transmute-v2-0-b23e5277ad02@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1765845890; l=5561; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=6wygOt1D76qaraHZDNszRyQ5DIBdZPIQXleSdYwh9us=; b=pIydZKX38osjwrZhA1nMfCFpTpxKPqd2SJF4SsLsdCdGf3IjsXuXYhZOzPebrFIKjn/FzJmv+ AlLs3q9lkpVA8is9WxX2c98oCDi8+ct34SbR7c3BDTkBL8cyUk1WoUy X-Mailer: b4 0.14.2 Message-ID: <20251216-transmute-v2-2-b23e5277ad02@google.com> Subject: [PATCH v2 2/3] rust: Add support for deriving `AsBytes` and `FromBytes` From: Matthew Maurer To: Miguel Ojeda , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Matthew Maurer Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This provides a derive macro for `AsBytes` and `FromBytes` for structs only. For both, it checks the respective trait on every underlying field. For `AsBytes`, it emits a const-time padding check that will fail the compilation if derived on a type with padding. Signed-off-by: Matthew Maurer --- rust/macros/lib.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++= ++++ rust/macros/transmute.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs index b38002151871a33f6b4efea70be2deb6ddad38e2..d66397942529f67697f74a908e2= 57cacc4201d84 100644 --- a/rust/macros/lib.rs +++ b/rust/macros/lib.rs @@ -20,9 +20,14 @@ mod kunit; mod module; mod paste; +mod transmute; mod vtable; =20 use proc_macro::TokenStream; +use syn::{ + parse_macro_input, + DeriveInput, // +}; =20 /// Declares a kernel module. /// @@ -475,3 +480,61 @@ pub fn paste(input: TokenStream) -> TokenStream { pub fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream { kunit::kunit_tests(attr, ts) } + +/// Implements `FromBytes` for a struct. +/// +/// It will fail compilation if the struct you are deriving on cannot be d= etermined to implement +/// `FromBytes` safely. It may still fail for some types which would be sa= fe to implement +/// `FromBytes` for, in which case you will need to write the implementati= on and justification +/// yourself. +/// +/// Main reasons your type may be rejected: +/// * Not a `struct` +/// * One of the fields is not `FromBytes` +/// +/// # Examples +/// +/// ``` +/// #[derive(FromBytes)] +/// #[repr(C)] +/// struct Foo { +/// x: u32, +/// y: u16, +/// z: u16, +/// } +/// ``` +#[proc_macro_derive(FromBytes)] +pub fn derive_from_bytes(tokens: TokenStream) -> TokenStream { + let input =3D parse_macro_input!(tokens as DeriveInput); + transmute::from_bytes(input).into() +} + +/// Implements `AsBytes` for a struct. +/// +/// It will fail compilation if the struct you are deriving on cannot be d= etermined to implement +/// `AsBytes` safely. It may still fail for some structures which would be= safe to implement +/// `AsBytes`, in which case you will need to write the implementation and= justification +/// yourself. +/// +/// Main reasons your type may be rejected: +/// * Not a `struct` +/// * One of the fields is not `AsBytes` +/// * Your struct has generic parameters +/// * There is padding somewhere in your struct +/// +/// # Examples +/// +/// ``` +/// #[derive(AsBytes)] +/// #[repr(C)] +/// struct Foo { +/// x: u32, +/// y: u16, +/// z: u16, +/// } +/// ``` +#[proc_macro_derive(AsBytes)] +pub fn derive_as_bytes(tokens: TokenStream) -> TokenStream { + let input =3D parse_macro_input!(tokens as DeriveInput); + transmute::as_bytes(input).into() +} diff --git a/rust/macros/transmute.rs b/rust/macros/transmute.rs new file mode 100644 index 0000000000000000000000000000000000000000..43cf36a1334f1fed23c0e777026= 392f987f78d8d --- /dev/null +++ b/rust/macros/transmute.rs @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0 + +use proc_macro2::TokenStream; +use syn::{parse_quote, DeriveInput, Fields, Ident, ItemConst, Path, WhereC= lause}; + +fn all_fields_impl(fields: &Fields, trait_: &Path) -> WhereClause { + let tys =3D fields.iter().map(|field| &field.ty); + parse_quote! { + where #(for<'a> #tys: #trait_),* + } +} + +fn struct_padding_check(fields: &Fields, name: &Ident) -> ItemConst { + let tys =3D fields.iter().map(|field| &field.ty); + parse_quote! { + const _: () =3D { + assert!(#(core::mem::size_of::<#tys>())+* =3D=3D core::mem::si= ze_of::<#name>()); + }; + } +} + +pub(crate) fn as_bytes(input: DeriveInput) -> TokenStream { + if !input.generics.params.is_empty() { + return quote::quote! { compile_error!("#[derive(AsBytes)] does not= support generics") }; + } + let syn::Data::Struct(ref ds) =3D &input.data else { + return quote::quote! { compile_error!("#[derive(AsBytes)] only sup= ports structs") }; + }; + let name =3D input.ident; + let trait_ =3D parse_quote! { ::kernel::transmute::AsBytes }; + let where_clause =3D all_fields_impl(&ds.fields, &trait_); + let padding_check =3D struct_padding_check(&ds.fields, &name); + quote::quote! { + #padding_check + // SAFETY: #name has no padding and all of its fields implement `A= sBytes` + unsafe impl #trait_ for #name #where_clause {} + } +} + +pub(crate) fn from_bytes(input: DeriveInput) -> TokenStream { + let syn::Data::Struct(ref ds) =3D &input.data else { + return quote::quote! { compile_error!("#[derive(FromBytes)] only s= upports structs") }; + }; + let (impl_generics, ty_generics, base_where_clause) =3D input.generics= .split_for_impl(); + let name =3D input.ident; + let trait_ =3D parse_quote! { ::kernel::transmute::FromBytes }; + let mut where_clause =3D all_fields_impl(&ds.fields, &trait_); + if let Some(base_clause) =3D base_where_clause { + where_clause + .predicates + .extend(base_clause.predicates.clone()) + }; + quote::quote! { + // SAFETY: All fields of #name implement `FromBytes` and it is a s= truct, so there is no + // implicit discriminator. + unsafe impl #impl_generics #trait_ for #name #ty_generics #where_c= lause {} + } +} --=20 2.52.0.305.g3fc767764a-goog