[sgen] Add debug option for valloc limit
[mono-project.git] / mono / utils / mono-threads-posix-signals.c
blob1dbb223608647398daaa6ff780b70fc868a889ad
1 /**
2 * \file
3 * Shared facility for Posix signals support
5 * Author:
6 * Ludovic Henry (ludovic@gmail.com)
8 * (C) 2015 Xamarin, Inc
9 */
11 #include <config.h>
12 #include <glib.h>
14 #include "mono-threads.h"
16 #if defined(USE_POSIX_BACKEND)
18 #include <errno.h>
19 #include <signal.h>
21 #include "mono-threads-debug.h"
23 gint
24 mono_threads_suspend_search_alternative_signal (void)
26 #if !defined (SIGRTMIN)
27 g_error ("signal search only works with RTMIN");
28 #else
29 int i;
30 /* we try to avoid SIGRTMIN and any one that might have been set already, see bug #75387 */
31 for (i = SIGRTMIN + 1; i < SIGRTMAX; ++i) {
32 struct sigaction sinfo;
33 sigaction (i, NULL, &sinfo);
34 if (sinfo.sa_handler == SIG_DFL && (void*)sinfo.sa_sigaction == (void*)SIG_DFL) {
35 return i;
38 g_error ("Could not find an available signal");
39 #endif
42 #ifndef __native_client__
44 static int suspend_signal_num = -1;
45 static int restart_signal_num = -1;
46 static int abort_signal_num = -1;
48 static sigset_t suspend_signal_mask;
49 static sigset_t suspend_ack_signal_mask;
51 static void
52 signal_add_handler (int signo, void (*handler)(int, siginfo_t *, void *), int flags)
54 struct sigaction sa;
55 int ret;
57 sa.sa_sigaction = handler;
58 sigfillset (&sa.sa_mask);
59 sa.sa_flags = SA_SIGINFO | flags;
60 ret = sigaction (signo, &sa, NULL);
61 g_assert (ret != -1);
64 static int
65 abort_signal_get (void)
67 #if defined(PLATFORM_ANDROID)
68 return SIGTTIN;
69 #elif defined (SIGRTMIN)
70 static int abort_signum = -1;
71 if (abort_signum == -1)
72 abort_signum = mono_threads_suspend_search_alternative_signal ();
73 return abort_signum;
74 #elif defined (SIGTTIN)
75 return SIGTTIN;
76 #else
77 g_error ("unable to get abort signal");
78 #endif
81 static int
82 suspend_signal_get (void)
84 #if defined(PLATFORM_ANDROID)
85 return SIGPWR;
86 #elif defined (SIGRTMIN)
87 static int suspend_signum = -1;
88 if (suspend_signum == -1)
89 suspend_signum = mono_threads_suspend_search_alternative_signal ();
90 return suspend_signum;
91 #else
92 #if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
93 return SIGXFSZ;
94 #else
95 return SIGPWR;
96 #endif
97 #endif
100 static int
101 restart_signal_get (void)
103 #if defined(PLATFORM_ANDROID)
104 return SIGXCPU;
105 #elif defined (SIGRTMIN)
106 static int restart_signum = -1;
107 if (restart_signum == -1)
108 restart_signum = mono_threads_suspend_search_alternative_signal ();
109 return restart_signum;
110 #else
111 return SIGXCPU;
112 #endif
115 static void
116 restart_signal_handler (int _dummy, siginfo_t *_info, void *context)
118 MonoThreadInfo *info;
119 int old_errno = errno;
121 info = mono_thread_info_current ();
122 info->signal = restart_signal_num;
123 errno = old_errno;
126 static void
127 suspend_signal_handler (int _dummy, siginfo_t *info, void *context)
129 int old_errno = errno;
130 int hp_save_index = mono_hazard_pointer_save_for_signal_handler ();
132 MonoThreadInfo *current = mono_thread_info_current ();
134 THREADS_SUSPEND_DEBUG ("SIGNAL HANDLER FOR %p [%p]\n", mono_thread_info_get_tid (current), (void*)current->native_handle);
135 if (current->syscall_break_signal) {
136 current->syscall_break_signal = FALSE;
137 THREADS_SUSPEND_DEBUG ("\tsyscall break for %p\n", mono_thread_info_get_tid (current));
138 mono_threads_notify_initiator_of_abort (current);
139 goto done;
142 /* Have we raced with self suspend? */
143 if (!mono_threads_transition_finish_async_suspend (current)) {
144 current->suspend_can_continue = TRUE;
145 THREADS_SUSPEND_DEBUG ("\tlost race with self suspend %p\n", mono_thread_info_get_tid (current));
146 goto done;
150 * If the thread is starting, then thread_state_init_from_sigctx returns FALSE,
151 * as the thread might have been attached without the domain or lmf having been
152 * initialized yet.
154 * One way to fix that is to keep the thread suspended (wait for the restart
155 * signal), and make sgen aware that even if a thread might be suspended, there
156 * would be cases where you cannot scan its stack/registers. That would in fact
157 * consist in removing the async suspend compensation, and treat the case directly
158 * in sgen. That's also how it was done in the sgen specific suspend code.
161 /* thread_state_init_from_sigctx return FALSE if the current thread is starting or detaching and suspend can't continue. */
162 current->suspend_can_continue = mono_threads_get_runtime_callbacks ()->thread_state_init_from_sigctx (&current->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX], context);
164 if (!current->suspend_can_continue)
165 THREADS_SUSPEND_DEBUG ("\tThread is starting or detaching, failed to capture state %p\n", mono_thread_info_get_tid (current));
168 Block the restart signal.
169 We need to block the restart signal while posting to the suspend_ack semaphore or we race to sigsuspend,
170 which might miss the signal and get stuck.
172 pthread_sigmask (SIG_BLOCK, &suspend_ack_signal_mask, NULL);
174 /* We're done suspending */
175 mono_threads_notify_initiator_of_suspend (current);
177 do {
178 current->signal = 0;
179 sigsuspend (&suspend_signal_mask);
180 } while (current->signal != restart_signal_num);
182 /* Unblock the restart signal. */
183 pthread_sigmask (SIG_UNBLOCK, &suspend_ack_signal_mask, NULL);
185 if (current->async_target) {
186 #if MONO_ARCH_HAS_MONO_CONTEXT
187 MonoContext tmp = current->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX].ctx;
188 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, current->async_target, current->user_data);
189 current->user_data = NULL;
190 current->async_target = NULL;
191 mono_monoctx_to_sigctx (&tmp, context);
192 #else
193 g_error ("The new interruption machinery requires a working mono-context");
194 #endif
197 /* We're done resuming */
198 mono_threads_notify_initiator_of_resume (current);
200 done:
201 mono_hazard_pointer_restore_for_signal_handler (hp_save_index);
202 errno = old_errno;
205 void
206 mono_threads_suspend_init_signals (void)
208 sigset_t signal_set;
210 sigemptyset (&signal_set);
212 /* add suspend signal */
213 suspend_signal_num = suspend_signal_get ();
215 signal_add_handler (suspend_signal_num, suspend_signal_handler, SA_RESTART);
217 sigaddset (&signal_set, suspend_signal_num);
219 /* add restart signal */
220 restart_signal_num = restart_signal_get ();
222 sigfillset (&suspend_signal_mask);
223 sigdelset (&suspend_signal_mask, restart_signal_num);
225 sigemptyset (&suspend_ack_signal_mask);
226 sigaddset (&suspend_ack_signal_mask, restart_signal_num);
228 signal_add_handler (restart_signal_num, restart_signal_handler, SA_RESTART);
230 sigaddset (&signal_set, restart_signal_num);
232 /* add abort signal */
233 abort_signal_num = abort_signal_get ();
235 /* the difference between abort and suspend here is made by not
236 * passing SA_RESTART, meaning we won't restart the syscall when
237 * receiving a signal */
238 signal_add_handler (abort_signal_num, suspend_signal_handler, 0);
240 sigaddset (&signal_set, abort_signal_num);
242 /* ensure all the new signals are unblocked */
243 sigprocmask (SIG_UNBLOCK, &signal_set, NULL);
246 On 32bits arm Android, signals with values >=32 are not usable as their headers ship a broken sigset_t.
247 See 5005c6f3fbc1da584c6a550281689cc23f59fe6d for more details.
249 #ifdef PLATFORM_ANDROID
250 g_assert (suspend_signal_num < 32);
251 g_assert (restart_signal_num < 32);
252 g_assert (abort_signal_num < 32);
253 #endif
256 gint
257 mono_threads_suspend_get_suspend_signal (void)
259 g_assert (suspend_signal_num != -1);
260 return suspend_signal_num;
263 gint
264 mono_threads_suspend_get_restart_signal (void)
266 g_assert (restart_signal_num != -1);
267 return restart_signal_num;
270 gint
271 mono_threads_suspend_get_abort_signal (void)
273 g_assert (abort_signal_num != -1);
274 return abort_signal_num;
277 #else
279 void
280 mono_threads_suspend_init_signals (void)
282 g_assert_not_reached ();
285 gint
286 mono_threads_suspend_get_suspend_signal (void)
288 return -1;
291 gint
292 mono_threads_suspend_get_restart_signal (void)
294 return -1;
297 gint
298 mono_threads_suspend_get_abort_signal (void)
300 return -1;
303 #endif /* __native_client__ */
306 #endif /* defined(USE_POSIX_BACKEND) */