Re: [PATCH v3] rust: transmute: Add implementation for FromBytes trait
From: Daniel Almeida
Date: Sat Nov 09 2024 - 18:33:52 EST
+cc Abdiel
Hi Christian,
> On 9 Nov 2024, at 02:54, Christian dos Santos de Lima <christiansantoslima21@xxxxxxxxx> wrote:
>
> Add implementation and documentation for FromBytes trait.
>
> Add new feature block in order to allow using ToBytes
> and bound to from_bytes_mut function. I'm adding this feature
> because is possible create a value with disallowed bit pattern
> and as_byte_mut could create such value by mutating the array and
> accessing the original value. So adding ToBytes this can be avoided.
I’ve read this a couple of times. It’s hard to understand what you’re trying to say.
There are some grammar errors, but the even the key idea is hard to understand. IOW,
I had to go on Github to get the extra context needed to understand this part below:
> possible create a value with disallowed bit pattern
> and as_byte_mut could create such value by mutating the array and
> accessing the original value. So adding ToBytes this can be avoided.
Also, core::simd::ToBytes? Maybe Bjorn meant a new kernel::transmute::ToBytes trait?
Christian, are you aware of Abdiel’s dma_alloc_coherent patch [0]? How is this patch
an improvement over the simpler code below, for example?
> + fn cpu_buf(&self) -> &[T]
> + {
> + // SAFETY: The pointer is valid due to type invariant on `CoherentAllocation` and
> + // is valid for reads for `self.count * size_of::<T>` bytes.
> + unsafe { core::slice::from_raw_parts(self.cpu_addr, self.count) }
> + }
> +
> + fn cpu_buf_mut(&mut self) -> &mut [T]
> + {
> + // SAFETY: The pointer is valid due to type invariant on `CoherentAllocation` and
> + // is valid for reads for `self.count * size_of::<T>` bytes.
> + unsafe { core::slice::from_raw_parts_mut(self.cpu_addr, self.count) }
> + }
I believe that most users would have a pointer to memory allocated on the C side, as you can
see from Abdiel’s excerpt above. Without a user, it’s hard to know why we need this, specially
given the simd::ToBytes bound.
Just my 2c.
— Daniel
[0] https://lore.kernel.org/rust-for-linux/20241105104726.3111058-3-abdiel.janulgue@xxxxxxxxx/