-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #3480 - RalfJung:alloc_error_handler, r=RalfJung
directly call handle_alloc_error Also test more codepaths. There's like 5 different things that can happen on allocation failure! Between `-Zoom`, `#[alloc_error_handler]`, and `set_alloc_error_hook`, we have 3 layers of behavior overrides. It's all a bit messy. rust-lang/rust#112331 seems intended to clean this up, but has not yet reached consensus.
- Loading branch information
Showing
16 changed files
with
171 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//@compile-flags: -Cpanic=abort | ||
#![feature(start, core_intrinsics)] | ||
#![feature(alloc_error_handler)] | ||
#![feature(allocator_api)] | ||
#![no_std] | ||
|
||
extern crate alloc; | ||
|
||
use alloc::alloc::*; | ||
use core::fmt::Write; | ||
|
||
#[path = "../../utils/mod.no_std.rs"] | ||
mod utils; | ||
|
||
#[alloc_error_handler] | ||
fn alloc_error_handler(layout: Layout) -> ! { | ||
let _ = writeln!(utils::MiriStderr, "custom alloc error handler: {layout:?}"); | ||
core::intrinsics::abort(); //~ERROR: aborted | ||
} | ||
|
||
// rustc requires us to provide some more things that aren't actually used by this test | ||
mod plumbing { | ||
use super::*; | ||
|
||
#[panic_handler] | ||
fn panic_handler(_: &core::panic::PanicInfo) -> ! { | ||
loop {} | ||
} | ||
|
||
struct NoAlloc; | ||
|
||
unsafe impl GlobalAlloc for NoAlloc { | ||
unsafe fn alloc(&self, _: Layout) -> *mut u8 { | ||
unreachable!(); | ||
} | ||
|
||
unsafe fn dealloc(&self, _: *mut u8, _: Layout) { | ||
unreachable!(); | ||
} | ||
} | ||
|
||
#[global_allocator] | ||
static GLOBAL: NoAlloc = NoAlloc; | ||
} | ||
|
||
#[start] | ||
fn start(_: isize, _: *const *const u8) -> isize { | ||
handle_alloc_error(Layout::for_value(&0)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
custom alloc error handler: Layout { size: 4, align: 4 (1 << 2) } | ||
error: abnormal termination: the program aborted execution | ||
--> $DIR/alloc_error_handler_custom.rs:LL:CC | ||
| | ||
LL | core::intrinsics::abort(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the program aborted execution | ||
| | ||
= note: BACKTRACE: | ||
= note: inside `alloc_error_handler` at $DIR/alloc_error_handler_custom.rs:LL:CC | ||
note: inside `_::__rg_oom` | ||
--> $DIR/alloc_error_handler_custom.rs:LL:CC | ||
| | ||
LL | #[alloc_error_handler] | ||
| ---------------------- in this procedural macro expansion | ||
LL | fn alloc_error_handler(layout: Layout) -> ! { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= note: inside `alloc::alloc::handle_alloc_error::rt_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC | ||
= note: inside `alloc::alloc::handle_alloc_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC | ||
note: inside `start` | ||
--> $DIR/alloc_error_handler_custom.rs:LL:CC | ||
| | ||
LL | handle_alloc_error(Layout::for_value(&0)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 1 previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,24 @@ | ||
custom alloc error handler called! | ||
custom panic handler called! | ||
panicked at RUSTLIB/alloc/src/alloc.rs:LL:CC: | ||
memory allocation of 4 bytes failed | ||
error: abnormal termination: the program aborted execution | ||
--> $DIR/alloc_error_handler_no_std.rs:LL:CC | ||
| | ||
LL | core::intrinsics::abort(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the program aborted execution | ||
| | ||
= note: BACKTRACE: | ||
= note: inside `alloc_error_handler` at $DIR/alloc_error_handler_no_std.rs:LL:CC | ||
note: inside `_::__rg_oom` | ||
--> $DIR/alloc_error_handler_no_std.rs:LL:CC | ||
| | ||
LL | #[alloc_error_handler] | ||
| ---------------------- in this procedural macro expansion | ||
LL | fn alloc_error_handler(_: Layout) -> ! { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= note: inside `panic_handler` at $DIR/alloc_error_handler_no_std.rs:LL:CC | ||
= note: inside `alloc::alloc::__alloc_error_handler::__rdl_oom` at RUSTLIB/alloc/src/alloc.rs:LL:CC | ||
= note: inside `alloc::alloc::handle_alloc_error::rt_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC | ||
= note: inside `alloc::alloc::handle_alloc_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC | ||
= note: inside `alloc::boxed::Box::<i32, BadAlloc>::new_uninit_in` at RUSTLIB/alloc/src/boxed.rs:LL:CC | ||
= note: inside `alloc::boxed::Box::<i32, BadAlloc>::new_in` at RUSTLIB/alloc/src/boxed.rs:LL:CC | ||
note: inside `start` | ||
--> $DIR/alloc_error_handler_no_std.rs:LL:CC | ||
| | ||
LL | let _b = Box::new_in(0, BadAlloc); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
LL | handle_alloc_error(Layout::for_value(&0)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to 1 previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#![feature(allocator_api, alloc_error_hook)] | ||
|
||
use std::alloc::*; | ||
|
||
struct Bomb; | ||
impl Drop for Bomb { | ||
fn drop(&mut self) { | ||
eprintln!("yes we are unwinding!"); | ||
} | ||
} | ||
|
||
#[allow(unreachable_code, unused_variables)] | ||
fn main() { | ||
// This is a particularly tricky hook, since it unwinds, which the default one does not. | ||
set_alloc_error_hook(|_layout| panic!("alloc error hook called")); | ||
|
||
let bomb = Bomb; | ||
handle_alloc_error(Layout::for_value(&0)); | ||
std::mem::forget(bomb); // defuse unwinding bomb | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
thread 'main' panicked at $DIR/alloc_error_handler_hook.rs:LL:CC: | ||
alloc error hook called | ||
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | ||
yes we are unwinding! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use core::fmt::{self, Write}; | ||
|
||
use super::miri_extern; | ||
|
||
pub struct MiriStderr; | ||
|
||
impl Write for MiriStderr { | ||
fn write_str(&mut self, s: &str) -> fmt::Result { | ||
unsafe { | ||
miri_extern::miri_write_to_stderr(s.as_bytes()); | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub struct MiriStdout; | ||
|
||
impl Write for MiriStdout { | ||
fn write_str(&mut self, s: &str) -> fmt::Result { | ||
unsafe { | ||
miri_extern::miri_write_to_stdout(s.as_bytes()); | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.