Skip to content

Commit cc229f9

Browse files
authored
Unrolled build for rust-lang#139103
Rollup merge of rust-lang#139103 - joboet:abort_dedup, r=tgross35 deduplicate abort implementations Currently, the code for process aborts is duplicated across `panic_abort` and `std`. This PR uses `#[rustc_std_internal_symbol]` to make the `std` implementation available to `panic_abort` via the linker, thereby deduplicating the code.
2 parents 2c12b4a + f77c160 commit cc229f9

38 files changed

+85
-152
lines changed

library/Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ name = "panic_abort"
196196
version = "0.0.0"
197197
dependencies = [
198198
"alloc",
199-
"cfg-if",
200199
"compiler_builtins",
201200
"core",
202201
"libc",

library/panic_abort/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ bench = false
1212
doc = false
1313

1414
[dependencies]
15-
alloc = { path = "../alloc" }
16-
cfg-if = { version = "1.0", features = ['rustc-dep-of-std'] }
1715
core = { path = "../core" }
1816
compiler_builtins = "0.1.0"
1917

20-
[target.'cfg(not(all(windows, target_env = "msvc")))'.dependencies]
18+
[target.'cfg(target_os = "android")'.dependencies]
2119
libc = { version = "0.2", default-features = false }
20+
21+
[target.'cfg(any(target_os = "android", target_os = "zkvm"))'.dependencies]
22+
alloc = { path = "../alloc" }

library/panic_abort/src/lib.rs

Lines changed: 5 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@
77
#![unstable(feature = "panic_abort", issue = "32837")]
88
#![doc(issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")]
99
#![panic_runtime]
10-
#![allow(unused_features)]
11-
#![feature(asm_experimental_arch)]
12-
#![feature(core_intrinsics)]
1310
#![feature(panic_runtime)]
1411
#![feature(std_internals)]
1512
#![feature(staged_api)]
1613
#![feature(rustc_attrs)]
1714
#![allow(internal_features)]
18-
#![deny(unsafe_op_in_unsafe_fn)]
1915

2016
#[cfg(target_os = "android")]
2117
mod android;
@@ -45,75 +41,13 @@ pub unsafe fn __rust_start_panic(_payload: &mut dyn PanicPayload) -> u32 {
4541
zkvm::zkvm_set_abort_message(_payload);
4642
}
4743

48-
unsafe {
49-
abort();
44+
unsafe extern "Rust" {
45+
// This is defined in std::rt.
46+
#[rustc_std_internal_symbol]
47+
safe fn __rust_abort() -> !;
5048
}
5149

52-
cfg_if::cfg_if! {
53-
if #[cfg(any(unix, target_os = "solid_asp3"))] {
54-
unsafe fn abort() -> ! {
55-
unsafe { libc::abort(); }
56-
}
57-
} else if #[cfg(any(target_os = "hermit",
58-
all(target_vendor = "fortanix", target_env = "sgx"),
59-
target_os = "xous",
60-
target_os = "uefi",
61-
))] {
62-
unsafe fn abort() -> ! {
63-
// call std::sys::abort_internal
64-
unsafe extern "C" {
65-
pub fn __rust_abort() -> !;
66-
}
67-
unsafe { __rust_abort(); }
68-
}
69-
} else if #[cfg(all(windows, not(miri)))] {
70-
// On Windows, use the processor-specific __fastfail mechanism. In Windows 8
71-
// and later, this will terminate the process immediately without running any
72-
// in-process exception handlers. In earlier versions of Windows, this
73-
// sequence of instructions will be treated as an access violation,
74-
// terminating the process but without necessarily bypassing all exception
75-
// handlers.
76-
//
77-
// https://docs.microsoft.com/en-us/cpp/intrinsics/fastfail
78-
//
79-
// Note: this is the same implementation as in std's `abort_internal`
80-
unsafe fn abort() -> ! {
81-
#[allow(unused)]
82-
const FAST_FAIL_FATAL_APP_EXIT: usize = 7;
83-
cfg_if::cfg_if! {
84-
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
85-
unsafe {
86-
core::arch::asm!("int $$0x29", in("ecx") FAST_FAIL_FATAL_APP_EXIT, options(noreturn, nostack));
87-
}
88-
} else if #[cfg(all(target_arch = "arm", target_feature = "thumb-mode"))] {
89-
unsafe {
90-
core::arch::asm!(".inst 0xDEFB", in("r0") FAST_FAIL_FATAL_APP_EXIT, options(noreturn, nostack));
91-
}
92-
} else if #[cfg(any(target_arch = "aarch64", target_arch = "arm64ec"))] {
93-
unsafe {
94-
core::arch::asm!("brk 0xF003", in("x0") FAST_FAIL_FATAL_APP_EXIT, options(noreturn, nostack));
95-
}
96-
} else {
97-
core::intrinsics::abort();
98-
}
99-
}
100-
}
101-
} else if #[cfg(target_os = "teeos")] {
102-
mod teeos {
103-
unsafe extern "C" {
104-
pub fn TEE_Panic(code: u32) -> !;
105-
}
106-
}
107-
108-
unsafe fn abort() -> ! {
109-
unsafe { teeos::TEE_Panic(1); }
110-
}
111-
} else {
112-
unsafe fn abort() -> ! {
113-
core::intrinsics::abort();
114-
}
115-
}
116-
}
50+
__rust_abort()
11751
}
11852

11953
// This... is a bit of an oddity. The tl;dr; is that this is required to link

library/panic_unwind/src/hermit.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@
55
use alloc::boxed::Box;
66
use core::any::Any;
77

8+
unsafe extern "Rust" {
9+
// This is defined in std::rt
10+
#[rustc_std_internal_symbol]
11+
safe fn __rust_abort() -> !;
12+
}
13+
814
pub(crate) unsafe fn cleanup(_ptr: *mut u8) -> Box<dyn Any + Send> {
9-
unsafe extern "C" {
10-
fn __rust_abort() -> !;
11-
}
12-
unsafe {
13-
__rust_abort();
14-
}
15+
__rust_abort()
1516
}
1617

1718
pub(crate) unsafe fn panic(_data: Box<dyn Any + Send>) -> u32 {
18-
unsafe extern "C" {
19-
fn __rust_abort() -> !;
20-
}
21-
unsafe {
22-
__rust_abort();
23-
}
19+
__rust_abort()
2420
}

library/std/src/rt.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ use crate::sync::Once;
2626
use crate::thread::{self, main_thread};
2727
use crate::{mem, panic, sys};
2828

29+
// This function is needed by the panic runtime.
30+
#[cfg(not(test))]
31+
#[rustc_std_internal_symbol]
32+
fn __rust_abort() {
33+
crate::process::abort();
34+
}
35+
2936
// Prints to the "panic output", depending on the platform this may be:
3037
// - the standard error output
3138
// - some dedicated platform specific output
@@ -47,7 +54,7 @@ macro_rules! rtabort {
4754
($($t:tt)*) => {
4855
{
4956
rtprintpanic!("fatal runtime error: {}, aborting\n", format_args!($($t)*));
50-
crate::sys::abort_internal();
57+
crate::process::abort();
5158
}
5259
}
5360
}

library/std/src/sys/pal/hermit/mod.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,6 @@ pub fn abort_internal() -> ! {
4343
unsafe { hermit_abi::abort() }
4444
}
4545

46-
// This function is needed by the panic runtime. The symbol is named in
47-
// pre-link args for the target specification, so keep that in sync.
48-
#[cfg(not(test))]
49-
#[unsafe(no_mangle)]
50-
// NB. used by both libunwind and libpanic_abort
51-
pub extern "C" fn __rust_abort() {
52-
abort_internal();
53-
}
54-
5546
// SAFETY: must be called only once during runtime initialization.
5647
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
5748
pub unsafe fn init(argc: isize, argv: *const *const u8, _sigpipe: u8) {

library/std/src/sys/pal/sgx/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,14 @@ pub fn abort_internal() -> ! {
112112
abi::usercalls::exit(true)
113113
}
114114

115-
// This function is needed by the panic runtime. The symbol is named in
115+
// This function is needed by libunwind. The symbol is named in
116116
// pre-link args for the target specification, so keep that in sync.
117+
// Note: contrary to the `__rust_abort` in `crate::rt`, this uses `no_mangle`
118+
// because it is actually used from C code. Because symbols annotated with
119+
// #[rustc_std_internal_symbol] get mangled, this will not lead to linker
120+
// conflicts.
117121
#[cfg(not(test))]
118122
#[unsafe(no_mangle)]
119-
// NB. used by both libunwind and libpanic_abort
120123
pub extern "C" fn __rust_abort() {
121124
abort_internal();
122125
}

library/std/src/sys/pal/uefi/mod.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,6 @@ pub fn abort_internal() -> ! {
161161
core::intrinsics::abort();
162162
}
163163

164-
// This function is needed by the panic runtime. The symbol is named in
165-
// pre-link args for the target specification, so keep that in sync.
166-
#[cfg(not(test))]
167-
#[unsafe(no_mangle)]
168-
pub extern "C" fn __rust_abort() {
169-
abort_internal();
170-
}
171-
172164
/// Disable access to BootServices if `EVT_SIGNAL_EXIT_BOOT_SERVICES` is signaled
173165
extern "efiapi" fn exit_boot_service_handler(_e: r_efi::efi::Event, _ctx: *mut crate::ffi::c_void) {
174166
uefi::env::disable_boot_services();

library/std/src/sys/pal/windows/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,12 @@ pub fn dur2timeout(dur: Duration) -> u32 {
328328

329329
/// Use `__fastfail` to abort the process
330330
///
331-
/// This is the same implementation as in libpanic_abort's `__rust_start_panic`. See
332-
/// that function for more information on `__fastfail`
331+
/// In Windows 8 and later, this will terminate the process immediately without
332+
/// running any in-process exception handlers. In earlier versions of Windows,
333+
/// this sequence of instructions will be treated as an access violation, which
334+
/// will still terminate the process but might run some exception handlers.
335+
///
336+
/// https://docs.microsoft.com/en-us/cpp/intrinsics/fastfail
333337
#[cfg(not(miri))] // inline assembly does not work in Miri
334338
pub fn abort_internal() -> ! {
335339
unsafe {

library/std/src/sys/pal/xous/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![forbid(unsafe_op_in_unsafe_fn)]
22

3+
use crate::os::xous::ffi::exit;
4+
35
pub mod os;
46
#[path = "../unsupported/pipe.rs"]
57
pub mod pipe;
@@ -9,3 +11,7 @@ pub mod time;
911
#[path = "../unsupported/common.rs"]
1012
mod common;
1113
pub use common::*;
14+
15+
pub fn abort_internal() -> ! {
16+
exit(101);
17+
}

library/std/src/sys/pal/xous/os.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,6 @@ mod c_compat {
6262
}
6363
exit(unsafe { main() });
6464
}
65-
66-
// This function is needed by the panic runtime. The symbol is named in
67-
// pre-link args for the target specification, so keep that in sync.
68-
#[unsafe(no_mangle)]
69-
// NB. used by both libunwind and libpanic_abort
70-
pub extern "C" fn __rust_abort() -> ! {
71-
exit(101);
72-
}
7365
}
7466

7567
pub fn errno() -> i32 {

src/tools/miri/tests/fail/alloc/alloc_error_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@error-in-other-file: aborted
2-
//@normalize-stderr-test: "unsafe \{ libc::abort\(\) \}|crate::intrinsics::abort\(\);" -> "ABORT();"
2+
//@normalize-stderr-test: "\|.*::abort\(\).*" -> "| ABORT()"
33
//@normalize-stderr-test: "\| +\^+" -> "| ^"
44
#![feature(allocator_api)]
55

src/tools/miri/tests/fail/alloc/alloc_error_handler.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ memory allocation of 4 bytes failed
22
error: abnormal termination: the program aborted execution
33
--> RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC
44
|
5-
LL | ABORT();
5+
LL | ABORT()
66
| ^ the program aborted execution
77
|
88
= note: BACKTRACE:

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.both.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ thread caused non-unwinding panic. aborting.
1111
error: abnormal termination: the program aborted execution
1212
--> RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC
1313
|
14-
LL | ABORT();
14+
LL | ABORT()
1515
| ^ the program aborted execution
1616
|
1717
= note: BACKTRACE:

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.definition.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ thread caused non-unwinding panic. aborting.
1111
error: abnormal termination: the program aborted execution
1212
--> RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC
1313
|
14-
LL | ABORT();
14+
LL | ABORT()
1515
| ^ the program aborted execution
1616
|
1717
= note: BACKTRACE:

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@revisions: extern_block definition both
2-
//@normalize-stderr-test: "unsafe \{ libc::abort\(\) \}|crate::intrinsics::abort\(\);" -> "ABORT();"
2+
//@normalize-stderr-test: "\|.*::abort\(\).*" -> "| ABORT()"
33
//@normalize-stderr-test: "\| +\^+" -> "| ^"
44
//@normalize-stderr-test: "\n +[0-9]+:[^\n]+" -> ""
55
//@normalize-stderr-test: "\n +at [^\n]+" -> ""

src/tools/miri/tests/fail/intrinsics/uninit_uninhabited_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@normalize-stderr-test: "unsafe \{ libc::abort\(\) \}|crate::intrinsics::abort\(\);" -> "ABORT();"
1+
//@normalize-stderr-test: "\|.*::abort\(\).*" -> "| ABORT()"
22
//@normalize-stderr-test: "\| +\^+" -> "| ^"
33
//@normalize-stderr-test: "\n +[0-9]+:[^\n]+" -> ""
44
//@normalize-stderr-test: "\n +at [^\n]+" -> ""

src/tools/miri/tests/fail/intrinsics/uninit_uninhabited_type.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ thread caused non-unwinding panic. aborting.
77
error: abnormal termination: the program aborted execution
88
--> RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC
99
|
10-
LL | ABORT();
10+
LL | ABORT()
1111
| ^ the program aborted execution
1212
|
1313
= note: BACKTRACE:

src/tools/miri/tests/fail/intrinsics/zero_fn_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@normalize-stderr-test: "unsafe \{ libc::abort\(\) \}|crate::intrinsics::abort\(\);" -> "ABORT();"
1+
//@normalize-stderr-test: "\|.*::abort\(\).*" -> "| ABORT()"
22
//@normalize-stderr-test: "\| +\^+" -> "| ^"
33
//@normalize-stderr-test: "\n +[0-9]+:[^\n]+" -> ""
44
//@normalize-stderr-test: "\n +at [^\n]+" -> ""

src/tools/miri/tests/fail/intrinsics/zero_fn_ptr.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ thread caused non-unwinding panic. aborting.
77
error: abnormal termination: the program aborted execution
88
--> RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC
99
|
10-
LL | ABORT();
10+
LL | ABORT()
1111
| ^ the program aborted execution
1212
|
1313
= note: BACKTRACE:

src/tools/miri/tests/fail/panic/abort_unwind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@error-in-other-file: the program aborted execution
2-
//@normalize-stderr-test: "unsafe \{ libc::abort\(\) \}|crate::intrinsics::abort\(\);" -> "ABORT();"
2+
//@normalize-stderr-test: "\|.*::abort\(\).*" -> "| ABORT()"
33
//@normalize-stderr-test: "\| +\^+" -> "| ^"
44
//@normalize-stderr-test: "\n +[0-9]+:[^\n]+" -> ""
55
//@normalize-stderr-test: "\n +at [^\n]+" -> ""

src/tools/miri/tests/fail/panic/abort_unwind.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ thread caused non-unwinding panic. aborting.
1111
error: abnormal termination: the program aborted execution
1212
--> RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC
1313
|
14-
LL | ABORT();
14+
LL | ABORT()
1515
| ^ the program aborted execution
1616
|
1717
= note: BACKTRACE:

src/tools/miri/tests/fail/panic/double_panic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@normalize-stderr-test: "unsafe \{ libc::abort\(\) \}|crate::intrinsics::abort\(\);" -> "ABORT();"
1+
//@normalize-stderr-test: "\|.*::abort\(\).*" -> "| ABORT()"
22
//@normalize-stderr-test: "\| +\^+" -> "| ^"
33
//@normalize-stderr-test: "\n +[0-9]+:[^\n]+" -> ""
44
//@normalize-stderr-test: "\n +at [^\n]+" -> ""

src/tools/miri/tests/fail/panic/double_panic.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ thread caused non-unwinding panic. aborting.
1414
error: abnormal termination: the program aborted execution
1515
--> RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC
1616
|
17-
LL | ABORT();
17+
LL | ABORT()
1818
| ^ the program aborted execution
1919
|
2020
= note: BACKTRACE:

src/tools/miri/tests/fail/panic/panic_abort1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@error-in-other-file: the program aborted execution
22
//@normalize-stderr-test: "\| +\^+" -> "| ^"
3-
//@normalize-stderr-test: "unsafe \{ libc::abort\(\); \}|core::intrinsics::abort\(\);" -> "ABORT();"
3+
//@normalize-stderr-test: "\|.*::abort\(\).*" -> "| ABORT()"
44
//@compile-flags: -C panic=abort
55

66
fn main() {

src/tools/miri/tests/fail/panic/panic_abort1.stderr

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ panicking from libstd
44
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
55
note: in Miri, you may have to set `MIRIFLAGS=-Zmiri-env-forward=RUST_BACKTRACE` for the environment variable to have an effect
66
error: abnormal termination: the program aborted execution
7-
--> RUSTLIB/panic_abort/src/lib.rs:LL:CC
7+
--> RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC
88
|
9-
LL | ABORT();
9+
LL | ABORT()
1010
| ^ the program aborted execution
1111
|
1212
= note: BACKTRACE:
13-
= note: inside `panic_abort::__rust_start_panic::abort` at RUSTLIB/panic_abort/src/lib.rs:LL:CC
13+
= note: inside `std::sys::pal::PLATFORM::abort_internal` at RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC
14+
= note: inside `std::process::abort` at RUSTLIB/std/src/process.rs:LL:CC
15+
= note: inside `std::rt::__rust_abort` at RUSTLIB/std/src/rt.rs:LL:CC
1416
= note: inside `panic_abort::__rust_start_panic` at RUSTLIB/panic_abort/src/lib.rs:LL:CC
1517
= note: inside `std::panicking::rust_panic` at RUSTLIB/std/src/panicking.rs:LL:CC
1618
= note: inside `std::panicking::rust_panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC

0 commit comments

Comments
 (0)