]> git.lizzy.rs Git - rust.git/blob - library/std/src/rt.rs
b4f2adf938b5630a03de4e841c3451b62ca9ad6f
[rust.git] / library / std / src / rt.rs
1 //! Runtime services
2 //!
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
7 //! time being.
8
9 #![unstable(
10     feature = "rt",
11     reason = "this public module should not exist and is highly likely \
12               to disappear",
13     issue = "none"
14 )]
15 #![doc(hidden)]
16
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};
19 pub use core::panicking::panic_display;
20
21 // To reduce the generated code of the new `lang_start`, this function is doing
22 // the real work.
23 #[cfg(not(test))]
24 fn lang_start_internal(
25     main: &(dyn Fn() -> i32 + Sync + crate::panic::RefUnwindSafe),
26     argc: isize,
27     argv: *const *const u8,
28 ) -> Result<isize, !> {
29     use crate::{mem, panic, sys, sys_common};
30     let rt_abort = move |e| {
31         mem::forget(e);
32         rtabort!("initialization or cleanup bug");
33     };
34     // Guard against the code called by this function from unwinding outside of the Rust-controlled
35     // code, which is UB. This is a requirement imposed by a combination of how the
36     // `#[lang="start"]` attribute is implemented as well as by the implementation of the panicking
37     // mechanism itself.
38     //
39     // There are a couple of instances where unwinding can begin. First is inside of the
40     // `rt::init`, `rt::cleanup` and similar functions controlled by libstd. In those instances a
41     // panic is a libstd implementation bug. A quite likely one too, as there isn't any way to
42     // prevent libstd from accidentally introducing a panic to these functions. Another is from
43     // user code from `main` or, more nefariously, as described in e.g. issue #86030.
44     // SAFETY: Only called once during runtime initialization.
45     panic::catch_unwind(move || unsafe { sys_common::rt::init(argc, argv) }).map_err(rt_abort)?;
46     let ret_code = panic::catch_unwind(move || panic::catch_unwind(main).unwrap_or(101) as isize)
47         .map_err(move |e| {
48             mem::forget(e);
49             rtprintpanic!("drop of the panic payload panicked");
50             sys::abort_internal()
51         });
52     panic::catch_unwind(sys_common::rt::cleanup).map_err(rt_abort)?;
53     ret_code
54 }
55
56 #[cfg(not(test))]
57 #[lang = "start"]
58 fn lang_start<T: crate::process::Termination + 'static>(
59     main: fn() -> T,
60     argc: isize,
61     argv: *const *const u8,
62 ) -> isize {
63     lang_start_internal(
64         &move || crate::sys_common::backtrace::__rust_begin_short_backtrace(main).report(),
65         argc,
66         argv,
67     )
68     .into_ok()
69 }