-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathomrsignal.c
2024 lines (1734 loc) · 69.6 KB
/
omrsignal.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 1991, 2021 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
/**
* @file
* @ingroup Port
* @brief Signal handling
*/
#include "omrcfg.h"
#include "omrport.h"
#include "omrutil.h"
#include "omrutilbase.h"
#include "omrportpriv.h"
#include "ut_omrport.h"
#include "omrthread.h"
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <setjmp.h>
#include <errno.h>
#include "omrsignal_context.h"
#if !defined(J9ZOS390)
#if defined(J9OS_I5) && defined(J9OS_I5_V5R4)
#include "semaphore_i5.h"
#else /* defined(J9OS_I5) && defined(J9OS_I5_V5R4) */
#include <semaphore.h>
#endif /* defined(J9OS_I5) && defined(J9OS_I5_V5R4) */
#endif /* !defined(J9ZOS390) */
#if defined(J9ZOS390)
#include <pthread.h>
#endif /* defined(J9ZOS390) */
#if defined(J9ZOS390)
#if defined(OMR_ENV_DATA64)
#include <__le_api.h>
#else /* defined(OMR_ENV_DATA64) */
#include <leawi.h>
#include <ceeedcct.h>
#endif /* defined(OMR_ENV_DATA64) */
#if defined(OMR_PORT_ZOS_CEEHDLRSUPPORT)
#include "omrsignal_ceehdlr.h"
#endif /* defined(OMR_PORT_ZOS_CEEHDLRSUPPORT) */
#endif /* defined(J9ZOS390) */
#if defined(OMRPORT_OMRSIG_SUPPORT)
#include "omrsig.h"
#endif /* defined(OMRPORT_OMRSIG_SUPPORT) */
#if defined(S390) && defined(LINUX)
typedef void (*unix_sigaction)(int, siginfo_t *, void *, uintptr_t);
#else /* defined(S390) && defined(LINUX) */
typedef void (*unix_sigaction)(int, siginfo_t *, void *);
#endif /* defined(S390) && defined(LINUX) */
#define ARRAY_SIZE_SIGNALS (MAX_UNIX_SIGNAL_TYPES + 1)
/* Keep track of signal counts. */
static volatile uintptr_t signalCounts[ARRAY_SIZE_SIGNALS] = {0};
/* Store the previous signal handlers. We need to restore them during shutdown. */
static struct {
struct sigaction action;
uint32_t restore;
} oldActions[ARRAY_SIZE_SIGNALS];
/* Records the (port library defined) signals for which a handler is registered.
* Access to these variables must be protected by the registerHandlerMonitor.
*
* syncSignalsWithHandlers represents if handlers are registered with synchronous
* signals, and asyncSignalsWithHandlers represents if handlers are registered with
* asynchronous signals.
*/
static uint32_t syncSignalsWithHandlers;
static uint32_t asyncSignalsWithHandlers;
/* Records the (port library defined) signals for which a main handler is
* registered. A main handler can be either mainSynchSignalHandler or
* mainASynchSignalHandler. A signal can only be associated to one main
* handler. If a main handler is already registered for a signal, then avoid
* re-registering a main handler for that signal. Access to these variables
* must be protected by the registerHandlerMonitor.
*
* syncSignalsWithMainHandlers represents if the synchronous main handler is
* registered with synchronous signals, and asyncSignalsWithMainHandlers
* represents if the asynchronous main handler is registered with asynchronous
* signals.
*/
static uint32_t syncSignalsWithMainHandlers;
static uint32_t asyncSignalsWithMainHandlers;
#if defined(OMR_PORT_ASYNC_HANDLER)
static uint32_t shutDownASynchReporter;
#endif
static uint32_t attachedPortLibraries;
typedef struct OMRUnixAsyncHandlerRecord {
OMRPortLibrary *portLib;
omrsig_handler_fn handler;
void *handler_arg;
uint32_t flags;
struct OMRUnixAsyncHandlerRecord *next;
} OMRUnixAsyncHandlerRecord;
/* holds the options set by omrsig_set_options */
uint32_t signalOptionsGlobal;
static OMRUnixAsyncHandlerRecord *asyncHandlerList;
#if !defined(J9ZOS390)
#if defined(OSX)
#define SIGSEM_T sem_t *
#define SIGSEM_POST(_sem) sem_post(_sem)
#define SIGSEM_ERROR SEM_FAILED
#define SIGSEM_INIT(_sem, _name) ((_sem) = sem_open((_name), O_CREAT|O_EXCL, S_IRWXU, 0))
#define SIGSEM_UNLINK(_name) sem_unlink(_name)
#define SIGSEM_DESTROY(_sem) sem_close(_sem)
#define SIGSEM_WAIT(_sem) sem_wait(_sem)
#define SIGSEM_TRY_WAIT(_sem) sem_trywait(_sem)
#else /* defined(OSX) */
#define SIGSEM_T sem_t
#define SIGSEM_POST(_sem) sem_post(&(_sem))
#define SIGSEM_ERROR -1
#define SIGSEM_INIT(_sem, _name) sem_init(&(_sem), 0, 0)
#define SIGSEM_UNLINK(_name) do { /* do nothing */ } while (0)
#define SIGSEM_DESTROY(_sem) sem_destroy(&(_sem))
#define SIGSEM_WAIT(_sem) sem_wait(&(_sem))
#define SIGSEM_TRY_WAIT(_sem) sem_trywait(&(_sem))
#endif /* defined(OSX) */
static SIGSEM_T wakeUpASyncReporter;
#else /* !defined(J9ZOS390) */
/* The asyncSignalReporter synchronization has to be done differently on ZOS.
* z/OS does not have system semaphores, yet we can't rely on thread library
* being present for the mainASyncSignalHandler, so use pthread synchronization.
* Note: the use of pthread functions is allowed on z/OS
*/
static pthread_cond_t wakeUpASyncReporterCond;
static pthread_mutex_t wakeUpASyncReporterMutex;
#endif /* !defined(J9ZOS390) */
static omrthread_monitor_t asyncMonitor;
static omrthread_monitor_t registerHandlerMonitor;
static omrthread_monitor_t asyncReporterShutdownMonitor;
static uint32_t asyncThreadCount;
static uint32_t attachedPortLibraries;
struct OMRSignalHandlerRecord {
struct OMRSignalHandlerRecord *previous;
struct OMRPortLibrary *portLibrary;
omrsig_handler_fn handler;
void *handler_arg;
sigjmp_buf returnBuf;
#if defined(J9ZOS390)
struct __jumpinfo farJumpInfo;
#endif /* defined(J9ZOS390) */
uint32_t flags;
} OMRSignalHandlerRecord;
typedef struct OMRCurrentSignal {
int signal;
siginfo_t *sigInfo;
void *contextInfo;
#if defined(S390) && defined(LINUX)
uintptr_t breakingEventAddr;
#endif /* defined(S390) && defined(LINUX) */
uint32_t portLibSignalType;
} OMRCurrentSignal;
/* key to get the end of the synchronous handler records */
static omrthread_tls_key_t tlsKey;
/* key to get the current synchronous signal */
static omrthread_tls_key_t tlsKeyCurrentSignal;
static struct {
uint32_t portLibSignalNo;
int unixSignalNo;
} signalMap[] = {
{OMRPORT_SIG_FLAG_SIGSEGV, SIGSEGV},
{OMRPORT_SIG_FLAG_SIGBUS, SIGBUS},
{OMRPORT_SIG_FLAG_SIGILL, SIGILL},
{OMRPORT_SIG_FLAG_SIGFPE, SIGFPE},
{OMRPORT_SIG_FLAG_SIGTRAP, SIGTRAP},
{OMRPORT_SIG_FLAG_SIGQUIT, SIGQUIT},
{OMRPORT_SIG_FLAG_SIGABRT, SIGABRT},
{OMRPORT_SIG_FLAG_SIGTERM, SIGTERM},
{OMRPORT_SIG_FLAG_SIGXFSZ, SIGXFSZ},
{OMRPORT_SIG_FLAG_SIGINT, SIGINT},
{OMRPORT_SIG_FLAG_SIGHUP, SIGHUP},
{OMRPORT_SIG_FLAG_SIGCONT, SIGCONT},
{OMRPORT_SIG_FLAG_SIGWINCH, SIGWINCH},
{OMRPORT_SIG_FLAG_SIGPIPE, SIGPIPE},
{OMRPORT_SIG_FLAG_SIGALRM, SIGALRM},
{OMRPORT_SIG_FLAG_SIGCHLD, SIGCHLD},
{OMRPORT_SIG_FLAG_SIGTSTP, SIGTSTP},
{OMRPORT_SIG_FLAG_SIGUSR1, SIGUSR1},
{OMRPORT_SIG_FLAG_SIGUSR2, SIGUSR2},
{OMRPORT_SIG_FLAG_SIGURG, SIGURG},
{OMRPORT_SIG_FLAG_SIGXCPU, SIGXCPU},
{OMRPORT_SIG_FLAG_SIGVTALRM, SIGVTALRM},
{OMRPORT_SIG_FLAG_SIGPROF, SIGPROF},
{OMRPORT_SIG_FLAG_SIGIO, SIGIO},
{OMRPORT_SIG_FLAG_SIGSYS, SIGSYS},
{OMRPORT_SIG_FLAG_SIGTTIN, SIGTTIN},
{OMRPORT_SIG_FLAG_SIGTTOU, SIGTTOU}
#if defined(SIGINFO)
, {OMRPORT_SIG_FLAG_SIGINFO, SIGINFO}
#endif /* defined(SIGINFO) */
#if defined(SIGIOT)
, {OMRPORT_SIG_FLAG_SIGIOT, SIGIOT}
#endif /* defined(SIGIOT) */
#if defined(SIGPOLL)
, {OMRPORT_SIG_FLAG_SIGPOLL, SIGPOLL}
#endif /* defined(SIGPOLL) */
#if defined(SIGRECONFIG)
, {OMRPORT_SIG_FLAG_SIGRECONFIG, SIGRECONFIG}
#endif /* defined(SIGRECONFIG) */
#if defined(SIGABND)
, {OMRPORT_SIG_FLAG_SIGABEND, SIGABND}
#endif /* defined(SIGABND) */
};
static omrthread_t asynchSignalReporterThread = NULL;
static int32_t registerMainHandlers(OMRPortLibrary *portLibrary, uint32_t flags, uint32_t allowedSubsetOfFlags, void **oldOSHandler);
static void removeAsyncHandlers(OMRPortLibrary *portLibrary);
static uint32_t mapOSSignalToPortLib(uint32_t signalNo, siginfo_t *sigInfo);
#if defined(J9ZOS390)
static intptr_t addAsyncSignalsToSet(sigset_t *ss);
#endif /* defined(J9ZOS390) */
#if defined(OMR_PORT_ASYNC_HANDLER)
static void runHandlers(uint32_t asyncSignalFlag, int unixSignal);
static int J9THREAD_PROC asynchSignalReporter(void *userData);
#endif /* defined(OMR_PORT_ASYNC_HANDLER) */
static int32_t registerSignalHandlerWithOS(OMRPortLibrary *portLibrary, uint32_t portLibrarySignalNo, unix_sigaction handler, void **oldOSHandler);
static uint32_t destroySignalTools(OMRPortLibrary *portLibrary);
static int mapPortLibSignalToOSSignal(uint32_t portLibSignal);
static uint32_t countInfoInCategory(struct OMRPortLibrary *portLibrary, void *info, uint32_t category);
static void sig_full_shutdown(struct OMRPortLibrary *portLibrary);
static int32_t initializeSignalTools(OMRPortLibrary *portLibrary);
#if defined(S390) && defined(LINUX)
static void mainSynchSignalHandler(int signal, siginfo_t *sigInfo, void *contextInfo, uintptr_t breakingEventAddr);
static void mainASynchSignalHandler(int signal, siginfo_t *sigInfo, void *contextInfo, uintptr_t nullArg);
#else /* defined(S390) && defined(LINUX) */
static void mainSynchSignalHandler(int signal, siginfo_t *sigInfo, void *contextInfo);
static void mainASynchSignalHandler(int signal, siginfo_t *sigInfo, void *contextInfo);
#endif /* defined(S390) && defined(LINUX) */
static int32_t unblockSignal(int signal);
static void setBitMaskSignalsWithHandlers(uint32_t flags);
static void unsetBitMaskSignalsWithHandlers(uint32_t flags);
static void setBitMaskSignalsWithMainHandlers(uint32_t flags);
static void unsetBitMaskSignalsWithMainHandlers(uint32_t flags);
static BOOLEAN checkForAmbiguousSignalFlags(uint32_t flags, const char *functionName);
int32_t
omrsig_can_protect(struct OMRPortLibrary *portLibrary, uint32_t flags)
{
uint32_t supportedFlags = OMRPORT_SIG_FLAG_MAY_RETURN;
Trc_PRT_signal_omrsig_can_protect_entered(flags);
if (checkForAmbiguousSignalFlags(flags, "omrsig_can_protect")) {
return OMRPORT_SIG_ERROR;
}
#if !defined(J9ZOS390)
supportedFlags |= OMRPORT_SIG_FLAG_MAY_CONTINUE_EXECUTION;
#else /* !defined(J9ZOS390) */
if (TRUE == PPG_resumableTrapsSupported) {
Trc_PRT_sig_can_protect_OMRPORT_SIG_FLAG_MAY_CONTINUE_EXECUTION_supported();
supportedFlags |= OMRPORT_SIG_FLAG_MAY_CONTINUE_EXECUTION;
} else {
Trc_PRT_sig_can_protect_OMRPORT_SIG_FLAG_MAY_CONTINUE_EXECUTION_NOT_supported();
}
#endif /* !defined(J9ZOS390) */
if (OMR_ARE_NO_BITS_SET(signalOptionsGlobal, OMRPORT_SIG_OPTIONS_REDUCED_SIGNALS_SYNCHRONOUS)) {
supportedFlags |= OMRPORT_SIG_FLAG_SIGALLSYNC;
}
if (OMR_ARE_ALL_BITS_SET(supportedFlags, flags)) {
Trc_PRT_signal_omrsig_can_protect_exiting_is_able_to_protect(supportedFlags);
return 1;
}
Trc_PRT_signal_omrsig_can_protect_exiting_is_not_able_to_protect(supportedFlags);
return 0;
}
uint32_t
omrsig_info(struct OMRPortLibrary *portLibrary, void *info, uint32_t category, int32_t index, const char **name, void **value)
{
*name = "";
switch (category) {
case OMRPORT_SIG_SIGNAL:
return infoForSignal(portLibrary, info, index, name, value);
case OMRPORT_SIG_GPR:
return infoForGPR(portLibrary, info, index, name, value);
case OMRPORT_SIG_CONTROL:
return infoForControl(portLibrary, info, index, name, value);
case OMRPORT_SIG_MODULE:
return infoForModule(portLibrary, info, index, name, value);
case OMRPORT_SIG_FPR:
return infoForFPR(portLibrary, info, index, name, value);
#if defined(J9ZOS390)
case OMRPORT_SIG_VR:
if (portLibrary->portGlobals->vectorRegsSupportOn) {
return infoForVR(portLibrary, info, index, name, value);
}
#endif /* defined(J9ZOS390) */
case OMRPORT_SIG_OTHER:
default:
return OMRPORT_SIG_VALUE_UNDEFINED;
}
}
uint32_t
omrsig_info_count(struct OMRPortLibrary *portLibrary, void *info, uint32_t category)
{
return countInfoInCategory(portLibrary, info, category);
}
/**
* We register the main signal handlers here to deal with -Xrs
*/
int32_t
omrsig_protect(struct OMRPortLibrary *portLibrary, omrsig_protected_fn fn, void *fn_arg, omrsig_handler_fn handler, void *handler_arg, uint32_t flags, uintptr_t *result)
{
struct OMRSignalHandlerRecord thisRecord = {0};
omrthread_t thisThread = NULL;
uint32_t flagsSignalsOnly = flags & OMRPORT_SIG_FLAG_SIGALLSYNC;
uint32_t flagsWithoutMainHandlers = (flagsSignalsOnly & ~syncSignalsWithMainHandlers) & ~OMRPORT_SIG_FLAG_CONTROL_BITS_MASK;
Trc_PRT_signal_omrsig_protect_entered(fn, fn_arg, handler, handler_arg, flags);
if (checkForAmbiguousSignalFlags(flags, "omrsig_protect")) {
return OMRPORT_SIG_ERROR;
}
if (OMR_ARE_ANY_BITS_SET(signalOptionsGlobal, OMRPORT_SIG_OPTIONS_REDUCED_SIGNALS_SYNCHRONOUS)) {
/* -Xrs was set, we can't protect against any signals, do not install the main handler */
Trc_PRT_signal_omrsig_protect_cannot_protect_dueto_Xrs(fn, fn_arg, flags);
*result = fn(portLibrary, fn_arg);
Trc_PRT_signal_omrsig_protect_exiting_did_not_protect_due_to_Xrs(fn, fn_arg, handler, handler_arg, flags);
return 0;
}
if (0 != flagsWithoutMainHandlers) {
uint32_t rc = 0;
/* Acquire the registerHandlerMonitor and install the handler via registerMainHandlers. */
omrthread_monitor_enter(registerHandlerMonitor);
rc = registerMainHandlers(portLibrary, flags, OMRPORT_SIG_FLAG_SIGALLSYNC, NULL);
omrthread_monitor_exit(registerHandlerMonitor);
if (0 != rc) {
return OMRPORT_SIG_ERROR;
}
}
thisThread = omrthread_self();
thisRecord.previous = omrthread_tls_get(thisThread, tlsKey);
thisRecord.portLibrary = portLibrary;
thisRecord.handler = handler;
thisRecord.handler_arg = handler_arg;
thisRecord.flags = flags;
if (OMR_ARE_ANY_BITS_SET(flags, OMRPORT_SIG_FLAG_MAY_RETURN)) {
/* Record the current signal. We need to store this value back into tls if we jump back into this function
* because any signals that may have occurred within the scope of this layer of protection would have been handled
* by that point.
*
* The only scenario where this is of real concern, is if more than one signal was handled per call to omrsig_protect. In
* this case, the current signal in tls will be pointing at a stale stack frame and signal: CMVC 126838
*/
OMRCurrentSignal *currentSignal = omrthread_tls_get(thisThread, tlsKeyCurrentSignal);
/* setjmp/longjmp does not clear the mask setup by the OS when it delivers the signal. User sigsetjmp/siglongjmp(buf, 1) instead */
if (0 != sigsetjmp(thisRecord.returnBuf, 1)) {
/* the handler had long jumped back here -- reset the signal handler stack and currentSignal and return */
omrthread_tls_set(thisThread, tlsKey, thisRecord.previous);
omrthread_tls_set(thisThread, tlsKeyCurrentSignal, currentSignal);
*result = 0;
Trc_PRT_signal_omrsignal_sig_protect_Exit_long_jumped_back_to_omrsig_protect(fn, fn_arg, handler, handler_arg, flags);
return OMRPORT_SIG_EXCEPTION_OCCURRED;
}
}
if (0 != omrthread_tls_set(thisThread, tlsKey, &thisRecord)) {
Trc_PRT_signal_omrsignal_sig_protect_Exit_ERROR_accessing_tls(fn, fn_arg, handler, handler_arg, flags);
return OMRPORT_SIG_ERROR;
}
*result = fn(portLibrary, fn_arg);
/* if the first omrthread_tls_set succeeded, then this one will always succeed */
omrthread_tls_set(thisThread, tlsKey, thisRecord.previous);
Trc_PRT_signal_omrsignal_sig_protect_Exit_after_returning_from_fn(fn, fn_arg, handler, handler_arg, flags, *result);
return 0;
}
int32_t
omrsig_set_async_signal_handler(struct OMRPortLibrary *portLibrary, omrsig_handler_fn handler, void *handler_arg, uint32_t flags)
{
int32_t rc = 0;
OMRUnixAsyncHandlerRecord *cursor = NULL;
OMRUnixAsyncHandlerRecord **previousLink = NULL;
Trc_PRT_signal_omrsig_set_async_signal_handler_entered(handler, handler_arg, flags);
if (checkForAmbiguousSignalFlags(flags, "omrsig_set_async_signal_handler")) {
return OMRPORT_SIG_ERROR;
}
omrthread_monitor_enter(registerHandlerMonitor);
if (OMR_ARE_ANY_BITS_SET(signalOptionsGlobal, OMRPORT_SIG_OPTIONS_REDUCED_SIGNALS_ASYNCHRONOUS)) {
/* -Xrs was set, we can't protect against any signals, do not install any handlers except SIGXFSZ */
if (OMR_ARE_ALL_BITS_SET(flags, OMRPORT_SIG_FLAG_SIGXFSZ) && OMR_ARE_ANY_BITS_SET(signalOptionsGlobal, OMRPORT_SIG_OPTIONS_SIGXFSZ)) {
rc = registerMainHandlers(portLibrary, OMRPORT_SIG_FLAG_SIGXFSZ, OMRPORT_SIG_FLAG_SIGALLASYNC, NULL);
} else {
Trc_PRT_signal_omrsig_set_async_signal_handler_will_not_set_handler_due_to_Xrs(handler, handler_arg, flags);
rc = OMRPORT_SIG_ERROR;
}
} else {
rc = registerMainHandlers(portLibrary, flags, OMRPORT_SIG_FLAG_SIGALLASYNC, NULL);
}
omrthread_monitor_exit(registerHandlerMonitor);
if (0 != rc) {
Trc_PRT_signal_omrsig_set_async_signal_handler_exiting_did_nothing_possible_error(handler, handler_arg, flags);
return rc;
}
omrthread_monitor_enter(asyncMonitor);
/* wait until no signals are being reported */
while (asyncThreadCount > 0) {
omrthread_monitor_wait(asyncMonitor);
}
/* is this handler already registered? */
previousLink = &asyncHandlerList;
cursor = asyncHandlerList;
while (NULL != cursor) {
if ((cursor->portLib == portLibrary) && (cursor->handler == handler) && (cursor->handler_arg == handler_arg)) {
if (0 == flags) {
/* Remove the listener
* NOTE: mainHandlers get removed at omrsignal shutdown */
/* remove this handler record */
*previousLink = cursor->next;
portLibrary->mem_free_memory(portLibrary, cursor);
Trc_PRT_signal_omrsig_set_async_signal_handler_user_handler_removed(handler, handler_arg, flags);
} else {
/* update the listener with the new flags */
Trc_PRT_signal_omrsig_set_async_signal_handler_user_handler_added_1(handler, handler_arg, flags);
cursor->flags |= flags;
}
break;
}
previousLink = &cursor->next;
cursor = cursor->next;
}
if (NULL == cursor) {
/* cursor will only be NULL if we failed to find it in the list */
if (0 != flags) {
OMRUnixAsyncHandlerRecord *record = portLibrary->mem_allocate_memory(portLibrary, sizeof(*record), OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (NULL == record) {
rc = OMRPORT_SIG_ERROR;
} else {
record->portLib = portLibrary;
record->handler = handler;
record->handler_arg = handler_arg;
record->flags = flags;
record->next = NULL;
/* add the new record to the end of the list */
Trc_PRT_signal_omrsig_set_async_signal_handler_user_handler_added_2(handler, handler_arg, flags);
*previousLink = record;
}
}
}
omrthread_monitor_exit(asyncMonitor);
Trc_PRT_signal_omrsig_set_async_signal_handler_exiting(handler, handler_arg, flags);
return rc;
}
int32_t
omrsig_set_single_async_signal_handler(struct OMRPortLibrary *portLibrary, omrsig_handler_fn handler, void *handler_arg, uint32_t portlibSignalFlag, void **oldOSHandler)
{
int32_t rc = 0;
OMRUnixAsyncHandlerRecord *cursor = NULL;
OMRUnixAsyncHandlerRecord **previousLink = NULL;
BOOLEAN foundHandler = FALSE;
Trc_PRT_signal_omrsig_set_single_async_signal_handler_entered(handler, handler_arg, portlibSignalFlag);
if (0 != portlibSignalFlag) {
/* For non-zero portlibSignalFlag, check if only one signal bit is set. Otherwise, fail. */
if (!OMR_IS_ONLY_ONE_BIT_SET(portlibSignalFlag & ~OMRPORT_SIG_FLAG_CONTROL_BITS_MASK)) {
Trc_PRT_signal_omrsig_set_single_async_signal_handler_error_multiple_signal_flags_found(portlibSignalFlag);
return OMRPORT_SIG_ERROR;
}
if (checkForAmbiguousSignalFlags(portlibSignalFlag, "omrsig_set_single_async_signal_handler")) {
return OMRPORT_SIG_ERROR;
}
}
omrthread_monitor_enter(registerHandlerMonitor);
if (OMR_ARE_ANY_BITS_SET(signalOptionsGlobal, OMRPORT_SIG_OPTIONS_REDUCED_SIGNALS_ASYNCHRONOUS)) {
/* -Xrs was set, we can't protect against any signals, do not install any handlers except SIGXFSZ*/
if (OMR_ARE_ALL_BITS_SET(portlibSignalFlag, OMRPORT_SIG_FLAG_SIGXFSZ) && OMR_ARE_ANY_BITS_SET(signalOptionsGlobal, OMRPORT_SIG_OPTIONS_SIGXFSZ)) {
rc = registerMainHandlers(portLibrary, OMRPORT_SIG_FLAG_SIGXFSZ, OMRPORT_SIG_FLAG_SIGALLASYNC, oldOSHandler);
} else {
Trc_PRT_signal_omrsig_set_single_async_signal_handler_will_not_set_handler_due_to_Xrs(handler, handler_arg, portlibSignalFlag);
rc = OMRPORT_SIG_ERROR;
}
} else {
rc = registerMainHandlers(portLibrary, portlibSignalFlag, OMRPORT_SIG_FLAG_SIGALLASYNC, oldOSHandler);
}
omrthread_monitor_exit(registerHandlerMonitor);
if (0 != rc) {
Trc_PRT_signal_omrsig_set_single_async_signal_handler_exiting_did_nothing_possible_error(rc, handler, handler_arg, portlibSignalFlag);
return rc;
}
omrthread_monitor_enter(asyncMonitor);
/* wait until no signals are being reported */
while (asyncThreadCount > 0) {
omrthread_monitor_wait(asyncMonitor);
}
/* is this handler already registered? */
previousLink = &asyncHandlerList;
cursor = asyncHandlerList;
while (NULL != cursor) {
if (cursor->portLib == portLibrary) {
if ((cursor->handler == handler) && (cursor->handler_arg == handler_arg)) {
foundHandler = TRUE;
if (0 == portlibSignalFlag) {
/* Remove the listener. Remove this handler record.
* NOTE: mainHandlers get removed at omrsignal shutdown
*/
*previousLink = cursor->next;
portLibrary->mem_free_memory(portLibrary, cursor);
Trc_PRT_signal_omrsig_set_single_async_signal_handler_user_handler_removed(handler, handler_arg, portlibSignalFlag);
break;
} else {
/* Update the listener with the new portlibSignalFlag */
Trc_PRT_signal_omrsig_set_single_async_signal_handler_user_handler_added_1(handler, handler_arg, portlibSignalFlag);
cursor->flags |= portlibSignalFlag;
}
} else {
/* Unset the portlibSignalFlag for other handlers. One signal must be associated to only one handler. */
cursor->flags &= ~(portlibSignalFlag & ~OMRPORT_SIG_FLAG_CONTROL_BITS_MASK);
}
}
previousLink = &cursor->next;
cursor = cursor->next;
}
if (!foundHandler && (0 != portlibSignalFlag)) {
OMRUnixAsyncHandlerRecord *record = portLibrary->mem_allocate_memory(portLibrary, sizeof(*record), OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (NULL == record) {
rc = OMRPORT_SIG_ERROR;
} else {
record->portLib = portLibrary;
record->handler = handler;
record->handler_arg = handler_arg;
record->flags = portlibSignalFlag;
record->next = NULL;
/* add the new record to the end of the list */
Trc_PRT_signal_omrsig_set_single_async_signal_handler_user_handler_added_2(handler, handler_arg, portlibSignalFlag);
*previousLink = record;
}
}
omrthread_monitor_exit(asyncMonitor);
if (NULL != oldOSHandler) {
Trc_PRT_signal_omrsig_set_single_async_signal_handler_exiting(rc, handler, handler_arg, portlibSignalFlag, *oldOSHandler);
} else {
Trc_PRT_signal_omrsig_set_single_async_signal_handler_exiting(rc, handler, handler_arg, portlibSignalFlag, NULL);
}
return rc;
}
uint32_t
omrsig_map_os_signal_to_portlib_signal(struct OMRPortLibrary *portLibrary, uint32_t osSignalValue)
{
return mapOSSignalToPortLib(osSignalValue, NULL);
}
int32_t
omrsig_map_portlib_signal_to_os_signal(struct OMRPortLibrary *portLibrary, uint32_t portlibSignalFlag)
{
return (int32_t)mapPortLibSignalToOSSignal(portlibSignalFlag);
}
int32_t
omrsig_register_os_handler(struct OMRPortLibrary *portLibrary, uint32_t portlibSignalFlag, void *newOSHandler, void **oldOSHandler)
{
int32_t rc = 0;
Trc_PRT_signal_omrsig_register_os_handler_entered(portlibSignalFlag, newOSHandler);
if ((0 == portlibSignalFlag) || !OMR_IS_ONLY_ONE_BIT_SET(portlibSignalFlag & ~OMRPORT_SIG_FLAG_CONTROL_BITS_MASK)) {
/* If portlibSignalFlag is 0 or if portlibSignalFlag has multiple signal bits set, then fail. */
Trc_PRT_signal_omrsig_register_os_handler_invalid_portlibSignalFlag(portlibSignalFlag);
rc = OMRPORT_SIG_ERROR;
} else if (checkForAmbiguousSignalFlags(portlibSignalFlag, "omrsig_register_os_handler")) {
return OMRPORT_SIG_ERROR;
} else {
omrthread_monitor_enter(registerHandlerMonitor);
rc = registerSignalHandlerWithOS(portLibrary, portlibSignalFlag, (unix_sigaction)newOSHandler, oldOSHandler);
omrthread_monitor_exit(registerHandlerMonitor);
}
if (NULL != oldOSHandler) {
Trc_PRT_signal_omrsig_register_os_handler_exiting(rc, portlibSignalFlag, newOSHandler, *oldOSHandler);
} else {
Trc_PRT_signal_omrsig_register_os_handler_exiting(rc, portlibSignalFlag, newOSHandler, NULL);
}
return rc;
}
BOOLEAN
omrsig_is_main_signal_handler(struct OMRPortLibrary *portLibrary, void *osHandler)
{
BOOLEAN rc = FALSE;
Trc_PRT_signal_omrsig_is_main_signal_handler_entered(osHandler);
if ((osHandler == (void *)mainSynchSignalHandler)
|| (osHandler == (void *)mainASynchSignalHandler)
) {
rc = TRUE;
}
Trc_PRT_signal_omrsig_is_main_signal_handler_exiting(rc);
return rc;
}
int32_t
omrsig_is_signal_ignored(struct OMRPortLibrary *portLibrary, uint32_t portlibSignalFlag, BOOLEAN *isSignalIgnored)
{
int32_t rc = 0;
int osSignalNo = OMRPORT_SIG_ERROR;
void *oldHandler = NULL;
struct sigaction oldSignalAction;
Trc_PRT_signal_omrsig_is_signal_ignored_entered(portlibSignalFlag);
*isSignalIgnored = FALSE;
if (0 != portlibSignalFlag) {
/* For non-zero portlibSignalFlag, check if only one signal bit is set. Otherwise, fail. */
if (!OMR_IS_ONLY_ONE_BIT_SET(portlibSignalFlag & ~OMRPORT_SIG_FLAG_CONTROL_BITS_MASK)) {
rc = OMRPORT_SIG_ERROR;
goto exit;
}
if (checkForAmbiguousSignalFlags(portlibSignalFlag, "omrsig_is_signal_ignored")) {
return OMRPORT_SIG_ERROR;
}
}
osSignalNo = mapPortLibSignalToOSSignal(portlibSignalFlag);
if (OMRPORT_SIG_ERROR == osSignalNo) {
rc = OMRPORT_SIG_ERROR;
goto exit;
}
memset(&oldSignalAction, 0, sizeof(struct sigaction));
OMRSIG_SIGACTION(osSignalNo, NULL, &oldSignalAction);
oldHandler = (void *)oldSignalAction.sa_sigaction;
if (NULL == oldHandler) {
oldHandler = (void *)oldSignalAction.sa_handler;
}
if (oldHandler == (void *)SIG_IGN) {
*isSignalIgnored = TRUE;
}
exit:
Trc_PRT_signal_omrsig_is_signal_ignored_exiting(rc, *isSignalIgnored);
return rc;
}
/*
* The full shutdown routine "sig_full_shutdown" overwrites this once we've completed startup
*/
void
omrsig_shutdown(struct OMRPortLibrary *portLibrary)
{
Trc_PRT_signal_omrsig_shutdown_empty_routine(portLibrary);
return;
}
/**
* Start up the signal handling component of the port library
*
* Note: none of the main handlers are registered with the OS until the first call to either of omrsig_protect or omrsig_set_async_signal_handler
*/
int32_t
omrsig_startup(struct OMRPortLibrary *portLibrary)
{
int32_t result = 0;
omrthread_monitor_t globalMonitor = NULL;
uint32_t index = 1;
Trc_PRT_signal_omrsig_startup_entered(portLibrary);
globalMonitor = omrthread_global_monitor();
omrthread_monitor_enter(globalMonitor);
if (attachedPortLibraries++ == 0) {
/* initialize the old actions */
for (index = 1; index < ARRAY_SIZE_SIGNALS; index++) {
oldActions[index].restore = 0;
}
result = initializeSignalTools(portLibrary);
}
omrthread_monitor_exit(globalMonitor);
if (result == 0) {
/* we have successfully started up the signal portion, install the full shutdown routine */
portLibrary->sig_shutdown = sig_full_shutdown;
}
Trc_PRT_signal_omrsig_startup_exiting(portLibrary, result);
return result;
}
static uint32_t
countInfoInCategory(struct OMRPortLibrary *portLibrary, void *info, uint32_t category)
{
void *value = NULL;
const char *name = NULL;
uint32_t count = 0;
while (portLibrary->sig_info(portLibrary, info, category, count, &name, &value) != OMRPORT_SIG_VALUE_UNDEFINED) {
count++;
}
return count;
}
#if defined(OMR_PORT_ASYNC_HANDLER)
/**
* Given a port library signal flag and Unix signal value, execute the associated handlers
* stored within asyncHandlerList (list of OMRUnixAsyncHandlerRecord).
*
* @param asyncSignalFlag port library signal flag
* @param unixSignal Unix signal value
*
* @return void
*/
static void
runHandlers(uint32_t asyncSignalFlag, int unixSignal)
{
OMRUnixAsyncHandlerRecord *cursor = asyncHandlerList;
/* report the signal recorded in signalType to all registered listeners (for this signal).
* incrementing the asyncThreadCount will prevent the list from being modified while we use it.
*/
omrthread_monitor_enter(asyncMonitor);
asyncThreadCount++;
omrthread_monitor_exit(asyncMonitor);
while (NULL != cursor) {
if (OMR_ARE_ALL_BITS_SET(cursor->flags, asyncSignalFlag)) {
Trc_PRT_signal_omrsig_asynchSignalReporter_calling_handler(cursor->portLib, asyncSignalFlag, cursor->handler_arg);
cursor->handler(cursor->portLib, asyncSignalFlag, NULL, cursor->handler_arg);
}
cursor = cursor->next;
}
omrthread_monitor_enter(asyncMonitor);
if (--asyncThreadCount == 0) {
omrthread_monitor_notify_all(asyncMonitor);
}
omrthread_monitor_exit(asyncMonitor);
#if defined(OMRPORT_OMRSIG_SUPPORT)
if (OMR_ARE_NO_BITS_SET(signalOptionsGlobal, OMRPORT_SIG_OPTIONS_OMRSIG_NO_CHAIN)) {
/* mapPortLibSignalToOSSignal returns OMRPORT_SIG_ERROR (-1) on unknown mapping */
if (OMRPORT_SIG_ERROR != unixSignal) {
omrsig_handler(unixSignal, NULL, NULL);
}
}
#endif /* defined(OMRPORT_OMRSIG_SUPPORT) */
}
/**
* Reports the asynchronous signal to all listeners.
*/
static int J9THREAD_PROC
asynchSignalReporter(void *userData)
{
#if defined(J9ZOS390)
/*
* CMVC 192198
* Prevent async signals that are handled by mainASynchSignalHandler()
* from being caught by this thread. We can't allow mainASynchSignalHandler()
* to recursively lock wakeUpASyncReporterMutex because it could cause this
* thread to miss signals on wakeUpASyncReporterCond.
*
* It's ok if this thread caught a signal just before this, because it has
* not locked wakeUpASyncReporterMutex yet.
*
* This limitation is unique to the z/OS implementation of async signal handling.
*/
{
sigset_t asyncSigs;
intptr_t rc = 0;
int osRc = 0;
osRc = sigemptyset(&asyncSigs);
Assert_PRT_true(0 == osRc);
rc = addAsyncSignalsToSet(&asyncSigs);
Assert_PRT_true(0 == rc);
osRc = sigprocmask(SIG_BLOCK, &asyncSigs, NULL);
Assert_PRT_true(0 == osRc);
}
#endif /* defined(J9ZOS390) */
omrthread_set_name(omrthread_self(), "Signal Reporter");
while (0 == shutDownASynchReporter) {
int unixSignal = 1;
uint32_t asyncSignalFlag = 0;
#if !defined(J9ZOS390)
/* CMVC 119663 sem_wait can return -1/EINTR on signal in NPTL */
while (0 != SIGSEM_WAIT(wakeUpASyncReporter));
#endif /* !defined(J9ZOS390) */
/* determine which signal we've been woken up for */
for (unixSignal = 1; unixSignal < ARRAY_SIZE_SIGNALS; unixSignal++) {
uintptr_t signalCount = signalCounts[unixSignal];
if (signalCount > 0) {
asyncSignalFlag = mapOSSignalToPortLib(unixSignal, NULL);
runHandlers(asyncSignalFlag, unixSignal);
subtractAtomic(&signalCounts[unixSignal], 1);
#if defined(J9ZOS390)
/* Before waiting on the condvar, we need to make sure all
* signals are handled. This will allow us to handle all signals
* even if some wake signals for the condvar are missed. Reset
* unixSignal to 0. for loop will start again with unixSignal = 1.
*/
unixSignal = 0;
#else /* defined(J9ZOS390) */
/* sem_wait will fall-through for each sem_post. We can handle
* one signal at a time. Ultimately, all signals will be handled
* So, break out of the for loop.
*/
break;
#endif /* defined(J9ZOS390) */
}
}
#if defined(J9ZOS390)
/* Only wait if no signal is pending and shutdown isn't requested. */
if (0 == shutDownASynchReporter) {
/* I won't attempt to generate diagnostics if the following pthread
* functions return errors because it may interfere with diagnostics
* we are attempting to generate for earlier events.
*/
Trc_PRT_signal_omrsig_asynchSignalReporterThread_going_to_sleep();
pthread_mutex_lock(&wakeUpASyncReporterMutex);
pthread_cond_wait(&wakeUpASyncReporterCond, &wakeUpASyncReporterMutex);
pthread_mutex_unlock(&wakeUpASyncReporterMutex);
}
#endif /* defined(J9ZOS390) */
Trc_PRT_signal_omrsig_asynchSignalReporter_woken_up();
}
omrthread_monitor_enter(asyncReporterShutdownMonitor);
shutDownASynchReporter = 0;
omrthread_monitor_notify(asyncReporterShutdownMonitor);
omrthread_exit(asyncReporterShutdownMonitor);
/* unreachable */
return 0;
}
#endif /* defined(OMR_PORT_ASYNC_HANDLER) */
/**
* This signal handler is specific to synchronous signals.
* It will call all of the user's handlers that were registered with the vm using omrsig_protect,
* upon receiving a signal they listen for.
*
*/
#if defined(S390) && defined(LINUX)
static void
mainSynchSignalHandler(int signal, siginfo_t *sigInfo, void *contextInfo, uintptr_t breakingEventAddr)
#else /* defined(S390) && defined(LINUX) */
static void
mainSynchSignalHandler(int signal, siginfo_t *sigInfo, void *contextInfo)
#endif /* defined(S390) && defined(LINUX) */
{
#if defined(LINUXPPC)
/*
* PR 56956: ensure the right register context is used if a signal occurs in a transaction.
* If there is a signal during a transaction, two contexts are provided: one for the start of the transaction
* and one for the context within the transaction.
*
* On Power 8 Linux, if a signal occurs during a transaction,
* the transaction aborts and the corresponding signal handler is invoked with the context from the start of the transaction.
* The mainSynchSignalHandler determines if the signal occurred within a transaction,
* and if so, returns such that the transaction is executed in the failed state,
* causing the non-transactional failure path to be taken (referred to as the "abort handler" in
* https://www.kernel.org/doc/Documentation/powerpc/transactional_memory.txt).
* Note: this means signals that occur during the transaction, but not in the failure path, are lost.
* Please see Jazz design 63039: Handle signals in PowerPC transactional memory operations for further discussion.
*/
/*
* This contains two contexts: the context at the start of the transaction, plus a link to the
* context within the transaction. We use the former, other than to determine the transaction state.
*/
ucontext_t *platformContext = contextInfo;
/*
* Check the Transaction State (TS) bits
* for either 01 (Suspended) or 10 (Transactional)
*/
#if defined(LINUXPPC64)
#define MSR_TS_MASK 0x600000000ULL
if (OMR_ARE_ANY_BITS_SET(platformContext->uc_mcontext.regs->msr, MSR_TS_MASK))
/* the transactional context is in the high order bits */
#else /* defined(LINUXPPC64) */
#define MSR_TS_MASK 0x6U
/*
* in 32-bit CPUs, the second context containing the transactional
* state is in a separate ucontext datastructure pointed to by uc_link.
*/
if ((NULL != platformContext->uc_link) && OMR_ARE_ANY_BITS_SET(platformContext->uc_link->uc_mcontext.regs->msr, MSR_TS_MASK))
#endif /* defined(LINUXPPC64) */
{
/*
* resume the transaction in the failed state, so it executes the failure path.
*/