rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] Support more safe `AsBytes`/`FromBytes` usage
@ 2025-12-12 23:42 Matthew Maurer
  2025-12-12 23:42 ` [PATCH 1/3] rust: transmute: Support transmuting slices of AsBytes/FromBytes types Matthew Maurer
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Matthew Maurer @ 2025-12-12 23:42 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich
  Cc: rust-for-linux, linux-kernel, Matthew Maurer

Currently:
* Slices of `AsBytes`/`FromBytes` types cannot be synthesized from
  bytes slices (without unsafe).
* Users must use `unsafe impl` to assert that structs are `AsBytes` or
  `FromBytes` and write appropriate justifications.
* Bindgen-generated types cannot implement `AsBytes` or `FromBytes`,
  meaning that casting them to or from bytes involves assumptions in the
  `unsafe impl` that could easily go out of sync if the underlying
  header is edited or an assumption is invalid on a platform the author
  did not consider.

This series seeks to address all there of these by:
1. Adding slice cast functions to `FromBytes`
2. Adding a derive for `AsBytes` and `FromBytes`, for now restricted to
   the simple case of structs.
3. Refactoring the crate structure to allow the derives added in 2 to be
   used on bindgen definitions.

1 or 2 can be taken independently, 3 requires 2.

1 and 2 I think are in pretty goood shape. 3 I'm throwing out their to
sketch out what a potential solution to this problem could look like.

Options for #3 that I can see:
* Common types/traits crate (what's done in this patch), maybe bikeshed
  name to something other than `traits`.
* Move `bindings` from being its own crate to being source-included into
  the `kernel` crate.
* Import `zerocopy` instead, and move off providing our own
  `AsBytes`/`FromBytes`. This would lose our special-casing of pointers
  as not supporting byte-casting, and require importing yet more
  third-party code, but would solve the problems I enumerated.

Signed-off-by: Matthew Maurer <mmaurer@google.com>
---
Matthew Maurer (3):
      rust: transmute: Support transmuting slices of AsBytes/FromBytes types
      rust: Add support for deriving `AsBytes` and `FromBytes`
      rust: Support deriving `AsBytes`/`FromBytes` on bindgen types

 rust/Makefile                        | 19 +++++++--
 rust/bindgen_parameters              |  8 ++++
 rust/bindings/lib.rs                 |  1 +
 rust/kernel/lib.rs                   |  3 +-
 rust/macros/lib.rs                   | 80 ++++++++++++++++++++++++++++++++++++
 rust/macros/transmute.rs             | 60 +++++++++++++++++++++++++++
 rust/traits/lib.rs                   |  6 +++
 rust/{kernel => traits}/transmute.rs | 72 ++++++++++++++++++++++++++++++++
 rust/uapi/lib.rs                     |  1 +
 9 files changed, 245 insertions(+), 5 deletions(-)
---
base-commit: 008d3547aae5bc86fac3eda317489169c3fda112
change-id: 20251212-transmute-8ab6076700a8

Best regards,
-- 
Matthew Maurer <mmaurer@google.com>


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-12-14  2:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-12 23:42 [PATCH 0/3] Support more safe `AsBytes`/`FromBytes` usage Matthew Maurer
2025-12-12 23:42 ` [PATCH 1/3] rust: transmute: Support transmuting slices of AsBytes/FromBytes types Matthew Maurer
2025-12-12 23:42 ` [PATCH 2/3] rust: Add support for deriving `AsBytes` and `FromBytes` Matthew Maurer
2025-12-12 23:42 ` [PATCH 3/3] rust: Support deriving `AsBytes`/`FromBytes` on bindgen types Matthew Maurer
2025-12-13  0:34   ` Matthew Maurer
2025-12-14  2:23   ` kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).