]> git.proxmox.com Git - rustc.git/blob - library/std/src/rt.rs
Update upstream source from tag 'upstream/1.55.0+dfsg1'
[rustc.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
20 // To reduce the generated code of the new `lang_start`, this function is doing
21 // the real work.
22 #[cfg(not(test))]
23 fn lang_start_internal(
24 main: &(dyn Fn() -> i32 + Sync + crate::panic::RefUnwindSafe),
25 argc: isize,
26 argv: *const *const u8,
27 ) -> Result<isize, !> {
28 use crate::{mem, panic, sys, sys_common};
29 let rt_abort = move |e| {
30 mem::forget(e);
31 rtabort!("initialization or cleanup bug");
32 };
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
36 // mechanism itself.
37 //
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)
46 .map_err(move |e| {
47 mem::forget(e);
48 rtprintpanic!("drop of the panic payload panicked");
49 sys::abort_internal()
50 });
51 panic::catch_unwind(sys_common::rt::cleanup).map_err(rt_abort)?;
52 ret_code
53 }
54
55 #[cfg(not(test))]
56 #[lang = "start"]
57 fn lang_start<T: crate::process::Termination + 'static>(
58 main: fn() -> T,
59 argc: isize,
60 argv: *const *const u8,
61 ) -> isize {
62 lang_start_internal(
63 &move || crate::sys_common::backtrace::__rust_begin_short_backtrace(main).report(),
64 argc,
65 argv,
66 )
67 .into_ok()
68 }