Skip to content

Commit 34cce58

Browse files
committed
Auto merge of #72204 - RalfJung:abort, r=Mark-Simulacrum
make abort intrinsic safe, and correct its documentation Turns out `std::process::abort` is not the same as the intrinsic, the comment was just wrong. Quoting from the unix implementation: ``` // On Unix-like platforms, libc::abort will unregister signal handlers // including the SIGABRT handler, preventing the abort from being blocked, and // fclose streams, with the side effect of flushing them so libc buffered // output will be printed. Additionally the shell will generally print a more // understandable error message like "Abort trap" rather than "Illegal // instruction" that intrinsics::abort would cause, as intrinsics::abort is // implemented as an illegal instruction. ```
2 parents 7faeae0 + 5980d97 commit 34cce58

File tree

10 files changed

+55
-14
lines changed

10 files changed

+55
-14
lines changed

src/liballoc/rc.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2027,6 +2027,8 @@ trait RcBoxPtr<T: ?Sized> {
20272027
// nevertheless, we insert an abort here to hint LLVM at
20282028
// an otherwise missed optimization.
20292029
if strong == 0 || strong == usize::max_value() {
2030+
// remove `unsafe` on bootstrap bump
2031+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))]
20302032
unsafe {
20312033
abort();
20322034
}
@@ -2053,6 +2055,8 @@ trait RcBoxPtr<T: ?Sized> {
20532055
// nevertheless, we insert an abort here to hint LLVM at
20542056
// an otherwise missed optimization.
20552057
if weak == 0 || weak == usize::max_value() {
2058+
// remove `unsafe` on bootstrap bump
2059+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))]
20562060
unsafe {
20572061
abort();
20582062
}

src/liballoc/sync.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,8 @@ impl<T: ?Sized> Clone for Arc<T> {
10961096
// We abort because such a program is incredibly degenerate, and we
10971097
// don't care to support it.
10981098
if old_size > MAX_REFCOUNT {
1099+
// remove `unsafe` on bootstrap bump
1100+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))]
10991101
unsafe {
11001102
abort();
11011103
}
@@ -1614,6 +1616,8 @@ impl<T: ?Sized> Weak<T> {
16141616

16151617
// See comments in `Arc::clone` for why we do this (for `mem::forget`).
16161618
if n > MAX_REFCOUNT {
1619+
// remove `unsafe` on bootstrap bump
1620+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))]
16171621
unsafe {
16181622
abort();
16191623
}
@@ -1753,6 +1757,7 @@ impl<T: ?Sized> Clone for Weak<T> {
17531757

17541758
// See comments in Arc::clone() for why we do this (for mem::forget).
17551759
if old_size > MAX_REFCOUNT {
1760+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))] // remove `unsafe` on bootstrap bump
17561761
unsafe {
17571762
abort();
17581763
}

src/libcore/cell.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,9 @@
133133
//! `Cell<T>`.
134134
//!
135135
//! ```
136-
//! #![feature(core_intrinsics)]
137136
//! use std::cell::Cell;
138137
//! use std::ptr::NonNull;
139-
//! use std::intrinsics::abort;
138+
//! use std::process::abort;
140139
//! use std::marker::PhantomData;
141140
//!
142141
//! struct Rc<T: ?Sized> {
@@ -173,7 +172,7 @@
173172
//! .strong
174173
//! .set(self.strong()
175174
//! .checked_add(1)
176-
//! .unwrap_or_else(|| unsafe { abort() }));
175+
//! .unwrap_or_else(|| abort() ));
177176
//! }
178177
//! }
179178
//!

src/libcore/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ extern "rust-intrinsic" {
918918

919919
/// Aborts the execution of the process.
920920
///
921-
/// The stabilized version of this intrinsic is
921+
/// A more user-friendly and stable version of this operation is
922922
/// [`std::process::abort`](../../std/process/fn.abort.html).
923923
pub fn abort() -> !;
924924

src/libcore/panicking.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ use crate::panic::{Location, PanicInfo};
3939
#[lang = "panic"] // needed by codegen for panic on overflow and other `Assert` MIR terminators
4040
pub fn panic(expr: &str) -> ! {
4141
if cfg!(feature = "panic_immediate_abort") {
42+
// remove `unsafe` (and safety comment) on bootstrap bump
43+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))]
4244
// SAFETY: the `abort` intrinsic has no requirements to be called.
43-
unsafe { super::intrinsics::abort() }
45+
unsafe {
46+
super::intrinsics::abort()
47+
}
4448
}
4549

4650
// Use Arguments::new_v1 instead of format_args!("{}", expr) to potentially
@@ -58,8 +62,12 @@ pub fn panic(expr: &str) -> ! {
5862
#[lang = "panic_bounds_check"] // needed by codegen for panic on OOB array/slice access
5963
fn panic_bounds_check(index: usize, len: usize) -> ! {
6064
if cfg!(feature = "panic_immediate_abort") {
65+
// remove `unsafe` (and safety comment) on bootstrap bump
66+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))]
6167
// SAFETY: the `abort` intrinsic has no requirements to be called.
62-
unsafe { super::intrinsics::abort() }
68+
unsafe {
69+
super::intrinsics::abort()
70+
}
6371
}
6472

6573
panic!("index out of bounds: the len is {} but the index is {}", len, index)
@@ -72,8 +80,12 @@ fn panic_bounds_check(index: usize, len: usize) -> ! {
7280
#[track_caller]
7381
pub fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
7482
if cfg!(feature = "panic_immediate_abort") {
83+
// remove `unsafe` (and safety comment) on bootstrap bump
84+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))]
7585
// SAFETY: the `abort` intrinsic has no requirements to be called.
76-
unsafe { super::intrinsics::abort() }
86+
unsafe {
87+
super::intrinsics::abort()
88+
}
7789
}
7890

7991
// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call

src/libpanic_unwind/seh.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -327,5 +327,8 @@ pub unsafe fn cleanup(payload: *mut u8) -> Box<dyn Any + Send> {
327327
#[lang = "eh_personality"]
328328
#[cfg(not(test))]
329329
fn rust_eh_personality() {
330-
unsafe { core::intrinsics::abort() }
330+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))] // remove `unsafe` on bootstrap bump
331+
unsafe {
332+
core::intrinsics::abort()
333+
}
331334
}

src/librustc_typeck/check/intrinsic.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn equate_intrinsic_type<'tcx>(
6969
/// Returns `true` if the given intrinsic is unsafe to call or not.
7070
pub fn intrinsic_operation_unsafety(intrinsic: &str) -> hir::Unsafety {
7171
match intrinsic {
72-
"size_of" | "min_align_of" | "needs_drop" | "caller_location" | "size_of_val"
72+
"abort" | "size_of" | "min_align_of" | "needs_drop" | "caller_location" | "size_of_val"
7373
| "min_align_of_val" | "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow"
7474
| "wrapping_add" | "wrapping_sub" | "wrapping_mul" | "saturating_add"
7575
| "saturating_sub" | "rotate_left" | "rotate_right" | "ctpop" | "ctlz" | "cttz"
@@ -130,7 +130,9 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
130130
}
131131
};
132132
(n_tps, inputs, output, hir::Unsafety::Unsafe)
133-
} else if &name[..] == "abort" || &name[..] == "unreachable" {
133+
} else if &name[..] == "abort" {
134+
(0, Vec::new(), tcx.types.never, hir::Unsafety::Normal)
135+
} else if &name[..] == "unreachable" {
134136
(0, Vec::new(), tcx.types.never, hir::Unsafety::Unsafe)
135137
} else {
136138
let unsafety = intrinsic_operation_unsafety(&name[..]);

src/libstd/panicking.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,10 @@ pub fn panicking() -> bool {
332332
#[cfg_attr(feature = "panic_immediate_abort", inline)]
333333
pub fn begin_panic_fmt(msg: &fmt::Arguments<'_>) -> ! {
334334
if cfg!(feature = "panic_immediate_abort") {
335-
unsafe { intrinsics::abort() }
335+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))] // remove `unsafe` on bootstrap bump
336+
unsafe {
337+
intrinsics::abort()
338+
}
336339
}
337340

338341
let info = PanicInfo::internal_constructor(Some(msg), Location::caller());
@@ -398,7 +401,10 @@ pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
398401
#[track_caller]
399402
pub fn begin_panic<M: Any + Send>(msg: M) -> ! {
400403
if cfg!(feature = "panic_immediate_abort") {
401-
unsafe { intrinsics::abort() }
404+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))] // remove `unsafe` on bootstrap bump
405+
unsafe {
406+
intrinsics::abort()
407+
}
402408
}
403409

404410
rust_panic_with_hook(&mut PanicPayload::new(msg), None, Location::caller());
@@ -458,7 +464,10 @@ fn rust_panic_with_hook(
458464
"thread panicked while processing \
459465
panic. aborting.\n"
460466
));
461-
unsafe { intrinsics::abort() }
467+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))] // remove `unsafe` on bootstrap bump
468+
unsafe {
469+
intrinsics::abort()
470+
}
462471
}
463472

464473
unsafe {
@@ -493,7 +502,10 @@ fn rust_panic_with_hook(
493502
"thread panicked while panicking. \
494503
aborting.\n"
495504
));
496-
unsafe { intrinsics::abort() }
505+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))] // remove `unsafe` on bootstrap bump
506+
unsafe {
507+
intrinsics::abort()
508+
}
497509
}
498510

499511
rust_panic(payload)

src/libstd/sync/mpsc/shared.rs

+2
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ impl<T> Packet<T> {
354354

355355
// See comments on Arc::clone() on why we do this (for `mem::forget`).
356356
if old_count > MAX_REFCOUNT {
357+
// remove `unsafe` on bootstrap bump
358+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))]
357359
unsafe {
358360
abort();
359361
}

src/libstd/sync/mpsc/sync.rs

+2
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ impl<T> Packet<T> {
358358

359359
// See comments on Arc::clone() on why we do this (for `mem::forget`).
360360
if old_count > MAX_REFCOUNT {
361+
// remove `unsafe` on bootstrap bump
362+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))]
361363
unsafe {
362364
abort();
363365
}

0 commit comments

Comments
 (0)