Skip to content

Commit cb14269

Browse files
committed
Replace a couple of asserts with rtassert! in rt code
This replaces a couple of panic locations with hard aborts. The panics can't be catched by the user anyway in these locations.
1 parent 1ad44b2 commit cb14269

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

library/std/src/rt.rs

+32-32
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,6 @@ use crate::sys;
2626
use crate::sys_common::thread_info;
2727
use crate::thread::Thread;
2828

29-
// One-time runtime initialization.
30-
// Runs before `main`.
31-
// SAFETY: must be called only once during runtime initialization.
32-
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
33-
#[cfg_attr(test, allow(dead_code))]
34-
unsafe fn init(argc: isize, argv: *const *const u8) {
35-
unsafe {
36-
sys::init(argc, argv);
37-
38-
let main_guard = sys::thread::guard::init();
39-
// Next, set up the current Thread with the guard information we just
40-
// created. Note that this isn't necessary in general for new threads,
41-
// but we just do this to name the main thread and to give it correct
42-
// info about the stack bounds.
43-
let thread = Thread::new(Some(CString::new("main").unwrap()));
44-
thread_info::set(main_guard, thread);
45-
}
46-
}
47-
48-
// One-time runtime cleanup.
49-
// Runs after `main` or at program exit.
50-
// NOTE: this is not guaranteed to run, for example when the program aborts.
51-
pub(crate) fn cleanup() {
52-
static CLEANUP: Once = Once::new();
53-
CLEANUP.call_once(|| unsafe {
54-
// Flush stdout and disable buffering.
55-
crate::io::cleanup();
56-
// SAFETY: Only called once during runtime cleanup.
57-
sys::cleanup();
58-
});
59-
}
60-
6129
// Prints to the "panic output", depending on the platform this may be:
6230
// - the standard error output
6331
// - some dedicated platform specific output
@@ -99,6 +67,38 @@ macro_rules! rtunwrap {
9967
};
10068
}
10169

70+
// One-time runtime initialization.
71+
// Runs before `main`.
72+
// SAFETY: must be called only once during runtime initialization.
73+
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
74+
#[cfg_attr(test, allow(dead_code))]
75+
unsafe fn init(argc: isize, argv: *const *const u8) {
76+
unsafe {
77+
sys::init(argc, argv);
78+
79+
let main_guard = sys::thread::guard::init();
80+
// Next, set up the current Thread with the guard information we just
81+
// created. Note that this isn't necessary in general for new threads,
82+
// but we just do this to name the main thread and to give it correct
83+
// info about the stack bounds.
84+
let thread = Thread::new(Some(rtunwrap!(Ok, CString::new("main"))));
85+
thread_info::set(main_guard, thread);
86+
}
87+
}
88+
89+
// One-time runtime cleanup.
90+
// Runs after `main` or at program exit.
91+
// NOTE: this is not guaranteed to run, for example when the program aborts.
92+
pub(crate) fn cleanup() {
93+
static CLEANUP: Once = Once::new();
94+
CLEANUP.call_once(|| unsafe {
95+
// Flush stdout and disable buffering.
96+
crate::io::cleanup();
97+
// SAFETY: Only called once during runtime cleanup.
98+
sys::cleanup();
99+
});
100+
}
101+
102102
// To reduce the generated code of the new `lang_start`, this function is doing
103103
// the real work.
104104
#[cfg(not(test))]

library/std/src/sys/unix/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8) {
120120

121121
unsafe fn reset_sigpipe() {
122122
#[cfg(not(any(target_os = "emscripten", target_os = "fuchsia")))]
123-
assert!(signal(libc::SIGPIPE, libc::SIG_IGN) != libc::SIG_ERR);
123+
rtassert!(signal(libc::SIGPIPE, libc::SIG_IGN) != libc::SIG_ERR);
124124
}
125125
}
126126

library/std/src/sys_common/thread_info.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ pub fn stack_guard() -> Option<Guard> {
3939
}
4040

4141
pub fn set(stack_guard: Option<Guard>, thread: Thread) {
42-
THREAD_INFO.with(|c| assert!(c.borrow().is_none()));
42+
THREAD_INFO.with(|c| rtassert!(c.borrow().is_none()));
4343
THREAD_INFO.with(move |c| *c.borrow_mut() = Some(ThreadInfo { stack_guard, thread }));
4444
}

0 commit comments

Comments
 (0)