Skip to content

Commit a275e8e

Browse files
authored
Rollup merge of #146627 - madsmtm:jemalloc-simplify, r=jdonszelmann
Simplify `jemalloc` setup In the past, `#[used]` had to appear in the top-level crate to have a consistent effect on the linker. This has been fixed a while ago for ELF with the introduction of the `symbols.o` file in rust-lang/rust#95604, and more recently for Mach-O in rust-lang/rust#133832, which means that libraries can now implement the required workarounds themselves. This allows moving these `#[used]` declarations out of our `main.rs`. Specifically, I have moved them into `tikv-jemalloc-sys` where they belong in tikv/jemallocator#109 and done the same for `mimalloc` in purpleprotocol/mimalloc_rust#146 (in case we want to experiment with switching to that one day). Test with: ```sh ./x build library src/tools/rustdoc src/tools/clippy --set rust.jemalloc=true # macOS lldb -- ./build/host/stage1/bin/rustc -vV (lldb) b _rjem_je_zone_register (lldb) run # Should breakpoint, this means that the allocator was properly linked # Linux lldb -- ./build/host/stage1/bin/rustc -vV (lldb) b malloc (lldb) run # Should breakpoint, inspect that the `malloc` symbol comes from the `rustc` binary and not from `libc` ``` try-job: `aarch64-gnu` try-job: `dist-aarch64-linux` try-job: `dist-x86_64-musl` try-job: `dist-x86_64-apple` try-job: `dist-aarch64-apple`
2 parents 3264b55 + 0532364 commit a275e8e

File tree

2 files changed

+7
-43
lines changed

2 files changed

+7
-43
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ serde_json = { version = "1.0", optional = true }
3333
# But only for some targets, it fails for others. Rustc configures this in its CI, but we can't
3434
# easily use that since we support of-tree builds.
3535
[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies.tikv-jemalloc-sys]
36-
version = "0.6.0"
37-
features = ['unprefixed_malloc_on_supported_platforms']
36+
version = "0.6.1"
37+
features = ['override_allocator_on_supported_platforms']
3838

3939
[target.'cfg(unix)'.dependencies]
4040
libc = "0.2"

src/bin/miri.rs

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ extern crate rustc_middle;
2020
extern crate rustc_session;
2121
extern crate rustc_span;
2222

23+
/// See docs in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc/src/main.rs
24+
/// and https://github.com/rust-lang/rust/pull/146627 for why we need this `use` statement.
25+
#[cfg(any(target_os = "linux", target_os = "macos"))]
26+
use tikv_jemalloc_sys as _;
27+
2328
mod log;
2429

2530
use std::env;
@@ -392,48 +397,7 @@ fn parse_range(val: &str) -> Result<Range<u32>, &'static str> {
392397
Ok(from..to)
393398
}
394399

395-
#[cfg(any(target_os = "linux", target_os = "macos"))]
396-
fn jemalloc_magic() {
397-
// These magic runes are copied from
398-
// <https://github.com/rust-lang/rust/blob/e89bd9428f621545c979c0ec686addc6563a394e/compiler/rustc/src/main.rs#L39>.
399-
// See there for further comments.
400-
use std::os::raw::{c_int, c_void};
401-
402-
use tikv_jemalloc_sys as jemalloc_sys;
403-
404-
#[used]
405-
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
406-
#[used]
407-
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
408-
jemalloc_sys::posix_memalign;
409-
#[used]
410-
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
411-
#[used]
412-
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
413-
#[used]
414-
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
415-
#[used]
416-
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
417-
418-
// On OSX, jemalloc doesn't directly override malloc/free, but instead
419-
// registers itself with the allocator's zone APIs in a ctor. However,
420-
// the linker doesn't seem to consider ctors as "used" when statically
421-
// linking, so we need to explicitly depend on the function.
422-
#[cfg(target_os = "macos")]
423-
{
424-
unsafe extern "C" {
425-
fn _rjem_je_zone_register();
426-
}
427-
428-
#[used]
429-
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
430-
}
431-
}
432-
433400
fn main() {
434-
#[cfg(any(target_os = "linux", target_os = "macos"))]
435-
jemalloc_magic();
436-
437401
let early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
438402

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

0 commit comments

Comments
 (0)