Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions library/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ dependencies = [
"rustc-std-workspace-core",
]

[[package]]
name = "moto-rt"
version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89b06cfad394d58f28e4c69ac064b4d0562b5c187cb982d67b44ec53f2acc64e"
dependencies = [
"rustc-std-workspace-alloc",
"rustc-std-workspace-core",
]

[[package]]
name = "object"
version = "0.37.3"
Expand Down Expand Up @@ -316,6 +326,7 @@ dependencies = [
"hermit-abi",
"libc",
"miniz_oxide",
"moto-rt",
"object",
"panic_abort",
"panic_unwind",
Expand Down
3 changes: 3 additions & 0 deletions library/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ fortanix-sgx-abi = { version = "0.6.1", features = [
'rustc-dep-of-std',
], public = true }

[target.'cfg(target_os = "motor")'.dependencies]
moto-rt = { version = "0.14", features = ['rustc-dep-of-std'], public = true }

[target.'cfg(target_os = "hermit")'.dependencies]
hermit-abi = { version = "0.5.0", features = [
'rustc-dep-of-std',
Expand Down
1 change: 1 addition & 0 deletions library/std/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn main() {
|| target_os == "windows"
|| target_os == "fuchsia"
|| (target_vendor == "fortanix" && target_env == "sgx")
|| target_os == "motor"
|| target_os == "hermit"
|| target_os == "trusty"
|| target_os == "l4re"
Expand Down
23 changes: 21 additions & 2 deletions library/std/src/os/fd/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#![stable(feature = "io_safety", since = "1.63.0")]
#![deny(unsafe_op_in_unsafe_fn)]

#[cfg(target_os = "motor")]
use moto_rt::libc;

use super::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(not(target_os = "trusty"))]
use crate::fs;
Expand All @@ -12,7 +15,8 @@ use crate::mem::ManuallyDrop;
target_arch = "wasm32",
target_env = "sgx",
target_os = "hermit",
target_os = "trusty"
target_os = "trusty",
target_os = "motor"
)))]
use crate::sys::cvt;
#[cfg(not(target_os = "trusty"))]
Expand Down Expand Up @@ -95,7 +99,12 @@ impl OwnedFd {
impl BorrowedFd<'_> {
/// Creates a new `OwnedFd` instance that shares the same underlying file
/// description as the existing `BorrowedFd` instance.
#[cfg(not(any(target_arch = "wasm32", target_os = "hermit", target_os = "trusty")))]
#[cfg(not(any(
target_arch = "wasm32",
target_os = "hermit",
target_os = "trusty",
target_os = "motor"
)))]
#[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 Down Expand Up @@ -123,6 +132,16 @@ impl BorrowedFd<'_> {
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
Err(crate::io::Error::UNSUPPORTED_PLATFORM)
}

/// Creates a new `OwnedFd` instance that shares the same underlying file
/// description as the existing `BorrowedFd` instance.
#[cfg(target_os = "motor")]
#[stable(feature = "io_safety", since = "1.63.0")]
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
let fd =
moto_rt::fs::duplicate(self.as_raw_fd()).map_err(crate::os::motor::map_motor_error)?;
Ok(unsafe { OwnedFd::from_raw_fd(fd) })
}
}

#[stable(feature = "io_safety", since = "1.63.0")]
Expand Down
10 changes: 7 additions & 3 deletions library/std/src/os/fd/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

#[cfg(target_os = "hermit")]
use hermit_abi as libc;
#[cfg(target_os = "motor")]
use moto_rt::libc;

#[cfg(target_os = "motor")]
use super::owned::OwnedFd;
#[cfg(not(target_os = "trusty"))]
use crate::fs;
use crate::io;
#[cfg(target_os = "hermit")]
use crate::os::hermit::io::OwnedFd;
#[cfg(not(target_os = "hermit"))]
#[cfg(all(not(target_os = "hermit"), not(target_os = "motor")))]
use crate::os::raw;
#[cfg(all(doc, not(target_arch = "wasm32")))]
use crate::os::unix::io::AsFd;
Expand All @@ -23,10 +27,10 @@ use crate::sys_common::{AsInner, FromInner, IntoInner};

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

/// A trait to extract the raw file descriptor from an underlying object.
Expand Down
11 changes: 10 additions & 1 deletion library/std/src/os/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ pub mod ios;
pub mod l4re;
#[cfg(target_os = "macos")]
pub mod macos;
#[cfg(target_os = "motor")]
pub mod motor;
#[cfg(target_os = "netbsd")]
pub mod netbsd;
#[cfg(target_os = "nto")]
Expand Down Expand Up @@ -182,7 +184,14 @@ pub mod vxworks;
#[cfg(target_os = "xous")]
pub mod xous;

#[cfg(any(unix, target_os = "hermit", target_os = "trusty", target_os = "wasi", doc))]
#[cfg(any(
unix,
target_os = "hermit",
target_os = "trusty",
target_os = "wasi",
target_os = "motor",
doc
))]
pub mod fd;

#[cfg(any(target_os = "linux", target_os = "android", target_os = "cygwin", doc))]
Expand Down
44 changes: 44 additions & 0 deletions library/std/src/os/motor/ffi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//! Motor OS-specific extensions to primitives in the [`std::ffi`] module.

#![stable(feature = "rust1", since = "1.0.0")]

use crate::ffi::{OsStr, OsString};
use crate::sealed::Sealed;

/// Motor OS-specific extensions to [`OsString`].
///
/// This trait is sealed: it cannot be implemented outside the standard library.
/// This is so that future additional methods are not breaking changes.
#[stable(feature = "rust1", since = "1.0.0")]
pub trait OsStringExt: Sealed {
/// Motor OS strings are utf-8, and thus just strings.
#[stable(feature = "rust1", since = "1.0.0")]
fn as_str(&self) -> &str;
}

#[stable(feature = "rust1", since = "1.0.0")]
impl OsStringExt for OsString {
#[inline]
fn as_str(&self) -> &str {
self.to_str().unwrap()
}
}

/// Motor OS-specific extensions to [`OsString`].
///
/// This trait is sealed: it cannot be implemented outside the standard library.
/// This is so that future additional methods are not breaking changes.
#[stable(feature = "rust1", since = "1.0.0")]
pub trait OsStrExt: Sealed {
/// Motor OS strings are utf-8, and thus just strings.
#[stable(feature = "rust1", since = "1.0.0")]
fn as_str(&self) -> &str;
}

#[stable(feature = "rust1", since = "1.0.0")]
impl OsStrExt for OsStr {
#[inline]
fn as_str(&self) -> &str {
self.to_str().unwrap()
}
}
27 changes: 27 additions & 0 deletions library/std/src/os/motor/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![unstable(feature = "motor_ext", issue = "none")]

#[unstable(feature = "motor_ext", issue = "none")]
pub fn map_motor_error(err: moto_rt::ErrorCode) -> crate::io::Error {
Comment on lines +3 to +4
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably doesn't need to be crate public?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If a Rust program calls into moto-sys (the Motor OS kernel API) or moto-rt (the Motor OS runtime API), they get moto_rt::ErrorCode. To convert it into io::Error (e.g. for Tokio) they need access to a similar function. So either the function should be exported here as pub, or duplicated elsewhere, from where std can't import (due to io::Error being in std). If io::Error is moved to core (which I've seen being discussed), I'll move this function to moto-rt.

I prefer to avoid code duplication and just export it here. If it is better to duplicate it elsewhere, I can do that also, let me know.

use moto_rt::error::*;

use crate::io::ErrorKind;

let kind: ErrorKind = match err {
E_ALREADY_IN_USE => ErrorKind::AlreadyExists,
E_INVALID_FILENAME => ErrorKind::InvalidFilename,
E_NOT_FOUND => ErrorKind::NotFound,
E_TIMED_OUT => ErrorKind::TimedOut,
E_NOT_IMPLEMENTED => ErrorKind::Unsupported,
E_FILE_TOO_LARGE => ErrorKind::FileTooLarge,
E_UNEXPECTED_EOF => ErrorKind::UnexpectedEof,
E_INVALID_ARGUMENT => ErrorKind::InvalidInput,
E_NOT_READY => ErrorKind::WouldBlock,
E_NOT_CONNECTED => ErrorKind::NotConnected,
_ => ErrorKind::Other,
};

crate::io::Error::from(kind)
}

pub mod ffi;
pub mod process;
16 changes: 16 additions & 0 deletions library/std/src/os/motor/process.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::sealed::Sealed;
use crate::sys_common::AsInner;

#[unstable(feature = "motor_ext", issue = "none")]
pub trait ChildExt: Sealed {
/// Extracts the main thread raw handle, without taking ownership
#[unstable(feature = "motor_ext", issue = "none")]
fn sys_handle(&self) -> u64;
}

#[unstable(feature = "motor_ext", issue = "none")]
impl ChildExt for crate::process::Child {
fn sys_handle(&self) -> u64 {
self.as_inner().handle()
}
}
3 changes: 3 additions & 0 deletions library/std/src/sys/alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ cfg_select! {
target_os = "hermit" => {
mod hermit;
}
target_os = "motor" => {
mod motor;
}
all(target_vendor = "fortanix", target_env = "sgx") => {
mod sgx;
}
Expand Down
28 changes: 28 additions & 0 deletions library/std/src/sys/alloc/motor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::alloc::{GlobalAlloc, Layout, System};

#[stable(feature = "alloc_system_type", since = "1.28.0")]
unsafe impl GlobalAlloc for System {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
// Safety: same requirements as in GlobalAlloc::alloc.
moto_rt::alloc::alloc(layout)
}

#[inline]
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
// Safety: same requirements as in GlobalAlloc::alloc_zeroed.
moto_rt::alloc::alloc_zeroed(layout)
}

#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
// Safety: same requirements as in GlobalAlloc::dealloc.
unsafe { moto_rt::alloc::dealloc(ptr, layout) }
}

#[inline]
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
// Safety: same requirements as in GlobalAlloc::realloc.
unsafe { moto_rt::alloc::realloc(ptr, layout, new_size) }
}
}
4 changes: 4 additions & 0 deletions library/std/src/sys/anonymous_pipe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ cfg_select! {
mod windows;
pub use windows::{AnonPipe, pipe};
}
target_os = "motor" => {
mod motor;
pub use motor::{AnonPipe, pipe};
}
_ => {
mod unsupported;
pub use unsupported::{AnonPipe, pipe};
Expand Down
11 changes: 11 additions & 0 deletions library/std/src/sys/anonymous_pipe/motor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::io;
use crate::sys::fd::FileDesc;
use crate::sys::pipe::anon_pipe;
use crate::sys_common::IntoInner;

pub type AnonPipe = FileDesc;

#[inline]
pub fn pipe() -> io::Result<(AnonPipe, AnonPipe)> {
anon_pipe().map(|(rx, wx)| (rx.into_inner(), wx.into_inner()))
}
5 changes: 5 additions & 0 deletions library/std/src/sys/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
all(target_family = "unix", not(any(target_os = "espidf", target_os = "vita"))),
target_family = "windows",
target_os = "hermit",
target_os = "motor",
target_os = "uefi",
target_os = "wasi",
target_os = "xous",
Expand All @@ -28,6 +29,10 @@ cfg_select! {
mod sgx;
pub use sgx::*;
}
target_os = "motor" => {
mod motor;
pub use motor::*;
}
target_os = "uefi" => {
mod uefi;
pub use uefi::*;
Expand Down
13 changes: 13 additions & 0 deletions library/std/src/sys/args/motor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub use super::common::Args;
use crate::ffi::OsString;

pub fn args() -> Args {
let motor_args: Vec<String> = moto_rt::process::args();
let mut rust_args = alloc::vec::Vec::new();

for arg in motor_args {
rust_args.push(OsString::from(arg));
}

Args::new(rust_args)
}
5 changes: 5 additions & 0 deletions library/std/src/sys/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#[cfg(any(
target_family = "unix",
target_os = "hermit",
target_os = "motor",
all(target_vendor = "fortanix", target_env = "sgx"),
target_os = "solid_asp3",
target_os = "uefi",
Expand All @@ -26,6 +27,10 @@ cfg_select! {
mod hermit;
pub use hermit::*;
}
target_os = "motor" => {
mod motor;
pub use motor::*;
}
all(target_vendor = "fortanix", target_env = "sgx") => {
mod sgx;
pub use sgx::*;
Expand Down
27 changes: 27 additions & 0 deletions library/std/src/sys/env/motor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pub use super::common::Env;
use crate::ffi::{OsStr, OsString};
use crate::io;
use crate::os::motor::ffi::OsStrExt;

pub fn env() -> Env {
let motor_env: Vec<(String, String)> = moto_rt::process::env();
let mut rust_env = vec![];

for (k, v) in motor_env {
rust_env.push((OsString::from(k), OsString::from(v)));
}

Env::new(rust_env)
}

pub fn getenv(key: &OsStr) -> Option<OsString> {
moto_rt::process::getenv(key.as_str()).map(|s| OsString::from(s))
}

pub unsafe fn setenv(key: &OsStr, val: &OsStr) -> io::Result<()> {
Ok(moto_rt::process::setenv(key.as_str(), val.as_str()))
}

pub unsafe fn unsetenv(key: &OsStr) -> io::Result<()> {
Ok(moto_rt::process::unsetenv(key.as_str()))
}
4 changes: 4 additions & 0 deletions library/std/src/sys/fd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ cfg_select! {
mod hermit;
pub use hermit::*;
}
target_os = "motor" => {
mod motor;
pub use motor::*;
}
all(target_vendor = "fortanix", target_env = "sgx") => {
mod sgx;
pub use sgx::*;
Expand Down
Loading
Loading