Skip to content

Commit 2c6a12e

Browse files
committed
Auto merge of #136780 - joboet:move_pal_stdio, r=Amanieu
std: move stdio to `sys` 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.
2 parents c8a5072 + c52e059 commit 2c6a12e

File tree

29 files changed

+77
-150
lines changed

29 files changed

+77
-150
lines changed

library/std/src/sys/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub mod net;
1818
pub mod os_str;
1919
pub mod path;
2020
pub mod random;
21+
pub mod stdio;
2122
pub mod sync;
2223
pub mod thread_local;
2324

library/std/src/sys/pal/hermit/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ pub mod os;
2727
pub mod pipe;
2828
#[path = "../unsupported/process.rs"]
2929
pub mod process;
30-
pub mod stdio;
3130
pub mod thread;
3231
pub mod time;
3332

library/std/src/sys/pal/hermit/stdio.rs

-97
This file was deleted.

library/std/src/sys/pal/sgx/abi/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::sync::atomic::{AtomicUsize, Ordering};
66
use crate::io::Write;
77

88
// runtime features
9-
pub(super) mod panic;
9+
pub mod panic;
1010
mod reloc;
1111

1212
// library features

library/std/src/sys/pal/sgx/libunwind_integration.rs

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![cfg(not(test))]
55

66
use crate::sys::sync::RwLock;
7+
use crate::{slice, str};
78

89
// Verify that the byte pattern libunwind uses to initialize an RwLock is
910
// equivalent to the value of RwLock::new(). If the value changes,
@@ -44,3 +45,14 @@ pub unsafe extern "C" fn __rust_rwlock_unlock(p: *mut RwLock) -> i32 {
4445
unsafe { (*p).write_unlock() };
4546
return 0;
4647
}
48+
49+
#[unsafe(no_mangle)]
50+
pub unsafe extern "C" fn __rust_print_err(m: *mut u8, s: i32) {
51+
if s < 0 {
52+
return;
53+
}
54+
let buf = unsafe { slice::from_raw_parts(m as *const u8, s as _) };
55+
if let Ok(s) = str::from_utf8(&buf[..buf.iter().position(|&b| b == 0).unwrap_or(buf.len())]) {
56+
eprint!("{s}");
57+
}
58+
}

library/std/src/sys/pal/sgx/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub mod os;
1818
pub mod pipe;
1919
#[path = "../unsupported/process.rs"]
2020
pub mod process;
21-
pub mod stdio;
2221
pub mod thread;
2322
pub mod thread_parking;
2423
pub mod time;

library/std/src/sys/pal/solid/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ pub mod os;
2727
pub mod pipe;
2828
#[path = "../unsupported/process.rs"]
2929
pub mod process;
30-
pub mod stdio;
3130
pub use self::itron::{thread, thread_parking};
3231
pub mod time;
3332

library/std/src/sys/pal/teeos/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ pub mod os;
1616
pub mod pipe;
1717
#[path = "../unsupported/process.rs"]
1818
pub mod process;
19-
pub mod stdio;
2019
pub mod thread;
2120
#[allow(non_upper_case_globals)]
2221
#[path = "../unix/time.rs"]

library/std/src/sys/pal/uefi/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub mod os;
2020
#[path = "../unsupported/pipe.rs"]
2121
pub mod pipe;
2222
pub mod process;
23-
pub mod stdio;
2423
pub mod thread;
2524
pub mod time;
2625

library/std/src/sys/pal/unix/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub mod os;
1818
pub mod pipe;
1919
pub mod process;
2020
pub mod stack_overflow;
21-
pub mod stdio;
2221
pub mod sync;
2322
pub mod thread;
2423
pub mod thread_parking;

library/std/src/sys/pal/unsupported/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ pub mod env;
55
pub mod os;
66
pub mod pipe;
77
pub mod process;
8-
pub mod stdio;
98
pub mod thread;
109
pub mod time;
1110

library/std/src/sys/pal/wasi/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ pub mod os;
2525
pub mod pipe;
2626
#[path = "../unsupported/process.rs"]
2727
pub mod process;
28-
pub mod stdio;
2928
pub mod thread;
3029
pub mod time;
3130

library/std/src/sys/pal/wasip2/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ pub mod os;
2222
pub mod pipe;
2323
#[path = "../unsupported/process.rs"]
2424
pub mod process;
25-
#[path = "../wasi/stdio.rs"]
26-
pub mod stdio;
2725
#[path = "../wasi/thread.rs"]
2826
pub mod thread;
2927
#[path = "../wasi/time.rs"]

library/std/src/sys/pal/wasm/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ pub mod os;
2525
pub mod pipe;
2626
#[path = "../unsupported/process.rs"]
2727
pub mod process;
28-
#[path = "../unsupported/stdio.rs"]
29-
pub mod stdio;
3028
#[path = "../unsupported/time.rs"]
3129
pub mod time;
3230

library/std/src/sys/pal/windows/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pub mod handle;
2323
pub mod os;
2424
pub mod pipe;
2525
pub mod process;
26-
pub mod stdio;
2726
pub mod thread;
2827
pub mod time;
2928
cfg_if::cfg_if! {

library/std/src/sys/pal/xous/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub mod os;
88
pub mod pipe;
99
#[path = "../unsupported/process.rs"]
1010
pub mod process;
11-
pub mod stdio;
1211
pub mod thread;
1312
pub mod time;
1413

library/std/src/sys/pal/zkvm/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub mod os;
1919
pub mod pipe;
2020
#[path = "../unsupported/process.rs"]
2121
pub mod process;
22-
pub mod stdio;
2322
#[path = "../unsupported/thread.rs"]
2423
pub mod thread;
2524
#[path = "../unsupported/time.rs"]

library/std/src/sys/stdio/mod.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#![forbid(unsafe_op_in_unsafe_fn)]
2+
3+
cfg_if::cfg_if! {
4+
if #[cfg(any(
5+
target_family = "unix",
6+
target_os = "hermit"
7+
))] {
8+
mod unix;
9+
pub use unix::*;
10+
} else if #[cfg(target_os = "windows")] {
11+
mod windows;
12+
pub use windows::*;
13+
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
14+
mod sgx;
15+
pub use sgx::*;
16+
} else if #[cfg(target_os = "solid_asp3")] {
17+
mod solid;
18+
pub use solid::*;
19+
} else if #[cfg(target_os = "teeos")] {
20+
mod teeos;
21+
pub use teeos::*;
22+
} else if #[cfg(target_os = "uefi")] {
23+
mod uefi;
24+
pub use uefi::*;
25+
} else if #[cfg(target_os = "wasi")] {
26+
mod wasi;
27+
pub use wasi::*;
28+
} else if #[cfg(target_os = "xous")] {
29+
mod xous;
30+
pub use xous::*;
31+
} else if #[cfg(target_os = "zkvm")] {
32+
mod zkvm;
33+
pub use zkvm::*;
34+
} else {
35+
mod unsupported;
36+
pub use unsupported::*;
37+
}
38+
}

library/std/src/sys/pal/sgx/stdio.rs library/std/src/sys/stdio/sgx.rs

+1-19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
use fortanix_sgx_abi as abi;
22

33
use crate::io;
4-
#[cfg(not(test))]
5-
use crate::slice;
6-
#[cfg(not(test))]
7-
use crate::str;
84
use crate::sys::fd::FileDesc;
95

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

7268
pub fn panic_output() -> Option<impl io::Write> {
73-
super::abi::panic::SgxPanicOutput::new()
74-
}
75-
76-
// This function is needed by libunwind. The symbol is named in pre-link args
77-
// for the target specification, so keep that in sync.
78-
#[cfg(not(test))]
79-
#[unsafe(no_mangle)]
80-
pub unsafe extern "C" fn __rust_print_err(m: *mut u8, s: i32) {
81-
if s < 0 {
82-
return;
83-
}
84-
let buf = unsafe { slice::from_raw_parts(m as *const u8, s as _) };
85-
if let Ok(s) = str::from_utf8(&buf[..buf.iter().position(|&b| b == 0).unwrap_or(buf.len())]) {
86-
eprint!("{s}");
87-
}
69+
crate::sys::pal::abi::panic::SgxPanicOutput::new()
8870
}

library/std/src/sys/pal/solid/stdio.rs library/std/src/sys/stdio/solid.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use super::abi;
21
use crate::io;
2+
use crate::sys::pal::abi;
33

44
pub struct Stdin;
55
pub struct Stdout;
File renamed without changes.
File renamed without changes.

library/std/src/sys/pal/unix/stdio.rs library/std/src/sys/stdio/unix.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
1+
#[cfg(target_os = "hermit")]
2+
use hermit_abi::{EBADF, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
3+
#[cfg(target_family = "unix")]
4+
use libc::{EBADF, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
5+
6+
#[cfg(target_family = "unix")]
7+
use crate::io::BorrowedCursor;
8+
use crate::io::{self, IoSlice, IoSliceMut};
29
use crate::mem::ManuallyDrop;
10+
#[cfg(target_os = "hermit")]
11+
use crate::os::hermit::io::FromRawFd;
12+
#[cfg(target_family = "unix")]
313
use crate::os::unix::io::FromRawFd;
414
use crate::sys::fd::FileDesc;
515

@@ -15,15 +25,16 @@ impl Stdin {
1525

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

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

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

2940
#[inline]
@@ -40,13 +51,11 @@ impl Stdout {
4051

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

4657
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
47-
unsafe {
48-
ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDOUT_FILENO)).write_vectored(bufs)
49-
}
58+
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDOUT_FILENO)).write_vectored(bufs) }
5059
}
5160

5261
#[inline]
@@ -68,13 +77,11 @@ impl Stderr {
6877

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

7483
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
75-
unsafe {
76-
ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDERR_FILENO)).write_vectored(bufs)
77-
}
84+
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDERR_FILENO)).write_vectored(bufs) }
7885
}
7986

8087
#[inline]
@@ -89,7 +96,7 @@ impl io::Write for Stderr {
8996
}
9097

9198
pub fn is_ebadf(err: &io::Error) -> bool {
92-
err.raw_os_error() == Some(libc::EBADF as i32)
99+
err.raw_os_error() == Some(EBADF as i32)
93100
}
94101

95102
pub const STDIN_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE;

0 commit comments

Comments
 (0)