Skip to content

Commit aaff37f

Browse files
authored
Add a SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC flag to allow/disallow uses of Darwin libmalloc APIs (malloc_default_zone, malloc_zone_malloc, etc.) (#33812)
1 parent c079c08 commit aaff37f

File tree

6 files changed

+18
-5
lines changed

6 files changed

+18
-5
lines changed

include/swift/Runtime/Concurrent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ struct ConcurrentReadableHashMap {
639639
/// Otherwise, just return the passed-in size, which is always valid even if
640640
/// not necessarily optimal.
641641
static size_t goodSize(size_t size) {
642-
#if defined(__APPLE__) && defined(__MACH__)
642+
#if defined(__APPLE__) && defined(__MACH__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC
643643
return malloc_good_size(size);
644644
#else
645645
return size;

stdlib/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ option(SWIFT_RUNTIME_MACHO_NO_DYLD
7575
"Build stdlib assuming the runtime environment uses Mach-O but does not support dynamic modules."
7676
FALSE)
7777

78+
option(SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC
79+
"Build stdlib assuming the Darwin build of stdlib can use extended libmalloc APIs"
80+
)
81+
7882
option(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
7983
"Build the standard libraries assuming that they will be used in an environment with only a single thread."
8084
FALSE)

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,12 @@ function(_add_target_variant_c_compile_flags)
320320
list(APPEND result "-DSWIFT_RUNTIME_MACHO_NO_DYLD")
321321
endif()
322322

323+
if(SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC)
324+
list(APPEND result "-DSWIFT_STDLIB_HAS_DARWIN_LIBMALLOC=1")
325+
else()
326+
list(APPEND result "-DSWIFT_STDLIB_HAS_DARWIN_LIBMALLOC=0")
327+
endif()
328+
323329
if(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME)
324330
list(APPEND result "-DSWIFT_STDLIB_SINGLE_THREADED_RUNTIME")
325331
endif()

stdlib/public/runtime/Heap.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "../SwiftShims/RuntimeShims.h"
2222
#include <algorithm>
2323
#include <stdlib.h>
24-
#if defined(__APPLE__)
24+
#if defined(__APPLE__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC
2525
#include "swift/Basic/Lazy.h"
2626
#include <malloc/malloc.h>
2727
#endif
@@ -61,7 +61,7 @@ using namespace swift;
6161
static_assert(_swift_MinAllocationAlignment > MALLOC_ALIGN_MASK,
6262
"Swift's default alignment must exceed platform malloc mask.");
6363

64-
#if defined(__APPLE__)
64+
#if defined(__APPLE__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC
6565
static inline malloc_zone_t *DEFAULT_ZONE() {
6666
static malloc_zone_t *z = SWIFT_LAZY_CONSTANT(malloc_default_zone());
6767
return z;
@@ -88,7 +88,7 @@ void *swift::swift_slowAlloc(size_t size, size_t alignMask) {
8888
void *p;
8989
// This check also forces "default" alignment to use AlignedAlloc.
9090
if (alignMask <= MALLOC_ALIGN_MASK) {
91-
#if defined(__APPLE__)
91+
#if defined(__APPLE__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC
9292
p = malloc_zone_malloc(DEFAULT_ZONE(), size);
9393
#else
9494
p = malloc(size);
@@ -121,7 +121,7 @@ void *swift::swift_slowAlloc(size_t size, size_t alignMask) {
121121
// consistent with allocation with the same alignment.
122122
void swift::swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask) {
123123
if (alignMask <= MALLOC_ALIGN_MASK) {
124-
#if defined(__APPLE__)
124+
#if defined(__APPLE__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC
125125
malloc_zone_free(DEFAULT_ZONE(), ptr);
126126
#else
127127
free(ptr);

utils/build-presets.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2431,6 +2431,7 @@ build-swift-static-stdlib=1
24312431
swift-objc-interop=0
24322432
swift-enable-compatibility-overrides=0
24332433
swift-runtime-macho-no-dyld=1
2434+
swift-stdlib-has-darwin-libmalloc=0
24342435
swift-stdlib-single-threaded-runtime=1
24352436
swift-stdlib-os-versioning=0
24362437
extra-cmake-options=

utils/build-script-impl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ KNOWN_SETTINGS=(
203203
swift-stdlib-single-threaded-runtime "0" "whether to build stdlib as a single-threaded runtime only"
204204
swift-stdlib-os-versioning "1" "whether to build stdlib with availability based on OS versions (Darwin only)"
205205
swift-stdlib-stable-abi "" "should stdlib be built with stable ABI, if not set defaults to true on Darwin, false otherwise"
206+
swift-stdlib-has-darwin-libmalloc "1" "whether the Darwin build of stdlib can use extended libmalloc APIs"
206207
swift-disable-dead-stripping "0" "turns off Darwin-specific dead stripping for Swift host tools"
207208
common-swift-flags "" "Flags used for Swift targets other than the stdlib, like the corelibs"
208209

@@ -1961,6 +1962,7 @@ for host in "${ALL_HOSTS[@]}"; do
19611962
-DSWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS:BOOL=$(true_false "${SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS}")
19621963
-DSWIFT_RUNTIME_MACHO_NO_DYLD:BOOL=$(true_false "${SWIFT_RUNTIME_MACHO_NO_DYLD}")
19631964
-DSWIFT_STDLIB_OS_VERSIONING:BOOL=$(true_false "${SWIFT_STDLIB_OS_VERSIONING}")
1965+
-DSWIFT_STDLIB_HAS_DARWIN_LIBMALLOC:BOOL=$(true_false "${SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC}")
19641966
-DSWIFT_NATIVE_LLVM_TOOLS_PATH:STRING="${native_llvm_tools_path}"
19651967
-DSWIFT_NATIVE_CLANG_TOOLS_PATH:STRING="${native_clang_tools_path}"
19661968
-DSWIFT_NATIVE_SWIFT_TOOLS_PATH:STRING="${native_swift_tools_path}"

0 commit comments

Comments
 (0)