Skip to content

Commit c8741dc

Browse files
committed
Auto merge of #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 #43596). Perf and binary size tests show negligible impact.
2 parents dcafc5f + ef56436 commit c8741dc

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

std/src/alloc.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,21 @@ pub fn take_alloc_error_hook() -> fn(Layout) {
315315
}
316316

317317
fn default_alloc_error_hook(layout: Layout) {
318-
rtprintpanic!("memory allocation of {} bytes failed\n", layout.size());
318+
#[cfg(not(bootstrap))]
319+
extern "Rust" {
320+
// This symbol is emitted by rustc next to __rust_alloc_error_handler.
321+
// Its value depends on the -Zoom={panic,abort} compiler option.
322+
static __rust_alloc_error_handler_should_panic: u8;
323+
}
324+
#[cfg(bootstrap)]
325+
let __rust_alloc_error_handler_should_panic = 0;
326+
327+
#[allow(unused_unsafe)]
328+
if unsafe { __rust_alloc_error_handler_should_panic != 0 } {
329+
panic!("memory allocation of {} bytes failed\n", layout.size());
330+
} else {
331+
rtprintpanic!("memory allocation of {} bytes failed\n", layout.size());
332+
}
319333
}
320334

321335
#[cfg(not(test))]

0 commit comments

Comments
 (0)