Skip to content

Commit 8c423b2

Browse files
committed
[Runtime] Define our own hidden global new/delete on Darwin.
On Darwin, define our own, hidden operator new/delete implementations. We don't want to pick up any overrides that come from other code, but we also don't want to expose our overrides to any other code. We can't do this directly in C++, as the compiler has an implicit prototype with default visibility. However, if we implement them as C functions using the C++ mangled names, the compiler accepts them without complaint, and the linker still links all internal uses with these overrides. rdar://92795919 (cherry picked from commit db1203a)
1 parent fa6883a commit 8c423b2

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

stdlib/public/runtime/Heap.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void *swift::swift_slowAlloc(size_t size, size_t alignMask) {
119119
// i.e. 0 < alignment <= _minAllocationAlignment:
120120
// The runtime may use either `free` or AlignedFree as long as it is
121121
// consistent with allocation with the same alignment.
122-
void swift::swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask) {
122+
static void swift_slowDeallocImpl(void *ptr, size_t alignMask) {
123123
if (alignMask <= MALLOC_ALIGN_MASK) {
124124
#if defined(__APPLE__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC
125125
malloc_zone_free(DEFAULT_ZONE(), ptr);
@@ -130,3 +130,34 @@ void swift::swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask) {
130130
AlignedFree(ptr);
131131
}
132132
}
133+
134+
void swift::swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask) {
135+
swift_slowDeallocImpl(ptr, alignMask);
136+
}
137+
138+
#if defined(__APPLE__) && defined(__MACH__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC
139+
// On Darwin, define our own, hidden operator new/delete implementations. We
140+
// don't want to pick up any overrides that come from other code, but we also
141+
// don't want to expose our overrides to any other code. We can't do this
142+
// directly in C++, as the compiler has an implicit prototype with default
143+
// visibility. However, if we implement them as C functions using the C++
144+
// mangled names, the compiler accepts them without complaint, and the linker
145+
// still links all internal uses with these overrides.
146+
147+
__attribute__((visibility(("hidden")))) extern "C" void *_Znwm(size_t size) {
148+
return swift_slowAlloc(size, MALLOC_ALIGN_MASK);
149+
}
150+
151+
__attribute__((visibility(("hidden")))) extern "C" void _ZdlPv(void *ptr) {
152+
swift_slowDeallocImpl(ptr, MALLOC_ALIGN_MASK);
153+
}
154+
155+
__attribute__((visibility(("hidden")))) extern "C" void *_Znam(size_t size) {
156+
return swift_slowAlloc(size, MALLOC_ALIGN_MASK);
157+
}
158+
159+
__attribute__((visibility(("hidden")))) extern "C" void _ZdaPv(void *ptr) {
160+
swift_slowDeallocImpl(ptr, MALLOC_ALIGN_MASK);
161+
}
162+
163+
#endif

0 commit comments

Comments
 (0)