/
classsupport.c
1529 lines (1354 loc) · 58.4 KB
/
classsupport.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 "j9.h"
#include "j9cfg.h"
#include "j9protos.h"
#include "j9consts.h"
#include "j2sever.h"
#include "vm_internal.h"
#include "j9cp.h"
#include "ut_j9vm.h"
#include "objhelp.h"
#include "j9vmnls.h"
#include <string.h>
#include "pcstack.h"
#include "rommeth.h"
#include "bcnames.h"
static UDATA classAndLoaderHashFn (void *key, void *userData);
static UDATA classAndLoaderHashEqualFn (void *leftKey, void *rightKey, void *userData);
static J9ContendedLoadTableEntry * contendedLoadTableAddThread (J9VMThread* vmThread, J9ClassLoader* classLoader, U_8* className,
UDATA classNameLength, UDATA status);
static J9ContendedLoadTableEntry*
contendedLoadTableGet(J9JavaVM* vm, J9ClassLoader* classLoader, U_8* className, UDATA classNameLength);
static UDATA
contendedLoadTableRemoveThread(J9VMThread* vmThread, J9ContendedLoadTableEntry *tableEntry, UDATA status);
static J9Class* findPrimitiveArrayClass (J9JavaVM* vm, jchar sigChar);
static VMINLINE J9Class* arbitratedLoadClass(J9VMThread* vmThread, U_8* className, UDATA classNameLength,
J9ClassLoader* classLoader, j9object_t * classNotFoundException);
static J9Class* internalFindArrayClass(J9VMThread* vmThread, J9Module *j9module, UDATA arity, U_8* name, UDATA length, J9ClassLoader* classLoader, UDATA options);
extern J9Method initialStaticMethod;
extern J9Method initialSpecialMethod;
UDATA
totalStaticSlotsForClass( J9ROMClass *romClass ) {
UDATA totalSlots;
totalSlots = romClass->objectStaticCount + romClass->singleScalarStaticCount;
#ifndef J9VM_ENV_DATA64
totalSlots = (totalSlots + 1) & ~(UDATA)1;
#endif
totalSlots += romClass->doubleScalarStaticCount;
#ifndef J9VM_ENV_DATA64
totalSlots += romClass->doubleScalarStaticCount;
#endif
return totalSlots;
}
/**
* @internal
*
* This function should only be called by internalFindClassUTF8()!
*
* Load the array class with the specified name.
* If nameData does not refer to a valid array, return NULL.
* If the array class cannot be loaded, return NULL and set the current exception.
*
* The caller must have VMAccess.
* This function may release VMAccess.
* This function may push objects on the stack. An appropriate stack frame must already be in place.
*/
static J9Class*
internalFindArrayClass(J9VMThread* vmThread, J9Module *j9module, UDATA arity, U_8* name, UDATA length, J9ClassLoader* classLoader, UDATA options)
{
J9JavaVM* vm = vmThread->javaVM;
jchar firstChar = 0;
jchar lastChar = 0;
J9Class* arrayClass = NULL; /* class of array element */
vmThread->privateFlags &= ~J9_PRIVATE_FLAGS_CLOAD_NO_MEM;
if (arity > 255) {
goto done;
}
if (length > arity) {
firstChar = name[arity];
lastChar = name[length-1];
}
if (length - arity == 1) {
arrayClass = findPrimitiveArrayClass(vm, firstChar);
/* the first level of arity is already present in the array class */
arity -= 1;
} else if (firstChar == 'L' && lastChar == ';') {
name += arity + 1; /* 1 for 'L' */
length -= arity + 2; /* 2 for 'L and ';' */
arrayClass = internalFindClassInModule(vmThread, j9module, name, length, classLoader, options);
} else {
goto done;
}
while (arrayClass && arity-- > 0) {
if (arrayClass->arrayClass) {
arrayClass = arrayClass->arrayClass;
} else {
if (options & J9_FINDCLASS_FLAG_EXISTING_ONLY) {
arrayClass = NULL;
} else {
J9ROMArrayClass* arrayOfObjectsROMClass = (J9ROMArrayClass*)J9ROMIMAGEHEADER_FIRSTCLASS(vm->arrayROMClasses);
arrayClass = internalCreateArrayClass(vmThread, arrayOfObjectsROMClass, arrayClass);
}
}
}
done:
return arrayClass;
}
/**
* @internal
*
* Calculate the arity of the array described by nameData.
*
* @return 0 if the name is not an array, or the number of '['s if it is
*
* @note The caller must have VMAccess.
*/
UDATA
calculateArity(J9VMThread* vmThread, U_8* name, UDATA length)
{
U_32 arity = 0;
while ((length > 0) && ('[' == *name)) {
name += 1;
length -= 1;
arity += 1;
}
return arity;
}
static J9Class*
findPrimitiveArrayClass(J9JavaVM* vm, jchar sigChar)
{
switch (sigChar) {
case 'B':
return vm->byteArrayClass;
case 'C':
return vm->charArrayClass;
case 'I':
return vm->intArrayClass;
case 'J':
return vm->longArrayClass;
case 'S':
return vm->shortArrayClass;
case 'Z':
return vm->booleanArrayClass;
#ifdef J9VM_INTERP_FLOAT_SUPPORT
case 'D':
return vm->doubleArrayClass;
case 'F':
return vm->floatArrayClass;
#endif
default:
return NULL;
}
}
/**
* @internal
*
* Allocates and fills in the relevant fields of an array class
*/
J9Class*
internalCreateArrayClass(J9VMThread* vmThread, J9ROMArrayClass* romClass, J9Class* elementClass)
{
J9Class *result = NULL;
j9object_t heapClass = J9VM_J9CLASS_TO_HEAPCLASS(elementClass);
j9object_t protectionDomain = NULL;
J9ROMClass* arrayRomClass = (J9ROMClass*) romClass;
J9JavaVM *const javaVM = vmThread->javaVM;
UDATA options = 0;
BOOLEAN elementInitSuccess = TRUE;
#if defined(J9VM_OPT_VALHALLA_VALUE_TYPES)
/* When creating an array of valuetype elements, the array elements are initialized to the defaultValue of the
* element type. As a result the element type must be fully initialized (if its a valuetype) before creating an
* instance of the array. Element class init must be done before the arrayClass is created so that in the case
* of an init failure the arrayClass is not temporarily exposed.
*/
if (J9_IS_J9CLASS_VALUETYPE(elementClass)) {
UDATA initStatus = elementClass->initializeStatus;
if ((J9ClassInitSucceeded != initStatus) && ((UDATA)vmThread != initStatus)) {
initializeClass(vmThread, elementClass);
if (NULL != vmThread->currentException) {
elementInitSuccess = FALSE;
}
}
}
#endif /* defined(J9VM_OPT_VALHALLA_VALUE_TYPES) */
if (elementInitSuccess) {
if (J9ROMCLASS_IS_HIDDEN(elementClass->romClass)) {
options |= (J9_FINDCLASS_FLAG_HIDDEN | J9_FINDCLASS_FLAG_UNSAFE | J9_FINDCLASS_FLAG_ANON);
if (J9ROMCLASS_IS_OPTIONNESTMATE_SET(elementClass->romClass)) {
options |= J9_FINDCLASS_FLAG_CLASS_OPTION_NESTMATE;
}
if (J9ROMCLASS_IS_OPTIONSTRONG_SET(elementClass->romClass)) {
options |= J9_FINDCLASS_FLAG_CLASS_OPTION_STRONG ;
}
} else if (J9_ARE_ANY_BITS_SET(elementClass->classFlags, J9ClassIsAnonymous)) {
options = J9_FINDCLASS_FLAG_ANON;
}
omrthread_monitor_enter(javaVM->classTableMutex);
if (NULL != heapClass) {
protectionDomain = J9VMJAVALANGCLASS_PROTECTIONDOMAIN(vmThread, heapClass);
}
result = internalCreateRAMClassFromROMClass(
vmThread,
elementClass->classLoader,
arrayRomClass,
options, /* options */
elementClass,
protectionDomain,
NULL,
J9_CP_INDEX_NONE,
LOAD_LOCATION_UNKNOWN,
NULL,
NULL);
}
return result;
}
/**
* Peek the classHashTable to see if the `className` class has already been loaded by `classLoader`.
*
* @param currentThread pointer to the current J9VMThread
* @param classLoader pointer to the J9ClassLoader being probed
* @param className pointer to the U_8 representation of the classaname. Doesn't need to be null terminated
* @param classNameLength length of the className
* @return a J9Class pointer if the class has already been loaded in this loader. Null otherwise.
*/
J9Class*
peekClassHashTable(J9VMThread* currentThread, J9ClassLoader* classLoader, U_8* className, UDATA classNameLength)
{
J9Class * ramClass = NULL;
J9JavaVM* vm = currentThread->javaVM;
BOOLEAN fastMode = J9_ARE_ALL_BITS_SET(vm->extendedRuntimeFlags, J9_EXTENDED_RUNTIME_FAST_CLASS_HASH_TABLE);
/* If -XX:+FastClassHashTable is enabled, do not lock anything to do the initial table peek */
if (!fastMode) {
omrthread_monitor_enter(vm->classTableMutex);
}
ramClass = hashClassTableAt(classLoader, className, classNameLength);
if (!fastMode) {
omrthread_monitor_exit(vm->classTableMutex);
}
return ramClass;
}
J9Class*
internalFindClassString(J9VMThread* currentThread, j9object_t moduleName, j9object_t className, J9ClassLoader* classLoader, UDATA options, UDATA allowedBitsForClassName)
{
J9Class *result = NULL;
J9JavaVM* vm = currentThread->javaVM;
BOOLEAN fastMode = J9_ARE_ALL_BITS_SET(vm->extendedRuntimeFlags, J9_EXTENDED_RUNTIME_FAST_CLASS_HASH_TABLE);
/* If -XX:+FastClassHashTable is enabled, do not lock anything to do the initial table peek */
if (!fastMode) {
omrthread_monitor_enter(vm->classTableMutex);
}
result = hashClassTableAtString(classLoader, (j9object_t) className);
if (!fastMode) {
omrthread_monitor_exit(vm->classTableMutex);
}
if (NULL == result) {
J9Module **findResult = NULL;
J9Module *j9module = NULL;
U_8 localBuf[J9VM_PACKAGE_NAME_BUFFER_LENGTH];
U_8 *utf8Name = NULL;
UDATA utf8Length = 0;
UDATA stringFlags = J9_STR_NULL_TERMINATE_RESULT;
PORT_ACCESS_FROM_JAVAVM(vm);
if (CLASSNAME_INVALID == allowedBitsForClassName) {
stringFlags |= J9_STR_XLAT;
}
utf8Name = (U_8*)copyStringToUTF8WithMemAlloc(currentThread, className, stringFlags, "", 0, (char *)localBuf, J9VM_PACKAGE_NAME_BUFFER_LENGTH, &utf8Length);
if (NULL == utf8Name) {
/* Throw out-of-memory */
setNativeOutOfMemoryError(currentThread, 0, 0);
return NULL;
}
/* Make sure the name is legal */
if ((CLASSNAME_INVALID == allowedBitsForClassName)
|| (CLASSNAME_INVALID != verifyQualifiedName(currentThread, utf8Name, utf8Length, allowedBitsForClassName))
) {
if (NULL != moduleName) {
J9Module module = {0};
J9Module *modulePtr = &module;
modulePtr->moduleName = moduleName;
findResult = hashTableFind(classLoader->moduleHashTable, &modulePtr);
if (NULL != findResult) {
j9module = *findResult;
}
}
result = internalFindClassInModule(currentThread, j9module, utf8Name, utf8Length, classLoader, options);
if (utf8Name != localBuf) {
j9mem_free_memory(utf8Name);
}
}
}
return result;
}
/* Assumes that the constant pool has been zeroed */
void
internalRunPreInitInstructions(J9Class * ramClass, J9VMThread * vmThread)
{
J9JavaVM * vm = vmThread->javaVM;
J9ROMClass * romClass = ramClass->romClass;
UDATA methodTypeIndex = 0;
UDATA ramConstantPoolCount = romClass->ramConstantPoolCount;
if (ramConstantPoolCount != 0) {
J9ConstantPool * ramConstantPool = J9_CP_FROM_CLASS(ramClass);
J9ROMConstantPoolItem * romConstantPool = ramConstantPool->romConstantPool;
U_32 * cpShapeDescription = J9ROMCLASS_CPSHAPEDESCRIPTION(romClass);
J9UTF8 *className = J9ROMCLASS_CLASSNAME(romClass);
UDATA descriptionCount = 0;
U_32 description = 0;
UDATA i;
BOOLEAN isAnonClass = J9_ARE_ANY_BITS_SET(romClass->extraModifiers, J9AccClassAnonClass);
for (i = 0; i < ramConstantPoolCount; ++i) {
if (descriptionCount == 0) {
description = *cpShapeDescription++;
descriptionCount = J9_CP_DESCRIPTIONS_PER_U32;
}
switch(description & J9_CP_DESCRIPTION_MASK) {
J9ROMMethodRef * romMethodRef;
J9ROMNameAndSignature * nas;
case J9CPTYPE_CLASS:
if (isAnonClass) {
/* anonClass RAM constant pool fix up */
J9ROMClassRef* classEntry = ((J9ROMClassRef*) &(romConstantPool[i]));
/* pointer comparison is done here because only one constant pool entry contains the className UTF8 */
if (SRP_GET(classEntry->name, J9UTF8*) == className) {
/* fill in classRef in RAM constantPool */
((J9RAMClassRef *) ramConstantPool)[i].value = ramClass;
}
}
break;
case J9CPTYPE_INT:
case J9CPTYPE_FLOAT:
*((U_32 *) &(((J9RAMSingleSlotConstantRef *) ramConstantPool)[i].value)) = ((J9ROMSingleSlotConstantRef *) romConstantPool)[i].data;
break;
case J9CPTYPE_FIELD:
((J9RAMFieldRef *) ramConstantPool)[i].valueOffset = -1;
break;
case J9CPTYPE_HANDLE_METHOD:
romMethodRef = ((J9ROMMethodRef *) romConstantPool) + i;
nas = J9ROMMETHODREF_NAMEANDSIGNATURE(romMethodRef);
((J9RAMMethodRef *) ramConstantPool)[i].methodIndexAndArgCount = (methodTypeIndex << 8) +
getSendSlotsFromSignature(J9UTF8_DATA(J9ROMNAMEANDSIGNATURE_SIGNATURE(nas)));
methodTypeIndex++;
/* In case MethodHandle.invoke[Exact] is called via invokestatic */
((J9RAMMethodRef *) ramConstantPool)[i].method = vm->initialMethods.initialStaticMethod;
break;
case J9CPTYPE_INSTANCE_METHOD:
case J9CPTYPE_INTERFACE_INSTANCE_METHOD:
romMethodRef = ((J9ROMMethodRef *) romConstantPool) + i;
nas = J9ROMMETHODREF_NAMEANDSIGNATURE(romMethodRef);
((J9RAMMethodRef *) ramConstantPool)[i].methodIndexAndArgCount = (J9VTABLE_INITIAL_VIRTUAL_OFFSET << 8) +
getSendSlotsFromSignature(J9UTF8_DATA(J9ROMNAMEANDSIGNATURE_SIGNATURE(nas)));
((J9RAMMethodRef *) ramConstantPool)[i].method = vm->initialMethods.initialSpecialMethod;
break;
case J9CPTYPE_STATIC_METHOD:
case J9CPTYPE_INTERFACE_STATIC_METHOD:
romMethodRef = ((J9ROMMethodRef *) romConstantPool) + i;
nas = J9ROMMETHODREF_NAMEANDSIGNATURE(romMethodRef);
/* In case this CP entry is shared with invokevirtual */
((J9RAMMethodRef *) ramConstantPool)[i].methodIndexAndArgCount = (J9VTABLE_INITIAL_VIRTUAL_OFFSET << 8) +
getSendSlotsFromSignature(J9UTF8_DATA(J9ROMNAMEANDSIGNATURE_SIGNATURE(nas)));
((J9RAMStaticMethodRef *) ramConstantPool)[i].method = vm->initialMethods.initialStaticMethod;
break;
case J9CPTYPE_INTERFACE_METHOD:
romMethodRef = ((J9ROMMethodRef *) romConstantPool) + i;
nas = J9ROMMETHODREF_NAMEANDSIGNATURE(romMethodRef);
((J9RAMInterfaceMethodRef *) ramConstantPool)[i].methodIndexAndArgCount = J9_ITABLE_INDEX_UNRESOLVED | getSendSlotsFromSignature(J9UTF8_DATA(J9ROMNAMEANDSIGNATURE_SIGNATURE(nas)));
break;
case J9CPTYPE_METHOD_TYPE:
{
J9ROMMethodTypeRef *romMethodTypeRef = ((J9ROMMethodTypeRef *) romConstantPool) + i;
J9UTF8 *signature = J9ROMMETHODTYPEREF_SIGNATURE(romMethodTypeRef);
((J9RAMMethodTypeRef *) ramConstantPool)[i].type = 0;
((J9RAMMethodTypeRef *) ramConstantPool)[i].slotCount = getSendSlotsFromSignature(J9UTF8_DATA(signature));
break;
}
}
description >>= J9_CP_BITS_PER_DESCRIPTION;
--descriptionCount;
}
}
}
J9Class*
resolveKnownClass(J9JavaVM * vm, UDATA index)
{
J9VMThread * currentThread = currentVMThread(vm);
J9ConstantPool * jclConstantPool = (J9ConstantPool *) vm->jclConstantPool;
J9RAMFieldRef * ramConstantPool = (J9RAMFieldRef *) vm->jclConstantPool;
J9ROMFieldRef * romConstantPool = (J9ROMFieldRef *) jclConstantPool->romConstantPool;
J9ROMClass * jclROMClass = jclConstantPool->ramClass->romClass;
U_32 * cpShapeDescription = J9ROMCLASS_CPSHAPEDESCRIPTION(jclROMClass);
U_32 constPoolCount = jclROMClass->romConstantPoolCount;
U_32 i;
J9Class * clazz;
Trc_VM_resolveKnownClass_Entry(currentThread, index);
clazz = resolveClassRef(currentThread, jclConstantPool, index, J9_RESOLVE_FLAG_RUNTIME_RESOLVE);
if (NULL != clazz) {
/* Forcibly resolve all instance field refs in the JCL constant pool that belong to the resolved class. */
for (i = 0; i < constPoolCount; i++) {
if ((J9CPTYPE_FIELD == J9_CP_TYPE(cpShapeDescription, i))
&& (index == romConstantPool[i].classRefCPIndex)) {
if (-1 == resolveInstanceFieldRef(currentThread, NULL, jclConstantPool, i, J9_RESOLVE_FLAG_RUNTIME_RESOLVE | J9_RESOLVE_FLAG_NO_THROW_ON_FAIL, NULL)) {
Trc_VM_resolveKnownClass_resolveInstanceFieldRef_Failed(currentThread, i, index, clazz);
} else
Trc_VM_resolveKnownClass_resolvedInstanceFieldRef(currentThread, i, index, clazz, ramConstantPool[i].valueOffset);
}
}
}
Trc_VM_resolveKnownClass_Exit(currentThread, clazz);
return clazz;
}
/**
* create RAM class from ROM class
* @param vmThread Current VM thread
* @param classLoader Class loader object
* @param options
* @param romClass pointer to romClass struct for the class
* @param errorRomClassP pointer to the pointer to the error rom class struct
* @param entryIndex class path or patch path index
* @return RAM class, or NULL if ROM class is errored
*
* Precondition: class table mutex locked
* Postcondition: class table mutex unlocked (NB)
*/
J9Class *foundROMClass(J9VMThread* vmThread, J9ClassLoader* classLoader, UDATA options, J9ROMClass *romClass, J9ROMClass **errorRomClassP, UDATA entryIndex, I_32 locationType) {
J9Class *foundClass = NULL;
/* check to see if this is an errored ROM class */
*errorRomClassP = NULL;
if (NULL == checkRomClassForError(romClass, vmThread)) {
*errorRomClassP = romClass;
omrthread_monitor_exit(vmThread->javaVM->classTableMutex);
} else {
/* this function unlocks the class table mutex */
foundClass = internalCreateRAMClassFromROMClass(vmThread,
classLoader, romClass, options, NULL, NULL, NULL, entryIndex, locationType, NULL, NULL);
}
return foundClass;
}
/**
* Give the hook interface a go at loading the class and call the thread's findLocallyDefinedClass
* @param vmThread Current VM thread
* @param j9module Pointer to J9Module in which the class is to be searched
* @param className Name of the class to load
* @param classNameLength Length of the class name
* @param classLoader Class loader object
* @param options
* @param [in/out] localBuffer contains values for entryIndex, loadLocationType and cpEntryUsed. This pointer can't be NULL.
* @return pointer to class if locally defined, 0 if not defined, -1 if we cannot do dynamic load
*
* Preconditions: none
* Postconditions: class segment mutex locked if dynamicLoadBuffers is null (this is likely a bug)
*
*/
static IDATA
callFindLocallyDefinedClass(J9VMThread* vmThread, J9Module *j9module, U_8* className, UDATA classNameLength, J9ClassLoader* classLoader, UDATA options, J9TranslationLocalBuffer *localBuffer) {
IDATA findResult = -1;
J9TranslationBufferSet *dynamicLoadBuffers = vmThread->javaVM->dynamicLoadBuffers;
J9ROMClass* returnVal = NULL;
IDATA classFound = 0;
IDATA* returnPointer = (IDATA*) &returnVal;
/* localBuffer should not be NULL */
Assert_VM_true(NULL != localBuffer);
if (NULL != dynamicLoadBuffers) {
J9ClassPathEntry* classPathEntries = NULL;
if (classLoader == vmThread->javaVM->systemClassLoader) {
classPathEntries = classLoader->classPathEntries;
}
TRIGGER_J9HOOK_VM_FIND_LOCALLY_DEFINED_CLASS(vmThread->javaVM->hookInterface, vmThread, classLoader, j9module, (char*)className, classNameLength,
classPathEntries, classLoader->classPathEntryCount, -1, NULL, 0, 0,
(IDATA *) &localBuffer->entryIndex, returnVal);
findResult = (IDATA) returnVal;
if (0 == findResult) {
TRIGGER_J9HOOK_VM_FIND_LOCALLY_DEFINED_CLASS_FROM_FILESYSTEM(vmThread->javaVM->hookInterface,
vmThread,
(const char *)className,
classNameLength,
classLoader,
&classFound,
returnPointer);
if (returnVal != NULL) {
findResult = classFound;
} else {
findResult = dynamicLoadBuffers->findLocallyDefinedClassFunction(vmThread, j9module, className, (U_32)classNameLength, classLoader, classPathEntries,
classLoader->classPathEntryCount, options, localBuffer);
if ((-1 == findResult) && (J9_PRIVATE_FLAGS_REPORT_ERROR_LOADING_CLASS & vmThread->privateFlags)) {
vmThread->privateFlags |= J9_PRIVATE_FLAGS_FAILED_LOADING_REQUIRED_CLASS;
}
}
} else {
/* The class is found in shared class cache. */
if (J2SE_VERSION(vmThread->javaVM) >= J2SE_V11) {
if (localBuffer->entryIndex >= 0) {
localBuffer->loadLocationType = LOAD_LOCATION_CLASSPATH;
} else {
localBuffer->loadLocationType = LOAD_LOCATION_MODULE;
}
} else {
localBuffer->loadLocationType = LOAD_LOCATION_CLASSPATH;
}
}
}
return findResult;
}
/**
* Attempt to perform dynamic load
* @param vmThread Current VM thread
* @param j9module Pointer to J9Module in which the class is to be searched
* @param className Name of the class to load
* @param classNameLength Length of the class name
* @param classLoader Class loader object
* @param options Options for defineClass
* @return loaded class object if success, NULL if fail
*
* Precondition: class table mutex locked
* Postcondition: class table mutex unlocked (NB)
*/
static J9Class*
attemptDynamicClassLoad(J9VMThread* vmThread, J9Module *j9module, U_8* className, UDATA classNameLength, J9ClassLoader* classLoader, UDATA options)
{
J9Class *foundClass = NULL;
Trc_VM_internalFindClass_attemptDynamicClassLoad_entry(vmThread, classLoader->classLoaderObject, classNameLength, className);
/* try to load classes from system class loader */
if ((J2SE_VERSION(vmThread->javaVM) >= J2SE_V11)
|| ((NULL != classLoader->classPathEntries) && (classLoader == vmThread->javaVM->systemClassLoader))
) {
IDATA findResult = -1;
J9TranslationLocalBuffer localBuffer = {J9_CP_INDEX_NONE, LOAD_LOCATION_UNKNOWN, NULL};
findResult = callFindLocallyDefinedClass(vmThread, j9module, className, classNameLength, classLoader, options, &localBuffer);
if (-1 != findResult) {
J9TranslationBufferSet *dynamicLoadBuffers = vmThread->javaVM->dynamicLoadBuffers;
if (0 == findResult) {
/* Loaded a class from disk. Need to convert it into internal representation */
if (NULL == dynamicLoadBuffers) {
setCurrentExceptionUTF(vmThread, J9VMCONSTANTPOOL_JAVALANGNOCLASSDEFFOUNDERROR, "dynamic loader is unavailable");
} else {
UDATA defineClassOptions = options & ~J9_FINDCLASS_FLAG_USE_LOADER_CP_ENTRIES; /* allow define class to use normal findClass */
/* this function exits the class table mutex */
foundClass = dynamicLoadBuffers->internalDefineClassFunction(vmThread, className, classNameLength,
dynamicLoadBuffers->sunClassFileBuffer, dynamicLoadBuffers->currentSunClassFileSize,
NULL, classLoader, NULL, defineClassOptions, NULL, NULL, &localBuffer); /* this function exits the class table mutex */
}
} else {
/* Dynamically loaded a ROM class.
* An existing ROMClass is found in the shared class cache.
* If -Xshareclasses:enableBCI is present, need to give VM a chance to trigger ClassFileLoadHook event.
*/
J9ROMClass *romClass = (J9ROMClass *) findResult;
if ((NULL != vmThread->javaVM->sharedClassConfig) && (0 != vmThread->javaVM->sharedClassConfig->isBCIEnabled(vmThread->javaVM))) {
UDATA defineClassOptions = options & ~J9_FINDCLASS_FLAG_USE_LOADER_CP_ENTRIES; /* allow define class to use normal findClass */
defineClassOptions |= J9_FINDCLASS_FLAG_SHRC_ROMCLASS_EXISTS;
foundClass = dynamicLoadBuffers->internalDefineClassFunction(vmThread, className, classNameLength,
J9ROMCLASS_INTERMEDIATECLASSDATA(romClass), romClass->intermediateClassDataLength,
NULL, classLoader, NULL, defineClassOptions, romClass, NULL, &localBuffer);
} else {
J9ROMClass *errorRomClass;
/* this function exits the class table mutex */
foundClass = foundROMClass(vmThread, classLoader, options, romClass, &errorRomClass, localBuffer.entryIndex, localBuffer.loadLocationType);
if (NULL != errorRomClass) {
if (0 != (J9_FINDCLASS_FLAG_THROW_ON_FAIL & options)) {
setExceptionForErroredRomClass(errorRomClass, vmThread);
}
}
/* class table mutex is now unlocked */
}
}
} else {
omrthread_monitor_exit(vmThread->javaVM->classTableMutex);
}
} else {
omrthread_monitor_exit(vmThread->javaVM->classTableMutex);
}
Trc_VM_internalFindClass_attemptDynamicClassLoad_exit(vmThread, foundClass, &foundClass);
return foundClass;
}
/**
* Call the class loader to get the specified class.
* Throw ClassNotFound exception if load failed
* @param vmThread Current VM thread
* @param className Name of the class to load
* @param classNameLength Length of the class name
* @param classLoader Class loader object
* @param classNotFoundException Pointer to store the ClassNotFoundException into if one is cleared
* @return loaded class object if success, NULL if fail
* If the class is not found, the exception is cleared and NULL returned.
* If a different exception occurs, that exception is retained and NULL returned
*
* Precondition: no mutexes locked, vm access, loader object locked
* Postcondition: no mutexes locked, vm access, loader object locked
*/
static VMINLINE J9Class*
callLoadClass(J9VMThread* vmThread, U_8* className, UDATA classNameLength, J9ClassLoader* classLoader, j9object_t * classNotFoundException)
{
j9object_t classNameString, sendLoadClassResult;
J9Class *foundClass = NULL;
Assert_VM_mustHaveVMAccess(vmThread);
classNameString = vmThread->javaVM->memoryManagerFunctions->j9gc_createJavaLangString(vmThread, className, classNameLength, J9_STR_XLAT);
if (classNameString != NULL) {
J9JavaVM * vm = vmThread->javaVM;
Trc_VM_internalFindClass_sendLoadClass(vmThread, classNameLength, className, classNameString, classLoader->classLoaderObject);
sendLoadClass(vmThread, classLoader->classLoaderObject, classNameString);
sendLoadClassResult = (j9object_t) vmThread->returnValue;
if (NULL == sendLoadClassResult) {
j9object_t exception;
exception = vmThread->currentException;
vmThread->currentException = NULL;
if (exception != NULL) {
J9Class* notFound;
/*
* if the load failed, see if it raised a ClassNotFound exception. If so,
* clear the exception and let the caller handle it (map to NoClassDefFound)
* copy the exception to a special stack frame to prevent it from being lost in GCs
*/
Trc_VM_callLoadClass_foundClass_Null(vmThread);
PUSH_OBJECT_IN_SPECIAL_FRAME(vmThread, exception);
notFound = internalFindKnownClass(vmThread, J9VMCONSTANTPOOL_JAVALANGCLASSNOTFOUNDEXCEPTION, J9_FINDKNOWNCLASS_FLAG_EXISTING_ONLY);
exception = POP_OBJECT_IN_SPECIAL_FRAME(vmThread);
vmThread->currentException = exception;
/* Test if the current exception is a ClassNotFound exception or subclass thereof */
if (notFound != NULL) {
J9Class *exceptionClass;
exceptionClass = J9OBJECT_CLAZZ(vmThread, exception);
if (isSameOrSuperClassOf(notFound, exceptionClass)) {
*classNotFoundException = exception;
vmThread->currentException = NULL;
vmThread->privateFlags &= ~J9_PRIVATE_FLAGS_REPORT_EXCEPTION_THROW;
}
}
}
} else { /* sendLoadClassResult is not NULL */
J9Class * ramClass;
J9UTF8* foundClassName;
Trc_VM_internalFindClass_sentLoadClass(vmThread, classNameLength, className, sendLoadClassResult);
Assert_VM_true(J9VM_IS_INITIALIZED_HEAPCLASS(vmThread, sendLoadClassResult));
foundClass = J9VM_J9CLASS_FROM_HEAPCLASS(vmThread, sendLoadClassResult);
omrthread_monitor_enter(vm->classTableMutex);
/* Verify that the actual name matches the expected */
foundClassName = J9ROMCLASS_CLASSNAME(foundClass->romClass);
if (!J9UTF8_DATA_EQUALS(className, classNameLength, J9UTF8_DATA(foundClassName), J9UTF8_LENGTH(foundClassName))) {
/* force failure */
foundClass = NULL;
} else {
/* See if a class of this name is already in the table - if not, add it */
ramClass = hashClassTableAt(classLoader, className, classNameLength);
if (NULL == ramClass) {
/* Ensure loading constraints have not been violated */
if (vm->runtimeFlags & J9_RUNTIME_VERIFY) {
J9Class * loadingConstraintError = j9bcv_satisfyClassLoadingConstraint(vmThread, classLoader, foundClass);
if (loadingConstraintError != NULL) {
omrthread_monitor_exit(vm->classTableMutex);
setClassLoadingConstraintError(vmThread, classLoader, loadingConstraintError);
return NULL;
}
}
/* Add the found class to our class table since we are an initiating loader */
if (hashClassTableAtPut(vmThread, classLoader, className, classNameLength, foundClass)) {
/* Failed to store the class - GC and retry */
omrthread_monitor_exit(vm->classTableMutex);
vm->memoryManagerFunctions->j9gc_modron_global_collect_with_overrides(vmThread, J9MMCONSTANT_EXPLICIT_GC_NATIVE_OUT_OF_MEMORY);
omrthread_monitor_enter(vm->classTableMutex);
/* See if a class of this name is already in the table - if not, try the add again */
ramClass = hashClassTableAt(classLoader, className, classNameLength);
if (NULL == ramClass) {
if (hashClassTableAtPut(vmThread, classLoader, className, classNameLength, foundClass)) {
/* Add failed again, throw native OOM */
omrthread_monitor_exit(vm->classTableMutex);
setNativeOutOfMemoryError(vmThread, 0, 0);
return NULL;
}
} else {
/* If the existing class in the table is different than the value from loadClass, fail */
if (ramClass != foundClass) {
foundClass = NULL;
}
}
}
} else {
/* If the existing class in the table is different than the value from loadClass, fail */
if (ramClass != foundClass) {
foundClass = NULL;
}
}
}
omrthread_monitor_exit(vm->classTableMutex);
}
} else {
foundClass = NULL;
}
return foundClass;
}
/**
* Waits for another thread to load the same class (using the same classloader).
* This is called if there is a classloading contention.
* @param vmStruct Current VM thread
* @param tableEntry: contains name of the class to load (see below), length of the class name
* and class loader object
* @param className: pointer to this thread's copy of the classname string
* @param classNameLength: length of the classname string
*
* @return loaded class object if success, NULL if fail
*
* Precondition: classTableMutex locked, vm access
* Postcondition: classTableMutex locked, vm access
* The className pointer becomes null when the loading thread completes the load.
*/
static J9Class*
waitForContendedLoadClass(J9VMThread* vmThread, J9ContendedLoadTableEntry *tableEntry, U_8* className, UDATA classNameLength)
{
UDATA recursionCount = 0;
UDATA i;
J9Class* foundClass = NULL;
J9VMThread* monitorOwner = NULL;
UDATA status = CLASSLOADING_DUMMY;
Trc_VM_waitForContendedLoadClass_getObjectMonitorOwner(vmThread, vmThread, tableEntry->classLoader, classNameLength, className);
Assert_VM_mustHaveVMAccess(vmThread);
/* get here if and only if someone else is loading the class */
/* give up the classloader monitor to allow other threads to run */
monitorOwner = getObjectMonitorOwner(vmThread->javaVM, tableEntry->classLoader->classLoaderObject, &recursionCount);
if (monitorOwner == vmThread) {
Trc_VM_waitForContendedLoadClass_release_object_monitor(vmThread, vmThread, tableEntry->classLoader, classNameLength, className);
for (i = 0; i < recursionCount; ++i) {
objectMonitorExit(vmThread, tableEntry->classLoader->classLoaderObject);
}
} else {
recursionCount = 0;
}
internalReleaseVMAccess(vmThread);
do {
omrthread_monitor_wait(vmThread->javaVM->classTableMutex);
} while ((status = tableEntry->status) == CLASSLOADING_LOAD_IN_PROGRESS);
/* still have classTableMutex here */
Trc_VM_waitForContendedLoadClass_waited(vmThread, vmThread, tableEntry->classLoader, classNameLength, className, status);
foundClass = hashClassTableAt(tableEntry->classLoader, className, classNameLength);
omrthread_monitor_exit(vmThread->javaVM->classTableMutex);
internalAcquireVMAccess(vmThread);
Trc_VM_waitForContendedLoadClass_acquired_vm_access(vmThread, vmThread, tableEntry->classLoader, classNameLength, className);
for (i = 0; i < recursionCount; ++i) {
objectMonitorEnter(vmThread, tableEntry->classLoader->classLoaderObject);
}
Assert_VM_mustHaveVMAccess(vmThread);
omrthread_monitor_enter(vmThread->javaVM->classTableMutex);
return foundClass;
}
/**
* Test if another thread is already loading the class. If not, load it, otherwise wait for it to be loaded.
* Disable serialization if the classloader has registered as parallel capable.
* @param vmStruct Current VM thread
* @param className Name of the class to load
* @param classNameLength Length of the class name
* @param classLoader Class loader object
* @param classNotFoundException Pointer to store the ClassNotFoundException into if one is cleared
*
* @return loaded class object if success, NULL if fail
*
* Precondition: classTableMutex locked, vm access
* Postcondition: classTableMutex locked, vm access
*
* Locking order:
* get classLoader mutex
* get VM Access
* get classTable mutex
*/
static VMINLINE J9Class*
arbitratedLoadClass(J9VMThread* vmThread, U_8* className, UDATA classNameLength, J9ClassLoader* classLoader, j9object_t * classNotFoundException)
{
J9Class* foundClass;
Assert_VM_mustHaveVMAccess(vmThread);
Trc_VM_arbitratedLoadClass(vmThread, vmThread, classLoader, classNameLength, className);
*classNotFoundException = NULL;
if ((NULL == vmThread->javaVM->contendedLoadTable) /* allow contended classloading in all loaders*/
|| (J9CLASSLOADER_PARALLEL_CAPABLE == (J9CLASSLOADER_PARALLEL_CAPABLE & classLoader->flags)) /* this loader is parallel capable */
) {
/* start old algorithm */
omrthread_monitor_exit(vmThread->javaVM->classTableMutex);
foundClass = callLoadClass(vmThread, className, classNameLength, classLoader, classNotFoundException);
omrthread_monitor_enter(vmThread->javaVM->classTableMutex);
/* end old algorithm */
} else {
J9ContendedLoadTableEntry *tableEntry = contendedLoadTableAddThread(vmThread, classLoader, className, classNameLength, CLASSLOADING_LOAD_IN_PROGRESS);
U_8 done = 1;
UDATA count = 0;
do {
if (tableEntry->thread == vmThread) {
/* get here if and only if I am the first to try loading the class or if I am retrying because the first guy failed. */
omrthread_monitor_exit(vmThread->javaVM->classTableMutex);
Trc_VM_arbitratedLoadClass_callLoadClass(vmThread, vmThread, classLoader, classNameLength, className);
foundClass = callLoadClass(vmThread, className, classNameLength, tableEntry->classLoader, classNotFoundException);
omrthread_monitor_enter(vmThread->javaVM->classTableMutex);
done = 1;
} else {
foundClass = waitForContendedLoadClass(vmThread, tableEntry, className, classNameLength);
if (NULL == foundClass) {
done = 0;
if (NULL == tableEntry->thread) { /* nobody else owns the record, so I'll take it */
tableEntry->thread = vmThread;
tableEntry->status = CLASSLOADING_LOAD_IN_PROGRESS;
/* The original owner's name and length are no longer valid */
tableEntry->className = className;
tableEntry->classNameLength = classNameLength;
}
Trc_VM_arbitratedLoadClass_retry(vmThread, vmThread, classLoader, classNameLength, className);
}
}
} while (!done);
count = contendedLoadTableRemoveThread(vmThread, tableEntry,
(NULL == foundClass) ? CLASSLOADING_LOAD_FAILED: CLASSLOADING_LOAD_SUCCEEDED);
if (count > 0) { /* only the owner(s) should do the notification */
if (tableEntry->thread == vmThread) {
tableEntry->thread = NULL; /* indicate that nobody owns this entry */
omrthread_monitor_notify_all(vmThread->javaVM->classTableMutex);
Trc_VM_arbitratedLoadClass_notify(vmThread, vmThread, tableEntry->classLoader, classNameLength, className);
/* May awaken threads waiting for other [className, classLoader] values */
}
}
}
Assert_VM_mustHaveVMAccess(vmThread);
Trc_VM_arbitratedLoadClass_exit(vmThread, vmThread, classLoader, classNameLength, className, foundClass);
return foundClass;
}
/**
* Load non-array class.
* @param vmThread Current VM thread
* @param j9module Pointer to J9Module in which the class is to be searched
* @param className Name of the class to load
* @param classNameLength Length of the class name
* @param classLoader Class loader object
* @param options load options such as J9_FINDCLASS_FLAG_EXISTING_ONLY
* @param exception Pointer to storage for loading exception
* @return loaded class object if success, NULL if fail
*
* Precondition: either J9_FINDCLASS_FLAG_EXISTING_ONLY set or classTableMutex not owned by this thread; this thread has vm access
* Postcondition: either J9_FINDCLASS_FLAG_EXISTING_ONLY set or classTableMutex not owned by this thread; this thread has vm access
*/
static VMINLINE J9Class *
loadNonArrayClass(J9VMThread* vmThread, J9Module *j9module, U_8* className, UDATA classNameLength, J9ClassLoader* classLoader, UDATA options, j9object_t *exception)
{
J9JavaVM * const vm = vmThread->javaVM;
J9Class * foundClass = NULL;
BOOLEAN lockLoaderMonitor = FALSE;
BOOLEAN fastMode = J9_ARE_ALL_BITS_SET(vm->extendedRuntimeFlags, J9_EXTENDED_RUNTIME_FAST_CLASS_HASH_TABLE);
BOOLEAN loaderMonitorLocked = FALSE;
vmThread->privateFlags &= ~J9_PRIVATE_FLAGS_CLOAD_NO_MEM;
if (J9CLASSLOADER_PARALLEL_CAPABLE == (J9CLASSLOADER_PARALLEL_CAPABLE & classLoader->flags)) {
Trc_VM_loadNonArrayClass_parallelCapable(vmThread, classNameLength, className, classLoader);
} else {
/*
* Test in decreasing probability of failure.
* J9_EXTENDED_RUNTIME_CLASSLOADER_LOCKING_ENABLED is true by default
*/
lockLoaderMonitor = (classLoader != vm->systemClassLoader)
&& (NULL != classLoader->classLoaderObject)
&& (0 == (options & J9_FINDCLASS_FLAG_EXISTING_ONLY))
&& (J9_EXTENDED_RUNTIME_CLASSLOADER_LOCKING_ENABLED ==
(vm->extendedRuntimeFlags & J9_EXTENDED_RUNTIME_CLASSLOADER_LOCKING_ENABLED));
}
/* If -XX:+FastClassHashTable is enabled, do not lock anything to do the initial table peek */
if (!fastMode) {
/* Match RI behaviour by implicitly locking the classloader */
if (lockLoaderMonitor) {
/* Must lock the classloader before the classTableMutex, otherwise we deadlock */
Assert_VM_mustNotOwnMonitor(vm->classTableMutex);
Trc_VM_loadNonArrayClass_enter_object_monitor(vmThread, classLoader, classNameLength, className);