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 **********************************************************************/
32 #include <TargetConditionals.h>
33 #include "objc-config.h"
36 # define WORD_SHIFT 3UL
37 # define WORD_MASK 7UL
39 # define WORD_SHIFT 2UL
40 # define WORD_MASK 3UL
45 # ifndef __STDC_LIMIT_MACROS
46 # define __STDC_LIMIT_MACROS
63 # include <crt_externs.h>
64 # include <AssertMacros.h>
66 # include <Availability.h>
67 # include <TargetConditionals.h>
68 # include <sys/mman.h>
69 # include <sys/time.h>
70 # include <sys/stat.h>
71 # include <sys/param.h>
72 # include <mach/mach.h>
73 # include <mach/vm_param.h>
74 # include <mach-o/dyld.h>
75 # include <mach-o/ldsyms.h>
76 # include <mach-o/loader.h>
77 # include <mach-o/getsect.h>
78 # include <mach-o/dyld_priv.h>
79 # include <malloc/malloc.h>
80 # include <os/lock_private.h>
81 # include <libkern/OSAtomic.h>
82 # include <libkern/OSCacheControl.h>
83 # include <System/pthread_machdep.h>
84 # include "objc-probes.h" // generated dtrace probe definitions.
86 // Some libc functions call objc_msgSend()
87 // so we can't use them without deadlocks.
88 void syslog(int, const char *, ...) UNAVAILABLE_ATTRIBUTE
;
89 void vsyslog(int, const char *, va_list) UNAVAILABLE_ATTRIBUTE
;
92 #define spinlock_t os_lock_handoff_s
93 #define spinlock_trylock(l) os_lock_trylock(l)
94 #define spinlock_lock(l) os_lock_lock(l)
95 #define spinlock_unlock(l) os_lock_unlock(l)
96 #define SPINLOCK_INITIALIZER OS_LOCK_HANDOFF_INIT
100 # include <CrashReporterClient.h>
102 // CrashReporterClient not yet available on iOS
104 extern const char *CRSetCrashLogMessage(const char *msg
);
105 extern const char *CRGetCrashLogMessage(void);
106 extern const char *CRSetCrashLogMessage2(const char *msg
);
112 # include <algorithm>
113 # include <functional>
117 # define PRIVATE_EXTERN __attribute__((visibility("hidden")))
118 # undef __private_extern__
119 # define __private_extern__ use_PRIVATE_EXTERN_instead
120 # undef private_extern
121 # define private_extern use_PRIVATE_EXTERN_instead
123 /* Use this for functions that are intended to be breakpoint hooks.
124 If you do not, the compiler may optimize them away.
125 BREAKPOINT_FUNCTION( void stop_on_error(void) ); */
126 # define BREAKPOINT_FUNCTION(prototype) \
127 OBJC_EXTERN __attribute__((noinline, visibility("hidden"))) \
128 prototype { asm(""); }
130 #elif TARGET_OS_WIN32
132 # define WINVER 0x0501 // target Windows XP and later
133 # define _WIN32_WINNT 0x0501 // target Windows XP and later
134 # define WIN32_LEAN_AND_MEAN
135 // hack: windef.h typedefs BOOL as int
136 # define BOOL WINBOOL
137 # include <windows.h>
147 # include <Availability.h>
151 # include <algorithm>
152 # include <functional>
154 # define __BEGIN_DECLS extern "C" {
155 # define __END_DECLS }
157 # define __BEGIN_DECLS /*empty*/
158 # define __END_DECLS /*empty*/
161 # define PRIVATE_EXTERN
162 # define __attribute__(x)
163 # define inline __inline
165 /* Use this for functions that are intended to be breakpoint hooks.
166 If you do not, the compiler may optimize them away.
167 BREAKPOINT_FUNCTION( void MyBreakpointFunction(void) ); */
168 # define BREAKPOINT_FUNCTION(prototype) \
169 __declspec(noinline) prototype { __asm { } }
171 /* stub out dtrace probes */
172 # define OBJC_RUNTIME_OBJC_EXCEPTION_RETHROW() do {} while(0)
173 # define OBJC_RUNTIME_OBJC_EXCEPTION_THROW(arg0) do {} while(0)
180 #include <objc/objc.h>
181 #include <objc/objc-api.h>
185 extern void _objc_fatal(const char *fmt
, ...) __attribute__((noreturn
, format (printf
, 1, 2)));
187 #define INIT_ONCE_PTR(var, create, delete) \
190 typeof(var) v = create; \
192 if (OSAtomicCompareAndSwapPtrBarrier(0, (void*)v, (void**)&var)){ \
200 #define INIT_ONCE_32(var, create, delete) \
203 typeof(var) v = create; \
205 if (OSAtomicCompareAndSwap32Barrier(0, v, (volatile int32_t *)&var)) { \
214 // Thread keys reserved by libc for our use.
215 // Keys [0..4] are used by autozone.
216 #if defined(__PTK_FRAMEWORK_OBJC_KEY5)
217 # define SUPPORT_DIRECT_THREAD_KEYS 1
218 # define TLS_DIRECT_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY5)
219 # define SYNC_DATA_DIRECT_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY6)
220 # define SYNC_COUNT_DIRECT_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY7)
221 # define AUTORELEASE_POOL_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY8)
222 # if SUPPORT_RETURN_AUTORELEASE
223 # define AUTORELEASE_POOL_RECLAIM_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY9)
226 # define SUPPORT_DIRECT_THREAD_KEYS 0
232 // Compiler compatibility
236 #define strdup _strdup
238 #define issetugid() 0
240 #define MIN(x, y) ((x) < (y) ? (x) : (y))
242 static __inline
void bcopy(const void *src
, void *dst
, size_t size
) { memcpy(dst
, src
, size
); }
243 static __inline
void bzero(void *dst
, size_t size
) { memset(dst
, 0, size
); }
245 int asprintf(char **dstp
, const char *format
, ...);
247 typedef void * malloc_zone_t
;
249 static __inline malloc_zone_t
malloc_default_zone(void) { return (malloc_zone_t
)-1; }
250 static __inline
void *malloc_zone_malloc(malloc_zone_t z
, size_t size
) { return malloc(size
); }
251 static __inline
void *malloc_zone_calloc(malloc_zone_t z
, size_t size
, size_t count
) { return calloc(size
, count
); }
252 static __inline
void *malloc_zone_realloc(malloc_zone_t z
, void *p
, size_t size
) { return realloc(p
, size
); }
253 static __inline
void malloc_zone_free(malloc_zone_t z
, void *p
) { free(p
); }
254 static __inline malloc_zone_t
malloc_zone_from_ptr(const void *p
) { return (malloc_zone_t
)-1; }
255 static __inline
size_t malloc_size(const void *p
) { return _msize((void*)p
); /* fixme invalid pointer check? */ }
260 #define require_action_string(cond, dest, act, msg) do { if (!(cond)) { { act; } goto dest; } } while (0)
261 #define require_noerr_string(err, dest, msg) do { if (err) goto dest; } while (0)
262 #define require_string(cond, dest, msg) do { if (!(cond)) goto dest; } while (0)
267 static __inline BOOL
OSAtomicCompareAndSwapLong(long oldl
, long newl
, long volatile *dst
)
269 // fixme barrier is overkill
270 long original
= InterlockedCompareExchange(dst
, newl
, oldl
);
271 return (original
== oldl
);
274 static __inline BOOL
OSAtomicCompareAndSwapPtrBarrier(void *oldp
, void *newp
, void * volatile *dst
)
276 void *original
= InterlockedCompareExchangePointer(dst
, newp
, oldp
);
277 return (original
== oldp
);
280 static __inline BOOL
OSAtomicCompareAndSwap32Barrier(int32_t oldl
, int32_t newl
, int32_t volatile *dst
)
282 long original
= InterlockedCompareExchange((volatile long *)dst
, newl
, oldl
);
283 return (original
== oldl
);
286 static __inline
int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst
)
288 return InterlockedDecrement((volatile long *)dst
);
291 static __inline
int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst
)
293 return InterlockedIncrement((volatile long *)dst
);
297 // Internal data types
299 typedef DWORD objc_thread_t
; // thread ID
300 static __inline
int thread_equal(objc_thread_t t1
, objc_thread_t t2
) {
303 static __inline objc_thread_t
thread_self(void) {
304 return GetCurrentThreadId();
309 void (*dtor
)(void *);
311 static __inline tls_key_t
tls_create(void (*dtor
)(void*)) {
312 // fixme need dtor registry for DllMain to call on thread detach
318 static __inline
void *tls_get(tls_key_t k
) {
319 return TlsGetValue(k
.key
);
321 static __inline
void tls_set(tls_key_t k
, void *value
) {
322 TlsSetValue(k
.key
, value
);
326 CRITICAL_SECTION
*lock
;
328 #define MUTEX_INITIALIZER {0};
329 extern void mutex_init(mutex_t
*m
);
330 static __inline
int _mutex_lock_nodebug(mutex_t
*m
) {
335 EnterCriticalSection(m
->lock
);
338 static __inline
bool _mutex_try_lock_nodebug(mutex_t
*m
) {
343 return TryEnterCriticalSection(m
->lock
);
345 static __inline
int _mutex_unlock_nodebug(mutex_t
*m
) {
347 LeaveCriticalSection(m
->lock
);
352 typedef mutex_t spinlock_t
;
353 #define spinlock_lock(l) mutex_lock(l)
354 #define spinlock_unlock(l) mutex_unlock(l)
355 #define SPINLOCK_INITIALIZER MUTEX_INITIALIZER
361 #define RECURSIVE_MUTEX_INITIALIZER {0};
362 #define RECURSIVE_MUTEX_NOT_LOCKED 1
363 extern void recursive_mutex_init(recursive_mutex_t
*m
);
364 static __inline
int _recursive_mutex_lock_nodebug(recursive_mutex_t
*m
) {
366 return WaitForSingleObject(m
->mutex
, INFINITE
);
368 static __inline
bool _recursive_mutex_try_lock_nodebug(recursive_mutex_t
*m
) {
370 return (WAIT_OBJECT_0
== WaitForSingleObject(m
->mutex
, 0));
372 static __inline
int _recursive_mutex_unlock_nodebug(recursive_mutex_t
*m
) {
374 return ReleaseMutex(m
->mutex
) ? 0 : RECURSIVE_MUTEX_NOT_LOCKED
;
379 typedef HANDLE mutex_t;
380 static inline void mutex_init(HANDLE *m) { *m = CreateMutex(NULL, FALSE, NULL); }
381 static inline void _mutex_lock(mutex_t *m) { WaitForSingleObject(*m, INFINITE); }
382 static inline bool mutex_try_lock(mutex_t *m) { return WaitForSingleObject(*m, 0) == WAIT_OBJECT_0; }
383 static inline void _mutex_unlock(mutex_t *m) { ReleaseMutex(*m); }
386 // based on http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
387 // Vista-only CONDITION_VARIABLE would be better
390 HANDLE waiters
; // semaphore for those in cond_wait()
391 HANDLE waitersDone
; // auto-reset event after everyone gets a broadcast
392 CRITICAL_SECTION waitCountLock
; // guards waitCount and didBroadcast
393 unsigned int waitCount
;
396 #define MONITOR_INITIALIZER { 0 }
397 #define MONITOR_NOT_ENTERED 1
398 extern int monitor_init(monitor_t
*c
);
400 static inline int _monitor_enter_nodebug(monitor_t
*c
) {
402 int err
= monitor_init(c
);
405 return WaitForSingleObject(c
->mutex
, INFINITE
);
407 static inline int _monitor_exit_nodebug(monitor_t
*c
) {
408 if (!ReleaseMutex(c
->mutex
)) return MONITOR_NOT_ENTERED
;
411 static inline int _monitor_wait_nodebug(monitor_t
*c
) {
413 EnterCriticalSection(&c
->waitCountLock
);
415 LeaveCriticalSection(&c
->waitCountLock
);
417 SignalObjectAndWait(c
->mutex
, c
->waiters
, INFINITE
, FALSE
);
419 EnterCriticalSection(&c
->waitCountLock
);
421 last
= c
->didBroadcast
&& c
->waitCount
== 0;
422 LeaveCriticalSection(&c
->waitCountLock
);
425 // tell broadcaster that all waiters have awoken
426 SignalObjectAndWait(c
->waitersDone
, c
->mutex
, INFINITE
, FALSE
);
428 WaitForSingleObject(c
->mutex
, INFINITE
);
431 // fixme error checking
434 static inline int monitor_notify(monitor_t
*c
) {
437 EnterCriticalSection(&c
->waitCountLock
);
438 haveWaiters
= c
->waitCount
> 0;
439 LeaveCriticalSection(&c
->waitCountLock
);
442 ReleaseSemaphore(c
->waiters
, 1, 0);
445 // fixme error checking
448 static inline int monitor_notifyAll(monitor_t
*c
) {
449 EnterCriticalSection(&c
->waitCountLock
);
450 if (c
->waitCount
== 0) {
451 LeaveCriticalSection(&c
->waitCountLock
);
455 ReleaseSemaphore(c
->waiters
, c
->waitCount
, 0);
456 LeaveCriticalSection(&c
->waitCountLock
);
458 // fairness: wait for everyone to move from waiters to mutex
459 WaitForSingleObject(c
->waitersDone
, INFINITE
);
460 // not under waitCountLock, but still under mutex
463 // fixme error checking
468 // fixme no rwlock yet
470 #define rwlock_t mutex_t
471 #define rwlock_init(r) mutex_init(r)
472 #define _rwlock_read_nodebug(m) _mutex_lock_nodebug(m)
473 #define _rwlock_write_nodebug(m) _mutex_lock_nodebug(m)
474 #define _rwlock_try_read_nodebug(m) _mutex_try_lock_nodebug(m)
475 #define _rwlock_try_write_nodebug(m) _mutex_try_lock_nodebug(m)
476 #define _rwlock_unlock_read_nodebug(m) _mutex_unlock_nodebug(m)
477 #define _rwlock_unlock_write_nodebug(m) _mutex_unlock_nodebug(m)
480 typedef IMAGE_DOS_HEADER headerType
;
481 // fixme YES bundle? NO bundle? sometimes?
482 #define headerIsBundle(hi) YES
483 OBJC_EXTERN IMAGE_DOS_HEADER __ImageBase
;
484 #define libobjc_header ((headerType *)&__ImageBase)
493 #include <mach-o/loader.h>
495 # define SEGMENT_CMD LC_SEGMENT
497 # define SEGMENT_CMD LC_SEGMENT_64
500 #ifndef VM_MEMORY_OBJC_DISPATCHERS
501 # define VM_MEMORY_OBJC_DISPATCHERS 0
505 // Compiler compatibility
509 // Internal data types
511 typedef pthread_t objc_thread_t
;
513 static __inline
int thread_equal(objc_thread_t t1
, objc_thread_t t2
) {
514 return pthread_equal(t1
, t2
);
516 static __inline objc_thread_t
thread_self(void) {
517 return pthread_self();
521 typedef pthread_key_t tls_key_t
;
523 static inline tls_key_t
tls_create(void (*dtor
)(void*)) {
525 pthread_key_create(&k
, dtor
);
528 static inline void *tls_get(tls_key_t k
) {
529 return pthread_getspecific(k
);
531 static inline void tls_set(tls_key_t k
, void *value
) {
532 pthread_setspecific(k
, value
);
535 #if SUPPORT_DIRECT_THREAD_KEYS
538 static bool is_valid_direct_key(tls_key_t k
) {
539 return ( k
== SYNC_DATA_DIRECT_KEY
540 || k
== SYNC_COUNT_DIRECT_KEY
541 || k
== AUTORELEASE_POOL_KEY
542 # if SUPPORT_RETURN_AUTORELEASE
543 || k
== AUTORELEASE_POOL_RECLAIM_KEY
551 // rdar://9162780 _pthread_get/setspecific_direct are inefficient
552 // copied from libdispatch
554 __attribute__((always_inline
)) __attribute__((const))
559 #if defined(__arm__) && defined(_ARM_ARCH_6)
560 __asm__("mrc p15, 0, %[p], c13, c0, 3" : [p
] "=&r" (p
));
561 return (void**)(p
& ~0x3ul
);
563 #error tls_base not implemented
567 __attribute__((always_inline
))
569 tls_set_direct(void **tsdb
, tls_key_t k
, void *v
)
571 assert(is_valid_direct_key(k
));
575 #define tls_set_direct(k, v) \
576 tls_set_direct(tls_base(), (k), (v))
578 __attribute__((always_inline
))
580 tls_get_direct(void **tsdb
, tls_key_t k
)
582 assert(is_valid_direct_key(k
));
586 #define tls_get_direct(k) \
587 tls_get_direct(tls_base(), (k))
593 static inline void *tls_get_direct(tls_key_t k
)
595 assert(is_valid_direct_key(k
));
597 if (_pthread_has_direct_tsd()) {
598 return _pthread_getspecific_direct(k
);
600 return pthread_getspecific(k
);
603 static inline void tls_set_direct(tls_key_t k
, void *value
)
605 assert(is_valid_direct_key(k
));
607 if (_pthread_has_direct_tsd()) {
608 _pthread_setspecific_direct(k
, value
);
610 pthread_setspecific(k
, value
);
617 // SUPPORT_DIRECT_THREAD_KEYS
621 typedef pthread_mutex_t mutex_t
;
622 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER;
624 static inline int _mutex_lock_nodebug(mutex_t
*m
) {
625 return pthread_mutex_lock(m
);
627 static inline bool _mutex_try_lock_nodebug(mutex_t
*m
) {
628 return !pthread_mutex_trylock(m
);
630 static inline int _mutex_unlock_nodebug(mutex_t
*m
) {
631 return pthread_mutex_unlock(m
);
636 pthread_mutex_t
*mutex
;
638 #define RECURSIVE_MUTEX_INITIALIZER {0};
639 #define RECURSIVE_MUTEX_NOT_LOCKED EPERM
640 extern void recursive_mutex_init(recursive_mutex_t
*m
);
642 static inline int _recursive_mutex_lock_nodebug(recursive_mutex_t
*m
) {
644 return pthread_mutex_lock(m
->mutex
);
646 static inline bool _recursive_mutex_try_lock_nodebug(recursive_mutex_t
*m
) {
648 return !pthread_mutex_trylock(m
->mutex
);
650 static inline int _recursive_mutex_unlock_nodebug(recursive_mutex_t
*m
) {
652 return pthread_mutex_unlock(m
->mutex
);
657 pthread_mutex_t mutex
;
660 #define MONITOR_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER }
661 #define MONITOR_NOT_ENTERED EPERM
663 static inline int monitor_init(monitor_t
*c
) {
664 int err
= pthread_mutex_init(&c
->mutex
, NULL
);
666 err
= pthread_cond_init(&c
->cond
, NULL
);
668 pthread_mutex_destroy(&c
->mutex
);
673 static inline int _monitor_enter_nodebug(monitor_t
*c
) {
674 return pthread_mutex_lock(&c
->mutex
);
676 static inline int _monitor_exit_nodebug(monitor_t
*c
) {
677 return pthread_mutex_unlock(&c
->mutex
);
679 static inline int _monitor_wait_nodebug(monitor_t
*c
) {
680 return pthread_cond_wait(&c
->cond
, &c
->mutex
);
682 static inline int monitor_notify(monitor_t
*c
) {
683 return pthread_cond_signal(&c
->cond
);
685 static inline int monitor_notifyAll(monitor_t
*c
) {
686 return pthread_cond_broadcast(&c
->cond
);
690 // semaphore_create formatted for INIT_ONCE use
691 static inline semaphore_t
create_semaphore(void)
695 k
= semaphore_create(mach_task_self(), &sem
, SYNC_POLICY_FIFO
, 0);
696 if (k
) _objc_fatal("semaphore_create failed (0x%x)", k
);
701 /* Custom read-write lock
702 - reader is atomic add/subtract
703 - writer is pthread mutex plus atomic add/subtract
704 - fairness: new readers wait if a writer wants in
705 - fairness: when writer completes, readers (probably) precede new writer
707 state: xxxxxxxx xxxxxxxx yyyyyyyy yyyyyyyz
708 x: blocked reader count
709 y: active reader count
710 z: readers allowed flag
713 pthread_rwlock_t rwl
;
716 static inline void rwlock_init(rwlock_t
*l
)
718 int err __unused
= pthread_rwlock_init(&l
->rwl
, NULL
);
722 static inline void _rwlock_read_nodebug(rwlock_t
*l
)
724 int err __unused
= pthread_rwlock_rdlock(&l
->rwl
);
728 static inline void _rwlock_unlock_read_nodebug(rwlock_t
*l
)
730 int err __unused
= pthread_rwlock_unlock(&l
->rwl
);
735 static inline bool _rwlock_try_read_nodebug(rwlock_t
*l
)
737 int err
= pthread_rwlock_tryrdlock(&l
->rwl
);
738 assert(err
== 0 || err
== EBUSY
);
743 static inline void _rwlock_write_nodebug(rwlock_t
*l
)
745 int err __unused
= pthread_rwlock_wrlock(&l
->rwl
);
749 static inline void _rwlock_unlock_write_nodebug(rwlock_t
*l
)
751 int err __unused
= pthread_rwlock_unlock(&l
->rwl
);
755 static inline bool _rwlock_try_write_nodebug(rwlock_t
*l
)
757 int err
= pthread_rwlock_trywrlock(&l
->rwl
);
758 assert(err
== 0 || err
== EBUSY
);
764 typedef struct mach_header headerType
;
765 typedef struct segment_command segmentType
;
766 typedef struct section sectionType
;
768 typedef struct mach_header_64 headerType
;
769 typedef struct segment_command_64 segmentType
;
770 typedef struct section_64 sectionType
;
772 #define headerIsBundle(hi) (hi->mhdr->filetype == MH_BUNDLE)
773 #define libobjc_header ((headerType *)&_mh_dylib_header)
777 /* Secure /tmp usage */
778 extern int secure_open(const char *filename
, int flags
, uid_t euid
);