Skip to content

Commit a5a075c

Browse files
committed
Bug 1465709 - Hook rust OOM handler on rustc 1.28. r=froydnj
Bug 1458161 added a rust OOM handler based on an unstable API that was removed in 1.27, replaced with something that didn't allow to get the failed allocation size. Latest 1.28 nightly (2018-06-13) has rust-lang/rust#50880, rust-lang/rust#51264 and rust-lang/rust#51241 merged, which allow to hook the OOM handler and get the failed allocation size again. Because this is still an unstable API, we explicitly depend on strict versions of rustc. We also explicitly error out if automation builds end up using a rustc version that doesn't allow us to get the allocation size for rust OOM, because we don't want that to happen without knowing. UltraBlame original commit: 5182bca90d0609f182d5a7b6b48ed2ffbbce32c2
1 parent 9d38877 commit a5a075c

File tree

7 files changed

+40
-1
lines changed

7 files changed

+40
-1
lines changed

toolkit/crashreporter/nsExceptionHandler.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ using mozilla::ipc::CrashReporterClient;
117117

118118
extern "C" {
119119
void install_rust_panic_hook();
120+
void install_rust_oom_hook();
120121
bool get_rust_panic_reason(char** reason, size_t* length);
121122
}
122123

@@ -1687,6 +1688,8 @@ nsresult SetExceptionHandler(nsIFile* aXREDirectory,
16871688

16881689
install_rust_panic_hook();
16891690

1691+
install_rust_oom_hook();
1692+
16901693
InitThreadAnnotation();
16911694

16921695
return NS_OK;

toolkit/library/gtest/rust/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ cubeb_pulse_rust = ["gkrust-shared/cubeb_pulse_rust"]
1414
gecko_debug = ["gkrust-shared/gecko_debug"]
1515
simd-accel = ["gkrust-shared/simd-accel"]
1616
oom_with_global_alloc = ["gkrust-shared/oom_with_global_alloc"]
17+
oom_with_hook = ["gkrust-shared/oom_with_hook"]
1718
moz_memory = ["gkrust-shared/moz_memory"]
1819

1920
[dependencies]

toolkit/library/rust/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ cubeb_pulse_rust = ["gkrust-shared/cubeb_pulse_rust"]
1414
gecko_debug = ["gkrust-shared/gecko_debug"]
1515
simd-accel = ["gkrust-shared/simd-accel"]
1616
oom_with_global_alloc = ["gkrust-shared/oom_with_global_alloc"]
17+
oom_with_hook = ["gkrust-shared/oom_with_hook"]
1718
moz_memory = ["gkrust-shared/moz_memory"]
1819

1920
[dependencies]

toolkit/library/rust/gkrust-features.mozbuild

+7
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@ if CONFIG['MOZ_MEMORY']:
3030
# A string test is not the best thing, but it works well enough here.
3131
if CONFIG['RUSTC_VERSION'] < "1.27":
3232
gkrust_features += ['oom_with_global_alloc']
33+
elif CONFIG['RUSTC_VERSION'] >= "1.28" and CONFIG['RUSTC_VERSION'] < "1.29":
34+
gkrust_features += ['oom_with_hook']
35+
elif not CONFIG['MOZ_AUTOMATION']:
36+
# We don't want builds on automation to unwillingly stop annotating OOM
37+
# crash reports from rust.
38+
error('Builds on automation must use a version of rust that supports OOM '
39+
'hooking')

toolkit/library/rust/shared/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ cubeb_pulse_rust = ["cubeb-sys", "cubeb-pulse"]
3838
gecko_debug = ["geckoservo/gecko_debug", "nsstring/gecko_debug"]
3939
simd-accel = ["encoding_c/simd-accel", "encoding_glue/simd-accel"]
4040
oom_with_global_alloc = []
41+
oom_with_hook = []
4142
moz_memory = ["mp4parse_capi/mp4parse_fallible"]
4243

4344
[lib]

toolkit/library/rust/shared/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ fn main() {
33

44

55

6-
#[cfg(feature = "oom_with_global_alloc")]
6+
#[cfg(any(feature = "oom_with_global_alloc", feature = "oom_with_hook"))]
77
println!("cargo:rustc-env=RUSTC_BOOTSTRAP=1");
88
}

toolkit/library/rust/shared/lib.rs

+26
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#![cfg_attr(feature = "oom_with_global_alloc",
66
feature(global_allocator, alloc, alloc_system, allocator_api))]
7+
#![cfg_attr(feature = "oom_with_hook", feature(oom_hook))]
78

89
#[cfg(feature="servo")]
910
extern crate geckoservo;
@@ -218,3 +219,28 @@ mod global_alloc {
218219
#[cfg(feature = "oom_with_global_alloc")]
219220
#[global_allocator]
220221
static HEAP: global_alloc::GeckoHeap = global_alloc::GeckoHeap;
222+
223+
#[cfg(feature = "oom_with_hook")]
224+
mod oom_hook {
225+
use std::alloc::{Layout, set_oom_hook};
226+
227+
extern "C" {
228+
fn GeckoHandleOOM(size: usize) -> !;
229+
}
230+
231+
pub fn hook(layout: Layout) {
232+
unsafe {
233+
GeckoHandleOOM(layout.size());
234+
}
235+
}
236+
237+
pub fn install() {
238+
set_oom_hook(hook);
239+
}
240+
}
241+
242+
#[no_mangle]
243+
pub extern "C" fn install_rust_oom_hook() {
244+
#[cfg(feature = "oom_with_hook")]
245+
oom_hook::install();
246+
}

0 commit comments

Comments
 (0)