Skip to content

Commit

Permalink
Add UI test to ensure no more allocations pre-main
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Apr 7, 2024
1 parent 87401ca commit 778330b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/ui/runtime/aborting-alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! Helper for 'no-allocation-before-main'.
//!
//! This also contains a meta-test to make sure that the AbortingAllocator does indeed abort.
//!
//! -Cprefer-dynamic=no is required as otherwise #[global_allocator] does nothing.
//@ run-fail
//@ compile-flags: -Cprefer-dynamic=no

pub struct AbortingAllocator;

unsafe impl std::alloc::GlobalAlloc for AbortingAllocator {
unsafe fn alloc(&self, _: std::alloc::Layout) -> *mut u8 {
std::process::abort()
}

unsafe fn dealloc(&self, _: *mut u8, _: std::alloc::Layout) {
std::process::abort()
}
}

#[global_allocator]
static ALLOCATOR: AbortingAllocator = AbortingAllocator;

fn main() {
std::hint::black_box(String::from("An allocation"));
}
13 changes: 13 additions & 0 deletions tests/ui/runtime/no-allocation-before-main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! Tests that a program with no body does not allocate.
//!
//! The initial runtime should not allocate for performance/binary size reasons.
//!
//! -Cprefer-dynamic=no is required as otherwise #[global_allocator] does nothing.
//@ run-pass
//@ compile-flags: -Cprefer-dynamic=no

#[allow(dead_code)]
#[path = "aborting-alloc.rs"]
mod aux;

fn main() {}

0 comments on commit 778330b

Please sign in to comment.