2 * Copyright (c) 2005-2009 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 /***********************************************************************
26 * Support for new-ABI classes and images.
27 **********************************************************************/
31 #include "objc-private.h"
32 #include "objc-runtime-new.h"
33 #include "objc-file.h"
34 #include "objc-cache.h"
36 #include <objc/message.h>
37 #include <mach/shared_region.h>
39 #define newprotocol(p) ((protocol_t *)p)
41 static void disableTaggedPointers();
42 static void detach_class(Class cls, bool isMeta);
43 static void free_class(Class cls);
44 static Class setSuperclass(Class cls, Class newSuper);
45 static Class realizeClass(Class cls);
46 static method_t *getMethodNoSuper_nolock(Class cls, SEL sel);
47 static method_t *getMethod_nolock(Class cls, SEL sel);
48 static IMP addMethod(Class cls, SEL name, IMP imp, const char *types, bool replace);
49 static bool isRRSelector(SEL sel);
50 static bool isAWZSelector(SEL sel);
51 static bool methodListImplementsRR(const method_list_t *mlist);
52 static bool methodListImplementsAWZ(const method_list_t *mlist);
53 static void updateCustomRR_AWZ(Class cls, method_t *meth);
54 static method_t *search_method_list(const method_list_t *mlist, SEL sel);
55 static void flushCaches(Class cls);
57 static void fixupMessageRef(message_ref_t *msg);
60 static bool MetaclassNSObjectAWZSwizzled;
61 static bool ClassNSObjectRRSwizzled;
64 /***********************************************************************
66 **********************************************************************/
69 mutex_t cacheUpdateLock;
70 recursive_mutex_t loadMethodLock;
73 pthread_priority_t BackgroundPriority = 0;
74 pthread_priority_t MainPriority = 0;
76 static __unused void destroyQOSKey(void *arg) {
77 _objc_fatal("QoS override level at thread exit is %zu instead of zero",
78 (size_t)(uintptr_t)arg);
86 BackgroundPriority = _pthread_qos_class_encode(QOS_CLASS_BACKGROUND, 0, 0);
87 MainPriority = _pthread_qos_class_encode(qos_class_main(), 0, 0);
89 pthread_key_init_np(QOS_KEY, &destroyQOSKey);
95 /***********************************************************************
96 * Class structure decoding
97 **********************************************************************/
99 const uintptr_t objc_debug_class_rw_data_mask = FAST_DATA_MASK;
102 /***********************************************************************
103 * Non-pointer isa decoding
104 **********************************************************************/
105 #if SUPPORT_INDEXED_ISA
107 // Indexed non-pointer isa.
109 // These are used to mask the ISA and see if its got an index or not.
110 const uintptr_t objc_debug_indexed_isa_magic_mask = ISA_INDEX_MAGIC_MASK;
111 const uintptr_t objc_debug_indexed_isa_magic_value = ISA_INDEX_MAGIC_VALUE;
113 // die if masks overlap
114 STATIC_ASSERT((ISA_INDEX_MASK & ISA_INDEX_MAGIC_MASK) == 0);
116 // die if magic is wrong
117 STATIC_ASSERT((~ISA_INDEX_MAGIC_MASK & ISA_INDEX_MAGIC_VALUE) == 0);
119 // Then these are used to extract the index from the ISA.
120 const uintptr_t objc_debug_indexed_isa_index_mask = ISA_INDEX_MASK;
121 const uintptr_t objc_debug_indexed_isa_index_shift = ISA_INDEX_SHIFT;
123 asm("\n .globl _objc_absolute_indexed_isa_magic_mask" \
124 "\n _objc_absolute_indexed_isa_magic_mask = " STRINGIFY2(ISA_INDEX_MAGIC_MASK));
125 asm("\n .globl _objc_absolute_indexed_isa_magic_value" \
126 "\n _objc_absolute_indexed_isa_magic_value = " STRINGIFY2(ISA_INDEX_MAGIC_VALUE));
127 asm("\n .globl _objc_absolute_indexed_isa_index_mask" \
128 "\n _objc_absolute_indexed_isa_index_mask = " STRINGIFY2(ISA_INDEX_MASK));
129 asm("\n .globl _objc_absolute_indexed_isa_index_shift" \
130 "\n _objc_absolute_indexed_isa_index_shift = " STRINGIFY2(ISA_INDEX_SHIFT));
133 // And then we can use that index to get the class from this array. Note
134 // the size is provided so that clients can ensure the index they get is in
135 // bounds and not read off the end of the array.
136 // Defined in the objc-msg-*.s files
137 // const Class objc_indexed_classes[]
139 // When we don't have enough bits to store a class*, we can instead store an
140 // index in to this array. Classes are added here when they are realized.
141 // Note, an index of 0 is illegal.
142 uintptr_t objc_indexed_classes_count = 0;
144 // SUPPORT_INDEXED_ISA
146 // not SUPPORT_INDEXED_ISA
148 // These variables exist but are all set to 0 so that they are ignored.
149 const uintptr_t objc_debug_indexed_isa_magic_mask = 0;
150 const uintptr_t objc_debug_indexed_isa_magic_value = 0;
151 const uintptr_t objc_debug_indexed_isa_index_mask = 0;
152 const uintptr_t objc_debug_indexed_isa_index_shift = 0;
153 Class objc_indexed_classes[1] = { nil };
154 uintptr_t objc_indexed_classes_count = 0;
156 // not SUPPORT_INDEXED_ISA
160 #if SUPPORT_PACKED_ISA
162 // Packed non-pointer isa.
164 asm("\n .globl _objc_absolute_packed_isa_class_mask" \
165 "\n _objc_absolute_packed_isa_class_mask = " STRINGIFY2(ISA_MASK));
167 const uintptr_t objc_debug_isa_class_mask = ISA_MASK;
168 const uintptr_t objc_debug_isa_magic_mask = ISA_MAGIC_MASK;
169 const uintptr_t objc_debug_isa_magic_value = ISA_MAGIC_VALUE;
171 // die if masks overlap
172 STATIC_ASSERT((ISA_MASK & ISA_MAGIC_MASK) == 0);
174 // die if magic is wrong
175 STATIC_ASSERT((~ISA_MAGIC_MASK & ISA_MAGIC_VALUE) == 0);
177 // die if virtual address space bound goes up
178 STATIC_ASSERT((~ISA_MASK & MACH_VM_MAX_ADDRESS) == 0 ||
179 ISA_MASK + sizeof(void*) == MACH_VM_MAX_ADDRESS);
181 // SUPPORT_PACKED_ISA
183 // not SUPPORT_PACKED_ISA
185 // These variables exist but enforce pointer alignment only.
186 const uintptr_t objc_debug_isa_class_mask = (~WORD_MASK);
187 const uintptr_t objc_debug_isa_magic_mask = WORD_MASK;
188 const uintptr_t objc_debug_isa_magic_value = 0;
190 // not SUPPORT_PACKED_ISA
194 typedef locstamped_category_list_t category_list;
198 Low two bits of mlist->entsize is used as the fixed-up marker.
199 PREOPTIMIZED VERSION:
200 Method lists from shared cache are 1 (uniqued) or 3 (uniqued and sorted).
201 (Protocol method lists are not sorted because of their extra parallel data)
202 Runtime fixed-up method lists get 3.
203 UN-PREOPTIMIZED VERSION:
204 Method lists from shared cache are 1 (uniqued) or 3 (uniqued and sorted)
205 Shared cache's sorting and uniquing are not trusted, but do affect the
206 location of the selector name string.
207 Runtime fixed-up method lists get 2.
209 High two bits of protocol->flags is used as the fixed-up marker.
210 PREOPTIMIZED VERSION:
211 Protocols from shared cache are 1<<30.
212 Runtime fixed-up protocols get 1<<30.
213 UN-PREOPTIMIZED VERSION:
214 Protocols from shared cache are 1<<30.
215 Shared cache's fixups are not trusted.
216 Runtime fixed-up protocols get 3<<30.
219 static uint32_t fixed_up_method_list = 3;
220 static uint32_t fixed_up_protocol = PROTOCOL_FIXED_UP_1;
223 disableSharedCacheOptimizations(void)
225 fixed_up_method_list = 2;
226 fixed_up_protocol = PROTOCOL_FIXED_UP_1 | PROTOCOL_FIXED_UP_2;
229 bool method_list_t::isFixedUp() const {
230 return flags() == fixed_up_method_list;
233 void method_list_t::setFixedUp() {
234 runtimeLock.assertWriting();
235 assert(!isFixedUp());
236 entsizeAndFlags = entsize() | fixed_up_method_list;
239 bool protocol_t::isFixedUp() const {
240 return (flags & PROTOCOL_FIXED_UP_MASK) == fixed_up_protocol;
243 void protocol_t::setFixedUp() {
244 runtimeLock.assertWriting();
245 assert(!isFixedUp());
246 flags = (flags & ~PROTOCOL_FIXED_UP_MASK) | fixed_up_protocol;
250 method_list_t **method_array_t::endCategoryMethodLists(Class cls)
252 method_list_t **mlists = beginLists();
253 method_list_t **mlistsEnd = endLists();
255 if (mlists == mlistsEnd || !cls->data()->ro->baseMethods())
257 // No methods, or no base methods.
258 // Everything here is a category method.
262 // Have base methods. Category methods are
263 // everything except the last method list.
264 return mlistsEnd - 1;
267 static const char *sel_cname(SEL sel)
269 return (const char *)(void *)sel;
273 static size_t protocol_list_size(const protocol_list_t *plist)
275 return sizeof(protocol_list_t) + plist->count * sizeof(protocol_t *);
279 static void try_free(const void *p)
281 if (p && malloc_size(p)) free((void *)p);
286 alloc_class_for_subclass(Class supercls, size_t extraBytes)
288 if (!supercls || !supercls->isSwift()) {
289 return _calloc_class(sizeof(objc_class) + extraBytes);
292 // Superclass is a Swift class. New subclass must duplicate its extra bits.
294 // Allocate the new class, with space for super's prefix and suffix
295 // and self's extraBytes.
296 swift_class_t *swiftSupercls = (swift_class_t *)supercls;
297 size_t superSize = swiftSupercls->classSize;
298 void *superBits = swiftSupercls->baseAddress();
299 void *bits = malloc(superSize + extraBytes);
301 // Copy all of the superclass's data to the new class.
302 memcpy(bits, superBits, superSize);
304 // Erase the objc data and the Swift description in the new class.
305 swift_class_t *swcls = (swift_class_t *)
306 ((uint8_t *)bits + swiftSupercls->classAddressOffset);
307 bzero(swcls, sizeof(objc_class));
308 swcls->description = nil;
310 // Mark this class as Swift-enhanced.
311 swcls->bits.setIsSwift();
317 /***********************************************************************
318 * object_getIndexedIvars.
319 **********************************************************************/
320 void *object_getIndexedIvars(id obj)
322 uint8_t *base = (uint8_t *)obj;
324 if (!obj) return nil;
325 if (obj->isTaggedPointer()) return nil;
327 if (!obj->isClass()) return base + obj->ISA()->alignedInstanceSize();
329 Class cls = (Class)obj;
330 if (!cls->isSwift()) return base + sizeof(objc_class);
332 swift_class_t *swcls = (swift_class_t *)cls;
333 return base - swcls->classAddressOffset + word_align(swcls->classSize);
337 /***********************************************************************
339 * Reallocates rw->ro if necessary to make it writeable.
340 * Locking: runtimeLock must be held by the caller.
341 **********************************************************************/
342 static class_ro_t *make_ro_writeable(class_rw_t *rw)
344 runtimeLock.assertWriting();
346 if (rw->flags & RW_COPIED_RO) {
347 // already writeable, do nothing
349 class_ro_t *ro = (class_ro_t *)
350 memdup(rw->ro, sizeof(*rw->ro));
352 rw->flags |= RW_COPIED_RO;
354 return (class_ro_t *)rw->ro;
358 /***********************************************************************
359 * unattachedCategories
360 * Returns the class => categories map of unattached categories.
361 * Locking: runtimeLock must be held by the caller.
362 **********************************************************************/
363 static NXMapTable *unattachedCategories(void)
365 runtimeLock.assertWriting();
367 static NXMapTable *category_map = nil;
369 if (category_map) return category_map;
371 // fixme initial map size
372 category_map = NXCreateMapTable(NXPtrValueMapPrototype, 16);
378 /***********************************************************************
379 * addUnattachedCategoryForClass
380 * Records an unattached category.
381 * Locking: runtimeLock must be held by the caller.
382 **********************************************************************/
383 static void addUnattachedCategoryForClass(category_t *cat, Class cls,
384 header_info *catHeader)
386 runtimeLock.assertWriting();
388 // DO NOT use cat->cls! cls may be cat->cls->isa instead
389 NXMapTable *cats = unattachedCategories();
392 list = (category_list *)NXMapGet(cats, cls);
394 list = (category_list *)
395 calloc(sizeof(*list) + sizeof(list->list[0]), 1);
397 list = (category_list *)
398 realloc(list, sizeof(*list) + sizeof(list->list[0]) * (list->count + 1));
400 list->list[list->count++] = (locstamped_category_t){cat, catHeader};
401 NXMapInsert(cats, cls, list);
405 /***********************************************************************
406 * removeUnattachedCategoryForClass
407 * Removes an unattached category.
408 * Locking: runtimeLock must be held by the caller.
409 **********************************************************************/
410 static void removeUnattachedCategoryForClass(category_t *cat, Class cls)
412 runtimeLock.assertWriting();
414 // DO NOT use cat->cls! cls may be cat->cls->isa instead
415 NXMapTable *cats = unattachedCategories();
418 list = (category_list *)NXMapGet(cats, cls);
422 for (i = 0; i < list->count; i++) {
423 if (list->list[i].cat == cat) {
424 // shift entries to preserve list order
425 memmove(&list->list[i], &list->list[i+1],
426 (list->count-i-1) * sizeof(list->list[i]));
434 /***********************************************************************
435 * unattachedCategoriesForClass
436 * Returns the list of unattached categories for a class, and
437 * deletes them from the list.
438 * The result must be freed by the caller.
439 * Locking: runtimeLock must be held by the caller.
440 **********************************************************************/
441 static category_list *
442 unattachedCategoriesForClass(Class cls, bool realizing)
444 runtimeLock.assertWriting();
445 return (category_list *)NXMapRemove(unattachedCategories(), cls);
449 /***********************************************************************
450 * removeAllUnattachedCategoriesForClass
451 * Deletes all unattached categories (loaded or not) for a class.
452 * Locking: runtimeLock must be held by the caller.
453 **********************************************************************/
454 static void removeAllUnattachedCategoriesForClass(Class cls)
456 runtimeLock.assertWriting();
458 void *list = NXMapRemove(unattachedCategories(), cls);
459 if (list) free(list);
463 /***********************************************************************
465 * Returns class NSObject.
467 **********************************************************************/
468 static Class classNSObject(void)
470 extern objc_class OBJC_CLASS_$_NSObject;
471 return (Class)&OBJC_CLASS_$_NSObject;
475 /***********************************************************************
477 * Implementation of PrintReplacedMethods / OBJC_PRINT_REPLACED_METHODS.
478 * Warn about methods from cats that override other methods in cats or cls.
479 * Assumes no methods from cats have been added to cls yet.
480 **********************************************************************/
481 static void printReplacements(Class cls, category_list *cats)
484 bool isMeta = cls->isMetaClass();
488 // Newest categories are LAST in cats
489 // Later categories override earlier ones.
490 for (c = 0; c < cats->count; c++) {
491 category_t *cat = cats->list[c].cat;
493 method_list_t *mlist = cat->methodsForMeta(isMeta);
494 if (!mlist) continue;
496 for (const auto& meth : *mlist) {
497 SEL s = sel_registerName(sel_cname(meth.name));
499 // Search for replaced methods in method lookup order.
500 // Complain about the first duplicate only.
502 // Look for method in earlier categories
503 for (uint32_t c2 = 0; c2 < c; c2++) {
504 category_t *cat2 = cats->list[c2].cat;
506 const method_list_t *mlist2 = cat2->methodsForMeta(isMeta);
507 if (!mlist2) continue;
509 for (const auto& meth2 : *mlist2) {
510 SEL s2 = sel_registerName(sel_cname(meth2.name));
512 logReplacedMethod(cls->nameForLogging(), s,
513 cls->isMetaClass(), cat->name,
514 meth2.imp, meth.imp);
520 // Look for method in cls
521 for (const auto& meth2 : cls->data()->methods) {
522 SEL s2 = sel_registerName(sel_cname(meth2.name));
524 logReplacedMethod(cls->nameForLogging(), s,
525 cls->isMetaClass(), cat->name,
526 meth2.imp, meth.imp);
538 static bool isBundleClass(Class cls)
540 return cls->data()->ro->flags & RO_FROM_BUNDLE;
545 fixupMethodList(method_list_t *mlist, bool bundleCopy, bool sort)
547 runtimeLock.assertWriting();
548 assert(!mlist->isFixedUp());
550 // fixme lock less in attachMethodLists ?
553 // Unique selectors in list.
554 for (auto& meth : *mlist) {
555 const char *name = sel_cname(meth.name);
556 meth.name = sel_registerNameNoLock(name, bundleCopy);
561 // Sort by selector address.
563 method_t::SortBySELAddress sorter;
564 std::stable_sort(mlist->begin(), mlist->end(), sorter);
567 // Mark method list as uniqued and sorted
573 prepareMethodLists(Class cls, method_list_t **addedLists, int addedCount,
574 bool baseMethods, bool methodsFromBundle)
576 runtimeLock.assertWriting();
578 if (addedCount == 0) return;
580 // Don't scan redundantly
581 bool scanForCustomRR = !cls->hasCustomRR();
582 bool scanForCustomAWZ = !cls->hasCustomAWZ();
584 // There exist RR/AWZ special cases for some class's base methods.
585 // But this code should never need to scan base methods for RR/AWZ:
586 // default RR/AWZ cannot be set before setInitialized().
587 // Therefore we need not handle any special cases here.
589 assert(!scanForCustomRR && !scanForCustomAWZ);
592 // Add method lists to array.
593 // Reallocate un-fixed method lists.
594 // The new methods are PREPENDED to the method list array.
596 for (int i = 0; i < addedCount; i++) {
597 method_list_t *mlist = addedLists[i];
600 // Fixup selectors if necessary
601 if (!mlist->isFixedUp()) {
602 fixupMethodList(mlist, methodsFromBundle, true/*sort*/);
605 // Scan for method implementations tracked by the class's flags
606 if (scanForCustomRR && methodListImplementsRR(mlist)) {
607 cls->setHasCustomRR();
608 scanForCustomRR = false;
610 if (scanForCustomAWZ && methodListImplementsAWZ(mlist)) {
611 cls->setHasCustomAWZ();
612 scanForCustomAWZ = false;
618 // Attach method lists and properties and protocols from categories to a class.
619 // Assumes the categories in cats are all loaded and sorted by load order,
620 // oldest categories first.
622 attachCategories(Class cls, category_list *cats, bool flush_caches)
625 if (PrintReplacedMethods) printReplacements(cls, cats);
627 bool isMeta = cls->isMetaClass();
629 // fixme rearrange to remove these intermediate allocations
630 method_list_t **mlists = (method_list_t **)
631 malloc(cats->count * sizeof(*mlists));
632 property_list_t **proplists = (property_list_t **)
633 malloc(cats->count * sizeof(*proplists));
634 protocol_list_t **protolists = (protocol_list_t **)
635 malloc(cats->count * sizeof(*protolists));
637 // Count backwards through cats to get newest categories first
642 bool fromBundle = NO;
644 auto& entry = cats->list[i];
646 method_list_t *mlist = entry.cat->methodsForMeta(isMeta);
648 mlists[mcount++] = mlist;
649 fromBundle |= entry.hi->isBundle();
652 property_list_t *proplist =
653 entry.cat->propertiesForMeta(isMeta, entry.hi);
655 proplists[propcount++] = proplist;
658 protocol_list_t *protolist = entry.cat->protocols;
660 protolists[protocount++] = protolist;
664 auto rw = cls->data();
666 prepareMethodLists(cls, mlists, mcount, NO, fromBundle);
667 rw->methods.attachLists(mlists, mcount);
669 if (flush_caches && mcount > 0) flushCaches(cls);
671 rw->properties.attachLists(proplists, propcount);
674 rw->protocols.attachLists(protolists, protocount);
679 /***********************************************************************
681 * Fixes up cls's method list, protocol list, and property list.
682 * Attaches any outstanding categories.
683 * Locking: runtimeLock must be held by the caller
684 **********************************************************************/
685 static void methodizeClass(Class cls)
687 runtimeLock.assertWriting();
689 bool isMeta = cls->isMetaClass();
690 auto rw = cls->data();
693 // Methodizing for the first time
694 if (PrintConnecting) {
695 _objc_inform("CLASS: methodizing class '%s' %s",
696 cls->nameForLogging(), isMeta ? "(meta)" : "");
699 // Install methods and properties that the class implements itself.
700 method_list_t *list = ro->baseMethods();
702 prepareMethodLists(cls, &list, 1, YES, isBundleClass(cls));
703 rw->methods.attachLists(&list, 1);
706 property_list_t *proplist = ro->baseProperties;
708 rw->properties.attachLists(&proplist, 1);
711 protocol_list_t *protolist = ro->baseProtocols;
713 rw->protocols.attachLists(&protolist, 1);
716 // Root classes get bonus method implementations if they don't have
717 // them already. These apply before category replacements.
718 if (cls->isRootMetaclass()) {
720 addMethod(cls, SEL_initialize, (IMP)&objc_noop_imp, "", NO);
723 // Attach categories.
724 category_list *cats = unattachedCategoriesForClass(cls, true /*realizing*/);
725 attachCategories(cls, cats, false /*don't flush caches*/);
727 if (PrintConnecting) {
729 for (uint32_t i = 0; i < cats->count; i++) {
730 _objc_inform("CLASS: attached category %c%s(%s)",
732 cls->nameForLogging(), cats->list[i].cat->name);
737 if (cats) free(cats);
740 // Debug: sanity-check all SELs; log method list contents
741 for (const auto& meth : rw->methods) {
742 if (PrintConnecting) {
743 _objc_inform("METHOD %c[%s %s]", isMeta ? '+' : '-',
744 cls->nameForLogging(), sel_getName(meth.name));
746 assert(sel_registerName(sel_getName(meth.name)) == meth.name);
752 /***********************************************************************
754 * Attach outstanding categories to an existing class.
755 * Fixes up cls's method list, protocol list, and property list.
756 * Updates method caches for cls and its subclasses.
757 * Locking: runtimeLock must be held by the caller
758 **********************************************************************/
759 static void remethodizeClass(Class cls)
764 runtimeLock.assertWriting();
766 isMeta = cls->isMetaClass();
768 // Re-methodizing: check for more categories
769 if ((cats = unattachedCategoriesForClass(cls, false/*not realizing*/))) {
770 if (PrintConnecting) {
771 _objc_inform("CLASS: attaching categories to class '%s' %s",
772 cls->nameForLogging(), isMeta ? "(meta)" : "");
775 attachCategories(cls, cats, true /*flush caches*/);
781 /***********************************************************************
783 * Returns the secondary metaclass => class map
784 * Used for some cases of +initialize and +resolveClassMethod:.
785 * This map does not contain all class and metaclass pairs. It only
786 * contains metaclasses whose classes would be in the runtime-allocated
787 * named-class table, but are not because some other class with the same name
789 * Classes with no duplicates are not included.
790 * Classes in the preoptimized named-class table are not included.
791 * Classes whose duplicates are in the preoptimized table are not included.
792 * Most code should use getNonMetaClass() instead of reading this table.
793 * Locking: runtimeLock must be read- or write-locked by the caller
794 **********************************************************************/
795 static NXMapTable *nonmeta_class_map = nil;
796 static NXMapTable *nonMetaClasses(void)
798 runtimeLock.assertLocked();
800 if (nonmeta_class_map) return nonmeta_class_map;
802 // nonmeta_class_map is typically small
803 INIT_ONCE_PTR(nonmeta_class_map,
804 NXCreateMapTable(NXPtrValueMapPrototype, 32),
807 return nonmeta_class_map;
811 /***********************************************************************
813 * Adds metacls => cls to the secondary metaclass map
814 * Locking: runtimeLock must be held by the caller
815 **********************************************************************/
816 static void addNonMetaClass(Class cls)
818 runtimeLock.assertWriting();
820 old = NXMapInsert(nonMetaClasses(), cls->ISA(), cls);
822 assert(!cls->isMetaClass());
823 assert(cls->ISA()->isMetaClass());
828 static void removeNonMetaClass(Class cls)
830 runtimeLock.assertWriting();
831 NXMapRemove(nonMetaClasses(), cls->ISA());
835 static bool scanMangledField(const char *&string, const char *end,
836 const char *&field, int& length)
838 // Leading zero not allowed.
839 if (*string == '0') return false;
843 while (field < end) {
845 if (!isdigit(c)) break;
847 if (__builtin_smul_overflow(length, 10, &length)) return false;
848 if (__builtin_sadd_overflow(length, c - '0', &length)) return false;
851 string = field + length;
852 return length > 0 && string <= end;
856 /***********************************************************************
857 * copySwiftV1DemangledName
858 * Returns the pretty form of the given Swift-v1-mangled class or protocol name.
859 * Returns nil if the string doesn't look like a mangled Swift v1 name.
860 * The result must be freed with free().
861 **********************************************************************/
862 static char *copySwiftV1DemangledName(const char *string, bool isProtocol = false)
864 if (!string) return nil;
866 // Swift mangling prefix.
867 if (strncmp(string, isProtocol ? "_TtP" : "_TtC", 4) != 0) return nil;
870 const char *end = string + strlen(string);
875 if (string[0] == 's') {
876 // "s" is the Swift module.
881 if (! scanMangledField(string, end, prefix, prefixLength)) return nil;
884 // Class or protocol name.
887 if (! scanMangledField(string, end, suffix, suffixLength)) return nil;
890 // Remainder must be "_".
891 if (strcmp(string, "_") != 0) return nil;
893 // Remainder must be empty.
894 if (string != end) return nil;
898 asprintf(&result, "%.*s.%.*s", prefixLength,prefix, suffixLength,suffix);
903 /***********************************************************************
904 * copySwiftV1MangledName
905 * Returns the Swift 1.0 mangled form of the given class or protocol name.
906 * Returns nil if the string doesn't look like an unmangled Swift name.
907 * The result must be freed with free().
908 **********************************************************************/
909 static char *copySwiftV1MangledName(const char *string, bool isProtocol = false)
911 if (!string) return nil;
916 for (s = string; *s; s++) {
919 dotIndex = s - string;
922 size_t stringLength = s - string;
924 if (dotCount != 1 || dotIndex == 0 || dotIndex >= stringLength-1) {
928 const char *prefix = string;
929 size_t prefixLength = dotIndex;
930 const char *suffix = string + dotIndex + 1;
931 size_t suffixLength = stringLength - (dotIndex + 1);
935 if (prefixLength == 5 && memcmp(prefix, "Swift", 5) == 0) {
936 asprintf(&name, "_Tt%cs%zu%.*s%s",
937 isProtocol ? 'P' : 'C',
938 suffixLength, (int)suffixLength, suffix,
939 isProtocol ? "_" : "");
941 asprintf(&name, "_Tt%c%zu%.*s%zu%.*s%s",
942 isProtocol ? 'P' : 'C',
943 prefixLength, (int)prefixLength, prefix,
944 suffixLength, (int)suffixLength, suffix,
945 isProtocol ? "_" : "");
951 /***********************************************************************
953 * Looks up a class by name. The class MIGHT NOT be realized.
954 * Demangled Swift names are recognized.
955 * Locking: runtimeLock must be read- or write-locked by the caller.
956 **********************************************************************/
958 // This is a misnomer: gdb_objc_realized_classes is actually a list of
959 // named classes not in the dyld shared cache, whether realized or not.
960 NXMapTable *gdb_objc_realized_classes; // exported for debuggers in objc-gdb.h
962 static Class getClass_impl(const char *name)
964 runtimeLock.assertLocked();
966 // allocated in _read_images
967 assert(gdb_objc_realized_classes);
969 // Try runtime-allocated table
970 Class result = (Class)NXMapGet(gdb_objc_realized_classes, name);
971 if (result) return result;
973 // Try table from dyld shared cache
974 return getPreoptimizedClass(name);
977 static Class getClass(const char *name)
979 runtimeLock.assertLocked();
982 Class result = getClass_impl(name);
983 if (result) return result;
985 // Try Swift-mangled equivalent of the given name.
986 if (char *swName = copySwiftV1MangledName(name)) {
987 result = getClass_impl(swName);
996 /***********************************************************************
998 * Adds name => cls to the named non-meta class map.
999 * Warns about duplicate class names and keeps the old mapping.
1000 * Locking: runtimeLock must be held by the caller
1001 **********************************************************************/
1002 static void addNamedClass(Class cls, const char *name, Class replacing = nil)
1004 runtimeLock.assertWriting();
1006 if ((old = getClass(name)) && old != replacing) {
1007 inform_duplicate(name, old, cls);
1009 // getNonMetaClass uses name lookups. Classes not found by name
1010 // lookup must be in the secondary meta->nonmeta table.
1011 addNonMetaClass(cls);
1013 NXMapInsert(gdb_objc_realized_classes, name, cls);
1015 assert(!(cls->data()->flags & RO_META));
1017 // wrong: constructed classes are already realized when they get here
1018 // assert(!cls->isRealized());
1022 /***********************************************************************
1024 * Removes cls from the name => cls map.
1025 * Locking: runtimeLock must be held by the caller
1026 **********************************************************************/
1027 static void removeNamedClass(Class cls, const char *name)
1029 runtimeLock.assertWriting();
1030 assert(!(cls->data()->flags & RO_META));
1031 if (cls == NXMapGet(gdb_objc_realized_classes, name)) {
1032 NXMapRemove(gdb_objc_realized_classes, name);
1034 // cls has a name collision with another class - don't remove the other
1035 // but do remove cls from the secondary metaclass->class map.
1036 removeNonMetaClass(cls);
1041 /***********************************************************************
1042 * unreasonableClassCount
1043 * Provides an upper bound for any iteration of classes,
1044 * to prevent spins when runtime metadata is corrupted.
1045 **********************************************************************/
1046 unsigned unreasonableClassCount()
1048 runtimeLock.assertLocked();
1050 int base = NXCountMapTable(gdb_objc_realized_classes) +
1051 getPreoptimizedClassUnreasonableCount();
1053 // Provide lots of slack here. Some iterations touch metaclasses too.
1054 // Some iterations backtrack (like realized class iteration).
1055 // We don't need an efficient bound, merely one that prevents spins.
1056 return (base + 1) * 16;
1060 /***********************************************************************
1061 * futureNamedClasses
1062 * Returns the classname => future class map for unrealized future classes.
1063 * Locking: runtimeLock must be held by the caller
1064 **********************************************************************/
1065 static NXMapTable *future_named_class_map = nil;
1066 static NXMapTable *futureNamedClasses()
1068 runtimeLock.assertWriting();
1070 if (future_named_class_map) return future_named_class_map;
1072 // future_named_class_map is big enough for CF's classes and a few others
1073 future_named_class_map =
1074 NXCreateMapTable(NXStrValueMapPrototype, 32);
1076 return future_named_class_map;
1080 static bool haveFutureNamedClasses() {
1081 return future_named_class_map && NXCountMapTable(future_named_class_map);
1085 /***********************************************************************
1086 * addFutureNamedClass
1087 * Installs cls as the class structure to use for the named class if it appears.
1088 * Locking: runtimeLock must be held by the caller
1089 **********************************************************************/
1090 static void addFutureNamedClass(const char *name, Class cls)
1094 runtimeLock.assertWriting();
1097 _objc_inform("FUTURE: reserving %p for %s", (void*)cls, name);
1100 class_rw_t *rw = (class_rw_t *)calloc(sizeof(class_rw_t), 1);
1101 class_ro_t *ro = (class_ro_t *)calloc(sizeof(class_ro_t), 1);
1102 ro->name = strdupIfMutable(name);
1105 cls->data()->flags = RO_FUTURE;
1107 old = NXMapKeyCopyingInsert(futureNamedClasses(), name, cls);
1112 /***********************************************************************
1113 * popFutureNamedClass
1114 * Removes the named class from the unrealized future class list,
1115 * because it has been realized.
1116 * Returns nil if the name is not used by a future class.
1117 * Locking: runtimeLock must be held by the caller
1118 **********************************************************************/
1119 static Class popFutureNamedClass(const char *name)
1121 runtimeLock.assertWriting();
1125 if (future_named_class_map) {
1126 cls = (Class)NXMapKeyFreeingRemove(future_named_class_map, name);
1127 if (cls && NXCountMapTable(future_named_class_map) == 0) {
1128 NXFreeMapTable(future_named_class_map);
1129 future_named_class_map = nil;
1137 /***********************************************************************
1139 * Returns the oldClass => newClass map for realized future classes.
1140 * Returns the oldClass => nil map for ignored weak-linked classes.
1141 * Locking: runtimeLock must be read- or write-locked by the caller
1142 **********************************************************************/
1143 static NXMapTable *remappedClasses(bool create)
1145 static NXMapTable *remapped_class_map = nil;
1147 runtimeLock.assertLocked();
1149 if (remapped_class_map) return remapped_class_map;
1150 if (!create) return nil;
1152 // remapped_class_map is big enough to hold CF's classes and a few others
1153 INIT_ONCE_PTR(remapped_class_map,
1154 NXCreateMapTable(NXPtrValueMapPrototype, 32),
1157 return remapped_class_map;
1161 /***********************************************************************
1163 * Returns YES if no classes have been remapped
1164 * Locking: runtimeLock must be read- or write-locked by the caller
1165 **********************************************************************/
1166 static bool noClassesRemapped(void)
1168 runtimeLock.assertLocked();
1170 bool result = (remappedClasses(NO) == nil);
1172 // Catch construction of an empty table, which defeats optimization.
1173 NXMapTable *map = remappedClasses(NO);
1174 if (map) assert(NXCountMapTable(map) > 0);
1180 /***********************************************************************
1182 * newcls is a realized future class, replacing oldcls.
1183 * OR newcls is nil, replacing ignored weak-linked class oldcls.
1184 * Locking: runtimeLock must be write-locked by the caller
1185 **********************************************************************/
1186 static void addRemappedClass(Class oldcls, Class newcls)
1188 runtimeLock.assertWriting();
1191 _objc_inform("FUTURE: using %p instead of %p for %s",
1192 (void*)newcls, (void*)oldcls, oldcls->nameForLogging());
1196 old = NXMapInsert(remappedClasses(YES), oldcls, newcls);
1201 /***********************************************************************
1203 * Returns the live class pointer for cls, which may be pointing to
1204 * a class struct that has been reallocated.
1205 * Returns nil if cls is ignored because of weak linking.
1206 * Locking: runtimeLock must be read- or write-locked by the caller
1207 **********************************************************************/
1208 static Class remapClass(Class cls)
1210 runtimeLock.assertLocked();
1214 if (!cls) return nil;
1216 NXMapTable *map = remappedClasses(NO);
1217 if (!map || NXMapMember(map, cls, (void**)&c2) == NX_MAPNOTAKEY) {
1224 static Class remapClass(classref_t cls)
1226 return remapClass((Class)cls);
1229 Class _class_remap(Class cls)
1231 rwlock_reader_t lock(runtimeLock);
1232 return remapClass(cls);
1235 /***********************************************************************
1237 * Fix up a class ref, in case the class referenced has been reallocated
1238 * or is an ignored weak-linked class.
1239 * Locking: runtimeLock must be read- or write-locked by the caller
1240 **********************************************************************/
1241 static void remapClassRef(Class *clsref)
1243 runtimeLock.assertLocked();
1245 Class newcls = remapClass(*clsref);
1246 if (*clsref != newcls) *clsref = newcls;
1250 /***********************************************************************
1252 * Return the ordinary class for this class or metaclass.
1253 * `inst` is an instance of `cls` or a subclass thereof, or nil.
1254 * Non-nil inst is faster.
1255 * Used by +initialize.
1256 * Locking: runtimeLock must be read- or write-locked by the caller
1257 **********************************************************************/
1258 static Class getNonMetaClass(Class metacls, id inst)
1260 static int total, named, secondary, sharedcache;
1261 runtimeLock.assertLocked();
1263 realizeClass(metacls);
1267 // return cls itself if it's already a non-meta class
1268 if (!metacls->isMetaClass()) return metacls;
1270 // metacls really is a metaclass
1272 // special case for root metaclass
1273 // where inst == inst->ISA() == metacls is possible
1274 if (metacls->ISA() == metacls) {
1275 Class cls = metacls->superclass;
1276 assert(cls->isRealized());
1277 assert(!cls->isMetaClass());
1278 assert(cls->ISA() == metacls);
1279 if (cls->ISA() == metacls) return cls;
1282 // use inst if available
1284 Class cls = (Class)inst;
1286 // cls may be a subclass - find the real class for metacls
1287 while (cls && cls->ISA() != metacls) {
1288 cls = cls->superclass;
1292 assert(!cls->isMetaClass());
1293 assert(cls->ISA() == metacls);
1297 _objc_fatal("cls is not an instance of metacls");
1299 // release build: be forgiving and fall through to slow lookups
1305 Class cls = getClass(metacls->mangledName());
1306 if (cls->ISA() == metacls) {
1308 if (PrintInitializing) {
1309 _objc_inform("INITIALIZE: %d/%d (%g%%) "
1310 "successful by-name metaclass lookups",
1311 named, total, named*100.0/total);
1319 // try secondary table
1321 Class cls = (Class)NXMapGet(nonMetaClasses(), metacls);
1324 if (PrintInitializing) {
1325 _objc_inform("INITIALIZE: %d/%d (%g%%) "
1326 "successful secondary metaclass lookups",
1327 secondary, total, secondary*100.0/total);
1330 assert(cls->ISA() == metacls);
1336 // try any duplicates in the dyld shared cache
1341 Class *classes = copyPreoptimizedClasses(metacls->mangledName(),&count);
1343 for (int i = 0; i < count; i++) {
1344 if (classes[i]->ISA() == metacls) {
1354 if (PrintInitializing) {
1355 _objc_inform("INITIALIZE: %d/%d (%g%%) "
1356 "successful shared cache metaclass lookups",
1357 sharedcache, total, sharedcache*100.0/total);
1365 _objc_fatal("no class for metaclass %p", (void*)metacls);
1369 /***********************************************************************
1370 * _class_getNonMetaClass
1371 * Return the ordinary class for this class or metaclass.
1372 * Used by +initialize.
1373 * Locking: acquires runtimeLock
1374 **********************************************************************/
1375 Class _class_getNonMetaClass(Class cls, id obj)
1377 rwlock_writer_t lock(runtimeLock);
1378 cls = getNonMetaClass(cls, obj);
1379 assert(cls->isRealized());
1384 /***********************************************************************
1386 * Adds cls as a new realized root class.
1387 * Locking: runtimeLock must be held by the caller.
1388 **********************************************************************/
1389 static Class _firstRealizedClass = nil;
1390 Class firstRealizedClass()
1392 runtimeLock.assertLocked();
1393 return _firstRealizedClass;
1396 static void addRootClass(Class cls)
1398 runtimeLock.assertWriting();
1400 assert(cls->isRealized());
1401 cls->data()->nextSiblingClass = _firstRealizedClass;
1402 _firstRealizedClass = cls;
1405 static void removeRootClass(Class cls)
1407 runtimeLock.assertWriting();
1410 for (classp = &_firstRealizedClass;
1412 classp = &(*classp)->data()->nextSiblingClass)
1415 *classp = (*classp)->data()->nextSiblingClass;
1419 /***********************************************************************
1421 * Adds subcls as a subclass of supercls.
1422 * Locking: runtimeLock must be held by the caller.
1423 **********************************************************************/
1424 static void addSubclass(Class supercls, Class subcls)
1426 runtimeLock.assertWriting();
1428 if (supercls && subcls) {
1429 assert(supercls->isRealized());
1430 assert(subcls->isRealized());
1431 subcls->data()->nextSiblingClass = supercls->data()->firstSubclass;
1432 supercls->data()->firstSubclass = subcls;
1434 if (supercls->hasCxxCtor()) {
1435 subcls->setHasCxxCtor();
1438 if (supercls->hasCxxDtor()) {
1439 subcls->setHasCxxDtor();
1442 if (supercls->hasCustomRR()) {
1443 subcls->setHasCustomRR(true);
1446 if (supercls->hasCustomAWZ()) {
1447 subcls->setHasCustomAWZ(true);
1450 // Special case: instancesRequireRawIsa does not propagate
1451 // from root class to root metaclass
1452 if (supercls->instancesRequireRawIsa() && supercls->superclass) {
1453 subcls->setInstancesRequireRawIsa(true);
1459 /***********************************************************************
1461 * Removes subcls as a subclass of supercls.
1462 * Locking: runtimeLock must be held by the caller.
1463 **********************************************************************/
1464 static void removeSubclass(Class supercls, Class subcls)
1466 runtimeLock.assertWriting();
1467 assert(supercls->isRealized());
1468 assert(subcls->isRealized());
1469 assert(subcls->superclass == supercls);
1472 for (cp = &supercls->data()->firstSubclass;
1473 *cp && *cp != subcls;
1474 cp = &(*cp)->data()->nextSiblingClass)
1476 assert(*cp == subcls);
1477 *cp = subcls->data()->nextSiblingClass;
1482 /***********************************************************************
1484 * Returns the protocol name => protocol map for protocols.
1485 * Locking: runtimeLock must read- or write-locked by the caller
1486 **********************************************************************/
1487 static NXMapTable *protocols(void)
1489 static NXMapTable *protocol_map = nil;
1491 runtimeLock.assertLocked();
1493 INIT_ONCE_PTR(protocol_map,
1494 NXCreateMapTable(NXStrValueMapPrototype, 16),
1495 NXFreeMapTable(v) );
1497 return protocol_map;
1501 /***********************************************************************
1503 * Looks up a protocol by name. Demangled Swift names are recognized.
1504 * Locking: runtimeLock must be read- or write-locked by the caller.
1505 **********************************************************************/
1506 static Protocol *getProtocol(const char *name)
1508 runtimeLock.assertLocked();
1511 Protocol *result = (Protocol *)NXMapGet(protocols(), name);
1512 if (result) return result;
1514 // Try Swift-mangled equivalent of the given name.
1515 if (char *swName = copySwiftV1MangledName(name, true/*isProtocol*/)) {
1516 result = (Protocol *)NXMapGet(protocols(), swName);
1525 /***********************************************************************
1527 * Returns the live protocol pointer for proto, which may be pointing to
1528 * a protocol struct that has been reallocated.
1529 * Locking: runtimeLock must be read- or write-locked by the caller
1530 **********************************************************************/
1531 static protocol_t *remapProtocol(protocol_ref_t proto)
1533 runtimeLock.assertLocked();
1535 protocol_t *newproto = (protocol_t *)
1536 getProtocol(((protocol_t *)proto)->mangledName);
1537 return newproto ? newproto : (protocol_t *)proto;
1541 /***********************************************************************
1543 * Fix up a protocol ref, in case the protocol referenced has been reallocated.
1544 * Locking: runtimeLock must be read- or write-locked by the caller
1545 **********************************************************************/
1546 static size_t UnfixedProtocolReferences;
1547 static void remapProtocolRef(protocol_t **protoref)
1549 runtimeLock.assertLocked();
1551 protocol_t *newproto = remapProtocol((protocol_ref_t)*protoref);
1552 if (*protoref != newproto) {
1553 *protoref = newproto;
1554 UnfixedProtocolReferences++;
1559 /***********************************************************************
1561 * Slides a class's ivars to accommodate the given superclass size.
1562 * Ivars are NOT compacted to compensate for a superclass that shrunk.
1563 * Locking: runtimeLock must be held by the caller.
1564 **********************************************************************/
1565 static void moveIvars(class_ro_t *ro, uint32_t superSize)
1567 runtimeLock.assertWriting();
1571 assert(superSize > ro->instanceStart);
1572 diff = superSize - ro->instanceStart;
1575 // Find maximum alignment in this class's ivars
1576 uint32_t maxAlignment = 1;
1577 for (const auto& ivar : *ro->ivars) {
1578 if (!ivar.offset) continue; // anonymous bitfield
1580 uint32_t alignment = ivar.alignment();
1581 if (alignment > maxAlignment) maxAlignment = alignment;
1584 // Compute a slide value that preserves that alignment
1585 uint32_t alignMask = maxAlignment - 1;
1586 diff = (diff + alignMask) & ~alignMask;
1588 // Slide all of this class's ivars en masse
1589 for (const auto& ivar : *ro->ivars) {
1590 if (!ivar.offset) continue; // anonymous bitfield
1592 uint32_t oldOffset = (uint32_t)*ivar.offset;
1593 uint32_t newOffset = oldOffset + diff;
1594 *ivar.offset = newOffset;
1597 _objc_inform("IVARS: offset %u -> %u for %s "
1598 "(size %u, align %u)",
1599 oldOffset, newOffset, ivar.name,
1600 ivar.size, ivar.alignment());
1605 *(uint32_t *)&ro->instanceStart += diff;
1606 *(uint32_t *)&ro->instanceSize += diff;
1610 static void reconcileInstanceVariables(Class cls, Class supercls, const class_ro_t*& ro)
1612 class_rw_t *rw = cls->data();
1615 assert(!cls->isMetaClass());
1617 /* debug: print them all before sliding
1619 for (const auto& ivar : *ro->ivars) {
1620 if (!ivar.offset) continue; // anonymous bitfield
1622 _objc_inform("IVARS: %s.%s (offset %u, size %u, align %u)",
1623 ro->name, ivar.name,
1624 *ivar.offset, ivar.size, ivar.alignment());
1629 // Non-fragile ivars - reconcile this class with its superclass
1630 const class_ro_t *super_ro = supercls->data()->ro;
1632 if (DebugNonFragileIvars) {
1633 // Debugging: Force non-fragile ivars to slide.
1634 // Intended to find compiler, runtime, and program bugs.
1635 // If it fails with this and works without, you have a problem.
1637 // Operation: Reset everything to 0 + misalignment.
1638 // Then force the normal sliding logic to push everything back.
1640 // Exceptions: root classes, metaclasses, *NSCF* classes,
1641 // __CF* classes, NSConstantString, NSSimpleCString
1643 // (already know it's not root because supercls != nil)
1644 const char *clsname = cls->mangledName();
1645 if (!strstr(clsname, "NSCF") &&
1646 0 != strncmp(clsname, "__CF", 4) &&
1647 0 != strcmp(clsname, "NSConstantString") &&
1648 0 != strcmp(clsname, "NSSimpleCString"))
1650 uint32_t oldStart = ro->instanceStart;
1651 class_ro_t *ro_w = make_ro_writeable(rw);
1654 // Find max ivar alignment in class.
1655 // default to word size to simplify ivar update
1656 uint32_t alignment = 1<<WORD_SHIFT;
1658 for (const auto& ivar : *ro->ivars) {
1659 if (ivar.alignment() > alignment) {
1660 alignment = ivar.alignment();
1664 uint32_t misalignment = ro->instanceStart % alignment;
1665 uint32_t delta = ro->instanceStart - misalignment;
1666 ro_w->instanceStart = misalignment;
1667 ro_w->instanceSize -= delta;
1670 _objc_inform("IVARS: DEBUG: forcing ivars for class '%s' "
1671 "to slide (instanceStart %zu -> %zu)",
1672 cls->nameForLogging(), (size_t)oldStart,
1673 (size_t)ro->instanceStart);
1677 for (const auto& ivar : *ro->ivars) {
1678 if (!ivar.offset) continue; // anonymous bitfield
1679 *ivar.offset -= delta;
1685 if (ro->instanceStart >= super_ro->instanceSize) {
1686 // Superclass has not overgrown its space. We're done here.
1689 // fixme can optimize for "class has no new ivars", etc
1691 if (ro->instanceStart < super_ro->instanceSize) {
1692 // Superclass has changed size. This class's ivars must move.
1693 // Also slide layout bits in parallel.
1694 // This code is incapable of compacting the subclass to
1695 // compensate for a superclass that shrunk, so don't do that.
1697 _objc_inform("IVARS: sliding ivars for class %s "
1698 "(superclass was %u bytes, now %u)",
1699 cls->nameForLogging(), ro->instanceStart,
1700 super_ro->instanceSize);
1702 class_ro_t *ro_w = make_ro_writeable(rw);
1704 moveIvars(ro_w, super_ro->instanceSize);
1705 gdb_objc_class_changed(cls, OBJC_CLASS_IVARS_CHANGED, ro->name);
1710 /***********************************************************************
1712 * Performs first-time initialization on class cls,
1713 * including allocating its read-write data.
1714 * Returns the real class structure for the class.
1715 * Locking: runtimeLock must be write-locked by the caller
1716 **********************************************************************/
1717 static Class realizeClass(Class cls)
1719 runtimeLock.assertWriting();
1721 const class_ro_t *ro;
1727 if (!cls) return nil;
1728 if (cls->isRealized()) return cls;
1729 assert(cls == remapClass(cls));
1731 // fixme verify class is not in an un-dlopened part of the shared cache?
1733 ro = (const class_ro_t *)cls->data();
1734 if (ro->flags & RO_FUTURE) {
1735 // This was a future class. rw data is already allocated.
1737 ro = cls->data()->ro;
1738 cls->changeInfo(RW_REALIZED|RW_REALIZING, RW_FUTURE);
1740 // Normal class. Allocate writeable class data.
1741 rw = (class_rw_t *)calloc(sizeof(class_rw_t), 1);
1743 rw->flags = RW_REALIZED|RW_REALIZING;
1747 isMeta = ro->flags & RO_META;
1749 rw->version = isMeta ? 7 : 0; // old runtime went up to 6
1752 // Choose an index for this class.
1753 // Sets cls->instancesRequireRawIsa if indexes no more indexes are available
1754 cls->chooseClassArrayIndex();
1756 if (PrintConnecting) {
1757 _objc_inform("CLASS: realizing class '%s'%s %p %p #%u",
1758 cls->nameForLogging(), isMeta ? " (meta)" : "",
1759 (void*)cls, ro, cls->classArrayIndex());
1762 // Realize superclass and metaclass, if they aren't already.
1763 // This needs to be done after RW_REALIZED is set above, for root classes.
1764 // This needs to be done after class index is chosen, for root metaclasses.
1765 supercls = realizeClass(remapClass(cls->superclass));
1766 metacls = realizeClass(remapClass(cls->ISA()));
1768 #if SUPPORT_NONPOINTER_ISA
1769 // Disable non-pointer isa for some classes and/or platforms.
1770 // Set instancesRequireRawIsa.
1771 bool instancesRequireRawIsa = cls->instancesRequireRawIsa();
1772 bool rawIsaIsInherited = false;
1773 static bool hackedDispatch = false;
1775 if (DisableNonpointerIsa) {
1776 // Non-pointer isa disabled by environment or app SDK version
1777 instancesRequireRawIsa = true;
1779 else if (!hackedDispatch && !(ro->flags & RO_META) &&
1780 0 == strcmp(ro->name, "OS_object"))
1782 // hack for libdispatch et al - isa also acts as vtable pointer
1783 hackedDispatch = true;
1784 instancesRequireRawIsa = true;
1786 else if (supercls && supercls->superclass &&
1787 supercls->instancesRequireRawIsa())
1789 // This is also propagated by addSubclass()
1790 // but nonpointer isa setup needs it earlier.
1791 // Special case: instancesRequireRawIsa does not propagate
1792 // from root class to root metaclass
1793 instancesRequireRawIsa = true;
1794 rawIsaIsInherited = true;
1797 if (instancesRequireRawIsa) {
1798 cls->setInstancesRequireRawIsa(rawIsaIsInherited);
1800 // SUPPORT_NONPOINTER_ISA
1803 // Update superclass and metaclass in case of remapping
1804 cls->superclass = supercls;
1805 cls->initClassIsa(metacls);
1807 // Reconcile instance variable offsets / layout.
1808 // This may reallocate class_ro_t, updating our ro variable.
1809 if (supercls && !isMeta) reconcileInstanceVariables(cls, supercls, ro);
1811 // Set fastInstanceSize if it wasn't set already.
1812 cls->setInstanceSize(ro->instanceSize);
1814 // Copy some flags from ro to rw
1815 if (ro->flags & RO_HAS_CXX_STRUCTORS) {
1816 cls->setHasCxxDtor();
1817 if (! (ro->flags & RO_HAS_CXX_DTOR_ONLY)) {
1818 cls->setHasCxxCtor();
1822 // Connect this class to its superclass's subclass lists
1824 addSubclass(supercls, cls);
1829 // Attach categories
1830 methodizeClass(cls);
1836 /***********************************************************************
1837 * missingWeakSuperclass
1838 * Return YES if some superclass of cls was weak-linked and is missing.
1839 **********************************************************************/
1841 missingWeakSuperclass(Class cls)
1843 assert(!cls->isRealized());
1845 if (!cls->superclass) {
1846 // superclass nil. This is normal for root classes only.
1847 return (!(cls->data()->flags & RO_ROOT));
1849 // superclass not nil. Check if a higher superclass is missing.
1850 Class supercls = remapClass(cls->superclass);
1851 assert(cls != cls->superclass);
1852 assert(cls != supercls);
1853 if (!supercls) return YES;
1854 if (supercls->isRealized()) return NO;
1855 return missingWeakSuperclass(supercls);
1860 /***********************************************************************
1861 * realizeAllClassesInImage
1862 * Non-lazily realizes all unrealized classes in the given image.
1863 * Locking: runtimeLock must be held by the caller.
1864 **********************************************************************/
1865 static void realizeAllClassesInImage(header_info *hi)
1867 runtimeLock.assertWriting();
1870 classref_t *classlist;
1872 if (hi->areAllClassesRealized()) return;
1874 classlist = _getObjc2ClassList(hi, &count);
1876 for (i = 0; i < count; i++) {
1877 realizeClass(remapClass(classlist[i]));
1880 hi->setAllClassesRealized(YES);
1884 /***********************************************************************
1886 * Non-lazily realizes all unrealized classes in all known images.
1887 * Locking: runtimeLock must be held by the caller.
1888 **********************************************************************/
1889 static void realizeAllClasses(void)
1891 runtimeLock.assertWriting();
1894 for (hi = FirstHeader; hi; hi = hi->getNext()) {
1895 realizeAllClassesInImage(hi);
1900 /***********************************************************************
1901 * _objc_allocateFutureClass
1902 * Allocate an unresolved future class for the given class name.
1903 * Returns any existing allocation if one was already made.
1904 * Assumes the named class doesn't exist yet.
1905 * Locking: acquires runtimeLock
1906 **********************************************************************/
1907 Class _objc_allocateFutureClass(const char *name)
1909 rwlock_writer_t lock(runtimeLock);
1912 NXMapTable *map = futureNamedClasses();
1914 if ((cls = (Class)NXMapGet(map, name))) {
1915 // Already have a future class for this name.
1919 cls = _calloc_class(sizeof(objc_class));
1920 addFutureNamedClass(name, cls);
1926 /***********************************************************************
1927 * objc_getFutureClass. Return the id of the named class.
1928 * If the class does not exist, return an uninitialized class
1929 * structure that will be used for the class when and if it
1932 **********************************************************************/
1933 Class objc_getFutureClass(const char *name)
1937 // YES unconnected, NO class handler
1938 // (unconnected is OK because it will someday be the real class)
1939 cls = look_up_class(name, YES, NO);
1942 _objc_inform("FUTURE: found %p already in use for %s",
1949 // No class or future class with that name yet. Make one.
1950 // fixme not thread-safe with respect to
1951 // simultaneous library load or getFutureClass.
1952 return _objc_allocateFutureClass(name);
1956 BOOL _class_isFutureClass(Class cls)
1958 return cls && cls->isFuture();
1962 /***********************************************************************
1963 * _objc_flush_caches
1964 * Flushes all caches.
1965 * (Historical behavior: flush caches for cls, its metaclass,
1966 * and subclasses thereof. Nil flushes all classes.)
1967 * Locking: acquires runtimeLock
1968 **********************************************************************/
1969 static void flushCaches(Class cls)
1971 runtimeLock.assertWriting();
1973 mutex_locker_t lock(cacheUpdateLock);
1976 foreach_realized_class_and_subclass(cls, ^(Class c){
1977 cache_erase_nolock(c);
1981 foreach_realized_class_and_metaclass(^(Class c){
1982 cache_erase_nolock(c);
1988 void _objc_flush_caches(Class cls)
1991 rwlock_writer_t lock(runtimeLock);
1993 if (cls && cls->superclass && cls != cls->getIsa()) {
1994 flushCaches(cls->getIsa());
1996 // cls is a root class or root metaclass. Its metaclass is itself
1997 // or a subclass so the metaclass caches were already flushed.
2002 // collectALot if cls==nil
2003 mutex_locker_t lock(cacheUpdateLock);
2004 cache_collect(true);
2009 /***********************************************************************
2011 * Process the given images which are being mapped in by dyld.
2012 * Calls ABI-agnostic code after taking ABI-specific locks.
2014 * Locking: write-locks runtimeLock
2015 **********************************************************************/
2017 map_images(unsigned count, const char * const paths[],
2018 const struct mach_header * const mhdrs[])
2020 rwlock_writer_t lock(runtimeLock);
2021 return map_images_nolock(count, paths, mhdrs);
2025 /***********************************************************************
2027 * Process +load in the given images which are being mapped in by dyld.
2029 * Locking: write-locks runtimeLock and loadMethodLock
2030 **********************************************************************/
2031 extern bool hasLoadMethods(const headerType *mhdr);
2032 extern void prepare_load_methods(const headerType *mhdr);
2035 load_images(const char *path __unused, const struct mach_header *mh)
2037 // Return without taking locks if there are no +load methods here.
2038 if (!hasLoadMethods((const headerType *)mh)) return;
2040 recursive_mutex_locker_t lock(loadMethodLock);
2042 // Discover load methods
2044 rwlock_writer_t lock2(runtimeLock);
2045 prepare_load_methods((const headerType *)mh);
2048 // Call +load methods (without runtimeLock - re-entrant)
2049 call_load_methods();
2053 /***********************************************************************
2055 * Process the given image which is about to be unmapped by dyld.
2057 * Locking: write-locks runtimeLock and loadMethodLock
2058 **********************************************************************/
2060 unmap_image(const char *path __unused, const struct mach_header *mh)
2062 recursive_mutex_locker_t lock(loadMethodLock);
2063 rwlock_writer_t lock2(runtimeLock);
2064 unmap_image_nolock(mh);
2070 /***********************************************************************
2072 * Preflight check in advance of readClass() from an image.
2073 **********************************************************************/
2074 bool mustReadClasses(header_info *hi)
2078 // If the image is not preoptimized then we must read classes.
2079 if (!hi->isPreoptimized()) {
2080 reason = nil; // Don't log this one because it is noisy.
2084 // If iOS simulator then we must read classes.
2085 #if TARGET_OS_SIMULATOR
2086 reason = "the image is for iOS simulator";
2090 assert(!hi->isBundle()); // no MH_BUNDLE in shared cache
2092 // If the image may have missing weak superclasses then we must read classes
2093 if (!noMissingWeakSuperclasses()) {
2094 reason = "the image may contain classes with missing weak superclasses";
2098 // If there are unresolved future classes then we must read classes.
2099 if (haveFutureNamedClasses()) {
2100 reason = "there are unresolved future classes pending";
2104 // readClass() does not need to do anything.
2108 if (PrintPreopt && reason) {
2109 _objc_inform("PREOPTIMIZATION: reading classes manually from %s "
2110 "because %s", hi->fname(), reason);
2116 /***********************************************************************
2118 * Read a class and metaclass as written by a compiler.
2119 * Returns the new class pointer. This could be:
2121 * - nil (cls has a missing weak-linked superclass)
2122 * - something else (space for this class was reserved by a future class)
2124 * Note that all work performed by this function is preflighted by
2125 * mustReadClasses(). Do not change this function without updating that one.
2127 * Locking: runtimeLock acquired by map_images or objc_readClassPair
2128 **********************************************************************/
2129 Class readClass(Class cls, bool headerIsBundle, bool headerIsPreoptimized)
2131 const char *mangledName = cls->mangledName();
2133 if (missingWeakSuperclass(cls)) {
2134 // No superclass (probably weak-linked).
2135 // Disavow any knowledge of this subclass.
2136 if (PrintConnecting) {
2137 _objc_inform("CLASS: IGNORING class '%s' with "
2138 "missing weak-linked superclass",
2139 cls->nameForLogging());
2141 addRemappedClass(cls, nil);
2142 cls->superclass = nil;
2146 // Note: Class __ARCLite__'s hack does not go through here.
2147 // Class structure fixups that apply to it also need to be
2148 // performed in non-lazy realization below.
2150 // These fields should be set to zero because of the
2151 // binding of _objc_empty_vtable, but OS X 10.8's dyld
2152 // does not bind shared cache absolute symbols as expected.
2153 // This (and the __ARCLite__ hack below) can be removed
2154 // once the simulator drops 10.8 support.
2155 #if TARGET_OS_SIMULATOR
2156 if (cls->cache._mask) cls->cache._mask = 0;
2157 if (cls->cache._occupied) cls->cache._occupied = 0;
2158 if (cls->ISA()->cache._mask) cls->ISA()->cache._mask = 0;
2159 if (cls->ISA()->cache._occupied) cls->ISA()->cache._occupied = 0;
2162 Class replacing = nil;
2163 if (Class newCls = popFutureNamedClass(mangledName)) {
2164 // This name was previously allocated as a future class.
2165 // Copy objc_class to future class's struct.
2166 // Preserve future's rw data block.
2168 if (newCls->isSwift()) {
2169 _objc_fatal("Can't complete future class request for '%s' "
2170 "because the real class is too big.",
2171 cls->nameForLogging());
2174 class_rw_t *rw = newCls->data();
2175 const class_ro_t *old_ro = rw->ro;
2176 memcpy(newCls, cls, sizeof(objc_class));
2177 rw->ro = (class_ro_t *)newCls->data();
2178 newCls->setData(rw);
2179 freeIfMutable((char *)old_ro->name);
2180 free((void *)old_ro);
2182 addRemappedClass(cls, newCls);
2188 if (headerIsPreoptimized && !replacing) {
2189 // class list built in shared cache
2190 // fixme strict assert doesn't work because of duplicates
2191 // assert(cls == getClass(name));
2192 assert(getClass(mangledName));
2194 addNamedClass(cls, mangledName, replacing);
2197 // for future reference: shared cache never contains MH_BUNDLEs
2198 if (headerIsBundle) {
2199 cls->data()->flags |= RO_FROM_BUNDLE;
2200 cls->ISA()->data()->flags |= RO_FROM_BUNDLE;
2207 /***********************************************************************
2209 * Read a protocol as written by a compiler.
2210 **********************************************************************/
2212 readProtocol(protocol_t *newproto, Class protocol_class,
2213 NXMapTable *protocol_map,
2214 bool headerIsPreoptimized, bool headerIsBundle)
2216 // This is not enough to make protocols in unloaded bundles safe,
2217 // but it does prevent crashes when looking up unrelated protocols.
2218 auto insertFn = headerIsBundle ? NXMapKeyCopyingInsert : NXMapInsert;
2220 protocol_t *oldproto = (protocol_t *)getProtocol(newproto->mangledName);
2223 // Some other definition already won.
2224 if (PrintProtocols) {
2225 _objc_inform("PROTOCOLS: protocol at %p is %s "
2226 "(duplicate of %p)",
2227 newproto, oldproto->nameForLogging(), oldproto);
2230 else if (headerIsPreoptimized) {
2231 // Shared cache initialized the protocol object itself,
2232 // but in order to allow out-of-cache replacement we need
2233 // to add it to the protocol table now.
2235 protocol_t *cacheproto = (protocol_t *)
2236 getPreoptimizedProtocol(newproto->mangledName);
2237 protocol_t *installedproto;
2238 if (cacheproto && cacheproto != newproto) {
2239 // Another definition in the shared cache wins (because
2240 // everything in the cache was fixed up to point to it).
2241 installedproto = cacheproto;
2244 // This definition wins.
2245 installedproto = newproto;
2248 assert(installedproto->getIsa() == protocol_class);
2249 assert(installedproto->size >= sizeof(protocol_t));
2250 insertFn(protocol_map, installedproto->mangledName,
2253 if (PrintProtocols) {
2254 _objc_inform("PROTOCOLS: protocol at %p is %s",
2255 installedproto, installedproto->nameForLogging());
2256 if (newproto != installedproto) {
2257 _objc_inform("PROTOCOLS: protocol at %p is %s "
2258 "(duplicate of %p)",
2259 newproto, installedproto->nameForLogging(),
2264 else if (newproto->size >= sizeof(protocol_t)) {
2265 // New protocol from an un-preoptimized image
2266 // with sufficient storage. Fix it up in place.
2267 // fixme duplicate protocols from unloadable bundle
2268 newproto->initIsa(protocol_class); // fixme pinned
2269 insertFn(protocol_map, newproto->mangledName, newproto);
2270 if (PrintProtocols) {
2271 _objc_inform("PROTOCOLS: protocol at %p is %s",
2272 newproto, newproto->nameForLogging());
2276 // New protocol from an un-preoptimized image
2277 // with insufficient storage. Reallocate it.
2278 // fixme duplicate protocols from unloadable bundle
2279 size_t size = max(sizeof(protocol_t), (size_t)newproto->size);
2280 protocol_t *installedproto = (protocol_t *)calloc(size, 1);
2281 memcpy(installedproto, newproto, newproto->size);
2282 installedproto->size = (typeof(installedproto->size))size;
2284 installedproto->initIsa(protocol_class); // fixme pinned
2285 insertFn(protocol_map, installedproto->mangledName, installedproto);
2286 if (PrintProtocols) {
2287 _objc_inform("PROTOCOLS: protocol at %p is %s ",
2288 installedproto, installedproto->nameForLogging());
2289 _objc_inform("PROTOCOLS: protocol at %p is %s "
2290 "(reallocated to %p)",
2291 newproto, installedproto->nameForLogging(),
2297 /***********************************************************************
2299 * Perform initial processing of the headers in the linked
2300 * list beginning with headerList.
2302 * Called by: map_images_nolock
2304 * Locking: runtimeLock acquired by map_images
2305 **********************************************************************/
2306 void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int unoptimizedTotalClasses)
2312 Class *resolvedFutureClasses = nil;
2313 size_t resolvedFutureClassCount = 0;
2314 static bool doneOnce;
2315 TimeLogger ts(PrintImageTimes);
2317 runtimeLock.assertWriting();
2319 #define EACH_HEADER \
2321 hIndex < hCount && (hi = hList[hIndex]); \
2327 #if SUPPORT_NONPOINTER_ISA
2328 // Disable non-pointer isa under some conditions.
2330 # if SUPPORT_INDEXED_ISA
2331 // Disable nonpointer isa if any image contains old Swift code
2333 if (hi->info()->containsSwift() &&
2334 hi->info()->swiftVersion() < objc_image_info::SwiftVersion3)
2336 DisableNonpointerIsa = true;
2338 _objc_inform("RAW ISA: disabling non-pointer isa because "
2339 "the app or a framework contains Swift code "
2340 "older than Swift 3.0");
2348 // Disable non-pointer isa if the app is too old
2349 // (linked before OS X 10.11)
2350 if (dyld_get_program_sdk_version() < DYLD_MACOSX_VERSION_10_11) {
2351 DisableNonpointerIsa = true;
2353 _objc_inform("RAW ISA: disabling non-pointer isa because "
2354 "the app is too old (SDK version " SDK_FORMAT ")",
2355 FORMAT_SDK(dyld_get_program_sdk_version()));
2359 // Disable non-pointer isa if the app has a __DATA,__objc_rawisa section
2360 // New apps that load old extensions may need this.
2362 if (hi->mhdr()->filetype != MH_EXECUTE) continue;
2364 if (getsectiondata(hi->mhdr(), "__DATA", "__objc_rawisa", &size)) {
2365 DisableNonpointerIsa = true;
2367 _objc_inform("RAW ISA: disabling non-pointer isa because "
2368 "the app has a __DATA,__objc_rawisa section");
2371 break; // assume only one MH_EXECUTE image
2377 if (DisableTaggedPointers) {
2378 disableTaggedPointers();
2381 if (PrintConnecting) {
2382 _objc_inform("CLASS: found %d classes during launch", totalClasses);
2386 // Preoptimized classes don't go in this table.
2387 // 4/3 is NXMapTable's load factor
2388 int namedClassesSize =
2389 (isPreoptimized() ? unoptimizedTotalClasses : totalClasses) * 4 / 3;
2390 gdb_objc_realized_classes =
2391 NXCreateMapTable(NXStrValueMapPrototype, namedClassesSize);
2393 ts.log("IMAGE TIMES: first time tasks");
2397 // Discover classes. Fix up unresolved future classes. Mark bundle classes.
2400 if (! mustReadClasses(hi)) {
2401 // Image is sufficiently optimized that we need not call readClass()
2405 bool headerIsBundle = hi->isBundle();
2406 bool headerIsPreoptimized = hi->isPreoptimized();
2408 classref_t *classlist = _getObjc2ClassList(hi, &count);
2409 for (i = 0; i < count; i++) {
2410 Class cls = (Class)classlist[i];
2411 Class newCls = readClass(cls, headerIsBundle, headerIsPreoptimized);
2413 if (newCls != cls && newCls) {
2414 // Class was moved but not deleted. Currently this occurs
2415 // only when the new class resolved a future class.
2416 // Non-lazily realize the class below.
2417 resolvedFutureClasses = (Class *)
2418 realloc(resolvedFutureClasses,
2419 (resolvedFutureClassCount+1) * sizeof(Class));
2420 resolvedFutureClasses[resolvedFutureClassCount++] = newCls;
2425 ts.log("IMAGE TIMES: discover classes");
2427 // Fix up remapped classes
2428 // Class list and nonlazy class list remain unremapped.
2429 // Class refs and super refs are remapped for message dispatching.
2431 if (!noClassesRemapped()) {
2433 Class *classrefs = _getObjc2ClassRefs(hi, &count);
2434 for (i = 0; i < count; i++) {
2435 remapClassRef(&classrefs[i]);
2437 // fixme why doesn't test future1 catch the absence of this?
2438 classrefs = _getObjc2SuperRefs(hi, &count);
2439 for (i = 0; i < count; i++) {
2440 remapClassRef(&classrefs[i]);
2445 ts.log("IMAGE TIMES: remap classes");
2447 // Fix up @selector references
2448 static size_t UnfixedSelectors;
2451 if (hi->isPreoptimized()) continue;
2453 bool isBundle = hi->isBundle();
2454 SEL *sels = _getObjc2SelectorRefs(hi, &count);
2455 UnfixedSelectors += count;
2456 for (i = 0; i < count; i++) {
2457 const char *name = sel_cname(sels[i]);
2458 sels[i] = sel_registerNameNoLock(name, isBundle);
2463 ts.log("IMAGE TIMES: fix up selector references");
2466 // Fix up old objc_msgSend_fixup call sites
2468 message_ref_t *refs = _getObjc2MessageRefs(hi, &count);
2469 if (count == 0) continue;
2472 _objc_inform("VTABLES: repairing %zu unsupported vtable dispatch "
2473 "call sites in %s", count, hi->fname());
2475 for (i = 0; i < count; i++) {
2476 fixupMessageRef(refs+i);
2480 ts.log("IMAGE TIMES: fix up objc_msgSend_fixup");
2483 // Discover protocols. Fix up protocol refs.
2485 extern objc_class OBJC_CLASS_$_Protocol;
2486 Class cls = (Class)&OBJC_CLASS_$_Protocol;
2488 NXMapTable *protocol_map = protocols();
2489 bool isPreoptimized = hi->isPreoptimized();
2490 bool isBundle = hi->isBundle();
2492 protocol_t **protolist = _getObjc2ProtocolList(hi, &count);
2493 for (i = 0; i < count; i++) {
2494 readProtocol(protolist[i], cls, protocol_map,
2495 isPreoptimized, isBundle);
2499 ts.log("IMAGE TIMES: discover protocols");
2501 // Fix up @protocol references
2502 // Preoptimized images may have the right
2503 // answer already but we don't know for sure.
2505 protocol_t **protolist = _getObjc2ProtocolRefs(hi, &count);
2506 for (i = 0; i < count; i++) {
2507 remapProtocolRef(&protolist[i]);
2511 ts.log("IMAGE TIMES: fix up @protocol references");
2513 // Realize non-lazy classes (for +load methods and static instances)
2515 classref_t *classlist =
2516 _getObjc2NonlazyClassList(hi, &count);
2517 for (i = 0; i < count; i++) {
2518 Class cls = remapClass(classlist[i]);
2521 // hack for class __ARCLite__, which didn't get this above
2522 #if TARGET_OS_SIMULATOR
2523 if (cls->cache._buckets == (void*)&_objc_empty_cache &&
2524 (cls->cache._mask || cls->cache._occupied))
2526 cls->cache._mask = 0;
2527 cls->cache._occupied = 0;
2529 if (cls->ISA()->cache._buckets == (void*)&_objc_empty_cache &&
2530 (cls->ISA()->cache._mask || cls->ISA()->cache._occupied))
2532 cls->ISA()->cache._mask = 0;
2533 cls->ISA()->cache._occupied = 0;
2541 ts.log("IMAGE TIMES: realize non-lazy classes");
2543 // Realize newly-resolved future classes, in case CF manipulates them
2544 if (resolvedFutureClasses) {
2545 for (i = 0; i < resolvedFutureClassCount; i++) {
2546 realizeClass(resolvedFutureClasses[i]);
2547 resolvedFutureClasses[i]->setInstancesRequireRawIsa(false/*inherited*/);
2549 free(resolvedFutureClasses);
2552 ts.log("IMAGE TIMES: realize future classes");
2554 // Discover categories.
2556 category_t **catlist =
2557 _getObjc2CategoryList(hi, &count);
2558 bool hasClassProperties = hi->info()->hasCategoryClassProperties();
2560 for (i = 0; i < count; i++) {
2561 category_t *cat = catlist[i];
2562 Class cls = remapClass(cat->cls);
2565 // Category's target class is missing (probably weak-linked).
2566 // Disavow any knowledge of this category.
2568 if (PrintConnecting) {
2569 _objc_inform("CLASS: IGNORING category \?\?\?(%s) %p with "
2570 "missing weak-linked target class",
2576 // Process this category.
2577 // First, register the category with its target class.
2578 // Then, rebuild the class's method lists (etc) if
2579 // the class is realized.
2580 bool classExists = NO;
2581 if (cat->instanceMethods || cat->protocols
2582 || cat->instanceProperties)
2584 addUnattachedCategoryForClass(cat, cls, hi);
2585 if (cls->isRealized()) {
2586 remethodizeClass(cls);
2589 if (PrintConnecting) {
2590 _objc_inform("CLASS: found category -%s(%s) %s",
2591 cls->nameForLogging(), cat->name,
2592 classExists ? "on existing class" : "");
2596 if (cat->classMethods || cat->protocols
2597 || (hasClassProperties && cat->_classProperties))
2599 addUnattachedCategoryForClass(cat, cls->ISA(), hi);
2600 if (cls->ISA()->isRealized()) {
2601 remethodizeClass(cls->ISA());
2603 if (PrintConnecting) {
2604 _objc_inform("CLASS: found category +%s(%s)",
2605 cls->nameForLogging(), cat->name);
2611 ts.log("IMAGE TIMES: discover categories");
2613 // Category discovery MUST BE LAST to avoid potential races
2614 // when other threads call the new category code before
2615 // this thread finishes its fixups.
2617 // +load handled by prepare_load_methods()
2619 if (DebugNonFragileIvars) {
2620 realizeAllClasses();
2624 // Print preoptimization statistics
2626 static unsigned int PreoptTotalMethodLists;
2627 static unsigned int PreoptOptimizedMethodLists;
2628 static unsigned int PreoptTotalClasses;
2629 static unsigned int PreoptOptimizedClasses;
2632 if (hi->isPreoptimized()) {
2633 _objc_inform("PREOPTIMIZATION: honoring preoptimized selectors "
2634 "in %s", hi->fname());
2636 else if (hi->info()->optimizedByDyld()) {
2637 _objc_inform("PREOPTIMIZATION: IGNORING preoptimized selectors "
2638 "in %s", hi->fname());
2641 classref_t *classlist = _getObjc2ClassList(hi, &count);
2642 for (i = 0; i < count; i++) {
2643 Class cls = remapClass(classlist[i]);
2646 PreoptTotalClasses++;
2647 if (hi->isPreoptimized()) {
2648 PreoptOptimizedClasses++;
2651 const method_list_t *mlist;
2652 if ((mlist = ((class_ro_t *)cls->data())->baseMethods())) {
2653 PreoptTotalMethodLists++;
2654 if (mlist->isFixedUp()) {
2655 PreoptOptimizedMethodLists++;
2658 if ((mlist=((class_ro_t *)cls->ISA()->data())->baseMethods())) {
2659 PreoptTotalMethodLists++;
2660 if (mlist->isFixedUp()) {
2661 PreoptOptimizedMethodLists++;
2667 _objc_inform("PREOPTIMIZATION: %zu selector references not "
2668 "pre-optimized", UnfixedSelectors);
2669 _objc_inform("PREOPTIMIZATION: %u/%u (%.3g%%) method lists pre-sorted",
2670 PreoptOptimizedMethodLists, PreoptTotalMethodLists,
2671 PreoptTotalMethodLists
2672 ? 100.0*PreoptOptimizedMethodLists/PreoptTotalMethodLists
2674 _objc_inform("PREOPTIMIZATION: %u/%u (%.3g%%) classes pre-registered",
2675 PreoptOptimizedClasses, PreoptTotalClasses,
2677 ? 100.0*PreoptOptimizedClasses/PreoptTotalClasses
2679 _objc_inform("PREOPTIMIZATION: %zu protocol references not "
2680 "pre-optimized", UnfixedProtocolReferences);
2687 /***********************************************************************
2688 * prepare_load_methods
2689 * Schedule +load for classes in this image, any un-+load-ed
2690 * superclasses in other images, and any categories in this image.
2691 **********************************************************************/
2692 // Recursively schedule +load for cls and any un-+load-ed superclasses.
2693 // cls must already be connected.
2694 static void schedule_class_load(Class cls)
2697 assert(cls->isRealized()); // _read_images should realize
2699 if (cls->data()->flags & RW_LOADED) return;
2701 // Ensure superclass-first ordering
2702 schedule_class_load(cls->superclass);
2704 add_class_to_loadable_list(cls);
2705 cls->setInfo(RW_LOADED);
2708 // Quick scan for +load methods that doesn't take a lock.
2709 bool hasLoadMethods(const headerType *mhdr)
2712 if (_getObjc2NonlazyClassList(mhdr, &count) && count > 0) return true;
2713 if (_getObjc2NonlazyCategoryList(mhdr, &count) && count > 0) return true;
2717 void prepare_load_methods(const headerType *mhdr)
2721 runtimeLock.assertWriting();
2723 classref_t *classlist =
2724 _getObjc2NonlazyClassList(mhdr, &count);
2725 for (i = 0; i < count; i++) {
2726 schedule_class_load(remapClass(classlist[i]));
2729 category_t **categorylist = _getObjc2NonlazyCategoryList(mhdr, &count);
2730 for (i = 0; i < count; i++) {
2731 category_t *cat = categorylist[i];
2732 Class cls = remapClass(cat->cls);
2733 if (!cls) continue; // category for ignored weak-linked class
2735 assert(cls->ISA()->isRealized());
2736 add_category_to_loadable_list(cat);
2741 /***********************************************************************
2743 * Only handles MH_BUNDLE for now.
2744 * Locking: write-lock and loadMethodLock acquired by unmap_image
2745 **********************************************************************/
2746 void _unload_image(header_info *hi)
2750 loadMethodLock.assertLocked();
2751 runtimeLock.assertWriting();
2753 // Unload unattached categories and categories waiting for +load.
2755 category_t **catlist = _getObjc2CategoryList(hi, &count);
2756 for (i = 0; i < count; i++) {
2757 category_t *cat = catlist[i];
2758 if (!cat) continue; // category for ignored weak-linked class
2759 Class cls = remapClass(cat->cls);
2760 assert(cls); // shouldn't have live category for dead class
2762 // fixme for MH_DYLIB cat's class may have been unloaded already
2765 removeUnattachedCategoryForClass(cat, cls);
2768 remove_category_from_loadable_list(cat);
2773 // Gather classes from both __DATA,__objc_clslist
2774 // and __DATA,__objc_nlclslist. arclite's hack puts a class in the latter
2775 // only, and we need to unload that class if we unload an arclite image.
2777 NXHashTable *classes = NXCreateHashTable(NXPtrPrototype, 0, nil);
2778 classref_t *classlist;
2780 classlist = _getObjc2ClassList(hi, &count);
2781 for (i = 0; i < count; i++) {
2782 Class cls = remapClass(classlist[i]);
2783 if (cls) NXHashInsert(classes, cls);
2786 classlist = _getObjc2NonlazyClassList(hi, &count);
2787 for (i = 0; i < count; i++) {
2788 Class cls = remapClass(classlist[i]);
2789 if (cls) NXHashInsert(classes, cls);
2792 // First detach classes from each other. Then free each class.
2793 // This avoid bugs where this loop unloads a subclass before its superclass
2798 hs = NXInitHashState(classes);
2799 while (NXNextHashState(classes, &hs, (void**)&cls)) {
2800 remove_class_from_loadable_list(cls);
2801 detach_class(cls->ISA(), YES);
2802 detach_class(cls, NO);
2804 hs = NXInitHashState(classes);
2805 while (NXNextHashState(classes, &hs, (void**)&cls)) {
2806 free_class(cls->ISA());
2810 NXFreeHashTable(classes);
2812 // XXX FIXME -- Clean up protocols:
2813 // <rdar://problem/9033191> Support unloading protocols at dylib/image unload time
2815 // fixme DebugUnload
2819 /***********************************************************************
2820 * method_getDescription
2821 * Returns a pointer to this method's objc_method_description.
2823 **********************************************************************/
2824 struct objc_method_description *
2825 method_getDescription(Method m)
2828 return (struct objc_method_description *)m;
2833 method_getImplementation(Method m)
2835 return m ? m->imp : nil;
2839 /***********************************************************************
2841 * Returns this method's selector.
2842 * The method must not be nil.
2843 * The method must already have been fixed-up.
2845 **********************************************************************/
2847 method_getName(Method m)
2851 assert(m->name == sel_registerName(sel_getName(m->name)));
2856 /***********************************************************************
2857 * method_getTypeEncoding
2858 * Returns this method's old-style type encoding string.
2859 * The method must not be nil.
2861 **********************************************************************/
2863 method_getTypeEncoding(Method m)
2870 /***********************************************************************
2871 * method_setImplementation
2872 * Sets this method's implementation to imp.
2873 * The previous implementation is returned.
2874 **********************************************************************/
2876 _method_setImplementation(Class cls, method_t *m, IMP imp)
2878 runtimeLock.assertWriting();
2881 if (!imp) return nil;
2886 // Cache updates are slow if cls is nil (i.e. unknown)
2887 // RR/AWZ updates are slow if cls is nil (i.e. unknown)
2888 // fixme build list of classes whose Methods are known externally?
2892 updateCustomRR_AWZ(cls, m);
2898 method_setImplementation(Method m, IMP imp)
2900 // Don't know the class - will be slow if RR/AWZ are affected
2901 // fixme build list of classes whose Methods are known externally?
2902 rwlock_writer_t lock(runtimeLock);
2903 return _method_setImplementation(Nil, m, imp);
2907 void method_exchangeImplementations(Method m1, Method m2)
2909 if (!m1 || !m2) return;
2911 rwlock_writer_t lock(runtimeLock);
2913 IMP m1_imp = m1->imp;
2918 // RR/AWZ updates are slow because class is unknown
2919 // Cache updates are slow because class is unknown
2920 // fixme build list of classes whose Methods are known externally?
2924 updateCustomRR_AWZ(nil, m1);
2925 updateCustomRR_AWZ(nil, m2);
2929 /***********************************************************************
2933 **********************************************************************/
2935 ivar_getOffset(Ivar ivar)
2937 if (!ivar) return 0;
2938 return *ivar->offset;
2942 /***********************************************************************
2946 **********************************************************************/
2948 ivar_getName(Ivar ivar)
2950 if (!ivar) return nil;
2955 /***********************************************************************
2956 * ivar_getTypeEncoding
2959 **********************************************************************/
2961 ivar_getTypeEncoding(Ivar ivar)
2963 if (!ivar) return nil;
2969 const char *property_getName(objc_property_t prop)
2974 const char *property_getAttributes(objc_property_t prop)
2976 return prop->attributes;
2979 objc_property_attribute_t *property_copyAttributeList(objc_property_t prop,
2980 unsigned int *outCount)
2983 if (outCount) *outCount = 0;
2987 rwlock_reader_t lock(runtimeLock);
2988 return copyPropertyAttributeList(prop->attributes,outCount);
2991 char * property_copyAttributeValue(objc_property_t prop, const char *name)
2993 if (!prop || !name || *name == '\0') return nil;
2995 rwlock_reader_t lock(runtimeLock);
2996 return copyPropertyAttributeValue(prop->attributes, name);
3000 /***********************************************************************
3001 * getExtendedTypesIndexesForMethod
3003 * a is the count of methods in all method lists before m's method list
3004 * b is the index of m in m's method list
3005 * a+b is the index of m's extended types in the extended types array
3006 **********************************************************************/
3007 static void getExtendedTypesIndexesForMethod(protocol_t *proto, const method_t *m, bool isRequiredMethod, bool isInstanceMethod, uint32_t& a, uint32_t &b)
3011 if (proto->instanceMethods) {
3012 if (isRequiredMethod && isInstanceMethod) {
3013 b = proto->instanceMethods->indexOfMethod(m);
3016 a += proto->instanceMethods->count;
3019 if (proto->classMethods) {
3020 if (isRequiredMethod && !isInstanceMethod) {
3021 b = proto->classMethods->indexOfMethod(m);
3024 a += proto->classMethods->count;
3027 if (proto->optionalInstanceMethods) {
3028 if (!isRequiredMethod && isInstanceMethod) {
3029 b = proto->optionalInstanceMethods->indexOfMethod(m);
3032 a += proto->optionalInstanceMethods->count;
3035 if (proto->optionalClassMethods) {
3036 if (!isRequiredMethod && !isInstanceMethod) {
3037 b = proto->optionalClassMethods->indexOfMethod(m);
3040 a += proto->optionalClassMethods->count;
3045 /***********************************************************************
3046 * getExtendedTypesIndexForMethod
3047 * Returns the index of m's extended types in proto's extended types array.
3048 **********************************************************************/
3049 static uint32_t getExtendedTypesIndexForMethod(protocol_t *proto, const method_t *m, bool isRequiredMethod, bool isInstanceMethod)
3053 getExtendedTypesIndexesForMethod(proto, m, isRequiredMethod,
3054 isInstanceMethod, a, b);
3059 /***********************************************************************
3060 * fixupProtocolMethodList
3061 * Fixes up a single method list in a protocol.
3062 **********************************************************************/
3064 fixupProtocolMethodList(protocol_t *proto, method_list_t *mlist,
3065 bool required, bool instance)
3067 runtimeLock.assertWriting();
3070 if (mlist->isFixedUp()) return;
3072 const char **extTypes = proto->extendedMethodTypes();
3073 fixupMethodList(mlist, true/*always copy for simplicity*/,
3074 !extTypes/*sort if no extended method types*/);
3077 // Sort method list and extended method types together.
3078 // fixupMethodList() can't do this.
3080 uint32_t count = mlist->count;
3083 getExtendedTypesIndexesForMethod(proto, &mlist->get(0),
3084 required, instance, prefix, junk);
3085 for (uint32_t i = 0; i < count; i++) {
3086 for (uint32_t j = i+1; j < count; j++) {
3087 method_t& mi = mlist->get(i);
3088 method_t& mj = mlist->get(j);
3089 if (mi.name > mj.name) {
3091 std::swap(extTypes[prefix+i], extTypes[prefix+j]);
3099 /***********************************************************************
3101 * Fixes up all of a protocol's method lists.
3102 **********************************************************************/
3104 fixupProtocol(protocol_t *proto)
3106 runtimeLock.assertWriting();
3108 if (proto->protocols) {
3109 for (uintptr_t i = 0; i < proto->protocols->count; i++) {
3110 protocol_t *sub = remapProtocol(proto->protocols->list[i]);
3111 if (!sub->isFixedUp()) fixupProtocol(sub);
3115 fixupProtocolMethodList(proto, proto->instanceMethods, YES, YES);
3116 fixupProtocolMethodList(proto, proto->classMethods, YES, NO);
3117 fixupProtocolMethodList(proto, proto->optionalInstanceMethods, NO, YES);
3118 fixupProtocolMethodList(proto, proto->optionalClassMethods, NO, NO);
3120 // fixme memory barrier so we can check this with no lock
3121 proto->setFixedUp();
3125 /***********************************************************************
3126 * fixupProtocolIfNeeded
3127 * Fixes up all of a protocol's method lists if they aren't fixed up already.
3128 * Locking: write-locks runtimeLock.
3129 **********************************************************************/
3131 fixupProtocolIfNeeded(protocol_t *proto)
3133 runtimeLock.assertUnlocked();
3136 if (!proto->isFixedUp()) {
3137 rwlock_writer_t lock(runtimeLock);
3138 fixupProtocol(proto);
3143 static method_list_t *
3144 getProtocolMethodList(protocol_t *proto, bool required, bool instance)
3146 method_list_t **mlistp = nil;
3149 mlistp = &proto->instanceMethods;
3151 mlistp = &proto->classMethods;
3155 mlistp = &proto->optionalInstanceMethods;
3157 mlistp = &proto->optionalClassMethods;
3165 /***********************************************************************
3166 * protocol_getMethod_nolock
3167 * Locking: runtimeLock must be held by the caller
3168 **********************************************************************/
3170 protocol_getMethod_nolock(protocol_t *proto, SEL sel,
3171 bool isRequiredMethod, bool isInstanceMethod,
3174 runtimeLock.assertLocked();
3176 if (!proto || !sel) return nil;
3178 assert(proto->isFixedUp());
3180 method_list_t *mlist =
3181 getProtocolMethodList(proto, isRequiredMethod, isInstanceMethod);
3183 method_t *m = search_method_list(mlist, sel);
3187 if (recursive && proto->protocols) {
3189 for (uint32_t i = 0; i < proto->protocols->count; i++) {
3190 protocol_t *realProto = remapProtocol(proto->protocols->list[i]);
3191 m = protocol_getMethod_nolock(realProto, sel,
3192 isRequiredMethod, isInstanceMethod,
3202 /***********************************************************************
3203 * protocol_getMethod
3205 * Locking: acquires runtimeLock
3206 **********************************************************************/
3208 protocol_getMethod(protocol_t *proto, SEL sel, bool isRequiredMethod, bool isInstanceMethod, bool recursive)
3210 if (!proto) return nil;
3211 fixupProtocolIfNeeded(proto);
3213 rwlock_reader_t lock(runtimeLock);
3214 return protocol_getMethod_nolock(proto, sel, isRequiredMethod,
3215 isInstanceMethod, recursive);
3219 /***********************************************************************
3220 * protocol_getMethodTypeEncoding_nolock
3221 * Return the @encode string for the requested protocol method.
3222 * Returns nil if the compiler did not emit any extended @encode data.
3223 * Locking: runtimeLock must be held for writing by the caller
3224 **********************************************************************/
3226 protocol_getMethodTypeEncoding_nolock(protocol_t *proto, SEL sel,
3227 bool isRequiredMethod,
3228 bool isInstanceMethod)
3230 runtimeLock.assertLocked();
3232 if (!proto) return nil;
3233 if (!proto->extendedMethodTypes()) return nil;
3235 assert(proto->isFixedUp());
3238 protocol_getMethod_nolock(proto, sel,
3239 isRequiredMethod, isInstanceMethod, false);
3241 uint32_t i = getExtendedTypesIndexForMethod(proto, m,
3244 return proto->extendedMethodTypes()[i];
3247 // No method with that name. Search incorporated protocols.
3248 if (proto->protocols) {
3249 for (uintptr_t i = 0; i < proto->protocols->count; i++) {
3251 protocol_getMethodTypeEncoding_nolock(remapProtocol(proto->protocols->list[i]), sel, isRequiredMethod, isInstanceMethod);
3252 if (enc) return enc;
3259 /***********************************************************************
3260 * _protocol_getMethodTypeEncoding
3261 * Return the @encode string for the requested protocol method.
3262 * Returns nil if the compiler did not emit any extended @encode data.
3263 * Locking: acquires runtimeLock
3264 **********************************************************************/
3266 _protocol_getMethodTypeEncoding(Protocol *proto_gen, SEL sel,
3267 BOOL isRequiredMethod, BOOL isInstanceMethod)
3269 protocol_t *proto = newprotocol(proto_gen);
3271 if (!proto) return nil;
3272 fixupProtocolIfNeeded(proto);
3274 rwlock_reader_t lock(runtimeLock);
3275 return protocol_getMethodTypeEncoding_nolock(proto, sel,
3281 /***********************************************************************
3282 * protocol_t::demangledName
3283 * Returns the (Swift-demangled) name of the given protocol.
3285 **********************************************************************/
3287 protocol_t::demangledName()
3289 assert(hasDemangledNameField());
3291 if (! _demangledName) {
3292 char *de = copySwiftV1DemangledName(mangledName, true/*isProtocol*/);
3293 if (! OSAtomicCompareAndSwapPtrBarrier(nil, (void*)(de ?: mangledName),
3294 (void**)&_demangledName))
3299 return _demangledName;
3302 /***********************************************************************
3304 * Returns the (Swift-demangled) name of the given protocol.
3305 * Locking: runtimeLock must not be held by the caller
3306 **********************************************************************/
3308 protocol_getName(Protocol *proto)
3310 if (!proto) return "nil";
3311 else return newprotocol(proto)->demangledName();
3315 /***********************************************************************
3316 * protocol_getInstanceMethodDescription
3317 * Returns the description of a named instance method.
3318 * Locking: runtimeLock must not be held by the caller
3319 **********************************************************************/
3320 struct objc_method_description
3321 protocol_getMethodDescription(Protocol *p, SEL aSel,
3322 BOOL isRequiredMethod, BOOL isInstanceMethod)
3325 protocol_getMethod(newprotocol(p), aSel,
3326 isRequiredMethod, isInstanceMethod, true);
3327 if (m) return *method_getDescription(m);
3328 else return (struct objc_method_description){nil, nil};
3332 /***********************************************************************
3333 * protocol_conformsToProtocol_nolock
3334 * Returns YES if self conforms to other.
3335 * Locking: runtimeLock must be held by the caller.
3336 **********************************************************************/
3338 protocol_conformsToProtocol_nolock(protocol_t *self, protocol_t *other)
3340 runtimeLock.assertLocked();
3342 if (!self || !other) {
3346 // protocols need not be fixed up
3348 if (0 == strcmp(self->mangledName, other->mangledName)) {
3352 if (self->protocols) {
3354 for (i = 0; i < self->protocols->count; i++) {
3355 protocol_t *proto = remapProtocol(self->protocols->list[i]);
3356 if (0 == strcmp(other->mangledName, proto->mangledName)) {
3359 if (protocol_conformsToProtocol_nolock(proto, other)) {
3369 /***********************************************************************
3370 * protocol_conformsToProtocol
3371 * Returns YES if self conforms to other.
3372 * Locking: acquires runtimeLock
3373 **********************************************************************/
3374 BOOL protocol_conformsToProtocol(Protocol *self, Protocol *other)
3376 rwlock_reader_t lock(runtimeLock);
3377 return protocol_conformsToProtocol_nolock(newprotocol(self),
3378 newprotocol(other));
3382 /***********************************************************************
3384 * Return YES if two protocols are equal (i.e. conform to each other)
3385 * Locking: acquires runtimeLock
3386 **********************************************************************/
3387 BOOL protocol_isEqual(Protocol *self, Protocol *other)
3389 if (self == other) return YES;
3390 if (!self || !other) return NO;
3392 if (!protocol_conformsToProtocol(self, other)) return NO;
3393 if (!protocol_conformsToProtocol(other, self)) return NO;
3399 /***********************************************************************
3400 * protocol_copyMethodDescriptionList
3401 * Returns descriptions of a protocol's methods.
3402 * Locking: acquires runtimeLock
3403 **********************************************************************/
3404 struct objc_method_description *
3405 protocol_copyMethodDescriptionList(Protocol *p,
3406 BOOL isRequiredMethod,BOOL isInstanceMethod,
3407 unsigned int *outCount)
3409 protocol_t *proto = newprotocol(p);
3410 struct objc_method_description *result = nil;
3411 unsigned int count = 0;
3414 if (outCount) *outCount = 0;
3418 fixupProtocolIfNeeded(proto);
3420 rwlock_reader_t lock(runtimeLock);
3422 method_list_t *mlist =
3423 getProtocolMethodList(proto, isRequiredMethod, isInstanceMethod);
3426 result = (struct objc_method_description *)
3427 calloc(mlist->count + 1, sizeof(struct objc_method_description));
3428 for (const auto& meth : *mlist) {
3429 result[count].name = meth.name;
3430 result[count].types = (char *)meth.types;
3435 if (outCount) *outCount = count;
3440 /***********************************************************************
3441 * protocol_getProperty
3443 * Locking: runtimeLock must be held by the caller
3444 **********************************************************************/
3446 protocol_getProperty_nolock(protocol_t *proto, const char *name,
3447 bool isRequiredProperty, bool isInstanceProperty)
3449 runtimeLock.assertLocked();
3451 if (!isRequiredProperty) {
3452 // Only required properties are currently supported.
3456 property_list_t *plist = isInstanceProperty ?
3457 proto->instanceProperties : proto->classProperties();
3459 for (auto& prop : *plist) {
3460 if (0 == strcmp(name, prop.name)) {
3466 if (proto->protocols) {
3468 for (i = 0; i < proto->protocols->count; i++) {
3469 protocol_t *p = remapProtocol(proto->protocols->list[i]);
3471 protocol_getProperty_nolock(p, name,
3473 isInstanceProperty);
3474 if (prop) return prop;
3481 objc_property_t protocol_getProperty(Protocol *p, const char *name,
3482 BOOL isRequiredProperty, BOOL isInstanceProperty)
3484 if (!p || !name) return nil;
3486 rwlock_reader_t lock(runtimeLock);
3487 return (objc_property_t)
3488 protocol_getProperty_nolock(newprotocol(p), name,
3489 isRequiredProperty, isInstanceProperty);
3493 /***********************************************************************
3494 * protocol_copyPropertyList
3495 * protocol_copyPropertyList2
3497 * Locking: acquires runtimeLock
3498 **********************************************************************/
3499 static property_t **
3500 copyPropertyList(property_list_t *plist, unsigned int *outCount)
3502 property_t **result = nil;
3503 unsigned int count = 0;
3506 count = plist->count;
3510 result = (property_t **)malloc((count+1) * sizeof(property_t *));
3513 for (auto& prop : *plist) {
3514 result[count++] = ∝
3516 result[count] = nil;
3519 if (outCount) *outCount = count;
3524 protocol_copyPropertyList2(Protocol *proto, unsigned int *outCount,
3525 BOOL isRequiredProperty, BOOL isInstanceProperty)
3527 if (!proto || !isRequiredProperty) {
3528 // Optional properties are not currently supported.
3529 if (outCount) *outCount = 0;
3533 rwlock_reader_t lock(runtimeLock);
3535 property_list_t *plist = isInstanceProperty
3536 ? newprotocol(proto)->instanceProperties
3537 : newprotocol(proto)->classProperties();
3538 return (objc_property_t *)copyPropertyList(plist, outCount);
3542 protocol_copyPropertyList(Protocol *proto, unsigned int *outCount)
3544 return protocol_copyPropertyList2(proto, outCount,
3545 YES/*required*/, YES/*instance*/);
3549 /***********************************************************************
3550 * protocol_copyProtocolList
3551 * Copies this protocol's incorporated protocols.
3552 * Does not copy those protocol's incorporated protocols in turn.
3553 * Locking: acquires runtimeLock
3554 **********************************************************************/
3555 Protocol * __unsafe_unretained *
3556 protocol_copyProtocolList(Protocol *p, unsigned int *outCount)
3558 unsigned int count = 0;
3559 Protocol **result = nil;
3560 protocol_t *proto = newprotocol(p);
3563 if (outCount) *outCount = 0;
3567 rwlock_reader_t lock(runtimeLock);
3569 if (proto->protocols) {
3570 count = (unsigned int)proto->protocols->count;
3573 result = (Protocol **)malloc((count+1) * sizeof(Protocol *));
3576 for (i = 0; i < count; i++) {
3577 result[i] = (Protocol *)remapProtocol(proto->protocols->list[i]);
3582 if (outCount) *outCount = count;
3587 /***********************************************************************
3588 * objc_allocateProtocol
3589 * Creates a new protocol. The protocol may not be used until
3590 * objc_registerProtocol() is called.
3591 * Returns nil if a protocol with the same name already exists.
3592 * Locking: acquires runtimeLock
3593 **********************************************************************/
3595 objc_allocateProtocol(const char *name)
3597 rwlock_writer_t lock(runtimeLock);
3599 if (getProtocol(name)) {
3603 protocol_t *result = (protocol_t *)calloc(sizeof(protocol_t), 1);
3605 extern objc_class OBJC_CLASS_$___IncompleteProtocol;
3606 Class cls = (Class)&OBJC_CLASS_$___IncompleteProtocol;
3607 result->initProtocolIsa(cls);
3608 result->size = sizeof(protocol_t);
3609 // fixme mangle the name if it looks swift-y?
3610 result->mangledName = strdupIfMutable(name);
3612 // fixme reserve name without installing
3614 return (Protocol *)result;
3618 /***********************************************************************
3619 * objc_registerProtocol
3620 * Registers a newly-constructed protocol. The protocol is now
3621 * ready for use and immutable.
3622 * Locking: acquires runtimeLock
3623 **********************************************************************/
3624 void objc_registerProtocol(Protocol *proto_gen)
3626 protocol_t *proto = newprotocol(proto_gen);
3628 rwlock_writer_t lock(runtimeLock);
3630 extern objc_class OBJC_CLASS_$___IncompleteProtocol;
3631 Class oldcls = (Class)&OBJC_CLASS_$___IncompleteProtocol;
3632 extern objc_class OBJC_CLASS_$_Protocol;
3633 Class cls = (Class)&OBJC_CLASS_$_Protocol;
3635 if (proto->ISA() == cls) {
3636 _objc_inform("objc_registerProtocol: protocol '%s' was already "
3637 "registered!", proto->nameForLogging());
3640 if (proto->ISA() != oldcls) {
3641 _objc_inform("objc_registerProtocol: protocol '%s' was not allocated "
3642 "with objc_allocateProtocol!", proto->nameForLogging());
3646 // NOT initProtocolIsa(). The protocol object may already
3647 // have been retained and we must preserve that count.
3648 proto->changeIsa(cls);
3650 NXMapKeyCopyingInsert(protocols(), proto->mangledName, proto);
3654 /***********************************************************************
3655 * protocol_addProtocol
3656 * Adds an incorporated protocol to another protocol.
3657 * No method enforcement is performed.
3658 * `proto` must be under construction. `addition` must not.
3659 * Locking: acquires runtimeLock
3660 **********************************************************************/
3662 protocol_addProtocol(Protocol *proto_gen, Protocol *addition_gen)
3664 protocol_t *proto = newprotocol(proto_gen);
3665 protocol_t *addition = newprotocol(addition_gen);
3667 extern objc_class OBJC_CLASS_$___IncompleteProtocol;
3668 Class cls = (Class)&OBJC_CLASS_$___IncompleteProtocol;
3670 if (!proto_gen) return;
3671 if (!addition_gen) return;
3673 rwlock_writer_t lock(runtimeLock);
3675 if (proto->ISA() != cls) {
3676 _objc_inform("protocol_addProtocol: modified protocol '%s' is not "
3677 "under construction!", proto->nameForLogging());
3680 if (addition->ISA() == cls) {
3681 _objc_inform("protocol_addProtocol: added protocol '%s' is still "
3682 "under construction!", addition->nameForLogging());
3686 protocol_list_t *protolist = proto->protocols;
3688 protolist = (protocol_list_t *)
3689 calloc(1, sizeof(protocol_list_t)
3690 + sizeof(protolist->list[0]));
3692 protolist = (protocol_list_t *)
3693 realloc(protolist, protocol_list_size(protolist)
3694 + sizeof(protolist->list[0]));
3697 protolist->list[protolist->count++] = (protocol_ref_t)addition;
3698 proto->protocols = protolist;
3702 /***********************************************************************
3703 * protocol_addMethodDescription
3704 * Adds a method to a protocol. The protocol must be under construction.
3705 * Locking: acquires runtimeLock
3706 **********************************************************************/
3708 protocol_addMethod_nolock(method_list_t*& list, SEL name, const char *types)
3711 list = (method_list_t *)calloc(sizeof(method_list_t), 1);
3712 list->entsizeAndFlags = sizeof(list->first);
3715 size_t size = list->byteSize() + list->entsize();
3716 list = (method_list_t *)realloc(list, size);
3719 method_t& meth = list->get(list->count++);
3721 meth.types = types ? strdupIfMutable(types) : "";
3726 protocol_addMethodDescription(Protocol *proto_gen, SEL name, const char *types,
3727 BOOL isRequiredMethod, BOOL isInstanceMethod)
3729 protocol_t *proto = newprotocol(proto_gen);
3731 extern objc_class OBJC_CLASS_$___IncompleteProtocol;
3732 Class cls = (Class)&OBJC_CLASS_$___IncompleteProtocol;
3734 if (!proto_gen) return;
3736 rwlock_writer_t lock(runtimeLock);
3738 if (proto->ISA() != cls) {
3739 _objc_inform("protocol_addMethodDescription: protocol '%s' is not "
3740 "under construction!", proto->nameForLogging());
3744 if (isRequiredMethod && isInstanceMethod) {
3745 protocol_addMethod_nolock(proto->instanceMethods, name, types);
3746 } else if (isRequiredMethod && !isInstanceMethod) {
3747 protocol_addMethod_nolock(proto->classMethods, name, types);
3748 } else if (!isRequiredMethod && isInstanceMethod) {
3749 protocol_addMethod_nolock(proto->optionalInstanceMethods, name,types);
3750 } else /* !isRequiredMethod && !isInstanceMethod) */ {
3751 protocol_addMethod_nolock(proto->optionalClassMethods, name, types);
3756 /***********************************************************************
3757 * protocol_addProperty
3758 * Adds a property to a protocol. The protocol must be under construction.
3759 * Locking: acquires runtimeLock
3760 **********************************************************************/
3762 protocol_addProperty_nolock(property_list_t *&plist, const char *name,
3763 const objc_property_attribute_t *attrs,
3767 plist = (property_list_t *)calloc(sizeof(property_list_t), 1);
3768 plist->entsizeAndFlags = sizeof(property_t);
3770 plist = (property_list_t *)
3771 realloc(plist, sizeof(property_list_t)
3772 + plist->count * plist->entsize());
3775 property_t& prop = plist->get(plist->count++);
3776 prop.name = strdupIfMutable(name);
3777 prop.attributes = copyPropertyAttributeString(attrs, count);
3781 protocol_addProperty(Protocol *proto_gen, const char *name,
3782 const objc_property_attribute_t *attrs,
3784 BOOL isRequiredProperty, BOOL isInstanceProperty)
3786 protocol_t *proto = newprotocol(proto_gen);
3788 extern objc_class OBJC_CLASS_$___IncompleteProtocol;
3789 Class cls = (Class)&OBJC_CLASS_$___IncompleteProtocol;
3794 rwlock_writer_t lock(runtimeLock);
3796 if (proto->ISA() != cls) {
3797 _objc_inform("protocol_addProperty: protocol '%s' is not "
3798 "under construction!", proto->nameForLogging());
3802 if (isRequiredProperty && isInstanceProperty) {
3803 protocol_addProperty_nolock(proto->instanceProperties, name, attrs, count);
3805 else if (isRequiredProperty && !isInstanceProperty) {
3806 protocol_addProperty_nolock(proto->_classProperties, name, attrs, count);
3808 //else if (!isRequiredProperty && isInstanceProperty) {
3809 // protocol_addProperty_nolock(proto->optionalInstanceProperties, name, attrs, count);
3811 //else /* !isRequiredProperty && !isInstanceProperty) */ {
3812 // protocol_addProperty_nolock(proto->optionalClassProperties, name, attrs, count);
3817 /***********************************************************************
3819 * Returns pointers to all classes.
3820 * This requires all classes be realized, which is regretfully non-lazy.
3821 * Locking: acquires runtimeLock
3822 **********************************************************************/
3824 objc_getClassList(Class *buffer, int bufferLen)
3826 rwlock_writer_t lock(runtimeLock);
3828 realizeAllClasses();
3830 __block int count = 0;
3831 foreach_realized_class_and_metaclass(^(Class cls) {
3832 if (!cls->isMetaClass()) count++;
3837 foreach_realized_class_and_metaclass(^(Class cls) {
3838 if (c < bufferLen && !cls->isMetaClass()) {
3848 /***********************************************************************
3849 * objc_copyClassList
3850 * Returns pointers to all classes.
3851 * This requires all classes be realized, which is regretfully non-lazy.
3853 * outCount may be nil. *outCount is the number of classes returned.
3854 * If the returned array is not nil, it is nil-terminated and must be
3855 * freed with free().
3856 * Locking: write-locks runtimeLock
3857 **********************************************************************/
3859 objc_copyClassList(unsigned int *outCount)
3861 rwlock_writer_t lock(runtimeLock);
3863 realizeAllClasses();
3865 Class *result = nil;
3867 __block unsigned int count = 0;
3868 foreach_realized_class_and_metaclass(^(Class cls) {
3869 if (!cls->isMetaClass()) count++;
3873 result = (Class *)malloc((1+count) * sizeof(Class));
3874 __block unsigned int c = 0;
3875 foreach_realized_class_and_metaclass(^(Class cls) {
3876 if (!cls->isMetaClass()) {
3883 if (outCount) *outCount = count;
3888 /***********************************************************************
3889 * objc_copyProtocolList
3890 * Returns pointers to all protocols.
3891 * Locking: read-locks runtimeLock
3892 **********************************************************************/
3893 Protocol * __unsafe_unretained *
3894 objc_copyProtocolList(unsigned int *outCount)
3896 rwlock_reader_t lock(runtimeLock);
3898 NXMapTable *protocol_map = protocols();
3900 unsigned int count = NXCountMapTable(protocol_map);
3902 if (outCount) *outCount = 0;
3906 Protocol **result = (Protocol **)malloc((count+1) * sizeof(Protocol*));
3911 NXMapState state = NXInitMapState(protocol_map);
3912 while (NXNextMapState(protocol_map, &state,
3913 (const void **)&name, (const void **)&proto))
3915 result[i++] = proto;
3919 assert(i == count+1);
3921 if (outCount) *outCount = count;
3926 /***********************************************************************
3928 * Get a protocol by name, or return nil
3929 * Locking: read-locks runtimeLock
3930 **********************************************************************/
3931 Protocol *objc_getProtocol(const char *name)
3933 rwlock_reader_t lock(runtimeLock);
3934 return getProtocol(name);
3938 /***********************************************************************
3939 * class_copyMethodList
3941 * Locking: read-locks runtimeLock
3942 **********************************************************************/
3944 class_copyMethodList(Class cls, unsigned int *outCount)
3946 unsigned int count = 0;
3947 Method *result = nil;
3950 if (outCount) *outCount = 0;
3954 rwlock_reader_t lock(runtimeLock);
3956 assert(cls->isRealized());
3958 count = cls->data()->methods.count();
3961 result = (Method *)malloc((count + 1) * sizeof(Method));
3964 for (auto& meth : cls->data()->methods) {
3965 result[count++] = &meth;
3967 result[count] = nil;
3970 if (outCount) *outCount = count;
3975 /***********************************************************************
3976 * class_copyIvarList
3978 * Locking: read-locks runtimeLock
3979 **********************************************************************/
3981 class_copyIvarList(Class cls, unsigned int *outCount)
3983 const ivar_list_t *ivars;
3985 unsigned int count = 0;
3988 if (outCount) *outCount = 0;
3992 rwlock_reader_t lock(runtimeLock);
3994 assert(cls->isRealized());
3996 if ((ivars = cls->data()->ro->ivars) && ivars->count) {
3997 result = (Ivar *)malloc((ivars->count+1) * sizeof(Ivar));
3999 for (auto& ivar : *ivars) {
4000 if (!ivar.offset) continue; // anonymous bitfield
4001 result[count++] = &ivar;
4003 result[count] = nil;
4006 if (outCount) *outCount = count;
4011 /***********************************************************************
4012 * class_copyPropertyList. Returns a heap block containing the
4013 * properties declared in the class, or nil if the class
4014 * declares no properties. Caller must free the block.
4015 * Does not copy any superclass's properties.
4016 * Locking: read-locks runtimeLock
4017 **********************************************************************/
4019 class_copyPropertyList(Class cls, unsigned int *outCount)
4022 if (outCount) *outCount = 0;
4026 rwlock_reader_t lock(runtimeLock);
4028 assert(cls->isRealized());
4029 auto rw = cls->data();
4031 property_t **result = nil;
4032 unsigned int count = rw->properties.count();
4034 result = (property_t **)malloc((count + 1) * sizeof(property_t *));
4037 for (auto& prop : rw->properties) {
4038 result[count++] = ∝
4040 result[count] = nil;
4043 if (outCount) *outCount = count;
4044 return (objc_property_t *)result;
4048 /***********************************************************************
4049 * objc_class::getLoadMethod
4051 * Called only from add_class_to_loadable_list.
4052 * Locking: runtimeLock must be read- or write-locked by the caller.
4053 **********************************************************************/
4055 objc_class::getLoadMethod()
4057 runtimeLock.assertLocked();
4059 const method_list_t *mlist;
4061 assert(isRealized());
4062 assert(ISA()->isRealized());
4063 assert(!isMetaClass());
4064 assert(ISA()->isMetaClass());
4066 mlist = ISA()->data()->ro->baseMethods();
4068 for (const auto& meth : *mlist) {
4069 const char *name = sel_cname(meth.name);
4070 if (0 == strcmp(name, "load")) {
4080 /***********************************************************************
4082 * Returns a category's name.
4084 **********************************************************************/
4086 _category_getName(Category cat)
4092 /***********************************************************************
4093 * _category_getClassName
4094 * Returns a category's class's name
4095 * Called only from add_category_to_loadable_list and
4096 * remove_category_from_loadable_list for logging purposes.
4097 * Locking: runtimeLock must be read- or write-locked by the caller
4098 **********************************************************************/
4100 _category_getClassName(Category cat)
4102 runtimeLock.assertLocked();
4103 return remapClass(cat->cls)->nameForLogging();
4107 /***********************************************************************
4108 * _category_getClass
4109 * Returns a category's class
4110 * Called only by call_category_loads.
4111 * Locking: read-locks runtimeLock
4112 **********************************************************************/
4114 _category_getClass(Category cat)
4116 rwlock_reader_t lock(runtimeLock);
4117 Class result = remapClass(cat->cls);
4118 assert(result->isRealized()); // ok for call_category_loads' usage
4123 /***********************************************************************
4124 * _category_getLoadMethod
4126 * Called only from add_category_to_loadable_list
4127 * Locking: runtimeLock must be read- or write-locked by the caller
4128 **********************************************************************/
4130 _category_getLoadMethod(Category cat)
4132 runtimeLock.assertLocked();
4134 const method_list_t *mlist;
4136 mlist = cat->classMethods;
4138 for (const auto& meth : *mlist) {
4139 const char *name = sel_cname(meth.name);
4140 if (0 == strcmp(name, "load")) {
4150 /***********************************************************************
4151 * category_t::propertiesForMeta
4152 * Return a category's instance or class properties.
4153 * hi is the image containing the category.
4154 **********************************************************************/
4156 category_t::propertiesForMeta(bool isMeta, struct header_info *hi)
4158 if (!isMeta) return instanceProperties;
4159 else if (hi->info()->hasCategoryClassProperties()) return _classProperties;
4164 /***********************************************************************
4165 * class_copyProtocolList
4167 * Locking: read-locks runtimeLock
4168 **********************************************************************/
4169 Protocol * __unsafe_unretained *
4170 class_copyProtocolList(Class cls, unsigned int *outCount)
4172 unsigned int count = 0;
4173 Protocol **result = nil;
4176 if (outCount) *outCount = 0;
4180 rwlock_reader_t lock(runtimeLock);
4182 assert(cls->isRealized());
4184 count = cls->data()->protocols.count();
4187 result = (Protocol **)malloc((count+1) * sizeof(Protocol *));
4190 for (const auto& proto : cls->data()->protocols) {
4191 result[count++] = (Protocol *)remapProtocol(proto);
4193 result[count] = nil;
4196 if (outCount) *outCount = count;
4201 /***********************************************************************
4202 * _objc_copyClassNamesForImage
4204 * Locking: write-locks runtimeLock
4205 **********************************************************************/
4207 _objc_copyClassNamesForImage(header_info *hi, unsigned int *outCount)
4209 size_t count, i, shift;
4210 classref_t *classlist;
4213 // Need to write-lock in case demangledName() needs to realize a class.
4214 rwlock_writer_t lock(runtimeLock);
4216 classlist = _getObjc2ClassList(hi, &count);
4217 names = (const char **)malloc((count+1) * sizeof(const char *));
4220 for (i = 0; i < count; i++) {
4221 Class cls = remapClass(classlist[i]);
4223 names[i-shift] = cls->demangledName(true/*realize*/);
4225 shift++; // ignored weak-linked class
4231 if (outCount) *outCount = (unsigned int)count;
4236 /***********************************************************************
4237 * saveTemporaryString
4238 * Save a string in a thread-local FIFO buffer.
4239 * This is suitable for temporary strings generated for logging purposes.
4240 **********************************************************************/
4242 saveTemporaryString(char *str)
4244 // Fixed-size FIFO. We free the first string, shift
4245 // the rest, and add the new string to the end.
4246 _objc_pthread_data *data = _objc_fetch_pthread_data(true);
4247 if (data->printableNames[0]) {
4248 free(data->printableNames[0]);
4250 int last = countof(data->printableNames) - 1;
4251 for (int i = 0; i < last; i++) {
4252 data->printableNames[i] = data->printableNames[i+1];
4254 data->printableNames[last] = str;
4258 /***********************************************************************
4259 * objc_class::nameForLogging
4260 * Returns the class's name, suitable for display.
4261 * The returned memory is TEMPORARY. Print it or copy it immediately.
4263 **********************************************************************/
4265 objc_class::nameForLogging()
4267 // Handle the easy case directly.
4268 if (isRealized() || isFuture()) {
4269 if (data()->demangledName) return data()->demangledName;
4274 const char *name = mangledName();
4275 char *de = copySwiftV1DemangledName(name);
4276 if (de) result = de;
4277 else result = strdup(name);
4279 saveTemporaryString(result);
4284 /***********************************************************************
4285 * objc_class::demangledName
4286 * If realize=false, the class must already be realized or future.
4287 * Locking: If realize=true, runtimeLock must be held for writing by the caller.
4288 **********************************************************************/
4289 mutex_t DemangleCacheLock;
4290 static NXHashTable *DemangleCache;
4292 objc_class::demangledName(bool realize)
4294 // Return previously demangled name if available.
4295 if (isRealized() || isFuture()) {
4296 if (data()->demangledName) return data()->demangledName;
4299 // Try demangling the mangled name.
4300 const char *mangled = mangledName();
4301 char *de = copySwiftV1DemangledName(mangled);
4302 if (isRealized() || isFuture()) {
4303 // Class is already realized or future.
4304 // Save demangling result in rw data.
4305 // We may not own rwlock for writing so use an atomic operation instead.
4306 if (! OSAtomicCompareAndSwapPtrBarrier(nil, (void*)(de ?: mangled),
4307 (void**)&data()->demangledName))
4311 return data()->demangledName;
4314 // Class is not yet realized.
4316 // Name is not mangled. Return it without caching.
4320 // Class is not yet realized and name is mangled. Realize the class.
4321 // Only objc_copyClassNamesForImage() should get here.
4323 // fixme lldb's calls to class_getName() can also get here when
4324 // interrogating the dyld shared cache. (rdar://27258517)
4325 // fixme runtimeLock.assertWriting();
4326 // fixme assert(realize);
4329 runtimeLock.assertWriting();
4330 realizeClass((Class)this);
4331 data()->demangledName = de;
4335 // Save the string to avoid leaks.
4338 mutex_locker_t lock(DemangleCacheLock);
4339 if (!DemangleCache) {
4340 DemangleCache = NXCreateHashTable(NXStrPrototype, 0, nil);
4342 cached = (char *)NXHashInsertIfAbsent(DemangleCache, de);
4344 if (cached != de) free(de);
4350 /***********************************************************************
4353 * Locking: acquires runtimeLock
4354 **********************************************************************/
4355 const char *class_getName(Class cls)
4357 if (!cls) return "nil";
4358 // fixme lldb calls class_getName() on unrealized classes (rdar://27258517)
4359 // assert(cls->isRealized() || cls->isFuture());
4360 return cls->demangledName();
4364 /***********************************************************************
4368 **********************************************************************/
4370 class_getVersion(Class cls)
4373 assert(cls->isRealized());
4374 return cls->data()->version;
4378 /***********************************************************************
4382 **********************************************************************/
4384 class_setVersion(Class cls, int version)
4387 assert(cls->isRealized());
4388 cls->data()->version = version;
4392 static method_t *findMethodInSortedMethodList(SEL key, const method_list_t *list)
4396 const method_t * const first = &list->first;
4397 const method_t *base = first;
4398 const method_t *probe;
4399 uintptr_t keyValue = (uintptr_t)key;
4402 for (count = list->count; count != 0; count >>= 1) {
4403 probe = base + (count >> 1);
4405 uintptr_t probeValue = (uintptr_t)probe->name;
4407 if (keyValue == probeValue) {
4408 // `probe` is a match.
4409 // Rewind looking for the *first* occurrence of this value.
4410 // This is required for correct category overrides.
4411 while (probe > first && keyValue == (uintptr_t)probe[-1].name) {
4414 return (method_t *)probe;
4417 if (keyValue > probeValue) {
4426 /***********************************************************************
4427 * getMethodNoSuper_nolock
4429 * Locking: runtimeLock must be read- or write-locked by the caller
4430 **********************************************************************/
4431 static method_t *search_method_list(const method_list_t *mlist, SEL sel)
4433 int methodListIsFixedUp = mlist->isFixedUp();
4434 int methodListHasExpectedSize = mlist->entsize() == sizeof(method_t);
4436 if (__builtin_expect(methodListIsFixedUp && methodListHasExpectedSize, 1)) {
4437 return findMethodInSortedMethodList(sel, mlist);
4439 // Linear search of unsorted method list
4440 for (auto& meth : *mlist) {
4441 if (meth.name == sel) return &meth;
4446 // sanity-check negative results
4447 if (mlist->isFixedUp()) {
4448 for (auto& meth : *mlist) {
4449 if (meth.name == sel) {
4450 _objc_fatal("linear search worked when binary search did not");
4460 getMethodNoSuper_nolock(Class cls, SEL sel)
4462 runtimeLock.assertLocked();
4464 assert(cls->isRealized());
4468 for (auto mlists = cls->data()->methods.beginLists(),
4469 end = cls->data()->methods.endLists();
4473 method_t *m = search_method_list(*mlists, sel);
4481 /***********************************************************************
4484 * Locking: runtimeLock must be read- or write-locked by the caller
4485 **********************************************************************/
4487 getMethod_nolock(Class cls, SEL sel)
4491 runtimeLock.assertLocked();
4496 assert(cls->isRealized());
4498 while (cls && ((m = getMethodNoSuper_nolock(cls, sel))) == nil) {
4499 cls = cls->superclass;
4506 /***********************************************************************
4509 * Locking: read-locks runtimeLock
4510 **********************************************************************/
4511 static Method _class_getMethod(Class cls, SEL sel)
4513 rwlock_reader_t lock(runtimeLock);
4514 return getMethod_nolock(cls, sel);
4518 /***********************************************************************
4519 * class_getInstanceMethod. Return the instance method for the
4520 * specified class and selector.
4521 **********************************************************************/
4522 Method class_getInstanceMethod(Class cls, SEL sel)
4524 if (!cls || !sel) return nil;
4526 // This deliberately avoids +initialize because it historically did so.
4528 // This implementation is a bit weird because it's the only place that
4529 // wants a Method instead of an IMP.
4531 #warning fixme build and search caches
4533 // Search method lists, try method resolver, etc.
4534 lookUpImpOrNil(cls, sel, nil,
4535 NO/*initialize*/, NO/*cache*/, YES/*resolver*/);
4537 #warning fixme build and search caches
4539 return _class_getMethod(cls, sel);
4543 /***********************************************************************
4544 * log_and_fill_cache
4545 * Log this method call. If the logger permits it, fill the method cache.
4546 * cls is the method whose cache should be filled.
4547 * implementer is the class that owns the implementation in question.
4548 **********************************************************************/
4550 log_and_fill_cache(Class cls, IMP imp, SEL sel, id receiver, Class implementer)
4552 #if SUPPORT_MESSAGE_LOGGING
4553 if (objcMsgLogEnabled) {
4554 bool cacheIt = logMessageSend(implementer->isMetaClass(),
4555 cls->nameForLogging(),
4556 implementer->nameForLogging(),
4558 if (!cacheIt) return;
4561 cache_fill (cls, sel, imp, receiver);
4565 /***********************************************************************
4566 * _class_lookupMethodAndLoadCache.
4567 * Method lookup for dispatchers ONLY. OTHER CODE SHOULD USE lookUpImp().
4568 * This lookup avoids optimistic cache scan because the dispatcher
4569 * already tried that.
4570 **********************************************************************/
4571 IMP _class_lookupMethodAndLoadCache3(id obj, SEL sel, Class cls)
4573 return lookUpImpOrForward(cls, sel, obj,
4574 YES/*initialize*/, NO/*cache*/, YES/*resolver*/);
4578 /***********************************************************************
4579 * lookUpImpOrForward.
4580 * The standard IMP lookup.
4581 * initialize==NO tries to avoid +initialize (but sometimes fails)
4582 * cache==NO skips optimistic unlocked lookup (but uses cache elsewhere)
4583 * Most callers should use initialize==YES and cache==YES.
4584 * inst is an instance of cls or a subclass thereof, or nil if none is known.
4585 * If cls is an un-initialized metaclass then a non-nil inst is faster.
4586 * May return _objc_msgForward_impcache. IMPs destined for external use
4587 * must be converted to _objc_msgForward or _objc_msgForward_stret.
4588 * If you don't want forwarding at all, use lookUpImpOrNil() instead.
4589 **********************************************************************/
4590 IMP lookUpImpOrForward(Class cls, SEL sel, id inst,
4591 bool initialize, bool cache, bool resolver)
4594 bool triedResolver = NO;
4596 runtimeLock.assertUnlocked();
4598 // Optimistic cache lookup
4600 imp = cache_getImp(cls, sel);
4601 if (imp) return imp;
4604 // runtimeLock is held during isRealized and isInitialized checking
4605 // to prevent races against concurrent realization.
4607 // runtimeLock is held during method search to make
4608 // method-lookup + cache-fill atomic with respect to method addition.
4609 // Otherwise, a category could be added but ignored indefinitely because
4610 // the cache was re-filled with the old value after the cache flush on
4611 // behalf of the category.
4615 if (!cls->isRealized()) {
4616 // Drop the read-lock and acquire the write-lock.
4617 // realizeClass() checks isRealized() again to prevent
4618 // a race while the lock is down.
4619 runtimeLock.unlockRead();
4620 runtimeLock.write();
4624 runtimeLock.unlockWrite();
4628 if (initialize && !cls->isInitialized()) {
4629 runtimeLock.unlockRead();
4630 _class_initialize (_class_getNonMetaClass(cls, inst));
4632 // If sel == initialize, _class_initialize will send +initialize and
4633 // then the messenger will send +initialize again after this
4634 // procedure finishes. Of course, if this is not being called
4635 // from the messenger then it won't happen. 2778172
4640 runtimeLock.assertReading();
4642 // Try this class's cache.
4644 imp = cache_getImp(cls, sel);
4647 // Try this class's method lists.
4649 Method meth = getMethodNoSuper_nolock(cls, sel);
4651 log_and_fill_cache(cls, meth->imp, sel, inst, cls);
4657 // Try superclass caches and method lists.
4659 unsigned attempts = unreasonableClassCount();
4660 for (Class curClass = cls->superclass;
4662 curClass = curClass->superclass)
4664 // Halt if there is a cycle in the superclass chain.
4665 if (--attempts == 0) {
4666 _objc_fatal("Memory corruption in class list.");
4669 // Superclass cache.
4670 imp = cache_getImp(curClass, sel);
4672 if (imp != (IMP)_objc_msgForward_impcache) {
4673 // Found the method in a superclass. Cache it in this class.
4674 log_and_fill_cache(cls, imp, sel, inst, curClass);
4678 // Found a forward:: entry in a superclass.
4679 // Stop searching, but don't cache yet; call method
4680 // resolver for this class first.
4685 // Superclass method list.
4686 Method meth = getMethodNoSuper_nolock(curClass, sel);
4688 log_and_fill_cache(cls, meth->imp, sel, inst, curClass);
4695 // No implementation found. Try method resolver once.
4697 if (resolver && !triedResolver) {
4698 runtimeLock.unlockRead();
4699 _class_resolveMethod(cls, sel, inst);
4701 // Don't cache the result; we don't hold the lock so it may have
4702 // changed already. Re-do the search from scratch instead.
4703 triedResolver = YES;
4707 // No implementation found, and method resolver didn't help.
4710 imp = (IMP)_objc_msgForward_impcache;
4711 cache_fill(cls, sel, imp, inst);
4714 runtimeLock.unlockRead();
4720 /***********************************************************************
4722 * Like lookUpImpOrForward, but returns nil instead of _objc_msgForward_impcache
4723 **********************************************************************/
4724 IMP lookUpImpOrNil(Class cls, SEL sel, id inst,
4725 bool initialize, bool cache, bool resolver)
4727 IMP imp = lookUpImpOrForward(cls, sel, inst, initialize, cache, resolver);
4728 if (imp == _objc_msgForward_impcache) return nil;
4733 /***********************************************************************
4734 * lookupMethodInClassAndLoadCache.
4735 * Like _class_lookupMethodAndLoadCache, but does not search superclasses.
4736 * Caches and returns objc_msgForward if the method is not found in the class.
4737 **********************************************************************/
4738 IMP lookupMethodInClassAndLoadCache(Class cls, SEL sel)
4743 // fixme this is incomplete - no resolver, +initialize -
4744 // but it's only used for .cxx_construct/destruct so we don't care
4745 assert(sel == SEL_cxx_construct || sel == SEL_cxx_destruct);
4747 // Search cache first.
4748 imp = cache_getImp(cls, sel);
4749 if (imp) return imp;
4751 // Cache miss. Search method list.
4753 rwlock_reader_t lock(runtimeLock);
4755 meth = getMethodNoSuper_nolock(cls, sel);
4758 // Hit in method list. Cache it.
4759 cache_fill(cls, sel, meth->imp, nil);
4762 // Miss in method list. Cache objc_msgForward.
4763 cache_fill(cls, sel, _objc_msgForward_impcache, nil);
4764 return _objc_msgForward_impcache;
4769 /***********************************************************************
4772 * Locking: read-locks runtimeLock
4773 **********************************************************************/
4774 objc_property_t class_getProperty(Class cls, const char *name)
4776 if (!cls || !name) return nil;
4778 rwlock_reader_t lock(runtimeLock);
4780 assert(cls->isRealized());
4782 for ( ; cls; cls = cls->superclass) {
4783 for (auto& prop : cls->data()->properties) {
4784 if (0 == strcmp(name, prop.name)) {
4785 return (objc_property_t)∝
4794 /***********************************************************************
4796 **********************************************************************/
4798 Class gdb_class_getClass(Class cls)
4800 const char *className = cls->mangledName();
4801 if(!className || !strlen(className)) return Nil;
4802 Class rCls = look_up_class(className, NO, NO);
4806 Class gdb_object_getClass(id obj)
4808 if (!obj) return nil;
4809 return gdb_class_getClass(obj->getIsa());
4813 /***********************************************************************
4814 * Locking: write-locks runtimeLock
4815 **********************************************************************/
4817 objc_class::setInitialized()
4822 assert(!isMetaClass());
4825 metacls = cls->ISA();
4827 rwlock_reader_t lock(runtimeLock);
4829 // Scan metaclass for custom AWZ.
4830 // Scan metaclass for custom RR.
4831 // Scan class for custom RR.
4832 // Also print custom RR/AWZ because we probably haven't done it yet.
4835 // NSObject AWZ class methods are default.
4836 // NSObject RR instance methods are default.
4837 // updateCustomRR_AWZ() also knows these special cases.
4838 // attachMethodLists() also knows these special cases.
4841 bool metaCustomAWZ = NO;
4842 if (MetaclassNSObjectAWZSwizzled) {
4843 // Somebody already swizzled NSObject's methods
4844 metaCustomAWZ = YES;
4847 else if (metacls == classNSObject()->ISA()) {
4848 // NSObject's metaclass AWZ is default, but we still need to check cats
4849 auto& methods = metacls->data()->methods;
4850 for (auto mlists = methods.beginCategoryMethodLists(),
4851 end = methods.endCategoryMethodLists(metacls);
4855 if (methodListImplementsAWZ(*mlists)) {
4856 metaCustomAWZ = YES;
4862 else if (metacls->superclass->hasCustomAWZ()) {
4863 // Superclass is custom AWZ, therefore we are too.
4864 metaCustomAWZ = YES;
4868 // Not metaclass NSObject.
4869 auto& methods = metacls->data()->methods;
4870 for (auto mlists = methods.beginLists(),
4871 end = methods.endLists();
4875 if (methodListImplementsAWZ(*mlists)) {
4876 metaCustomAWZ = YES;
4882 if (!metaCustomAWZ) metacls->setHasDefaultAWZ();
4884 if (PrintCustomAWZ && metaCustomAWZ) metacls->printCustomAWZ(inherited);
4885 // metacls->printCustomRR();
4888 bool clsCustomRR = NO;
4889 if (ClassNSObjectRRSwizzled) {
4890 // Somebody already swizzled NSObject's methods
4894 if (cls == classNSObject()) {
4895 // NSObject's RR is default, but we still need to check categories
4896 auto& methods = cls->data()->methods;
4897 for (auto mlists = methods.beginCategoryMethodLists(),
4898 end = methods.endCategoryMethodLists(cls);
4902 if (methodListImplementsRR(*mlists)) {
4909 else if (!cls->superclass) {
4910 // Custom root class
4914 else if (cls->superclass->hasCustomRR()) {
4915 // Superclass is custom RR, therefore we are too.
4920 // Not class NSObject.
4921 auto& methods = cls->data()->methods;
4922 for (auto mlists = methods.beginLists(),
4923 end = methods.endLists();
4927 if (methodListImplementsRR(*mlists)) {
4934 if (!clsCustomRR) cls->setHasDefaultRR();
4936 // cls->printCustomAWZ();
4937 if (PrintCustomRR && clsCustomRR) cls->printCustomRR(inherited);
4939 // Update the +initialize flags.
4941 metacls->changeInfo(RW_INITIALIZED, RW_INITIALIZING);
4945 /***********************************************************************
4946 * Return YES if sel is used by retain/release implementors
4947 **********************************************************************/
4949 isRRSelector(SEL sel)
4951 return (sel == SEL_retain || sel == SEL_release ||
4952 sel == SEL_autorelease || sel == SEL_retainCount ||
4953 sel == SEL_tryRetain || sel == SEL_retainWeakReference ||
4954 sel == SEL_isDeallocating || sel == SEL_allowsWeakReference);
4958 /***********************************************************************
4959 * Return YES if mlist implements one of the isRRSelector() methods
4960 **********************************************************************/
4962 methodListImplementsRR(const method_list_t *mlist)
4964 return (search_method_list(mlist, SEL_retain) ||
4965 search_method_list(mlist, SEL_release) ||
4966 search_method_list(mlist, SEL_autorelease) ||
4967 search_method_list(mlist, SEL_retainCount) ||
4968 search_method_list(mlist, SEL_tryRetain) ||
4969 search_method_list(mlist, SEL_isDeallocating) ||
4970 search_method_list(mlist, SEL_retainWeakReference) ||
4971 search_method_list(mlist, SEL_allowsWeakReference));
4975 /***********************************************************************
4976 * Return YES if sel is used by alloc or allocWithZone implementors
4977 **********************************************************************/
4979 isAWZSelector(SEL sel)
4981 return (sel == SEL_allocWithZone || sel == SEL_alloc);
4985 /***********************************************************************
4986 * Return YES if mlist implements one of the isAWZSelector() methods
4987 **********************************************************************/
4989 methodListImplementsAWZ(const method_list_t *mlist)
4991 return (search_method_list(mlist, SEL_allocWithZone) ||
4992 search_method_list(mlist, SEL_alloc));
4997 objc_class::printCustomRR(bool inherited)
4999 assert(PrintCustomRR);
5000 assert(hasCustomRR());
5001 _objc_inform("CUSTOM RR: %s%s%s", nameForLogging(),
5002 isMetaClass() ? " (meta)" : "",
5003 inherited ? " (inherited)" : "");
5007 objc_class::printCustomAWZ(bool inherited)
5009 assert(PrintCustomAWZ);
5010 assert(hasCustomAWZ());
5011 _objc_inform("CUSTOM AWZ: %s%s%s", nameForLogging(),
5012 isMetaClass() ? " (meta)" : "",
5013 inherited ? " (inherited)" : "");
5017 objc_class::printInstancesRequireRawIsa(bool inherited)
5019 assert(PrintRawIsa);
5020 assert(instancesRequireRawIsa());
5021 _objc_inform("RAW ISA: %s%s%s", nameForLogging(),
5022 isMetaClass() ? " (meta)" : "",
5023 inherited ? " (inherited)" : "");
5027 /***********************************************************************
5028 * Mark this class and all of its subclasses as implementors or
5029 * inheritors of custom RR (retain/release/autorelease/retainCount)
5030 **********************************************************************/
5031 void objc_class::setHasCustomRR(bool inherited)
5033 Class cls = (Class)this;
5034 runtimeLock.assertWriting();
5036 if (hasCustomRR()) return;
5038 foreach_realized_class_and_subclass(cls, ^(Class c){
5039 if (c != cls && !c->isInitialized()) {
5040 // Subclass not yet initialized. Wait for setInitialized() to do it
5041 // fixme short circuit recursion?
5044 if (c->hasCustomRR()) {
5045 // fixme short circuit recursion?
5049 c->bits.setHasCustomRR();
5051 if (PrintCustomRR) c->printCustomRR(inherited || c != cls);
5055 /***********************************************************************
5056 * Mark this class and all of its subclasses as implementors or
5057 * inheritors of custom alloc/allocWithZone:
5058 **********************************************************************/
5059 void objc_class::setHasCustomAWZ(bool inherited)
5061 Class cls = (Class)this;
5062 runtimeLock.assertWriting();
5064 if (hasCustomAWZ()) return;
5066 foreach_realized_class_and_subclass(cls, ^(Class c){
5067 if (c != cls && !c->isInitialized()) {
5068 // Subclass not yet initialized. Wait for setInitialized() to do it
5069 // fixme short circuit recursion?
5072 if (c->hasCustomAWZ()) {
5073 // fixme short circuit recursion?
5077 c->bits.setHasCustomAWZ();
5079 if (PrintCustomAWZ) c->printCustomAWZ(inherited || c != cls);
5084 /***********************************************************************
5085 * Mark this class and all of its subclasses as requiring raw isa pointers
5086 **********************************************************************/
5087 void objc_class::setInstancesRequireRawIsa(bool inherited)
5089 Class cls = (Class)this;
5090 runtimeLock.assertWriting();
5092 if (instancesRequireRawIsa()) return;
5094 foreach_realized_class_and_subclass(cls, ^(Class c){
5095 if (c->instancesRequireRawIsa()) {
5096 // fixme short circuit recursion?
5100 c->bits.setInstancesRequireRawIsa();
5102 if (PrintRawIsa) c->printInstancesRequireRawIsa(inherited || c != cls);
5107 /***********************************************************************
5108 * Choose a class index.
5109 * Set instancesRequireRawIsa if no more class indexes are available.
5110 **********************************************************************/
5111 void objc_class::chooseClassArrayIndex()
5113 #if SUPPORT_INDEXED_ISA
5114 Class cls = (Class)this;
5115 runtimeLock.assertWriting();
5117 if (objc_indexed_classes_count >= ISA_INDEX_COUNT) {
5118 // No more indexes available.
5119 assert(cls->classArrayIndex() == 0);
5120 cls->setInstancesRequireRawIsa(false/*not inherited*/);
5124 unsigned index = objc_indexed_classes_count++;
5125 if (index == 0) index = objc_indexed_classes_count++; // index 0 is unused
5126 classForIndex(index) = cls;
5127 cls->setClassArrayIndex(index);
5132 /***********************************************************************
5133 * Update custom RR and AWZ when a method changes its IMP
5134 **********************************************************************/
5136 updateCustomRR_AWZ(Class cls, method_t *meth)
5138 // In almost all cases, IMP swizzling does not affect custom RR/AWZ bits.
5139 // Custom RR/AWZ search will already find the method whether or not
5140 // it is swizzled, so it does not transition from non-custom to custom.
5142 // The only cases where IMP swizzling can affect the RR/AWZ bits is
5143 // if the swizzled method is one of the methods that is assumed to be
5144 // non-custom. These special cases are listed in setInitialized().
5145 // We look for such cases here.
5147 if (isRRSelector(meth->name)) {
5149 if ((classNSObject()->isInitialized() &&
5150 classNSObject()->hasCustomRR())
5152 ClassNSObjectRRSwizzled)
5154 // already custom, nothing would change
5158 bool swizzlingNSObject = NO;
5159 if (cls == classNSObject()) {
5160 swizzlingNSObject = YES;
5162 // Don't know the class.
5163 // The only special case is class NSObject.
5164 for (const auto& meth2 : classNSObject()->data()->methods) {
5165 if (meth == &meth2) {
5166 swizzlingNSObject = YES;
5171 if (swizzlingNSObject) {
5172 if (classNSObject()->isInitialized()) {
5173 classNSObject()->setHasCustomRR();
5175 // NSObject not yet +initialized, so custom RR has not yet
5176 // been checked, and setInitialized() will not notice the
5178 ClassNSObjectRRSwizzled = YES;
5182 else if (isAWZSelector(meth->name)) {
5183 Class metaclassNSObject = classNSObject()->ISA();
5185 if ((metaclassNSObject->isInitialized() &&
5186 metaclassNSObject->hasCustomAWZ())
5188 MetaclassNSObjectAWZSwizzled)
5190 // already custom, nothing would change
5194 bool swizzlingNSObject = NO;
5195 if (cls == metaclassNSObject) {
5196 swizzlingNSObject = YES;
5198 // Don't know the class.
5199 // The only special case is metaclass NSObject.
5200 for (const auto& meth2 : metaclassNSObject->data()->methods) {
5201 if (meth == &meth2) {
5202 swizzlingNSObject = YES;
5207 if (swizzlingNSObject) {
5208 if (metaclassNSObject->isInitialized()) {
5209 metaclassNSObject->setHasCustomAWZ();
5211 // NSObject not yet +initialized, so custom RR has not yet
5212 // been checked, and setInitialized() will not notice the
5214 MetaclassNSObjectAWZSwizzled = YES;
5221 /***********************************************************************
5222 * class_getIvarLayout
5223 * Called by the garbage collector.
5224 * The class must be nil or already realized.
5226 **********************************************************************/
5228 class_getIvarLayout(Class cls)
5230 if (cls) return cls->data()->ro->ivarLayout;
5235 /***********************************************************************
5236 * class_getWeakIvarLayout
5237 * Called by the garbage collector.
5238 * The class must be nil or already realized.
5240 **********************************************************************/
5242 class_getWeakIvarLayout(Class cls)
5244 if (cls) return cls->data()->ro->weakIvarLayout;
5249 /***********************************************************************
5250 * class_setIvarLayout
5251 * Changes the class's ivar layout.
5252 * nil layout means no unscanned ivars
5253 * The class must be under construction.
5254 * fixme: sanity-check layout vs instance size?
5255 * fixme: sanity-check layout vs superclass?
5256 * Locking: acquires runtimeLock
5257 **********************************************************************/
5259 class_setIvarLayout(Class cls, const uint8_t *layout)
5263 rwlock_writer_t lock(runtimeLock);
5265 // Can only change layout of in-construction classes.
5266 // note: if modifications to post-construction classes were
5267 // allowed, there would be a race below (us vs. concurrent object_setIvar)
5268 if (!(cls->data()->flags & RW_CONSTRUCTING)) {
5269 _objc_inform("*** Can't set ivar layout for already-registered "
5270 "class '%s'", cls->nameForLogging());
5274 class_ro_t *ro_w = make_ro_writeable(cls->data());
5276 try_free(ro_w->ivarLayout);
5277 ro_w->ivarLayout = ustrdupMaybeNil(layout);
5280 // SPI: Instance-specific object layout.
5283 _class_setIvarLayoutAccessor(Class cls, const uint8_t* (*accessor) (id object)) {
5286 rwlock_writer_t lock(runtimeLock);
5288 class_ro_t *ro_w = make_ro_writeable(cls->data());
5290 // FIXME: this really isn't safe to free if there are instances of this class already.
5291 if (!(cls->data()->flags & RW_HAS_INSTANCE_SPECIFIC_LAYOUT)) try_free(ro_w->ivarLayout);
5292 ro_w->ivarLayout = (uint8_t *)accessor;
5293 cls->setInfo(RW_HAS_INSTANCE_SPECIFIC_LAYOUT);
5297 _object_getIvarLayout(Class cls, id object)
5300 const uint8_t* layout = cls->data()->ro->ivarLayout;
5301 if (cls->data()->flags & RW_HAS_INSTANCE_SPECIFIC_LAYOUT) {
5302 const uint8_t* (*accessor) (id object) = (const uint8_t* (*)(id))layout;
5303 layout = accessor(object);
5310 /***********************************************************************
5311 * class_setWeakIvarLayout
5312 * Changes the class's weak ivar layout.
5313 * nil layout means no weak ivars
5314 * The class must be under construction.
5315 * fixme: sanity-check layout vs instance size?
5316 * fixme: sanity-check layout vs superclass?
5317 * Locking: acquires runtimeLock
5318 **********************************************************************/
5320 class_setWeakIvarLayout(Class cls, const uint8_t *layout)
5324 rwlock_writer_t lock(runtimeLock);
5326 // Can only change layout of in-construction classes.
5327 // note: if modifications to post-construction classes were
5328 // allowed, there would be a race below (us vs. concurrent object_setIvar)
5329 if (!(cls->data()->flags & RW_CONSTRUCTING)) {
5330 _objc_inform("*** Can't set weak ivar layout for already-registered "
5331 "class '%s'", cls->nameForLogging());
5335 class_ro_t *ro_w = make_ro_writeable(cls->data());
5337 try_free(ro_w->weakIvarLayout);
5338 ro_w->weakIvarLayout = ustrdupMaybeNil(layout);
5342 /***********************************************************************
5344 * Look up an ivar by name.
5345 * Locking: runtimeLock must be read- or write-locked by the caller.
5346 **********************************************************************/
5347 static ivar_t *getIvar(Class cls, const char *name)
5349 runtimeLock.assertLocked();
5351 const ivar_list_t *ivars;
5352 assert(cls->isRealized());
5353 if ((ivars = cls->data()->ro->ivars)) {
5354 for (auto& ivar : *ivars) {
5355 if (!ivar.offset) continue; // anonymous bitfield
5357 // ivar.name may be nil for anonymous bitfields etc.
5358 if (ivar.name && 0 == strcmp(name, ivar.name)) {
5368 /***********************************************************************
5369 * _class_getClassForIvar
5370 * Given a class and an ivar that is in it or one of its superclasses,
5371 * find the actual class that defined the ivar.
5372 **********************************************************************/
5373 Class _class_getClassForIvar(Class cls, Ivar ivar)
5375 rwlock_reader_t lock(runtimeLock);
5377 for ( ; cls; cls = cls->superclass) {
5378 if (auto ivars = cls->data()->ro->ivars) {
5379 if (ivars->containsIvar(ivar)) {
5389 /***********************************************************************
5390 * _class_getVariable
5392 * Locking: read-locks runtimeLock
5393 **********************************************************************/
5395 _class_getVariable(Class cls, const char *name)
5397 rwlock_reader_t lock(runtimeLock);
5399 for ( ; cls; cls = cls->superclass) {
5400 ivar_t *ivar = getIvar(cls, name);
5410 /***********************************************************************
5411 * class_conformsToProtocol
5413 * Locking: read-locks runtimeLock
5414 **********************************************************************/
5415 BOOL class_conformsToProtocol(Class cls, Protocol *proto_gen)
5417 protocol_t *proto = newprotocol(proto_gen);
5419 if (!cls) return NO;
5420 if (!proto_gen) return NO;
5422 rwlock_reader_t lock(runtimeLock);
5424 assert(cls->isRealized());
5426 for (const auto& proto_ref : cls->data()->protocols) {
5427 protocol_t *p = remapProtocol(proto_ref);
5428 if (p == proto || protocol_conformsToProtocol_nolock(p, proto)) {
5437 /**********************************************************************
5440 * Locking: runtimeLock must be held by the caller
5441 **********************************************************************/
5443 addMethod(Class cls, SEL name, IMP imp, const char *types, bool replace)
5447 runtimeLock.assertWriting();
5450 assert(cls->isRealized());
5453 if ((m = getMethodNoSuper_nolock(cls, name))) {
5458 result = _method_setImplementation(cls, m, imp);
5462 method_list_t *newlist;
5463 newlist = (method_list_t *)calloc(sizeof(*newlist), 1);
5464 newlist->entsizeAndFlags =
5465 (uint32_t)sizeof(method_t) | fixed_up_method_list;
5467 newlist->first.name = name;
5468 newlist->first.types = strdupIfMutable(types);
5469 newlist->first.imp = imp;
5471 prepareMethodLists(cls, &newlist, 1, NO, NO);
5472 cls->data()->methods.attachLists(&newlist, 1);
5483 class_addMethod(Class cls, SEL name, IMP imp, const char *types)
5485 if (!cls) return NO;
5487 rwlock_writer_t lock(runtimeLock);
5488 return ! addMethod(cls, name, imp, types ?: "", NO);
5493 class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
5495 if (!cls) return nil;
5497 rwlock_writer_t lock(runtimeLock);
5498 return addMethod(cls, name, imp, types ?: "", YES);
5502 /***********************************************************************
5504 * Adds an ivar to a class.
5505 * Locking: acquires runtimeLock
5506 **********************************************************************/
5508 class_addIvar(Class cls, const char *name, size_t size,
5509 uint8_t alignment, const char *type)
5511 if (!cls) return NO;
5513 if (!type) type = "";
5514 if (name && 0 == strcmp(name, "")) name = nil;
5516 rwlock_writer_t lock(runtimeLock);
5518 assert(cls->isRealized());
5520 // No class variables
5521 if (cls->isMetaClass()) {
5525 // Can only add ivars to in-construction classes.
5526 if (!(cls->data()->flags & RW_CONSTRUCTING)) {
5530 // Check for existing ivar with this name, unless it's anonymous.
5531 // Check for too-big ivar.
5532 // fixme check for superclass ivar too?
5533 if ((name && getIvar(cls, name)) || size > UINT32_MAX) {
5537 class_ro_t *ro_w = make_ro_writeable(cls->data());
5539 // fixme allocate less memory here
5541 ivar_list_t *oldlist, *newlist;
5542 if ((oldlist = (ivar_list_t *)cls->data()->ro->ivars)) {
5543 size_t oldsize = oldlist->byteSize();
5544 newlist = (ivar_list_t *)calloc(oldsize + oldlist->entsize(), 1);
5545 memcpy(newlist, oldlist, oldsize);
5548 newlist = (ivar_list_t *)calloc(sizeof(ivar_list_t), 1);
5549 newlist->entsizeAndFlags = (uint32_t)sizeof(ivar_t);
5552 uint32_t offset = cls->unalignedInstanceSize();
5553 uint32_t alignMask = (1<<alignment)-1;
5554 offset = (offset + alignMask) & ~alignMask;
5556 ivar_t& ivar = newlist->get(newlist->count++);
5558 // Deliberately over-allocate the ivar offset variable.
5559 // Use calloc() to clear all 64 bits. See the note in struct ivar_t.
5560 ivar.offset = (int32_t *)(int64_t *)calloc(sizeof(int64_t), 1);
5562 ivar.offset = (int32_t *)malloc(sizeof(int32_t));
5564 *ivar.offset = offset;
5565 ivar.name = name ? strdupIfMutable(name) : nil;
5566 ivar.type = strdupIfMutable(type);
5567 ivar.alignment_raw = alignment;
5568 ivar.size = (uint32_t)size;
5570 ro_w->ivars = newlist;
5571 cls->setInstanceSize((uint32_t)(offset + size));
5573 // Ivar layout updated in registerClass.
5579 /***********************************************************************
5581 * Adds a protocol to a class.
5582 * Locking: acquires runtimeLock
5583 **********************************************************************/
5584 BOOL class_addProtocol(Class cls, Protocol *protocol_gen)
5586 protocol_t *protocol = newprotocol(protocol_gen);
5588 if (!cls) return NO;
5589 if (class_conformsToProtocol(cls, protocol_gen)) return NO;
5591 rwlock_writer_t lock(runtimeLock);
5593 assert(cls->isRealized());
5596 protocol_list_t *protolist = (protocol_list_t *)
5597 malloc(sizeof(protocol_list_t) + sizeof(protocol_t *));
5598 protolist->count = 1;
5599 protolist->list[0] = (protocol_ref_t)protocol;
5601 cls->data()->protocols.attachLists(&protolist, 1);
5609 /***********************************************************************
5611 * Adds a property to a class.
5612 * Locking: acquires runtimeLock
5613 **********************************************************************/
5615 _class_addProperty(Class cls, const char *name,
5616 const objc_property_attribute_t *attrs, unsigned int count,
5619 if (!cls) return NO;
5620 if (!name) return NO;
5622 property_t *prop = class_getProperty(cls, name);
5623 if (prop && !replace) {
5624 // already exists, refuse to replace
5629 rwlock_writer_t lock(runtimeLock);
5630 try_free(prop->attributes);
5631 prop->attributes = copyPropertyAttributeString(attrs, count);
5635 rwlock_writer_t lock(runtimeLock);
5637 assert(cls->isRealized());
5639 property_list_t *proplist = (property_list_t *)
5640 malloc(sizeof(*proplist));
5641 proplist->count = 1;
5642 proplist->entsizeAndFlags = sizeof(proplist->first);
5643 proplist->first.name = strdupIfMutable(name);
5644 proplist->first.attributes = copyPropertyAttributeString(attrs, count);
5646 cls->data()->properties.attachLists(&proplist, 1);
5653 class_addProperty(Class cls, const char *name,
5654 const objc_property_attribute_t *attrs, unsigned int n)
5656 return _class_addProperty(cls, name, attrs, n, NO);
5660 class_replaceProperty(Class cls, const char *name,
5661 const objc_property_attribute_t *attrs, unsigned int n)
5663 _class_addProperty(cls, name, attrs, n, YES);
5667 /***********************************************************************
5669 * Look up a class by name, and realize it.
5670 * Locking: acquires runtimeLock
5671 **********************************************************************/
5673 look_up_class(const char *name,
5674 bool includeUnconnected __attribute__((unused)),
5675 bool includeClassHandler __attribute__((unused)))
5677 if (!name) return nil;
5682 rwlock_reader_t lock(runtimeLock);
5683 result = getClass(name);
5684 unrealized = result && !result->isRealized();
5687 rwlock_writer_t lock(runtimeLock);
5688 realizeClass(result);
5694 /***********************************************************************
5695 * objc_duplicateClass
5697 * Locking: acquires runtimeLock
5698 **********************************************************************/
5700 objc_duplicateClass(Class original, const char *name,
5705 rwlock_writer_t lock(runtimeLock);
5707 assert(original->isRealized());
5708 assert(!original->isMetaClass());
5710 duplicate = alloc_class_for_subclass(original, extraBytes);
5712 duplicate->initClassIsa(original->ISA());
5713 duplicate->superclass = original->superclass;
5715 duplicate->cache.initializeToEmpty();
5717 class_rw_t *rw = (class_rw_t *)calloc(sizeof(*original->data()), 1);
5718 rw->flags = (original->data()->flags | RW_COPIED_RO | RW_REALIZING);
5719 rw->version = original->data()->version;
5720 rw->firstSubclass = nil;
5721 rw->nextSiblingClass = nil;
5723 duplicate->bits = original->bits;
5724 duplicate->setData(rw);
5726 rw->ro = (class_ro_t *)
5727 memdup(original->data()->ro, sizeof(*original->data()->ro));
5728 *(char **)&rw->ro->name = strdupIfMutable(name);
5730 rw->methods = original->data()->methods.duplicate();
5732 // fixme dies when categories are added to the base
5733 rw->properties = original->data()->properties;
5734 rw->protocols = original->data()->protocols;
5736 duplicate->chooseClassArrayIndex();
5738 if (duplicate->superclass) {
5739 addSubclass(duplicate->superclass, duplicate);
5740 // duplicate->isa == original->isa so don't addSubclass() for it
5742 addRootClass(duplicate);
5745 // Don't methodize class - construction above is correct
5747 addNamedClass(duplicate, duplicate->data()->ro->name);
5749 if (PrintConnecting) {
5750 _objc_inform("CLASS: realizing class '%s' (duplicate of %s) %p %p",
5751 name, original->nameForLogging(),
5752 (void*)duplicate, duplicate->data()->ro);
5755 duplicate->clearInfo(RW_REALIZING);
5760 /***********************************************************************
5761 * objc_initializeClassPair
5762 * Locking: runtimeLock must be write-locked by the caller
5763 **********************************************************************/
5765 // &UnsetLayout is the default ivar layout during class construction
5766 static const uint8_t UnsetLayout = 0;
5768 static void objc_initializeClassPair_internal(Class superclass, const char *name, Class cls, Class meta)
5770 runtimeLock.assertWriting();
5772 class_ro_t *cls_ro_w, *meta_ro_w;
5774 cls->setData((class_rw_t *)calloc(sizeof(class_rw_t), 1));
5775 meta->setData((class_rw_t *)calloc(sizeof(class_rw_t), 1));
5776 cls_ro_w = (class_ro_t *)calloc(sizeof(class_ro_t), 1);
5777 meta_ro_w = (class_ro_t *)calloc(sizeof(class_ro_t), 1);
5778 cls->data()->ro = cls_ro_w;
5779 meta->data()->ro = meta_ro_w;
5783 cls->data()->flags = RW_CONSTRUCTING | RW_COPIED_RO | RW_REALIZED | RW_REALIZING;
5784 meta->data()->flags = RW_CONSTRUCTING | RW_COPIED_RO | RW_REALIZED | RW_REALIZING;
5785 cls->data()->version = 0;
5786 meta->data()->version = 7;
5788 cls_ro_w->flags = 0;
5789 meta_ro_w->flags = RO_META;
5791 cls_ro_w->flags |= RO_ROOT;
5792 meta_ro_w->flags |= RO_ROOT;
5795 cls_ro_w->instanceStart = superclass->unalignedInstanceSize();
5796 meta_ro_w->instanceStart = superclass->ISA()->unalignedInstanceSize();
5797 cls->setInstanceSize(cls_ro_w->instanceStart);
5798 meta->setInstanceSize(meta_ro_w->instanceStart);
5800 cls_ro_w->instanceStart = 0;
5801 meta_ro_w->instanceStart = (uint32_t)sizeof(objc_class);
5802 cls->setInstanceSize((uint32_t)sizeof(id)); // just an isa
5803 meta->setInstanceSize(meta_ro_w->instanceStart);
5806 cls_ro_w->name = strdupIfMutable(name);
5807 meta_ro_w->name = strdupIfMutable(name);
5809 cls_ro_w->ivarLayout = &UnsetLayout;
5810 cls_ro_w->weakIvarLayout = &UnsetLayout;
5812 meta->chooseClassArrayIndex();
5813 cls->chooseClassArrayIndex();
5815 // Connect to superclasses and metaclasses
5816 cls->initClassIsa(meta);
5818 meta->initClassIsa(superclass->ISA()->ISA());
5819 cls->superclass = superclass;
5820 meta->superclass = superclass->ISA();
5821 addSubclass(superclass, cls);
5822 addSubclass(superclass->ISA(), meta);
5824 meta->initClassIsa(meta);
5825 cls->superclass = Nil;
5826 meta->superclass = cls;
5828 addSubclass(cls, meta);
5831 cls->cache.initializeToEmpty();
5832 meta->cache.initializeToEmpty();
5836 /***********************************************************************
5838 * Sanity-check the superclass provided to
5839 * objc_allocateClassPair, objc_initializeClassPair, or objc_readClassPair.
5840 **********************************************************************/
5842 verifySuperclass(Class superclass, bool rootOK)
5845 // Superclass does not exist.
5846 // If subclass may be a root class, this is OK.
5847 // If subclass must not be a root class, this is bad.
5851 // Superclass must be realized.
5852 if (! superclass->isRealized()) return false;
5854 // Superclass must not be under construction.
5855 if (superclass->data()->flags & RW_CONSTRUCTING) return false;
5861 /***********************************************************************
5862 * objc_initializeClassPair
5863 **********************************************************************/
5864 Class objc_initializeClassPair(Class superclass, const char *name, Class cls, Class meta)
5866 rwlock_writer_t lock(runtimeLock);
5868 // Fail if the class name is in use.
5869 // Fail if the superclass isn't kosher.
5870 if (getClass(name) || !verifySuperclass(superclass, true/*rootOK*/)) {
5874 objc_initializeClassPair_internal(superclass, name, cls, meta);
5880 /***********************************************************************
5881 * objc_allocateClassPair
5883 * Locking: acquires runtimeLock
5884 **********************************************************************/
5885 Class objc_allocateClassPair(Class superclass, const char *name,
5890 rwlock_writer_t lock(runtimeLock);
5892 // Fail if the class name is in use.
5893 // Fail if the superclass isn't kosher.
5894 if (getClass(name) || !verifySuperclass(superclass, true/*rootOK*/)) {
5898 // Allocate new classes.
5899 cls = alloc_class_for_subclass(superclass, extraBytes);
5900 meta = alloc_class_for_subclass(superclass, extraBytes);
5902 // fixme mangle the name if it looks swift-y?
5903 objc_initializeClassPair_internal(superclass, name, cls, meta);
5909 /***********************************************************************
5910 * objc_registerClassPair
5912 * Locking: acquires runtimeLock
5913 **********************************************************************/
5914 void objc_registerClassPair(Class cls)
5916 rwlock_writer_t lock(runtimeLock);
5918 if ((cls->data()->flags & RW_CONSTRUCTED) ||
5919 (cls->ISA()->data()->flags & RW_CONSTRUCTED))
5921 _objc_inform("objc_registerClassPair: class '%s' was already "
5922 "registered!", cls->data()->ro->name);
5926 if (!(cls->data()->flags & RW_CONSTRUCTING) ||
5927 !(cls->ISA()->data()->flags & RW_CONSTRUCTING))
5929 _objc_inform("objc_registerClassPair: class '%s' was not "
5930 "allocated with objc_allocateClassPair!",
5931 cls->data()->ro->name);
5935 // Clear "under construction" bit, set "done constructing" bit
5936 cls->ISA()->changeInfo(RW_CONSTRUCTED, RW_CONSTRUCTING | RW_REALIZING);
5937 cls->changeInfo(RW_CONSTRUCTED, RW_CONSTRUCTING | RW_REALIZING);
5939 // Add to named class table.
5940 addNamedClass(cls, cls->data()->ro->name);
5944 /***********************************************************************
5945 * objc_readClassPair()
5946 * Read a class and metaclass as written by a compiler.
5947 * Assumes the class and metaclass are not referenced by other things
5948 * that might need to be fixed up (such as categories and subclasses).
5949 * Does not call +load.
5950 * Returns the class pointer, or nil.
5952 * Locking: runtimeLock acquired by map_images
5953 **********************************************************************/
5954 Class objc_readClassPair(Class bits, const struct objc_image_info *info)
5956 rwlock_writer_t lock(runtimeLock);
5958 // No info bits are significant yet.
5961 // Fail if the class name is in use.
5962 // Fail if the superclass isn't kosher.
5963 const char *name = bits->mangledName();
5964 bool rootOK = bits->data()->flags & RO_ROOT;
5965 if (getClass(name) || !verifySuperclass(bits->superclass, rootOK)){
5969 Class cls = readClass(bits, false/*bundle*/, false/*shared cache*/);
5971 // This function isn't allowed to remap anything.
5972 _objc_fatal("objc_readClassPair for class %s changed %p to %p",
5973 cls->nameForLogging(), bits, cls);
5981 /***********************************************************************
5983 * Disconnect a class from other data structures.
5984 * Exception: does not remove the class from the +load list
5985 * Call this before free_class.
5986 * Locking: runtimeLock must be held by the caller.
5987 **********************************************************************/
5988 static void detach_class(Class cls, bool isMeta)
5990 runtimeLock.assertWriting();
5992 // categories not yet attached to this class
5993 removeAllUnattachedCategoriesForClass(cls);
5995 // superclass's subclass list
5996 if (cls->isRealized()) {
5997 Class supercls = cls->superclass;
5999 removeSubclass(supercls, cls);
6001 removeRootClass(cls);
6005 // class tables and +load queue
6007 removeNamedClass(cls, cls->mangledName());
6012 /***********************************************************************
6014 * Frees a class's data structures.
6015 * Call this after detach_class.
6016 * Locking: runtimeLock must be held by the caller
6017 **********************************************************************/
6018 static void free_class(Class cls)
6020 runtimeLock.assertWriting();
6022 if (! cls->isRealized()) return;
6024 auto rw = cls->data();
6029 for (auto& meth : rw->methods) {
6030 try_free(meth.types);
6032 rw->methods.tryFree();
6034 const ivar_list_t *ivars = ro->ivars;
6036 for (auto& ivar : *ivars) {
6037 try_free(ivar.offset);
6038 try_free(ivar.name);
6039 try_free(ivar.type);
6044 for (auto& prop : rw->properties) {
6045 try_free(prop.name);
6046 try_free(prop.attributes);
6048 rw->properties.tryFree();
6050 rw->protocols.tryFree();
6052 try_free(ro->ivarLayout);
6053 try_free(ro->weakIvarLayout);
6061 void objc_disposeClassPair(Class cls)
6063 rwlock_writer_t lock(runtimeLock);
6065 if (!(cls->data()->flags & (RW_CONSTRUCTED|RW_CONSTRUCTING)) ||
6066 !(cls->ISA()->data()->flags & (RW_CONSTRUCTED|RW_CONSTRUCTING)))
6068 // class not allocated with objc_allocateClassPair
6069 // disposing still-unregistered class is OK!
6070 _objc_inform("objc_disposeClassPair: class '%s' was not "
6071 "allocated with objc_allocateClassPair!",
6072 cls->data()->ro->name);
6076 if (cls->isMetaClass()) {
6077 _objc_inform("objc_disposeClassPair: class '%s' is a metaclass, "
6078 "not a class!", cls->data()->ro->name);
6082 // Shouldn't have any live subclasses.
6083 if (cls->data()->firstSubclass) {
6084 _objc_inform("objc_disposeClassPair: class '%s' still has subclasses, "
6085 "including '%s'!", cls->data()->ro->name,
6086 cls->data()->firstSubclass->nameForLogging());
6088 if (cls->ISA()->data()->firstSubclass) {
6089 _objc_inform("objc_disposeClassPair: class '%s' still has subclasses, "
6090 "including '%s'!", cls->data()->ro->name,
6091 cls->ISA()->data()->firstSubclass->nameForLogging());
6094 // don't remove_class_from_loadable_list()
6095 // - it's not there and we don't have the lock
6096 detach_class(cls->ISA(), YES);
6097 detach_class(cls, NO);
6098 free_class(cls->ISA());
6103 /***********************************************************************
6104 * objc_constructInstance
6105 * Creates an instance of `cls` at the location pointed to by `bytes`.
6106 * `bytes` must point to at least class_getInstanceSize(cls) bytes of
6107 * well-aligned zero-filled memory.
6108 * The new object's isa is set. Any C++ constructors are called.
6109 * Returns `bytes` if successful. Returns nil if `cls` or `bytes` is
6110 * nil, or if C++ constructors fail.
6111 * Note: class_createInstance() and class_createInstances() preflight this.
6112 **********************************************************************/
6114 objc_constructInstance(Class cls, void *bytes)
6116 if (!cls || !bytes) return nil;
6120 // Read class's info bits all at once for performance
6121 bool hasCxxCtor = cls->hasCxxCtor();
6122 bool hasCxxDtor = cls->hasCxxDtor();
6123 bool fast = cls->canAllocNonpointer();
6126 obj->initInstanceIsa(cls, hasCxxDtor);
6132 return object_cxxConstructFromClass(obj, cls);
6139 /***********************************************************************
6140 * class_createInstance
6143 **********************************************************************/
6145 static __attribute__((always_inline))
6147 _class_createInstanceFromZone(Class cls, size_t extraBytes, void *zone,
6148 bool cxxConstruct = true,
6149 size_t *outAllocatedSize = nil)
6151 if (!cls) return nil;
6153 assert(cls->isRealized());
6155 // Read class's info bits all at once for performance
6156 bool hasCxxCtor = cls->hasCxxCtor();
6157 bool hasCxxDtor = cls->hasCxxDtor();
6158 bool fast = cls->canAllocNonpointer();
6160 size_t size = cls->instanceSize(extraBytes);
6161 if (outAllocatedSize) *outAllocatedSize = size;
6164 if (!zone && fast) {
6165 obj = (id)calloc(1, size);
6166 if (!obj) return nil;
6167 obj->initInstanceIsa(cls, hasCxxDtor);
6171 obj = (id)malloc_zone_calloc ((malloc_zone_t *)zone, 1, size);
6173 obj = (id)calloc(1, size);
6175 if (!obj) return nil;
6177 // Use raw pointer isa on the assumption that they might be
6178 // doing something weird with the zone or RR.
6182 if (cxxConstruct && hasCxxCtor) {
6183 obj = _objc_constructOrFree(obj, cls);
6191 class_createInstance(Class cls, size_t extraBytes)
6193 return _class_createInstanceFromZone(cls, extraBytes, nil);
6197 /***********************************************************************
6198 * class_createInstances
6201 **********************************************************************/
6202 #if SUPPORT_NONPOINTER_ISA
6203 #warning fixme optimize class_createInstances
6206 class_createInstances(Class cls, size_t extraBytes,
6207 id *results, unsigned num_requested)
6209 return _class_createInstancesFromZone(cls, extraBytes, nil,
6210 results, num_requested);
6213 /***********************************************************************
6214 * object_copyFromZone
6217 **********************************************************************/
6219 _object_copyFromZone(id oldObj, size_t extraBytes, void *zone)
6221 if (!oldObj) return nil;
6222 if (oldObj->isTaggedPointer()) return oldObj;
6224 // fixme this doesn't handle C++ ivars correctly (#4619414)
6226 Class cls = oldObj->ISA();
6228 id obj = _class_createInstanceFromZone(cls, extraBytes, zone, false, &size);
6229 if (!obj) return nil;
6231 // Copy everything except the isa, which was already set above.
6232 uint8_t *copyDst = (uint8_t *)obj + sizeof(Class);
6233 uint8_t *copySrc = (uint8_t *)oldObj + sizeof(Class);
6234 size_t copySize = size - sizeof(Class);
6235 memmove(copyDst, copySrc, copySize);
6237 fixupCopiedIvars(obj, oldObj);
6243 /***********************************************************************
6247 **********************************************************************/
6249 object_copy(id oldObj, size_t extraBytes)
6251 return _object_copyFromZone(oldObj, extraBytes, malloc_default_zone());
6255 #if !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)
6257 /***********************************************************************
6258 * class_createInstanceFromZone
6261 **********************************************************************/
6263 class_createInstanceFromZone(Class cls, size_t extraBytes, void *zone)
6265 return _class_createInstanceFromZone(cls, extraBytes, zone);
6268 /***********************************************************************
6269 * object_copyFromZone
6272 **********************************************************************/
6274 object_copyFromZone(id oldObj, size_t extraBytes, void *zone)
6276 return _object_copyFromZone(oldObj, extraBytes, zone);
6282 /***********************************************************************
6283 * objc_destructInstance
6284 * Destroys an instance without freeing memory.
6285 * Calls C++ destructors.
6286 * Calls ARC ivar cleanup.
6287 * Removes associative references.
6288 * Returns `obj`. Does nothing if `obj` is nil.
6289 **********************************************************************/
6290 void *objc_destructInstance(id obj)
6293 // Read all of the flags at once for performance.
6294 bool cxx = obj->hasCxxDtor();
6295 bool assoc = obj->hasAssociatedObjects();
6297 // This order is important.
6298 if (cxx) object_cxxDestruct(obj);
6299 if (assoc) _object_remove_assocations(obj);
6300 obj->clearDeallocating();
6307 /***********************************************************************
6311 **********************************************************************/
6313 object_dispose(id obj)
6315 if (!obj) return nil;
6317 objc_destructInstance(obj);
6324 /***********************************************************************
6325 * _objc_getFreedObjectClass
6328 **********************************************************************/
6329 Class _objc_getFreedObjectClass (void)
6336 /***********************************************************************
6337 * Tagged pointer objects.
6339 * Tagged pointer objects store the class and the object value in the
6340 * object pointer; the "pointer" does not actually point to anything.
6342 * Tagged pointer objects currently use this representation:
6344 * 1 bit set if tagged, clear if ordinary object pointer
6348 * The tag index defines the object's class.
6349 * The payload format is defined by the object's class.
6351 * If the tag index is 0b111, the tagged pointer object uses an
6352 * "extended" representation, allowing more classes but with smaller payloads:
6354 * 1 bit set if tagged, clear if ordinary object pointer
6356 * 8 bits extended tag index
6360 * Some architectures reverse the MSB and LSB in these representations.
6362 * This representation is subject to change. Representation-agnostic SPI is:
6363 * objc-internal.h for class implementers.
6364 * objc-gdb.h for debuggers.
6365 **********************************************************************/
6366 #if !SUPPORT_TAGGED_POINTERS
6368 // These variables are always provided for debuggers.
6369 uintptr_t objc_debug_taggedpointer_mask = 0;
6370 unsigned objc_debug_taggedpointer_slot_shift = 0;
6371 uintptr_t objc_debug_taggedpointer_slot_mask = 0;
6372 unsigned objc_debug_taggedpointer_payload_lshift = 0;
6373 unsigned objc_debug_taggedpointer_payload_rshift = 0;
6374 Class objc_debug_taggedpointer_classes[1] = { nil };
6376 uintptr_t objc_debug_taggedpointer_ext_mask = 0;
6377 unsigned objc_debug_taggedpointer_ext_slot_shift = 0;
6378 uintptr_t objc_debug_taggedpointer_ext_slot_mask = 0;
6379 unsigned objc_debug_taggedpointer_ext_payload_lshift = 0;
6380 unsigned objc_debug_taggedpointer_ext_payload_rshift = 0;
6381 Class objc_debug_taggedpointer_ext_classes[1] = { nil };
6384 disableTaggedPointers() { }
6388 // The "slot" used in the class table and given to the debugger
6389 // includes the is-tagged bit. This makes objc_msgSend faster.
6390 // The "ext" representation doesn't do that.
6392 uintptr_t objc_debug_taggedpointer_mask = _OBJC_TAG_MASK;
6393 unsigned objc_debug_taggedpointer_slot_shift = _OBJC_TAG_SLOT_SHIFT;
6394 uintptr_t objc_debug_taggedpointer_slot_mask = _OBJC_TAG_SLOT_MASK;
6395 unsigned objc_debug_taggedpointer_payload_lshift = _OBJC_TAG_PAYLOAD_LSHIFT;
6396 unsigned objc_debug_taggedpointer_payload_rshift = _OBJC_TAG_PAYLOAD_RSHIFT;
6397 // objc_debug_taggedpointer_classes is defined in objc-msg-*.s
6399 uintptr_t objc_debug_taggedpointer_ext_mask = _OBJC_TAG_EXT_MASK;
6400 unsigned objc_debug_taggedpointer_ext_slot_shift = _OBJC_TAG_EXT_SLOT_SHIFT;
6401 uintptr_t objc_debug_taggedpointer_ext_slot_mask = _OBJC_TAG_EXT_SLOT_MASK;
6402 unsigned objc_debug_taggedpointer_ext_payload_lshift = _OBJC_TAG_EXT_PAYLOAD_LSHIFT;
6403 unsigned objc_debug_taggedpointer_ext_payload_rshift = _OBJC_TAG_EXT_PAYLOAD_RSHIFT;
6404 // objc_debug_taggedpointer_ext_classes is defined in objc-msg-*.s
6407 disableTaggedPointers()
6409 objc_debug_taggedpointer_mask = 0;
6410 objc_debug_taggedpointer_slot_shift = 0;
6411 objc_debug_taggedpointer_slot_mask = 0;
6412 objc_debug_taggedpointer_payload_lshift = 0;
6413 objc_debug_taggedpointer_payload_rshift = 0;
6415 objc_debug_taggedpointer_ext_mask = 0;
6416 objc_debug_taggedpointer_ext_slot_shift = 0;
6417 objc_debug_taggedpointer_ext_slot_mask = 0;
6418 objc_debug_taggedpointer_ext_payload_lshift = 0;
6419 objc_debug_taggedpointer_ext_payload_rshift = 0;
6423 // Returns a pointer to the class's storage in the tagged class arrays.
6424 // Assumes the tag is a valid basic tag.
6426 classSlotForBasicTagIndex(objc_tag_index_t tag)
6428 // Array index in objc_tag_classes includes the tagged bit itself
6429 #if SUPPORT_MSB_TAGGED_POINTERS
6430 return &objc_tag_classes[0x8 | tag];
6432 return &objc_tag_classes[(tag << 1) | 1];
6437 // Returns a pointer to the class's storage in the tagged class arrays,
6438 // or nil if the tag is out of range.
6440 classSlotForTagIndex(objc_tag_index_t tag)
6442 if (tag >= OBJC_TAG_First60BitPayload && tag <= OBJC_TAG_Last60BitPayload) {
6443 return classSlotForBasicTagIndex(tag);
6446 if (tag >= OBJC_TAG_First52BitPayload && tag <= OBJC_TAG_Last52BitPayload) {
6447 return &objc_tag_ext_classes[tag - OBJC_TAG_First52BitPayload];
6454 /***********************************************************************
6455 * _objc_registerTaggedPointerClass
6456 * Set the class to use for the given tagged pointer index.
6457 * Aborts if the tag is out of range, or if the tag is already
6458 * used by some other class.
6459 **********************************************************************/
6461 _objc_registerTaggedPointerClass(objc_tag_index_t tag, Class cls)
6463 if (objc_debug_taggedpointer_mask == 0) {
6464 _objc_fatal("tagged pointers are disabled");
6467 Class *slot = classSlotForTagIndex(tag);
6469 _objc_fatal("tag index %u is invalid", (unsigned int)tag);
6472 Class oldCls = *slot;
6474 if (cls && oldCls && cls != oldCls) {
6475 _objc_fatal("tag index %u used for two different classes "
6476 "(was %p %s, now %p %s)", tag,
6477 oldCls, oldCls->nameForLogging(),
6478 cls, cls->nameForLogging());
6483 // Store a placeholder class in the basic tag slot that is
6484 // reserved for the extended tag space, if it isn't set already.
6485 // Do this lazily when the first extended tag is registered so
6486 // that old debuggers characterize bogus pointers correctly more often.
6487 if (tag < OBJC_TAG_First60BitPayload || tag > OBJC_TAG_Last60BitPayload) {
6488 Class *extSlot = classSlotForBasicTagIndex(OBJC_TAG_RESERVED_7);
6489 if (*extSlot == nil) {
6490 extern objc_class OBJC_CLASS_$___NSUnrecognizedTaggedPointer;
6491 *extSlot = (Class)&OBJC_CLASS_$___NSUnrecognizedTaggedPointer;
6497 /***********************************************************************
6498 * _objc_getClassForTag
6499 * Returns the class that is using the given tagged pointer tag.
6500 * Returns nil if no class is using that tag or the tag is out of range.
6501 **********************************************************************/
6503 _objc_getClassForTag(objc_tag_index_t tag)
6505 Class *slot = classSlotForTagIndex(tag);
6506 if (slot) return *slot;
6515 OBJC_EXTERN void objc_msgSend_fixup(void);
6516 OBJC_EXTERN void objc_msgSendSuper2_fixup(void);
6517 OBJC_EXTERN void objc_msgSend_stret_fixup(void);
6518 OBJC_EXTERN void objc_msgSendSuper2_stret_fixup(void);
6519 #if defined(__i386__) || defined(__x86_64__)
6520 OBJC_EXTERN void objc_msgSend_fpret_fixup(void);
6522 #if defined(__x86_64__)
6523 OBJC_EXTERN void objc_msgSend_fp2ret_fixup(void);
6526 OBJC_EXTERN void objc_msgSend_fixedup(void);
6527 OBJC_EXTERN void objc_msgSendSuper2_fixedup(void);
6528 OBJC_EXTERN void objc_msgSend_stret_fixedup(void);
6529 OBJC_EXTERN void objc_msgSendSuper2_stret_fixedup(void);
6530 #if defined(__i386__) || defined(__x86_64__)
6531 OBJC_EXTERN void objc_msgSend_fpret_fixedup(void);
6533 #if defined(__x86_64__)
6534 OBJC_EXTERN void objc_msgSend_fp2ret_fixedup(void);
6537 /***********************************************************************
6539 * Repairs an old vtable dispatch call site.
6540 * vtable dispatch itself is not supported.
6541 **********************************************************************/
6543 fixupMessageRef(message_ref_t *msg)
6545 msg->sel = sel_registerName((const char *)msg->sel);
6547 if (msg->imp == &objc_msgSend_fixup) {
6548 if (msg->sel == SEL_alloc) {
6549 msg->imp = (IMP)&objc_alloc;
6550 } else if (msg->sel == SEL_allocWithZone) {
6551 msg->imp = (IMP)&objc_allocWithZone;
6552 } else if (msg->sel == SEL_retain) {
6553 msg->imp = (IMP)&objc_retain;
6554 } else if (msg->sel == SEL_release) {
6555 msg->imp = (IMP)&objc_release;
6556 } else if (msg->sel == SEL_autorelease) {
6557 msg->imp = (IMP)&objc_autorelease;
6559 msg->imp = &objc_msgSend_fixedup;
6562 else if (msg->imp == &objc_msgSendSuper2_fixup) {
6563 msg->imp = &objc_msgSendSuper2_fixedup;
6565 else if (msg->imp == &objc_msgSend_stret_fixup) {
6566 msg->imp = &objc_msgSend_stret_fixedup;
6568 else if (msg->imp == &objc_msgSendSuper2_stret_fixup) {
6569 msg->imp = &objc_msgSendSuper2_stret_fixedup;
6571 #if defined(__i386__) || defined(__x86_64__)
6572 else if (msg->imp == &objc_msgSend_fpret_fixup) {
6573 msg->imp = &objc_msgSend_fpret_fixedup;
6576 #if defined(__x86_64__)
6577 else if (msg->imp == &objc_msgSend_fp2ret_fixup) {
6578 msg->imp = &objc_msgSend_fp2ret_fixedup;
6588 static Class setSuperclass(Class cls, Class newSuper)
6592 runtimeLock.assertWriting();
6594 assert(cls->isRealized());
6595 assert(newSuper->isRealized());
6597 oldSuper = cls->superclass;
6598 removeSubclass(oldSuper, cls);
6599 removeSubclass(oldSuper->ISA(), cls->ISA());
6601 cls->superclass = newSuper;
6602 cls->ISA()->superclass = newSuper->ISA();
6603 addSubclass(newSuper, cls);
6604 addSubclass(newSuper->ISA(), cls->ISA());
6606 // Flush subclass's method caches.
6608 flushCaches(cls->ISA());
6614 Class class_setSuperclass(Class cls, Class newSuper)
6616 rwlock_writer_t lock(runtimeLock);
6617 return setSuperclass(cls, newSuper);