Skip to content

Commit 5ab0f37

Browse files
committed
Auto merge of #84697 - CDirkx:util, r=m-ou-se
Introduce `sys_common::rt::rtprintpanic!` to replace `sys_common::util` functionality This PR introduces a new macro `rtprintpanic!`, similar to `sys_common::util::dumb_print` and uses that macro to replace all `sys_common::util` functionality.
2 parents a426fc3 + 4ff5ab5 commit 5ab0f37

File tree

7 files changed

+34
-46
lines changed

7 files changed

+34
-46
lines changed

Diff for: library/std/src/alloc.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ use core::ptr::NonNull;
6363
use core::sync::atomic::{AtomicPtr, Ordering};
6464
use core::{mem, ptr};
6565

66-
use crate::sys_common::util::dumb_print;
67-
6866
#[stable(feature = "alloc_module", since = "1.28.0")]
6967
#[doc(inline)]
7068
pub use alloc_crate::alloc::*;
@@ -317,7 +315,7 @@ pub fn take_alloc_error_hook() -> fn(Layout) {
317315
}
318316

319317
fn default_alloc_error_hook(layout: Layout) {
320-
dumb_print(format_args!("memory allocation of {} bytes failed\n", layout.size()));
318+
rtprintpanic!("memory allocation of {} bytes failed\n", layout.size());
321319
}
322320

323321
#[cfg(not(test))]

Diff for: library/std/src/panicking.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::sync::atomic::{AtomicBool, Ordering};
2020
use crate::sys::stdio::panic_output;
2121
use crate::sys_common::backtrace::{self, RustBacktrace};
2222
use crate::sys_common::rwlock::RWLock;
23-
use crate::sys_common::{thread_info, util};
23+
use crate::sys_common::thread_info;
2424
use crate::thread;
2525

2626
#[cfg(not(test))]
@@ -596,15 +596,12 @@ fn rust_panic_with_hook(
596596
if panics > 2 {
597597
// Don't try to print the message in this case
598598
// - perhaps that is causing the recursive panics.
599-
util::dumb_print(format_args!("thread panicked while processing panic. aborting.\n"));
599+
rtprintpanic!("thread panicked while processing panic. aborting.\n");
600600
} else {
601601
// Unfortunately, this does not print a backtrace, because creating
602602
// a `Backtrace` will allocate, which we must to avoid here.
603603
let panicinfo = PanicInfo::internal_constructor(message, location);
604-
util::dumb_print(format_args!(
605-
"{}\npanicked after panic::always_abort(), aborting.\n",
606-
panicinfo
607-
));
604+
rtprintpanic!("{}\npanicked after panic::always_abort(), aborting.\n", panicinfo);
608605
}
609606
intrinsics::abort()
610607
}
@@ -637,7 +634,7 @@ fn rust_panic_with_hook(
637634
// have limited options. Currently our preference is to
638635
// just abort. In the future we may consider resuming
639636
// unwinding or otherwise exiting the thread cleanly.
640-
util::dumb_print(format_args!("thread panicked while panicking. aborting.\n"));
637+
rtprintpanic!("thread panicked while panicking. aborting.\n");
641638
intrinsics::abort()
642639
}
643640

Diff for: library/std/src/sys/unix/stack_overflow.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ mod imp {
4242
use crate::io;
4343
use crate::mem;
4444
use crate::ptr;
45+
use crate::thread;
4546

4647
use libc::MAP_FAILED;
4748
use libc::{mmap, munmap};
@@ -95,15 +96,16 @@ mod imp {
9596
info: *mut libc::siginfo_t,
9697
_data: *mut libc::c_void,
9798
) {
98-
use crate::sys_common::util::report_overflow;
99-
10099
let guard = thread_info::stack_guard().unwrap_or(0..0);
101100
let addr = siginfo_si_addr(info);
102101

103102
// If the faulting address is within the guard page, then we print a
104103
// message saying so and abort.
105104
if guard.start <= addr && addr < guard.end {
106-
report_overflow();
105+
rtprintpanic!(
106+
"\nthread '{}' has overflowed its stack\n",
107+
thread::current().name().unwrap_or("<unknown>")
108+
);
107109
rtabort!("stack overflow");
108110
} else {
109111
// Unregister ourselves by reverting back to the default behavior.

Diff for: library/std/src/sys/windows/stack_overflow.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![cfg_attr(test, allow(dead_code))]
22

33
use crate::sys::c;
4-
use crate::sys_common::util::report_overflow;
4+
use crate::thread;
55

66
pub struct Handler;
77

@@ -24,7 +24,10 @@ extern "system" fn vectored_handler(ExceptionInfo: *mut c::EXCEPTION_POINTERS) -
2424
let code = rec.ExceptionCode;
2525

2626
if code == c::EXCEPTION_STACK_OVERFLOW {
27-
report_overflow();
27+
rtprintpanic!(
28+
"\nthread '{}' has overflowed its stack\n",
29+
thread::current().name().unwrap_or("<unknown>")
30+
);
2831
}
2932
c::EXCEPTION_CONTINUE_SEARCH
3033
}

Diff for: library/std/src/sys_common/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ pub mod thread_info;
4040
pub mod thread_local_dtor;
4141
pub mod thread_local_key;
4242
pub mod thread_parker;
43-
pub mod util;
4443
pub mod wtf8;
4544

4645
cfg_if::cfg_if! {

Diff for: library/std/src/sys_common/rt.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![deny(unsafe_op_in_unsafe_fn)]
2+
#![allow(unused_macros)]
23

34
use crate::sync::Once;
45
use crate::sys;
@@ -38,8 +39,25 @@ pub fn cleanup() {
3839
});
3940
}
4041

42+
// Prints to the "panic output", depending on the platform this may be:
43+
// - the standard error output
44+
// - some dedicated platform specific output
45+
// - nothing (so this macro is a no-op)
46+
macro_rules! rtprintpanic {
47+
($($t:tt)*) => {
48+
if let Some(mut out) = crate::sys::stdio::panic_output() {
49+
let _ = crate::io::Write::write_fmt(&mut out, format_args!($($t)*));
50+
}
51+
}
52+
}
53+
4154
macro_rules! rtabort {
42-
($($t:tt)*) => (crate::sys_common::util::abort(format_args!($($t)*)))
55+
($($t:tt)*) => {
56+
{
57+
rtprintpanic!("fatal runtime error: {}\n", format_args!($($t)*));
58+
crate::sys::abort_internal();
59+
}
60+
}
4361
}
4462

4563
macro_rules! rtassert {
@@ -50,7 +68,6 @@ macro_rules! rtassert {
5068
};
5169
}
5270

53-
#[allow(unused_macros)] // not used on all platforms
5471
macro_rules! rtunwrap {
5572
($ok:ident, $e:expr) => {
5673
match $e {

Diff for: library/std/src/sys_common/util.rs

-28
This file was deleted.

0 commit comments

Comments
 (0)