Skip to content

Commit 86daae2

Browse files
committed
Auto merge of rust-lang#88098 - Amanieu:oom_panic, r=nagisa
Implement -Z oom=panic This PR removes the `#[rustc_allocator_nounwind]` attribute on `alloc_error_handler` which allows it to unwind with a panic instead of always aborting. This is then used to implement `-Z oom=panic` as per RFC 2116 (tracking issue rust-lang#43596). Perf and binary size tests show negligible impact.
2 parents c56a10c + e291495 commit 86daae2

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/allocator.rs

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

66
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
7+
use rustc_session::config::OomStrategy;
78

89
/// Returns whether an allocator shim was created
910
pub(crate) fn codegen(
@@ -18,7 +19,13 @@ pub(crate) fn codegen(
1819
if any_dynamic_crate {
1920
false
2021
} else if let Some(kind) = tcx.allocator_kind(()) {
21-
codegen_inner(module, unwind_context, kind, tcx.lang_items().oom().is_some());
22+
codegen_inner(
23+
module,
24+
unwind_context,
25+
kind,
26+
tcx.lang_items().oom().is_some(),
27+
tcx.sess.opts.debugging_opts.oom,
28+
);
2229
true
2330
} else {
2431
false
@@ -30,6 +37,7 @@ fn codegen_inner(
3037
unwind_context: &mut UnwindContext,
3138
kind: AllocatorKind,
3239
has_alloc_error_handler: bool,
40+
oom_strategy: OomStrategy,
3341
) {
3442
let usize_ty = module.target_config().pointer_type();
3543

@@ -129,4 +137,11 @@ fn codegen_inner(
129137
}
130138
module.define_function(func_id, &mut ctx).unwrap();
131139
unwind_context.add_function(func_id, &ctx, module.isa());
140+
141+
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
142+
let mut data_ctx = DataContext::new();
143+
data_ctx.set_align(1);
144+
let val = oom_strategy.should_panic();
145+
data_ctx.define(Box::new([val]));
146+
module.define_data(data_id, &data_ctx).unwrap();
132147
}

0 commit comments

Comments
 (0)