summaryrefslogtreecommitdiff
path: root/compiler/rustc_codegen_cranelift
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-04-25 00:08:35 +0200
committerMatthias Krüger <matthias.krueger@famsik.de>2023-04-25 00:08:35 +0200
commitf54dbe6e3116a475f63b580884f07474239a0b25 (patch)
tree0b806920a4a86480c1a59c8cc126525d38310216 /compiler/rustc_codegen_cranelift
parent33253fa6a42565eeae40e63401a41ac2dcc9e92f (diff)
downloadrust-f54dbe6e3116a475f63b580884f07474239a0b25.tar.gz
Revert "Remove #[alloc_error_handler] from the compiler and library"
This reverts commit abc0660118cc95f47445fd33502a11dd448f5968.
Diffstat (limited to 'compiler/rustc_codegen_cranelift')
-rw-r--r--compiler/rustc_codegen_cranelift/example/alloc_example.rs7
-rw-r--r--compiler/rustc_codegen_cranelift/src/allocator.rs23
2 files changed, 28 insertions, 2 deletions
diff --git a/compiler/rustc_codegen_cranelift/example/alloc_example.rs b/compiler/rustc_codegen_cranelift/example/alloc_example.rs
index e39c3272958..4ede2fe4efe 100644
--- a/compiler/rustc_codegen_cranelift/example/alloc_example.rs
+++ b/compiler/rustc_codegen_cranelift/example/alloc_example.rs
@@ -1,4 +1,4 @@
-#![feature(start, core_intrinsics)]
+#![feature(start, core_intrinsics, alloc_error_handler)]
#![no_std]
extern crate alloc;
@@ -22,6 +22,11 @@ fn panic_handler(_: &core::panic::PanicInfo) -> ! {
core::intrinsics::abort();
}
+#[alloc_error_handler]
+fn alloc_error_handler(_: alloc::alloc::Layout) -> ! {
+ core::intrinsics::abort();
+}
+
#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
let world: Box<&str> = Box::new("Hello World!\0");
diff --git a/compiler/rustc_codegen_cranelift/src/allocator.rs b/compiler/rustc_codegen_cranelift/src/allocator.rs
index 9fb8079a21f..2c246ceb37d 100644
--- a/compiler/rustc_codegen_cranelift/src/allocator.rs
+++ b/compiler/rustc_codegen_cranelift/src/allocator.rs
@@ -6,6 +6,7 @@ use crate::prelude::*;
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
use rustc_codegen_ssa::base::allocator_kind_for_codegen;
use rustc_session::config::OomStrategy;
+use rustc_span::symbol::sym;
/// Returns whether an allocator shim was created
pub(crate) fn codegen(
@@ -14,7 +15,13 @@ pub(crate) fn codegen(
unwind_context: &mut UnwindContext,
) -> bool {
let Some(kind) = allocator_kind_for_codegen(tcx) else { return false };
- codegen_inner(module, unwind_context, kind, tcx.sess.opts.unstable_opts.oom);
+ codegen_inner(
+ module,
+ unwind_context,
+ kind,
+ tcx.alloc_error_handler_kind(()).unwrap(),
+ tcx.sess.opts.unstable_opts.oom,
+ );
true
}
@@ -22,6 +29,7 @@ fn codegen_inner(
module: &mut impl Module,
unwind_context: &mut UnwindContext,
kind: AllocatorKind,
+ alloc_error_handler_kind: AllocatorKind,
oom_strategy: OomStrategy,
) {
let usize_ty = module.target_config().pointer_type();
@@ -63,6 +71,19 @@ fn codegen_inner(
);
}
+ let sig = Signature {
+ call_conv: module.target_config().default_call_conv,
+ params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
+ returns: vec![],
+ };
+ crate::common::create_wrapper_function(
+ module,
+ unwind_context,
+ sig,
+ "__rust_alloc_error_handler",
+ &alloc_error_handler_kind.fn_name(sym::oom),
+ );
+
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
let mut data_ctx = DataContext::new();
data_ctx.set_align(1);