/
omrsignal.c
1357 lines (1146 loc) · 41.4 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, 2018 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
*******************************************************************************/
#include <windows.h>
#include <stdlib.h>
#include "omrport.h"
#include "omrthread.h"
#include "omrsignal.h"
#include "omrutil.h"
#include "omrutilbase.h"
#include "ut_omrport.h"
#if defined(OMRPORT_OMRSIG_SUPPORT)
#include "omrsig.h"
#endif /* defined(OMRPORT_OMRSIG_SUPPORT) */
typedef struct J9Win32AsyncHandlerRecord {
OMRPortLibrary *portLib;
omrsig_handler_fn handler;
void *handler_arg;
uint32_t flags;
struct J9Win32AsyncHandlerRecord *next;
} J9Win32AsyncHandlerRecord;
static struct {
uint32_t portLibSignalNo;
int osSignalNo;
} signalMap[] = {
{OMRPORT_SIG_FLAG_SIGSEGV, SIGSEGV},
{OMRPORT_SIG_FLAG_SIGILL, SIGILL},
{OMRPORT_SIG_FLAG_SIGFPE, SIGFPE},
{OMRPORT_SIG_FLAG_SIGABRT, SIGABRT},
{OMRPORT_SIG_FLAG_SIGTERM, SIGTERM},
{OMRPORT_SIG_FLAG_SIGINT, SIGINT},
{OMRPORT_SIG_FLAG_SIGQUIT, SIGBREAK},
};
#define ARRAY_SIZE_SIGNALS (NSIG + 1)
typedef void (*win_signal)(int);
/* Store the original signal handler. During shutdown, we need to restore
* the signal handler to the original OS handler.
*/
static struct {
win_signal originalHandler;
uint32_t restore;
} handlerInfo[ARRAY_SIZE_SIGNALS];
/* Keep track of signal counts. */
static volatile uintptr_t signalCounts[ARRAY_SIZE_SIGNALS] = {0};
/* Thread to invoke signal handlers for asynchronous signals. */
static omrthread_t asynchSignalReporterThread;
static J9Win32AsyncHandlerRecord *asyncHandlerList;
static omrthread_monitor_t asyncMonitor;
static uint32_t asyncThreadCount;
static uint32_t attachedPortLibraries;
/* holds the options set by omrsig_set_options */
static uint32_t signalOptions;
/* key to get the current synchronous signal */
static omrthread_tls_key_t tlsKeyCurrentSignal;
/* Calls to registerSignalHandlerWithOS are synchronized using registerHandlerMonitor */
static omrthread_monitor_t registerHandlerMonitor;
/* wakeUpASyncReporter semaphore coordinates between master async signal handler and
* async signal reporter thread.
*/
static j9sem_t wakeUpASyncReporter;
/* Used to synchronize shutdown of asynchSignalReporterThread. */
static omrthread_monitor_t asyncReporterShutdownMonitor;
/* Used to indicate start and end of asynchSignalReporterThread termination. */
static uint32_t shutDownASynchReporter;
static uint32_t mapWin32ExceptionToPortlibType(uint32_t exceptionCode);
static uint32_t infoForGPR(struct OMRPortLibrary *portLibrary, struct J9Win32SignalInfo *info, int32_t index, const char **name, void **value);
static void removeAsyncHandlers(OMRPortLibrary *portLibrary);
static void fillInWin32SignalInfo(struct OMRPortLibrary *portLibrary, omrsig_handler_fn handler, EXCEPTION_POINTERS *exceptionInfo, struct J9Win32SignalInfo *j9info);
static uint32_t infoForSignal(struct OMRPortLibrary *portLibrary, struct J9Win32SignalInfo *info, int32_t index, const char **name, void **value);
static uint32_t infoForModule(struct OMRPortLibrary *portLibrary, struct J9Win32SignalInfo *info, int32_t index, const char **name, void **value);
static uint32_t countInfoInCategory(struct OMRPortLibrary *portLibrary, void *info, uint32_t category);
static BOOL WINAPI consoleCtrlHandler(DWORD dwCtrlType);
int structuredExceptionHandler(struct OMRPortLibrary *portLibrary, omrsig_handler_fn handler, void *handler_arg, uint32_t flags, EXCEPTION_POINTERS *exceptionInfo);
static uint32_t infoForControl(struct OMRPortLibrary *portLibrary, struct J9Win32SignalInfo *info, int32_t index, const char **name, void **value);
static intptr_t setCurrentSignal(struct OMRPortLibrary *portLibrary, intptr_t signal);
static uint32_t mapOSSignalToPortLib(uint32_t signalNo);
static int mapPortLibSignalToOSSignal(uint32_t portLibSignal);
static int32_t registerSignalHandlerWithOS(OMRPortLibrary *portLibrary, uint32_t portLibrarySignalNo, win_signal handler, void **oldOSHandler);
static int32_t initializeSignalTools(OMRPortLibrary *portLibrary);
static void destroySignalTools(OMRPortLibrary *portLibrary);
static void updateSignalCount(int osSignalNo);
static void masterASynchSignalHandler(int osSignalNo);
#if defined(OMR_PORT_ASYNC_HANDLER)
static int J9THREAD_PROC asynchSignalReporter(void *userData);
static void runHandlers(uint32_t asyncSignalFlag, int osSignal);
#endif /* defined(OMR_PORT_ASYNC_HANDLER) */
static int32_t registerMasterHandlers(OMRPortLibrary *portLibrary, uint32_t flags, uint32_t allowedSubsetOfFlags, void **oldOSHandler);
static J9Win32AsyncHandlerRecord *createAsyncHandlerRecord(struct OMRPortLibrary *portLibrary, omrsig_handler_fn handler, void *handler_arg, uint32_t flags);
static int32_t setReporterPriority(OMRPortLibrary *portLibrary, uintptr_t priority);
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:
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);
}
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)
{
if (0 == (signalOptions & OMRPORT_SIG_OPTIONS_REDUCED_SIGNALS_SYNCHRONOUS)) {
__try {
*result = fn(portLibrary, fn_arg);
} __except (structuredExceptionHandler(portLibrary, handler, handler_arg, flags, GetExceptionInformation())) {
/* this code is only reachable if the handler returned OMRPORT_SIG_EXCEPTION_RETURN */
*result = 0;
return OMRPORT_SIG_EXCEPTION_OCCURRED;
}
} else {
/* reduced signals, no protection */
*result = fn(portLibrary, fn_arg);
}
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;
J9Win32AsyncHandlerRecord *cursor = NULL;
J9Win32AsyncHandlerRecord **previousLink = NULL;
Trc_PRT_signal_omrsig_set_async_signal_handler_entered(handler, handler_arg, flags);
rc = registerMasterHandlers(portLibrary, flags, OMRPORT_SIG_FLAG_SIGALLASYNC, NULL);
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) {
*previousLink = cursor->next;
portLibrary->mem_free_memory(portLibrary, cursor);
Trc_PRT_signal_omrsig_set_async_signal_handler_user_handler_removed(handler, handler_arg, flags);
/* If this is the last handler, unregister the handler function. */
if (NULL == asyncHandlerList) {
SetConsoleCtrlHandler(consoleCtrlHandler, FALSE);
}
} 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;
}
/* Cursor will only be NULL if we failed to find it in the list. */
if ((NULL == cursor) && (0 != flags)) {
J9Win32AsyncHandlerRecord *record = createAsyncHandlerRecord(portLibrary, handler, handler_arg, flags);
if (NULL != record) {
/* 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;
} else {
rc = OMRPORT_SIG_ERROR;
}
}
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;
J9Win32AsyncHandlerRecord *cursor = NULL;
J9Win32AsyncHandlerRecord **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)) {
Trc_PRT_signal_omrsig_set_single_async_signal_handler_error_multiple_signal_flags_found(portlibSignalFlag);
return OMRPORT_SIG_ERROR;
}
}
rc = registerMasterHandlers(portLibrary, portlibSignalFlag, OMRPORT_SIG_FLAG_SIGALLASYNC, oldOSHandler);
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 this handler record. */
*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);
/* If this is the last handler, then unregister the handler function. */
if (NULL == asyncHandlerList) {
SetConsoleCtrlHandler(consoleCtrlHandler, FALSE);
}
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;
}
}
previousLink = &cursor->next;
cursor = cursor->next;
}
if (!foundHandler && (0 != portlibSignalFlag)) {
J9Win32AsyncHandlerRecord *record = createAsyncHandlerRecord(portLibrary, handler, handler_arg, portlibSignalFlag);
if (NULL != record) {
/* 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;
} else {
rc = OMRPORT_SIG_ERROR;
}
}
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);
}
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)) {
/* 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 {
omrthread_monitor_enter(registerHandlerMonitor);
rc = registerSignalHandlerWithOS(portLibrary, portlibSignalFlag, (win_signal)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_master_signal_handler(struct OMRPortLibrary *portLibrary, void *osHandler)
{
BOOLEAN rc = FALSE;
Trc_PRT_signal_omrsig_is_master_signal_handler_entered(osHandler);
if (osHandler == (void *)masterASynchSignalHandler) {
rc = TRUE;
}
Trc_PRT_signal_omrsig_is_master_signal_handler_exiting(rc);
return rc;
}
int32_t
omrsig_is_signal_ignored(struct OMRPortLibrary *portLibrary, uint32_t portlibSignalFlag, BOOLEAN *isSignalIgnored)
{
int32_t rc = 0;
Trc_PRT_signal_omrsig_is_signal_ignored_entered(portlibSignalFlag);
*isSignalIgnored = FALSE;
Trc_PRT_signal_omrsig_is_signal_ignored_exiting(rc, *isSignalIgnored);
return rc;
}
int32_t
omrsig_can_protect(struct OMRPortLibrary *portLibrary, uint32_t flags)
{
uint32_t supportedFlags = OMRPORT_SIG_FLAG_MAY_RETURN | OMRPORT_SIG_FLAG_MAY_CONTINUE_EXECUTION;
if (0 == (signalOptions & OMRPORT_SIG_OPTIONS_REDUCED_SIGNALS_SYNCHRONOUS)) {
supportedFlags |= OMRPORT_SIG_FLAG_SIGALLSYNC;
}
/* split this up into OMRPORT_SIG_FLAG_SIGALLSYNC and OMRPORT_SIG_FLAG_SIGALLASYNC */
if (0 == (signalOptions & OMRPORT_SIG_OPTIONS_REDUCED_SIGNALS_ASYNCHRONOUS)) {
supportedFlags |= OMRPORT_SIG_FLAG_SIGQUIT | OMRPORT_SIG_FLAG_SIGTERM;
}
if ((flags & supportedFlags) == flags) {
return 1;
} else {
return 0;
}
}
/**
* Shutdown the signal handling component of the port library
*/
void
omrsig_shutdown(struct OMRPortLibrary *portLibrary)
{
uint32_t index = 1;
omrthread_monitor_t globalMonitor = omrthread_global_monitor();
removeAsyncHandlers(portLibrary);
omrthread_monitor_enter(globalMonitor);
if (--attachedPortLibraries == 0) {
#if defined(OMR_PORT_ASYNC_HANDLER)
/* Terminate asynchSignalReporterThread. */
omrthread_monitor_enter(asyncReporterShutdownMonitor);
shutDownASynchReporter = 1;
j9sem_post(wakeUpASyncReporter);
while (0 != shutDownASynchReporter) {
omrthread_monitor_wait(asyncReporterShutdownMonitor);
}
omrthread_monitor_exit(asyncReporterShutdownMonitor);
#endif /* defined(OMR_PORT_ASYNC_HANDLER) */
/* Register the original signal handlers, which were overwritten. */
for (index = 1; index < ARRAY_SIZE_SIGNALS; index++) {
if (0 != handlerInfo[index].restore) {
OMRSIG_SIGNAL(index, handlerInfo[index].originalHandler);
/* record that we no longer have a handler installed with the OS for this signal */
Trc_PRT_signal_sig_full_shutdown_deregistered_handler_with_OS(portLibrary, index);
handlerInfo[index].restore = 0;
}
}
destroySignalTools(portLibrary);
}
omrthread_monitor_exit(globalMonitor);
}
/**
* Start up the signal handling component of the port library
*/
int32_t
omrsig_startup(struct OMRPortLibrary *portLibrary)
{
omrthread_monitor_t globalMonitor = omrthread_global_monitor();
int32_t result = 0;
uint32_t index = 1;
omrthread_monitor_enter(globalMonitor);
if (attachedPortLibraries++ == 0) {
/* initialize handlerInfo */
for (index = 1; index < ARRAY_SIZE_SIGNALS; index++) {
handlerInfo[index].restore = 0;
}
result = initializeSignalTools(portLibrary);
}
omrthread_monitor_exit(globalMonitor);
return result;
}
int
structuredExceptionHandler(struct OMRPortLibrary *portLibrary, omrsig_handler_fn handler, void *handler_arg, uint32_t flags, EXCEPTION_POINTERS *exceptionInfo)
{
uint32_t result;
uint32_t type;
intptr_t prevSigType;
struct J9Win32SignalInfo j9info;
if ((exceptionInfo->ExceptionRecord->ExceptionCode & (ERROR_SEVERITY_ERROR | APPLICATION_ERROR_MASK)) != ERROR_SEVERITY_ERROR) {
return EXCEPTION_CONTINUE_SEARCH;
}
type = mapWin32ExceptionToPortlibType(exceptionInfo->ExceptionRecord->ExceptionCode);
if (0 == (type & flags)) {
return EXCEPTION_CONTINUE_SEARCH;
}
fillInWin32SignalInfo(portLibrary, handler, exceptionInfo, &j9info);
prevSigType = portLibrary->sig_get_current_signal(portLibrary);
setCurrentSignal(portLibrary, type);
__try {
result = handler(portLibrary, j9info.type, &j9info, handler_arg);
} __except (EXCEPTION_EXECUTE_HANDLER) {
/* if a recursive exception occurs, ignore it and pass control to the next handler */
setCurrentSignal(portLibrary, prevSigType);
return EXCEPTION_CONTINUE_SEARCH;
}
setCurrentSignal(portLibrary, prevSigType);
if (result == OMRPORT_SIG_EXCEPTION_CONTINUE_SEARCH) {
return EXCEPTION_CONTINUE_SEARCH;
} else if (result == OMRPORT_SIG_EXCEPTION_CONTINUE_EXECUTION) {
return EXCEPTION_CONTINUE_EXECUTION;
} else /* if (result == OMRPORT_SIG_EXCEPTION_RETURN) */ {
return EXCEPTION_EXECUTE_HANDLER;
}
}
static uint32_t
mapWin32ExceptionToPortlibType(uint32_t exceptionCode)
{
switch (exceptionCode) {
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
return OMRPORT_SIG_FLAG_SIGFPE_DIV_BY_ZERO;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
return OMRPORT_SIG_FLAG_SIGFPE_INT_DIV_BY_ZERO;
case EXCEPTION_INT_OVERFLOW:
return OMRPORT_SIG_FLAG_SIGFPE_INT_OVERFLOW;
case EXCEPTION_FLT_OVERFLOW:
case EXCEPTION_FLT_UNDERFLOW:
case EXCEPTION_FLT_INVALID_OPERATION:
case EXCEPTION_FLT_INEXACT_RESULT:
case EXCEPTION_FLT_DENORMAL_OPERAND:
case EXCEPTION_FLT_STACK_CHECK:
return OMRPORT_SIG_FLAG_SIGFPE;
case EXCEPTION_PRIV_INSTRUCTION:
case EXCEPTION_ILLEGAL_INSTRUCTION:
return OMRPORT_SIG_FLAG_SIGILL;
case EXCEPTION_ACCESS_VIOLATION:
return OMRPORT_SIG_FLAG_SIGSEGV;
case EXCEPTION_IN_PAGE_ERROR:
case EXCEPTION_DATATYPE_MISALIGNMENT:
return OMRPORT_SIG_FLAG_SIGBUS;
default:
return OMRPORT_SIG_FLAG_SIGTRAP;
}
}
static void
fillInWin32SignalInfo(struct OMRPortLibrary *portLibrary, omrsig_handler_fn handler, EXCEPTION_POINTERS *exceptionInfo, struct J9Win32SignalInfo *j9info)
{
memset(j9info, 0, sizeof(*j9info));
j9info->type = mapWin32ExceptionToPortlibType(exceptionInfo->ExceptionRecord->ExceptionCode);
j9info->handlerAddress = (void *)handler;
j9info->handlerAddress2 = (void *)structuredExceptionHandler;
j9info->ExceptionRecord = exceptionInfo->ExceptionRecord;
j9info->ContextRecord = exceptionInfo->ContextRecord;
/* module info is filled on demand */
}
static uint32_t
infoForSignal(struct OMRPortLibrary *portLibrary, struct J9Win32SignalInfo *info, int32_t index, const char **name, void **value)
{
*name = "";
switch (index) {
case OMRPORT_SIG_SIGNAL_TYPE:
case 0:
*name = "J9Generic_Signal_Number";
*value = &info->type;
return OMRPORT_SIG_VALUE_32;
case OMRPORT_SIG_SIGNAL_PLATFORM_SIGNAL_TYPE:
case 1:
*name = "ExceptionCode";
*value = &info->ExceptionRecord->ExceptionCode;
return OMRPORT_SIG_VALUE_32;
case OMRPORT_SIG_SIGNAL_ADDRESS:
case 2:
*name = "ExceptionAddress";
*value = &info->ExceptionRecord->ExceptionAddress;
return OMRPORT_SIG_VALUE_ADDRESS;
case 3:
*name = "ContextFlags";
*value = &info->ContextRecord->ContextFlags;
return OMRPORT_SIG_VALUE_32;
case OMRPORT_SIG_SIGNAL_HANDLER:
case 4:
*name = "Handler1";
*value = &info->handlerAddress;
return OMRPORT_SIG_VALUE_ADDRESS;
case 5:
*name = "Handler2";
*value = &info->handlerAddress2;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_SIGNAL_INACCESSIBLE_ADDRESS:
case 6:
/* For EXCEPTION_ACCESS_VIOLATION (very common) we can extract the address
* that was being read or written to and whether it was an attempt to read, write
* or execute the data at that address.
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa363082%28v=vs.85%29.aspx */
if (EXCEPTION_ACCESS_VIOLATION == info->ExceptionRecord->ExceptionCode) {
if (EXCEPTION_READ_FAULT == info->ExceptionRecord->ExceptionInformation[0]) {
*name = "InaccessibleReadAddress";
} else if (EXCEPTION_WRITE_FAULT == info->ExceptionRecord->ExceptionInformation[0]) {
*name = "InaccessibleWriteAddress";
} else if (EXCEPTION_EXECUTE_FAULT == info->ExceptionRecord->ExceptionInformation[0]) {
*name = "DEPAddress";
}
*value = &info->ExceptionRecord->ExceptionInformation[1];
return OMRPORT_SIG_VALUE_ADDRESS;
} else {
return OMRPORT_SIG_VALUE_UNDEFINED;
}
default:
return OMRPORT_SIG_VALUE_UNDEFINED;
}
}
static uint32_t
infoForGPR(struct OMRPortLibrary *portLibrary, struct J9Win32SignalInfo *info, int32_t index, const char **name, void **value)
{
*name = "";
if (info->ContextRecord->ContextFlags & CONTEXT_INTEGER) {
switch (index) {
case OMRPORT_SIG_GPR_X86_EDI:
case 0:
*name = "EDI";
*value = &info->ContextRecord->Edi;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_X86_ESI:
case 1:
*name = "ESI";
*value = &info->ContextRecord->Esi;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_X86_EAX:
case 2:
*name = "EAX";
*value = &info->ContextRecord->Eax;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_X86_EBX:
case 3:
*name = "EBX";
*value = &info->ContextRecord->Ebx;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_X86_ECX:
case 4:
*name = "ECX";
*value = &info->ContextRecord->Ecx;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_X86_EDX:
case 5:
*name = "EDX";
*value = &info->ContextRecord->Edx;
return OMRPORT_SIG_VALUE_ADDRESS;
default:
return OMRPORT_SIG_VALUE_UNDEFINED;
}
}
return OMRPORT_SIG_VALUE_UNDEFINED;
}
static uint32_t
infoForControl(struct OMRPortLibrary *portLibrary, struct J9Win32SignalInfo *info, int32_t index, const char **name, void **value)
{
*name = "";
if (info->ContextRecord->ContextFlags & CONTEXT_CONTROL) {
switch (index) {
case OMRPORT_SIG_CONTROL_PC:
case 0:
*name = "EIP";
*value = &info->ContextRecord->Eip;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_CONTROL_SP:
case 1:
*name = "ESP";
*value = &info->ContextRecord->Esp;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_CONTROL_BP:
case 2:
*name = "EBP";
*value = &info->ContextRecord->Ebp;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_CONTROL_X86_EFLAGS:
case 3:
*name = "EFLAGS";
*value = &info->ContextRecord->EFlags;
return OMRPORT_SIG_VALUE_ADDRESS;
}
}
if (info->ContextRecord->ContextFlags & CONTEXT_SEGMENTS) {
switch (index) {
case 4:
*name = "GS";
*value = &info->ContextRecord->SegGs;
return OMRPORT_SIG_VALUE_16;
case 5:
*name = "FS";
*value = &info->ContextRecord->SegFs;
return OMRPORT_SIG_VALUE_16;
case 6:
*name = "ES";
*value = &info->ContextRecord->SegEs;
return OMRPORT_SIG_VALUE_16;
case 7:
*name = "DS";
*value = &info->ContextRecord->SegDs;
return OMRPORT_SIG_VALUE_16;
}
}
return OMRPORT_SIG_VALUE_UNDEFINED;
}
static uint32_t
infoForModule(struct OMRPortLibrary *portLibrary, struct J9Win32SignalInfo *info, int32_t index, const char **name, void **value)
{
if (info->moduleBaseAddress == NULL) {
MEMORY_BASIC_INFORMATION mbi;
if (VirtualQuery((LPBYTE) info->ExceptionRecord->ExceptionAddress, &mbi, sizeof(mbi)) == sizeof(mbi)) {
if (MEM_FREE == mbi.State) {
/* misc hack from Advanced Windows (Richter) */
mbi.AllocationBase = mbi.BaseAddress;
}
GetModuleFileName((HINSTANCE) mbi.AllocationBase, info->moduleName, sizeof(info->moduleName));
info->moduleBaseAddress = mbi.AllocationBase;
info->offsetInDLL = (uintptr_t) info->ExceptionRecord->ExceptionAddress - (uintptr_t) mbi.AllocationBase;
}
}
*name = "";
switch (index) {
case OMRPORT_SIG_MODULE_NAME:
case 0:
*name = "Module";
*value = &info->moduleName;
return OMRPORT_SIG_VALUE_STRING;
case 1:
*name = "Module_base_address";
*value = &info->moduleBaseAddress;
return OMRPORT_SIG_VALUE_ADDRESS;
case 2:
*name = "Offset_in_DLL";
*value = &info->offsetInDLL;
return OMRPORT_SIG_VALUE_32;
default:
return OMRPORT_SIG_VALUE_UNDEFINED;
}
}
static uint32_t
countInfoInCategory(struct OMRPortLibrary *portLibrary, void *info, uint32_t category)
{
void *value;
const char *name;
uint32_t count = 0;
while (portLibrary->sig_info(portLibrary, info, category, count, &name, &value) != OMRPORT_SIG_VALUE_UNDEFINED) {
count++;
}
return count;
}
/**
* Translate the control signal into an OS signal, update signalCounts
* and notify asynchSignalReporterThread to execute the associated handlers.
*
* @param[in] dwCtrlType the type of control signal received
*
* @return TRUE if the control signal is handled. Otherwise, return FALSE.
*/
static BOOL WINAPI
consoleCtrlHandler(DWORD dwCtrlType)
{
BOOL result = FALSE;
int osSignalNo = OMRPORT_SIG_ERROR;
switch (dwCtrlType) {
case CTRL_BREAK_EVENT:
osSignalNo = SIGBREAK;
break;
case CTRL_C_EVENT:
osSignalNo = SIGINT;
break;
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
osSignalNo = SIGTERM;
break;
default:
break;
}
if (OMRPORT_SIG_ERROR != osSignalNo) {
updateSignalCount(osSignalNo);
result = TRUE;
}
return result;
}
static void
removeAsyncHandlers(OMRPortLibrary *portLibrary)
{
/* clean up the list of async handlers */
J9Win32AsyncHandlerRecord *cursor;
J9Win32AsyncHandlerRecord **previousLink;
omrthread_monitor_enter(asyncMonitor);
/* wait until no signals are being reported */
while (asyncThreadCount > 0) {
omrthread_monitor_wait(asyncMonitor);
}
previousLink = &asyncHandlerList;
cursor = asyncHandlerList;
while (cursor) {
if (cursor->portLib == portLibrary) {
*previousLink = cursor->next;
portLibrary->mem_free_memory(portLibrary, cursor);
cursor = *previousLink;
} else {
previousLink = &cursor->next;
cursor = cursor->next;
}
}
if (asyncHandlerList == NULL) {
SetConsoleCtrlHandler(consoleCtrlHandler, FALSE);
}
omrthread_monitor_exit(asyncMonitor);
}
/**
* none of the current options are supported by windows IA32
*/
int32_t
omrsig_set_options(struct OMRPortLibrary *portLibrary, uint32_t options)
{
uint32_t supportedFlags = OMRPORT_SIG_OPTIONS_REDUCED_SIGNALS_SYNCHRONOUS | OMRPORT_SIG_OPTIONS_REDUCED_SIGNALS_ASYNCHRONOUS;
if (options & ~supportedFlags) {
return -1;
}
signalOptions = options;
return 0;
}
/* these options should always be 0 */
uint32_t
omrsig_get_options(struct OMRPortLibrary *portLibrary)
{
return signalOptions;
}
/**
* Set the priority of the asynchronous signal reporting thread (asynchSignalReporterThread).
*
* @param[in] portLibrary the OMR port library
* @param[in] priority the thread priority
*
* @return 0 upon success and non-zero upon failure.
*/
int32_t
omrsig_set_reporter_priority(struct OMRPortLibrary *portLibrary, uintptr_t priority)
{
int32_t result = 0;
omrthread_monitor_t globalMonitor = omrthread_global_monitor();
omrthread_monitor_enter(globalMonitor);
if (attachedPortLibraries > 0) {
result = setReporterPriority(portLibrary, priority);
}
omrthread_monitor_exit(globalMonitor);
return result;
}
static intptr_t
setCurrentSignal(struct OMRPortLibrary *portLibrary, intptr_t signal)
{
omrthread_t thisThread = omrthread_self();
omrthread_tls_set(thisThread, tlsKeyCurrentSignal, (intptr_t *) signal);
return 1;
}
intptr_t
omrsig_get_current_signal(struct OMRPortLibrary *portLibrary)
{
omrthread_t thisThread = omrthread_self();
return (intptr_t) omrthread_tls_get(thisThread, tlsKeyCurrentSignal);
}
/**
* The OS signal number is converted to the corresponding port library
* signal flag.
*
* @param[in] signalNo OS signal number
*
* @return The corresponding port library signal flag on success.
* Otherwise, return 0 in case of error.
*/
static uint32_t
mapOSSignalToPortLib(uint32_t signalNo)
{
uint32_t index = 0;
for (index = 0; index < sizeof(signalMap) / sizeof(signalMap[0]); index++) {
if (signalMap[index].osSignalNo == signalNo) {
return signalMap[index].portLibSignalNo;
}
}
Trc_PRT_signal_mapOSSignalToPortLib_ERROR_unknown_signal(signalNo);
return 0;
}
/**
* The port library signal flag is converted to the corresponding OS signal number.
*
* @param portLibSignal the port library signal flag
*
* @return The corresponding OS signal number or OMRPORT_SIG_ERROR (-1) if the portLibSignal
* can't be mapped.
*/
static int
mapPortLibSignalToOSSignal(uint32_t portLibSignal)
{
uint32_t index = 0;
for (index = 0; index < sizeof(signalMap) / sizeof(signalMap[0]); index++) {
if (signalMap[index].portLibSignalNo == portLibSignal) {
return signalMap[index].osSignalNo;
}
}
Trc_PRT_signal_mapPortLibSignalToOSSignal_ERROR_unknown_signal(portLibSignal);
return OMRPORT_SIG_ERROR;
}
/**
* Register the signal handler with the OS. Calls to this function must be synchronized using
* "registerHandlerMonitor". *oldOSHandler points to the old signal handler function. It is only
* set if oldOSHandler is non-null. During the first registration, the old signal handler is
* stored in handlerInfo[osSignalNo].originalHandler. The original OS handler must be restored
* during port library shut down.
*
* @param[in] portLibrary the OMR port library
* @param[in] portLibrarySignalNo the port library signal flag
* @param[in] handler the OS handler to register
* @param[out] oldOSHandler points to the old OS handler, if oldOSHandler is non-null
*
* @return 0 upon success, non-zero otherwise.
*/