Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Make handle_alloc_error default to panic (for no_std + liballoc) #76448

Merged
merged 1 commit into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion compiler/rustc_codegen_llvm/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ use libc::c_uint;
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
use rustc_middle::bug;
use rustc_middle::ty::TyCtxt;
use rustc_span::symbol::sym;

use crate::llvm::{self, False, True};
use crate::ModuleLlvm;

pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut ModuleLlvm, kind: AllocatorKind) {
pub(crate) unsafe fn codegen(
tcx: TyCtxt<'_>,
mods: &mut ModuleLlvm,
kind: AllocatorKind,
has_alloc_error_handler: bool,
) {
let llcx = &*mods.llcx;
let llmod = mods.llmod();
let usize = match &tcx.sess.target.target.target_pointer_width[..] {
Expand Down Expand Up @@ -82,4 +88,41 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut ModuleLlvm, kind: Alloc
}
llvm::LLVMDisposeBuilder(llbuilder);
}

// rust alloc error handler
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason you duplicated this code rather than adding a new entry to ALLOCATOR_METHODS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjorn3

  1. ALLOCATOR_METHODS is also used elsewhere to build other stuff, which I didn't want to clobber
  2. the alloc error handler has a different function signature
  3. the alloc error handler has a different name

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjorn3 feel free to refactor the stuff without side effects

let args = [usize, usize]; // size, align

let ty = llvm::LLVMFunctionType(void, args.as_ptr(), args.len() as c_uint, False);
let name = format!("__rust_alloc_error_handler");
let llfn = llvm::LLVMRustGetOrInsertFunction(llmod, name.as_ptr().cast(), name.len(), ty);
// -> ! DIFlagNoReturn
llvm::Attribute::NoReturn.apply_llfn(llvm::AttributePlace::Function, llfn);

if tcx.sess.target.target.options.default_hidden_visibility {
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
}
if tcx.sess.must_emit_unwind_tables() {
attributes::emit_uwtable(llfn, true);
}

let kind = if has_alloc_error_handler { AllocatorKind::Global } else { AllocatorKind::Default };
let callee = kind.fn_name(sym::oom);
let callee = llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr().cast(), callee.len(), ty);
// -> ! DIFlagNoReturn
llvm::Attribute::NoReturn.apply_llfn(llvm::AttributePlace::Function, callee);
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);

let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, "entry\0".as_ptr().cast());

let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);
let args = args
.iter()
.enumerate()
.map(|(i, _)| llvm::LLVMGetParam(llfn, i as c_uint))
.collect::<Vec<_>>();
let ret = llvm::LLVMRustBuildCall(llbuilder, callee, args.as_ptr(), args.len() as c_uint, None);
llvm::LLVMSetTailCall(ret, True);
llvm::LLVMBuildRetVoid(llbuilder);
llvm::LLVMDisposeBuilder(llbuilder);
}
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_llvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
tcx: TyCtxt<'tcx>,
mods: &mut ModuleLlvm,
kind: AllocatorKind,
has_alloc_error_handler: bool,
) {
unsafe { allocator::codegen(tcx, mods, kind) }
unsafe { allocator::codegen(tcx, mods, kind, has_alloc_error_handler) }
}
fn compile_codegen_unit(
&self,
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,9 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
let llmod_id =
cgu_name_builder.build_cgu_name(LOCAL_CRATE, &["crate"], Some("allocator")).to_string();
let mut modules = backend.new_metadata(tcx, &llmod_id);
tcx.sess
.time("write_allocator_module", || backend.codegen_allocator(tcx, &mut modules, kind));
tcx.sess.time("write_allocator_module", || {
backend.codegen_allocator(tcx, &mut modules, kind, tcx.lang_items().oom().is_some())
});

Some(ModuleCodegen { name: llmod_id, module_llvm: modules, kind: ModuleKind::Allocator })
} else {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_ssa/src/traits/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Se
tcx: TyCtxt<'tcx>,
mods: &mut Self::Module,
kind: AllocatorKind,
has_alloc_error_handler: bool,
);
/// This generates the codegen unit and returns it along with
/// a `u64` giving an estimate of the unit's processing cost.
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,9 @@ declare_features! (
/// Allows to use the `#[cmse_nonsecure_entry]` attribute.
(active, cmse_nonsecure_entry, "1.48.0", Some(75835), None),

/// Allows rustc to inject a default alloc_error_handler
(active, default_alloc_error_handler, "1.48.0", Some(66741), None),

// -------------------------------------------------------------------------
// feature-group-end: actual feature gates
// -------------------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_passes/src/weak_lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
if item == LangItem::PanicImpl {
tcx.sess.err("`#[panic_handler]` function required, but not found");
} else if item == LangItem::Oom {
tcx.sess.err("`#[alloc_error_handler]` function required, but not found");
if !tcx.features().default_alloc_error_handler {
tcx.sess.err("`#[alloc_error_handler]` function required, but not found.");
tcx.sess.note_without_error("Use `#![feature(default_alloc_error_handler)]` for a default error handler.");
}
} else {
tcx.sess.err(&format!("language item required, but not found: `{}`", name));
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ symbols! {
decl_macro,
declare_lint_pass,
decode,
default_alloc_error_handler,
default_lib_allocator,
default_type_parameter_fallback,
default_type_params,
Expand Down
47 changes: 47 additions & 0 deletions library/alloc/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ extern "Rust" {
fn __rust_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8;
#[rustc_allocator_nounwind]
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
#[rustc_allocator_nounwind]
fn __rust_alloc_error_handler(size: usize, align: usize) -> !;
}

/// The global memory allocator.
Expand Down Expand Up @@ -334,6 +336,24 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
/// [`set_alloc_error_hook`]: ../../std/alloc/fn.set_alloc_error_hook.html
/// [`take_alloc_error_hook`]: ../../std/alloc/fn.take_alloc_error_hook.html
#[stable(feature = "global_alloc", since = "1.28.0")]
#[cfg(not(any(test, bootstrap)))]
#[rustc_allocator_nounwind]
pub fn handle_alloc_error(layout: Layout) -> ! {
unsafe {
__rust_alloc_error_handler(layout.size(), layout.align());
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For tests you can just do:

pub use std::alloc::handle_alloc_error;

This works since std is imported by tests: https://github.com/haraldh/rust-1/blob/5ac1d7867411e1f28d3cee45df8c219fb39f414e/library/alloc/src/lib.rs#L146

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, changed


// For alloc test `std::alloc::handle_alloc_error` can be used directly.
#[cfg(test)]
pub use std::alloc::handle_alloc_error;

// In stage0 (bootstrap) `__rust_alloc_error_handler`,
// might not be generated yet, because an old compiler is used,
// so use the old direct call.
#[cfg(all(bootstrap, not(test)))]
#[stable(feature = "global_alloc", since = "1.28.0")]
#[doc(hidden)]
#[rustc_allocator_nounwind]
pub fn handle_alloc_error(layout: Layout) -> ! {
extern "Rust" {
Expand All @@ -342,3 +362,30 @@ pub fn handle_alloc_error(layout: Layout) -> ! {
}
unsafe { oom_impl(layout) }
}

#[cfg(not(any(test, bootstrap)))]
#[doc(hidden)]
#[allow(unused_attributes)]
#[unstable(feature = "alloc_internals", issue = "none")]
pub mod __default_lib_allocator {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this module name, given that this is not an allocator, but an alloc-error handler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true

use crate::alloc::Layout;

// called via generated `__rust_alloc_error_handler`

// if there is no `#[alloc_error_handler]`
#[rustc_std_internal_symbol]
pub unsafe extern "C" fn __rdl_oom(size: usize, _align: usize) -> ! {
panic!("memory allocation of {} bytes failed", size)
}

// if there is a `#[alloc_error_handler]`
#[rustc_std_internal_symbol]
pub unsafe extern "C" fn __rg_oom(size: usize, align: usize) -> ! {
Copy link
Member

@RalfJung RalfJung Oct 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing like this exists for the allocator, right? There the generated __rust_alloc etc symbols directly call the lang item? Looks like this is needed because the argument types differ; is there any plan to get rid of that mismatch (and the resulting two indirections through unknown extern functions)?

Copy link
Member

@RalfJung RalfJung Oct 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah no, the __rd functions are just generated by the macro expansion code. That has the advantage that they do not themselves call an unknown extern function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed, if compiler/rustc_codegen_llvm/src/allocator.rs calls directly into the lang item, but with the extern "Rust" API and a Layout object reference.

I just didn't know, how to generate a extern "Rust" fn in compiler/rustc_codegen_llvm/src/allocator.rs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the symmetric thing to do (when compared with #[global_allocator]) would be for the #[alloc_error_handler] attribute to generate __rg_oom, but I guess it doesn't really matter.

It is just somewhat dissatisfying that the very same problem is solved three times in slightly different ways with various amounts of code sharing (global allocator, alloc error handler, panic handler).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the symmetric thing to do (when compared with #[global_allocator]) would be for the #[alloc_error_handler] attribute to generate __rg_oom, but I guess it doesn't really matter.

It is just somewhat dissatisfying that the very same problem is solved three times in slightly different ways with various amounts of code sharing (global allocator, alloc error handler, panic handler).

I agree, but to implement it efficiently, I lack the expertise to redirect it via the rust ABI.

let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
extern "Rust" {
#[lang = "oom"]
fn oom_impl(layout: Layout) -> !;
}
unsafe { oom_impl(layout) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that, unlike before, the #[alloc_error_handler] is now allowed to unwind? There's no #[rustc_allocator_nounwind] any more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, but __rust_alloc_error_handler above still has #[rustc_allocator_nounwind]. How is that not UB when it ends up calling __rdl_oom?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I blindly copied #[rustc_allocator_nounwind]. Sorry. Remove it, if it is safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, or the other way round, I might have missed it.

}
}
4 changes: 3 additions & 1 deletion src/test/ui/allocator/auxiliary/helper.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// no-prefer-dynamic

#![crate_type = "rlib"]
#![no_std]

use std::fmt;
extern crate alloc;
use alloc::fmt;

pub fn work_with(p: &fmt::Debug) {
drop(p);
Expand Down
97 changes: 97 additions & 0 deletions src/test/ui/allocator/no_std-alloc-error-handler-custom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// run-pass
// ignore-android no libc
// ignore-cloudabi no libc
// ignore-emscripten no libc
// ignore-sgx no libc
// ignore-wasm32 no libc
// only-linux
// compile-flags:-C panic=abort
// aux-build:helper.rs

#![feature(start, rustc_private, new_uninit, panic_info_message)]
#![feature(alloc_error_handler)]
#![no_std]

extern crate alloc;
extern crate libc;

// ARM targets need these symbols
#[no_mangle]
pub fn __aeabi_unwind_cpp_pr0() {}

#[no_mangle]
pub fn __aeabi_unwind_cpp_pr1() {}

use core::ptr::null_mut;
use core::alloc::{GlobalAlloc, Layout};
use alloc::boxed::Box;

extern crate helper;

struct MyAllocator;

#[alloc_error_handler]
fn my_oom(layout: Layout) -> !
{
use alloc::fmt::write;
unsafe {
let size = layout.size();
let mut s = alloc::string::String::new();
write(&mut s, format_args!("My OOM: failed to allocate {} bytes!\n", size)).unwrap();
let s = s.as_str();
libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len());
libc::exit(0)
}
}

unsafe impl GlobalAlloc for MyAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
if layout.size() < 4096 {
libc::malloc(layout.size()) as _
} else {
null_mut()
}
}
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
}

#[global_allocator]
static A: MyAllocator = MyAllocator;

#[panic_handler]
fn panic(panic_info: &core::panic::PanicInfo) -> ! {
unsafe {
if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
const PSTR: &str = "panic occurred: ";
const CR: &str = "\n";
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len());
libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len());
libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len());
}
if let Some(args) = panic_info.message() {
let mut s = alloc::string::String::new();
alloc::fmt::write(&mut s, *args).unwrap();
let s = s.as_str();
const PSTR: &str = "panic occurred: ";
const CR: &str = "\n";
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len());
libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len());
libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len());
} else {
const PSTR: &str = "panic occurred\n";
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len());
}
libc::exit(1)
}
}

#[derive(Debug)]
struct Page([[u64; 32]; 16]);

#[start]
pub fn main(_argc: isize, _argv: *const *const u8) -> isize {
let zero = Box::<Page>::new_zeroed();
let zero = unsafe { zero.assume_init() };
helper::work_with(&zero);
1
}
84 changes: 84 additions & 0 deletions src/test/ui/allocator/no_std-alloc-error-handler-default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// run-pass
// ignore-android no libc
// ignore-cloudabi no libc
// ignore-emscripten no libc
// ignore-sgx no libc
// ignore-wasm32 no libc
// only-linux
// compile-flags:-C panic=abort
// aux-build:helper.rs
// gate-test-default_alloc_error_handler

#![feature(start, rustc_private, new_uninit, panic_info_message)]
#![feature(default_alloc_error_handler)]
#![no_std]

extern crate alloc;
extern crate libc;

// ARM targets need these symbols
#[no_mangle]
pub fn __aeabi_unwind_cpp_pr0() {}

#[no_mangle]
pub fn __aeabi_unwind_cpp_pr1() {}

use alloc::boxed::Box;
use core::alloc::{GlobalAlloc, Layout};
use core::ptr::null_mut;

extern crate helper;

struct MyAllocator;

unsafe impl GlobalAlloc for MyAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
if layout.size() < 4096 {
libc::malloc(layout.size()) as _
} else {
null_mut()
}
}
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
}

#[global_allocator]
static A: MyAllocator = MyAllocator;

#[panic_handler]
fn panic(panic_info: &core::panic::PanicInfo) -> ! {
unsafe {
if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
const PSTR: &str = "panic occurred: ";
const CR: &str = "\n";
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len());
libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len());
libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len());
}
if let Some(args) = panic_info.message() {
let mut s = alloc::string::String::new();
alloc::fmt::write(&mut s, *args).unwrap();
let s = s.as_str();
const PSTR: &str = "panic occurred: ";
const CR: &str = "\n";
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len());
libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len());
libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len());
} else {
const PSTR: &str = "panic occurred\n";
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len());
}
libc::exit(0)
}
}

#[derive(Debug)]
struct Page([[u64; 32]; 16]);

#[start]
pub fn main(_argc: isize, _argv: *const *const u8) -> isize {
let zero = Box::<Page>::new_zeroed();
let zero = unsafe { zero.assume_init() };
helper::work_with(&zero);
1
}
4 changes: 3 additions & 1 deletion src/test/ui/missing/missing-alloc_error_handler.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
error: `#[alloc_error_handler]` function required, but not found
error: `#[alloc_error_handler]` function required, but not found.

note: Use `#![feature(default_alloc_error_handler)]` for a default error handler.

error: aborting due to previous error