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