Skip to content

Commit b2813fc

Browse files
committed
Add "override" Cargo feature flag
Signed-off-by: Mads Marquart <mads@marquart.dk>
1 parent bd94cbe commit b2813fc

File tree

10 files changed

+23
-26
lines changed

10 files changed

+23
-26
lines changed

ci/run.sh

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

34-
cargo test --target "${TARGET}" \
35-
--features unprefixed_malloc_on_supported_platforms
34+
cargo test --target "${TARGET}" --features override
3635
cargo test --target "${TARGET}" --no-default-features
3736
cargo test --target "${TARGET}" --no-default-features \
3837
--features background_threads_runtime_support
@@ -48,7 +47,7 @@ cargo test --target "${TARGET}" --release
4847
cargo test --target "${TARGET}" --manifest-path jemalloc-sys/Cargo.toml
4948
cargo test --target "${TARGET}" \
5049
--manifest-path jemalloc-sys/Cargo.toml \
51-
--features unprefixed_malloc_on_supported_platforms
50+
--features override
5251

5352
# FIXME: jemalloc-ctl fails in the following targets
5453
case "${TARGET}" in
@@ -78,13 +77,11 @@ cargo test --target "${TARGET}" \
7877
# ${CARGO_CMD} test --target "${TARGET}" --features alloc_trait
7978
# fi
8079

81-
# Test that unprefixed_malloc_on_supported_platforms works in dylibs.
80+
# Test that overriding works in dylibs.
8281
case "$TARGET" in
8382
"i686-unknown-linux-musl") ;;
8483
"x86_64-unknown-linux-musl") ;;
8584
*)
86-
cargo run --target "${TARGET}" \
87-
-p test-dylib \
88-
--features unprefixed_malloc_on_supported_platforms
85+
cargo run --target "${TARGET}" -p test-dylib --features override
8986
;;
9087
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 = [ "unprefixed_malloc_on_supported_platforms" ]
4950
disable_initial_exec_tls = []
5051
disable_cache_oblivious = []
5152

jemalloc-sys/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ 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`: override the system allocator, even outside Rust code.
80+
81+
This enables the `unprefixed_malloc_on_supported_platforms` feature, with the
82+
addition that it forces overriding the allocator even if `malloc` and `free`
83+
would not usually have been seen by the linker. It also overrides the
84+
allocator on Apple platforms.
85+
7986
Note that to use this, the `jemalloc-sys` crate must actually be visible to
8087
`rustc` (it is not enough to only declare it in `Cargo.toml`). This can be
8188
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").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: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -891,8 +891,8 @@ mod env;
891891

892892
pub use env::*;
893893

894-
// When using the `"unprefixed_malloc_on_supported_platforms"` feature flag,
895-
// the user wants us to globally override the system allocator.
894+
// When using the `"override"` feature flag, the user wants us to globally
895+
// override the system allocator.
896896
//
897897
// However, since we build `jemalloc` as a static library (an archive), the
898898
// linker may decide to not care about our overrides if it can't directly see
@@ -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", 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", 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", 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 = ["tikv-jemalloc-sys/override"]
5657
disable_initial_exec_tls = ["tikv-jemalloc-sys/disable_initial_exec_tls"]
5758
disable_cache_oblivious = ["tikv-jemalloc-sys/disable_cache_oblivious"]
5859

jemallocator/tests/background_thread_enabled.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! library was compiled with background thread run-time support.
33
#![cfg(feature = "background_threads_runtime_support")]
44
#![cfg(not(feature = "unprefixed_malloc_on_supported_platforms"))]
5+
#![cfg(not(feature = "override"))]
56
#![cfg(not(target_env = "musl"))]
67

78
use tikv_jemallocator::Jemalloc;

test-dylib/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ 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",
19-
]
17+
override = ["tikv-jemalloc-sys/override"]
2018

2119
[[bin]]
2220
name = "test-dylib"

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") {
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)