1*19c3b8c2SApple OSS Distributions /*
2*19c3b8c2SApple OSS Distributions * runtime.c
3*19c3b8c2SApple OSS Distributions * libclosure
4*19c3b8c2SApple OSS Distributions *
5*19c3b8c2SApple OSS Distributions * Copyright (c) 2008-2010 Apple Inc. All rights reserved.
6*19c3b8c2SApple OSS Distributions *
7*19c3b8c2SApple OSS Distributions * @APPLE_LLVM_LICENSE_HEADER@
8*19c3b8c2SApple OSS Distributions */
9*19c3b8c2SApple OSS Distributions
10*19c3b8c2SApple OSS Distributions
11*19c3b8c2SApple OSS Distributions #ifndef KERNEL
12*19c3b8c2SApple OSS Distributions
13*19c3b8c2SApple OSS Distributions #include "Block_private.h"
14*19c3b8c2SApple OSS Distributions #include <stdio.h>
15*19c3b8c2SApple OSS Distributions #include <stdlib.h>
16*19c3b8c2SApple OSS Distributions #include <dlfcn.h>
17*19c3b8c2SApple OSS Distributions #include <os/assumes.h>
18*19c3b8c2SApple OSS Distributions #include <TargetConditionals.h>
19*19c3b8c2SApple OSS Distributions
20*19c3b8c2SApple OSS Distributions #else /* !KERNEL */
21*19c3b8c2SApple OSS Distributions #define TARGET_OS_WIN32 0
22*19c3b8c2SApple OSS Distributions
23*19c3b8c2SApple OSS Distributions #include <libkern/Block_private.h>
24*19c3b8c2SApple OSS Distributions __BEGIN_DECLS
25*19c3b8c2SApple OSS Distributions #include <kern/kalloc.h>
26*19c3b8c2SApple OSS Distributions __END_DECLS
27*19c3b8c2SApple OSS Distributions
28*19c3b8c2SApple OSS Distributions __typed_allocators_ignore_push
29*19c3b8c2SApple OSS Distributions
30*19c3b8c2SApple OSS Distributions static inline void *
malloc(size_t size)31*19c3b8c2SApple OSS Distributions malloc(size_t size)
32*19c3b8c2SApple OSS Distributions {
33*19c3b8c2SApple OSS Distributions if (size == 0) {
34*19c3b8c2SApple OSS Distributions return NULL;
35*19c3b8c2SApple OSS Distributions }
36*19c3b8c2SApple OSS Distributions return kheap_alloc(KHEAP_DEFAULT, size,
37*19c3b8c2SApple OSS Distributions Z_VM_TAG_BT(Z_WAITOK_ZERO, VM_KERN_MEMORY_LIBKERN));
38*19c3b8c2SApple OSS Distributions }
39*19c3b8c2SApple OSS Distributions
40*19c3b8c2SApple OSS Distributions static inline void
free(void * addr,size_t size)41*19c3b8c2SApple OSS Distributions free(void *addr, size_t size)
42*19c3b8c2SApple OSS Distributions {
43*19c3b8c2SApple OSS Distributions kheap_free(KHEAP_DEFAULT, addr, size);
44*19c3b8c2SApple OSS Distributions }
45*19c3b8c2SApple OSS Distributions
46*19c3b8c2SApple OSS Distributions __typed_allocators_ignore_pop
47*19c3b8c2SApple OSS Distributions
48*19c3b8c2SApple OSS Distributions #endif /* KERNEL */
49*19c3b8c2SApple OSS Distributions
50*19c3b8c2SApple OSS Distributions #include <machine/atomic.h>
51*19c3b8c2SApple OSS Distributions #include <string.h>
52*19c3b8c2SApple OSS Distributions #include <stdint.h>
53*19c3b8c2SApple OSS Distributions #ifndef os_assumes
54*19c3b8c2SApple OSS Distributions #define os_assumes(_x) (_x)
55*19c3b8c2SApple OSS Distributions #endif
56*19c3b8c2SApple OSS Distributions #ifndef os_assert
57*19c3b8c2SApple OSS Distributions #define os_assert(_x) assert(_x)
58*19c3b8c2SApple OSS Distributions #endif
59*19c3b8c2SApple OSS Distributions
60*19c3b8c2SApple OSS Distributions #if TARGET_OS_WIN32
61*19c3b8c2SApple OSS Distributions #define _CRT_SECURE_NO_WARNINGS 1
62*19c3b8c2SApple OSS Distributions #include <windows.h>
63*19c3b8c2SApple OSS Distributions static __inline bool
OSAtomicCompareAndSwapLong(long oldl,long newl,long volatile * dst)64*19c3b8c2SApple OSS Distributions OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst)
65*19c3b8c2SApple OSS Distributions {
66*19c3b8c2SApple OSS Distributions // fixme barrier is overkill -- see objc-os.h
67*19c3b8c2SApple OSS Distributions long original = InterlockedCompareExchange(dst, newl, oldl);
68*19c3b8c2SApple OSS Distributions return original == oldl;
69*19c3b8c2SApple OSS Distributions }
70*19c3b8c2SApple OSS Distributions
71*19c3b8c2SApple OSS Distributions static __inline bool
OSAtomicCompareAndSwapInt(int oldi,int newi,int volatile * dst)72*19c3b8c2SApple OSS Distributions OSAtomicCompareAndSwapInt(int oldi, int newi, int volatile *dst)
73*19c3b8c2SApple OSS Distributions {
74*19c3b8c2SApple OSS Distributions // fixme barrier is overkill -- see objc-os.h
75*19c3b8c2SApple OSS Distributions int original = InterlockedCompareExchange(dst, newi, oldi);
76*19c3b8c2SApple OSS Distributions return original == oldi;
77*19c3b8c2SApple OSS Distributions }
78*19c3b8c2SApple OSS Distributions #else
79*19c3b8c2SApple OSS Distributions #define OSAtomicCompareAndSwapLong(_Old, _New, _Ptr) os_atomic_cmpxchg(_Ptr, _Old, _New, relaxed)
80*19c3b8c2SApple OSS Distributions #define OSAtomicCompareAndSwapInt(_Old, _New, _Ptr) os_atomic_cmpxchg(_Ptr, _Old, _New, relaxed)
81*19c3b8c2SApple OSS Distributions #endif
82*19c3b8c2SApple OSS Distributions
83*19c3b8c2SApple OSS Distributions
84*19c3b8c2SApple OSS Distributions /*******************************************************************************
85*19c3b8c2SApple OSS Distributions * Internal Utilities
86*19c3b8c2SApple OSS Distributions ********************************************************************************/
87*19c3b8c2SApple OSS Distributions
88*19c3b8c2SApple OSS Distributions static int32_t
latching_incr_int(volatile int32_t * where)89*19c3b8c2SApple OSS Distributions latching_incr_int(volatile int32_t *where)
90*19c3b8c2SApple OSS Distributions {
91*19c3b8c2SApple OSS Distributions while (1) {
92*19c3b8c2SApple OSS Distributions int32_t old_value = *where;
93*19c3b8c2SApple OSS Distributions if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {
94*19c3b8c2SApple OSS Distributions return BLOCK_REFCOUNT_MASK;
95*19c3b8c2SApple OSS Distributions }
96*19c3b8c2SApple OSS Distributions if (OSAtomicCompareAndSwapInt(old_value, old_value + 2, where)) {
97*19c3b8c2SApple OSS Distributions return old_value + 2;
98*19c3b8c2SApple OSS Distributions }
99*19c3b8c2SApple OSS Distributions }
100*19c3b8c2SApple OSS Distributions }
101*19c3b8c2SApple OSS Distributions
102*19c3b8c2SApple OSS Distributions static bool
latching_incr_int_not_deallocating(volatile int32_t * where)103*19c3b8c2SApple OSS Distributions latching_incr_int_not_deallocating(volatile int32_t *where)
104*19c3b8c2SApple OSS Distributions {
105*19c3b8c2SApple OSS Distributions while (1) {
106*19c3b8c2SApple OSS Distributions int32_t old_value = *where;
107*19c3b8c2SApple OSS Distributions if (old_value & BLOCK_DEALLOCATING) {
108*19c3b8c2SApple OSS Distributions // if deallocating we can't do this
109*19c3b8c2SApple OSS Distributions return false;
110*19c3b8c2SApple OSS Distributions }
111*19c3b8c2SApple OSS Distributions if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {
112*19c3b8c2SApple OSS Distributions // if latched, we're leaking this block, and we succeed
113*19c3b8c2SApple OSS Distributions return true;
114*19c3b8c2SApple OSS Distributions }
115*19c3b8c2SApple OSS Distributions if (OSAtomicCompareAndSwapInt(old_value, old_value + 2, where)) {
116*19c3b8c2SApple OSS Distributions // otherwise, we must store a new retained value without the deallocating bit set
117*19c3b8c2SApple OSS Distributions return true;
118*19c3b8c2SApple OSS Distributions }
119*19c3b8c2SApple OSS Distributions }
120*19c3b8c2SApple OSS Distributions }
121*19c3b8c2SApple OSS Distributions
122*19c3b8c2SApple OSS Distributions
123*19c3b8c2SApple OSS Distributions // return should_deallocate?
124*19c3b8c2SApple OSS Distributions static bool
latching_decr_int_should_deallocate(volatile int32_t * where)125*19c3b8c2SApple OSS Distributions latching_decr_int_should_deallocate(volatile int32_t *where)
126*19c3b8c2SApple OSS Distributions {
127*19c3b8c2SApple OSS Distributions while (1) {
128*19c3b8c2SApple OSS Distributions int32_t old_value = *where;
129*19c3b8c2SApple OSS Distributions if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {
130*19c3b8c2SApple OSS Distributions return false; // latched high
131*19c3b8c2SApple OSS Distributions }
132*19c3b8c2SApple OSS Distributions if ((old_value & BLOCK_REFCOUNT_MASK) == 0) {
133*19c3b8c2SApple OSS Distributions return false; // underflow, latch low
134*19c3b8c2SApple OSS Distributions }
135*19c3b8c2SApple OSS Distributions int32_t new_value = old_value - 2;
136*19c3b8c2SApple OSS Distributions bool result = false;
137*19c3b8c2SApple OSS Distributions if ((old_value & (BLOCK_REFCOUNT_MASK | BLOCK_DEALLOCATING)) == 2) {
138*19c3b8c2SApple OSS Distributions new_value = old_value - 1;
139*19c3b8c2SApple OSS Distributions result = true;
140*19c3b8c2SApple OSS Distributions }
141*19c3b8c2SApple OSS Distributions if (OSAtomicCompareAndSwapInt(old_value, new_value, where)) {
142*19c3b8c2SApple OSS Distributions return result;
143*19c3b8c2SApple OSS Distributions }
144*19c3b8c2SApple OSS Distributions }
145*19c3b8c2SApple OSS Distributions }
146*19c3b8c2SApple OSS Distributions
147*19c3b8c2SApple OSS Distributions
148*19c3b8c2SApple OSS Distributions /**************************************************************************
149*19c3b8c2SApple OSS Distributions * Framework callback functions and their default implementations.
150*19c3b8c2SApple OSS Distributions ***************************************************************************/
151*19c3b8c2SApple OSS Distributions #if !TARGET_OS_WIN32
152*19c3b8c2SApple OSS Distributions #pragma mark Framework Callback Routines
153*19c3b8c2SApple OSS Distributions #endif
154*19c3b8c2SApple OSS Distributions #if KERNEL
155*19c3b8c2SApple OSS Distributions static inline void
_Block_retain_object(const void * ptr __unused)156*19c3b8c2SApple OSS Distributions _Block_retain_object(const void *ptr __unused)
157*19c3b8c2SApple OSS Distributions {
158*19c3b8c2SApple OSS Distributions }
159*19c3b8c2SApple OSS Distributions
160*19c3b8c2SApple OSS Distributions static inline void
_Block_release_object(const void * ptr __unused)161*19c3b8c2SApple OSS Distributions _Block_release_object(const void *ptr __unused)
162*19c3b8c2SApple OSS Distributions {
163*19c3b8c2SApple OSS Distributions }
164*19c3b8c2SApple OSS Distributions
165*19c3b8c2SApple OSS Distributions static inline void
_Block_destructInstance(const void * aBlock __unused)166*19c3b8c2SApple OSS Distributions _Block_destructInstance(const void *aBlock __unused)
167*19c3b8c2SApple OSS Distributions {
168*19c3b8c2SApple OSS Distributions }
169*19c3b8c2SApple OSS Distributions
170*19c3b8c2SApple OSS Distributions #else
171*19c3b8c2SApple OSS Distributions
172*19c3b8c2SApple OSS Distributions static void
_Block_retain_object_default(const void * ptr __unused)173*19c3b8c2SApple OSS Distributions _Block_retain_object_default(const void *ptr __unused)
174*19c3b8c2SApple OSS Distributions {
175*19c3b8c2SApple OSS Distributions }
176*19c3b8c2SApple OSS Distributions
177*19c3b8c2SApple OSS Distributions static void
_Block_release_object_default(const void * ptr __unused)178*19c3b8c2SApple OSS Distributions _Block_release_object_default(const void *ptr __unused)
179*19c3b8c2SApple OSS Distributions {
180*19c3b8c2SApple OSS Distributions }
181*19c3b8c2SApple OSS Distributions
182*19c3b8c2SApple OSS Distributions static void
_Block_destructInstance_default(const void * aBlock __unused)183*19c3b8c2SApple OSS Distributions _Block_destructInstance_default(const void *aBlock __unused)
184*19c3b8c2SApple OSS Distributions {
185*19c3b8c2SApple OSS Distributions }
186*19c3b8c2SApple OSS Distributions
187*19c3b8c2SApple OSS Distributions static void (*_Block_retain_object)(const void *ptr) = _Block_retain_object_default;
188*19c3b8c2SApple OSS Distributions static void (*_Block_release_object)(const void *ptr) = _Block_release_object_default;
189*19c3b8c2SApple OSS Distributions static void (*_Block_destructInstance) (const void *aBlock) = _Block_destructInstance_default;
190*19c3b8c2SApple OSS Distributions
191*19c3b8c2SApple OSS Distributions
192*19c3b8c2SApple OSS Distributions /**************************************************************************
193*19c3b8c2SApple OSS Distributions * Callback registration from ObjC runtime and CoreFoundation
194*19c3b8c2SApple OSS Distributions ***************************************************************************/
195*19c3b8c2SApple OSS Distributions
196*19c3b8c2SApple OSS Distributions void
_Block_use_RR2(const Block_callbacks_RR * callbacks)197*19c3b8c2SApple OSS Distributions _Block_use_RR2(const Block_callbacks_RR *callbacks)
198*19c3b8c2SApple OSS Distributions {
199*19c3b8c2SApple OSS Distributions _Block_retain_object = callbacks->retain;
200*19c3b8c2SApple OSS Distributions _Block_release_object = callbacks->release;
201*19c3b8c2SApple OSS Distributions _Block_destructInstance = callbacks->destructInstance;
202*19c3b8c2SApple OSS Distributions }
203*19c3b8c2SApple OSS Distributions #endif // !KERNEL
204*19c3b8c2SApple OSS Distributions
205*19c3b8c2SApple OSS Distributions /****************************************************************************
206*19c3b8c2SApple OSS Distributions * Accessors for block descriptor fields
207*19c3b8c2SApple OSS Distributions *****************************************************************************/
208*19c3b8c2SApple OSS Distributions
209*19c3b8c2SApple OSS Distributions #if BLOCK_SMALL_DESCRIPTOR_SUPPORTED
210*19c3b8c2SApple OSS Distributions template <class T>
211*19c3b8c2SApple OSS Distributions static T *
unwrap_relative_pointer(int32_t & offset)212*19c3b8c2SApple OSS Distributions unwrap_relative_pointer(int32_t &offset)
213*19c3b8c2SApple OSS Distributions {
214*19c3b8c2SApple OSS Distributions if (offset == 0) {
215*19c3b8c2SApple OSS Distributions return nullptr;
216*19c3b8c2SApple OSS Distributions }
217*19c3b8c2SApple OSS Distributions
218*19c3b8c2SApple OSS Distributions uintptr_t base = (uintptr_t)&offset;
219*19c3b8c2SApple OSS Distributions uintptr_t extendedOffset = (uintptr_t)(intptr_t)offset;
220*19c3b8c2SApple OSS Distributions uintptr_t pointer = base + extendedOffset;
221*19c3b8c2SApple OSS Distributions return (T *)pointer;
222*19c3b8c2SApple OSS Distributions }
223*19c3b8c2SApple OSS Distributions #endif
224*19c3b8c2SApple OSS Distributions
225*19c3b8c2SApple OSS Distributions #if 0
226*19c3b8c2SApple OSS Distributions static struct Block_descriptor_2 *
227*19c3b8c2SApple OSS Distributions _Block_descriptor_2(struct Block_layout *aBlock)
228*19c3b8c2SApple OSS Distributions {
229*19c3b8c2SApple OSS Distributions uint8_t *desc = (uint8_t *)_Block_get_descriptor(aBlock);
230*19c3b8c2SApple OSS Distributions desc += sizeof(struct Block_descriptor_1);
231*19c3b8c2SApple OSS Distributions return __IGNORE_WCASTALIGN((struct Block_descriptor_2 *)desc);
232*19c3b8c2SApple OSS Distributions }
233*19c3b8c2SApple OSS Distributions #endif
234*19c3b8c2SApple OSS Distributions
235*19c3b8c2SApple OSS Distributions static struct Block_descriptor_3 *
_Block_descriptor_3(struct Block_layout * aBlock)236*19c3b8c2SApple OSS Distributions _Block_descriptor_3(struct Block_layout *aBlock)
237*19c3b8c2SApple OSS Distributions {
238*19c3b8c2SApple OSS Distributions uint8_t *desc = (uint8_t *)_Block_get_descriptor(aBlock);
239*19c3b8c2SApple OSS Distributions desc += sizeof(struct Block_descriptor_1);
240*19c3b8c2SApple OSS Distributions if (aBlock->flags & BLOCK_HAS_COPY_DISPOSE) {
241*19c3b8c2SApple OSS Distributions desc += sizeof(struct Block_descriptor_2);
242*19c3b8c2SApple OSS Distributions }
243*19c3b8c2SApple OSS Distributions return __IGNORE_WCASTALIGN((struct Block_descriptor_3 *)desc);
244*19c3b8c2SApple OSS Distributions }
245*19c3b8c2SApple OSS Distributions
246*19c3b8c2SApple OSS Distributions static void
_Block_call_copy_helper(void * result,struct Block_layout * aBlock)247*19c3b8c2SApple OSS Distributions _Block_call_copy_helper(void *result, struct Block_layout *aBlock)
248*19c3b8c2SApple OSS Distributions {
249*19c3b8c2SApple OSS Distributions if (auto *pFn = _Block_get_copy_function(aBlock)) {
250*19c3b8c2SApple OSS Distributions pFn(result, aBlock);
251*19c3b8c2SApple OSS Distributions }
252*19c3b8c2SApple OSS Distributions }
253*19c3b8c2SApple OSS Distributions
254*19c3b8c2SApple OSS Distributions static void
_Block_call_dispose_helper(struct Block_layout * aBlock)255*19c3b8c2SApple OSS Distributions _Block_call_dispose_helper(struct Block_layout *aBlock)
256*19c3b8c2SApple OSS Distributions {
257*19c3b8c2SApple OSS Distributions if (auto *pFn = _Block_get_dispose_function(aBlock)) {
258*19c3b8c2SApple OSS Distributions pFn(aBlock);
259*19c3b8c2SApple OSS Distributions }
260*19c3b8c2SApple OSS Distributions }
261*19c3b8c2SApple OSS Distributions
262*19c3b8c2SApple OSS Distributions /*******************************************************************************
263*19c3b8c2SApple OSS Distributions * Internal Support routines for copying
264*19c3b8c2SApple OSS Distributions ********************************************************************************/
265*19c3b8c2SApple OSS Distributions
266*19c3b8c2SApple OSS Distributions #if !TARGET_OS_WIN32
267*19c3b8c2SApple OSS Distributions #pragma mark Copy/Release support
268*19c3b8c2SApple OSS Distributions #endif
269*19c3b8c2SApple OSS Distributions
270*19c3b8c2SApple OSS Distributions // Copy, or bump refcount, of a block. If really copying, call the copy helper if present.
271*19c3b8c2SApple OSS Distributions void *
_Block_copy(const void * arg)272*19c3b8c2SApple OSS Distributions _Block_copy(const void *arg)
273*19c3b8c2SApple OSS Distributions {
274*19c3b8c2SApple OSS Distributions struct Block_layout *aBlock;
275*19c3b8c2SApple OSS Distributions
276*19c3b8c2SApple OSS Distributions if (!arg) {
277*19c3b8c2SApple OSS Distributions return NULL;
278*19c3b8c2SApple OSS Distributions }
279*19c3b8c2SApple OSS Distributions
280*19c3b8c2SApple OSS Distributions // The following would be better done as a switch statement
281*19c3b8c2SApple OSS Distributions aBlock = (struct Block_layout *)arg;
282*19c3b8c2SApple OSS Distributions if (aBlock->flags & BLOCK_NEEDS_FREE) {
283*19c3b8c2SApple OSS Distributions // latches on high
284*19c3b8c2SApple OSS Distributions latching_incr_int(&aBlock->flags);
285*19c3b8c2SApple OSS Distributions return aBlock;
286*19c3b8c2SApple OSS Distributions } else if (aBlock->flags & BLOCK_IS_GLOBAL) {
287*19c3b8c2SApple OSS Distributions return aBlock;
288*19c3b8c2SApple OSS Distributions } else {
289*19c3b8c2SApple OSS Distributions // Its a stack block. Make a copy.
290*19c3b8c2SApple OSS Distributions size_t size = Block_size(aBlock);
291*19c3b8c2SApple OSS Distributions struct Block_layout *result = (struct Block_layout *)malloc(size);
292*19c3b8c2SApple OSS Distributions if (!result) {
293*19c3b8c2SApple OSS Distributions return NULL;
294*19c3b8c2SApple OSS Distributions }
295*19c3b8c2SApple OSS Distributions memmove(result, aBlock, size); // bitcopy first
296*19c3b8c2SApple OSS Distributions #if __has_feature(ptrauth_calls)
297*19c3b8c2SApple OSS Distributions // Resign the invoke pointer as it uses address authentication.
298*19c3b8c2SApple OSS Distributions result->invoke = aBlock->invoke;
299*19c3b8c2SApple OSS Distributions
300*19c3b8c2SApple OSS Distributions #if __has_feature(ptrauth_signed_block_descriptors)
301*19c3b8c2SApple OSS Distributions uintptr_t oldDesc =
302*19c3b8c2SApple OSS Distributions ptrauth_blend_discriminator(
303*19c3b8c2SApple OSS Distributions &aBlock->descriptor, _Block_descriptor_ptrauth_discriminator);
304*19c3b8c2SApple OSS Distributions uintptr_t newDesc =
305*19c3b8c2SApple OSS Distributions ptrauth_blend_discriminator(
306*19c3b8c2SApple OSS Distributions &result->descriptor, _Block_descriptor_ptrauth_discriminator);
307*19c3b8c2SApple OSS Distributions
308*19c3b8c2SApple OSS Distributions result->descriptor =
309*19c3b8c2SApple OSS Distributions ptrauth_auth_and_resign(aBlock->descriptor, ptrauth_key_asda, oldDesc,
310*19c3b8c2SApple OSS Distributions ptrauth_key_asda, newDesc);
311*19c3b8c2SApple OSS Distributions #endif
312*19c3b8c2SApple OSS Distributions #endif
313*19c3b8c2SApple OSS Distributions
314*19c3b8c2SApple OSS Distributions // reset refcount
315*19c3b8c2SApple OSS Distributions result->flags &= ~(BLOCK_REFCOUNT_MASK | BLOCK_DEALLOCATING); // XXX not needed
316*19c3b8c2SApple OSS Distributions result->flags |= BLOCK_NEEDS_FREE | 2; // logical refcount 1
317*19c3b8c2SApple OSS Distributions _Block_call_copy_helper(result, aBlock);
318*19c3b8c2SApple OSS Distributions // Set isa last so memory analysis tools see a fully-initialized object.
319*19c3b8c2SApple OSS Distributions result->isa = _NSConcreteMallocBlock;
320*19c3b8c2SApple OSS Distributions return result;
321*19c3b8c2SApple OSS Distributions }
322*19c3b8c2SApple OSS Distributions }
323*19c3b8c2SApple OSS Distributions
324*19c3b8c2SApple OSS Distributions
325*19c3b8c2SApple OSS Distributions // Runtime entry points for maintaining the sharing knowledge of byref data blocks.
326*19c3b8c2SApple OSS Distributions
327*19c3b8c2SApple OSS Distributions // A closure has been copied and its fixup routine is asking us to fix up the reference to the shared byref data
328*19c3b8c2SApple OSS Distributions // Closures that aren't copied must still work, so everyone always accesses variables after dereferencing the forwarding ptr.
329*19c3b8c2SApple OSS Distributions // We ask if the byref pointer that we know about has already been copied to the heap, and if so, increment and return it.
330*19c3b8c2SApple OSS Distributions // Otherwise we need to copy it and update the stack forwarding pointer
331*19c3b8c2SApple OSS Distributions static struct Block_byref *
_Block_byref_copy(const void * arg)332*19c3b8c2SApple OSS Distributions _Block_byref_copy(const void *arg)
333*19c3b8c2SApple OSS Distributions {
334*19c3b8c2SApple OSS Distributions struct Block_byref *src = (struct Block_byref *)arg;
335*19c3b8c2SApple OSS Distributions
336*19c3b8c2SApple OSS Distributions if ((src->forwarding->flags & BLOCK_REFCOUNT_MASK) == 0) {
337*19c3b8c2SApple OSS Distributions // src points to stack
338*19c3b8c2SApple OSS Distributions struct Block_byref *copy = (struct Block_byref *)malloc(src->size);
339*19c3b8c2SApple OSS Distributions copy->isa = NULL;
340*19c3b8c2SApple OSS Distributions // byref value 4 is logical refcount of 2: one for caller, one for stack
341*19c3b8c2SApple OSS Distributions copy->flags = src->flags | BLOCK_BYREF_NEEDS_FREE | 4;
342*19c3b8c2SApple OSS Distributions copy->forwarding = copy; // patch heap copy to point to itself
343*19c3b8c2SApple OSS Distributions src->forwarding = copy; // patch stack to point to heap copy
344*19c3b8c2SApple OSS Distributions copy->size = src->size;
345*19c3b8c2SApple OSS Distributions
346*19c3b8c2SApple OSS Distributions if (src->flags & BLOCK_BYREF_HAS_COPY_DISPOSE) {
347*19c3b8c2SApple OSS Distributions // Trust copy helper to copy everything of interest
348*19c3b8c2SApple OSS Distributions // If more than one field shows up in a byref block this is wrong XXX
349*19c3b8c2SApple OSS Distributions struct Block_byref_2 *src2 = (struct Block_byref_2 *)(src + 1);
350*19c3b8c2SApple OSS Distributions struct Block_byref_2 *copy2 = (struct Block_byref_2 *)(copy + 1);
351*19c3b8c2SApple OSS Distributions copy2->byref_keep = src2->byref_keep;
352*19c3b8c2SApple OSS Distributions copy2->byref_destroy = src2->byref_destroy;
353*19c3b8c2SApple OSS Distributions
354*19c3b8c2SApple OSS Distributions if (src->flags & BLOCK_BYREF_LAYOUT_EXTENDED) {
355*19c3b8c2SApple OSS Distributions struct Block_byref_3 *src3 = (struct Block_byref_3 *)(src2 + 1);
356*19c3b8c2SApple OSS Distributions struct Block_byref_3 *copy3 = (struct Block_byref_3*)(copy2 + 1);
357*19c3b8c2SApple OSS Distributions copy3->layout = src3->layout;
358*19c3b8c2SApple OSS Distributions }
359*19c3b8c2SApple OSS Distributions
360*19c3b8c2SApple OSS Distributions (*src2->byref_keep)(copy, src);
361*19c3b8c2SApple OSS Distributions } else {
362*19c3b8c2SApple OSS Distributions // Bitwise copy.
363*19c3b8c2SApple OSS Distributions // This copy includes Block_byref_3, if any.
364*19c3b8c2SApple OSS Distributions memmove(copy + 1, src + 1, src->size - sizeof(*src));
365*19c3b8c2SApple OSS Distributions }
366*19c3b8c2SApple OSS Distributions }
367*19c3b8c2SApple OSS Distributions // already copied to heap
368*19c3b8c2SApple OSS Distributions else if ((src->forwarding->flags & BLOCK_BYREF_NEEDS_FREE) == BLOCK_BYREF_NEEDS_FREE) {
369*19c3b8c2SApple OSS Distributions latching_incr_int(&src->forwarding->flags);
370*19c3b8c2SApple OSS Distributions }
371*19c3b8c2SApple OSS Distributions
372*19c3b8c2SApple OSS Distributions return src->forwarding;
373*19c3b8c2SApple OSS Distributions }
374*19c3b8c2SApple OSS Distributions
375*19c3b8c2SApple OSS Distributions static void
_Block_byref_release(const void * arg)376*19c3b8c2SApple OSS Distributions _Block_byref_release(const void *arg)
377*19c3b8c2SApple OSS Distributions {
378*19c3b8c2SApple OSS Distributions struct Block_byref *byref = (struct Block_byref *)arg;
379*19c3b8c2SApple OSS Distributions
380*19c3b8c2SApple OSS Distributions // dereference the forwarding pointer since the compiler isn't doing this anymore (ever?)
381*19c3b8c2SApple OSS Distributions byref = byref->forwarding;
382*19c3b8c2SApple OSS Distributions
383*19c3b8c2SApple OSS Distributions if (byref->flags & BLOCK_BYREF_NEEDS_FREE) {
384*19c3b8c2SApple OSS Distributions __assert_only int32_t refcount = byref->flags & BLOCK_REFCOUNT_MASK;
385*19c3b8c2SApple OSS Distributions os_assert(refcount);
386*19c3b8c2SApple OSS Distributions if (latching_decr_int_should_deallocate(&byref->flags)) {
387*19c3b8c2SApple OSS Distributions if (byref->flags & BLOCK_BYREF_HAS_COPY_DISPOSE) {
388*19c3b8c2SApple OSS Distributions struct Block_byref_2 *byref2 = (struct Block_byref_2 *)(byref + 1);
389*19c3b8c2SApple OSS Distributions (*byref2->byref_destroy)(byref);
390*19c3b8c2SApple OSS Distributions }
391*19c3b8c2SApple OSS Distributions free(byref, byref->size);
392*19c3b8c2SApple OSS Distributions }
393*19c3b8c2SApple OSS Distributions }
394*19c3b8c2SApple OSS Distributions }
395*19c3b8c2SApple OSS Distributions
396*19c3b8c2SApple OSS Distributions
397*19c3b8c2SApple OSS Distributions /************************************************************
398*19c3b8c2SApple OSS Distributions *
399*19c3b8c2SApple OSS Distributions * API supporting SPI
400*19c3b8c2SApple OSS Distributions * _Block_copy, _Block_release, and (old) _Block_destroy
401*19c3b8c2SApple OSS Distributions *
402*19c3b8c2SApple OSS Distributions ***********************************************************/
403*19c3b8c2SApple OSS Distributions
404*19c3b8c2SApple OSS Distributions #if !TARGET_OS_WIN32
405*19c3b8c2SApple OSS Distributions #pragma mark SPI/API
406*19c3b8c2SApple OSS Distributions #endif
407*19c3b8c2SApple OSS Distributions
408*19c3b8c2SApple OSS Distributions
409*19c3b8c2SApple OSS Distributions // API entry point to release a copied Block
410*19c3b8c2SApple OSS Distributions void
_Block_release(const void * arg)411*19c3b8c2SApple OSS Distributions _Block_release(const void *arg)
412*19c3b8c2SApple OSS Distributions {
413*19c3b8c2SApple OSS Distributions struct Block_layout *aBlock = (struct Block_layout *)arg;
414*19c3b8c2SApple OSS Distributions if (!aBlock) {
415*19c3b8c2SApple OSS Distributions return;
416*19c3b8c2SApple OSS Distributions }
417*19c3b8c2SApple OSS Distributions if (aBlock->flags & BLOCK_IS_GLOBAL) {
418*19c3b8c2SApple OSS Distributions return;
419*19c3b8c2SApple OSS Distributions }
420*19c3b8c2SApple OSS Distributions if (!(aBlock->flags & BLOCK_NEEDS_FREE)) {
421*19c3b8c2SApple OSS Distributions return;
422*19c3b8c2SApple OSS Distributions }
423*19c3b8c2SApple OSS Distributions
424*19c3b8c2SApple OSS Distributions if (latching_decr_int_should_deallocate(&aBlock->flags)) {
425*19c3b8c2SApple OSS Distributions _Block_call_dispose_helper(aBlock);
426*19c3b8c2SApple OSS Distributions _Block_destructInstance(aBlock);
427*19c3b8c2SApple OSS Distributions free(aBlock, Block_size(aBlock));
428*19c3b8c2SApple OSS Distributions }
429*19c3b8c2SApple OSS Distributions }
430*19c3b8c2SApple OSS Distributions
431*19c3b8c2SApple OSS Distributions bool
_Block_tryRetain(const void * arg)432*19c3b8c2SApple OSS Distributions _Block_tryRetain(const void *arg)
433*19c3b8c2SApple OSS Distributions {
434*19c3b8c2SApple OSS Distributions struct Block_layout *aBlock = (struct Block_layout *)arg;
435*19c3b8c2SApple OSS Distributions return latching_incr_int_not_deallocating(&aBlock->flags);
436*19c3b8c2SApple OSS Distributions }
437*19c3b8c2SApple OSS Distributions
438*19c3b8c2SApple OSS Distributions bool
_Block_isDeallocating(const void * arg)439*19c3b8c2SApple OSS Distributions _Block_isDeallocating(const void *arg)
440*19c3b8c2SApple OSS Distributions {
441*19c3b8c2SApple OSS Distributions struct Block_layout *aBlock = (struct Block_layout *)arg;
442*19c3b8c2SApple OSS Distributions return (aBlock->flags & BLOCK_DEALLOCATING) != 0;
443*19c3b8c2SApple OSS Distributions }
444*19c3b8c2SApple OSS Distributions
445*19c3b8c2SApple OSS Distributions
446*19c3b8c2SApple OSS Distributions /************************************************************
447*19c3b8c2SApple OSS Distributions *
448*19c3b8c2SApple OSS Distributions * SPI used by other layers
449*19c3b8c2SApple OSS Distributions *
450*19c3b8c2SApple OSS Distributions ***********************************************************/
451*19c3b8c2SApple OSS Distributions
452*19c3b8c2SApple OSS Distributions size_t
Block_size(void * aBlock)453*19c3b8c2SApple OSS Distributions Block_size(void *aBlock)
454*19c3b8c2SApple OSS Distributions {
455*19c3b8c2SApple OSS Distributions auto *layout = (Block_layout *)aBlock;
456*19c3b8c2SApple OSS Distributions void *desc = _Block_get_descriptor(layout);
457*19c3b8c2SApple OSS Distributions #if BLOCK_SMALL_DESCRIPTOR_SUPPORTED
458*19c3b8c2SApple OSS Distributions if (layout->flags & BLOCK_SMALL_DESCRIPTOR) {
459*19c3b8c2SApple OSS Distributions return ((Block_descriptor_small *)desc)->size;
460*19c3b8c2SApple OSS Distributions }
461*19c3b8c2SApple OSS Distributions #endif
462*19c3b8c2SApple OSS Distributions return ((Block_descriptor_1 *)desc)->size;
463*19c3b8c2SApple OSS Distributions }
464*19c3b8c2SApple OSS Distributions
465*19c3b8c2SApple OSS Distributions bool
_Block_use_stret(void * aBlock)466*19c3b8c2SApple OSS Distributions _Block_use_stret(void *aBlock)
467*19c3b8c2SApple OSS Distributions {
468*19c3b8c2SApple OSS Distributions struct Block_layout *layout = (struct Block_layout *)aBlock;
469*19c3b8c2SApple OSS Distributions
470*19c3b8c2SApple OSS Distributions int requiredFlags = BLOCK_HAS_SIGNATURE | BLOCK_USE_STRET;
471*19c3b8c2SApple OSS Distributions return (layout->flags & requiredFlags) == requiredFlags;
472*19c3b8c2SApple OSS Distributions }
473*19c3b8c2SApple OSS Distributions
474*19c3b8c2SApple OSS Distributions // Checks for a valid signature, not merely the BLOCK_HAS_SIGNATURE bit.
475*19c3b8c2SApple OSS Distributions bool
_Block_has_signature(void * aBlock)476*19c3b8c2SApple OSS Distributions _Block_has_signature(void *aBlock)
477*19c3b8c2SApple OSS Distributions {
478*19c3b8c2SApple OSS Distributions return _Block_signature(aBlock) ? true : false;
479*19c3b8c2SApple OSS Distributions }
480*19c3b8c2SApple OSS Distributions
481*19c3b8c2SApple OSS Distributions const char *
_Block_signature(void * aBlock)482*19c3b8c2SApple OSS Distributions _Block_signature(void *aBlock)
483*19c3b8c2SApple OSS Distributions {
484*19c3b8c2SApple OSS Distributions struct Block_layout *layout = (struct Block_layout *)aBlock;
485*19c3b8c2SApple OSS Distributions if (!(layout->flags & BLOCK_HAS_SIGNATURE)) {
486*19c3b8c2SApple OSS Distributions return nullptr;
487*19c3b8c2SApple OSS Distributions }
488*19c3b8c2SApple OSS Distributions
489*19c3b8c2SApple OSS Distributions #if BLOCK_SMALL_DESCRIPTOR_SUPPORTED
490*19c3b8c2SApple OSS Distributions if (layout->flags & BLOCK_SMALL_DESCRIPTOR) {
491*19c3b8c2SApple OSS Distributions auto *bds = (Block_descriptor_small *)_Block_get_descriptor(layout);
492*19c3b8c2SApple OSS Distributions return unwrap_relative_pointer<const char>(bds->signature);
493*19c3b8c2SApple OSS Distributions }
494*19c3b8c2SApple OSS Distributions #endif
495*19c3b8c2SApple OSS Distributions
496*19c3b8c2SApple OSS Distributions struct Block_descriptor_3 *desc3 = _Block_descriptor_3(layout);
497*19c3b8c2SApple OSS Distributions return desc3->signature;
498*19c3b8c2SApple OSS Distributions }
499*19c3b8c2SApple OSS Distributions
500*19c3b8c2SApple OSS Distributions const char *
_Block_layout(void * aBlock)501*19c3b8c2SApple OSS Distributions _Block_layout(void *aBlock)
502*19c3b8c2SApple OSS Distributions {
503*19c3b8c2SApple OSS Distributions // Don't return extended layout to callers expecting old GC layout
504*19c3b8c2SApple OSS Distributions Block_layout *layout = (Block_layout *)aBlock;
505*19c3b8c2SApple OSS Distributions if ((layout->flags & BLOCK_HAS_EXTENDED_LAYOUT) ||
506*19c3b8c2SApple OSS Distributions !(layout->flags & BLOCK_HAS_SIGNATURE)) {
507*19c3b8c2SApple OSS Distributions return nullptr;
508*19c3b8c2SApple OSS Distributions }
509*19c3b8c2SApple OSS Distributions
510*19c3b8c2SApple OSS Distributions #if BLOCK_SMALL_DESCRIPTOR_SUPPORTED
511*19c3b8c2SApple OSS Distributions if (layout->flags & BLOCK_SMALL_DESCRIPTOR) {
512*19c3b8c2SApple OSS Distributions auto *bds = (Block_descriptor_small *)_Block_get_descriptor(layout);
513*19c3b8c2SApple OSS Distributions return unwrap_relative_pointer<const char>(bds->layout);
514*19c3b8c2SApple OSS Distributions }
515*19c3b8c2SApple OSS Distributions #endif
516*19c3b8c2SApple OSS Distributions
517*19c3b8c2SApple OSS Distributions Block_descriptor_3 *desc = _Block_descriptor_3(layout);
518*19c3b8c2SApple OSS Distributions return desc->layout;
519*19c3b8c2SApple OSS Distributions }
520*19c3b8c2SApple OSS Distributions
521*19c3b8c2SApple OSS Distributions const char *
_Block_extended_layout(void * aBlock)522*19c3b8c2SApple OSS Distributions _Block_extended_layout(void *aBlock)
523*19c3b8c2SApple OSS Distributions {
524*19c3b8c2SApple OSS Distributions // Don't return old GC layout to callers expecting extended layout
525*19c3b8c2SApple OSS Distributions Block_layout *layout = (Block_layout *)aBlock;
526*19c3b8c2SApple OSS Distributions if (!(layout->flags & BLOCK_HAS_EXTENDED_LAYOUT) ||
527*19c3b8c2SApple OSS Distributions !(layout->flags & BLOCK_HAS_SIGNATURE)) {
528*19c3b8c2SApple OSS Distributions return nullptr;
529*19c3b8c2SApple OSS Distributions }
530*19c3b8c2SApple OSS Distributions
531*19c3b8c2SApple OSS Distributions const char *extLayout;
532*19c3b8c2SApple OSS Distributions #if BLOCK_SMALL_DESCRIPTOR_SUPPORTED
533*19c3b8c2SApple OSS Distributions if (layout->flags & BLOCK_SMALL_DESCRIPTOR) {
534*19c3b8c2SApple OSS Distributions auto *bds = (Block_descriptor_small *)_Block_get_descriptor(layout);
535*19c3b8c2SApple OSS Distributions if (layout->flags & BLOCK_INLINE_LAYOUT_STRING) {
536*19c3b8c2SApple OSS Distributions extLayout = (const char *)(uintptr_t)bds->layout;
537*19c3b8c2SApple OSS Distributions } else {
538*19c3b8c2SApple OSS Distributions extLayout = unwrap_relative_pointer<const char>(bds->layout);
539*19c3b8c2SApple OSS Distributions }
540*19c3b8c2SApple OSS Distributions } else
541*19c3b8c2SApple OSS Distributions #endif
542*19c3b8c2SApple OSS Distributions {
543*19c3b8c2SApple OSS Distributions Block_descriptor_3 *desc3 = _Block_descriptor_3(layout);
544*19c3b8c2SApple OSS Distributions extLayout = desc3->layout;
545*19c3b8c2SApple OSS Distributions }
546*19c3b8c2SApple OSS Distributions
547*19c3b8c2SApple OSS Distributions // Return empty string (all non-object bytes) instead of NULL
548*19c3b8c2SApple OSS Distributions // so callers can distinguish "empty layout" from "no layout".
549*19c3b8c2SApple OSS Distributions if (!extLayout) {
550*19c3b8c2SApple OSS Distributions extLayout = "";
551*19c3b8c2SApple OSS Distributions }
552*19c3b8c2SApple OSS Distributions return extLayout;
553*19c3b8c2SApple OSS Distributions }
554*19c3b8c2SApple OSS Distributions
555*19c3b8c2SApple OSS Distributions #if !TARGET_OS_WIN32
556*19c3b8c2SApple OSS Distributions #pragma mark Compiler SPI entry points
557*19c3b8c2SApple OSS Distributions #endif
558*19c3b8c2SApple OSS Distributions
559*19c3b8c2SApple OSS Distributions
560*19c3b8c2SApple OSS Distributions /*******************************************************
561*19c3b8c2SApple OSS Distributions *
562*19c3b8c2SApple OSS Distributions * Entry points used by the compiler - the real API!
563*19c3b8c2SApple OSS Distributions *
564*19c3b8c2SApple OSS Distributions *
565*19c3b8c2SApple OSS Distributions * A Block can reference four different kinds of things that require help when the Block is copied to the heap.
566*19c3b8c2SApple OSS Distributions * 1) C++ stack based objects
567*19c3b8c2SApple OSS Distributions * 2) References to Objective-C objects
568*19c3b8c2SApple OSS Distributions * 3) Other Blocks
569*19c3b8c2SApple OSS Distributions * 4) __block variables
570*19c3b8c2SApple OSS Distributions *
571*19c3b8c2SApple OSS Distributions * In these cases helper functions are synthesized by the compiler for use in Block_copy and Block_release, called the copy and dispose helpers. The copy helper emits a call to the C++ const copy constructor for C++ stack based objects and for the rest calls into the runtime support function _Block_object_assign. The dispose helper has a call to the C++ destructor for case 1 and a call into _Block_object_dispose for the rest.
572*19c3b8c2SApple OSS Distributions *
573*19c3b8c2SApple OSS Distributions * The flags parameter of _Block_object_assign and _Block_object_dispose is set to
574*19c3b8c2SApple OSS Distributions * BLOCK_FIELD_IS_OBJECT (3), for the case of an Objective-C Object,
575*19c3b8c2SApple OSS Distributions * BLOCK_FIELD_IS_BLOCK (7), for the case of another Block, and
576*19c3b8c2SApple OSS Distributions * BLOCK_FIELD_IS_BYREF (8), for the case of a __block variable.
577*19c3b8c2SApple OSS Distributions * If the __block variable is marked weak the compiler also or's in BLOCK_FIELD_IS_WEAK (16)
578*19c3b8c2SApple OSS Distributions *
579*19c3b8c2SApple OSS Distributions * So the Block copy/dispose helpers should only ever generate the four flag values of 3, 7, 8, and 24.
580*19c3b8c2SApple OSS Distributions *
581*19c3b8c2SApple OSS Distributions * When a __block variable is either a C++ object, an Objective-C object, or another Block then the compiler also generates copy/dispose helper functions. Similarly to the Block copy helper, the "__block" copy helper (formerly and still a.k.a. "byref" copy helper) will do a C++ copy constructor (not a const one though!) and the dispose helper will do the destructor. And similarly the helpers will call into the same two support functions with the same values for objects and Blocks with the additional BLOCK_BYREF_CALLER (128) bit of information supplied.
582*19c3b8c2SApple OSS Distributions *
583*19c3b8c2SApple OSS Distributions * So the __block copy/dispose helpers will generate flag values of 3 or 7 for objects and Blocks respectively, with BLOCK_FIELD_IS_WEAK (16) or'ed as appropriate and always 128 or'd in, for the following set of possibilities:
584*19c3b8c2SApple OSS Distributions * __block id 128+3 (0x83)
585*19c3b8c2SApple OSS Distributions * __block (^Block) 128+7 (0x87)
586*19c3b8c2SApple OSS Distributions * __weak __block id 128+3+16 (0x93)
587*19c3b8c2SApple OSS Distributions * __weak __block (^Block) 128+7+16 (0x97)
588*19c3b8c2SApple OSS Distributions *
589*19c3b8c2SApple OSS Distributions *
590*19c3b8c2SApple OSS Distributions ********************************************************/
591*19c3b8c2SApple OSS Distributions
592*19c3b8c2SApple OSS Distributions //
593*19c3b8c2SApple OSS Distributions // When Blocks or Block_byrefs hold objects then their copy routine helpers use this entry point
594*19c3b8c2SApple OSS Distributions // to do the assignment.
595*19c3b8c2SApple OSS Distributions //
596*19c3b8c2SApple OSS Distributions void
_Block_object_assign(void * destArg,const void * object,const int flags)597*19c3b8c2SApple OSS Distributions _Block_object_assign(void *destArg, const void *object, const int flags)
598*19c3b8c2SApple OSS Distributions {
599*19c3b8c2SApple OSS Distributions const void **dest = (const void **)destArg;
600*19c3b8c2SApple OSS Distributions switch (os_assumes(flags & BLOCK_ALL_COPY_DISPOSE_FLAGS)) {
601*19c3b8c2SApple OSS Distributions case BLOCK_FIELD_IS_OBJECT:
602*19c3b8c2SApple OSS Distributions /*******
603*19c3b8c2SApple OSS Distributions * id object = ...;
604*19c3b8c2SApple OSS Distributions * [^{ object; } copy];
605*19c3b8c2SApple OSS Distributions ********/
606*19c3b8c2SApple OSS Distributions
607*19c3b8c2SApple OSS Distributions _Block_retain_object(object);
608*19c3b8c2SApple OSS Distributions *dest = object;
609*19c3b8c2SApple OSS Distributions break;
610*19c3b8c2SApple OSS Distributions
611*19c3b8c2SApple OSS Distributions case BLOCK_FIELD_IS_BLOCK:
612*19c3b8c2SApple OSS Distributions /*******
613*19c3b8c2SApple OSS Distributions * void (^object)(void) = ...;
614*19c3b8c2SApple OSS Distributions * [^{ object; } copy];
615*19c3b8c2SApple OSS Distributions ********/
616*19c3b8c2SApple OSS Distributions
617*19c3b8c2SApple OSS Distributions *dest = _Block_copy(object);
618*19c3b8c2SApple OSS Distributions break;
619*19c3b8c2SApple OSS Distributions
620*19c3b8c2SApple OSS Distributions case BLOCK_FIELD_IS_BYREF | BLOCK_FIELD_IS_WEAK:
621*19c3b8c2SApple OSS Distributions case BLOCK_FIELD_IS_BYREF:
622*19c3b8c2SApple OSS Distributions /*******
623*19c3b8c2SApple OSS Distributions * // copy the onstack __block container to the heap
624*19c3b8c2SApple OSS Distributions * // Note this __weak is old GC-weak/MRC-unretained.
625*19c3b8c2SApple OSS Distributions * // ARC-style __weak is handled by the copy helper directly.
626*19c3b8c2SApple OSS Distributions * __block ... x;
627*19c3b8c2SApple OSS Distributions * __weak __block ... x;
628*19c3b8c2SApple OSS Distributions * [^{ x; } copy];
629*19c3b8c2SApple OSS Distributions ********/
630*19c3b8c2SApple OSS Distributions
631*19c3b8c2SApple OSS Distributions *dest = _Block_byref_copy(object);
632*19c3b8c2SApple OSS Distributions break;
633*19c3b8c2SApple OSS Distributions
634*19c3b8c2SApple OSS Distributions case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT:
635*19c3b8c2SApple OSS Distributions case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK:
636*19c3b8c2SApple OSS Distributions /*******
637*19c3b8c2SApple OSS Distributions * // copy the actual field held in the __block container
638*19c3b8c2SApple OSS Distributions * // Note this is MRC unretained __block only.
639*19c3b8c2SApple OSS Distributions * // ARC retained __block is handled by the copy helper directly.
640*19c3b8c2SApple OSS Distributions * __block id object;
641*19c3b8c2SApple OSS Distributions * __block void (^object)(void);
642*19c3b8c2SApple OSS Distributions * [^{ object; } copy];
643*19c3b8c2SApple OSS Distributions ********/
644*19c3b8c2SApple OSS Distributions
645*19c3b8c2SApple OSS Distributions *dest = object;
646*19c3b8c2SApple OSS Distributions break;
647*19c3b8c2SApple OSS Distributions
648*19c3b8c2SApple OSS Distributions case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT | BLOCK_FIELD_IS_WEAK:
649*19c3b8c2SApple OSS Distributions case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK | BLOCK_FIELD_IS_WEAK:
650*19c3b8c2SApple OSS Distributions /*******
651*19c3b8c2SApple OSS Distributions * // copy the actual field held in the __block container
652*19c3b8c2SApple OSS Distributions * // Note this __weak is old GC-weak/MRC-unretained.
653*19c3b8c2SApple OSS Distributions * // ARC-style __weak is handled by the copy helper directly.
654*19c3b8c2SApple OSS Distributions * __weak __block id object;
655*19c3b8c2SApple OSS Distributions * __weak __block void (^object)(void);
656*19c3b8c2SApple OSS Distributions * [^{ object; } copy];
657*19c3b8c2SApple OSS Distributions ********/
658*19c3b8c2SApple OSS Distributions
659*19c3b8c2SApple OSS Distributions *dest = object;
660*19c3b8c2SApple OSS Distributions break;
661*19c3b8c2SApple OSS Distributions
662*19c3b8c2SApple OSS Distributions default:
663*19c3b8c2SApple OSS Distributions break;
664*19c3b8c2SApple OSS Distributions }
665*19c3b8c2SApple OSS Distributions }
666*19c3b8c2SApple OSS Distributions
667*19c3b8c2SApple OSS Distributions // When Blocks or Block_byrefs hold objects their destroy helper routines call this entry point
668*19c3b8c2SApple OSS Distributions // to help dispose of the contents
669*19c3b8c2SApple OSS Distributions void
_Block_object_dispose(const void * object,const int flags)670*19c3b8c2SApple OSS Distributions _Block_object_dispose(const void *object, const int flags)
671*19c3b8c2SApple OSS Distributions {
672*19c3b8c2SApple OSS Distributions switch (os_assumes(flags & BLOCK_ALL_COPY_DISPOSE_FLAGS)) {
673*19c3b8c2SApple OSS Distributions case BLOCK_FIELD_IS_BYREF | BLOCK_FIELD_IS_WEAK:
674*19c3b8c2SApple OSS Distributions case BLOCK_FIELD_IS_BYREF:
675*19c3b8c2SApple OSS Distributions // get rid of the __block data structure held in a Block
676*19c3b8c2SApple OSS Distributions _Block_byref_release(object);
677*19c3b8c2SApple OSS Distributions break;
678*19c3b8c2SApple OSS Distributions case BLOCK_FIELD_IS_BLOCK:
679*19c3b8c2SApple OSS Distributions _Block_release(object);
680*19c3b8c2SApple OSS Distributions break;
681*19c3b8c2SApple OSS Distributions case BLOCK_FIELD_IS_OBJECT:
682*19c3b8c2SApple OSS Distributions _Block_release_object(object);
683*19c3b8c2SApple OSS Distributions break;
684*19c3b8c2SApple OSS Distributions case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT:
685*19c3b8c2SApple OSS Distributions case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK:
686*19c3b8c2SApple OSS Distributions case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT | BLOCK_FIELD_IS_WEAK:
687*19c3b8c2SApple OSS Distributions case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK | BLOCK_FIELD_IS_WEAK:
688*19c3b8c2SApple OSS Distributions break;
689*19c3b8c2SApple OSS Distributions default:
690*19c3b8c2SApple OSS Distributions break;
691*19c3b8c2SApple OSS Distributions }
692*19c3b8c2SApple OSS Distributions }
693*19c3b8c2SApple OSS Distributions
694*19c3b8c2SApple OSS Distributions
695*19c3b8c2SApple OSS Distributions // Workaround for <rdar://26015603> dylib with no __DATA segment fails to rebase
696*19c3b8c2SApple OSS Distributions __attribute__((used))
697*19c3b8c2SApple OSS Distributions static int let_there_be_data = 42;
698