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