]> git.saurik.com Git - apple/libsystem.git/blob - init.c
dafcce2edf0e8b3ad421f8e5deb30c9723ff4d86
[apple/libsystem.git] / init.c
1 /*
2 * Copyright (c) 2007, 2008, 2011-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <TargetConditionals.h> // for TARGET_OS_*
30
31 #include <stddef.h>
32 #include <stdlib.h>
33 #include <libc_private.h>
34 #include <pthread.h>
35 #include <pthread/private.h>
36 #include <dlfcn.h>
37 #include <fcntl.h>
38 #include <errno.h>
39 #include <_libkernel_init.h> // Must be after voucher_private.h
40
41 #include <mach-o/dyld_priv.h>
42
43 // system library initialisers
44 extern void mach_init(void); // from libsystem_kernel.dylib
45 extern void __libplatform_init(void *future_use, const char *envp[], const char *apple[], const struct ProgramVars *vars);
46 extern void __pthread_init(const struct _libpthread_functions *libpthread_funcs, const char *envp[], const char *apple[], const struct ProgramVars *vars); // from libsystem_pthread.dylib
47 extern void __malloc_init(const char *apple[]); // from libsystem_malloc.dylib
48 extern void __keymgr_initializer(void); // from libkeymgr.dylib
49 extern void _dyld_initializer(void); // from libdyld.dylib
50 extern void libdispatch_init(void); // from libdispatch.dylib
51 extern void _libxpc_initializer(void); // from libxpc.dylib
52 extern void _libsecinit_initializer(void); // from libsecinit.dylib
53 extern void _libtrace_init(void); // from libsystem_trace.dylib
54 extern void _container_init(const char *apple[]); // from libsystem_containermanager.dylib
55 extern void __libdarwin_init(void); // from libsystem_darwin.dylib
56
57
58 // signal malloc stack logging that initialisation has finished
59 extern void __stack_logging_early_finished(void); // form libsystem_c.dylib
60
61 // clear qos tsd (from pthread)
62 extern void _pthread_clear_qos_tsd(mach_port_t) __attribute__((weak_import));
63
64 // system library atfork handlers
65 extern void _pthread_atfork_prepare(void);
66 extern void _pthread_atfork_parent(void);
67 extern void _pthread_atfork_child(void);
68 extern void _pthread_atfork_prepare_handlers();
69 extern void _pthread_atfork_parent_handlers(void);
70 extern void _pthread_atfork_child_handlers(void);
71 extern void _pthread_exit_if_canceled(int);
72
73 extern void dispatch_atfork_prepare(void);
74 extern void dispatch_atfork_parent(void);
75 extern void dispatch_atfork_child(void);
76
77 extern void _libtrace_fork_child(void);
78
79 extern void _malloc_fork_prepare(void);
80 extern void _malloc_fork_parent(void);
81 extern void _malloc_fork_child(void);
82
83 extern void _mach_fork_child(void);
84 extern void _notify_fork_child(void);
85 extern void _dyld_fork_child(void);
86 extern void xpc_atfork_prepare(void);
87 extern void xpc_atfork_parent(void);
88 extern void xpc_atfork_child(void);
89 extern void _libSC_info_fork_prepare(void);
90 extern void _libSC_info_fork_parent(void);
91 extern void _libSC_info_fork_child(void);
92 extern void _asl_fork_child(void);
93
94 #if defined(HAVE_SYSTEM_CORESERVICES)
95 // libsystem_coreservices.dylib
96 extern void _libcoreservices_fork_child(void);
97 extern char *_dirhelper(int, char *, size_t);
98 #endif
99
100 #if TARGET_OS_EMBEDDED && !TARGET_OS_WATCH && !__LP64__
101 extern void _vminterpose_init(void);
102 #endif
103
104 // advance decls for below;
105 void libSystem_atfork_prepare(void);
106 void libSystem_atfork_parent(void);
107 void libSystem_atfork_child(void);
108
109 #if CURRENT_VARIANT_asan
110 const char *__asan_default_options(void);
111 #endif
112
113 // libsyscall_initializer() initializes all of libSystem.dylib
114 // <rdar://problem/4892197>
115 __attribute__((constructor))
116 static void
117 libSystem_initializer(int argc,
118 const char* argv[],
119 const char* envp[],
120 const char* apple[],
121 const struct ProgramVars* vars)
122 {
123 static const struct _libkernel_functions libkernel_funcs = {
124 .version = 3,
125 // V1 functions
126 .dlsym = dlsym,
127 .malloc = malloc,
128 .free = free,
129 .realloc = realloc,
130 ._pthread_exit_if_canceled = _pthread_exit_if_canceled,
131 // V2 functions (removed)
132 // V3 functions
133 .pthread_clear_qos_tsd = _pthread_clear_qos_tsd,
134 };
135
136 static const struct _libpthread_functions libpthread_funcs = {
137 .version = 2,
138 .exit = exit,
139 .malloc = malloc,
140 .free = free,
141 };
142
143 static const struct _libc_functions libc_funcs = {
144 .version = 1,
145 .atfork_prepare = libSystem_atfork_prepare,
146 .atfork_parent = libSystem_atfork_parent,
147 .atfork_child = libSystem_atfork_child,
148 #if defined(HAVE_SYSTEM_CORESERVICES)
149 .dirhelper = _dirhelper,
150 #endif
151 };
152
153 __libkernel_init(&libkernel_funcs, envp, apple, vars);
154
155 __libplatform_init(NULL, envp, apple, vars);
156
157 __pthread_init(&libpthread_funcs, envp, apple, vars);
158
159 _libc_initializer(&libc_funcs, envp, apple, vars);
160
161 // TODO: Move __malloc_init before __libc_init after breaking malloc's upward link to Libc
162 __malloc_init(apple);
163
164 #if TARGET_OS_OSX
165 /* <rdar://problem/9664631> */
166 __keymgr_initializer();
167 #endif
168
169 // No ASan interceptors are invoked before this point. ASan is normally initialized via the malloc interceptor:
170 // _dyld_initializer() -> tlv_load_notification -> wrap_malloc -> ASanInitInternal
171
172 _dyld_initializer();
173
174 libdispatch_init();
175 _libxpc_initializer();
176
177 #if CURRENT_VARIANT_asan
178 setenv("DT_BYPASS_LEAKS_CHECK", "1", 1);
179 #endif
180
181 // must be initialized after dispatch
182 _libtrace_init();
183
184 #if !(TARGET_OS_EMBEDDED || TARGET_OS_SIMULATOR)
185 _libsecinit_initializer();
186 #endif
187
188 #if TARGET_OS_EMBEDDED
189 _container_init(apple);
190 #endif
191
192 __libdarwin_init();
193
194 __stack_logging_early_finished();
195
196 #if TARGET_OS_EMBEDDED && TARGET_OS_IOS && !__LP64__
197 _vminterpose_init();
198 #endif
199
200 #if !TARGET_OS_IPHONE
201 /* <rdar://problem/22139800> - Preserve the old behavior of apple[] for
202 * programs that haven't linked against newer SDK.
203 */
204 #define APPLE0_PREFIX "executable_path="
205 if (dyld_get_program_sdk_version() < DYLD_MACOSX_VERSION_10_11){
206 if (strncmp(apple[0], APPLE0_PREFIX, strlen(APPLE0_PREFIX)) == 0){
207 apple[0] = apple[0] + strlen(APPLE0_PREFIX);
208 }
209 }
210 #endif
211
212 /* <rdar://problem/11588042>
213 * C99 standard has the following in section 7.5(3):
214 * "The value of errno is zero at program startup, but is never set
215 * to zero by any library function."
216 */
217 errno = 0;
218 }
219
220 /*
221 * libSystem_atfork_{prepare,parent,child}() are called by libc during fork(2).
222 */
223 void
224 libSystem_atfork_prepare(void)
225 {
226 // first call client prepare handlers registered with pthread_atfork()
227 _pthread_atfork_prepare_handlers();
228
229 // second call hardwired fork prepare handlers for Libsystem components
230 // in the _reverse_ order of library initalization above
231 _libSC_info_fork_prepare();
232 xpc_atfork_prepare();
233 dispatch_atfork_prepare();
234 _malloc_fork_prepare();
235 _pthread_atfork_prepare();
236 }
237
238 void
239 libSystem_atfork_parent(void)
240 {
241 // first call hardwired fork parent handlers for Libsystem components
242 // in the order of library initalization above
243 _pthread_atfork_parent();
244 _malloc_fork_parent();
245 dispatch_atfork_parent();
246 xpc_atfork_parent();
247 _libSC_info_fork_parent();
248
249 // second call client parent handlers registered with pthread_atfork()
250 _pthread_atfork_parent_handlers();
251 }
252
253 void
254 libSystem_atfork_child(void)
255 {
256 // first call hardwired fork child handlers for Libsystem components
257 // in the order of library initalization above
258 _dyld_fork_child();
259 _pthread_atfork_child();
260 _mach_fork_child();
261 _malloc_fork_child();
262 _libc_fork_child(); // _arc4_fork_child calls malloc
263 dispatch_atfork_child();
264 #if defined(HAVE_SYSTEM_CORESERVICES)
265 _libcoreservices_fork_child();
266 #endif
267 _asl_fork_child();
268 _notify_fork_child();
269 xpc_atfork_child();
270 _libtrace_fork_child();
271 _libSC_info_fork_child();
272
273 // second call client parent handlers registered with pthread_atfork()
274 _pthread_atfork_child_handlers();
275 }
276
277 #if CURRENT_VARIANT_asan
278 char dynamic_asan_opts[1024] = {0};
279 const char *__asan_default_options(void) {
280 int fd = open("/System/Library/Preferences/com.apple.asan.options", O_RDONLY);
281 if (fd != -1) {
282 ssize_t remaining_size = sizeof(dynamic_asan_opts) - 1;
283 char *p = dynamic_asan_opts;
284 ssize_t read_bytes = 0;
285 do {
286 read_bytes = read(fd, p, remaining_size);
287 remaining_size -= read_bytes;
288 } while (read_bytes > 0);
289 close(fd);
290
291 if (dynamic_asan_opts[0]) {
292 return dynamic_asan_opts;
293 }
294 }
295
296 return "color=never:handle_segv=0:handle_sigbus=0:handle_sigill=0:handle_sigfpe=0";
297 }
298 #endif
299
300 /*
301 * Old crt1.o glue used to call through mach_init_routine which was used to initialize libSystem.
302 * LibSystem now auto-initializes but mach_init_routine is left for binary compatibility.
303 */
304 static void mach_init_old(void) {}
305 void (*mach_init_routine)(void) = &mach_init_old;
306
307 /*
308 * This __crashreporter_info__ symbol is for all non-dylib parts of libSystem.
309 */
310 const char *__crashreporter_info__;
311 asm (".desc __crashreporter_info__, 0x10");