Skip to content

Commit 321337b

Browse files
committed
Auto merge of #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 #51540 Closes #51245
2 parents f40e4da + 5f30b63 commit 321337b

File tree

3 files changed

+4
-41
lines changed

3 files changed

+4
-41
lines changed

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 {}

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();

src/lib.rs

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

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

0 commit comments

Comments
 (0)