Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce sys_common::rt::rtprintpanic! to replace sys_common::util functionality #84697

Merged
merged 5 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions library/std/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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()));
rtprintpanic!("memory allocation of {} bytes failed\n", layout.size());
}

#[cfg(not(test))]
Expand Down
11 changes: 4 additions & 7 deletions library/std/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -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.
util::dumb_print(format_args!("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);
util::dumb_print(format_args!(
"{}\npanicked after panic::always_abort(), aborting.\n",
panicinfo
));
rtprintpanic!("{}\npanicked after panic::always_abort(), aborting.\n", panicinfo);
}
intrinsics::abort()
}
Expand Down Expand Up @@ -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.
util::dumb_print(format_args!("thread panicked while panicking. aborting.\n"));
rtprintpanic!("thread panicked while panicking. aborting.\n");
intrinsics::abort()
}

Expand Down
8 changes: 5 additions & 3 deletions library/std/src/sys/unix/stack_overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
rtprintpanic!(
"\nthread '{}' has overflowed its stack\n",
thread::current().name().unwrap_or("<unknown>")
);
rtabort!("stack overflow");
} else {
// Unregister ourselves by reverting back to the default behavior.
Expand Down
7 changes: 5 additions & 2 deletions library/std/src/sys/windows/stack_overflow.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();
rtprintpanic!(
"\nthread '{}' has overflowed its stack\n",
thread::current().name().unwrap_or("<unknown>")
);
}
c::EXCEPTION_CONTINUE_SEARCH
}
Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys_common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down
21 changes: 19 additions & 2 deletions library/std/src/sys_common/rt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![deny(unsafe_op_in_unsafe_fn)]
#![allow(unused_macros)]

use crate::sync::Once;
use crate::sys;
Expand Down Expand Up @@ -38,8 +39,25 @@ pub fn cleanup() {
});
}

// 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)*));
}
}
}

macro_rules! rtabort {
($($t:tt)*) => (crate::sys_common::util::abort(format_args!($($t)*)))
($($t:tt)*) => {
{
rtprintpanic!("fatal runtime error: {}\n", format_args!($($t)*));
crate::sys::abort_internal();
}
}
}

macro_rules! rtassert {
Expand All @@ -50,7 +68,6 @@ macro_rules! rtassert {
};
}

#[allow(unused_macros)] // not used on all platforms
macro_rules! rtunwrap {
($ok:ident, $e:expr) => {
match $e {
Expand Down
28 changes: 0 additions & 28 deletions library/std/src/sys_common/util.rs

This file was deleted.