Merge pull request #2894 from marek-safar/mono.security
[mono.git] / mono / utils / mono-threads-mach.c
1 /*
2  * mono-threads-mach.c: Low-level threading, mach version
3  *
4  * Author:
5  *      Rodrigo Kumpera (kumpera@gmail.com)
6  *
7  * (C) 2011 Novell, Inc
8  */
9
10 #include "config.h"
11
12 /* For pthread_main_np, pthread_get_stackaddr_np and pthread_get_stacksize_np */
13 #if defined (__MACH__)
14 #define _DARWIN_C_SOURCE 1
15 #endif
16
17 #include <mono/utils/mono-threads.h>
18 #include <mono/utils/mono-mmap.h>
19
20 #if defined (USE_MACH_BACKEND)
21
22 #include <mono/utils/mach-support.h>
23 #include <mono/utils/mono-compiler.h>
24 #include <mono/utils/mono-threads.h>
25 #include <mono/utils/hazard-pointer.h>
26
27 void
28 mono_threads_init_platform (void)
29 {
30         mono_threads_init_dead_letter ();
31 }
32
33 #if defined(HOST_WATCHOS) || defined(HOST_TVOS)
34
35 gboolean
36 mono_threads_core_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
37 {
38         g_assert_not_reached ();
39 }
40
41 gboolean
42 mono_threads_core_check_suspend_result (MonoThreadInfo *info)
43 {
44         g_assert_not_reached ();
45 }
46
47 gboolean
48 mono_threads_core_begin_async_resume (MonoThreadInfo *info)
49 {
50         g_assert_not_reached ();
51 }
52
53 #else /* defined(HOST_WATCHOS) || defined(HOST_TVOS) */
54
55 gboolean
56 mono_threads_core_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
57 {
58         kern_return_t ret;
59         gboolean res;
60
61         g_assert (info);
62
63
64         do {
65                 ret = thread_suspend (info->native_handle);
66         } while (ret == KERN_ABORTED);
67
68         THREADS_SUSPEND_DEBUG ("SUSPEND %p -> %d\n", (void*)info->native_handle, ret);
69         if (ret != KERN_SUCCESS)
70                 return FALSE;
71
72         /* We're in the middle of a self-suspend, resume and register */
73         if (!mono_threads_transition_finish_async_suspend (info)) {
74                 mono_threads_add_to_pending_operation_set (info);
75                 do {
76                         ret = thread_resume (info->native_handle);
77                 } while (ret == KERN_ABORTED);
78                 g_assert (ret == KERN_SUCCESS);
79                 THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/1 %p -> %d\n", (void*)info->native_handle, 0);
80                 //XXX interrupt_kernel doesn't make sense in this case as the target is not in a syscall
81                 return TRUE;
82         }
83         res = mono_threads_get_runtime_callbacks ()->
84                 thread_state_init_from_handle (&info->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX], info);
85         THREADS_SUSPEND_DEBUG ("thread state %p -> %d\n", (void*)info->native_handle, res);
86         if (res) {
87                 if (interrupt_kernel)
88                         thread_abort (info->native_handle);
89         } else {
90                 mono_threads_transition_async_suspend_compensation (info);
91                 do {
92                         ret = thread_resume (info->native_handle);
93                 } while (ret == KERN_ABORTED);
94                 g_assert (ret == KERN_SUCCESS);
95                 THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/2 %p -> %d\n", (void*)info->native_handle, 0);
96         }
97         return res;
98 }
99
100 gboolean
101 mono_threads_core_check_suspend_result (MonoThreadInfo *info)
102 {
103         return TRUE;
104 }
105
106 gboolean
107 mono_threads_core_begin_async_resume (MonoThreadInfo *info)
108 {
109         kern_return_t ret;
110
111         if (info->async_target) {
112                 MonoContext tmp = info->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX].ctx;
113                 mach_msg_type_number_t num_state;
114                 thread_state_t state;
115                 ucontext_t uctx;
116                 mcontext_t mctx;
117
118                 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, info->async_target, info->user_data);
119                 info->user_data = NULL;
120                 info->async_target = (void (*)(void *)) info->user_data;
121
122                 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
123                 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
124
125                 do {
126                         ret = mono_mach_arch_get_thread_state (info->native_handle, state, &num_state);
127                 } while (ret == KERN_ABORTED);
128
129                 if (ret != KERN_SUCCESS)
130                         return FALSE;
131
132                 mono_mach_arch_thread_state_to_mcontext (state, mctx);
133                 uctx.uc_mcontext = mctx;
134                 mono_monoctx_to_sigctx (&tmp, &uctx);
135
136                 mono_mach_arch_mcontext_to_thread_state (mctx, state);
137
138                 do {
139                         ret = mono_mach_arch_set_thread_state (info->native_handle, state, num_state);
140                 } while (ret == KERN_ABORTED);
141
142                 if (ret != KERN_SUCCESS)
143                         return FALSE;
144         }
145
146         do {
147                 ret = thread_resume (info->native_handle);
148         } while (ret == KERN_ABORTED);
149         THREADS_SUSPEND_DEBUG ("RESUME %p -> %d\n", (void*)info->native_handle, ret);
150
151         return ret == KERN_SUCCESS;
152 }
153
154 #endif /* defined(HOST_WATCHOS) || defined(HOST_TVOS) */
155
156 void
157 mono_threads_platform_register (MonoThreadInfo *info)
158 {
159         char thread_name [64];
160
161         info->native_handle = mach_thread_self ();
162
163         snprintf (thread_name, sizeof (thread_name), "tid_%x", (int) info->native_handle);
164         pthread_setname_np (thread_name);
165
166         mono_threads_install_dead_letter ();
167 }
168
169 void
170 mono_threads_platform_free (MonoThreadInfo *info)
171 {
172         mach_port_deallocate (current_task (), info->native_handle);
173 }
174
175 #endif /* USE_MACH_BACKEND */
176
177 #ifdef __MACH__
178 void
179 mono_threads_core_get_stack_bounds (guint8 **staddr, size_t *stsize)
180 {
181         *staddr = (guint8*)pthread_get_stackaddr_np (pthread_self());
182         *stsize = pthread_get_stacksize_np (pthread_self());
183
184 #ifdef TARGET_OSX
185         /*
186          * Mavericks reports stack sizes as 512kb:
187          * http://permalink.gmane.org/gmane.comp.java.openjdk.hotspot.devel/11590
188          * https://bugs.openjdk.java.net/browse/JDK-8020753
189          */
190         if (pthread_main_np () && *stsize == 512 * 1024)
191                 *stsize = 2048 * mono_pagesize ();
192 #endif
193
194         /* staddr points to the start of the stack, not the end */
195         *staddr -= *stsize;
196 }
197
198 #endif