Skip to content

Commit a0331c5

Browse files
authored
Merge pull request #246 from struct/preload_fixes_nov_2026
Make LD_PRELOAD and DYLD_INSERT_LIBRARIES work on macOS
2 parents f6bd789 + b64f747 commit a0331c5

19 files changed

Lines changed: 1980 additions & 138 deletions

‎.github/workflows/testsuite.yml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ jobs:
77
- uses: actions/checkout@v2
88
- run: make tests
99
- run: make cpp_tests
10+
- run: make preload_tests
1011
testsuite-clang:
1112
runs-on: ubuntu-latest
1213
strategy:
1314
matrix:
14-
version: [15, 16, 17, 18]
15+
version: [17, 18, 19, 20, 21]
1516
steps:
1617
- uses: actions/checkout@v4
1718
- run: |
19+
sudo apt-get update
1820
sudo apt install clang-${{ matrix.version }}
1921
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-${{ matrix.version }} 100
2022
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-${{ matrix.version }} 100
2123
- run: make tests
2224
- run: make cpp_tests
25+
- run: make preload_tests
2326
testsuite-clang18-perf:
2427
runs-on: ubuntu-latest
2528
steps:
@@ -38,6 +41,7 @@ jobs:
3841
- uses: actions/checkout@v4
3942
- run: make tests CC=gcc CXX=g++
4043
- run: make cpp_tests CC=gcc CXX=g++
44+
- run: make preload_tests CC=gcc CXX=g++
4145
testsuite-musl:
4246
runs-on: ubuntu-latest
4347
container:

‎Makefile‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,17 @@ STDC=c11
262262
LTO = -flto
263263

264264
LIBNAME = libisoalloc.so
265+
SOEXT = so
266+
## Shared libraries built for the preload tests get an rpath
267+
## relative install name on macOS so dyld finds them from any cwd
268+
PRELOAD_LIB_FLAGS =
265269

266270
UNAME := $(shell uname)
267271
ifeq ($(UNAME), Darwin)
268272
OS_FLAGS =
269273
LIBNAME = libisoalloc.dylib
274+
SOEXT = dylib
275+
PRELOAD_LIB_FLAGS = -Wl,-install_name,@rpath/libpreload_exit_order.dylib
270276
ifeq ($(MEMCPY_SANITY), -DMEMCPY_SANITY=1)
271277
CFLAGS += -D_FORTIFY_SOURCE=0
272278
endif
@@ -546,6 +552,31 @@ endif
546552
@echo "Running system malloc Performance Test"
547553
build/malloc_tests
548554

555+
## Preload tests. Builds the release library with the C and C++
556+
## hooks, then builds programs that do not link it at all and runs
557+
## them with the library inserted the way a user would: LD_PRELOAD
558+
## on Linux, DYLD_INSERT_LIBRARIES on macOS. They check that every
559+
## allocation entry point is taken over, that pointers from the
560+
## system allocator are handled, that fork and threads are safe
561+
## and that other libraries can allocate from their destructors.
562+
## Pass ABORT_ON_UNOWNED_PTR=-DABORT_ON_UNOWNED_PTR=0 to run them
563+
## against the less strict configuration too.
564+
PRELOAD_TEST_CFLAGS = -O1 -Wall -pthread $(OS_FLAGS)
565+
preload_tests: cpp_library
566+
@echo "make preload_tests"
567+
$(CC) $(PRELOAD_TEST_CFLAGS) tests/preload/interpose_check.c -o $(BUILD_DIR)/preload_interpose_check -ldl
568+
$(CC) $(PRELOAD_TEST_CFLAGS) tests/preload/foreign_ptr.c -o $(BUILD_DIR)/preload_foreign_ptr -ldl
569+
$(CC) $(PRELOAD_TEST_CFLAGS) tests/preload/fork_safety.c -o $(BUILD_DIR)/preload_fork_safety -ldl
570+
$(CC) $(PRELOAD_TEST_CFLAGS) tests/preload/thread_churn.c -o $(BUILD_DIR)/preload_thread_churn -ldl
571+
$(CXX) -std=$(STDCXX) $(PRELOAD_TEST_CFLAGS) tests/preload/cpp_hooks.cpp -o $(BUILD_DIR)/preload_cpp_hooks -ldl
572+
$(CC) $(PRELOAD_TEST_CFLAGS) -fPIC -shared $(PRELOAD_LIB_FLAGS) tests/preload/exit_order_lib.c -o $(BUILD_DIR)/libpreload_exit_order.$(SOEXT)
573+
$(CC) $(PRELOAD_TEST_CFLAGS) tests/preload/exit_order_main.c -L$(BUILD_DIR) -lpreload_exit_order -Wl,-rpath,$(abspath $(BUILD_DIR)) -o $(BUILD_DIR)/preload_exit_order -ldl
574+
bash utils/run_preload_tests.sh $(BUILD_DIR)/$(LIBNAME) "$(THREAD_SUPPORT)"
575+
576+
## The macOS specific name for the same thing. Interposing and the
577+
## malloc zone only exist there, see malloc_hook.c
578+
test_macos: preload_tests
579+
549580
## C++ Support - Build a debug version of the unit test
550581
cpp_tests: clean cpp_library_debug
551582
@echo "make cpp_tests"
@@ -556,10 +587,10 @@ install:
556587
cp -pR build/$(LIBNAME) /usr/lib/
557588

558589
format:
559-
clang-format-18 $(SRC_DIR)/*.* tests/*.* include/*.h -i
590+
clang-format-18 $(SRC_DIR)/*.* tests/*.* tests/preload/*.* include/*.h -i
560591

561592
format-ci:
562-
clang-format-18 --Werror --dry-run $(SRC_DIR)/*.* tests/*.* include/*.h -i
593+
clang-format-18 --Werror --dry-run $(SRC_DIR)/*.* tests/*.* tests/preload/*.* include/*.h -i
563594

564595
clean:
565596
rm -rf build/* tests_perf_analysis.txt big_tests_perf_analysis.txt gmon.out \

‎README.md‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ The Makefile targets are very simple:
9898

9999
`make library` - Builds a release version of the library without C++ support
100100

101-
`make library_less_strict` - Builds a release library with `ABORT_ON_UNOWNED_PTR=0`. Recommended when using IsoAlloc via `LD_PRELOAD`.
101+
`make library_less_strict` - Builds a release library with `ABORT_ON_UNOWNED_PTR=0`. Recommended when using IsoAlloc via `LD_PRELOAD` or `DYLD_INSERT_LIBRARIES` with programs that hand pointers from other allocators to `free`.
102+
103+
`make preload_tests` - Builds the release library and runs programs that do not link it with the library preloaded, see [Using IsoAlloc As The System Allocator](#using-isoalloc-as-the-system-allocator).
102104

103105
`make library_debug` - Builds a debug version of the library
104106

@@ -143,6 +145,22 @@ or
143145

144146
If you want to use IsoAlloc with a C++ program you can use the `c_library_objects` Makefile target. This will produce .o object files you can pass to your compiler. These targets are used internally to build the library with `new` and `delete` support.
145147

148+
## Using IsoAlloc As The System Allocator
149+
150+
The library built by `make library` (or `make cpp_library` for `new` and `delete` as well) takes over `malloc`, `calloc`, `realloc`, `reallocarray`, `free`, `posix_memalign`, `aligned_alloc`, `memalign`, `valloc`, `pvalloc` and `malloc_usable_size` in any program it is loaded into, so it can be used with existing and closed source programs without recompiling them.
151+
152+
On Linux use `LD_PRELOAD=/path/to/libisoalloc.so <program>`. The hooks are linker aliases for the `iso_` functions.
153+
154+
On macOS use `DYLD_INSERT_LIBRARIES=/path/to/libisoalloc.dylib <program>`. Every image on macOS is linked with a two level namespace, so a dylib that merely defines `malloc` is never called, and `DYLD_FORCE_FLAT_NAMESPACE` no longer changes that. Instead the library carries a `__DATA,__interpose` table that makes dyld rebind every image's references to the libSystem allocator (`malloc`, `calloc`, `realloc`, `reallocf`, `free`, `posix_memalign`, `aligned_alloc`, `valloc`, `malloc_size`, `malloc_good_size`, the typed `malloc_type_*` entry points and the C++ operators) to IsoAlloc. It also registers a `malloc_zone_t` and makes it the default zone, so `malloc_zone_malloc(malloc_default_zone(), ...)`, `malloc_zone_from_ptr`, `malloc_zone_free` and the rest of the zone API recognize IsoAlloc chunks, and libSystem drives IsoAlloc's `fork()` handlers through it. The same interpose table applies when a program is linked against the dylib directly. Some things to know on macOS:
155+
156+
* System Integrity Protection strips `DYLD_INSERT_LIBRARIES` from platform binaries such as `/usr/bin/python3`, silently. The program runs with the system allocator. Use a binary you built or installed yourself, for example from Homebrew.
157+
* Applications with the hardened runtime need the `com.apple.security.cs.allow-dyld-environment-variables` entitlement, and ones with library validation only load dylibs signed by the same team.
158+
* libSystem allocates before IsoAlloc's constructor runs and frameworks such as CoreFoundation allocate from their own zones, so pointers from the system allocator reach `free`, `realloc` and `malloc_size`. IsoAlloc hands those back to the zone that owns them, and `realloc` migrates them into IsoAlloc. Only a pointer that no allocator owns is treated as an error, which `ABORT_ON_UNOWNED_PTR` controls as usual.
159+
160+
On both platforms IsoAlloc is usually initialized by the first allocation rather than by its constructor, and `fork()` is safe while other threads allocate: on Linux through `pthread_atfork`, on macOS through the malloc zone callbacks. The library's destructor leaves all memory mapped unless `ISO_DTOR_CLEANUP` is set, because destructors of other libraries run after it and still use their allocations.
161+
162+
`make preload_tests` (or `make test_macos`, the same target) builds the library and a set of programs that do not link it, then runs them with the library inserted. They check every entry point is taken over, that pointers from the system allocator work, that threads and `fork()` are safe, that C++ operators are replaced, and that another library can allocate from its destructor. Run it with `ABORT_ON_UNOWNED_PTR=-DABORT_ON_UNOWNED_PTR=0` to cover the less strict configuration as well. A library built with `THREAD_SUPPORT=-DTHREAD_SUPPORT=0` only runs the single threaded programs.
163+
146164
## Debugging
147165

148166
If you try to use Isolation Alloc in an existing program then and you are getting crashes here are some tips to help you get started. If you aren't using `LD_PRELOAD` then first make sure you actually replaced all `malloc, calloc, realloc` and `free` calls to their `iso_alloc` equivalents. Don't forget things like `strdup` that return a pointer from `malloc`.

‎include/iso_alloc_internal.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,22 @@ INTERNAL_HIDDEN uint8_t _iso_alloc_get_mem_tag(void *p, iso_alloc_zone_t *zone);
563563
INTERNAL_HIDDEN void _iso_alloc_verify_tag(void *p, iso_alloc_zone_t *zone);
564564
INTERNAL_HIDDEN size_t _iso_alloc_print_stats(void);
565565
INTERNAL_HIDDEN size_t _iso_chunk_size(void *p);
566+
INTERNAL_HIDDEN bool _iso_alloc_chunk_lookup(void *p, size_t *size);
566567
INTERNAL_HIDDEN int64_t check_canary_no_abort(iso_alloc_zone_t *zone, const void *p);
567568
INTERNAL_HIDDEN void _iso_alloc_initialize(void);
568569
INTERNAL_HIDDEN void _iso_alloc_destroy(void);
570+
#if THREAD_SUPPORT
571+
INTERNAL_HIDDEN void _iso_alloc_fork_prepare(void);
572+
INTERNAL_HIDDEN void _iso_alloc_fork_parent(void);
573+
INTERNAL_HIDDEN void _iso_alloc_fork_child(void);
574+
#endif
575+
#if __APPLE__ && MALLOC_HOOK
576+
/* The macOS hook layer, see malloc_hook.c */
577+
INTERNAL_HIDDEN void _iso_alloc_darwin_zone_init(void);
578+
INTERNAL_HIDDEN void _iso_alloc_darwin_free(void *p);
579+
INTERNAL_HIDDEN void _iso_alloc_darwin_free_size(void *p, size_t size);
580+
INTERNAL_HIDDEN void *_iso_alloc_darwin_realloc(void *p, size_t size);
581+
#endif
569582
#ifdef __cplusplus
570583
}
571584
#endif

‎include/os/macos.h‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,3 @@
99
#define bswap_32(x) OSSwapInt32(x)
1010
#define bswap_64(x) OSSwapInt64(x)
1111
#define ENVIRON NULL
12-
13-
/*
14-
* On pthread_exit, it expects to be dealing with libmalloc
15-
* rather than isoalloc at releasing time
16-
*/
17-
__attribute__((weak)) void _pthread_tsd_cleanup(pthread_t);
18-
void _pthread_tsd_cleanup(pthread_t p)
19-
{
20-
(void)p;
21-
}

0 commit comments

Comments
 (0)