Skip to content

Commit aa00196

Browse files
authored
Unrolled build for #147725
Rollup merge of #147725 - bjorn3:remove_oom_panic, r=Amanieu 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. With this PR the sole remaining symbol in the allocator shim that is not effectively emulating weak symbols is the symbol that prevents skipping the allocator shim on stable even when it would otherwise be empty because libstd + `#[global_allocator]` is used. Closes #43596 Fixes #126683
2 parents bad5026 + 8f55c15 commit aa00196

File tree

13 files changed

+22
-221
lines changed

13 files changed

+22
-221
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::{Arch, Os, TlsModel};
2121
use tracing::debug;
@@ -493,7 +493,6 @@ pub(crate) fn allocator_shim_symbols(
493493
.map(move |method| mangle_internal_symbol(tcx, global_fn_name(method.name).as_str()))
494494
.chain([
495495
mangle_internal_symbol(tcx, global_fn_name(ALLOC_ERROR_HANDLER).as_str()),
496-
mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
497496
mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
498497
])
499498
.map(move |symbol_name| {

‎compiler/rustc_interface/src/tests.rs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ use rustc_session::config::{
1414
CoverageOptions, DebugInfo, DumpMonoStatsFormat, ErrorOutputType, ExternEntry, ExternLocation,
1515
Externs, FmtDebug, FunctionReturn, InliningThreshold, Input, InstrumentCoverage,
1616
InstrumentXRay, LinkSelfContained, LinkerPluginLto, LocationDetail, LtoCli, MirIncludeSpans,
17-
NextSolverConfig, Offload, OomStrategy, Options, OutFileName, OutputType, OutputTypes,
18-
PAuthKey, PacRet, Passes, PatchableFunctionEntry, Polonius, ProcMacroExecutionStrategy, Strip,
19-
SwitchWithOptPath, SymbolManglingVersion, WasiExecModel, build_configuration,
20-
build_session_options, rustc_optgroups,
17+
NextSolverConfig, Offload, Options, OutFileName, OutputType, OutputTypes, PAuthKey, PacRet,
18+
Passes, PatchableFunctionEntry, Polonius, ProcMacroExecutionStrategy, Strip, SwitchWithOptPath,
19+
SymbolManglingVersion, WasiExecModel, build_configuration, build_session_options,
20+
rustc_optgroups,
2121
};
2222
use rustc_session::lint::Level;
2323
use rustc_session::search_paths::SearchPath;
@@ -839,7 +839,6 @@ fn test_unstable_options_tracking_hash() {
839839
tracked!(no_unique_section_names, true);
840840
tracked!(offload, vec![Offload::Enable]);
841841
tracked!(on_broken_pipe, OnBrokenPipe::Kill);
842-
tracked!(oom, OomStrategy::Panic);
843842
tracked!(osx_rpath_install_name, true);
844843
tracked!(packed_bundled_libs, true);
845844
tracked!(panic_abort_tests, true);

‎compiler/rustc_session/src/config.rs‎

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3127,8 +3127,8 @@ pub(crate) mod dep_tracking {
31273127
AnnotateMoves, AutoDiff, BranchProtection, CFGuard, CFProtection, CollapseMacroDebuginfo,
31283128
CoverageOptions, CrateType, DebugInfo, DebugInfoCompression, ErrorOutputType, FmtDebug,
31293129
FunctionReturn, InliningThreshold, InstrumentCoverage, InstrumentXRay, LinkerPluginLto,
3130-
LocationDetail, LtoCli, MirStripDebugInfo, NextSolverConfig, Offload, OomStrategy,
3131-
OptLevel, OutFileName, OutputType, OutputTypes, PatchableFunctionEntry, Polonius,
3130+
LocationDetail, LtoCli, MirStripDebugInfo, NextSolverConfig, Offload, OptLevel,
3131+
OutFileName, OutputType, OutputTypes, PatchableFunctionEntry, Polonius,
31323132
RemapPathScopeComponents, ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind,
31333133
SwitchWithOptPath, SymbolManglingVersion, WasiExecModel,
31343134
};
@@ -3227,7 +3227,6 @@ pub(crate) mod dep_tracking {
32273227
LocationDetail,
32283228
FmtDebug,
32293229
BranchProtection,
3230-
OomStrategy,
32313230
LanguageIdentifier,
32323231
NextSolverConfig,
32333232
PatchableFunctionEntry,
@@ -3340,27 +3339,6 @@ pub(crate) mod dep_tracking {
33403339
}
33413340
}
33423341

3343-
/// Default behavior to use in out-of-memory situations.
3344-
#[derive(Clone, Copy, PartialEq, Hash, Debug, Encodable, Decodable, HashStable_Generic)]
3345-
pub enum OomStrategy {
3346-
/// Generate a panic that can be caught by `catch_unwind`.
3347-
Panic,
3348-
3349-
/// Abort the process immediately.
3350-
Abort,
3351-
}
3352-
3353-
impl OomStrategy {
3354-
pub const SYMBOL: &'static str = "__rust_alloc_error_handler_should_panic_v2";
3355-
3356-
pub fn should_panic(self) -> u8 {
3357-
match self {
3358-
OomStrategy::Panic => 1,
3359-
OomStrategy::Abort => 0,
3360-
}
3361-
}
3362-
}
3363-
33643342
/// How to run proc-macro code when building this crate
33653343
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
33663344
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`, `thread`, or 'realtime'";
812811
pub(crate) const parse_sanitizer_memory_track_origins: &str = "0, 1, or 2";
@@ -1242,15 +1241,6 @@ pub mod parse {
12421241
false
12431242
}
12441243

1245-
pub(crate) fn parse_oom_strategy(slot: &mut OomStrategy, v: Option<&str>) -> bool {
1246-
match v {
1247-
Some("panic") => *slot = OomStrategy::Panic,
1248-
Some("abort") => *slot = OomStrategy::Abort,
1249-
_ => return false,
1250-
}
1251-
true
1252-
}
1253-
12541244
pub(crate) fn parse_relro_level(slot: &mut Option<RelroLevel>, v: Option<&str>) -> bool {
12551245
match v {
12561246
Some(s) => match s.parse::<RelroLevel>() {
@@ -2529,8 +2519,6 @@ options! {
25292519
Currently the only option available"),
25302520
on_broken_pipe: OnBrokenPipe = (OnBrokenPipe::Default, parse_on_broken_pipe, [TRACKED],
25312521
"behavior of std::io::ErrorKind::BrokenPipe (SIGPIPE)"),
2532-
oom: OomStrategy = (OomStrategy::Abort, parse_oom_strategy, [TRACKED],
2533-
"panic strategy for out-of-memory handling"),
25342522
osx_rpath_install_name: bool = (false, parse_bool, [TRACKED],
25352523
"pass `-install_name @rpath/...` to the macOS linker (default: no)"),
25362524
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: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -357,17 +357,6 @@ fn default_alloc_error_hook(layout: Layout) {
357357
return;
358358
}
359359

360-
unsafe extern "Rust" {
361-
// This symbol is emitted by rustc next to __rust_alloc_error_handler.
362-
// Its value depends on the -Zoom={panic,abort} compiler option.
363-
#[rustc_std_internal_symbol]
364-
fn __rust_alloc_error_handler_should_panic_v2() -> u8;
365-
}
366-
367-
if unsafe { __rust_alloc_error_handler_should_panic_v2() != 0 } {
368-
panic!("memory allocation of {} bytes failed", layout.size());
369-
}
370-
371360
// This is the default path taken on OOM, and the only path taken on stable with std.
372361
// Crucially, it does *not* call any user-defined code, and therefore users do not have to
373362
// worry about allocation failure causing reentrancy issues. That makes it different from

‎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
use rustc_target::spec::{Arch, Os};
@@ -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)