Skip to content

Replace {Alloc,GlobalAlloc}::oom with a lang item. #50144

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

Merged
merged 3 commits into from
Apr 22, 2018
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
1 change: 0 additions & 1 deletion src/Cargo.lock

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

38 changes: 17 additions & 21 deletions src/liballoc/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ 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 @@ -107,16 +104,6 @@ unsafe impl GlobalAlloc for Global {
let ptr = __rust_alloc_zeroed(layout.size(), layout.align(), &mut 0);
ptr as *mut Opaque
}

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

unsafe impl Alloc for Global {
Expand Down Expand Up @@ -144,11 +131,6 @@ unsafe impl Alloc for Global {
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<Opaque>, AllocErr> {
NonNull::new(GlobalAlloc::alloc_zeroed(self, layout)).ok_or(AllocErr)
}

#[inline]
fn oom(&mut self) -> ! {
GlobalAlloc::oom(self)
}
}

/// The allocator for unique pointers.
Expand All @@ -165,7 +147,7 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
if !ptr.is_null() {
ptr as *mut u8
} else {
Global.oom()
oom()
}
}
}
Expand All @@ -182,19 +164,33 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
}
}

#[cfg(stage0)]
pub fn oom() -> ! {
unsafe { ::core::intrinsics::abort() }
}

#[cfg(not(stage0))]
pub fn oom() -> ! {
extern {
#[lang = "oom"]
fn oom_impl() -> !;
}
unsafe { oom_impl() }
}

#[cfg(test)]
mod tests {
extern crate test;
use self::test::Bencher;
use boxed::Box;
use alloc::{Global, Alloc, Layout};
use alloc::{Global, Alloc, Layout, oom};

#[test]
fn allocate_zeroed() {
unsafe {
let layout = Layout::from_size_align(1024, 1).unwrap();
let ptr = Global.alloc_zeroed(layout.clone())
.unwrap_or_else(|_| Global.oom());
.unwrap_or_else(|_| oom());

let mut i = ptr.cast::<u8>().as_ptr();
let end = i.offset(layout.size() as isize);
Expand Down
4 changes: 2 additions & 2 deletions src/liballoc/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use core::hash::{Hash, Hasher};
use core::{isize, usize};
use core::convert::From;

use alloc::{Global, Alloc, Layout, box_free};
use alloc::{Global, Alloc, Layout, box_free, oom};
use boxed::Box;
use string::String;
use vec::Vec;
Expand Down Expand Up @@ -553,7 +553,7 @@ impl<T: ?Sized> Arc<T> {
let layout = Layout::for_value(&*fake_ptr);

let mem = Global.alloc(layout)
.unwrap_or_else(|_| Global.oom());
.unwrap_or_else(|_| oom());

// Initialize the real ArcInner
let inner = set_data_ptr(ptr as *mut T, mem.as_ptr() as *mut u8) as *mut ArcInner<T>;
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ unsafe impl<T> Alloc for T where T: CoreAlloc {
}

fn oom(&mut self, _: AllocErr) -> ! {
CoreAlloc::oom(self)
unsafe { ::core::intrinsics::abort() }
Copy link
Member

Choose a reason for hiding this comment

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

Why doesn't this use the weak lang item?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is only used for stage0 I believe.

}

fn usable_size(&self, layout: &Layout) -> (usize, usize) {
Expand Down
14 changes: 7 additions & 7 deletions src/liballoc/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use core::ops::Drop;
use core::ptr::{self, NonNull, Unique};
use core::slice;

use alloc::{Alloc, Layout, Global};
use alloc::{Alloc, Layout, Global, oom};
use alloc::CollectionAllocErr;
use alloc::CollectionAllocErr::*;
use boxed::Box;
Expand Down Expand Up @@ -101,7 +101,7 @@ impl<T, A: Alloc> RawVec<T, A> {
};
match result {
Ok(ptr) => ptr,
Err(_) => a.oom(),
Err(_) => oom(),
}
};

Expand Down Expand Up @@ -316,7 +316,7 @@ impl<T, A: Alloc> RawVec<T, A> {
new_size);
match ptr_res {
Ok(ptr) => (new_cap, ptr.cast().into()),
Err(_) => self.a.oom(),
Err(_) => oom(),
}
}
None => {
Expand All @@ -325,7 +325,7 @@ impl<T, A: Alloc> RawVec<T, A> {
let new_cap = if elem_size > (!0) / 8 { 1 } else { 4 };
match self.a.alloc_array::<T>(new_cap) {
Ok(ptr) => (new_cap, ptr.into()),
Err(_) => self.a.oom(),
Err(_) => oom(),
}
}
};
Expand Down Expand Up @@ -442,7 +442,7 @@ impl<T, A: Alloc> RawVec<T, A> {
pub fn reserve_exact(&mut self, used_cap: usize, needed_extra_cap: usize) {
match self.try_reserve_exact(used_cap, needed_extra_cap) {
Err(CapacityOverflow) => capacity_overflow(),
Err(AllocErr) => self.a.oom(),
Err(AllocErr) => oom(),
Ok(()) => { /* yay */ }
}
}
Expand Down Expand Up @@ -552,7 +552,7 @@ impl<T, A: Alloc> RawVec<T, A> {
pub fn reserve(&mut self, used_cap: usize, needed_extra_cap: usize) {
match self.try_reserve(used_cap, needed_extra_cap) {
Err(CapacityOverflow) => capacity_overflow(),
Err(AllocErr) => self.a.oom(),
Err(AllocErr) => oom(),
Ok(()) => { /* yay */ }
}
}
Expand Down Expand Up @@ -667,7 +667,7 @@ impl<T, A: Alloc> RawVec<T, A> {
old_layout,
new_size) {
Ok(p) => self.ptr = p.cast().into(),
Err(_) => self.a.oom(),
Err(_) => oom(),
}
}
self.cap = amount;
Expand Down
4 changes: 2 additions & 2 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ use core::ops::CoerceUnsized;
use core::ptr::{self, NonNull};
use core::convert::From;

use alloc::{Global, Alloc, Layout, Opaque, box_free};
use alloc::{Global, Alloc, Layout, Opaque, box_free, oom};
use string::String;
use vec::Vec;

Expand Down Expand Up @@ -668,7 +668,7 @@ impl<T: ?Sized> Rc<T> {
let layout = Layout::for_value(&*fake_ptr);

let mem = Global.alloc(layout)
.unwrap_or_else(|_| Global.oom());
.unwrap_or_else(|_| oom());

// Initialize the real RcBox
let inner = set_data_ptr(ptr as *mut T, mem.as_ptr() as *mut u8) as *mut RcBox<T>;
Expand Down
1 change: 0 additions & 1 deletion src/liballoc_jemalloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ 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
8 changes: 3 additions & 5 deletions src/liballoc_jemalloc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
reason = "this library is unlikely to be stabilized in its current \
form or name",
issue = "27783")]
#![feature(alloc_system)]
#![feature(core_intrinsics)]
#![feature(libc)]
#![feature(linkage)]
#![feature(staged_api)]
Expand All @@ -23,15 +23,12 @@
#![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 @@ -100,10 +97,11 @@ mod contents {
ptr
}

#[cfg(stage0)]
#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rde_oom() -> ! {
System.oom()
::core::intrinsics::abort();
}

#[no_mangle]
Expand Down
70 changes: 0 additions & 70 deletions src/liballoc_system/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ unsafe impl Alloc for System {
new_size: usize) -> Result<NonNull<Opaque>, AllocErr> {
NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
}

#[inline]
fn oom(&mut self) -> ! {
::oom()
}
}

#[cfg(stage0)]
Expand Down Expand Up @@ -103,11 +98,6 @@ unsafe impl<'a> Alloc for &'a System {
new_size: usize) -> Result<NonNull<Opaque>, AllocErr> {
NonNull::new(GlobalAlloc::realloc(*self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
}

#[inline]
fn oom(&mut self) -> ! {
::oom()
}
}

#[cfg(any(windows, unix, target_os = "cloudabi", target_os = "redox"))]
Expand Down Expand Up @@ -366,63 +356,3 @@ mod platform {
}
}
}

#[inline]
fn oom() -> ! {
write_to_stderr("fatal runtime error: memory allocation failed");
unsafe {
::core::intrinsics::abort();
}
}

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

unsafe {
libc::write(libc::STDERR_FILENO,
s.as_ptr() as *const libc::c_void,
s.len());
}
}

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

type LPVOID = *mut u8;
type HANDLE = LPVOID;
type DWORD = u32;
type BOOL = i32;
type LPDWORD = *mut DWORD;
type LPOVERLAPPED = *mut u8;

const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;

extern "system" {
fn WriteFile(hFile: HANDLE,
lpBuffer: LPVOID,
nNumberOfBytesToWrite: DWORD,
lpNumberOfBytesWritten: LPDWORD,
lpOverlapped: LPOVERLAPPED)
-> BOOL;
fn GetStdHandle(which: DWORD) -> HANDLE;
}

unsafe {
// WriteFile silently fails if it is passed an invalid
// handle, so there is no need to check the result of
// GetStdHandle.
WriteFile(GetStdHandle(STD_ERROR_HANDLE),
s.as_ptr() as LPVOID,
s.len() as DWORD,
ptr::null_mut(),
ptr::null_mut());
}
}

#[cfg(not(any(windows, unix, target_os = "redox")))]
#[inline]
fn write_to_stderr(_: &str) {}
37 changes: 0 additions & 37 deletions src/libcore/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,17 +451,6 @@ pub unsafe trait GlobalAlloc {
}
new_ptr
}

/// Aborts the thread or process, optionally performing
/// cleanup or logging diagnostic information before panicking or
/// aborting.
///
/// `oom` is meant to be used by clients unable to cope with an
/// unsatisfied allocation request, and wish to abandon
/// computation rather than attempt to recover locally.
fn oom(&self) -> ! {
unsafe { ::intrinsics::abort() }
}
}

/// An implementation of `Alloc` can allocate, reallocate, and
Expand Down Expand Up @@ -614,32 +603,6 @@ pub unsafe trait Alloc {
/// to allocate that block of memory.
unsafe fn dealloc(&mut self, ptr: NonNull<Opaque>, layout: Layout);

/// Allocator-specific method for signaling an out-of-memory
/// condition.
///
/// `oom` aborts the thread or process, optionally performing
/// cleanup or logging diagnostic information before panicking or
/// aborting.
///
/// `oom` is meant to be used by clients unable to cope with an
/// unsatisfied allocation request, and wish to abandon
/// computation rather than attempt to recover locally.
///
/// Implementations of the `oom` method are discouraged from
/// infinitely regressing in nested calls to `oom`. In
/// practice this means implementors should eschew allocating,
/// especially from `self` (directly or indirectly).
///
/// Implementations of the allocation and reallocation methods
/// (e.g. `alloc`, `alloc_one`, `realloc`) are discouraged from
/// panicking (or aborting) in the event of memory exhaustion;
/// instead they should return an appropriate error from the
/// invoked method, and let the client decide whether to invoke
/// this `oom` method in response.
fn oom(&mut self) -> ! {
unsafe { ::intrinsics::abort() }
}

// == ALLOCATOR-SPECIFIC QUANTITIES AND LIMITS ==
// usable_size

Expand Down
3 changes: 2 additions & 1 deletion src/librustc/middle/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ language_item_table! {

ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn;
BoxFreeFnLangItem, "box_free", box_free_fn;
DropInPlaceFnLangItem, "drop_in_place", drop_in_place_fn;
DropInPlaceFnLangItem, "drop_in_place", drop_in_place_fn;
OomLangItem, "oom", oom;
Copy link
Member

Choose a reason for hiding this comment

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

Are weak lang items not grouped in here?


StartFnLangItem, "start", start_fn;

Expand Down
Loading