@@ -111,21 +111,33 @@ static HeapObject *_swift_tryRetain_(HeapObject *object)
111111#ifdef SWIFT_STDLIB_OVERRIDABLE_RETAIN_RELEASE
112112
113113#define CALL_IMPL (name, args ) do { \
114- void *fptr; \
115- memcpy (&fptr, (void *)&_ ## name, sizeof (fptr)); \
116- extern char _ ## name ## _as_char asm (" __" #name " _" ); \
117- fptr = __ptrauth_swift_runtime_function_entry_strip (fptr); \
118- if (SWIFT_UNLIKELY (fptr != &_ ## name ## _as_char)) \
119- return _ ## name args; \
120- return _ ## name ## _ args; \
114+ if (SWIFT_UNLIKELY (_swift_enableSwizzlingOfAllocationAndRefCountingFunctions_forInstrumentsOnly.load (std::memory_order_relaxed))) \
115+ return _ ## name args; \
116+ return _ ## name ## _ args; \
121117} while (0 )
122118
119+ #define CALL_IMPL_CHECK (name, args ) do { \
120+ void *fptr; \
121+ memcpy (&fptr, (void *)&_ ## name, sizeof (fptr)); \
122+ extern char _ ## name ## _as_char asm (" __" #name " _" ); \
123+ fptr = __ptrauth_swift_runtime_function_entry_strip (fptr); \
124+ if (SWIFT_UNLIKELY (fptr != &_ ## name ## _as_char)) { \
125+ if (SWIFT_UNLIKELY (!_swift_enableSwizzlingOfAllocationAndRefCountingFunctions_forInstrumentsOnly.load (std::memory_order_relaxed))) { \
126+ _swift_enableSwizzlingOfAllocationAndRefCountingFunctions_forInstrumentsOnly.store (true , std::memory_order_relaxed); \
127+ } \
128+ return _ ## name args; \
129+ } \
130+ return _ ## name ## _ args; \
131+ } while (0 )
123132#else
124133
125134// If retain/release etc. aren't overridable, just call the real implementation.
126135#define CALL_IMPL (name, args ) \
127136 return _ ## name ## _ args;
128137
138+ #define CALL_IMPL_CHECK (name, args ) \
139+ return _ ## name ## _ args;
140+
129141#endif
130142
131143#if SWIFT_STDLIB_HAS_MALLOC_TYPE
@@ -230,6 +242,9 @@ HeapObject *(*SWIFT_RT_DECLARE_ENTRY _swift_allocObject)(
230242 HeapMetadata const *metadata, size_t requiredSize,
231243 size_t requiredAlignmentMask) = _swift_allocObject_;
232244
245+ SWIFT_RUNTIME_EXPORT
246+ std::atomic<bool > _swift_enableSwizzlingOfAllocationAndRefCountingFunctions_forInstrumentsOnly = false ;
247+
233248SWIFT_RUNTIME_EXPORT
234249HeapObject *(*SWIFT_RT_DECLARE_ENTRY _swift_retain)(HeapObject *object) =
235250 _swift_retain_;
@@ -280,7 +295,7 @@ static HeapObject *_swift_allocObject_(HeapMetadata const *metadata,
280295HeapObject *swift::swift_allocObject (HeapMetadata const *metadata,
281296 size_t requiredSize,
282297 size_t requiredAlignmentMask) {
283- CALL_IMPL (swift_allocObject, (metadata, requiredSize, requiredAlignmentMask));
298+ CALL_IMPL_CHECK (swift_allocObject, (metadata, requiredSize, requiredAlignmentMask));
284299}
285300
286301HeapObject *
0 commit comments