Re: [PATCH v3 1/4] rust: uaccess: add userspace pointers

Alice Ryhl
2024-03-18T20:10:19Z

On Mon, Mar 18, 2024 at 8:33 PM Boqun Feng <boqun.feng@gmail.com> wrote:
>
> On Mon, Mar 18, 2024 at 08:12:27PM +0100, Alice Ryhl wrote:
> > On Mon, Mar 18, 2024 at 7:59 PM Boqun Feng <boqun.feng@gmail.com> wrote:
> > >
> > > On Mon, Mar 11, 2024 at 10:47:13AM +0000, Alice Ryhl wrote:
> > > > +
> > > > + /// Reads raw data from the user slice into a raw kernel buffer.
> > > > + ///
> > > > + /// Fails with `EFAULT` if the read encounters a page fault.
> > > > + ///
> > > > + /// # Safety
> > > > + ///
> > > > + /// The `out` pointer must be valid for writing `len` bytes.
> > > > + pub unsafe fn read_raw(&mut self, out: *mut u8, len: usize) -> Result {
> > >
> > > I don't think we want to promote the pub usage of this unsafe function,
> > > right? We can provide a safe version:
> > >
> > > pub fn read_slice(&mut self, to: &[u8]) -> Result
> > >
> > > and all users can just use the safe version (with the help of
> > > slice::from_raw_parts_mut() if necessary).
> >
> > Personally, I think having the function be unsafe is plenty discouragement.
> >
> > Also, this method would need an &mut [u8], which opens the can of
> > worms related to uninitialized memory. The _raw version of this method
>
> make it a `&mut [MayUninit<u8>]` then? If that works, then _raw version
> is not more powerful therefore no need to pub it.

Nobody actually has a need for that. Also, it doesn't even remove the
need for unsafe code in the caller, since the caller still needs to
assert that the call has initialized the memory.

> > is strictly more powerful.
> >
> > I don't think I actually use it directly in Binder, so I can make it
> > private if you think that's important. It needs to be pub(crate),
>
> I might be too picky, but avoiding pub unsafe functions if not necessary
> could help us reduce unnecessary unsafe code ;-)
>
> Regards,
> Boqun
>
> > though, since it is used in `Page`.
> >
> > Alice