5 * Copyright (c) 2008-2010 Apple Inc. All rights reserved.
7 * @APPLE_LLVM_LICENSE_HEADER@
13 #include "Block_private.h"
17 #include <os/assumes.h>
20 #define TARGET_OS_WIN32 0
22 #include <libkern/Block_private.h>
24 #include <kern/kalloc.h>
33 return kheap_alloc_tag_bt(KHEAP_DEFAULT
, size
,
34 (zalloc_flags_t
) (Z_WAITOK
| Z_ZERO
), VM_KERN_MEMORY_LIBKERN
);
40 kheap_free_addr(KHEAP_DEFAULT
, addr
);
45 #include <machine/atomic.h>
49 #define os_assumes(_x) (_x)
52 #define os_assert(_x) assert(_x)
56 #define _CRT_SECURE_NO_WARNINGS 1
59 OSAtomicCompareAndSwapLong(long oldl
, long newl
, long volatile *dst
)
61 // fixme barrier is overkill -- see objc-os.h
62 long original
= InterlockedCompareExchange(dst
, newl
, oldl
);
63 return original
== oldl
;
67 OSAtomicCompareAndSwapInt(int oldi
, int newi
, int volatile *dst
)
69 // fixme barrier is overkill -- see objc-os.h
70 int original
= InterlockedCompareExchange(dst
, newi
, oldi
);
71 return original
== oldi
;
74 #define OSAtomicCompareAndSwapLong(_Old, _New, _Ptr) os_atomic_cmpxchg(_Ptr, _Old, _New, relaxed)
75 #define OSAtomicCompareAndSwapInt(_Old, _New, _Ptr) os_atomic_cmpxchg(_Ptr, _Old, _New, relaxed)
79 /*******************************************************************************
81 ********************************************************************************/
84 latching_incr_int(volatile int32_t *where
)
87 int32_t old_value
= *where
;
88 if ((old_value
& BLOCK_REFCOUNT_MASK
) == BLOCK_REFCOUNT_MASK
) {
89 return BLOCK_REFCOUNT_MASK
;
91 if (OSAtomicCompareAndSwapInt(old_value
, old_value
+ 2, where
)) {
98 latching_incr_int_not_deallocating(volatile int32_t *where
)
101 int32_t old_value
= *where
;
102 if (old_value
& BLOCK_DEALLOCATING
) {
103 // if deallocating we can't do this
106 if ((old_value
& BLOCK_REFCOUNT_MASK
) == BLOCK_REFCOUNT_MASK
) {
107 // if latched, we're leaking this block, and we succeed
110 if (OSAtomicCompareAndSwapInt(old_value
, old_value
+ 2, where
)) {
111 // otherwise, we must store a new retained value without the deallocating bit set
118 // return should_deallocate?
120 latching_decr_int_should_deallocate(volatile int32_t *where
)
123 int32_t old_value
= *where
;
124 if ((old_value
& BLOCK_REFCOUNT_MASK
) == BLOCK_REFCOUNT_MASK
) {
125 return false; // latched high
127 if ((old_value
& BLOCK_REFCOUNT_MASK
) == 0) {
128 return false; // underflow, latch low
130 int32_t new_value
= old_value
- 2;
132 if ((old_value
& (BLOCK_REFCOUNT_MASK
| BLOCK_DEALLOCATING
)) == 2) {
133 new_value
= old_value
- 1;
136 if (OSAtomicCompareAndSwapInt(old_value
, new_value
, where
)) {
143 /**************************************************************************
144 * Framework callback functions and their default implementations.
145 ***************************************************************************/
147 #pragma mark Framework Callback Routines
151 _Block_retain_object(const void *ptr __unused
)
156 _Block_release_object(const void *ptr __unused
)
161 _Block_destructInstance(const void *aBlock __unused
)
168 _Block_retain_object_default(const void *ptr __unused
)
173 _Block_release_object_default(const void *ptr __unused
)
178 _Block_destructInstance_default(const void *aBlock __unused
)
182 static void (*_Block_retain_object
)(const void *ptr
) = _Block_retain_object_default
;
183 static void (*_Block_release_object
)(const void *ptr
) = _Block_release_object_default
;
184 static void (*_Block_destructInstance
) (const void *aBlock
) = _Block_destructInstance_default
;
187 /**************************************************************************
188 * Callback registration from ObjC runtime and CoreFoundation
189 ***************************************************************************/
192 _Block_use_RR2(const Block_callbacks_RR
*callbacks
)
194 _Block_retain_object
= callbacks
->retain
;
195 _Block_release_object
= callbacks
->release
;
196 _Block_destructInstance
= callbacks
->destructInstance
;
200 /****************************************************************************
201 * Accessors for block descriptor fields
202 *****************************************************************************/
206 unwrap_relative_pointer(int32_t &offset
)
212 uintptr_t base
= (uintptr_t)&offset
;
213 uintptr_t extendedOffset
= (uintptr_t)(intptr_t)offset
;
214 uintptr_t pointer
= base
+ extendedOffset
;
219 static struct Block_descriptor_2
*
220 _Block_descriptor_2(struct Block_layout
*aBlock
)
222 uint8_t *desc
= (uint8_t *)_Block_get_descriptor(aBlock
);
223 desc
+= sizeof(struct Block_descriptor_1
);
224 return __IGNORE_WCASTALIGN((struct Block_descriptor_2
*)desc
);
228 static struct Block_descriptor_3
*
229 _Block_descriptor_3(struct Block_layout
*aBlock
)
231 uint8_t *desc
= (uint8_t *)_Block_get_descriptor(aBlock
);
232 desc
+= sizeof(struct Block_descriptor_1
);
233 if (aBlock
->flags
& BLOCK_HAS_COPY_DISPOSE
) {
234 desc
+= sizeof(struct Block_descriptor_2
);
236 return __IGNORE_WCASTALIGN((struct Block_descriptor_3
*)desc
);
240 _Block_call_copy_helper(void *result
, struct Block_layout
*aBlock
)
242 if (auto *pFn
= _Block_get_copy_function(aBlock
)) {
248 _Block_call_dispose_helper(struct Block_layout
*aBlock
)
250 if (auto *pFn
= _Block_get_dispose_function(aBlock
)) {
255 /*******************************************************************************
256 * Internal Support routines for copying
257 ********************************************************************************/
260 #pragma mark Copy/Release support
263 // Copy, or bump refcount, of a block. If really copying, call the copy helper if present.
265 _Block_copy(const void *arg
)
267 struct Block_layout
*aBlock
;
273 // The following would be better done as a switch statement
274 aBlock
= (struct Block_layout
*)arg
;
275 if (aBlock
->flags
& BLOCK_NEEDS_FREE
) {
277 latching_incr_int(&aBlock
->flags
);
279 } else if (aBlock
->flags
& BLOCK_IS_GLOBAL
) {
282 // Its a stack block. Make a copy.
283 size_t size
= Block_size(aBlock
);
284 struct Block_layout
*result
= (struct Block_layout
*)malloc(size
);
288 memmove(result
, aBlock
, size
); // bitcopy first
289 #if __has_feature(ptrauth_calls)
290 // Resign the invoke pointer as it uses address authentication.
291 result
->invoke
= aBlock
->invoke
;
293 #if __has_feature(ptrauth_signed_block_descriptors)
295 ptrauth_blend_discriminator(
296 &aBlock
->descriptor
, _Block_descriptor_ptrauth_discriminator
);
298 ptrauth_blend_discriminator(
299 &result
->descriptor
, _Block_descriptor_ptrauth_discriminator
);
302 ptrauth_auth_and_resign(aBlock
->descriptor
, ptrauth_key_asda
, oldDesc
,
303 ptrauth_key_asda
, newDesc
);
308 result
->flags
&= ~(BLOCK_REFCOUNT_MASK
| BLOCK_DEALLOCATING
); // XXX not needed
309 result
->flags
|= BLOCK_NEEDS_FREE
| 2; // logical refcount 1
310 _Block_call_copy_helper(result
, aBlock
);
311 // Set isa last so memory analysis tools see a fully-initialized object.
312 result
->isa
= _NSConcreteMallocBlock
;
318 // Runtime entry points for maintaining the sharing knowledge of byref data blocks.
320 // A closure has been copied and its fixup routine is asking us to fix up the reference to the shared byref data
321 // Closures that aren't copied must still work, so everyone always accesses variables after dereferencing the forwarding ptr.
322 // We ask if the byref pointer that we know about has already been copied to the heap, and if so, increment and return it.
323 // Otherwise we need to copy it and update the stack forwarding pointer
324 static struct Block_byref
*
325 _Block_byref_copy(const void *arg
)
327 struct Block_byref
*src
= (struct Block_byref
*)arg
;
329 if ((src
->forwarding
->flags
& BLOCK_REFCOUNT_MASK
) == 0) {
330 // src points to stack
331 struct Block_byref
*copy
= (struct Block_byref
*)malloc(src
->size
);
333 // byref value 4 is logical refcount of 2: one for caller, one for stack
334 copy
->flags
= src
->flags
| BLOCK_BYREF_NEEDS_FREE
| 4;
335 copy
->forwarding
= copy
; // patch heap copy to point to itself
336 src
->forwarding
= copy
; // patch stack to point to heap copy
337 copy
->size
= src
->size
;
339 if (src
->flags
& BLOCK_BYREF_HAS_COPY_DISPOSE
) {
340 // Trust copy helper to copy everything of interest
341 // If more than one field shows up in a byref block this is wrong XXX
342 struct Block_byref_2
*src2
= (struct Block_byref_2
*)(src
+ 1);
343 struct Block_byref_2
*copy2
= (struct Block_byref_2
*)(copy
+ 1);
344 copy2
->byref_keep
= src2
->byref_keep
;
345 copy2
->byref_destroy
= src2
->byref_destroy
;
347 if (src
->flags
& BLOCK_BYREF_LAYOUT_EXTENDED
) {
348 struct Block_byref_3
*src3
= (struct Block_byref_3
*)(src2
+ 1);
349 struct Block_byref_3
*copy3
= (struct Block_byref_3
*)(copy2
+ 1);
350 copy3
->layout
= src3
->layout
;
353 (*src2
->byref_keep
)(copy
, src
);
356 // This copy includes Block_byref_3, if any.
357 memmove(copy
+ 1, src
+ 1, src
->size
- sizeof(*src
));
360 // already copied to heap
361 else if ((src
->forwarding
->flags
& BLOCK_BYREF_NEEDS_FREE
) == BLOCK_BYREF_NEEDS_FREE
) {
362 latching_incr_int(&src
->forwarding
->flags
);
365 return src
->forwarding
;
369 _Block_byref_release(const void *arg
)
371 struct Block_byref
*byref
= (struct Block_byref
*)arg
;
373 // dereference the forwarding pointer since the compiler isn't doing this anymore (ever?)
374 byref
= byref
->forwarding
;
376 if (byref
->flags
& BLOCK_BYREF_NEEDS_FREE
) {
377 __assert_only
int32_t refcount
= byref
->flags
& BLOCK_REFCOUNT_MASK
;
379 if (latching_decr_int_should_deallocate(&byref
->flags
)) {
380 if (byref
->flags
& BLOCK_BYREF_HAS_COPY_DISPOSE
) {
381 struct Block_byref_2
*byref2
= (struct Block_byref_2
*)(byref
+ 1);
382 (*byref2
->byref_destroy
)(byref
);
390 /************************************************************
393 * _Block_copy, _Block_release, and (old) _Block_destroy
395 ***********************************************************/
402 // API entry point to release a copied Block
404 _Block_release(const void *arg
)
406 struct Block_layout
*aBlock
= (struct Block_layout
*)arg
;
410 if (aBlock
->flags
& BLOCK_IS_GLOBAL
) {
413 if (!(aBlock
->flags
& BLOCK_NEEDS_FREE
)) {
417 if (latching_decr_int_should_deallocate(&aBlock
->flags
)) {
418 _Block_call_dispose_helper(aBlock
);
419 _Block_destructInstance(aBlock
);
425 _Block_tryRetain(const void *arg
)
427 struct Block_layout
*aBlock
= (struct Block_layout
*)arg
;
428 return latching_incr_int_not_deallocating(&aBlock
->flags
);
432 _Block_isDeallocating(const void *arg
)
434 struct Block_layout
*aBlock
= (struct Block_layout
*)arg
;
435 return (aBlock
->flags
& BLOCK_DEALLOCATING
) != 0;
439 /************************************************************
441 * SPI used by other layers
443 ***********************************************************/
446 Block_size(void *aBlock
)
448 auto *layout
= (Block_layout
*)aBlock
;
449 void *desc
= _Block_get_descriptor(layout
);
450 if (layout
->flags
& BLOCK_SMALL_DESCRIPTOR
) {
451 return ((Block_descriptor_small
*)desc
)->size
;
453 return ((Block_descriptor_1
*)desc
)->size
;
457 _Block_use_stret(void *aBlock
)
459 struct Block_layout
*layout
= (struct Block_layout
*)aBlock
;
461 int requiredFlags
= BLOCK_HAS_SIGNATURE
| BLOCK_USE_STRET
;
462 return (layout
->flags
& requiredFlags
) == requiredFlags
;
465 // Checks for a valid signature, not merely the BLOCK_HAS_SIGNATURE bit.
467 _Block_has_signature(void *aBlock
)
469 return _Block_signature(aBlock
) ? true : false;
473 _Block_signature(void *aBlock
)
475 struct Block_layout
*layout
= (struct Block_layout
*)aBlock
;
476 if (!(layout
->flags
& BLOCK_HAS_SIGNATURE
)) {
480 if (layout
->flags
& BLOCK_SMALL_DESCRIPTOR
) {
481 auto *bds
= (Block_descriptor_small
*)_Block_get_descriptor(layout
);
482 return unwrap_relative_pointer
<const char>(bds
->signature
);
485 struct Block_descriptor_3
*desc3
= _Block_descriptor_3(layout
);
486 return desc3
->signature
;
490 _Block_layout(void *aBlock
)
492 // Don't return extended layout to callers expecting old GC layout
493 Block_layout
*layout
= (Block_layout
*)aBlock
;
494 if ((layout
->flags
& BLOCK_HAS_EXTENDED_LAYOUT
) ||
495 !(layout
->flags
& BLOCK_HAS_SIGNATURE
)) {
499 if (layout
->flags
& BLOCK_SMALL_DESCRIPTOR
) {
500 auto *bds
= (Block_descriptor_small
*)_Block_get_descriptor(layout
);
501 return unwrap_relative_pointer
<const char>(bds
->layout
);
504 Block_descriptor_3
*desc
= _Block_descriptor_3(layout
);
509 _Block_extended_layout(void *aBlock
)
511 // Don't return old GC layout to callers expecting extended layout
512 Block_layout
*layout
= (Block_layout
*)aBlock
;
513 if (!(layout
->flags
& BLOCK_HAS_EXTENDED_LAYOUT
) ||
514 !(layout
->flags
& BLOCK_HAS_SIGNATURE
)) {
518 const char *extLayout
;
519 if (layout
->flags
& BLOCK_SMALL_DESCRIPTOR
) {
520 auto *bds
= (Block_descriptor_small
*)_Block_get_descriptor(layout
);
521 if (layout
->flags
& BLOCK_INLINE_LAYOUT_STRING
) {
522 extLayout
= (const char *)(uintptr_t)bds
->layout
;
524 extLayout
= unwrap_relative_pointer
<const char>(bds
->layout
);
527 Block_descriptor_3
*desc3
= _Block_descriptor_3(layout
);
528 extLayout
= desc3
->layout
;
531 // Return empty string (all non-object bytes) instead of NULL
532 // so callers can distinguish "empty layout" from "no layout".
540 #pragma mark Compiler SPI entry points
544 /*******************************************************
546 * Entry points used by the compiler - the real API!
549 * A Block can reference four different kinds of things that require help when the Block is copied to the heap.
550 * 1) C++ stack based objects
551 * 2) References to Objective-C objects
553 * 4) __block variables
555 * 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.
557 * The flags parameter of _Block_object_assign and _Block_object_dispose is set to
558 * BLOCK_FIELD_IS_OBJECT (3), for the case of an Objective-C Object,
559 * BLOCK_FIELD_IS_BLOCK (7), for the case of another Block, and
560 * BLOCK_FIELD_IS_BYREF (8), for the case of a __block variable.
561 * If the __block variable is marked weak the compiler also or's in BLOCK_FIELD_IS_WEAK (16)
563 * So the Block copy/dispose helpers should only ever generate the four flag values of 3, 7, 8, and 24.
565 * 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.
567 * 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:
568 * __block id 128+3 (0x83)
569 * __block (^Block) 128+7 (0x87)
570 * __weak __block id 128+3+16 (0x93)
571 * __weak __block (^Block) 128+7+16 (0x97)
574 ********************************************************/
577 // When Blocks or Block_byrefs hold objects then their copy routine helpers use this entry point
578 // to do the assignment.
581 _Block_object_assign(void *destArg
, const void *object
, const int flags
)
583 const void **dest
= (const void **)destArg
;
584 switch (os_assumes(flags
& BLOCK_ALL_COPY_DISPOSE_FLAGS
)) {
585 case BLOCK_FIELD_IS_OBJECT
:
588 * [^{ object; } copy];
591 _Block_retain_object(object
);
595 case BLOCK_FIELD_IS_BLOCK
:
597 * void (^object)(void) = ...;
598 * [^{ object; } copy];
601 *dest
= _Block_copy(object
);
604 case BLOCK_FIELD_IS_BYREF
| BLOCK_FIELD_IS_WEAK
:
605 case BLOCK_FIELD_IS_BYREF
:
607 * // copy the onstack __block container to the heap
608 * // Note this __weak is old GC-weak/MRC-unretained.
609 * // ARC-style __weak is handled by the copy helper directly.
611 * __weak __block ... x;
615 *dest
= _Block_byref_copy(object
);
618 case BLOCK_BYREF_CALLER
| BLOCK_FIELD_IS_OBJECT
:
619 case BLOCK_BYREF_CALLER
| BLOCK_FIELD_IS_BLOCK
:
621 * // copy the actual field held in the __block container
622 * // Note this is MRC unretained __block only.
623 * // ARC retained __block is handled by the copy helper directly.
625 * __block void (^object)(void);
626 * [^{ object; } copy];
632 case BLOCK_BYREF_CALLER
| BLOCK_FIELD_IS_OBJECT
| BLOCK_FIELD_IS_WEAK
:
633 case BLOCK_BYREF_CALLER
| BLOCK_FIELD_IS_BLOCK
| BLOCK_FIELD_IS_WEAK
:
635 * // copy the actual field held in the __block container
636 * // Note this __weak is old GC-weak/MRC-unretained.
637 * // ARC-style __weak is handled by the copy helper directly.
638 * __weak __block id object;
639 * __weak __block void (^object)(void);
640 * [^{ object; } copy];
651 // When Blocks or Block_byrefs hold objects their destroy helper routines call this entry point
652 // to help dispose of the contents
654 _Block_object_dispose(const void *object
, const int flags
)
656 switch (os_assumes(flags
& BLOCK_ALL_COPY_DISPOSE_FLAGS
)) {
657 case BLOCK_FIELD_IS_BYREF
| BLOCK_FIELD_IS_WEAK
:
658 case BLOCK_FIELD_IS_BYREF
:
659 // get rid of the __block data structure held in a Block
660 _Block_byref_release(object
);
662 case BLOCK_FIELD_IS_BLOCK
:
663 _Block_release(object
);
665 case BLOCK_FIELD_IS_OBJECT
:
666 _Block_release_object(object
);
668 case BLOCK_BYREF_CALLER
| BLOCK_FIELD_IS_OBJECT
:
669 case BLOCK_BYREF_CALLER
| BLOCK_FIELD_IS_BLOCK
:
670 case BLOCK_BYREF_CALLER
| BLOCK_FIELD_IS_OBJECT
| BLOCK_FIELD_IS_WEAK
:
671 case BLOCK_BYREF_CALLER
| BLOCK_FIELD_IS_BLOCK
| BLOCK_FIELD_IS_WEAK
:
679 // Workaround for <rdar://26015603> dylib with no __DATA segment fails to rebase
680 __attribute__((used
))
681 static int let_there_be_data
= 42;