Skip to content

Commit b01436d

Browse files
authored
Merge pull request #85222 from mikeash/client-retain-release-no-weak-defs
[Runtime] Avoid weak definitions for client retain/release.
2 parents 0b344f8 + f3b0dbb commit b01436d

File tree

3 files changed

+67
-58
lines changed

3 files changed

+67
-58
lines changed

stdlib/public/ClientRetainRelease/RetainRelease.s

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
// offset so that it's still correct when the weak reference resolves to zero.
3737
.weak_reference __swift_retainRelease_slowpath_mask_v1
3838

39+
// preservemost retain/release entrypoints in the runtime
40+
.weak_reference _swift_retain_preservemost
41+
.weak_reference _swift_release_preservemost
42+
43+
3944
// Grab our own copy of the slowpath mask. This mask is a value which indicates
4045
// when we must call into the runtime slowpath. If the object's refcount field
4146
// has any bits set that are in the mask, then we must take the slow path. The
@@ -87,57 +92,13 @@ ret
8792
// limits, in addition to the usual x19 and up. Any calls to functions that use
8893
// the standard calling convention need to save/restore x9-x15.
8994

90-
.globl _swift_retain_preservemost
91-
.weak_definition _swift_retain_preservemost
92-
_swift_retain_preservemost:
93-
maybe_pacibsp
94-
stp x0, x9, [sp, #-0x50]!
95-
stp x10, x11, [sp, #0x10]
96-
stp x12, x13, [sp, #0x20]
97-
stp x14, x15, [sp, #0x30]
98-
stp fp, lr, [sp, #0x40];
99-
add fp, sp, #0x40
100-
101-
// Clear the unused bits from the pointer
102-
and x0, x0, #BRIDGEOBJECT_POINTER_BITS
103-
bl _swift_retain
104-
105-
ldp fp, lr, [sp, #0x40]
106-
ldp x14, x15, [sp, #0x30]
107-
ldp x12, x13, [sp, #0x20]
108-
ldp x10, x11, [sp, #0x10]
109-
ldp x0, x9, [sp], #0x50
110-
ret_maybe_ab
111-
112-
.globl _swift_release_preservemost
113-
.weak_definition _swift_release_preservemost
114-
_swift_release_preservemost:
115-
maybe_pacibsp
116-
str x9, [sp, #-0x50]!
117-
stp x10, x11, [sp, #0x10]
118-
stp x12, x13, [sp, #0x20]
119-
stp x14, x15, [sp, #0x30]
120-
stp fp, lr, [sp, #0x40];
121-
add fp, sp, #0x40
122-
123-
// Clear the unused bits from the pointer
124-
and x0, x0, #BRIDGEOBJECT_POINTER_BITS
125-
bl _swift_release
126-
127-
ldp fp, lr, [sp, #0x40]
128-
ldp x14, x15, [sp, #0x30]
129-
ldp x12, x13, [sp, #0x20]
130-
ldp x10, x11, [sp, #0x10]
131-
ldr x9, [sp], #0x50
132-
ret_maybe_ab
133-
13495
.private_extern _swift_bridgeObjectReleaseClient
13596
#if SWIFT_OBJC_INTEROP
13697
_swift_bridgeObjectReleaseClient:
13798
tbz x0, #63, LbridgeObjectReleaseNotTagged
13899
ret
139100
LbridgeObjectReleaseNotTagged:
140-
tbnz x0, #62, _bridgeObjectReleaseClientObjC
101+
tbnz x0, #62, LbridgeObjectReleaseClientObjC
141102
and x0, x0, 0x0ffffffffffffff8
142103

143104
#else
@@ -221,10 +182,35 @@ Lrelease_ret:
221182
Lslowpath_release:
222183
CONDITIONAL USE_LDX_STX, \
223184
clrex
185+
// If the weak preservemost symbol is NULL, call our helper. Otherwise call the
186+
// runtime directly.
187+
adrp x17, _swift_release_preservemost@GOTPAGE
188+
ldr x17, [x17, _swift_release_preservemost@GOTPAGEOFF]
189+
cbz x17, Lcall_swift_release
224190
b _swift_release_preservemost
225191

226-
.alt_entry _bridgeObjectReleaseClientObjC
227-
_bridgeObjectReleaseClientObjC:
192+
// Save/restore the preservemost registers and call swift_retain.
193+
Lcall_swift_release:
194+
maybe_pacibsp
195+
str x9, [sp, #-0x50]!
196+
stp x10, x11, [sp, #0x10]
197+
stp x12, x13, [sp, #0x20]
198+
stp x14, x15, [sp, #0x30]
199+
stp fp, lr, [sp, #0x40];
200+
add fp, sp, #0x40
201+
202+
// Clear the unused bits from the pointer
203+
and x0, x0, #BRIDGEOBJECT_POINTER_BITS
204+
bl _swift_release
205+
206+
ldp fp, lr, [sp, #0x40]
207+
ldp x14, x15, [sp, #0x30]
208+
ldp x12, x13, [sp, #0x20]
209+
ldp x10, x11, [sp, #0x10]
210+
ldr x9, [sp], #0x50
211+
ret_maybe_ab
212+
213+
LbridgeObjectReleaseClientObjC:
228214
maybe_pacibsp
229215
stp x0, x9, [sp, #-0x50]!
230216
stp x10, x11, [sp, #0x10]
@@ -252,7 +238,7 @@ _swift_bridgeObjectRetainClient:
252238
tbz x0, #63, LbridgeObjectRetainNotTagged
253239
ret
254240
LbridgeObjectRetainNotTagged:
255-
tbnz x0, #62, _swift_bridgeObjectRetainClientObjC
241+
tbnz x0, #62, Lswift_bridgeObjectRetainClientObjC
256242

257243
.alt_entry _swift_retainClient
258244
#else
@@ -334,10 +320,35 @@ Lretain_ret:
334320
Lslowpath_retain:
335321
CONDITIONAL USE_LDX_STX, \
336322
clrex
323+
// If the weak preservemost symbol is NULL, call our helper. Otherwise call the
324+
// runtime directly.
325+
adrp x17, _swift_retain_preservemost@GOTPAGE
326+
ldr x17, [x17, _swift_retain_preservemost@GOTPAGEOFF]
327+
cbz x17, Lcall_swift_retain
337328
b _swift_retain_preservemost
338329

339-
.alt_entry _swift_bridgeObjectRetainClientObjC
340-
_swift_bridgeObjectRetainClientObjC:
330+
// Save/restore the preservemost registers and call swift_retain.
331+
Lcall_swift_retain:
332+
maybe_pacibsp
333+
stp x0, x9, [sp, #-0x50]!
334+
stp x10, x11, [sp, #0x10]
335+
stp x12, x13, [sp, #0x20]
336+
stp x14, x15, [sp, #0x30]
337+
stp fp, lr, [sp, #0x40];
338+
add fp, sp, #0x40
339+
340+
// Clear the unused bits from the pointer
341+
and x0, x0, #BRIDGEOBJECT_POINTER_BITS
342+
bl _swift_retain
343+
344+
ldp fp, lr, [sp, #0x40]
345+
ldp x14, x15, [sp, #0x30]
346+
ldp x12, x13, [sp, #0x20]
347+
ldp x10, x11, [sp, #0x10]
348+
ldp x0, x9, [sp], #0x50
349+
ret_maybe_ab
350+
351+
Lswift_bridgeObjectRetainClientObjC:
341352
maybe_pacibsp
342353
stp x0, x9, [sp, #-0x50]!
343354
stp x10, x11, [sp, #0x10]

test/abi/macOS/arm64/stdlib.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,5 +1147,4 @@ Added: __swift_debug_metadataAllocatorPageSize
11471147
// New symbols to support emit-into-client retain/release.
11481148
Added: __swift_retainRelease_slowpath_mask_v1
11491149
Added: _swift_release_preservemost
1150-
Added: _swift_release_preservemost_weak_placeholder
11511150
Added: _swift_retain_preservemost

test/stdlib/symbol-visibility-darwin.test-sh

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
// Ensure that we do not export any unwanted weak symbols from the dylibs.
1+
// Ensure that we do not export any weak symbols from the dylibs.
22
//
33
// Weak symbols require additional work from the loader to resolve the symbol at
44
// load time and can cause ODR violations as well as unexpected symbol
55
// satisfaction because the weak symbol may be used from a separate module.
66
//
7-
// The _swift_release_preservemost_weak_placeholder symbol is present to allow
8-
// strong symbols from swiftCore to override weak symbols in other libraries,
9-
// to allow back-deployment usage of the swift_retain_preservemost and
10-
// swift_release_preservemost entrypoints.
7+
// Weak symbols in the runtime also conflict with the use of weak symbols in
8+
// embedded Swift. The presence of any weak symbol in swiftCore will allow its
9+
// strong symbols to override the weak symbols in the embedded runtime, and they
10+
// are not compatible.
1111

1212
// RUN: %empty-directory(%t)
1313

1414
// RUN: %llvm-nm --defined-only --extern-only --demangle %platform-dylib-dir/%target-library-name(swiftCore) > %t/swiftCore-all.txt
1515
// RUN: %llvm-nm --defined-only --extern-only --no-weak --demangle %platform-dylib-dir/%target-library-name(swiftCore) > %t/swiftCore-no-weak.txt
16-
// RUN: grep -v _swift_release_preservemost_weak_placeholder < %t/swiftCore-all.txt > %t/swiftCore-all-filtered.txt
17-
// RUN: diff -u %t/swiftCore-all-filtered.txt %t/swiftCore-no-weak.txt
16+
// RUN: diff -u %t/swiftCore-all.txt %t/swiftCore-no-weak.txt
1817

1918
// RUN: %llvm-nm --defined-only --extern-only --demangle %platform-dylib-dir/%target-library-name(swiftRemoteMirror) > %t/swiftRemoteMirror-all.txt
2019
// RUN: %llvm-nm --defined-only --extern-only --no-weak --demangle %platform-dylib-dir/%target-library-name(swiftRemoteMirror) > %t/swiftRemoteMirror-no-weak.txt

0 commit comments

Comments
 (0)