Skip to content

Commit 5cd90ee

Browse files
authored
Rollup merge of rust-lang#109507 - Amanieu:panic-oom-payload, r=davidtwco
Report allocation errors as panics OOM is now reported as a panic but with a custom payload type (`AllocErrorPanicPayload`) which holds the layout that was passed to `handle_alloc_error`. This should be review one commit at a time: - The first commit adds `AllocErrorPanicPayload` and changes allocation errors to always be reported as panics. - The second commit removes `#[alloc_error_handler]` and the `alloc_error_hook` API. ACP: rust-lang/libs-team#192 Closes rust-lang#51540 Closes rust-lang#51245
2 parents 31656e7 + 4b981c2 commit 5cd90ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+135
-875
lines changed

compiler/rustc_builtin_macros/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ builtin_macros_requires_cfg_pattern =
44
55
builtin_macros_expected_one_cfg_pattern = expected 1 cfg-pattern
66
7-
builtin_macros_alloc_error_must_be_fn = alloc_error_handler must be a function
8-
97
builtin_macros_assert_requires_boolean = macro requires a boolean expression as an argument
108
.label = boolean expression required
119

compiler/rustc_builtin_macros/src/alloc_error_handler.rs

-97
This file was deleted.

compiler/rustc_builtin_macros/src/errors.rs

-7
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ pub(crate) struct OneCfgPattern {
1919
pub(crate) span: Span,
2020
}
2121

22-
#[derive(Diagnostic)]
23-
#[diag(builtin_macros_alloc_error_must_be_fn)]
24-
pub(crate) struct AllocErrorMustBeFn {
25-
#[primary_span]
26-
pub(crate) span: Span,
27-
}
28-
2922
#[derive(Diagnostic)]
3023
#[diag(builtin_macros_assert_requires_boolean)]
3124
pub(crate) struct AssertRequiresBoolean {

compiler/rustc_builtin_macros/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use rustc_expand::proc_macro::BangProcMacro;
2727
use rustc_macros::fluent_messages;
2828
use rustc_span::symbol::sym;
2929

30-
mod alloc_error_handler;
3130
mod assert;
3231
mod cfg;
3332
mod cfg_accessible;
@@ -102,7 +101,6 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
102101
}
103102

104103
register_attr! {
105-
alloc_error_handler: alloc_error_handler::expand,
106104
bench: test::expand_bench,
107105
cfg_accessible: cfg_accessible::Expander,
108106
cfg_eval: cfg_eval::expand,

compiler/rustc_codegen_cranelift/example/alloc_example.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(start, core_intrinsics, alloc_error_handler)]
1+
#![feature(start, core_intrinsics)]
22
#![no_std]
33

44
extern crate alloc;
@@ -22,11 +22,6 @@ fn panic_handler(_: &core::panic::PanicInfo) -> ! {
2222
core::intrinsics::abort();
2323
}
2424

25-
#[alloc_error_handler]
26-
fn alloc_error_handler(_: alloc::alloc::Layout) -> ! {
27-
core::intrinsics::abort();
28-
}
29-
3025
#[start]
3126
fn main(_argc: isize, _argv: *const *const u8) -> isize {
3227
let world: Box<&str> = Box::new("Hello World!\0");

compiler/rustc_codegen_cranelift/src/allocator.rs

+1-22
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::prelude::*;
66
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
77
use rustc_codegen_ssa::base::allocator_kind_for_codegen;
88
use rustc_session::config::OomStrategy;
9-
use rustc_span::symbol::sym;
109

1110
/// Returns whether an allocator shim was created
1211
pub(crate) fn codegen(
@@ -15,21 +14,14 @@ pub(crate) fn codegen(
1514
unwind_context: &mut UnwindContext,
1615
) -> bool {
1716
let Some(kind) = allocator_kind_for_codegen(tcx) else { return false };
18-
codegen_inner(
19-
module,
20-
unwind_context,
21-
kind,
22-
tcx.alloc_error_handler_kind(()).unwrap(),
23-
tcx.sess.opts.unstable_opts.oom,
24-
);
17+
codegen_inner(module, unwind_context, kind, tcx.sess.opts.unstable_opts.oom);
2518
true
2619
}
2720

2821
fn codegen_inner(
2922
module: &mut impl Module,
3023
unwind_context: &mut UnwindContext,
3124
kind: AllocatorKind,
32-
alloc_error_handler_kind: AllocatorKind,
3325
oom_strategy: OomStrategy,
3426
) {
3527
let usize_ty = module.target_config().pointer_type();
@@ -71,19 +63,6 @@ fn codegen_inner(
7163
);
7264
}
7365

74-
let sig = Signature {
75-
call_conv: module.target_config().default_call_conv,
76-
params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
77-
returns: vec![],
78-
};
79-
crate::common::create_wrapper_function(
80-
module,
81-
unwind_context,
82-
sig,
83-
"__rust_alloc_error_handler",
84-
&alloc_error_handler_kind.fn_name(sym::oom),
85-
);
86-
8766
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
8867
let mut data_ctx = DataContext::new();
8968
data_ctx.set_align(1);

compiler/rustc_codegen_gcc/example/alloc_example.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(start, core_intrinsics, alloc_error_handler, lang_items)]
1+
#![feature(start, core_intrinsics, lang_items)]
22
#![no_std]
33

44
extern crate alloc;
@@ -21,11 +21,6 @@ fn panic_handler(_: &core::panic::PanicInfo) -> ! {
2121
core::intrinsics::abort();
2222
}
2323

24-
#[alloc_error_handler]
25-
fn alloc_error_handler(_: alloc::alloc::Layout) -> ! {
26-
core::intrinsics::abort();
27-
}
28-
2924
#[lang = "eh_personality"]
3025
fn eh_personality() -> ! {
3126
loop {}

compiler/rustc_codegen_gcc/src/allocator.rs

+1-33
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS
55
use rustc_middle::bug;
66
use rustc_middle::ty::TyCtxt;
77
use rustc_session::config::OomStrategy;
8-
use rustc_span::symbol::sym;
98

109
use crate::GccContext;
1110

12-
pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_name: &str, kind: AllocatorKind, alloc_error_handler_kind: AllocatorKind) {
11+
pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_name: &str, kind: AllocatorKind) {
1312
let context = &mods.context;
1413
let usize =
1514
match tcx.sess.target.pointer_width {
@@ -87,37 +86,6 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
8786
// as described in https://github.com/rust-lang/rust/commit/77a96ed5646f7c3ee8897693decc4626fe380643
8887
}
8988

90-
let types = [usize, usize];
91-
let name = "__rust_alloc_error_handler".to_string();
92-
let args: Vec<_> = types.iter().enumerate()
93-
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
94-
.collect();
95-
let func = context.new_function(None, FunctionType::Exported, void, &args, name, false);
96-
97-
if tcx.sess.target.default_hidden_visibility {
98-
#[cfg(feature="master")]
99-
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
100-
}
101-
102-
let callee = alloc_error_handler_kind.fn_name(sym::oom);
103-
let args: Vec<_> = types.iter().enumerate()
104-
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
105-
.collect();
106-
let callee = context.new_function(None, FunctionType::Extern, void, &args, callee, false);
107-
#[cfg(feature="master")]
108-
callee.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
109-
110-
let block = func.new_block("entry");
111-
112-
let args = args
113-
.iter()
114-
.enumerate()
115-
.map(|(i, _)| func.get_param(i as i32).to_rvalue())
116-
.collect::<Vec<_>>();
117-
let _ret = context.new_call(None, callee, &args);
118-
//llvm::LLVMSetTailCall(ret, True);
119-
block.end_with_void_return(None);
120-
12189
let name = OomStrategy::SYMBOL.to_string();
12290
let global = context.new_global(None, GlobalKind::Exported, i8, name);
12391
let value = tcx.sess.opts.unstable_opts.oom.should_panic();

compiler/rustc_codegen_gcc/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ impl CodegenBackend for GccCodegenBackend {
162162
}
163163

164164
impl ExtraBackendMethods for GccCodegenBackend {
165-
fn codegen_allocator<'tcx>(&self, tcx: TyCtxt<'tcx>, module_name: &str, kind: AllocatorKind, alloc_error_handler_kind: AllocatorKind) -> Self::Module {
165+
fn codegen_allocator<'tcx>(&self, tcx: TyCtxt<'tcx>, module_name: &str, kind: AllocatorKind) -> Self::Module {
166166
let mut mods = GccContext {
167167
context: Context::default(),
168168
};
169-
unsafe { allocator::codegen(tcx, &mut mods, module_name, kind, alloc_error_handler_kind); }
169+
unsafe { allocator::codegen(tcx, &mut mods, module_name, kind); }
170170
mods
171171
}
172172

compiler/rustc_codegen_llvm/src/allocator.rs

-48
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS
44
use rustc_middle::bug;
55
use rustc_middle::ty::TyCtxt;
66
use rustc_session::config::{DebugInfo, OomStrategy};
7-
use rustc_span::symbol::sym;
87

98
use crate::debuginfo;
109
use crate::llvm::{self, False, True};
@@ -15,7 +14,6 @@ pub(crate) unsafe fn codegen(
1514
module_llvm: &mut ModuleLlvm,
1615
module_name: &str,
1716
kind: AllocatorKind,
18-
alloc_error_handler_kind: AllocatorKind,
1917
) {
2018
let llcx = &*module_llvm.llcx;
2119
let llmod = module_llvm.llmod();
@@ -100,52 +98,6 @@ pub(crate) unsafe fn codegen(
10098
llvm::LLVMDisposeBuilder(llbuilder);
10199
}
102100

103-
// rust alloc error handler
104-
let args = [usize, usize]; // size, align
105-
106-
let ty = llvm::LLVMFunctionType(void, args.as_ptr(), args.len() as c_uint, False);
107-
let name = "__rust_alloc_error_handler";
108-
let llfn = llvm::LLVMRustGetOrInsertFunction(llmod, name.as_ptr().cast(), name.len(), ty);
109-
// -> ! DIFlagNoReturn
110-
let no_return = llvm::AttributeKind::NoReturn.create_attr(llcx);
111-
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[no_return]);
112-
113-
if tcx.sess.target.default_hidden_visibility {
114-
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
115-
}
116-
if tcx.sess.must_emit_unwind_tables() {
117-
let uwtable = attributes::uwtable_attr(llcx);
118-
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[uwtable]);
119-
}
120-
121-
let callee = alloc_error_handler_kind.fn_name(sym::oom);
122-
let callee = llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr().cast(), callee.len(), ty);
123-
// -> ! DIFlagNoReturn
124-
attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
125-
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
126-
127-
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, "entry\0".as_ptr().cast());
128-
129-
let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
130-
llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);
131-
let args = args
132-
.iter()
133-
.enumerate()
134-
.map(|(i, _)| llvm::LLVMGetParam(llfn, i as c_uint))
135-
.collect::<Vec<_>>();
136-
let ret = llvm::LLVMRustBuildCall(
137-
llbuilder,
138-
ty,
139-
callee,
140-
args.as_ptr(),
141-
args.len() as c_uint,
142-
[].as_ptr(),
143-
0 as c_uint,
144-
);
145-
llvm::LLVMSetTailCall(ret, True);
146-
llvm::LLVMBuildRetVoid(llbuilder);
147-
llvm::LLVMDisposeBuilder(llbuilder);
148-
149101
// __rust_alloc_error_handler_should_panic
150102
let name = OomStrategy::SYMBOL;
151103
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);

compiler/rustc_codegen_llvm/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,10 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
115115
tcx: TyCtxt<'tcx>,
116116
module_name: &str,
117117
kind: AllocatorKind,
118-
alloc_error_handler_kind: AllocatorKind,
119118
) -> ModuleLlvm {
120119
let mut module_llvm = ModuleLlvm::new_metadata(tcx, module_name);
121120
unsafe {
122-
allocator::codegen(tcx, &mut module_llvm, module_name, kind, alloc_error_handler_kind);
121+
allocator::codegen(tcx, &mut module_llvm, module_name, kind);
123122
}
124123
module_llvm
125124
}

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ fn exported_symbols_provider_local(
219219
for symbol_name in ALLOCATOR_METHODS
220220
.iter()
221221
.map(|method| format!("__rust_{}", method.name))
222-
.chain(["__rust_alloc_error_handler".to_string(), OomStrategy::SYMBOL.to_string()])
222+
.chain([OomStrategy::SYMBOL.to_string()])
223223
{
224224
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
225225

compiler/rustc_codegen_ssa/src/base.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -635,16 +635,9 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
635635
if let Some(kind) = allocator_kind_for_codegen(tcx) {
636636
let llmod_id =
637637
cgu_name_builder.build_cgu_name(LOCAL_CRATE, &["crate"], Some("allocator")).to_string();
638-
let module_llvm = tcx.sess.time("write_allocator_module", || {
639-
backend.codegen_allocator(
640-
tcx,
641-
&llmod_id,
642-
kind,
643-
// If allocator_kind is Some then alloc_error_handler_kind must
644-
// also be Some.
645-
tcx.alloc_error_handler_kind(()).unwrap(),
646-
)
647-
});
638+
let module_llvm = tcx
639+
.sess
640+
.time("write_allocator_module", || backend.codegen_allocator(tcx, &llmod_id, kind));
648641

649642
ongoing_codegen.submit_pre_codegened_module_to_llvm(
650643
tcx,

0 commit comments

Comments
 (0)