Re: [PATCH 2/6] rust: binder: transmute transaction data

From: Alice Ryhl

Date: Tue May 26 2026 - 09:49:33 EST


On Tue, May 26, 2026 at 3:39 PM Tamir Duberstein <tamird@xxxxxxxxxx> wrote:
>
> On Tue, May 26, 2026 at 9:36 AM Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:
> >
> > On Tue, May 26, 2026 at 3:34 PM Tamir Duberstein <tamird@xxxxxxxxxx> wrote:
> > >
> > > On Tue, May 26, 2026 at 8:33 AM Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:
> > > >
> > > > On Fri, May 22, 2026 at 07:12:47PM +0200, Tamir Duberstein wrote:
> > > > > `BinderTransactionData` is a transparent wrapper around
> > > > > `binder_transaction_data`. Use a transmute to view the transaction data in
> > > > > `BinderTransactionDataSecctx` through that wrapper, matching the safety
> > > > > argument at the conversion site and avoiding the raw pointer round trip.
> > > > >
> > > > > Signed-off-by: Tamir Duberstein <tamird@xxxxxxxxxx>
> > > >
> > > > I think it is an anti-pattern to transmute references. Please keep the
> > > > raw pointer cast.
> > >
> > > Can you please elaborate? We already have this for kernel::io::Mmio:
> > >
> > > ```
> > > rust/kernel/io.rs- pub fn relaxed(&self) -> &RelaxedMmio<SIZE> {
> > > rust/kernel/io.rs- // SAFETY: `RelaxedMmio` is
> > > `#[repr(transparent)]` over `Mmio`, so `Mmio<SIZE>` and
> > > rust/kernel/io.rs- // `RelaxedMmio<SIZE>` have identical layout.
> > > rust/kernel/io.rs: unsafe { core::mem::transmute(self) }
> > > rust/kernel/io.rs- }
> > > ```
> >
> > I also think that code is using the same anti-pattern.
>
> As in previous email: please elaborate. Why is casting through pointers better?

The reasoning is similar to the reasoning against `as` casts. In this
particular case, it may be okay, but it's not always okay because if
the reference is a wide ptr, then the layout of whether the data or
vtable pointer is stored first is not defined by Rust, and a transmute
assumes that this layout is unchanged between the two reference types.
With a cast, this potential layout divergence does not matter, as the
cast will do the right thing.

Alice