Use uninitialized buffers for read and recvfrom#1606
Merged
mkroening merged 2 commits intohermit-os:mainfrom Feb 19, 2025
Merged
Use uninitialized buffers for read and recvfrom#1606mkroening merged 2 commits intohermit-os:mainfrom
read and recvfrom#1606mkroening merged 2 commits intohermit-os:mainfrom
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
Contributor
Author
|
The CI failure seems likely to be unrelated: |
Contributor
Author
|
How should I coordinate with the Hermit side when I submit the PR to Rust for |
Member
|
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. :) |
jhpratt
added a commit
to jhpratt/rust
that referenced
this pull request
Mar 18, 2025
…rmit, r=tgross35 Implement `read_buf` for Hermit Following hermit-os/kernel#1606, it is now safe to implement `Read::read_buf` for file descriptors on Hermit. cc `@mkroening`
jhpratt
added a commit
to jhpratt/rust
that referenced
this pull request
Mar 18, 2025
…rmit, r=tgross35 Implement `read_buf` for Hermit Following hermit-os/kernel#1606, it is now safe to implement `Read::read_buf` for file descriptors on Hermit. cc ``@mkroening``
matthiaskrgr
added a commit
to matthiaskrgr/rust
that referenced
this pull request
Mar 18, 2025
…rmit, r=tgross35 Implement `read_buf` for Hermit Following hermit-os/kernel#1606, it is now safe to implement `Read::read_buf` for file descriptors on Hermit. cc ```@mkroening```
github-actions bot
pushed a commit
to model-checking/verify-rust-std
that referenced
this pull request
Apr 2, 2025
…rmit, r=tgross35 Implement `read_buf` for Hermit Following hermit-os/kernel#1606, it is now safe to implement `Read::read_buf` for file descriptors on Hermit. cc ```@mkroening```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently, when using an uninitialized buffer for
readorrecvfrom, as would be typical in C or required forRead::read_buf, it is casted from*mut u8at the FFI boundary insys_read/sys_readv/sys_recvfromto 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_slicetakes 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_bufforstd::fs::Fileandstd::io::Stdinon Hermit. That effort is tracked in rust-lang/rust#136756.