Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/testsuite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jobs:
- uses: actions/checkout@v2
- run: make tests
- run: make cpp_tests
- run: make preload_tests
testsuite-clang:
runs-on: ubuntu-latest
strategy:
Expand All @@ -20,6 +21,7 @@ jobs:
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-${{ matrix.version }} 100
- run: make tests
- run: make cpp_tests
- run: make preload_tests
testsuite-clang18-perf:
runs-on: ubuntu-latest
steps:
Expand All @@ -38,6 +40,7 @@ jobs:
- uses: actions/checkout@v4
- run: make tests CC=gcc CXX=g++
- run: make cpp_tests CC=gcc CXX=g++
- run: make preload_tests CC=gcc CXX=g++
testsuite-musl:
runs-on: ubuntu-latest
container:
Expand Down
35 changes: 33 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,17 @@ STDC=c11
LTO = -flto

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

UNAME := $(shell uname)
ifeq ($(UNAME), Darwin)
OS_FLAGS =
LIBNAME = libisoalloc.dylib
SOEXT = dylib
PRELOAD_LIB_FLAGS = -Wl,-install_name,@rpath/libpreload_exit_order.dylib
ifeq ($(MEMCPY_SANITY), -DMEMCPY_SANITY=1)
CFLAGS += -D_FORTIFY_SOURCE=0
endif
Expand Down Expand Up @@ -546,6 +552,31 @@ endif
@echo "Running system malloc Performance Test"
build/malloc_tests

## Preload tests. Builds the release library with the C and C++
## hooks, then builds programs that do not link it at all and runs
## them with the library inserted the way a user would: LD_PRELOAD
## on Linux, DYLD_INSERT_LIBRARIES on macOS. They check that every
## allocation entry point is taken over, that pointers from the
## system allocator are handled, that fork and threads are safe
## and that other libraries can allocate from their destructors.
## Pass ABORT_ON_UNOWNED_PTR=-DABORT_ON_UNOWNED_PTR=0 to run them
## against the less strict configuration too.
PRELOAD_TEST_CFLAGS = -O1 -Wall -pthread $(OS_FLAGS)
preload_tests: cpp_library
@echo "make preload_tests"
$(CC) $(PRELOAD_TEST_CFLAGS) tests/preload/interpose_check.c -o $(BUILD_DIR)/preload_interpose_check -ldl
$(CC) $(PRELOAD_TEST_CFLAGS) tests/preload/foreign_ptr.c -o $(BUILD_DIR)/preload_foreign_ptr -ldl
$(CC) $(PRELOAD_TEST_CFLAGS) tests/preload/fork_safety.c -o $(BUILD_DIR)/preload_fork_safety -ldl
$(CC) $(PRELOAD_TEST_CFLAGS) tests/preload/thread_churn.c -o $(BUILD_DIR)/preload_thread_churn -ldl
$(CXX) -std=$(STDCXX) $(PRELOAD_TEST_CFLAGS) tests/preload/cpp_hooks.cpp -o $(BUILD_DIR)/preload_cpp_hooks -ldl
$(CC) $(PRELOAD_TEST_CFLAGS) -fPIC -shared $(PRELOAD_LIB_FLAGS) tests/preload/exit_order_lib.c -o $(BUILD_DIR)/libpreload_exit_order.$(SOEXT)
$(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
bash utils/run_preload_tests.sh $(BUILD_DIR)/$(LIBNAME) "$(THREAD_SUPPORT)"

## The macOS specific name for the same thing. Interposing and the
## malloc zone only exist there, see malloc_hook.c
test_macos: preload_tests

## C++ Support - Build a debug version of the unit test
cpp_tests: clean cpp_library_debug
@echo "make cpp_tests"
Expand All @@ -556,10 +587,10 @@ install:
cp -pR build/$(LIBNAME) /usr/lib/

format:
clang-format-18 $(SRC_DIR)/*.* tests/*.* include/*.h -i
clang-format-18 $(SRC_DIR)/*.* tests/*.* tests/preload/*.* include/*.h -i

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

clean:
rm -rf build/* tests_perf_analysis.txt big_tests_perf_analysis.txt gmon.out \
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ The Makefile targets are very simple:

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

`make library_less_strict` - Builds a release library with `ABORT_ON_UNOWNED_PTR=0`. Recommended when using IsoAlloc via `LD_PRELOAD`.
`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`.

`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).

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

Expand Down Expand Up @@ -143,6 +145,22 @@ or

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.

## Using IsoAlloc As The System Allocator

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.

On Linux use `LD_PRELOAD=/path/to/libisoalloc.so <program>`. The hooks are linker aliases for the `iso_` functions.

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:

* 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.
* 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.
* 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.

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.

`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.

## Debugging

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`.
Expand Down
13 changes: 13 additions & 0 deletions include/iso_alloc_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,22 @@ INTERNAL_HIDDEN uint8_t _iso_alloc_get_mem_tag(void *p, iso_alloc_zone_t *zone);
INTERNAL_HIDDEN void _iso_alloc_verify_tag(void *p, iso_alloc_zone_t *zone);
INTERNAL_HIDDEN size_t _iso_alloc_print_stats(void);
INTERNAL_HIDDEN size_t _iso_chunk_size(void *p);
INTERNAL_HIDDEN bool _iso_alloc_chunk_lookup(void *p, size_t *size);
INTERNAL_HIDDEN int64_t check_canary_no_abort(iso_alloc_zone_t *zone, const void *p);
INTERNAL_HIDDEN void _iso_alloc_initialize(void);
INTERNAL_HIDDEN void _iso_alloc_destroy(void);
#if THREAD_SUPPORT
INTERNAL_HIDDEN void _iso_alloc_fork_prepare(void);
INTERNAL_HIDDEN void _iso_alloc_fork_parent(void);
INTERNAL_HIDDEN void _iso_alloc_fork_child(void);
#endif
#if __APPLE__ && MALLOC_HOOK
/* The macOS hook layer, see malloc_hook.c */
INTERNAL_HIDDEN void _iso_alloc_darwin_zone_init(void);
INTERNAL_HIDDEN void _iso_alloc_darwin_free(void *p);
INTERNAL_HIDDEN void _iso_alloc_darwin_free_size(void *p, size_t size);
INTERNAL_HIDDEN void *_iso_alloc_darwin_realloc(void *p, size_t size);
#endif
#ifdef __cplusplus
}
#endif
Expand Down
10 changes: 0 additions & 10 deletions include/os/macos.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,3 @@
#define bswap_32(x) OSSwapInt32(x)
#define bswap_64(x) OSSwapInt64(x)
#define ENVIRON NULL

/*
* On pthread_exit, it expects to be dealing with libmalloc
* rather than isoalloc at releasing time
*/
__attribute__((weak)) void _pthread_tsd_cleanup(pthread_t);
void _pthread_tsd_cleanup(pthread_t p)
{
(void)p;
}
Loading
Loading