/
klassVtable.cpp
1645 lines (1484 loc) · 67.6 KB
/
klassVtable.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 (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "jvm.h"
#include "classfile/classLoaderDataGraph.hpp"
#include "classfile/javaClasses.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/vmSymbols.hpp"
#include "interpreter/linkResolver.hpp"
#include "logging/log.hpp"
#include "logging/logStream.hpp"
#include "memory/metaspaceShared.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "oops/instanceKlass.inline.hpp"
#include "oops/klass.inline.hpp"
#include "oops/klassVtable.hpp"
#include "oops/method.hpp"
#include "oops/objArrayOop.hpp"
#include "oops/oop.inline.hpp"
#include "runtime/arguments.hpp"
#include "runtime/flags/flagSetting.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/safepointVerifiers.hpp"
#include "utilities/copy.hpp"
inline InstanceKlass* klassVtable::ik() const {
return InstanceKlass::cast(_klass);
}
bool klassVtable::is_preinitialized_vtable() {
return _klass->is_shared() && !MetaspaceShared::remapped_readwrite();
}
// this function computes the vtable size (including the size needed for miranda
// methods) and the number of miranda methods in this class.
// Note on Miranda methods: Let's say there is a class C that implements
// interface I, and none of C's superclasses implements I.
// Let's say there is an abstract method m in I that neither C
// nor any of its super classes implement (i.e there is no method of any access,
// with the same name and signature as m), then m is a Miranda method which is
// entered as a public abstract method in C's vtable. From then on it should
// treated as any other public method in C for method over-ride purposes.
void klassVtable::compute_vtable_size_and_num_mirandas(
int* vtable_length_ret, int* num_new_mirandas,
GrowableArray<Method*>* all_mirandas, const Klass* super,
Array<Method*>* methods, AccessFlags class_flags, u2 major_version,
Handle classloader, Symbol* classname, Array<InstanceKlass*>* local_interfaces,
TRAPS) {
NoSafepointVerifier nsv;
// set up default result values
int vtable_length = 0;
// start off with super's vtable length
vtable_length = super == NULL ? 0 : super->vtable_length();
// go thru each method in the methods table to see if it needs a new entry
int len = methods->length();
for (int i = 0; i < len; i++) {
Method* method = methods->at(i);
if (needs_new_vtable_entry(method, super, classloader, classname, class_flags, major_version)) {
assert(!method->is_private(), "private methods should not need a vtable entry");
vtable_length += vtableEntry::size(); // we need a new entry
}
}
GrowableArray<Method*> new_mirandas(20);
// compute the number of mirandas methods that must be added to the end
get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces,
class_flags.is_interface());
*num_new_mirandas = new_mirandas.length();
// Interfaces do not need interface methods in their vtables
// This includes miranda methods and during later processing, default methods
if (!class_flags.is_interface()) {
vtable_length += *num_new_mirandas * vtableEntry::size();
}
if (Universe::is_bootstrapping() && vtable_length == 0) {
// array classes don't have their superclass set correctly during
// bootstrapping
vtable_length = Universe::base_vtable_size();
}
if (super == NULL && vtable_length != Universe::base_vtable_size()) {
if (Universe::is_bootstrapping()) {
// Someone is attempting to override java.lang.Object incorrectly on the
// bootclasspath. The JVM cannot recover from this error including throwing
// an exception
vm_exit_during_initialization("Incompatible definition of java.lang.Object");
} else {
// Someone is attempting to redefine java.lang.Object incorrectly. The
// only way this should happen is from
// SystemDictionary::resolve_from_stream(), which will detect this later
// and throw a security exception. So don't assert here to let
// the exception occur.
vtable_length = Universe::base_vtable_size();
}
}
assert(vtable_length % vtableEntry::size() == 0, "bad vtable length");
assert(vtable_length >= Universe::base_vtable_size(), "vtable too small");
*vtable_length_ret = vtable_length;
}
// Copy super class's vtable to the first part (prefix) of this class's vtable,
// and return the number of entries copied. Expects that 'super' is the Java
// super class (arrays can have "array" super classes that must be skipped).
int klassVtable::initialize_from_super(Klass* super) {
if (super == NULL) {
return 0;
} else if (is_preinitialized_vtable()) {
// A shared class' vtable is preinitialized at dump time. No need to copy
// methods from super class for shared class, as that was already done
// during archiving time. However, if Jvmti has redefined a class,
// copy super class's vtable in case the super class has changed.
return super->vtable().length();
} else {
// copy methods from superKlass
klassVtable superVtable = super->vtable();
assert(superVtable.length() <= _length, "vtable too short");
#ifdef ASSERT
superVtable.verify(tty, true);
#endif
superVtable.copy_vtable_to(table());
if (log_develop_is_enabled(Trace, vtables)) {
ResourceMark rm;
log_develop_trace(vtables)("copy vtable from %s to %s size %d",
super->internal_name(), klass()->internal_name(),
_length);
}
return superVtable.length();
}
}
//
// Revised lookup semantics introduced 1.3 (Kestrel beta)
void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
// Note: Arrays can have intermediate array supers. Use java_super to skip them.
InstanceKlass* super = _klass->java_super();
bool is_shared = _klass->is_shared();
if (!_klass->is_array_klass()) {
ResourceMark rm(THREAD);
log_develop_debug(vtables)("Initializing: %s", _klass->name()->as_C_string());
}
#ifdef ASSERT
oop* end_of_obj = (oop*)_klass + _klass->size();
oop* end_of_vtable = (oop*)&table()[_length];
assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");
#endif
if (Universe::is_bootstrapping()) {
assert(!is_shared, "sanity");
// just clear everything
for (int i = 0; i < _length; i++) table()[i].clear();
return;
}
int super_vtable_len = initialize_from_super(super);
if (_klass->is_array_klass()) {
assert(super_vtable_len == _length, "arrays shouldn't introduce new methods");
} else {
assert(_klass->is_instance_klass(), "must be InstanceKlass");
Array<Method*>* methods = ik()->methods();
int len = methods->length();
int initialized = super_vtable_len;
// Check each of this class's methods against super;
// if override, replace in copy of super vtable, otherwise append to end
for (int i = 0; i < len; i++) {
// update_inherited_vtable can stop for gc - ensure using handles
methodHandle mh(THREAD, methods->at(i));
bool needs_new_entry = update_inherited_vtable(mh, super_vtable_len, -1, checkconstraints, CHECK);
if (needs_new_entry) {
put_method_at(mh(), initialized);
mh->set_vtable_index(initialized); // set primary vtable index
initialized++;
}
}
// update vtable with default_methods
Array<Method*>* default_methods = ik()->default_methods();
if (default_methods != NULL) {
len = default_methods->length();
if (len > 0) {
Array<int>* def_vtable_indices = NULL;
if ((def_vtable_indices = ik()->default_vtable_indices()) == NULL) {
assert(!is_shared, "shared class def_vtable_indices does not exist");
def_vtable_indices = ik()->create_new_default_vtable_indices(len, CHECK);
} else {
assert(def_vtable_indices->length() == len, "reinit vtable len?");
}
for (int i = 0; i < len; i++) {
bool needs_new_entry;
{
// Reduce the scope of this handle so that it is fetched again.
// The methodHandle keeps it from being deleted by RedefineClasses while
// we're using it.
methodHandle mh(THREAD, default_methods->at(i));
assert(!mh->is_private(), "private interface method in the default method list");
needs_new_entry = update_inherited_vtable(mh, super_vtable_len, i, checkconstraints, CHECK);
}
// needs new entry
if (needs_new_entry) {
// Refetch this default method in case of redefinition that might
// happen during constraint checking in the update_inherited_vtable call above.
Method* method = default_methods->at(i);
put_method_at(method, initialized);
if (is_preinitialized_vtable()) {
// At runtime initialize_vtable is rerun for a shared class
// (loaded by the non-boot loader) as part of link_class_impl().
// The dumptime vtable index should be the same as the runtime index.
assert(def_vtable_indices->at(i) == initialized,
"dump time vtable index is different from runtime index");
} else {
def_vtable_indices->at_put(i, initialized); //set vtable index
}
initialized++;
}
}
}
}
// add miranda methods; it will also return the updated initialized
// Interfaces do not need interface methods in their vtables
// This includes miranda methods and during later processing, default methods
if (!ik()->is_interface()) {
initialized = fill_in_mirandas(initialized, THREAD);
}
// In class hierarchies where the accessibility is not increasing (i.e., going from private ->
// package_private -> public/protected), the vtable might actually be smaller than our initial
// calculation, for classfile versions for which we do not do transitive override
// calculations.
if (ik()->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION) {
assert(initialized == _length, "vtable initialization failed");
} else {
assert(initialized <= _length, "vtable initialization failed");
for(;initialized < _length; initialized++) {
table()[initialized].clear();
}
}
NOT_PRODUCT(verify(tty, true));
}
}
// Returns true iff super_method can be overridden by a method in targetclassname
// See JLS 3rd edition 8.4.6.1
// Assumes name-signature match
// Note that the InstanceKlass of the method in the targetclassname has not always been created yet
static bool can_be_overridden(Method* super_method, Handle targetclassloader, Symbol* targetclassname) {
// Private methods can not be overridden
assert(!super_method->is_private(), "shouldn't call with a private method");
// If super method is accessible, then override
if ((super_method->is_protected()) ||
(super_method->is_public())) {
return true;
}
// Package-private methods are not inherited outside of package
assert(super_method->is_package_private(), "must be package private");
return(super_method->method_holder()->is_same_class_package(targetclassloader(), targetclassname));
}
// Called for cases where a method does not override its superclass' vtable entry
// For bytecodes not produced by javac together it is possible that a method does not override
// the superclass's method, but might indirectly override a super-super class's vtable entry
// If none found, return a null superk, else return the superk of the method this does override
// For public and protected methods: if they override a superclass, they will
// also be overridden themselves appropriately.
// Private methods do not override, and are not overridden and are not in the vtable.
// Package Private methods are trickier:
// e.g. P1.A, pub m
// P2.B extends A, package private m
// P1.C extends B, public m
// P1.C.m needs to override P1.A.m and can not override P2.B.m
// Therefore: all package private methods need their own vtable entries for
// them to be the root of an inheritance overriding decision
// Package private methods may also override other vtable entries
InstanceKlass* klassVtable::find_transitive_override(InstanceKlass* initialsuper,
const methodHandle& target_method,
int vtable_index,
Handle target_loader,
Symbol* target_classname) {
InstanceKlass* superk = initialsuper;
while (superk != NULL && superk->super() != NULL) {
klassVtable ssVtable = (superk->super())->vtable();
if (vtable_index < ssVtable.length()) {
Method* super_method = ssVtable.method_at(vtable_index);
#ifndef PRODUCT
Symbol* name= target_method()->name();
Symbol* signature = target_method()->signature();
assert(super_method->name() == name && super_method->signature() == signature, "vtable entry name/sig mismatch");
#endif
if (can_be_overridden(super_method, target_loader, target_classname)) {
if (log_develop_is_enabled(Trace, vtables)) {
ResourceMark rm;
LogTarget(Trace, vtables) lt;
LogStream ls(lt);
char* sig = target_method()->name_and_sig_as_C_string();
ls.print("transitive overriding superclass %s with %s index %d, original flags: ",
super_method->method_holder()->internal_name(),
sig, vtable_index);
super_method->print_linkage_flags(&ls);
ls.print("overriders flags: ");
target_method->print_linkage_flags(&ls);
ls.cr();
}
break; // return found superk
}
} else {
// super class has no vtable entry here, stop transitive search
superk = (InstanceKlass*)NULL;
break;
}
// if no override found yet, continue to search up
superk = superk->super() == NULL ? NULL : InstanceKlass::cast(superk->super());
}
return superk;
}
static void log_vtables(int i, bool overrides, const methodHandle& target_method,
Klass* target_klass, Method* super_method,
Thread* thread) {
#ifndef PRODUCT
if (log_develop_is_enabled(Trace, vtables)) {
ResourceMark rm(thread);
LogTarget(Trace, vtables) lt;
LogStream ls(lt);
char* sig = target_method()->name_and_sig_as_C_string();
if (overrides) {
ls.print("overriding with %s index %d, original flags: ",
sig, i);
} else {
ls.print("NOT overriding with %s index %d, original flags: ",
sig, i);
}
super_method->print_linkage_flags(&ls);
ls.print("overriders flags: ");
target_method->print_linkage_flags(&ls);
ls.cr();
}
#endif
}
// Update child's copy of super vtable for overrides
// OR return true if a new vtable entry is required.
// Only called for InstanceKlass's, i.e. not for arrays
// If that changed, could not use _klass as handle for klass
bool klassVtable::update_inherited_vtable(const methodHandle& target_method,
int super_vtable_len, int default_index,
bool checkconstraints, TRAPS) {
ResourceMark rm(THREAD);
bool allocate_new = true;
InstanceKlass* klass = ik();
Array<int>* def_vtable_indices = NULL;
bool is_default = false;
// default methods are non-private concrete methods in superinterfaces which are added
// to the vtable with their real method_holder.
// Since vtable and itable indices share the same storage, don't touch
// the default method's real vtable/itable index.
// default_vtable_indices stores the vtable value relative to this inheritor
if (default_index >= 0 ) {
is_default = true;
def_vtable_indices = klass->default_vtable_indices();
assert(!target_method->is_private(), "private interface method flagged as default");
assert(def_vtable_indices != NULL, "def vtable alloc?");
assert(default_index <= def_vtable_indices->length(), "def vtable len?");
} else {
assert(klass == target_method->method_holder(), "caller resp.");
// Initialize the method's vtable index to "nonvirtual".
// If we allocate a vtable entry, we will update it to a non-negative number.
target_method->set_vtable_index(Method::nonvirtual_vtable_index);
}
// Private, static and <init> methods are never in
if (target_method->is_private() || target_method->is_static() ||
(target_method->name()->fast_compare(vmSymbols::object_initializer_name()) == 0)) {
return false;
}
if (target_method->is_final_method(klass->access_flags())) {
// a final method never needs a new entry; final methods can be statically
// resolved and they have to be present in the vtable only if they override
// a super's method, in which case they re-use its entry
allocate_new = false;
} else if (klass->is_interface()) {
allocate_new = false; // see note below in needs_new_vtable_entry
// An interface never allocates new vtable slots, only inherits old ones.
// This method will either be assigned its own itable index later,
// or be assigned an inherited vtable index in the loop below.
// default methods inherited by classes store their vtable indices
// in the inheritor's default_vtable_indices.
// default methods inherited by interfaces may already have a
// valid itable index, if so, don't change it.
// Overpass methods in an interface will be assigned an itable index later
// by an inheriting class.
if ((!is_default || !target_method->has_itable_index())) {
target_method->set_vtable_index(Method::pending_itable_index);
}
}
// we need a new entry if there is no superclass
Klass* super = klass->super();
if (super == NULL) {
return allocate_new;
}
// search through the vtable and update overridden entries
// Since check_signature_loaders acquires SystemDictionary_lock
// which can block for gc, once we are in this loop, use handles
// For classfiles built with >= jdk7, we now look for transitive overrides
Symbol* name = target_method->name();
Symbol* signature = target_method->signature();
Klass* target_klass = target_method->method_holder();
assert(target_klass != NULL, "impossible");
if (target_klass == NULL) {
target_klass = _klass;
}
HandleMark hm(THREAD);
Handle target_loader(THREAD, target_klass->class_loader());
Symbol* target_classname = target_klass->name();
for(int i = 0; i < super_vtable_len; i++) {
Method* super_method;
if (is_preinitialized_vtable()) {
// If this is a shared class, the vtable is already in the final state (fully
// initialized). Need to look at the super's vtable.
klassVtable superVtable = super->vtable();
super_method = superVtable.method_at(i);
} else {
super_method = method_at(i);
}
// Check if method name matches. Ignore match if klass is an interface and the
// matching method is a non-public java.lang.Object method. (See JVMS 5.4.3.4)
// This is safe because the method at this slot should never get invoked.
// (TBD: put in a method to throw NoSuchMethodError if this slot is ever used.)
if (super_method->name() == name && super_method->signature() == signature &&
(!klass->is_interface() ||
!SystemDictionary::is_nonpublic_Object_method(super_method))) {
// get super_klass for method_holder for the found method
InstanceKlass* super_klass = super_method->method_holder();
// Whether the method is being overridden
bool overrides = false;
// private methods are also never overridden
if (!super_method->is_private() &&
(is_default ||
can_be_overridden(super_method, target_loader, target_classname) ||
(klass->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION &&
(super_klass = find_transitive_override(super_klass,
target_method, i, target_loader,
target_classname)) != NULL))) {
// Package private methods always need a new entry to root their own
// overriding. They may also override other methods.
if (!target_method->is_package_private()) {
allocate_new = false;
}
// Set the vtable index before the constraint check safepoint, which potentially
// redefines this method if this method is a default method belonging to a
// super class or interface.
put_method_at(target_method(), i);
// Do not check loader constraints for overpass methods because overpass
// methods are created by the jvm to throw exceptions.
if (checkconstraints && !target_method->is_overpass()) {
// Override vtable entry if passes loader constraint check
// if loader constraint checking requested
// No need to visit his super, since he and his super
// have already made any needed loader constraints.
// Since loader constraints are transitive, it is enough
// to link to the first super, and we get all the others.
Handle super_loader(THREAD, super_klass->class_loader());
if (target_loader() != super_loader()) {
ResourceMark rm(THREAD);
Symbol* failed_type_symbol =
SystemDictionary::check_signature_loaders(signature, _klass,
target_loader, super_loader,
true, CHECK_(false));
if (failed_type_symbol != NULL) {
stringStream ss;
ss.print("loader constraint violation for class %s: when selecting "
"overriding method '", klass->external_name());
target_method->print_external_name(&ss),
ss.print("' the class loader %s of the "
"selected method's type %s, and the class loader %s for its super "
"type %s have different Class objects for the type %s used in the signature (%s; %s)",
target_klass->class_loader_data()->loader_name_and_id(),
target_klass->external_name(),
super_klass->class_loader_data()->loader_name_and_id(),
super_klass->external_name(),
failed_type_symbol->as_klass_external_name(),
target_klass->class_in_module_of_loader(false, true),
super_klass->class_in_module_of_loader(false, true));
THROW_MSG_(vmSymbols::java_lang_LinkageError(), ss.as_string(), false);
}
}
}
overrides = true;
if (!is_default) {
target_method->set_vtable_index(i);
} else {
if (def_vtable_indices != NULL) {
if (is_preinitialized_vtable()) {
// At runtime initialize_vtable is rerun as part of link_class_impl()
// for a shared class loaded by the non-boot loader.
// The dumptime vtable index should be the same as the runtime index.
assert(def_vtable_indices->at(default_index) == i,
"dump time vtable index is different from runtime index");
} else {
def_vtable_indices->at_put(default_index, i);
}
}
assert(super_method->is_default_method() || super_method->is_overpass()
|| super_method->is_abstract(), "default override error");
}
} else {
overrides = false;
}
log_vtables(i, overrides, target_method, target_klass, super_method, THREAD);
}
}
return allocate_new;
}
void klassVtable::put_method_at(Method* m, int index) {
assert(!m->is_private(), "private methods should not be in vtable");
JVMTI_ONLY(assert(!m->is_old() || ik()->is_being_redefined(), "old methods should not be in vtable"));
if (is_preinitialized_vtable()) {
// At runtime initialize_vtable is rerun as part of link_class_impl()
// for shared class loaded by the non-boot loader to obtain the loader
// constraints based on the runtime classloaders' context. The dumptime
// method at the vtable index should be the same as the runtime method.
assert(table()[index].method() == m,
"archived method is different from the runtime method");
} else {
if (log_develop_is_enabled(Trace, vtables)) {
ResourceMark rm;
LogTarget(Trace, vtables) lt;
LogStream ls(lt);
const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
ls.print("adding %s at index %d, flags: ", sig, index);
if (m != NULL) {
m->print_linkage_flags(&ls);
}
ls.cr();
}
table()[index].set(m);
}
}
// Find out if a method "m" with superclass "super", loader "classloader" and
// name "classname" needs a new vtable entry. Let P be a class package defined
// by "classloader" and "classname".
// NOTE: The logic used here is very similar to the one used for computing
// the vtables indices for a method. We cannot directly use that function because,
// we allocate the InstanceKlass at load time, and that requires that the
// superclass has been loaded.
// However, the vtable entries are filled in at link time, and therefore
// the superclass' vtable may not yet have been filled in.
bool klassVtable::needs_new_vtable_entry(Method* target_method,
const Klass* super,
Handle classloader,
Symbol* classname,
AccessFlags class_flags,
u2 major_version) {
if (class_flags.is_interface()) {
// Interfaces do not use vtables, except for java.lang.Object methods,
// so there is no point to assigning
// a vtable index to any of their local methods. If we refrain from doing this,
// we can use Method::_vtable_index to hold the itable index
return false;
}
if (target_method->is_final_method(class_flags) ||
// a final method never needs a new entry; final methods can be statically
// resolved and they have to be present in the vtable only if they override
// a super's method, in which case they re-use its entry
(target_method->is_private()) ||
// private methods don't need to be in vtable
(target_method->is_static()) ||
// static methods don't need to be in vtable
(target_method->name()->fast_compare(vmSymbols::object_initializer_name()) == 0)
// <init> is never called dynamically-bound
) {
return false;
}
// Concrete interface methods do not need new entries, they override
// abstract method entries using default inheritance rules
if (target_method->method_holder() != NULL &&
target_method->method_holder()->is_interface() &&
!target_method->is_abstract()) {
assert(target_method->is_default_method(),
"unexpected interface method type");
return false;
}
// we need a new entry if there is no superclass
if (super == NULL) {
return true;
}
// Package private methods always need a new entry to root their own
// overriding. This allows transitive overriding to work.
if (target_method->is_package_private()) {
return true;
}
// search through the super class hierarchy to see if we need
// a new entry
Symbol* name = target_method->name();
Symbol* signature = target_method->signature();
const Klass* k = super;
Method* super_method = NULL;
InstanceKlass *holder = NULL;
Method* recheck_method = NULL;
bool found_pkg_prvt_method = false;
while (k != NULL) {
// lookup through the hierarchy for a method with matching name and sign.
super_method = InstanceKlass::cast(k)->lookup_method(name, signature);
if (super_method == NULL) {
break; // we still have to search for a matching miranda method
}
// get the class holding the matching method
InstanceKlass* superk = super_method->method_holder();
// we want only instance method matches
// ignore private methods found via lookup_method since they do not participate in overriding,
// and since we do override around them: e.g. a.m pub/b.m private/c.m pub,
// ignore private, c.m pub does override a.m pub
// For classes that were not javac'd together, we also do transitive overriding around
// methods that have less accessibility
if (!super_method->is_static() &&
!super_method->is_private()) {
if (can_be_overridden(super_method, classloader, classname)) {
return false;
// else keep looking for transitive overrides
}
// If we get here then one of the super classes has a package private method
// that will not get overridden because it is in a different package. But,
// that package private method does "override" any matching methods in super
// interfaces, so there will be no miranda vtable entry created. So, set flag
// to TRUE for use below, in case there are no methods in super classes that
// this target method overrides.
assert(super_method->is_package_private(), "super_method must be package private");
assert(!superk->is_same_class_package(classloader(), classname),
"Must be different packages");
found_pkg_prvt_method = true;
}
// Start with lookup result and continue to search up, for versions supporting transitive override
if (major_version >= VTABLE_TRANSITIVE_OVERRIDE_VERSION) {
k = superk->super(); // haven't found an override match yet; continue to look
} else {
break;
}
}
// If found_pkg_prvt_method is set, then the ONLY matching method in the
// superclasses is package private in another package. That matching method will
// prevent a miranda vtable entry from being created. Because the target method can not
// override the package private method in another package, then it needs to be the root
// for its own vtable entry.
if (found_pkg_prvt_method) {
return true;
}
// if the target method is public or protected it may have a matching
// miranda method in the super, whose entry it should re-use.
// Actually, to handle cases that javac would not generate, we need
// this check for all access permissions.
const InstanceKlass *sk = InstanceKlass::cast(super);
if (sk->has_miranda_methods()) {
if (sk->lookup_method_in_all_interfaces(name, signature, Klass::DefaultsLookupMode::find) != NULL) {
return false; // found a matching miranda; we do not need a new entry
}
}
return true; // found no match; we need a new entry
}
// Support for miranda methods
// get the vtable index of a miranda method with matching "name" and "signature"
int klassVtable::index_of_miranda(Symbol* name, Symbol* signature) {
// search from the bottom, might be faster
for (int i = (length() - 1); i >= 0; i--) {
Method* m = table()[i].method();
if (is_miranda_entry_at(i) &&
m->name() == name && m->signature() == signature) {
return i;
}
}
return Method::invalid_vtable_index;
}
// check if an entry at an index is miranda
// requires that method m at entry be declared ("held") by an interface.
bool klassVtable::is_miranda_entry_at(int i) {
Method* m = method_at(i);
InstanceKlass* holder = m->method_holder();
// miranda methods are public abstract instance interface methods in a class's vtable
if (holder->is_interface()) {
assert(m->is_public(), "should be public");
assert(ik()->implements_interface(holder) , "this class should implement the interface");
if (is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super(), klass()->is_interface())) {
return true;
}
}
return false;
}
// Check if a method is a miranda method, given a class's methods array,
// its default_method table and its super class.
// "Miranda" means an abstract non-private method that would not be
// overridden for the local class.
// A "miranda" method should only include non-private interface
// instance methods, i.e. not private methods, not static methods,
// not default methods (concrete interface methods), not overpass methods.
// If a given class already has a local (including overpass) method, a
// default method, or any of its superclasses has the same which would have
// overridden an abstract method, then this is not a miranda method.
//
// Miranda methods are checked multiple times.
// Pass 1: during class load/class file parsing: before vtable size calculation:
// include superinterface abstract and default methods (non-private instance).
// We include potential default methods to give them space in the vtable.
// During the first run, the current instanceKlass has not yet been
// created, the superclasses and superinterfaces do have instanceKlasses
// but may not have vtables, the default_methods list is empty, no overpasses.
// Default method generation uses the all_mirandas array as the starter set for
// maximally-specific default method calculation. So, for both classes and
// interfaces, it is necessary that the first pass will find all non-private
// interface instance methods, whether or not they are concrete.
//
// Pass 2: recalculated during vtable initialization: only include abstract methods.
// The goal of pass 2 is to walk through the superinterfaces to see if any of
// the superinterface methods (which were all abstract pre-default methods)
// need to be added to the vtable.
// With the addition of default methods, we have three new challenges:
// overpasses, static interface methods and private interface methods.
// Static and private interface methods do not get added to the vtable and
// are not seen by the method resolution process, so we skip those.
// Overpass methods are already in the vtable, so vtable lookup will
// find them and we don't need to add a miranda method to the end of
// the vtable. So we look for overpass methods and if they are found we
// return false. Note that we inherit our superclasses vtable, so
// the superclass' search also needs to use find_overpass so that if
// one is found we return false.
// False means - we don't need a miranda method added to the vtable.
//
// During the second run, default_methods is set up, so concrete methods from
// superinterfaces with matching names/signatures to default_methods are already
// in the default_methods list and do not need to be appended to the vtable
// as mirandas. Abstract methods may already have been handled via
// overpasses - either local or superclass overpasses, which may be
// in the vtable already.
//
// Pass 3: They are also checked by link resolution and selection,
// for invocation on a method (not interface method) reference that
// resolves to a method with an interface as its method_holder.
// Used as part of walking from the bottom of the vtable to find
// the vtable index for the miranda method.
//
// Part of the Miranda Rights in the US mean that if you do not have
// an attorney one will be appointed for you.
bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods,
Array<Method*>* default_methods, const Klass* super,
bool is_interface) {
if (m->is_static() || m->is_private() || m->is_overpass()) {
return false;
}
Symbol* name = m->name();
Symbol* signature = m->signature();
// First look in local methods to see if already covered
if (InstanceKlass::find_local_method(class_methods, name, signature,
Klass::OverpassLookupMode::find,
Klass::StaticLookupMode::skip,
Klass::PrivateLookupMode::skip) != NULL)
{
return false;
}
// Check local default methods
if ((default_methods != NULL) &&
(InstanceKlass::find_method(default_methods, name, signature) != NULL))
{
return false;
}
// Iterate on all superclasses, which should be InstanceKlasses.
// Note that we explicitly look for overpasses at each level.
// Overpasses may or may not exist for supers for pass 1,
// they should have been created for pass 2 and later.
for (const Klass* cursuper = super; cursuper != NULL; cursuper = cursuper->super())
{
Method* found_mth = InstanceKlass::cast(cursuper)->find_local_method(name, signature,
Klass::OverpassLookupMode::find,
Klass::StaticLookupMode::skip,
Klass::PrivateLookupMode::skip);
// Ignore non-public methods in java.lang.Object if klass is an interface.
if (found_mth != NULL && (!is_interface ||
!SystemDictionary::is_nonpublic_Object_method(found_mth))) {
return false;
}
}
return true;
}
// Scans current_interface_methods for miranda methods that do not
// already appear in new_mirandas, or default methods, and are also not defined-and-non-private
// in super (superclass). These mirandas are added to all_mirandas if it is
// not null; in addition, those that are not duplicates of miranda methods
// inherited by super from its interfaces are added to new_mirandas.
// Thus, new_mirandas will be the set of mirandas that this class introduces,
// all_mirandas will be the set of all mirandas applicable to this class
// including all defined in superclasses.
void klassVtable::add_new_mirandas_to_lists(
GrowableArray<Method*>* new_mirandas, GrowableArray<Method*>* all_mirandas,
Array<Method*>* current_interface_methods, Array<Method*>* class_methods,
Array<Method*>* default_methods, const Klass* super, bool is_interface) {
// iterate thru the current interface's method to see if it a miranda
int num_methods = current_interface_methods->length();
for (int i = 0; i < num_methods; i++) {
Method* im = current_interface_methods->at(i);
bool is_duplicate = false;
int num_of_current_mirandas = new_mirandas->length();
// check for duplicate mirandas in different interfaces we implement
for (int j = 0; j < num_of_current_mirandas; j++) {
Method* miranda = new_mirandas->at(j);
if ((im->name() == miranda->name()) &&
(im->signature() == miranda->signature())) {
is_duplicate = true;
break;
}
}
if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable
if (is_miranda(im, class_methods, default_methods, super, is_interface)) { // is it a miranda at all?
const InstanceKlass *sk = InstanceKlass::cast(super);
// check if it is a duplicate of a super's miranda
if (sk->lookup_method_in_all_interfaces(im->name(), im->signature(), Klass::DefaultsLookupMode::find) == NULL) {
new_mirandas->append(im);
}
if (all_mirandas != NULL) {
all_mirandas->append(im);
}
}
}
}
}
void klassVtable::get_mirandas(GrowableArray<Method*>* new_mirandas,
GrowableArray<Method*>* all_mirandas,
const Klass* super,
Array<Method*>* class_methods,
Array<Method*>* default_methods,
Array<InstanceKlass*>* local_interfaces,
bool is_interface) {
assert((new_mirandas->length() == 0) , "current mirandas must be 0");
// iterate thru the local interfaces looking for a miranda
int num_local_ifs = local_interfaces->length();
for (int i = 0; i < num_local_ifs; i++) {
InstanceKlass *ik = local_interfaces->at(i);
add_new_mirandas_to_lists(new_mirandas, all_mirandas,
ik->methods(), class_methods,
default_methods, super, is_interface);
// iterate thru each local's super interfaces
Array<InstanceKlass*>* super_ifs = ik->transitive_interfaces();
int num_super_ifs = super_ifs->length();
for (int j = 0; j < num_super_ifs; j++) {
InstanceKlass *sik = super_ifs->at(j);
add_new_mirandas_to_lists(new_mirandas, all_mirandas,
sik->methods(), class_methods,
default_methods, super, is_interface);
}
}
}
// Discover miranda methods ("miranda" = "interface abstract, no binding"),
// and append them into the vtable starting at index initialized,
// return the new value of initialized.
// Miranda methods use vtable entries, but do not get assigned a vtable_index
// The vtable_index is discovered by searching from the end of the vtable
int klassVtable::fill_in_mirandas(int initialized, TRAPS) {
ResourceMark rm(THREAD);
GrowableArray<Method*> mirandas(20);
get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
ik()->default_methods(), ik()->local_interfaces(),
klass()->is_interface());
for (int i = 0; i < mirandas.length(); i++) {
if (log_develop_is_enabled(Trace, vtables)) {
Method* meth = mirandas.at(i);
LogTarget(Trace, vtables) lt;
LogStream ls(lt);
if (meth != NULL) {
char* sig = meth->name_and_sig_as_C_string();
ls.print("fill in mirandas with %s index %d, flags: ",
sig, initialized);
meth->print_linkage_flags(&ls);
ls.cr();
}
}
put_method_at(mirandas.at(i), initialized);
++initialized;
}
return initialized;
}
// Copy this class's vtable to the vtable beginning at start.
// Used to copy superclass vtable to prefix of subclass's vtable.
void klassVtable::copy_vtable_to(vtableEntry* start) {
Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size());
}
#if INCLUDE_JVMTI
bool klassVtable::adjust_default_method(int vtable_index, Method* old_method, Method* new_method) {
// If old_method is default, find this vtable index in default_vtable_indices
// and replace that method in the _default_methods list
bool updated = false;
Array<Method*>* default_methods = ik()->default_methods();
if (default_methods != NULL) {
int len = default_methods->length();
for (int idx = 0; idx < len; idx++) {
if (vtable_index == ik()->default_vtable_indices()->at(idx)) {
if (default_methods->at(idx) == old_method) {
default_methods->at_put(idx, new_method);
updated = true;
}
break;
}
}
}
return updated;
}
// search the vtable for uses of either obsolete or EMCP methods
void klassVtable::adjust_method_entries(bool * trace_name_printed) {
int prn_enabled = 0;
ResourceMark rm;
for (int index = 0; index < length(); index++) {
Method* old_method = unchecked_method_at(index);
if (old_method == NULL || !old_method->is_old()) {