From d56969656eb0c933e9a7b442ebdc48b565f11f04 Mon Sep 17 00:00:00 2001 From: Mohsen Zohrevandi Date: Wed, 11 Nov 2020 10:36:54 -0800 Subject: [PATCH 1/2] Add Metadata in std::os::fortanix_sgx::io::FromRawFd --- library/std/src/sys/sgx/ext/io.rs | 37 +++++++++++++++++++++++++------ library/std/src/sys/sgx/net.rs | 6 ++--- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/library/std/src/sys/sgx/ext/io.rs b/library/std/src/sys/sgx/ext/io.rs index f79874a4aec97..ffa926417e306 100644 --- a/library/std/src/sys/sgx/ext/io.rs +++ b/library/std/src/sys/sgx/ext/io.rs @@ -25,8 +25,11 @@ pub trait AsRawFd { /// descriptor. #[unstable(feature = "sgx_platform", issue = "56975")] pub trait FromRawFd { + /// An associated type that contains relevant metadata for `Self`. + type Metadata: Default; + /// Constructs a new instance of `Self` from the given raw file - /// descriptor. + /// descriptor and metadata. /// /// This function **consumes ownership** of the specified file /// descriptor. The returned object will take responsibility for closing @@ -38,7 +41,7 @@ pub trait FromRawFd { /// accidentally allow violating this contract which can cause memory /// unsafety in code that relies on it being true. #[unstable(feature = "sgx_platform", issue = "56975")] - unsafe fn from_raw_fd(fd: RawFd) -> Self; + unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> Self; } /// A trait to express the ability to consume an object and acquire ownership of @@ -71,18 +74,38 @@ impl AsRawFd for net::TcpListener { } } +/// Metadata for `TcpStream`. +#[derive(Debug, Clone, Default)] +pub struct TcpStreamMetadata { + /// Local address of the TCP stream + pub local_addr: Option, + /// Peer address of the TCP stream + pub peer_addr: Option, +} + impl FromRawFd for net::TcpStream { - unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream { + type Metadata = TcpStreamMetadata; + + unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpStream { let fd = sys::fd::FileDesc::from_inner(fd); - let socket = sys::net::Socket::from_inner(fd); - net::TcpStream::from_inner(sys::net::TcpStream::from_inner((socket, None))) + let socket = sys::net::Socket::from_inner((fd, metadata.local_addr)); + net::TcpStream::from_inner(sys::net::TcpStream::from_inner((socket, metadata.peer_addr))) } } +/// Metadata for `TcpListener`. +#[derive(Debug, Clone, Default)] +pub struct TcpListenerMetadata { + /// Local address of the TCP listener + pub local_addr: Option, +} + impl FromRawFd for net::TcpListener { - unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener { + type Metadata = TcpListenerMetadata; + + unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpListener { let fd = sys::fd::FileDesc::from_inner(fd); - let socket = sys::net::Socket::from_inner(fd); + let socket = sys::net::Socket::from_inner((fd, metadata.local_addr)); net::TcpListener::from_inner(sys::net::TcpListener::from_inner(socket)) } } diff --git a/library/std/src/sys/sgx/net.rs b/library/std/src/sys/sgx/net.rs index 666a157b09cd0..3dd8267921e5e 100644 --- a/library/std/src/sys/sgx/net.rs +++ b/library/std/src/sys/sgx/net.rs @@ -37,9 +37,9 @@ impl TryIntoInner for Socket { } } -impl FromInner for Socket { - fn from_inner(inner: FileDesc) -> Socket { - Socket { inner: Arc::new(inner), local_addr: None } +impl FromInner<(FileDesc, Option)> for Socket { + fn from_inner((inner, local_addr): (FileDesc, Option)) -> Socket { + Socket { inner: Arc::new(inner), local_addr } } } From 084b0da933c2430e2ef80ae0e00ddf76e2bc5f10 Mon Sep 17 00:00:00 2001 From: Mohsen Zohrevandi Date: Thu, 12 Nov 2020 09:11:05 -0800 Subject: [PATCH 2/2] Add missing stability attribute --- library/std/src/sys/sgx/ext/io.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/std/src/sys/sgx/ext/io.rs b/library/std/src/sys/sgx/ext/io.rs index ffa926417e306..795a4d190cf4e 100644 --- a/library/std/src/sys/sgx/ext/io.rs +++ b/library/std/src/sys/sgx/ext/io.rs @@ -76,6 +76,7 @@ impl AsRawFd for net::TcpListener { /// Metadata for `TcpStream`. #[derive(Debug, Clone, Default)] +#[unstable(feature = "sgx_platform", issue = "56975")] pub struct TcpStreamMetadata { /// Local address of the TCP stream pub local_addr: Option, @@ -95,6 +96,7 @@ impl FromRawFd for net::TcpStream { /// Metadata for `TcpListener`. #[derive(Debug, Clone, Default)] +#[unstable(feature = "sgx_platform", issue = "56975")] pub struct TcpListenerMetadata { /// Local address of the TCP listener pub local_addr: Option,