2 * Copyright (c) 1999-2007 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 /***********************************************************************
27 **********************************************************************/
29 /***********************************************************************
30 * Thread-safety during class initialization (GrP 2001-9-24)
32 * Initial state: CLS_INITIALIZING and CLS_INITIALIZED both clear.
33 * During initialization: CLS_INITIALIZING is set
34 * After initialization: CLS_INITIALIZING clear and CLS_INITIALIZED set.
35 * CLS_INITIALIZING and CLS_INITIALIZED are never set at the same time.
36 * CLS_INITIALIZED is never cleared once set.
38 * Only one thread is allowed to actually initialize a class and send
39 * +initialize. Enforced by allowing only one thread to set CLS_INITIALIZING.
41 * Additionally, threads trying to send messages to a class must wait for
42 * +initialize to finish. During initialization of a class, that class's
43 * method cache is kept empty. objc_msgSend will revert to
44 * class_lookupMethodAndLoadCache, which checks CLS_INITIALIZED before
45 * messaging. If CLS_INITIALIZED is clear but CLS_INITIALIZING is set,
46 * the thread must block, unless it is the thread that started
47 * initializing the class in the first place.
49 * Each thread keeps a list of classes it's initializing.
50 * The global classInitLock is used to synchronize changes to CLS_INITIALIZED
51 * and CLS_INITIALIZING: the transition to CLS_INITIALIZING must be
52 * an atomic test-and-set with respect to itself and the transition
54 * The global classInitWaitCond is used to block threads waiting for an
55 * initialization to complete. The classInitLock synchronizes
56 * condition checking and the condition variable.
57 **********************************************************************/
59 /***********************************************************************
60 * +initialize deadlock case when a class is marked initializing while
61 * its superclass is initialized. Solved by completely initializing
62 * superclasses before beginning to initialize a class.
64 * OmniWeb class hierarchy:
69 * OWAddressEntry OWController
73 * Thread 1 (evil testing thread):
74 * initialize OWAddressEntry
77 * [OBObject initialize] runs OBPostLoader, which inits lots of classes...
78 * initialize OWConsoleController
79 * super init OWController - wait for Thread 2 to finish OWController init
81 * Thread 2 (normal OmniWeb thread):
82 * initialize OWController
83 * super init OFObject - wait for Thread 1 to finish OFObject init
87 * Solution: fully initialize super classes before beginning to initialize
88 * a subclass. Then the initializing+initialized part of the class hierarchy
89 * will be a contiguous subtree starting at the root, so other threads
90 * can't jump into the middle between two initializing classes, and we won't
91 * get stuck while a superclass waits for its subclass which waits for the
93 **********************************************************************/
95 #include "objc-private.h"
97 #include "objc-initialize.h"
98 #include "DenseMapExtras.h"
100 /* classInitLock protects CLS_INITIALIZED and CLS_INITIALIZING, and
101 * is signalled when any class is done initializing.
102 * Threads that are waiting for a class to finish initializing wait on this. */
103 monitor_t classInitLock;
106 struct _objc_willInitializeClassCallback {
107 _objc_func_willInitializeClass f;
110 static GlobalSmallVector<_objc_willInitializeClassCallback, 1> willInitializeFuncs;
113 /***********************************************************************
114 * struct _objc_initializing_classes
115 * Per-thread list of classes currently being initialized by that thread.
116 * During initialization, that thread is allowed to send messages to that
117 * class, but other threads have to wait.
118 * The list is a simple array of metaclasses (the metaclass stores
119 * the initialization state).
120 **********************************************************************/
121 typedef struct _objc_initializing_classes {
122 int classesAllocated;
124 } _objc_initializing_classes;
127 /***********************************************************************
128 * _fetchInitializingClassList
129 * Return the list of classes being initialized by this thread.
130 * If create == YES, create the list when no classes are being initialized by this thread.
131 * If create == NO, return nil when no classes are being initialized by this thread.
132 **********************************************************************/
133 static _objc_initializing_classes *_fetchInitializingClassList(bool create)
135 _objc_pthread_data *data;
136 _objc_initializing_classes *list;
139 data = _objc_fetch_pthread_data(create);
140 if (data == nil) return nil;
142 list = data->initializingClasses;
147 list = (_objc_initializing_classes *)
148 calloc(1, sizeof(_objc_initializing_classes));
149 data->initializingClasses = list;
153 classes = list->metaclasses;
154 if (classes == nil) {
155 // If _objc_initializing_classes exists, allocate metaclass array,
156 // even if create == NO.
157 // Allow 4 simultaneous class inits on this thread before realloc.
158 list->classesAllocated = 4;
160 calloc(list->classesAllocated, sizeof(Class));
161 list->metaclasses = classes;
167 /***********************************************************************
168 * _destroyInitializingClassList
169 * Deallocate memory used by the given initialization list.
170 * Any part of the list may be nil.
171 * Called from _objc_pthread_destroyspecific().
172 **********************************************************************/
174 void _destroyInitializingClassList(struct _objc_initializing_classes *list)
177 if (list->metaclasses != nil) {
178 free(list->metaclasses);
185 /***********************************************************************
186 * _thisThreadIsInitializingClass
187 * Return TRUE if this thread is currently initializing the given class.
188 **********************************************************************/
189 bool _thisThreadIsInitializingClass(Class cls)
193 _objc_initializing_classes *list = _fetchInitializingClassList(NO);
195 cls = cls->getMeta();
196 for (i = 0; i < list->classesAllocated; i++) {
197 if (cls == list->metaclasses[i]) return YES;
201 // no list or not found in list
206 /***********************************************************************
207 * _setThisThreadIsInitializingClass
208 * Record that this thread is currently initializing the given class.
209 * This thread will be allowed to send messages to the class, but
210 * other threads will have to wait.
211 **********************************************************************/
212 static void _setThisThreadIsInitializingClass(Class cls)
215 _objc_initializing_classes *list = _fetchInitializingClassList(YES);
216 cls = cls->getMeta();
218 // paranoia: explicitly disallow duplicates
219 for (i = 0; i < list->classesAllocated; i++) {
220 if (cls == list->metaclasses[i]) {
221 _objc_fatal("thread is already initializing this class!");
222 return; // already the initializer
226 for (i = 0; i < list->classesAllocated; i++) {
227 if (! list->metaclasses[i]) {
228 list->metaclasses[i] = cls;
233 // class list is full - reallocate
234 list->classesAllocated = list->classesAllocated * 2 + 1;
235 list->metaclasses = (Class *)
236 realloc(list->metaclasses,
237 list->classesAllocated * sizeof(Class));
238 // zero out the new entries
239 list->metaclasses[i++] = cls;
240 for ( ; i < list->classesAllocated; i++) {
241 list->metaclasses[i] = nil;
246 /***********************************************************************
247 * _setThisThreadIsNotInitializingClass
248 * Record that this thread is no longer initializing the given class.
249 **********************************************************************/
250 static void _setThisThreadIsNotInitializingClass(Class cls)
254 _objc_initializing_classes *list = _fetchInitializingClassList(NO);
256 cls = cls->getMeta();
257 for (i = 0; i < list->classesAllocated; i++) {
258 if (cls == list->metaclasses[i]) {
259 list->metaclasses[i] = nil;
265 // no list or not found in list
266 _objc_fatal("thread is not initializing this class!");
270 typedef struct PendingInitialize {
272 struct PendingInitialize *next;
274 PendingInitialize(Class cls) : subclass(cls), next(nullptr) { }
277 typedef objc::DenseMap<Class, PendingInitialize *> PendingInitializeMap;
278 static PendingInitializeMap *pendingInitializeMap;
280 /***********************************************************************
281 * _finishInitializing
282 * cls has completed its +initialize method, and so has its superclass.
283 * Mark cls as initialized as well, then mark any of cls's subclasses
284 * that have already finished their own +initialize methods.
285 **********************************************************************/
286 static void _finishInitializing(Class cls, Class supercls)
288 PendingInitialize *pending;
290 classInitLock.assertLocked();
291 ASSERT(!supercls || supercls->isInitialized());
293 if (PrintInitializing) {
294 _objc_inform("INITIALIZE: thread %p: %s is fully +initialized",
295 objc_thread_self(), cls->nameForLogging());
298 // mark this class as fully +initialized
299 cls->setInitialized();
300 classInitLock.notifyAll();
301 _setThisThreadIsNotInitializingClass(cls);
303 // mark any subclasses that were merely waiting for this class
304 if (!pendingInitializeMap) return;
306 auto it = pendingInitializeMap->find(cls);
307 if (it == pendingInitializeMap->end()) return;
309 pending = it->second;
310 pendingInitializeMap->erase(it);
312 // Destroy the pending table if it's now empty, to save memory.
313 if (pendingInitializeMap->size() == 0) {
314 delete pendingInitializeMap;
315 pendingInitializeMap = nil;
319 PendingInitialize *next = pending->next;
320 if (pending->subclass) _finishInitializing(pending->subclass, cls);
327 /***********************************************************************
328 * _finishInitializingAfter
329 * cls has completed its +initialize method, but its superclass has not.
330 * Wait until supercls finishes before marking cls as initialized.
331 **********************************************************************/
332 static void _finishInitializingAfter(Class cls, Class supercls)
335 classInitLock.assertLocked();
337 if (PrintInitializing) {
338 _objc_inform("INITIALIZE: thread %p: class %s will be marked as fully "
339 "+initialized after superclass +[%s initialize] completes",
340 objc_thread_self(), cls->nameForLogging(),
341 supercls->nameForLogging());
344 if (!pendingInitializeMap) {
345 pendingInitializeMap = new PendingInitializeMap{10};
346 // fixme pre-size this table for CF/NSObject +initialize
349 PendingInitialize *pending = new PendingInitialize{cls};
350 auto result = pendingInitializeMap->try_emplace(supercls, pending);
351 if (!result.second) {
352 pending->next = result.first->second;
353 result.first->second = pending;
358 // Provide helpful messages in stack traces.
359 OBJC_EXTERN __attribute__((noinline, used, visibility("hidden")))
360 void waitForInitializeToComplete(Class cls)
361 asm("_WAITING_FOR_ANOTHER_THREAD_TO_FINISH_CALLING_+initialize");
362 OBJC_EXTERN __attribute__((noinline, used, visibility("hidden")))
363 void callInitialize(Class cls)
364 asm("_CALLING_SOME_+initialize_METHOD");
367 void waitForInitializeToComplete(Class cls)
369 if (PrintInitializing) {
370 _objc_inform("INITIALIZE: thread %p: blocking until +[%s initialize] "
371 "completes", objc_thread_self(), cls->nameForLogging());
374 monitor_locker_t lock(classInitLock);
375 while (!cls->isInitialized()) {
376 classInitLock.wait();
382 void callInitialize(Class cls)
384 ((void(*)(Class, SEL))objc_msgSend)(cls, @selector(initialize));
389 /***********************************************************************
390 * classHasTrivialInitialize
391 * Returns true if the class has no +initialize implementation or
392 * has a +initialize implementation that looks empty.
393 * Any root class +initialize implemetation is assumed to be trivial.
394 **********************************************************************/
395 static bool classHasTrivialInitialize(Class cls)
397 if (cls->isRootClass() || cls->isRootMetaclass()) return true;
399 Class rootCls = cls->ISA()->ISA()->superclass;
401 IMP rootImp = lookUpImpOrNil(rootCls, @selector(initialize), rootCls->ISA());
402 IMP imp = lookUpImpOrNil(cls, @selector(initialize), cls->ISA());
403 return (imp == nil || imp == (IMP)&objc_noop_imp || imp == rootImp);
407 /***********************************************************************
408 * lockAndFinishInitializing
409 * Mark a class as finished initializing and notify waiters, or queue for later.
410 * If the superclass is also done initializing, then update
411 * the info bits and notify waiting threads.
412 * If not, update them later. (This can happen if this +initialize
413 * was itself triggered from inside a superclass +initialize.)
414 **********************************************************************/
415 static void lockAndFinishInitializing(Class cls, Class supercls)
417 monitor_locker_t lock(classInitLock);
418 if (!supercls || supercls->isInitialized()) {
419 _finishInitializing(cls, supercls);
421 _finishInitializingAfter(cls, supercls);
426 /***********************************************************************
427 * performForkChildInitialize
428 * +initialize after fork() is problematic. It's possible for the
429 * fork child process to call some +initialize that would deadlock waiting
430 * for another +initialize in the parent process.
431 * We wouldn't know how much progress it made therein, so we can't
432 * act as if +initialize completed nor can we restart +initialize
435 * Instead we proceed introspectively. If the class has some
436 * +initialize implementation, we halt. If the class has no
437 * +initialize implementation of its own, we continue. Root
438 * class +initialize is assumed to be empty if it exists.
440 * We apply this rule even if the child's +initialize does not appear
441 * to be blocked by anything. This prevents races wherein the +initialize
442 * deadlock only rarely hits. Instead we disallow it even when we "won"
445 * Exception: processes that are single-threaded when fork() is called
446 * have no restrictions on +initialize in the child. Examples: sshd and httpd.
448 * Classes that wish to implement +initialize and be callable after
449 * fork() must use an atfork() handler to provoke +initialize in fork prepare.
450 **********************************************************************/
452 // Called before halting when some +initialize
453 // method can't be called after fork().
455 void objc_initializeAfterForkError(Class cls)
458 void performForkChildInitialize(Class cls, Class supercls)
460 if (classHasTrivialInitialize(cls)) {
461 if (PrintInitializing) {
462 _objc_inform("INITIALIZE: thread %p: skipping trivial +[%s "
463 "initialize] in fork() child process",
464 objc_thread_self(), cls->nameForLogging());
466 lockAndFinishInitializing(cls, supercls);
469 if (PrintInitializing) {
470 _objc_inform("INITIALIZE: thread %p: refusing to call +[%s "
471 "initialize] in fork() child process because "
472 "it may have been in progress when fork() was called",
473 objc_thread_self(), cls->nameForLogging());
475 _objc_inform_now_and_on_crash
476 ("+[%s initialize] may have been in progress in another thread "
477 "when fork() was called.",
478 cls->nameForLogging());
479 objc_initializeAfterForkError(cls);
481 ("+[%s initialize] may have been in progress in another thread "
482 "when fork() was called. We cannot safely call it or "
483 "ignore it in the fork() child process. Crashing instead. "
484 "Set a breakpoint on objc_initializeAfterForkError to debug.",
485 cls->nameForLogging());
490 /***********************************************************************
491 * class_initialize. Send the '+initialize' message on demand to any
492 * uninitialized class. Force initialization of superclasses first.
493 **********************************************************************/
494 void initializeNonMetaClass(Class cls)
496 ASSERT(!cls->isMetaClass());
499 bool reallyInitialize = NO;
501 // Make sure super is done initializing BEFORE beginning to initialize cls.
502 // See note about deadlock above.
503 supercls = cls->superclass;
504 if (supercls && !supercls->isInitialized()) {
505 initializeNonMetaClass(supercls);
508 // Try to atomically set CLS_INITIALIZING.
509 SmallVector<_objc_willInitializeClassCallback, 1> localWillInitializeFuncs;
511 monitor_locker_t lock(classInitLock);
512 if (!cls->isInitialized() && !cls->isInitializing()) {
513 cls->setInitializing();
514 reallyInitialize = YES;
516 // Grab a copy of the will-initialize funcs with the lock held.
517 localWillInitializeFuncs.initFrom(willInitializeFuncs);
521 if (reallyInitialize) {
522 // We successfully set the CLS_INITIALIZING bit. Initialize the class.
524 // Record that we're initializing this class so we can message it.
525 _setThisThreadIsInitializingClass(cls);
527 if (MultithreadedForkChild) {
528 // LOL JK we don't really call +initialize methods after fork().
529 performForkChildInitialize(cls, supercls);
533 for (auto callback : localWillInitializeFuncs)
534 callback.f(callback.context, cls);
536 // Send the +initialize message.
537 // Note that +initialize is sent to the superclass (again) if
538 // this class doesn't implement +initialize. 2157218
539 if (PrintInitializing) {
540 _objc_inform("INITIALIZE: thread %p: calling +[%s initialize]",
541 objc_thread_self(), cls->nameForLogging());
544 // Exceptions: A +initialize call that throws an exception
545 // is deemed to be a complete and successful +initialize.
547 // Only __OBJC2__ adds these handlers. !__OBJC2__ has a
548 // bootstrapping problem of this versus CF's call to
549 // objc_exception_set_functions().
556 if (PrintInitializing) {
557 _objc_inform("INITIALIZE: thread %p: finished +[%s initialize]",
558 objc_thread_self(), cls->nameForLogging());
563 if (PrintInitializing) {
564 _objc_inform("INITIALIZE: thread %p: +[%s initialize] "
565 "threw an exception",
566 objc_thread_self(), cls->nameForLogging());
573 // Done initializing.
574 lockAndFinishInitializing(cls, supercls);
579 else if (cls->isInitializing()) {
580 // We couldn't set INITIALIZING because INITIALIZING was already set.
581 // If this thread set it earlier, continue normally.
582 // If some other thread set it, block until initialize is done.
583 // It's ok if INITIALIZING changes to INITIALIZED while we're here,
584 // because we safely check for INITIALIZED inside the lock
586 if (_thisThreadIsInitializingClass(cls)) {
588 } else if (!MultithreadedForkChild) {
589 waitForInitializeToComplete(cls);
592 // We're on the child side of fork(), facing a class that
593 // was initializing by some other thread when fork() was called.
594 _setThisThreadIsInitializingClass(cls);
595 performForkChildInitialize(cls, supercls);
599 else if (cls->isInitialized()) {
600 // Set CLS_INITIALIZING failed because someone else already
601 // initialized the class. Continue normally.
602 // NOTE this check must come AFTER the ISINITIALIZING case.
603 // Otherwise: Another thread is initializing this class. ISINITIALIZED
604 // is false. Skip this clause. Then the other thread finishes
605 // initialization and sets INITIALIZING=no and INITIALIZED=yes.
606 // Skip the ISINITIALIZING clause. Die horribly.
611 // We shouldn't be here.
612 _objc_fatal("thread-safe class init in objc runtime is buggy!");
616 void _objc_addWillInitializeClassFunc(_objc_func_willInitializeClass _Nonnull func, void * _Nullable context) {
619 Class *realizedClasses;
621 // Fetch all currently initialized classes. Do this with classInitLock held
622 // so we don't race with setting those flags.
624 monitor_locker_t initLock(classInitLock);
625 realizedClasses = objc_copyRealizedClassList(&count);
626 for (unsigned i = 0; i < count; i++) {
627 // Remove uninitialized classes from the array.
628 if (!realizedClasses[i]->isInitializing() && !realizedClasses[i]->isInitialized())
629 realizedClasses[i] = Nil;
632 willInitializeFuncs.append({func, context});
635 // Invoke the callback for all realized classes that weren't cleared out.
636 for (unsigned i = 0; i < count; i++) {
637 if (Class cls = realizedClasses[i]) {
642 free(realizedClasses);