Skip to content

Support more safe `AsBytes`/`FromBytes` usage

Matthew Maurer lkml rust-for-linux 3 versions – In review

Get series with b4
b4 am [email protected] change-id 20251212-transmute-8ab6076700a8

Versions newest first

  1. v3 Latest4 patches 4 repliesIn review
  2. v23 patches 14 repliesSuperseded
  3. v13 patches 1 replySuperseded

Changes in v3 from the cover letter

- Explained the for<'a> usage in the where clauses generated by macro.
- Split bindgen support into two commits, one which does the crate migration and one which enables it.
- Link to v2: https://lore.kernel.org/r/[email protected]

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4rust: transmute: Support transmuting slices of AsBytes/FromBytes typesrust/kernel/transmute.rs+72 -00 replies
2/4rust: transmute: Add support for deriving `AsBytes` and `FromBytes`rust/macros/lib.rs rust/macros/transmute.rs+128 -00 replies
3/4rust: transmute: Migrate AsBytes/FromBytes to ffi crate for bindgenrust/Makefile rust/ffi/lib.rs rust/ffi/transmute.rs +2 more+10 -50 replies
4/4rust: transmute: Support deriving AsBytes/FromBytes on bindgen typesrust/Makefile rust/bindgen_parameters rust/bindings/lib.rs +3 more+51 -90 replies

Reviews trailers per patch

1 of 4 patches has a Reviewed-by or Acked-by.

PatchDaniel Almeida
1/4R-b
2/4
3/4
4/4

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v3 thread

Cover letter [PATCH v3 0/4] Support more safe `AsBytes`/`FromBytes` usage

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 move AsBytes and FromBytes to ffi to allow bindings and uapi to reference them.
4. Enabling bindgen derivation in bindings/uapi through AsBytesFfi and FromBytesFfi.

1+2 can be landed separately if needed, 3 has no purpose without 4, and 3+4 need 1+2.

---
Changes in v3:
- Explained the for<'a> usage in the where clauses generated by macro.
- Split bindgen support into two commits, one which does the crate migration and one which enables it.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Reworked to put transmute in ffi rather than creating a new crate, per Alice's comment on Zulip.
- Switched to new kernel import style.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Matthew Maurer (4):
rust: transmute: Support transmuting slices of AsBytes/FromBytes types
rust: transmute: Add support for deriving AsBytes and FromBytes
rust: transmute: Migrate AsBytes/FromBytes to ffi crate for bindgen
rust: transmute: Support deriving AsBytes/FromBytes on bindgen types

 rust/Makefile                     | 14 ++++---
 rust/bindgen_parameters           |  8 ++++
 rust/bindings/lib.rs              |  4 ++
 rust/{ffi.rs => ffi/lib.rs}       |  5 +++
 rust/{kernel => ffi}/transmute.rs | 72 +++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs                |  2 +-
 rust/macros/lib.rs                | 83 +++++++++++++++++++++++++++++++++++++++
 rust/macros/transmute.rs          | 67 +++++++++++++++++++++++++++++++
 rust/uapi/lib.rs                  |  4 ++
 scripts/generate_rust_analyzer.py |  2 +-
 10 files changed, 254 insertions(+), 7 deletions(-)

---
base-commit: 008d3547aae5bc86fac3eda317489169c3fda112
change-id: 20251212-transmute-8ab6076700a8

Best regards,

Signature · 1 line
Matthew Maurer <[email protected]>