objc-runtime-new.h 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. /*
  2. * Copyright (c) 2005-2007 Apple Inc. All Rights Reserved.
  3. *
  4. * @APPLE_LICENSE_HEADER_START@
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apple Public Source License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://www.opensource.apple.com/apsl/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. *
  21. * @APPLE_LICENSE_HEADER_END@
  22. */
  23. #ifndef _OBJC_RUNTIME_NEW_H
  24. #define _OBJC_RUNTIME_NEW_H
  25. #if __LP64__
  26. typedef uint32_t mask_t; // x86_64 & arm64 asm are less efficient with 16-bits
  27. #else
  28. typedef uint16_t mask_t;
  29. #endif
  30. typedef uintptr_t cache_key_t;
  31. struct swift_class_t;
  32. struct bucket_t {
  33. private:
  34. // IMP-first is better for arm64e ptrauth and no worse for arm64.
  35. // SEL-first is better for armv7* and i386 and x86_64.
  36. #if __arm64__
  37. MethodCacheIMP _imp;
  38. cache_key_t _key;
  39. #else
  40. cache_key_t _key;
  41. MethodCacheIMP _imp;
  42. #endif
  43. public:
  44. inline cache_key_t key() const { return _key; }
  45. inline IMP imp() const { return (IMP)_imp; }
  46. inline void setKey(cache_key_t newKey) { _key = newKey; }
  47. inline void setImp(IMP newImp) { _imp = newImp; }
  48. void set(cache_key_t newKey, IMP newImp);
  49. };
  50. struct cache_t {
  51. struct bucket_t *_buckets;
  52. mask_t _mask;
  53. mask_t _occupied;
  54. public:
  55. struct bucket_t *buckets();
  56. mask_t mask();
  57. mask_t occupied();
  58. void incrementOccupied();
  59. void setBucketsAndMask(struct bucket_t *newBuckets, mask_t newMask);
  60. void initializeToEmpty();
  61. mask_t capacity();
  62. bool isConstantEmptyCache();
  63. bool canBeFreed();
  64. static size_t bytesForCapacity(uint32_t cap);
  65. static struct bucket_t * endMarker(struct bucket_t *b, uint32_t cap);
  66. void expand();
  67. void reallocate(mask_t oldCapacity, mask_t newCapacity);
  68. struct bucket_t * find(cache_key_t key, id receiver);
  69. static void bad_cache(id receiver, SEL sel, Class isa) __attribute__((noreturn));
  70. };
  71. // classref_t is unremapped class_t*
  72. typedef struct classref * classref_t;
  73. /***********************************************************************
  74. * entsize_list_tt<Element, List, FlagMask>
  75. * Generic implementation of an array of non-fragile structs.
  76. *
  77. * Element is the struct type (e.g. method_t)
  78. * List is the specialization of entsize_list_tt (e.g. method_list_t)
  79. * FlagMask is used to stash extra bits in the entsize field
  80. * (e.g. method list fixup markers)
  81. **********************************************************************/
  82. template <typename Element, typename List, uint32_t FlagMask>
  83. struct entsize_list_tt {
  84. uint32_t entsizeAndFlags;
  85. uint32_t count;
  86. Element first;
  87. uint32_t entsize() const {
  88. return entsizeAndFlags & ~FlagMask;
  89. }
  90. uint32_t flags() const {
  91. return entsizeAndFlags & FlagMask;
  92. }
  93. Element& getOrEnd(uint32_t i) const {
  94. assert(i <= count);
  95. return *(Element *)((uint8_t *)&first + i*entsize());
  96. }
  97. Element& get(uint32_t i) const {
  98. assert(i < count);
  99. return getOrEnd(i);
  100. }
  101. size_t byteSize() const {
  102. return byteSize(entsize(), count);
  103. }
  104. static size_t byteSize(uint32_t entsize, uint32_t count) {
  105. return sizeof(entsize_list_tt) + (count-1)*entsize;
  106. }
  107. List *duplicate() const {
  108. auto *dup = (List *)calloc(this->byteSize(), 1);
  109. dup->entsizeAndFlags = this->entsizeAndFlags;
  110. dup->count = this->count;
  111. std::copy(begin(), end(), dup->begin());
  112. return dup;
  113. }
  114. struct iterator;
  115. const iterator begin() const {
  116. return iterator(*static_cast<const List*>(this), 0);
  117. }
  118. iterator begin() {
  119. return iterator(*static_cast<const List*>(this), 0);
  120. }
  121. const iterator end() const {
  122. return iterator(*static_cast<const List*>(this), count);
  123. }
  124. iterator end() {
  125. return iterator(*static_cast<const List*>(this), count);
  126. }
  127. struct iterator {
  128. uint32_t entsize;
  129. uint32_t index; // keeping track of this saves a divide in operator-
  130. Element* element;
  131. typedef std::random_access_iterator_tag iterator_category;
  132. typedef Element value_type;
  133. typedef ptrdiff_t difference_type;
  134. typedef Element* pointer;
  135. typedef Element& reference;
  136. iterator() { }
  137. iterator(const List& list, uint32_t start = 0)
  138. : entsize(list.entsize())
  139. , index(start)
  140. , element(&list.getOrEnd(start))
  141. { }
  142. const iterator& operator += (ptrdiff_t delta) {
  143. element = (Element*)((uint8_t *)element + delta*entsize);
  144. index += (int32_t)delta;
  145. return *this;
  146. }
  147. const iterator& operator -= (ptrdiff_t delta) {
  148. element = (Element*)((uint8_t *)element - delta*entsize);
  149. index -= (int32_t)delta;
  150. return *this;
  151. }
  152. const iterator operator + (ptrdiff_t delta) const {
  153. return iterator(*this) += delta;
  154. }
  155. const iterator operator - (ptrdiff_t delta) const {
  156. return iterator(*this) -= delta;
  157. }
  158. iterator& operator ++ () { *this += 1; return *this; }
  159. iterator& operator -- () { *this -= 1; return *this; }
  160. iterator operator ++ (int) {
  161. iterator result(*this); *this += 1; return result;
  162. }
  163. iterator operator -- (int) {
  164. iterator result(*this); *this -= 1; return result;
  165. }
  166. ptrdiff_t operator - (const iterator& rhs) const {
  167. return (ptrdiff_t)this->index - (ptrdiff_t)rhs.index;
  168. }
  169. Element& operator * () const { return *element; }
  170. Element* operator -> () const { return element; }
  171. operator Element& () const { return *element; }
  172. bool operator == (const iterator& rhs) const {
  173. return this->element == rhs.element;
  174. }
  175. bool operator != (const iterator& rhs) const {
  176. return this->element != rhs.element;
  177. }
  178. bool operator < (const iterator& rhs) const {
  179. return this->element < rhs.element;
  180. }
  181. bool operator > (const iterator& rhs) const {
  182. return this->element > rhs.element;
  183. }
  184. };
  185. };
  186. struct method_t {
  187. SEL name;
  188. const char *types;
  189. MethodListIMP imp;
  190. struct SortBySELAddress :
  191. public std::binary_function<const method_t&,
  192. const method_t&, bool>
  193. {
  194. bool operator() (const method_t& lhs,
  195. const method_t& rhs)
  196. { return lhs.name < rhs.name; }
  197. };
  198. };
  199. struct ivar_t {
  200. #if __x86_64__
  201. // *offset was originally 64-bit on some x86_64 platforms.
  202. // We read and write only 32 bits of it.
  203. // Some metadata provides all 64 bits. This is harmless for unsigned
  204. // little-endian values.
  205. // Some code uses all 64 bits. class_addIvar() over-allocates the
  206. // offset for their benefit.
  207. #endif
  208. int32_t *offset;
  209. const char *name;
  210. const char *type;
  211. // alignment is sometimes -1; use alignment() instead
  212. uint32_t alignment_raw;
  213. uint32_t size;
  214. uint32_t alignment() const {
  215. if (alignment_raw == ~(uint32_t)0) return 1U << WORD_SHIFT;
  216. return 1 << alignment_raw;
  217. }
  218. };
  219. struct property_t {
  220. const char *name;
  221. const char *attributes;
  222. };
  223. // Two bits of entsize are used for fixup markers.
  224. struct method_list_t : entsize_list_tt<method_t, method_list_t, 0x3> {
  225. bool isFixedUp() const;
  226. void setFixedUp();
  227. uint32_t indexOfMethod(const method_t *meth) const {
  228. uint32_t i =
  229. (uint32_t)(((uintptr_t)meth - (uintptr_t)this) / entsize());
  230. assert(i < count);
  231. return i;
  232. }
  233. };
  234. struct ivar_list_t : entsize_list_tt<ivar_t, ivar_list_t, 0> {
  235. bool containsIvar(Ivar ivar) const {
  236. return (ivar >= (Ivar)&*begin() && ivar < (Ivar)&*end());
  237. }
  238. };
  239. struct property_list_t : entsize_list_tt<property_t, property_list_t, 0> {
  240. };
  241. typedef uintptr_t protocol_ref_t; // protocol_t *, but unremapped
  242. // Values for protocol_t->flags
  243. #define PROTOCOL_FIXED_UP_2 (1<<31) // must never be set by compiler
  244. #define PROTOCOL_FIXED_UP_1 (1<<30) // must never be set by compiler
  245. // Bits 0..15 are reserved for Swift's use.
  246. #define PROTOCOL_FIXED_UP_MASK (PROTOCOL_FIXED_UP_1 | PROTOCOL_FIXED_UP_2)
  247. struct protocol_t : objc_object {
  248. const char *mangledName;
  249. struct protocol_list_t *protocols;
  250. method_list_t *instanceMethods;
  251. method_list_t *classMethods;
  252. method_list_t *optionalInstanceMethods;
  253. method_list_t *optionalClassMethods;
  254. property_list_t *instanceProperties;
  255. uint32_t size; // sizeof(protocol_t)
  256. uint32_t flags;
  257. // Fields below this point are not always present on disk.
  258. const char **_extendedMethodTypes;
  259. const char *_demangledName;
  260. property_list_t *_classProperties;
  261. const char *demangledName();
  262. const char *nameForLogging() {
  263. return demangledName();
  264. }
  265. bool isFixedUp() const;
  266. void setFixedUp();
  267. # define HAS_FIELD(f) (size >= offsetof(protocol_t, f) + sizeof(f))
  268. bool hasExtendedMethodTypesField() const {
  269. return HAS_FIELD(_extendedMethodTypes);
  270. }
  271. bool hasDemangledNameField() const {
  272. return HAS_FIELD(_demangledName);
  273. }
  274. bool hasClassPropertiesField() const {
  275. return HAS_FIELD(_classProperties);
  276. }
  277. # undef HAS_FIELD
  278. const char **extendedMethodTypes() const {
  279. return hasExtendedMethodTypesField() ? _extendedMethodTypes : nil;
  280. }
  281. property_list_t *classProperties() const {
  282. return hasClassPropertiesField() ? _classProperties : nil;
  283. }
  284. };
  285. struct protocol_list_t {
  286. // count is 64-bit by accident.
  287. uintptr_t count;
  288. protocol_ref_t list[0]; // variable-size
  289. size_t byteSize() const {
  290. return sizeof(*this) + count*sizeof(list[0]);
  291. }
  292. protocol_list_t *duplicate() const {
  293. return (protocol_list_t *)memdup(this, this->byteSize());
  294. }
  295. typedef protocol_ref_t* iterator;
  296. typedef const protocol_ref_t* const_iterator;
  297. const_iterator begin() const {
  298. return list;
  299. }
  300. iterator begin() {
  301. return list;
  302. }
  303. const_iterator end() const {
  304. return list + count;
  305. }
  306. iterator end() {
  307. return list + count;
  308. }
  309. };
  310. struct locstamped_category_t {
  311. category_t *cat;
  312. struct header_info *hi;
  313. };
  314. struct locstamped_category_list_t {
  315. uint32_t count;
  316. #if __LP64__
  317. uint32_t reserved;
  318. #endif
  319. locstamped_category_t list[0];
  320. };
  321. // class_data_bits_t is the class_t->data field (class_rw_t pointer plus flags)
  322. // The extra bits are optimized for the retain/release and alloc/dealloc paths.
  323. // Values for class_ro_t->flags
  324. // These are emitted by the compiler and are part of the ABI.
  325. // Note: See CGObjCNonFragileABIMac::BuildClassRoTInitializer in clang
  326. // class is a metaclass
  327. #define RO_META (1<<0)
  328. // class is a root class
  329. #define RO_ROOT (1<<1)
  330. // class has .cxx_construct/destruct implementations
  331. #define RO_HAS_CXX_STRUCTORS (1<<2)
  332. // class has +load implementation
  333. // #define RO_HAS_LOAD_METHOD (1<<3)
  334. // class has visibility=hidden set
  335. #define RO_HIDDEN (1<<4)
  336. // class has attribute(objc_exception): OBJC_EHTYPE_$_ThisClass is non-weak
  337. #define RO_EXCEPTION (1<<5)
  338. // this bit is available for reassignment
  339. // #define RO_REUSE_ME (1<<6)
  340. // class compiled with ARC
  341. #define RO_IS_ARC (1<<7)
  342. // class has .cxx_destruct but no .cxx_construct (with RO_HAS_CXX_STRUCTORS)
  343. #define RO_HAS_CXX_DTOR_ONLY (1<<8)
  344. // class is not ARC but has ARC-style weak ivar layout
  345. #define RO_HAS_WEAK_WITHOUT_ARC (1<<9)
  346. // class is in an unloadable bundle - must never be set by compiler
  347. #define RO_FROM_BUNDLE (1<<29)
  348. // class is unrealized future class - must never be set by compiler
  349. #define RO_FUTURE (1<<30)
  350. // class is realized - must never be set by compiler
  351. #define RO_REALIZED (1<<31)
  352. // Values for class_rw_t->flags
  353. // These are not emitted by the compiler and are never used in class_ro_t.
  354. // Their presence should be considered in future ABI versions.
  355. // class_t->data is class_rw_t, not class_ro_t
  356. #define RW_REALIZED (1<<31)
  357. // class is unresolved future class
  358. #define RW_FUTURE (1<<30)
  359. // class is initialized
  360. #define RW_INITIALIZED (1<<29)
  361. // class is initializing
  362. #define RW_INITIALIZING (1<<28)
  363. // class_rw_t->ro is heap copy of class_ro_t
  364. #define RW_COPIED_RO (1<<27)
  365. // class allocated but not yet registered
  366. #define RW_CONSTRUCTING (1<<26)
  367. // class allocated and registered
  368. #define RW_CONSTRUCTED (1<<25)
  369. // available for use; was RW_FINALIZE_ON_MAIN_THREAD
  370. // #define RW_24 (1<<24)
  371. // class +load has been called
  372. #define RW_LOADED (1<<23)
  373. #if !SUPPORT_NONPOINTER_ISA
  374. // class instances may have associative references
  375. #define RW_INSTANCES_HAVE_ASSOCIATED_OBJECTS (1<<22)
  376. #endif
  377. // class has instance-specific GC layout
  378. #define RW_HAS_INSTANCE_SPECIFIC_LAYOUT (1 << 21)
  379. // available for use
  380. // #define RW_20 (1<<20)
  381. // class has started realizing but not yet completed it
  382. #define RW_REALIZING (1<<19)
  383. // NOTE: MORE RW_ FLAGS DEFINED BELOW
  384. // Values for class_rw_t->flags or class_t->bits
  385. // These flags are optimized for retain/release and alloc/dealloc
  386. // 64-bit stores more of them in class_t->bits to reduce pointer indirection.
  387. #if !__LP64__
  388. // class or superclass has .cxx_construct implementation
  389. #define RW_HAS_CXX_CTOR (1<<18)
  390. // class or superclass has .cxx_destruct implementation
  391. #define RW_HAS_CXX_DTOR (1<<17)
  392. // class or superclass has default alloc/allocWithZone: implementation
  393. // Note this is is stored in the metaclass.
  394. #define RW_HAS_DEFAULT_AWZ (1<<16)
  395. // class's instances requires raw isa
  396. #if SUPPORT_NONPOINTER_ISA
  397. #define RW_REQUIRES_RAW_ISA (1<<15)
  398. #endif
  399. // class or superclass has default retain/release/autorelease/retainCount/
  400. // _tryRetain/_isDeallocating/retainWeakReference/allowsWeakReference
  401. #define RW_HAS_DEFAULT_RR (1<<14)
  402. // class is a Swift class from the pre-stable Swift ABI
  403. #define FAST_IS_SWIFT_LEGACY (1UL<<0)
  404. // class is a Swift class from the stable Swift ABI
  405. #define FAST_IS_SWIFT_STABLE (1UL<<1)
  406. // data pointer
  407. #define FAST_DATA_MASK 0xfffffffcUL
  408. #elif 1
  409. // Leaks-compatible version that steals low bits only.
  410. // class or superclass has .cxx_construct implementation
  411. #define RW_HAS_CXX_CTOR (1<<18)
  412. // class or superclass has .cxx_destruct implementation
  413. #define RW_HAS_CXX_DTOR (1<<17)
  414. // class or superclass has default alloc/allocWithZone: implementation
  415. // Note this is is stored in the metaclass.
  416. #define RW_HAS_DEFAULT_AWZ (1<<16)
  417. // class's instances requires raw isa
  418. #define RW_REQUIRES_RAW_ISA (1<<15)
  419. // class is a Swift class from the pre-stable Swift ABI
  420. #define FAST_IS_SWIFT_LEGACY (1UL<<0)
  421. // class is a Swift class from the stable Swift ABI
  422. #define FAST_IS_SWIFT_STABLE (1UL<<1)
  423. // class or superclass has default retain/release/autorelease/retainCount/
  424. // _tryRetain/_isDeallocating/retainWeakReference/allowsWeakReference
  425. #define FAST_HAS_DEFAULT_RR (1UL<<2)
  426. // data pointer
  427. #define FAST_DATA_MASK 0x00007ffffffffff8UL
  428. #else
  429. // Leaks-incompatible version that steals lots of bits.
  430. // class is a Swift class from the pre-stable Swift ABI
  431. #define FAST_IS_SWIFT_LEGACY (1UL<<0)
  432. // class is a Swift class from the stable Swift ABI
  433. #define FAST_IS_SWIFT_STABLE (1UL<<1)
  434. // summary bit for fast alloc path: !hasCxxCtor and
  435. // !instancesRequireRawIsa and instanceSize fits into shiftedSize
  436. #define FAST_ALLOC (1UL<<2)
  437. // data pointer
  438. #define FAST_DATA_MASK 0x00007ffffffffff8UL
  439. // class or superclass has .cxx_construct implementation
  440. #define FAST_HAS_CXX_CTOR (1UL<<47)
  441. // class or superclass has default alloc/allocWithZone: implementation
  442. // Note this is is stored in the metaclass.
  443. #define FAST_HAS_DEFAULT_AWZ (1UL<<48)
  444. // class or superclass has default retain/release/autorelease/retainCount/
  445. // _tryRetain/_isDeallocating/retainWeakReference/allowsWeakReference
  446. #define FAST_HAS_DEFAULT_RR (1UL<<49)
  447. // class's instances requires raw isa
  448. // This bit is aligned with isa_t->hasCxxDtor to save an instruction.
  449. #define FAST_REQUIRES_RAW_ISA (1UL<<50)
  450. // class or superclass has .cxx_destruct implementation
  451. #define FAST_HAS_CXX_DTOR (1UL<<51)
  452. // instance size in units of 16 bytes
  453. // or 0 if the instance size is too big in this field
  454. // This field must be LAST
  455. #define FAST_SHIFTED_SIZE_SHIFT 52
  456. // FAST_ALLOC means
  457. // FAST_HAS_CXX_CTOR is set
  458. // FAST_REQUIRES_RAW_ISA is not set
  459. // FAST_SHIFTED_SIZE is not zero
  460. // FAST_ALLOC does NOT check FAST_HAS_DEFAULT_AWZ because that
  461. // bit is stored on the metaclass.
  462. #define FAST_ALLOC_MASK (FAST_HAS_CXX_CTOR | FAST_REQUIRES_RAW_ISA)
  463. #define FAST_ALLOC_VALUE (0)
  464. #endif
  465. // The Swift ABI requires that these bits be defined like this on all platforms.
  466. static_assert(FAST_IS_SWIFT_LEGACY == 1, "resistance is futile");
  467. static_assert(FAST_IS_SWIFT_STABLE == 2, "resistance is futile");
  468. struct class_ro_t {
  469. uint32_t flags;
  470. uint32_t instanceStart;
  471. uint32_t instanceSize;
  472. #ifdef __LP64__
  473. uint32_t reserved;
  474. #endif
  475. const uint8_t * ivarLayout;
  476. const char * name;
  477. method_list_t * baseMethodList;
  478. protocol_list_t * baseProtocols;
  479. const ivar_list_t * ivars;
  480. const uint8_t * weakIvarLayout;
  481. property_list_t *baseProperties;
  482. method_list_t *baseMethods() const {
  483. return baseMethodList;
  484. }
  485. };
  486. /***********************************************************************
  487. * list_array_tt<Element, List>
  488. * Generic implementation for metadata that can be augmented by categories.
  489. *
  490. * Element is the underlying metadata type (e.g. method_t)
  491. * List is the metadata's list type (e.g. method_list_t)
  492. *
  493. * A list_array_tt has one of three values:
  494. * - empty
  495. * - a pointer to a single list
  496. * - an array of pointers to lists
  497. *
  498. * countLists/beginLists/endLists iterate the metadata lists
  499. * count/begin/end iterate the underlying metadata elements
  500. **********************************************************************/
  501. template <typename Element, typename List>
  502. class list_array_tt {
  503. struct array_t {
  504. uint32_t count;
  505. List* lists[0];
  506. static size_t byteSize(uint32_t count) {
  507. return sizeof(array_t) + count*sizeof(lists[0]);
  508. }
  509. size_t byteSize() {
  510. return byteSize(count);
  511. }
  512. };
  513. protected:
  514. class iterator {
  515. List **lists;
  516. List **listsEnd;
  517. typename List::iterator m, mEnd;
  518. public:
  519. iterator(List **begin, List **end)
  520. : lists(begin), listsEnd(end)
  521. {
  522. if (begin != end) {
  523. m = (*begin)->begin();
  524. mEnd = (*begin)->end();
  525. }
  526. }
  527. const Element& operator * () const {
  528. return *m;
  529. }
  530. Element& operator * () {
  531. return *m;
  532. }
  533. bool operator != (const iterator& rhs) const {
  534. if (lists != rhs.lists) return true;
  535. if (lists == listsEnd) return false; // m is undefined
  536. if (m != rhs.m) return true;
  537. return false;
  538. }
  539. const iterator& operator ++ () {
  540. assert(m != mEnd);
  541. m++;
  542. if (m == mEnd) {
  543. assert(lists != listsEnd);
  544. lists++;
  545. if (lists != listsEnd) {
  546. m = (*lists)->begin();
  547. mEnd = (*lists)->end();
  548. }
  549. }
  550. return *this;
  551. }
  552. };
  553. private:
  554. union {
  555. List* list;
  556. uintptr_t arrayAndFlag;
  557. };
  558. bool hasArray() const {
  559. return arrayAndFlag & 1;
  560. }
  561. array_t *array() {
  562. return (array_t *)(arrayAndFlag & ~1);
  563. }
  564. void setArray(array_t *array) {
  565. arrayAndFlag = (uintptr_t)array | 1;
  566. }
  567. public:
  568. uint32_t count() {
  569. uint32_t result = 0;
  570. for (auto lists = beginLists(), end = endLists();
  571. lists != end;
  572. ++lists)
  573. {
  574. result += (*lists)->count;
  575. }
  576. return result;
  577. }
  578. iterator begin() {
  579. return iterator(beginLists(), endLists());
  580. }
  581. iterator end() {
  582. List **e = endLists();
  583. return iterator(e, e);
  584. }
  585. uint32_t countLists() {
  586. if (hasArray()) {
  587. return array()->count;
  588. } else if (list) {
  589. return 1;
  590. } else {
  591. return 0;
  592. }
  593. }
  594. List** beginLists() {
  595. if (hasArray()) {
  596. return array()->lists;
  597. } else {
  598. return &list;
  599. }
  600. }
  601. List** endLists() {
  602. if (hasArray()) {
  603. return array()->lists + array()->count;
  604. } else if (list) {
  605. return &list + 1;
  606. } else {
  607. return &list;
  608. }
  609. }
  610. void attachLists(List* const * addedLists, uint32_t addedCount) {
  611. if (addedCount == 0) return;
  612. if (hasArray()) {
  613. // many lists -> many lists
  614. uint32_t oldCount = array()->count;
  615. uint32_t newCount = oldCount + addedCount;
  616. setArray((array_t *)realloc(array(), array_t::byteSize(newCount)));
  617. array()->count = newCount;
  618. memmove(array()->lists + addedCount, array()->lists,
  619. oldCount * sizeof(array()->lists[0]));
  620. memcpy(array()->lists, addedLists,
  621. addedCount * sizeof(array()->lists[0]));
  622. }
  623. else if (!list && addedCount == 1) {
  624. // 0 lists -> 1 list
  625. list = addedLists[0];
  626. }
  627. else {
  628. // 1 list -> many lists
  629. List* oldList = list;
  630. uint32_t oldCount = oldList ? 1 : 0;
  631. uint32_t newCount = oldCount + addedCount;
  632. setArray((array_t *)malloc(array_t::byteSize(newCount)));
  633. array()->count = newCount;
  634. if (oldList) array()->lists[addedCount] = oldList;
  635. memcpy(array()->lists, addedLists,
  636. addedCount * sizeof(array()->lists[0]));
  637. }
  638. }
  639. void tryFree() {
  640. if (hasArray()) {
  641. for (uint32_t i = 0; i < array()->count; i++) {
  642. try_free(array()->lists[i]);
  643. }
  644. try_free(array());
  645. }
  646. else if (list) {
  647. try_free(list);
  648. }
  649. }
  650. template<typename Result>
  651. Result duplicate() {
  652. Result result;
  653. if (hasArray()) {
  654. array_t *a = array();
  655. result.setArray((array_t *)memdup(a, a->byteSize()));
  656. for (uint32_t i = 0; i < a->count; i++) {
  657. result.array()->lists[i] = a->lists[i]->duplicate();
  658. }
  659. } else if (list) {
  660. result.list = list->duplicate();
  661. } else {
  662. result.list = nil;
  663. }
  664. return result;
  665. }
  666. };
  667. class method_array_t :
  668. public list_array_tt<method_t, method_list_t>
  669. {
  670. typedef list_array_tt<method_t, method_list_t> Super;
  671. public:
  672. method_list_t **beginCategoryMethodLists() {
  673. return beginLists();
  674. }
  675. method_list_t **endCategoryMethodLists(Class cls);
  676. method_array_t duplicate() {
  677. return Super::duplicate<method_array_t>();
  678. }
  679. };
  680. class property_array_t :
  681. public list_array_tt<property_t, property_list_t>
  682. {
  683. typedef list_array_tt<property_t, property_list_t> Super;
  684. public:
  685. property_array_t duplicate() {
  686. return Super::duplicate<property_array_t>();
  687. }
  688. };
  689. class protocol_array_t :
  690. public list_array_tt<protocol_ref_t, protocol_list_t>
  691. {
  692. typedef list_array_tt<protocol_ref_t, protocol_list_t> Super;
  693. public:
  694. protocol_array_t duplicate() {
  695. return Super::duplicate<protocol_array_t>();
  696. }
  697. };
  698. struct class_rw_t {
  699. // Be warned that Symbolication knows the layout of this structure.
  700. uint32_t flags;
  701. uint32_t version;
  702. const class_ro_t *ro;
  703. method_array_t methods;
  704. property_array_t properties;
  705. protocol_array_t protocols;
  706. Class firstSubclass;
  707. Class nextSiblingClass;
  708. char *demangledName;
  709. #if SUPPORT_INDEXED_ISA
  710. uint32_t index;
  711. #endif
  712. void setFlags(uint32_t set)
  713. {
  714. OSAtomicOr32Barrier(set, &flags);
  715. }
  716. void clearFlags(uint32_t clear)
  717. {
  718. OSAtomicXor32Barrier(clear, &flags);
  719. }
  720. // set and clear must not overlap
  721. void changeFlags(uint32_t set, uint32_t clear)
  722. {
  723. assert((set & clear) == 0);
  724. uint32_t oldf, newf;
  725. do {
  726. oldf = flags;
  727. newf = (oldf | set) & ~clear;
  728. } while (!OSAtomicCompareAndSwap32Barrier(oldf, newf, (volatile int32_t *)&flags));
  729. }
  730. };
  731. struct class_data_bits_t {
  732. // Values are the FAST_ flags above.
  733. uintptr_t bits;
  734. private:
  735. bool getBit(uintptr_t bit)
  736. {
  737. return bits & bit;
  738. }
  739. #if FAST_ALLOC
  740. static uintptr_t updateFastAlloc(uintptr_t oldBits, uintptr_t change)
  741. {
  742. if (change & FAST_ALLOC_MASK) {
  743. if (((oldBits & FAST_ALLOC_MASK) == FAST_ALLOC_VALUE) &&
  744. ((oldBits >> FAST_SHIFTED_SIZE_SHIFT) != 0))
  745. {
  746. oldBits |= FAST_ALLOC;
  747. } else {
  748. oldBits &= ~FAST_ALLOC;
  749. }
  750. }
  751. return oldBits;
  752. }
  753. #else
  754. static uintptr_t updateFastAlloc(uintptr_t oldBits, uintptr_t change) {
  755. return oldBits;
  756. }
  757. #endif
  758. void setBits(uintptr_t set)
  759. {
  760. uintptr_t oldBits;
  761. uintptr_t newBits;
  762. do {
  763. oldBits = LoadExclusive(&bits);
  764. newBits = updateFastAlloc(oldBits | set, set);
  765. } while (!StoreReleaseExclusive(&bits, oldBits, newBits));
  766. }
  767. void clearBits(uintptr_t clear)
  768. {
  769. uintptr_t oldBits;
  770. uintptr_t newBits;
  771. do {
  772. oldBits = LoadExclusive(&bits);
  773. newBits = updateFastAlloc(oldBits & ~clear, clear);
  774. } while (!StoreReleaseExclusive(&bits, oldBits, newBits));
  775. }
  776. public:
  777. class_rw_t* data() {
  778. return (class_rw_t *)(bits & FAST_DATA_MASK);
  779. }
  780. void setData(class_rw_t *newData)
  781. {
  782. assert(!data() || (newData->flags & (RW_REALIZING | RW_FUTURE)));
  783. // Set during realization or construction only. No locking needed.
  784. // Use a store-release fence because there may be concurrent
  785. // readers of data and data's contents.
  786. uintptr_t newBits = (bits & ~FAST_DATA_MASK) | (uintptr_t)newData;
  787. atomic_thread_fence(memory_order_release);
  788. bits = newBits;
  789. }
  790. #if FAST_HAS_DEFAULT_RR
  791. bool hasDefaultRR() {
  792. return getBit(FAST_HAS_DEFAULT_RR);
  793. }
  794. void setHasDefaultRR() {
  795. setBits(FAST_HAS_DEFAULT_RR);
  796. }
  797. void setHasCustomRR() {
  798. clearBits(FAST_HAS_DEFAULT_RR);
  799. }
  800. #else
  801. bool hasDefaultRR() {
  802. return data()->flags & RW_HAS_DEFAULT_RR;
  803. }
  804. void setHasDefaultRR() {
  805. data()->setFlags(RW_HAS_DEFAULT_RR);
  806. }
  807. void setHasCustomRR() {
  808. data()->clearFlags(RW_HAS_DEFAULT_RR);
  809. }
  810. #endif
  811. #if FAST_HAS_DEFAULT_AWZ
  812. bool hasDefaultAWZ() {
  813. return getBit(FAST_HAS_DEFAULT_AWZ);
  814. }
  815. void setHasDefaultAWZ() {
  816. setBits(FAST_HAS_DEFAULT_AWZ);
  817. }
  818. void setHasCustomAWZ() {
  819. clearBits(FAST_HAS_DEFAULT_AWZ);
  820. }
  821. #else
  822. bool hasDefaultAWZ() {
  823. return data()->flags & RW_HAS_DEFAULT_AWZ;
  824. }
  825. void setHasDefaultAWZ() {
  826. data()->setFlags(RW_HAS_DEFAULT_AWZ);
  827. }
  828. void setHasCustomAWZ() {
  829. data()->clearFlags(RW_HAS_DEFAULT_AWZ);
  830. }
  831. #endif
  832. #if FAST_HAS_CXX_CTOR
  833. bool hasCxxCtor() {
  834. return getBit(FAST_HAS_CXX_CTOR);
  835. }
  836. void setHasCxxCtor() {
  837. setBits(FAST_HAS_CXX_CTOR);
  838. }
  839. #else
  840. bool hasCxxCtor() {
  841. return data()->flags & RW_HAS_CXX_CTOR;
  842. }
  843. void setHasCxxCtor() {
  844. data()->setFlags(RW_HAS_CXX_CTOR);
  845. }
  846. #endif
  847. #if FAST_HAS_CXX_DTOR
  848. bool hasCxxDtor() {
  849. return getBit(FAST_HAS_CXX_DTOR);
  850. }
  851. void setHasCxxDtor() {
  852. setBits(FAST_HAS_CXX_DTOR);
  853. }
  854. #else
  855. bool hasCxxDtor() {
  856. return data()->flags & RW_HAS_CXX_DTOR;
  857. }
  858. void setHasCxxDtor() {
  859. data()->setFlags(RW_HAS_CXX_DTOR);
  860. }
  861. #endif
  862. #if FAST_REQUIRES_RAW_ISA
  863. bool instancesRequireRawIsa() {
  864. return getBit(FAST_REQUIRES_RAW_ISA);
  865. }
  866. void setInstancesRequireRawIsa() {
  867. setBits(FAST_REQUIRES_RAW_ISA);
  868. }
  869. #elif SUPPORT_NONPOINTER_ISA
  870. bool instancesRequireRawIsa() {
  871. return data()->flags & RW_REQUIRES_RAW_ISA;
  872. }
  873. void setInstancesRequireRawIsa() {
  874. data()->setFlags(RW_REQUIRES_RAW_ISA);
  875. }
  876. #else
  877. bool instancesRequireRawIsa() {
  878. return true;
  879. }
  880. void setInstancesRequireRawIsa() {
  881. // nothing
  882. }
  883. #endif
  884. #if FAST_ALLOC
  885. size_t fastInstanceSize()
  886. {
  887. assert(bits & FAST_ALLOC);
  888. return (bits >> FAST_SHIFTED_SIZE_SHIFT) * 16;
  889. }
  890. void setFastInstanceSize(size_t newSize)
  891. {
  892. // Set during realization or construction only. No locking needed.
  893. assert(data()->flags & RW_REALIZING);
  894. // Round up to 16-byte boundary, then divide to get 16-byte units
  895. newSize = ((newSize + 15) & ~15) / 16;
  896. uintptr_t newBits = newSize << FAST_SHIFTED_SIZE_SHIFT;
  897. if ((newBits >> FAST_SHIFTED_SIZE_SHIFT) == newSize) {
  898. int shift = WORD_BITS - FAST_SHIFTED_SIZE_SHIFT;
  899. uintptr_t oldBits = (bits << shift) >> shift;
  900. if ((oldBits & FAST_ALLOC_MASK) == FAST_ALLOC_VALUE) {
  901. newBits |= FAST_ALLOC;
  902. }
  903. bits = oldBits | newBits;
  904. }
  905. }
  906. bool canAllocFast() {
  907. return bits & FAST_ALLOC;
  908. }
  909. #else
  910. size_t fastInstanceSize() {
  911. abort();
  912. }
  913. void setFastInstanceSize(size_t) {
  914. // nothing
  915. }
  916. bool canAllocFast() {
  917. return false;
  918. }
  919. #endif
  920. void setClassArrayIndex(unsigned Idx) {
  921. #if SUPPORT_INDEXED_ISA
  922. // 0 is unused as then we can rely on zero-initialisation from calloc.
  923. assert(Idx > 0);
  924. data()->index = Idx;
  925. #endif
  926. }
  927. unsigned classArrayIndex() {
  928. #if SUPPORT_INDEXED_ISA
  929. return data()->index;
  930. #else
  931. return 0;
  932. #endif
  933. }
  934. bool isAnySwift() {
  935. return isSwiftStable() || isSwiftLegacy();
  936. }
  937. bool isSwiftStable() {
  938. return getBit(FAST_IS_SWIFT_STABLE);
  939. }
  940. void setIsSwiftStable() {
  941. setBits(FAST_IS_SWIFT_STABLE);
  942. }
  943. bool isSwiftLegacy() {
  944. return getBit(FAST_IS_SWIFT_LEGACY);
  945. }
  946. void setIsSwiftLegacy() {
  947. setBits(FAST_IS_SWIFT_LEGACY);
  948. }
  949. };
  950. struct objc_class : objc_object {
  951. // Class ISA;
  952. Class superclass;
  953. cache_t cache; // formerly cache pointer and vtable
  954. class_data_bits_t bits; // class_rw_t * plus custom rr/alloc flags
  955. class_rw_t *data() {
  956. return bits.data();
  957. }
  958. void setData(class_rw_t *newData) {
  959. bits.setData(newData);
  960. }
  961. void setInfo(uint32_t set) {
  962. assert(isFuture() || isRealized());
  963. data()->setFlags(set);
  964. }
  965. void clearInfo(uint32_t clear) {
  966. assert(isFuture() || isRealized());
  967. data()->clearFlags(clear);
  968. }
  969. // set and clear must not overlap
  970. void changeInfo(uint32_t set, uint32_t clear) {
  971. assert(isFuture() || isRealized());
  972. assert((set & clear) == 0);
  973. data()->changeFlags(set, clear);
  974. }
  975. bool hasCustomRR() {
  976. return ! bits.hasDefaultRR();
  977. }
  978. void setHasDefaultRR() {
  979. assert(isInitializing());
  980. bits.setHasDefaultRR();
  981. }
  982. void setHasCustomRR(bool inherited = false);
  983. void printCustomRR(bool inherited);
  984. bool hasCustomAWZ() {
  985. return ! bits.hasDefaultAWZ();
  986. }
  987. void setHasDefaultAWZ() {
  988. assert(isInitializing());
  989. bits.setHasDefaultAWZ();
  990. }
  991. void setHasCustomAWZ(bool inherited = false);
  992. void printCustomAWZ(bool inherited);
  993. bool instancesRequireRawIsa() {
  994. return bits.instancesRequireRawIsa();
  995. }
  996. void setInstancesRequireRawIsa(bool inherited = false);
  997. void printInstancesRequireRawIsa(bool inherited);
  998. bool canAllocNonpointer() {
  999. assert(!isFuture());
  1000. return !instancesRequireRawIsa();
  1001. }
  1002. bool canAllocFast() {
  1003. assert(!isFuture());
  1004. return bits.canAllocFast();
  1005. }
  1006. bool hasCxxCtor() {
  1007. // addSubclass() propagates this flag from the superclass.
  1008. assert(isRealized());
  1009. return bits.hasCxxCtor();
  1010. }
  1011. void setHasCxxCtor() {
  1012. bits.setHasCxxCtor();
  1013. }
  1014. bool hasCxxDtor() {
  1015. // addSubclass() propagates this flag from the superclass.
  1016. assert(isRealized());
  1017. return bits.hasCxxDtor();
  1018. }
  1019. void setHasCxxDtor() {
  1020. bits.setHasCxxDtor();
  1021. }
  1022. bool isSwiftStable() {
  1023. return bits.isSwiftStable();
  1024. }
  1025. bool isSwiftLegacy() {
  1026. return bits.isSwiftLegacy();
  1027. }
  1028. bool isAnySwift() {
  1029. return bits.isAnySwift();
  1030. }
  1031. // Return YES if the class's ivars are managed by ARC,
  1032. // or the class is MRC but has ARC-style weak ivars.
  1033. bool hasAutomaticIvars() {
  1034. return data()->ro->flags & (RO_IS_ARC | RO_HAS_WEAK_WITHOUT_ARC);
  1035. }
  1036. // Return YES if the class's ivars are managed by ARC.
  1037. bool isARC() {
  1038. return data()->ro->flags & RO_IS_ARC;
  1039. }
  1040. #if SUPPORT_NONPOINTER_ISA
  1041. // Tracked in non-pointer isas; not tracked otherwise
  1042. #else
  1043. bool instancesHaveAssociatedObjects() {
  1044. // this may be an unrealized future class in the CF-bridged case
  1045. assert(isFuture() || isRealized());
  1046. return data()->flags & RW_INSTANCES_HAVE_ASSOCIATED_OBJECTS;
  1047. }
  1048. void setInstancesHaveAssociatedObjects() {
  1049. // this may be an unrealized future class in the CF-bridged case
  1050. assert(isFuture() || isRealized());
  1051. setInfo(RW_INSTANCES_HAVE_ASSOCIATED_OBJECTS);
  1052. }
  1053. #endif
  1054. bool shouldGrowCache() {
  1055. return true;
  1056. }
  1057. void setShouldGrowCache(bool) {
  1058. // fixme good or bad for memory use?
  1059. }
  1060. bool isInitializing() {
  1061. return getMeta()->data()->flags & RW_INITIALIZING;
  1062. }
  1063. void setInitializing() {
  1064. assert(!isMetaClass());
  1065. ISA()->setInfo(RW_INITIALIZING);
  1066. }
  1067. bool isInitialized() {
  1068. return getMeta()->data()->flags & RW_INITIALIZED;
  1069. }
  1070. void setInitialized();
  1071. bool isLoadable() {
  1072. assert(isRealized());
  1073. return true; // any class registered for +load is definitely loadable
  1074. }
  1075. IMP getLoadMethod();
  1076. // Locking: To prevent concurrent realization, hold runtimeLock.
  1077. bool isRealized() {
  1078. return data()->flags & RW_REALIZED;
  1079. }
  1080. // Returns true if this is an unrealized future class.
  1081. // Locking: To prevent concurrent realization, hold runtimeLock.
  1082. bool isFuture() {
  1083. return data()->flags & RW_FUTURE;
  1084. }
  1085. bool isMetaClass() {
  1086. assert(this);
  1087. assert(isRealized());
  1088. return data()->ro->flags & RO_META;
  1089. }
  1090. // NOT identical to this->ISA when this is a metaclass
  1091. Class getMeta() {
  1092. if (isMetaClass()) return (Class)this;
  1093. else return this->ISA();
  1094. }
  1095. bool isRootClass() {
  1096. return superclass == nil;
  1097. }
  1098. bool isRootMetaclass() {
  1099. return ISA() == (Class)this;
  1100. }
  1101. const char *mangledName() {
  1102. // fixme can't assert locks here
  1103. assert(this);
  1104. if (isRealized() || isFuture()) {
  1105. return data()->ro->name;
  1106. } else {
  1107. return ((const class_ro_t *)data())->name;
  1108. }
  1109. }
  1110. const char *demangledName(bool realize = false);
  1111. const char *nameForLogging();
  1112. // May be unaligned depending on class's ivars.
  1113. uint32_t unalignedInstanceStart() {
  1114. assert(isRealized());
  1115. return data()->ro->instanceStart;
  1116. }
  1117. // Class's instance start rounded up to a pointer-size boundary.
  1118. // This is used for ARC layout bitmaps.
  1119. uint32_t alignedInstanceStart() {
  1120. return word_align(unalignedInstanceStart());
  1121. }
  1122. // May be unaligned depending on class's ivars.
  1123. uint32_t unalignedInstanceSize() {
  1124. assert(isRealized());
  1125. return data()->ro->instanceSize;
  1126. }
  1127. // Class's ivar size rounded up to a pointer-size boundary.
  1128. uint32_t alignedInstanceSize() {
  1129. return word_align(unalignedInstanceSize());
  1130. }
  1131. size_t instanceSize(size_t extraBytes) {
  1132. size_t size = alignedInstanceSize() + extraBytes;
  1133. // CF requires all objects be at least 16 bytes.
  1134. if (size < 16) size = 16;
  1135. return size;
  1136. }
  1137. void setInstanceSize(uint32_t newSize) {
  1138. assert(isRealized());
  1139. if (newSize != data()->ro->instanceSize) {
  1140. assert(data()->flags & RW_COPIED_RO);
  1141. *const_cast<uint32_t *>(&data()->ro->instanceSize) = newSize;
  1142. }
  1143. bits.setFastInstanceSize(newSize);
  1144. }
  1145. void chooseClassArrayIndex();
  1146. void setClassArrayIndex(unsigned Idx) {
  1147. bits.setClassArrayIndex(Idx);
  1148. }
  1149. unsigned classArrayIndex() {
  1150. return bits.classArrayIndex();
  1151. }
  1152. };
  1153. struct swift_class_t : objc_class {
  1154. uint32_t flags;
  1155. uint32_t instanceAddressOffset;
  1156. uint32_t instanceSize;
  1157. uint16_t instanceAlignMask;
  1158. uint16_t reserved;
  1159. uint32_t classSize;
  1160. uint32_t classAddressOffset;
  1161. void *description;
  1162. // ...
  1163. void *baseAddress() {
  1164. return (void *)((uint8_t *)this - classAddressOffset);
  1165. }
  1166. };
  1167. struct category_t {
  1168. const char *name;
  1169. classref_t cls;
  1170. struct method_list_t *instanceMethods;
  1171. struct method_list_t *classMethods;
  1172. struct protocol_list_t *protocols;
  1173. struct property_list_t *instanceProperties;
  1174. // Fields below this point are not always present on disk.
  1175. struct property_list_t *_classProperties;
  1176. method_list_t *methodsForMeta(bool isMeta) {
  1177. if (isMeta) return classMethods;
  1178. else return instanceMethods;
  1179. }
  1180. property_list_t *propertiesForMeta(bool isMeta, struct header_info *hi);
  1181. };
  1182. struct objc_super2 {
  1183. id receiver;
  1184. Class current_class;
  1185. };
  1186. struct message_ref_t {
  1187. IMP imp;
  1188. SEL sel;
  1189. };
  1190. extern Method protocol_getMethod(protocol_t *p, SEL sel, bool isRequiredMethod, bool isInstanceMethod, bool recursive);
  1191. static inline void
  1192. foreach_realized_class_and_subclass_2(Class top, unsigned& count,
  1193. std::function<bool (Class)> code)
  1194. {
  1195. // runtimeLock.assertLocked();
  1196. assert(top);
  1197. Class cls = top;
  1198. while (1) {
  1199. if (--count == 0) {
  1200. _objc_fatal("Memory corruption in class list.");
  1201. }
  1202. if (!code(cls)) break;
  1203. if (cls->data()->firstSubclass) {
  1204. cls = cls->data()->firstSubclass;
  1205. } else {
  1206. while (!cls->data()->nextSiblingClass && cls != top) {
  1207. cls = cls->superclass;
  1208. if (--count == 0) {
  1209. _objc_fatal("Memory corruption in class list.");
  1210. }
  1211. }
  1212. if (cls == top) break;
  1213. cls = cls->data()->nextSiblingClass;
  1214. }
  1215. }
  1216. }
  1217. extern Class firstRealizedClass();
  1218. extern unsigned int unreasonableClassCount();
  1219. // Enumerates a class and all of its realized subclasses.
  1220. static inline void
  1221. foreach_realized_class_and_subclass(Class top,
  1222. std::function<void (Class)> code)
  1223. {
  1224. unsigned int count = unreasonableClassCount();
  1225. foreach_realized_class_and_subclass_2(top, count,
  1226. [&code](Class cls) -> bool
  1227. {
  1228. code(cls);
  1229. return true;
  1230. });
  1231. }
  1232. // Enumerates all realized classes and metaclasses.
  1233. static inline void
  1234. foreach_realized_class_and_metaclass(std::function<void (Class)> code)
  1235. {
  1236. unsigned int count = unreasonableClassCount();
  1237. for (Class top = firstRealizedClass();
  1238. top != nil;
  1239. top = top->data()->nextSiblingClass)
  1240. {
  1241. foreach_realized_class_and_subclass_2(top, count,
  1242. [&code](Class cls) -> bool
  1243. {
  1244. code(cls);
  1245. return true;
  1246. });
  1247. }
  1248. }
  1249. #endif