Skip to content

Commit daf2b32

Browse files
committed
Print backtrace in AbortingAllocator
1 parent 0cd4b46 commit daf2b32

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

tests/ui/runtime/aborting-alloc.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,34 @@
66
//@ run-fail
77
//@ compile-flags: -Cprefer-dynamic=no
88

9-
pub struct AbortingAllocator;
9+
use std::{sync::atomic::{AtomicBool, Ordering}, alloc::System};
10+
11+
static ABORT: AtomicBool = AtomicBool::new(true);
12+
13+
pub struct AbortingAllocator(System);
1014

1115
unsafe impl std::alloc::GlobalAlloc for AbortingAllocator {
12-
unsafe fn alloc(&self, _: std::alloc::Layout) -> *mut u8 {
13-
std::process::abort()
16+
unsafe fn alloc(&self, layout: std::alloc::Layout) -> *mut u8 {
17+
if ABORT.swap(false, Ordering::SeqCst) {
18+
println!("{}", std::backtrace::Backtrace::force_capture());
19+
std::process::abort();
20+
}
21+
22+
self.0.alloc(layout)
1423
}
1524

16-
unsafe fn dealloc(&self, _: *mut u8, _: std::alloc::Layout) {
17-
std::process::abort()
25+
unsafe fn dealloc(&self, ptr: *mut u8, layout: std::alloc::Layout) {
26+
if ABORT.swap(false, Ordering::SeqCst) {
27+
println!("{}", std::backtrace::Backtrace::force_capture());
28+
std::process::abort();
29+
}
30+
31+
self.0.dealloc(ptr, layout)
1832
}
1933
}
2034

2135
#[global_allocator]
22-
static ALLOCATOR: AbortingAllocator = AbortingAllocator;
36+
static ALLOCATOR: AbortingAllocator = AbortingAllocator(System);
2337

2438
fn main() {
2539
std::hint::black_box(String::from("An allocation"));

0 commit comments

Comments
 (0)