2015-09-08 22:53:46 +00:00
|
|
|
//! Runtime services
|
|
|
|
//!
|
|
|
|
//! The `rt` module provides a narrow set of runtime services,
|
|
|
|
//! including the global heap (exported in `heap`) and unwinding and
|
|
|
|
//! backtrace support. The APIs in this module are highly unstable,
|
|
|
|
//! and should be considered as private implementation details for the
|
|
|
|
//! time being.
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
#![unstable(
|
|
|
|
feature = "rt",
|
|
|
|
reason = "this public module should not exist and is highly likely \
|
|
|
|
to disappear",
|
2019-12-21 11:16:18 +00:00
|
|
|
issue = "none"
|
2019-11-27 18:29:00 +00:00
|
|
|
)]
|
2015-09-08 22:53:46 +00:00
|
|
|
#![doc(hidden)]
|
2021-09-16 12:28:21 +00:00
|
|
|
#![deny(unsafe_op_in_unsafe_fn)]
|
|
|
|
#![allow(unused_macros)]
|
2015-09-08 22:53:46 +00:00
|
|
|
|
2024-07-19 01:02:59 +00:00
|
|
|
#[rustfmt::skip]
|
2021-09-11 02:44:02 +00:00
|
|
|
pub use crate::panicking::{begin_panic, panic_count};
|
|
|
|
pub use core::panicking::{panic_display, panic_fmt};
|
2015-09-08 22:53:46 +00:00
|
|
|
|
2024-07-19 01:02:59 +00:00
|
|
|
#[rustfmt::skip]
|
2024-07-18 11:59:48 +00:00
|
|
|
use crate::any::Any;
|
2021-09-16 12:28:21 +00:00
|
|
|
use crate::sync::Once;
|
2024-11-25 12:01:35 +00:00
|
|
|
use crate::thread::{self, main_thread};
|
2024-07-18 11:59:48 +00:00
|
|
|
use crate::{mem, panic, sys};
|
2021-09-16 12:28:21 +00:00
|
|
|
|
|
|
|
// Prints to the "panic output", depending on the platform this may be:
|
|
|
|
// - the standard error output
|
|
|
|
// - some dedicated platform specific output
|
|
|
|
// - nothing (so this macro is a no-op)
|
|
|
|
macro_rules! rtprintpanic {
|
|
|
|
($($t:tt)*) => {
|
2025-01-13 18:00:57 +00:00
|
|
|
#[cfg(not(feature = "panic_immediate_abort"))]
|
2021-09-16 12:28:21 +00:00
|
|
|
if let Some(mut out) = crate::sys::stdio::panic_output() {
|
|
|
|
let _ = crate::io::Write::write_fmt(&mut out, format_args!($($t)*));
|
|
|
|
}
|
2025-01-13 18:00:57 +00:00
|
|
|
#[cfg(feature = "panic_immediate_abort")]
|
|
|
|
{
|
|
|
|
let _ = format_args!($($t)*);
|
|
|
|
}
|
2021-09-16 12:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! rtabort {
|
|
|
|
($($t:tt)*) => {
|
|
|
|
{
|
|
|
|
rtprintpanic!("fatal runtime error: {}\n", format_args!($($t)*));
|
|
|
|
crate::sys::abort_internal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! rtassert {
|
|
|
|
($e:expr) => {
|
|
|
|
if !$e {
|
|
|
|
rtabort!(concat!("assertion failed: ", stringify!($e)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! rtunwrap {
|
|
|
|
($ok:ident, $e:expr) => {
|
|
|
|
match $e {
|
|
|
|
$ok(v) => v,
|
|
|
|
ref err => {
|
|
|
|
let err = err.as_ref().map(drop); // map Ok/Some which might not be Debug
|
|
|
|
rtabort!(concat!("unwrap failed: ", stringify!($e), " = {:?}"), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2025-01-11 09:35:00 +00:00
|
|
|
fn handle_rt_panic<T>(e: Box<dyn Any + Send>) -> T {
|
2024-07-18 11:59:48 +00:00
|
|
|
mem::forget(e);
|
|
|
|
rtabort!("initialization or cleanup bug");
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:20:44 +00:00
|
|
|
// One-time runtime initialization.
|
|
|
|
// Runs before `main`.
|
|
|
|
// SAFETY: must be called only once during runtime initialization.
|
|
|
|
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
|
2022-09-01 04:43:03 +00:00
|
|
|
//
|
|
|
|
// # The `sigpipe` parameter
|
|
|
|
//
|
|
|
|
// Since 2014, the Rust runtime on Unix has set the `SIGPIPE` handler to
|
|
|
|
// `SIG_IGN`. Applications have good reasons to want a different behavior
|
2024-04-28 16:02:21 +00:00
|
|
|
// though, so there is a `-Zon-broken-pipe` compiler flag that
|
2022-09-01 04:43:03 +00:00
|
|
|
// can be used to select how `SIGPIPE` shall be setup (if changed at all) before
|
|
|
|
// `fn main()` is called. See <https://github.com/rust-lang/rust/issues/97889>
|
|
|
|
// for more info.
|
|
|
|
//
|
|
|
|
// The `sigpipe` parameter to this function gets its value via the code that
|
|
|
|
// rustc generates to invoke `fn lang_start()`. The reason we have `sigpipe` for
|
|
|
|
// all platforms and not only Unix, is because std is not allowed to have `cfg`
|
|
|
|
// directives as this high level. See the module docs in
|
|
|
|
// `src/tools/tidy/src/pal.rs` for more info. On all other platforms, `sigpipe`
|
|
|
|
// has a value, but its value is ignored.
|
|
|
|
//
|
2022-09-23 05:48:14 +00:00
|
|
|
// Even though it is an `u8`, it only ever has 4 values. These are documented in
|
2022-09-01 04:43:03 +00:00
|
|
|
// `compiler/rustc_session/src/config/sigpipe.rs`.
|
2021-09-16 13:20:44 +00:00
|
|
|
#[cfg_attr(test, allow(dead_code))]
|
2022-07-05 17:56:22 +00:00
|
|
|
unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
|
2024-07-14 18:43:20 +00:00
|
|
|
#[cfg_attr(target_os = "teeos", allow(unused_unsafe))]
|
2021-09-16 13:20:44 +00:00
|
|
|
unsafe {
|
2024-07-14 18:43:20 +00:00
|
|
|
sys::init(argc, argv, sigpipe)
|
|
|
|
};
|
2021-09-16 13:20:44 +00:00
|
|
|
|
2024-11-25 12:01:35 +00:00
|
|
|
// Remember the main thread ID to give it the correct name.
|
|
|
|
// SAFETY: this is the only time and place where we call this function.
|
|
|
|
unsafe { main_thread::set(thread::current_id()) };
|
2021-09-16 13:20:44 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 11:59:48 +00:00
|
|
|
/// Clean up the thread-local runtime state. This *should* be run after all other
|
|
|
|
/// code managed by the Rust runtime, but will not cause UB if that condition is
|
|
|
|
/// not fulfilled. Also note that this function is not guaranteed to be run, but
|
|
|
|
/// skipping it will cause leaks and therefore is to be avoided.
|
|
|
|
pub(crate) fn thread_cleanup() {
|
|
|
|
// This function is run in situations where unwinding leads to an abort
|
|
|
|
// (think `extern "C"` functions). Abort here instead so that we can
|
|
|
|
// print a nice message.
|
|
|
|
panic::catch_unwind(|| {
|
|
|
|
crate::thread::drop_current();
|
|
|
|
})
|
|
|
|
.unwrap_or_else(handle_rt_panic);
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:20:44 +00:00
|
|
|
// One-time runtime cleanup.
|
|
|
|
// Runs after `main` or at program exit.
|
|
|
|
// NOTE: this is not guaranteed to run, for example when the program aborts.
|
|
|
|
pub(crate) fn cleanup() {
|
|
|
|
static CLEANUP: Once = Once::new();
|
|
|
|
CLEANUP.call_once(|| unsafe {
|
|
|
|
// Flush stdout and disable buffering.
|
|
|
|
crate::io::cleanup();
|
|
|
|
// SAFETY: Only called once during runtime cleanup.
|
|
|
|
sys::cleanup();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-23 23:15:38 +00:00
|
|
|
// To reduce the generated code of the new `lang_start`, this function is doing
|
|
|
|
// the real work.
|
2018-01-02 01:53:47 +00:00
|
|
|
#[cfg(not(test))]
|
2019-11-27 18:29:00 +00:00
|
|
|
fn lang_start_internal(
|
|
|
|
main: &(dyn Fn() -> i32 + Sync + crate::panic::RefUnwindSafe),
|
|
|
|
argc: isize,
|
|
|
|
argv: *const *const u8,
|
2022-07-05 17:56:22 +00:00
|
|
|
sigpipe: u8,
|
2025-01-11 14:43:43 +00:00
|
|
|
) -> isize {
|
2021-06-05 18:47:35 +00:00
|
|
|
// Guard against the code called by this function from unwinding outside of the Rust-controlled
|
|
|
|
// code, which is UB. This is a requirement imposed by a combination of how the
|
|
|
|
// `#[lang="start"]` attribute is implemented as well as by the implementation of the panicking
|
|
|
|
// mechanism itself.
|
|
|
|
//
|
|
|
|
// There are a couple of instances where unwinding can begin. First is inside of the
|
2022-10-28 23:48:00 +00:00
|
|
|
// `rt::init`, `rt::cleanup` and similar functions controlled by bstd. In those instances a
|
|
|
|
// panic is a std implementation bug. A quite likely one too, as there isn't any way to
|
|
|
|
// prevent std from accidentally introducing a panic to these functions. Another is from
|
2021-06-05 18:47:35 +00:00
|
|
|
// user code from `main` or, more nefariously, as described in e.g. issue #86030.
|
2025-01-11 09:35:00 +00:00
|
|
|
//
|
|
|
|
// We use `catch_unwind` with `handle_rt_panic` instead of `abort_unwind` to make the error in
|
|
|
|
// case of a panic a bit nicer.
|
|
|
|
panic::catch_unwind(move || {
|
|
|
|
// SAFETY: Only called once during runtime initialization.
|
|
|
|
unsafe { init(argc, argv, sigpipe) };
|
2025-01-11 14:43:43 +00:00
|
|
|
|
|
|
|
let ret_code = panic::catch_unwind(main).unwrap_or_else(move |payload| {
|
|
|
|
// Carefully dispose of the panic payload.
|
|
|
|
let payload = panic::AssertUnwindSafe(payload);
|
|
|
|
panic::catch_unwind(move || drop({ payload }.0)).unwrap_or_else(move |e| {
|
|
|
|
mem::forget(e); // do *not* drop the 2nd payload
|
|
|
|
rtabort!("drop of the panic payload panicked");
|
|
|
|
});
|
|
|
|
// Return error code for panicking programs.
|
|
|
|
101
|
|
|
|
});
|
|
|
|
let ret_code = ret_code as isize;
|
|
|
|
|
2025-01-11 09:35:00 +00:00
|
|
|
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();
|
2025-01-11 14:43:43 +00:00
|
|
|
|
2025-01-11 09:35:00 +00:00
|
|
|
ret_code
|
|
|
|
})
|
|
|
|
.unwrap_or_else(handle_rt_panic)
|
2017-12-03 21:16:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 15:47:00 +00:00
|
|
|
#[cfg(not(any(test, doctest)))]
|
2017-12-23 23:15:38 +00:00
|
|
|
#[lang = "start"]
|
2019-11-27 18:29:00 +00:00
|
|
|
fn lang_start<T: crate::process::Termination + 'static>(
|
|
|
|
main: fn() -> T,
|
|
|
|
argc: isize,
|
|
|
|
argv: *const *const u8,
|
2022-09-20 13:41:42 +00:00
|
|
|
sigpipe: u8,
|
2019-11-27 18:29:00 +00:00
|
|
|
) -> isize {
|
2025-01-11 14:43:43 +00:00
|
|
|
lang_start_internal(
|
2024-06-16 11:14:01 +00:00
|
|
|
&move || crate::sys::backtrace::__rust_begin_short_backtrace(main).report().to_i32(),
|
2020-08-04 21:18:20 +00:00
|
|
|
argc,
|
|
|
|
argv,
|
2022-07-05 17:56:22 +00:00
|
|
|
sigpipe,
|
2025-01-11 14:43:43 +00:00
|
|
|
)
|
2017-12-23 23:15:38 +00:00
|
|
|
}
|