Skip to content

Commit de50b0a

Browse files
authored
Eliminate pointer auth overhead in interposable refcount support (swiftlang#71448)
Fixes rdar://122595662
1 parent 923bf1f commit de50b0a

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

include/swift/Runtime/InstrumentsSupport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ void (*SWIFT_RT_DECLARE_ENTRY _swift_release)(HeapObject *object);
4242
SWIFT_RUNTIME_EXPORT
4343
void (*SWIFT_RT_DECLARE_ENTRY _swift_release_n)(HeapObject *object, uint32_t n);
4444
SWIFT_RUNTIME_EXPORT
45+
std::atomic<bool> _swift_enableSwizzlingOfAllocationAndRefCountingFunctions_forInstrumentsOnly;
46+
SWIFT_RUNTIME_EXPORT
4547
size_t swift_retainCount(HeapObject *object);
4648

4749
// liboainject tries to patch the function pointers and call the functions below

stdlib/public/runtime/HeapObject.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
233248
SWIFT_RUNTIME_EXPORT
234249
HeapObject *(*SWIFT_RT_DECLARE_ENTRY _swift_retain)(HeapObject *object) =
235250
_swift_retain_;
@@ -280,7 +295,7 @@ static HeapObject *_swift_allocObject_(HeapMetadata const *metadata,
280295
HeapObject *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

286301
HeapObject *

test/abi/macOS/arm64/stdlib.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,4 @@ Added: __swift_pod_indirect_initializeBufferWithCopyOfBuffer
256256
Added: __swift_validatePrespecializedMetadata
257257
Added: __swift_exceptionPersonality
258258
Added: _swift_willThrowTypedImpl
259+
Added: __swift_enableSwizzlingOfAllocationAndRefCountingFunctions_forInstrumentsOnly

test/abi/macOS/x86_64/stdlib.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,4 @@ Added: __swift_pod_indirect_initializeBufferWithCopyOfBuffer
256256
Added: __swift_validatePrespecializedMetadata
257257
Added: __swift_exceptionPersonality
258258
Added: _swift_willThrowTypedImpl
259+
Added: __swift_enableSwizzlingOfAllocationAndRefCountingFunctions_forInstrumentsOnly

0 commit comments

Comments
 (0)