objc-cache.mm 33 KB

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