2009-05-18 Miguel de Icaza <miguel@novell.com>
[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-2008 Ximian, Inc.
9  *
10  * See LICENSE for licensing information.
11  */
12 #include <config.h>
13 #include <signal.h>
14 #ifdef HAVE_ALLOCA_H
15 #include <alloca.h>
16 #endif
17 #ifdef HAVE_UNISTD_H
18 #include <unistd.h>
19 #endif
20 #include <math.h>
21 #ifdef HAVE_SYS_TIME_H
22 #include <sys/time.h>
23 #endif
24
25 #ifdef HAVE_VALGRIND_MEMCHECK_H
26 #include <valgrind/memcheck.h>
27 #endif
28
29 #include <mono/metadata/assembly.h>
30 #include <mono/metadata/loader.h>
31 #include <mono/metadata/tabledefs.h>
32 #include <mono/metadata/class.h>
33 #include <mono/metadata/object.h>
34 #include <mono/metadata/tokentype.h>
35 #include <mono/metadata/tabledefs.h>
36 #include <mono/metadata/threads.h>
37 #include <mono/metadata/appdomain.h>
38 #include <mono/metadata/debug-helpers.h>
39 #include <mono/io-layer/io-layer.h>
40 #include "mono/metadata/profiler.h"
41 #include <mono/metadata/profiler-private.h>
42 #include <mono/metadata/mono-config.h>
43 #include <mono/metadata/environment.h>
44 #include <mono/metadata/mono-debug.h>
45 #include <mono/metadata/gc-internal.h>
46 #include <mono/metadata/threads-types.h>
47 #include <mono/metadata/verify.h>
48 #include <mono/metadata/verify-internals.h>
49 #include <mono/metadata/mempool-internals.h>
50 #include <mono/metadata/attach.h>
51 #include <mono/utils/mono-math.h>
52 #include <mono/utils/mono-compiler.h>
53 #include <mono/utils/mono-counters.h>
54 #include <mono/utils/mono-logger.h>
55 #include <mono/utils/mono-mmap.h>
56 #include <mono/utils/dtrace.h>
57
58 #include "mini.h"
59 #include <string.h>
60 #include <ctype.h>
61 #include "trace.h"
62 #include "version.h"
63
64 #include "jit-icalls.h"
65
66 /* MacOS includes */
67 #include <mach/mach.h>
68 #include <mach/mach_error.h>
69 #include <mach/exception.h>
70 #include <mach/task.h>
71 #include <pthread.h>
72
73 /*
74  * This code disables the CrashReporter of MacOS X by installing
75  * a dummy Mach exception handler.
76  */
77
78 /*
79  * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/exc_server.html
80  */
81 extern boolean_t exc_server (mach_msg_header_t *request_msg, mach_msg_header_t *reply_msg);
82
83 /*
84  * The exception message
85  */
86 typedef struct {
87         mach_msg_base_t msg;  /* common mach message header */
88         char payload [1024];  /* opaque */
89 } mach_exception_msg_t;
90
91 /* The exception port */
92 static mach_port_t mach_exception_port = VM_MAP_NULL;
93
94 /*
95  * Implicitly called by exc_server. Must be public.
96  *
97  * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/catch_exception_raise.html
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         /* consume the exception */
109         return KERN_FAILURE;
110 }
111
112 /*
113  * Exception thread handler.
114  */
115 static
116 void *
117 mach_exception_thread (void *arg)
118 {
119         for (;;) {
120                 mach_exception_msg_t request;
121                 mach_exception_msg_t reply;
122                 mach_msg_return_t result;
123
124                 /* receive from "mach_exception_port" */
125                 result = mach_msg (&request.msg.header,
126                                    MACH_RCV_MSG | MACH_RCV_LARGE,
127                                    0,
128                                    sizeof (request),
129                                    mach_exception_port,
130                                    MACH_MSG_TIMEOUT_NONE,
131                                    MACH_PORT_NULL);
132
133                 g_assert (result == MACH_MSG_SUCCESS);
134
135                 /* dispatch to catch_exception_raise () */
136                 exc_server (&request.msg.header, &reply.msg.header);
137
138                 /* send back to sender */
139                 result = mach_msg (&reply.msg.header,
140                                    MACH_SEND_MSG,
141                                    reply.msg.header.msgh_size,
142                                    0,
143                                    MACH_PORT_NULL,
144                                    MACH_MSG_TIMEOUT_NONE,
145                                    MACH_PORT_NULL);
146
147                 g_assert (result == MACH_MSG_SUCCESS);
148         }
149         return NULL;
150 }
151
152 static void
153 macosx_register_exception_handler ()
154 {
155         mach_port_t task;
156         pthread_attr_t attr;
157         pthread_t thread;
158
159         if (mach_exception_port != VM_MAP_NULL)
160                 return;
161
162         task = mach_task_self ();
163
164         /* create the "mach_exception_port" with send & receive rights */
165         g_assert (mach_port_allocate (task, MACH_PORT_RIGHT_RECEIVE,
166                                       &mach_exception_port) == KERN_SUCCESS);
167         g_assert (mach_port_insert_right (task, mach_exception_port, mach_exception_port,
168                                           MACH_MSG_TYPE_MAKE_SEND) == KERN_SUCCESS);
169
170         /* create the exception handler thread */
171         g_assert (!pthread_attr_init (&attr));
172         g_assert (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED));
173         g_assert (!pthread_create (&thread, &attr, mach_exception_thread, NULL));
174         pthread_attr_destroy (&attr);
175
176         /*
177          * register "mach_exception_port" as a receiver for the
178          * EXC_BAD_ACCESS exception
179          *
180          * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/task_set_exception_ports.html
181          */
182         g_assert (task_set_exception_ports (task, EXC_MASK_BAD_ACCESS,
183                                             mach_exception_port,
184                                             EXCEPTION_DEFAULT,
185                                             MACHINE_THREAD_STATE) == KERN_SUCCESS);
186 }
187
188 void
189 mono_runtime_install_handlers (void)
190 {
191         macosx_register_exception_handler ();
192         mono_runtime_posix_install_handlers ();
193 }