Skip to content

Commit b8209e2

Browse files
committed
use jemalloc as global allocator
1 parent 0814a56 commit b8209e2

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ 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+
[dependencies.jemalloc-sys]
32+
version = "0.5.0"
33+
features = ['unprefixed_malloc_on_supported_platforms']
34+
3135
[target.'cfg(unix)'.dependencies]
3236
libc = "0.2"
3337

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

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

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

0 commit comments

Comments
 (0)