forked from ocaml-flambda/flambda-backend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsignals.c
538 lines (488 loc) · 14.4 KB
/
signals.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
#define CAML_INTERNALS
/* Signal handling, code common to the bytecode and native systems */
#include <signal.h>
#include <errno.h>
#include "caml/alloc.h"
#include "caml/callback.h"
#include "caml/config.h"
#include "caml/fail.h"
#include "caml/memory.h"
#include "caml/misc.h"
#include "caml/mlvalues.h"
#include "caml/roots.h"
#include "caml/signals.h"
#include "caml/sys.h"
#include "caml/memprof.h"
#include "caml/finalise.h"
/* The set of pending signals (received but not yet processed).
It is represented as a bit vector.
Valid signal numbers range from 1 to NSIG - 1 included.
(This is checked when we install a signal handler.)
Signal 1 is the least significant bit of caml_pending_signals[0]. */
CAMLexport atomic_uintnat caml_pending_signals[NSIG_WORDS];
static caml_plat_mutex signal_install_mutex = CAML_PLAT_MUTEX_INITIALIZER;
int caml_check_pending_signals(void)
{
int i;
/* [MM] This fence compensates for the fact that Caml_check_gc_interrupt
reads young_limit with a relaxed load. It is possible in theory to
see young_limit updated without caml_pending_signals being set
and then resetting young_limit after the check. This would delay
processing the pending signal until young_limit is updated again.
There may be nicer ways to address this scenario. */
atomic_thread_fence(memory_order_acquire);
for (i = 0; i < NSIG_WORDS; i++) {
if (atomic_load_explicit(&caml_pending_signals[i], memory_order_relaxed))
return 1;
}
return 0;
}
/* Execute all pending signals */
CAMLexport value caml_process_pending_signals_exn(void)
{
int i, j, signo;
uintnat curr, mask ;
value exn;
#ifdef POSIX_SIGNALS
sigset_t set;
#endif
/* Check that there is indeed a pending signal before issuing the
syscall in [pthread_sigmask]. */
if (!caml_check_pending_signals())
return Val_unit;
#ifdef POSIX_SIGNALS
pthread_sigmask(/* dummy */ SIG_BLOCK, NULL, &set);
#endif
for (i = 0; i < NSIG_WORDS; i++) {
curr = atomic_load_explicit(&caml_pending_signals[i],
memory_order_relaxed);
if (curr == 0) goto next_word;
/* Scan curr for bits set */
for (j = 0; j < BITS_PER_WORD; j++) {
mask = (uintnat)1 << j;
if ((curr & mask) == 0) goto next_bit;
signo = i * 8 + j + 1;
#ifdef POSIX_SIGNALS
if (sigismember(&set, signo)) goto next_bit;
#endif
while (! atomic_compare_exchange_strong(&caml_pending_signals[i],
&curr, curr & ~mask)) {
/* curr was refreshed, test it again */
if (curr == 0) goto next_word;
if ((curr & mask) == 0) goto next_bit;
}
exn = caml_execute_signal_exn(signo, 0);
if (Is_exception_result(exn)) return exn;
/* curr probably changed during the evaluation of the signal handler;
refresh it from memory */
curr = atomic_load_explicit(&caml_pending_signals[i],
memory_order_relaxed);
if (curr == 0) goto next_word;
next_bit: /* skip */;
}
next_word: /* skip */;
}
return Val_unit;
}
/* Record the delivery of a signal, and arrange for it to be processed
as soon as possible:
- via the pending signal bitvector, processed in
caml_process_pending_signals_exn.
- by playing with the allocation limit, processed in
caml_garbage_collection
*/
CAMLexport void caml_record_signal(int signal_number)
{
unsigned int i;
if (signal_number <= 0 || signal_number >= NSIG) return;
i = signal_number - 1;
atomic_fetch_or(&caml_pending_signals[i / BITS_PER_WORD],
(uintnat)1 << (i % BITS_PER_WORD));
caml_interrupt_self();
}
/* Management of blocking sections. */
static void caml_enter_blocking_section_default(void)
{
caml_bt_exit_ocaml();
caml_release_domain_lock();
}
static void caml_leave_blocking_section_default(void)
{
caml_bt_enter_ocaml();
caml_acquire_domain_lock();
}
CAMLexport void (*caml_enter_blocking_section_hook)(void) =
caml_enter_blocking_section_default;
CAMLexport void (*caml_leave_blocking_section_hook)(void) =
caml_leave_blocking_section_default;
CAMLexport void caml_enter_blocking_section(void)
{
while (1){
/* Process all pending signals now */
caml_raise_if_exception(caml_process_pending_signals_exn());
caml_enter_blocking_section_hook ();
/* Check again for pending signals.
If none, done; otherwise, try again */
if (!caml_check_pending_signals()) break;
caml_leave_blocking_section_hook ();
}
}
CAMLexport void caml_enter_blocking_section_no_pending(void)
{
caml_enter_blocking_section_hook ();
}
CAMLexport void caml_leave_blocking_section(void)
{
int saved_errno;
/* Save the value of errno (PR#5982). */
saved_errno = errno;
caml_leave_blocking_section_hook ();
errno = saved_errno;
}
static value caml_signal_handlers;
void caml_init_signal_handling(void) {
mlsize_t i;
caml_signal_handlers = caml_alloc_shr(NSIG, 0);
for (i = 0; i < NSIG; i++)
Field(caml_signal_handlers, i) = Val_unit;
caml_register_generational_global_root(&caml_signal_handlers);
}
/* Execute a signal handler immediately */
value caml_execute_signal_exn(int signal_number, int in_signal_handler)
{
value res;
value handler;
#ifdef POSIX_SIGNALS
sigset_t nsigs, sigs;
/* Block the signal before executing the handler, and record in sigs
the original signal mask */
sigemptyset(&nsigs);
sigaddset(&nsigs, signal_number);
pthread_sigmask(SIG_BLOCK, &nsigs, &sigs);
#endif
handler = Field(caml_signal_handlers, signal_number);
res = caml_callback_exn(
handler,
Val_int(caml_rev_convert_signal_number(signal_number)));
#ifdef POSIX_SIGNALS
if (! in_signal_handler) {
/* Restore the original signal mask */
pthread_sigmask(SIG_SETMASK, &sigs, NULL);
} else if (Is_exception_result(res)) {
/* Restore the original signal mask and unblock the signal itself */
sigdelset(&sigs, signal_number);
pthread_sigmask(SIG_SETMASK, &sigs, NULL);
}
#endif
return res;
}
/* Arrange for a garbage collection to be performed as soon as possible */
void caml_request_major_slice (void)
{
Caml_state->requested_major_slice = 1;
caml_interrupt_self();
}
void caml_request_minor_gc (void)
{
Caml_state->requested_minor_gc = 1;
caml_interrupt_self();
}
CAMLexport int caml_check_pending_actions(void)
{
return (Caml_check_gc_interrupt(Caml_state) ||
caml_check_pending_signals());
}
CAMLexport void caml_process_pending_actions(void)
{
caml_handle_gc_interrupt();
caml_raise_if_exception(caml_process_pending_signals_exn());
}
value caml_process_pending_actions_with_root(value root)
{
if (caml_check_pending_actions()) {
CAMLparam1(root);
caml_process_pending_actions();
CAMLdrop;
}
return root;
}
value caml_process_pending_actions_with_root_exn(value root)
{
/* FIXME: call handle_gc_interrupt and finalisers */
if (caml_check_pending_signals()) {
CAMLparam1(root);
value exn = caml_process_pending_signals_exn();
if (Is_exception_result(exn)) CAMLreturn(exn);
CAMLdrop;
}
return root;
}
/* FIXME: not implemented (see above)
CAMLexport value caml_process_pending_actions_exn(void)
{
return caml_process_pending_actions_with_root_exn(Val_unit);
}
*/
/* OS-independent numbering of signals */
#ifndef SIGABRT
#define SIGABRT -1
#endif
#ifndef SIGALRM
#define SIGALRM -1
#endif
#ifndef SIGFPE
#define SIGFPE -1
#endif
#ifndef SIGHUP
#define SIGHUP -1
#endif
#ifndef SIGILL
#define SIGILL -1
#endif
#ifndef SIGINT
#define SIGINT -1
#endif
#ifndef SIGKILL
#define SIGKILL -1
#endif
#ifndef SIGPIPE
#define SIGPIPE -1
#endif
#ifndef SIGQUIT
#define SIGQUIT -1
#endif
#ifndef SIGSEGV
#define SIGSEGV -1
#endif
#ifndef SIGTERM
#define SIGTERM -1
#endif
#ifndef SIGUSR1
#define SIGUSR1 -1
#endif
#ifndef SIGUSR2
#define SIGUSR2 -1
#endif
#ifndef SIGCHLD
#define SIGCHLD -1
#endif
#ifndef SIGCONT
#define SIGCONT -1
#endif
#ifndef SIGSTOP
#define SIGSTOP -1
#endif
#ifndef SIGTSTP
#define SIGTSTP -1
#endif
#ifndef SIGTTIN
#define SIGTTIN -1
#endif
#ifndef SIGTTOU
#define SIGTTOU -1
#endif
#ifndef SIGVTALRM
#define SIGVTALRM -1
#endif
#ifndef SIGPROF
#define SIGPROF -1
#endif
#ifndef SIGBUS
#define SIGBUS -1
#endif
#ifndef SIGPOLL
#define SIGPOLL -1
#endif
#ifndef SIGSYS
#define SIGSYS -1
#endif
#ifndef SIGTRAP
#define SIGTRAP -1
#endif
#ifndef SIGURG
#define SIGURG -1
#endif
#ifndef SIGXCPU
#define SIGXCPU -1
#endif
#ifndef SIGXFSZ
#define SIGXFSZ -1
#endif
static int posix_signals[] = {
SIGABRT, SIGALRM, SIGFPE, SIGHUP, SIGILL, SIGINT, SIGKILL, SIGPIPE,
SIGQUIT, SIGSEGV, SIGTERM, SIGUSR1, SIGUSR2, SIGCHLD, SIGCONT,
SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGVTALRM, SIGPROF, SIGBUS,
SIGPOLL, SIGSYS, SIGTRAP, SIGURG, SIGXCPU, SIGXFSZ
};
CAMLexport int caml_convert_signal_number(int signo)
{
if (signo < 0 && signo >= -(sizeof(posix_signals) / sizeof(int)))
return posix_signals[-signo-1];
else
return signo;
}
CAMLexport int caml_rev_convert_signal_number(int signo)
{
int i;
for (i = 0; i < sizeof(posix_signals) / sizeof(int); i++)
if (signo == posix_signals[i]) return -i - 1;
return signo;
}
int caml_init_signal_stack(void)
{
#ifdef POSIX_SIGNALS
stack_t stk;
stk.ss_flags = 0;
stk.ss_size = SIGSTKSZ;
/* The memory used for the alternate signal stack must not free'd before
calling sigaltstack with SS_DISABLE. malloc is therefore used rather
than caml_stat_alloc_noexc so that if a shutdown path erroneously fails
to call caml_free_signal_stack then we have a memory leak rather than a
nasty piece of undefined behaviour forced on the caller. */
stk.ss_sp = malloc(stk.ss_size);
if(stk.ss_sp == NULL) {
return -1;
}
if (sigaltstack(&stk, NULL) < 0) {
free(stk.ss_sp);
return -1;
}
/* gprof installs a signal handler for SIGPROF.
Make it run on the alternate signal stack, to prevent segfaults. */
{
struct sigaction act;
sigaction(SIGPROF, NULL, &act);
if ((act.sa_flags & SA_SIGINFO) ||
(act.sa_handler != SIG_IGN && act.sa_handler != SIG_DFL)) {
/* found a handler */
if ((act.sa_flags & SA_ONSTACK) == 0) {
act.sa_flags |= SA_ONSTACK;
sigaction(SIGPROF, &act, NULL);
}
}
}
#endif
return 0;
}
void caml_free_signal_stack(void)
{
#ifdef POSIX_SIGNALS
stack_t stk, disable = {0};
disable.ss_flags = SS_DISABLE;
/* POSIX says ss_size is ignored when SS_DISABLE is set,
but OSX/Darwin fails if the size isn't set. */
disable.ss_size = SIGSTKSZ;
if (sigaltstack(&disable, &stk) < 0) {
caml_fatal_error("Failed to reset signal stack (err %d)", errno);
}
/* Memory was allocated with malloc directly; see caml_init_signal_stack */
free(stk.ss_sp);
#endif
}
/* Installation of a signal handler (as per [Sys.signal]) */
static void handle_signal(int signal_number)
{
int saved_errno;
/* Save the value of errno (PR#5982). */
saved_errno = errno;
#if !defined(POSIX_SIGNALS) && !defined(BSD_SIGNALS)
signal(signal_number, handle_signal);
#endif
caml_record_signal(signal_number);
errno = saved_errno;
}
static int caml_set_signal_action(int signo, int action)
{
void (*act)(int signo), (*oldact)(int signo);
#ifdef POSIX_SIGNALS
struct sigaction sigact, oldsigact;
#endif
switch (action) {
case 0: act = SIG_DFL; break;
case 1: act = SIG_IGN; break;
default: act = handle_signal; break;
}
#ifdef POSIX_SIGNALS
sigact.sa_handler = act;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
if (sigaction(signo, &sigact, &oldsigact) == -1) return -1;
oldact = oldsigact.sa_handler;
#else
oldact = signal(signo, act);
if (oldact == SIG_ERR) return -1;
#endif
if (oldact == handle_signal)
return 2;
else if (oldact == SIG_IGN)
return 1;
else
return 0;
}
CAMLprim value caml_install_signal_handler(value signal_number, value action)
{
CAMLparam2 (signal_number, action);
CAMLlocal2 (res, tmp_signal_handlers);
int sig, act, oldact;
sig = caml_convert_signal_number(Int_val(signal_number));
if (sig <= 0 || sig >= NSIG)
caml_invalid_argument("Sys.signal: unavailable signal");
switch(action) {
case Val_int(0): /* Signal_default */
act = 0;
break;
case Val_int(1): /* Signal_ignore */
act = 1;
break;
default: /* Signal_handle */
act = 2;
break;
}
oldact = caml_set_signal_action(sig, act);
switch (oldact) {
case 0: /* was Signal_default */
res = Val_int(0);
break;
case 1: /* was Signal_ignore */
res = Val_int(1);
break;
case 2: /* was Signal_handle */
res = caml_alloc_small (1, 0);
Field(res, 0) = Field(caml_signal_handlers, sig);
break;
default: /* error in caml_set_signal_action */
caml_sys_error(NO_ARG);
}
if (Is_block(action)) {
/* Speculatively allocate this so we don't hold the lock for
a GC */
if (caml_signal_handlers == 0) {
tmp_signal_handlers = caml_alloc(NSIG, 0);
}
caml_plat_lock(&signal_install_mutex);
if (caml_signal_handlers == 0) {
/* caml_alloc cannot raise asynchronous exceptions from signals
so this is safe */
caml_signal_handlers = tmp_signal_handlers;
caml_register_global_root(&caml_signal_handlers);
}
caml_modify(&Field(caml_signal_handlers, sig), Field(action, 0));
caml_plat_unlock(&signal_install_mutex);
}
caml_raise_if_exception(caml_process_pending_signals_exn());
CAMLreturn (res);
}