[runtime] Transition the trampoline code to use memory managers for memory allocation...
[mono-project.git] / mono / mini / mini-darwin.c
blob834a60a13e725535824902c734882c29ff7d5764
1 /**
2 * \file
3 * Darwin/MacOS support for Mono.
5 * Authors:
6 * Mono Team (mono-list@lists.ximian.com)
8 * Copyright 2001-2003 Ximian, Inc.
9 * Copyright 2003-2011 Novell, Inc (http://www.novell.com)
10 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 #include <config.h>
15 #include <signal.h>
16 #ifdef HAVE_ALLOCA_H
17 #include <alloca.h>
18 #endif
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22 #include <math.h>
23 #ifdef HAVE_SYS_TIME_H
24 #include <sys/time.h>
25 #endif
27 #include <mono/metadata/assembly.h>
28 #include <mono/metadata/loader.h>
29 #include <mono/metadata/tabledefs.h>
30 #include <mono/metadata/class.h>
31 #include <mono/metadata/object.h>
32 #include <mono/metadata/tokentype.h>
33 #include <mono/metadata/tabledefs.h>
34 #include <mono/metadata/threads.h>
35 #include <mono/metadata/appdomain.h>
36 #include <mono/metadata/debug-helpers.h>
37 #include <mono/metadata/profiler-private.h>
38 #include <mono/metadata/mono-config.h>
39 #include <mono/metadata/environment.h>
40 #include <mono/metadata/mono-debug.h>
41 #include <mono/metadata/threads-types.h>
42 #include <mono/metadata/verify.h>
43 #include <mono/metadata/verify-internals.h>
44 #include <mono/metadata/mempool-internals.h>
45 #include <mono/metadata/attach.h>
46 #include <mono/metadata/gc-internals.h>
47 #include <mono/utils/mono-math.h>
48 #include <mono/utils/mono-compiler.h>
49 #include <mono/utils/mono-counters.h>
50 #include <mono/utils/mono-logger-internals.h>
51 #include <mono/utils/mono-mmap.h>
52 #include <mono/utils/dtrace.h>
54 #include "mini.h"
55 #include "mini-runtime.h"
56 #include <string.h>
57 #include <ctype.h>
58 #include "trace.h"
59 #include "version.h"
61 #include "jit-icalls.h"
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>
72 /* This is #define'd by Boehm GC to _GC_dlopen. */
73 #undef dlopen
75 void* dlopen(const char* path, int mode);
77 void
78 mono_runtime_install_handlers (void)
80 mono_runtime_posix_install_handlers ();
82 #if !defined (HOST_WATCHOS) && !defined (HOST_TVOS)
83 /* LLDB installs task-wide Mach exception handlers. XNU dispatches Mach
84 * exceptions first to any registered "activation" handler and then to
85 * any registered task handler before dispatching the exception to a
86 * host-wide Mach exception handler that does translation to POSIX
87 * signals. This makes it impossible to use LLDB with an
88 * implicit-null-check-enabled Mono; continuing execution after LLDB
89 * traps an EXC_BAD_ACCESS will result in LLDB's EXC_BAD_ACCESS handler
90 * being invoked again. This also interferes with the translation of
91 * SIGFPEs to .NET-level ArithmeticExceptions. Work around this here by
92 * installing a no-op task-wide Mach exception handler for
93 * EXC_BAD_ACCESS and EXC_ARITHMETIC.
95 kern_return_t kr = task_set_exception_ports (
96 mach_task_self (),
97 EXC_MASK_BAD_ACCESS | EXC_MASK_ARITHMETIC, /* SIGSEGV, SIGFPE */
98 MACH_PORT_NULL,
99 EXCEPTION_STATE_IDENTITY,
100 MACHINE_THREAD_STATE);
101 if (kr != KERN_SUCCESS)
102 g_warning ("mono_runtime_install_handlers: task_set_exception_ports failed");
103 #endif
105 /* Snow Leopard has a horrible bug: http://openradar.appspot.com/7209349
106 * This causes obscure SIGTRAP's for any application that comes across this built on
107 * Snow Leopard. This is a horrible hack to ensure that the private __CFInitialize
108 * is run on the main thread, so that we don't get SIGTRAPs later
110 #if defined (__APPLE__) && (defined (__i386__) || defined (__x86_64__))
112 void *handle = dlopen ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
113 if (handle == NULL)
114 return;
116 dlclose (handle);
118 #endif
121 gboolean
122 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoThreadInfo *info, void *sigctx)
124 kern_return_t ret;
125 mach_msg_type_number_t num_state, num_fpstate;
126 thread_state_t state, fpstate;
128 g_assert (info);
129 /*Zero enough state to make sure the caller doesn't confuse itself*/
130 tctx->valid = FALSE;
131 tctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = NULL;
132 tctx->unwind_data [MONO_UNWIND_DATA_LMF] = NULL;
133 tctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = NULL;
135 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
136 fpstate = (thread_state_t) alloca (mono_mach_arch_get_thread_fpstate_size ());
138 do {
139 ret = mono_mach_arch_get_thread_states (info->native_handle, state, &num_state, fpstate, &num_fpstate);
140 } while (ret == KERN_ABORTED);
141 if (ret != KERN_SUCCESS)
142 return FALSE;
144 mono_mach_arch_thread_states_to_mono_context (state, fpstate, &tctx->ctx);
146 /* mono_set_jit_tls () sets this */
147 void *jit_tls = mono_thread_info_tls_get (info, TLS_KEY_JIT_TLS);
148 /* SET_APPDOMAIN () sets this */
149 void *domain = mono_thread_info_tls_get (info, TLS_KEY_DOMAIN);
151 /*Thread already started to cleanup, can no longer capture unwind state*/
152 if (!jit_tls || !domain)
153 return FALSE;
156 * The current LMF address is kept in a separate TLS variable, and its hard to read its value without
157 * arch-specific code. But the address of the TLS variable is stored in another TLS variable which
158 * can be accessed through MonoThreadInfo.
160 /* mono_set_lmf_addr () sets this */
161 MonoLMF *lmf = NULL;
162 MonoLMF **addr = (MonoLMF**)mono_thread_info_tls_get (info, TLS_KEY_LMF_ADDR);
163 if (addr)
164 lmf = *addr;
166 tctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = domain;
167 tctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = jit_tls;
168 tctx->unwind_data [MONO_UNWIND_DATA_LMF] = lmf;
169 tctx->valid = TRUE;
171 return TRUE;