Skip to content

Commit c21ea0b

Browse files
committed
Bug 1829780 - Adjust our OOM hook to the new API from rustc 1.71. r=emilio
The new API landed in rust-lang/rust#109507 Differential Revision: https://phabricator.services.mozilla.com/D176383
1 parent 3914571 commit c21ea0b

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

js/src/vm/Initialization.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ static void SetupCanonicalNaN() {
105105
if (!code) return #code " failed"; \
106106
} while (0)
107107

108-
extern "C" void install_rust_panic_hook();
109-
extern "C" void install_rust_oom_hook();
108+
extern "C" void install_rust_hooks();
110109

111110
JS_PUBLIC_API const char* JS::detail::InitWithFailureDiagnostic(
112111
bool isDebugBuild) {
@@ -127,8 +126,7 @@ JS_PUBLIC_API const char* JS::detail::InitWithFailureDiagnostic(
127126

128127
#ifdef JS_STANDALONE
129128
// The rust hooks are initialized by Gecko on non-standalone builds.
130-
install_rust_panic_hook();
131-
install_rust_oom_hook();
129+
install_rust_hooks();
132130
#endif
133131

134132
PRMJ_NowInit();

mozglue/static/rust/build.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ fn main() {
1616
println!("cargo:rerun-if-changed=wrappers.cpp");
1717

1818
let ver = version().unwrap();
19-
let max_oom_hook_version = Version::parse("1.70.0-alpha").unwrap();
19+
let max_oom_hook_version = Version::parse("1.71.0-alpha").unwrap();
20+
let max_alloc_error_panic_version = Version::parse("1.72.0-alpha").unwrap();
2021

2122
if ver < max_oom_hook_version {
2223
println!("cargo:rustc-cfg=feature=\"oom_with_hook\"");
24+
} else if ver < max_alloc_error_panic_version {
25+
println!("cargo:rustc-cfg=feature=\"oom_with_alloc_error_panic\"");
2326
} else if std::env::var("MOZ_AUTOMATION").is_ok() {
2427
panic!("Builds on automation must use a version of rust for which we know how to hook OOM: want < {}, have {}",
25-
max_oom_hook_version, ver);
28+
max_alloc_error_panic_version, ver);
2629
}
2730
}

mozglue/static/rust/lib.rs

+27-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
#![cfg_attr(feature = "oom_with_hook", feature(alloc_error_hook))]
6+
#![cfg_attr(feature = "oom_with_alloc_error_panic", feature(panic_oom_payload))]
67

78
use arrayvec::ArrayString;
89
use std::cmp;
@@ -68,7 +69,11 @@ impl<const CAP: usize> Deref for ArrayCString<CAP> {
6869
fn panic_hook(info: &panic::PanicInfo) {
6970
// Try to handle &str/String payloads, which should handle 99% of cases.
7071
let payload = info.payload();
71-
let message = if let Some(s) = payload.downcast_ref::<&str>() {
72+
let message = if let Some(layout) = oom_hook::oom_layout(payload) {
73+
unsafe {
74+
oom_hook::RustHandleOOM(layout.size());
75+
}
76+
} else if let Some(s) = payload.downcast_ref::<&str>() {
7277
s
7378
} else if let Some(s) = payload.downcast_ref::<String>() {
7479
s.as_str()
@@ -98,33 +103,40 @@ fn panic_hook(info: &panic::PanicInfo) {
98103

99104
/// Configure a panic hook to redirect rust panics to MFBT's MOZ_Crash.
100105
#[no_mangle]
101-
pub extern "C" fn install_rust_panic_hook() {
106+
pub extern "C" fn install_rust_hooks() {
102107
panic::set_hook(Box::new(panic_hook));
108+
#[cfg(feature = "oom_with_hook")]
109+
use std::alloc::set_alloc_error_hook;
110+
#[cfg(feature = "oom_with_hook")]
111+
set_alloc_error_hook(oom_hook::hook);
103112
}
104113

105-
#[cfg(feature = "oom_with_hook")]
106114
mod oom_hook {
107-
use std::alloc::{set_alloc_error_hook, Layout};
115+
#[cfg(feature = "oom_with_alloc_error_panic")]
116+
use std::alloc::AllocErrorPanicPayload;
117+
use std::alloc::Layout;
118+
use std::any::Any;
119+
120+
#[inline(always)]
121+
pub fn oom_layout(_payload: &dyn Any) -> Option<Layout> {
122+
#[cfg(feature = "oom_with_alloc_error_panic")]
123+
return _payload
124+
.downcast_ref::<AllocErrorPanicPayload>()
125+
.map(|p| p.layout());
126+
#[cfg(not(feature = "oom_with_alloc_error_panic"))]
127+
return None;
128+
}
108129

109130
extern "C" {
110-
fn RustHandleOOM(size: usize) -> !;
131+
pub fn RustHandleOOM(size: usize) -> !;
111132
}
112133

134+
#[cfg(feature = "oom_with_hook")]
113135
pub fn hook(layout: Layout) {
114136
unsafe {
115137
RustHandleOOM(layout.size());
116138
}
117139
}
118-
119-
pub fn install() {
120-
set_alloc_error_hook(hook);
121-
}
122-
}
123-
124-
#[no_mangle]
125-
pub extern "C" fn install_rust_oom_hook() {
126-
#[cfg(feature = "oom_with_hook")]
127-
oom_hook::install();
128140
}
129141

130142
#[cfg(feature = "moz_memory")]

toolkit/xre/nsAppRunner.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -6100,14 +6100,10 @@ mozilla::BinPathType XRE_GetChildProcBinPathType(
61006100
}
61016101

61026102
// From mozglue/static/rust/lib.rs
6103-
extern "C" void install_rust_panic_hook();
6104-
extern "C" void install_rust_oom_hook();
6103+
extern "C" void install_rust_hooks();
61056104

61066105
struct InstallRustHooks {
6107-
InstallRustHooks() {
6108-
install_rust_panic_hook();
6109-
install_rust_oom_hook();
6110-
}
6106+
InstallRustHooks() { install_rust_hooks(); }
61116107
};
61126108

61136109
InstallRustHooks sInstallRustHooks;

0 commit comments

Comments
 (0)