-
Notifications
You must be signed in to change notification settings - Fork 392
/
omrsignal.c
1913 lines (1623 loc) · 60.1 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
*******************************************************************************/
#include <windows.h>
#include <setjmp.h>
#include "omrsignal.h"
#include "omrport.h"
#include "omrutil.h"
#include "omrutilbase.h"
#include "omrthread.h"
#include "ut_omrport.h"
#if defined(OMRPORT_OMRSIG_SUPPORT)
#include "omrsig.h"
#endif /* defined(OMRPORT_OMRSIG_SUPPORT) */
/* UNWIND_CODE and UNWIND_INFO are not in the Windows SDK headers, but they are documented on MSDN */
typedef union UNWIND_CODE {
struct {
uint8_t CodeOffset;
uint8_t UnwindOp : 4;
uint8_t OpInfo : 4;
};
uint16_t FrameOffset;
} UNWIND_CODE;
typedef struct UNWIND_INFO {
uint8_t Version : 3;
uint8_t Flags : 5;
uint8_t SizeOfProlog;
uint8_t CountOfCodes;
uint8_t FrameRegister : 4;
uint8_t FrameOffset : 4;
UNWIND_CODE UnwindCode[1];
union {
OPTIONAL uint32_t ExceptionHandler;
OPTIONAL uint32_t FunctionEntry;
};
OPTIONAL uint32_t ExceptionData[];
} UNWIND_INFO;
typedef struct OMRCurrentSignal {
EXCEPTION_POINTERS *exceptionInfo;
uint32_t portLibSignalType;
} OMRCurrentSignal;
/* key to get the current synchronous signal */
static omrthread_tls_key_t tlsKeyCurrentSignal;
static RUNTIME_FUNCTION *sigProtectFunction = NULL;
struct OMRSignalHandlerRecord {
struct OMRSignalHandlerRecord *previous;
struct OMRPortLibrary *portLibrary;
omrsig_handler_fn handler;
void *handler_arg;
jmp_buf returnBuf;
uint32_t flags;
BOOLEAN deferToTryExcept;
} OMRSignalHandlerRecord;
typedef struct OMRWinAMD64AsyncHandlerRecord {
OMRPortLibrary *portLib;
omrsig_handler_fn handler;
void *handler_arg;
uint32_t flags;
struct OMRWinAMD64AsyncHandlerRecord *next;
} OMRWinAMD64AsyncHandlerRecord;
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};
static omrthread_monitor_t mainExceptionMonitor;
/* access to this must be synchronized using mainExceptionMonitor */
static uint32_t vectoredExceptionHandlerInstalled;
/* Thread to invoke signal handlers for asynchronous signals. */
static omrthread_t asynchSignalReporterThread;
static OMRWinAMD64AsyncHandlerRecord *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;
static omrthread_tls_key_t tlsKey;
/* Calls to registerSignalHandlerWithOS are synchronized using registerHandlerMonitor */
static omrthread_monitor_t registerHandlerMonitor;
/* wakeUpASyncReporter semaphore coordinates between main async signal handler and
* async signal reporter thread.
*/
static j9sem_t wakeUpASyncReporter;
/* Used to synchronize shutdown of asynchSignalReporterThread. */
static omrthread_monitor_t asyncReporterShutdownMonitor;
#if defined(OMR_PORT_ASYNC_HANDLER)
/* Used to indicate start and end of asynchSignalReporterThread termination. */
static uint32_t shutDownASynchReporter;
#endif /* defined(OMR_PORT_ASYNC_HANDLER) */
static uint32_t mapWin32ExceptionToPortlibType(uint32_t exceptionCode);
static uint32_t infoForGPR(struct OMRPortLibrary *portLibrary, struct OMRWin32SignalInfo *info, int32_t index, const char **name, void **value);
static uint32_t infoForFPR(struct OMRPortLibrary *portLibrary, struct OMRWin32SignalInfo *info, int32_t index, const char **name, void **value);
static uint32_t infoForSignal(struct OMRPortLibrary *portLibrary, struct OMRWin32SignalInfo *info, int32_t index, const char **name, void **value);
static uint32_t infoForModule(struct OMRPortLibrary *portLibrary, struct OMRWin32SignalInfo *info, int32_t index, const char **name, void **value);
static uint32_t infoForOther(struct OMRPortLibrary *portLibrary, struct OMRWin32SignalInfo *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);
static LONG WINAPI mainVectoredExceptionHandler(EXCEPTION_POINTERS *exceptionInfo);
static uint32_t infoForControl(struct OMRPortLibrary *portLibrary, struct OMRWin32SignalInfo *info, int32_t index, const char **name, void **value);
static void fillInWinAMD64SignalInfo(struct OMRPortLibrary *portLibrary, omrsig_handler_fn handler, EXCEPTION_POINTERS *exceptionInfo, struct OMRWin32SignalInfo *signalInfo);
static void sig_full_shutdown(struct OMRPortLibrary *portLibrary);
static void destroySignalTools(OMRPortLibrary *portLibrary);
static uint32_t addMainVectoredExceptionHandler(struct OMRPortLibrary *portLibrary);
static int32_t initializeSignalTools(OMRPortLibrary *portLibrary);
static int structuredExceptionHandler(struct OMRPortLibrary *portLibrary, omrsig_handler_fn handler, void *handler_arg, uint32_t flags, EXCEPTION_POINTERS *exceptionInfo);
static int32_t runInTryExcept(struct OMRPortLibrary *portLibrary, omrsig_protected_fn fn, void *fn_arg, omrsig_handler_fn handler, void *handler_arg, uint32_t flags, uintptr_t *result);
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 void updateSignalCount(int osSignalNo);
static void mainASynchSignalHandler(int osSignalNo);
static void removeAsyncHandlers(OMRPortLibrary *portLibrary);
#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 registerMainHandlers(OMRPortLibrary *portLibrary, uint32_t flags, uint32_t allowedSubsetOfFlags, void **oldOSHandler);
static int32_t setReporterPriority(OMRPortLibrary *portLibrary, uintptr_t priority);
static OMRWinAMD64AsyncHandlerRecord *createAsyncHandlerRecord(struct OMRPortLibrary *portLibrary, omrsig_handler_fn handler, void *handler_arg, uint32_t flags);
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);
case OMRPORT_SIG_OTHER:
return infoForOther(portLibrary, info, index, name, value);
default:
return OMRPORT_SIG_VALUE_UNDEFINED;
}
}
uint32_t
omrsig_info_count(struct OMRPortLibrary *portLibrary, void *info, uint32_t category)
{
return countInfoInCategory(portLibrary, info, category);
}
static int32_t
runInTryExcept(struct OMRPortLibrary *portLibrary,
omrsig_protected_fn fn, void *fn_arg,
omrsig_handler_fn handler, void *handler_arg,
uint32_t flags, uintptr_t *result)
{
__try {
*result = fn(portLibrary, fn_arg);
} __except (structuredExceptionHandler(portLibrary, handler, handler_arg, flags, GetExceptionInformation())) {
*result = 0;
return OMRPORT_SIG_EXCEPTION_OCCURRED;
}
return OMRPORT_SIG_NO_EXCEPTION;
}
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;
uintptr_t rc = 0;
omrthread_t thisThread = omrthread_self();
thisRecord.previous = omrthread_tls_get(thisThread, tlsKey);
thisRecord.portLibrary = portLibrary;
thisRecord.handler = handler;
thisRecord.handler_arg = handler_arg;
thisRecord.flags = flags;
thisRecord.deferToTryExcept = FALSE;
/* omrsig_protect cannot protect with a handler that may do both OMRPORT_SIG_FLAG_MAY_RETURN and OMRPORT_SIG_FLAG_CONTINUE_EXECUTION */
Assert_PRT_true((OMRPORT_SIG_FLAG_MAY_RETURN | OMRPORT_SIG_FLAG_MAY_CONTINUE_EXECUTION) != (flags & (OMRPORT_SIG_FLAG_MAY_RETURN | OMRPORT_SIG_FLAG_MAY_CONTINUE_EXECUTION)));
if (OMRPORT_SIG_OPTIONS_REDUCED_SIGNALS_SYNCHRONOUS & signalOptions) {
/* -Xrs was set, we can't protect against any signals, do not install the main handler */
*result = fn(portLibrary, fn_arg);
return 0;
}
/* Check to see if we have already registered the mainVectoredExceptionHandler.
* Because we are checking without acquiring the monitor first, we'll have to check again.
* Note that that we don't uninstall the mainHandlerMonitor, so we only have to account for it going from not installed to installed.
*/
if (0 == vectoredExceptionHandlerInstalled) {
omrthread_monitor_enter(mainExceptionMonitor);
if (0 == vectoredExceptionHandlerInstalled) {
rc = addMainVectoredExceptionHandler(portLibrary);
}
omrthread_monitor_exit(mainExceptionMonitor);
if (rc) {
return OMRPORT_SIG_ERROR;
}
}
omrthread_tls_set(thisThread, tlsKey, &thisRecord);
/* We can not use setjmp/longjmp to jump out of a VectoredExceptionHandler (CMVC 175576), so use _try/_except semantics instead */
if (OMR_ARE_ALL_BITS_SET(flags, OMRPORT_SIG_FLAG_MAY_RETURN)) {
OMRCurrentSignal *currentSignal = omrthread_tls_get(thisThread, tlsKeyCurrentSignal);
int32_t exceptionStatus = OMRPORT_SIG_NO_EXCEPTION;
/* The VectoredExceptionHandler will get notified before the _except clause/block below,
* this tells the VectoredExceptionHandler to defer to this _except clause/block */
thisRecord.deferToTryExcept = TRUE;
/*
* Jazz 55704 - functions are statically marked with UNW_FLAG_EHANDLER if they contain a _try/__except block.
* This confuses tryExceptHandlerExistsOnStack() into seeing omrsig_protect as a _try/__except function,
* even when it (dynamically) isn't using it.
*/
exceptionStatus = runInTryExcept(portLibrary, fn, fn_arg, handler, handler_arg, flags, result);
if (OMRPORT_SIG_EXCEPTION_OCCURRED == exceptionStatus) {
omrthread_tls_set(thisThread, tlsKey, thisRecord.previous);
omrthread_tls_set(thisThread, tlsKeyCurrentSignal, currentSignal);
return OMRPORT_SIG_EXCEPTION_OCCURRED;
}
omrthread_tls_set(thisThread, tlsKey, thisRecord.previous);
return 0;
}
*result = fn(portLibrary, fn_arg);
omrthread_tls_set(thisThread, tlsKey, thisRecord.previous);
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;
OMRWinAMD64AsyncHandlerRecord *cursor = NULL;
OMRWinAMD64AsyncHandlerRecord **previousLink = NULL;
Trc_PRT_signal_omrsig_set_async_signal_handler_entered(handler, handler_arg, flags);
rc = registerMainHandlers(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) {
/* 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);
/* 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)) {
OMRWinAMD64AsyncHandlerRecord *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;
OMRWinAMD64AsyncHandlerRecord *cursor = NULL;
OMRWinAMD64AsyncHandlerRecord **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 = registerMainHandlers(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)) {
OMRWinAMD64AsyncHandlerRecord *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_main_signal_handler(struct OMRPortLibrary *portLibrary, void *osHandler)
{
BOOLEAN rc = FALSE;
Trc_PRT_signal_omrsig_is_main_signal_handler_entered(osHandler);
if (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;
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;
Trc_PRT_signal_omrsig_can_protect_entered(flags);
if (OMR_ARE_NO_BITS_SET(signalOptions, 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;
}
/*
* The full shutdown routine "sig_full_shutdown" overwrites this once we've completed startup
*/
void
omrsig_shutdown(struct OMRPortLibrary *portLibrary)
{
}
/**
* Start up the signal handling component of the port library
*/
int32_t
omrsig_startup(struct OMRPortLibrary *portLibrary)
{
int32_t result = 0;
ULONG64 imageBase = 0;
uint32_t index = 1;
UNWIND_HISTORY_TABLE unwindHistoryTable;
omrthread_monitor_t globalMonitor = omrthread_global_monitor();
if (omrthread_tls_alloc(&tlsKey)) {
return OMRPORT_ERROR_STARTUP_SIGNAL;
}
if (omrthread_tls_alloc(&tlsKeyCurrentSignal)) {
return OMRPORT_ERROR_STARTUP_SIGNAL;
}
omrthread_monitor_enter(globalMonitor);
if (attachedPortLibraries++ == 0) {
/* initialize handlerInfo */
for (index = 1; index < ARRAY_SIZE_SIGNALS; index++) {
handlerInfo[index].restore = 0;
}
if (0 != initializeSignalTools(portLibrary)) {
result = OMRPORT_ERROR_STARTUP_SIGNAL_TOOLS;
}
}
memset(&unwindHistoryTable, 0, sizeof(UNWIND_HISTORY_TABLE));
sigProtectFunction = RtlLookupFunctionEntry((ULONG64)omrsig_protect, &imageBase, &unwindHistoryTable);
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;
}
return result;
}
int32_t
omrsig_set_options(struct OMRPortLibrary *portLibrary, uint32_t options)
{
uintptr_t handlersInstalled = 0;
if ((OMRPORT_SIG_OPTIONS_REDUCED_SIGNALS_SYNCHRONOUS | OMRPORT_SIG_OPTIONS_REDUCED_SIGNALS_ASYNCHRONOUS) & options) {
/* check that we haven't already set any main handlers */
omrthread_monitor_enter(mainExceptionMonitor);
if (0 != vectoredExceptionHandlerInstalled) {
handlersInstalled = 1;
}
omrthread_monitor_exit(mainExceptionMonitor);
}
if (handlersInstalled) {
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;
}
intptr_t
omrsig_get_current_signal(struct OMRPortLibrary *portLibrary)
{
omrthread_t thisThread = omrthread_self();
struct OMRCurrentSignal *currentSignal = omrthread_tls_get(thisThread, tlsKeyCurrentSignal);
if (currentSignal != NULL) {
return currentSignal->portLibSignalType;
}
return 0;
}
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:
default:
return OMRPORT_SIG_FLAG_SIGSEGV;
}
}
/**
* Look for a try/except signal handler on the stack. Do not search past omrsig_protect.
* @param portLibrary port library
* @param context Windows _CONTEXT structure containing register information
* @return TRUE if the there is a try/except handler registered on the stack
*/
static BOOLEAN
tryExceptHandlerExistsOnStack(OMRPortLibrary *portLibrary, CONTEXT *originalContext)
{
KNONVOLATILE_CONTEXT_POINTERS nvContext;
UNWIND_HISTORY_TABLE unwindHistoryTable;
RUNTIME_FUNCTION *runtimeFunction;
PVOID handlerData;
ULONG64 establisherFrame;
ULONG64 imageBase;
UNWIND_INFO *unwindInfo;
BOOLEAN firstFrame = TRUE;
CONTEXT unwindContext;
CONTEXT *context = &unwindContext;
#if !defined(UNW_FLAG_NHANDLER)
#define UNW_FLAG_NHANDLER 0x0
#endif
#if !defined(UNW_FLAG_EHANDLER)
#define UNW_FLAG_EHANDLER 0x01
#endif
#if !defined(UNW_FLAG_UHANDLER)
#define UNW_FLAG_UHANDLER 0x02
#endif
#if !defined(UNW_FLAG_CHAININFO)
#define UNW_FLAG_CHAININFO 0x04
#endif
memset(&unwindHistoryTable, 0, sizeof(UNWIND_HISTORY_TABLE));
memcpy(context, originalContext, sizeof(CONTEXT));
do {
runtimeFunction = RtlLookupFunctionEntry(context->Rip, &imageBase, &unwindHistoryTable);
memset(&nvContext, 0 , sizeof(KNONVOLATILE_CONTEXT_POINTERS));
if (!runtimeFunction) {
if (!firstFrame) {
break;
}
context->Rip = (ULONG64)(*(PULONG64)context->Rsp);
context->Rsp += 8;
} else {
uint8_t flags;
unwindInfo = (UNWIND_INFO *)(runtimeFunction->UnwindData + imageBase);
flags = unwindInfo->Flags;
if (runtimeFunction == sigProtectFunction) {
return FALSE;
} else if (UNW_FLAG_EHANDLER == flags) {
/* we found a try/except handler */
return TRUE;
}
RtlVirtualUnwind(
UNW_FLAG_NHANDLER, /* suppresses the invocation of any unwind or exception handlers registered by the function).*/
imageBase,
context->Rip,
runtimeFunction,
context,
&handlerData,
&establisherFrame,
&nvContext);
}
firstFrame = FALSE;
} while (0 != context->Rip);
/* no try/except handler was found */
return FALSE;
}
static uint32_t
infoForOther(struct OMRPortLibrary *portLibrary, struct OMRWin32SignalInfo *info, int32_t index, const char **name, void **value)
{
*name = "";
switch (index) {
/* hide this from users that iterate through all of the omrsig_info() by only exposing it through OMRPORT_SIG_WINDOWS_DEFER_TRY_EXCEPT_HANDLER
* which is a negative value
*/
case OMRPORT_SIG_WINDOWS_DEFER_TRY_EXCEPT_HANDLER:
*name = "Defer_to_Try_Except_Handler";
info->deferToTryExcept = !info->tryExceptHandlerIgnore
&& tryExceptHandlerExistsOnStack(portLibrary, info->ContextRecord);
*value = &info->deferToTryExcept;
return OMRPORT_SIG_VALUE_32;
default:
return OMRPORT_SIG_VALUE_UNDEFINED;
}
}
static uint32_t
infoForSignal(struct OMRPortLibrary *portLibrary, struct OMRWin32SignalInfo *info, int32_t index, const char **name, void **value)
{
*name = "";
switch (index) {
case OMRPORT_SIG_SIGNAL_PLATFORM_SIGNAL_TYPE:
case 0:
*name = "Windows_ExceptionCode";
*value = &info->systemType;
return OMRPORT_SIG_VALUE_32;
case OMRPORT_SIG_SIGNAL_TYPE:
case 1:
*name = "J9Generic_Signal";
*value = &info->portLibType;
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 OMRWin32SignalInfo *info, int32_t index, const char **name, void **value)
{
*name = "";
if (info->ContextRecord->ContextFlags & CONTEXT_INTEGER) {
switch (index) {
case OMRPORT_SIG_GPR_AMD64_RDI:
case 0:
*name = "RDI";
*value = &info->ContextRecord->Rdi;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_AMD64_RSI:
case 1:
*name = "RSI";
*value = &info->ContextRecord->Rsi;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_AMD64_RAX:
case 2:
*name = "RAX";
*value = &info->ContextRecord->Rax;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_AMD64_RBX:
case 3:
*name = "RBX";
*value = &info->ContextRecord->Rbx;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_AMD64_RCX:
case 4:
*name = "RCX";
*value = &info->ContextRecord->Rcx;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_AMD64_RDX:
case 5:
*name = "RDX";
*value = &info->ContextRecord->Rdx;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_AMD64_R8:
case 6:
*name = "R8";
*value = &info->ContextRecord->R8;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_AMD64_R9:
case 7:
*name = "R9";
*value = &info->ContextRecord->R9;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_AMD64_R10:
case 8:
*name = "R10";
*value = &info->ContextRecord->R10;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_AMD64_R11:
case 9:
*name = "R11";
*value = &info->ContextRecord->R11;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_AMD64_R12:
case 10:
*name = "R12";
*value = &info->ContextRecord->R12;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_AMD64_R13:
case 11:
*name = "R13";
*value = &info->ContextRecord->R13;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_AMD64_R14:
case 12:
*name = "R14";
*value = &info->ContextRecord->R14;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_GPR_AMD64_R15:
case 13:
*name = "R15";
*value = &info->ContextRecord->R15;
return OMRPORT_SIG_VALUE_ADDRESS;
default:
return OMRPORT_SIG_VALUE_UNDEFINED;
}
}
return OMRPORT_SIG_VALUE_UNDEFINED;
}
static uint32_t
infoForControl(struct OMRPortLibrary *portLibrary, struct OMRWin32SignalInfo *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 = "RIP";
*value = &info->ContextRecord->Rip;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_CONTROL_SP:
case 1:
*name = "RSP";
*value = &info->ContextRecord->Rsp;
return OMRPORT_SIG_VALUE_ADDRESS;
case OMRPORT_SIG_CONTROL_BP:
case 2:
*name = "RBP";
*value = &info->ContextRecord->Rbp;
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 3:
*name = "GS";
*value = &info->ContextRecord->SegGs;
return OMRPORT_SIG_VALUE_16;
case 4: