Skip to content
Merged
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
2 changes: 1 addition & 1 deletion library/std/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2464,7 +2464,7 @@ impl Child {
#[cfg_attr(not(test), rustc_diagnostic_item = "process_exit")]
pub fn exit(code: i32) -> ! {
crate::rt::cleanup();
crate::sys::os::exit(code)
crate::sys::exit::exit(code)
}

/// Terminates the process in an abnormal fashion.
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ fn lang_start_internal(
cleanup();
// Guard against multiple threads calling `libc::exit` concurrently.
// See the documentation for `unique_thread_exit` for more information.
crate::sys::exit_guard::unique_thread_exit();
crate::sys::exit::unique_thread_exit();

ret_code
})
Expand Down
81 changes: 77 additions & 4 deletions library/std/src/sys/exit_guard.rs → library/std/src/sys/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ cfg_select! {
/// * If it is called again on the same thread as the first call, it will abort.
/// * If it is called again on a different thread, it will wait in a loop
/// (waiting for the process to exit).
#[cfg_attr(any(test, doctest), allow(dead_code))]
pub(crate) fn unique_thread_exit() {
pub fn unique_thread_exit() {
use crate::ffi::c_int;
use crate::ptr;
use crate::sync::atomic::AtomicPtr;
Expand Down Expand Up @@ -62,9 +61,83 @@ cfg_select! {
///
/// Mitigation is ***NOT*** implemented on this platform, either because this platform
/// is not affected, or because mitigation is not yet implemented for this platform.
#[cfg_attr(any(test, doctest), allow(dead_code))]
pub(crate) fn unique_thread_exit() {
#[cfg_attr(any(test, doctest), expect(dead_code))]
pub fn unique_thread_exit() {
// Mitigation not required on platforms where `exit` is thread-safe.
}
}
}

pub fn exit(code: i32) -> ! {
cfg_select! {
target_os = "hermit" => {
unsafe { hermit_abi::exit(code) }
}
target_os = "linux" => {
unsafe {
unique_thread_exit();
libc::exit(code)
}
}
target_os = "motor" => {
moto_rt::process::exit(code)
}
all(target_vendor = "fortanix", target_env = "sgx") => {
crate::sys::pal::abi::exit_with_code(code as _)
}
target_os = "solid_asp3" => {
rtabort!("exit({}) called", code)
}
target_os = "teeos" => {
let _ = code;
panic!("TA should not call `exit`")
}
target_os = "uefi" => {
use r_efi::base::Status;

use crate::os::uefi::env;

if let (Some(boot_services), Some(handle)) =
(env::boot_services(), env::try_image_handle())
{
let boot_services = boot_services.cast::<r_efi::efi::BootServices>();
let _ = unsafe {
((*boot_services.as_ptr()).exit)(
handle.as_ptr(),
Status::from_usize(code as usize),
0,
crate::ptr::null_mut(),
)
};
}
crate::intrinsics::abort()
}
any(
target_family = "unix",
target_os = "wasi",
) => {
unsafe { libc::exit(code as crate::ffi::c_int) }
}
target_os = "vexos" => {
let _ = code;

unsafe {
vex_sdk::vexSystemExitRequest();

loop {
vex_sdk::vexTasksRun();
}
}
}
target_os = "windows" => {
unsafe { crate::sys::pal::c::ExitProcess(code as u32) }
}
target_os = "xous" => {
crate::os::xous::ffi::exit(code as u32)
}
_ => {
let _ = code;
crate::intrinsics::abort()
}
}
}
2 changes: 1 addition & 1 deletion library/std/src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod backtrace;
pub mod cmath;
pub mod env;
pub mod env_consts;
pub mod exit_guard;
pub mod exit;
pub mod fd;
pub mod fs;
pub mod io;
Expand Down
4 changes: 0 additions & 4 deletions library/std/src/sys/pal/hermit/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ pub fn home_dir() -> Option<PathBuf> {
None
}

pub fn exit(code: i32) -> ! {
unsafe { hermit_abi::exit(code) }
}

pub fn getpid() -> u32 {
unsafe { hermit_abi::getpid() as u32 }
}
4 changes: 0 additions & 4 deletions library/std/src/sys/pal/motor/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ pub fn home_dir() -> Option<PathBuf> {
None
}

pub fn exit(code: i32) -> ! {
moto_rt::process::exit(code)
}

pub fn getpid() -> u32 {
panic!("Pids on Motor OS are u64.")
}
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 @@ -96,7 +96,7 @@ extern "C" fn entry(p1: u64, p2: u64, p3: u64, secondary: bool, p4: u64, p5: u64
}
}

pub(super) fn exit_with_code(code: isize) -> ! {
pub fn exit_with_code(code: isize) -> ! {
if code != 0 {
if let Some(mut out) = panic::SgxPanicOutput::new() {
let _ = write!(out, "Exited with status code {code}");
Expand Down
4 changes: 0 additions & 4 deletions library/std/src/sys/pal/sgx/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ pub fn home_dir() -> Option<PathBuf> {
None
}

pub fn exit(code: i32) -> ! {
super::abi::exit_with_code(code as _)
}

pub fn getpid() -> u32 {
panic!("no pids in SGX")
}
4 changes: 0 additions & 4 deletions library/std/src/sys/pal/solid/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ pub fn home_dir() -> Option<PathBuf> {
None
}

pub fn exit(code: i32) -> ! {
rtabort!("exit({}) called", code);
}

pub fn getpid() -> u32 {
panic!("no pids on this platform")
}
4 changes: 0 additions & 4 deletions library/std/src/sys/pal/teeos/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ pub fn home_dir() -> Option<PathBuf> {
None
}

pub fn exit(_code: i32) -> ! {
panic!("TA should not call `exit`")
}

pub fn getpid() -> u32 {
panic!("no pids on this platform")
}
20 changes: 0 additions & 20 deletions library/std/src/sys/pal/uefi/os.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use r_efi::efi::Status;
use r_efi::efi::protocols::{device_path, loaded_image_device_path};

use super::{helpers, unsupported_err};
use crate::ffi::{OsStr, OsString};
use crate::marker::PhantomData;
use crate::os::uefi;
use crate::os::uefi::ffi::{OsStrExt, OsStringExt};
use crate::path::{self, PathBuf};
use crate::ptr::NonNull;
use crate::{fmt, io};

const PATHS_SEP: u16 = b';' as u16;
Expand Down Expand Up @@ -105,23 +102,6 @@ pub fn home_dir() -> Option<PathBuf> {
None
}

pub fn exit(code: i32) -> ! {
if let (Some(boot_services), Some(handle)) =
(uefi::env::boot_services(), uefi::env::try_image_handle())
{
let boot_services: NonNull<r_efi::efi::BootServices> = boot_services.cast();
let _ = unsafe {
((*boot_services.as_ptr()).exit)(
handle.as_ptr(),
Status::from_usize(code as usize),
0,
crate::ptr::null_mut(),
)
};
}
crate::intrinsics::abort()
}

pub fn getpid() -> u32 {
panic!("no pids on this platform")
}
5 changes: 0 additions & 5 deletions library/std/src/sys/pal/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,11 +533,6 @@ pub fn home_dir() -> Option<PathBuf> {
}
}

pub fn exit(code: i32) -> ! {
crate::sys::exit_guard::unique_thread_exit();
unsafe { libc::exit(code as c_int) }
}

pub fn getpid() -> u32 {
unsafe { libc::getpid() as u32 }
}
Expand Down
4 changes: 0 additions & 4 deletions library/std/src/sys/pal/unsupported/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ pub fn home_dir() -> Option<PathBuf> {
None
}

pub fn exit(_code: i32) -> ! {
crate::intrinsics::abort()
}

pub fn getpid() -> u32 {
panic!("no pids on this platform")
}
1 change: 1 addition & 0 deletions library/std/src/sys/pal/vexos/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[path = "../unsupported/os.rs"]
pub mod os;

#[expect(dead_code)]
Expand Down
19 changes: 0 additions & 19 deletions library/std/src/sys/pal/vexos/os.rs

This file was deleted.

4 changes: 0 additions & 4 deletions library/std/src/sys/pal/wasi/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ pub fn home_dir() -> Option<PathBuf> {
None
}

pub fn exit(code: i32) -> ! {
unsafe { libc::exit(code) }
}

pub fn getpid() -> u32 {
panic!("unsupported");
}
Expand Down
4 changes: 0 additions & 4 deletions library/std/src/sys/pal/windows/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,6 @@ pub fn home_dir() -> Option<PathBuf> {
.or_else(home_dir_crt)
}

pub fn exit(code: i32) -> ! {
unsafe { c::ExitProcess(code as u32) }
}

pub fn getpid() -> u32 {
unsafe { c::GetCurrentProcessId() }
}
4 changes: 0 additions & 4 deletions library/std/src/sys/pal/xous/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ pub fn home_dir() -> Option<PathBuf> {
None
}

pub fn exit(code: i32) -> ! {
crate::os::xous::ffi::exit(code as u32);
}

pub fn getpid() -> u32 {
panic!("no pids on this platform")
}
4 changes: 0 additions & 4 deletions library/std/src/sys/pal/zkvm/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ pub fn home_dir() -> Option<PathBuf> {
None
}

pub fn exit(_code: i32) -> ! {
crate::intrinsics::abort()
}

pub fn getpid() -> u32 {
panic!("no pids on this platform")
}
Loading