-
Notifications
You must be signed in to change notification settings - Fork 752
/
Copy pathgphandle.c
1385 lines (1171 loc) · 42.1 KB
/
gphandle.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, 2020 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
*******************************************************************************/
/* Turn off FPO optimisation on Windows 32-bit to improve native stack traces */
#if defined(WIN32)
#pragma optimize("y",off)
#endif
#include <stdlib.h>
#include <string.h>
#include "j9.h"
#include "j9cfg.h"
#include "j9protos.h"
#include "j9port.h"
#include "j9dump.h"
#include "j9consts.h"
#include "gp.h"
#include "rommeth.h"
#include "j9cp.h"
#include "vmhook_internal.h"
#include "fltconst.h"
#include "vm_internal.h"
#include "ut_j9vm.h"
#include "j9vmnls.h"
#include "j9version.h"
#if defined(J9VM_PORT_ZOS_CEEHDLRSUPPORT)
#include "leconditionexceptionsup.h"
#include "jni.h"
#include "atoe.h"
#define K8ZOS_CONTINUE_SEARCH 1
#define K8ZOS_RESUME_EXECUTION 2
#define K8ZOS_GO_DOWN 3
static UDATA prepareToResumeBackInJava(struct J9PortLibrary* portLibrary, J9VMThread *vmThread, void *sigInfo);
static UDATA handleJNIZOSLECondition(struct J9PortLibrary* portLibrary, J9VMThread *vmThread, void* gpInfo);
static UDATA throwConditionException(J9VMThread *vmThread, struct ConditionExceptionConstructorArgs *ceArgs);
#endif
#if defined(J9VM_PORT_OMRSIG_SUPPORT)
#include "omrsig.h"
#endif /* defined(J9VM_PORT_OMRSIG_SUPPORT) */
#define J9DO_NOT_WRITE_DUMP "J9NO_DUMPS"
#define INFO_COLS 4
#define MEMORY_SEGMENT_LIST_DO(segmentList, imageSegment) {\
J9MemorySegment *imageSegment, *i2; \
imageSegment = segmentList->nextSegment; \
while(imageSegment) { \
i2 = (J9MemorySegment *)(imageSegment->nextSegment);
#define END_MEMORY_SEGMENT_LIST_DO(imageSegment) \
imageSegment = i2; }}
typedef struct J9CrashData {
J9JavaVM* javaVM;
J9VMThread* vmThread;
U_32 gpType;
void* gpInfo;
char *consoleOutputBuf;
size_t sizeofConsoleOutputBuf;
#ifdef J9VM_RAS_EYECATCHERS
J9RASCrashInfo *rasCrashInfo;
#endif
} J9CrashData;
typedef struct J9WriteGPInfoData {
char *s;
UDATA length;
void* gpInfo;
U_32 category;
} J9WriteGPInfoData;
typedef struct J9RecursiveCrashData {
char *protectedFunctionName;
U_32 info;
} J9RecursiveCrashData;
typedef struct vmDetails {
UDATA cpus;
U_64 mem;
const char *osBuf;
const char *cpuBuf;
const char *versionBuf;
} vmDetails;
typedef struct J9SignalHandlerParams {
J9JavaVM *vm;
J9VMThread *thread;
} J9SignalHandlerParams;
static void generateSystemDump(struct J9PortLibrary* portLibrary, void* gpInfo);
static UDATA executeAbortHook (struct J9PortLibrary* portLibrary, void* userData);
static UDATA writeCrashDataToConsole (struct J9PortLibrary* portLibrary, void* userData);
static const char* getSignalDescription (struct J9PortLibrary* portLibrary, U_32 gpType);
static UDATA recursiveCrashHandler (struct J9PortLibrary* portLibrary, U_32 gpType, void* gpInfo, void *recursiveCrashHandlerData);
static void dumpVmDetailString(struct J9PortLibrary *portLibrary, J9JavaVM* vm);
static void getVmDetailsFromPortLib(struct J9PortLibrary *portLibrary, struct vmDetails *details);
static UDATA writeVMInfo(J9JavaVM* vm, char* s, UDATA length);
static UDATA writeGPInfo(struct J9PortLibrary* portLibrary, void *writeGPInfoCrashData);
static UDATA reportThreadCrash(struct J9PortLibrary* portLibrary, void* userData);
static UDATA generateDiagnosticFiles(struct J9PortLibrary* portLibrary, void* userData);
UDATA vmSignalHandler(struct J9PortLibrary* portLibrary, U_32 gpType, void* gpInfo, void* userData);
#if defined(J9VM_INTERP_USE_UNSAFE_HELPER)
static UDATA isCallerClassJavaNio(J9VMThread *currentThread, J9StackWalkState *walkState);
static UDATA sigBusHandler(J9VMThread *vmThread, void* gpInfo);
#endif /* defined(J9VM_INTERP_USE_UNSAFE_HELPER) */
#ifdef J9VM_RAS_EYECATCHERS
static UDATA setupRasCrashInfo(struct J9PortLibrary* portLibrary, void* userData);
#endif
#if defined(J9VM_INTERP_NATIVE_SUPPORT)
static UDATA writeJITInfo (J9VMThread* vmThread, char* s, UDATA length, void* gpInfo);
#endif /* J9VM_INTERP_NATIVE_SUPPORT */
#if defined(J9VM_RAS_DUMP_AGENTS)
static void printBacktrace(struct J9JavaVM *vm, void* gpInfo);
#endif /* J9VM_RAS_DUMP_AGENTS */
#if defined(J9VM_ARCH_X86) && defined(J9VM_ENV_DATA64)
#define UNSAFE_TARGET_REGISTER J9PORT_SIG_GPR_AMD64_RDI
#elif defined(J9VM_ARCH_X86) && !defined(J9VM_ENV_DATA64)
#define UNSAFE_TARGET_REGISTER J9PORT_SIG_GPR_X86_EAX
#elif defined(AIXPPC) || (defined(LINUXPPC64) && defined(J9VM_ENV_LITTLE_ENDIAN))
/* The target register is GPR3 */
#define UNSAFE_TARGET_REGISTER 3
#elif defined(S390) && defined(LINUX)
/* The target register is GPR2 */
#define UNSAFE_TARGET_REGISTER 2
#elif defined(J9VM_ARCH_RISCV)
/* The target register is GPR1 */
#define UNSAFE_TARGET_REGISTER 1
#elif defined(J9ZOS390)
/* The target register is GPR1 */
#define UNSAFE_TARGET_REGISTER 1
#elif defined(J9VM_ARCH_ARM) && defined(LINUX)
/* The target register is GPR0 */
#define UNSAFE_TARGET_REGISTER 0
#elif defined(J9VM_ARCH_AARCH64)
/* The target register is GPR0 */
#define UNSAFE_TARGET_REGISTER 0
#endif /* defined(J9VM_ARCH_X86) && defined(J9VM_ENV_DATA64) */
static void
getVmDetailsFromPortLib(struct J9PortLibrary *portLibrary, struct vmDetails *details)
{
PORT_ACCESS_FROM_PORT(portLibrary);
details->osBuf = j9sysinfo_get_OS_type();
details->versionBuf = j9sysinfo_get_OS_version();
details->cpuBuf = j9sysinfo_get_CPU_architecture();
details->cpus = j9sysinfo_get_number_CPUs_by_type(J9PORT_CPU_ONLINE);
details->mem = j9sysinfo_get_physical_memory();
}
static void
dumpVmDetailString(struct J9PortLibrary *portLibrary, J9JavaVM* vm)
{
PORT_ACCESS_FROM_PORT(portLibrary);
struct vmDetails details;
#if defined(J9VM_RAS_EYECATCHERS)
if (NULL != vm->j9ras) {
details.osBuf = (char*)vm->j9ras->osname;
details.versionBuf = (char*)vm->j9ras->osversion;
details.cpuBuf = (char*)vm->j9ras->osarch;
details.cpus = vm->j9ras->cpus;
details.mem = vm->j9ras->memory;
} else
#endif
{
getVmDetailsFromPortLib(portLibrary, &details);
}
j9tty_printf(portLibrary, "Target=%u_%02u_" EsBuildVersionString " (%s %s)\n", EsVersionMajor, EsVersionMinor,
details.osBuf ? details.osBuf : "unknown", details.versionBuf ? details.versionBuf : "unknown");
j9tty_printf(portLibrary, "CPU=%s (%d logical CPUs) (0x%llx RAM)\n",
details.cpuBuf ? details.cpuBuf : "unknown", details.cpus, details.mem);
}
static UDATA
reportThreadCrash(struct J9PortLibrary* portLibrary, void* userData)
{
J9CrashData* data = userData;
J9JavaVM* vm = data->javaVM;
J9VMThread *crashingThread = data->vmThread;
TRIGGER_J9HOOK_VM_THREAD_CRASH(vm->hookInterface, crashingThread);
return 0;
}
#if defined(J9VM_INTERP_USE_UNSAFE_HELPER)
static UDATA
isCallerClassJavaNio(J9VMThread *currentThread, J9StackWalkState *walkState)
{
UDATA rc = J9_STACKWALK_KEEP_ITERATING;
if (NULL != walkState->method) {
J9UTF8 *className = J9ROMCLASS_CLASSNAME(J9_CLASS_FROM_METHOD(walkState->method)->romClass);
UDATA length = J9UTF8_LENGTH(className);
U_8 *data = J9UTF8_DATA(className);
/* Ignore any methods in Unsafe */
if (J9UTF8_LITERAL_EQUALS(data, length, "sun/misc/Unsafe") || J9UTF8_LITERAL_EQUALS(data, length, "jdk/internal/misc/Unsafe")) {
goto done;
}
if (length >= 9) {
if (0 == memcmp(data, "java/nio/", 9)) {
walkState->userData1 = (void *)TRUE;
}
}
rc = J9_STACKWALK_STOP_ITERATING;
}
done:
return rc;
}
static UDATA
sigBusHandler(J9VMThread *vmThread, void* gpInfo)
{
UDATA rc = J9PORT_SIG_EXCEPTION_CONTINUE_SEARCH;
J9JavaVM* vm = vmThread->javaVM;
J9StackWalkState walkState;
PORT_ACCESS_FROM_VMC(vmThread);
walkState.skipCount = 0;
walkState.walkThread = vmThread;
walkState.flags = J9_STACKWALK_VISIBLE_ONLY | J9_STACKWALK_ITERATE_FRAMES;
walkState.frameWalkFunction = isCallerClassJavaNio;
walkState.userData1 = (void *) FALSE;
vm->walkStackFrames(vmThread, &walkState);
if ((void *)TRUE == walkState.userData1) {
U_32 gpType = 0;
UDATA* rspPtr = NULL;
UDATA* targetRegPtr = NULL;
const char* infoName = NULL;
void* infoValue = NULL;
#if defined(AIXPPC) || (defined(LINUXPPC64) && defined(J9VM_ENV_LITTLE_ENDIAN))
/* GPR1 is the stack pointer */
gpType = j9sig_info(gpInfo, J9PORT_SIG_GPR, 1, &infoName, &infoValue);
#elif defined(S390) && defined(LINUX)
/* GPR15 is the stack pointer */
gpType = j9sig_info(gpInfo, J9PORT_SIG_GPR, 15, &infoName, &infoValue);
#elif defined(J9ZOS390)
/* GPR4 is the stack pointer */
gpType = j9sig_info(gpInfo, J9PORT_SIG_GPR, 4, &infoName, &infoValue);
#else
gpType = j9sig_info(gpInfo, J9PORT_SIG_CONTROL, J9PORT_SIG_CONTROL_SP, &infoName, &infoValue);
#endif /* defined(AIXPPC) || (defined(LINUXPPC64) && defined(J9VM_ENV_LITTLE_ENDIAN)) */
if (gpType != J9PORT_SIG_VALUE_ADDRESS) {
goto done;
}
rspPtr = (UDATA*) infoValue;
gpType = j9sig_info(gpInfo, J9PORT_SIG_GPR, UNSAFE_TARGET_REGISTER, &infoName, &infoValue);
if (gpType != J9PORT_SIG_VALUE_ADDRESS) {
goto done;
}
targetRegPtr = (UDATA*) infoValue;
/* change the target register to a writable address and clear J9_PRIVATE_FLAGS2_UNSAFE_HANDLE_SIGBUS. */
#if defined(J9ZOS390)
/* Stack register is biased. It is 2048 bytes before the top of stack. It is safer to add the bias back. */
*targetRegPtr = *rspPtr + 2048 - 16;
#else
*targetRegPtr = *rspPtr - 16;
#endif /* defined(J9ZOS390) */
vmThread->privateFlags2 &= ~J9_PRIVATE_FLAGS2_UNSAFE_HANDLE_SIGBUS;
rc = J9PORT_SIG_EXCEPTION_CONTINUE_EXECUTION;
}
done:
return rc;
}
#endif /* defined(J9VM_INTERP_USE_UNSAFE_HELPER) */
#if defined(J9VM_PORT_ZOS_CEEHDLRSUPPORT)
/*
* Returns non-zero if it failed to throw the ConditionException, in which case the caller
* should check for a pending Java exception.
*
* @param vmthread
* @param ceArgs contains all the parameters required by com.ibm.le.conditionhandling.ConditionHandler()
* some of these need to be cast/converted to types understood by the JNI.
*/
static UDATA
throwConditionException(J9VMThread *vmThread, struct ConditionExceptionConstructorArgs *ceArgs)
{
JNIEnv *env = (JNIEnv *) vmThread;
jclass cls;
jmethodID cid;
jobject ceObject;
jint throwResult;
jstring failingRoutineJString;
jstring facilityIDJString;
jbyteArray rawTokenBytesJBArray;
char *failingRoutine = "";
UDATA rc = 1;
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
rawTokenBytesJBArray = (*env)->NewByteArray(env, ceArgs->lenBytesRawTokenBytes);
if (NULL == rawTokenBytesJBArray) {
goto cleanup;
}
(*env)->SetByteArrayRegion(env, rawTokenBytesJBArray, 0, ceArgs->lenBytesRawTokenBytes, (jbyte *)ceArgs->rawTokenBytes);
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
if (NULL != ceArgs->failingRoutine) {
failingRoutine = ceArgs->failingRoutine;
}
failingRoutineJString = (*env)->NewStringUTF(env, ceArgs->failingRoutine);
if (NULL == failingRoutineJString) {
goto cleanup;
}
facilityIDJString = (*env)->NewStringUTF(env, ceArgs->facilityID);
if (NULL == facilityIDJString) {
goto cleanup;
}
cls = (*env)->FindClass(env, "com/ibm/le/conditionhandling/ConditionException");
if (NULL == cls) {
goto cleanup;
}
cid = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/lang/String;J[BLjava/lang/String;IIIIIJ)V");
if (NULL == cid) {
goto cleanup;
}
ceObject = (*env)->NewObject(env, cls, cid, failingRoutineJString, ceArgs->offset, rawTokenBytesJBArray,
facilityIDJString, ceArgs->c1, ceArgs->c2, ceArgs->caze,
ceArgs->severity, ceArgs->control, ceArgs->iSInfo);
if (NULL == ceObject) {
goto cleanup;
}
throwResult = (*env)->Throw(env, ceObject);
if (0 != throwResult) {
goto cleanup;
}
/* success */
rc = 0;
cleanup:
if (NULL != failingRoutineJString) {
(*env)->DeleteLocalRef(env,failingRoutineJString);
}
if (NULL != facilityIDJString) {
(*env)->DeleteLocalRef(env,facilityIDJString);
}
if (NULL != rawTokenBytesJBArray) {
(*env)->DeleteLocalRef(env,rawTokenBytesJBArray);
}
if (NULL != cls) {
(*env)->DeleteLocalRef(env,cls);
}
if (NULL != ceObject) {
(*env)->DeleteLocalRef(env,ceObject);
}
return rc;
}
/*
* Prepares to resume execution
*
* Return K8ZOS_RESUME_EXECUTION if you want to resume execution, K8ZOS_GO_DOWN if there was a failure
*/
static UDATA
prepareToResumeBackInJava(struct J9PortLibrary* portLibrary, J9VMThread *vmThread, void *sigInfo)
{
void *infoValue;
const char* infoName;
U_32 infoType;
long builderDSA;
UDATA *builderGPRs;
U_64 *builderFPRs;
UDATA i;
long targetAddress;
PORT_ACCESS_FROM_PORT(portLibrary);
if ((vmThread->omrVMThread->vmState) == J9VMSTATE_JNI_FROM_JIT) {
builderGPRs = vmThread->entryLocalStorage->jitGlobalStorageBase;
builderFPRs = (U_64 *)vmThread->entryLocalStorage->jitFPRegisterStorageBase;
} else if ((vmThread->omrVMThread->vmState) == J9VMSTATE_JNI){
builderGPRs = vmThread->entryLocalStorage->ceehdlrGPRBase;
builderFPRs = (U_64 *)vmThread->entryLocalStorage->ceehdlrFPRBase;
} else {
Assert_VM_unreachable();
}
builderDSA = builderGPRs[4];
targetAddress = getReturnAddressOfJNINative((void *)builderDSA);
if (-1 == targetAddress) {
return K8ZOS_GO_DOWN;
}
/* gprs 1-3 need to be set to zero so that the return value of the native isn't mistaken for a valid JNI ref */
for (i = 1 ; i <=3 ; i ++) {
infoType = j9sig_info(sigInfo, J9PORT_SIG_GPR, i, &infoName, &infoValue);
if (infoType != J9PORT_SIG_VALUE_ADDRESS) {
return K8ZOS_GO_DOWN;
}
*(UDATA *)infoValue = 0;
}
/* low gprs */
for (i = 4 ; i <=15 ; i++) {
infoType = j9sig_info(sigInfo, J9PORT_SIG_GPR, i, &infoName, &infoValue);
if (infoType != J9PORT_SIG_VALUE_ADDRESS) {
return K8ZOS_GO_DOWN;
}
*(UDATA *)infoValue = builderGPRs[i];
}
#if !defined(J9VM_ENV_DATA_64)
/* high gprs */
for (i = 16 ; i <=31 ; i ++) {
infoType = j9sig_info(sigInfo, J9PORT_SIG_GPR, i, &infoName, &infoValue);
if (infoType != J9PORT_SIG_VALUE_ADDRESS) {
return K8ZOS_GO_DOWN;
}
*(UDATA *)infoValue = builderGPRs[i];
}
#endif
/* FPRs */
for (i = 0 ; i <=15 ; i++) {
infoType = j9sig_info(sigInfo, J9PORT_SIG_FPR, i, &infoName, &infoValue);
if (infoType != J9PORT_SIG_VALUE_64) {
return K8ZOS_GO_DOWN;
}
*(U_64 *)infoValue = builderFPRs[i];
}
infoType = j9sig_info(sigInfo, J9PORT_SIG_CONTROL, J9PORT_SIG_CONTROL_S390_FPC, &infoName, &infoValue);
if (infoType != J9PORT_SIG_VALUE_32) {
return K8ZOS_GO_DOWN;
}
*(U_32 *)infoValue = *((U_32 *)vmThread->entryLocalStorage->ceehdlrFPCLocation);
infoType = j9sig_info(sigInfo, J9PORT_SIG_GPR, 7, &infoName, &infoValue);
if (infoType != J9PORT_SIG_VALUE_ADDRESS) {
return K8ZOS_GO_DOWN;
}
*(UDATA *)infoValue = targetAddress;
return K8ZOS_RESUME_EXECUTION;
}
/*
* If the condition corresponding to the gpInfo token
* is between CEE 3208-3234 inclusive and condition occurred while executing a JNI native
* create a com.ibm.le.conditionhandling.ConditionException, throw it,
* and K8ZOS_RESUME_EXECUTION indicating to the caller that the signal handler
* should return J9PORT_SIG_EXCEPTION_CONTINUE_EXECUTION
*
* If the condition is sev 0 or 1, return K8ZOS_CONTINUE_SEARCH so that the condition is percolated.
*
* If a problem occurs, return K8ZOS_GO_DOWN, indicating to the caller that it should proceed to generate
* diagnostics and shut down.
*
* @param portLibrary
* @param thread
* @param goInfo
*
* @return
* K8ZOS_RESUME_EXECUTION if the signal handler should continue execution
* K8ZOS_CONTINUE_SEARCH if you want the signal handler to continue searching,
* K8ZOS_GO_DOWN if you want to follow the crash route.
*
*/
static UDATA
handleJNIZOSLECondition(struct J9PortLibrary* portLibrary, J9VMThread *vmThread, void* gpInfo) {
J9JavaVM *vm = vmThread->javaVM;
J9VMEntryLocalStorage* els = vmThread->entryLocalStorage;
void *infoValue;
const char *infoName;
U_32 infoType;
U_32 infoKind;
U_16 severity;
char *facilityID;
PORT_ACCESS_FROM_PORT(portLibrary);
if ( (vmThread->omrVMThread->vmState & J9VMSTATE_MAJOR) == J9VMSTATE_JNI) {
infoType = j9sig_info(gpInfo, J9PORT_SIG_SIGNAL, J9PORT_SIG_SIGNAL_ZOS_CONDITION_SEVERITY, &infoName, &infoValue);
if (J9PORT_SIG_VALUE_16 == infoType) {
severity = *(U_16 *)infoValue;
} else {
return K8ZOS_GO_DOWN;
}
/* Check to see if the condition is in the range CEE 3208-3234 inclusive */
infoType = j9sig_info(gpInfo, J9PORT_SIG_SIGNAL, J9PORT_SIG_SIGNAL_ZOS_CONDITION_FACILITY_ID, &infoName, &infoValue);
if (J9PORT_SIG_VALUE_STRING == infoType) {
facilityID = (char *)infoValue;
} else {
return K8ZOS_GO_DOWN;
}
if ( (0 == strncmp("CEE",facilityID,3)) && (3 == severity) ) {
U_16 messageNumber;
infoType = j9sig_info(gpInfo, J9PORT_SIG_SIGNAL, J9PORT_SIG_SIGNAL_ZOS_CONDITION_MESSAGE_NUMBER, &infoName, &infoValue);
if (J9PORT_SIG_VALUE_16 != infoType) {
return K8ZOS_GO_DOWN;
}
messageNumber = *(U_16 *)infoValue;
if ( ((3208 <= messageNumber) && (messageNumber <= 3234)) ) {
struct ConditionExceptionConstructorArgs ceArgs;
UDATA prepJmpRC = 0;
UDATA throwRC = 0;
/* convert the condition to an exception */
getConditionExceptionConstructorArgs(portLibrary, gpInfo, &ceArgs);
throwRC = throwConditionException(vmThread, &ceArgs);
/* free memory allocated by e2a_func() */
if (NULL != ceArgs.failingRoutine) {
free(ceArgs.failingRoutine);
}
if (0 != throwRC) {
return K8ZOS_GO_DOWN;
}
prepJmpRC = prepareToResumeBackInJava(portLibrary, vmThread, gpInfo);
if (K8ZOS_RESUME_EXECUTION == prepJmpRC) {
/* record that we converted a LE Condition to a Java Exception */
vmThread->javaVM->leConditionConvertedToJavaException = 1;
vmThread->leConditionConvertedToJavaException = 1;
return K8ZOS_RESUME_EXECUTION;
}
}
}
}
return K8ZOS_GO_DOWN;
}
#endif /* J9VM_PORT_ZOS_CEEHDLRSUPPORT */
#if defined(J9VM_OPT_SWITCH_STACKS_FOR_SIGNAL_HANDLER)
UDATA swapStacksAndRunHandler(struct J9PortLibrary* portLibrary, U_32 gpType, void* gpInfo, J9VMThread* userData);
/**
* @internal
*
* The generic VM signal handler.
* Assumes the void* parameter is a J9JavaVM.
*/
UDATA
structuredSignalHandlerVM(struct J9PortLibrary* portLibrary, U_32 gpType, void* gpInfo, void* userData)
{
void *handlerParam = NULL;
J9JavaVM *javaVM = userData;
J9VMThread* vmThread = currentVMThread(javaVM);
/* We may not always be executing on a Java thread! */
if (NULL == vmThread) {
handlerParam = javaVM;
} else {
handlerParam = vmThread;
}
return structuredSignalHandler(portLibrary, gpType, gpInfo, handlerParam);
}
/**
* @internal
*
* The generic VM signal handler.
* Assumes the void* parameter is a J9VMThread.
*/
UDATA
structuredSignalHandler(struct J9PortLibrary* portLibrary, U_32 gpType, void* gpInfo, void* userData)
{
J9VMThread *vmThread = userData;
/* See if it *is* actually a thread and not a VM pointer! */
if ((NULL != vmThread) && ( ((J9JavaVM*) vmThread) != vmThread->javaVM)) {
/* Figure out the bounds of the Java stack */
J9JavaStack* javaStack = vmThread->stackObject;
UDATA* lowestSlot = J9_LOWEST_STACK_SLOT(vmThread);
UDATA* highestSlot = javaStack->end;
UDATA* localAddress = (UDATA*)&highestSlot;
/* if our local variables are located in the Java stack we know that an ESP flip is required */
if ((localAddress >= lowestSlot) && (localAddress < highestSlot)) {
return swapStacksAndRunHandler(portLibrary, gpType, gpInfo, vmThread);
}
}
return vmSignalHandler(portLibrary, gpType, gpInfo, vmThread);
}
#else
#define vmSignalHandler structuredSignalHandler
#define vmSignalHandlerVM structuredSignalHandlerVM
#endif
#if defined(WIN64)
/* return non-zero if we should defer to a try/except handler */
static UDATA
checkForTryExceptHandler(struct J9PortLibrary* portLibrary, void* userData)
{
PORT_ACCESS_FROM_PORT(portLibrary);
J9CrashData* data = userData;
void* gpInfo = data->gpInfo;
const char* name;
U_32 *value;
U_32 infoKind;
infoKind = j9sig_info(gpInfo, J9PORT_SIG_OTHER, J9PORT_SIG_WINDOWS_DEFER_TRY_EXCEPT_HANDLER, &name, &value);
if (infoKind != J9PORT_SIG_VALUE_32) {
/* the call failed, do not defer to any handlers */
return 0;
}
if ( 0 != *value) {
/* there is a try/except handler */
return 1;
}
return 0;
}
#endif /* WIN64 */
/**
* @internal
*
* The generic VM signal handler.
* Assumes the void* parameter is a J9JavaVM.
*/
UDATA
vmSignalHandlerVM(struct J9PortLibrary* portLibrary, U_32 gpType, void* gpInfo, void* userData)
{
void *handlerParam = NULL;
J9JavaVM *javaVM = userData;
J9VMThread* vmThread = currentVMThread(javaVM);
/* We may not always be executing on a Java thread! */
if (NULL == vmThread) {
handlerParam = javaVM;
} else {
handlerParam = vmThread;
}
return vmSignalHandler(portLibrary, gpType, gpInfo, handlerParam);
}
/**
* @internal
*
* The generic VM signal handler.
*/
UDATA
vmSignalHandler(struct J9PortLibrary* portLibrary, U_32 gpType, void* gpInfo, void* userData)
{
J9VMThread *vmThread = userData;
J9JavaVM *vm = NULL;
J9CrashData crashData;
UDATA result;
PORT_ACCESS_FROM_PORT(portLibrary);
char consoleOutputBuf[3560]; /* Reduced from 4096 so that stack frame fits in one page */
J9RecursiveCrashData recursiveCrashData;
#ifdef J9VM_RAS_EYECATCHERS
J9RASCrashInfo rasCrashInfo;
#endif
/* We no longer have J9VMToken, but we must still use the second slot to
* figure out whether we were passed a VM or a Thread in this case, because the stack swap code
* calls this handler with a thread, but in some cases it may be called with a VM only (e.g. Signal Handler thread).
*/
/* Case 1: It's not actually a thread, but a VM pointer! */
if (((J9JavaVM*) vmThread) == vmThread->javaVM) {
vm = (J9JavaVM*) vmThread;
vmThread = NULL;
/* Case 2: It's a proper VM Thread, get the VM pointer. */
} else {
vm = vmThread->javaVM;
}
memset(&recursiveCrashData, 0, sizeof(J9RecursiveCrashData));
#if defined(J9VM_INTERP_NATIVE_SUPPORT) && !defined(J9ZTPF)
/* give the JIT a chance to recover from the exception */
if (NULL != vmThread) {
J9JITConfig *jitConfig = vm->jitConfig;
if (jitConfig != NULL) {
if (jitConfig->jitSignalHandler != NULL) {
if (jitConfig->jitSignalHandler(vmThread, gpType, gpInfo) == J9PORT_SIG_EXCEPTION_CONTINUE_EXECUTION) {
return J9PORT_SIG_EXCEPTION_CONTINUE_EXECUTION;
}
}
}
}
#endif /* defined(J9VM_INTERP_NATIVE_SUPPORT) && !defined(J9ZTPF) */
#if defined(J9VM_PORT_ZOS_CEEHDLRSUPPORT)
if (J9_SIG_ZOS_CEEHDLR == (vm->sigFlags & J9_SIG_ZOS_CEEHDLR)) {
if (NULL != vmThread) {
UDATA handleJNIRC = 0;
if (J9_SIG_PERCOLATE_CONDITIONS == (vm->sigFlags & J9_SIG_PERCOLATE_CONDITIONS)) {
if (0 != vmThread->percolatedConditionAndTriggeredDiagnostics) {
/* we've already generated RAS diagnostics for this, percolate the condition */
return J9PORT_SIG_EXCEPTION_CONTINUE_SEARCH;
}
/* fall through to generating RAS diagnostics */
} else {
handleJNIRC = handleJNIZOSLECondition(portLibrary, vmThread, gpInfo);
if (K8ZOS_RESUME_EXECUTION == handleJNIRC) {
return J9PORT_SIG_EXCEPTION_CONTINUE_EXECUTION;
} else if (K8ZOS_CONTINUE_SEARCH == handleJNIRC) {
return J9PORT_SIG_EXCEPTION_CONTINUE_SEARCH;
} else {
/* fall through, continue down normal crash path */
}
}
}
}
#endif
#if defined(J9VM_INTERP_USE_UNSAFE_HELPER)
if ((J9PORT_SIG_FLAG_SIGBUS == (gpType & J9PORT_SIG_FLAG_SIGALLSYNC))
#if defined(J9ZOS390)
|| (J9PORT_SIG_FLAG_SIGABEND == (gpType & J9PORT_SIG_FLAG_SIGALLSYNC))
#endif /* defined(J9ZOS390) */
) {
J9VMThread *currentThread = ((NULL == vmThread) ? vm->internalVMFunctions->currentVMThread(vm): vmThread);
if (NULL != currentThread) {
if (J9_ARE_ALL_BITS_SET(currentThread->privateFlags2, J9_PRIVATE_FLAGS2_UNSAFE_HANDLE_SIGBUS)) {
if (J9PORT_SIG_EXCEPTION_CONTINUE_EXECUTION == sigBusHandler(currentThread, gpInfo)) {
return J9PORT_SIG_EXCEPTION_CONTINUE_EXECUTION;
}
}
}
}
#endif /* defined(J9VM_INTERP_USE_UNSAFE_HELPER) */
#if defined(J9ZOS390)
if (J9PORT_SIG_FLAG_SIGABEND == (gpType & J9PORT_SIG_FLAG_SIGALLSYNC)) {
/* percolate the condition */
return J9PORT_SIG_EXCEPTION_CONTINUE_SEARCH;
}
#endif /* defined(J9ZOS390) */
crashData.javaVM = vm;
crashData.vmThread = vmThread;
crashData.gpType = gpType;
crashData.gpInfo = gpInfo;
crashData.consoleOutputBuf = consoleOutputBuf;
crashData.sizeofConsoleOutputBuf = sizeof(consoleOutputBuf);
#if defined(WIN64)
result = 0;
recursiveCrashData.protectedFunctionName = "checkForTryExceptHandler";
j9sig_protect(
checkForTryExceptHandler, &crashData,
recursiveCrashHandler, &recursiveCrashData,
J9PORT_SIG_FLAG_MAY_RETURN | J9PORT_SIG_FLAG_SIGALLSYNC,
&result);
if (0 != result) {
return J9PORT_SIG_EXCEPTION_CONTINUE_SEARCH;
}
#endif
#ifdef J9VM_RAS_EYECATCHERS
crashData.rasCrashInfo = &rasCrashInfo;
recursiveCrashData.protectedFunctionName = "setupRasCrashInfo";
j9sig_protect(
setupRasCrashInfo, &crashData,
recursiveCrashHandler, &recursiveCrashData,
J9PORT_SIG_FLAG_MAY_RETURN | J9PORT_SIG_FLAG_SIGALLSYNC,
&result);
#endif
recursiveCrashData.protectedFunctionName = "writeCrashDataToConsole";
j9sig_protect(
writeCrashDataToConsole, &crashData,
recursiveCrashHandler, &recursiveCrashData,
J9PORT_SIG_FLAG_MAY_RETURN | J9PORT_SIG_FLAG_SIGALLSYNC,
&result);
recursiveCrashData.protectedFunctionName = "generateDiagnosticFiles";
j9sig_protect(
generateDiagnosticFiles, &crashData,
recursiveCrashHandler, &recursiveCrashData,
J9PORT_SIG_FLAG_MAY_RETURN | J9PORT_SIG_FLAG_SIGALLSYNC,
&result);
recursiveCrashData.protectedFunctionName = "reportThreadCrash";
j9sig_protect(
reportThreadCrash, &crashData,
recursiveCrashHandler, &recursiveCrashData,
J9PORT_SIG_FLAG_MAY_RETURN | J9PORT_SIG_FLAG_SIGALLSYNC,
&result);
recursiveCrashData.protectedFunctionName = "executeAbortHook";
j9sig_protect(
executeAbortHook, &crashData,
recursiveCrashHandler, &recursiveCrashData,
J9PORT_SIG_FLAG_MAY_RETURN | J9PORT_SIG_FLAG_SIGALLSYNC,
&result);
#if defined(J9VM_PORT_ZOS_CEEHDLRSUPPORT)
if (J9PORT_SIG_OPTIONS_ZOS_USE_CEEHDLR == (J9PORT_SIG_OPTIONS_ZOS_USE_CEEHDLR & j9sig_get_options())) {
if (J9_SIG_PERCOLATE_CONDITIONS == (vm->sigFlags & J9_SIG_PERCOLATE_CONDITIONS)) {
/* record that we already triggered diagnostics, so that any le condition handlers further down the call chain do not */
vmThread->percolatedConditionAndTriggeredDiagnostics = 1;
vmThread->privateFlags |= J9_PRIVATE_FLAGS_STACKS_OUT_OF_SYNC;
return J9PORT_SIG_EXCEPTION_CONTINUE_SEARCH;
} else {
_INT4 code = PORT_ABEND_CODE;
_INT4 reason = 0; /* arbitrarily chosen reason code*/
_INT4 cleanup = 0; /* 0 - issue the abend without clean-up */
CEE3AB2(&code, &reason, &cleanup);
}
}
#endif
#if defined(J9ZOS390)
if (J9_SIG_POSIX_COOPERATIVE_SHUTDOWN == (J9_SIG_POSIX_COOPERATIVE_SHUTDOWN & vm->sigFlags)) {
return J9PORT_SIG_EXCEPTION_COOPERATIVE_SHUTDOWN;
}
#endif
j9exit_shutdown_and_exit(-1);
/* even if we don't have signal support, we still need a minimal handler so that we can pass it in to j9sig_protect */
return J9PORT_SIG_EXCEPTION_CONTINUE_SEARCH;
}
static void
generateSystemDump(struct J9PortLibrary* portLibrary, void* gpInfo)
{
char dumpName[EsMaxPath];
UDATA dumpCreateReturnCode;
IDATA getEnvForDumpReturnCode;
PORT_ACCESS_FROM_PORT(portLibrary);
getEnvForDumpReturnCode = j9sysinfo_get_env(J9DO_NOT_WRITE_DUMP, NULL, 0);
if (getEnvForDumpReturnCode != 0) {
/* failed to find the env var, so write the dump */
*dumpName = '\0';
#ifdef LINUX
/* Ensure that userdata argument is NULL on Linux. */
dumpCreateReturnCode = j9dump_create(dumpName, NULL, NULL);
#else
dumpCreateReturnCode = j9dump_create(dumpName, NULL, gpInfo);
#endif
if (dumpCreateReturnCode == 0) {
j9tty_printf(PORTLIB, "\nGenerated system dump: %s\n", dumpName);
} else {
/* failed, error message in dumpName */
j9tty_err_printf(PORTLIB, "\nError: %s\n", dumpName);
}
} else {
j9tty_err_printf(PORTLIB, "\nSystem dump not written since \""J9DO_NOT_WRITE_DUMP"\" was defined in the environment\n");
}
}
static const char*
getSignalDescription(struct J9PortLibrary* portLibrary, U_32 gpType)
{
switch (gpType & J9PORT_SIG_FLAG_SIGALLSYNC) {
case J9PORT_SIG_FLAG_SIGSEGV:
return "Segmentation error";
case J9PORT_SIG_FLAG_SIGBUS:
return "Bus error";
case J9PORT_SIG_FLAG_SIGILL:
return "Illegal instruction";
case J9PORT_SIG_FLAG_SIGFPE:
return "Floating point error";
case J9PORT_SIG_FLAG_SIGTRAP:
return "Unhandled trap";
#if defined(J9ZOS390)
case J9PORT_SIG_FLAG_SIGABEND:
return "Abend error";
#endif /* defined(J9ZOS390) */
default:
return "Unknown error";
}
}
static UDATA
writeGPInfo(struct J9PortLibrary* portLibrary, void *writeGPInfoCrashData)
{
J9WriteGPInfoData *crashData = writeGPInfoCrashData;
PORT_ACCESS_FROM_PORT(portLibrary);
char *cursor = crashData->s;
UDATA length = crashData->length;
void *gpInfo = crashData->gpInfo;
U_32 category = crashData->category;
UDATA numBytesWritten = 0;
U_32 info;
U_32 infoCount;
UDATA n;
infoCount = j9sig_info_count(gpInfo, category);
for (info = 0; info < infoCount; info++) {
char c;
U_32 infoKind;
void *value;
const char* name;
infoKind = j9sig_info(gpInfo, category, info, &name, &value);
if (((info%INFO_COLS)==INFO_COLS-1) || (info==infoCount-1) || (infoKind == J9PORT_SIG_VALUE_STRING) || (infoKind == J9PORT_SIG_VALUE_FLOAT_64)) {
c = '\n';
} else {
c = ' ';
}
switch (infoKind) {
case J9PORT_SIG_VALUE_16:
n = j9str_printf(PORTLIB, cursor, length, "%s=%04X%c", name, *(U_16 *)value, c);
break;
case J9PORT_SIG_VALUE_32:
n = j9str_printf(PORTLIB, cursor , length, "%s=%08.8x%c", name, *(U_32 *)value, c);
break;
case J9PORT_SIG_VALUE_64:
n = j9str_printf(PORTLIB, cursor, length,"%s=%016.16llx%c", name, *(U_64 *)value, c);
break;
case J9PORT_SIG_VALUE_128:
{
const U_128 * const v = (U_128 *) value;
const U_64 h = v->high64;
const U_64 l = v->low64;
n = j9str_printf(PORTLIB, cursor, length,"%s=%016.16llx%016.16llx%c", name, h, l, c);
}
break;
case J9PORT_SIG_VALUE_STRING:
n = j9str_printf(PORTLIB, cursor, length, "%s=%s%c", name, (char *)value, c);