Skip to content

Commit 9f7fe81

Browse files
committed
use a single large catch_unwind in lang_start
1 parent 7e4077d commit 9f7fe81

File tree

5 files changed

+56
-24
lines changed

5 files changed

+56
-24
lines changed

library/std/src/rt.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ macro_rules! rtunwrap {
6767
};
6868
}
6969

70-
fn handle_rt_panic(e: Box<dyn Any + Send>) {
70+
fn handle_rt_panic<T>(e: Box<dyn Any + Send>) -> T {
7171
mem::forget(e);
7272
rtabort!("initialization or cleanup bug");
7373
}
@@ -168,19 +168,27 @@ fn lang_start_internal(
168168
// panic is a std implementation bug. A quite likely one too, as there isn't any way to
169169
// prevent std from accidentally introducing a panic to these functions. Another is from
170170
// user code from `main` or, more nefariously, as described in e.g. issue #86030.
171-
// SAFETY: Only called once during runtime initialization.
172-
panic::catch_unwind(move || unsafe { init(argc, argv, sigpipe) })
173-
.unwrap_or_else(handle_rt_panic);
174-
let ret_code = panic::catch_unwind(move || panic::catch_unwind(main).unwrap_or(101) as isize)
175-
.map_err(move |e| {
176-
mem::forget(e);
177-
rtabort!("drop of the panic payload panicked");
178-
});
179-
panic::catch_unwind(cleanup).unwrap_or_else(handle_rt_panic);
180-
// Guard against multiple threads calling `libc::exit` concurrently.
181-
// See the documentation for `unique_thread_exit` for more information.
182-
panic::catch_unwind(crate::sys::exit_guard::unique_thread_exit).unwrap_or_else(handle_rt_panic);
183-
ret_code
171+
//
172+
// We use `catch_unwind` with `handle_rt_panic` instead of `abort_unwind` to make the error in
173+
// case of a panic a bit nicer.
174+
panic::catch_unwind(move || {
175+
// SAFETY: Only called once during runtime initialization.
176+
unsafe { init(argc, argv, sigpipe) };
177+
let ret_code =
178+
panic::catch_unwind(move || panic::catch_unwind(main).unwrap_or(101) as isize).map_err(
179+
move |e| {
180+
// Print a specific error when we abort due to a panicing payload destructor.
181+
mem::forget(e);
182+
rtabort!("drop of the panic payload panicked");
183+
},
184+
);
185+
cleanup();
186+
// Guard against multiple threads calling `libc::exit` concurrently.
187+
// See the documentation for `unique_thread_exit` for more information.
188+
crate::sys::exit_guard::unique_thread_exit();
189+
ret_code
190+
})
191+
.unwrap_or_else(handle_rt_panic)
184192
}
185193

186194
#[cfg(not(any(test, doctest)))]

src/tools/miri/tests/fail/tail_calls/cc-mismatch.stderr

+7-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ LL | extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
1515
= note: inside `std::panicking::r#try::<i32, &dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe>` at RUSTLIB/std/src/panicking.rs:LL:CC
1616
= note: inside `std::panic::catch_unwind::<&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe, i32>` at RUSTLIB/std/src/panic.rs:LL:CC
1717
= note: inside closure at RUSTLIB/std/src/rt.rs:LL:CC
18-
= note: inside `std::panicking::r#try::do_call::<{closure@std::rt::lang_start_internal::{closure#1}}, isize>` at RUSTLIB/std/src/panicking.rs:LL:CC
19-
= note: inside `std::panicking::r#try::<isize, {closure@std::rt::lang_start_internal::{closure#1}}>` at RUSTLIB/std/src/panicking.rs:LL:CC
20-
= note: inside `std::panic::catch_unwind::<{closure@std::rt::lang_start_internal::{closure#1}}, isize>` at RUSTLIB/std/src/panic.rs:LL:CC
18+
= note: inside `std::panicking::r#try::do_call::<{closure@std::rt::lang_start_internal::{closure#0}::{closure#0}}, isize>` at RUSTLIB/std/src/panicking.rs:LL:CC
19+
= note: inside `std::panicking::r#try::<isize, {closure@std::rt::lang_start_internal::{closure#0}::{closure#0}}>` at RUSTLIB/std/src/panicking.rs:LL:CC
20+
= note: inside `std::panic::catch_unwind::<{closure@std::rt::lang_start_internal::{closure#0}::{closure#0}}, isize>` at RUSTLIB/std/src/panic.rs:LL:CC
21+
= note: inside closure at RUSTLIB/std/src/rt.rs:LL:CC
22+
= note: inside `std::panicking::r#try::do_call::<{closure@std::rt::lang_start_internal::{closure#0}}, std::result::Result<isize, !>>` at RUSTLIB/std/src/panicking.rs:LL:CC
23+
= note: inside `std::panicking::r#try::<std::result::Result<isize, !>, {closure@std::rt::lang_start_internal::{closure#0}}>` at RUSTLIB/std/src/panicking.rs:LL:CC
24+
= note: inside `std::panic::catch_unwind::<{closure@std::rt::lang_start_internal::{closure#0}}, std::result::Result<isize, !>>` at RUSTLIB/std/src/panic.rs:LL:CC
2125
= note: inside `std::rt::lang_start_internal` at RUSTLIB/std/src/rt.rs:LL:CC
2226
= note: inside `std::rt::lang_start::<()>` at RUSTLIB/std/src/rt.rs:LL:CC
2327

src/tools/miri/tests/pass/backtrace/backtrace-api-v1.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ RUSTLIB/core/src/ops/function.rs:LL:CC (std::ops::function::impls::call_once)
1010
RUSTLIB/std/src/panicking.rs:LL:CC (std::panicking::r#try::do_call)
1111
RUSTLIB/std/src/panicking.rs:LL:CC (std::panicking::r#try)
1212
RUSTLIB/std/src/panic.rs:LL:CC (std::panic::catch_unwind)
13-
RUSTLIB/std/src/rt.rs:LL:CC (std::rt::lang_start_internal::{closure#1})
13+
RUSTLIB/std/src/rt.rs:LL:CC (std::rt::lang_start_internal::{closure#0}::{closure#0})
14+
RUSTLIB/std/src/panicking.rs:LL:CC (std::panicking::r#try::do_call)
15+
RUSTLIB/std/src/panicking.rs:LL:CC (std::panicking::r#try)
16+
RUSTLIB/std/src/panic.rs:LL:CC (std::panic::catch_unwind)
17+
RUSTLIB/std/src/rt.rs:LL:CC (std::rt::lang_start_internal::{closure#0})
1418
RUSTLIB/std/src/panicking.rs:LL:CC (std::panicking::r#try::do_call)
1519
RUSTLIB/std/src/panicking.rs:LL:CC (std::panicking::r#try)
1620
RUSTLIB/std/src/panic.rs:LL:CC (std::panic::catch_unwind)

src/tools/miri/tests/pass/backtrace/backtrace-global-alloc.stderr

+11-3
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,23 @@
1414
at RUSTLIB/std/src/panicking.rs:LL:CC
1515
7: std::panic::catch_unwind
1616
at RUSTLIB/std/src/panic.rs:LL:CC
17-
8: std::rt::lang_start_internal::{closure#1}
17+
8: std::rt::lang_start_internal::{closure#0}::{closure#0}
1818
at RUSTLIB/std/src/rt.rs:LL:CC
1919
9: std::panicking::r#try::do_call
2020
at RUSTLIB/std/src/panicking.rs:LL:CC
2121
10: std::panicking::r#try
2222
at RUSTLIB/std/src/panicking.rs:LL:CC
2323
11: std::panic::catch_unwind
2424
at RUSTLIB/std/src/panic.rs:LL:CC
25-
12: std::rt::lang_start_internal
25+
12: std::rt::lang_start_internal::{closure#0}
2626
at RUSTLIB/std/src/rt.rs:LL:CC
27-
13: std::rt::lang_start
27+
13: std::panicking::r#try::do_call
28+
at RUSTLIB/std/src/panicking.rs:LL:CC
29+
14: std::panicking::r#try
30+
at RUSTLIB/std/src/panicking.rs:LL:CC
31+
15: std::panic::catch_unwind
32+
at RUSTLIB/std/src/panic.rs:LL:CC
33+
16: std::rt::lang_start_internal
34+
at RUSTLIB/std/src/rt.rs:LL:CC
35+
17: std::rt::lang_start
2836
at RUSTLIB/std/src/rt.rs:LL:CC

src/tools/miri/tests/pass/backtrace/backtrace-std.stderr

+11-3
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,23 @@
2222
at RUSTLIB/std/src/panicking.rs:LL:CC
2323
11: std::panic::catch_unwind
2424
at RUSTLIB/std/src/panic.rs:LL:CC
25-
12: std::rt::lang_start_internal::{closure#1}
25+
12: std::rt::lang_start_internal::{closure#0}::{closure#0}
2626
at RUSTLIB/std/src/rt.rs:LL:CC
2727
13: std::panicking::r#try::do_call
2828
at RUSTLIB/std/src/panicking.rs:LL:CC
2929
14: std::panicking::r#try
3030
at RUSTLIB/std/src/panicking.rs:LL:CC
3131
15: std::panic::catch_unwind
3232
at RUSTLIB/std/src/panic.rs:LL:CC
33-
16: std::rt::lang_start_internal
33+
16: std::rt::lang_start_internal::{closure#0}
3434
at RUSTLIB/std/src/rt.rs:LL:CC
35-
17: std::rt::lang_start
35+
17: std::panicking::r#try::do_call
36+
at RUSTLIB/std/src/panicking.rs:LL:CC
37+
18: std::panicking::r#try
38+
at RUSTLIB/std/src/panicking.rs:LL:CC
39+
19: std::panic::catch_unwind
40+
at RUSTLIB/std/src/panic.rs:LL:CC
41+
20: std::rt::lang_start_internal
42+
at RUSTLIB/std/src/rt.rs:LL:CC
43+
21: std::rt::lang_start
3644
at RUSTLIB/std/src/rt.rs:LL:CC

0 commit comments

Comments
 (0)