Skip to content

Commit 8c7a94e

Browse files
committed
Implement read_buf and vectored read/write for SGX stdio
1 parent b526668 commit 8c7a94e

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

‎library/std/src/sys/pal/sgx/abi/usercalls/mod.rs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
usecrate::cmp;
2-
usecrate::io::{ErrorasIoError,ErrorKind,IoSlice,IoSliceMut,ResultasIoResult};
2+
usecrate::io::{
3+
BorrowedCursor,ErrorasIoError,ErrorKind,IoSlice,IoSliceMut,ResultasIoResult,
4+
};
35
usecrate::random::{DefaultRandomSource,Random};
46
usecrate::time::{Duration,Instant};
57

@@ -36,6 +38,19 @@ pub fn read(fd: Fd, bufs: &mut [IoSliceMut<'_>]) -> IoResult<usize> {
3638
}
3739
}
3840

41+
/// Usercall `read` with an uninitialized buffer. See the ABI documentation for
42+
/// more information.
43+
#[unstable(feature = "sgx_platform", issue = "56975")]
44+
pubfnread_buf(fd:Fd,mutbuf:BorrowedCursor<'_>) -> IoResult<()>{
45+
unsafe{
46+
letmut userbuf = alloc::User::<[u8]>::uninitialized(buf.capacity());
47+
let len = raw::read(fd, userbuf.as_mut_ptr().cast(), userbuf.len()).from_sgx_result()?;
48+
userbuf[..len].copy_to_enclave(&mut buf.as_mut()[..len]);
49+
buf.advance_unchecked(len);
50+
Ok(())
51+
}
52+
}
53+
3954
/// Usercall `read_alloc`. See the ABI documentation for more information.
4055
#[unstable(feature = "sgx_platform", issue = "56975")]
4156
pubfnread_alloc(fd:Fd) -> IoResult<Vec<u8>>{

‎library/std/src/sys/pal/sgx/fd.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl FileDesc {
2929
}
3030

3131
pubfnread_buf(&self,buf:BorrowedCursor<'_>) -> io::Result<()>{
32-
crate::io::default_read_buf(|b| self.read(b), buf)
32+
usercalls::read_buf(self.fd, buf)
3333
}
3434

3535
pubfnread_vectored(&self,bufs:&mut[IoSliceMut<'_>]) -> io::Result<usize>{

‎library/std/src/sys/stdio/sgx.rs‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use fortanix_sgx_abi as abi;
22

3-
usecrate::io;
3+
usecrate::io::{self,BorrowedCursor,IoSlice,IoSliceMut};
44
usecrate::sys::fd::FileDesc;
55

66
pubstructStdin(());
@@ -24,6 +24,19 @@ impl io::Read for Stdin {
2424
fnread(&mutself,buf:&mut[u8]) -> io::Result<usize>{
2525
with_std_fd(abi::FD_STDIN, |fd| fd.read(buf))
2626
}
27+
28+
fnread_buf(&mutself,buf:BorrowedCursor<'_>) -> io::Result<()>{
29+
with_std_fd(abi::FD_STDIN, |fd| fd.read_buf(buf))
30+
}
31+
32+
fnread_vectored(&mutself,bufs:&mut[IoSliceMut<'_>]) -> io::Result<usize>{
33+
with_std_fd(abi::FD_STDIN, |fd| fd.read_vectored(bufs))
34+
}
35+
36+
#[inline]
37+
fnis_read_vectored(&self) -> bool{
38+
true
39+
}
2740
}
2841

2942
implStdout{
@@ -40,6 +53,15 @@ impl io::Write for Stdout {
4053
fnflush(&mutself) -> io::Result<()>{
4154
with_std_fd(abi::FD_STDOUT, |fd| fd.flush())
4255
}
56+
57+
fnwrite_vectored(&mutself,bufs:&[IoSlice<'_>]) -> io::Result<usize>{
58+
with_std_fd(abi::FD_STDOUT, |fd| fd.write_vectored(bufs))
59+
}
60+
61+
#[inline]
62+
fnis_write_vectored(&self) -> bool{
63+
true
64+
}
4365
}
4466

4567
implStderr{
@@ -56,6 +78,15 @@ impl io::Write for Stderr {
5678
fnflush(&mutself) -> io::Result<()>{
5779
with_std_fd(abi::FD_STDERR, |fd| fd.flush())
5880
}
81+
82+
fnwrite_vectored(&mutself,bufs:&[IoSlice<'_>]) -> io::Result<usize>{
83+
with_std_fd(abi::FD_STDERR, |fd| fd.write_vectored(bufs))
84+
}
85+
86+
#[inline]
87+
fnis_write_vectored(&self) -> bool{
88+
true
89+
}
5990
}
6091

6192
pubconstSTDIN_BUF_SIZE:usize = crate::sys::io::DEFAULT_BUF_SIZE;

0 commit comments

Comments
 (0)