Skip to content

Commit d93ca6e

Browse files
committed
Auto merge of #3259 - RalfJung:jemalloc, r=RalfJung
use jemalloc as global allocator Hopefully fixes rust-lang/miri#3255
2 parents 0814a56 + c4a11ea commit d93ca6e

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/tools/miri/Cargo.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,16 @@ version = "1.0.9"
417417
source = "registry+https://github.com/rust-lang/crates.io-index"
418418
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
419419

420+
[[package]]
421+
name = "jemalloc-sys"
422+
version = "0.5.4+5.3.0-patched"
423+
source = "registry+https://github.com/rust-lang/crates.io-index"
424+
checksum = "ac6c1946e1cea1788cbfde01c993b52a10e2da07f4bac608228d1bed20bfebf2"
425+
dependencies = [
426+
"cc",
427+
"libc",
428+
]
429+
420430
[[package]]
421431
name = "lazy_static"
422432
version = "1.4.0"
@@ -533,6 +543,7 @@ dependencies = [
533543
"ctrlc",
534544
"env_logger",
535545
"getrandom",
546+
"jemalloc-sys",
536547
"lazy_static",
537548
"libc",
538549
"libffi",

src/tools/miri/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ log = "0.4"
2424
rand = "0.8"
2525
smallvec = "1.7"
2626
aes = { version = "0.8.3", features = ["hazmat"] }
27-
2827
measureme = "10.0.0"
2928
ctrlc = "3.2.5"
3029

30+
# Copied from `compiler/rustc/Cargo.toml`.
31+
# But only for Unix, it fails on Windows.
32+
[target.'cfg(unix)'.dependencies.jemalloc-sys]
33+
version = "0.5.0"
34+
features = ['unprefixed_malloc_on_supported_platforms']
35+
3136
[target.'cfg(unix)'.dependencies]
3237
libc = "0.2"
3338

src/tools/miri/src/bin/miri.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,51 @@ fn run_compiler(
293293
}
294294

295295
/// Parses a comma separated list of `T` from the given string:
296-
///
297296
/// `<value1>,<value2>,<value3>,...`
298297
fn parse_comma_list<T: FromStr>(input: &str) -> Result<Vec<T>, T::Err> {
299298
input.split(',').map(str::parse::<T>).collect()
300299
}
301300

301+
#[cfg(unix)]
302+
fn jemalloc_magic() {
303+
// These magic runes are copied from
304+
// <https://github.com/rust-lang/rust/blob/e89bd9428f621545c979c0ec686addc6563a394e/compiler/rustc/src/main.rs#L39>.
305+
// See there for further comments.
306+
use std::os::raw::{c_int, c_void};
307+
308+
#[used]
309+
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
310+
#[used]
311+
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
312+
jemalloc_sys::posix_memalign;
313+
#[used]
314+
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
315+
#[used]
316+
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
317+
#[used]
318+
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
319+
#[used]
320+
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
321+
322+
// On OSX, jemalloc doesn't directly override malloc/free, but instead
323+
// registers itself with the allocator's zone APIs in a ctor. However,
324+
// the linker doesn't seem to consider ctors as "used" when statically
325+
// linking, so we need to explicitly depend on the function.
326+
#[cfg(target_os = "macos")]
327+
{
328+
extern "C" {
329+
fn _rjem_je_zone_register();
330+
}
331+
332+
#[used]
333+
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
334+
}
335+
}
336+
302337
fn main() {
338+
#[cfg(unix)]
339+
jemalloc_magic();
340+
303341
let early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
304342

305343
// Snapshot a copy of the environment before `rustc` starts messing with it.

0 commit comments

Comments
 (0)