Skip to content

Commit 4a29aae

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 c6ea29f commit 4a29aae

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

src/shims/foreign_items.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::io::Write;
33
use std::path::Path;
44

55
use rustc_abi::{Align, AlignFromBytesError, CanonAbi, Size};
6-
use rustc_ast::expand::allocator::alloc_error_handler_name;
6+
use rustc_ast::expand::allocator::AllocatorKind;
77
use rustc_hir::attrs::Linkage;
88
use rustc_hir::def::DefKind;
99
use rustc_hir::def_id::CrateNum;
@@ -51,6 +51,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
5151

5252
// Some shims forward to other MIR bodies.
5353
match link_name.as_str() {
54+
// FIXME should use global_fn_name, but mangle_internal_symbol requires a static str.
5455
name if name == this.mangle_internal_symbol("__rust_alloc_error_handler") => {
5556
// Forward to the right symbol that implements this function.
5657
let Some(handler_kind) = this.tcx.alloc_error_handler_kind(()) else {
@@ -59,12 +60,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
5960
"`__rust_alloc_error_handler` cannot be called when no alloc error handler is set"
6061
);
6162
};
62-
let name = Symbol::intern(
63-
this.mangle_internal_symbol(alloc_error_handler_name(handler_kind)),
64-
);
65-
let handler =
66-
this.lookup_exported_symbol(name)?.expect("missing alloc error handler symbol");
67-
return interp_ok(Some(handler));
63+
if handler_kind == AllocatorKind::Default {
64+
let name =
65+
Symbol::intern(this.mangle_internal_symbol("__rdl_alloc_error_handler"));
66+
let handler = this
67+
.lookup_exported_symbol(name)?
68+
.expect("missing alloc error handler symbol");
69+
return interp_ok(Some(handler));
70+
}
6871
}
6972
_ => {}
7073
}

tests/fail/alloc/alloc_error_handler.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | crate::process::abort()
77
|
88
= note: BACKTRACE:
99
= note: inside `std::alloc::rust_oom` at RUSTLIB/std/src/alloc.rs:LL:CC
10-
= note: inside `std::alloc::_::__rg_oom` at RUSTLIB/std/src/alloc.rs:LL:CC
10+
= note: inside `std::alloc::_::__rust_alloc_error_handler` at RUSTLIB/std/src/alloc.rs:LL:CC
1111
= note: inside `std::alloc::handle_alloc_error::rt_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC
1212
= note: inside `std::alloc::handle_alloc_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC
1313
note: inside `main`

tests/fail/alloc/alloc_error_handler_custom.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | core::intrinsics::abort();
77
|
88
= note: BACKTRACE:
99
= note: inside `alloc_error_handler` at tests/fail/alloc/alloc_error_handler_custom.rs:LL:CC
10-
note: inside `_::__rg_oom`
10+
note: inside `_::__rust_alloc_error_handler`
1111
--> tests/fail/alloc/alloc_error_handler_custom.rs:LL:CC
1212
|
1313
LL | #[alloc_error_handler]

tests/fail/alloc/alloc_error_handler_no_std.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | core::intrinsics::abort();
99
|
1010
= note: BACKTRACE:
1111
= note: inside `panic_handler` at tests/fail/alloc/alloc_error_handler_no_std.rs:LL:CC
12-
= note: inside `alloc::alloc::__alloc_error_handler::__rdl_oom` at RUSTLIB/alloc/src/alloc.rs:LL:CC
12+
= note: inside `alloc::alloc::__alloc_error_handler::__rdl_alloc_error_handler` at RUSTLIB/alloc/src/alloc.rs:LL:CC
1313
= note: inside `alloc::alloc::handle_alloc_error::rt_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC
1414
= note: inside `alloc::alloc::handle_alloc_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC
1515
note: inside `miri_start`

0 commit comments

Comments
 (0)