Skip to content

Commit 2c45b39

Browse files
committed
Add UI test to ensure no more allocations pre-main
1 parent 1f11a0d commit 2c45b39

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

tests/ui/runtime/aborting-alloc.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//! Helper for 'no-allocation-before-main'.
2+
//!
3+
//! This also contains a meta-test to make sure that the AbortingAllocator does indeed abort.
4+
//!
5+
//! -Cprefer-dynamic=no is required as otherwise #[global_allocator] does nothing.
6+
//@ run-fail
7+
//@ compile-flags: -Cprefer-dynamic=no
8+
9+
pub struct AbortingAllocator;
10+
11+
unsafe impl std::alloc::GlobalAlloc for AbortingAllocator {
12+
unsafe fn alloc(&self, _: std::alloc::Layout) -> *mut u8 {
13+
std::process::abort()
14+
}
15+
16+
unsafe fn dealloc(&self, _: *mut u8, _: std::alloc::Layout) {
17+
std::process::abort()
18+
}
19+
}
20+
21+
#[global_allocator]
22+
static ALLOCATOR: AbortingAllocator = AbortingAllocator;
23+
24+
fn main() {
25+
std::hint::black_box(String::from("An allocation"));
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//! Tests that a program with no body does not allocate.
2+
//!
3+
//! The initial runtime should not allocate for performance/binary size reasons.
4+
//!
5+
//! -Cprefer-dynamic=no is required as otherwise #[global_allocator] does nothing.
6+
//@ run-pass
7+
//@ compile-flags: -Cprefer-dynamic=no
8+
9+
#[allow(dead_code)]
10+
#[path = "aborting-alloc.rs"]
11+
mod aux;
12+
13+
fn main() {}

0 commit comments

Comments
 (0)