123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041 |
- #include "objc-private.h"
- #include "objc-loadmethod.h"
- #if TARGET_OS_WIN32
- #include "objc-runtime-old.h"
- #include "objcrt.h"
- const fork_unsafe_lock_t fork_unsafe_lock;
- int monitor_init(monitor_t *c)
- {
-
- HANDLE mutex = CreateMutex(NULL, TRUE, NULL);
- while (!c->mutex) {
-
- if (0 == InterlockedCompareExchangePointer(&c->mutex, mutex, 0)) {
-
- c->waiters = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
- c->waitersDone = CreateEvent(NULL, FALSE, FALSE, NULL);
- InitializeCriticalSection(&c->waitCountLock);
- c->waitCount = 0;
- c->didBroadcast = 0;
- ReleaseMutex(c->mutex);
- return 0;
- }
- }
-
- ReleaseMutex(mutex);
- CloseHandle(mutex);
- return 0;
- }
- void mutex_init(mutex_t *m)
- {
- while (!m->lock) {
- CRITICAL_SECTION *newlock = malloc(sizeof(CRITICAL_SECTION));
- InitializeCriticalSection(newlock);
-
- if (0 == InterlockedCompareExchangePointer(&m->lock, newlock, 0)) {
- return;
- }
-
- DeleteCriticalSection(newlock);
- free(newlock);
- }
- }
- void recursive_mutex_init(recursive_mutex_t *m)
- {
-
- HANDLE newmutex = CreateMutex(NULL, FALSE, NULL);
- while (!m->mutex) {
-
- if (0 == InterlockedCompareExchangePointer(&m->mutex, newmutex, 0)) {
-
- return;
- }
- }
-
-
- CloseHandle(newmutex);
- }
- WINBOOL APIENTRY DllMain( HMODULE hModule,
- DWORD ul_reason_for_call,
- LPVOID lpReserved
- )
- {
- switch (ul_reason_for_call) {
- case DLL_PROCESS_ATTACH:
- environ_init();
- tls_init();
- lock_init();
- sel_init(3500);
- exception_init();
- break;
- case DLL_THREAD_ATTACH:
- break;
- case DLL_THREAD_DETACH:
- case DLL_PROCESS_DETACH:
- break;
- }
- return TRUE;
- }
- OBJC_EXPORT void *_objc_init_image(HMODULE image, const objc_sections *sects)
- {
- header_info *hi = malloc(sizeof(header_info));
- size_t count, i;
- hi->mhdr = (const headerType *)image;
- hi->info = sects->iiStart;
- hi->allClassesRealized = NO;
- hi->modules = sects->modStart ? (Module *)((void **)sects->modStart+1) : 0;
- hi->moduleCount = (Module *)sects->modEnd - hi->modules;
- hi->protocols = sects->protoStart ? (struct old_protocol **)((void **)sects->protoStart+1) : 0;
- hi->protocolCount = (struct old_protocol **)sects->protoEnd - hi->protocols;
- hi->imageinfo = NULL;
- hi->imageinfoBytes = 0;
-
- hi->selrefs = sects->selrefsStart ? (SEL *)((void **)sects->selrefsStart+1) : 0;
- hi->selrefCount = (SEL *)sects->selrefsEnd - hi->selrefs;
- hi->clsrefs = sects->clsrefsStart ? (Class *)((void **)sects->clsrefsStart+1) : 0;
- hi->clsrefCount = (Class *)sects->clsrefsEnd - hi->clsrefs;
- count = 0;
- for (i = 0; i < hi->moduleCount; i++) {
- if (hi->modules[i]) count++;
- }
- hi->mod_count = 0;
- hi->mod_ptr = 0;
- if (count > 0) {
- hi->mod_ptr = malloc(count * sizeof(struct objc_module));
- for (i = 0; i < hi->moduleCount; i++) {
- if (hi->modules[i]) memcpy(&hi->mod_ptr[hi->mod_count++], hi->modules[i], sizeof(struct objc_module));
- }
- }
-
- hi->moduleName = malloc(MAX_PATH * sizeof(TCHAR));
- GetModuleFileName((HMODULE)(hi->mhdr), hi->moduleName, MAX_PATH * sizeof(TCHAR));
- appendHeader(hi);
- if (PrintImages) {
- _objc_inform("IMAGES: loading image for %s%s%s%s\n",
- hi->fname,
- headerIsBundle(hi) ? " (bundle)" : "",
- hi->info->isReplacement() ? " (replacement)":"",
- hi->info->hasCategoryClassProperties() ? " (has class properties)":"");
- }
-
- int total = 0;
- int unoptimizedTotal = 0;
- {
- if (_getObjc2ClassList(hi, &count)) {
- total += (int)count;
- if (!hi->getInSharedCache()) unoptimizedTotal += count;
- }
- }
- _read_images(&hi, 1, total, unoptimizedTotal);
- return hi;
- }
- OBJC_EXPORT void _objc_load_image(HMODULE image, header_info *hinfo)
- {
- prepare_load_methods(hinfo);
- call_load_methods();
- }
- OBJC_EXPORT void _objc_unload_image(HMODULE image, header_info *hinfo)
- {
- _objc_fatal("image unload not supported");
- }
- #elif TARGET_OS_MAC
- #include "objc-file-old.h"
- #include "objc-file.h"
- extern "C" int __cxa_atexit();
- extern "C" int __cxa_atexit() { return 0; }
- bool bad_magic(const headerType *mhdr)
- {
- return (mhdr->magic != MH_MAGIC && mhdr->magic != MH_MAGIC_64 &&
- mhdr->magic != MH_CIGAM && mhdr->magic != MH_CIGAM_64);
- }
- static header_info * addHeader(const headerType *mhdr, const char *path, int &totalClasses, int &unoptimizedTotalClasses)
- {
- header_info *hi;
- if (bad_magic(mhdr)) return NULL;
- bool inSharedCache = false;
-
- hi = preoptimizedHinfoForHeader(mhdr);
- if (hi) {
-
-
- if (hi->isLoaded()) {
- return NULL;
- }
- inSharedCache = true;
-
-
- hi->setLoaded(true);
- if (PrintPreopt) {
- _objc_inform("PREOPTIMIZATION: honoring preoptimized header info at %p for %s", hi, hi->fname());
- }
- #if !__OBJC2__
- _objc_fatal("shouldn't be here");
- #endif
- #if DEBUG
-
- size_t info_size = 0;
- const objc_image_info *image_info = _getObjcImageInfo(mhdr,&info_size);
- assert(image_info == hi->info());
- #endif
- }
- else
- {
-
-
- for (hi = FirstHeader; hi; hi = hi->getNext()) {
- if (mhdr == hi->mhdr()) return NULL;
- }
-
- size_t info_size = 0;
- unsigned long seg_size;
- const objc_image_info *image_info = _getObjcImageInfo(mhdr,&info_size);
- const uint8_t *objc_segment = getsegmentdata(mhdr,SEG_OBJC,&seg_size);
- if (!objc_segment && !image_info) return NULL;
-
-
-
- hi = (header_info *)calloc(sizeof(header_info) + sizeof(header_info_rw), 1);
-
- hi->setmhdr(mhdr);
- #if !__OBJC2__
-
- hi->mod_count = 0;
- hi->mod_ptr = _getObjcModules(hi, &hi->mod_count);
- #endif
-
- static const objc_image_info emptyInfo = {0, 0};
- hi->setinfo(image_info ?: &emptyInfo);
- hi->setLoaded(true);
- hi->setAllClassesRealized(NO);
- }
- #if __OBJC2__
- {
- size_t count = 0;
- if (_getObjc2ClassList(hi, &count)) {
- totalClasses += (int)count;
- if (!inSharedCache) unoptimizedTotalClasses += count;
- }
- }
- #endif
- appendHeader(hi);
-
- return hi;
- }
- bool
- linksToLibrary(const header_info *hi, const char *name)
- {
- const struct dylib_command *cmd;
- unsigned long i;
-
- cmd = (const struct dylib_command *) (hi->mhdr() + 1);
- for (i = 0; i < hi->mhdr()->ncmds; i++) {
- if (cmd->cmd == LC_LOAD_DYLIB || cmd->cmd == LC_LOAD_UPWARD_DYLIB ||
- cmd->cmd == LC_LOAD_WEAK_DYLIB || cmd->cmd == LC_REEXPORT_DYLIB)
- {
- const char *dylib = cmd->dylib.name.offset + (const char *)cmd;
- if (0 == strcmp(dylib, name)) return true;
- }
- cmd = (const struct dylib_command *)((char *)cmd + cmd->cmdsize);
- }
- return false;
- }
- #if SUPPORT_GC_COMPAT
- static bool shouldRejectGCApp(const header_info *hi)
- {
- assert(hi->mhdr()->filetype == MH_EXECUTE);
- if (!hi->info()->supportsGC()) {
-
- return NO;
- }
-
-
-
-
-
-
- size_t classcount = 0;
- size_t refcount = 0;
- #if __OBJC2__
- _getObjc2ClassList(hi, &classcount);
- _getObjc2ClassRefs(hi, &refcount);
- #else
- if (hi->mod_count == 0 || (hi->mod_count == 1 && !hi->mod_ptr[0].symtab)) classcount = 0;
- else classcount = 1;
- _getObjcClassRefs(hi, &refcount);
- #endif
- if (classcount == 0 && refcount == 1 &&
- linksToLibrary(hi, "/System/Library/Frameworks"
- "/AppleScriptObjC.framework/Versions/A"
- "/AppleScriptObjC"))
- {
-
- return NO;
- }
- else {
-
- return YES;
- }
- }
- static bool shouldRejectGCImage(const headerType *mhdr)
- {
- assert(mhdr->filetype != MH_EXECUTE);
- objc_image_info *image_info;
- size_t size;
-
- #if !__OBJC2__
- unsigned long seg_size;
-
- if (!getsegmentdata(mhdr, "__OBJC", &seg_size)) {
-
- return NO;
- }
- image_info = _getObjcImageInfo(mhdr, &size);
- if (!image_info) {
-
- return NO;
- }
- #else
-
- image_info = _getObjcImageInfo(mhdr, &size);
- if (!image_info) {
-
- return NO;
- }
- #endif
- return image_info->requiresGC();
- }
- #endif
- #if __OBJC2__
- #include "objc-file.h"
- #else
- #include "objc-file-old.h"
- #endif
- void
- map_images_nolock(unsigned mhCount, const char * const mhPaths[],
- const struct mach_header * const mhdrs[])
- {
- static bool firstTime = YES;
- header_info *hList[mhCount];
- uint32_t hCount;
- size_t selrefCount = 0;
-
-
-
- if (firstTime) {
- preopt_init();
- }
- if (PrintImages) {
- _objc_inform("IMAGES: processing %u newly-mapped images...\n", mhCount);
- }
-
- hCount = 0;
-
- int totalClasses = 0;
- int unoptimizedTotalClasses = 0;
- {
- uint32_t i = mhCount;
- while (i--) {
- const headerType *mhdr = (const headerType *)mhdrs[i];
- auto hi = addHeader(mhdr, mhPaths[i], totalClasses, unoptimizedTotalClasses);
- if (!hi) {
-
- continue;
- }
-
- if (mhdr->filetype == MH_EXECUTE) {
-
- #if __OBJC2__
- size_t count;
- _getObjc2SelectorRefs(hi, &count);
- selrefCount += count;
- _getObjc2MessageRefs(hi, &count);
- selrefCount += count;
- #else
- _getObjcSelectorRefs(hi, &selrefCount);
- #endif
-
- #if SUPPORT_GC_COMPAT
-
- if (shouldRejectGCApp(hi)) {
- _objc_fatal_with_reason
- (OBJC_EXIT_REASON_GC_NOT_SUPPORTED,
- OS_REASON_FLAG_CONSISTENT_FAILURE,
- "Objective-C garbage collection "
- "is no longer supported.");
- }
- #endif
- }
-
- hList[hCount++] = hi;
-
- if (PrintImages) {
- _objc_inform("IMAGES: loading image for %s%s%s%s%s\n",
- hi->fname(),
- mhdr->filetype == MH_BUNDLE ? " (bundle)" : "",
- hi->info()->isReplacement() ? " (replacement)" : "",
- hi->info()->hasCategoryClassProperties() ? " (has class properties)" : "",
- hi->info()->optimizedByDyld()?" (preoptimized)":"");
- }
- }
- }
-
-
-
-
-
-
- if (firstTime) {
- sel_init(selrefCount);
- arr_init();
- #if SUPPORT_GC_COMPAT
-
-
-
- for (uint32_t i = 0; i < hCount; i++) {
- auto hi = hList[i];
- auto mh = hi->mhdr();
- if (mh->filetype != MH_EXECUTE && shouldRejectGCImage(mh)) {
- _objc_fatal_with_reason
- (OBJC_EXIT_REASON_GC_NOT_SUPPORTED,
- OS_REASON_FLAG_CONSISTENT_FAILURE,
- "%s requires Objective-C garbage collection "
- "which is no longer supported.", hi->fname());
- }
- }
- #endif
- #if TARGET_OS_OSX
-
-
-
- if (dyld_get_program_sdk_version() < DYLD_MACOSX_VERSION_10_13) {
- DisableInitializeForkSafety = true;
- if (PrintInitializing) {
- _objc_inform("INITIALIZE: disabling +initialize fork "
- "safety enforcement because the app is "
- "too old (SDK version " SDK_FORMAT ")",
- FORMAT_SDK(dyld_get_program_sdk_version()));
- }
- }
- for (uint32_t i = 0; i < hCount; i++) {
- auto hi = hList[i];
- auto mh = hi->mhdr();
- if (mh->filetype != MH_EXECUTE) continue;
- unsigned long size;
- if (getsectiondata(hi->mhdr(), "__DATA", "__objc_fork_ok", &size)) {
- DisableInitializeForkSafety = true;
- if (PrintInitializing) {
- _objc_inform("INITIALIZE: disabling +initialize fork "
- "safety enforcement because the app has "
- "a __DATA,__objc_fork_ok section");
- }
- }
- break;
- }
- #endif
- }
- if (hCount > 0) {
- _read_images(hList, hCount, totalClasses, unoptimizedTotalClasses);
- }
- firstTime = NO;
- }
- void
- unmap_image_nolock(const struct mach_header *mh)
- {
- if (PrintImages) {
- _objc_inform("IMAGES: processing 1 newly-unmapped image...\n");
- }
- header_info *hi;
-
-
- for (hi = FirstHeader; hi != NULL; hi = hi->getNext()) {
- if (hi->mhdr() == (const headerType *)mh) {
- break;
- }
- }
- if (!hi) return;
- if (PrintImages) {
- _objc_inform("IMAGES: unloading image for %s%s%s\n",
- hi->fname(),
- hi->mhdr()->filetype == MH_BUNDLE ? " (bundle)" : "",
- hi->info()->isReplacement() ? " (replacement)" : "");
- }
- _unload_image(hi);
-
- removeHeader(hi);
- free(hi);
- }
- static void static_init()
- {
- size_t count;
- auto inits = getLibobjcInitializers(&_mh_dylib_header, &count);
- for (size_t i = 0; i < count; i++) {
- inits[i]();
- }
- }
- #if LOCKDEBUG
- __attribute__((constructor))
- static void defineLockOrder()
- {
-
-
- lockdebug_lock_precedes_lock(&loadMethodLock, &crashlog_lock);
- lockdebug_lock_precedes_lock(&classInitLock, &crashlog_lock);
- #if __OBJC2__
- lockdebug_lock_precedes_lock(&runtimeLock, &crashlog_lock);
- lockdebug_lock_precedes_lock(&DemangleCacheLock, &crashlog_lock);
- #else
- lockdebug_lock_precedes_lock(&classLock, &crashlog_lock);
- lockdebug_lock_precedes_lock(&methodListLock, &crashlog_lock);
- lockdebug_lock_precedes_lock(&NXUniqueStringLock, &crashlog_lock);
- lockdebug_lock_precedes_lock(&impLock, &crashlog_lock);
- #endif
- lockdebug_lock_precedes_lock(&selLock, &crashlog_lock);
- lockdebug_lock_precedes_lock(&cacheUpdateLock, &crashlog_lock);
- lockdebug_lock_precedes_lock(&objcMsgLogLock, &crashlog_lock);
- lockdebug_lock_precedes_lock(&AltHandlerDebugLock, &crashlog_lock);
- lockdebug_lock_precedes_lock(&AssociationsManagerLock, &crashlog_lock);
- SideTableLocksPrecedeLock(&crashlog_lock);
- PropertyLocks.precedeLock(&crashlog_lock);
- StructLocks.precedeLock(&crashlog_lock);
- CppObjectLocks.precedeLock(&crashlog_lock);
-
-
- lockdebug_lock_precedes_lock(&loadMethodLock, &classInitLock);
- #if __OBJC2__
- lockdebug_lock_precedes_lock(&loadMethodLock, &runtimeLock);
- lockdebug_lock_precedes_lock(&loadMethodLock, &DemangleCacheLock);
- #else
- lockdebug_lock_precedes_lock(&loadMethodLock, &methodListLock);
- lockdebug_lock_precedes_lock(&loadMethodLock, &classLock);
- lockdebug_lock_precedes_lock(&loadMethodLock, &NXUniqueStringLock);
- lockdebug_lock_precedes_lock(&loadMethodLock, &impLock);
- #endif
- lockdebug_lock_precedes_lock(&loadMethodLock, &selLock);
- lockdebug_lock_precedes_lock(&loadMethodLock, &cacheUpdateLock);
- lockdebug_lock_precedes_lock(&loadMethodLock, &objcMsgLogLock);
- lockdebug_lock_precedes_lock(&loadMethodLock, &AltHandlerDebugLock);
- lockdebug_lock_precedes_lock(&loadMethodLock, &AssociationsManagerLock);
- SideTableLocksSucceedLock(&loadMethodLock);
- PropertyLocks.succeedLock(&loadMethodLock);
- StructLocks.succeedLock(&loadMethodLock);
- CppObjectLocks.succeedLock(&loadMethodLock);
-
-
-
-
- auto PropertyAndCppObjectAndAssocLocksPrecedeLock = [&](const void *lock) {
- PropertyLocks.precedeLock(lock);
- CppObjectLocks.precedeLock(lock);
- lockdebug_lock_precedes_lock(&AssociationsManagerLock, lock);
- };
- #if __OBJC2__
- PropertyAndCppObjectAndAssocLocksPrecedeLock(&runtimeLock);
- PropertyAndCppObjectAndAssocLocksPrecedeLock(&DemangleCacheLock);
- #else
- PropertyAndCppObjectAndAssocLocksPrecedeLock(&methodListLock);
- PropertyAndCppObjectAndAssocLocksPrecedeLock(&classLock);
- PropertyAndCppObjectAndAssocLocksPrecedeLock(&NXUniqueStringLock);
- PropertyAndCppObjectAndAssocLocksPrecedeLock(&impLock);
- #endif
- PropertyAndCppObjectAndAssocLocksPrecedeLock(&classInitLock);
- PropertyAndCppObjectAndAssocLocksPrecedeLock(&selLock);
- PropertyAndCppObjectAndAssocLocksPrecedeLock(&cacheUpdateLock);
- PropertyAndCppObjectAndAssocLocksPrecedeLock(&objcMsgLogLock);
- PropertyAndCppObjectAndAssocLocksPrecedeLock(&AltHandlerDebugLock);
- SideTableLocksSucceedLocks(PropertyLocks);
- SideTableLocksSucceedLocks(CppObjectLocks);
- SideTableLocksSucceedLock(&AssociationsManagerLock);
- PropertyLocks.precedeLock(&AssociationsManagerLock);
- CppObjectLocks.precedeLock(&AssociationsManagerLock);
-
- #if __OBJC2__
- lockdebug_lock_precedes_lock(&classInitLock, &runtimeLock);
- #endif
- #if __OBJC2__
-
-
- SideTableLocksPrecedeLock(&runtimeLock);
- SideTableLocksPrecedeLock(&classInitLock);
-
- lockdebug_lock_precedes_lock(&runtimeLock, &selLock);
- lockdebug_lock_precedes_lock(&runtimeLock, &cacheUpdateLock);
- lockdebug_lock_precedes_lock(&runtimeLock, &DemangleCacheLock);
- #else
-
-
- SideTableLocksPrecedeLock(&methodListLock);
- SideTableLocksPrecedeLock(&classInitLock);
-
- lockdebug_lock_precedes_lock(&methodListLock, &classLock);
- lockdebug_lock_precedes_lock(&methodListLock, &selLock);
- lockdebug_lock_precedes_lock(&methodListLock, &cacheUpdateLock);
- lockdebug_lock_precedes_lock(&methodListLock, &impLock);
- lockdebug_lock_precedes_lock(&classLock, &selLock);
- lockdebug_lock_precedes_lock(&classLock, &cacheUpdateLock);
- #endif
-
- SideTableDefineLockOrder();
- PropertyLocks.defineLockOrder();
- StructLocks.defineLockOrder();
- CppObjectLocks.defineLockOrder();
- }
- #endif
- static bool ForkIsMultithreaded;
- void _objc_atfork_prepare()
- {
-
- ForkIsMultithreaded = pthread_is_threaded_np();
- lockdebug_assert_no_locks_locked();
- lockdebug_setInForkPrepare(true);
- loadMethodLock.lock();
- PropertyLocks.lockAll();
- CppObjectLocks.lockAll();
- AssociationsManagerLock.lock();
- SideTableLockAll();
- classInitLock.enter();
- #if __OBJC2__
- runtimeLock.lock();
- DemangleCacheLock.lock();
- #else
- methodListLock.lock();
- classLock.lock();
- NXUniqueStringLock.lock();
- impLock.lock();
- #endif
- selLock.lock();
- cacheUpdateLock.lock();
- objcMsgLogLock.lock();
- AltHandlerDebugLock.lock();
- StructLocks.lockAll();
- crashlog_lock.lock();
- lockdebug_assert_all_locks_locked();
- lockdebug_setInForkPrepare(false);
- }
- void _objc_atfork_parent()
- {
- lockdebug_assert_all_locks_locked();
- CppObjectLocks.unlockAll();
- StructLocks.unlockAll();
- PropertyLocks.unlockAll();
- AssociationsManagerLock.unlock();
- AltHandlerDebugLock.unlock();
- objcMsgLogLock.unlock();
- crashlog_lock.unlock();
- loadMethodLock.unlock();
- cacheUpdateLock.unlock();
- selLock.unlock();
- SideTableUnlockAll();
- #if __OBJC2__
- DemangleCacheLock.unlock();
- runtimeLock.unlock();
- #else
- impLock.unlock();
- NXUniqueStringLock.unlock();
- methodListLock.unlock();
- classLock.unlock();
- #endif
- classInitLock.leave();
- lockdebug_assert_no_locks_locked();
- }
- void _objc_atfork_child()
- {
-
- if (ForkIsMultithreaded && !DisableInitializeForkSafety) {
- MultithreadedForkChild = true;
- }
- lockdebug_assert_all_locks_locked();
- CppObjectLocks.forceResetAll();
- StructLocks.forceResetAll();
- PropertyLocks.forceResetAll();
- AssociationsManagerLock.forceReset();
- AltHandlerDebugLock.forceReset();
- objcMsgLogLock.forceReset();
- crashlog_lock.forceReset();
- loadMethodLock.forceReset();
- cacheUpdateLock.forceReset();
- selLock.forceReset();
- SideTableForceResetAll();
- #if __OBJC2__
- DemangleCacheLock.forceReset();
- runtimeLock.forceReset();
- #else
- impLock.forceReset();
- NXUniqueStringLock.forceReset();
- methodListLock.forceReset();
- classLock.forceReset();
- #endif
- classInitLock.forceReset();
- lockdebug_assert_no_locks_locked();
- }
- void _objc_init(void)
- {
- static bool initialized = false;
- if (initialized) return;
- initialized = true;
-
-
- environ_init();
- tls_init();
- static_init();
- lock_init();
- exception_init();
- _dyld_objc_notify_register(&map_images, load_images, unmap_image);
- }
- static const header_info *_headerForAddress(void *addr)
- {
- #if __OBJC2__
- const char *segnames[] = { "__DATA", "__DATA_CONST", "__DATA_DIRTY" };
- #else
- const char *segnames[] = { "__OBJC" };
- #endif
- header_info *hi;
- for (hi = FirstHeader; hi != NULL; hi = hi->getNext()) {
- for (size_t i = 0; i < sizeof(segnames)/sizeof(segnames[0]); i++) {
- unsigned long seg_size;
- uint8_t *seg = getsegmentdata(hi->mhdr(), segnames[i], &seg_size);
- if (!seg) continue;
-
-
- if ((uint8_t *)addr >= seg && (uint8_t *)addr < seg + seg_size) {
- return hi;
- }
- }
- }
-
- return 0;
- }
- const header_info *_headerForClass(Class cls)
- {
- return _headerForAddress(cls);
- }
- int secure_open(const char *filename, int flags, uid_t euid)
- {
- struct stat fs, ls;
- int fd = -1;
- bool truncate = NO;
- bool create = NO;
- if (flags & O_TRUNC) {
-
- truncate = YES;
- flags &= ~O_TRUNC;
- }
- if (flags & O_CREAT) {
-
- create = YES;
- flags &= ~O_CREAT;
- flags &= ~O_EXCL;
- }
- if (lstat(filename, &ls) < 0) {
- if (errno == ENOENT && create) {
-
- fd = open(filename, flags | O_CREAT | O_EXCL, 0600);
- if (fd >= 0) {
-
-
- return fd;
- } else {
-
- return -1;
- }
- } else {
-
- return -1;
- }
- } else {
-
- if (S_ISREG(ls.st_mode) &&
- ls.st_nlink == 1 &&
- ls.st_uid == euid &&
- (ls.st_mode & ALLPERMS) == (S_IRUSR | S_IWUSR))
- {
-
- fd = open(filename, flags, 0000);
- if (fd >= 0) {
-
- if (0 == fstat(fd, &fs) &&
- fs.st_nlink == ls.st_nlink &&
- fs.st_uid == ls.st_uid &&
- fs.st_mode == ls.st_mode &&
- fs.st_ino == ls.st_ino &&
- fs.st_dev == ls.st_dev)
- {
-
- if (truncate) ftruncate(fd, 0);
- return fd;
- } else {
-
- close(fd);
- return -1;
- }
- } else {
-
- return -1;
- }
- } else {
-
- return -1;
- }
- }
- }
- #if TARGET_OS_IPHONE
- const char *__crashreporter_info__ = NULL;
- const char *CRSetCrashLogMessage(const char *msg)
- {
- __crashreporter_info__ = msg;
- return msg;
- }
- const char *CRGetCrashLogMessage(void)
- {
- return __crashreporter_info__;
- }
- #endif
- #else
- #error unknown OS
- #endif
|