Skip to content

Conversation

madsmtm
Copy link
Contributor

@madsmtm madsmtm commented Sep 16, 2025

Define the MI_OSX_ZONE=1 and MI_OSX_INTERPOSE=1 when compiling. These allow using e.g. libc::free on pointers returned from dynamic libraries (which will otherwise fail, as the dynamic library would use the system allocator, while the current library would use mimalloc).

These are enabled by default in mimalloc's CMakeLists.txt, but since we build without that, we need to specify these flags ourselves.

Implementation-wise, I added a test in the first commit that fails like the following, and which is fixed in the second commit.

$ cargo run -ptest-override-with-dylib --features override
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.05s
     Running `target/debug/test-override-with-dylib`
test-override-with-dylib(15544,0x1f9c220c0) malloc: *** error for object 0x53d34040030: pointer being freed was not allocated
test-override-with-dylib(15544,0x1f9c220c0) malloc: *** set a breakpoint in malloc_error_break to debug
zsh: abort      cargo run -ptest-override-with-dylib --features override

Related: #41

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.
@madsmtm
Copy link
Contributor Author

madsmtm commented Sep 16, 2025

We might need something similar to tikv/jemallocator#109 to truly make the override feature work (since it needs a bit of help from the linker, which we can request with #[used] in rustc). But this PR should at least get us closer to working properly on macOS.

@madsmtm
Copy link
Contributor Author

madsmtm commented Sep 16, 2025

Actually, I went ahead and did that, see #146.

@octavonce
Copy link
Collaborator

Great stuff 💯

@octavonce octavonce merged commit 2586fdc into purpleprotocol:master Sep 17, 2025
5 checks passed
@madsmtm madsmtm deleted the override branch September 17, 2025 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants