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