Skip to content

Commit b0a00dc

Browse files
committed
Auto merge of rust-lang#76448 - haraldh:default_alloc_error_handler_reduced, 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.
2 parents 94a3620 + 5052107 commit b0a00dc

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

alloc/src/alloc.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ extern "Rust" {
2626
fn __rust_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8;
2727
#[rustc_allocator_nounwind]
2828
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
29+
#[rustc_allocator_nounwind]
30+
fn __rust_alloc_error_handler(size: usize, align: usize) -> !;
2931
}
3032

3133
/// The global memory allocator.
@@ -334,6 +336,24 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
334336
/// [`set_alloc_error_hook`]: ../../std/alloc/fn.set_alloc_error_hook.html
335337
/// [`take_alloc_error_hook`]: ../../std/alloc/fn.take_alloc_error_hook.html
336338
#[stable(feature = "global_alloc", since = "1.28.0")]
339+
#[cfg(not(any(test, bootstrap)))]
340+
#[rustc_allocator_nounwind]
341+
pub fn handle_alloc_error(layout: Layout) -> ! {
342+
unsafe {
343+
__rust_alloc_error_handler(layout.size(), layout.align());
344+
}
345+
}
346+
347+
// For alloc test `std::alloc::handle_alloc_error` can be used directly.
348+
#[cfg(test)]
349+
pub use std::alloc::handle_alloc_error;
350+
351+
// In stage0 (bootstrap) `__rust_alloc_error_handler`,
352+
// might not be generated yet, because an old compiler is used,
353+
// so use the old direct call.
354+
#[cfg(all(bootstrap, not(test)))]
355+
#[stable(feature = "global_alloc", since = "1.28.0")]
356+
#[doc(hidden)]
337357
#[rustc_allocator_nounwind]
338358
pub fn handle_alloc_error(layout: Layout) -> ! {
339359
extern "Rust" {
@@ -342,3 +362,30 @@ pub fn handle_alloc_error(layout: Layout) -> ! {
342362
}
343363
unsafe { oom_impl(layout) }
344364
}
365+
366+
#[cfg(not(any(test, bootstrap)))]
367+
#[doc(hidden)]
368+
#[allow(unused_attributes)]
369+
#[unstable(feature = "alloc_internals", issue = "none")]
370+
pub mod __default_lib_allocator {
371+
use crate::alloc::Layout;
372+
373+
// called via generated `__rust_alloc_error_handler`
374+
375+
// if there is no `#[alloc_error_handler]`
376+
#[rustc_std_internal_symbol]
377+
pub unsafe extern "C" fn __rdl_oom(size: usize, _align: usize) -> ! {
378+
panic!("memory allocation of {} bytes failed", size)
379+
}
380+
381+
// if there is a `#[alloc_error_handler]`
382+
#[rustc_std_internal_symbol]
383+
pub unsafe extern "C" fn __rg_oom(size: usize, align: usize) -> ! {
384+
let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
385+
extern "Rust" {
386+
#[lang = "oom"]
387+
fn oom_impl(layout: Layout) -> !;
388+
}
389+
unsafe { oom_impl(layout) }
390+
}
391+
}

0 commit comments

Comments
 (0)