From 61e4b83f3695271a8d771655de8d8896c8111f34 Mon Sep 17 00:00:00 2001 From: Jim Huang Date: Wed, 28 Aug 2019 06:44:40 +0800 Subject: [PATCH 1/3] Clarify LD_PRELOAD on dynamically-linked ELF LD_PRELOAD is an environment variable that can be set to load ELF shared objects before all others. Linux and BSD are known to fit. This change also unifies the use of command line, ensuring the prefix "> " prompt. --- readme.md | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/readme.md b/readme.md index c2efcc19..9352199d 100644 --- a/readme.md +++ b/readme.md @@ -13,7 +13,7 @@ Initially developed by Daan Leijen for the run-time systems of the [Koka](https://github.com/koka-lang/koka) and [Lean](https://github.com/leanprover/lean) languages. It is a drop-in replacement for `malloc` and can be used in other programs -without code changes, for example, on Unix you can use it as: +without code changes, for example, on dynamically linked ELF-based systems (Linux, BSD, etc.) you can use it as: ``` > LD_PRELOAD=/usr/bin/libmimalloc.so myprogram ``` @@ -117,7 +117,7 @@ Notes: The preferred usage is including ``, linking with the shared- or static library, and using the `mi_malloc` API exclusively for allocation. For example, ``` -gcc -o myprogram -lmimalloc myfile.c +> gcc -o myprogram -lmimalloc myfile.c ``` mimalloc uses only safe OS calls (`mmap` and `VirtualAlloc`) and can co-exist @@ -207,20 +207,21 @@ This is the recommended way to override the standard malloc interface. ### Linux, BSD -On these systems we preload the mimalloc shared +On these ELF-based systems we preload the mimalloc shared library so all calls to the standard `malloc` interface are resolved to the _mimalloc_ library. - -- `env LD_PRELOAD=/usr/lib/libmimalloc.so myprogram` +``` +> env LD_PRELOAD=/usr/lib/libmimalloc.so myprogram +``` You can set extra environment variables to check that mimalloc is running, like: ``` -env MIMALLOC_VERBOSE=1 LD_PRELOAD=/usr/lib/libmimalloc.so myprogram +> env MIMALLOC_VERBOSE=1 LD_PRELOAD=/usr/lib/libmimalloc.so myprogram ``` or run with the debug version to get detailed statistics: ``` -env MIMALLOC_SHOW_STATS=1 LD_PRELOAD=/usr/lib/libmimalloc-debug.so myprogram +> env MIMALLOC_SHOW_STATS=1 LD_PRELOAD=/usr/lib/libmimalloc-debug.so myprogram ``` ### MacOS @@ -228,8 +229,9 @@ env MIMALLOC_SHOW_STATS=1 LD_PRELOAD=/usr/lib/libmimalloc-debug.so myprogram On macOS we can also preload the mimalloc shared library so all calls to the standard `malloc` interface are resolved to the _mimalloc_ library. - -- `env DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=/usr/lib/libmimalloc.dylib myprogram` +``` +> env DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=/usr/lib/libmimalloc.dylib myprogram +``` Note that certain security restrictions may apply when doing this from the [shell](https://stackoverflow.com/questions/43941322/dyld-insert-libraries-ignored-when-calling-application-through-bash). @@ -257,16 +259,15 @@ robust; try this out if you experience troubles. ## Static override -On Unix systems, you can also statically link with _mimalloc_ to override the standard +On Unix-like systems, you can also statically link with _mimalloc_ to override the standard malloc interface. The recommended way is to link the final program with the _mimalloc_ single object file (`mimalloc-override.o`). We use an object file instead of a library file as linkers give preference to that over archives to resolve symbols. To ensure that the standard malloc interface resolves to the _mimalloc_ library, link it as the first object file. For example: - ``` -gcc -o myprogram mimalloc-override.o myfile1.c ... +> gcc -o myprogram mimalloc-override.o myfile1.c ... ``` From cbc392434c0fe482aebbe0163374122d94fcc4a8 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Wed, 28 Aug 2019 17:26:56 +0200 Subject: [PATCH 2/3] Add an option to disable building tests --- CMakeLists.txt | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d44b3408..0600ed5d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,12 +4,13 @@ include("cmake/mimalloc-config-version.cmake") set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 17) -option(MI_OVERRIDE "Override the standard malloc interface" ON) -option(MI_INTERPOSE "Use interpose to override standard malloc on macOS" ON) -option(MI_SEE_ASM "Generate assembly files" OFF) -option(MI_CHECK_FULL "Use full internal invariant checking in DEBUG mode" OFF) -option(MI_USE_CXX "Use the C++ compiler to compile the library" OFF) -option(MI_SECURE "Use security mitigations (like guard pages and randomization)" OFF) +option(MI_OVERRIDE "Override the standard malloc interface" ON) +option(MI_INTERPOSE "Use interpose to override standard malloc on macOS" ON) +option(MI_SEE_ASM "Generate assembly files" OFF) +option(MI_CHECK_FULL "Use full internal invariant checking in DEBUG mode" OFF) +option(MI_USE_CXX "Use the C++ compiler to compile the library" OFF) +option(MI_SECURE "Use security mitigations (like guard pages and randomization)" OFF) +option(MI_BUILD_TESTS "Build test executables" ON) set(mi_install_dir "lib/mimalloc-${mi_version}") @@ -179,21 +180,24 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/mimalloc-obj.dir/src/static # ----------------------------------------------------------------------------- # API surface testing # ----------------------------------------------------------------------------- -add_executable(mimalloc-test-api test/test-api.c) -target_compile_definitions(mimalloc-test-api PRIVATE ${mi_defines}) -target_compile_options(mimalloc-test-api PRIVATE ${mi_cflags}) -target_include_directories(mimalloc-test-api PRIVATE include) -target_link_libraries(mimalloc-test-api PRIVATE mimalloc-static) -add_executable(mimalloc-test-stress test/test-stress.c) -target_compile_definitions(mimalloc-test-stress PRIVATE ${mi_defines}) -target_compile_options(mimalloc-test-stress PRIVATE ${mi_cflags}) -target_include_directories(mimalloc-test-stress PRIVATE include) -target_link_libraries(mimalloc-test-stress PRIVATE mimalloc-static) +if (MI_BUILD_TESTS MATCHES "ON") + add_executable(mimalloc-test-api test/test-api.c) + target_compile_definitions(mimalloc-test-api PRIVATE ${mi_defines}) + target_compile_options(mimalloc-test-api PRIVATE ${mi_cflags}) + target_include_directories(mimalloc-test-api PRIVATE include) + target_link_libraries(mimalloc-test-api PRIVATE mimalloc-static ${mi_libraries}) -enable_testing() -add_test(test_api, mimalloc-test-api) -add_test(test_stress, mimalloc-test-stress) + add_executable(mimalloc-test-stress test/test-stress.c) + target_compile_definitions(mimalloc-test-stress PRIVATE ${mi_defines}) + target_compile_options(mimalloc-test-stress PRIVATE ${mi_cflags}) + target_include_directories(mimalloc-test-stress PRIVATE include) + target_link_libraries(mimalloc-test-stress PRIVATE mimalloc-static ${mi_libraries}) + + enable_testing() + add_test(test_api, mimalloc-test-api) + add_test(test_stress, mimalloc-test-stress) +endif() # ----------------------------------------------------------------------------- # Set override properties From 0e35ec3879f885c8f5b72e96232d2d76bca93c8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlo=20Marcelo=20Arenas=20Bel=C3=B3n?= Date: Sun, 1 Sep 2019 01:06:01 -0700 Subject: [PATCH 3/3] avoid deadlock with OpenBSD --- include/mimalloc-internal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/mimalloc-internal.h b/include/mimalloc-internal.h index 26251878..df04c1b8 100644 --- a/include/mimalloc-internal.h +++ b/include/mimalloc-internal.h @@ -10,7 +10,7 @@ terms of the MIT license. A copy of the license can be found in the file #include "mimalloc-types.h" -#if defined(MI_MALLOC_OVERRIDE) && defined(__APPLE__) +#if defined(MI_MALLOC_OVERRIDE) && (defined(__APPLE__) || defined(__OpenBSD__)) #define MI_TLS_RECURSE_GUARD #endif