First set of licensing changes
[mono.git] / mono / mini / mini-darwin.c
1 /*
2  * mini-darwin.c: Darwin/MacOS support for Mono.
3  *
4  * Authors:
5  *   Mono Team (mono-list@lists.ximian.com)
6  *
7  * Copyright 2001-2003 Ximian, Inc.
8  * Copyright 2003-2011 Novell, Inc (http://www.novell.com)
9  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
10  *
11  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
12  */
13 #include <config.h>
14 #include <signal.h>
15 #ifdef HAVE_ALLOCA_H
16 #include <alloca.h>
17 #endif
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #include <math.h>
22 #ifdef HAVE_SYS_TIME_H
23 #include <sys/time.h>
24 #endif
25
26 #include <mono/metadata/assembly.h>
27 #include <mono/metadata/loader.h>
28 #include <mono/metadata/tabledefs.h>
29 #include <mono/metadata/class.h>
30 #include <mono/metadata/object.h>
31 #include <mono/metadata/tokentype.h>
32 #include <mono/metadata/tabledefs.h>
33 #include <mono/metadata/threads.h>
34 #include <mono/metadata/appdomain.h>
35 #include <mono/metadata/debug-helpers.h>
36 #include <mono/io-layer/io-layer.h>
37 #include "mono/metadata/profiler.h"
38 #include <mono/metadata/profiler-private.h>
39 #include <mono/metadata/mono-config.h>
40 #include <mono/metadata/environment.h>
41 #include <mono/metadata/mono-debug.h>
42 #include <mono/metadata/threads-types.h>
43 #include <mono/metadata/verify.h>
44 #include <mono/metadata/verify-internals.h>
45 #include <mono/metadata/mempool-internals.h>
46 #include <mono/metadata/attach.h>
47 #include <mono/metadata/gc-internals.h>
48 #include <mono/utils/mono-math.h>
49 #include <mono/utils/mono-compiler.h>
50 #include <mono/utils/mono-counters.h>
51 #include <mono/utils/mono-logger-internals.h>
52 #include <mono/utils/mono-mmap.h>
53 #include <mono/utils/dtrace.h>
54
55 #include "mini.h"
56 #include <string.h>
57 #include <ctype.h>
58 #include "trace.h"
59 #include "version.h"
60
61 #include "jit-icalls.h"
62
63 /* MacOS includes */
64 #include <mach/mach.h>
65 #include <mach/mach_error.h>
66 #include <mach/exception.h>
67 #include <mach/task.h>
68 #include <pthread.h>
69 #include <dlfcn.h>
70 #include <AvailabilityMacros.h>
71
72 #if defined (TARGET_OSX) && (MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_5)
73 #define NEEDS_EXCEPTION_THREAD
74 #endif
75
76 #ifdef NEEDS_EXCEPTION_THREAD
77
78 /*
79  * This code disables the CrashReporter of MacOS X by installing
80  * a dummy Mach exception handler.
81  */
82
83 /*
84  * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/exc_server.html
85  */
86 extern boolean_t exc_server (mach_msg_header_t *request_msg, mach_msg_header_t *reply_msg);
87
88 /*
89  * The exception message
90  */
91 typedef struct {
92         mach_msg_base_t msg;  /* common mach message header */
93         char payload [1024];  /* opaque */
94 } mach_exception_msg_t;
95
96 /* The exception port */
97 static mach_port_t mach_exception_port = VM_MAP_NULL;
98
99 kern_return_t
100 catch_exception_raise (
101         mach_port_t exception_port,
102         mach_port_t thread,
103         mach_port_t task,
104         exception_type_t exception,
105         exception_data_t code,
106         mach_msg_type_number_t code_count);
107
108 /*
109  * Implicitly called by exc_server. Must be public.
110  *
111  * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/catch_exception_raise.html
112  */
113 kern_return_t
114 catch_exception_raise (
115         mach_port_t exception_port,
116         mach_port_t thread,
117         mach_port_t task,
118         exception_type_t exception,
119         exception_data_t code,
120         mach_msg_type_number_t code_count)
121 {
122         /* consume the exception */
123         return KERN_FAILURE;
124 }
125
126 /*
127  * Exception thread handler.
128  */
129 static
130 void *
131 mach_exception_thread (void *arg)
132 {
133         for (;;) {
134                 mach_exception_msg_t request;
135                 mach_exception_msg_t reply;
136                 mach_msg_return_t result;
137
138                 /* receive from "mach_exception_port" */
139                 result = mach_msg (&request.msg.header,
140                                    MACH_RCV_MSG | MACH_RCV_LARGE,
141                                    0,
142                                    sizeof (request),
143                                    mach_exception_port,
144                                    MACH_MSG_TIMEOUT_NONE,
145                                    MACH_PORT_NULL);
146
147                 g_assert (result == MACH_MSG_SUCCESS);
148
149                 /* dispatch to catch_exception_raise () */
150                 exc_server (&request.msg.header, &reply.msg.header);
151
152                 /* send back to sender */
153                 result = mach_msg (&reply.msg.header,
154                                    MACH_SEND_MSG,
155                                    reply.msg.header.msgh_size,
156                                    0,
157                                    MACH_PORT_NULL,
158                                    MACH_MSG_TIMEOUT_NONE,
159                                    MACH_PORT_NULL);
160
161                 /*
162                 If we try to abort the thread while delivering an exception. The port will be gone since the kernel
163                 setup a send once port to deliver the resume message and thread_abort will consume it.
164                 */
165                 g_assert (result == MACH_MSG_SUCCESS || result == MACH_SEND_INVALID_DEST);
166         }
167         return NULL;
168 }
169
170 static void
171 macosx_register_exception_handler (void)
172 {
173         mach_port_t task;
174         pthread_attr_t attr;
175         pthread_t thread;
176
177         if (mach_exception_port != VM_MAP_NULL)
178                 return;
179
180         task = mach_task_self ();
181
182         /* create the "mach_exception_port" with send & receive rights */
183         g_assert (mach_port_allocate (task, MACH_PORT_RIGHT_RECEIVE,
184                                       &mach_exception_port) == KERN_SUCCESS);
185         g_assert (mach_port_insert_right (task, mach_exception_port, mach_exception_port,
186                                           MACH_MSG_TYPE_MAKE_SEND) == KERN_SUCCESS);
187
188         /* create the exception handler thread */
189         g_assert (!pthread_attr_init (&attr));
190         g_assert (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED));
191         g_assert (!pthread_create (&thread, &attr, mach_exception_thread, NULL));
192         pthread_attr_destroy (&attr);
193
194         /*
195          * register "mach_exception_port" as a receiver for the
196          * EXC_BAD_ACCESS exception
197          *
198          * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/task_set_exception_ports.html
199          */
200         g_assert (task_set_exception_ports (task, EXC_MASK_BAD_ACCESS,
201                                             mach_exception_port,
202                                             EXCEPTION_DEFAULT,
203                                             MACHINE_THREAD_STATE) == KERN_SUCCESS);
204
205         mono_gc_register_mach_exception_thread (thread);
206 }
207
208 #endif
209
210 /* This is #define'd by Boehm GC to _GC_dlopen. */
211 #undef dlopen
212
213 void* dlopen(const char* path, int mode);
214
215 void
216 mono_runtime_install_handlers (void)
217 {
218 #ifdef NEEDS_EXCEPTION_THREAD
219         macosx_register_exception_handler ();
220 #endif
221         mono_runtime_posix_install_handlers ();
222
223         /* Snow Leopard has a horrible bug: http://openradar.appspot.com/7209349
224          * This causes obscure SIGTRAP's for any application that comes across this built on
225          * Snow Leopard.  This is a horrible hack to ensure that the private __CFInitialize
226          * is run on the main thread, so that we don't get SIGTRAPs later
227          */
228 #if defined (__APPLE__) && (defined (__i386__) || defined (__x86_64__))
229         {
230                 void *handle = dlopen ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
231                 if (handle == NULL)
232                         return;
233
234                 dlclose (handle);
235         }
236 #endif
237 }
238
239 pid_t
240 mono_runtime_syscall_fork ()
241 {
242 #ifdef HAVE_FORK
243         return (pid_t) fork ();
244 #else
245         g_assert_not_reached ();
246 #endif
247 }
248
249 void
250 mono_gdb_render_native_backtraces (pid_t crashed_pid)
251 {
252 #ifdef HAVE_EXECV
253         const char *argv [5];
254         char template [] = "/tmp/mono-gdb-commands.XXXXXX";
255         FILE *commands;
256         gboolean using_lldb = FALSE;
257
258         using_lldb = TRUE;
259
260         argv [0] = g_find_program_in_path ("gdb");
261         if (argv [0])
262                 using_lldb = FALSE;
263
264         if (using_lldb)
265                 argv [0] = g_find_program_in_path ("lldb");
266
267         if (argv [0] == NULL)
268                 return;
269
270         if (mkstemp (template) == -1)
271                 return;
272
273         commands = fopen (template, "w");
274         if (using_lldb) {
275                 fprintf (commands, "process attach --pid %ld\n", (long) crashed_pid);
276                 fprintf (commands, "thread list\n");
277                 fprintf (commands, "thread backtrace all\n");
278                 fprintf (commands, "detach\n");
279                 fprintf (commands, "quit\n");
280                 argv [1] = "--source";
281                 argv [2] = template;
282                 argv [3] = 0;
283                 
284         } else {
285                 fprintf (commands, "attach %ld\n", (long) crashed_pid);
286                 fprintf (commands, "info threads\n");
287                 fprintf (commands, " t a a info thread\n");
288                 fprintf (commands, "thread apply all bt\n");
289                 argv [1] = "-batch";
290                 argv [2] = "-x";
291                 argv [3] = template;
292                 argv [4] = 0;
293         }
294         fflush (commands);
295         fclose (commands);
296
297         fclose (stdin);
298
299         execv (argv [0], (char**)argv);
300         unlink (template);
301 #else
302         fprintf (stderr, "mono_gdb_render_native_backtraces not supported on this platform\n");
303 #endif // HAVE_EXECV
304 }
305
306 gboolean
307 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoThreadInfo *info)
308 {
309         kern_return_t ret;
310         mach_msg_type_number_t num_state;
311         thread_state_t state;
312         ucontext_t ctx;
313         mcontext_t mctx;
314         MonoJitTlsData *jit_tls;
315         void *domain;
316         MonoLMF *lmf = NULL;
317         gpointer *addr;
318
319         g_assert (info);
320         /*Zero enough state to make sure the caller doesn't confuse itself*/
321         tctx->valid = FALSE;
322         tctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = NULL;
323         tctx->unwind_data [MONO_UNWIND_DATA_LMF] = NULL;
324         tctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = NULL;
325
326         state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
327         mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
328
329         ret = mono_mach_arch_get_thread_state (info->native_handle, state, &num_state);
330         if (ret != KERN_SUCCESS)
331                 return FALSE;
332
333         mono_mach_arch_thread_state_to_mcontext (state, mctx);
334         ctx.uc_mcontext = mctx;
335
336         mono_sigctx_to_monoctx (&ctx, &tctx->ctx);
337
338         /* mono_set_jit_tls () sets this */
339         jit_tls = mono_thread_info_tls_get (info, TLS_KEY_JIT_TLS);
340         /* SET_APPDOMAIN () sets this */
341         domain = mono_thread_info_tls_get (info, TLS_KEY_DOMAIN);
342
343         /*Thread already started to cleanup, can no longer capture unwind state*/
344         if (!jit_tls || !domain)
345                 return FALSE;
346
347         /*
348          * The current LMF address is kept in a separate TLS variable, and its hard to read its value without
349          * arch-specific code. But the address of the TLS variable is stored in another TLS variable which
350          * can be accessed through MonoThreadInfo.
351          */
352         /* mono_set_lmf_addr () sets this */
353         addr = mono_thread_info_tls_get (info, TLS_KEY_LMF_ADDR);
354         if (addr)
355                 lmf = *addr;
356
357
358         tctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = domain;
359         tctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = jit_tls;
360         tctx->unwind_data [MONO_UNWIND_DATA_LMF] = lmf;
361         tctx->valid = TRUE;
362
363         return TRUE;
364 }