Skip to content

Make LD_PRELOAD and DYLD_INSERT_LIBRARIES work on macOS - #246

Merged
struct merged 2 commits into
masterfrom
preload_fixes_nov_2026
Nov 29, 2026
Merged

struct merged 2 commits into
masterfrom
preload_fixes_nov_2026

Conversation

@struct

@struct struct commented Nov 29, 2026

Copy link
Copy Markdown
Owner

Fixes some longstanding issues with using IsoAlloc with LD_PRELOAD or DYLD_INTERPOSE / DYLD_INSERT_LIBRARIES

macOS: interpose malloc, free, calloc, realloc, reallocf, valloc, posix_memalign, aligned_alloc, malloc_size, malloc_good_size, the typed malloc entry points and the libc++ operator new and delete symbols through a __DATA,__interpose table, register an IsoAlloc malloc zone as the default zone, and forward pointers that belong to libSystem back to their zone instead of aborting or losing them.

Both: initialize every mutex and the one time state on the lazy init path (libSystem calls malloc before our constructor), add fork handlers that take every lock in prepare, keep big zones mapped at exit unless ISO_DTOR_CLEANUP, hook valloc and pvalloc, fix the nothrow new and delete signatures, guard the free paths against a NULL root, and never abort from the size lookup used by realloc on foreign pointers.

Tests: tests/preload with make preload_tests (alias make test_macos) runs six programs under the real preload mechanism of each OS, in CI for the macOS, clang and gcc jobs.

struct and others added 2 commits November 29, 2026 11:11
macOS: interpose malloc, free, calloc, realloc, reallocf, valloc,
posix_memalign, aligned_alloc, malloc_size, malloc_good_size, the
typed malloc entry points and the libc++ operator new and delete
symbols through a __DATA,__interpose table, register an IsoAlloc
malloc zone as the default zone, and forward pointers that belong
to libSystem back to their zone instead of aborting or losing them.

Both: initialize every mutex and the one time state on the lazy
init path (libSystem calls malloc before our constructor), add
fork handlers that take every lock in prepare, keep big zones
mapped at exit unless ISO_DTOR_CLEANUP, hook valloc and pvalloc,
fix the nothrow new and delete signatures, guard the free paths
against a NULL root, and never abort from the size lookup used
by realloc on foreign pointers.

Tests: tests/preload with make preload_tests (alias make test_macos)
runs six programs under the real preload mechanism of each OS, in
CI for the macOS, clang and gcc jobs.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
ubuntu-latest now resolves to 26.04, which no longer packages
clang-15 or clang-16. The clang matrix moves to 17 through 21 and
refreshes the apt lists first. The clang-format-18 build there
(18.1.8) and the one on 24.04 (18.1.3) format a braced list inside
a macro differently, so the C++ interpose table builds its entries
from a cast macro instead. preload_test.h only defines _GNU_SOURCE
when the compiler has not, newer clang predefines it for C++.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@struct
struct merged commit a0331c5 into master Nov 29, 2026
20 checks passed
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.

1 participant