-
Notifications
You must be signed in to change notification settings - Fork 723
/
jvminit.c
8752 lines (7640 loc) · 309 KB
/
jvminit.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 IBM Corp. and others 1991
*
* 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] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#if defined(WIN32)
#include <windows.h>
#define USER32_DLL "user32.dll"
#include <WinSDKVer.h>
#if defined(_WIN32_WINNT_WINBLUE) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WINBLUE)
#include <VersionHelpers.h>
#endif
#endif /* defined(WIN32) */
/* _GNU_SOURCE forces GLIBC_2.0 sscanf/vsscanf/fscanf for RHEL5 compatibility */
#if defined(LINUX) && !defined(J9ZTPF)
#define _GNU_SOURCE
#endif /* defined(LINUX) && !defined(J9ZTPF) */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <limits.h>
#include "util_api.h"
#if JAVA_SPEC_VERSION >= 16
#include "vm_internal.h"
#endif /* JAVA_SPEC_VERSION >= 16 */
#if !defined(stdout)
#define stdout NULL
#endif
#if !defined(WIN32)
/* Needed for JCL dependency on JVM to set SIGPIPE to SIG_IGN */
#include <signal.h>
#endif
#if defined(J9ZOS390)
#include "atoe.h"
#endif
#include "omrcfg.h"
#include "jvminitcommon.h"
#include "j9user.h"
#include "j9.h"
#include "omr.h"
#include "j9protos.h"
#include "jni.h"
#include "j9port.h"
#if defined(J9VM_OPT_SNAPSHOTS)
#include "j9port_generated.h"
#endif /* defined(J9VM_OPT_SNAPSHOTS) */
#include "omrthread.h"
#include "j9consts.h"
#include "j9dump.h"
#include "jvminit.h"
#include "vm_api.h"
#include "vmaccess.h"
#include "vmhook_internal.h"
#include "mmhook.h"
#include "portsock.h"
#include "vmi.h"
#include "vm_internal.h"
#include "javaPriority.h"
#include "thread_api.h"
#include "jvmstackusage.h"
#include "omrsig.h"
#include "bcnames.h"
#include "jimagereader.h"
#include "vendor_version.h"
#include "omrlinkedlist.h"
#ifdef J9VM_OPT_ZIP_SUPPORT
#include "zip_api.h"
#endif
#define _UTE_STATIC_
#include "ut_j9vm.h"
#ifdef J9OS_I5
#include "Xj9I5OSDebug.H"
#endif
#ifdef J9ZTPF
#include <tpf/cujvm.h> /* for z/TPF OS VM exit hook: cjvm_jvm_shutdown_hook() */
#endif
/*#define JVMINIT_UNIT_TEST*/
#include "j2sever.h"
#include "locknursery.h"
#include "vmargs_api.h"
#include "rommeth.h"
#if defined(J9VM_OPT_SHARED_CLASSES)
#include "SCAbstractAPI.h"
#endif
#if JAVA_SPEC_VERSION >= 19
#include "omrutil.h"
#endif /* JAVA_SPEC_VERSION >= 19 */
#if defined(AIXPPC) && !defined(J9OS_I5)
#include <sys/systemcfg.h> /* for isPPC64bit() */
#endif /* AIXPPC && !J9OS_I5 */
J9_EXTERN_BUILDER_SYMBOL(cInterpreter);
/* Generic rounding macro - result is a UDATA */
#define ROUND_TO(granularity, number) (((UDATA)(number) + (granularity) - 1) & ~((UDATA)(granularity) - 1))
extern vmiError J9VMI_Initialize(J9JavaVM* vm);
#if (defined(J9VM_OPT_SIDECAR))
void sidecarInit (J9VMThread *mainThread);
#endif
typedef void (JNICALL * J9_EXIT_HANDLER_PROC) (jint);
typedef void (JNICALL * J9_ABORT_HANDLER_PROC) (void);
struct J9VMIgnoredOption {
char *optionName;
IDATA match;
};
typedef struct {
void * vm_args;
void * osMainThread;
J9JavaVM * vm;
J9JavaVM** globalJavaVM;
UDATA j2seVersion;
char* j2seRootDirectory;
char* j9libvmDirectory;
} J9InitializeJavaVMArgs;
#define IGNORE_ME_STRING "_ignore_me"
#define SILENT_EXIT_STRING "_silent_exit"
#define OPT_NONE "none"
#define OPT_NONE_CAPS "NONE"
#define DJCOPT_JITC "jitc"
#define OPT_TRUE "true"
#define XRUN_LEN (sizeof(VMOPT_XRUN)-1)
#define DLLNAME_LEN 32 /* this value should be consistent with that in J9VMDllLoadInfo */
#define SMALL_STRING_BUF_SIZE 64
#define MED_STRING_BUF_SIZE 128
#define LARGE_STRING_BUF_SIZE 256
#define FUNCTION_VM_INIT "VMInitStages"
#define FUNCTION_THREAD_INIT "threadInitStages"
#define FUNCTION_ZERO_INIT "zeroInitStages"
static const struct J9VMIgnoredOption ignoredOptionTable[] = {
{ IGNORE_ME_STRING, EXACT_MATCH },
{ VMOPT_XDEBUG, EXACT_MATCH },
{ VMOPT_XNOAGENT, EXACT_MATCH },
{ VMOPT_XINCGC, EXACT_MATCH }, /* JNLP param */
{ VMOPT_XMIXED, EXACT_MATCH }, /* JNLP param */
{ VMOPT_XPROF, EXACT_MATCH }, /* JNLP param */
{ VMOPT_XBATCH, EXACT_MATCH }, /* JNLP param */
{ VMOPT_PORT_LIBRARY, EXACT_MATCH },
{ VMOPT_BFU_JAVA, EXACT_MATCH },
{ VMOPT_BP_JXE, EXACT_MATCH },
{ VMOPT_NEEDS_JCL, EXACT_MATCH },
{ VMOPT_VFPRINTF, EXACT_MATCH },
{ VMOPT_EXIT, EXACT_MATCH },
{ VMOPT_ABORT, EXACT_MATCH },
{ VMOPT_XNOQUICKSTART, EXACT_MATCH },
{ VMOPT_XJ9, EXACT_MATCH },
{ VMOPT_XMXCL, STARTSWITH_MATCH },
/* extra-extended options start with -XX. Ignore any not explicitly processed. */
#if defined(J9VM_OPT_SIDECAR)
{ VMOPT_XJVM, STARTSWITH_MATCH },
{ VMOPT_SERVER, EXACT_MATCH },
{ VMOPT_X142BOOSTGCTHRPRIO, EXACT_MATCH },
{ MAPOPT_XSIGCATCH, EXACT_MATCH },
{ MAPOPT_XNOSIGCATCH, EXACT_MATCH },
{ MAPOPT_XINITACSH, EXACT_MEMORY_MATCH },
{ MAPOPT_XINITSH, EXACT_MEMORY_MATCH },
{ MAPOPT_XINITTH, EXACT_MEMORY_MATCH },
{ MAPOPT_XK, EXACT_MEMORY_MATCH },
{ MAPOPT_XP, EXACT_MEMORY_MATCH },
{ VMOPT_XNORTSJ, EXACT_MATCH },
#if !(defined(OMR_GC_COMPRESSED_POINTERS) && defined(OMR_GC_FULL_POINTERS))
{ VMOPT_XCOMPRESSEDREFS, EXACT_MATCH },
{ VMOPT_XNOCOMPRESSEDREFS, EXACT_MATCH },
#endif /* !(defined(OMR_GC_COMPRESSED_POINTERS) && defined(OMR_GC_FULL_POINTERS)) */
#endif /* defined(J9VM_OPT_SIDECAR) */
};
#define ignoredOptionTableSize (sizeof(ignoredOptionTable) / sizeof(struct J9VMIgnoredOption))
IDATA VMInitStages (J9JavaVM *vm, IDATA stage, void* reserved);
IDATA registerCmdLineMapping (J9JavaVM* vm, char* sov_option, char* j9_option, UDATA mapFlags);
IDATA postInitLoadJ9DLL (J9JavaVM* vm, const char* dllName, void* argData);
void freeJavaVM (J9JavaVM * vm);
IDATA threadInitStages (J9JavaVM* vm, IDATA stage, void* reserved);
IDATA zeroInitStages (J9JavaVM* vm, IDATA stage, void* reserved);
UDATA runJVMOnLoad (J9JavaVM* vm, J9VMDllLoadInfo* loadInfo, char* options);
static void consumeVMArgs (J9JavaVM* vm, J9VMInitArgs* j9vm_args);
static BOOLEAN isEmpty (const char * str);
#if (defined(J9VM_OPT_SIDECAR))
static UDATA initializeJVMExtensionInterface (J9JavaVM* vm);
#endif /* J9VM_OPT_SIDECAR */
static UDATA initializeVprintfHook (J9JavaVM* vm);
#if (defined(J9VM_INTERP_VERBOSE))
static const char* getNameForLoadStage (IDATA stage);
#endif /* J9VM_INTERP_VERBOSE */
static jint runInitializationStage (J9JavaVM* vm, IDATA stage);
static IDATA setSignalOptions(J9JavaVM *vm, J9PortLibrary *portLibrary);
#if (defined(J9VM_OPT_SIDECAR))
void sidecarExit(J9VMThread* shutdownThread);
#endif /* J9VM_OPT_SIDECAR */
static jint runLoadStage (J9JavaVM *vm, IDATA flags);
#if defined(J9VM_GC_DYNAMIC_CLASS_UNLOADING)
static void freeClassNativeMemory (J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData);
static void vmHookAnonClassesUnload(J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData);
#endif /* GC_DYNAMIC_CLASS_UNLOADING */
static jint runShutdownStage (J9JavaVM* vm, IDATA stage, void* reserved, UDATA filterFlags);
static jint modifyDllLoadTable (J9JavaVM * vm, J9Pool* loadTable, J9VMInitArgs* j9vm_args);
static jint processVMArgsFromFirstToLast(J9JavaVM * vm);
static void processCompressionOptions(J9JavaVM *vm);
static IDATA setMemoryOptionToOptElse (J9JavaVM* vm, UDATA* thingToSet, char* optionName, UDATA defaultValue, UDATA doConsumeArg);
static IDATA setIntegerValueOptionToOptElse (J9JavaVM* vm, UDATA* thingToSet, char* optionName, UDATA defaultValue, UDATA doConsumeArg);
#ifdef JVMINIT_UNIT_TEST
static void testFindArgs (J9JavaVM* vm);
static void testOptionValueOps (J9JavaVM* vm);
#endif
static jint initializeXruns (J9JavaVM* vm);
static void unloadDLL (void* dllLoadInfo, void* userDataTemp);
#if (defined(J9VM_PROF_COUNT_ARGS_TEMPS))
static void report (J9JavaVM * vm);
#endif /* J9VM_PROF_COUNT_ARGS_TEMPS */
#if (defined(J9VM_OPT_SIDECAR))
static IDATA createMapping (J9JavaVM* vm, char* j9Name, char* mapName, UDATA flags, IDATA atIndex);
#endif /* J9VM_OPT_SIDECAR */
#if (defined(J9VM_OPT_JVMTI))
static void detectAgentXruns (J9JavaVM* vm);
#endif /* J9VM_OPT_JVMTI */
static void vfprintfHook (struct OMRPortLibrary *portLib, const char *format, ...);
static IDATA vfprintfHook_file_write_text(struct OMRPortLibrary *portLibrary, IDATA fd, const char *buf, IDATA nbytes);
static void runJ9VMDllMain (void* dllLoadInfo, void* userDataTemp);
static jint checkPostStage (J9JavaVM* vm, IDATA stage);
static void runUnOnloads (J9JavaVM* vm, UDATA shutdownDueToExit);
static void generateMemoryOptionParseError (J9JavaVM* vm, J9VMDllLoadInfo* loadInfo, UDATA errorType, char* optionWithError);
static void loadDLL (void* dllLoadInfo, void* userDataTemp);
static void registerIgnoredOptions (J9PortLibrary *portLibrary, J9VMInitArgs* j9vm_args);
static UDATA protectedInitializeJavaVM (J9PortLibrary* portLibrary, void * userData);
static J9Pool *initializeDllLoadTable (J9PortLibrary *portLibrary, J9VMInitArgs* j9vm_args, UDATA verboseFlags, J9JavaVM *vm);
#if defined(J9VM_OPT_SIDECAR) && (JAVA_SPEC_VERSION < 21)
static IDATA checkDjavacompiler (J9PortLibrary *portLibrary, J9VMInitArgs* j9vm_args);
#endif /* defined(J9VM_OPT_SIDECAR) && (JAVA_SPEC_VERSION < 21) */
static void* getOptionExtraInfo (J9PortLibrary *portLibrary, J9VMInitArgs* j9vm_args, IDATA match, char* optionName);
static void closeAllDLLs (J9JavaVM* vm);
#if (defined(J9VM_INTERP_VERBOSE))
static const char* getNameForStage (IDATA stage);
#endif /* J9VM_INTERP_VERBOSE */
#if (defined(J9VM_OPT_SIDECAR))
static IDATA registerVMCmdLineMappings (J9JavaVM* vm);
#endif /* J9VM_OPT_SIDECAR */
static void checkDllInfo (void* dllLoadInfo, void* userDataTemp);
static UDATA initializeVTableScratch(J9JavaVM* vm);
static jint initializeDDR (J9JavaVM * vm);
static void setThreadNameAsyncHandler(J9VMThread *currentThread, IDATA handlerKey, void *userData);
#if defined(J9VM_INTERP_CUSTOM_SPIN_OPTIONS)
static void cleanCustomSpinOptions(void *element, void *userData);
#endif /* J9VM_INTERP_CUSTOM_SPIN_OPTIONS */
/* Imports from vm/rasdump.c */
extern void J9RASInitialize (J9JavaVM* javaVM);
extern void J9RelocateRASData (J9JavaVM* javaVM);
extern void J9RASShutdown (J9JavaVM* javaVM);
extern void populateRASNetData (J9JavaVM *javaVM, J9RAS *rasStruct);
const U_8 J9CallInReturnPC[] = { 0xFF, 0x00, 0x00, 0xFF }; /* impdep2, parm, parm, impdep2 */
#if defined(J9VM_OPT_METHOD_HANDLE)
const U_8 J9Impdep1PC[] = { 0xFE, 0x00, 0x00, 0xFE }; /* impdep1, parm, parm, impdep1 */
#endif /* defined(J9VM_OPT_METHOD_HANDLE) */
static jint (JNICALL * vprintfHookFunction)(FILE *fp, const char *format, va_list args) = NULL;
static IDATA (* portLibrary_file_write_text) (struct OMRPortLibrary *portLibrary, IDATA fd, const char *buf, IDATA nbytes) = NULL;
#if !defined(WIN32)
static UDATA sigxfszHandler(struct J9PortLibrary* portLibrary, U_32 gpType, void* gpInfo, void* userData);
#endif /* !defined(WIN32) */
/* SSE2 support on 32 bit linux_x86 and win_x86 */
#ifdef J9VM_ENV_SSE2_SUPPORT_DETECTION
extern U_32 J9SSE2cpuidFeatures(void);
static BOOLEAN isSSE2SupportedOnX86();
#endif /* J9VM_ENV_SSE2_SUPPORT_DETECTION */
#if (defined(AIXPPC) || defined(LINUXPPC)) && !defined(J9OS_I5)
static BOOLEAN isPPC64bit(void);
#endif /* (AIXPPC || LINUXPPC) & !J9OS_I5 */
static UDATA predefinedHandlerWrapper(struct J9PortLibrary *portLibrary, U_32 gpType, void *gpInfo, void *userData);
static void signalDispatch(J9VMThread *vmThread, I_32 sigNum);
static UDATA parseGlrConfig(J9JavaVM* jvm, char* options);
static UDATA parseGlrOption(J9JavaVM* jvm, char* option);
J9_DECLARE_CONSTANT_UTF8(j9_int_void, "(I)V");
J9_DECLARE_CONSTANT_UTF8(j9_dispatch, "dispatch");
/* The appropriate bytecodeLoop is selected based on interpreter mode */
#if defined(OMR_GC_FULL_POINTERS)
UDATA bytecodeLoopFull(J9VMThread *currentThread);
UDATA debugBytecodeLoopFull(J9VMThread *currentThread);
#endif /* defined(OMR_GC_FULL_POINTERS) */
#if defined(OMR_GC_COMPRESSED_POINTERS)
UDATA bytecodeLoopCompressed(J9VMThread *currentThread);
UDATA debugBytecodeLoopCompressed(J9VMThread *currentThread);
#endif /* defined(OMR_GC_COMPRESSED_POINTERS) */
#if (defined(OMR_GC_COMPRESSED_POINTERS) && defined(OMR_GC_FULL_POINTERS)) || (defined(LINUX) && defined(J9VM_GC_REALTIME))
static BOOLEAN isGCPolicyMetronome(J9JavaVM *javaVM);
#endif /* (defined(OMR_GC_COMPRESSED_POINTERS) && defined(OMR_GC_FULL_POINTERS)) || (defined(LINUX) && defined(J9VM_GC_REALTIME)) */
#if defined(COUNT_BYTECODE_PAIRS)
static jint
initializeBytecodePairs(J9JavaVM *vm)
{
PORT_ACCESS_FROM_JAVAVM(vm);
jint rc = JNI_ENOMEM;
UDATA allocSize = sizeof(UDATA) * 256 * 256;
UDATA *matrix = j9mem_allocate_memory(allocSize, OMRMEM_CATEGORY_VM);
if (NULL != matrix) {
memset(matrix, 0, allocSize);
vm->debugField1 = (UDATA)matrix;
rc = JNI_OK;
}
return rc;
}
void
printBytecodePairs(J9JavaVM *vm)
{
UDATA *matrix = (UDATA*)vm->debugField1;
if (NULL != matrix) {
PORT_ACCESS_FROM_JAVAVM(vm);
UDATA i = 0;
UDATA j = 0;
for (i = 0; i < 256; ++i) {
for (j = 0; j < 256; ++j) {
UDATA count = matrix[(i * 256) + j];
if (0 != count) {
j9tty_printf(PORTLIB, "%09zu %s->%s\n", count, JavaBCNames[i], JavaBCNames[j]);
}
}
}
}
}
static void
freeBytecodePairs(J9JavaVM *vm)
{
PORT_ACCESS_FROM_JAVAVM(vm);
UDATA *matrix = (UDATA*)vm->debugField1;
printBytecodePairs(vm);
vm->debugField1 = 0;
j9mem_free_memory(matrix);
}
#endif /* COUNT_BYTECODE_PAIRS */
static void
print_verbose_stackusage_of_nonsystem_threads(J9VMThread* vmThread)
{
J9VMThread * currentThread;
J9JavaVM * vm = vmThread->javaVM;
if ((vm->runtimeFlags & J9_RUNTIME_REPORT_STACK_USE) && vmThread->stackObject && (vm->verboseLevel & VERBOSE_STACK)) {
/* Failing to check for NULL leads to a endless spin.*/
if((NULL == vm->vmThreadListMutex) || omrthread_monitor_try_enter(vm->vmThreadListMutex)) {
/*vmThreadListMutex is null. Nothing to get lock on.*/
/*If omrthread_monitor_try_enter returns true, it failed to get the lock. If it succeeds to enter, then it returns 0*/
PORT_ACCESS_FROM_JAVAVM(vm);
j9nls_printf(PORTLIB, J9NLS_INFO, J9NLS_VERB_STACK_USAGE_FOR_RUNNING_THREADS_FAILURE_1);
}else{
/*got the lock*/
currentThread = vmThread->linkNext;
while (currentThread != vmThread) {
J9VMThread * nextThread = currentThread->linkNext;
if (currentThread->privateFlags & J9_PRIVATE_FLAGS_SYSTEM_THREAD ) {
/*DO NOTHING. These threads will be handled automatically by system.exit()*/
} else {
/*
* Print stack usage info for running non-system threads.
* When system.exit() is called, it prints stack usage info for system thread and a thread that calls system.exit()
*/
print_verbose_stackUsage(currentThread, TRUE);
}
currentThread = nextThread;
}
/*Release the lock*/
omrthread_monitor_exit(vm->vmThreadListMutex);
}
}
}
void
print_verbose_stackUsage(J9VMThread* vmThread, UDATA stillRunning)
{
UDATA *stackSlot = J9_LOWEST_STACK_SLOT(vmThread);
UDATA nbyteUsed = (vmThread->stackObject->end - stackSlot) * sizeof(UDATA);
UDATA cbyteUsed = omrthread_get_stack_usage(vmThread->osThread);
J9JavaVM * vm = vmThread->javaVM;
while (*stackSlot == J9_RUNTIME_STACK_FILL) {
stackSlot++;
nbyteUsed -= sizeof(UDATA);
}
if (vmThread->threadObject) {
char* name = getOMRVMThreadName(vmThread->omrVMThread);
PORT_ACCESS_FROM_JAVAVM(vm);
if(stillRunning == FALSE){
/* J9NLS_VERB_STACK_USAGE=Verbose stack: \"%.*s\" used %zd/%zd bytes on Java/C stacks */
j9nls_printf(PORTLIB, J9NLS_INFO, J9NLS_VERB_STACK_USAGE, strlen(name), name, nbyteUsed, cbyteUsed);
}else{ /*if stillRunning == TRUE*/
/* J9NLS_VERB_STACK_USAGE_FOR_RUNNING_THREADS=Verbose stack: Running \"%2$.*1$s\" is using %3$zd/%4$zd bytes on Java/C stacks*/
j9nls_printf(PORTLIB, J9NLS_INFO, J9NLS_VERB_STACK_USAGE_FOR_RUNNING_THREADS, strlen(name), name, nbyteUsed, cbyteUsed);
}
releaseOMRVMThreadName(vmThread->omrVMThread);
}
if (nbyteUsed > vm->maxStackUse) vm->maxStackUse = nbyteUsed;
if (cbyteUsed > vm->maxCStackUse) vm->maxCStackUse = cbyteUsed;
}
/**
* @internal
*
* Detect -Xipt and prevent enablement of iconv converter initialization
* in portlibrary.
*
* @return 0 if success, 1 otherwise
*/
static UDATA
setGlobalConvertersAware(J9JavaVM *vm)
{
UDATA rc = 0;
if ((FIND_AND_CONSUME_VMARG(EXACT_MATCH, VMOPT_XIPT, NULL)) >= 0) {
/* -Xipt detected. Prevent enablement of cached UTF8 converters. */
rc = 0;
}
#if defined(J9VM_USE_ICONV) || defined(J9ZOS390)
{
PORT_ACCESS_FROM_JAVAVM(vm);
rc = j9port_control(J9PORT_CTLDATA_NOIPT, 1);
}
#endif
return rc;
}
void OMRNORETURN
exitJavaVM(J9VMThread * vmThread, IDATA rc)
{
J9JavaVM *vm = NULL;
/* if no VM is specified, just try to exit from the first VM.
* Arguably, we should shutdown ALL the VMs.
*/
if (vmThread == NULL) {
jint nVMs = 0;
if (JNI_OK == J9_GetCreatedJavaVMs((JavaVM **)&vm, 1, &nVMs)) {
if (nVMs == 1) {
vmThread = currentVMThread(vm);
}
}
} else {
vm = vmThread->javaVM;
if ((vm->runtimeFlags & J9_RUNTIME_REPORT_STACK_USE) && vmThread->stackObject && (vm->verboseLevel & VERBOSE_STACK)) {
print_verbose_stackusage_of_nonsystem_threads(vmThread);
print_verbose_stackUsage(vmThread, FALSE);
}
}
if (vm != NULL) {
PORT_ACCESS_FROM_JAVAVM(vm);
#if defined(J9VM_INTERP_ATOMIC_FREE_JNI)
/* exitJavaVM is always called from a JNI context */
enterVMFromJNI(vmThread);
releaseVMAccess(vmThread);
#endif /* J9VM_INTERP_ATOMIC_FREE_JNI */
/* we only let the shutdown code run once */
if(vm->runtimeFlagsMutex != NULL) {
omrthread_monitor_enter(vm->runtimeFlagsMutex);
}
if(vm->runtimeFlags & J9_RUNTIME_EXIT_STARTED) {
if (vm->runtimeFlagsMutex != NULL) {
omrthread_monitor_exit(vm->runtimeFlagsMutex);
}
if (vmThread->publicFlags & J9_PUBLIC_FLAGS_VM_ACCESS) {
internalReleaseVMAccess(vmThread);
}
/* Do nothing. Wait for the process to exit. */
while (1) {
omrthread_suspend();
}
}
vm->runtimeFlags |= J9_RUNTIME_EXIT_STARTED;
if(vm->runtimeFlagsMutex != NULL) {
omrthread_monitor_exit(vm->runtimeFlagsMutex);
}
#ifdef J9VM_OPT_SIDECAR
if (vm->sidecarExitHook)
(*(vm->sidecarExitHook))(vm);
#endif
#ifdef J9VM_PROF_COUNT_ARGS_TEMPS
report(vm);
#endif
if (vmThread) {
/* we can only perform these shutdown steps if the current thread is attached */
TRIGGER_J9HOOK_VM_SHUTTING_DOWN(vm->hookInterface, vmThread, rc);
}
/* exitJavaVM runs special exit stage, but doesn't close the libraries or deallocate the VM thread */
runExitStages(vm, vmThread);
/* Acquire exclusive VM access to bring all threads to a safe
* point before shutting down. This prevents intermittent crashes (particularly
* in the GC) when attempting to access the entryLocalStorage on the native
* stack of a thread that the OS is in the process of destroying.
*
* This is safe from deadlock with the exclusive access in DestroyJavaVM because
* J9_RUNTIME_EXIT_STARTED is set in DestroyJavaVM before acquiring exclusive
* access. This function has already checked that J9_RUNTIME_EXIT_STARTED was not
* set before reaching this point.
*
* In rare cases, the VM may fatal exit while holding exclusive access, so don't attempt to
* acquire it if another thread already has it.
*/
if (J9_XACCESS_NONE == vm->exclusiveAccessState) {
internalAcquireVMAccess(vmThread);
acquireExclusiveVMAccess(vmThread);
}
#if defined(WIN32)
/* Do not attempt to exit while a JNI shared library open is in progress */
omrthread_monitor_enter(vm->classLoaderBlocksMutex);
#endif
#if defined(J9VM_OPT_SNAPSHOTS)
if (IS_SNAPSHOT_RUN(vm)) {
teardownVMSnapshotImpl(vm);
}
#endif /* defined(J9VM_OPT_SNAPSHOTS) */
#if defined(COUNT_BYTECODE_PAIRS)
printBytecodePairs(vm);
#endif /* COUNT_BYTECODE_PAIRS */
#ifdef J9ZTPF
/* run z/TPF OS exit hook */
cjvm_jvm_shutdown_hook();
#endif
if (vm->exitHook) {
#if defined(J9VM_ZOS_3164_INTEROPERABILITY)
if (J9_IS_31BIT_INTEROP_TARGET(vm->exitHook)) {
execute31BitExitHook(vm, (jint) rc);
} else
#endif /* defined(J9VM_ZOS_3164_INTEROPERABILITY) */
{
vm->exitHook((jint) rc);
}
}
j9exit_shutdown_and_exit((I_32) rc);
}
/* If we got here, then either we couldn't find a VM(!) or j9exit_shutdown_and_exit() returned(!)
* Neither of those scenarios should be possible
* But if somehow it does happen, we don't have many options here!
* We can't even print a message, since we don't have a port library.
*/
exit( (int)rc );
dontreturn: goto dontreturn; /* avoid warnings */
}
#if defined(J9VM_INTERP_CUSTOM_SPIN_OPTIONS)
static void
cleanCustomSpinOptions(void *element, void *userData)
{
J9PortLibrary *portLibrary = (J9PortLibrary *)userData;
PORT_ACCESS_FROM_PORT(portLibrary);
J9VMCustomSpinOptions *option = (J9VMCustomSpinOptions *)element;
j9mem_free_memory((char *)option->className);
}
#endif /* J9VM_INTERP_CUSTOM_SPIN_OPTIONS */
#if defined(J9VM_OPT_JITSERVER)
BOOLEAN
isJITServerEnabled(J9JavaVM *vm)
{
return J9_ARE_ALL_BITS_SET(vm->extendedRuntimeFlags2, J9_EXTENDED_RUNTIME2_ENABLE_START_JITSERVER);
}
#endif /* J9VM_OPT_JITSERVER */
void
freeJavaVM(J9JavaVM * vm)
{
BOOLEAN hotReferenceFieldRequired = FALSE;
J9PortLibrary *tmpLib = NULL;
PORT_ACCESS_FROM_JAVAVM(vm);
J9VMThread *currentThread = currentVMThread(vm);
IDATA traceDescriptor = 0;
#if !defined(WIN32)
j9sig_set_async_signal_handler(sigxfszHandler, NULL, 0);
#endif /* !defined(WIN32) */
#if JAVA_SPEC_VERSION >= 16
if (NULL != vm->cifNativeCalloutDataCache) {
pool_state poolState;
void *cifNode = pool_startDo(vm->cifNativeCalloutDataCache, &poolState);
while (NULL != cifNode) {
freeAllStructFFITypes(currentThread, cifNode);
cifNode = pool_nextDo(&poolState);
}
pool_kill(vm->cifNativeCalloutDataCache);
vm->cifNativeCalloutDataCache = NULL;
}
if (NULL != vm->cifArgumentTypesCache) {
pool_state poolState;
J9CifArgumentTypes *cifArgTypesNode = pool_startDo(vm->cifArgumentTypesCache, &poolState);
while (NULL != cifArgTypesNode) {
j9mem_free_memory(cifArgTypesNode->argumentTypes);
cifArgTypesNode = pool_nextDo(&poolState);
}
pool_kill(vm->cifArgumentTypesCache);
vm->cifArgumentTypesCache = NULL;
}
/* Delete the layout string hashtable if exists. */
if (NULL != vm->layoutStrFFITypeTable) {
releaseLayoutStrFFITypeTable(vm->layoutStrFFITypeTable);
vm->layoutStrFFITypeTable = NULL;
}
/* Empty the thunk heap list if exists. */
if (NULL != vm->thunkHeapHead) {
releaseThunkHeap(vm);
}
#endif /* JAVA_SPEC_VERSION >= 16 */
/* Remove the predefinedHandlerWrapper. */
j9sig_set_single_async_signal_handler(predefinedHandlerWrapper, vm, 0, NULL);
/* Unload before trace engine exits */
UT_MODULE_UNLOADED(J9_UTINTERFACE_FROM_VM(vm));
if (0 != vm->vmRuntimeStateListener.minIdleWaitTime) {
stopVMRuntimeStateListener(vm);
}
if (NULL != vm->dllLoadTable) {
runShutdownStage(vm, INTERPRETER_SHUTDOWN, NULL, 0);
}
#if defined(J9VM_OPT_SNAPSHOTS)
if (IS_SNAPSHOTTING_ENABLED(vm)) {
teardownVMSnapshotImpl(vm);
}
#endif /* defined(J9VM_OPT_SNAPSHOTS) */
/* Kill global hot field class info pool and its monitor if dynamicBreadthFirstScanOrdering is enabled */
if (NULL != vm->memoryManagerFunctions) {
hotReferenceFieldRequired = vm->memoryManagerFunctions->j9gc_hot_reference_field_required(vm);
if (hotReferenceFieldRequired && NULL != vm->hotFieldClassInfoPool) {
pool_kill(vm->hotFieldClassInfoPool);
vm->hotFieldClassInfoPool = NULL;
}
if (hotReferenceFieldRequired && NULL != vm->hotFieldClassInfoPoolMutex) {
omrthread_monitor_destroy(vm->hotFieldClassInfoPoolMutex);
}
if (hotReferenceFieldRequired && NULL != vm->globalHotFieldPoolMutex) {
omrthread_monitor_destroy(vm->globalHotFieldPoolMutex);
}
}
if (NULL != vm->classMemorySegments) {
J9ClassWalkState classWalkState;
J9Class * clazz;
clazz = allClassesStartDo(&classWalkState, vm, NULL);
while (NULL != clazz) {
j9mem_free_memory(clazz->jniIDs);
clazz->jniIDs = NULL;
clazz = allClassesNextDo(&classWalkState);
}
allClassesEndDo(&classWalkState);
}
if (NULL != vm->classLoaderBlocks) {
pool_state clState = {0};
void *clToFree = NULL;
if (NULL != currentThread) {
internalAcquireVMAccess(currentThread);
}
clToFree = pool_startDo(vm->classLoaderBlocks, &clState);
while (NULL != clToFree) {
void *tmpToFree = NULL;
tmpToFree = clToFree;
clToFree = pool_nextDo(&clState);
freeClassLoader(tmpToFree, vm, currentThread, JNI_TRUE);
}
if (NULL != currentThread) {
internalReleaseVMAccess(currentThread);
}
}
if (NULL != vm->classLoadingConstraints) {
hashTableFree(vm->classLoadingConstraints);
vm->classLoadingConstraints = NULL;
}
#ifdef J9VM_OPT_ZIP_SUPPORT
if (NULL != vm->zipCachePool) {
zipCachePool_kill(vm->zipCachePool);
vm->zipCachePool = NULL;
}
#endif
#if defined(J9VM_INTERP_ATOMIC_FREE_JNI_USES_FLUSH)
shutDownExclusiveAccess(vm);
#endif /* J9VM_INTERP_ATOMIC_FREE_JNI_USES_FLUSH */
freeNativeMethodBindTable(vm);
freeHiddenInstanceFieldsList(vm);
cleanupLockwordConfig(vm);
cleanupEnsureHashedConfig(vm);
destroyJvmInitArgs(vm->portLibrary, vm->vmArgsArray);
vm->vmArgsArray = NULL;
if (NULL != vm->modulesPathEntry) {
j9mem_free_memory(vm->modulesPathEntry);
vm->modulesPathEntry = NULL;
}
if (NULL != vm->unnamedModuleForSystemLoader) {
vm->internalVMFunctions->freeJ9Module(vm, vm->unnamedModuleForSystemLoader);
vm->unnamedModuleForSystemLoader = NULL;
}
if (NULL != vm->modularityPool) {
pool_kill(vm->modularityPool);
vm->modularityPool = NULL;
/* vm->javaBaseModule should have already been freed when vm->systemClassLoader was freed earlier */
vm->javaBaseModule = NULL;
}
if (NULL != vm->jniGlobalReferences) {
pool_kill(vm->jniGlobalReferences);
vm->jniGlobalReferences = NULL;
}
if (NULL != vm->dllLoadTable) {
J9VMDllLoadInfo *traceLoadInfo = NULL;
if (NULL != currentThread) {
/* Send thread destroy event now to free some things before memcheck does the unfreed block scan */
TRIGGER_J9HOOK_VM_THREAD_DESTROY(vm->hookInterface, currentThread);
}
runShutdownStage(vm, LIBRARIES_ONUNLOAD, NULL, 0);
runUnOnloads(vm, FALSE);
runShutdownStage(vm, HEAP_STRUCTURES_FREED, NULL, 0);
if (NULL != currentThread) {
/* No need to worry about the zombie counter at this point */
deallocateVMThread(currentThread, FALSE, FALSE);
}
runShutdownStage(vm, GC_SHUTDOWN_COMPLETE, NULL, 0);
/* zOS: Do not close any of the DLLs. This is necessary
* because we do not know for sure whether all the threads
* that may depend on these DLLs have terminated.
*
* Note that this solution is also suggested in the
* 'z/OS XL C/C++ Programming Guide' V1R13 (Chapter 21
* under the heading 'DLL Restrictions').
*/
#if !defined(J9ZOS390)
closeAllDLLs(vm);
/* Remember the file descriptor of the trace DLL. This has to be closed later than other DLLs. */
traceLoadInfo = FIND_DLL_TABLE_ENTRY(J9_RAS_TRACE_DLL_NAME);
if (NULL != traceLoadInfo) {
traceDescriptor = traceLoadInfo->descriptor;
}
#endif /* !defined(J9ZOS390) */
freeDllLoadTable(vm->dllLoadTable);
vm->dllLoadTable = NULL;
}
/* Detach the VM from OMR */
detachVMFromOMR(vm);
if (NULL != vm->jniWeakGlobalReferences) {
pool_kill(vm->jniWeakGlobalReferences);
vm->jniWeakGlobalReferences = NULL;
}
if (NULL != vm->classLoaderBlocks) {
pool_kill(vm->classLoaderBlocks);
vm->classLoaderBlocks = NULL;
}
if (NULL != vm->classLoadingStackPool) {
pool_kill(vm->classLoadingStackPool);
vm->classLoadingStackPool = NULL;
}
#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES)
if (NULL != vm->valueTypeVerificationStackPool) {
pool_kill(vm->valueTypeVerificationStackPool);
vm->valueTypeVerificationStackPool = NULL;
}
#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */
#if JAVA_SPEC_VERSION >= 19
if (NULL != vm->tlsPool) {
pool_kill(vm->tlsPool);
vm->tlsPool = NULL;
}
if (NULL != vm->continuationT2Cache) {
for (U_32 i = 0; i < vm->continuationT2Size; i++) {
if (NULL != vm->continuationT2Cache[i]) {
freeJavaStack(vm, vm->continuationT2Cache[i]->stackObject);
j9mem_free_memory(vm->continuationT2Cache[i]);
}
}
j9mem_free_memory(vm->continuationT2Cache);
}
#endif /* JAVA_SPEC_VERSION >= 19 */
j9mem_free_memory(vm->vTableScratch);
vm->vTableScratch = NULL;
j9mem_free_memory(vm->osrGlobalBuffer);
vm->osrGlobalBuffer = NULL;
#if defined(COUNT_BYTECODE_PAIRS)
freeBytecodePairs(vm);
#endif /* COUNT_BYTECODE_PAIRS */
deleteStatistics(vm);
terminateVMThreading(vm);
tmpLib = vm->portLibrary;
#ifdef J9VM_INTERP_VERBOSE
if (J9_ARE_ANY_BITS_SET(vm->runtimeFlags, J9_RUNTIME_REPORT_STACK_USE)) {
/* J9NLS_VERB_MAX_STACK_USAGE=Verbose stack: maximum stack use was %zd/%zd bytes on Java/C stacks\n */
j9nls_printf(PORTLIB, J9NLS_INFO, J9NLS_VERB_MAX_STACK_USAGE, vm->maxStackUse, vm->maxCStackUse);
}
#endif
#ifdef J9VM_PROF_COUNT_ARGS_TEMPS
report(vm);
#endif
#if defined(J9VM_OPT_SHARED_CLASSES)
if (NULL != vm->sharedClassPreinitConfig) {
j9mem_free_memory(vm->sharedClassPreinitConfig);
vm->sharedClassPreinitConfig = NULL;
}
#endif
#ifdef J9VM_OPT_SIDECAR
if (NULL != vm->jvmExtensionInterface) {
j9mem_free_memory((void*)(vm->jvmExtensionInterface));
vm->jvmExtensionInterface = NULL;
}
#endif
shutdownVMHookInterface(vm);
freeSystemProperties(vm);
if (NULL != vm->j9ras) {
J9RASShutdown(vm);
}
contendedLoadTableFree(vm);
#ifndef J9VM_SIZE_SMALL_CODE
fieldIndexTableFree(vm);
#endif
/* Close the trace DLL. This has to be after all hashtable and pool free events, otherwise we'll crash on pool tracepoints */
if (0 != traceDescriptor) {
j9sl_close_shared_library(traceDescriptor);
}
#if !defined(WIN32)
/* restore any handler we may have overwritten */
if (NULL != vm->originalSIGPIPESignalAction) {
sigaction(SIGPIPE,(struct sigaction *)vm->originalSIGPIPESignalAction, NULL);
j9mem_free_memory(vm->originalSIGPIPESignalAction);
vm->originalSIGPIPESignalAction = NULL;
}
#endif
#if defined(J9VM_INTERP_CUSTOM_SPIN_OPTIONS)
/* Free custom spin options */
if (NULL != vm->customSpinOptions) {
pool_do(vm->customSpinOptions, cleanCustomSpinOptions, (void *)tmpLib);
pool_kill(vm->customSpinOptions);
vm->customSpinOptions = NULL;
}
#endif /* J9VM_INTERP_CUSTOM_SPIN_OPTIONS */
if (NULL != vm->realtimeSizeClasses) {
j9mem_free_memory(vm->realtimeSizeClasses);
vm->realtimeSizeClasses = NULL;
}
#if defined(J9VM_OPT_OPENJDK_METHODHANDLE)
if (NULL != vm->memberNameListsMutex) {
omrthread_monitor_destroy(vm->memberNameListsMutex);
vm->memberNameListsMutex = NULL;
}
if (NULL != vm->memberNameListNodePool) {
pool_kill(vm->memberNameListNodePool);
vm->memberNameListNodePool = NULL;
}
#endif /* defined(J9VM_OPT_OPENJDK_METHODHANDLE) */
#if defined(J9VM_OPT_CRIU_SUPPORT)
{
J9Pool *hookRecords = vm->checkpointState.hookRecords;
J9VMInitArgs *restoreArgsList = vm->checkpointState.restoreArgsList;
J9Pool *classIterationRestoreHookRecords = vm->checkpointState.classIterationRestoreHookRecords;
j9mem_free_memory(vm->checkpointState.restoreArgsChars);
if (NULL != hookRecords) {
pool_kill(hookRecords);
vm->checkpointState.hookRecords = NULL;
}
if (NULL != classIterationRestoreHookRecords) {
pool_kill(classIterationRestoreHookRecords);
vm->checkpointState.classIterationRestoreHookRecords = NULL;
}
j9sl_close_shared_library(vm->checkpointState.libCRIUHandle);
if (NULL != vm->delayedLockingOperationsMutex) {
omrthread_monitor_destroy(vm->delayedLockingOperationsMutex);
vm->delayedLockingOperationsMutex = NULL;
}
while (NULL != restoreArgsList) {
J9VMInitArgs *previousArgs = restoreArgsList->previousArgs;
destroyJvmInitArgs(vm->portLibrary, restoreArgsList);
restoreArgsList = previousArgs;
}