Skip to content

Commit d10f613

Browse files
committed
alloc_error_handler tests: directly call handle_alloc_error; test more codepaths
1 parent 8ad72b2 commit d10f613

9 files changed

+122
-75
lines changed

src/tools/miri/tests/fail/alloc/alloc_error_handler.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,7 @@
44
#![feature(allocator_api)]
55

66
use std::alloc::*;
7-
use std::ptr::NonNull;
8-
9-
struct BadAlloc;
10-
11-
// Create a failing allocator; Miri's native allocator never fails so this is the only way to
12-
// actually call the alloc error handler.
13-
unsafe impl Allocator for BadAlloc {
14-
fn allocate(&self, _l: Layout) -> Result<NonNull<[u8]>, AllocError> {
15-
Err(AllocError)
16-
}
17-
18-
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {
19-
unreachable!();
20-
}
21-
}
227

238
fn main() {
24-
let _b = Box::new_in(0, BadAlloc);
9+
handle_alloc_error(Layout::for_value(&0));
2510
}

src/tools/miri/tests/fail/alloc/alloc_error_handler.stderr

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ LL | ABORT();
1212
= note: inside `std::alloc::_::__rg_oom` at RUSTLIB/std/src/alloc.rs:LL:CC
1313
= note: inside `std::alloc::handle_alloc_error::rt_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC
1414
= note: inside `std::alloc::handle_alloc_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC
15-
= note: inside `std::boxed::Box::<i32, BadAlloc>::new_uninit_in` at RUSTLIB/alloc/src/boxed.rs:LL:CC
16-
= note: inside `std::boxed::Box::<i32, BadAlloc>::new_in` at RUSTLIB/alloc/src/boxed.rs:LL:CC
1715
note: inside `main`
1816
--> $DIR/alloc_error_handler.rs:LL:CC
1917
|
20-
LL | let _b = Box::new_in(0, BadAlloc);
18+
LL | handle_alloc_error(Layout::for_value(&0));
2119
| ^
2220

2321
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//@compile-flags: -Cpanic=abort
2+
#![feature(start, core_intrinsics)]
3+
#![feature(alloc_error_handler)]
4+
#![feature(allocator_api)]
5+
#![no_std]
6+
7+
extern crate alloc;
8+
9+
use alloc::alloc::*;
10+
11+
extern "Rust" {
12+
fn miri_write_to_stderr(bytes: &[u8]);
13+
}
14+
15+
#[alloc_error_handler]
16+
fn alloc_error_handler(_: Layout) -> ! {
17+
let msg = "custom alloc error handler called!\n";
18+
unsafe { miri_write_to_stderr(msg.as_bytes()) };
19+
core::intrinsics::abort(); //~ERROR: aborted
20+
}
21+
22+
// rustc requires us to provide some more things that aren't actually used by this test
23+
mod plumbing {
24+
use super::*;
25+
26+
#[panic_handler]
27+
fn panic_handler(_: &core::panic::PanicInfo) -> ! {
28+
let msg = "custom panic handler called!\n";
29+
unsafe { miri_write_to_stderr(msg.as_bytes()) };
30+
core::intrinsics::abort();
31+
}
32+
33+
struct NoAlloc;
34+
35+
unsafe impl GlobalAlloc for NoAlloc {
36+
unsafe fn alloc(&self, _: Layout) -> *mut u8 {
37+
unreachable!();
38+
}
39+
40+
unsafe fn dealloc(&self, _: *mut u8, _: Layout) {
41+
unreachable!();
42+
}
43+
}
44+
45+
#[global_allocator]
46+
static GLOBAL: NoAlloc = NoAlloc;
47+
}
48+
49+
#[start]
50+
fn start(_: isize, _: *const *const u8) -> isize {
51+
handle_alloc_error(Layout::for_value(&0));
52+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
custom alloc error handler called!
2+
error: abnormal termination: the program aborted execution
3+
--> $DIR/alloc_error_handler_custom.rs:LL:CC
4+
|
5+
LL | core::intrinsics::abort();
6+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the program aborted execution
7+
|
8+
= note: BACKTRACE:
9+
= note: inside `alloc_error_handler` at $DIR/alloc_error_handler_custom.rs:LL:CC
10+
note: inside `_::__rg_oom`
11+
--> $DIR/alloc_error_handler_custom.rs:LL:CC
12+
|
13+
LL | #[alloc_error_handler]
14+
| ---------------------- in this procedural macro expansion
15+
LL | fn alloc_error_handler(_: Layout) -> ! {
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
= note: inside `alloc::alloc::handle_alloc_error::rt_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC
18+
= note: inside `alloc::alloc::handle_alloc_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC
19+
note: inside `start`
20+
--> $DIR/alloc_error_handler_custom.rs:LL:CC
21+
|
22+
LL | handle_alloc_error(Layout::for_value(&0));
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
25+
26+
error: aborting due to 1 previous error
27+

src/tools/miri/tests/fail/alloc/alloc_error_handler_no_std.rs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,16 @@
77
extern crate alloc;
88

99
use alloc::alloc::*;
10-
use alloc::boxed::Box;
11-
use core::ptr::NonNull;
1210

13-
struct BadAlloc;
14-
15-
// Create a failing allocator; that is the only way to actually call the alloc error handler.
16-
unsafe impl Allocator for BadAlloc {
17-
fn allocate(&self, _l: Layout) -> Result<NonNull<[u8]>, AllocError> {
18-
Err(AllocError)
19-
}
20-
21-
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {
22-
unreachable!();
23-
}
11+
extern "Rust" {
12+
fn miri_write_to_stderr(bytes: &[u8]);
2413
}
2514

26-
#[alloc_error_handler]
27-
fn alloc_error_handler(_: Layout) -> ! {
28-
extern "Rust" {
29-
fn miri_write_to_stderr(bytes: &[u8]);
30-
}
31-
let msg = "custom alloc error handler called!\n";
15+
// The default no_std alloc_error_handler is a panic.
16+
17+
#[panic_handler]
18+
fn panic_handler(_panic_info: &core::panic::PanicInfo) -> ! {
19+
let msg = "custom panic handler called!\n";
3220
unsafe { miri_write_to_stderr(msg.as_bytes()) };
3321
core::intrinsics::abort(); //~ERROR: aborted
3422
}
@@ -37,11 +25,6 @@ fn alloc_error_handler(_: Layout) -> ! {
3725
mod plumbing {
3826
use super::*;
3927

40-
#[panic_handler]
41-
fn panic_handler(_: &core::panic::PanicInfo) -> ! {
42-
core::intrinsics::abort();
43-
}
44-
4528
struct NoAlloc;
4629

4730
unsafe impl GlobalAlloc for NoAlloc {
@@ -60,6 +43,5 @@ mod plumbing {
6043

6144
#[start]
6245
fn start(_: isize, _: *const *const u8) -> isize {
63-
let _b = Box::new_in(0, BadAlloc);
64-
0
46+
handle_alloc_error(Layout::for_value(&0));
6547
}
Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
1-
custom alloc error handler called!
1+
custom panic handler called!
22
error: abnormal termination: the program aborted execution
33
--> $DIR/alloc_error_handler_no_std.rs:LL:CC
44
|
55
LL | core::intrinsics::abort();
66
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the program aborted execution
77
|
88
= note: BACKTRACE:
9-
= note: inside `alloc_error_handler` at $DIR/alloc_error_handler_no_std.rs:LL:CC
10-
note: inside `_::__rg_oom`
11-
--> $DIR/alloc_error_handler_no_std.rs:LL:CC
12-
|
13-
LL | #[alloc_error_handler]
14-
| ---------------------- in this procedural macro expansion
15-
LL | fn alloc_error_handler(_: Layout) -> ! {
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9+
= note: inside `panic_handler` at $DIR/alloc_error_handler_no_std.rs:LL:CC
10+
= note: inside `alloc::alloc::__alloc_error_handler::__rdl_oom` at RUSTLIB/alloc/src/alloc.rs:LL:CC
1711
= note: inside `alloc::alloc::handle_alloc_error::rt_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC
1812
= note: inside `alloc::alloc::handle_alloc_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC
19-
= note: inside `alloc::boxed::Box::<i32, BadAlloc>::new_uninit_in` at RUSTLIB/alloc/src/boxed.rs:LL:CC
20-
= note: inside `alloc::boxed::Box::<i32, BadAlloc>::new_in` at RUSTLIB/alloc/src/boxed.rs:LL:CC
2113
note: inside `start`
2214
--> $DIR/alloc_error_handler_no_std.rs:LL:CC
2315
|
24-
LL | let _b = Box::new_in(0, BadAlloc);
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^
26-
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
LL | handle_alloc_error(Layout::for_value(&0));
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
19+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
2720

2821
error: aborting due to 1 previous error
2922

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(allocator_api, alloc_error_hook)]
2+
3+
use std::alloc::*;
4+
5+
struct Bomb;
6+
impl Drop for Bomb {
7+
fn drop(&mut self) {
8+
eprintln!("yes we are unwinding!");
9+
}
10+
}
11+
12+
#[allow(unreachable_code, unused_variables)]
13+
fn main() {
14+
// This is a particularly tricky hook, since it unwinds, which the default one does not.
15+
set_alloc_error_hook(|_layout| panic!("alloc error hook called"));
16+
17+
let bomb = Bomb;
18+
handle_alloc_error(Layout::for_value(&0));
19+
std::mem::forget(bomb); // defuse unwinding bomb
20+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
thread 'main' panicked at $DIR/alloc_error_handler_hook.rs:LL:CC:
2+
alloc error hook called
3+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
4+
yes we are unwinding!

src/tools/miri/tests/panic/alloc_error_handler_panic.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,6 @@
22
#![feature(allocator_api)]
33

44
use std::alloc::*;
5-
use std::ptr::NonNull;
6-
7-
struct BadAlloc;
8-
9-
// Create a failing allocator; Miri's native allocator never fails so this is the only way to
10-
// actually call the alloc error handler.
11-
unsafe impl Allocator for BadAlloc {
12-
fn allocate(&self, _l: Layout) -> Result<NonNull<[u8]>, AllocError> {
13-
Err(AllocError)
14-
}
15-
16-
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {
17-
unreachable!();
18-
}
19-
}
205

216
struct Bomb;
227
impl Drop for Bomb {
@@ -25,8 +10,9 @@ impl Drop for Bomb {
2510
}
2611
}
2712

13+
#[allow(unreachable_code, unused_variables)]
2814
fn main() {
2915
let bomb = Bomb;
30-
let _b = Box::new_in(0, BadAlloc);
16+
handle_alloc_error(Layout::for_value(&0));
3117
std::mem::forget(bomb); // defuse unwinding bomb
3218
}

0 commit comments

Comments
 (0)