Skip to content

Commit

Permalink
Rollup merge of rust-lang#59869 - jethrogb:jb/sgx-iovec, r=sfackler
Browse files Browse the repository at this point in the history
SGX target: implemented vectored I/O

r? @sfackler

Includes rust-lang#59857
  • Loading branch information
Centril authored Apr 30, 2019
2 parents 00859e3 + 09f4008 commit c54b77c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
12 changes: 10 additions & 2 deletions src/libstd/sys/sgx/abi/usercalls/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,11 @@ impl<T, I: SliceIndex<[T]>> Index<I> for UserRef<[T]> where [T]: UserSafe, I::Ou
#[inline]
fn index(&self, index: I) -> &UserRef<I::Output> {
unsafe {
UserRef::from_ptr(index.index(&*self.as_raw_ptr()))
if let Some(slice) = index.get(&*self.as_raw_ptr()) {
UserRef::from_ptr(slice)
} else {
rtabort!("index out of range for user slice");
}
}
}
}
Expand All @@ -533,7 +537,11 @@ impl<T, I: SliceIndex<[T]>> IndexMut<I> for UserRef<[T]> where [T]: UserSafe, I:
#[inline]
fn index_mut(&mut self, index: I) -> &mut UserRef<I::Output> {
unsafe {
UserRef::from_mut_ptr(index.index_mut(&mut*self.as_raw_mut_ptr()))
if let Some(slice) = index.get_mut(&mut*self.as_raw_mut_ptr()) {
UserRef::from_mut_ptr(slice)
} else {
rtabort!("index out of range for user slice");
}
}
}
}
Expand Down
45 changes: 37 additions & 8 deletions src/libstd/sys/sgx/abi/usercalls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::io::{Error as IoError, Result as IoResult};
use crate::cmp;
use crate::io::{Error as IoError, Result as IoResult, IoSlice, IoSliceMut};
use crate::time::Duration;

pub(crate) mod alloc;
Expand All @@ -8,13 +9,27 @@ pub(crate) mod raw;
use self::raw::*;

/// Usercall `read`. See the ABI documentation for more information.
///
/// This will do a single `read` usercall and scatter the read data among
/// `bufs`. To read to a single buffer, just pass a slice of length one.
#[unstable(feature = "sgx_platform", issue = "56975")]
pub fn read(fd: Fd, buf: &mut [u8]) -> IoResult<usize> {
pub fn read(fd: Fd, bufs: &mut [IoSliceMut<'_>]) -> IoResult<usize> {
unsafe {
let mut userbuf = alloc::User::<[u8]>::uninitialized(buf.len());
let len = raw::read(fd, userbuf.as_mut_ptr(), userbuf.len()).from_sgx_result()?;
userbuf[..len].copy_to_enclave(&mut buf[..len]);
Ok(len)
let total_len = bufs.iter().fold(0usize, |sum, buf| sum.saturating_add(buf.len()));
let mut userbuf = alloc::User::<[u8]>::uninitialized(total_len);
let ret_len = raw::read(fd, userbuf.as_mut_ptr(), userbuf.len()).from_sgx_result()?;
let userbuf = &userbuf[..ret_len];
let mut index = 0;
for buf in bufs {
let end = cmp::min(index + buf.len(), userbuf.len());
if let Some(buflen) = end.checked_sub(index) {
userbuf[index..end].copy_to_enclave(&mut buf[..buflen]);
index += buf.len();
} else {
break
}
}
Ok(userbuf.len())
}
}

Expand All @@ -30,10 +45,24 @@ pub fn read_alloc(fd: Fd) -> IoResult<Vec<u8>> {
}

/// Usercall `write`. See the ABI documentation for more information.
///
/// This will do a single `write` usercall and gather the written data from
/// `bufs`. To write from a single buffer, just pass a slice of length one.
#[unstable(feature = "sgx_platform", issue = "56975")]
pub fn write(fd: Fd, buf: &[u8]) -> IoResult<usize> {
pub fn write(fd: Fd, bufs: &[IoSlice<'_>]) -> IoResult<usize> {
unsafe {
let userbuf = alloc::User::new_from_enclave(buf);
let total_len = bufs.iter().fold(0usize, |sum, buf| sum.saturating_add(buf.len()));
let mut userbuf = alloc::User::<[u8]>::uninitialized(total_len);
let mut index = 0;
for buf in bufs {
let end = cmp::min(index + buf.len(), userbuf.len());
if let Some(buflen) = end.checked_sub(index) {
userbuf[index..end].copy_from_enclave(&buf[..buflen]);
index += buf.len();
} else {
break
}
}
raw::write(fd, userbuf.as_ptr(), userbuf.len()).from_sgx_result()
}
}
Expand Down
14 changes: 11 additions & 3 deletions src/libstd/sys/sgx/fd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use fortanix_sgx_abi::Fd;

use crate::io;
use crate::io::{self, IoSlice, IoSliceMut};
use crate::mem;
use crate::sys::{AsInner, FromInner, IntoInner};
use super::abi::usercalls;
Expand All @@ -25,11 +25,19 @@ impl FileDesc {
}

pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
usercalls::read(self.fd, buf)
usercalls::read(self.fd, &mut [IoSliceMut::new(buf)])
}

pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
usercalls::read(self.fd, bufs)
}

pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
usercalls::write(self.fd, buf)
usercalls::write(self.fd, &[IoSlice::new(buf)])
}

pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
usercalls::write(self.fd, bufs)
}

pub fn flush(&self) -> io::Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/sys/sgx/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ impl TcpStream {
}

pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
io::default_read_vectored(|b| self.read(b), bufs)
self.inner.inner.read_vectored(bufs)
}

pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
self.inner.inner.write(buf)
}

pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
io::default_write_vectored(|b| self.write(b), bufs)
self.inner.inner.write_vectored(bufs)
}

pub fn peer_addr(&self) -> io::Result<SocketAddr> {
Expand Down

0 comments on commit c54b77c

Please sign in to comment.