Skip to content

Commit 0a17be9

Browse files
authored
Rollup merge of #72289 - RalfJung:abort_internal, r=Mark-Simulacrum
abort_internal is safe `sys::abort_internal` is stably exposed as a safe function. Forward that assumption "inwards" to the `sys` module by making the function itself safe, too. This corresponds to what #72204 did for the intrinsic. We should probably wait until that lands because some of the intrinsic calls in this PR might then need adjustments.
2 parents 78c1690 + bdbe35d commit 0a17be9

File tree

11 files changed

+30
-24
lines changed

11 files changed

+30
-24
lines changed

src/libstd/alloc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ pub fn rust_oom(layout: Layout) -> ! {
279279
let hook: fn(Layout) =
280280
if hook.is_null() { default_alloc_error_hook } else { unsafe { mem::transmute(hook) } };
281281
hook(layout);
282-
unsafe { crate::sys::abort_internal() }
282+
crate::process::abort()
283283
}
284284

285285
#[cfg(not(test))]

src/libstd/process.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,7 @@ pub fn exit(code: i32) -> ! {
16201620
/// [panic hook]: ../../std/panic/fn.set_hook.html
16211621
#[stable(feature = "process_abort", since = "1.17.0")]
16221622
pub fn abort() -> ! {
1623-
unsafe { crate::sys::abort_internal() };
1623+
crate::sys::abort_internal();
16241624
}
16251625

16261626
/// Returns the OS-assigned process identifier associated with this process.

src/libstd/sys/cloudabi/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
5151
}
5252
}
5353

54-
pub unsafe fn abort_internal() -> ! {
55-
core::intrinsics::abort();
54+
pub fn abort_internal() -> ! {
55+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))] // remove `unsafe` on bootstrap bump
56+
unsafe {
57+
core::intrinsics::abort();
58+
}
5659
}
5760

5861
pub use libc::strlen;

src/libstd/sys/hermit/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ pub extern "C" fn floor(x: f64) -> f64 {
7474
unsafe { intrinsics::floorf64(x) }
7575
}
7676

77-
pub unsafe fn abort_internal() -> ! {
78-
abi::abort();
77+
pub fn abort_internal() -> ! {
78+
unsafe {
79+
abi::abort();
80+
}
7981
}
8082

8183
// FIXME: just a workaround to test the system
@@ -88,7 +90,7 @@ pub fn hashmap_random_keys() -> (u64, u64) {
8890
#[cfg(not(test))]
8991
#[no_mangle]
9092
// NB. used by both libunwind and libpanic_abort
91-
pub unsafe extern "C" fn __rust_abort() {
93+
pub extern "C" fn __rust_abort() {
9294
abort_internal();
9395
}
9496

src/libstd/sys/sgx/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,16 @@ pub unsafe fn strlen(mut s: *const c_char) -> usize {
124124
return n;
125125
}
126126

127-
pub unsafe fn abort_internal() -> ! {
128-
abi::usercalls::exit(true)
127+
pub fn abort_internal() -> ! {
128+
unsafe { abi::usercalls::exit(true) }
129129
}
130130

131131
// This function is needed by the panic runtime. The symbol is named in
132132
// pre-link args for the target specification, so keep that in sync.
133133
#[cfg(not(test))]
134134
#[no_mangle]
135135
// NB. used by both libunwind and libpanic_abort
136-
pub unsafe extern "C" fn __rust_abort() {
136+
pub extern "C" fn __rust_abort() {
137137
abort_internal();
138138
}
139139

src/libstd/sys/unix/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,6 @@ where
163163
// understandable error message like "Abort trap" rather than "Illegal
164164
// instruction" that intrinsics::abort would cause, as intrinsics::abort is
165165
// implemented as an illegal instruction.
166-
pub unsafe fn abort_internal() -> ! {
167-
libc::abort()
166+
pub fn abort_internal() -> ! {
167+
unsafe { libc::abort() }
168168
}

src/libstd/sys/vxworks/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,6 @@ where
108108
// understandable error message like "Abort trap" rather than "Illegal
109109
// instruction" that intrinsics::abort would cause, as intrinsics::abort is
110110
// implemented as an illegal instruction.
111-
pub unsafe fn abort_internal() -> ! {
112-
libc::abort()
111+
pub fn abort_internal() -> ! {
112+
unsafe { libc::abort() }
113113
}

src/libstd/sys/wasi/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ pub unsafe fn strlen(mut s: *const c_char) -> usize {
100100
return n;
101101
}
102102

103-
pub unsafe fn abort_internal() -> ! {
104-
libc::abort()
103+
pub fn abort_internal() -> ! {
104+
unsafe { libc::abort() }
105105
}
106106

107107
pub fn hashmap_random_keys() -> (u64, u64) {

src/libstd/sys/wasm/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ pub unsafe fn strlen(mut s: *const c_char) -> usize {
8181
return n;
8282
}
8383

84-
pub unsafe fn abort_internal() -> ! {
85-
crate::arch::wasm32::unreachable()
84+
pub fn abort_internal() -> ! {
85+
unsafe { crate::arch::wasm32::unreachable() }
8686
}
8787

8888
// We don't have randomness yet, but I totally used a random number generator to

src/libstd/sys/windows/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,14 @@ pub fn dur2timeout(dur: Duration) -> c::DWORD {
308308
//
309309
// https://docs.microsoft.com/en-us/cpp/intrinsics/fastfail
310310
#[allow(unreachable_code)]
311-
pub unsafe fn abort_internal() -> ! {
311+
pub fn abort_internal() -> ! {
312312
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
313-
{
313+
unsafe {
314314
llvm_asm!("int $$0x29" :: "{ecx}"(7) ::: volatile); // 7 is FAST_FAIL_FATAL_APP_EXIT
315315
crate::intrinsics::unreachable();
316316
}
317-
crate::intrinsics::abort();
317+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))] // remove `unsafe` on bootstrap bump
318+
unsafe {
319+
crate::intrinsics::abort();
320+
}
318321
}

src/libstd/sys_common/util.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ pub fn dumb_print(args: fmt::Arguments<'_>) {
1616

1717
pub fn abort(args: fmt::Arguments<'_>) -> ! {
1818
dumb_print(format_args!("fatal runtime error: {}\n", args));
19-
unsafe {
20-
crate::sys::abort_internal();
21-
}
19+
crate::sys::abort_internal();
2220
}
2321

2422
#[allow(dead_code)] // stack overflow detection not enabled on all platforms

0 commit comments

Comments
 (0)