Skip to content

Commit 82b4a4e

Browse files
committed
Add "override_allocator_on_supported_platforms" feature flag
Signed-off-by: Mads Marquart <[email protected]>
1 parent bd94cbe commit 82b4a4e

File tree

9 files changed

+23
-21
lines changed

9 files changed

+23
-21
lines changed

ci/run.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ cargo test --target "${TARGET}" --features stats
3232
cargo test --target "${TARGET}" --features 'debug profiling'
3333

3434
cargo test --target "${TARGET}" \
35-
--features unprefixed_malloc_on_supported_platforms
35+
--features override_allocator_on_supported_platforms
3636
cargo test --target "${TARGET}" --no-default-features
3737
cargo test --target "${TARGET}" --no-default-features \
3838
--features background_threads_runtime_support
@@ -48,7 +48,7 @@ cargo test --target "${TARGET}" --release
4848
cargo test --target "${TARGET}" --manifest-path jemalloc-sys/Cargo.toml
4949
cargo test --target "${TARGET}" \
5050
--manifest-path jemalloc-sys/Cargo.toml \
51-
--features unprefixed_malloc_on_supported_platforms
51+
--features override_allocator_on_supported_platforms
5252

5353
# FIXME: jemalloc-ctl fails in the following targets
5454
case "${TARGET}" in
@@ -78,13 +78,13 @@ cargo test --target "${TARGET}" \
7878
# ${CARGO_CMD} test --target "${TARGET}" --features alloc_trait
7979
# fi
8080

81-
# Test that unprefixed_malloc_on_supported_platforms works in dylibs.
81+
# Test that overriding works in dylibs.
8282
case "$TARGET" in
8383
"i686-unknown-linux-musl") ;;
8484
"x86_64-unknown-linux-musl") ;;
8585
*)
8686
cargo run --target "${TARGET}" \
8787
-p test-dylib \
88-
--features unprefixed_malloc_on_supported_platforms
88+
--features override_allocator_on_supported_platforms
8989
;;
9090
esac

jemalloc-sys/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ background_threads_runtime_support = []
4646
background_threads = [ "background_threads_runtime_support" ]
4747
stats = []
4848
unprefixed_malloc_on_supported_platforms = []
49+
override_allocator_on_supported_platforms = [ "unprefixed_malloc_on_supported_platforms" ]
4950
disable_initial_exec_tls = []
5051
disable_cache_oblivious = []
5152

jemalloc-sys/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ This crate provides following cargo feature flags:
7676
program to use `jemalloc` as well. On some platforms prefixes are always used
7777
because unprefixing is known to cause segfaults due to allocator mismatches.
7878

79+
* `override_allocator_on_supported_platforms`: override the system allocator,
80+
even outside Rust code.
81+
82+
This enables the `unprefixed_malloc_on_supported_platforms` feature, with the
83+
addition that it forces overriding the allocator even if `malloc` and `free`
84+
would not usually have been seen by the linker. It also overrides the
85+
allocator on Apple platforms.
86+
7987
Note that to use this, the `jemalloc-sys` crate must actually be visible to
8088
`rustc` (it is not enough to only declare it in `Cargo.toml`). This can be
8189
done by adding:

jemalloc-sys/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ fn main() {
114114
// Apple targets don't support unprefixed, but they do support
115115
// overriding (if you do the `zone_register` trick), so no need to
116116
// warn there.
117-
if !target.contains("apple") {
117+
let override_ = env::var("CARGO_FEATURE_OVERRIDE_ALLOCATOR_ON_SUPPORTED_PLATFORMS").is_ok();
118+
if !target.contains("apple") || !override_ {
118119
warning!(
119120
"Unprefixed `malloc` requested on unsupported platform `{}` => using prefixed `malloc`",
120121
target

jemalloc-sys/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ mod env;
891891

892892
pub use env::*;
893893

894-
// When using the `"unprefixed_malloc_on_supported_platforms"` feature flag,
894+
// When using the `"override_allocator_on_supported_platforms"` feature flag,
895895
// the user wants us to globally override the system allocator.
896896
//
897897
// However, since we build `jemalloc` as a static library (an archive), the
@@ -930,7 +930,7 @@ pub use env::*;
930930
// how this works:
931931
// <https://github.com/rust-lang/rust/pull/95604>
932932

933-
#[cfg(not(prefixed))]
933+
#[cfg(all(feature = "override_allocator_on_supported_platforms", not(target_vendor = "apple")))]
934934
mod set_up_statics {
935935
use super::*;
936936

@@ -957,10 +957,7 @@ mod set_up_statics {
957957
// as "used" when defined in an archive member in a static library, so we need
958958
// to explicitly reference the function via. Rust's `#[used]`.
959959

960-
#[cfg(all(
961-
feature = "unprefixed_malloc_on_supported_platforms",
962-
target_vendor = "apple"
963-
))]
960+
#[cfg(all(feature = "override_allocator_on_supported_platforms", target_vendor = "apple"))]
964961
#[used]
965962
static USED_ZONE_REGISTER: unsafe extern "C" fn() = {
966963
extern "C" {

jemalloc-sys/tests/unprefixed_malloc.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@ fn malloc_is_overridden() {
1010
assert_eq!(tikv_jemalloc_sys::malloc as usize, libc::malloc as usize)
1111
}
1212

13-
#[cfg(any(
14-
not(prefixed),
15-
all(
16-
feature = "unprefixed_malloc_on_supported_platforms",
17-
target_vendor = "apple"
18-
),
19-
))]
13+
#[cfg(any(not(prefixed), all(feature = "override_allocator_on_supported_platforms", target_vendor = "apple"),))]
2014
#[test]
2115
fn malloc_and_libc_are_interoperable_when_overridden() {
2216
let ptr = unsafe { tikv_jemalloc_sys::malloc(42) };

jemallocator/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ stats = ["tikv-jemalloc-sys/stats"]
5353
background_threads_runtime_support = ["tikv-jemalloc-sys/background_threads_runtime_support"]
5454
background_threads = ["tikv-jemalloc-sys/background_threads"]
5555
unprefixed_malloc_on_supported_platforms = ["tikv-jemalloc-sys/unprefixed_malloc_on_supported_platforms"]
56+
override_allocator_on_supported_platforms = ["tikv-jemalloc-sys/override_allocator_on_supported_platforms"]
5657
disable_initial_exec_tls = ["tikv-jemalloc-sys/disable_initial_exec_tls"]
5758
disable_cache_oblivious = ["tikv-jemalloc-sys/disable_cache_oblivious"]
5859

test-dylib/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ tikv-jemalloc-sys = { path = "../jemalloc-sys" }
1414
cc = "^1.0.13"
1515

1616
[features]
17-
unprefixed_malloc_on_supported_platforms = [
18-
"tikv-jemalloc-sys/unprefixed_malloc_on_supported_platforms",
17+
override_allocator_on_supported_platforms = [
18+
"tikv-jemalloc-sys/override_allocator_on_supported_platforms",
1919
]
2020

2121
[[bin]]

test-dylib/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn main() {
3232

3333
// If overidden, test that the same is true for `tikv_jemalloc_sys`'
3434
// symbols being interoperable with `free`.
35-
if cfg!(feature = "unprefixed_malloc_on_supported_platforms") {
35+
if cfg!(feature = "override_allocator_on_supported_platforms") {
3636
let ptr = unsafe { tikv_jemalloc_sys::malloc(10) };
3737
unsafe { dep_free(ptr) };
3838
let ptr = unsafe { tikv_jemalloc_sys::malloc(10) };

0 commit comments

Comments
 (0)