-
Notifications
You must be signed in to change notification settings - Fork 722
/
createramclass.cpp
4081 lines (3698 loc) · 173 KB
/
createramclass.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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright IBM Corp. and others 1991
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include <stdlib.h>
#include "j9.h"
#include "j9protos.h"
#include "j9consts.h"
#include "omrgcconsts.h"
#include "rommeth.h"
/* include stackwalk.h to get value of J9SW_NEEDS_JIT_2_INTERP_THUNKS */
#include "stackwalk.h"
#include "objhelp.h"
#include "../util/ut_module.h"
#undef UT_MODULE_LOADED
#undef UT_MODULE_UNLOADED
#include "ut_j9vm.h"
#include "j9bcvnls.h"
#include "j9vmnls.h"
#include "j2sever.h"
#include "vm_internal.h"
#include "VMHelpers.hpp"
#undef J9VM_TRACE_VTABLE_ACCESS
extern "C" {
#define VTABLE_SLOT_TAG_MASK 3
#define ROM_METHOD_ID_TAG 1
#define DEFAULT_CONFLICT_METHOD_ID_TAG 2
#define EQUIVALENT_SET_ID_TAG 3
#define INTERFACE_TAG 1
#define LOCAL_INTERFACE_ARRAY_SIZE 10
#define DEFAULT_NUMBER_OF_ENTRIES_IN_FLATTENED_CLASS_CACHE 8
enum J9ClassFragments {
RAM_CLASS_HEADER_FRAGMENT,
RAM_METHODS_FRAGMENT,
RAM_SUPERCLASSES_FRAGMENT,
RAM_INSTANCE_DESCRIPTION_FRAGMENT,
RAM_ITABLE_FRAGMENT,
RAM_STATICS_FRAGMENT,
RAM_CONSTANT_POOL_FRAGMENT,
RAM_CALL_SITES_FRAGMENT,
#if defined(J9VM_OPT_OPENJDK_METHODHANDLE)
RAM_INVOKE_CACHE_FRAGMENT,
#else /* defined(J9VM_OPT_OPENJDK_METHODHANDLE) */
RAM_METHOD_TYPES_FRAGMENT,
RAM_VARHANDLE_METHOD_TYPES_FRAGMENT,
#endif /* defined(J9VM_OPT_OPENJDK_METHODHANDLE) */
RAM_STATIC_SPLIT_TABLE_FRAGMENT,
RAM_SPECIAL_SPLIT_TABLE_FRAGMENT,
#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES)
RAM_CLASS_FLATTENED_CLASS_CACHE,
#endif /* J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES */
RAM_CLASS_FRAGMENT_COUNT
};
/*
* RAM_CLASS_FRAGMENT_LIMIT is the inclusive lower bound of "large" RAM class free blocks. Its value
* is the largest alignment constraint amongst RAM class fragments: J9_REQUIRED_CLASS_ALIGNMENT.
* RAM_CLASS_SMALL_FRAGMENT_LIMIT is the inclusive lower bound of "small" RAM class free blocks.
* Smaller blocks are placed on the "tiny" free block list. Its value is chosen experimentally to
* provide optimal performance on the shared classes class loading benchmark.
*/
#define RAM_CLASS_FRAGMENT_LIMIT J9_REQUIRED_CLASS_ALIGNMENT
#define RAM_CLASS_SMALL_FRAGMENT_LIMIT ((sizeof(UDATA) + 4) * sizeof(UDATA))
/* align method table start such that CP is correctly aligned */
#if defined(J9VM_INTERP_NATIVE_SUPPORT)
/* 3 bits of tag needed in J9Method->constantPool */
#if defined(J9VM_JIT_DYNAMIC_LOOP_TRANSFER)
/* 4 bits of tag needed in J9Method->constantPool */
#define REQUIRED_CONSTANT_POOL_ALIGNMENT 16
#endif
#endif
#if !defined(REQUIRED_CONSTANT_POOL_ALIGNMENT)
#define REQUIRED_CONSTANT_POOL_ALIGNMENT 8
#endif
/* Static J9ITable used as a non-NULL iTable cache value by classes that don't implement any interfaces. Defined in hshelp.c. */
extern const J9ITable invalidITable;
typedef struct J9RAMClassFreeListLargeBlock {
UDATA size;
struct J9RAMClassFreeListLargeBlock* nextFreeListBlock;
UDATA maxSizeInList;
} J9RAMClassFreeListLargeBlock;
typedef struct RAMClassAllocationRequest {
UDATA prefixSize;
UDATA alignment;
UDATA alignedSize;
UDATA *address;
UDATA index;
UDATA fragmentSize;
struct RAMClassAllocationRequest *next;
} RAMClassAllocationRequest;
typedef struct J9CreateRAMClassState {
J9Class *ramClass;
j9object_t classObject;
BOOLEAN retry;
J9Class *classBeingRedefined;
IDATA entryIndex;
I_32 locationType;
#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES)
UDATA valueTypeFlags;
#endif /* J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES */
} J9CreateRAMClassState;
typedef struct J9EquivalentEntry {
J9Method *method;
struct J9EquivalentEntry *next;
} J9EquivalentEntry;
typedef struct J9OverrideErrorData {
J9ClassLoader *loader1;
J9UTF8 *class1NameUTF;
J9ClassLoader *loader2;
J9UTF8 *class2NameUTF;
J9UTF8 *exceptionClassNameUTF;
J9UTF8 *methodNameUTF;
J9UTF8 *methodSigUTF;
} J9OverrideErrorData;
static J9Class* markInterfaces(J9ROMClass *romClass, J9Class *superclass, J9ClassLoader *classLoader, BOOLEAN *foundCloneable, UDATA *markedInterfaceCount, UDATA *inheritedInterfaceCount, IDATA *maxInterfaceDepth);
static void unmarkInterfaces(J9Class *interfaceHead);
static void createITable(J9VMThread* vmStruct, J9Class *ramClass, J9Class *interfaceClass, J9ITable ***previousLink, UDATA **currentSlot, UDATA depth);
static UDATA* initializeRAMClassITable(J9VMThread* vmStruct, J9Class *ramClass, J9Class *superclass, UDATA* currentSlot, J9Class *interfaceHead, IDATA maxInterfaceDepth);
static UDATA addInterfaceMethods(J9VMThread *vmStruct, J9ClassLoader *classLoader, J9Class *interfaceClass, UDATA vTableMethodCount, UDATA *vTableAddress, J9Class *superclass, J9ROMClass *romClass, UDATA *defaultConflictCount, J9Pool *equivalentSets, UDATA *equivSetCount, J9OverrideErrorData *errorData);
static UDATA* computeVTable(J9VMThread *vmStruct, J9ClassLoader *classLoader, J9Class *superclass, J9ROMClass *taggedClass, UDATA packageID, J9ROMMethod ** methodRemapArray, J9Class *interfaceHead, UDATA *defaultConflictCount, UDATA interfaceCount, UDATA inheritedInterfaceCount, J9OverrideErrorData *errorData);
static void copyVTable(J9VMThread *vmStruct, J9Class *ramClass, J9Class *superclass, UDATA *vTable, UDATA defaultConflictCount);
static UDATA processVTableMethod(J9VMThread *vmThread, J9ClassLoader *classLoader, UDATA *vTableAddress, J9Class *superclass, J9ROMClass *romClass, J9ROMMethod *romMethod, UDATA localPackageID, UDATA vTableMethodCount, void *storeValue, J9OverrideErrorData *errorData);
static VMINLINE UDATA growNewVTableSlot(UDATA *vTableAddress, UDATA vTableMethodCount, void *storeValue);
static UDATA getVTableIndexForNameAndSigStartingAt(UDATA *vTable, J9UTF8 *name, J9UTF8 *signature, UDATA vTableIndex);
static UDATA checkPackageAccess(J9VMThread *vmThread, J9Class *foundClass, UDATA classPreloadFlags);
static void setCurrentExceptionForBadClass(J9VMThread *vmThread, J9UTF8 *badClassName, UDATA exceptionIndex, U_32 nlsModuleName, U_32 nlsMessageID);
static BOOLEAN verifyClassLoadingStack(J9VMThread *vmThread, J9ClassLoader *classLoader, J9ROMClass *romClass);
static void popFromClassLoadingStack(J9VMThread *vmThread);
static VMINLINE BOOLEAN loadSuperClassAndInterfaces(J9VMThread *vmThread, J9ClassLoader *classLoader, J9ROMClass *romClass, UDATA options, J9Class *elementClass, BOOLEAN hotswapping, UDATA classPreloadFlags, J9Class **superclassOut, J9Module *module);
static VMINLINE BOOLEAN checkSuperClassAndInterfaces(J9VMThread *vmThread, J9ClassLoader *classLoader, J9ROMClass *romClass, UDATA options, UDATA packageID, BOOLEAN hotswapping, J9Class *superclass, J9Module *module, J9ROMClass **badClassOut, bool *incompatibleOut);
static J9Class* internalCreateRAMClassDropAndReturn(J9VMThread *vmThread, J9ROMClass *romClass, J9CreateRAMClassState *state);
static J9Class* internalCreateRAMClassDoneNoMutex(J9VMThread *vmThread, J9ROMClass *romClass, UDATA options, J9CreateRAMClassState *state);
static J9Class* internalCreateRAMClassDone(J9VMThread *vmThread, J9ClassLoader *classLoader, J9ClassLoader *hostClassLoader, J9ROMClass *romClass, UDATA options, J9Class *elementClass,
J9UTF8 *className, J9CreateRAMClassState *state, J9Class *superclass, J9MemorySegment *segment);
#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES)
static BOOLEAN loadFlattenableFieldValueClasses(J9VMThread *vmThread, J9ClassLoader *classLoader, J9ROMClass *romClass, UDATA classPreloadFlags, J9Module *module, UDATA *valueTypeFlags, J9FlattenedClassCache *flattenedClassCache, J9Class *superClazz);
static BOOLEAN checkFlattenableFieldValueClasses(J9VMThread *currentThread, J9ClassLoader *classLoader, J9ROMClass *romClass, UDATA packageID, J9Module *module, J9ROMClass **badClassOut);
static J9Class* internalCreateRAMClassFromROMClassImpl(J9VMThread *vmThread, J9ClassLoader *classLoader, J9ROMClass *romClass, UDATA options, J9Class *elementClass,
J9ROMMethod **methodRemapArray, IDATA entryIndex, I_32 locationType, J9Class *classBeingRedefined, J9Class *superclass, J9CreateRAMClassState *state,
J9ClassLoader* hostClassLoader, J9Class *hostClass, J9Module *module, J9FlattenedClassCache *flattenedClassCache, UDATA *valueTypeFlags);
#else /* J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES */
static J9Class* internalCreateRAMClassFromROMClassImpl(J9VMThread *vmThread, J9ClassLoader *classLoader, J9ROMClass *romClass, UDATA options, J9Class *elementClass,
J9ROMMethod **methodRemapArray, IDATA entryIndex, I_32 locationType, J9Class *classBeingRedefined, J9Class *superclass, J9CreateRAMClassState *state,
J9ClassLoader* hostClassLoader, J9Class *hostClass, J9Module *module);
#endif /* J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES */
static J9MemorySegment* internalAllocateRAMClass(J9JavaVM *javaVM, J9ClassLoader *classLoader, RAMClassAllocationRequest *allocationRequests, UDATA allocationRequestCount);
static I_32 interfaceDepthCompare(const void *a, const void *b);
#if defined(J9VM_INTERP_CUSTOM_SPIN_OPTIONS)
static void checkForCustomSpinOptions(void *element, void *userData);
#endif /* J9VM_INTERP_CUSTOM_SPIN_OPTIONS */
#if JAVA_SPEC_VERSION >= 11
static void trcModulesSettingPackage(J9VMThread *vmThread, J9Class *ramClass, J9ClassLoader *classLoader, J9UTF8 *className);
#endif /* JAVA_SPEC_VERSION >= 11 */
static void initializeClassLinks(J9Class *ramClass, J9Class *superclass, J9MemorySegment *segment, UDATA options);
/*
* A class which extends (perhaps indirectly) the 'magic'
* accessor class is exempt from the normal access rules.
*/
#if JAVA_SPEC_VERSION == 8
#define MAGIC_ACCESSOR_IMPL "sun/reflect/MagicAccessorImpl"
#else /* JAVA_SPEC_VERSION == 8 */
#define MAGIC_ACCESSOR_IMPL "jdk/internal/reflect/MagicAccessorImpl"
#endif /* JAVA_SPEC_VERSION == 8 */
static VMINLINE J9Class *
getArrayClass(J9Class *elementClass, UDATA options)
{
#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES)
if (J9_ARE_ALL_BITS_SET(options, J9_FINDCLASS_FLAG_CLASS_OPTION_NULL_RESTRICTED_ARRAY)) {
return elementClass->nullRestrictedArrayClass;
} else
#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */
{
return elementClass->arrayClass;
}
}
/**
* Mark all of the interfaces supported by this class, including all interfaces
* inherited by superinterfaces. Unmark all interfaces which are inherited from
* the superclass.
*
* Returns the head of a linked list of interfaces (through the tagged instanceDescription field).
*/
static J9Class *
markInterfaces(J9ROMClass *romClass, J9Class *superclass, J9ClassLoader *classLoader, BOOLEAN *foundCloneable, UDATA *markedInterfaceCount, UDATA *inheritedInterfaceCount, IDATA *maxInterfaceDepth)
{
J9Class *interfaceHead = NULL;
J9Class *lastInterface = NULL;
UDATA interfaceCount = romClass->interfaceCount;
UDATA foundInterfaces = 0;
UDATA localInheritedInterfaceCount = 0;
IDATA localMaxInterfaceDepth = *maxInterfaceDepth;
/* Mark all interfaces which are inherited from the superclass. */
if (superclass != NULL) {
J9ITable *iTable = (J9ITable *)superclass->iTable;
while (iTable != NULL) {
/* No need to check if the new class is an interface, since all
* interfaces have Object as their superclass, which will not
* incorrectly implement any interfaces.
*/
localMaxInterfaceDepth = OMR_MAX(localMaxInterfaceDepth, (IDATA)iTable->depth);
localInheritedInterfaceCount += 1;
iTable->interfaceClass->instanceDescription = (UDATA *)-1;
iTable = iTable->next;
}
}
/* Mark all of the interfaces supported by this class, including all interfaces inherited by superinterfaces. */
if (interfaceCount != 0) {
UDATA i;
J9SRP *interfaceNames = J9ROMCLASS_INTERFACES(romClass);
for (i = 0; i<interfaceCount; i++) {
J9ITable *iTable;
J9UTF8 *interfaceName = NNSRP_GET(interfaceNames[i], J9UTF8*);
/* peek the table, do not load */
J9Class *interfaceClass = hashClassTableAt(classLoader, J9UTF8_DATA(interfaceName), J9UTF8_LENGTH(interfaceName));
/* the interface classes are not NULL, as this was checked by the caller */
if ((foundCloneable != NULL) && (J9AccClassCloneable == (J9CLASS_FLAGS(interfaceClass) & J9AccClassCloneable))) {
*foundCloneable = TRUE;
}
iTable = (J9ITable *)interfaceClass->iTable;
while (iTable != NULL) {
/* the marked field is shared with the instanceDescription
* field. Always keep the low bit tagged so that, if anyone
* does look at it, they don't try to indirect it. Interfaces
* which are inherited are marked, so skip them.
*/
if (iTable->interfaceClass->instanceDescription == (UDATA *)1) {
foundInterfaces++;
/* Mark the current interface so it cannot be linked to itself. */
iTable->interfaceClass->instanceDescription = (UDATA *)-1;
if (lastInterface == NULL) {
interfaceHead = lastInterface = iTable->interfaceClass;
} else {
lastInterface->instanceDescription = (UDATA *)((UDATA)iTable->interfaceClass | INTERFACE_TAG);
lastInterface = iTable->interfaceClass;
}
}
localMaxInterfaceDepth = OMR_MAX(localMaxInterfaceDepth, (IDATA)iTable->depth);
iTable = iTable->next;
}
}
}
if (lastInterface != NULL) {
/* Unmark the last interface. */
lastInterface->instanceDescription = (UDATA *)1;
}
/* Unmark all interfaces which are inherited from the superclass. */
if (superclass != NULL) {
J9ITable *iTable = (J9ITable *)superclass->iTable;
while (iTable != NULL) {
/* No need to check if the new class is an interface, since all
* interfaces have Object as their superclass, which will not
* incorrectly implement any interfaces.
*/
iTable->interfaceClass->instanceDescription = (UDATA *)1;
iTable = iTable->next;
}
}
*markedInterfaceCount = foundInterfaces;
*inheritedInterfaceCount = localInheritedInterfaceCount;
*maxInterfaceDepth = localMaxInterfaceDepth;
return interfaceHead;
}
static void
unmarkInterfaces(J9Class *interfaceHead)
{
while (interfaceHead != NULL) {
J9Class *nextInterface = (J9Class *)((UDATA)interfaceHead->instanceDescription & ~INTERFACE_TAG);
interfaceHead->instanceDescription = (UDATA *)1;
interfaceHead = nextInterface;
}
}
static void
addITableMethods(J9VMThread* vmStruct, J9Class *ramClass, J9Class *interfaceClass, UDATA **currentSlot)
{
J9ROMClass *interfaceRomClass = interfaceClass->romClass;
UDATA count = interfaceRomClass->romMethodCount;
if (count != 0) {
J9VTableHeader *vTableHeader = J9VTABLE_HEADER_FROM_RAM_CLASS(ramClass);
UDATA vTableSize = vTableHeader->size;
J9Method **vTable = J9VTABLE_FROM_HEADER(vTableHeader);
J9Method *interfaceRamMethod = interfaceClass->ramMethods;
U_32 *ordering = J9INTERFACECLASS_METHODORDERING(interfaceClass);
UDATA index = 0;
while (count-- > 0) {
if (NULL != ordering) {
interfaceRamMethod = interfaceClass->ramMethods + ordering[index++];
}
J9ROMMethod *interfaceRomMethod = J9_ROM_METHOD_FROM_RAM_METHOD(interfaceRamMethod);
if (J9ROMMETHOD_IN_ITABLE(interfaceRomMethod)) {
J9UTF8 *interfaceMethodName = J9ROMMETHOD_NAME(interfaceRomMethod);
J9UTF8 *interfaceMethodSig = J9ROMMETHOD_SIGNATURE(interfaceRomMethod);
UDATA vTableOffset = 0;
UDATA searchIndex = 0;
/* Search the vTable for a public method of the correct name. */
while (searchIndex < vTableSize) {
J9Method *vTableRamMethod = vTable[searchIndex];
J9ROMMethod *vTableRomMethod = J9_ROM_METHOD_FROM_RAM_METHOD(vTableRamMethod);
if (J9ROMMETHOD_IN_ITABLE(vTableRomMethod)) {
J9UTF8 *vTableMethodName = J9ROMMETHOD_NAME(vTableRomMethod);
J9UTF8 *vTableMethodSig = J9ROMMETHOD_SIGNATURE(vTableRomMethod);
if ((J9UTF8_LENGTH(interfaceMethodName) == J9UTF8_LENGTH(vTableMethodName))
&& (J9UTF8_LENGTH(interfaceMethodSig) == J9UTF8_LENGTH(vTableMethodSig))
&& (memcmp(J9UTF8_DATA(interfaceMethodName), J9UTF8_DATA(vTableMethodName), J9UTF8_LENGTH(vTableMethodName)) == 0)
&& (memcmp(J9UTF8_DATA(interfaceMethodSig), J9UTF8_DATA(vTableMethodSig), J9UTF8_LENGTH(vTableMethodSig)) == 0)
) {
/* fill in interface index --> vTableOffset mapping */
vTableOffset = J9VTABLE_OFFSET_FROM_INDEX(searchIndex);
**currentSlot = vTableOffset;
(*currentSlot)++;
break;
}
}
searchIndex++;
}
#if defined(J9VM_TRACE_ITABLE)
{
PORT_ACCESS_FROM_VMC(vmStruct);
j9tty_printf(PORTLIB, "\n map %.*s%.*s to vTableOffset=%d (%d)", J9UTF8_LENGTH(interfaceMethodName),
J9UTF8_DATA(interfaceMethodName), J9UTF8_LENGTH(interfaceMethodSig), J9UTF8_DATA(interfaceMethodSig), vTableOffset, searchIndex);
}
#endif
}
interfaceRamMethod++;
}
}
}
static void
createITable(J9VMThread* vmStruct, J9Class *ramClass, J9Class *interfaceClass, J9ITable ***previousLink, UDATA **currentSlot, UDATA depth)
{
/* Fill in the iTable header and link it into the list. */
J9ITable *iTable = (J9ITable *)(*currentSlot);
**previousLink = iTable;
*previousLink = &iTable->next;
iTable->interfaceClass = interfaceClass;
iTable->depth = depth;
*currentSlot = (UDATA *)((UDATA)(*currentSlot) + sizeof(J9ITable));
/* If the newly-built class is not an interface class, fill in the iTable. */
if (J9AccInterface != (ramClass->romClass->modifiers & J9AccInterface)) {
/* iTables contain all methods from the local interface, and any interfaces it extends */
J9ITable *allInterfaces = (J9ITable*)interfaceClass->iTable;
do {
addITableMethods(vmStruct, ramClass, allInterfaces->interfaceClass, currentSlot);
allInterfaces = allInterfaces->next;
} while (NULL != allInterfaces);
}
}
static UDATA *
initializeRAMClassITable (J9VMThread* vmStruct, J9Class *ramClass, J9Class *superclass, UDATA* currentSlot, J9Class *interfaceHead, IDATA maxInterfaceDepth)
{
J9Class *booleanArrayClass;
J9ROMClass *romClass = ramClass->romClass;
#if defined(J9VM_TRACE_ITABLE)
{
PORT_ACCESS_FROM_VMC(vmStruct);
J9UTF8 *className = J9ROMCLASS_CLASSNAME(romClass);
j9tty_printf(PORTLIB, "\n<initializeRAMClassITable: %.*s %d>", J9UTF8_LENGTH(className), J9UTF8_DATA(className), (romClass->modifiers & J9AccInterface));
}
#endif
/* All array classes share the same iTable. If this is an array class and [Z (the first allocated
* array class) has been filled in, copy the iTable from [Z.
*/
booleanArrayClass = vmStruct->javaVM->booleanArrayClass;
if (J9ROMCLASS_IS_ARRAY(romClass) && (booleanArrayClass != NULL)) {
ramClass->iTable = booleanArrayClass->iTable;
if ((J9CLASS_FLAGS(booleanArrayClass) & J9AccClassCloneable) == J9AccClassCloneable) {
ramClass->classDepthAndFlags |= J9AccClassCloneable;
}
unmarkInterfaces(interfaceHead);
} else {
J9ITable *superclassInterfaces = NULL;
J9ITable **previousLink;
if (superclass != NULL) {
superclassInterfaces = (J9ITable *)superclass->iTable;
}
/* Create the iTables. Interface classes must add themselves to their iTables. */
previousLink = (J9ITable **)&ramClass->iTable;
if ((romClass->modifiers & J9AccInterface) == J9AccInterface) {
createITable(vmStruct, ramClass, ramClass, &previousLink, ¤tSlot, (UDATA)(maxInterfaceDepth + 1));
}
while (interfaceHead != NULL) {
J9Class *nextInterface;
createITable(vmStruct, ramClass, interfaceHead, &previousLink, ¤tSlot, ((J9ITable*)interfaceHead->iTable)->depth);
nextInterface = (J9Class *)((UDATA)interfaceHead->instanceDescription & ~INTERFACE_TAG);
/* This is the last walk, so unmark the interfaces */
interfaceHead->instanceDescription = (UDATA *)1;
interfaceHead = nextInterface;
}
*previousLink = superclassInterfaces;
}
return currentSlot;
}
/* Helper function to compare two name and sigs.
* It compares the lengths of both name and sig first before doing any memcmp.
*
* @param[in] name1 The first name
* @param[in] sig1 The first signature
* @param[in] name2 The second name
* @param[in] sig2 The second signature
* @return true iff the names and signatures are the same.
*/
static bool VMINLINE
areNamesAndSignaturesEqual(J9UTF8 *name1, J9UTF8 *sig1, J9UTF8 *name2, J9UTF8 *sig2)
{
/* utf8 pointers may be equal due to utf sharing */
return ((name1 == name2) && (sig1 == sig2))
|| /* Do length checks first as they are less expensive than memcmp */
(((J9UTF8_LENGTH(name1) == J9UTF8_LENGTH(name2))
&& (J9UTF8_LENGTH(sig1) == J9UTF8_LENGTH(sig2))
&& (0 == memcmp(J9UTF8_DATA(name1), J9UTF8_DATA(name2), J9UTF8_LENGTH(name2)))
&& (0 == memcmp(J9UTF8_DATA(sig1), J9UTF8_DATA(sig2), J9UTF8_LENGTH(sig2)))));
}
typedef enum {
SLOT_IS_ROM_METHOD,
SLOT_IS_RAM_METHOD,
SLOT_IS_INTERFACE_RAM_METHOD,
SLOT_IS_CONFLICT_METHOD,
SLOT_IS_CONFLICT_TAG,
SLOT_IS_EQUIVSET_TAG,
SLOT_IS_INVALID = 0x80000000 /* force wide enums */
}INTERFACE_STATE;
static UDATA
addInterfaceMethods(J9VMThread *vmStruct, J9ClassLoader *classLoader, J9Class *interfaceClass, UDATA vTableMethodCount, UDATA *vTableAddress, J9Class *superclass, J9ROMClass *romClass, UDATA *defaultConflictCount, J9Pool *equivalentSets, UDATA *equivSetCount, J9OverrideErrorData *errorData)
{
J9Method **vTableMethods = J9VTABLE_FROM_HEADER(vTableAddress);
J9ROMClass *interfaceROMClass = interfaceClass->romClass;
UDATA count = interfaceROMClass->romMethodCount;
bool verifierEnabled = J9_ARE_ANY_BITS_SET(vmStruct->javaVM->runtimeFlags, J9_RUNTIME_VERIFY);
if (0 != count) {
const void * conflictRunAddress = J9_BCLOOP_ENCODE_SEND_TARGET(J9_BCLOOP_SEND_TARGET_DEFAULT_CONFLICT);
J9Method *interfaceMethod = interfaceClass->ramMethods;
UDATA interfaceDepth = ((J9ITable *)interfaceClass->iTable)->depth;
UDATA j = 0;
for (j=0; j < count; j++) {
J9ROMMethod *romMethod = J9_ROM_METHOD_FROM_RAM_METHOD(interfaceMethod);
/* Ignore the <clinit> from the interface class. */
if (J9ROMMETHOD_IN_ITABLE(romMethod)) {
J9UTF8 *interfaceMethodNameUTF = J9ROMMETHOD_NAME(romMethod);
J9UTF8 *interfaceMethodSigUTF = J9ROMMETHOD_SIGNATURE(romMethod);
UDATA tempIndex = vTableMethodCount;
INTERFACE_STATE state = SLOT_IS_INVALID;
/* If the vTable already has a public declaration of the method that isn't
* either an abstract interface method or default method conflict, do not
* process the interface method at all. */
while (tempIndex > 0) {
/* Decrement the index, convert from one based to zero based index */
tempIndex -= 1;
J9ROMClass *methodROMClass = NULL;
J9Class *methodClass = NULL;
J9ROMMethod *vTableMethod = (J9ROMMethod *)vTableMethods[tempIndex];
if (ROM_METHOD_ID_TAG == ((UDATA)vTableMethod & VTABLE_SLOT_TAG_MASK)) {
methodClass = NULL;
methodROMClass = romClass;
vTableMethod = (J9ROMMethod *)((UDATA)vTableMethod & ~ROM_METHOD_ID_TAG);
state = SLOT_IS_ROM_METHOD;
} else if (DEFAULT_CONFLICT_METHOD_ID_TAG == ((UDATA)vTableMethod & VTABLE_SLOT_TAG_MASK)) {
J9Method *conflictMethod = (J9Method *)((UDATA)vTableMethod & ~DEFAULT_CONFLICT_METHOD_ID_TAG);
methodClass = J9_CLASS_FROM_METHOD(conflictMethod);
methodROMClass = methodClass->romClass;
vTableMethod = J9_ROM_METHOD_FROM_RAM_METHOD(conflictMethod);
state = SLOT_IS_CONFLICT_TAG;
} else if (EQUIVALENT_SET_ID_TAG == ((UDATA)vTableMethod & VTABLE_SLOT_TAG_MASK)) {
state = SLOT_IS_EQUIVSET_TAG;
J9EquivalentEntry *entry = (J9EquivalentEntry*)((UDATA)vTableMethod & ~EQUIVALENT_SET_ID_TAG);
methodClass = J9_CLASS_FROM_METHOD((J9Method *)entry->method);
methodROMClass = methodClass->romClass;
vTableMethod = J9_ROM_METHOD_FROM_RAM_METHOD(entry->method);
} else {
methodClass = J9_CLASS_FROM_METHOD((J9Method *)vTableMethod);
methodROMClass = methodClass->romClass;
vTableMethod = J9_ROM_METHOD_FROM_RAM_METHOD(vTableMethod);
state = SLOT_IS_RAM_METHOD;
if (conflictRunAddress == ((J9Method *)vTableMethod)->methodRunAddress) {
state = SLOT_IS_CONFLICT_METHOD;
} else if (J9ROMCLASS_IS_INTERFACE(methodROMClass)) {
state = SLOT_IS_INTERFACE_RAM_METHOD;
}
}
J9UTF8 *vTableMethodNameUTF = J9ROMMETHOD_NAME(vTableMethod);
J9UTF8 *vTableMethodSigUTF = J9ROMMETHOD_SIGNATURE(vTableMethod);
if (areNamesAndSignaturesEqual(interfaceMethodNameUTF, interfaceMethodSigUTF, vTableMethodNameUTF, vTableMethodSigUTF)) {
switch(state) {
case SLOT_IS_RAM_METHOD:
/* Existing, non-interface method found - only need to do the public check */
break;
case SLOT_IS_ROM_METHOD:
/* Found method defined in this class - only need to do the public check */
break;
case SLOT_IS_CONFLICT_METHOD:
/* conflict detected when building the superclass vtable. Replace with current interface method
* and allow the conflict to be re-detected if it hasn't been resolved by subsequent interfaces
*/
vTableMethods[tempIndex] = interfaceMethod;
goto continueInterfaceScan;
case SLOT_IS_INTERFACE_RAM_METHOD:
/* Here we need to determine if this is a potential conflict or equivalent set */
if (((J9ITable *)methodClass->iTable)->depth < interfaceDepth) {
/* If the interface method is less deep then the candidate method, the interface method
* was filled in by the super class's vtable build. Replace with the current method
* and continue.
*/
vTableMethods[tempIndex] = interfaceMethod;
goto continueInterfaceScan;
} else {
/* Locally added interface method - look for either a merge, conflict or equivSet */
if (isSameOrSuperInterfaceOf(interfaceClass, methodClass)) {
/* Valid defender merge - do nothing as the current vtable method is
* the most *specific* one.
*/
} else {
/* if either is abstract, convert to equivSet, else conflict */
const UDATA combinedModifiers = J9_ROM_METHOD_FROM_RAM_METHOD(interfaceMethod)->modifiers | vTableMethod->modifiers;
if (J9_ARE_ANY_BITS_SET(combinedModifiers, J9AccAbstract)) {
/* Convert to equivSet by adding the existing vtable method to the equivSet */
J9EquivalentEntry *entry = (J9EquivalentEntry*) pool_newElement(equivalentSets);
if (NULL == entry) {
/* OOM will be thrown */
goto fail;
}
entry->method = vTableMethods[tempIndex];
entry->next = NULL;
vTableMethods[tempIndex] = (J9Method *)((UDATA)entry | EQUIVALENT_SET_ID_TAG);
/* This will either be the single default method or the first abstract method */
/* ... and then adding the new method */
*equivSetCount += 1;
goto add_existing;
} else {
/* Conflict detected */
vTableMethods[tempIndex] = (J9Method *)((UDATA)vTableMethods[tempIndex] | DEFAULT_CONFLICT_METHOD_ID_TAG);
*defaultConflictCount += 1;
}
}
}
break;
case SLOT_IS_EQUIVSET_TAG: {
add_existing:
J9EquivalentEntry * existing_entry = (J9EquivalentEntry*)((UDATA)vTableMethods[tempIndex] & ~EQUIVALENT_SET_ID_TAG);
J9EquivalentEntry * previous_entry = existing_entry;
while (NULL != existing_entry) {
if (isSameOrSuperInterfaceOf(interfaceClass, J9_CLASS_FROM_METHOD(existing_entry->method))) {
/* Do nothing - this is already shadowed by an existing method */
goto continueInterfaceScan;
}
previous_entry = existing_entry;
existing_entry = existing_entry->next;
}
existing_entry = previous_entry;
J9EquivalentEntry * new_entry = (J9EquivalentEntry*) pool_newElement(equivalentSets);
if (NULL == new_entry) {
/* OOM will be thrown */
goto fail;
}
new_entry->method = interfaceMethod;
new_entry->next = existing_entry->next;
existing_entry->next = new_entry;
goto continueInterfaceScan;
}
case SLOT_IS_CONFLICT_TAG:
/* Already marked as a conflict found in *this* class */
goto continueInterfaceScan;
case SLOT_IS_INVALID:
default:
Assert_VM_unreachable();
break;
}
if (J9AccPublic == (vTableMethod->modifiers & J9AccPublic)) {
if (verifierEnabled) {
J9ClassLoader *interfaceLoader = interfaceClass->classLoader;
J9ClassLoader *vTableMethodLoader = classLoader;
if (NULL != methodClass) {
vTableMethodLoader = methodClass->classLoader;
}
if (interfaceLoader != vTableMethodLoader) {
if (0 != j9bcv_checkClassLoadingConstraintsForSignature(vmStruct, vTableMethodLoader, interfaceLoader, vTableMethodSigUTF, interfaceMethodSigUTF)) {
J9UTF8 *vTableMethodClassNameUTF = J9ROMCLASS_CLASSNAME(romClass);
if (NULL != methodClass) {
vTableMethodClassNameUTF = J9ROMCLASS_CLASSNAME(methodClass->romClass);
}
J9UTF8 *interfaceClassNameUTF = J9ROMCLASS_CLASSNAME(interfaceClass->romClass);
/* LinkageError will be thrown */
errorData->loader1 = vTableMethodLoader;
errorData->class1NameUTF = vTableMethodClassNameUTF;
errorData->loader2 = interfaceLoader;
errorData->class2NameUTF = interfaceClassNameUTF;
errorData->exceptionClassNameUTF = interfaceClassNameUTF;
errorData->methodNameUTF = vTableMethodNameUTF;
errorData->methodSigUTF = vTableMethodSigUTF;
goto fail;
}
}
}
goto continueInterfaceScan;
}
}
}
/* Add the interface method as the Local class does not implement a public method of the given name. */
vTableMethodCount = growNewVTableSlot((UDATA *)vTableMethods, vTableMethodCount, interfaceMethod);
#if defined(J9VM_TRACE_VTABLE_ACCESS)
{
PORT_ACCESS_FROM_VMC(vmStruct);
J9UTF8 *classNameUTF = J9ROMCLASS_CLASSNAME(romClass);
j9tty_printf(PORTLIB, "\n<vtbl_init: adding @ index=%d %.*s.%.*s%.*s (0x%x)>",
vTableMethodCount,
J9UTF8_LENGTH(classNameUTF), J9UTF8_DATA(classNameUTF),
J9UTF8_LENGTH(interfaceMethodNameUTF), J9UTF8_DATA(interfaceMethodNameUTF),
J9UTF8_LENGTH(interfaceMethodSigUTF), J9UTF8_DATA(interfaceMethodSigUTF),
romMethod);
}
#endif
}
continueInterfaceScan:
interfaceMethod++;
}
}
done:
return vTableMethodCount;
fail:
vTableMethodCount = (UDATA)-1;
goto done;
}
/*
* Compare two classes based on the itable->depth field.
* Results in shallowest -> deepest ordering.
* Return
* 0 if the depths are the same
* - if a->depth < b->depth
* + if a->depth > b->depth
*/
static I_32
interfaceDepthCompare(const void *a, const void *b)
{
J9ITable *iA = (J9ITable *)(*(J9Class **)a)->iTable;
J9ITable *iB = (J9ITable *)(*(J9Class **)b)->iTable;
return (I_32)(iA->depth - iB->depth);
}
/*
* Process the "equivalent sets" stored in the vtable slots. The sets
* will contain either:
* A) All abstract interface methods
* B) A single default method and 1+ abstract interface methods
* C) Multiple default methods and 1+ abstract interface methods
* The equivalent set will be replaced by:
* A) One of the abstract interface methods so that AME will be thrown when invoked
* B) The single default method
* C) Converted to a 'default conflict method'
* @param[in] vmStruct The J9VMThread
* @param[in,out] defaultConflictCount The count of conflict methods. Possibly updated
* @param[in] vTableMethodCount The number of methods in vtable
* @param[in] vTableAddress The vtable itself
*/
static void
processEquivalentSets(J9VMThread *vmStruct, UDATA *defaultConflictCount, UDATA vTableMethodCount, UDATA *vTableAddress)
{
UDATA *vTableMethods = (UDATA *)J9VTABLE_FROM_HEADER(vTableAddress);
for (UDATA i = 0; i < vTableMethodCount; i++) {
UDATA slotValue = vTableMethods[i];
if (EQUIVALENT_SET_ID_TAG == (slotValue & VTABLE_SLOT_TAG_MASK)) {
/* Process set and determine if there is a method that satisfies or if this is a conflict */
J9EquivalentEntry *entry = (J9EquivalentEntry*)(slotValue & ~EQUIVALENT_SET_ID_TAG);
J9Method *candidate = entry->method;
bool foundNonAbstract = false;
while (NULL != entry) {
if (J9_ARE_NO_BITS_SET(J9_ROM_METHOD_FROM_RAM_METHOD(entry->method)->modifiers, J9AccAbstract)) {
if (foundNonAbstract) {
/* Two non-abstract methods -> conflict */
vTableMethods[i] = ((UDATA)entry->method | DEFAULT_CONFLICT_METHOD_ID_TAG);
*defaultConflictCount += 1;
goto continueProcessingVTable;
}
foundNonAbstract = true;
candidate = entry->method;
}
entry = entry->next;
}
/* This will either be the single default method or the first abstract method */
vTableMethods[i] = (UDATA)candidate;
}
continueProcessingVTable: ;
}
}
/**
* Computes the virtual function dispatch table from the specified
* superclass and ROM information. Only virtual methods (not constructors) appear
* in the vtable -- the vtable inherits the number of slots from the superclass and
* adds a single slot for every method defined at this level.
*
* Return the malloc'ed vTable, or the ramClass vTable in the case of hotswap vTable rebuild.
*
* The caller must hold the class table mutex or exclusive access.
*/
static UDATA *
computeVTable(J9VMThread *vmStruct, J9ClassLoader *classLoader, J9Class *superclass, J9ROMClass *taggedClass, UDATA packageID, J9ROMMethod ** methodRemapArray, J9Class *interfaceHead, UDATA *defaultConflictCount, UDATA interfaceCount, UDATA inheritedInterfaceCount, J9OverrideErrorData *errorData)
{
J9JavaVM *vm = vmStruct->javaVM;
J9ROMClass *romClass = taggedClass;
UDATA maxSlots;
UDATA *vTableAddress = NULL;
bool vTableAllocated = false;
PORT_ACCESS_FROM_VMC(vmStruct);
#if defined(J9VM_INTERP_HOT_CODE_REPLACEMENT)
if (((UDATA)taggedClass & ROM_METHOD_ID_TAG) == ROM_METHOD_ID_TAG) {
taggedClass = (J9ROMClass *)((UDATA)taggedClass - 1);
romClass = ((J9Class *)taggedClass)->romClass;
}
#endif
#if defined(J9VM_TRACE_VTABLE_ACCESS)
{
J9UTF8 *className = J9ROMCLASS_CLASSNAME(romClass);
j9tty_printf(PORTLIB, "\n<computeVTable: %.*s>", J9UTF8_LENGTH(className), J9UTF8_DATA(className));
}
#endif
vmStruct->tempSlot = 0;
/* Compute the absolute maximum size of the vTable and allocate it. */
if ((romClass->modifiers & J9AccInterface) == J9AccInterface) {
maxSlots = 1;
} else {
/* All methods in the current class might need new slots in the vTable. */
maxSlots = romClass->romMethodCount;
/* Add in all real method slots from the superclass. */
if (superclass != NULL) {
J9VTableHeader *superVTable = J9VTABLE_HEADER_FROM_RAM_CLASS(superclass);
maxSlots += superVTable->size;
}
/* header slots */
maxSlots += (sizeof(J9VTableHeader) / sizeof(UDATA));
/* For non-array classes, compute the total possible size of implementing all interface methods. */
if (J9ROMCLASS_IS_ARRAY(romClass) == 0) {
J9Class *interfaceWalk = interfaceHead;
while (interfaceWalk != NULL) {
maxSlots += J9INTERFACECLASS_ITABLEMETHODCOUNT(interfaceWalk);
interfaceWalk = (J9Class *)((UDATA)interfaceWalk->instanceDescription & ~INTERFACE_TAG);
}
}
}
/* convert slots to bytes */
maxSlots *= sizeof(UDATA);
#if defined(J9VM_INTERP_HOT_CODE_REPLACEMENT)
if (taggedClass != romClass) {
vTableAddress = (UDATA *)J9VTABLE_HEADER_FROM_RAM_CLASS(taggedClass);
}
#endif
if (NULL == vTableAddress) {
if (maxSlots > vm->vTableScratchSize) {
vTableAddress = (UDATA *)j9mem_allocate_memory(maxSlots, J9MEM_CATEGORY_CLASSES);
vTableAllocated = true;
} else {
vTableAddress = vm->vTableScratch;
}
}
if (NULL != vTableAddress) {
UDATA vTableMethodCount = 0;
J9VTableHeader *vTableHeader = (J9VTableHeader *)vTableAddress;
/* Write size of 0 */
if (J9AccInterface == (romClass->modifiers & J9AccInterface)) {
vTableHeader->size = 0;
goto done;
}
if (superclass == NULL) {
/* no inherited slots, write default slot in header */
vTableHeader->initialVirtualMethod = (J9Method *)vm->initialMethods.initialVirtualMethod;
vTableHeader->invokePrivateMethod = (J9Method *)vm->initialMethods.invokePrivateMethod;
} else {
J9VTableHeader *superVTable = J9VTABLE_HEADER_FROM_RAM_CLASS(superclass);
vTableMethodCount = superVTable->size;
/* + 1 to account for size slot */
memcpy(vTableAddress, superVTable, ((vTableMethodCount * sizeof(UDATA)) + sizeof(J9VTableHeader)));
}
if (J9ROMCLASS_IS_ARRAY(romClass) == 0) {
J9ROMMethod *romMethod = NULL;
UDATA romMethodIndex = 0;
UDATA count = romClass->romMethodCount;
/* Walk over ROM Methods. If the methodRemapArray is supplied, use the array as a source of
* J9ROMMethods instead of romClass->romMethods. The methodRemapArray is specified by
* HCR to ensure that the replacement class vtable has the same method order as the original class
*/
if (methodRemapArray == NULL) {
romMethod = J9ROMCLASS_ROMMETHODS(romClass);
} else {
romMethod = methodRemapArray[romMethodIndex];
}
if (count != 0) {
UDATA i;
for (i=count; i>0; i--) {
J9UTF8* methodName = J9ROMMETHOD_NAME(romMethod);
/* Check for '<' to exclude <init> from being processed */
if (J9_ARE_ANY_BITS_SET(romMethod->modifiers, J9AccMethodVTable | J9AccPrivate)
&& J9_ARE_NO_BITS_SET(romMethod->modifiers, J9AccStatic)
&& ('<' != J9UTF8_DATA(methodName)[0])
) {
vTableMethodCount = processVTableMethod(vmStruct, classLoader, vTableAddress, superclass, romClass, romMethod,
packageID, vTableMethodCount, (J9ROMMethod *)((UDATA)romMethod + ROM_METHOD_ID_TAG), errorData);
if ((UDATA)-1 == vTableMethodCount) {
goto fail;
}
}
if (i > 1) {
if (methodRemapArray == NULL) {
romMethod = nextROMMethod(romMethod);
} else {
romMethodIndex++;
romMethod = methodRemapArray[romMethodIndex];
}
}
}
}
/* TODO: optimize this so only done if the class's interfaces have defenders? */
if (interfaceCount > 0) {
UDATA totalInterfaces = interfaceCount + inheritedInterfaceCount;
J9Class *interfaceWalk = interfaceHead;
J9Class *localBuffer[LOCAL_INTERFACE_ARRAY_SIZE];
J9Class **interfaces = NULL;
J9Class **iterator = NULL;
UDATA i = 0;
if (totalInterfaces <= LOCAL_INTERFACE_ARRAY_SIZE) {
memset(localBuffer, 0, sizeof(localBuffer));
interfaces = localBuffer;
} else {
/* No memset required as interfaces will be completely initialized */
interfaces = (J9Class **)j9mem_allocate_memory(totalInterfaces * sizeof(J9Class *), J9MEM_CATEGORY_CLASSES);
if (NULL == interfaces) {
goto fail;
}
}
iterator = interfaces;
/* Add the local interfaces to interfaces[] */
while (NULL != interfaceWalk) {
*iterator = interfaceWalk;
iterator += 1;
interfaceWalk = (J9Class *)((UDATA)interfaceWalk->instanceDescription & ~INTERFACE_TAG);
}
/* Add the superclass's interfaces to interfaces[] */
if (NULL != superclass) {
J9ITable *iTableIter = (J9ITable *)superclass->iTable;
while (NULL != iTableIter) {
*iterator = iTableIter->interfaceClass;
iTableIter = iTableIter->next;
iterator += 1;
}
}
J9_SORT(interfaces, totalInterfaces, sizeof(J9Class *), &interfaceDepthCompare);
#if defined(VERBOSE_INTERFACE_METHODS)
{
J9UTF8 *className = J9ROMCLASS_CLASSNAME(romClass);
j9tty_printf(PORTLIB, "\nAdding interfaceMethods for class <%.*s> using interface[%i]=%p. #Local=%i\t#Inherited=", J9UTF8_LENGTH(className), J9UTF8_DATA(className), totalInterfaces, interfaces, interfaceCount, inheritedInterfaceCount);
}
#endif /* VERBOSE_INTERFACE_METHODS */
J9Pool *equivalentSet = pool_new(sizeof(J9EquivalentEntry), 0, 0, 0, J9_GET_CALLSITE(), J9MEM_CATEGORY_CLASSES, POOL_FOR_PORT(vm->portLibrary));
if (NULL == equivalentSet) {
/* OOM will be thrown */
goto fail;
}
UDATA equivSetCount = 0;
for (i = totalInterfaces; i > 0; i--) {
#if defined(VERBOSE_INTERFACE_METHODS)
{
J9UTF8 *className = J9ROMCLASS_CLASSNAME(interfaces[i - 1]->romClass);
j9tty_printf(PORTLIB, "\n\t<%.*s>", J9UTF8_LENGTH(className), J9UTF8_DATA(className));
}
#endif /* VERBOSE_INTERFACE_METHODS */
vTableMethodCount = addInterfaceMethods(vmStruct, classLoader, interfaces[i - 1], vTableMethodCount, vTableAddress, superclass, romClass, defaultConflictCount, equivalentSet, &equivSetCount, errorData);
if ((UDATA)-1 == vTableMethodCount) {
if (NULL != equivalentSet) {
pool_kill(equivalentSet);
}
if (interfaces != localBuffer) {
j9mem_free_memory(interfaces);
}
goto fail;
}
}
if (equivSetCount > 0) {
processEquivalentSets(vmStruct, defaultConflictCount, vTableMethodCount, vTableAddress);
}
if (NULL != equivalentSet) {
pool_kill(equivalentSet);
}
if (interfaces != localBuffer) {
j9mem_free_memory(interfaces);
}
}
/* record number of slots used */
*vTableAddress = vTableMethodCount;
}
}
done:
return vTableAddress;
fail:
if (vTableAllocated) {
j9mem_free_memory(vTableAddress);
}
vTableAddress = NULL;
goto done;
}
/**
* Copy the vTable, converting local ROM method to their RAM equivalents.
*/
static void
copyVTable(J9VMThread *vmStruct, J9Class *ramClass, J9Class *superclass, UDATA *vTable, UDATA defaultConflictCount)
{
UDATA superCount = 0;
UDATA count;
J9VTableHeader *vTableAddress;
J9Method **sourceVTable;
J9Method **newVTable;
UDATA index;
J9Method *ramMethods = ramClass->ramMethods;