Skip to content

Commit 812cd88

Browse files
committed
Remove -Zoom=panic
There are major questions remaining about the reentrancy that this allows. It doesn't have any users on github outside of a single project that uses it in a panic=abort project to show backtraces. It can still be emulated through #[alloc_error_handler] or set_alloc_error_hook depending on if you use the standard library or not. And finally it makes it harder to do various improvements to the allocator shim.
1 parent 227da2d commit 812cd88

File tree

12 files changed

+28
-226
lines changed

12 files changed

+28
-226
lines changed

compiler/rustc_codegen_cranelift/src/allocator.rs

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_ast::expand::allocator::{
66
AllocatorMethod, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE, default_fn_name, global_fn_name,
77
};
88
use rustc_codegen_ssa::base::{allocator_kind_for_codegen, allocator_shim_contents};
9-
use rustc_session::config::OomStrategy;
109
use rustc_symbol_mangling::mangle_internal_symbol;
1110

1211
use crate::prelude::*;
@@ -15,16 +14,11 @@ use crate::prelude::*;
1514
pub(crate) fn codegen(tcx: TyCtxt<'_>, module: &mut dyn Module) -> bool {
1615
let Some(kind) = allocator_kind_for_codegen(tcx) else { return false };
1716
let methods = allocator_shim_contents(tcx, kind);
18-
codegen_inner(tcx, module, &methods, tcx.sess.opts.unstable_opts.oom);
17+
codegen_inner(tcx, module, &methods);
1918
true
2019
}
2120

22-
fn codegen_inner(
23-
tcx: TyCtxt<'_>,
24-
module: &mut dyn Module,
25-
methods: &[AllocatorMethod],
26-
oom_strategy: OomStrategy,
27-
) {
21+
fn codegen_inner(tcx: TyCtxt<'_>, module: &mut dyn Module, methods: &[AllocatorMethod]) {
2822
let usize_ty = module.target_config().pointer_type();
2923

3024
for method in methods {
@@ -65,35 +59,6 @@ fn codegen_inner(
6559
);
6660
}
6761

68-
{
69-
let sig = Signature {
70-
call_conv: module.target_config().default_call_conv,
71-
params: vec![],
72-
returns: vec![AbiParam::new(types::I8)],
73-
};
74-
let func_id = module
75-
.declare_function(
76-
&mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
77-
Linkage::Export,
78-
&sig,
79-
)
80-
.unwrap();
81-
let mut ctx = Context::new();
82-
ctx.func.signature = sig;
83-
{
84-
let mut func_ctx = FunctionBuilderContext::new();
85-
let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
86-
87-
let block = bcx.create_block();
88-
bcx.switch_to_block(block);
89-
let value = bcx.ins().iconst(types::I8, oom_strategy.should_panic() as i64);
90-
bcx.ins().return_(&[value]);
91-
bcx.seal_all_blocks();
92-
bcx.finalize();
93-
}
94-
module.define_function(func_id, &mut ctx).unwrap();
95-
}
96-
9762
{
9863
let sig = Signature {
9964
call_conv: module.target_config().default_call_conv,

compiler/rustc_codegen_gcc/src/allocator.rs

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#[cfg(feature = "master")]
22
use gccjit::FnAttribute;
3-
use gccjit::{Context, FunctionType, RValue, ToRValue, Type};
3+
use gccjit::{Context, FunctionType, ToRValue, Type};
44
use rustc_ast::expand::allocator::{
55
AllocatorMethod, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE, default_fn_name, global_fn_name,
66
};
77
use rustc_middle::bug;
88
use rustc_middle::ty::TyCtxt;
9-
use rustc_session::config::OomStrategy;
109
use rustc_symbol_mangling::mangle_internal_symbol;
1110

1211
use crate::GccContext;
@@ -59,14 +58,6 @@ pub(crate) unsafe fn codegen(
5958
create_wrapper_function(tcx, context, &from_name, Some(&to_name), &types, output);
6059
}
6160

62-
create_const_value_function(
63-
tcx,
64-
context,
65-
&mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
66-
i8,
67-
context.new_rvalue_from_int(i8, tcx.sess.opts.unstable_opts.oom.should_panic() as i32),
68-
);
69-
7061
create_wrapper_function(
7162
tcx,
7263
context,
@@ -77,34 +68,6 @@ pub(crate) unsafe fn codegen(
7768
);
7869
}
7970

80-
fn create_const_value_function(
81-
tcx: TyCtxt<'_>,
82-
context: &Context<'_>,
83-
name: &str,
84-
output: Type<'_>,
85-
value: RValue<'_>,
86-
) {
87-
let func = context.new_function(None, FunctionType::Exported, output, &[], name, false);
88-
89-
#[cfg(feature = "master")]
90-
{
91-
func.add_attribute(FnAttribute::Visibility(symbol_visibility_to_gcc(
92-
tcx.sess.default_visibility(),
93-
)));
94-
95-
// FIXME(antoyo): cg_llvm sets AlwaysInline, but AlwaysInline is different in GCC and using
96-
// it here will causes linking errors when using LTO.
97-
func.add_attribute(FnAttribute::Inline);
98-
}
99-
100-
if tcx.sess.must_emit_unwind_tables() {
101-
// TODO(antoyo): emit unwind tables.
102-
}
103-
104-
let block = func.new_block("entry");
105-
block.end_with_return(None, value);
106-
}
107-
10871
fn create_wrapper_function(
10972
tcx: TyCtxt<'_>,
11073
context: &Context<'_>,

compiler/rustc_codegen_llvm/src/allocator.rs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use rustc_codegen_ssa::traits::BaseTypeCodegenMethods as _;
77
use rustc_middle::bug;
88
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
99
use rustc_middle::ty::TyCtxt;
10-
use rustc_session::config::{DebugInfo, OomStrategy};
10+
use rustc_session::config::DebugInfo;
1111
use rustc_symbol_mangling::mangle_internal_symbol;
1212

1313
use crate::attributes::llfn_attrs_from_instance;
1414
use crate::builder::SBuilder;
1515
use crate::declare::declare_simple_fn;
16-
use crate::llvm::{self, FALSE, FromGeneric, TRUE, Type, Value};
16+
use crate::llvm::{self, FromGeneric, TRUE, Type};
1717
use crate::{SimpleCx, attributes, debuginfo};
1818

1919
pub(crate) unsafe fn codegen(
@@ -28,7 +28,6 @@ pub(crate) unsafe fn codegen(
2828
64 => cx.type_i64(),
2929
tws => bug!("Unsupported target word size for int: {}", tws),
3030
};
31-
let i8 = cx.type_i8();
3231
let i8p = cx.type_ptr();
3332

3433
for method in methods {
@@ -87,17 +86,6 @@ pub(crate) unsafe fn codegen(
8786
);
8887
}
8988

90-
// __rust_alloc_error_handler_should_panic_v2
91-
create_const_value_function(
92-
tcx,
93-
&cx,
94-
&mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
95-
&i8,
96-
unsafe {
97-
llvm::LLVMConstInt(i8, tcx.sess.opts.unstable_opts.oom.should_panic() as u64, FALSE)
98-
},
99-
);
100-
10189
// __rust_no_alloc_shim_is_unstable_v2
10290
create_wrapper_function(
10391
tcx,
@@ -117,34 +105,6 @@ pub(crate) unsafe fn codegen(
117105
}
118106
}
119107

120-
fn create_const_value_function(
121-
tcx: TyCtxt<'_>,
122-
cx: &SimpleCx<'_>,
123-
name: &str,
124-
output: &Type,
125-
value: &Value,
126-
) {
127-
let ty = cx.type_func(&[], output);
128-
let llfn = declare_simple_fn(
129-
&cx,
130-
name,
131-
llvm::CallConv::CCallConv,
132-
llvm::UnnamedAddr::Global,
133-
llvm::Visibility::from_generic(tcx.sess.default_visibility()),
134-
ty,
135-
);
136-
137-
attributes::apply_to_llfn(
138-
llfn,
139-
llvm::AttributePlace::Function,
140-
&[llvm::AttributeKind::AlwaysInline.create_attr(cx.llcx)],
141-
);
142-
143-
let llbb = unsafe { llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, c"entry".as_ptr()) };
144-
let mut bx = SBuilder::build(&cx, llbb);
145-
bx.ret(value);
146-
}
147-
148108
fn create_wrapper_function(
149109
tcx: TyCtxt<'_>,
150110
cx: &SimpleCx<'_>,

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::middle::exported_symbols::{
1515
use rustc_middle::query::LocalCrate;
1616
use rustc_middle::ty::{self, GenericArgKind, GenericArgsRef, Instance, SymbolName, Ty, TyCtxt};
1717
use rustc_middle::util::Providers;
18-
use rustc_session::config::{CrateType, OomStrategy};
18+
use rustc_session::config::CrateType;
1919
use rustc_symbol_mangling::mangle_internal_symbol;
2020
use rustc_target::spec::TlsModel;
2121
use tracing::debug;
@@ -501,7 +501,6 @@ pub(crate) fn allocator_shim_symbols(
501501
.map(move |method| mangle_internal_symbol(tcx, global_fn_name(method.name).as_str()))
502502
.chain([
503503
mangle_internal_symbol(tcx, global_fn_name(ALLOC_ERROR_HANDLER).as_str()),
504-
mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
505504
mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
506505
])
507506
.map(move |symbol_name| {

compiler/rustc_session/src/config.rs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3242,10 +3242,10 @@ pub(crate) mod dep_tracking {
32423242
AutoDiff, BranchProtection, CFGuard, CFProtection, CollapseMacroDebuginfo, CoverageOptions,
32433243
CrateType, DebugInfo, DebugInfoCompression, ErrorOutputType, FmtDebug, FunctionReturn,
32443244
InliningThreshold, InstrumentCoverage, InstrumentXRay, LinkerPluginLto, LocationDetail,
3245-
LtoCli, MirStripDebugInfo, NextSolverConfig, Offload, OomStrategy, OptLevel, OutFileName,
3246-
OutputType, OutputTypes, PatchableFunctionEntry, Polonius, RemapPathScopeComponents,
3247-
ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind, SwitchWithOptPath,
3248-
SymbolManglingVersion, WasiExecModel,
3245+
LtoCli, MirStripDebugInfo, NextSolverConfig, Offload, OptLevel, OutFileName, OutputType,
3246+
OutputTypes, PatchableFunctionEntry, Polonius, RemapPathScopeComponents, ResolveDocLinks,
3247+
SourceFileHashAlgorithm, SplitDwarfKind, SwitchWithOptPath, SymbolManglingVersion,
3248+
WasiExecModel,
32493249
};
32503250
use crate::lint;
32513251
use crate::utils::NativeLib;
@@ -3341,7 +3341,6 @@ pub(crate) mod dep_tracking {
33413341
LocationDetail,
33423342
FmtDebug,
33433343
BranchProtection,
3344-
OomStrategy,
33453344
LanguageIdentifier,
33463345
NextSolverConfig,
33473346
PatchableFunctionEntry,
@@ -3454,27 +3453,6 @@ pub(crate) mod dep_tracking {
34543453
}
34553454
}
34563455

3457-
/// Default behavior to use in out-of-memory situations.
3458-
#[derive(Clone, Copy, PartialEq, Hash, Debug, Encodable, Decodable, HashStable_Generic)]
3459-
pub enum OomStrategy {
3460-
/// Generate a panic that can be caught by `catch_unwind`.
3461-
Panic,
3462-
3463-
/// Abort the process immediately.
3464-
Abort,
3465-
}
3466-
3467-
impl OomStrategy {
3468-
pub const SYMBOL: &'static str = "__rust_alloc_error_handler_should_panic_v2";
3469-
3470-
pub fn should_panic(self) -> u8 {
3471-
match self {
3472-
OomStrategy::Panic => 1,
3473-
OomStrategy::Abort => 0,
3474-
}
3475-
}
3476-
}
3477-
34783456
/// How to run proc-macro code when building this crate
34793457
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
34803458
pub enum ProcMacroExecutionStrategy {

compiler/rustc_session/src/options.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,6 @@ mod desc {
806806
pub(crate) const parse_on_broken_pipe: &str = "either `kill`, `error`, or `inherit`";
807807
pub(crate) const parse_patchable_function_entry: &str = "either two comma separated integers (total_nops,prefix_nops), with prefix_nops <= total_nops, or one integer (total_nops)";
808808
pub(crate) const parse_opt_panic_strategy: &str = parse_panic_strategy;
809-
pub(crate) const parse_oom_strategy: &str = "either `panic` or `abort`";
810809
pub(crate) const parse_relro_level: &str = "one of: `full`, `partial`, or `off`";
811810
pub(crate) const parse_sanitizers: &str = "comma separated list of sanitizers: `address`, `cfi`, `dataflow`, `hwaddress`, `kcfi`, `kernel-address`, `leak`, `memory`, `memtag`, `safestack`, `shadow-call-stack`, or `thread`";
812811
pub(crate) const parse_sanitizer_memory_track_origins: &str = "0, 1, or 2";
@@ -1218,15 +1217,6 @@ pub mod parse {
12181217
false
12191218
}
12201219

1221-
pub(crate) fn parse_oom_strategy(slot: &mut OomStrategy, v: Option<&str>) -> bool {
1222-
match v {
1223-
Some("panic") => *slot = OomStrategy::Panic,
1224-
Some("abort") => *slot = OomStrategy::Abort,
1225-
_ => return false,
1226-
}
1227-
true
1228-
}
1229-
12301220
pub(crate) fn parse_relro_level(slot: &mut Option<RelroLevel>, v: Option<&str>) -> bool {
12311221
match v {
12321222
Some(s) => match s.parse::<RelroLevel>() {
@@ -2500,8 +2490,6 @@ options! {
25002490
Currently the only option available"),
25012491
on_broken_pipe: OnBrokenPipe = (OnBrokenPipe::Default, parse_on_broken_pipe, [TRACKED],
25022492
"behavior of std::io::ErrorKind::BrokenPipe (SIGPIPE)"),
2503-
oom: OomStrategy = (OomStrategy::Abort, parse_oom_strategy, [TRACKED],
2504-
"panic strategy for out-of-memory handling"),
25052493
osx_rpath_install_name: bool = (false, parse_bool, [TRACKED],
25062494
"pass `-install_name @rpath/...` to the macOS linker (default: no)"),
25072495
packed_bundled_libs: bool = (false, parse_bool, [TRACKED],

library/alloc/src/alloc.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -426,20 +426,9 @@ pub mod __alloc_error_handler {
426426
// `#[alloc_error_handler]`.
427427
#[rustc_std_internal_symbol]
428428
pub unsafe fn __rdl_alloc_error_handler(size: usize, _align: usize) -> ! {
429-
unsafe extern "Rust" {
430-
// This symbol is emitted by rustc next to __rust_alloc_error_handler.
431-
// Its value depends on the -Zoom={panic,abort} compiler option.
432-
#[rustc_std_internal_symbol]
433-
fn __rust_alloc_error_handler_should_panic_v2() -> u8;
434-
}
435-
436-
if unsafe { __rust_alloc_error_handler_should_panic_v2() != 0 } {
437-
panic!("memory allocation of {size} bytes failed")
438-
} else {
439-
core::panicking::panic_nounwind_fmt(
440-
format_args!("memory allocation of {size} bytes failed"),
441-
/* force_no_backtrace */ false,
442-
)
443-
}
429+
core::panicking::panic_nounwind_fmt(
430+
format_args!("memory allocation of {size} bytes failed"),
431+
/* force_no_backtrace */ false,
432+
)
444433
}
445434
}

library/std/src/alloc.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -345,25 +345,14 @@ pub fn take_alloc_error_hook() -> fn(Layout) {
345345
}
346346

347347
fn default_alloc_error_hook(layout: Layout) {
348-
unsafe extern "Rust" {
349-
// This symbol is emitted by rustc next to __rust_alloc_error_handler.
350-
// Its value depends on the -Zoom={panic,abort} compiler option.
351-
#[rustc_std_internal_symbol]
352-
fn __rust_alloc_error_handler_should_panic_v2() -> u8;
353-
}
354-
355-
if unsafe { __rust_alloc_error_handler_should_panic_v2() != 0 } {
356-
panic!("memory allocation of {} bytes failed", layout.size());
357-
} else {
358-
// This is the default path taken on OOM, and the only path taken on stable with std.
359-
// Crucially, it does *not* call any user-defined code, and therefore users do not have to
360-
// worry about allocation failure causing reentrancy issues. That makes it different from
361-
// the default `__rdl_alloc_error_handler` defined in alloc (i.e., the default alloc error
362-
// handler that is called when there is no `#[alloc_error_handler]`), which triggers a
363-
// regular panic and thus can invoke a user-defined panic hook, executing arbitrary
364-
// user-defined code.
365-
rtprintpanic!("memory allocation of {} bytes failed\n", layout.size());
366-
}
348+
// This is the default path taken on OOM, and the only path taken on stable with std.
349+
// Crucially, it does *not* call any user-defined code, and therefore users do not have to
350+
// worry about allocation failure causing reentrancy issues. That makes it different from
351+
// the default `__rdl_alloc_error_handler` defined in alloc (i.e., the default alloc error
352+
// handler that is called when there is no `#[alloc_error_handler]`), which triggers a
353+
// regular panic and thus can invoke a user-defined panic hook, executing arbitrary
354+
// user-defined code.
355+
rtprintpanic!("memory allocation of {} bytes failed\n", layout.size());
367356
}
368357

369358
#[cfg(not(test))]

src/tools/miri/src/shims/foreign_items.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1212
use rustc_middle::mir::interpret::AllocInit;
1313
use rustc_middle::ty::{Instance, Ty};
1414
use rustc_middle::{mir, ty};
15-
use rustc_session::config::OomStrategy;
1615
use rustc_span::Symbol;
1716
use rustc_target::callconv::FnAbi;
1817

@@ -305,18 +304,12 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
305304
// Here we dispatch all the shims for foreign functions. If you have a platform specific
306305
// shim, add it to the corresponding submodule.
307306
match link_name.as_str() {
308-
// Magic functions Rust emits (and not as part of the allocator shim).
307+
// Magic function Rust emits (and not as part of the allocator shim).
309308
name if name == this.mangle_internal_symbol(NO_ALLOC_SHIM_IS_UNSTABLE) => {
310309
// This is a no-op shim that only exists to prevent making the allocator shims
311310
// instantly stable.
312311
let [] = this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
313312
}
314-
name if name == this.mangle_internal_symbol(OomStrategy::SYMBOL) => {
315-
// Gets the value of the `oom` option.
316-
let [] = this.check_shim_sig_lenient(abi, CanonAbi::Rust, link_name, args)?;
317-
let val = this.tcx.sess.opts.unstable_opts.oom.should_panic();
318-
this.write_int(val, dest)?;
319-
}
320313

321314
// Miri-specific extern functions
322315
"miri_alloc" => {

0 commit comments

Comments
 (0)