-
Notifications
You must be signed in to change notification settings - Fork 738
/
Copy pathjavanextvmi.cpp
790 lines (655 loc) · 25.7 KB
/
javanextvmi.cpp
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
/*******************************************************************************
* Copyright IBM Corp. and others 2018
*
* 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
*******************************************************************************/
#include <assert.h>
#include <jni.h>
#include "bcverify_api.h"
#include "j9.h"
#include "j9cfg.h"
#include "jvminit.h"
#include "rommeth.h"
#include "ut_j9scar.h"
#include "util_api.h"
#if JAVA_SPEC_VERSION >= 19
#include "VMHelpers.hpp"
#include "ContinuationHelpers.hpp"
#endif /* JAVA_SPEC_VERSION >= 19 */
extern "C" {
#if JAVA_SPEC_VERSION >= 19
extern J9JavaVM *BFUjavaVM;
extern IDATA (*f_threadSleep)(I_64 millis);
#endif /* JAVA_SPEC_VERSION >= 19 */
/* Define for debug
#define DEBUG_BCV
*/
#if JAVA_SPEC_VERSION >= 16
JNIEXPORT void JNICALL
JVM_DefineArchivedModules(JNIEnv *env, jobject obj1, jobject obj2)
{
Assert_SC_true(!"JVM_DefineArchivedModules unimplemented");
}
JNIEXPORT void JNICALL
JVM_LogLambdaFormInvoker(JNIEnv *env, jstring str)
{
Assert_SC_true(!"JVM_LogLambdaFormInvoker unimplemented");
}
#endif /* JAVA_SPEC_VERSION >= 16 */
#if JAVA_SPEC_VERSION >= 11
JNIEXPORT void JNICALL
JVM_InitializeFromArchive(JNIEnv *env, jclass clz)
{
/* A no-op implementation is ok. */
}
#endif /* JAVA_SPEC_VERSION >= 11 */
#if JAVA_SPEC_VERSION >= 14
typedef struct GetStackTraceElementUserData {
J9ROMClass *romClass;
J9ROMMethod *romMethod;
UDATA bytecodeOffset;
} GetStackTraceElementUserData;
static UDATA
getStackTraceElementIterator(J9VMThread *vmThread, void *voidUserData, UDATA bytecodeOffset, J9ROMClass *romClass, J9ROMMethod *romMethod, J9UTF8 *fileName, UDATA lineNumber, J9ClassLoader *classLoader, J9Class* ramClass)
{
UDATA result = J9_STACKWALK_STOP_ITERATING;
if ((NULL != romMethod)
&& J9_ARE_ALL_BITS_SET(romMethod->modifiers, J9AccMethodFrameIteratorSkip)
) {
/* Skip methods with java.lang.invoke.FrameIteratorSkip / jdk.internal.vm.annotation.Hidden / java.lang.invoke.LambdaForm$Hidden annotation */
result = J9_STACKWALK_KEEP_ITERATING;
} else {
GetStackTraceElementUserData *userData = (GetStackTraceElementUserData *)voidUserData;
/* We are done, first non-hidden stack frame is found. */
userData->romClass = romClass;
userData->romMethod = romMethod;
userData->bytecodeOffset = bytecodeOffset;
}
return result;
}
#if defined(DEBUG_BCV)
static void cfdumpBytecodePrintFunction(void *userData, char *format, ...)
{
PORT_ACCESS_FROM_PORT((J9PortLibrary*)userData);
va_list args;
char outputBuffer[512] = {0};
va_start(args, format);
j9str_vprintf(outputBuffer, 512, format, args);
va_end(args);
j9tty_printf(PORTLIB, "%s", outputBuffer);
}
#endif /* defined(DEBUG_BCV) */
JNIEXPORT jstring JNICALL
JVM_GetExtendedNPEMessage(JNIEnv *env, jthrowable throwableObj)
{
J9VMThread *vmThread = (J9VMThread *)env;
J9JavaVM *vm = vmThread->javaVM;
jobject msgObjectRef = NULL;
Trc_SC_GetExtendedNPEMessage_Entry(vmThread, throwableObj);
if (J9_ARE_ANY_BITS_SET(vm->extendedRuntimeFlags2, J9_EXTENDED_RUNTIME2_SHOW_EXTENDED_NPEMSG)) {
J9InternalVMFunctions const * const vmFuncs = vm->internalVMFunctions;
char *npeMsg = NULL;
GetStackTraceElementUserData userData = {0};
/* If -XX:+ShowHiddenFrames option has not been set, skip hidden method frames */
UDATA skipHiddenFrames = J9_ARE_NO_BITS_SET(vm->runtimeFlags, J9_RUNTIME_SHOW_HIDDEN_FRAMES);
Trc_SC_GetExtendedNPEMessage_Entry2(vmThread, throwableObj);
vmFuncs->internalEnterVMFromJNI(vmThread);
userData.bytecodeOffset = UDATA_MAX;
vmFuncs->iterateStackTrace(vmThread, (j9object_t*)throwableObj, getStackTraceElementIterator, &userData, TRUE, skipHiddenFrames);
if ((NULL != userData.romClass)
&& (NULL != userData.romMethod)
&& (UDATA_MAX != userData.bytecodeOffset)
) {
PORT_ACCESS_FROM_VMC(vmThread);
J9NPEMessageData npeMsgData = {0};
#if defined(DEBUG_BCV)
{
U_8 *bytecodes = J9_BYTECODE_START_FROM_ROM_METHOD(userData.romMethod);
U_32 flags = 0;
#if defined(J9VM_ENV_LITTLE_ENDIAN)
flags |= BCT_LittleEndianOutput;
#else /* defined(J9VM_ENV_LITTLE_ENDIAN) */
flags |= BCT_BigEndianOutput;
#endif /* defined(J9VM_ENV_LITTLE_ENDIAN) */
j9bcutil_dumpBytecodes(PORTLIB, userData.romClass, bytecodes, 0, userData.bytecodeOffset, flags, (void *)cfdumpBytecodePrintFunction, PORTLIB, "");
}
#endif /* defined(DEBUG_BCV) */
npeMsgData.npePC = userData.bytecodeOffset;
npeMsgData.vmThread = vmThread;
npeMsgData.romClass = userData.romClass;
npeMsgData.romMethod = userData.romMethod;
npeMsg = vmFuncs->getNPEMessage(&npeMsgData);
if (NULL != npeMsg) {
j9object_t msgObject = vm->memoryManagerFunctions->j9gc_createJavaLangString(vmThread, (U_8 *)npeMsg, strlen(npeMsg), 0);
if (NULL != msgObject) {
msgObjectRef = vmFuncs->j9jni_createLocalRef(env, msgObject);
}
j9mem_free_memory(npeMsg);
}
j9mem_free_memory(npeMsgData.liveStack);
j9mem_free_memory(npeMsgData.bytecodeOffset);
j9mem_free_memory(npeMsgData.bytecodeMap);
j9mem_free_memory(npeMsgData.stackMaps);
j9mem_free_memory(npeMsgData.unwalkedQueue);
} else {
Trc_SC_GetExtendedNPEMessage_Null_NPE_MSG(vmThread, userData.romClass, userData.romMethod, userData.bytecodeOffset);
}
vmFuncs->internalExitVMToJNI(vmThread);
}
Trc_SC_GetExtendedNPEMessage_Exit(vmThread, msgObjectRef);
return (jstring)msgObjectRef;
}
#endif /* JAVA_SPEC_VERSION >= 14 */
#if JAVA_SPEC_VERSION >= 17
JNIEXPORT void JNICALL
JVM_DumpClassListToFile(JNIEnv *env, jstring str)
{
Assert_SC_true(!"JVM_DumpClassListToFile unimplemented");
}
JNIEXPORT void JNICALL
JVM_DumpDynamicArchive(JNIEnv *env, jstring str)
{
Assert_SC_true(!"JVM_DumpDynamicArchive unimplemented");
}
#endif /* JAVA_SPEC_VERSION >= 17 */
#if JAVA_SPEC_VERSION >= 18
JNIEXPORT jboolean JNICALL
JVM_IsFinalizationEnabled(JNIEnv *env)
{
jboolean isFinalizationEnabled = JNI_TRUE;
J9VMThread *currentThread = (J9VMThread*)env;
if (J9_ARE_ANY_BITS_SET(currentThread->javaVM->extendedRuntimeFlags2, J9_EXTENDED_RUNTIME2_DISABLE_FINALIZATION)) {
isFinalizationEnabled = JNI_FALSE;
}
return isFinalizationEnabled;
}
JNIEXPORT void JNICALL
JVM_ReportFinalizationComplete(JNIEnv *env, jobject obj)
{
Assert_SC_true(!"JVM_ReportFinalizationComplete unimplemented");
}
#endif /* JAVA_SPEC_VERSION >= 18 */
#if JAVA_SPEC_VERSION >= 19
JNIEXPORT void * JNICALL
JVM_LoadZipLibrary(void)
{
void *zipHandle = NULL;
J9JavaVM *vm = BFUjavaVM;
if (NULL != vm) {
PORT_ACCESS_FROM_JAVAVM(vm);
uintptr_t handle = 0;
if (J9PORT_SL_FOUND == j9sl_open_shared_library(
(char *)"zip",
&handle,
OMRPORT_SLOPEN_DECORATE | OMRPORT_SLOPEN_LAZY)
) {
zipHandle = (void *)handle;
}
}
/* We may as well assert here: we won't make much progress without the library. */
Assert_SC_notNull(zipHandle);
return zipHandle;
}
JNIEXPORT void JNICALL
JVM_RegisterContinuationMethods(JNIEnv *env, jclass clz)
{
Assert_SC_true(!"JVM_RegisterContinuationMethods unimplemented");
}
JNIEXPORT jboolean JNICALL
JVM_IsContinuationsSupported(void)
{
return JNI_TRUE;
}
JNIEXPORT jboolean JNICALL
JVM_IsPreviewEnabled(void)
{
jboolean isPreviewEnabled = JNI_FALSE;
J9VMThread *currentThread = BFUjavaVM->internalVMFunctions->currentVMThread(BFUjavaVM);
if (J9_ARE_ANY_BITS_SET(currentThread->javaVM->extendedRuntimeFlags2, J9_EXTENDED_RUNTIME2_ENABLE_PREVIEW)) {
isPreviewEnabled = JNI_TRUE;
}
return isPreviewEnabled;
}
static void
enterVThreadTransitionCritical(J9VMThread *currentThread, jobject thread)
{
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
MM_ObjectAccessBarrierAPI objectAccessBarrier = MM_ObjectAccessBarrierAPI(currentThread);
j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE(thread);
retry:
while (!objectAccessBarrier.inlineMixedObjectCompareAndSwapU64(currentThread, threadObj, vm->virtualThreadInspectorCountOffset, 0, ~(U_64)0)) {
/* Thread is being inspected or unmounted, wait. */
vmFuncs->internalReleaseVMAccess(currentThread);
VM_AtomicSupport::yieldCPU();
/* After wait, the thread may suspend here. */
vmFuncs->internalAcquireVMAccess(currentThread);
threadObj = J9_JNI_UNWRAP_REFERENCE(thread);
}
/* Link the current J9VMThread with the virtual thread object. */
if (!objectAccessBarrier.inlineMixedObjectCompareAndSwapU64(currentThread, threadObj, vm->internalSuspendStateOffset, J9_VIRTUALTHREAD_INTERNAL_STATE_NONE, (U_64)currentThread)) {
/* If virtual thread is suspended while unmounted, reset the inspectorCount and do a wait and retry. */
if (VM_VMHelpers::isThreadSuspended(currentThread, threadObj)) {
J9OBJECT_I64_STORE(currentThread, threadObj, vm->virtualThreadInspectorCountOffset, 0);
}
vmFuncs->internalReleaseVMAccess(currentThread);
/* Spin is used instead of the halt flag as we cannot guarantee suspend flag is still set now.
*
* TODO: Dynamically increase the sleep time to a bounded maximum.
*/
f_threadSleep(10);
/* After wait, the thread may suspend here. */
vmFuncs->internalAcquireVMAccess(currentThread);
threadObj = J9_JNI_UNWRAP_REFERENCE(thread);
goto retry;
}
}
static void
exitVThreadTransitionCritical(J9VMThread *currentThread, jobject thread)
{
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
j9object_t vthread = J9_JNI_UNWRAP_REFERENCE(thread);
MM_ObjectAccessBarrierAPI objectAccessBarrier = MM_ObjectAccessBarrierAPI(currentThread);
/* Remove J9VMThread address from internalSuspendedState field, as the thread state is no longer in a transition. */
while (!objectAccessBarrier.inlineMixedObjectCompareAndSwapU64(currentThread, vthread, vm->internalSuspendStateOffset, (U_64)currentThread, J9_VIRTUALTHREAD_INTERNAL_STATE_NONE)) {
/* Wait if the suspend flag is set. */
vmFuncs->internalReleaseVMAccess(currentThread);
VM_AtomicSupport::yieldCPU();
/* After wait, the thread may suspend here. */
vmFuncs->internalAcquireVMAccess(currentThread);
vthread = J9_JNI_UNWRAP_REFERENCE(thread);
}
/* Update to virtualThreadInspectorCount must be after clearing isSuspendedInternal field to retain sync ordering. */
Assert_SC_true(-1 == J9OBJECT_I64_LOAD(currentThread, vthread, vm->virtualThreadInspectorCountOffset));
J9OBJECT_I64_STORE(currentThread, vthread, vm->virtualThreadInspectorCountOffset, 0);
}
static void
setContinuationStateToLastUnmount(J9VMThread *currentThread, jobject thread)
{
enterVThreadTransitionCritical(currentThread, thread);
/* Re-fetch reference as enterVThreadTransitionCritical may release VMAccess. */
j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE(thread);
j9object_t continuationObj = J9VMJAVALANGVIRTUALTHREAD_CONT(currentThread, threadObj);
ContinuationState volatile *continuationStatePtr = VM_ContinuationHelpers::getContinuationStateAddress(currentThread, continuationObj);
/* Used in JVMTI to not suspend the virtual thread once it enters the last unmount phase. */
VM_ContinuationHelpers::setLastUnmount(continuationStatePtr);
exitVThreadTransitionCritical(currentThread, thread);
}
/* Caller must have VMAccess. */
static void
virtualThreadMountBegin(JNIEnv *env, jobject thread)
{
J9VMThread *currentThread = (J9VMThread *)env;
j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE(thread);
Assert_SC_true(IS_JAVA_LANG_VIRTUALTHREAD(currentThread, threadObj));
if (TrcEnabled_Trc_SC_VirtualThread_Info) {
J9JavaVM *vm = currentThread->javaVM;
j9object_t continuationObj = J9VMJAVALANGVIRTUALTHREAD_CONT(currentThread, threadObj);
J9VMContinuation *continuation = J9VMJDKINTERNALVMCONTINUATION_VMREF(currentThread, continuationObj);
Trc_SC_VirtualThread_Info(
currentThread,
threadObj,
J9VMJAVALANGVIRTUALTHREAD_STATE(currentThread, threadObj),
J9OBJECT_I64_LOAD(currentThread, threadObj, vm->virtualThreadInspectorCountOffset),
J9VMJAVALANGVIRTUALTHREAD_CARRIERTHREAD(currentThread, threadObj),
continuationObj,
continuation);
}
enterVThreadTransitionCritical(currentThread, thread);
VM_VMHelpers::virtualThreadHideFrames(currentThread, JNI_TRUE);
}
/* Caller must have VMAccess. */
static void
virtualThreadMountEnd(JNIEnv *env, jobject thread)
{
J9VMThread *currentThread = (J9VMThread *)env;
J9JavaVM *vm = currentThread->javaVM;
j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE(thread);
Assert_SC_true(IS_JAVA_LANG_VIRTUALTHREAD(currentThread, threadObj));
if (TrcEnabled_Trc_SC_VirtualThread_Info) {
j9object_t continuationObj = J9VMJAVALANGVIRTUALTHREAD_CONT(currentThread, threadObj);
Trc_SC_VirtualThread_Info(
currentThread,
threadObj,
J9VMJAVALANGVIRTUALTHREAD_STATE(currentThread, threadObj),
J9OBJECT_I64_LOAD(currentThread, threadObj, vm->virtualThreadInspectorCountOffset),
J9VMJAVALANGVIRTUALTHREAD_CARRIERTHREAD(currentThread, threadObj),
continuationObj,
J9VMJDKINTERNALVMCONTINUATION_VMREF(currentThread, continuationObj));
}
VM_VMHelpers::virtualThreadHideFrames(currentThread, JNI_FALSE);
/* Allow thread to be inspected again. */
exitVThreadTransitionCritical(currentThread, thread);
TRIGGER_J9HOOK_VM_VIRTUAL_THREAD_MOUNT(vm->hookInterface, currentThread);
}
/* Caller must have VMAccess. */
static void
virtualThreadUnmountBegin(JNIEnv *env, jobject thread)
{
J9VMThread *currentThread = (J9VMThread *)env;
J9JavaVM *vm = currentThread->javaVM;
j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE(thread);
Assert_SC_true(IS_JAVA_LANG_VIRTUALTHREAD(currentThread, threadObj));
if (TrcEnabled_Trc_SC_VirtualThread_Info) {
j9object_t continuationObj = J9VMJAVALANGVIRTUALTHREAD_CONT(currentThread, threadObj);
Trc_SC_VirtualThread_Info(
currentThread,
threadObj,
J9VMJAVALANGVIRTUALTHREAD_STATE(currentThread, threadObj),
J9OBJECT_I64_LOAD(currentThread, threadObj, vm->virtualThreadInspectorCountOffset),
J9VMJAVALANGVIRTUALTHREAD_CARRIERTHREAD(currentThread, threadObj),
continuationObj,
J9VMJDKINTERNALVMCONTINUATION_VMREF(currentThread, continuationObj));
}
TRIGGER_J9HOOK_VM_VIRTUAL_THREAD_UNMOUNT(vm->hookInterface, currentThread);
enterVThreadTransitionCritical(currentThread, thread);
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
j9object_t carrierThreadObject = currentThread->carrierThreadObject;
/* Virtual thread is being umounted. If its carrier thread is suspended, spin until
* the carrier thread is resumed. The carrier thread should not be mounted until it
* is resumed.
*/
while (VM_VMHelpers::isThreadSuspended(currentThread, carrierThreadObject)) {
exitVThreadTransitionCritical(currentThread, thread);
vmFuncs->internalReleaseVMAccess(currentThread);
/* Spin is used instead of the halt flag; otherwise, the virtual thread will
* show as suspended.
*
* TODO: Dynamically increase the sleep time to a bounded maximum.
*/
f_threadSleep(10);
vmFuncs->internalAcquireVMAccess(currentThread);
enterVThreadTransitionCritical(currentThread, thread);
carrierThreadObject = currentThread->carrierThreadObject;
}
VM_VMHelpers::virtualThreadHideFrames(currentThread, JNI_TRUE);
}
/* Caller must have VMAccess. */
static void
virtualThreadUnmountEnd(JNIEnv *env, jobject thread)
{
J9VMThread *currentThread = (J9VMThread *)env;
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE(thread);
j9object_t continuationObj = J9VMJAVALANGVIRTUALTHREAD_CONT(currentThread, threadObj);
ContinuationState continuationState = *VM_ContinuationHelpers::getContinuationStateAddress(currentThread, continuationObj);
Assert_SC_true(IS_JAVA_LANG_VIRTUALTHREAD(currentThread, threadObj));
if (TrcEnabled_Trc_SC_VirtualThread_Info) {
Trc_SC_VirtualThread_Info(
currentThread,
threadObj,
J9VMJAVALANGVIRTUALTHREAD_STATE(currentThread, threadObj),
J9OBJECT_I64_LOAD(currentThread, threadObj, vm->virtualThreadInspectorCountOffset),
J9VMJAVALANGVIRTUALTHREAD_CARRIERTHREAD(currentThread, threadObj),
continuationObj,
J9VMJDKINTERNALVMCONTINUATION_VMREF(currentThread, continuationObj));
}
if (VM_ContinuationHelpers::isFinished(continuationState)) {
vmFuncs->freeTLS(currentThread, threadObj);
}
VM_VMHelpers::virtualThreadHideFrames(currentThread, JNI_FALSE);
/* Allow thread to be inspected again. */
exitVThreadTransitionCritical(currentThread, thread);
}
#endif /* JAVA_SPEC_VERSION >= 19 */
#if (19 <= JAVA_SPEC_VERSION) && (JAVA_SPEC_VERSION < 21)
JNIEXPORT void JNICALL
JVM_VirtualThreadMountBegin(JNIEnv *env, jobject thread, jboolean firstMount)
{
J9VMThread *currentThread = (J9VMThread *)env;
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
Trc_SC_VirtualThreadMountBegin_Entry(currentThread, thread, firstMount);
vmFuncs->internalEnterVMFromJNI(currentThread);
virtualThreadMountBegin(env, thread);
vmFuncs->internalExitVMToJNI(currentThread);
Trc_SC_VirtualThreadMountBegin_Exit(currentThread, thread, firstMount);
}
JNIEXPORT void JNICALL
JVM_VirtualThreadMountEnd(JNIEnv *env, jobject thread, jboolean firstMount)
{
J9VMThread *currentThread = (J9VMThread *)env;
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
Trc_SC_VirtualThreadMountEnd_Entry(currentThread, thread, firstMount);
vmFuncs->internalEnterVMFromJNI(currentThread);
virtualThreadMountEnd(env, thread);
if (firstMount) {
TRIGGER_J9HOOK_VM_VIRTUAL_THREAD_STARTED(vm->hookInterface, currentThread);
}
vmFuncs->internalExitVMToJNI(currentThread);
Trc_SC_VirtualThreadMountEnd_Exit(currentThread, thread, firstMount);
}
JNIEXPORT void JNICALL
JVM_VirtualThreadUnmountBegin(JNIEnv *env, jobject thread, jboolean lastUnmount)
{
J9VMThread *currentThread = (J9VMThread *)env;
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
Trc_SC_VirtualThreadUnmountBegin_Entry(currentThread, thread, lastUnmount);
vmFuncs->internalEnterVMFromJNI(currentThread);
if (lastUnmount) {
TRIGGER_J9HOOK_VM_VIRTUAL_THREAD_END(vm->hookInterface, currentThread);
setContinuationStateToLastUnmount((J9VMThread *)env, thread);
}
virtualThreadUnmountBegin(env, thread);
vmFuncs->internalExitVMToJNI(currentThread);
Trc_SC_VirtualThreadUnmountBegin_Exit(currentThread, thread,lastUnmount);
}
JNIEXPORT void JNICALL
JVM_VirtualThreadUnmountEnd(JNIEnv *env, jobject thread, jboolean lastUnmount)
{
J9VMThread *currentThread = (J9VMThread *)env;
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
Trc_SC_VirtualThreadUnmountEnd_Entry(currentThread, thread, lastUnmount);
vmFuncs->internalEnterVMFromJNI(currentThread);
virtualThreadUnmountEnd(env, thread);
vmFuncs->internalExitVMToJNI(currentThread);
Trc_SC_VirtualThreadUnmountEnd_Exit(currentThread, thread, lastUnmount);
}
#endif /* (19 <= JAVA_SPEC_VERSION) && (JAVA_SPEC_VERSION < 21) */
#if JAVA_SPEC_VERSION >= 20
JNIEXPORT jint JNICALL
JVM_GetClassFileVersion(JNIEnv *env, jclass cls)
{
jint version = 0;
J9VMThread *currentThread = (J9VMThread *)env;
J9InternalVMFunctions const * const vmFuncs = currentThread->javaVM->internalVMFunctions;
vmFuncs->internalEnterVMFromJNI(currentThread);
if (NULL == cls) {
vmFuncs->setCurrentException(currentThread, J9VMCONSTANTPOOL_JAVALANGNULLPOINTEREXCEPTION, NULL);
} else {
J9Class *clazz = J9VM_J9CLASS_FROM_JCLASS(currentThread, cls);
version = (jint)getClassFileVersion(currentThread, clazz);
}
vmFuncs->internalExitVMToJNI(currentThread);
return version;
}
JNIEXPORT void JNICALL
JVM_VirtualThreadHideFrames(
JNIEnv *env,
#if JAVA_SPEC_VERSION >= 23
jclass clz,
#else /* JAVA_SPEC_VERSION >= 23 */
jobject vthread,
#endif /* JAVA_SPEC_VERSION >= 23 */
jboolean hide)
{
J9VMThread *currentThread = (J9VMThread *)env;
J9InternalVMFunctions const * const vmFuncs = currentThread->javaVM->internalVMFunctions;
vmFuncs->internalEnterVMFromJNI(currentThread);
j9object_t vThreadObj = currentThread->threadObject;
Assert_SC_true(IS_JAVA_LANG_VIRTUALTHREAD(currentThread, vThreadObj));
/* Do not allow JVMTI operations because J9VMThread->threadObject is modified
* between the first invocation with hide=true and the second invocation with
* hide=false. Otherwise, JVMTI functions will see an unstable
* J9VMThread->threadObject.
*/
bool hiddenFrames = J9_ARE_ALL_BITS_SET(currentThread->privateFlags, J9_PRIVATE_FLAGS_VIRTUAL_THREAD_HIDDEN_FRAMES);
if (hide) {
Assert_SC_true(!hiddenFrames);
#if JAVA_SPEC_VERSION < 23
Assert_SC_true(vThreadObj == J9_JNI_UNWRAP_REFERENCE(vthread));
#endif /* JAVA_SPEC_VERSION < 23 */
enterVThreadTransitionCritical(currentThread, (jobject)¤tThread->threadObject);
}
VM_VMHelpers::virtualThreadHideFrames(currentThread, hide);
if (!hide) {
Assert_SC_true(hiddenFrames);
exitVThreadTransitionCritical(currentThread, (jobject)¤tThread->threadObject);
}
vmFuncs->internalExitVMToJNI(currentThread);
}
#endif /* JAVA_SPEC_VERSION >= 20 */
#if JAVA_SPEC_VERSION >= 21
JNIEXPORT jboolean JNICALL
JVM_PrintWarningAtDynamicAgentLoad()
{
jboolean result = JNI_TRUE;
J9JavaVM *vm = BFUjavaVM;
if (J9_ARE_ANY_BITS_SET(vm->runtimeFlags, J9_RUNTIME_ALLOW_DYNAMIC_AGENT)
&& (0 <= FIND_ARG_IN_VMARGS(EXACT_MATCH, VMOPT_XXENABLEDYNAMICAGENTLOADING, NULL))
) {
result = JNI_FALSE;
}
return result;
}
JNIEXPORT void JNICALL
JVM_VirtualThreadMount(JNIEnv *env, jobject vthread, jboolean hide)
{
J9VMThread *currentThread = (J9VMThread *)env;
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
Trc_SC_VirtualThreadMount_Entry(currentThread, vthread, hide);
vmFuncs->internalEnterVMFromJNI(currentThread);
if (hide) {
virtualThreadMountBegin(env, vthread);
} else {
virtualThreadMountEnd(env, vthread);
}
vmFuncs->internalExitVMToJNI(currentThread);
Trc_SC_VirtualThreadMount_Exit(currentThread, vthread, hide);
}
JNIEXPORT void JNICALL
JVM_VirtualThreadUnmount(JNIEnv *env, jobject vthread, jboolean hide)
{
J9VMThread *currentThread = (J9VMThread *)env;
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
Trc_SC_VirtualThreadUnmount_Entry(currentThread, vthread, hide);
vmFuncs->internalEnterVMFromJNI(currentThread);
if (hide) {
virtualThreadUnmountBegin(env, vthread);
} else {
virtualThreadUnmountEnd(env, vthread);
}
vmFuncs->internalExitVMToJNI(currentThread);
Trc_SC_VirtualThreadUnmount_Exit(currentThread, vthread, hide);
}
JNIEXPORT jboolean JNICALL
JVM_IsForeignLinkerSupported()
{
return JNI_TRUE;
}
JNIEXPORT void JNICALL
JVM_VirtualThreadStart(JNIEnv *env, jobject vthread)
{
J9VMThread *currentThread = (J9VMThread *)env;
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
Trc_SC_VirtualThreadStart_Entry(currentThread, vthread);
vmFuncs->internalEnterVMFromJNI(currentThread);
virtualThreadMountEnd(env, vthread);
TRIGGER_J9HOOK_VM_VIRTUAL_THREAD_STARTED(vm->hookInterface, currentThread);
vmFuncs->internalExitVMToJNI(currentThread);
Trc_SC_VirtualThreadStart_Exit(currentThread, vthread);
}
JNIEXPORT void JNICALL
JVM_VirtualThreadEnd(JNIEnv *env, jobject vthread)
{
J9VMThread *currentThread = (J9VMThread *)env;
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
Trc_SC_VirtualThreadEnd_Entry(currentThread, vthread);
vmFuncs->internalEnterVMFromJNI(currentThread);
TRIGGER_J9HOOK_VM_VIRTUAL_THREAD_END(vm->hookInterface, currentThread);
setContinuationStateToLastUnmount((J9VMThread *)env, vthread);
virtualThreadUnmountBegin(env, vthread);
vmFuncs->internalExitVMToJNI(currentThread);
Trc_SC_VirtualThreadEnd_Exit(currentThread, vthread);
}
#endif /* JAVA_SPEC_VERSION >= 21 */
#if defined(J9VM_OPT_VALHALLA_VALUE_TYPES)
JNIEXPORT jboolean JNICALL
JVM_IsValhallaEnabled()
{
return JNI_TRUE;
}
JNIEXPORT jboolean JNICALL
JVM_IsImplicitlyConstructibleClass(JNIEnv *env, jclass cls)
{
assert(!"JVM_IsImplicitlyConstructibleClass unimplemented");
return JNI_FALSE;
}
JNIEXPORT jboolean JNICALL
JVM_IsNullRestrictedArray(JNIEnv *env, jobject obj)
{
assert(!"JVM_IsNullRestrictedArray unimplemented");
return JNI_FALSE;
}
JNIEXPORT jarray JNICALL
JVM_NewNullRestrictedArray(JNIEnv *env, jclass cls, jint length)
{
assert(!"JVM_NewNullRestrictedArray unimplemented");
return NULL;
}
#endif /* defined(J9VM_OPT_VALHALLA_VALUE_TYPES) */
#if JAVA_SPEC_VERSION >= 22
JNIEXPORT void JNICALL
JVM_ExpandStackFrameInfo(JNIEnv *env, jobject object)
{
assert(!"JVM_ExpandStackFrameInfo unimplemented");
}
JNIEXPORT void JNICALL
JVM_VirtualThreadDisableSuspend(
JNIEnv *env,
#if JAVA_SPEC_VERSION >= 23
jclass clz,
#else /* JAVA_SPEC_VERSION >= 23 */
jobject vthread,
#endif /* JAVA_SPEC_VERSION >= 23 */
jboolean enter)
{
/* TODO: Add implementation.
* See https://github.com/eclipse-openj9/openj9/issues/18671 for more details.
*/
}
#endif /* JAVA_SPEC_VERSION >= 22 */
#if JAVA_SPEC_VERSION >= 23
JNIEXPORT jint JNICALL
JVM_GetCDSConfigStatus()
{
/* OpenJ9 does not support CDS, so we return 0 to indicate that there is no CDS config available. */
return 0;
}
#endif /* JAVA_SPEC_VERSION >= 23 */
} /* extern "C" */