]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-os.h
objc4-706.tar.gz
[apple/objc4.git] / runtime / objc-os.h
1 /*
2 * Copyright (c) 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
24 /***********************************************************************
25 * objc-os.h
26 * OS portability layer.
27 **********************************************************************/
28
29 #ifndef _OBJC_OS_H
30 #define _OBJC_OS_H
31
32 #include <TargetConditionals.h>
33 #include "objc-config.h"
34
35 #ifdef __LP64__
36 # define WORD_SHIFT 3UL
37 # define WORD_MASK 7UL
38 # define WORD_BITS 64
39 #else
40 # define WORD_SHIFT 2UL
41 # define WORD_MASK 3UL
42 # define WORD_BITS 32
43 #endif
44
45 static inline uint32_t word_align(uint32_t x) {
46 return (x + WORD_MASK) & ~WORD_MASK;
47 }
48 static inline size_t word_align(size_t x) {
49 return (x + WORD_MASK) & ~WORD_MASK;
50 }
51
52
53 // Mix-in for classes that must not be copied.
54 class nocopy_t {
55 private:
56 nocopy_t(const nocopy_t&) = delete;
57 const nocopy_t& operator=(const nocopy_t&) = delete;
58 protected:
59 nocopy_t() { }
60 ~nocopy_t() { }
61 };
62
63
64 #if TARGET_OS_MAC
65
66 # define OS_UNFAIR_LOCK_INLINE 1
67
68 # ifndef __STDC_LIMIT_MACROS
69 # define __STDC_LIMIT_MACROS
70 # endif
71
72 # include <stdio.h>
73 # include <stdlib.h>
74 # include <stdint.h>
75 # include <stdarg.h>
76 # include <string.h>
77 # include <ctype.h>
78 # include <errno.h>
79 # include <dlfcn.h>
80 # include <fcntl.h>
81 # include <assert.h>
82 # include <limits.h>
83 # include <syslog.h>
84 # include <unistd.h>
85 # include <pthread.h>
86 # include <crt_externs.h>
87 # undef check
88 # include <Availability.h>
89 # include <TargetConditionals.h>
90 # include <sys/mman.h>
91 # include <sys/time.h>
92 # include <sys/stat.h>
93 # include <sys/param.h>
94 # include <sys/reason.h>
95 # include <mach/mach.h>
96 # include <mach/vm_param.h>
97 # include <mach/mach_time.h>
98 # include <mach-o/dyld.h>
99 # include <mach-o/ldsyms.h>
100 # include <mach-o/loader.h>
101 # include <mach-o/getsect.h>
102 # include <mach-o/dyld_priv.h>
103 # include <malloc/malloc.h>
104 # include <os/lock_private.h>
105 # include <libkern/OSAtomic.h>
106 # include <libkern/OSCacheControl.h>
107 # include <System/pthread_machdep.h>
108 # include "objc-probes.h" // generated dtrace probe definitions.
109
110 // Some libc functions call objc_msgSend()
111 // so we can't use them without deadlocks.
112 void syslog(int, const char *, ...) UNAVAILABLE_ATTRIBUTE;
113 void vsyslog(int, const char *, va_list) UNAVAILABLE_ATTRIBUTE;
114
115
116 #define ALWAYS_INLINE inline __attribute__((always_inline))
117 #define NEVER_INLINE inline __attribute__((noinline))
118
119 #define fastpath(x) (__builtin_expect(bool(x), 1))
120 #define slowpath(x) (__builtin_expect(bool(x), 0))
121
122
123 static ALWAYS_INLINE uintptr_t
124 addc(uintptr_t lhs, uintptr_t rhs, uintptr_t carryin, uintptr_t *carryout)
125 {
126 return __builtin_addcl(lhs, rhs, carryin, carryout);
127 }
128
129 static ALWAYS_INLINE uintptr_t
130 subc(uintptr_t lhs, uintptr_t rhs, uintptr_t carryin, uintptr_t *carryout)
131 {
132 return __builtin_subcl(lhs, rhs, carryin, carryout);
133 }
134
135
136 #if __arm64__
137
138 static ALWAYS_INLINE
139 uintptr_t
140 LoadExclusive(uintptr_t *src)
141 {
142 uintptr_t result;
143 asm("ldxr %x0, [%x1]"
144 : "=r" (result)
145 : "r" (src), "m" (*src));
146 return result;
147 }
148
149 static ALWAYS_INLINE
150 bool
151 StoreExclusive(uintptr_t *dst, uintptr_t oldvalue __unused, uintptr_t value)
152 {
153 uint32_t result;
154 asm("stxr %w0, %x2, [%x3]"
155 : "=r" (result), "=m" (*dst)
156 : "r" (value), "r" (dst));
157 return !result;
158 }
159
160
161 static ALWAYS_INLINE
162 bool
163 StoreReleaseExclusive(uintptr_t *dst, uintptr_t oldvalue __unused, uintptr_t value)
164 {
165 uint32_t result;
166 asm("stlxr %w0, %x2, [%x3]"
167 : "=r" (result), "=m" (*dst)
168 : "r" (value), "r" (dst));
169 return !result;
170 }
171
172 static ALWAYS_INLINE
173 void
174 ClearExclusive(uintptr_t *dst)
175 {
176 // pretend it writes to *dst for instruction ordering purposes
177 asm("clrex" : "=m" (*dst));
178 }
179
180
181 #elif __arm__
182
183 static ALWAYS_INLINE
184 uintptr_t
185 LoadExclusive(uintptr_t *src)
186 {
187 return *src;
188 }
189
190 static ALWAYS_INLINE
191 bool
192 StoreExclusive(uintptr_t *dst, uintptr_t oldvalue, uintptr_t value)
193 {
194 return OSAtomicCompareAndSwapPtr((void *)oldvalue, (void *)value,
195 (void **)dst);
196 }
197
198 static ALWAYS_INLINE
199 bool
200 StoreReleaseExclusive(uintptr_t *dst, uintptr_t oldvalue, uintptr_t value)
201 {
202 return OSAtomicCompareAndSwapPtrBarrier((void *)oldvalue, (void *)value,
203 (void **)dst);
204 }
205
206 static ALWAYS_INLINE
207 void
208 ClearExclusive(uintptr_t *dst __unused)
209 {
210 }
211
212
213 #elif __x86_64__ || __i386__
214
215 static ALWAYS_INLINE
216 uintptr_t
217 LoadExclusive(uintptr_t *src)
218 {
219 return *src;
220 }
221
222 static ALWAYS_INLINE
223 bool
224 StoreExclusive(uintptr_t *dst, uintptr_t oldvalue, uintptr_t value)
225 {
226
227 return __sync_bool_compare_and_swap((void **)dst, (void *)oldvalue, (void *)value);
228 }
229
230 static ALWAYS_INLINE
231 bool
232 StoreReleaseExclusive(uintptr_t *dst, uintptr_t oldvalue, uintptr_t value)
233 {
234 return StoreExclusive(dst, oldvalue, value);
235 }
236
237 static ALWAYS_INLINE
238 void
239 ClearExclusive(uintptr_t *dst __unused)
240 {
241 }
242
243
244 #else
245 # error unknown architecture
246 #endif
247
248
249 #if !TARGET_OS_IPHONE
250 # include <CrashReporterClient.h>
251 #else
252 // CrashReporterClient not yet available on iOS
253 __BEGIN_DECLS
254 extern const char *CRSetCrashLogMessage(const char *msg);
255 extern const char *CRGetCrashLogMessage(void);
256 __END_DECLS
257 #endif
258
259 # if __cplusplus
260 # include <vector>
261 # include <algorithm>
262 # include <functional>
263 using namespace std;
264 # endif
265
266 # define PRIVATE_EXTERN __attribute__((visibility("hidden")))
267 # undef __private_extern__
268 # define __private_extern__ use_PRIVATE_EXTERN_instead
269 # undef private_extern
270 # define private_extern use_PRIVATE_EXTERN_instead
271
272 /* Use this for functions that are intended to be breakpoint hooks.
273 If you do not, the compiler may optimize them away.
274 BREAKPOINT_FUNCTION( void stop_on_error(void) ); */
275 # define BREAKPOINT_FUNCTION(prototype) \
276 OBJC_EXTERN __attribute__((noinline, used, visibility("hidden"))) \
277 prototype { asm(""); }
278
279 #elif TARGET_OS_WIN32
280
281 # define WINVER 0x0501 // target Windows XP and later
282 # define _WIN32_WINNT 0x0501 // target Windows XP and later
283 # define WIN32_LEAN_AND_MEAN
284 // hack: windef.h typedefs BOOL as int
285 # define BOOL WINBOOL
286 # include <windows.h>
287 # undef BOOL
288
289 # include <stdio.h>
290 # include <stdlib.h>
291 # include <stdint.h>
292 # include <stdarg.h>
293 # include <string.h>
294 # include <assert.h>
295 # include <malloc.h>
296 # include <Availability.h>
297
298 # if __cplusplus
299 # include <vector>
300 # include <algorithm>
301 # include <functional>
302 using namespace std;
303 # define __BEGIN_DECLS extern "C" {
304 # define __END_DECLS }
305 # else
306 # define __BEGIN_DECLS /*empty*/
307 # define __END_DECLS /*empty*/
308 # endif
309
310 # define PRIVATE_EXTERN
311 # define __attribute__(x)
312 # define inline __inline
313
314 /* Use this for functions that are intended to be breakpoint hooks.
315 If you do not, the compiler may optimize them away.
316 BREAKPOINT_FUNCTION( void MyBreakpointFunction(void) ); */
317 # define BREAKPOINT_FUNCTION(prototype) \
318 __declspec(noinline) prototype { __asm { } }
319
320 /* stub out dtrace probes */
321 # define OBJC_RUNTIME_OBJC_EXCEPTION_RETHROW() do {} while(0)
322 # define OBJC_RUNTIME_OBJC_EXCEPTION_THROW(arg0) do {} while(0)
323
324 #else
325 # error unknown OS
326 #endif
327
328
329 #include <objc/objc.h>
330 #include <objc/objc-api.h>
331
332 extern void _objc_fatal(const char *fmt, ...)
333 __attribute__((noreturn, format (printf, 1, 2)));
334 extern void _objc_fatal_with_reason(uint64_t reason, uint64_t flags,
335 const char *fmt, ...)
336 __attribute__((noreturn, format (printf, 3, 4)));
337
338 #define INIT_ONCE_PTR(var, create, delete) \
339 do { \
340 if (var) break; \
341 typeof(var) v = create; \
342 while (!var) { \
343 if (OSAtomicCompareAndSwapPtrBarrier(0, (void*)v, (void**)&var)){ \
344 goto done; \
345 } \
346 } \
347 delete; \
348 done:; \
349 } while (0)
350
351 #define INIT_ONCE_32(var, create, delete) \
352 do { \
353 if (var) break; \
354 typeof(var) v = create; \
355 while (!var) { \
356 if (OSAtomicCompareAndSwap32Barrier(0, v, (volatile int32_t *)&var)) { \
357 goto done; \
358 } \
359 } \
360 delete; \
361 done:; \
362 } while (0)
363
364
365 // Thread keys reserved by libc for our use.
366 #if defined(__PTK_FRAMEWORK_OBJC_KEY0)
367 # define SUPPORT_DIRECT_THREAD_KEYS 1
368 # define TLS_DIRECT_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY0)
369 # define SYNC_DATA_DIRECT_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY1)
370 # define SYNC_COUNT_DIRECT_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY2)
371 # define AUTORELEASE_POOL_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY3)
372 # if SUPPORT_RETURN_AUTORELEASE
373 # define RETURN_DISPOSITION_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY4)
374 # endif
375 # if SUPPORT_QOS_HACK
376 # define QOS_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY5)
377 # endif
378 #else
379 # define SUPPORT_DIRECT_THREAD_KEYS 0
380 #endif
381
382
383 #if TARGET_OS_WIN32
384
385 // Compiler compatibility
386
387 // OS compatibility
388
389 #define strdup _strdup
390
391 #define issetugid() 0
392
393 #define MIN(x, y) ((x) < (y) ? (x) : (y))
394
395 static __inline void bcopy(const void *src, void *dst, size_t size) { memcpy(dst, src, size); }
396 static __inline void bzero(void *dst, size_t size) { memset(dst, 0, size); }
397
398 int asprintf(char **dstp, const char *format, ...);
399
400 typedef void * malloc_zone_t;
401
402 static __inline malloc_zone_t malloc_default_zone(void) { return (malloc_zone_t)-1; }
403 static __inline void *malloc_zone_malloc(malloc_zone_t z, size_t size) { return malloc(size); }
404 static __inline void *malloc_zone_calloc(malloc_zone_t z, size_t size, size_t count) { return calloc(size, count); }
405 static __inline void *malloc_zone_realloc(malloc_zone_t z, void *p, size_t size) { return realloc(p, size); }
406 static __inline void malloc_zone_free(malloc_zone_t z, void *p) { free(p); }
407 static __inline malloc_zone_t malloc_zone_from_ptr(const void *p) { return (malloc_zone_t)-1; }
408 static __inline size_t malloc_size(const void *p) { return _msize((void*)p); /* fixme invalid pointer check? */ }
409
410
411 // OSAtomic
412
413 static __inline BOOL OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst)
414 {
415 // fixme barrier is overkill
416 long original = InterlockedCompareExchange(dst, newl, oldl);
417 return (original == oldl);
418 }
419
420 static __inline BOOL OSAtomicCompareAndSwapPtrBarrier(void *oldp, void *newp, void * volatile *dst)
421 {
422 void *original = InterlockedCompareExchangePointer(dst, newp, oldp);
423 return (original == oldp);
424 }
425
426 static __inline BOOL OSAtomicCompareAndSwap32Barrier(int32_t oldl, int32_t newl, int32_t volatile *dst)
427 {
428 long original = InterlockedCompareExchange((volatile long *)dst, newl, oldl);
429 return (original == oldl);
430 }
431
432 static __inline int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst)
433 {
434 return InterlockedDecrement((volatile long *)dst);
435 }
436
437 static __inline int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst)
438 {
439 return InterlockedIncrement((volatile long *)dst);
440 }
441
442
443 // Internal data types
444
445 typedef DWORD objc_thread_t; // thread ID
446 static __inline int thread_equal(objc_thread_t t1, objc_thread_t t2) {
447 return t1 == t2;
448 }
449 static __inline objc_thread_t thread_self(void) {
450 return GetCurrentThreadId();
451 }
452
453 typedef struct {
454 DWORD key;
455 void (*dtor)(void *);
456 } tls_key_t;
457 static __inline tls_key_t tls_create(void (*dtor)(void*)) {
458 // fixme need dtor registry for DllMain to call on thread detach
459 tls_key_t k;
460 k.key = TlsAlloc();
461 k.dtor = dtor;
462 return k;
463 }
464 static __inline void *tls_get(tls_key_t k) {
465 return TlsGetValue(k.key);
466 }
467 static __inline void tls_set(tls_key_t k, void *value) {
468 TlsSetValue(k.key, value);
469 }
470
471 typedef struct {
472 CRITICAL_SECTION *lock;
473 } mutex_t;
474 #define MUTEX_INITIALIZER {0};
475 extern void mutex_init(mutex_t *m);
476 static __inline int _mutex_lock_nodebug(mutex_t *m) {
477 // fixme error check
478 if (!m->lock) {
479 mutex_init(m);
480 }
481 EnterCriticalSection(m->lock);
482 return 0;
483 }
484 static __inline bool _mutex_try_lock_nodebug(mutex_t *m) {
485 // fixme error check
486 if (!m->lock) {
487 mutex_init(m);
488 }
489 return TryEnterCriticalSection(m->lock);
490 }
491 static __inline int _mutex_unlock_nodebug(mutex_t *m) {
492 // fixme error check
493 LeaveCriticalSection(m->lock);
494 return 0;
495 }
496
497
498 typedef mutex_t spinlock_t;
499 #define spinlock_lock(l) mutex_lock(l)
500 #define spinlock_unlock(l) mutex_unlock(l)
501 #define SPINLOCK_INITIALIZER MUTEX_INITIALIZER
502
503
504 typedef struct {
505 HANDLE mutex;
506 } recursive_mutex_t;
507 #define RECURSIVE_MUTEX_INITIALIZER {0};
508 #define RECURSIVE_MUTEX_NOT_LOCKED 1
509 extern void recursive_mutex_init(recursive_mutex_t *m);
510 static __inline int _recursive_mutex_lock_nodebug(recursive_mutex_t *m) {
511 assert(m->mutex);
512 return WaitForSingleObject(m->mutex, INFINITE);
513 }
514 static __inline bool _recursive_mutex_try_lock_nodebug(recursive_mutex_t *m) {
515 assert(m->mutex);
516 return (WAIT_OBJECT_0 == WaitForSingleObject(m->mutex, 0));
517 }
518 static __inline int _recursive_mutex_unlock_nodebug(recursive_mutex_t *m) {
519 assert(m->mutex);
520 return ReleaseMutex(m->mutex) ? 0 : RECURSIVE_MUTEX_NOT_LOCKED;
521 }
522
523
524 /*
525 typedef HANDLE mutex_t;
526 static inline void mutex_init(HANDLE *m) { *m = CreateMutex(NULL, FALSE, NULL); }
527 static inline void _mutex_lock(mutex_t *m) { WaitForSingleObject(*m, INFINITE); }
528 static inline bool mutex_try_lock(mutex_t *m) { return WaitForSingleObject(*m, 0) == WAIT_OBJECT_0; }
529 static inline void _mutex_unlock(mutex_t *m) { ReleaseMutex(*m); }
530 */
531
532 // based on http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
533 // Vista-only CONDITION_VARIABLE would be better
534 typedef struct {
535 HANDLE mutex;
536 HANDLE waiters; // semaphore for those in cond_wait()
537 HANDLE waitersDone; // auto-reset event after everyone gets a broadcast
538 CRITICAL_SECTION waitCountLock; // guards waitCount and didBroadcast
539 unsigned int waitCount;
540 int didBroadcast;
541 } monitor_t;
542 #define MONITOR_INITIALIZER { 0 }
543 #define MONITOR_NOT_ENTERED 1
544 extern int monitor_init(monitor_t *c);
545
546 static inline int _monitor_enter_nodebug(monitor_t *c) {
547 if (!c->mutex) {
548 int err = monitor_init(c);
549 if (err) return err;
550 }
551 return WaitForSingleObject(c->mutex, INFINITE);
552 }
553 static inline int _monitor_leave_nodebug(monitor_t *c) {
554 if (!ReleaseMutex(c->mutex)) return MONITOR_NOT_ENTERED;
555 else return 0;
556 }
557 static inline int _monitor_wait_nodebug(monitor_t *c) {
558 int last;
559 EnterCriticalSection(&c->waitCountLock);
560 c->waitCount++;
561 LeaveCriticalSection(&c->waitCountLock);
562
563 SignalObjectAndWait(c->mutex, c->waiters, INFINITE, FALSE);
564
565 EnterCriticalSection(&c->waitCountLock);
566 c->waitCount--;
567 last = c->didBroadcast && c->waitCount == 0;
568 LeaveCriticalSection(&c->waitCountLock);
569
570 if (last) {
571 // tell broadcaster that all waiters have awoken
572 SignalObjectAndWait(c->waitersDone, c->mutex, INFINITE, FALSE);
573 } else {
574 WaitForSingleObject(c->mutex, INFINITE);
575 }
576
577 // fixme error checking
578 return 0;
579 }
580 static inline int monitor_notify(monitor_t *c) {
581 int haveWaiters;
582
583 EnterCriticalSection(&c->waitCountLock);
584 haveWaiters = c->waitCount > 0;
585 LeaveCriticalSection(&c->waitCountLock);
586
587 if (haveWaiters) {
588 ReleaseSemaphore(c->waiters, 1, 0);
589 }
590
591 // fixme error checking
592 return 0;
593 }
594 static inline int monitor_notifyAll(monitor_t *c) {
595 EnterCriticalSection(&c->waitCountLock);
596 if (c->waitCount == 0) {
597 LeaveCriticalSection(&c->waitCountLock);
598 return 0;
599 }
600 c->didBroadcast = 1;
601 ReleaseSemaphore(c->waiters, c->waitCount, 0);
602 LeaveCriticalSection(&c->waitCountLock);
603
604 // fairness: wait for everyone to move from waiters to mutex
605 WaitForSingleObject(c->waitersDone, INFINITE);
606 // not under waitCountLock, but still under mutex
607 c->didBroadcast = 0;
608
609 // fixme error checking
610 return 0;
611 }
612
613
614 // fixme no rwlock yet
615
616
617 typedef IMAGE_DOS_HEADER headerType;
618 // fixme YES bundle? NO bundle? sometimes?
619 #define headerIsBundle(hi) YES
620 OBJC_EXTERN IMAGE_DOS_HEADER __ImageBase;
621 #define libobjc_header ((headerType *)&__ImageBase)
622
623 // Prototypes
624
625
626 #elif TARGET_OS_MAC
627
628
629 // OS headers
630 #include <mach-o/loader.h>
631 #ifndef __LP64__
632 # define SEGMENT_CMD LC_SEGMENT
633 #else
634 # define SEGMENT_CMD LC_SEGMENT_64
635 #endif
636
637 #ifndef VM_MEMORY_OBJC_DISPATCHERS
638 # define VM_MEMORY_OBJC_DISPATCHERS 0
639 #endif
640
641
642 // Compiler compatibility
643
644 // OS compatibility
645
646 static inline uint64_t nanoseconds() {
647 return mach_absolute_time();
648 }
649
650 // Internal data types
651
652 typedef pthread_t objc_thread_t;
653
654 static __inline int thread_equal(objc_thread_t t1, objc_thread_t t2) {
655 return pthread_equal(t1, t2);
656 }
657 static __inline objc_thread_t thread_self(void) {
658 return pthread_self();
659 }
660
661
662 typedef pthread_key_t tls_key_t;
663
664 static inline tls_key_t tls_create(void (*dtor)(void*)) {
665 tls_key_t k;
666 pthread_key_create(&k, dtor);
667 return k;
668 }
669 static inline void *tls_get(tls_key_t k) {
670 return pthread_getspecific(k);
671 }
672 static inline void tls_set(tls_key_t k, void *value) {
673 pthread_setspecific(k, value);
674 }
675
676 #if SUPPORT_DIRECT_THREAD_KEYS
677
678 #if DEBUG
679 static bool is_valid_direct_key(tls_key_t k) {
680 return ( k == SYNC_DATA_DIRECT_KEY
681 || k == SYNC_COUNT_DIRECT_KEY
682 || k == AUTORELEASE_POOL_KEY
683 # if SUPPORT_RETURN_AUTORELEASE
684 || k == RETURN_DISPOSITION_KEY
685 # endif
686 # if SUPPORT_QOS_HACK
687 || k == QOS_KEY
688 # endif
689 );
690 }
691 #endif
692
693 #if __arm__
694
695 // rdar://9162780 _pthread_get/setspecific_direct are inefficient
696 // copied from libdispatch
697
698 __attribute__((const))
699 static ALWAYS_INLINE void**
700 tls_base(void)
701 {
702 uintptr_t p;
703 #if defined(__arm__) && defined(_ARM_ARCH_6)
704 __asm__("mrc p15, 0, %[p], c13, c0, 3" : [p] "=&r" (p));
705 return (void**)(p & ~0x3ul);
706 #else
707 #error tls_base not implemented
708 #endif
709 }
710
711
712 static ALWAYS_INLINE void
713 tls_set_direct(void **tsdb, tls_key_t k, void *v)
714 {
715 assert(is_valid_direct_key(k));
716
717 tsdb[k] = v;
718 }
719 #define tls_set_direct(k, v) \
720 tls_set_direct(tls_base(), (k), (v))
721
722
723 static ALWAYS_INLINE void *
724 tls_get_direct(void **tsdb, tls_key_t k)
725 {
726 assert(is_valid_direct_key(k));
727
728 return tsdb[k];
729 }
730 #define tls_get_direct(k) \
731 tls_get_direct(tls_base(), (k))
732
733 // arm
734 #else
735 // not arm
736
737 static inline void *tls_get_direct(tls_key_t k)
738 {
739 assert(is_valid_direct_key(k));
740
741 if (_pthread_has_direct_tsd()) {
742 return _pthread_getspecific_direct(k);
743 } else {
744 return pthread_getspecific(k);
745 }
746 }
747 static inline void tls_set_direct(tls_key_t k, void *value)
748 {
749 assert(is_valid_direct_key(k));
750
751 if (_pthread_has_direct_tsd()) {
752 _pthread_setspecific_direct(k, value);
753 } else {
754 pthread_setspecific(k, value);
755 }
756 }
757
758 // not arm
759 #endif
760
761 // SUPPORT_DIRECT_THREAD_KEYS
762 #endif
763
764
765 static inline pthread_t pthread_self_direct()
766 {
767 return (pthread_t)
768 _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_SELF);
769 }
770
771 static inline mach_port_t mach_thread_self_direct()
772 {
773 return (mach_port_t)(uintptr_t)
774 _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_MACH_THREAD_SELF);
775 }
776
777 #if SUPPORT_QOS_HACK
778 static inline pthread_priority_t pthread_self_priority_direct()
779 {
780 pthread_priority_t pri = (pthread_priority_t)
781 _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS);
782 return pri & ~_PTHREAD_PRIORITY_FLAGS_MASK;
783 }
784 #endif
785
786
787 template <bool Debug> class mutex_tt;
788 template <bool Debug> class monitor_tt;
789 template <bool Debug> class rwlock_tt;
790 template <bool Debug> class recursive_mutex_tt;
791
792 using spinlock_t = mutex_tt<DEBUG>;
793 using mutex_t = mutex_tt<DEBUG>;
794 using monitor_t = monitor_tt<DEBUG>;
795 using rwlock_t = rwlock_tt<DEBUG>;
796 using recursive_mutex_t = recursive_mutex_tt<DEBUG>;
797
798 #include "objc-lockdebug.h"
799
800 template <bool Debug>
801 class mutex_tt : nocopy_t {
802 os_unfair_lock mLock;
803 public:
804 mutex_tt() : mLock(OS_UNFAIR_LOCK_INIT) { }
805
806 void lock() {
807 lockdebug_mutex_lock(this);
808
809 os_unfair_lock_lock_with_options_inline
810 (&mLock, OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION);
811 }
812
813 void unlock() {
814 lockdebug_mutex_unlock(this);
815
816 os_unfair_lock_unlock_inline(&mLock);
817 }
818
819 void assertLocked() {
820 lockdebug_mutex_assert_locked(this);
821 }
822
823 void assertUnlocked() {
824 lockdebug_mutex_assert_unlocked(this);
825 }
826
827
828 // Address-ordered lock discipline for a pair of locks.
829
830 static void lockTwo(mutex_tt *lock1, mutex_tt *lock2) {
831 if (lock1 > lock2) {
832 lock1->lock();
833 lock2->lock();
834 } else {
835 lock2->lock();
836 if (lock2 != lock1) lock1->lock();
837 }
838 }
839
840 static void unlockTwo(mutex_tt *lock1, mutex_tt *lock2) {
841 lock1->unlock();
842 if (lock2 != lock1) lock2->unlock();
843 }
844 };
845
846
847 template <bool Debug>
848 class recursive_mutex_tt : nocopy_t {
849 pthread_mutex_t mLock;
850
851 public:
852 recursive_mutex_tt() : mLock(PTHREAD_RECURSIVE_MUTEX_INITIALIZER) { }
853
854 void lock()
855 {
856 lockdebug_recursive_mutex_lock(this);
857
858 int err = pthread_mutex_lock(&mLock);
859 if (err) _objc_fatal("pthread_mutex_lock failed (%d)", err);
860 }
861
862 void unlock()
863 {
864 lockdebug_recursive_mutex_unlock(this);
865
866 int err = pthread_mutex_unlock(&mLock);
867 if (err) _objc_fatal("pthread_mutex_unlock failed (%d)", err);
868 }
869
870 bool tryUnlock()
871 {
872 int err = pthread_mutex_unlock(&mLock);
873 if (err == 0) {
874 lockdebug_recursive_mutex_unlock(this);
875 return true;
876 } else if (err == EPERM) {
877 return false;
878 } else {
879 _objc_fatal("pthread_mutex_unlock failed (%d)", err);
880 }
881 }
882
883
884 void assertLocked() {
885 lockdebug_recursive_mutex_assert_locked(this);
886 }
887
888 void assertUnlocked() {
889 lockdebug_recursive_mutex_assert_unlocked(this);
890 }
891 };
892
893
894 template <bool Debug>
895 class monitor_tt {
896 pthread_mutex_t mutex;
897 pthread_cond_t cond;
898
899 public:
900 monitor_tt()
901 : mutex(PTHREAD_MUTEX_INITIALIZER), cond(PTHREAD_COND_INITIALIZER) { }
902
903 void enter()
904 {
905 lockdebug_monitor_enter(this);
906
907 int err = pthread_mutex_lock(&mutex);
908 if (err) _objc_fatal("pthread_mutex_lock failed (%d)", err);
909 }
910
911 void leave()
912 {
913 lockdebug_monitor_leave(this);
914
915 int err = pthread_mutex_unlock(&mutex);
916 if (err) _objc_fatal("pthread_mutex_unlock failed (%d)", err);
917 }
918
919 void wait()
920 {
921 lockdebug_monitor_wait(this);
922
923 int err = pthread_cond_wait(&cond, &mutex);
924 if (err) _objc_fatal("pthread_cond_wait failed (%d)", err);
925 }
926
927 void notify()
928 {
929 int err = pthread_cond_signal(&cond);
930 if (err) _objc_fatal("pthread_cond_signal failed (%d)", err);
931 }
932
933 void notifyAll()
934 {
935 int err = pthread_cond_broadcast(&cond);
936 if (err) _objc_fatal("pthread_cond_broadcast failed (%d)", err);
937 }
938
939 void assertLocked()
940 {
941 lockdebug_monitor_assert_locked(this);
942 }
943
944 void assertUnlocked()
945 {
946 lockdebug_monitor_assert_unlocked(this);
947 }
948 };
949
950
951 // semaphore_create formatted for INIT_ONCE use
952 static inline semaphore_t create_semaphore(void)
953 {
954 semaphore_t sem;
955 kern_return_t k;
956 k = semaphore_create(mach_task_self(), &sem, SYNC_POLICY_FIFO, 0);
957 if (k) _objc_fatal("semaphore_create failed (0x%x)", k);
958 return sem;
959 }
960
961
962 #if SUPPORT_QOS_HACK
963 // Override QOS class to avoid priority inversion in rwlocks
964 // <rdar://17697862> do a qos override before taking rw lock in objc
965
966 #include <pthread/workqueue_private.h>
967 extern pthread_priority_t BackgroundPriority;
968 extern pthread_priority_t MainPriority;
969
970 static inline void qosStartOverride()
971 {
972 uintptr_t overrideRefCount = (uintptr_t)tls_get_direct(QOS_KEY);
973 if (overrideRefCount > 0) {
974 // If there is a qos override, increment the refcount and continue
975 tls_set_direct(QOS_KEY, (void *)(overrideRefCount + 1));
976 }
977 else {
978 pthread_priority_t currentPriority = pthread_self_priority_direct();
979 // Check if override is needed. Only override if we are background qos
980 if (currentPriority != 0 && currentPriority <= BackgroundPriority) {
981 int res __unused = _pthread_override_qos_class_start_direct(mach_thread_self_direct(), MainPriority);
982 assert(res == 0);
983 // Once we override, we set the reference count in the tsd
984 // to know when to end the override
985 tls_set_direct(QOS_KEY, (void *)1);
986 }
987 }
988 }
989
990 static inline void qosEndOverride()
991 {
992 uintptr_t overrideRefCount = (uintptr_t)tls_get_direct(QOS_KEY);
993 if (overrideRefCount == 0) return;
994
995 if (overrideRefCount == 1) {
996 // end the override
997 int res __unused = _pthread_override_qos_class_end_direct(mach_thread_self_direct());
998 assert(res == 0);
999 }
1000
1001 // decrement refcount
1002 tls_set_direct(QOS_KEY, (void *)(overrideRefCount - 1));
1003 }
1004
1005 // SUPPORT_QOS_HACK
1006 #else
1007 // not SUPPORT_QOS_HACK
1008
1009 static inline void qosStartOverride() { }
1010 static inline void qosEndOverride() { }
1011
1012 // not SUPPORT_QOS_HACK
1013 #endif
1014
1015
1016 template <bool Debug>
1017 class rwlock_tt : nocopy_t {
1018 pthread_rwlock_t mLock;
1019
1020 public:
1021 rwlock_tt() : mLock(PTHREAD_RWLOCK_INITIALIZER) { }
1022
1023 void read()
1024 {
1025 lockdebug_rwlock_read(this);
1026
1027 qosStartOverride();
1028 int err = pthread_rwlock_rdlock(&mLock);
1029 if (err) _objc_fatal("pthread_rwlock_rdlock failed (%d)", err);
1030 }
1031
1032 void unlockRead()
1033 {
1034 lockdebug_rwlock_unlock_read(this);
1035
1036 int err = pthread_rwlock_unlock(&mLock);
1037 if (err) _objc_fatal("pthread_rwlock_unlock failed (%d)", err);
1038 qosEndOverride();
1039 }
1040
1041 bool tryRead()
1042 {
1043 qosStartOverride();
1044 int err = pthread_rwlock_tryrdlock(&mLock);
1045 if (err == 0) {
1046 lockdebug_rwlock_try_read_success(this);
1047 return true;
1048 } else if (err == EBUSY) {
1049 qosEndOverride();
1050 return false;
1051 } else {
1052 _objc_fatal("pthread_rwlock_tryrdlock failed (%d)", err);
1053 }
1054 }
1055
1056 void write()
1057 {
1058 lockdebug_rwlock_write(this);
1059
1060 qosStartOverride();
1061 int err = pthread_rwlock_wrlock(&mLock);
1062 if (err) _objc_fatal("pthread_rwlock_wrlock failed (%d)", err);
1063 }
1064
1065 void unlockWrite()
1066 {
1067 lockdebug_rwlock_unlock_write(this);
1068
1069 int err = pthread_rwlock_unlock(&mLock);
1070 if (err) _objc_fatal("pthread_rwlock_unlock failed (%d)", err);
1071 qosEndOverride();
1072 }
1073
1074 bool tryWrite()
1075 {
1076 qosStartOverride();
1077 int err = pthread_rwlock_trywrlock(&mLock);
1078 if (err == 0) {
1079 lockdebug_rwlock_try_write_success(this);
1080 return true;
1081 } else if (err == EBUSY) {
1082 qosEndOverride();
1083 return false;
1084 } else {
1085 _objc_fatal("pthread_rwlock_trywrlock failed (%d)", err);
1086 }
1087 }
1088
1089
1090 void assertReading() {
1091 lockdebug_rwlock_assert_reading(this);
1092 }
1093
1094 void assertWriting() {
1095 lockdebug_rwlock_assert_writing(this);
1096 }
1097
1098 void assertLocked() {
1099 lockdebug_rwlock_assert_locked(this);
1100 }
1101
1102 void assertUnlocked() {
1103 lockdebug_rwlock_assert_unlocked(this);
1104 }
1105 };
1106
1107
1108 #ifndef __LP64__
1109 typedef struct mach_header headerType;
1110 typedef struct segment_command segmentType;
1111 typedef struct section sectionType;
1112 #else
1113 typedef struct mach_header_64 headerType;
1114 typedef struct segment_command_64 segmentType;
1115 typedef struct section_64 sectionType;
1116 #endif
1117 #define headerIsBundle(hi) (hi->mhdr()->filetype == MH_BUNDLE)
1118 #define libobjc_header ((headerType *)&_mh_dylib_header)
1119
1120 // Prototypes
1121
1122 /* Secure /tmp usage */
1123 extern int secure_open(const char *filename, int flags, uid_t euid);
1124
1125
1126 #else
1127
1128
1129 #error unknown OS
1130
1131
1132 #endif
1133
1134
1135 static inline void *
1136 memdup(const void *mem, size_t len)
1137 {
1138 void *dup = malloc(len);
1139 memcpy(dup, mem, len);
1140 return dup;
1141 }
1142
1143 // strdup that doesn't copy read-only memory
1144 static inline char *
1145 strdupIfMutable(const char *str)
1146 {
1147 size_t size = strlen(str) + 1;
1148 if (_dyld_is_memory_immutable(str, size)) {
1149 return (char *)str;
1150 } else {
1151 return (char *)memdup(str, size);
1152 }
1153 }
1154
1155 // free strdupIfMutable() result
1156 static inline void
1157 freeIfMutable(char *str)
1158 {
1159 size_t size = strlen(str) + 1;
1160 if (_dyld_is_memory_immutable(str, size)) {
1161 // nothing
1162 } else {
1163 free(str);
1164 }
1165 }
1166
1167 // nil-checking unsigned strdup
1168 static inline uint8_t *
1169 ustrdupMaybeNil(const uint8_t *str)
1170 {
1171 if (!str) return nil;
1172 return (uint8_t *)strdupIfMutable((char *)str);
1173 }
1174
1175 // OS version checking:
1176 //
1177 // sdkVersion()
1178 // DYLD_OS_VERSION(mac, ios, tv, watch)
1179 // sdkIsOlderThan(mac, ios, tv, watch)
1180 // sdkIsAtLeast(mac, ios, tv, watch)
1181 //
1182 // This version order matches OBJC_AVAILABLE.
1183
1184 #if TARGET_OS_OSX
1185 # define DYLD_OS_VERSION(x, i, t, w) DYLD_MACOSX_VERSION_##x
1186 # define sdkVersion() dyld_get_program_sdk_version()
1187
1188 #elif TARGET_OS_IOS
1189 # define DYLD_OS_VERSION(x, i, t, w) DYLD_IOS_VERSION_##i
1190 # define sdkVersion() dyld_get_program_sdk_version()
1191
1192 #elif TARGET_OS_TV
1193 // dyld does not currently have distinct constants for tvOS
1194 # define DYLD_OS_VERSION(x, i, t, w) DYLD_IOS_VERSION_##t
1195 # define sdkVersion() dyld_get_program_sdk_version()
1196
1197 #elif TARGET_OS_WATCH
1198 # define DYLD_OS_VERSION(x, i, t, w) DYLD_WATCHOS_VERSION_##w
1199 // watchOS has its own API for compatibility reasons
1200 # define sdkVersion() dyld_get_program_sdk_watch_os_version()
1201
1202 #else
1203 # error unknown OS
1204 #endif
1205
1206
1207 #define sdkIsOlderThan(x, i, t, w) \
1208 (sdkVersion() < DYLD_OS_VERSION(x, i, t, w))
1209 #define sdkIsAtLeast(x, i, t, w) \
1210 (sdkVersion() >= DYLD_OS_VERSION(x, i, t, w))
1211
1212 // Allow bare 0 to be used in DYLD_OS_VERSION() and sdkIsOlderThan()
1213 #define DYLD_MACOSX_VERSION_0 0
1214 #define DYLD_IOS_VERSION_0 0
1215 #define DYLD_TVOS_VERSION_0 0
1216 #define DYLD_WATCHOS_VERSION_0 0
1217
1218 // Pretty-print a DYLD_*_VERSION_* constant.
1219 #define SDK_FORMAT "%hu.%hhu.%hhu"
1220 #define FORMAT_SDK(v) \
1221 (unsigned short)(((uint32_t)(v))>>16), \
1222 (unsigned char)(((uint32_t)(v))>>8), \
1223 (unsigned char)(((uint32_t)(v))>>0)
1224
1225 #endif