about summary refs log tree commit diff
path: root/library/std/src/alloc.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-03-18 03:01:46 +0000
committerbors <bors@rust-lang.org>2022-03-18 03:01:46 +0000
commitd6f3a4ecb48ead838638e902f2fa4e5f3059779b (patch)
treea0ced664fbcf095dac8d3b0a498b9ce8b335cb53 /library/std/src/alloc.rs
parentcd119057160cedea245aa2679add56723f3dc784 (diff)
parent0254e318b820d79bb448d4d22d93e345e67b25eb (diff)
downloadrust-d6f3a4ecb48ead838638e902f2fa4e5f3059779b.tar.gz
rust-d6f3a4ecb48ead838638e902f2fa4e5f3059779b.zip
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.
Diffstat (limited to 'library/std/src/alloc.rs')
-rw-r--r--library/std/src/alloc.rs16
1 files changed, 15 insertions, 1 deletions
diff --git a/library/std/src/alloc.rs b/library/std/src/alloc.rs
index a8804396246..ae11964cb56 100644
--- a/library/std/src/alloc.rs
+++ b/library/std/src/alloc.rs
@@ -315,7 +315,21 @@ pub fn take_alloc_error_hook() -> fn(Layout) {
 }
 
 fn default_alloc_error_hook(layout: Layout) {
-    rtprintpanic!("memory allocation of {} bytes failed\n", layout.size());
+    #[cfg(not(bootstrap))]
+    extern "Rust" {
+        // This symbol is emitted by rustc next to __rust_alloc_error_handler.
+        // Its value depends on the -Zoom={panic,abort} compiler option.
+        static __rust_alloc_error_handler_should_panic: u8;
+    }
+    #[cfg(bootstrap)]
+    let __rust_alloc_error_handler_should_panic = 0;
+
+    #[allow(unused_unsafe)]
+    if unsafe { __rust_alloc_error_handler_should_panic != 0 } {
+        panic!("memory allocation of {} bytes failed\n", layout.size());
+    } else {
+        rtprintpanic!("memory allocation of {} bytes failed\n", layout.size());
+    }
 }
 
 #[cfg(not(test))]