Skip to content

Commit

Permalink
Restore Global.oom() functionality
Browse files Browse the repository at this point in the history
… now that #[global_allocator] does not define a symbol for it
  • Loading branch information
SimonSapin committed Apr 12, 2018
1 parent 96c9d22 commit eae0d46
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/liballoc/alloc.rs
Expand Up @@ -26,6 +26,9 @@ extern "Rust" {
#[allocator]
#[rustc_allocator_nounwind]
fn __rust_alloc(size: usize, align: usize, err: *mut u8) -> *mut u8;
#[cold]
#[rustc_allocator_nounwind]
fn __rust_oom(err: *const u8) -> !;
#[rustc_allocator_nounwind]
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
#[rustc_allocator_nounwind]
Expand All @@ -44,6 +47,9 @@ extern "Rust" {
#[allocator]
#[rustc_allocator_nounwind]
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
#[cold]
#[rustc_allocator_nounwind]
fn __rust_oom() -> !;
#[rustc_allocator_nounwind]
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
#[rustc_allocator_nounwind]
Expand Down Expand Up @@ -120,6 +126,16 @@ unsafe impl Alloc for Global {
Err(AllocErr)
}
}

#[inline]
fn oom(&mut self) -> ! {
unsafe {
#[cfg(not(stage0))]
__rust_oom();
#[cfg(stage0)]
__rust_oom(&mut 0);
}
}
}

/// The allocator for unique pointers.
Expand Down
1 change: 1 addition & 0 deletions src/liballoc/lib.rs
Expand Up @@ -97,6 +97,7 @@
#![feature(from_ref)]
#![feature(fundamental)]
#![feature(lang_items)]
#![feature(libc)]
#![feature(needs_allocator)]
#![feature(nonzero)]
#![feature(optin_builtin_traits)]
Expand Down
1 change: 1 addition & 0 deletions src/liballoc_jemalloc/Cargo.toml
Expand Up @@ -12,6 +12,7 @@ test = false
doc = false

[dependencies]
alloc_system = { path = "../liballoc_system" }
core = { path = "../libcore" }
libc = { path = "../rustc/libc_shim" }
compiler_builtins = { path = "../rustc/compiler_builtins_shim" }
Expand Down
10 changes: 10 additions & 0 deletions src/liballoc_jemalloc/lib.rs
Expand Up @@ -14,6 +14,7 @@
reason = "this library is unlikely to be stabilized in its current \
form or name",
issue = "27783")]
#![feature(alloc_system)]
#![feature(libc)]
#![feature(linkage)]
#![feature(staged_api)]
Expand All @@ -22,12 +23,15 @@
#![cfg_attr(not(dummy_jemalloc), feature(allocator_api))]
#![rustc_alloc_kind = "exe"]

extern crate alloc_system;
extern crate libc;

#[cfg(not(dummy_jemalloc))]
pub use contents::*;
#[cfg(not(dummy_jemalloc))]
mod contents {
use core::alloc::GlobalAlloc;
use alloc_system::System;
use libc::{c_int, c_void, size_t};

// Note that the symbols here are prefixed by default on macOS and Windows (we
Expand Down Expand Up @@ -96,6 +100,12 @@ mod contents {
ptr
}

#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rde_oom() -> ! {
System.oom()
}

#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rde_dealloc(ptr: *mut u8,
Expand Down
4 changes: 4 additions & 0 deletions src/liballoc_system/lib.rs
Expand Up @@ -367,6 +367,7 @@ mod platform {
}
}

#[inline]
fn oom() -> ! {
write_to_stderr("fatal runtime error: memory allocation failed");
unsafe {
Expand All @@ -375,6 +376,7 @@ fn oom() -> ! {
}

#[cfg(any(unix, target_os = "redox"))]
#[inline]
fn write_to_stderr(s: &str) {
extern crate libc;

Expand All @@ -386,6 +388,7 @@ fn write_to_stderr(s: &str) {
}

#[cfg(windows)]
#[inline]
fn write_to_stderr(s: &str) {
use core::ptr;

Expand Down Expand Up @@ -421,4 +424,5 @@ fn write_to_stderr(s: &str) {
}

#[cfg(not(any(windows, unix, target_os = "redox")))]
#[inline]
fn write_to_stderr(_: &str) {}
4 changes: 4 additions & 0 deletions src/libcore/alloc.rs
Expand Up @@ -438,6 +438,10 @@ pub unsafe trait GlobalAlloc {
}
new_ptr
}

fn oom(&self) -> ! {
unsafe { ::intrinsics::abort() }
}
}

/// An implementation of `Alloc` can allocate, reallocate, and
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_allocator/expand.rs
Expand Up @@ -231,6 +231,7 @@ impl<'a> AllocFnFactory<'a> {
}

AllocatorTy::ResultPtr |
AllocatorTy::Bang |
AllocatorTy::Unit => {
panic!("can't convert AllocatorTy to an argument")
}
Expand All @@ -248,6 +249,10 @@ impl<'a> AllocFnFactory<'a> {
(self.ptr_u8(), expr)
}

AllocatorTy::Bang => {
(self.cx.ty(self.span, TyKind::Never), expr)
}

AllocatorTy::Unit => {
(self.cx.ty(self.span, TyKind::Tup(Vec::new())), expr)
}
Expand Down
6 changes: 6 additions & 0 deletions src/librustc_allocator/lib.rs
Expand Up @@ -23,6 +23,11 @@ pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
inputs: &[AllocatorTy::Layout],
output: AllocatorTy::ResultPtr,
},
AllocatorMethod {
name: "oom",
inputs: &[],
output: AllocatorTy::Bang,
},
AllocatorMethod {
name: "dealloc",
inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout],
Expand All @@ -47,6 +52,7 @@ pub struct AllocatorMethod {
}

pub enum AllocatorTy {
Bang,
Layout,
Ptr,
ResultPtr,
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_trans/allocator.rs
Expand Up @@ -43,11 +43,13 @@ pub(crate) unsafe fn trans(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind)
AllocatorTy::Ptr => args.push(i8p),
AllocatorTy::Usize => args.push(usize),

AllocatorTy::Bang |
AllocatorTy::ResultPtr |
AllocatorTy::Unit => panic!("invalid allocator arg"),
}
}
let output = match method.output {
AllocatorTy::Bang => None,
AllocatorTy::ResultPtr => Some(i8p),
AllocatorTy::Unit => None,

Expand Down
6 changes: 6 additions & 0 deletions src/libstd/alloc.rs
Expand Up @@ -35,6 +35,12 @@ pub mod __default_lib_allocator {
System.alloc(layout) as *mut u8
}

#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rdl_oom() -> ! {
System.oom()
}

#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rdl_dealloc(ptr: *mut u8,
Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/allocator/not-an-allocator.rs
Expand Up @@ -16,5 +16,6 @@ static A: usize = 0;
//~| the trait bound `usize:
//~| the trait bound `usize:
//~| the trait bound `usize:
//~| the trait bound `usize:

fn main() {}

0 comments on commit eae0d46

Please sign in to comment.