-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use uninitialized buffers for read
and recvfrom
#1606
Conversation
Currently, when using an uninitialized buffer for `read`, as would be typical in C or required for `Read::read_buf`, it is casted from `*mut u8` at the FFI boundary in `sys_read`/`sys_readv` to a `&[u8]`. I think this is unsound. Instead, use `&[MaybeUninit<u8>]` internally. I use this instead of `core::io::BorrowedCursor<'_>`, because there are currently no cases where the initialized portion needs to be separately tracked. This enables implementing `std::io::Read::read_buf` for `std::fs::File` and `std::io::Stdin` on Hermit. That effort is tracked in rust-lang/rust#136756.
Since `smoltcp::socket::tcp::Socket::recv_slice` takes a `&[u8]`, inline it and refactor. As a bonus, this avoids a copy and clear in the error case and it makes it more clear that packets are dropped when given an insufficiently large buffer.
9951a8d
to
2d15daa
Compare
read
and recvfrom
2d15daa
to
5aab4b0
Compare
The CI failure seems likely to be unrelated:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot! :)
How should I coordinate with the Hermit side when I submit the PR to Rust for |
The kernel is built separately from the application and then linked together without LTO at the moment, so I think the chances of something bad happening due to undefined behavior are really slim in this case. So I'd say you can go ahead with that PR right away. Feel free to Tag @stlankes and me in everything Hermit-related to keep us in the loop, of course. :) |
Currently, when using an uninitialized buffer for
read
orrecvfrom
, as would be typical in C or required forRead::read_buf
, it is casted from*mut u8
at the FFI boundary insys_read
/sys_readv
/sys_recvfrom
to a&[u8]
. I think this is unsound. Instead, use&[MaybeUninit<u8>]
internally.I use
&[u8]
instead ofcore::io::BorrowedCursor<'_>
, because there are currently no cases where the initialized portion needs to be separately tracked.Since
smoltcp::socket::tcp::Socket::recv_slice
takes a&[u8]
, inline it and refactor. As a bonus, this avoids a copy and clear in the error case and it makes it more clear that packets are dropped when given an insufficiently large buffer.This PR enables implementing
std::io::Read::read_buf
forstd::fs::File
andstd::io::Stdin
on Hermit. That effort is tracked in rust-lang/rust#136756.