]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-cache.mm
objc4-750.tar.gz
[apple/objc4.git] / runtime / objc-cache.mm
1 /*
2 * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /***********************************************************************
25 * objc-cache.m
26 * Method cache management
27 * Cache flushing
28 * Cache garbage collection
29 * Cache instrumentation
30 * Dedicated allocator for large caches
31 **********************************************************************/
32
33
34 /***********************************************************************
35 * Method cache locking (GrP 2001-1-14)
36 *
37 * For speed, objc_msgSend does not acquire any locks when it reads
38 * method caches. Instead, all cache changes are performed so that any
39 * objc_msgSend running concurrently with the cache mutator will not
40 * crash or hang or get an incorrect result from the cache.
41 *
42 * When cache memory becomes unused (e.g. the old cache after cache
43 * expansion), it is not immediately freed, because a concurrent
44 * objc_msgSend could still be using it. Instead, the memory is
45 * disconnected from the data structures and placed on a garbage list.
46 * The memory is now only accessible to instances of objc_msgSend that
47 * were running when the memory was disconnected; any further calls to
48 * objc_msgSend will not see the garbage memory because the other data
49 * structures don't point to it anymore. The collecting_in_critical
50 * function checks the PC of all threads and returns FALSE when all threads
51 * are found to be outside objc_msgSend. This means any call to objc_msgSend
52 * that could have had access to the garbage has finished or moved past the
53 * cache lookup stage, so it is safe to free the memory.
54 *
55 * All functions that modify cache data or structures must acquire the
56 * cacheUpdateLock to prevent interference from concurrent modifications.
57 * The function that frees cache garbage must acquire the cacheUpdateLock
58 * and use collecting_in_critical() to flush out cache readers.
59 * The cacheUpdateLock is also used to protect the custom allocator used
60 * for large method cache blocks.
61 *
62 * Cache readers (PC-checked by collecting_in_critical())
63 * objc_msgSend*
64 * cache_getImp
65 *
66 * Cache writers (hold cacheUpdateLock while reading or writing; not PC-checked)
67 * cache_fill (acquires lock)
68 * cache_expand (only called from cache_fill)
69 * cache_create (only called from cache_expand)
70 * bcopy (only called from instrumented cache_expand)
71 * flush_caches (acquires lock)
72 * cache_flush (only called from cache_fill and flush_caches)
73 * cache_collect_free (only called from cache_expand and cache_flush)
74 *
75 * UNPROTECTED cache readers (NOT thread-safe; used for debug info only)
76 * cache_print
77 * _class_printMethodCaches
78 * _class_printDuplicateCacheEntries
79 * _class_printMethodCacheStatistics
80 *
81 ***********************************************************************/
82
83
84 #if __OBJC2__
85
86 #include "objc-private.h"
87 #include "objc-cache.h"
88
89
90 /* Initial cache bucket count. INIT_CACHE_SIZE must be a power of two. */
91 enum {
92 INIT_CACHE_SIZE_LOG2 = 2,
93 INIT_CACHE_SIZE = (1 << INIT_CACHE_SIZE_LOG2)
94 };
95
96 static void cache_collect_free(struct bucket_t *data, mask_t capacity);
97 static int _collecting_in_critical(void);
98 static void _garbage_make_room(void);
99
100
101 /***********************************************************************
102 * Cache statistics for OBJC_PRINT_CACHE_SETUP
103 **********************************************************************/
104 static unsigned int cache_counts[16];
105 static size_t cache_allocations;
106 static size_t cache_collections;
107
108 static void recordNewCache(mask_t capacity)
109 {
110 size_t bucket = log2u(capacity);
111 if (bucket < countof(cache_counts)) {
112 cache_counts[bucket]++;
113 }
114 cache_allocations++;
115 }
116
117 static void recordDeadCache(mask_t capacity)
118 {
119 size_t bucket = log2u(capacity);
120 if (bucket < countof(cache_counts)) {
121 cache_counts[bucket]--;
122 }
123 }
124
125 /***********************************************************************
126 * Pointers used by compiled class objects
127 * These use asm to avoid conflicts with the compiler's internal declarations
128 **********************************************************************/
129
130 // EMPTY_BYTES includes space for a cache end marker bucket.
131 // This end marker doesn't actually have the wrap-around pointer
132 // because cache scans always find an empty bucket before they might wrap.
133 // 1024 buckets is fairly common.
134 #if DEBUG
135 // Use a smaller size to exercise heap-allocated empty caches.
136 # define EMPTY_BYTES ((8+1)*16)
137 #else
138 # define EMPTY_BYTES ((1024+1)*16)
139 #endif
140
141 #define stringize(x) #x
142 #define stringize2(x) stringize(x)
143
144 // "cache" is cache->buckets; "vtable" is cache->mask/occupied
145 // hack to avoid conflicts with compiler's internal declaration
146 asm("\n .section __TEXT,__const"
147 "\n .globl __objc_empty_vtable"
148 "\n .set __objc_empty_vtable, 0"
149 "\n .globl __objc_empty_cache"
150 "\n .align 3"
151 "\n __objc_empty_cache: .space " stringize2(EMPTY_BYTES)
152 );
153
154
155 #if __arm__ || __x86_64__ || __i386__
156 // objc_msgSend has few registers available.
157 // Cache scan increments and wraps at special end-marking bucket.
158 #define CACHE_END_MARKER 1
159 static inline mask_t cache_next(mask_t i, mask_t mask) {
160 return (i+1) & mask;
161 }
162
163 #elif __arm64__
164 // objc_msgSend has lots of registers available.
165 // Cache scan decrements. No end marker needed.
166 #define CACHE_END_MARKER 0
167 static inline mask_t cache_next(mask_t i, mask_t mask) {
168 return i ? i-1 : mask;
169 }
170
171 #else
172 #error unknown architecture
173 #endif
174
175
176 // copied from dispatch_atomic_maximally_synchronizing_barrier
177 // fixme verify that this barrier hack does in fact work here
178 #if __x86_64__
179 #define mega_barrier() \
180 do { unsigned long _clbr; __asm__ __volatile__( \
181 "cpuid" \
182 : "=a" (_clbr) : "0" (0) : "rbx", "rcx", "rdx", "cc", "memory" \
183 ); } while(0)
184
185 #elif __i386__
186 #define mega_barrier() \
187 do { unsigned long _clbr; __asm__ __volatile__( \
188 "cpuid" \
189 : "=a" (_clbr) : "0" (0) : "ebx", "ecx", "edx", "cc", "memory" \
190 ); } while(0)
191
192 #elif __arm__ || __arm64__
193 #define mega_barrier() \
194 __asm__ __volatile__( \
195 "dsb ish" \
196 : : : "memory")
197
198 #else
199 #error unknown architecture
200 #endif
201
202 #if __arm64__
203
204 // Pointer-size register prefix for inline asm
205 # if __LP64__
206 # define p "x" // true arm64
207 # else
208 # define p "w" // arm64_32
209 # endif
210
211 // Use atomic double-word instructions to update cache entries.
212 // This requires cache buckets not cross cache line boundaries.
213 static ALWAYS_INLINE void
214 stp(uintptr_t onep, uintptr_t twop, void *destp)
215 {
216 __asm__ ("stp %" p "[one], %" p "[two], [%x[dest]]"
217 : "=m" (((uintptr_t *)(destp))[0]),
218 "=m" (((uintptr_t *)(destp))[1])
219 : [one] "r" (onep),
220 [two] "r" (twop),
221 [dest] "r" (destp)
222 : /* no clobbers */
223 );
224 }
225
226 static ALWAYS_INLINE void __unused
227 ldp(uintptr_t& onep, uintptr_t& twop, const void *srcp)
228 {
229 __asm__ ("ldp %" p "[one], %" p "[two], [%x[src]]"
230 : [one] "=r" (onep),
231 [two] "=r" (twop)
232 : "m" (((const uintptr_t *)(srcp))[0]),
233 "m" (((const uintptr_t *)(srcp))[1]),
234 [src] "r" (srcp)
235 : /* no clobbers */
236 );
237 }
238
239 #undef p
240 #endif
241
242
243 // Class points to cache. SEL is key. Cache buckets store SEL+IMP.
244 // Caches are never built in the dyld shared cache.
245
246 static inline mask_t cache_hash(cache_key_t key, mask_t mask)
247 {
248 return (mask_t)(key & mask);
249 }
250
251 cache_t *getCache(Class cls)
252 {
253 assert(cls);
254 return &cls->cache;
255 }
256
257 cache_key_t getKey(SEL sel)
258 {
259 assert(sel);
260 return (cache_key_t)sel;
261 }
262
263 #if __arm64__
264
265 void bucket_t::set(cache_key_t newKey, IMP newImp)
266 {
267 assert(_key == 0 || _key == newKey);
268
269 static_assert(offsetof(bucket_t,_imp) == 0 && offsetof(bucket_t,_key) == sizeof(void *),
270 "bucket_t doesn't match arm64 bucket_t::set()");
271
272 #if __has_feature(ptrauth_calls)
273 // Authenticate as a C function pointer and re-sign for the cache bucket.
274 uintptr_t signedImp = _imp.prepareWrite(newImp);
275 #else
276 // No function pointer signing.
277 uintptr_t signedImp = (uintptr_t)newImp;
278 #endif
279
280 // Write to the bucket.
281 // LDP/STP guarantees that all observers get
282 // either imp/key or newImp/newKey
283 stp(signedImp, newKey, this);
284 }
285
286 #else
287
288 void bucket_t::set(cache_key_t newKey, IMP newImp)
289 {
290 assert(_key == 0 || _key == newKey);
291
292 // objc_msgSend uses key and imp with no locks.
293 // It is safe for objc_msgSend to see new imp but NULL key
294 // (It will get a cache miss but not dispatch to the wrong place.)
295 // It is unsafe for objc_msgSend to see old imp and new key.
296 // Therefore we write new imp, wait a lot, then write new key.
297
298 _imp = newImp;
299
300 if (_key != newKey) {
301 mega_barrier();
302 _key = newKey;
303 }
304 }
305
306 #endif
307
308 void cache_t::setBucketsAndMask(struct bucket_t *newBuckets, mask_t newMask)
309 {
310 // objc_msgSend uses mask and buckets with no locks.
311 // It is safe for objc_msgSend to see new buckets but old mask.
312 // (It will get a cache miss but not overrun the buckets' bounds).
313 // It is unsafe for objc_msgSend to see old buckets and new mask.
314 // Therefore we write new buckets, wait a lot, then write new mask.
315 // objc_msgSend reads mask first, then buckets.
316
317 // ensure other threads see buckets contents before buckets pointer
318 mega_barrier();
319
320 _buckets = newBuckets;
321
322 // ensure other threads see new buckets before new mask
323 mega_barrier();
324
325 _mask = newMask;
326 _occupied = 0;
327 }
328
329
330 struct bucket_t *cache_t::buckets()
331 {
332 return _buckets;
333 }
334
335 mask_t cache_t::mask()
336 {
337 return _mask;
338 }
339
340 mask_t cache_t::occupied()
341 {
342 return _occupied;
343 }
344
345 void cache_t::incrementOccupied()
346 {
347 _occupied++;
348 }
349
350 void cache_t::initializeToEmpty()
351 {
352 bzero(this, sizeof(*this));
353 _buckets = (bucket_t *)&_objc_empty_cache;
354 }
355
356
357 mask_t cache_t::capacity()
358 {
359 return mask() ? mask()+1 : 0;
360 }
361
362
363 #if CACHE_END_MARKER
364
365 size_t cache_t::bytesForCapacity(uint32_t cap)
366 {
367 // fixme put end marker inline when capacity+1 malloc is inefficient
368 return sizeof(bucket_t) * (cap + 1);
369 }
370
371 bucket_t *cache_t::endMarker(struct bucket_t *b, uint32_t cap)
372 {
373 // bytesForCapacity() chooses whether the end marker is inline or not
374 return (bucket_t *)((uintptr_t)b + bytesForCapacity(cap)) - 1;
375 }
376
377 bucket_t *allocateBuckets(mask_t newCapacity)
378 {
379 // Allocate one extra bucket to mark the end of the list.
380 // This can't overflow mask_t because newCapacity is a power of 2.
381 // fixme instead put the end mark inline when +1 is malloc-inefficient
382 bucket_t *newBuckets = (bucket_t *)
383 calloc(cache_t::bytesForCapacity(newCapacity), 1);
384
385 bucket_t *end = cache_t::endMarker(newBuckets, newCapacity);
386
387 #if __arm__
388 // End marker's key is 1 and imp points BEFORE the first bucket.
389 // This saves an instruction in objc_msgSend.
390 end->setKey((cache_key_t)(uintptr_t)1);
391 end->setImp((IMP)(newBuckets - 1));
392 #else
393 // End marker's key is 1 and imp points to the first bucket.
394 end->setKey((cache_key_t)(uintptr_t)1);
395 end->setImp((IMP)newBuckets);
396 #endif
397
398 if (PrintCaches) recordNewCache(newCapacity);
399
400 return newBuckets;
401 }
402
403 #else
404
405 size_t cache_t::bytesForCapacity(uint32_t cap)
406 {
407 return sizeof(bucket_t) * cap;
408 }
409
410 bucket_t *allocateBuckets(mask_t newCapacity)
411 {
412 if (PrintCaches) recordNewCache(newCapacity);
413
414 return (bucket_t *)calloc(cache_t::bytesForCapacity(newCapacity), 1);
415 }
416
417 #endif
418
419
420 bucket_t *emptyBucketsForCapacity(mask_t capacity, bool allocate = true)
421 {
422 cacheUpdateLock.assertLocked();
423
424 size_t bytes = cache_t::bytesForCapacity(capacity);
425
426 // Use _objc_empty_cache if the buckets is small enough.
427 if (bytes <= EMPTY_BYTES) {
428 return (bucket_t *)&_objc_empty_cache;
429 }
430
431 // Use shared empty buckets allocated on the heap.
432 static bucket_t **emptyBucketsList = nil;
433 static mask_t emptyBucketsListCount = 0;
434
435 mask_t index = log2u(capacity);
436
437 if (index >= emptyBucketsListCount) {
438 if (!allocate) return nil;
439
440 mask_t newListCount = index + 1;
441 bucket_t *newBuckets = (bucket_t *)calloc(bytes, 1);
442 emptyBucketsList = (bucket_t**)
443 realloc(emptyBucketsList, newListCount * sizeof(bucket_t *));
444 // Share newBuckets for every un-allocated size smaller than index.
445 // The array is therefore always fully populated.
446 for (mask_t i = emptyBucketsListCount; i < newListCount; i++) {
447 emptyBucketsList[i] = newBuckets;
448 }
449 emptyBucketsListCount = newListCount;
450
451 if (PrintCaches) {
452 _objc_inform("CACHES: new empty buckets at %p (capacity %zu)",
453 newBuckets, (size_t)capacity);
454 }
455 }
456
457 return emptyBucketsList[index];
458 }
459
460
461 bool cache_t::isConstantEmptyCache()
462 {
463 return
464 occupied() == 0 &&
465 buckets() == emptyBucketsForCapacity(capacity(), false);
466 }
467
468 bool cache_t::canBeFreed()
469 {
470 return !isConstantEmptyCache();
471 }
472
473
474 void cache_t::reallocate(mask_t oldCapacity, mask_t newCapacity)
475 {
476 bool freeOld = canBeFreed();
477
478 bucket_t *oldBuckets = buckets();
479 bucket_t *newBuckets = allocateBuckets(newCapacity);
480
481 // Cache's old contents are not propagated.
482 // This is thought to save cache memory at the cost of extra cache fills.
483 // fixme re-measure this
484
485 assert(newCapacity > 0);
486 assert((uintptr_t)(mask_t)(newCapacity-1) == newCapacity-1);
487
488 setBucketsAndMask(newBuckets, newCapacity - 1);
489
490 if (freeOld) {
491 cache_collect_free(oldBuckets, oldCapacity);
492 cache_collect(false);
493 }
494 }
495
496
497 void cache_t::bad_cache(id receiver, SEL sel, Class isa)
498 {
499 // Log in separate steps in case the logging itself causes a crash.
500 _objc_inform_now_and_on_crash
501 ("Method cache corrupted. This may be a message to an "
502 "invalid object, or a memory error somewhere else.");
503 cache_t *cache = &isa->cache;
504 _objc_inform_now_and_on_crash
505 ("%s %p, SEL %p, isa %p, cache %p, buckets %p, "
506 "mask 0x%x, occupied 0x%x",
507 receiver ? "receiver" : "unused", receiver,
508 sel, isa, cache, cache->_buckets,
509 cache->_mask, cache->_occupied);
510 _objc_inform_now_and_on_crash
511 ("%s %zu bytes, buckets %zu bytes",
512 receiver ? "receiver" : "unused", malloc_size(receiver),
513 malloc_size(cache->_buckets));
514 _objc_inform_now_and_on_crash
515 ("selector '%s'", sel_getName(sel));
516 _objc_inform_now_and_on_crash
517 ("isa '%s'", isa->nameForLogging());
518 _objc_fatal
519 ("Method cache corrupted. This may be a message to an "
520 "invalid object, or a memory error somewhere else.");
521 }
522
523
524 bucket_t * cache_t::find(cache_key_t k, id receiver)
525 {
526 assert(k != 0);
527
528 bucket_t *b = buckets();
529 mask_t m = mask();
530 mask_t begin = cache_hash(k, m);
531 mask_t i = begin;
532 do {
533 if (b[i].key() == 0 || b[i].key() == k) {
534 return &b[i];
535 }
536 } while ((i = cache_next(i, m)) != begin);
537
538 // hack
539 Class cls = (Class)((uintptr_t)this - offsetof(objc_class, cache));
540 cache_t::bad_cache(receiver, (SEL)k, cls);
541 }
542
543
544 void cache_t::expand()
545 {
546 cacheUpdateLock.assertLocked();
547
548 uint32_t oldCapacity = capacity();
549 uint32_t newCapacity = oldCapacity ? oldCapacity*2 : INIT_CACHE_SIZE;
550
551 if ((uint32_t)(mask_t)newCapacity != newCapacity) {
552 // mask overflow - can't grow further
553 // fixme this wastes one bit of mask
554 newCapacity = oldCapacity;
555 }
556
557 reallocate(oldCapacity, newCapacity);
558 }
559
560
561 static void cache_fill_nolock(Class cls, SEL sel, IMP imp, id receiver)
562 {
563 cacheUpdateLock.assertLocked();
564
565 // Never cache before +initialize is done
566 if (!cls->isInitialized()) return;
567
568 // Make sure the entry wasn't added to the cache by some other thread
569 // before we grabbed the cacheUpdateLock.
570 if (cache_getImp(cls, sel)) return;
571
572 cache_t *cache = getCache(cls);
573 cache_key_t key = getKey(sel);
574
575 // Use the cache as-is if it is less than 3/4 full
576 mask_t newOccupied = cache->occupied() + 1;
577 mask_t capacity = cache->capacity();
578 if (cache->isConstantEmptyCache()) {
579 // Cache is read-only. Replace it.
580 cache->reallocate(capacity, capacity ?: INIT_CACHE_SIZE);
581 }
582 else if (newOccupied <= capacity / 4 * 3) {
583 // Cache is less than 3/4 full. Use it as-is.
584 }
585 else {
586 // Cache is too full. Expand it.
587 cache->expand();
588 }
589
590 // Scan for the first unused slot and insert there.
591 // There is guaranteed to be an empty slot because the
592 // minimum size is 4 and we resized at 3/4 full.
593 bucket_t *bucket = cache->find(key, receiver);
594 if (bucket->key() == 0) cache->incrementOccupied();
595 bucket->set(key, imp);
596 }
597
598 void cache_fill(Class cls, SEL sel, IMP imp, id receiver)
599 {
600 #if !DEBUG_TASK_THREADS
601 mutex_locker_t lock(cacheUpdateLock);
602 cache_fill_nolock(cls, sel, imp, receiver);
603 #else
604 _collecting_in_critical();
605 return;
606 #endif
607 }
608
609
610 // Reset this entire cache to the uncached lookup by reallocating it.
611 // This must not shrink the cache - that breaks the lock-free scheme.
612 void cache_erase_nolock(Class cls)
613 {
614 cacheUpdateLock.assertLocked();
615
616 cache_t *cache = getCache(cls);
617
618 mask_t capacity = cache->capacity();
619 if (capacity > 0 && cache->occupied() > 0) {
620 auto oldBuckets = cache->buckets();
621 auto buckets = emptyBucketsForCapacity(capacity);
622 cache->setBucketsAndMask(buckets, capacity - 1); // also clears occupied
623
624 cache_collect_free(oldBuckets, capacity);
625 cache_collect(false);
626 }
627 }
628
629
630 void cache_delete(Class cls)
631 {
632 mutex_locker_t lock(cacheUpdateLock);
633 if (cls->cache.canBeFreed()) {
634 if (PrintCaches) recordDeadCache(cls->cache.capacity());
635 free(cls->cache.buckets());
636 }
637 }
638
639
640 /***********************************************************************
641 * cache collection.
642 **********************************************************************/
643
644 #if !TARGET_OS_WIN32
645
646 // A sentinel (magic value) to report bad thread_get_state status.
647 // Must not be a valid PC.
648 // Must not be zero - thread_get_state() on a new thread returns PC == 0.
649 #define PC_SENTINEL 1
650
651 static uintptr_t _get_pc_for_thread(thread_t thread)
652 #if defined(__i386__)
653 {
654 i386_thread_state_t state;
655 unsigned int count = i386_THREAD_STATE_COUNT;
656 kern_return_t okay = thread_get_state (thread, i386_THREAD_STATE, (thread_state_t)&state, &count);
657 return (okay == KERN_SUCCESS) ? state.__eip : PC_SENTINEL;
658 }
659 #elif defined(__x86_64__)
660 {
661 x86_thread_state64_t state;
662 unsigned int count = x86_THREAD_STATE64_COUNT;
663 kern_return_t okay = thread_get_state (thread, x86_THREAD_STATE64, (thread_state_t)&state, &count);
664 return (okay == KERN_SUCCESS) ? state.__rip : PC_SENTINEL;
665 }
666 #elif defined(__arm__)
667 {
668 arm_thread_state_t state;
669 unsigned int count = ARM_THREAD_STATE_COUNT;
670 kern_return_t okay = thread_get_state (thread, ARM_THREAD_STATE, (thread_state_t)&state, &count);
671 return (okay == KERN_SUCCESS) ? state.__pc : PC_SENTINEL;
672 }
673 #elif defined(__arm64__)
674 {
675 arm_thread_state64_t state;
676 unsigned int count = ARM_THREAD_STATE64_COUNT;
677 kern_return_t okay = thread_get_state (thread, ARM_THREAD_STATE64, (thread_state_t)&state, &count);
678 return (okay == KERN_SUCCESS) ? arm_thread_state64_get_pc(state) : PC_SENTINEL;
679 }
680 #else
681 {
682 #error _get_pc_for_thread () not implemented for this architecture
683 }
684 #endif
685
686 #endif
687
688 /***********************************************************************
689 * _collecting_in_critical.
690 * Returns TRUE if some thread is currently executing a cache-reading
691 * function. Collection of cache garbage is not allowed when a cache-
692 * reading function is in progress because it might still be using
693 * the garbage memory.
694 **********************************************************************/
695 extern "C" uintptr_t objc_entryPoints[];
696 extern "C" uintptr_t objc_exitPoints[];
697
698 static int _collecting_in_critical(void)
699 {
700 #if TARGET_OS_WIN32
701 return TRUE;
702 #else
703 thread_act_port_array_t threads;
704 unsigned number;
705 unsigned count;
706 kern_return_t ret;
707 int result;
708
709 mach_port_t mythread = pthread_mach_thread_np(pthread_self());
710
711 // Get a list of all the threads in the current task
712 #if !DEBUG_TASK_THREADS
713 ret = task_threads(mach_task_self(), &threads, &number);
714 #else
715 ret = objc_task_threads(mach_task_self(), &threads, &number);
716 #endif
717
718 if (ret != KERN_SUCCESS) {
719 // See DEBUG_TASK_THREADS below to help debug this.
720 _objc_fatal("task_threads failed (result 0x%x)\n", ret);
721 }
722
723 // Check whether any thread is in the cache lookup code
724 result = FALSE;
725 for (count = 0; count < number; count++)
726 {
727 int region;
728 uintptr_t pc;
729
730 // Don't bother checking ourselves
731 if (threads[count] == mythread)
732 continue;
733
734 // Find out where thread is executing
735 pc = _get_pc_for_thread (threads[count]);
736
737 // Check for bad status, and if so, assume the worse (can't collect)
738 if (pc == PC_SENTINEL)
739 {
740 result = TRUE;
741 goto done;
742 }
743
744 // Check whether it is in the cache lookup code
745 for (region = 0; objc_entryPoints[region] != 0; region++)
746 {
747 if ((pc >= objc_entryPoints[region]) &&
748 (pc <= objc_exitPoints[region]))
749 {
750 result = TRUE;
751 goto done;
752 }
753 }
754 }
755
756 done:
757 // Deallocate the port rights for the threads
758 for (count = 0; count < number; count++) {
759 mach_port_deallocate(mach_task_self (), threads[count]);
760 }
761
762 // Deallocate the thread list
763 vm_deallocate (mach_task_self (), (vm_address_t) threads, sizeof(threads[0]) * number);
764
765 // Return our finding
766 return result;
767 #endif
768 }
769
770
771 /***********************************************************************
772 * _garbage_make_room. Ensure that there is enough room for at least
773 * one more ref in the garbage.
774 **********************************************************************/
775
776 // amount of memory represented by all refs in the garbage
777 static size_t garbage_byte_size = 0;
778
779 // do not empty the garbage until garbage_byte_size gets at least this big
780 static size_t garbage_threshold = 32*1024;
781
782 // table of refs to free
783 static bucket_t **garbage_refs = 0;
784
785 // current number of refs in garbage_refs
786 static size_t garbage_count = 0;
787
788 // capacity of current garbage_refs
789 static size_t garbage_max = 0;
790
791 // capacity of initial garbage_refs
792 enum {
793 INIT_GARBAGE_COUNT = 128
794 };
795
796 static void _garbage_make_room(void)
797 {
798 static int first = 1;
799
800 // Create the collection table the first time it is needed
801 if (first)
802 {
803 first = 0;
804 garbage_refs = (bucket_t**)
805 malloc(INIT_GARBAGE_COUNT * sizeof(void *));
806 garbage_max = INIT_GARBAGE_COUNT;
807 }
808
809 // Double the table if it is full
810 else if (garbage_count == garbage_max)
811 {
812 garbage_refs = (bucket_t**)
813 realloc(garbage_refs, garbage_max * 2 * sizeof(void *));
814 garbage_max *= 2;
815 }
816 }
817
818
819 /***********************************************************************
820 * cache_collect_free. Add the specified malloc'd memory to the list
821 * of them to free at some later point.
822 * size is used for the collection threshold. It does not have to be
823 * precisely the block's size.
824 * Cache locks: cacheUpdateLock must be held by the caller.
825 **********************************************************************/
826 static void cache_collect_free(bucket_t *data, mask_t capacity)
827 {
828 cacheUpdateLock.assertLocked();
829
830 if (PrintCaches) recordDeadCache(capacity);
831
832 _garbage_make_room ();
833 garbage_byte_size += cache_t::bytesForCapacity(capacity);
834 garbage_refs[garbage_count++] = data;
835 }
836
837
838 /***********************************************************************
839 * cache_collect. Try to free accumulated dead caches.
840 * collectALot tries harder to free memory.
841 * Cache locks: cacheUpdateLock must be held by the caller.
842 **********************************************************************/
843 void cache_collect(bool collectALot)
844 {
845 cacheUpdateLock.assertLocked();
846
847 // Done if the garbage is not full
848 if (garbage_byte_size < garbage_threshold && !collectALot) {
849 return;
850 }
851
852 // Synchronize collection with objc_msgSend and other cache readers
853 if (!collectALot) {
854 if (_collecting_in_critical ()) {
855 // objc_msgSend (or other cache reader) is currently looking in
856 // the cache and might still be using some garbage.
857 if (PrintCaches) {
858 _objc_inform ("CACHES: not collecting; "
859 "objc_msgSend in progress");
860 }
861 return;
862 }
863 }
864 else {
865 // No excuses.
866 while (_collecting_in_critical())
867 ;
868 }
869
870 // No cache readers in progress - garbage is now deletable
871
872 // Log our progress
873 if (PrintCaches) {
874 cache_collections++;
875 _objc_inform ("CACHES: COLLECTING %zu bytes (%zu allocations, %zu collections)", garbage_byte_size, cache_allocations, cache_collections);
876 }
877
878 // Dispose all refs now in the garbage
879 // Erase each entry so debugging tools don't see stale pointers.
880 while (garbage_count--) {
881 auto dead = garbage_refs[garbage_count];
882 garbage_refs[garbage_count] = nil;
883 free(dead);
884 }
885
886 // Clear the garbage count and total size indicator
887 garbage_count = 0;
888 garbage_byte_size = 0;
889
890 if (PrintCaches) {
891 size_t i;
892 size_t total_count = 0;
893 size_t total_size = 0;
894
895 for (i = 0; i < countof(cache_counts); i++) {
896 int count = cache_counts[i];
897 int slots = 1 << i;
898 size_t size = count * slots * sizeof(bucket_t);
899
900 if (!count) continue;
901
902 _objc_inform("CACHES: %4d slots: %4d caches, %6zu bytes",
903 slots, count, size);
904
905 total_count += count;
906 total_size += size;
907 }
908
909 _objc_inform("CACHES: total: %4zu caches, %6zu bytes",
910 total_count, total_size);
911 }
912 }
913
914
915 /***********************************************************************
916 * objc_task_threads
917 * Replacement for task_threads(). Define DEBUG_TASK_THREADS to debug
918 * crashes when task_threads() is failing.
919 *
920 * A failure in task_threads() usually means somebody has botched their
921 * Mach or MIG traffic. For example, somebody's error handling was wrong
922 * and they left a message queued on the MIG reply port for task_threads()
923 * to trip over.
924 *
925 * The code below is a modified version of task_threads(). It logs
926 * the msgh_id of the reply message. The msgh_id can identify the sender
927 * of the message, which can help pinpoint the faulty code.
928 * DEBUG_TASK_THREADS also calls collecting_in_critical() during every
929 * message dispatch, which can increase reproducibility of bugs.
930 *
931 * This code can be regenerated by running
932 * `mig /usr/include/mach/task.defs`.
933 **********************************************************************/
934 #if DEBUG_TASK_THREADS
935
936 #include <mach/mach.h>
937 #include <mach/message.h>
938 #include <mach/mig.h>
939
940 #define __MIG_check__Reply__task_subsystem__ 1
941 #define mig_internal static inline
942 #define __DeclareSendRpc(a, b)
943 #define __BeforeSendRpc(a, b)
944 #define __AfterSendRpc(a, b)
945 #define msgh_request_port msgh_remote_port
946 #define msgh_reply_port msgh_local_port
947
948 #ifndef __MachMsgErrorWithTimeout
949 #define __MachMsgErrorWithTimeout(_R_) { \
950 switch (_R_) { \
951 case MACH_SEND_INVALID_DATA: \
952 case MACH_SEND_INVALID_DEST: \
953 case MACH_SEND_INVALID_HEADER: \
954 mig_put_reply_port(InP->Head.msgh_reply_port); \
955 break; \
956 case MACH_SEND_TIMED_OUT: \
957 case MACH_RCV_TIMED_OUT: \
958 default: \
959 mig_dealloc_reply_port(InP->Head.msgh_reply_port); \
960 } \
961 }
962 #endif /* __MachMsgErrorWithTimeout */
963
964 #ifndef __MachMsgErrorWithoutTimeout
965 #define __MachMsgErrorWithoutTimeout(_R_) { \
966 switch (_R_) { \
967 case MACH_SEND_INVALID_DATA: \
968 case MACH_SEND_INVALID_DEST: \
969 case MACH_SEND_INVALID_HEADER: \
970 mig_put_reply_port(InP->Head.msgh_reply_port); \
971 break; \
972 default: \
973 mig_dealloc_reply_port(InP->Head.msgh_reply_port); \
974 } \
975 }
976 #endif /* __MachMsgErrorWithoutTimeout */
977
978
979 #if ( __MigTypeCheck )
980 #if __MIG_check__Reply__task_subsystem__
981 #if !defined(__MIG_check__Reply__task_threads_t__defined)
982 #define __MIG_check__Reply__task_threads_t__defined
983
984 mig_internal kern_return_t __MIG_check__Reply__task_threads_t(__Reply__task_threads_t *Out0P)
985 {
986
987 typedef __Reply__task_threads_t __Reply;
988 boolean_t msgh_simple;
989 #if __MigTypeCheck
990 unsigned int msgh_size;
991 #endif /* __MigTypeCheck */
992 if (Out0P->Head.msgh_id != 3502) {
993 if (Out0P->Head.msgh_id == MACH_NOTIFY_SEND_ONCE)
994 { return MIG_SERVER_DIED; }
995 else
996 { return MIG_REPLY_MISMATCH; }
997 }
998
999 msgh_simple = !(Out0P->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX);
1000 #if __MigTypeCheck
1001 msgh_size = Out0P->Head.msgh_size;
1002
1003 if ((msgh_simple || Out0P->msgh_body.msgh_descriptor_count != 1 ||
1004 msgh_size != (mach_msg_size_t)sizeof(__Reply)) &&
1005 (!msgh_simple || msgh_size != (mach_msg_size_t)sizeof(mig_reply_error_t) ||
1006 ((mig_reply_error_t *)Out0P)->RetCode == KERN_SUCCESS))
1007 { return MIG_TYPE_ERROR ; }
1008 #endif /* __MigTypeCheck */
1009
1010 if (msgh_simple) {
1011 return ((mig_reply_error_t *)Out0P)->RetCode;
1012 }
1013
1014 #if __MigTypeCheck
1015 if (Out0P->act_list.type != MACH_MSG_OOL_PORTS_DESCRIPTOR ||
1016 Out0P->act_list.disposition != 17) {
1017 return MIG_TYPE_ERROR;
1018 }
1019 #endif /* __MigTypeCheck */
1020
1021 return MACH_MSG_SUCCESS;
1022 }
1023 #endif /* !defined(__MIG_check__Reply__task_threads_t__defined) */
1024 #endif /* __MIG_check__Reply__task_subsystem__ */
1025 #endif /* ( __MigTypeCheck ) */
1026
1027
1028 /* Routine task_threads */
1029 static kern_return_t objc_task_threads
1030 (
1031 task_t target_task,
1032 thread_act_array_t *act_list,
1033 mach_msg_type_number_t *act_listCnt
1034 )
1035 {
1036
1037 #ifdef __MigPackStructs
1038 #pragma pack(4)
1039 #endif
1040 typedef struct {
1041 mach_msg_header_t Head;
1042 } Request;
1043 #ifdef __MigPackStructs
1044 #pragma pack()
1045 #endif
1046
1047 #ifdef __MigPackStructs
1048 #pragma pack(4)
1049 #endif
1050 typedef struct {
1051 mach_msg_header_t Head;
1052 /* start of the kernel processed data */
1053 mach_msg_body_t msgh_body;
1054 mach_msg_ool_ports_descriptor_t act_list;
1055 /* end of the kernel processed data */
1056 NDR_record_t NDR;
1057 mach_msg_type_number_t act_listCnt;
1058 mach_msg_trailer_t trailer;
1059 } Reply;
1060 #ifdef __MigPackStructs
1061 #pragma pack()
1062 #endif
1063
1064 #ifdef __MigPackStructs
1065 #pragma pack(4)
1066 #endif
1067 typedef struct {
1068 mach_msg_header_t Head;
1069 /* start of the kernel processed data */
1070 mach_msg_body_t msgh_body;
1071 mach_msg_ool_ports_descriptor_t act_list;
1072 /* end of the kernel processed data */
1073 NDR_record_t NDR;
1074 mach_msg_type_number_t act_listCnt;
1075 } __Reply;
1076 #ifdef __MigPackStructs
1077 #pragma pack()
1078 #endif
1079 /*
1080 * typedef struct {
1081 * mach_msg_header_t Head;
1082 * NDR_record_t NDR;
1083 * kern_return_t RetCode;
1084 * } mig_reply_error_t;
1085 */
1086
1087 union {
1088 Request In;
1089 Reply Out;
1090 } Mess;
1091
1092 Request *InP = &Mess.In;
1093 Reply *Out0P = &Mess.Out;
1094
1095 mach_msg_return_t msg_result;
1096
1097 #ifdef __MIG_check__Reply__task_threads_t__defined
1098 kern_return_t check_result;
1099 #endif /* __MIG_check__Reply__task_threads_t__defined */
1100
1101 __DeclareSendRpc(3402, "task_threads")
1102
1103 InP->Head.msgh_bits =
1104 MACH_MSGH_BITS(19, MACH_MSG_TYPE_MAKE_SEND_ONCE);
1105 /* msgh_size passed as argument */
1106 InP->Head.msgh_request_port = target_task;
1107 InP->Head.msgh_reply_port = mig_get_reply_port();
1108 InP->Head.msgh_id = 3402;
1109
1110 __BeforeSendRpc(3402, "task_threads")
1111 msg_result = mach_msg(&InP->Head, MACH_SEND_MSG|MACH_RCV_MSG|MACH_MSG_OPTION_NONE, (mach_msg_size_t)sizeof(Request), (mach_msg_size_t)sizeof(Reply), InP->Head.msgh_reply_port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
1112 __AfterSendRpc(3402, "task_threads")
1113 if (msg_result != MACH_MSG_SUCCESS) {
1114 _objc_inform("task_threads received unexpected reply msgh_id 0x%zx",
1115 (size_t)Out0P->Head.msgh_id);
1116 __MachMsgErrorWithoutTimeout(msg_result);
1117 { return msg_result; }
1118 }
1119
1120
1121 #if defined(__MIG_check__Reply__task_threads_t__defined)
1122 check_result = __MIG_check__Reply__task_threads_t((__Reply__task_threads_t *)Out0P);
1123 if (check_result != MACH_MSG_SUCCESS)
1124 { return check_result; }
1125 #endif /* defined(__MIG_check__Reply__task_threads_t__defined) */
1126
1127 *act_list = (thread_act_array_t)(Out0P->act_list.address);
1128 *act_listCnt = Out0P->act_listCnt;
1129
1130 return KERN_SUCCESS;
1131 }
1132
1133 // DEBUG_TASK_THREADS
1134 #endif
1135
1136
1137 // __OBJC2__
1138 #endif