Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Metadata in std::os::fortanix_sgx::io::FromRawFd #78953

Merged
merged 2 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions library/std/src/sys/sgx/ext/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -71,18 +74,38 @@ impl AsRawFd for net::TcpListener {
}
}

/// Metadata for `TcpStream`.
#[derive(Debug, Clone, Default)]
pub struct TcpStreamMetadata {
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing stability attribute

/// Local address of the TCP stream
pub local_addr: Option<String>,
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't it be SocketAddr instead of String?

Copy link
Contributor

Choose a reason for hiding this comment

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

/// Peer address of the TCP stream
pub peer_addr: Option<String>,
}

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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing stability attribute

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

/// Local address of the TCP listener
pub local_addr: Option<String>,
}

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))
}
}
Expand Down
6 changes: 3 additions & 3 deletions library/std/src/sys/sgx/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ impl TryIntoInner<FileDesc> for Socket {
}
}

impl FromInner<FileDesc> for Socket {
fn from_inner(inner: FileDesc) -> Socket {
Socket { inner: Arc::new(inner), local_addr: None }
impl FromInner<(FileDesc, Option<String>)> for Socket {
fn from_inner((inner, local_addr): (FileDesc, Option<String>)) -> Socket {
Socket { inner: Arc::new(inner), local_addr }
}
}

Expand Down