Skip to content

Commit 4d10ff3

Browse files
committed
Support #[alloc_error_handler] without the allocator shim
Currently it is possible to avoid linking the allocator shim when __rust_no_alloc_shim_is_unstable_v2 is defined when linking rlibs directly as some build systems need. However this requires liballoc to be compiled with --cfg no_global_oom_handling, which places huge restrictions on what functions you can call and makes it impossible to use libstd. Or alternatively you have to define __rust_alloc_error_handler and (when using libstd) __rust_alloc_error_handler_should_panic using #[rustc_std_internal_symbol]. With this commit you can either use libstd and define __rust_alloc_error_handler_should_panic or not use libstd and use #[alloc_error_handler] instead. Both options are still unstable though. Eventually the alloc_error_handler may either be removed entirely (though the PR for that has been stale for years now) or we may start using weak symbols for it instead. For the latter case this commit is a prerequisite anyway.
1 parent 638cd60 commit 4d10ff3

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

alloc/src/alloc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
361361
unsafe extern "Rust" {
362362
// This is the magic symbol to call the global alloc error handler. rustc generates
363363
// it to call `__rg_oom` if there is a `#[alloc_error_handler]`, or to call the
364-
// default implementations below (`__rdl_oom`) otherwise.
364+
// default implementations below (`__rdl_alloc_error_handler`) otherwise.
365365
#[rustc_std_internal_symbol]
366366
fn __rust_alloc_error_handler(size: usize, align: usize) -> !;
367367
}
@@ -425,7 +425,7 @@ pub mod __alloc_error_handler {
425425
// called via generated `__rust_alloc_error_handler` if there is no
426426
// `#[alloc_error_handler]`.
427427
#[rustc_std_internal_symbol]
428-
pub unsafe fn __rdl_oom(size: usize, _align: usize) -> ! {
428+
pub unsafe fn __rdl_alloc_error_handler(size: usize, _align: usize) -> ! {
429429
unsafe extern "Rust" {
430430
// This symbol is emitted by rustc next to __rust_alloc_error_handler.
431431
// Its value depends on the -Zoom={panic,abort} compiler option.

std/src/alloc.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,10 @@ fn default_alloc_error_hook(layout: Layout) {
358358
// This is the default path taken on OOM, and the only path taken on stable with std.
359359
// Crucially, it does *not* call any user-defined code, and therefore users do not have to
360360
// worry about allocation failure causing reentrancy issues. That makes it different from
361-
// the default `__rdl_oom` defined in alloc (i.e., the default alloc error handler that is
362-
// called when there is no `#[alloc_error_handler]`), which triggers a regular panic and
363-
// thus can invoke a user-defined panic hook, executing arbitrary user-defined code.
361+
// the default `__rdl_alloc_error_handler` defined in alloc (i.e., the default alloc error
362+
// handler that is called when there is no `#[alloc_error_handler]`), which triggers a
363+
// regular panic and thus can invoke a user-defined panic hook, executing arbitrary
364+
// user-defined code.
364365
rtprintpanic!("memory allocation of {} bytes failed\n", layout.size());
365366
}
366367
}

0 commit comments

Comments
 (0)