3 //! The `rt` module provides a narrow set of runtime services,
4 //! including the global heap (exported in `heap`) and unwinding and
5 //! backtrace support. The APIs in this module are highly unstable,
6 //! and should be considered as private implementation details for the
11 reason = "this public module should not exist and is highly likely \
17 // Re-export some of our utilities which are expected by other crates.
18 pub use crate::panicking::{begin_panic, begin_panic_fmt, panic_count};
20 // To reduce the generated code of the new `lang_start`, this function is doing
23 fn lang_start_internal(
24 main: &(dyn Fn() -> i32 + Sync + crate::panic::RefUnwindSafe),
26 argv: *const *const u8,
27 ) -> Result<isize, !> {
28 use crate::{mem, panic, sys, sys_common};
29 let rt_abort = move |e| {
31 rtabort!("initialization or cleanup bug");
33 // Guard against the code called by this function from unwinding outside of the Rust-controlled
34 // code, which is UB. This is a requirement imposed by a combination of how the
35 // `#[lang="start"]` attribute is implemented as well as by the implementation of the panicking
38 // There are a couple of instances where unwinding can begin. First is inside of the
39 // `rt::init`, `rt::cleanup` and similar functions controlled by libstd. In those instances a
40 // panic is a libstd implementation bug. A quite likely one too, as there isn't any way to
41 // prevent libstd from accidentally introducing a panic to these functions. Another is from
42 // user code from `main` or, more nefariously, as described in e.g. issue #86030.
43 // SAFETY: Only called once during runtime initialization.
44 panic::catch_unwind(move || unsafe { sys_common::rt::init(argc, argv) }).map_err(rt_abort)?;
45 let ret_code = panic::catch_unwind(move || panic::catch_unwind(main).unwrap_or(101) as isize)
48 rtprintpanic!("drop of the panic payload panicked");
51 panic::catch_unwind(sys_common::rt::cleanup).map_err(rt_abort)?;
57 fn lang_start<T: crate::process::Termination + 'static>(
60 argv: *const *const u8,
63 &move || crate::sys_common::backtrace::__rust_begin_short_backtrace(main).report(),