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

Trigger a panic on allocation error on nightly Rust #66347

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 10 additions & 0 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#![feature(set_stdio)]
#![feature(no_debug)]
#![feature(integer_atomics)]
#![feature(alloc_error_hook)]
#![feature(alloc_internals)]

#![recursion_limit="256"]

Expand Down Expand Up @@ -149,6 +151,14 @@ pub fn run_compiler(
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
emitter: Option<Box<dyn Write + Send>>
) -> interface::Result<()> {

// Install a custom alloc error hook on Nightly, to try
// so we can try to generate a backtrace on OOM in the compiler
// See https:/rust-lang/rust/issues/66342 for details
if UnstableFeatures::from_environment().is_nightly_build() {
std::alloc::set_alloc_error_hook(std::alloc::rustc_alloc_error_hook);
}

let mut args = Vec::new();
for arg in at_args {
match args::arg_expand(arg.clone()) {
Expand Down
15 changes: 15 additions & 0 deletions src/libstd/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ pub fn take_alloc_error_hook() -> fn(Layout) {
}
}

/// A custom error hook, used by librustc_driver.
/// This lives in `libstd` so that we can call `dumb_print`.
///
/// This hook prints out a message like the default error hook,
/// and then attempts to print a backtrace (without panicking).
/// If we are completely out of memory, backtrace generation may fail:
/// this is fine, since the backtrace is best-effort only. We
/// are guaranteed to print the actual error message, though.
#[doc(hidden)]
#[unstable(feature = "alloc_internals", issue = "0")]
pub fn rustc_alloc_error_hook(layout: Layout) {
dumb_print(format_args!("memory allocation of {} bytes failed. backtrace:", layout.size()));
dumb_print(format_args!("{:?}", crate::backtrace::Backtrace::capture()));
}

fn default_alloc_error_hook(layout: Layout) {
dumb_print(format_args!("memory allocation of {} bytes failed", layout.size()));
}
Expand Down