Skip to content

[RTE-1352] Add read_exact, avoid using recursion, handle EINTR and EOF - #12

Open
gianlu33 wants to merge 2 commits into
fortanixvme-2023-05-07-alphafrom
gianlu33/rte-1352-improve-read-write
Open

gianlu33 wants to merge 2 commits into
fortanixvme-2023-05-07-alphafrom
gianlu33/rte-1352-improve-read-write

Conversation

@gianlu33

@gianlu33 gianlu33 commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

This PR improves read/write functions in the following way:

  • added a new read_exact function, required for the enclave-runner-client crate. This function is basically the same as read_all but it does not return the number of bytes read. This mirrors read_exact in std::io.
  • Removed #[cfg(not(feature="std"))] on write_all and read_all. In the client, we don't rely on std::io::Read/Write traits, but we use these functions for both std and non-std compilation.
  • Improved the implementation of read/write functions by removing unnecessary recursion
  • handle EINTR errors by retrying the read/write operation
  • return UnexpectedEof and WriteZero errors when reading or writing 0 bytes respectively (this also fixed infinite recursion in read_all)

@gianlu33
gianlu33 force-pushed the gianlu33/rte-1352-improve-read-write branch 2 times, most recently from 9fbe0a5 to 26cd179 Compare December 1, 2026 14:05
@gianlu33
gianlu33 changed the base branch from master to fortanixvme-2023-05-07-alpha December 1, 2026 14:05
@gianlu33
gianlu33 force-pushed the gianlu33/rte-1352-improve-read-write branch from 26cd179 to da00842 Compare December 1, 2026 14:46
@gianlu33
gianlu33 requested a review from raoulstrackx December 1, 2026 14:51
@gianlu33 gianlu33 changed the title [RTE-1352] Add read_exact, avoid using recursion, handle EINTR [RTE-1352] Add read_exact, avoid using recursion, handle EINTR and EOF Dec 1, 2026
Comment thread src/lib.rs Outdated
while !buf.is_empty() {
match self.read(buf) {
Ok(0) => return Err(Error::UnexpectedEof),
Ok(n) => buf = &mut core::mem::take(&mut buf)[n..],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just do buf = &buf[n..]?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, indeed that's better. thanks

Comment thread src/lib.rs Outdated

while !buf.is_empty() {
match self.write(buf) {
Ok(0) => return Err(Error::WriteZero),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some data may have been written in the previous call to write. That's a bit confusing. Can we turn this into a WRITE_ALL_EOF so it matches https://doc.rust-lang.org/src/core/io/write.rs.html#214. Eventually we'll use that implementation anyway.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I can do that, but note that WRITE_ALL_EOF in io is still mapped to ErrorKind::WriteZero: https://doc.rust-lang.org/src/core/io/error.rs.html#125

So in our impl From<Error> for io::Error I will have to do something like this:

#[cfg(feature="std")]
impl From<Error> for io::Error {
    fn from(err: Error) -> io::Error {
        match err {
            // ...
            Error::WriteAllEof         => io::Error::new(ErrorKind::WriteZero, err),
        }
    }
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Comment thread src/lib.rs Resolved
Comment thread src/lib.rs Outdated
pub fn read_exact(&self, mut buf: &mut [u8]) -> Result<(), Error> {
while !buf.is_empty() {
match self.read(buf) {
Ok(0) => return Err(Error::UnexpectedEof),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a similar issue with signals. See https://doc.rust-lang.org/src/alloc/io/read.rs.html#1037

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, now our read_exact implementation matches that one.

@gianlu33
gianlu33 force-pushed the gianlu33/rte-1352-improve-read-write branch from da00842 to b128d59 Compare December 3, 2026 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants