2 * Copyright (c) 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 /***********************************************************************
26 * OS portability layer.
27 **********************************************************************/
29 #include "objc-private.h"
30 #include "objc-loadmethod.h"
34 #include "objc-runtime-old.h"
37 const fork_unsafe_lock_t fork_unsafe_lock;
39 int monitor_init(monitor_t *c)
41 // fixme error checking
42 HANDLE mutex = CreateMutex(NULL, TRUE, NULL);
44 // fixme memory barrier here?
45 if (0 == InterlockedCompareExchangePointer(&c->mutex, mutex, 0)) {
46 // we win - finish construction
47 c->waiters = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
48 c->waitersDone = CreateEvent(NULL, FALSE, FALSE, NULL);
49 InitializeCriticalSection(&c->waitCountLock);
52 ReleaseMutex(c->mutex);
57 // someone else allocated the mutex and constructed the monitor
63 void mutex_init(mutex_t *m)
66 CRITICAL_SECTION *newlock = malloc(sizeof(CRITICAL_SECTION));
67 InitializeCriticalSection(newlock);
68 // fixme memory barrier here?
69 if (0 == InterlockedCompareExchangePointer(&m->lock, newlock, 0)) {
72 // someone else installed their lock first
73 DeleteCriticalSection(newlock);
79 void recursive_mutex_init(recursive_mutex_t *m)
81 // fixme error checking
82 HANDLE newmutex = CreateMutex(NULL, FALSE, NULL);
84 // fixme memory barrier here?
85 if (0 == InterlockedCompareExchangePointer(&m->mutex, newmutex, 0)) {
91 // someone else installed their lock first
92 CloseHandle(newmutex);
96 WINBOOL APIENTRY DllMain( HMODULE hModule,
97 DWORD ul_reason_for_call,
101 switch (ul_reason_for_call) {
102 case DLL_PROCESS_ATTACH:
106 sel_init(3500); // old selector heuristic
110 case DLL_THREAD_ATTACH:
113 case DLL_THREAD_DETACH:
114 case DLL_PROCESS_DETACH:
120 OBJC_EXPORT void *_objc_init_image(HMODULE image, const objc_sections *sects)
122 header_info *hi = malloc(sizeof(header_info));
125 hi->mhdr = (const headerType *)image;
126 hi->info = sects->iiStart;
127 hi->allClassesRealized = NO;
128 hi->modules = sects->modStart ? (Module *)((void **)sects->modStart+1) : 0;
129 hi->moduleCount = (Module *)sects->modEnd - hi->modules;
130 hi->protocols = sects->protoStart ? (struct old_protocol **)((void **)sects->protoStart+1) : 0;
131 hi->protocolCount = (struct old_protocol **)sects->protoEnd - hi->protocols;
132 hi->imageinfo = NULL;
133 hi->imageinfoBytes = 0;
134 // hi->imageinfo = sects->iiStart ? (uint8_t *)((void **)sects->iiStart+1) : 0;;
135 // hi->imageinfoBytes = (uint8_t *)sects->iiEnd - hi->imageinfo;
136 hi->selrefs = sects->selrefsStart ? (SEL *)((void **)sects->selrefsStart+1) : 0;
137 hi->selrefCount = (SEL *)sects->selrefsEnd - hi->selrefs;
138 hi->clsrefs = sects->clsrefsStart ? (Class *)((void **)sects->clsrefsStart+1) : 0;
139 hi->clsrefCount = (Class *)sects->clsrefsEnd - hi->clsrefs;
142 for (i = 0; i < hi->moduleCount; i++) {
143 if (hi->modules[i]) count++;
148 hi->mod_ptr = malloc(count * sizeof(struct objc_module));
149 for (i = 0; i < hi->moduleCount; i++) {
150 if (hi->modules[i]) memcpy(&hi->mod_ptr[hi->mod_count++], hi->modules[i], sizeof(struct objc_module));
154 hi->moduleName = malloc(MAX_PATH * sizeof(TCHAR));
155 GetModuleFileName((HMODULE)(hi->mhdr), hi->moduleName, MAX_PATH * sizeof(TCHAR));
160 _objc_inform("IMAGES: loading image for %s%s%s%s\n",
162 headerIsBundle(hi) ? " (bundle)" : "",
163 hi->info->isReplacement() ? " (replacement)":"",
164 hi->info->hasCategoryClassProperties() ? " (has class properties)":"");
167 // Count classes. Size various table based on the total.
169 int unoptimizedTotal = 0;
171 if (_getObjc2ClassList(hi, &count)) {
173 if (!hi->getInSharedCache()) unoptimizedTotal += count;
177 _read_images(&hi, 1, total, unoptimizedTotal);
182 OBJC_EXPORT void _objc_load_image(HMODULE image, header_info *hinfo)
184 prepare_load_methods(hinfo);
188 OBJC_EXPORT void _objc_unload_image(HMODULE image, header_info *hinfo)
190 _objc_fatal("image unload not supported");
197 #include "objc-file-old.h"
198 #include "objc-file.h"
201 /***********************************************************************
202 * libobjc must never run static destructors.
203 * Cover libc's __cxa_atexit with our own definition that runs nothing.
204 * rdar://21734598 ER: Compiler option to suppress C++ static destructors
205 **********************************************************************/
206 extern "C" int __cxa_atexit();
207 extern "C" int __cxa_atexit() { return 0; }
210 /***********************************************************************
212 * Return YES if the header has invalid Mach-o magic.
213 **********************************************************************/
214 bool bad_magic(const headerType *mhdr)
216 return (mhdr->magic != MH_MAGIC && mhdr->magic != MH_MAGIC_64 &&
217 mhdr->magic != MH_CIGAM && mhdr->magic != MH_CIGAM_64);
221 static header_info * addHeader(const headerType *mhdr, const char *path, int &totalClasses, int &unoptimizedTotalClasses)
225 if (bad_magic(mhdr)) return NULL;
227 bool inSharedCache = false;
229 // Look for hinfo from the dyld shared cache.
230 hi = preoptimizedHinfoForHeader(mhdr);
232 // Found an hinfo in the dyld shared cache.
234 // Weed out duplicates.
235 if (hi->isLoaded()) {
239 inSharedCache = true;
241 // Initialize fields not set by the shared cache
242 // hi->next is set by appendHeader
246 _objc_inform("PREOPTIMIZATION: honoring preoptimized header info at %p for %s", hi, hi->fname());
250 _objc_fatal("shouldn't be here");
254 size_t info_size = 0;
255 const objc_image_info *image_info = _getObjcImageInfo(mhdr,&info_size);
256 assert(image_info == hi->info());
261 // Didn't find an hinfo in the dyld shared cache.
263 // Weed out duplicates
264 for (hi = FirstHeader; hi; hi = hi->getNext()) {
265 if (mhdr == hi->mhdr()) return NULL;
268 // Locate the __OBJC segment
269 size_t info_size = 0;
270 unsigned long seg_size;
271 const objc_image_info *image_info = _getObjcImageInfo(mhdr,&info_size);
272 const uint8_t *objc_segment = getsegmentdata(mhdr,SEG_OBJC,&seg_size);
273 if (!objc_segment && !image_info) return NULL;
275 // Allocate a header_info entry.
276 // Note we also allocate space for a single header_info_rw in the
277 // rw_data[] inside header_info.
278 hi = (header_info *)calloc(sizeof(header_info) + sizeof(header_info_rw), 1);
280 // Set up the new header_info entry.
283 // mhdr must already be set
285 hi->mod_ptr = _getObjcModules(hi, &hi->mod_count);
287 // Install a placeholder image_info if absent to simplify code elsewhere
288 static const objc_image_info emptyInfo = {0, 0};
289 hi->setinfo(image_info ?: &emptyInfo);
292 hi->setAllClassesRealized(NO);
298 if (_getObjc2ClassList(hi, &count)) {
299 totalClasses += (int)count;
300 if (!inSharedCache) unoptimizedTotalClasses += count;
311 /***********************************************************************
313 * Returns true if the image links directly to a dylib whose install name
314 * is exactly the given name.
315 **********************************************************************/
317 linksToLibrary(const header_info *hi, const char *name)
319 const struct dylib_command *cmd;
322 cmd = (const struct dylib_command *) (hi->mhdr() + 1);
323 for (i = 0; i < hi->mhdr()->ncmds; i++) {
324 if (cmd->cmd == LC_LOAD_DYLIB || cmd->cmd == LC_LOAD_UPWARD_DYLIB ||
325 cmd->cmd == LC_LOAD_WEAK_DYLIB || cmd->cmd == LC_REEXPORT_DYLIB)
327 const char *dylib = cmd->dylib.name.offset + (const char *)cmd;
328 if (0 == strcmp(dylib, name)) return true;
330 cmd = (const struct dylib_command *)((char *)cmd + cmd->cmdsize);
337 #if SUPPORT_GC_COMPAT
339 /***********************************************************************
341 * Return YES if the executable requires GC.
342 **********************************************************************/
343 static bool shouldRejectGCApp(const header_info *hi)
345 assert(hi->mhdr()->filetype == MH_EXECUTE);
347 if (!hi->info()->supportsGC()) {
348 // App does not use GC. Don't reject it.
352 // Exception: Trivial AppleScriptObjC apps can run without GC.
353 // 1. executable defines no classes
354 // 2. executable references NSBundle only
355 // 3. executable links to AppleScriptObjC.framework
356 // Note that objc_appRequiresGC() also knows about this.
357 size_t classcount = 0;
360 _getObjc2ClassList(hi, &classcount);
361 _getObjc2ClassRefs(hi, &refcount);
363 if (hi->mod_count == 0 || (hi->mod_count == 1 && !hi->mod_ptr[0].symtab)) classcount = 0;
365 _getObjcClassRefs(hi, &refcount);
367 if (classcount == 0 && refcount == 1 &&
368 linksToLibrary(hi, "/System/Library/Frameworks"
369 "/AppleScriptObjC.framework/Versions/A"
372 // It's AppleScriptObjC. Don't reject it.
376 // GC and not trivial AppleScriptObjC. Reject it.
382 /***********************************************************************
384 * Halt if an image requires GC.
385 * Testing of the main executable should use rejectGCApp() instead.
386 **********************************************************************/
387 static bool shouldRejectGCImage(const headerType *mhdr)
389 assert(mhdr->filetype != MH_EXECUTE);
391 objc_image_info *image_info;
395 unsigned long seg_size;
396 // 32-bit: __OBJC seg but no image_info means no GC support
397 if (!getsegmentdata(mhdr, "__OBJC", &seg_size)) {
398 // Not objc, therefore not GC. Don't reject it.
401 image_info = _getObjcImageInfo(mhdr, &size);
403 // No image_info, therefore not GC. Don't reject it.
407 // 64-bit: no image_info means no objc at all
408 image_info = _getObjcImageInfo(mhdr, &size);
410 // Not objc, therefore not GC. Don't reject it.
415 return image_info->requiresGC();
422 /***********************************************************************
424 * Process the given images which are being mapped in by dyld.
425 * All class registration and fixups are performed (or deferred pending
426 * discovery of missing superclasses etc), and +load methods are called.
428 * info[] is in bottom-up order i.e. libobjc will be earlier in the
429 * array than any library that links to libobjc.
431 * Locking: loadMethodLock(old) or runtimeLock(new) acquired by map_images.
432 **********************************************************************/
434 #include "objc-file.h"
436 #include "objc-file-old.h"
440 map_images_nolock(unsigned mhCount, const char * const mhPaths[],
441 const struct mach_header * const mhdrs[])
443 static bool firstTime = YES;
444 header_info *hList[mhCount];
446 size_t selrefCount = 0;
448 // Perform first-time initialization if necessary.
449 // This function is called before ordinary library initializers.
450 // fixme defer initialization until an objc-using image is found?
456 _objc_inform("IMAGES: processing %u newly-mapped images...\n", mhCount);
460 // Find all images with Objective-C metadata.
463 // Count classes. Size various table based on the total.
464 int totalClasses = 0;
465 int unoptimizedTotalClasses = 0;
467 uint32_t i = mhCount;
469 const headerType *mhdr = (const headerType *)mhdrs[i];
471 auto hi = addHeader(mhdr, mhPaths[i], totalClasses, unoptimizedTotalClasses);
473 // no objc data in this entry
477 if (mhdr->filetype == MH_EXECUTE) {
478 // Size some data structures based on main executable's size
481 _getObjc2SelectorRefs(hi, &count);
482 selrefCount += count;
483 _getObjc2MessageRefs(hi, &count);
484 selrefCount += count;
486 _getObjcSelectorRefs(hi, &selrefCount);
489 #if SUPPORT_GC_COMPAT
490 // Halt if this is a GC app.
491 if (shouldRejectGCApp(hi)) {
492 _objc_fatal_with_reason
493 (OBJC_EXIT_REASON_GC_NOT_SUPPORTED,
494 OS_REASON_FLAG_CONSISTENT_FAILURE,
495 "Objective-C garbage collection "
496 "is no longer supported.");
501 hList[hCount++] = hi;
504 _objc_inform("IMAGES: loading image for %s%s%s%s%s\n",
506 mhdr->filetype == MH_BUNDLE ? " (bundle)" : "",
507 hi->info()->isReplacement() ? " (replacement)" : "",
508 hi->info()->hasCategoryClassProperties() ? " (has class properties)" : "",
509 hi->info()->optimizedByDyld()?" (preoptimized)":"");
514 // Perform one-time runtime initialization that must be deferred until
515 // the executable itself is found. This needs to be done before
516 // further initialization.
517 // (The executable may not be present in this infoList if the
518 // executable does not contain Objective-C code but Objective-C
519 // is dynamically loaded later.
521 sel_init(selrefCount);
524 #if SUPPORT_GC_COMPAT
525 // Reject any GC images linked to the main executable.
526 // We already rejected the app itself above.
527 // Images loaded after launch will be rejected by dyld.
529 for (uint32_t i = 0; i < hCount; i++) {
531 auto mh = hi->mhdr();
532 if (mh->filetype != MH_EXECUTE && shouldRejectGCImage(mh)) {
533 _objc_fatal_with_reason
534 (OBJC_EXIT_REASON_GC_NOT_SUPPORTED,
535 OS_REASON_FLAG_CONSISTENT_FAILURE,
536 "%s requires Objective-C garbage collection "
537 "which is no longer supported.", hi->fname());
543 // Disable +initialize fork safety if the app is too old (< 10.13).
544 // Disable +initialize fork safety if the app has a
545 // __DATA,__objc_fork_ok section.
547 if (dyld_get_program_sdk_version() < DYLD_MACOSX_VERSION_10_13) {
548 DisableInitializeForkSafety = true;
549 if (PrintInitializing) {
550 _objc_inform("INITIALIZE: disabling +initialize fork "
551 "safety enforcement because the app is "
552 "too old (SDK version " SDK_FORMAT ")",
553 FORMAT_SDK(dyld_get_program_sdk_version()));
557 for (uint32_t i = 0; i < hCount; i++) {
559 auto mh = hi->mhdr();
560 if (mh->filetype != MH_EXECUTE) continue;
562 if (getsectiondata(hi->mhdr(), "__DATA", "__objc_fork_ok", &size)) {
563 DisableInitializeForkSafety = true;
564 if (PrintInitializing) {
565 _objc_inform("INITIALIZE: disabling +initialize fork "
566 "safety enforcement because the app has "
567 "a __DATA,__objc_fork_ok section");
570 break; // assume only one MH_EXECUTE image
577 _read_images(hList, hCount, totalClasses, unoptimizedTotalClasses);
584 /***********************************************************************
586 * Process the given image which is about to be unmapped by dyld.
587 * mh is mach_header instead of headerType because that's what
588 * dyld_priv.h says even for 64-bit.
590 * Locking: loadMethodLock(both) and runtimeLock(new) acquired by unmap_image.
591 **********************************************************************/
593 unmap_image_nolock(const struct mach_header *mh)
596 _objc_inform("IMAGES: processing 1 newly-unmapped image...\n");
601 // Find the runtime's header_info struct for the image
602 for (hi = FirstHeader; hi != NULL; hi = hi->getNext()) {
603 if (hi->mhdr() == (const headerType *)mh) {
611 _objc_inform("IMAGES: unloading image for %s%s%s\n",
613 hi->mhdr()->filetype == MH_BUNDLE ? " (bundle)" : "",
614 hi->info()->isReplacement() ? " (replacement)" : "");
619 // Remove header_info from header list
625 /***********************************************************************
627 * Run C++ static constructor functions.
628 * libc calls _objc_init() before dyld would call our static constructors,
629 * so we have to do it ourselves.
630 **********************************************************************/
631 static void static_init()
634 Initializer *inits = getLibobjcInitializers(&_mh_dylib_header, &count);
635 for (size_t i = 0; i < count; i++) {
641 /***********************************************************************
642 * _objc_atfork_prepare
643 * _objc_atfork_parent
645 * Allow ObjC to be used between fork() and exec().
646 * libc requires this because it has fork-safe functions that use os_objects.
648 * _objc_atfork_prepare() acquires all locks.
649 * _objc_atfork_parent() releases the locks again.
650 * _objc_atfork_child() forcibly resets the locks.
651 **********************************************************************/
653 // Declare lock ordering.
655 __attribute__((constructor))
656 static void defineLockOrder()
658 // Every lock precedes crashlog_lock
659 // on the assumption that fatal errors could be anywhere.
660 lockdebug_lock_precedes_lock(&loadMethodLock, &crashlog_lock);
661 lockdebug_lock_precedes_lock(&classInitLock, &crashlog_lock);
663 lockdebug_lock_precedes_lock(&runtimeLock, &crashlog_lock);
664 lockdebug_lock_precedes_lock(&DemangleCacheLock, &crashlog_lock);
666 lockdebug_lock_precedes_lock(&classLock, &crashlog_lock);
667 lockdebug_lock_precedes_lock(&methodListLock, &crashlog_lock);
668 lockdebug_lock_precedes_lock(&NXUniqueStringLock, &crashlog_lock);
669 lockdebug_lock_precedes_lock(&impLock, &crashlog_lock);
671 lockdebug_lock_precedes_lock(&selLock, &crashlog_lock);
672 lockdebug_lock_precedes_lock(&cacheUpdateLock, &crashlog_lock);
673 lockdebug_lock_precedes_lock(&objcMsgLogLock, &crashlog_lock);
674 lockdebug_lock_precedes_lock(&AltHandlerDebugLock, &crashlog_lock);
675 lockdebug_lock_precedes_lock(&AssociationsManagerLock, &crashlog_lock);
676 SideTableLocksPrecedeLock(&crashlog_lock);
677 PropertyLocks.precedeLock(&crashlog_lock);
678 StructLocks.precedeLock(&crashlog_lock);
679 CppObjectLocks.precedeLock(&crashlog_lock);
681 // loadMethodLock precedes everything
682 // because it is held while +load methods run
683 lockdebug_lock_precedes_lock(&loadMethodLock, &classInitLock);
685 lockdebug_lock_precedes_lock(&loadMethodLock, &runtimeLock);
686 lockdebug_lock_precedes_lock(&loadMethodLock, &DemangleCacheLock);
688 lockdebug_lock_precedes_lock(&loadMethodLock, &methodListLock);
689 lockdebug_lock_precedes_lock(&loadMethodLock, &classLock);
690 lockdebug_lock_precedes_lock(&loadMethodLock, &NXUniqueStringLock);
691 lockdebug_lock_precedes_lock(&loadMethodLock, &impLock);
693 lockdebug_lock_precedes_lock(&loadMethodLock, &selLock);
694 lockdebug_lock_precedes_lock(&loadMethodLock, &cacheUpdateLock);
695 lockdebug_lock_precedes_lock(&loadMethodLock, &objcMsgLogLock);
696 lockdebug_lock_precedes_lock(&loadMethodLock, &AltHandlerDebugLock);
697 lockdebug_lock_precedes_lock(&loadMethodLock, &AssociationsManagerLock);
698 SideTableLocksSucceedLock(&loadMethodLock);
699 PropertyLocks.succeedLock(&loadMethodLock);
700 StructLocks.succeedLock(&loadMethodLock);
701 CppObjectLocks.succeedLock(&loadMethodLock);
703 // PropertyLocks and CppObjectLocks and AssociationManagerLock
704 // precede everything because they are held while objc_retain()
705 // or C++ copy are called.
706 // (StructLocks do not precede everything because it calls memmove only.)
707 auto PropertyAndCppObjectAndAssocLocksPrecedeLock = [&](const void *lock) {
708 PropertyLocks.precedeLock(lock);
709 CppObjectLocks.precedeLock(lock);
710 lockdebug_lock_precedes_lock(&AssociationsManagerLock, lock);
713 PropertyAndCppObjectAndAssocLocksPrecedeLock(&runtimeLock);
714 PropertyAndCppObjectAndAssocLocksPrecedeLock(&DemangleCacheLock);
716 PropertyAndCppObjectAndAssocLocksPrecedeLock(&methodListLock);
717 PropertyAndCppObjectAndAssocLocksPrecedeLock(&classLock);
718 PropertyAndCppObjectAndAssocLocksPrecedeLock(&NXUniqueStringLock);
719 PropertyAndCppObjectAndAssocLocksPrecedeLock(&impLock);
721 PropertyAndCppObjectAndAssocLocksPrecedeLock(&classInitLock);
722 PropertyAndCppObjectAndAssocLocksPrecedeLock(&selLock);
723 PropertyAndCppObjectAndAssocLocksPrecedeLock(&cacheUpdateLock);
724 PropertyAndCppObjectAndAssocLocksPrecedeLock(&objcMsgLogLock);
725 PropertyAndCppObjectAndAssocLocksPrecedeLock(&AltHandlerDebugLock);
727 SideTableLocksSucceedLocks(PropertyLocks);
728 SideTableLocksSucceedLocks(CppObjectLocks);
729 SideTableLocksSucceedLock(&AssociationsManagerLock);
731 PropertyLocks.precedeLock(&AssociationsManagerLock);
732 CppObjectLocks.precedeLock(&AssociationsManagerLock);
735 lockdebug_lock_precedes_lock(&classInitLock, &runtimeLock);
739 // Runtime operations may occur inside SideTable locks
740 // (such as storeWeak calling getMethodImplementation)
741 SideTableLocksPrecedeLock(&runtimeLock);
742 SideTableLocksPrecedeLock(&classInitLock);
743 // Some operations may occur inside runtimeLock.
744 lockdebug_lock_precedes_lock(&runtimeLock, &selLock);
745 lockdebug_lock_precedes_lock(&runtimeLock, &cacheUpdateLock);
746 lockdebug_lock_precedes_lock(&runtimeLock, &DemangleCacheLock);
748 // Runtime operations may occur inside SideTable locks
749 // (such as storeWeak calling getMethodImplementation)
750 SideTableLocksPrecedeLock(&methodListLock);
751 SideTableLocksPrecedeLock(&classInitLock);
752 // Method lookup and fixup.
753 lockdebug_lock_precedes_lock(&methodListLock, &classLock);
754 lockdebug_lock_precedes_lock(&methodListLock, &selLock);
755 lockdebug_lock_precedes_lock(&methodListLock, &cacheUpdateLock);
756 lockdebug_lock_precedes_lock(&methodListLock, &impLock);
757 lockdebug_lock_precedes_lock(&classLock, &selLock);
758 lockdebug_lock_precedes_lock(&classLock, &cacheUpdateLock);
761 // Striped locks use address order internally.
762 SideTableDefineLockOrder();
763 PropertyLocks.defineLockOrder();
764 StructLocks.defineLockOrder();
765 CppObjectLocks.defineLockOrder();
770 static bool ForkIsMultithreaded;
771 void _objc_atfork_prepare()
773 // Save threaded-ness for the child's use.
774 ForkIsMultithreaded = pthread_is_threaded_np();
776 lockdebug_assert_no_locks_locked();
777 lockdebug_setInForkPrepare(true);
779 loadMethodLock.lock();
780 PropertyLocks.lockAll();
781 CppObjectLocks.lockAll();
782 AssociationsManagerLock.lock();
784 classInitLock.enter();
787 DemangleCacheLock.lock();
789 methodListLock.lock();
791 NXUniqueStringLock.lock();
795 cacheUpdateLock.lock();
796 objcMsgLogLock.lock();
797 AltHandlerDebugLock.lock();
798 StructLocks.lockAll();
799 crashlog_lock.lock();
801 lockdebug_assert_all_locks_locked();
802 lockdebug_setInForkPrepare(false);
805 void _objc_atfork_parent()
807 lockdebug_assert_all_locks_locked();
809 CppObjectLocks.unlockAll();
810 StructLocks.unlockAll();
811 PropertyLocks.unlockAll();
812 AssociationsManagerLock.unlock();
813 AltHandlerDebugLock.unlock();
814 objcMsgLogLock.unlock();
815 crashlog_lock.unlock();
816 loadMethodLock.unlock();
817 cacheUpdateLock.unlock();
818 selLock.unlockWrite();
819 SideTableUnlockAll();
821 DemangleCacheLock.unlock();
822 runtimeLock.unlockWrite();
825 NXUniqueStringLock.unlock();
826 methodListLock.unlock();
829 classInitLock.leave();
831 lockdebug_assert_no_locks_locked();
834 void _objc_atfork_child()
836 // Turn on +initialize fork safety enforcement if applicable.
837 if (ForkIsMultithreaded && !DisableInitializeForkSafety) {
838 MultithreadedForkChild = true;
841 lockdebug_assert_all_locks_locked();
843 CppObjectLocks.forceResetAll();
844 StructLocks.forceResetAll();
845 PropertyLocks.forceResetAll();
846 AssociationsManagerLock.forceReset();
847 AltHandlerDebugLock.forceReset();
848 objcMsgLogLock.forceReset();
849 crashlog_lock.forceReset();
850 loadMethodLock.forceReset();
851 cacheUpdateLock.forceReset();
852 selLock.forceReset();
853 SideTableForceResetAll();
855 DemangleCacheLock.forceReset();
856 runtimeLock.forceReset();
858 impLock.forceReset();
859 NXUniqueStringLock.forceReset();
860 methodListLock.forceReset();
861 classLock.forceReset();
863 classInitLock.forceReset();
865 lockdebug_assert_no_locks_locked();
869 /***********************************************************************
871 * Bootstrap initialization. Registers our image notifier with dyld.
872 * Called by libSystem BEFORE library initialization time
873 **********************************************************************/
875 void _objc_init(void)
877 static bool initialized = false;
878 if (initialized) return;
881 // fixme defer initialization until an objc-using image is found?
888 _dyld_objc_notify_register(&map_images, load_images, unmap_image);
892 /***********************************************************************
894 * addr can be a class or a category
895 **********************************************************************/
896 static const header_info *_headerForAddress(void *addr)
899 const char *segnames[] = { "__DATA", "__DATA_CONST", "__DATA_DIRTY" };
901 const char *segnames[] = { "__OBJC" };
905 for (hi = FirstHeader; hi != NULL; hi = hi->getNext()) {
906 for (size_t i = 0; i < sizeof(segnames)/sizeof(segnames[0]); i++) {
907 unsigned long seg_size;
908 uint8_t *seg = getsegmentdata(hi->mhdr(), segnames[i], &seg_size);
911 // Is the class in this header?
912 if ((uint8_t *)addr >= seg && (uint8_t *)addr < seg + seg_size) {
923 /***********************************************************************
925 * Return the image header containing this class, or NULL.
926 * Returns NULL on runtime-constructed classes, and the NSCF classes.
927 **********************************************************************/
928 const header_info *_headerForClass(Class cls)
930 return _headerForAddress(cls);
934 /**********************************************************************
936 * Securely open a file from a world-writable directory (like /tmp)
937 * If the file does not exist, it will be atomically created with mode 0600
938 * If the file exists, it must be, and remain after opening:
939 * 1. a regular file (in particular, not a symlink)
941 * 3. permissions 0600
943 * Returns a file descriptor or -1. Errno may or may not be set on error.
944 **********************************************************************/
945 int secure_open(const char *filename, int flags, uid_t euid)
952 if (flags & O_TRUNC) {
953 // Don't truncate the file until after it is open and verified.
957 if (flags & O_CREAT) {
958 // Don't create except when we're ready for it
964 if (lstat(filename, &ls) < 0) {
965 if (errno == ENOENT && create) {
966 // No such file - create it
967 fd = open(filename, flags | O_CREAT | O_EXCL, 0600);
969 // File was created successfully.
970 // New file does not need to be truncated.
973 // File creation failed.
977 // lstat failed, or user doesn't want to create the file
981 // lstat succeeded - verify attributes and open
982 if (S_ISREG(ls.st_mode) && // regular file?
983 ls.st_nlink == 1 && // link count == 1?
984 ls.st_uid == euid && // owned by euid?
985 (ls.st_mode & ALLPERMS) == (S_IRUSR | S_IWUSR)) // mode 0600?
987 // Attributes look ok - open it and check attributes again
988 fd = open(filename, flags, 0000);
990 // File is open - double-check attributes
991 if (0 == fstat(fd, &fs) &&
992 fs.st_nlink == ls.st_nlink && // link count == 1?
993 fs.st_uid == ls.st_uid && // owned by euid?
994 fs.st_mode == ls.st_mode && // regular file, 0600?
995 fs.st_ino == ls.st_ino && // same inode as before?
996 fs.st_dev == ls.st_dev) // same device as before?
998 // File is open and OK
999 if (truncate) ftruncate(fd, 0);
1002 // Opened file looks funny - close it
1011 // Unopened file looks funny - don't open it
1018 #if TARGET_OS_IPHONE
1020 const char *__crashreporter_info__ = NULL;
1022 const char *CRSetCrashLogMessage(const char *msg)
1024 __crashreporter_info__ = msg;
1027 const char *CRGetCrashLogMessage(void)
1029 return __crashreporter_info__;