Skip to content

Commit b166ad0

Browse files
committed
Rewrite implementation of #[alloc_error_handler]
The new implementation doesn't use weak lang items and instead changes `#[alloc_error_handler]` to an attribute macro just like `#[global_allocator]`. The attribute will generate the `__rg_oom` function which is called by the compiler-generated `__rust_alloc_error_handler`. If no `__rg_oom` function is defined in any crate then the compiler shim will call `__rdl_oom` in the alloc crate which will simply panic. This also fixes link errors with `-C link-dead-code` with `default_alloc_error_handler`: `__rg_oom` was previously defined in the alloc crate and would attempt to reference the `oom` lang item, even if it didn't exist. This worked as long as `__rg_oom` was excluded from linking since it was not called. This is a prerequisite for the stabilization of `default_alloc_error_handler` (#102318).
1 parent 33a92bc commit b166ad0

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/allocator.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::prelude::*;
55

66
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
77
use rustc_session::config::OomStrategy;
8+
use rustc_span::symbol::sym;
89

910
/// Returns whether an allocator shim was created
1011
pub(crate) fn codegen(
@@ -23,7 +24,7 @@ pub(crate) fn codegen(
2324
module,
2425
unwind_context,
2526
kind,
26-
tcx.lang_items().oom().is_some(),
27+
tcx.alloc_error_handler_kind(()).unwrap(),
2728
tcx.sess.opts.unstable_opts.oom,
2829
);
2930
true
@@ -36,7 +37,7 @@ fn codegen_inner(
3637
module: &mut impl Module,
3738
unwind_context: &mut UnwindContext,
3839
kind: AllocatorKind,
39-
has_alloc_error_handler: bool,
40+
alloc_error_handler_kind: AllocatorKind,
4041
oom_strategy: OomStrategy,
4142
) {
4243
let usize_ty = module.target_config().pointer_type();
@@ -108,12 +109,12 @@ fn codegen_inner(
108109
returns: vec![],
109110
};
110111

111-
let callee_name = if has_alloc_error_handler { "__rg_oom" } else { "__rdl_oom" };
112+
let callee_name = alloc_error_handler_kind.fn_name(sym::oom);
112113

113114
let func_id =
114115
module.declare_function("__rust_alloc_error_handler", Linkage::Export, &sig).unwrap();
115116

116-
let callee_func_id = module.declare_function(callee_name, Linkage::Import, &sig).unwrap();
117+
let callee_func_id = module.declare_function(&callee_name, Linkage::Import, &sig).unwrap();
117118

118119
let mut ctx = Context::new();
119120
ctx.func.signature = sig;

0 commit comments

Comments
 (0)