-
Notifications
You must be signed in to change notification settings - Fork 390
implement support for __rust_alloc_error_handler #3478
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we care to change the tracked ID to keep this stderr file intact? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, oops |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
User code can directly call
std::alloc::handle_alloc_error(layout)
on stable to call the alloc error handler.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, thanks!