-
Notifications
You must be signed in to change notification settings - Fork 722
/
hshelp.c
4116 lines (3454 loc) · 142 KB
/
hshelp.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, 2021 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] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#include <string.h>
#include <stdlib.h>
#include "j9.h"
#include "j9protos.h"
#include "j9user.h"
#include "util_api.h"
#include "jni.h"
#include "j9comp.h"
#include "vmaccess.h"
#include "j9consts.h"
#include "rommeth.h"
#include "j9port.h"
#include "j9cp.h"
#include "bcnames.h"
#include "pcstack.h"
#include "jvmti.h"
#include "jvmtiInternal.h"
#include "HeapIteratorAPI.h"
#include "ut_j9hshelp.h"
#include "j9modron.h"
#include "VM_MethodHandleKinds.h"
#include "j2sever.h"
#include "vrfytbl.h"
#include "bytecodewalk.h"
/* Static J9ITable used as a non-NULL iTable cache value by classes that don't implement any interfaces */
const J9ITable invalidITable = { (J9Class *) (UDATA) 0xDEADBEEF, 0, (J9ITable *) NULL };
#if defined(J9VM_INTERP_HOT_CODE_REPLACEMENT)
#define GET_SUPERCLASS(clazz) \
((J9CLASS_DEPTH(clazz) == 0) ? NULL : \
(clazz)->superclasses[J9CLASS_DEPTH(clazz) - 1])
#define NAME_AND_SIG_IDENTICAL(o1, o2, getNameMacro, getSigMacro) \
areUTFPairsIdentical(getNameMacro(o1), getSigMacro(o1), getNameMacro(o2), getSigMacro(o2))
#define J9ROMMETHOD_NAME_AND_SIG_IDENTICAL(o1, o2) \
areUTFPairsIdentical(J9ROMMETHOD_NAME(o1), J9ROMMETHOD_SIGNATURE(o1), J9ROMMETHOD_NAME(o2), J9ROMMETHOD_SIGNATURE(o2))
static UDATA equivalenceHash (void *key, void *userData);
static UDATA equivalenceEquals (void *leftKey, void *rightKey, void *userData);
static jvmtiError addMethodEquivalence(J9VMThread * currentThread, J9Method * oldMethod, J9Method * newMethod, J9HashTable ** methodEquivalences, U_32 size);
static J9Method * getMethodEquivalence (J9VMThread * currentThread, J9Method * method, J9HashTable ** methodEquivalences);
static UDATA fixJNIFieldID PROTOTYPE((J9VMThread * currentThread, J9JNIFieldID * fieldID, J9Class * replacementRAMClass));
static UDATA areMethodRefsIdentical(J9ROMConstantPoolItem * romCP1, U_32 index1, J9ROMConstantPoolItem * romCP2, U_32 index2);
static UDATA areClassRefsIdentical(J9ROMConstantPoolItem * romCP1, U_32 index1, J9ROMConstantPoolItem * romCP2, U_32 index2);
static UDATA areFieldRefsIdentical(J9ROMConstantPoolItem * romCP1, U_32 index1, J9ROMConstantPoolItem * romCP2, U_32 index2);
static UDATA areNameAndSigsIdentical(J9ROMNameAndSignature * nas1, J9ROMNameAndSignature * nas2);
static UDATA areSingleSlotConstantRefsIdentical(J9ROMConstantPoolItem * romCP1, U_32 index1, J9ROMConstantPoolItem * romCP2, U_32 index2);
static UDATA areMethodsEquivalentPropagateCallSites(J9ROMMethod * method1, J9Class * ramClass1, J9ROMMethod * method2, J9Class * ramClass2);
static UDATA areMethodsEquivalentSub(J9ROMMethod * method1, J9ROMClass * romClass1, J9Class * ramClass1, J9ROMMethod * method2, J9ROMClass * romClass2, J9Class * ramClass2);
static UDATA areCallSiteDataMethodsEquivalent(J9ROMClass* romClass1, UDATA callSiteIndex1, J9ROMClass* romClass2, UDATA callSiteIndes2);
static UDATA areDoubleSlotConstantRefsIdentical(J9ROMConstantPoolItem * romCP1, U_32 index1, J9ROMConstantPoolItem * romCP2, U_32 index2);
static void fixClassSlot(J9VMThread* currentThread, J9Class** classSlot, J9HashTable *classPairs);
static void fixJNIFieldIDs(J9VMThread * currentThread, J9Class * originalClass, J9Class * replacementClass);
static void copyStaticFields (J9VMThread * currentThread, J9Class * originalRAMClass, J9Class * replacementRAMClass);
static void fixLoadingConstraints (J9JavaVM * vm, J9Class * oldClass, J9Class * newClass);
static UDATA classPairHash(void* entry, void* userData);
static UDATA classPairEquals(void* left, void* right, void* userData);
static UDATA findMethodInVTable(J9Method *method, UDATA *vTable);
static jvmtiError addClassesRequiringNewITables(J9JavaVM *vm, J9HashTable *classHashTable, UDATA *addedMethodCountPtr, UDATA *addedClassCountPtr, BOOLEAN fastHCR);
static jvmtiError verifyFieldsAreSame (J9VMThread * currentThread, UDATA fieldType, J9ROMClass * originalROMClass, J9ROMClass * replacementROMClass,
UDATA extensionsEnabled, UDATA * extensionsUsed);
static jvmtiError verifyMethodsAreSame (J9VMThread * currentThread, J9JVMTIClassPair * classPair, UDATA extensionsEnabled, UDATA * extensionsUsed);
static int compareClassDepth (const void *leftPair, const void *rightPair);
static UDATA utfsAreIdentical(J9UTF8 * utf1, J9UTF8 * utf2);
static UDATA areUTFPairsIdentical(J9UTF8 * leftUtf1, J9UTF8 * leftUtf2, J9UTF8 * rightUtf1, J9UTF8 * rightUtf2);
static jvmtiError fixJNIMethodID(J9VMThread *currentThread, J9Method *oldMethod, J9Method *newMethod, BOOLEAN equivalent, UDATA extensionsUsed);
#if defined(J9VM_OPT_OPENJDK_METHODHANDLE)
static jvmtiIterationControl fixMemberNamesObjectIteratorCallback(J9JavaVM *vm, J9MM_IterateObjectDescriptor *objectDesc, void *userData);
#endif /* defined(J9VM_OPT_OPENJDK_METHODHANDLE) */
static jvmtiIterationControl fixHeapRefsHeapIteratorCallback(J9JavaVM *vm, J9MM_IterateHeapDescriptor *heapDesc, void *userData);
static jvmtiIterationControl fixHeapRefsSpaceIteratorCallback(J9JavaVM *vm, J9MM_IterateSpaceDescriptor *spaceDesc, void *userData);
static jvmtiIterationControl fixHeapRefsRegionIteratorCallback(J9JavaVM *vm, J9MM_IterateRegionDescriptor *regionDesc, void *userData);
static jvmtiIterationControl fixHeapRefsObjectIteratorCallback(J9JavaVM *vm, J9MM_IterateObjectDescriptor *objectDesc, void *userData);
static void replaceInAllClassLoaders(J9VMThread * currentThread, J9Class * originalRAMClass, J9Class * replacementRAMClass);
#ifdef J9VM_INTERP_NATIVE_SUPPORT
static jvmtiError jitEventInitialize(J9VMThread * currentThread, jint redefinedClassCount, UDATA redefinedMethodCount, J9JVMTIHCRJitEventData * eventData);
static void jitEventAddMethod(J9VMThread * currentThread, J9JVMTIHCRJitEventData * eventData, J9Method * oldMethod, J9Method * newMethod, UDATA equivalent);
static void jitEventAddClass(J9VMThread * currentThread, J9JVMTIHCRJitEventData * eventData, J9Class * originalRAMClass, J9Class * replacementRAMClass);
#endif
static void swapClassesForFastHCR(J9Class *originalClass, J9Class *newClass);
#undef J9HSHELP_DEBUG_SANITY_CHECK
#define J9HSHELP_DEBUG 1
#ifdef J9HSHELP_DEBUG
static char *
getClassName(J9Class * c)
{
static char buf[512];
J9UTF8 * className = J9ROMCLASS_CLASSNAME(c->romClass);
memcpy(buf, J9UTF8_DATA(className), J9UTF8_LENGTH(className));
buf[J9UTF8_LENGTH(className)] = 0;
return buf;
}
#endif
#ifdef J9HSHELP_DEBUG
static char *
getMethodName(J9Method * m)
{
static char buf[512];
J9UTF8 * n = J9ROMMETHOD_NAME(J9_ROM_METHOD_FROM_RAM_METHOD(m));
memcpy(buf, J9UTF8_DATA(n), J9UTF8_LENGTH(n));
buf[J9UTF8_LENGTH(n)] = 0;
return buf;
}
#endif
#ifdef J9HSHELP_DEBUG_SANITY_CHECK
void
hcrSanityCheck(J9JavaVM * vm)
{
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
J9Class * clazz;
J9JVMTIClassPair * result;
J9JVMTIClassPair exemplar;
J9ClassWalkState state;
clazz = vmFuncs->allClassesStartDo(&state, vm, NULL);
while (clazz != NULL) {
/* Make sure all the arrayClass ptrs in obsolete classes point at something (naive null check) */
if (J9_IS_CLASS_OBSOLETE(clazz)) {
if (clazz->arrayClass == NULL) {
abort();
}
}
clazz = vmFuncs->allClassesNextDo(&state);
}
vmFuncs->allClassesEndDo(&state);
}
#endif
/**
* @brief
* Compares two J9UTF8s for identical contents.
* @param first J9UTF8 (must not be NULL)
* @param second J9UTF8 (must not be NULL)
* @return TRUE if they are identical, FALSE otherwise
*/
static UDATA
utfsAreIdentical(J9UTF8 * utf1, J9UTF8 * utf2)
{
if (J9UTF8_LENGTH(utf1) != J9UTF8_LENGTH(utf2)) {
return FALSE;
}
return (memcmp(J9UTF8_DATA(utf1), J9UTF8_DATA(utf2), J9UTF8_LENGTH(utf1)) == 0) ? TRUE : FALSE;
}
/**
* @brief
* Compares two pairs of J9UTF8s for identical contents.
* @param leftUtf1 J9UTF8 (must not be NULL)
* @param leftUtf2 J9UTF8 (must not be NULL)
* @param rightUtf1 J9UTF8 (must not be NULL)
* @param rightUtf2 J9UTF8 (must not be NULL)
* @return TRUE if they are identical, FALSE otherwise
*/
static UDATA
areUTFPairsIdentical(J9UTF8 * leftUtf1, J9UTF8 * leftUtf2, J9UTF8 * rightUtf1, J9UTF8 * rightUtf2)
{
if ((J9UTF8_LENGTH(leftUtf1) != J9UTF8_LENGTH(rightUtf1)) || (J9UTF8_LENGTH(leftUtf2) != J9UTF8_LENGTH(rightUtf2))) {
return FALSE;
}
if (0 != memcmp(J9UTF8_DATA(leftUtf1), J9UTF8_DATA(rightUtf1), J9UTF8_LENGTH(leftUtf1))) {
return FALSE;
}
if (0 != memcmp(J9UTF8_DATA(leftUtf2), J9UTF8_DATA(rightUtf2), J9UTF8_LENGTH(leftUtf2))) {
return FALSE;
}
return TRUE;
}
static UDATA
fixJNIFieldID(J9VMThread * currentThread, J9JNIFieldID * fieldID, J9Class * replacementRAMClass)
{
J9ROMFieldShape * field = fieldID->field;
J9ROMFieldShape * newField = NULL;
UDATA offset;
J9UTF8 * fieldName = J9ROMFIELDSHAPE_NAME(field);
J9UTF8 * fieldSignature = J9ROMFIELDSHAPE_SIGNATURE(field);
J9JavaVM * vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
J9ROMClass * replacementROMClass;
UDATA newFieldIndex;
J9ROMFieldWalkState state;
J9ROMFieldShape * current;
if (field->modifiers & J9AccStatic) {
J9ROMFieldShape * resolvedField;
void * newFieldAddress;
J9Class * declaringClass;
newFieldAddress = vmFuncs->staticFieldAddress(
currentThread,
replacementRAMClass,
J9UTF8_DATA(fieldName),
J9UTF8_LENGTH(fieldName),
J9UTF8_DATA(fieldSignature),
J9UTF8_LENGTH(fieldSignature),
&declaringClass,
(UDATA *) &resolvedField,
J9_LOOK_NO_JAVA,
NULL);
if ((newFieldAddress != NULL) && (J9_CURRENT_CLASS(declaringClass) == replacementRAMClass)) {
offset = (UDATA) newFieldAddress - (UDATA) replacementRAMClass->ramStatics;
newField = resolvedField;
}
} else {
J9ROMFieldShape * resolvedField;
UDATA newFieldOffset;
J9Class * declaringClass;
newFieldOffset = vmFuncs->instanceFieldOffset(
currentThread,
replacementRAMClass,
J9UTF8_DATA(fieldName),
J9UTF8_LENGTH(fieldName),
J9UTF8_DATA(fieldSignature),
J9UTF8_LENGTH(fieldSignature),
&declaringClass,
(UDATA *) &resolvedField,
J9_LOOK_NO_JAVA);
if ((newFieldOffset != -1) && (declaringClass == replacementRAMClass)) {
offset = newFieldOffset;
newField = resolvedField;
}
}
if (newField == NULL) {
return FALSE;
}
/* Determine the field index (field IDs are stored after method IDs in the array) */
replacementROMClass = replacementRAMClass->romClass;
newFieldIndex = replacementROMClass->romMethodCount;
current = romFieldsStartDo(replacementROMClass, &state);
while (newField != current) {
++newFieldIndex;
current = romFieldsNextDo(&state);
}
fieldID->index = newFieldIndex;
fieldID->field = newField;
fieldID->offset = offset;
fieldID->declaringClass = replacementRAMClass;
return TRUE;
}
static void
fixJNIFieldIDs(J9VMThread * currentThread, J9Class * originalClass, J9Class * replacementClass)
{
void ** oldJNIIDs = originalClass->jniIDs;
if (oldJNIIDs != NULL) {
J9JavaVM * vm = currentThread->javaVM;
void ** newJNIIDs;
newJNIIDs = vm->internalVMFunctions->ensureJNIIDTable(currentThread, replacementClass);
if (newJNIIDs == NULL) {
/* do something */
Assert_hshelp_ShouldNeverHappen();
} else {
J9ROMClass * originalROMClass = originalClass->romClass;
UDATA oldMethodCount = originalROMClass->romMethodCount;
UDATA oldFieldCount = originalROMClass->romFieldCount;
UDATA oldFieldIndex;
for (oldFieldIndex = oldMethodCount; oldFieldIndex < oldMethodCount + oldFieldCount; ++oldFieldIndex) {
J9JNIFieldID * fieldID = oldJNIIDs[oldFieldIndex];
if (fieldID != NULL) {
/* Always invalidate the old field ID slot (do not free IDs for deleted fields, since a reflect field might be holding onto it) */
oldJNIIDs[oldFieldIndex] = NULL;
/* If the old field was not deleted, move the fieldID to the new class */
if (fixJNIFieldID(currentThread, fieldID, replacementClass)) {
newJNIIDs[fieldID->index] = fieldID;
}
}
}
}
}
}
/*
* @param currentThread
* @param oldMethod
* @param newMethod
* @param equivalent
*
* if equivalent and old exists, copy old methodID to new method
* if equivalent and old doesn't exists, create methodID for old method copy to new method
* if non-equivalent and old exists, copy old methodID to new method, and create a new methodID for the old chain
* if non-equivalent and old does not exist, do nothing
* if new doesn't exist do nothing
*/
static jvmtiError
fixJNIMethodID(J9VMThread *currentThread, J9Method *oldMethod, J9Method *newMethod, BOOLEAN equivalent, UDATA extensionsUsed)
{
jvmtiError rc = JVMTI_ERROR_NONE;
if (NULL != newMethod) {
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
J9Class *oldMethodClass = J9_CLASS_FROM_METHOD(oldMethod);
void **oldJNIIDs = oldMethodClass->jniIDs;
UDATA oldMethodIndex = 0;
J9JNIMethodID *oldMethodID = NULL;
J9Class *newMethodClass = J9_CLASS_FROM_METHOD(newMethod);
void **newJNIIDs = NULL;
oldMethodIndex = getMethodIndex(oldMethod);
if (equivalent) {
if (NULL == oldJNIIDs) {
oldJNIIDs = vmFuncs->ensureJNIIDTable(currentThread, oldMethodClass);
if (NULL == oldJNIIDs) {
rc = JVMTI_ERROR_OUT_OF_MEMORY;
goto done;
}
}
oldMethodID = oldJNIIDs[oldMethodIndex];
/* we need to have a methodID so we can keep track of equivalences */
if (NULL == oldMethodID) {
oldMethodID = vmFuncs->getJNIMethodID(currentThread, oldMethod);
}
} else {
/* NOT Equivalent case */
J9JNIMethodID *oldMethodIDReplacement = NULL;
J9Class *currentClass = oldMethodClass;
if (NULL == oldJNIIDs) {
goto done;
}
oldMethodID = oldJNIIDs[oldMethodIndex];
if (NULL == oldMethodID) {
goto done;
}
/* invalidate the old methodID and create a new one */
oldJNIIDs[oldMethodIndex] = NULL;
oldMethodIDReplacement = vmFuncs->getJNIMethodID(currentThread, oldMethod);
if (NULL == oldMethodIDReplacement) {
/* Put the MethodID back so that the chain is still correct */
oldJNIIDs[oldMethodIndex] = oldMethodID;
rc = JVMTI_ERROR_OUT_OF_MEMORY;
goto done;
}
vmFuncs->initializeMethodID(currentThread, oldMethodIDReplacement, oldMethod);
oldJNIIDs[oldMethodIndex] = oldMethodIDReplacement;
/* walk back the class chain and fix up old equivalence */
while (NULL != currentClass->replacedClass) {
void **methodIDs = NULL;
J9Method *equivalentMethod = NULL;
BOOLEAN found = FALSE;
currentClass = currentClass->replacedClass;
methodIDs = currentClass->jniIDs;
if (NULL != methodIDs) {
UDATA methodIndex = 0;
UDATA methodCount = currentClass->romClass->romMethodCount;
for (methodIndex = 0; methodIndex < methodCount; methodIndex++) {
if (methodIDs[methodIndex] == oldMethodID) {
methodIDs[methodIndex] = oldMethodIDReplacement;
found = TRUE;
break;
}
}
}
if (!found) {
/* method is not found in the currentClass, stop searching now */
break;
}
}
}
if (NULL == newJNIIDs) {
newJNIIDs = vmFuncs->ensureJNIIDTable(currentThread, newMethodClass);
if (newJNIIDs == NULL) {
rc = JVMTI_ERROR_OUT_OF_MEMORY;
goto done;
}
}
newJNIIDs[getMethodIndex(newMethod)] = oldMethodID;
vmFuncs->initializeMethodID(currentThread, oldMethodID, newMethod);
}
done:
return rc;
}
static UDATA
areMethodRefsIdentical(J9ROMConstantPoolItem * romCP1, U_32 index1, J9ROMConstantPoolItem * romCP2, U_32 index2)
{
J9ROMMethodRef * ref1 = (J9ROMMethodRef *) &romCP1[index1];
J9ROMMethodRef * ref2 = (J9ROMMethodRef *) &romCP2[index2];
return
areClassRefsIdentical(romCP1, ref1->classRefCPIndex, romCP2, ref2->classRefCPIndex) &&
areNameAndSigsIdentical(J9ROMMETHODREF_NAMEANDSIGNATURE(ref1), J9ROMMETHODREF_NAMEANDSIGNATURE(ref2));
}
static UDATA
areClassRefsIdentical(J9ROMConstantPoolItem * romCP1, U_32 index1, J9ROMConstantPoolItem * romCP2, U_32 index2)
{
J9ROMClassRef * ref1 = (J9ROMClassRef *) &romCP1[index1];
J9ROMClassRef * ref2 = (J9ROMClassRef *) &romCP2[index2];
return utfsAreIdentical(J9ROMCLASSREF_NAME(ref1), J9ROMCLASSREF_NAME(ref2));
}
static UDATA
areFieldRefsIdentical(J9ROMConstantPoolItem * romCP1, U_32 index1, J9ROMConstantPoolItem * romCP2, U_32 index2)
{
J9ROMFieldRef * ref1 = (J9ROMFieldRef *) &romCP1[index1];
J9ROMFieldRef * ref2 = (J9ROMFieldRef *) &romCP2[index2];
return
areClassRefsIdentical(romCP1, ref1->classRefCPIndex, romCP2, ref2->classRefCPIndex) &&
areNameAndSigsIdentical(J9ROMFIELDREF_NAMEANDSIGNATURE(ref1), J9ROMFIELDREF_NAMEANDSIGNATURE(ref2));
}
static UDATA
areNameAndSigsIdentical(J9ROMNameAndSignature * nas1, J9ROMNameAndSignature * nas2)
{
return NAME_AND_SIG_IDENTICAL(nas1, nas2, J9ROMNAMEANDSIGNATURE_NAME, J9ROMNAMEANDSIGNATURE_SIGNATURE);
}
static UDATA
areSingleSlotConstantRefsIdentical(J9ROMConstantPoolItem * romCP1, U_32 index1, J9ROMConstantPoolItem * romCP2, U_32 index2)
{
J9ROMSingleSlotConstantRef * ref1 = (J9ROMSingleSlotConstantRef *) &romCP1[index1];
J9ROMSingleSlotConstantRef * ref2 = (J9ROMSingleSlotConstantRef *) &romCP2[index2];
if (ref1->cpType != ref2->cpType) {
return FALSE;
}
if ((ref1->cpType == J9DescriptionCpTypeScalar) || (ref1->cpType == J9DescriptionCpTypeMethodHandle)) {
return ref1->data == ref2->data;
}
/* all remaining cases { class, string, methodtype } are single UTF8 slots */
return utfsAreIdentical(J9ROMSTRINGREF_UTF8DATA((J9ROMStringRef *) ref1), J9ROMSTRINGREF_UTF8DATA((J9ROMStringRef *) ref2));
}
/*
* Helper methods for areCallSiteDataMethodsEquivalent
*/
/* ROM call site data header size (16 bits of method handle address + additional argument count) */
#define CALL_SITE_HEADER_OFFSET 2
static UDATA
isMethodHandleAField(UDATA cpType)
{
return (cpType == MH_REF_GETFIELD) || (cpType == MH_REF_GETSTATIC) || (cpType == MH_REF_PUTSTATIC) || (cpType == MH_REF_PUTFIELD);
}
static U_16*
findBSMDataAtIndex(U_16* bsmData, U_16 bsmIndex)
{
U_16 i = 0;
for (i = 0; i < bsmIndex; i++) {
/* increment by size of bsm data plus header */
bsmData += (bsmData[1] + CALL_SITE_HEADER_OFFSET);
}
return bsmData;
}
static U_32
iterateToNextArgument(U_32 sigIndex, U_32 sigLength, U_8* sigData)
{
if (sigIndex >= sigLength) return sigIndex;
/* check for object */
if ('L' == sigData[sigIndex]) {
while ((sigIndex < sigLength) && (';' != sigData[sigIndex])) {
sigIndex += 1;
}
}
/* for an object this will move past the ;, for a primitive this will move past the argument */
sigIndex += 1;
return sigIndex;
}
/**
* Verify callsite equivalence.
*
* Structure of CallSiteData structure in ROM class (also see romclasswalk.c):
*
* romClass->callSiteData : {
* SRP callSiteNAS[romClass->callSiteCount]; // structures describing the resolved callsite. Note: SRP is 32 bits
* U_16 callSiteBSMIndex[romClass->callSiteCount]; // map from callSiteIndex value to bootStrapMethodData index
* {
* U_16 bootStrapMethodHandleRef; // (header) index of the bsm's MethodHandle in the constant pool.
* U_16 argumentCount; // (header) number of bsm arguments in addition to the required three (MethodHandles.Lookup, String, MethodType).
* U_16 argument[argumentCount]; // The additional BSM arguments, these are constant pool indices.
* } bootStrapMethodData[romClass->bsmCount];
* }
*
*/
static UDATA
areCallSiteDataMethodsEquivalent(J9ROMClass* romClass1, UDATA callSiteIndex1, J9ROMClass* romClass2, UDATA callSiteIndex2)
{
J9ROMConstantPoolItem *romCP1 = J9_ROM_CP_FROM_ROM_CLASS(romClass1);
J9ROMConstantPoolItem *romCP2 = J9_ROM_CP_FROM_ROM_CLASS(romClass2);
J9SRP *callSiteData1 = (J9SRP *) J9ROMCLASS_CALLSITEDATA(romClass1);
J9SRP *callSiteData2 = (J9SRP *) J9ROMCLASS_CALLSITEDATA(romClass2);
/* get call site name and signature */
J9ROMNameAndSignature* nas1 = SRP_PTR_GET(callSiteData1 + callSiteIndex1, J9ROMNameAndSignature*);
J9ROMNameAndSignature* nas2 = SRP_PTR_GET(callSiteData2 + callSiteIndex2, J9ROMNameAndSignature*);
/* get call site BSM index */
U_16 *bsmIndices1 = (U_16 *) (callSiteData1 + romClass1->callSiteCount);
U_16 *bsmIndices2 = (U_16 *) (callSiteData2 + romClass2->callSiteCount);
U_16 bsmIndex1 = bsmIndices1[callSiteIndex1];
U_16 bsmIndex2 = bsmIndices2[callSiteIndex2];
/* get top of bsm data */
U_16 *bsmData1 = findBSMDataAtIndex(bsmIndices1 + romClass1->callSiteCount, bsmIndex1);
U_16 *bsmData2 = findBSMDataAtIndex(bsmIndices2 + romClass2->callSiteCount, bsmIndex2);
/* additional arg variables */
U_16 additionalArgCount1 = bsmData1[1];
U_16 additionalArgCount2 = bsmData2[1];
/* BSM MethodHandle reference */
J9ROMMethodHandleRef *mhRef1 = (J9ROMMethodHandleRef*) &romCP1[bsmData1[0]];
J9ROMMethodHandleRef *mhRef2 = (J9ROMMethodHandleRef*) &romCP2[bsmData2[0]];
U_32 bsmHandleTypeAndCpType1 = mhRef1->handleTypeAndCpType;
U_32 bsmHandleTypeAndCpType2 = mhRef2->handleTypeAndCpType;
U_32 bsmCpTypeIndex1 = bsmHandleTypeAndCpType1 & J9DescriptionCpTypeMask;
U_32 bsmCpTypeIndex2 = bsmHandleTypeAndCpType2 & J9DescriptionCpTypeMask;
UDATA bsmCpType1 = J9_CP_TYPE(J9ROMCLASS_CPSHAPEDESCRIPTION(romClass1), bsmCpTypeIndex1);
UDATA bsmCpType2 = J9_CP_TYPE(J9ROMCLASS_CPSHAPEDESCRIPTION(romClass2), bsmCpTypeIndex2);
/* additional helpers */
J9ROMMethodRef* bsmMethod = NULL;
J9ROMNameAndSignature *bsmNAS = NULL;
J9UTF8 *bsmSig = NULL;
U_8* sigData = NULL;
U_32 sigIndex = 0;
U_32 sigLength = 0;
U_16 i = 0;
/* compare method name and signature */
if (!areNameAndSigsIdentical(nas1, nas2)) {
return FALSE;
}
/* compare number of additional arguments */
if (additionalArgCount1 != additionalArgCount2) {
return FALSE;
}
/* verify that bsm MethodHandles are the same:
* step 1) verify that the handle types and cp types match
* step 2) verify that the method references are the same (at this point we know they are
* both method references)
*/
if (bsmHandleTypeAndCpType1 != bsmHandleTypeAndCpType2) {
return FALSE;
}
/* Before continuing verify that the bsm MethodHandle refers to a Method and not a Field. The method
* signature will be used to determine
* whether the additional arguments are double or single slot which is impossible with a field.
* The indy call will fail at a later point. It is only necessary to check one because from the previous
* check both bsm1 and bsm2 are of the same type.
*/
if (isMethodHandleAField(bsmCpType1)) {
return FALSE;
}
if (!areMethodRefsIdentical(romCP1, mhRef1->methodOrFieldRefIndex, romCP2, mhRef2->methodOrFieldRefIndex)) {
return FALSE;
}
/* Compare additional argument types. We know that both call site entries have the same number of additional arguments.
* Each argument represents an index into the constant pool to be compared. If there are no additional arguments
* just skip this step.
*/
if (0 == additionalArgCount1) {
return TRUE;
}
bsmMethod = (J9ROMMethodRef *) &romCP1[mhRef1->methodOrFieldRefIndex];
bsmNAS = J9ROMMETHODREF_NAMEANDSIGNATURE(bsmMethod);
bsmSig = J9ROMNAMEANDSIGNATURE_SIGNATURE(bsmNAS);
sigData = J9UTF8_DATA(bsmSig);
sigLength = J9UTF8_LENGTH(bsmSig);
/* Iterate past the first three arguments which are mandatory (MethodHandle.Lookup, String, MethodType) */
sigIndex += 1; /* move past first char '(' */
for (i = 0; i < 3; i++) {
sigIndex = iterateToNextArgument(sigIndex, sigLength, sigData);
}
/* compare additional arguments */
for (i = 0; i < additionalArgCount1; i++) {
if (sigIndex >= sigLength) {
return FALSE;
}
if (('D' == sigData[sigIndex]) || ('J' == sigData[sigIndex])) {
if (!areDoubleSlotConstantRefsIdentical
(romCP1, bsmData1[i + CALL_SITE_HEADER_OFFSET], romCP2, bsmData2[i + CALL_SITE_HEADER_OFFSET])
) {
return FALSE;
}
} else {
if (!areSingleSlotConstantRefsIdentical
(romCP1, bsmData1[i + CALL_SITE_HEADER_OFFSET], romCP2, bsmData2[i + CALL_SITE_HEADER_OFFSET])
) {
return FALSE;
}
}
sigIndex = iterateToNextArgument(sigIndex, sigLength, sigData);
}
/* verify that each argument was checked */
if (i != additionalArgCount1) {
return FALSE;
}
return TRUE;
}
static UDATA
areDoubleSlotConstantRefsIdentical(J9ROMConstantPoolItem * romCP1, U_32 index1, J9ROMConstantPoolItem * romCP2, U_32 index2)
{
J9ROMConstantRef * ref1 = (J9ROMConstantRef *) &romCP1[index1];
J9ROMConstantRef * ref2 = (J9ROMConstantRef *) &romCP2[index2];
return (ref1->slot1 == ref2->slot1) && (ref1->slot2 == ref2->slot2);
}
/**
* Compares two methods bytecode by bytecode to determine equivalence.
*
* @param method1 first method to compare
* @param romClass1 ROM class associated with method1
* @param method2 second method to compare
* @param romClass2 ROM class associated with method2
* @return true if equivalent, false otherwise
*/
UDATA
areMethodsEquivalent(J9ROMMethod * method1, J9ROMClass * romClass1, J9ROMMethod * method2, J9ROMClass * romClass2)
{
return areMethodsEquivalentSub(method1, romClass1, NULL, method2, romClass2, NULL);
}
/**
* Compares two methods bytecode by bytecode to determine equivalence.
* For invokedynamic if the referenced callsite was resolved in method1
* the resolved MethodHandle will be copied to the RAM class callsite table
* in method2.
*
* @param method1 first method to compare
* param ramClass1 RAM class associated with method1
* @param method2 second method to compare
* @param ramClass2 RAM class associated with method2
* @return true if equivalent, false otherwise
*/
static UDATA
areMethodsEquivalentPropagateCallSites(J9ROMMethod * method1, J9Class *ramClass1, J9ROMMethod * method2, J9Class *ramClass2)
{
return areMethodsEquivalentSub(method1, ramClass1->romClass, ramClass1, method2, ramClass2->romClass, ramClass2);
}
/**
* Compares two methods bytecode by bytecode to determine equivalence. If invoke
* dynamic callsites are determined to be equivalent and RAM classes are not null, resolved
* callsites will be propagated from ramClass1 to ramClass2. It is assumed that callsites will
* be propagated from 1 (original) to 2 (new).
*
* @param method1 first method to compare
* @param romClass1 ROM class associated with method1
* @param ramClass1 RAM class associated with method1, must be provided to propagate callsites
* @param method2 second method to compare
* @param romClass2 ROM class associated with method2
* @param ramClass2 RAM class associated with method2, must be provided to propagate callsites
* @return true if equivalent, false otherwise
*/
static UDATA
areMethodsEquivalentSub(J9ROMMethod * method1, J9ROMClass * romClass1, J9Class * ramClass1, J9ROMMethod * method2, J9ROMClass * romClass2, J9Class * ramClass2)
{
U_8 * bytecodes1;
J9ROMConstantPoolItem * romCP1;
U_8 * bytecodes2;
J9ROMConstantPoolItem * romCP2;
UDATA size;
UDATA index;
#ifdef J9VM_ENV_LITTLE_ENDIAN
#define NEXT_U16(bytecodes) (((U_16 ) ((bytecodes)[index + alreadyCompared + 0])) + (((U_16 ) ((bytecodes)[index + alreadyCompared + 1])) << 8))
#else
#define NEXT_U16(bytecodes) (((U_16 ) ((bytecodes)[index + alreadyCompared + 1])) + (((U_16 ) ((bytecodes)[index + alreadyCompared + 0])) << 8))
#endif
/* Modifiers must match exactly */
if (method1->modifiers != method2->modifiers) {
return FALSE;
}
/* Bytecode array sizes must match exactly */
size = J9_BYTECODE_SIZE_FROM_ROM_METHOD(method1);
if (size != J9_BYTECODE_SIZE_FROM_ROM_METHOD(method2)) {
return FALSE;
}
/* For a native or an abstract method, there are no bytecodes.
* A native method has a method signature in place of bytecodes.
* Method signature is derived from the method descriptor.
* As the method descriptor has already been compared by the caller fixMethodEquivalencesAndCallSites(),
* there is no need to compare native method signature.
*/
if (J9_ARE_NO_BITS_SET(J9AccNative | J9AccAbstract, method1->modifiers)) {
/* Walk the bytecodes in parallel looking for mismatches */
bytecodes1 = J9_BYTECODE_START_FROM_ROM_METHOD(method1);
romCP1 = J9_ROM_CP_FROM_ROM_CLASS(romClass1);
bytecodes2 = J9_BYTECODE_START_FROM_ROM_METHOD(method2);
romCP2 = J9_ROM_CP_FROM_ROM_CLASS(romClass2);
index = 0;
while (index < size) {
U_8 bc = bytecodes1[index];
UDATA bytecodeSize;
UDATA alreadyCompared;
U_8 bc2 = bytecodes2[index];
/* Bytecode numbers/indices in each method must be identical */
if (bc != bc2) {
/* Treat all return instructions to JBgenericReturn */
if (RTV_RETURN == (J9JavaBytecodeVerificationTable[bc] >> 8)) {
bc = JBgenericReturn;
}
if (RTV_RETURN == (J9JavaBytecodeVerificationTable[bc2] >> 8)) {
bc2 = JBgenericReturn;
}
if (bc != bc2) {
return FALSE;
}
}
alreadyCompared = 1;
bytecodeSize = J9JavaInstructionSizeAndBranchActionTable[bc] & 7;
switch (bc) {
case JBcheckcast:
case JBinstanceof:
case JBmultianewarray:
case JBnew:
case JBanewarray:
if (!areClassRefsIdentical(romCP1, NEXT_U16(bytecodes1), romCP2, NEXT_U16(bytecodes2))) {
return FALSE;
}
alreadyCompared += 2;
break;
case JBinvokeinterface2:
alreadyCompared += 2;
/* Intentional fall-through */
case JBinvokespecial:
case JBinvokestatic:
case JBinvokespecialsplit:
case JBinvokestaticsplit:
case JBinvokevirtual: {
U_16 cpIndex1 = NEXT_U16(bytecodes1);
U_16 cpIndex2 = NEXT_U16(bytecodes2);
if (JBinvokestaticsplit == bc) {
/* use cpIndex1 as index into slit table */
cpIndex1 = *(U_16 *)(J9ROMCLASS_STATICSPLITMETHODREFINDEXES(romClass1) + cpIndex1);
} else if (JBinvokespecialsplit == bc) {
/* use cpIndex1 as index into slit table */
cpIndex1 = *(U_16 *)(J9ROMCLASS_SPECIALSPLITMETHODREFINDEXES(romClass1) + cpIndex1);
}
if (JBinvokestaticsplit == bc2) {
/* use cpIndex2 as index into slit table */
cpIndex2 = *(U_16 *)(J9ROMCLASS_STATICSPLITMETHODREFINDEXES(romClass2) + cpIndex2);
} else if (JBinvokespecialsplit == bc) {
/* use cpIndex2 as index into slit table */
cpIndex2 = *(U_16 *)(J9ROMCLASS_SPECIALSPLITMETHODREFINDEXES(romClass2) + cpIndex2);
}
if (!areMethodRefsIdentical(romCP1, cpIndex1, romCP2, cpIndex2)) {
return FALSE;
}
alreadyCompared += 2;
break;
}
case JBgetfield:
case JBputfield:
case JBgetstatic:
case JBputstatic:
if (!areFieldRefsIdentical(romCP1, NEXT_U16(bytecodes1), romCP2, NEXT_U16(bytecodes2))) {
return FALSE;
}
alreadyCompared += 2;
break;
case JBldc:
if (!areSingleSlotConstantRefsIdentical(romCP1, bytecodes1[index + alreadyCompared], romCP2, bytecodes2[index + alreadyCompared])) {
return FALSE;
}
alreadyCompared += 1;
break;
case JBldcw:
if (!areSingleSlotConstantRefsIdentical(romCP1, NEXT_U16(bytecodes1), romCP2, NEXT_U16(bytecodes2))) {
return FALSE;
}
alreadyCompared += 2;
break;
case JBldc2dw:
case JBldc2lw:
if (!areDoubleSlotConstantRefsIdentical(romCP1, NEXT_U16(bytecodes1), romCP2, NEXT_U16(bytecodes2))) {
return FALSE;
}
alreadyCompared += 2;
break;
case JBlookupswitch:
case JBtableswitch: {
UDATA tempIndex = index + (4 - (index & 3));
UDATA numEntries;
I_32 low;
tempIndex += 4;
low = *((I_32 *) (bytecodes1 + tempIndex));
tempIndex += 4;
if (bc == JBtableswitch) {
I_32 high = *((I_32 *) (bytecodes1 + tempIndex));
tempIndex += 4;
numEntries = (UDATA) (high - low + 1);
} else {
numEntries = ((UDATA) low) * 2;
}
bytecodeSize = (tempIndex + (4 * numEntries)) - index;
break;
}
case JBinvokedynamic: {
U_16 callSiteIndex1 = NEXT_U16(bytecodes1);
U_16 callSiteIndex2 = NEXT_U16(bytecodes2);
if (!areCallSiteDataMethodsEquivalent(romClass1, callSiteIndex1, romClass2, callSiteIndex2)) {
return FALSE;
}
/* if RAM classes are not provided do not attempt to propagate resolved callsite */
if ((NULL != ramClass1) && (NULL != ramClass2)) {
/* propagate call site data */
if (NULL != ramClass1->callSites[callSiteIndex1]) {
/* callsite resolution exists */
ramClass2->callSites[callSiteIndex2] = ramClass1->callSites[callSiteIndex1];
}
}
alreadyCompared += 2;
break;
}
}
/* Compare any remaining bytes that have not been treated */
if (memcmp(bytecodes1 + index + alreadyCompared, bytecodes2 + index + alreadyCompared, bytecodeSize - alreadyCompared) != 0) {
return FALSE;
}
index += bytecodeSize;
}
}
return TRUE;
#undef NEXT_U16
}
void
fixSubclassHierarchy(J9VMThread * currentThread, J9HashTable * classPairs)
{
J9JavaVM * vm = currentThread->javaVM;
PORT_ACCESS_FROM_JAVAVM(vm);
J9HashTableState hashTableState;
J9JVMTIClassPair * classPair;
J9JVMTIClassPair ** array;
J9JVMTIClassPair exemplar;
J9JVMTIClassPair * result;
UDATA classCount;
UDATA i;
/*
* Update the subclass traversal list with the replaced classes.
*/
classPair = hashTableStartDo(classPairs, &hashTableState);
while (classPair != NULL) {
J9Class *replacementClass = classPair->replacementClass.ramClass;
if (NULL != replacementClass) {
J9Class *originalClass = classPair->originalRAMClass;
J9Class *previous = originalClass->subclassTraversalReverseLink;
J9Class *next = originalClass->subclassTraversalLink;
/* Put the new class into the subclass hierarchy */
previous->subclassTraversalLink = replacementClass;
next->subclassTraversalReverseLink = replacementClass;
replacementClass->subclassTraversalReverseLink = previous;
replacementClass->subclassTraversalLink = next;
/* link this obsolete class to itself so that it won't have dangling pointers into the subclass traversal list */
originalClass->subclassTraversalReverseLink = originalClass;
originalClass->subclassTraversalLink = originalClass;
}
classPair = hashTableNextDo(&hashTableState);
}
/* copy the pairs in the hashTable into an array so that they may be sorted */
classCount = hashTableGetCount(classPairs);
array = j9mem_allocate_memory(classCount * sizeof(*array), OMRMEM_CATEGORY_VM);
if (array == NULL) {
return;
}
classPair = hashTableStartDo(classPairs, &hashTableState);
for (i = 0; i < classCount; ++i) {
array[i] = classPair;
classPair = hashTableNextDo(&hashTableState);
}
/* sort the array so that superclasses appear before subclasses */
J9_SORT(array, (UDATA)classCount, sizeof(J9JVMTIClassPair*), compareClassDepth);
/* Add all the new classes to the graph */
for (i = 0; i < classCount; ++i) {
J9Class * replacementClass;
J9Class * superclass;
if (array[i]->replacementClass.ramClass) {
replacementClass = array[i]->replacementClass.ramClass;
} else {
replacementClass = array[i]->originalRAMClass;
}
superclass = GET_SUPERCLASS(replacementClass);
/* Find the correct superclass. If the superclass of replacementClass
* was replaced itself, make sure we use the new superclass
*/
exemplar.originalRAMClass = superclass;
result = hashTableFind(classPairs, &exemplar);