Skip to content

Commit ffcc8f5

Browse files
authored
Rollup merge of #147526 - bjorn3:alloc_shim_weak_shape, r=petrochenkov,RalfJung
Move computation of allocator shim contents to cg_ssa In the future this should make it easier to use weak symbols for the allocator shim on platforms that properly support weak symbols. And it would allow reusing the allocator shim code for handling default implementations of the upcoming externally implementable items feature on platforms that don't properly support weak symbols. In addition to make this possible, the alloc error handler is now handled in a way such that it is possible to avoid using the allocator shim when liballoc is compiled without `no_global_oom_handling` if you use `#[alloc_error_handler]`. Previously this was only possible if you avoided liballoc entirely or compiled it with `no_global_oom_handling`. You still need to avoid libstd and to define the symbol that indicates that avoiding the allocator shim is unstable.
2 parents 09c2a34 + ceb6197 commit ffcc8f5

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

src/shims/foreign_items.rs

Lines changed: 14 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;
@@ -52,6 +52,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
5252

5353
// Some shims forward to other MIR bodies.
5454
match link_name.as_str() {
55+
// This allocator function has forwarding shims synthesized during normal codegen
56+
// (see `allocator_shim_contents`); this is where we emulate that behavior.
57+
// FIXME should use global_fn_name, but mangle_internal_symbol requires a static str.
5558
name if name == this.mangle_internal_symbol("__rust_alloc_error_handler") => {
5659
// Forward to the right symbol that implements this function.
5760
let Some(handler_kind) = this.tcx.alloc_error_handler_kind(()) else {
@@ -60,12 +63,16 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
6063
"`__rust_alloc_error_handler` cannot be called when no alloc error handler is set"
6164
);
6265
};
63-
let name = Symbol::intern(
64-
this.mangle_internal_symbol(alloc_error_handler_name(handler_kind)),
65-
);
66-
let handler =
67-
this.lookup_exported_symbol(name)?.expect("missing alloc error handler symbol");
68-
return interp_ok(Some(handler));
66+
if handler_kind == AllocatorKind::Default {
67+
let name =
68+
Symbol::intern(this.mangle_internal_symbol("__rdl_alloc_error_handler"));
69+
let handler = this
70+
.lookup_exported_symbol(name)?
71+
.expect("missing alloc error handler symbol");
72+
return interp_ok(Some(handler));
73+
}
74+
// Fall through to the `lookup_exported_symbol` below which should find
75+
// a `__rust_alloc_error_handler`.
6976
}
7077
_ => {}
7178
}

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)