Skip to content

Add a SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC flag to allow/disallow uses of Darwin libmalloc APIs (malloc_default_zone, malloc_zone_malloc, etc.) #33812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2021
Merged
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
2 changes: 1 addition & 1 deletion include/swift/Runtime/Concurrent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/runtime/Heap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "../SwiftShims/RuntimeShims.h"
#include <algorithm>
#include <stdlib.h>
#if defined(__APPLE__)
#if defined(__APPLE__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC
#include "swift/Basic/Lazy.h"
#include <malloc/malloc.h>
#endif
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions utils/build-presets.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 2 additions & 0 deletions utils/build-script-impl
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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}"
Expand Down