Skip to content

Commit eae0d46

Browse files
committed
Restore Global.oom() functionality
… now that #[global_allocator] does not define a symbol for it
1 parent 96c9d22 commit eae0d46

File tree

12 files changed

+57
-0
lines changed

12 files changed

+57
-0
lines changed

src/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/liballoc/alloc.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ extern "Rust" {
2626
#[allocator]
2727
#[rustc_allocator_nounwind]
2828
fn __rust_alloc(size: usize, align: usize, err: *mut u8) -> *mut u8;
29+
#[cold]
30+
#[rustc_allocator_nounwind]
31+
fn __rust_oom(err: *const u8) -> !;
2932
#[rustc_allocator_nounwind]
3033
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
3134
#[rustc_allocator_nounwind]
@@ -44,6 +47,9 @@ extern "Rust" {
4447
#[allocator]
4548
#[rustc_allocator_nounwind]
4649
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
50+
#[cold]
51+
#[rustc_allocator_nounwind]
52+
fn __rust_oom() -> !;
4753
#[rustc_allocator_nounwind]
4854
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
4955
#[rustc_allocator_nounwind]
@@ -120,6 +126,16 @@ unsafe impl Alloc for Global {
120126
Err(AllocErr)
121127
}
122128
}
129+
130+
#[inline]
131+
fn oom(&mut self) -> ! {
132+
unsafe {
133+
#[cfg(not(stage0))]
134+
__rust_oom();
135+
#[cfg(stage0)]
136+
__rust_oom(&mut 0);
137+
}
138+
}
123139
}
124140

125141
/// The allocator for unique pointers.

src/liballoc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
#![feature(from_ref)]
9898
#![feature(fundamental)]
9999
#![feature(lang_items)]
100+
#![feature(libc)]
100101
#![feature(needs_allocator)]
101102
#![feature(nonzero)]
102103
#![feature(optin_builtin_traits)]

src/liballoc_jemalloc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ test = false
1212
doc = false
1313

1414
[dependencies]
15+
alloc_system = { path = "../liballoc_system" }
1516
core = { path = "../libcore" }
1617
libc = { path = "../rustc/libc_shim" }
1718
compiler_builtins = { path = "../rustc/compiler_builtins_shim" }

src/liballoc_jemalloc/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
reason = "this library is unlikely to be stabilized in its current \
1515
form or name",
1616
issue = "27783")]
17+
#![feature(alloc_system)]
1718
#![feature(libc)]
1819
#![feature(linkage)]
1920
#![feature(staged_api)]
@@ -22,12 +23,15 @@
2223
#![cfg_attr(not(dummy_jemalloc), feature(allocator_api))]
2324
#![rustc_alloc_kind = "exe"]
2425

26+
extern crate alloc_system;
2527
extern crate libc;
2628

2729
#[cfg(not(dummy_jemalloc))]
2830
pub use contents::*;
2931
#[cfg(not(dummy_jemalloc))]
3032
mod contents {
33+
use core::alloc::GlobalAlloc;
34+
use alloc_system::System;
3135
use libc::{c_int, c_void, size_t};
3236

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

103+
#[no_mangle]
104+
#[rustc_std_internal_symbol]
105+
pub unsafe extern fn __rde_oom() -> ! {
106+
System.oom()
107+
}
108+
99109
#[no_mangle]
100110
#[rustc_std_internal_symbol]
101111
pub unsafe extern fn __rde_dealloc(ptr: *mut u8,

src/liballoc_system/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ mod platform {
367367
}
368368
}
369369

370+
#[inline]
370371
fn oom() -> ! {
371372
write_to_stderr("fatal runtime error: memory allocation failed");
372373
unsafe {
@@ -375,6 +376,7 @@ fn oom() -> ! {
375376
}
376377

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

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

388390
#[cfg(windows)]
391+
#[inline]
389392
fn write_to_stderr(s: &str) {
390393
use core::ptr;
391394

@@ -421,4 +424,5 @@ fn write_to_stderr(s: &str) {
421424
}
422425

423426
#[cfg(not(any(windows, unix, target_os = "redox")))]
427+
#[inline]
424428
fn write_to_stderr(_: &str) {}

src/libcore/alloc.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@ pub unsafe trait GlobalAlloc {
438438
}
439439
new_ptr
440440
}
441+
442+
fn oom(&self) -> ! {
443+
unsafe { ::intrinsics::abort() }
444+
}
441445
}
442446

443447
/// An implementation of `Alloc` can allocate, reallocate, and

src/librustc_allocator/expand.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ impl<'a> AllocFnFactory<'a> {
231231
}
232232

233233
AllocatorTy::ResultPtr |
234+
AllocatorTy::Bang |
234235
AllocatorTy::Unit => {
235236
panic!("can't convert AllocatorTy to an argument")
236237
}
@@ -248,6 +249,10 @@ impl<'a> AllocFnFactory<'a> {
248249
(self.ptr_u8(), expr)
249250
}
250251

252+
AllocatorTy::Bang => {
253+
(self.cx.ty(self.span, TyKind::Never), expr)
254+
}
255+
251256
AllocatorTy::Unit => {
252257
(self.cx.ty(self.span, TyKind::Tup(Vec::new())), expr)
253258
}

src/librustc_allocator/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
2323
inputs: &[AllocatorTy::Layout],
2424
output: AllocatorTy::ResultPtr,
2525
},
26+
AllocatorMethod {
27+
name: "oom",
28+
inputs: &[],
29+
output: AllocatorTy::Bang,
30+
},
2631
AllocatorMethod {
2732
name: "dealloc",
2833
inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout],
@@ -47,6 +52,7 @@ pub struct AllocatorMethod {
4752
}
4853

4954
pub enum AllocatorTy {
55+
Bang,
5056
Layout,
5157
Ptr,
5258
ResultPtr,

src/librustc_trans/allocator.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ pub(crate) unsafe fn trans(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind)
4343
AllocatorTy::Ptr => args.push(i8p),
4444
AllocatorTy::Usize => args.push(usize),
4545

46+
AllocatorTy::Bang |
4647
AllocatorTy::ResultPtr |
4748
AllocatorTy::Unit => panic!("invalid allocator arg"),
4849
}
4950
}
5051
let output = match method.output {
52+
AllocatorTy::Bang => None,
5153
AllocatorTy::ResultPtr => Some(i8p),
5254
AllocatorTy::Unit => None,
5355

src/libstd/alloc.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ pub mod __default_lib_allocator {
3535
System.alloc(layout) as *mut u8
3636
}
3737

38+
#[no_mangle]
39+
#[rustc_std_internal_symbol]
40+
pub unsafe extern fn __rdl_oom() -> ! {
41+
System.oom()
42+
}
43+
3844
#[no_mangle]
3945
#[rustc_std_internal_symbol]
4046
pub unsafe extern fn __rdl_dealloc(ptr: *mut u8,

src/test/compile-fail/allocator/not-an-allocator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ static A: usize = 0;
1616
//~| the trait bound `usize:
1717
//~| the trait bound `usize:
1818
//~| the trait bound `usize:
19+
//~| the trait bound `usize:
1920

2021
fn main() {}

0 commit comments

Comments
 (0)