Skip to content

Commit

Permalink
std: move stdio to sys
Browse files Browse the repository at this point in the history
As per #117276, this moves the platform definitions of `Stdout` and friends into `sys`. This PR also unifies the UNIX and Hermit implementations and moves the `__rust_print_err` function needed by libunwind on SGX into the dedicated module for such helper functions.
  • Loading branch information
joboet committed Feb 17, 2025
1 parent 273465e commit a1a2a46
Show file tree
Hide file tree
Showing 29 changed files with 77 additions and 150 deletions.
1 change: 1 addition & 0 deletions library/std/src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod net;
pub mod os_str;
pub mod path;
pub mod random;
pub mod stdio;
pub mod sync;
pub mod thread_local;

Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/pal/hermit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pub mod os;
pub mod pipe;
#[path = "../unsupported/process.rs"]
pub mod process;
pub mod stdio;
pub mod thread;
pub mod time;

Expand Down
97 changes: 0 additions & 97 deletions library/std/src/sys/pal/hermit/stdio.rs

This file was deleted.

2 changes: 1 addition & 1 deletion library/std/src/sys/pal/sgx/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::sync::atomic::{AtomicUsize, Ordering};
use crate::io::Write;

// runtime features
pub(super) mod panic;
pub mod panic;
mod reloc;

// library features
Expand Down
12 changes: 12 additions & 0 deletions library/std/src/sys/pal/sgx/libunwind_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![cfg(not(test))]

use crate::sys::sync::RwLock;
use crate::{slice, str};

// Verify that the byte pattern libunwind uses to initialize an RwLock is
// equivalent to the value of RwLock::new(). If the value changes,
Expand Down Expand Up @@ -44,3 +45,14 @@ pub unsafe extern "C" fn __rust_rwlock_unlock(p: *mut RwLock) -> i32 {
unsafe { (*p).write_unlock() };
return 0;
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn __rust_print_err(m: *mut u8, s: i32) {
if s < 0 {
return;
}
let buf = unsafe { slice::from_raw_parts(m as *const u8, s as _) };
if let Ok(s) = str::from_utf8(&buf[..buf.iter().position(|&b| b == 0).unwrap_or(buf.len())]) {
eprint!("{s}");
}
}
1 change: 0 additions & 1 deletion library/std/src/sys/pal/sgx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub mod os;
pub mod pipe;
#[path = "../unsupported/process.rs"]
pub mod process;
pub mod stdio;
pub mod thread;
pub mod thread_parking;
pub mod time;
Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/pal/solid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pub mod os;
pub mod pipe;
#[path = "../unsupported/process.rs"]
pub mod process;
pub mod stdio;
pub use self::itron::{thread, thread_parking};
pub mod time;

Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/pal/teeos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub mod os;
pub mod pipe;
#[path = "../unsupported/process.rs"]
pub mod process;
pub mod stdio;
pub mod thread;
#[allow(non_upper_case_globals)]
#[path = "../unix/time.rs"]
Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/pal/uefi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub mod os;
#[path = "../unsupported/pipe.rs"]
pub mod pipe;
pub mod process;
pub mod stdio;
pub mod thread;
pub mod time;

Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/pal/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pub mod os;
pub mod pipe;
pub mod process;
pub mod stack_overflow;
pub mod stdio;
pub mod sync;
pub mod thread;
pub mod thread_parking;
Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/pal/unsupported/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub mod fs;
pub mod os;
pub mod pipe;
pub mod process;
pub mod stdio;
pub mod thread;
pub mod time;

Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/pal/wasi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub mod os;
pub mod pipe;
#[path = "../unsupported/process.rs"]
pub mod process;
pub mod stdio;
pub mod thread;
pub mod time;

Expand Down
2 changes: 0 additions & 2 deletions library/std/src/sys/pal/wasip2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ pub mod os;
pub mod pipe;
#[path = "../unsupported/process.rs"]
pub mod process;
#[path = "../wasi/stdio.rs"]
pub mod stdio;
#[path = "../wasi/thread.rs"]
pub mod thread;
#[path = "../wasi/time.rs"]
Expand Down
2 changes: 0 additions & 2 deletions library/std/src/sys/pal/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ pub mod os;
pub mod pipe;
#[path = "../unsupported/process.rs"]
pub mod process;
#[path = "../unsupported/stdio.rs"]
pub mod stdio;
#[path = "../unsupported/time.rs"]
pub mod time;

Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/pal/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub mod handle;
pub mod os;
pub mod pipe;
pub mod process;
pub mod stdio;
pub mod thread;
pub mod time;
cfg_if::cfg_if! {
Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/pal/xous/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub mod os;
pub mod pipe;
#[path = "../unsupported/process.rs"]
pub mod process;
pub mod stdio;
pub mod thread;
pub mod time;

Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/pal/zkvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub mod os;
pub mod pipe;
#[path = "../unsupported/process.rs"]
pub mod process;
pub mod stdio;
#[path = "../unsupported/thread.rs"]
pub mod thread;
#[path = "../unsupported/time.rs"]
Expand Down
38 changes: 38 additions & 0 deletions library/std/src/sys/stdio/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#![forbid(unsafe_op_in_unsafe_fn)]

cfg_if::cfg_if! {
if #[cfg(any(
target_family = "unix",
target_os = "hermit"
))] {
mod unix_like;
pub use unix_like::*;
} else if #[cfg(target_os = "windows")] {
mod windows;
pub use windows::*;
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
mod sgx;
pub use sgx::*;
} else if #[cfg(target_os = "solid_asp3")] {
mod solid;
pub use solid::*;
} else if #[cfg(target_os = "teeos")] {
mod teeos;
pub use teeos::*;
} else if #[cfg(target_os = "uefi")] {
mod uefi;
pub use uefi::*;
} else if #[cfg(target_os = "wasi")] {
mod wasi;
pub use wasi::*;
} else if #[cfg(target_os = "xous")] {
mod xous;
pub use xous::*;
} else if #[cfg(target_os = "zkvm")] {
mod zkvm;
pub use zkvm::*;
} else {
mod unsupported;
pub use unsupported::*;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use fortanix_sgx_abi as abi;

use crate::io;
#[cfg(not(test))]
use crate::slice;
#[cfg(not(test))]
use crate::str;
use crate::sys::fd::FileDesc;

pub struct Stdin(());
Expand Down Expand Up @@ -70,19 +66,5 @@ pub fn is_ebadf(err: &io::Error) -> bool {
}

pub fn panic_output() -> Option<impl io::Write> {
super::abi::panic::SgxPanicOutput::new()
}

// This function is needed by libunwind. The symbol is named in pre-link args
// for the target specification, so keep that in sync.
#[cfg(not(test))]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn __rust_print_err(m: *mut u8, s: i32) {
if s < 0 {
return;
}
let buf = unsafe { slice::from_raw_parts(m as *const u8, s as _) };
if let Ok(s) = str::from_utf8(&buf[..buf.iter().position(|&b| b == 0).unwrap_or(buf.len())]) {
eprint!("{s}");
}
crate::sys::pal::abi::panic::SgxPanicOutput::new()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::abi;
use crate::io;
use crate::sys::pal::abi;

pub struct Stdin;
pub struct Stdout;
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
#[cfg(target_os = "hermit")]
use hermit_abi::{EBADF, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
#[cfg(target_family = "unix")]
use libc::{EBADF, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};

#[cfg(target_family = "unix")]
use crate::io::BorrowedCursor;
use crate::io::{self, IoSlice, IoSliceMut};
use crate::mem::ManuallyDrop;
#[cfg(target_os = "hermit")]
use crate::os::hermit::io::FromRawFd;
#[cfg(target_family = "unix")]
use crate::os::unix::io::FromRawFd;
use crate::sys::fd::FileDesc;

Expand All @@ -15,15 +25,16 @@ impl Stdin {

impl io::Read for Stdin {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDIN_FILENO)).read(buf) }
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDIN_FILENO)).read(buf) }
}

#[cfg(not(target_os = "hermit"))]
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> io::Result<()> {
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDIN_FILENO)).read_buf(buf) }
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDIN_FILENO)).read_buf(buf) }
}

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDIN_FILENO)).read_vectored(bufs) }
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDIN_FILENO)).read_vectored(bufs) }
}

#[inline]
Expand All @@ -40,13 +51,11 @@ impl Stdout {

impl io::Write for Stdout {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDOUT_FILENO)).write(buf) }
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDOUT_FILENO)).write(buf) }
}

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
unsafe {
ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDOUT_FILENO)).write_vectored(bufs)
}
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDOUT_FILENO)).write_vectored(bufs) }
}

#[inline]
Expand All @@ -68,13 +77,11 @@ impl Stderr {

impl io::Write for Stderr {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDERR_FILENO)).write(buf) }
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDERR_FILENO)).write(buf) }
}

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
unsafe {
ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDERR_FILENO)).write_vectored(bufs)
}
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDERR_FILENO)).write_vectored(bufs) }
}

#[inline]
Expand All @@ -89,7 +96,7 @@ impl io::Write for Stderr {
}

pub fn is_ebadf(err: &io::Error) -> bool {
err.raw_os_error() == Some(libc::EBADF as i32)
err.raw_os_error() == Some(EBADF as i32)
}

pub const STDIN_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE;
Expand Down
File renamed without changes.
Loading

0 comments on commit a1a2a46

Please sign in to comment.