Skip to content

Commit

Permalink
Auto merge of #107405 - hermitcore:bsd, r=bjorn3
Browse files Browse the repository at this point in the history
add support of RustyHermit's BSD socket layer

RustyHermit is a tier 3 platform and publishes a new kernel interface. The new version supports a common BSD socket layer. By supporting this interface, the implementation of `std` can be harmonized to other operating systems. In `sys_common/mod.rs` we remove only a special case for RustyHermit. All changes are done in the RustyHermit specific directories.

To realize this socket layer, the handling of file descriptors is also harmonized to other operating systems.
  • Loading branch information
bors committed Feb 25, 2023
2 parents 31448ba + af8ee64 commit 34e6673
Show file tree
Hide file tree
Showing 16 changed files with 594 additions and 478 deletions.
12 changes: 10 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1927,8 +1927,16 @@ version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7"
dependencies = [
"compiler_builtins",
"libc",
]

[[package]]
name = "hermit-abi"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "856b5cb0902c2b6d65d5fd97dfa30f9b70c7538e770b98eab5ed52d8db923e01"
dependencies = [
"compiler_builtins",
"rustc-std-workspace-alloc",
"rustc-std-workspace-core",
]
Expand Down Expand Up @@ -5294,7 +5302,7 @@ dependencies = [
"dlmalloc",
"fortanix-sgx-abi",
"hashbrown 0.12.3",
"hermit-abi 0.2.6",
"hermit-abi 0.3.0",
"libc",
"miniz_oxide",
"object 0.29.0",
Expand Down
2 changes: 1 addition & 1 deletion library/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dlmalloc = { version = "0.2.3", features = ['rustc-dep-of-std'] }
fortanix-sgx-abi = { version = "0.5.0", features = ['rustc-dep-of-std'] }

[target.'cfg(target_os = "hermit")'.dependencies]
hermit-abi = { version = "0.2.6", features = ['rustc-dep-of-std'] }
hermit-abi = { version = "0.3.0", features = ['rustc-dep-of-std'] }

[target.wasm32-wasi.dependencies]
wasi = { version = "0.11.0", features = ['rustc-dep-of-std'], default-features = false }
Expand Down
9 changes: 6 additions & 3 deletions library/std/src/os/fd/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::fs;
use crate::io;
use crate::marker::PhantomData;
use crate::mem::forget;
#[cfg(not(any(target_arch = "wasm32", target_env = "sgx")))]
#[cfg(not(any(target_arch = "wasm32", target_env = "sgx", target_os = "hermit")))]
use crate::sys::cvt;
use crate::sys_common::{AsInner, FromInner, IntoInner};

Expand Down Expand Up @@ -89,7 +89,7 @@ impl OwnedFd {
impl BorrowedFd<'_> {
/// Creates a new `OwnedFd` instance that shares the same underlying file
/// description as the existing `BorrowedFd` instance.
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(any(target_arch = "wasm32", target_os = "hermit")))]
#[stable(feature = "io_safety", since = "1.63.0")]
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
// We want to atomically duplicate this file descriptor and set the
Expand All @@ -112,7 +112,7 @@ impl BorrowedFd<'_> {

/// Creates a new `OwnedFd` instance that shares the same underlying file
/// description as the existing `BorrowedFd` instance.
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", target_os = "hermit"))]
#[stable(feature = "io_safety", since = "1.63.0")]
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
Err(crate::io::const_io_error!(
Expand Down Expand Up @@ -174,7 +174,10 @@ impl Drop for OwnedFd {
// the file descriptor was closed or not, and if we retried (for
// something like EINTR), we might close another valid file descriptor
// opened after we closed ours.
#[cfg(not(target_os = "hermit"))]
let _ = libc::close(self.fd);
#[cfg(target_os = "hermit")]
let _ = hermit_abi::close(self.fd);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions library/std/src/os/fd/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use crate::fs;
use crate::io;
#[cfg(target_os = "hermit")]
use crate::os::hermit::io::OwnedFd;
#[cfg(not(target_os = "hermit"))]
use crate::os::raw;
#[cfg(all(doc, not(target_arch = "wasm32")))]
use crate::os::unix::io::AsFd;
Expand All @@ -12,11 +15,18 @@ use crate::os::unix::io::OwnedFd;
#[cfg(target_os = "wasi")]
use crate::os::wasi::io::OwnedFd;
use crate::sys_common::{AsInner, IntoInner};
#[cfg(target_os = "hermit")]
use hermit_abi as libc;

/// Raw file descriptors.
#[rustc_allowed_through_unstable_modules]
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg(not(target_os = "hermit"))]
pub type RawFd = raw::c_int;
#[rustc_allowed_through_unstable_modules]
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg(target_os = "hermit")]
pub type RawFd = i32;

/// A trait to extract the raw file descriptor from an underlying object.
///
Expand Down
13 changes: 13 additions & 0 deletions library/std/src/os/hermit/io/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![stable(feature = "os_fd", since = "1.66.0")]

mod net;
#[path = "../../fd/owned.rs"]
mod owned;
#[path = "../../fd/raw.rs"]
mod raw;

// Export the types and traits for the public API.
#[stable(feature = "os_fd", since = "1.66.0")]
pub use owned::*;
#[stable(feature = "os_fd", since = "1.66.0")]
pub use raw::*;
46 changes: 46 additions & 0 deletions library/std/src/os/hermit/io/net.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::os::hermit::io::OwnedFd;
use crate::os::hermit::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use crate::sys_common::{self, AsInner, FromInner, IntoInner};
use crate::{net, sys};

macro_rules! impl_as_raw_fd {
($($t:ident)*) => {$(
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawFd for net::$t {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.as_inner().socket().as_raw_fd()
}
}
)*};
}
impl_as_raw_fd! { TcpStream TcpListener UdpSocket }

macro_rules! impl_from_raw_fd {
($($t:ident)*) => {$(
#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawFd for net::$t {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> net::$t {
unsafe {
let socket = sys::net::Socket::from_inner(FromInner::from_inner(OwnedFd::from_raw_fd(fd)));
net::$t::from_inner(sys_common::net::$t::from_inner(socket))
}
}
}
)*};
}
impl_from_raw_fd! { TcpStream TcpListener UdpSocket }

macro_rules! impl_into_raw_fd {
($($t:ident)*) => {$(
#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for net::$t {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_socket().into_inner().into_inner().into_raw_fd()
}
}
)*};
}
impl_into_raw_fd! { TcpStream TcpListener UdpSocket }
5 changes: 5 additions & 0 deletions library/std/src/os/hermit/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#![stable(feature = "rust1", since = "1.0.0")]

#[allow(unused_extern_crates)]
#[stable(feature = "rust1", since = "1.0.0")]
pub extern crate hermit_abi as abi;

pub mod ffi;
pub mod io;

/// A prelude for conveniently writing platform-specific code.
///
Expand Down
12 changes: 2 additions & 10 deletions library/std/src/os/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@ pub mod windows {}
all(target_vendor = "fortanix", target_env = "sgx")
)
)))]
#[cfg(target_os = "hermit")]
#[path = "hermit/mod.rs"]
pub mod unix;
#[cfg(not(all(
doc,
any(
all(target_arch = "wasm32", not(target_os = "wasi")),
all(target_vendor = "fortanix", target_env = "sgx")
)
)))]
#[cfg(all(not(target_os = "hermit"), any(unix, doc)))]
pub mod unix;

Expand Down Expand Up @@ -123,6 +113,8 @@ pub mod freebsd;
pub mod fuchsia;
#[cfg(target_os = "haiku")]
pub mod haiku;
#[cfg(target_os = "hermit")]
pub mod hermit;
#[cfg(target_os = "horizon")]
pub mod horizon;
#[cfg(target_os = "illumos")]
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/hermit/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ffi::{c_char, CStr, OsString};
use crate::fmt;
use crate::os::unix::ffi::OsStringExt;
use crate::os::hermit::ffi::OsStringExt;
use crate::ptr;
use crate::sync::atomic::{
AtomicIsize, AtomicPtr,
Expand Down
77 changes: 45 additions & 32 deletions library/std/src/sys/hermit/fd.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
#![unstable(reason = "not public", issue = "none", feature = "fd")]

use crate::io::{self, Read};
use crate::mem;
use crate::os::hermit::io::{FromRawFd, OwnedFd, RawFd};
use crate::sys::cvt;
use crate::sys::hermit::abi;
use crate::sys::unsupported;
use crate::sys_common::AsInner;
use crate::sys_common::{AsInner, FromInner, IntoInner};

use crate::os::hermit::io::*;

#[derive(Debug)]
pub struct FileDesc {
fd: i32,
fd: OwnedFd,
}

impl FileDesc {
pub fn new(fd: i32) -> FileDesc {
FileDesc { fd }
}

pub fn raw(&self) -> i32 {
self.fd
}

/// Extracts the actual file descriptor without closing it.
pub fn into_raw(self) -> i32 {
let fd = self.fd;
mem::forget(self);
fd
}

pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
let result = unsafe { abi::read(self.fd, buf.as_mut_ptr(), buf.len()) };
cvt(result as i32)
let result = cvt(unsafe { abi::read(self.fd.as_raw_fd(), buf.as_mut_ptr(), buf.len()) })?;
Ok(result as usize)
}

pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
Expand All @@ -39,8 +26,8 @@ impl FileDesc {
}

pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
let result = unsafe { abi::write(self.fd, buf.as_ptr(), buf.len()) };
cvt(result as i32)
let result = cvt(unsafe { abi::write(self.fd.as_raw_fd(), buf.as_ptr(), buf.len()) })?;
Ok(result as usize)
}

pub fn duplicate(&self) -> io::Result<FileDesc> {
Expand Down Expand Up @@ -69,19 +56,45 @@ impl<'a> Read for &'a FileDesc {
}
}

impl AsInner<i32> for FileDesc {
fn as_inner(&self) -> &i32 {
impl IntoInner<OwnedFd> for FileDesc {
fn into_inner(self) -> OwnedFd {
self.fd
}
}

impl FromInner<OwnedFd> for FileDesc {
fn from_inner(owned_fd: OwnedFd) -> Self {
Self { fd: owned_fd }
}
}

impl FromRawFd for FileDesc {
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
Self { fd: FromRawFd::from_raw_fd(raw_fd) }
}
}

impl AsInner<OwnedFd> for FileDesc {
fn as_inner(&self) -> &OwnedFd {
&self.fd
}
}

impl Drop for FileDesc {
fn drop(&mut self) {
// Note that errors are ignored when closing a file descriptor. The
// reason for this is that if an error occurs we don't actually know if
// the file descriptor was closed or not, and if we retried (for
// something like EINTR), we might close another valid file descriptor
// (opened after we closed ours.
let _ = unsafe { abi::close(self.fd) };
impl AsFd for FileDesc {
fn as_fd(&self) -> BorrowedFd<'_> {
self.fd.as_fd()
}
}

impl AsRawFd for FileDesc {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.fd.as_raw_fd()
}
}

impl IntoRawFd for FileDesc {
fn into_raw_fd(self) -> RawFd {
self.fd.into_raw_fd()
}
}
Loading

0 comments on commit 34e6673

Please sign in to comment.