-
Notifications
You must be signed in to change notification settings - Fork 751
/
Copy pathjniinv.c
1326 lines (1079 loc) · 37 KB
/
jniinv.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
*******************************************************************************/
#include <string.h>
#if defined (LINUXPPC64) || (defined (AIXPPC) && defined (PPC64)) || defined (J9ZOS39064) || defined(J9ZTPF)
#include <stdlib.h> /* for getenv */
#endif
#include "j9user.h"
#include "j9.h"
#include "jni.h"
#include "j9protos.h"
#include "vmaccess.h"
#include "j9consts.h"
#include "omrthread.h"
#include "j9dump.h"
#include "jvminit.h"
#include "j9vmnls.h"
#include "j2sever.h"
#include "vmhook_internal.h"
#include "vm_internal.h"
#include "zip_api.h"
#include "ut_j9vm.h"
#include "jvmri.h"
static J9JavaVM * vmList = NULL;
#ifdef J9VM_OPT_VM_LOCAL_STORAGE
#include "j9vmls.h"
#define VM_FROM_ENV(env) (((J9VMThread *) (env))->javaVM)
static J9VMLSTable VMLSTable;
#endif
static UDATA terminateRemainingThreads (J9VMThread* vmThread);
static jint attachCurrentThread (JavaVM * vm, void ** p_env, void * thr_args, UDATA threadType);
jint JNICALL JNI_GetDefaultJavaVMInitArgs (void * vm_args);
static jint verifyCurrentThreadAttached (J9JavaVM * vm, J9VMThread ** pvmThread);
#if (defined(J9VM_OPT_SIDECAR))
void sidecarShutdown (J9VMThread* shutdownThread);
void sidecarExit(J9VMThread* shutdownThread);
#endif /* J9VM_OPT_SIDECAR */
static UDATA protectedDestroyJavaVM(J9PortLibrary* portLibrary, void * userData);
static UDATA protectedDetachCurrentThread(J9PortLibrary* portLibrary, void * userData);
static UDATA protectedInternalAttachCurrentThread(J9PortLibrary* portLibrary, void * userData);
static IDATA launchAttachApi(J9VMThread *currentThread);
J9_DECLARE_CONSTANT_UTF8(j9_init, "<init>");
J9_DECLARE_CONSTANT_UTF8(j9_clinit, "<clinit>");
J9_DECLARE_CONSTANT_UTF8(j9_printStackTrace, "printStackTrace");
J9_DECLARE_CONSTANT_UTF8(j9_shutdown, "shutdown");
J9_DECLARE_CONSTANT_UTF8(j9_exit, "exit");
J9_DECLARE_CONSTANT_UTF8(j9_run, "run");
J9_DECLARE_CONSTANT_UTF8(j9_initCause, "initCause");
J9_DECLARE_CONSTANT_UTF8(j9_completeInitialization, "completeInitialization");
J9_DECLARE_CONSTANT_UTF8(j9_void_void, "()V");
J9_DECLARE_CONSTANT_UTF8(j9_class_void, "(Ljava/lang/Class;)V");
J9_DECLARE_CONSTANT_UTF8(j9_class_class_void, "(Ljava/lang/Class;Ljava/lang/Class;)V");
J9_DECLARE_CONSTANT_UTF8(j9_string_void, "(Ljava/lang/String;)V");
J9_DECLARE_CONSTANT_UTF8(j9_throwable_void, "(Ljava/lang/Throwable;)V");
J9_DECLARE_CONSTANT_UTF8(j9_throwable_throwable, "(Ljava/lang/Throwable;)Ljava/lang/Throwable;");
J9_DECLARE_CONSTANT_UTF8(j9_int_void, "(I)V");
J9_DECLARE_CONSTANT_NAS(initCauseNameAndSig, j9_initCause, j9_throwable_throwable);
J9_DECLARE_CONSTANT_NAS(completeInitializationNameAndSig, j9_completeInitialization, j9_void_void);
J9_DECLARE_CONSTANT_NAS(threadRunNameAndSig, j9_run, j9_void_void);
J9_DECLARE_CONSTANT_NAS(initNameAndSig, j9_init, j9_void_void);
J9_DECLARE_CONSTANT_NAS(printStackTraceNameAndSig, j9_printStackTrace, j9_void_void);
J9_DECLARE_CONSTANT_NAS(clinitNameAndSig, j9_clinit, j9_void_void);
J9_DECLARE_CONSTANT_NAS(throwableNameAndSig , j9_init, j9_string_void);
J9_DECLARE_CONSTANT_NAS(arrayIndexNameAndSig, j9_init, j9_int_void);
J9_DECLARE_CONSTANT_NAS(throwableClassNameAndSig, j9_init, j9_class_void);
J9_DECLARE_CONSTANT_NAS(throwableClassClassNameAndSig, j9_init, j9_class_class_void);
J9_DECLARE_CONSTANT_NAS(throwableCauseNameAndSig, j9_init, j9_throwable_void);
J9NameAndSignature const * const exceptionConstructors[] = {
&throwableNameAndSig, /* J9ExceptionConstructorString */
&arrayIndexNameAndSig, /* J9ExceptionConstructorInt */
&throwableClassNameAndSig, /* J9ExceptionConstructorClass */
&throwableClassClassNameAndSig, /* J9ExceptionConstructorClassClass */
&throwableCauseNameAndSig, /* J9ExceptionConstructorThrowable */
};
typedef struct {
J9JavaVM * vm;
J9VMThread ** p_env;
J9JavaVMAttachArgs * thr_args;
UDATA threadType;
void * osThread;
} J9InternalAttachCurrentThreadArgs;
jint JNICALL J9_CreateJavaVM(JavaVM ** p_vm, void ** p_env, J9CreateJavaVMParams *createParams)
{
J9JavaVM *vm;
J9VMThread *env = NULL;
IDATA result;
omrthread_t osThread = NULL;
J9JavaVM **pvmList = GLOBAL_DATA(vmList);
UDATA version = (UDATA)((JDK1_1InitArgs *) createParams->vm_args->actualVMArgs)->version;
#ifdef J9VM_THR_PREEMPTIVE
omrthread_monitor_t globalMonitor;
#endif
if (!jniVersionIsValid(version) || version == JNI_VERSION_1_1)
return JNI_EVERSION; /* unknown arg style, exit. */
#ifndef J9VM_OPT_MULTI_VM
if (*pvmList)
return JNI_ERR;
#endif
if (omrthread_attach_ex(&osThread, J9THREAD_ATTR_DEFAULT))
return JNI_ERR;
#ifdef J9VM_THR_PREEMPTIVE
/* we must wait until after we attach to get the global monitor, since it may be * undefined before attach on some
platform (e.g. Unix) */
globalMonitor = omrthread_global_monitor();
#endif
/* Create the JavaVM structure */
if ((result = initializeJavaVM(osThread, (J9JavaVM**)p_vm, createParams)) != JNI_OK)
{
goto error0;
}
vm = *(J9JavaVM**)p_vm;
/* This has been set during initializeJavaVM */
env = vm->mainThread;
/* Success */
vm->runtimeFlags |= J9_RUNTIME_INITIALIZED;
*p_env = (void*) env;
/* Link the VM into the list */
#ifdef J9VM_THR_PREEMPTIVE
omrthread_monitor_enter(globalMonitor);
#endif
#ifdef J9VM_OPT_MULTI_VM
if (*pvmList) {
vm->linkNext = *pvmList;
vm->linkPrevious = (*pvmList)->linkPrevious;
(*pvmList)->linkPrevious = vm;
vm->linkPrevious->linkNext = vm;
} else {
*pvmList = vm->linkNext = vm->linkPrevious = vm;
}
#else
/* On single threaded VMs, the check at the beginning of this function is sufficient */
#ifdef J9VM_THR_PREEMPTIVE
if (*pvmList) {
omrthread_monitor_exit(globalMonitor);
result = JNI_ERR;
goto error;
}
#endif
*pvmList = vm;
#endif
#ifdef J9VM_THR_PREEMPTIVE
omrthread_monitor_exit(globalMonitor);
#endif
#if defined(J9VM_INTERP_ATOMIC_FREE_JNI)
enterVMFromJNI(env);
releaseVMAccess(env);
#endif /* J9VM_INTERP_ATOMIC_FREE_JNI */
TRIGGER_J9HOOK_VM_INITIALIZED(vm->hookInterface, env);
TRIGGER_J9HOOK_VM_THREAD_STARTED(vm->hookInterface, env, env);
if (0 != launchAttachApi(env)) {
result = JNI_ERR;
goto error;
}
enterVMFromJNI(env);
jniResetStackReferences((JNIEnv *) env);
releaseVMAccess(env);
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
if (NULL != vm->javaOffloadSwitchOffWithReasonFunc) {
env->javaOffloadState = 0;
vm->javaOffloadSwitchOffWithReasonFunc(env, J9_JNI_OFFLOAD_SWITCH_CREATE_JAVA_VM);
}
#endif
return JNI_OK;
error:
if (env) exceptionDescribe((JNIEnv *) env);
#ifdef J9VM_INTERP_SIG_QUIT_THREAD
vm->J9SigQuitShutdown(vm);
#endif
#ifdef J9VM_PROF_EVENT_REPORTING
if (env) {
#if defined(J9VM_INTERP_ATOMIC_FREE_JNI)
enterVMFromJNI(env);
releaseVMAccess(env);
#endif /* J9VM_INTERP_ATOMIC_FREE_JNI */
TRIGGER_J9HOOK_VM_SHUTTING_DOWN(vm->hookInterface, env, result);
}
#endif /* J9VM_PROF_EVENT_REPORTING */
freeJavaVM(vm);
error0:
omrthread_detach(osThread);
*p_vm = NULL;
*p_env = NULL;
return (jint) result;
}
jint JNICALL J9_GetCreatedJavaVMs(JavaVM ** vm_buf, jsize bufLen, jsize * nVMs)
{
jint result = JNI_ERR;
J9JavaVM**pvmList = GLOBAL_DATA(vmList);
jsize count = 0;
#ifdef J9VM_THR_PREEMPTIVE
omrthread_monitor_t globalMonitor;
if (omrthread_attach_ex(NULL, J9THREAD_ATTR_DEFAULT)) return -1;
globalMonitor = omrthread_global_monitor();
omrthread_monitor_enter(globalMonitor);
#endif
if (bufLen == 0) goto done;
if (*pvmList) {
#ifdef J9VM_OPT_MULTI_VM
J9JavaVM * vm = *pvmList;
while (count < bufLen) {
++count;
*(vm_buf++) = (JavaVM *) vm;
vm = vm->linkNext;
if (vm == *pvmList) break;
}
#else
count = 1;
*vm_buf = (JavaVM *) *pvmList;
#endif
}
#if defined (LINUXPPC64) || (defined (AIXPPC) && defined (PPC64)) || defined (J9ZOS39064)
/* there was a bug in older VMs on these platforms where jsize was defined to
* be 64-bits, rather than the 32-bits required by the JNI spec. Provide backwards
* compatibility if the JAVA_JSIZE_COMPAT environment variable is set
*/
if (getenv("JAVA_JSIZE_COMPAT")) {
*(jlong*)nVMs = (jlong)count;
} else {
*nVMs = count;
}
#else
*nVMs = count;
#endif
result = JNI_OK;
done:
#ifdef J9VM_THR_PREEMPTIVE
omrthread_monitor_exit(globalMonitor);
omrthread_detach(NULL);
#endif
return result;
}
static UDATA
protectedDestroyJavaVM(J9PortLibrary* portLibrary, void * userData)
{
J9VMThread * vmThread = userData;
J9JavaVM * vm = vmThread->javaVM;
J9JavaVM**pvmList = GLOBAL_DATA(vmList);
/* Note: we don't have a matching exit call because by the time we leave this function the trace engine
* has been shutdown
*/
Trc_JNIinv_protectedDestroyJavaVM_Entry();
/* Note: No need to ever unset this, since the vmThread is unuseable after DestroyJavaVM returns */
vmThread->gpProtected = TRUE;
Trc_JNIinv_protectedDestroyJavaVM_StartingThreadWait();
/* Wait until the current thread is the only non-daemon thread still alive */
omrthread_monitor_enter(vm->vmThreadListMutex);
while (vm->totalThreadCount != (vm->daemonThreadCount + 1)) {
omrthread_monitor_wait(vm->vmThreadListMutex);
}
omrthread_monitor_exit(vm->vmThreadListMutex);
Trc_JNIinv_protectedDestroyJavaVM_FinishedThreadWait();
/* If exit has already started, refuse the destroy request */
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);
}
/* Do not acquire exclusive here as it may cause deadlocks with
* the other thread shutting down.
*/
return JNI_ERR;
}
if (vm->runtimeFlagsMutex != NULL) {
omrthread_monitor_exit(vm->runtimeFlagsMutex);
}
/* run exit hooks */
sidecarShutdown(vmThread);
/* Prevent daemon threads from exiting */
if (vm->runtimeFlagsMutex != NULL) {
omrthread_monitor_enter(vm->runtimeFlagsMutex);
}
vm->runtimeFlags |= J9_RUNTIME_EXIT_STARTED;
if (vm->runtimeFlagsMutex != NULL) {
omrthread_monitor_exit(vm->runtimeFlagsMutex);
}
Trc_JNIinv_protectedDestroyJavaVM_vmCleanupDone();
/* mark the current thread as no-longer alive */
setEventFlag(vmThread, J9_PUBLIC_FLAGS_STOPPED);
/* We are dead at this point. Clear the suspend bit prior to triggering the thread end hook */
clearHaltFlag(vmThread, J9_PUBLIC_FLAGS_HALT_THREAD_JAVA_SUSPEND);
TRIGGER_J9HOOK_VM_THREAD_END(vm->hookInterface, vmThread, 1);
/* Do the java cleanup */
enterVMFromJNI(vmThread);
cleanUpAttachedThread(vmThread);
releaseVMAccess(vmThread);
TRIGGER_J9HOOK_VM_SHUTTING_DOWN(vm->hookInterface, vmThread, 0);
Trc_JNIinv_protectedDestroyJavaVM_vmShutdownHookFired();
#ifdef J9VM_OPT_SIDECAR
if (vm->sidecarExitHook)
(*(vm->sidecarExitHook))(vm);
#endif
/*
* shutdown the finalizer BEFORE we shutdown daemon threads, as
* finalize methods could spawn more threads
*/
vm->memoryManagerFunctions->gcShutdownHeapManagement(vm);
Trc_JNIinv_protectedDestroyJavaVM_HeapManagementShutdown();
#ifdef J9VM_INTERP_SIG_QUIT_THREAD
if (vm->J9SigQuitShutdown) {
vm->J9SigQuitShutdown(vm);
}
#endif
Trc_JNIinv_protectedDestroyJavaVM_vmShutdownDone();
if (terminateRemainingThreads(vmThread)) {
Trc_JNIinv_protectedDestroyJavaVM_terminateRemainingThreadsFailed();
/* If we cannot shut down, at least run exit code in loaded modules */
runExitStages(vm, vmThread);
/* bring all threads to a safepoint before we exit. Can't kill the remaining
* threads attached to the vm (daemons) so bring them to a safe point and let
* exit proceed. This will prevent intermittent crashes when GC occurs due to
* running daemon threads after native stacks have been unloaded from the process
*/
internalAcquireVMAccess(vmThread);
acquireExclusiveVMAccess(vmThread);
#if defined(COUNT_BYTECODE_PAIRS)
printBytecodePairs(vm);
#endif /* COUNT_BYTECODE_PAIRS */
Trc_JNIinv_protectedDestroyJavaVM_CallingExitHookSecondary();
if (vm->exitHook) {
vm->exitHook(0);
}
return JNI_ERR;
}
#ifdef J9VM_OPT_MULTI_VM
#ifdef J9VM_THR_PREEMPTIVE
{
omrthread_monitor_t globalMonitor = omrthread_global_monitor();
omrthread_monitor_enter(globalMonitor);
#endif
vm->linkPrevious->linkNext = vm->linkNext;
vm->linkNext->linkPrevious = vm->linkPrevious;
if (vm == *pvmList)
*pvmList = (vm->linkNext == vm) ? NULL : vm->linkNext;
#ifdef J9VM_THR_PREEMPTIVE
omrthread_monitor_exit(globalMonitor);
}
#endif
#else
*pvmList = NULL;
#endif
Trc_JNIinv_protectedDestroyJavaVM_CallingExitHook();
if (vm->exitHook) {
vm->exitHook(0);
}
freeJavaVM(vm);
return JNI_OK;
}
jint JNICALL DestroyJavaVM(JavaVM * javaVM)
{
J9JavaVM * vm = ((J9InvocationJavaVM *)javaVM)->j9vm;
J9VMThread * vmThread;
UDATA result;
J9PortLibrary * selfHandle;
PORT_ACCESS_FROM_PORT(vm->portLibrary);
/* There isn't an exit tracepoint on every path because the trace engine will be shut down
* in protectedDestroyJavaVM
*/
Trc_JNIinv_DestroyJavaVM_Entry(javaVM);
/* There is a cyclic dependency when using HealthCenter which causes JVM to hang during shutdown.
* To break this cycle, thread doing the shutdown needs to be detached to notify Healthcenter that
* it has terminated, and then re-attached as "DestroyJavaVM helper thread".
* As part of this fix, Healthcenter also needs to be modified to not do the join on "DestroyJavaVM helper thread".
* Therefore, if ever this thread name is modified, HealthCenter should be informed to adapt accordingly.
*/
result = DetachCurrentThread(javaVM);
if ((JNI_OK == result) || (JNI_EDETACHED == result)) {
J9JavaVMAttachArgs thr_args;
thr_args.version = JNI_VERSION_1_2;
thr_args.name = "DestroyJavaVM helper thread";
thr_args.group = vm->systemThreadGroupRef;
if (AttachCurrentThread((JavaVM *) vm, (void **) &vmThread, &thr_args) != JNI_OK) {
Trc_JNIinv_DestroyJavaVM_failedAttach_Exit();
return JNI_ERR;
}
} else {
Trc_JNIinv_DestroyJavaVM_DetachCurrentThread_Exit(result);
return (jint) result;
}
if(vm->runtimeFlagsMutex != NULL) {
omrthread_monitor_enter(vm->runtimeFlagsMutex);
}
/* we only allow shutdown code to run once */
if(vm->runtimeFlags & J9_RUNTIME_SHUTDOWN_STARTED) {
if(vm->runtimeFlagsMutex != NULL) {
omrthread_monitor_exit(vm->runtimeFlagsMutex);
}
Trc_JNIinv_DestroyJavaVM_alreadyShutdown_Exit();
return JNI_ERR;
}
vm->runtimeFlags |= J9_RUNTIME_SHUTDOWN_STARTED;
if(vm->runtimeFlagsMutex != NULL) {
omrthread_monitor_exit(vm->runtimeFlagsMutex);
}
/* Make sure freeJavaVM does not free the port library inside the signal protection */
selfHandle = PORTLIB->self_handle;
PORTLIB->self_handle = NULL;
if (j9sig_protect(
protectedDestroyJavaVM, vmThread,
structuredSignalHandler, vmThread,
J9PORT_SIG_FLAG_SIGALLSYNC | J9PORT_SIG_FLAG_MAY_CONTINUE_EXECUTION,
&result)
) {
result = JNI_ERR;
}
if (selfHandle != NULL) {
PORTLIB->self_handle = selfHandle;
if (result == JNI_OK) {
j9port_shutdown_library();
}
}
if (result == JNI_OK) {
/* Detach the OS thread from the VM (this will discard the thread on the last detach) */
omrthread_detach(NULL);
}
return (jint) result;
}
jint JNICALL AttachCurrentThread(JavaVM * vm, void ** p_env, void * thr_args)
{
return attachCurrentThread(vm, p_env, thr_args, 0);
}
static UDATA
protectedDetachCurrentThread(J9PortLibrary* portLibrary, void * userData)
{
J9VMThread * vmThread = userData;
/* Note: No need to ever unset this, since the vmThread will be discarded */
vmThread->gpProtected = TRUE;
threadCleanup(vmThread, FALSE);
return JNI_OK;
}
jint JNICALL DetachCurrentThread(JavaVM * javaVM)
{
J9JavaVM * vm = ((J9InvocationJavaVM *)javaVM)->j9vm;
J9VMThread * vmThread = NULL;
UDATA result = 0;
PORT_ACCESS_FROM_PORT(vm->portLibrary);
/* Verify that the current thread is allowed to detach from the VM */
result = (UDATA) verifyCurrentThreadAttached(vm, &vmThread);
if (JNI_OK == result) {
/* If exit has started, do not perform the detach operation to avoid
* the unexpected hang caused by the checking of exclusive VM access in deallocateVMThread()
*
* Allow system threads (those forked internally by the VM) to detach if exit has
* started (VM shutdown itself will detach system threads).
*/
if (J9_ARE_ALL_BITS_SET(vm->runtimeFlags, J9_RUNTIME_EXIT_STARTED)) {
if (J9_ARE_NO_BITS_SET(vmThread->privateFlags, J9_PRIVATE_FLAGS_SYSTEM_THREAD)) {
goto done;
}
}
Trc_JNIinv_DetachCurrentThread(vmThread);
/* Perform thread cleanup */
if (j9sig_protect(
protectedDetachCurrentThread, vmThread,
structuredSignalHandler, vmThread,
J9PORT_SIG_FLAG_SIGALLSYNC | J9PORT_SIG_FLAG_MAY_CONTINUE_EXECUTION,
&result)
) {
result = JNI_ERR;
}
if (result == JNI_OK) {
/* Detach the OS thread from the VM (this will discard the thread on the last detach) */
omrthread_detach(NULL);
}
}
done:
return (jint) result;
}
static UDATA
protectedInternalAttachCurrentThread(J9PortLibrary* portLibrary, void * userData)
{
J9InternalAttachCurrentThreadArgs * args = userData;
J9JavaVM * vm = args->vm;
J9VMThread ** p_env = args->p_env;
J9JavaVMAttachArgs * thr_args = args->thr_args;
UDATA threadType = args->threadType;
const char *threadName = NULL;
char *modifiedThreadName = NULL; /* needed if the original thread name is bad UTF8 */
j9object_t *threadGroup = NULL;
J9VMThread *env;
UDATA osStackFree;
void *memorySpace = vm->defaultMemorySpace;
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
BOOLEAN ifaEnabled = FALSE;
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) */
PORT_ACCESS_FROM_PORT(portLibrary);
threadType |= J9_PRIVATE_FLAGS_ATTACHED_THREAD;
if(thr_args != NULL) {
U_32 jniVersion = (U_32)thr_args->version;
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
/* Threads which are already IFA enabled can set the IFA_ENABLED_JNI_VERSION flag
* in the JNI version to indicate this to the VM. Otherwise the JVM will remove
* the thread from the IFA when it's not running Java code.
*/
if (IFA_ENABLED_JNI_VERSION == (jniVersion & IFA_ENABLED_JNI_VERSION_MASK)) {
jniVersion &= ~IFA_ENABLED_JNI_VERSION_MASK;
ifaEnabled = TRUE;
}
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) */
if(!jniVersionIsValid((UDATA)jniVersion)) {
return JNI_EVERSION;
}
threadGroup = thr_args->group;
threadName = thr_args->name;
if (NULL != threadName) {
size_t threadNameLength = strlen(threadName);
if (!isValidUtf8((U_8*)threadName, threadNameLength)) {
/* this code is very rarely executed, so not optimized */
const char *errorMessage = j9nls_lookup_message(J9NLS_DO_NOT_PRINT_MESSAGE_TAG | J9NLS_DO_NOT_APPEND_NEWLINE,
J9NLS_VM_JNI_INVALID_UTF8, "Invalid UTF8");
size_t errorMessageLength = strlen(errorMessage);
modifiedThreadName = j9mem_allocate_memory(errorMessageLength+threadNameLength+3, OMRMEM_CATEGORY_VM);
/* add 2 bytes for the ": " and one byte for the null */
if (NULL != modifiedThreadName) {
strcpy(modifiedThreadName, errorMessage);
strcat(modifiedThreadName, ": ");
fixBadUtf8((U_8*)threadName, (U_8*)modifiedThreadName+strlen(modifiedThreadName), threadNameLength); /* concatenate the corrected name with the error string */
threadName = modifiedThreadName;
} else {
return JNI_ENOMEM;
}
}
}
}
if ((env = allocateVMThread(vm, args->osThread, threadType, memorySpace, NULL)) == NULL) {
return JNI_ENOMEM;
}
env->gpProtected = TRUE;
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
if (ifaEnabled) {
env->javaOffloadState = 1;
}
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) */
/* Determine the thread's remaining OS stack */
osStackFree = omrthread_current_stack_free();
if (osStackFree == 0) {
osStackFree = vm->defaultOSStackSize;
}
env->currentOSStackFree = osStackFree - (osStackFree / J9VMTHREAD_RESERVED_C_STACK_FRACTION);
threadAboutToStart(env);
/* if this is the main thread, the VM hasn't been bootstrapped yet, so we can't do this yet */
if ( (threadType & J9_PRIVATE_FLAGS_NO_OBJECT) == 0) {
#if defined(J9VM_INTERP_ATOMIC_FREE_JNI)
internalEnterVMFromJNI(env);
internalReleaseVMAccess(env);
#endif /* J9VM_INTERP_ATOMIC_FREE_JNI */
initializeAttachedThread(
env,
threadName,
threadGroup,
(threadType & J9_PRIVATE_FLAGS_DAEMON_THREAD) != 0,
env);
j9mem_free_memory(modifiedThreadName);
if ((env->currentException != NULL) || (env->threadObject == NULL)) {
/* We did not run threadCleanup, so the zombie counter was not incremented */
deallocateVMThread(env, FALSE, TRUE);
return JNI_ERR;
}
TRIGGER_J9HOOK_VM_THREAD_STARTED(vm->hookInterface, env, env);
} else {
j9mem_free_memory(modifiedThreadName);
#ifdef J9VM_OPT_JAVA_OFFLOAD_SUPPORT
env->javaOffloadState = 1;
#endif
}
/* Success */
env->gpProtected = FALSE;
*p_env = env;
return JNI_OK;
}
IDATA
internalAttachCurrentThread(J9JavaVM * vm, J9VMThread ** p_env, J9JavaVMAttachArgs * thr_args, UDATA threadType, void * osThread)
{
PORT_ACCESS_FROM_PORT(vm->portLibrary);
J9InternalAttachCurrentThreadArgs args;
UDATA result;
args.vm = vm;
args.p_env = p_env;
args.thr_args = thr_args;
args.threadType = threadType;
args.osThread = osThread;
if (j9sig_protect(
protectedInternalAttachCurrentThread, &args,
structuredSignalHandlerVM, vm,
J9PORT_SIG_FLAG_SIGALLSYNC | J9PORT_SIG_FLAG_MAY_CONTINUE_EXECUTION,
&result)
) {
result = JNI_ERR;
}
return (IDATA) result;
}
static J9ThreadEnv threadEnv = {
omrthread_get_priority,
omrthread_set_priority,
omrthread_self,
omrthread_global,
omrthread_attach,
omrthread_sleep,
omrthread_create,
omrthread_exit,
omrthread_abort,
omrthread_priority_interrupt,
omrthread_clear_interrupted,
omrthread_monitor_enter,
omrthread_monitor_exit,
omrthread_monitor_init_with_name,
omrthread_monitor_destroy,
omrthread_monitor_wait,
omrthread_monitor_notify_all,
omrthread_tls_get,
omrthread_tls_set,
omrthread_tls_alloc,
omrthread_tls_free
};
/**
* JVMTI_VERSION_1_0 0x30010000
* IFA_ENABLED_JNI_VERSION 0x79xxxxxx - combine with the JNI_VERSION
* J9THREAD_VERSION_1_1 0x7C010001
* SUNVMI_VERSION_1_1 0x7D010001
* UTE_VERSION_1_1 0x7E000101
* JVMEXT_VERSION_1_1 0x7E010001
* JVMRAS_VERSION_1_1 0x7F000001
* JVMRAS_VERSION_1_3 0x7F000003
* JVMRAS_VERSION_1_5 0x7F000005
*/
jint JNICALL GetEnv(JavaVM *jvm, void **penv, jint version)
{
J9JavaVM * vm = ((J9InvocationJavaVM *)jvm)->j9vm;
J9VMThread *vmThread = NULL;
jint rc = JNI_EVERSION;
/* all failure cases require that *penv be set to NULL */
*penv = NULL;
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
/* The IFA_ENABLED_JNI_VERSION flag can be used as the version to determine
* if the JVM contains this support when the thread is not attached.
*/
if (IFA_ENABLED_JNI_VERSION == version) {
return JNI_OK;
}
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) */
if (version == J9THREAD_VERSION_1_1) {
*penv = (void*)&threadEnv;
return JNI_OK;
}
/* GetEnv is only allowed on attached threads */
vmThread = currentVMThread(vm);
if (vmThread == NULL) {
return JNI_EDETACHED;
}
/* Call the hook - if rc changes from JNI_EVERSION, return it.
* Note: the JavaVM parameter must be used in the call instead of the J9JavaVM
* because the JavaVM parameter should be a J9InvocationJavaVM* when GetEnv()
* is called from JVMTI agents.
*/
TRIGGER_J9HOOK_VM_GETENV(vm->hookInterface, jvm, penv, version, rc);
if (rc != JNI_EVERSION) {
return rc;
}
if (version == UTE_VERSION_1_1) {
if (vm->j9rasGlobalStorage != NULL) {
*penv = ((RasGlobalStorage *)vm->j9rasGlobalStorage)->utIntf;
}
return *penv == NULL ? JNI_EVERSION : JNI_OK;
}
if (version == JVMRAS_VERSION_1_1 || version == JVMRAS_VERSION_1_3 || version == JVMRAS_VERSION_1_5 ) {
PORT_ACCESS_FROM_JAVAVM(vm);
if (vm->j9rasGlobalStorage == NULL) {
j9nls_printf(PORTLIB, J9NLS_ERROR, J9NLS_VM_JVMRI_REQUESTED_WITHOUT_TRACE);
return JNI_EINVAL;
} else {
*penv = ((RasGlobalStorage *)vm->j9rasGlobalStorage)->jvmriInterface;
}
return *penv == NULL ? JNI_EVERSION : JNI_OK;
}
#if defined(J9VM_OPT_SIDECAR)
if (version == JVMEXT_VERSION_1_1) {
*penv = (void*)&vm->jvmExtensionInterface;
return JNI_OK;
}
#endif /* J9VM_OPT_SIDECAR */
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
/* The IFA_ENABLED_JNI_VERSION flag can be set in the JNI version to determine
* if the JVM contains this support.
*/
if (IFA_ENABLED_JNI_VERSION == (version & IFA_ENABLED_JNI_VERSION_MASK)) {
version &= ~IFA_ENABLED_JNI_VERSION_MASK;
}
#endif /* defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT) */
if (!jniVersionIsValid((UDATA)version)) {
return JNI_EVERSION;
}
*penv = (void *)vmThread;
return JNI_OK;
}
IDATA
attachSystemDaemonThread(J9JavaVM * vm, J9VMThread ** p_env, const char * threadName)
{
J9JavaVMAttachArgs attachArgs;
omrthread_t osThread;
IDATA rc;
if (omrthread_attach_ex(&osThread, J9THREAD_ATTR_DEFAULT)) {
return JNI_EDETACHED;
}
attachArgs.version = JNI_VERSION_1_2;
attachArgs.name = threadName;
attachArgs.group = vm->systemThreadGroupRef;
rc = internalAttachCurrentThread(
vm, p_env, &attachArgs,
J9_PRIVATE_FLAGS_SYSTEM_THREAD | J9_PRIVATE_FLAGS_DAEMON_THREAD,
osThread);
if (rc != JNI_OK) {
omrthread_detach(osThread);
}
return rc;
}
#if (defined(J9VM_OPT_VM_LOCAL_STORAGE))
void initializeVMLocalStorage(J9JavaVM * vm)
{
extern J9VMLSFunctionTable J9VMLSFunctions;
J9VMLSTable* vmls = GLOBAL_DATA(VMLSTable);
vm->vmLocalStorageFunctions = GLOBAL_TABLE( J9VMLSFunctions );
#ifdef J9VM_OPT_MULTI_VM
if (!vmls->initialized) {
UDATA i;
#ifdef J9VM_THR_PREEMPTIVE
omrthread_monitor_t globalMonitor = omrthread_global_monitor();
omrthread_monitor_enter(globalMonitor);
if (!vmls->initialized) {
#endif
for (i = 1; i < J9VMLS_MAX_KEYS-1; ++i)
vmls->keys[i] = i + 1;
vmls->keys[0] = 0;
vmls->keys[J9VMLS_MAX_KEYS-1] = 0;
vmls->head = 1;
vmls->freeKeys = J9VMLS_MAX_KEYS-1;
vmls->initialized = TRUE;
#ifdef J9VM_THR_PREEMPTIVE
}
omrthread_monitor_exit(globalMonitor);
#endif
}
#endif
}
#endif /* J9VM_OPT_VM_LOCAL_STORAGE */
#if (defined(J9VM_OPT_VM_LOCAL_STORAGE))
UDATA JNICALL J9VMLSAllocKeys(JNIEnv * env, UDATA * pInitCount, ...)
{
va_list args;
J9VMLSTable *vmls = GLOBAL_DATA(VMLSTable);
J9JavaVM **pvmList = GLOBAL_DATA(vmList);
#ifdef J9VM_THR_PREEMPTIVE
omrthread_monitor_t globalMonitor = omrthread_global_monitor();
omrthread_monitor_enter(globalMonitor);
#endif
if (++(*pInitCount) == 1) {
void **pKey;
#ifdef J9VM_OPT_MULTI_VM
UDATA count = 0;
/* Count the number of keys being initialized */
va_start(args, pInitCount);
while ((va_arg(args, void *)) != NULL) {
++count;
}
va_end(args);
/* Fail if there are not enough free keys left */
if (count > vmls->freeKeys) {
#ifdef J9VM_THR_PREEMPTIVE
omrthread_monitor_exit(globalMonitor);
#endif
return 1;
}
/* Allocate the keys */
va_start(args, pInitCount);
while ((pKey = va_arg(args, void *)) != NULL) {
UDATA key;
key = vmls->head;
vmls->head = vmls->keys[key];
vmls->keys[key] = (UDATA) - 1;
*pKey = (void *) key;
/* Initialize the value of the key to NULL this VM and all other known VMs. VMs which are being created
(i.e. not in the list yet) will have all VMLS values set to NULL already. The current VM may or may not
be in the list, which means that the list may be empty. The global monitor protects the VM list as well
as the key list. */
VM_FROM_ENV(env)->vmLocalStorage[key - 1] = NULL;
if (*pvmList) {
J9JavaVM *vm = *pvmList;
do {
vm->vmLocalStorage[key - 1] = NULL;
vm = vm->linkNext;
} while (vm != *pvmList);
}
}
va_end(args);
vmls->freeKeys -= count;
#else
va_start(args, pInitCount);
while ((pKey = va_arg(args, void *)) != NULL) {
*pKey = NULL;
}
va_end(args);
#endif
}