Skip to content

Commit 1dd2f56

Browse files
committed
Use weak linkage for the alloc error handler too
1 parent b7c3171 commit 1dd2f56

File tree

17 files changed

+40
-332
lines changed

17 files changed

+40
-332
lines changed

compiler/rustc_ast/src/expand/allocator.rs

-7
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ pub fn global_fn_name(base: Symbol) -> String {
1111
format!("__rust_{base}")
1212
}
1313

14-
pub fn alloc_error_handler_name(alloc_error_handler_kind: AllocatorKind) -> &'static str {
15-
match alloc_error_handler_kind {
16-
AllocatorKind::Global => "__rg_oom",
17-
AllocatorKind::Default => "__rdl_oom",
18-
}
19-
}
20-
2114
pub const NO_ALLOC_SHIM_IS_UNSTABLE: &str = "__rust_no_alloc_shim_is_unstable";
2215

2316
pub enum AllocatorTy {

compiler/rustc_builtin_macros/src/alloc_error_handler.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub(crate) fn expand(
5656
}
5757

5858
// #[rustc_std_internal_symbol]
59-
// unsafe fn __rg_oom(size: usize, align: usize) -> ! {
59+
// unsafe fn __rust_alloc_error_handler(size: usize, align: usize) -> ! {
6060
// handler(core::alloc::Layout::from_size_align_unchecked(size, align))
6161
// }
6262
fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span) -> Stmt {
@@ -90,6 +90,7 @@ fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span
9090

9191
let attrs = thin_vec![cx.attr_word(sym::rustc_std_internal_symbol, span)];
9292

93-
let item = cx.item(span, Ident::from_str_and_span("__rg_oom", span), attrs, kind);
93+
let item =
94+
cx.item(span, Ident::from_str_and_span("__rust_alloc_error_handler", span), attrs, kind);
9495
cx.stmt_item(sig_span, item)
9596
}

compiler/rustc_codegen_cranelift/src/allocator.rs

+3-27
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
//! Allocator shim
22
// Adapted from rustc
33

4-
use rustc_ast::expand::allocator::{
5-
AllocatorKind, NO_ALLOC_SHIM_IS_UNSTABLE, alloc_error_handler_name,
6-
};
4+
use rustc_ast::expand::allocator::NO_ALLOC_SHIM_IS_UNSTABLE;
75
use rustc_codegen_ssa::base::needs_allocator_shim;
86
use rustc_session::config::OomStrategy;
97

@@ -12,36 +10,14 @@ use crate::prelude::*;
1210
/// Returns whether an allocator shim was created
1311
pub(crate) fn codegen(tcx: TyCtxt<'_>, module: &mut dyn Module) -> bool {
1412
if needs_allocator_shim(tcx) {
15-
codegen_inner(
16-
module,
17-
tcx.alloc_error_handler_kind(()).unwrap(),
18-
tcx.sess.opts.unstable_opts.oom,
19-
);
13+
codegen_inner(module, tcx.sess.opts.unstable_opts.oom);
2014
true
2115
} else {
2216
false
2317
}
2418
}
2519

26-
fn codegen_inner(
27-
module: &mut dyn Module,
28-
alloc_error_handler_kind: AllocatorKind,
29-
oom_strategy: OomStrategy,
30-
) {
31-
let usize_ty = module.target_config().pointer_type();
32-
33-
let sig = Signature {
34-
call_conv: module.target_config().default_call_conv,
35-
params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
36-
returns: vec![],
37-
};
38-
crate::common::create_wrapper_function(
39-
module,
40-
sig,
41-
"__rust_alloc_error_handler",
42-
&alloc_error_handler_name(alloc_error_handler_kind),
43-
);
44-
20+
fn codegen_inner(module: &mut dyn Module, oom_strategy: OomStrategy) {
4521
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
4622
let mut data = DataDescription::new();
4723
data.set_align(1);
+3-103
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,14 @@
1-
#[cfg(feature = "master")]
2-
use gccjit::FnAttribute;
3-
use gccjit::{Context, FunctionType, GlobalKind, ToRValue, Type};
4-
use rustc_ast::expand::allocator::{
5-
AllocatorKind, NO_ALLOC_SHIM_IS_UNSTABLE, alloc_error_handler_name,
6-
};
7-
use rustc_middle::bug;
1+
use gccjit::GlobalKind;
2+
use rustc_ast::expand::allocator::NO_ALLOC_SHIM_IS_UNSTABLE;
83
use rustc_middle::ty::TyCtxt;
94
use rustc_session::config::OomStrategy;
105

116
use crate::GccContext;
127

13-
pub(crate) unsafe fn codegen(
14-
tcx: TyCtxt<'_>,
15-
mods: &mut GccContext,
16-
_module_name: &str,
17-
alloc_error_handler_kind: AllocatorKind,
18-
) {
8+
pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_name: &str) {
199
let context = &mods.context;
20-
let usize = match tcx.sess.target.pointer_width {
21-
16 => context.new_type::<u16>(),
22-
32 => context.new_type::<u32>(),
23-
64 => context.new_type::<u64>(),
24-
tws => bug!("Unsupported target word size for int: {}", tws),
25-
};
2610
let i8 = context.new_type::<i8>();
2711

28-
// FIXME(bjorn3): Add noreturn attribute
29-
create_wrapper_function(
30-
tcx,
31-
context,
32-
"__rust_alloc_error_handler",
33-
alloc_error_handler_name(alloc_error_handler_kind),
34-
&[usize, usize],
35-
None,
36-
);
37-
3812
let name = OomStrategy::SYMBOL.to_string();
3913
let global = context.new_global(None, GlobalKind::Exported, i8, name);
4014
let value = tcx.sess.opts.unstable_opts.oom.should_panic();
@@ -46,77 +20,3 @@ pub(crate) unsafe fn codegen(
4620
let value = context.new_rvalue_from_int(i8, 0);
4721
global.global_set_initializer_rvalue(value);
4822
}
49-
50-
fn create_wrapper_function(
51-
tcx: TyCtxt<'_>,
52-
context: &Context<'_>,
53-
from_name: &str,
54-
to_name: &str,
55-
types: &[Type<'_>],
56-
output: Option<Type<'_>>,
57-
) {
58-
let void = context.new_type::<()>();
59-
60-
let args: Vec<_> = types
61-
.iter()
62-
.enumerate()
63-
.map(|(index, typ)| context.new_parameter(None, *typ, format!("param{}", index)))
64-
.collect();
65-
let func = context.new_function(
66-
None,
67-
FunctionType::Exported,
68-
output.unwrap_or(void),
69-
&args,
70-
from_name,
71-
false,
72-
);
73-
74-
#[cfg(feature = "master")]
75-
match tcx.sess.default_visibility() {
76-
rustc_target::spec::SymbolVisibility::Hidden => {
77-
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden))
78-
}
79-
rustc_target::spec::SymbolVisibility::Protected => {
80-
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Protected))
81-
}
82-
rustc_target::spec::SymbolVisibility::Interposable => {}
83-
}
84-
85-
if tcx.sess.must_emit_unwind_tables() {
86-
// TODO(antoyo): emit unwind tables.
87-
}
88-
89-
let args: Vec<_> = types
90-
.iter()
91-
.enumerate()
92-
.map(|(index, typ)| context.new_parameter(None, *typ, format!("param{}", index)))
93-
.collect();
94-
let callee = context.new_function(
95-
None,
96-
FunctionType::Extern,
97-
output.unwrap_or(void),
98-
&args,
99-
to_name,
100-
false,
101-
);
102-
#[cfg(feature = "master")]
103-
callee.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
104-
105-
let block = func.new_block("entry");
106-
107-
let args = args
108-
.iter()
109-
.enumerate()
110-
.map(|(i, _)| func.get_param(i as i32).to_rvalue())
111-
.collect::<Vec<_>>();
112-
let ret = context.new_call(None, callee, &args);
113-
//llvm::LLVMSetTailCall(ret, True);
114-
if output.is_some() {
115-
block.end_with_return(None, ret);
116-
} else {
117-
block.end_with_void_return(None);
118-
}
119-
120-
// TODO(@Commeownist): Check if we need to emit some extra debugging info in certain circumstances
121-
// as described in https://github.com/rust-lang/rust/commit/77a96ed5646f7c3ee8897693decc4626fe380643
122-
}

compiler/rustc_codegen_gcc/src/lib.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ use errors::LTONotSupported;
9292
use gccjit::{CType, Context, OptimizationLevel};
9393
#[cfg(feature = "master")]
9494
use gccjit::{TargetInfo, Version};
95-
use rustc_ast::expand::allocator::AllocatorKind;
9695
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
9796
use rustc_codegen_ssa::back::write::{
9897
CodegenContext, FatLtoInput, ModuleConfig, TargetMachineFactoryFn,
@@ -287,20 +286,15 @@ fn new_context<'gcc, 'tcx>(tcx: TyCtxt<'tcx>) -> Context<'gcc> {
287286
}
288287

289288
impl ExtraBackendMethods for GccCodegenBackend {
290-
fn codegen_allocator(
291-
&self,
292-
tcx: TyCtxt<'_>,
293-
module_name: &str,
294-
alloc_error_handler_kind: AllocatorKind,
295-
) -> Self::Module {
289+
fn codegen_allocator(&self, tcx: TyCtxt<'_>, module_name: &str) -> Self::Module {
296290
let mut mods = GccContext {
297291
context: Arc::new(SyncContext::new(new_context(tcx))),
298292
should_combine_object_files: false,
299293
temp_dir: None,
300294
};
301295

302296
unsafe {
303-
allocator::codegen(tcx, &mut mods, module_name, alloc_error_handler_kind);
297+
allocator::codegen(tcx, &mut mods, module_name);
304298
}
305299
mods
306300
}
+4-110
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,16 @@
1-
use libc::c_uint;
2-
use rustc_ast::expand::allocator::{
3-
AllocatorKind, NO_ALLOC_SHIM_IS_UNSTABLE, alloc_error_handler_name,
4-
};
5-
use rustc_middle::bug;
1+
use rustc_ast::expand::allocator::NO_ALLOC_SHIM_IS_UNSTABLE;
62
use rustc_middle::ty::TyCtxt;
73
use rustc_session::config::{DebugInfo, OomStrategy};
84

95
use crate::common::AsCCharPtr;
10-
use crate::llvm::{self, Context, False, Module, True, Type};
11-
use crate::{ModuleLlvm, attributes, debuginfo};
6+
use crate::llvm::{self, False};
7+
use crate::{ModuleLlvm, debuginfo};
128

13-
pub(crate) unsafe fn codegen(
14-
tcx: TyCtxt<'_>,
15-
module_llvm: &mut ModuleLlvm,
16-
module_name: &str,
17-
alloc_error_handler_kind: AllocatorKind,
18-
) {
9+
pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, module_llvm: &mut ModuleLlvm, module_name: &str) {
1910
let llcx = &*module_llvm.llcx;
2011
let llmod = module_llvm.llmod();
21-
let usize = unsafe {
22-
match tcx.sess.target.pointer_width {
23-
16 => llvm::LLVMInt16TypeInContext(llcx),
24-
32 => llvm::LLVMInt32TypeInContext(llcx),
25-
64 => llvm::LLVMInt64TypeInContext(llcx),
26-
tws => bug!("Unsupported target word size for int: {}", tws),
27-
}
28-
};
2912
let i8 = unsafe { llvm::LLVMInt8TypeInContext(llcx) };
3013

31-
// rust alloc error handler
32-
create_wrapper_function(
33-
tcx,
34-
llcx,
35-
llmod,
36-
"__rust_alloc_error_handler",
37-
alloc_error_handler_name(alloc_error_handler_kind),
38-
&[usize, usize], // size, align
39-
None,
40-
true,
41-
);
42-
4314
unsafe {
4415
// __rust_alloc_error_handler_should_panic
4516
let name = OomStrategy::SYMBOL;
@@ -62,80 +33,3 @@ pub(crate) unsafe fn codegen(
6233
dbg_cx.finalize(tcx.sess);
6334
}
6435
}
65-
66-
fn create_wrapper_function(
67-
tcx: TyCtxt<'_>,
68-
llcx: &Context,
69-
llmod: &Module,
70-
from_name: &str,
71-
to_name: &str,
72-
args: &[&Type],
73-
output: Option<&Type>,
74-
no_return: bool,
75-
) {
76-
unsafe {
77-
let ty = llvm::LLVMFunctionType(
78-
output.unwrap_or_else(|| llvm::LLVMVoidTypeInContext(llcx)),
79-
args.as_ptr(),
80-
args.len() as c_uint,
81-
False,
82-
);
83-
let llfn = llvm::LLVMRustGetOrInsertFunction(
84-
llmod,
85-
from_name.as_c_char_ptr(),
86-
from_name.len(),
87-
ty,
88-
);
89-
let no_return = if no_return {
90-
// -> ! DIFlagNoReturn
91-
let no_return = llvm::AttributeKind::NoReturn.create_attr(llcx);
92-
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[no_return]);
93-
Some(no_return)
94-
} else {
95-
None
96-
};
97-
98-
llvm::set_visibility(llfn, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
99-
100-
if tcx.sess.must_emit_unwind_tables() {
101-
let uwtable =
102-
attributes::uwtable_attr(llcx, tcx.sess.opts.unstable_opts.use_sync_unwind);
103-
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[uwtable]);
104-
}
105-
106-
let callee =
107-
llvm::LLVMRustGetOrInsertFunction(llmod, to_name.as_c_char_ptr(), to_name.len(), ty);
108-
if let Some(no_return) = no_return {
109-
// -> ! DIFlagNoReturn
110-
attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
111-
}
112-
llvm::set_visibility(callee, llvm::Visibility::Hidden);
113-
114-
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr());
115-
116-
let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
117-
llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);
118-
let args = args
119-
.iter()
120-
.enumerate()
121-
.map(|(i, _)| llvm::LLVMGetParam(llfn, i as c_uint))
122-
.collect::<Vec<_>>();
123-
let ret = llvm::LLVMBuildCallWithOperandBundles(
124-
llbuilder,
125-
ty,
126-
callee,
127-
args.as_ptr(),
128-
args.len() as c_uint,
129-
[].as_ptr(),
130-
0 as c_uint,
131-
c"".as_ptr(),
132-
);
133-
llvm::LLVMSetTailCall(ret, True);
134-
if output.is_some() {
135-
llvm::LLVMBuildRet(llbuilder, ret);
136-
} else {
137-
llvm::LLVMBuildRetVoid(llbuilder);
138-
}
139-
llvm::LLVMDisposeBuilder(llbuilder);
140-
}
141-
}

compiler/rustc_codegen_llvm/src/lib.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use back::owned_target_machine::OwnedTargetMachine;
2929
use back::write::{create_informational_target_machine, create_target_machine};
3030
use errors::ParseTargetMachineConfig;
3131
pub use llvm_util::target_features_cfg;
32-
use rustc_ast::expand::allocator::AllocatorKind;
3332
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
3433
use rustc_codegen_ssa::back::write::{
3534
CodegenContext, FatLtoInput, ModuleConfig, TargetMachineFactoryConfig, TargetMachineFactoryFn,
@@ -111,15 +110,10 @@ impl Drop for TimeTraceProfiler {
111110
}
112111

113112
impl ExtraBackendMethods for LlvmCodegenBackend {
114-
fn codegen_allocator<'tcx>(
115-
&self,
116-
tcx: TyCtxt<'tcx>,
117-
module_name: &str,
118-
alloc_error_handler_kind: AllocatorKind,
119-
) -> ModuleLlvm {
113+
fn codegen_allocator<'tcx>(&self, tcx: TyCtxt<'tcx>, module_name: &str) -> ModuleLlvm {
120114
let mut module_llvm = ModuleLlvm::new_metadata(tcx, module_name);
121115
unsafe {
122-
allocator::codegen(tcx, &mut module_llvm, module_name, alloc_error_handler_kind);
116+
allocator::codegen(tcx, &mut module_llvm, module_name);
123117
}
124118
module_llvm
125119
}

0 commit comments

Comments
 (0)