forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
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 rust-lang#76448 - haraldh:default_alloc_error_handler_r…
…educed, r=Amanieu Implement Make `handle_alloc_error` default to panic (for no_std + liballoc) Related: rust-lang#66741 Guarded with `#![feature(default_alloc_error_handler)]` a default `alloc_error_handler` is called, if a custom allocator is used and no other custom `#[alloc_error_handler]` is defined.
- Loading branch information
Showing
12 changed files
with
292 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
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
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
97 changes: 97 additions & 0 deletions
97
src/test/ui/allocator/no_std-alloc-error-handler-custom.rs
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,97 @@ | ||
// run-pass | ||
// ignore-android no libc | ||
// ignore-cloudabi no libc | ||
// ignore-emscripten no libc | ||
// ignore-sgx no libc | ||
// ignore-wasm32 no libc | ||
// only-linux | ||
// compile-flags:-C panic=abort | ||
// aux-build:helper.rs | ||
|
||
#![feature(start, rustc_private, new_uninit, panic_info_message)] | ||
#![feature(alloc_error_handler)] | ||
#![no_std] | ||
|
||
extern crate alloc; | ||
extern crate libc; | ||
|
||
// ARM targets need these symbols | ||
#[no_mangle] | ||
pub fn __aeabi_unwind_cpp_pr0() {} | ||
|
||
#[no_mangle] | ||
pub fn __aeabi_unwind_cpp_pr1() {} | ||
|
||
use core::ptr::null_mut; | ||
use core::alloc::{GlobalAlloc, Layout}; | ||
use alloc::boxed::Box; | ||
|
||
extern crate helper; | ||
|
||
struct MyAllocator; | ||
|
||
#[alloc_error_handler] | ||
fn my_oom(layout: Layout) -> ! | ||
{ | ||
use alloc::fmt::write; | ||
unsafe { | ||
let size = layout.size(); | ||
let mut s = alloc::string::String::new(); | ||
write(&mut s, format_args!("My OOM: failed to allocate {} bytes!\n", size)).unwrap(); | ||
let s = s.as_str(); | ||
libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len()); | ||
libc::exit(0) | ||
} | ||
} | ||
|
||
unsafe impl GlobalAlloc for MyAllocator { | ||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
if layout.size() < 4096 { | ||
libc::malloc(layout.size()) as _ | ||
} else { | ||
null_mut() | ||
} | ||
} | ||
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} | ||
} | ||
|
||
#[global_allocator] | ||
static A: MyAllocator = MyAllocator; | ||
|
||
#[panic_handler] | ||
fn panic(panic_info: &core::panic::PanicInfo) -> ! { | ||
unsafe { | ||
if let Some(s) = panic_info.payload().downcast_ref::<&str>() { | ||
const PSTR: &str = "panic occurred: "; | ||
const CR: &str = "\n"; | ||
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len()); | ||
libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len()); | ||
libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len()); | ||
} | ||
if let Some(args) = panic_info.message() { | ||
let mut s = alloc::string::String::new(); | ||
alloc::fmt::write(&mut s, *args).unwrap(); | ||
let s = s.as_str(); | ||
const PSTR: &str = "panic occurred: "; | ||
const CR: &str = "\n"; | ||
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len()); | ||
libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len()); | ||
libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len()); | ||
} else { | ||
const PSTR: &str = "panic occurred\n"; | ||
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len()); | ||
} | ||
libc::exit(1) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Page([[u64; 32]; 16]); | ||
|
||
#[start] | ||
pub fn main(_argc: isize, _argv: *const *const u8) -> isize { | ||
let zero = Box::<Page>::new_zeroed(); | ||
let zero = unsafe { zero.assume_init() }; | ||
helper::work_with(&zero); | ||
1 | ||
} |
84 changes: 84 additions & 0 deletions
84
src/test/ui/allocator/no_std-alloc-error-handler-default.rs
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,84 @@ | ||
// run-pass | ||
// ignore-android no libc | ||
// ignore-cloudabi no libc | ||
// ignore-emscripten no libc | ||
// ignore-sgx no libc | ||
// ignore-wasm32 no libc | ||
// only-linux | ||
// compile-flags:-C panic=abort | ||
// aux-build:helper.rs | ||
// gate-test-default_alloc_error_handler | ||
|
||
#![feature(start, rustc_private, new_uninit, panic_info_message)] | ||
#![feature(default_alloc_error_handler)] | ||
#![no_std] | ||
|
||
extern crate alloc; | ||
extern crate libc; | ||
|
||
// ARM targets need these symbols | ||
#[no_mangle] | ||
pub fn __aeabi_unwind_cpp_pr0() {} | ||
|
||
#[no_mangle] | ||
pub fn __aeabi_unwind_cpp_pr1() {} | ||
|
||
use alloc::boxed::Box; | ||
use core::alloc::{GlobalAlloc, Layout}; | ||
use core::ptr::null_mut; | ||
|
||
extern crate helper; | ||
|
||
struct MyAllocator; | ||
|
||
unsafe impl GlobalAlloc for MyAllocator { | ||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
if layout.size() < 4096 { | ||
libc::malloc(layout.size()) as _ | ||
} else { | ||
null_mut() | ||
} | ||
} | ||
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} | ||
} | ||
|
||
#[global_allocator] | ||
static A: MyAllocator = MyAllocator; | ||
|
||
#[panic_handler] | ||
fn panic(panic_info: &core::panic::PanicInfo) -> ! { | ||
unsafe { | ||
if let Some(s) = panic_info.payload().downcast_ref::<&str>() { | ||
const PSTR: &str = "panic occurred: "; | ||
const CR: &str = "\n"; | ||
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len()); | ||
libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len()); | ||
libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len()); | ||
} | ||
if let Some(args) = panic_info.message() { | ||
let mut s = alloc::string::String::new(); | ||
alloc::fmt::write(&mut s, *args).unwrap(); | ||
let s = s.as_str(); | ||
const PSTR: &str = "panic occurred: "; | ||
const CR: &str = "\n"; | ||
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len()); | ||
libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len()); | ||
libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len()); | ||
} else { | ||
const PSTR: &str = "panic occurred\n"; | ||
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len()); | ||
} | ||
libc::exit(0) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Page([[u64; 32]; 16]); | ||
|
||
#[start] | ||
pub fn main(_argc: isize, _argv: *const *const u8) -> isize { | ||
let zero = Box::<Page>::new_zeroed(); | ||
let zero = unsafe { zero.assume_init() }; | ||
helper::work_with(&zero); | ||
1 | ||
} |
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,4 +1,6 @@ | ||
error: `#[alloc_error_handler]` function required, but not found | ||
error: `#[alloc_error_handler]` function required, but not found. | ||
|
||
note: Use `#![feature(default_alloc_error_handler)]` for a default error handler. | ||
|
||
error: aborting due to previous error | ||
|