1
Fork 0

Implement read_buf and vectored read/write for SGX stdio

This commit is contained in:
Thalia Archibald 2025-02-07 15:02:08 -08:00
parent b52666868f
commit 8c7a94e4cd
3 changed files with 49 additions and 3 deletions

View file

@ -1,5 +1,7 @@
use crate::cmp; use crate::cmp;
use crate::io::{Error as IoError, ErrorKind, IoSlice, IoSliceMut, Result as IoResult}; use crate::io::{
BorrowedCursor, Error as IoError, ErrorKind, IoSlice, IoSliceMut, Result as IoResult,
};
use crate::random::{DefaultRandomSource, Random}; use crate::random::{DefaultRandomSource, Random};
use crate::time::{Duration, Instant}; use crate::time::{Duration, Instant};
@ -36,6 +38,19 @@ pub fn read(fd: Fd, bufs: &mut [IoSliceMut<'_>]) -> IoResult<usize> {
} }
} }
/// Usercall `read` with an uninitialized buffer. See the ABI documentation for
/// more information.
#[unstable(feature = "sgx_platform", issue = "56975")]
pub fn read_buf(fd: Fd, mut buf: BorrowedCursor<'_>) -> IoResult<()> {
unsafe {
let mut userbuf = alloc::User::<[u8]>::uninitialized(buf.capacity());
let len = raw::read(fd, userbuf.as_mut_ptr().cast(), userbuf.len()).from_sgx_result()?;
userbuf[..len].copy_to_enclave(&mut buf.as_mut()[..len]);
buf.advance_unchecked(len);
Ok(())
}
}
/// Usercall `read_alloc`. See the ABI documentation for more information. /// Usercall `read_alloc`. See the ABI documentation for more information.
#[unstable(feature = "sgx_platform", issue = "56975")] #[unstable(feature = "sgx_platform", issue = "56975")]
pub fn read_alloc(fd: Fd) -> IoResult<Vec<u8>> { pub fn read_alloc(fd: Fd) -> IoResult<Vec<u8>> {

View file

@ -29,7 +29,7 @@ impl FileDesc {
} }
pub fn read_buf(&self, buf: BorrowedCursor<'_>) -> io::Result<()> { pub fn read_buf(&self, buf: BorrowedCursor<'_>) -> io::Result<()> {
crate::io::default_read_buf(|b| self.read(b), buf) usercalls::read_buf(self.fd, buf)
} }
pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {

View file

@ -1,6 +1,6 @@
use fortanix_sgx_abi as abi; use fortanix_sgx_abi as abi;
use crate::io; use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
use crate::sys::fd::FileDesc; use crate::sys::fd::FileDesc;
pub struct Stdin(()); pub struct Stdin(());
@ -24,6 +24,19 @@ impl io::Read for Stdin {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
with_std_fd(abi::FD_STDIN, |fd| fd.read(buf)) with_std_fd(abi::FD_STDIN, |fd| fd.read(buf))
} }
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> io::Result<()> {
with_std_fd(abi::FD_STDIN, |fd| fd.read_buf(buf))
}
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
with_std_fd(abi::FD_STDIN, |fd| fd.read_vectored(bufs))
}
#[inline]
fn is_read_vectored(&self) -> bool {
true
}
} }
impl Stdout { impl Stdout {
@ -40,6 +53,15 @@ impl io::Write for Stdout {
fn flush(&mut self) -> io::Result<()> { fn flush(&mut self) -> io::Result<()> {
with_std_fd(abi::FD_STDOUT, |fd| fd.flush()) with_std_fd(abi::FD_STDOUT, |fd| fd.flush())
} }
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
with_std_fd(abi::FD_STDOUT, |fd| fd.write_vectored(bufs))
}
#[inline]
fn is_write_vectored(&self) -> bool {
true
}
} }
impl Stderr { impl Stderr {
@ -56,6 +78,15 @@ impl io::Write for Stderr {
fn flush(&mut self) -> io::Result<()> { fn flush(&mut self) -> io::Result<()> {
with_std_fd(abi::FD_STDERR, |fd| fd.flush()) with_std_fd(abi::FD_STDERR, |fd| fd.flush())
} }
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
with_std_fd(abi::FD_STDERR, |fd| fd.write_vectored(bufs))
}
#[inline]
fn is_write_vectored(&self) -> bool {
true
}
} }
pub const STDIN_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE; pub const STDIN_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE;