Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make unprefixed consistently override the system allocator #109

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions jemalloc-sys/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ This crate provides following cargo feature flags:
* `stats` (configure `jemalloc` with `--enable-stats`): Enable statistics
gathering functionality. See the `jemalloc`'s "`opt.stats_print`" option
documentation for usage details.

* `debug` (configure `jemalloc` with `--enable-debug`): Enable assertions and
validation code. This incurs a substantial performance hit, but is very useful
during application development.

* `background_threads_runtime_support` (enabled by default): enables
background-threads run-time support when building `jemalloc-sys` on some POSIX
targets supported by `jemalloc`. Background threads are disabled at run-time
Expand All @@ -72,16 +72,36 @@ This crate provides following cargo feature flags:
* `unprefixed_malloc_on_supported_platforms`: when disabled, configure
`jemalloc` with `--with-jemalloc-prefix=_rjem_`. Enabling this causes symbols
like `malloc` to be emitted without a prefix, overriding the ones defined by
libc. This usually causes C and C++ code linked in the same program to use
`jemalloc` as well. On some platforms prefixes are always used because
unprefixing is known to cause segfaults due to allocator mismatches.

libc. This usually causes C, Objective-C and C++ code linked in the same
program to use `jemalloc` as well. On some platforms prefixes are always used
because unprefixing is known to cause segfaults due to allocator mismatches.

Note that to use this, the `jemalloc-sys` crate must actually be visible to
`rustc` (it is not enough to only declare it in `Cargo.toml`). This can be
done with:
```rust
use jemalloc_sys as _;
```

On macOS, you'll need to do a bit more to work around a bug in `rustc`:
```rust
// Workaround for /~https://github.com/rust-lang/rust/issues/133491
#[cfg(target_vendor = "apple")]
#[used]
static USED_ZONE_REGISTER: unsafe extern "C" fn() = {
extern "C" {
fn _rjem_je_zone_register();
}
_rjem_je_zone_register
};
```

* `disable_initial_exec_tls` (disabled by default): when enabled, jemalloc is
built with the `--disable-initial-exec-tls` option. It disables the
initial-exec TLS model for jemalloc's internal thread-local storage (on those
platforms that support explicit settings). This can allow jemalloc to be
built with the `--disable-initial-exec-tls` option. It disables the
initial-exec TLS model for jemalloc's internal thread-local storage (on those
platforms that support explicit settings). This can allow jemalloc to be
dynamically loaded after program startup (e.g. using dlopen). If you encounter
the error `yourlib.so: cannot allocate memory in static TLS block`, you'll
the error `yourlib.so: cannot allocate memory in static TLS block`, you'll
likely want to enable this.

* `disable_cache_oblivious` (disabled by default): when enabled, jemalloc is
Expand All @@ -104,7 +124,7 @@ hyphens `-` are replaced with underscores `_`(see
variable, the `/etc/malloc.conf` symlink, and the `MALLOC_CONF` environment
variable (note: this variable might be prefixed as `_RJEM_MALLOC_CONF`). For
example, to change the default decay time for dirty pages to 30 seconds:

```
JEMALLOC_SYS_WITH_MALLOC_CONF=dirty_decay_ms:30000
```
Expand All @@ -115,17 +135,17 @@ hyphens `-` are replaced with underscores `_`(see
allocator page size equal to the system page size, so this option need not be
specified unless the system page size may change between configuration and
execution, e.g. when cross compiling.

* `JEMALLOC_SYS_WITH_LG_HUGEPAGE=<lg-hugepage>`: Specify the base 2 log of the
system huge page size. This option is useful when cross compiling, or when
overriding the default for systems that do not explicitly support huge pages.


* `JEMALLOC_SYS_WITH_LG_QUANTUM=<lg-quantum>`: Specify the base 2 log of the
minimum allocation alignment. jemalloc needs to know the minimum alignment
that meets the following C standard requirement (quoted from the April 12,
2011 draft of the C11 standard):

> The pointer returned if the allocation succeeds is suitably aligned so that
> it may be assigned to a pointer to any type of object with a fundamental
> alignment requirement and then used to access such an object or an array of
Expand Down
13 changes: 9 additions & 4 deletions jemalloc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,15 @@ fn main() {
.iter()
.any(|i| target.contains(i))
{
warning!(
"Unprefixed `malloc` requested on unsupported platform `{}` => using prefixed `malloc`",
target
);
// Apple targets don't support unprefixed, but they do support
// overriding (if you do the `zone_register` trick), so no need to
// warn there.
if !target.contains("apple") {
warning!(
"Unprefixed `malloc` requested on unsupported platform `{}` => using prefixed `malloc`",
target
);
}
use_prefix = true;
}

Expand Down
2 changes: 1 addition & 1 deletion jemalloc-sys/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ pub static NO_BG_THREAD_TARGETS: &[&str] = &["musl"];
// /~https://github.com/rust-lang/rust/commit/e3b414d8612314e74e2b0ebde1ed5c6997d28e8d
// /~https://github.com/rust-lang/rust/commit/9f3de647326fbe50e0e283b9018ab7c41abccde3
// /~https://github.com/rust-lang/rust/commit/ed015456a114ae907a36af80c06f81ea93182a24
pub static NO_UNPREFIXED_MALLOC_TARGETS: &[&str] = &["android", "dragonfly", "darwin"];
pub static NO_UNPREFIXED_MALLOC_TARGETS: &[&str] = &["android", "dragonfly", "apple"];
55 changes: 55 additions & 0 deletions jemalloc-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,3 +890,58 @@ pub type extent_merge_t = unsafe extern "C" fn(
mod env;

pub use env::*;

// When using the `"unprefixed_malloc_on_supported_platforms"` feature flag to
// override the system allocator, make sure that all allocator functions are
// visible to the linker, such that it will override all of them.
//
// For example, this would fail horribly if one used a dynamic library that
// calls `malloc`, and then later `free`'d that in Rust code that used
// jemalloc-sys, since then the linker might think that only `free` from
// jemalloc is needed, and wouldn't override `malloc`.
#[cfg(not(prefixed))]
mod set_up_statics {
use super::*;

#[used]
static USED_MALLOC: unsafe extern "C" fn(usize) -> *mut c_void = malloc;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any references on how these statics are processed?

Copy link
Author

@madsmtm madsmtm Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's the reference on #[used], and the equivalent Clang attribute and GCC attribute.

But these are somewhat vague, perhaps intentionally so as this is very much a linker concept? I don't really have a good reference on linkers, the best I can do is reference this piece of source code in rustc that talks about a workaround for static libs, and the following section from the manual page for ld64:

A static library (aka static archive) is a collection of .o files with a table of contents that lists the global symbols in the .o files. ld will only pull .o files out of a static library if needed to resolve some symbol reference. Unlike traditional linkers, ld will continually search a static library while linking.

(Note that Rust .rlibs are internally archives / static libraries, and so the rules for static libaries apply to them as well).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or if you have more specific questions about how things work then I can try to answer them, to the best of my ability?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. How about adding a test to show it work as expected? You can add a dylib crate that allocs in the root directory and then add a test crate that links both the dylib and jemalloc-sys. If it works as expected the test shoud be able to use jemalloc's free to dealloc the pointer from dylib.

#[used]
static USED_CALLOC: unsafe extern "C" fn(usize, usize) -> *mut c_void = calloc;
#[used]
static USED_POSIX_MEMALIGN: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
posix_memalign;
#[used]
static USED_ALLOC: unsafe extern "C" fn(usize, usize) -> *mut c_void = aligned_alloc;
#[used]
static USED_REALLOC: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = realloc;
#[used]
static USED_FREE: unsafe extern "C" fn(*mut c_void) = free;
}

// On macOS, jemalloc doesn't directly override malloc/free, but instead
// registers itself with the allocator's zone APIs in a ctor. However, ld64
// doesn't consider ctors as "used" when defined in an object file / archive
// member in a static library, so we need to explicitly depend on the function
// using Rust's `#[used]`.
//
// NOTE: `#[used]` currently doesn't actually work on macOS, but that's an
// upstream rustc issue, see /~https://github.com/rust-lang/rust/issues/133491.
#[cfg(all(
feature = "unprefixed_malloc_on_supported_platforms",
target_vendor = "apple"
))]
#[used]
static USED_ZONE_REGISTER: unsafe extern "C" fn() = {
extern "C" {
#[cfg_attr(prefixed, link_name = "_rjem_je_zone_register")]
#[cfg_attr(not(prefixed), link_name = "je_zone_register")]
fn zone_register();
}

// NOTE: This internal function is marked as __attribute__((constructor)),
// and thus we neither can nor should call it ourselves.
//
// Instead, we simply need to reference it in a `#[used]` static to get
// the linker to see that it should be registering it as a constructor.
zone_register
};