From c137ab047dbb59b9d62f8fe23efd3bbd77de28dc Mon Sep 17 00:00:00 2001 From: Kuba Mracek Date: Mon, 23 Aug 2021 13:53:29 -0700 Subject: [PATCH] Add a SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC flag to allow/disallow uses of Darwin libmalloc APIs (malloc_default_zone, malloc_zone_malloc, etc.) --- include/swift/Runtime/Concurrent.h | 2 +- stdlib/CMakeLists.txt | 4 ++++ stdlib/cmake/modules/AddSwiftStdlib.cmake | 6 ++++++ stdlib/public/runtime/Heap.cpp | 8 ++++---- utils/build-presets.ini | 1 + utils/build-script-impl | 2 ++ 6 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/swift/Runtime/Concurrent.h b/include/swift/Runtime/Concurrent.h index 693f2104d9ad8..e5452d0b7bc28 100644 --- a/include/swift/Runtime/Concurrent.h +++ b/include/swift/Runtime/Concurrent.h @@ -639,7 +639,7 @@ struct ConcurrentReadableHashMap { /// Otherwise, just return the passed-in size, which is always valid even if /// not necessarily optimal. static size_t goodSize(size_t size) { -#if defined(__APPLE__) && defined(__MACH__) +#if defined(__APPLE__) && defined(__MACH__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC return malloc_good_size(size); #else return size; diff --git a/stdlib/CMakeLists.txt b/stdlib/CMakeLists.txt index 12c7ec15264f3..bd90f5349c7d2 100644 --- a/stdlib/CMakeLists.txt +++ b/stdlib/CMakeLists.txt @@ -75,6 +75,10 @@ option(SWIFT_RUNTIME_MACHO_NO_DYLD "Build stdlib assuming the runtime environment uses Mach-O but does not support dynamic modules." FALSE) +option(SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC + "Build stdlib assuming the Darwin build of stdlib can use extended libmalloc APIs" + ) + option(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME "Build the standard libraries assuming that they will be used in an environment with only a single thread." FALSE) diff --git a/stdlib/cmake/modules/AddSwiftStdlib.cmake b/stdlib/cmake/modules/AddSwiftStdlib.cmake index bdeadc6cbe9ce..45dd11b6e6ab9 100644 --- a/stdlib/cmake/modules/AddSwiftStdlib.cmake +++ b/stdlib/cmake/modules/AddSwiftStdlib.cmake @@ -320,6 +320,12 @@ function(_add_target_variant_c_compile_flags) list(APPEND result "-DSWIFT_RUNTIME_MACHO_NO_DYLD") endif() + if(SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC) + list(APPEND result "-DSWIFT_STDLIB_HAS_DARWIN_LIBMALLOC=1") + else() + list(APPEND result "-DSWIFT_STDLIB_HAS_DARWIN_LIBMALLOC=0") + endif() + if(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME) list(APPEND result "-DSWIFT_STDLIB_SINGLE_THREADED_RUNTIME") endif() diff --git a/stdlib/public/runtime/Heap.cpp b/stdlib/public/runtime/Heap.cpp index d235cb907cd1e..e3246b4e0a07d 100644 --- a/stdlib/public/runtime/Heap.cpp +++ b/stdlib/public/runtime/Heap.cpp @@ -21,7 +21,7 @@ #include "../SwiftShims/RuntimeShims.h" #include #include -#if defined(__APPLE__) +#if defined(__APPLE__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC #include "swift/Basic/Lazy.h" #include #endif @@ -61,7 +61,7 @@ using namespace swift; static_assert(_swift_MinAllocationAlignment > MALLOC_ALIGN_MASK, "Swift's default alignment must exceed platform malloc mask."); -#if defined(__APPLE__) +#if defined(__APPLE__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC static inline malloc_zone_t *DEFAULT_ZONE() { static malloc_zone_t *z = SWIFT_LAZY_CONSTANT(malloc_default_zone()); return z; @@ -88,7 +88,7 @@ void *swift::swift_slowAlloc(size_t size, size_t alignMask) { void *p; // This check also forces "default" alignment to use AlignedAlloc. if (alignMask <= MALLOC_ALIGN_MASK) { -#if defined(__APPLE__) +#if defined(__APPLE__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC p = malloc_zone_malloc(DEFAULT_ZONE(), size); #else p = malloc(size); @@ -121,7 +121,7 @@ void *swift::swift_slowAlloc(size_t size, size_t alignMask) { // consistent with allocation with the same alignment. void swift::swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask) { if (alignMask <= MALLOC_ALIGN_MASK) { -#if defined(__APPLE__) +#if defined(__APPLE__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC malloc_zone_free(DEFAULT_ZONE(), ptr); #else free(ptr); diff --git a/utils/build-presets.ini b/utils/build-presets.ini index c6990572c0b9d..55be54f7fe5a8 100644 --- a/utils/build-presets.ini +++ b/utils/build-presets.ini @@ -2431,6 +2431,7 @@ build-swift-static-stdlib=1 swift-objc-interop=0 swift-enable-compatibility-overrides=0 swift-runtime-macho-no-dyld=1 +swift-stdlib-has-darwin-libmalloc=0 swift-stdlib-single-threaded-runtime=1 swift-stdlib-os-versioning=0 extra-cmake-options= diff --git a/utils/build-script-impl b/utils/build-script-impl index 744425a792101..c1ae10f9e10c2 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -203,6 +203,7 @@ KNOWN_SETTINGS=( swift-stdlib-single-threaded-runtime "0" "whether to build stdlib as a single-threaded runtime only" swift-stdlib-os-versioning "1" "whether to build stdlib with availability based on OS versions (Darwin only)" swift-stdlib-stable-abi "" "should stdlib be built with stable ABI, if not set defaults to true on Darwin, false otherwise" + swift-stdlib-has-darwin-libmalloc "1" "whether the Darwin build of stdlib can use extended libmalloc APIs" swift-disable-dead-stripping "0" "turns off Darwin-specific dead stripping for Swift host tools" common-swift-flags "" "Flags used for Swift targets other than the stdlib, like the corelibs" @@ -1961,6 +1962,7 @@ for host in "${ALL_HOSTS[@]}"; do -DSWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS:BOOL=$(true_false "${SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS}") -DSWIFT_RUNTIME_MACHO_NO_DYLD:BOOL=$(true_false "${SWIFT_RUNTIME_MACHO_NO_DYLD}") -DSWIFT_STDLIB_OS_VERSIONING:BOOL=$(true_false "${SWIFT_STDLIB_OS_VERSIONING}") + -DSWIFT_STDLIB_HAS_DARWIN_LIBMALLOC:BOOL=$(true_false "${SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC}") -DSWIFT_NATIVE_LLVM_TOOLS_PATH:STRING="${native_llvm_tools_path}" -DSWIFT_NATIVE_CLANG_TOOLS_PATH:STRING="${native_clang_tools_path}" -DSWIFT_NATIVE_SWIFT_TOOLS_PATH:STRING="${native_swift_tools_path}"