From 5b0908587e5e549051bcc6294b034049421de4f8 Mon Sep 17 00:00:00 2001 From: Christiaan Dirkx Date: Thu, 29 Apr 2021 15:57:05 +0200 Subject: [PATCH 1/5] Introduce `sys_common::rt::rterr!` --- library/std/src/sys_common/rt.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/library/std/src/sys_common/rt.rs b/library/std/src/sys_common/rt.rs index c70f2ecc04e3a..9ce6aa7c36963 100644 --- a/library/std/src/sys_common/rt.rs +++ b/library/std/src/sys_common/rt.rs @@ -1,4 +1,5 @@ #![deny(unsafe_op_in_unsafe_fn)] +#![allow(unused_macros)] use crate::sync::Once; use crate::sys; @@ -38,6 +39,14 @@ pub fn cleanup() { }); } +macro_rules! rterr { + ($($t:tt)*) => { + if let Some(mut out) = crate::sys::stdio::panic_output() { + let _ = crate::io::Write::write_fmt(&mut out, format_args!($($t)*)); + } + } +} + macro_rules! rtabort { ($($t:tt)*) => (crate::sys_common::util::abort(format_args!($($t)*))) } @@ -50,7 +59,6 @@ macro_rules! rtassert { }; } -#[allow(unused_macros)] // not used on all platforms macro_rules! rtunwrap { ($ok:ident, $e:expr) => { match $e { From b987f74f05b3fbeb81eed27c43678dc71cdab8cf Mon Sep 17 00:00:00 2001 From: Christiaan Dirkx Date: Thu, 29 Apr 2021 15:59:05 +0200 Subject: [PATCH 2/5] Remove `sys_common::util::abort` --- library/std/src/sys_common/rt.rs | 7 ++++++- library/std/src/sys_common/util.rs | 10 ---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/library/std/src/sys_common/rt.rs b/library/std/src/sys_common/rt.rs index 9ce6aa7c36963..e4c9b60594ef7 100644 --- a/library/std/src/sys_common/rt.rs +++ b/library/std/src/sys_common/rt.rs @@ -48,7 +48,12 @@ macro_rules! rterr { } macro_rules! rtabort { - ($($t:tt)*) => (crate::sys_common::util::abort(format_args!($($t)*))) + ($($t:tt)*) => { + { + rterr!("fatal runtime error: {}\n", format_args!($($t)*)); + crate::sys::abort_internal(); + } + } } macro_rules! rtassert { diff --git a/library/std/src/sys_common/util.rs b/library/std/src/sys_common/util.rs index 9f7c3bd87952f..b8cae26d04c58 100644 --- a/library/std/src/sys_common/util.rs +++ b/library/std/src/sys_common/util.rs @@ -9,16 +9,6 @@ pub fn dumb_print(args: fmt::Arguments<'_>) { } } -// Other platforms should use the appropriate platform-specific mechanism for -// aborting the process. If no platform-specific mechanism is available, -// crate::intrinsics::abort() may be used instead. The above implementations cover -// all targets currently supported by libstd. - -pub fn abort(args: fmt::Arguments<'_>) -> ! { - dumb_print(format_args!("fatal runtime error: {}\n", args)); - crate::sys::abort_internal(); -} - #[allow(dead_code)] // stack overflow detection not enabled on all platforms pub unsafe fn report_overflow() { dumb_print(format_args!( From 236705f3c30905623f97dc8887c83db520eb27c1 Mon Sep 17 00:00:00 2001 From: Christiaan Dirkx Date: Thu, 29 Apr 2021 16:05:10 +0200 Subject: [PATCH 3/5] Replace `sys_common::util::report_overflow` with `rterr!` --- library/std/src/sys/unix/stack_overflow.rs | 8 +++++--- library/std/src/sys/windows/stack_overflow.rs | 7 +++++-- library/std/src/sys_common/util.rs | 9 --------- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/library/std/src/sys/unix/stack_overflow.rs b/library/std/src/sys/unix/stack_overflow.rs index 2a487fff54ae7..72fd48278bc16 100644 --- a/library/std/src/sys/unix/stack_overflow.rs +++ b/library/std/src/sys/unix/stack_overflow.rs @@ -42,6 +42,7 @@ mod imp { use crate::io; use crate::mem; use crate::ptr; + use crate::thread; use libc::MAP_FAILED; use libc::{mmap, munmap}; @@ -95,15 +96,16 @@ mod imp { info: *mut libc::siginfo_t, _data: *mut libc::c_void, ) { - use crate::sys_common::util::report_overflow; - let guard = thread_info::stack_guard().unwrap_or(0..0); let addr = siginfo_si_addr(info); // If the faulting address is within the guard page, then we print a // message saying so and abort. if guard.start <= addr && addr < guard.end { - report_overflow(); + rterr!( + "\nthread '{}' has overflowed its stack\n", + thread::current().name().unwrap_or("") + ); rtabort!("stack overflow"); } else { // Unregister ourselves by reverting back to the default behavior. diff --git a/library/std/src/sys/windows/stack_overflow.rs b/library/std/src/sys/windows/stack_overflow.rs index 39efb778207fc..24ba35ad17e63 100644 --- a/library/std/src/sys/windows/stack_overflow.rs +++ b/library/std/src/sys/windows/stack_overflow.rs @@ -1,7 +1,7 @@ #![cfg_attr(test, allow(dead_code))] use crate::sys::c; -use crate::sys_common::util::report_overflow; +use crate::thread; pub struct Handler; @@ -24,7 +24,10 @@ extern "system" fn vectored_handler(ExceptionInfo: *mut c::EXCEPTION_POINTERS) - let code = rec.ExceptionCode; if code == c::EXCEPTION_STACK_OVERFLOW { - report_overflow(); + rterr!( + "\nthread '{}' has overflowed its stack\n", + thread::current().name().unwrap_or("") + ); } c::EXCEPTION_CONTINUE_SEARCH } diff --git a/library/std/src/sys_common/util.rs b/library/std/src/sys_common/util.rs index b8cae26d04c58..f7072cc501103 100644 --- a/library/std/src/sys_common/util.rs +++ b/library/std/src/sys_common/util.rs @@ -1,18 +1,9 @@ use crate::fmt; use crate::io::prelude::*; use crate::sys::stdio::panic_output; -use crate::thread; pub fn dumb_print(args: fmt::Arguments<'_>) { if let Some(mut out) = panic_output() { let _ = out.write_fmt(args); } } - -#[allow(dead_code)] // stack overflow detection not enabled on all platforms -pub unsafe fn report_overflow() { - dumb_print(format_args!( - "\nthread '{}' has overflowed its stack\n", - thread::current().name().unwrap_or("") - )); -} From 6145051eeebeba030cef3aa01a99683b84ff24fc Mon Sep 17 00:00:00 2001 From: Christiaan Dirkx Date: Thu, 29 Apr 2021 16:10:32 +0200 Subject: [PATCH 4/5] Replace `sys_common::util::dumb_print` with `rterr!` --- library/std/src/alloc.rs | 4 +--- library/std/src/panicking.rs | 10 +++++----- library/std/src/sys_common/mod.rs | 1 - library/std/src/sys_common/util.rs | 9 --------- 4 files changed, 6 insertions(+), 18 deletions(-) delete mode 100644 library/std/src/sys_common/util.rs diff --git a/library/std/src/alloc.rs b/library/std/src/alloc.rs index 843ef09a5842f..9e9052ff92e1b 100644 --- a/library/std/src/alloc.rs +++ b/library/std/src/alloc.rs @@ -63,8 +63,6 @@ use core::ptr::NonNull; use core::sync::atomic::{AtomicPtr, Ordering}; use core::{mem, ptr}; -use crate::sys_common::util::dumb_print; - #[stable(feature = "alloc_module", since = "1.28.0")] #[doc(inline)] pub use alloc_crate::alloc::*; @@ -317,7 +315,7 @@ pub fn take_alloc_error_hook() -> fn(Layout) { } fn default_alloc_error_hook(layout: Layout) { - dumb_print(format_args!("memory allocation of {} bytes failed\n", layout.size())); + rterr!("memory allocation of {} bytes failed\n", layout.size()); } #[cfg(not(test))] diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index a8410bea7342b..9091151518579 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -20,7 +20,7 @@ use crate::sync::atomic::{AtomicBool, Ordering}; use crate::sys::stdio::panic_output; use crate::sys_common::backtrace::{self, RustBacktrace}; use crate::sys_common::rwlock::RWLock; -use crate::sys_common::{thread_info, util}; +use crate::sys_common::thread_info; use crate::thread; #[cfg(not(test))] @@ -596,15 +596,15 @@ fn rust_panic_with_hook( if panics > 2 { // Don't try to print the message in this case // - perhaps that is causing the recursive panics. - util::dumb_print(format_args!("thread panicked while processing panic. aborting.\n")); + rterr!("thread panicked while processing panic. aborting.\n"); } else { // Unfortunately, this does not print a backtrace, because creating // a `Backtrace` will allocate, which we must to avoid here. let panicinfo = PanicInfo::internal_constructor(message, location); - util::dumb_print(format_args!( + rterr!( "{}\npanicked after panic::always_abort(), aborting.\n", panicinfo - )); + ); } intrinsics::abort() } @@ -637,7 +637,7 @@ fn rust_panic_with_hook( // have limited options. Currently our preference is to // just abort. In the future we may consider resuming // unwinding or otherwise exiting the thread cleanly. - util::dumb_print(format_args!("thread panicked while panicking. aborting.\n")); + rterr!("thread panicked while panicking. aborting.\n"); intrinsics::abort() } diff --git a/library/std/src/sys_common/mod.rs b/library/std/src/sys_common/mod.rs index 7fa6977f2af26..4ef0e72adf020 100644 --- a/library/std/src/sys_common/mod.rs +++ b/library/std/src/sys_common/mod.rs @@ -40,7 +40,6 @@ pub mod thread_info; pub mod thread_local_dtor; pub mod thread_local_key; pub mod thread_parker; -pub mod util; pub mod wtf8; cfg_if::cfg_if! { diff --git a/library/std/src/sys_common/util.rs b/library/std/src/sys_common/util.rs deleted file mode 100644 index f7072cc501103..0000000000000 --- a/library/std/src/sys_common/util.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::fmt; -use crate::io::prelude::*; -use crate::sys::stdio::panic_output; - -pub fn dumb_print(args: fmt::Arguments<'_>) { - if let Some(mut out) = panic_output() { - let _ = out.write_fmt(args); - } -} From 4ff5ab52966203b8ad8da96f897566e4a218308a Mon Sep 17 00:00:00 2001 From: Christiaan Dirkx Date: Thu, 6 May 2021 14:03:50 +0200 Subject: [PATCH 5/5] Rename `rterr` to `rtprintpanic` --- library/std/src/alloc.rs | 2 +- library/std/src/panicking.rs | 9 +++------ library/std/src/sys/unix/stack_overflow.rs | 2 +- library/std/src/sys/windows/stack_overflow.rs | 2 +- library/std/src/sys_common/rt.rs | 8 ++++++-- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/library/std/src/alloc.rs b/library/std/src/alloc.rs index 9e9052ff92e1b..8ee55234cea43 100644 --- a/library/std/src/alloc.rs +++ b/library/std/src/alloc.rs @@ -315,7 +315,7 @@ pub fn take_alloc_error_hook() -> fn(Layout) { } fn default_alloc_error_hook(layout: Layout) { - rterr!("memory allocation of {} bytes failed\n", layout.size()); + rtprintpanic!("memory allocation of {} bytes failed\n", layout.size()); } #[cfg(not(test))] diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index 9091151518579..02957e75a7409 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -596,15 +596,12 @@ fn rust_panic_with_hook( if panics > 2 { // Don't try to print the message in this case // - perhaps that is causing the recursive panics. - rterr!("thread panicked while processing panic. aborting.\n"); + rtprintpanic!("thread panicked while processing panic. aborting.\n"); } else { // Unfortunately, this does not print a backtrace, because creating // a `Backtrace` will allocate, which we must to avoid here. let panicinfo = PanicInfo::internal_constructor(message, location); - rterr!( - "{}\npanicked after panic::always_abort(), aborting.\n", - panicinfo - ); + rtprintpanic!("{}\npanicked after panic::always_abort(), aborting.\n", panicinfo); } intrinsics::abort() } @@ -637,7 +634,7 @@ fn rust_panic_with_hook( // have limited options. Currently our preference is to // just abort. In the future we may consider resuming // unwinding or otherwise exiting the thread cleanly. - rterr!("thread panicked while panicking. aborting.\n"); + rtprintpanic!("thread panicked while panicking. aborting.\n"); intrinsics::abort() } diff --git a/library/std/src/sys/unix/stack_overflow.rs b/library/std/src/sys/unix/stack_overflow.rs index 72fd48278bc16..81f47a779d33b 100644 --- a/library/std/src/sys/unix/stack_overflow.rs +++ b/library/std/src/sys/unix/stack_overflow.rs @@ -102,7 +102,7 @@ mod imp { // If the faulting address is within the guard page, then we print a // message saying so and abort. if guard.start <= addr && addr < guard.end { - rterr!( + rtprintpanic!( "\nthread '{}' has overflowed its stack\n", thread::current().name().unwrap_or("") ); diff --git a/library/std/src/sys/windows/stack_overflow.rs b/library/std/src/sys/windows/stack_overflow.rs index 24ba35ad17e63..755dc0a6c8b47 100644 --- a/library/std/src/sys/windows/stack_overflow.rs +++ b/library/std/src/sys/windows/stack_overflow.rs @@ -24,7 +24,7 @@ extern "system" fn vectored_handler(ExceptionInfo: *mut c::EXCEPTION_POINTERS) - let code = rec.ExceptionCode; if code == c::EXCEPTION_STACK_OVERFLOW { - rterr!( + rtprintpanic!( "\nthread '{}' has overflowed its stack\n", thread::current().name().unwrap_or("") ); diff --git a/library/std/src/sys_common/rt.rs b/library/std/src/sys_common/rt.rs index e4c9b60594ef7..02013ecc4ced6 100644 --- a/library/std/src/sys_common/rt.rs +++ b/library/std/src/sys_common/rt.rs @@ -39,7 +39,11 @@ pub fn cleanup() { }); } -macro_rules! rterr { +// Prints to the "panic output", depending on the platform this may be: +// - the standard error output +// - some dedicated platform specific output +// - nothing (so this macro is a no-op) +macro_rules! rtprintpanic { ($($t:tt)*) => { if let Some(mut out) = crate::sys::stdio::panic_output() { let _ = crate::io::Write::write_fmt(&mut out, format_args!($($t)*)); @@ -50,7 +54,7 @@ macro_rules! rterr { macro_rules! rtabort { ($($t:tt)*) => { { - rterr!("fatal runtime error: {}\n", format_args!($($t)*)); + rtprintpanic!("fatal runtime error: {}\n", format_args!($($t)*)); crate::sys::abort_internal(); } }