-
Notifications
You must be signed in to change notification settings - Fork 356
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 #3478 - RalfJung:alloc_error_handler, r=RalfJung
implement support for __rust_alloc_error_handler Fixes #3439
- Loading branch information
Showing
10 changed files
with
207 additions
and
7 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,25 @@ | ||
//@error-in-other-file: aborted | ||
//@normalize-stderr-test: "unsafe \{ libc::abort\(\) \}|crate::intrinsics::abort\(\);" -> "ABORT();" | ||
//@normalize-stderr-test: "\| +\^+" -> "| ^" | ||
#![feature(allocator_api)] | ||
|
||
use std::alloc::*; | ||
use std::ptr::NonNull; | ||
|
||
struct BadAlloc; | ||
|
||
// Create a failing allocator; Miri's native allocator never fails so this is the only way to | ||
// actually call the alloc error handler. | ||
unsafe impl Allocator for BadAlloc { | ||
fn allocate(&self, _l: Layout) -> Result<NonNull<[u8]>, AllocError> { | ||
Err(AllocError) | ||
} | ||
|
||
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) { | ||
unreachable!(); | ||
} | ||
} | ||
|
||
fn main() { | ||
let _b = Box::new_in(0, BadAlloc); | ||
} |
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,26 @@ | ||
memory allocation of 4 bytes failed | ||
error: abnormal termination: the program aborted execution | ||
--> RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC | ||
| | ||
LL | ABORT(); | ||
| ^ the program aborted execution | ||
| | ||
= note: BACKTRACE: | ||
= note: inside `std::sys::pal::PLATFORM::abort_internal` at RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC | ||
= note: inside `std::process::abort` at RUSTLIB/std/src/process.rs:LL:CC | ||
= note: inside `std::alloc::rust_oom` at RUSTLIB/std/src/alloc.rs:LL:CC | ||
= note: inside `std::alloc::_::__rg_oom` at RUSTLIB/std/src/alloc.rs:LL:CC | ||
= note: inside `std::alloc::handle_alloc_error::rt_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC | ||
= note: inside `std::alloc::handle_alloc_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC | ||
= note: inside `std::boxed::Box::<i32, BadAlloc>::new_uninit_in` at RUSTLIB/alloc/src/boxed.rs:LL:CC | ||
= note: inside `std::boxed::Box::<i32, BadAlloc>::new_in` at RUSTLIB/alloc/src/boxed.rs:LL:CC | ||
note: inside `main` | ||
--> $DIR/alloc_error_handler.rs:LL:CC | ||
| | ||
LL | let _b = Box::new_in(0, BadAlloc); | ||
| ^ | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//@compile-flags: -Cpanic=abort | ||
#![feature(start, core_intrinsics)] | ||
#![feature(alloc_error_handler)] | ||
#![feature(allocator_api)] | ||
#![no_std] | ||
|
||
extern crate alloc; | ||
|
||
use alloc::alloc::*; | ||
use alloc::boxed::Box; | ||
use core::ptr::NonNull; | ||
|
||
struct BadAlloc; | ||
|
||
// Create a failing allocator; that is the only way to actually call the alloc error handler. | ||
unsafe impl Allocator for BadAlloc { | ||
fn allocate(&self, _l: Layout) -> Result<NonNull<[u8]>, AllocError> { | ||
Err(AllocError) | ||
} | ||
|
||
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) { | ||
unreachable!(); | ||
} | ||
} | ||
|
||
#[alloc_error_handler] | ||
fn alloc_error_handler(_: Layout) -> ! { | ||
extern "Rust" { | ||
fn miri_write_to_stderr(bytes: &[u8]); | ||
} | ||
let msg = "custom alloc error handler called!\n"; | ||
unsafe { miri_write_to_stderr(msg.as_bytes()) }; | ||
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) -> ! { | ||
core::intrinsics::abort(); | ||
} | ||
|
||
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 { | ||
let _b = Box::new_in(0, BadAlloc); | ||
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,29 @@ | ||
custom alloc error handler called! | ||
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 `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) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//@compile-flags: -Zoom=panic | ||
#![feature(allocator_api)] | ||
|
||
use std::alloc::*; | ||
use std::ptr::NonNull; | ||
|
||
struct BadAlloc; | ||
|
||
// Create a failing allocator; Miri's native allocator never fails so this is the only way to | ||
// actually call the alloc error handler. | ||
unsafe impl Allocator for BadAlloc { | ||
fn allocate(&self, _l: Layout) -> Result<NonNull<[u8]>, AllocError> { | ||
Err(AllocError) | ||
} | ||
|
||
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) { | ||
unreachable!(); | ||
} | ||
} | ||
|
||
struct Bomb; | ||
impl Drop for Bomb { | ||
fn drop(&mut self) { | ||
eprintln!("yes we are unwinding!"); | ||
} | ||
} | ||
|
||
fn main() { | ||
let bomb = Bomb; | ||
let _b = Box::new_in(0, BadAlloc); | ||
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 RUSTLIB/std/src/alloc.rs:LL:CC: | ||
memory allocation of 4 bytes failed | ||
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