Skip to content

Commit af89d04

Browse files
committed
Enable dyld interposing on Apple platforms when overriding
Enables the MI_OSX_ZONE and MI_OSX_INTERPOSE flags. These allow using `malloc`/`free` on pointers returned from dynamic libraries (which will otherwise fail, as the dynamic libraries use the System allocator). These are enabled by default in mimalloc's CMakeLists.txt, but since we build without that, we need to specify these flags ourselves.
1 parent ddb551f commit af89d04

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

libmimalloc-sys/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn main() {
1515

1616
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("target_os not defined!");
1717
let target_family = env::var("CARGO_CFG_TARGET_FAMILY").expect("target_family not defined!");
18+
let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").expect("target_vendor not defined!");
1819
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("target_arch not defined!");
1920

2021
if target_family != "windows" {
@@ -27,6 +28,10 @@ fn main() {
2728
if target_family != "windows" {
2829
build.define("MI_MALLOC_OVERRIDE", None);
2930
}
31+
if target_vendor == "apple" {
32+
build.define("MI_OSX_ZONE", Some("1"));
33+
build.define("MI_OSX_INTERPOSE", Some("1"));
34+
}
3035
}
3136

3237
if env::var_os("CARGO_FEATURE_SECURE").is_some() {

test-override-with-dylib/src/main.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,15 @@ fn main() {
4949
// Extra check that the symbol was actually from the same place.
5050
let dep = unsafe { CStr::from_ptr(dep_lookup_malloc_address()) };
5151
let here = unsafe { CStr::from_ptr(lookup_malloc_address()) };
52-
assert_eq!(dep, here);
52+
53+
if cfg!(target_vendor = "apple") {
54+
// macOS / Mach-O symbols are not overriden in dependencies, they are
55+
// hooked into with `zone_register`.
56+
assert_eq!(
57+
dep.to_str().unwrap(),
58+
"/usr/lib/system/libsystem_malloc.dylib"
59+
);
60+
} else {
61+
assert_eq!(dep, here);
62+
}
5363
}

0 commit comments

Comments
 (0)