Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/oops/klassVtable.cpp
32285 views
1
/*
2
* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#include "precompiled.hpp"
26
#include "classfile/systemDictionary.hpp"
27
#include "classfile/vmSymbols.hpp"
28
#include "gc_implementation/shared/markSweep.inline.hpp"
29
#include "memory/gcLocker.hpp"
30
#include "memory/metaspaceShared.hpp"
31
#include "memory/resourceArea.hpp"
32
#include "memory/universe.inline.hpp"
33
#include "oops/instanceKlass.hpp"
34
#include "oops/klassVtable.hpp"
35
#include "oops/method.hpp"
36
#include "oops/objArrayOop.hpp"
37
#include "oops/oop.inline.hpp"
38
#include "prims/jvmtiRedefineClassesTrace.hpp"
39
#include "runtime/arguments.hpp"
40
#include "runtime/handles.inline.hpp"
41
#include "utilities/copy.hpp"
42
43
PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
44
45
inline InstanceKlass* klassVtable::ik() const {
46
Klass* k = _klass();
47
assert(k->oop_is_instance(), "not an InstanceKlass");
48
return (InstanceKlass*)k;
49
}
50
51
bool klassVtable::is_preinitialized_vtable() {
52
return _klass->is_shared() && !MetaspaceShared::remapped_readwrite();
53
}
54
55
56
// this function computes the vtable size (including the size needed for miranda
57
// methods) and the number of miranda methods in this class.
58
// Note on Miranda methods: Let's say there is a class C that implements
59
// interface I, and none of C's superclasses implements I.
60
// Let's say there is an abstract method m in I that neither C
61
// nor any of its super classes implement (i.e there is no method of any access,
62
// with the same name and signature as m), then m is a Miranda method which is
63
// entered as a public abstract method in C's vtable. From then on it should
64
// treated as any other public method in C for method over-ride purposes.
65
void klassVtable::compute_vtable_size_and_num_mirandas(
66
int* vtable_length_ret, int* num_new_mirandas,
67
GrowableArray<Method*>* all_mirandas, Klass* super,
68
Array<Method*>* methods, AccessFlags class_flags,
69
Handle classloader, Symbol* classname, Array<Klass*>* local_interfaces,
70
TRAPS) {
71
No_Safepoint_Verifier nsv;
72
73
// set up default result values
74
int vtable_length = 0;
75
76
// start off with super's vtable length
77
InstanceKlass* sk = (InstanceKlass*)super;
78
vtable_length = super == NULL ? 0 : sk->vtable_length();
79
80
// go thru each method in the methods table to see if it needs a new entry
81
int len = methods->length();
82
for (int i = 0; i < len; i++) {
83
assert(methods->at(i)->is_method(), "must be a Method*");
84
methodHandle mh(THREAD, methods->at(i));
85
86
if (needs_new_vtable_entry(mh, super, classloader, classname, class_flags, THREAD)) {
87
vtable_length += vtableEntry::size(); // we need a new entry
88
}
89
}
90
91
GrowableArray<Method*> new_mirandas(20);
92
// compute the number of mirandas methods that must be added to the end
93
get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces);
94
*num_new_mirandas = new_mirandas.length();
95
96
// Interfaces do not need interface methods in their vtables
97
// This includes miranda methods and during later processing, default methods
98
if (!class_flags.is_interface()) {
99
vtable_length += *num_new_mirandas * vtableEntry::size();
100
}
101
102
if (Universe::is_bootstrapping() && vtable_length == 0) {
103
// array classes don't have their superclass set correctly during
104
// bootstrapping
105
vtable_length = Universe::base_vtable_size();
106
}
107
108
if (super == NULL && !Universe::is_bootstrapping() &&
109
vtable_length != Universe::base_vtable_size()) {
110
// Someone is attempting to redefine java.lang.Object incorrectly. The
111
// only way this should happen is from
112
// SystemDictionary::resolve_from_stream(), which will detect this later
113
// and throw a security exception. So don't assert here to let
114
// the exception occur.
115
vtable_length = Universe::base_vtable_size();
116
}
117
assert(super != NULL || vtable_length == Universe::base_vtable_size(),
118
"bad vtable size for class Object");
119
assert(vtable_length % vtableEntry::size() == 0, "bad vtable length");
120
assert(vtable_length >= Universe::base_vtable_size(), "vtable too small");
121
122
*vtable_length_ret = vtable_length;
123
}
124
125
int klassVtable::index_of(Method* m, int len) const {
126
assert(m->has_vtable_index(), "do not ask this of non-vtable methods");
127
return m->vtable_index();
128
}
129
130
// Copy super class's vtable to the first part (prefix) of this class's vtable,
131
// and return the number of entries copied. Expects that 'super' is the Java
132
// super class (arrays can have "array" super classes that must be skipped).
133
int klassVtable::initialize_from_super(KlassHandle super) {
134
if (super.is_null()) {
135
return 0;
136
} else if (is_preinitialized_vtable()) {
137
// A shared class' vtable is preinitialized at dump time. No need to copy
138
// methods from super class for shared class, as that was already done
139
// during archiving time. However, if Jvmti has redefined a class,
140
// copy super class's vtable in case the super class has changed.
141
return super->vtable()->length();
142
} else {
143
// copy methods from superKlass
144
// can't inherit from array class, so must be InstanceKlass
145
assert(super->oop_is_instance(), "must be instance klass");
146
InstanceKlass* sk = (InstanceKlass*)super();
147
klassVtable* superVtable = sk->vtable();
148
assert(superVtable->length() <= _length, "vtable too short");
149
#ifdef ASSERT
150
superVtable->verify(tty, true);
151
#endif
152
superVtable->copy_vtable_to(table());
153
#ifndef PRODUCT
154
if (PrintVtables && Verbose) {
155
ResourceMark rm;
156
tty->print_cr("copy vtable from %s to %s size %d", sk->internal_name(), klass()->internal_name(), _length);
157
}
158
#endif
159
return superVtable->length();
160
}
161
}
162
163
//
164
// Revised lookup semantics introduced 1.3 (Kestrel beta)
165
void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
166
167
// Note: Arrays can have intermediate array supers. Use java_super to skip them.
168
KlassHandle super (THREAD, klass()->java_super());
169
int nofNewEntries = 0;
170
171
bool is_shared = _klass->is_shared();
172
173
if (PrintVtables && !klass()->oop_is_array()) {
174
ResourceMark rm(THREAD);
175
tty->print_cr("Initializing: %s", _klass->name()->as_C_string());
176
}
177
178
#ifdef ASSERT
179
oop* end_of_obj = (oop*)_klass() + _klass()->size();
180
oop* end_of_vtable = (oop*)&table()[_length];
181
assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");
182
#endif
183
184
if (Universe::is_bootstrapping()) {
185
assert(!is_shared, "sanity");
186
// just clear everything
187
for (int i = 0; i < _length; i++) table()[i].clear();
188
return;
189
}
190
191
int super_vtable_len = initialize_from_super(super);
192
if (klass()->oop_is_array()) {
193
assert(super_vtable_len == _length, "arrays shouldn't introduce new methods");
194
} else {
195
assert(_klass->oop_is_instance(), "must be InstanceKlass");
196
197
Array<Method*>* methods = ik()->methods();
198
int len = methods->length();
199
int initialized = super_vtable_len;
200
201
// Check each of this class's methods against super;
202
// if override, replace in copy of super vtable, otherwise append to end
203
for (int i = 0; i < len; i++) {
204
// update_inherited_vtable can stop for gc - ensure using handles
205
HandleMark hm(THREAD);
206
assert(methods->at(i)->is_method(), "must be a Method*");
207
methodHandle mh(THREAD, methods->at(i));
208
209
bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, -1, checkconstraints, CHECK);
210
211
if (needs_new_entry) {
212
put_method_at(mh(), initialized);
213
mh()->set_vtable_index(initialized); // set primary vtable index
214
initialized++;
215
}
216
}
217
218
// update vtable with default_methods
219
Array<Method*>* default_methods = ik()->default_methods();
220
if (default_methods != NULL) {
221
len = default_methods->length();
222
if (len > 0) {
223
Array<int>* def_vtable_indices = NULL;
224
if ((def_vtable_indices = ik()->default_vtable_indices()) == NULL) {
225
assert(!is_shared, "shared class def_vtable_indices does not exist");
226
def_vtable_indices = ik()->create_new_default_vtable_indices(len, CHECK);
227
} else {
228
assert(def_vtable_indices->length() == len, "reinit vtable len?");
229
}
230
for (int i = 0; i < len; i++) {
231
HandleMark hm(THREAD);
232
assert(default_methods->at(i)->is_method(), "must be a Method*");
233
methodHandle mh(THREAD, default_methods->at(i));
234
235
bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, i, checkconstraints, CHECK);
236
237
// needs new entry
238
if (needs_new_entry) {
239
put_method_at(mh(), initialized);
240
if (is_preinitialized_vtable()) {
241
// At runtime initialize_vtable is rerun for a shared class
242
// (loaded by the non-boot loader) as part of link_class_impl().
243
// The dumptime vtable index should be the same as the runtime index.
244
assert(def_vtable_indices->at(i) == initialized,
245
"dump time vtable index is different from runtime index");
246
} else {
247
def_vtable_indices->at_put(i, initialized); //set vtable index
248
}
249
initialized++;
250
}
251
}
252
}
253
}
254
255
// add miranda methods; it will also return the updated initialized
256
// Interfaces do not need interface methods in their vtables
257
// This includes miranda methods and during later processing, default methods
258
if (!ik()->is_interface()) {
259
initialized = fill_in_mirandas(initialized);
260
}
261
262
// In class hierarchies where the accessibility is not increasing (i.e., going from private ->
263
// package_private -> public/protected), the vtable might actually be smaller than our initial
264
// calculation.
265
assert(initialized <= _length, "vtable initialization failed");
266
for(;initialized < _length; initialized++) {
267
put_method_at(NULL, initialized);
268
}
269
NOT_PRODUCT(verify(tty, true));
270
}
271
}
272
273
// Called for cases where a method does not override its superclass' vtable entry
274
// For bytecodes not produced by javac together it is possible that a method does not override
275
// the superclass's method, but might indirectly override a super-super class's vtable entry
276
// If none found, return a null superk, else return the superk of the method this does override
277
// For public and protected methods: if they override a superclass, they will
278
// also be overridden themselves appropriately.
279
// Private methods do not override and are not overridden.
280
// Package Private methods are trickier:
281
// e.g. P1.A, pub m
282
// P2.B extends A, package private m
283
// P1.C extends B, public m
284
// P1.C.m needs to override P1.A.m and can not override P2.B.m
285
// Therefore: all package private methods need their own vtable entries for
286
// them to be the root of an inheritance overriding decision
287
// Package private methods may also override other vtable entries
288
InstanceKlass* klassVtable::find_transitive_override(InstanceKlass* initialsuper, methodHandle target_method,
289
int vtable_index, Handle target_loader, Symbol* target_classname, Thread * THREAD) {
290
InstanceKlass* superk = initialsuper;
291
while (superk != NULL && superk->super() != NULL) {
292
klassVtable* ssVtable = (superk->super())->vtable();
293
if (vtable_index < ssVtable->length()) {
294
Method* super_method = ssVtable->method_at(vtable_index);
295
// get the class holding the matching method
296
// make sure you use that class for is_override
297
InstanceKlass* supermethodholder = super_method->method_holder();
298
#ifndef PRODUCT
299
Symbol* name= target_method()->name();
300
Symbol* signature = target_method()->signature();
301
assert(super_method->name() == name && super_method->signature() == signature, "vtable entry name/sig mismatch");
302
#endif
303
304
if (supermethodholder->is_override(super_method, target_loader, target_classname, THREAD)) {
305
#ifndef PRODUCT
306
if (PrintVtables && Verbose) {
307
ResourceMark rm(THREAD);
308
char* sig = target_method()->name_and_sig_as_C_string();
309
tty->print("transitive overriding superclass %s with %s::%s index %d, original flags: ",
310
supermethodholder->internal_name(),
311
_klass->internal_name(), sig, vtable_index);
312
super_method->access_flags().print_on(tty);
313
if (super_method->is_default_method()) {
314
tty->print("default ");
315
}
316
tty->print("overriders flags: ");
317
target_method->access_flags().print_on(tty);
318
if (target_method->is_default_method()) {
319
tty->print("default ");
320
}
321
}
322
#endif /*PRODUCT*/
323
break; // return found superk
324
}
325
} else {
326
// super class has no vtable entry here, stop transitive search
327
superk = (InstanceKlass*)NULL;
328
break;
329
}
330
// if no override found yet, continue to search up
331
superk = InstanceKlass::cast(superk->super());
332
}
333
334
return superk;
335
}
336
337
// Update child's copy of super vtable for overrides
338
// OR return true if a new vtable entry is required.
339
// Only called for InstanceKlass's, i.e. not for arrays
340
// If that changed, could not use _klass as handle for klass
341
bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method,
342
int super_vtable_len, int default_index,
343
bool checkconstraints, TRAPS) {
344
ResourceMark rm;
345
bool allocate_new = true;
346
assert(klass->oop_is_instance(), "must be InstanceKlass");
347
348
Array<int>* def_vtable_indices = NULL;
349
bool is_default = false;
350
// default methods are concrete methods in superinterfaces which are added to the vtable
351
// with their real method_holder
352
// Since vtable and itable indices share the same storage, don't touch
353
// the default method's real vtable/itable index
354
// default_vtable_indices stores the vtable value relative to this inheritor
355
if (default_index >= 0 ) {
356
is_default = true;
357
def_vtable_indices = klass->default_vtable_indices();
358
assert(def_vtable_indices != NULL, "def vtable alloc?");
359
assert(default_index <= def_vtable_indices->length(), "def vtable len?");
360
} else {
361
assert(klass == target_method()->method_holder(), "caller resp.");
362
// Initialize the method's vtable index to "nonvirtual".
363
// If we allocate a vtable entry, we will update it to a non-negative number.
364
target_method()->set_vtable_index(Method::nonvirtual_vtable_index);
365
}
366
367
// Static and <init> methods are never in
368
if (target_method()->is_static() || target_method()->name() == vmSymbols::object_initializer_name()) {
369
return false;
370
}
371
372
if (target_method->is_final_method(klass->access_flags())) {
373
// a final method never needs a new entry; final methods can be statically
374
// resolved and they have to be present in the vtable only if they override
375
// a super's method, in which case they re-use its entry
376
allocate_new = false;
377
} else if (klass->is_interface()) {
378
allocate_new = false; // see note below in needs_new_vtable_entry
379
// An interface never allocates new vtable slots, only inherits old ones.
380
// This method will either be assigned its own itable index later,
381
// or be assigned an inherited vtable index in the loop below.
382
// default methods inherited by classes store their vtable indices
383
// in the inheritor's default_vtable_indices
384
// default methods inherited by interfaces may already have a
385
// valid itable index, if so, don't change it
386
// overpass methods in an interface will be assigned an itable index later
387
// by an inheriting class
388
if (!is_default || !target_method()->has_itable_index()) {
389
target_method()->set_vtable_index(Method::pending_itable_index);
390
}
391
}
392
393
// we need a new entry if there is no superclass
394
Klass* super = klass->super();
395
if (super == NULL) {
396
return allocate_new;
397
}
398
399
// private methods in classes always have a new entry in the vtable
400
// specification interpretation since classic has
401
// private methods not overriding
402
// JDK8 adds private methods in interfaces which require invokespecial
403
if (target_method()->is_private()) {
404
return allocate_new;
405
}
406
407
// search through the vtable and update overridden entries
408
// Since check_signature_loaders acquires SystemDictionary_lock
409
// which can block for gc, once we are in this loop, use handles
410
// For classfiles built with >= jdk7, we now look for transitive overrides
411
412
Symbol* name = target_method()->name();
413
Symbol* signature = target_method()->signature();
414
415
KlassHandle target_klass(THREAD, target_method()->method_holder());
416
if (target_klass == NULL) {
417
target_klass = _klass;
418
}
419
420
Handle target_loader(THREAD, target_klass->class_loader());
421
422
Symbol* target_classname = target_klass->name();
423
for(int i = 0; i < super_vtable_len; i++) {
424
Method* super_method;
425
if (is_preinitialized_vtable()) {
426
// If this is a shared class, the vtable is already in the final state (fully
427
// initialized). Need to look at the super's vtable.
428
klassVtable* superVtable = super->vtable();
429
super_method = superVtable->method_at(i);
430
} else {
431
super_method = method_at(i);
432
}
433
// Check if method name matches
434
if (super_method->name() == name && super_method->signature() == signature) {
435
436
// get super_klass for method_holder for the found method
437
InstanceKlass* super_klass = super_method->method_holder();
438
439
// private methods are also never overridden
440
if (!super_method->is_private() &&
441
(is_default
442
|| ((super_klass->is_override(super_method, target_loader, target_classname, THREAD))
443
|| ((klass->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION)
444
&& ((super_klass = find_transitive_override(super_klass,
445
target_method, i, target_loader,
446
target_classname, THREAD))
447
!= (InstanceKlass*)NULL)))))
448
{
449
// Package private methods always need a new entry to root their own
450
// overriding. They may also override other methods.
451
if (!target_method()->is_package_private()) {
452
allocate_new = false;
453
}
454
455
if (checkconstraints) {
456
// Override vtable entry if passes loader constraint check
457
// if loader constraint checking requested
458
// No need to visit his super, since he and his super
459
// have already made any needed loader constraints.
460
// Since loader constraints are transitive, it is enough
461
// to link to the first super, and we get all the others.
462
Handle super_loader(THREAD, super_klass->class_loader());
463
464
if (target_loader() != super_loader()) {
465
ResourceMark rm(THREAD);
466
Symbol* failed_type_symbol =
467
SystemDictionary::check_signature_loaders(signature, target_loader,
468
super_loader, true,
469
CHECK_(false));
470
if (failed_type_symbol != NULL) {
471
const char* msg = "loader constraint violation: when resolving "
472
"overridden method \"%s\" the class loader (instance"
473
" of %s) of the current class, %s, and its superclass loader "
474
"(instance of %s), have different Class objects for the type "
475
"%s used in the signature";
476
char* sig = target_method()->name_and_sig_as_C_string();
477
const char* loader1 = SystemDictionary::loader_name(target_loader());
478
char* current = target_klass->name()->as_C_string();
479
const char* loader2 = SystemDictionary::loader_name(super_loader());
480
char* failed_type_name = failed_type_symbol->as_C_string();
481
size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
482
strlen(current) + strlen(loader2) + strlen(failed_type_name);
483
char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
484
jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
485
failed_type_name);
486
THROW_MSG_(vmSymbols::java_lang_LinkageError(), buf, false);
487
}
488
}
489
}
490
491
put_method_at(target_method(), i);
492
if (!is_default) {
493
target_method()->set_vtable_index(i);
494
} else {
495
if (def_vtable_indices != NULL) {
496
if (is_preinitialized_vtable()) {
497
// At runtime initialize_vtable is rerun as part of link_class_impl()
498
// for a shared class loaded by the non-boot loader.
499
// The dumptime vtable index should be the same as the runtime index.
500
assert(def_vtable_indices->at(default_index) == i,
501
"dump time vtable index is different from runtime index");
502
} else {
503
def_vtable_indices->at_put(default_index, i);
504
}
505
}
506
assert(super_method->is_default_method() || super_method->is_overpass()
507
|| super_method->is_abstract(), "default override error");
508
}
509
510
511
#ifndef PRODUCT
512
if (PrintVtables && Verbose) {
513
ResourceMark rm(THREAD);
514
char* sig = target_method()->name_and_sig_as_C_string();
515
tty->print("overriding with %s::%s index %d, original flags: ",
516
target_klass->internal_name(), sig, i);
517
super_method->access_flags().print_on(tty);
518
if (super_method->is_default_method()) {
519
tty->print("default ");
520
}
521
if (super_method->is_overpass()) {
522
tty->print("overpass");
523
}
524
tty->print("overriders flags: ");
525
target_method->access_flags().print_on(tty);
526
if (target_method->is_default_method()) {
527
tty->print("default ");
528
}
529
if (target_method->is_overpass()) {
530
tty->print("overpass");
531
}
532
tty->cr();
533
}
534
#endif /*PRODUCT*/
535
} else {
536
// allocate_new = true; default. We might override one entry,
537
// but not override another. Once we override one, not need new
538
#ifndef PRODUCT
539
if (PrintVtables && Verbose) {
540
ResourceMark rm(THREAD);
541
char* sig = target_method()->name_and_sig_as_C_string();
542
tty->print("NOT overriding with %s::%s index %d, original flags: ",
543
target_klass->internal_name(), sig,i);
544
super_method->access_flags().print_on(tty);
545
if (super_method->is_default_method()) {
546
tty->print("default ");
547
}
548
if (super_method->is_overpass()) {
549
tty->print("overpass");
550
}
551
tty->print("overriders flags: ");
552
target_method->access_flags().print_on(tty);
553
if (target_method->is_default_method()) {
554
tty->print("default ");
555
}
556
if (target_method->is_overpass()) {
557
tty->print("overpass");
558
}
559
tty->cr();
560
}
561
#endif /*PRODUCT*/
562
}
563
}
564
}
565
return allocate_new;
566
}
567
568
void klassVtable::put_method_at(Method* m, int index) {
569
if (is_preinitialized_vtable()) {
570
// At runtime initialize_vtable is rerun as part of link_class_impl()
571
// for shared class loaded by the non-boot loader to obtain the loader
572
// constraints based on the runtime classloaders' context. The dumptime
573
// method at the vtable index should be the same as the runtime method.
574
assert(table()[index].method() == m,
575
"archived method is different from the runtime method");
576
} else {
577
#ifndef PRODUCT
578
if (PrintVtables && Verbose) {
579
ResourceMark rm;
580
const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
581
tty->print("adding %s at index %d, flags: ", sig, index);
582
if (m != NULL) {
583
m->access_flags().print_on(tty);
584
if (m->is_default_method()) {
585
tty->print("default ");
586
}
587
if (m->is_overpass()) {
588
tty->print("overpass");
589
}
590
}
591
tty->cr();
592
}
593
#endif
594
table()[index].set(m);
595
}
596
}
597
598
// Find out if a method "m" with superclass "super", loader "classloader" and
599
// name "classname" needs a new vtable entry. Let P be a class package defined
600
// by "classloader" and "classname".
601
// NOTE: The logic used here is very similar to the one used for computing
602
// the vtables indices for a method. We cannot directly use that function because,
603
// we allocate the InstanceKlass at load time, and that requires that the
604
// superclass has been loaded.
605
// However, the vtable entries are filled in at link time, and therefore
606
// the superclass' vtable may not yet have been filled in.
607
bool klassVtable::needs_new_vtable_entry(methodHandle target_method,
608
Klass* super,
609
Handle classloader,
610
Symbol* classname,
611
AccessFlags class_flags,
612
TRAPS) {
613
if (class_flags.is_interface()) {
614
// Interfaces do not use vtables, except for java.lang.Object methods,
615
// so there is no point to assigning
616
// a vtable index to any of their local methods. If we refrain from doing this,
617
// we can use Method::_vtable_index to hold the itable index
618
return false;
619
}
620
621
if (target_method->is_final_method(class_flags) ||
622
// a final method never needs a new entry; final methods can be statically
623
// resolved and they have to be present in the vtable only if they override
624
// a super's method, in which case they re-use its entry
625
(target_method()->is_static()) ||
626
// static methods don't need to be in vtable
627
(target_method()->name() == vmSymbols::object_initializer_name())
628
// <init> is never called dynamically-bound
629
) {
630
return false;
631
}
632
633
// Concrete interface methods do not need new entries, they override
634
// abstract method entries using default inheritance rules
635
if (target_method()->method_holder() != NULL &&
636
target_method()->method_holder()->is_interface() &&
637
!target_method()->is_abstract() ) {
638
return false;
639
}
640
641
// we need a new entry if there is no superclass
642
if (super == NULL) {
643
return true;
644
}
645
646
// private methods in classes always have a new entry in the vtable
647
// specification interpretation since classic has
648
// private methods not overriding
649
// JDK8 adds private methods in interfaces which require invokespecial
650
if (target_method()->is_private()) {
651
return true;
652
}
653
654
// Package private methods always need a new entry to root their own
655
// overriding. This allows transitive overriding to work.
656
if (target_method()->is_package_private()) {
657
return true;
658
}
659
660
// search through the super class hierarchy to see if we need
661
// a new entry
662
ResourceMark rm(THREAD);
663
Symbol* name = target_method()->name();
664
Symbol* signature = target_method()->signature();
665
Klass* k = super;
666
Method* super_method = NULL;
667
InstanceKlass *holder = NULL;
668
Method* recheck_method = NULL;
669
bool found_pkg_prvt_method = false;
670
while (k != NULL) {
671
// lookup through the hierarchy for a method with matching name and sign.
672
super_method = InstanceKlass::cast(k)->lookup_method(name, signature);
673
if (super_method == NULL) {
674
break; // we still have to search for a matching miranda method
675
}
676
// get the class holding the matching method
677
// make sure you use that class for is_override
678
InstanceKlass* superk = super_method->method_holder();
679
// we want only instance method matches
680
// pretend private methods are not in the super vtable
681
// since we do override around them: e.g. a.m pub/b.m private/c.m pub,
682
// ignore private, c.m pub does override a.m pub
683
// For classes that were not javac'd together, we also do transitive overriding around
684
// methods that have less accessibility
685
if ((!super_method->is_static()) &&
686
(!super_method->is_private())) {
687
if (superk->is_override(super_method, classloader, classname, THREAD)) {
688
return false;
689
// else keep looking for transitive overrides
690
}
691
// If we get here then one of the super classes has a package private method
692
// that will not get overridden because it is in a different package. But,
693
// that package private method does "override" any matching methods in super
694
// interfaces, so there will be no miranda vtable entry created. So, set flag
695
// to TRUE for use below, in case there are no methods in super classes that
696
// this target method overrides.
697
assert(super_method->is_package_private(), "super_method must be package private");
698
assert(!superk->is_same_class_package(classloader(), classname),
699
"Must be different packages");
700
found_pkg_prvt_method = true;
701
}
702
703
// Start with lookup result and continue to search up
704
k = superk->super(); // haven't found an override match yet; continue to look
705
}
706
707
// If found_pkg_prvt_method is set, then the ONLY matching method in the
708
// superclasses is package private in another package. That matching method will
709
// prevent a miranda vtable entry from being created. Because the target method can not
710
// override the package private method in another package, then it needs to be the root
711
// for its own vtable entry.
712
if (found_pkg_prvt_method) {
713
return true;
714
}
715
716
// if the target method is public or protected it may have a matching
717
// miranda method in the super, whose entry it should re-use.
718
// Actually, to handle cases that javac would not generate, we need
719
// this check for all access permissions.
720
InstanceKlass *sk = InstanceKlass::cast(super);
721
if (sk->has_miranda_methods()) {
722
if (sk->lookup_method_in_all_interfaces(name, signature, Klass::find_defaults) != NULL) {
723
return false; // found a matching miranda; we do not need a new entry
724
}
725
}
726
return true; // found no match; we need a new entry
727
}
728
729
// Support for miranda methods
730
731
// get the vtable index of a miranda method with matching "name" and "signature"
732
int klassVtable::index_of_miranda(Symbol* name, Symbol* signature) {
733
// search from the bottom, might be faster
734
for (int i = (length() - 1); i >= 0; i--) {
735
Method* m = table()[i].method();
736
if (is_miranda_entry_at(i) &&
737
m->name() == name && m->signature() == signature) {
738
return i;
739
}
740
}
741
return Method::invalid_vtable_index;
742
}
743
744
// check if an entry at an index is miranda
745
// requires that method m at entry be declared ("held") by an interface.
746
bool klassVtable::is_miranda_entry_at(int i) {
747
Method* m = method_at(i);
748
Klass* method_holder = m->method_holder();
749
InstanceKlass *mhk = InstanceKlass::cast(method_holder);
750
751
// miranda methods are public abstract instance interface methods in a class's vtable
752
if (mhk->is_interface()) {
753
assert(m->is_public(), "should be public");
754
assert(ik()->implements_interface(method_holder) , "this class should implement the interface");
755
if (is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super())) {
756
return true;
757
}
758
}
759
return false;
760
}
761
762
// Check if a method is a miranda method, given a class's methods array,
763
// its default_method table and its super class.
764
// "Miranda" means an abstract non-private method that would not be
765
// overridden for the local class.
766
// A "miranda" method should only include non-private interface
767
// instance methods, i.e. not private methods, not static methods,
768
// not default methods (concrete interface methods), not overpass methods.
769
// If a given class already has a local (including overpass) method, a
770
// default method, or any of its superclasses has the same which would have
771
// overridden an abstract method, then this is not a miranda method.
772
//
773
// Miranda methods are checked multiple times.
774
// Pass 1: during class load/class file parsing: before vtable size calculation:
775
// include superinterface abstract and default methods (non-private instance).
776
// We include potential default methods to give them space in the vtable.
777
// During the first run, the current instanceKlass has not yet been
778
// created, the superclasses and superinterfaces do have instanceKlasses
779
// but may not have vtables, the default_methods list is empty, no overpasses.
780
// This is seen by default method creation.
781
//
782
// Pass 2: recalculated during vtable initialization: only include abstract methods.
783
// The goal of pass 2 is to walk through the superinterfaces to see if any of
784
// the superinterface methods (which were all abstract pre-default methods)
785
// need to be added to the vtable.
786
// With the addition of default methods, we have three new challenges:
787
// overpasses, static interface methods and private interface methods.
788
// Static and private interface methods do not get added to the vtable and
789
// are not seen by the method resolution process, so we skip those.
790
// Overpass methods are already in the vtable, so vtable lookup will
791
// find them and we don't need to add a miranda method to the end of
792
// the vtable. So we look for overpass methods and if they are found we
793
// return false. Note that we inherit our superclasses vtable, so
794
// the superclass' search also needs to use find_overpass so that if
795
// one is found we return false.
796
// False means - we don't need a miranda method added to the vtable.
797
//
798
// During the second run, default_methods is set up, so concrete methods from
799
// superinterfaces with matching names/signatures to default_methods are already
800
// in the default_methods list and do not need to be appended to the vtable
801
// as mirandas. Abstract methods may already have been handled via
802
// overpasses - either local or superclass overpasses, which may be
803
// in the vtable already.
804
//
805
// Pass 3: They are also checked by link resolution and selection,
806
// for invocation on a method (not interface method) reference that
807
// resolves to a method with an interface as its method_holder.
808
// Used as part of walking from the bottom of the vtable to find
809
// the vtable index for the miranda method.
810
//
811
// Part of the Miranda Rights in the US mean that if you do not have
812
// an attorney one will be appointed for you.
813
bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods,
814
Array<Method*>* default_methods, Klass* super) {
815
if (m->is_static() || m->is_private() || m->is_overpass()) {
816
return false;
817
}
818
Symbol* name = m->name();
819
Symbol* signature = m->signature();
820
821
// First look in local methods to see if already covered
822
if (InstanceKlass::find_local_method(class_methods, name, signature,
823
Klass::find_overpass, Klass::skip_static, Klass::skip_private) != NULL)
824
{
825
return false;
826
}
827
828
// Check local default methods
829
if ((default_methods != NULL) &&
830
(InstanceKlass::find_method(default_methods, name, signature) != NULL))
831
{
832
return false;
833
}
834
835
InstanceKlass* cursuper;
836
// Iterate on all superclasses, which should have instanceKlasses
837
// Note that we explicitly look for overpasses at each level.
838
// Overpasses may or may not exist for supers for pass 1,
839
// they should have been created for pass 2 and later.
840
841
for (cursuper = InstanceKlass::cast(super); cursuper != NULL; cursuper = (InstanceKlass*)cursuper->super())
842
{
843
if (cursuper->find_local_method(name, signature,
844
Klass::find_overpass, Klass::skip_static, Klass::skip_private) != NULL) {
845
return false;
846
}
847
}
848
849
return true;
850
}
851
852
// Scans current_interface_methods for miranda methods that do not
853
// already appear in new_mirandas, or default methods, and are also not defined-and-non-private
854
// in super (superclass). These mirandas are added to all_mirandas if it is
855
// not null; in addition, those that are not duplicates of miranda methods
856
// inherited by super from its interfaces are added to new_mirandas.
857
// Thus, new_mirandas will be the set of mirandas that this class introduces,
858
// all_mirandas will be the set of all mirandas applicable to this class
859
// including all defined in superclasses.
860
void klassVtable::add_new_mirandas_to_lists(
861
GrowableArray<Method*>* new_mirandas, GrowableArray<Method*>* all_mirandas,
862
Array<Method*>* current_interface_methods, Array<Method*>* class_methods,
863
Array<Method*>* default_methods, Klass* super) {
864
865
// iterate thru the current interface's method to see if it a miranda
866
int num_methods = current_interface_methods->length();
867
for (int i = 0; i < num_methods; i++) {
868
Method* im = current_interface_methods->at(i);
869
bool is_duplicate = false;
870
int num_of_current_mirandas = new_mirandas->length();
871
// check for duplicate mirandas in different interfaces we implement
872
for (int j = 0; j < num_of_current_mirandas; j++) {
873
Method* miranda = new_mirandas->at(j);
874
if ((im->name() == miranda->name()) &&
875
(im->signature() == miranda->signature())) {
876
is_duplicate = true;
877
break;
878
}
879
}
880
881
if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable
882
if (is_miranda(im, class_methods, default_methods, super)) { // is it a miranda at all?
883
InstanceKlass *sk = InstanceKlass::cast(super);
884
// check if it is a duplicate of a super's miranda
885
if (sk->lookup_method_in_all_interfaces(im->name(), im->signature(), Klass::find_defaults) == NULL) {
886
new_mirandas->append(im);
887
}
888
if (all_mirandas != NULL) {
889
all_mirandas->append(im);
890
}
891
}
892
}
893
}
894
}
895
896
void klassVtable::get_mirandas(GrowableArray<Method*>* new_mirandas,
897
GrowableArray<Method*>* all_mirandas,
898
Klass* super, Array<Method*>* class_methods,
899
Array<Method*>* default_methods,
900
Array<Klass*>* local_interfaces) {
901
assert((new_mirandas->length() == 0) , "current mirandas must be 0");
902
903
// iterate thru the local interfaces looking for a miranda
904
int num_local_ifs = local_interfaces->length();
905
for (int i = 0; i < num_local_ifs; i++) {
906
InstanceKlass *ik = InstanceKlass::cast(local_interfaces->at(i));
907
add_new_mirandas_to_lists(new_mirandas, all_mirandas,
908
ik->methods(), class_methods,
909
default_methods, super);
910
// iterate thru each local's super interfaces
911
Array<Klass*>* super_ifs = ik->transitive_interfaces();
912
int num_super_ifs = super_ifs->length();
913
for (int j = 0; j < num_super_ifs; j++) {
914
InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j));
915
add_new_mirandas_to_lists(new_mirandas, all_mirandas,
916
sik->methods(), class_methods,
917
default_methods, super);
918
}
919
}
920
}
921
922
// Discover miranda methods ("miranda" = "interface abstract, no binding"),
923
// and append them into the vtable starting at index initialized,
924
// return the new value of initialized.
925
// Miranda methods use vtable entries, but do not get assigned a vtable_index
926
// The vtable_index is discovered by searching from the end of the vtable
927
int klassVtable::fill_in_mirandas(int initialized) {
928
GrowableArray<Method*> mirandas(20);
929
get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
930
ik()->default_methods(), ik()->local_interfaces());
931
for (int i = 0; i < mirandas.length(); i++) {
932
if (PrintVtables && Verbose) {
933
Method* meth = mirandas.at(i);
934
ResourceMark rm(Thread::current());
935
if (meth != NULL) {
936
char* sig = meth->name_and_sig_as_C_string();
937
tty->print("fill in mirandas with %s index %d, flags: ",
938
sig, initialized);
939
meth->access_flags().print_on(tty);
940
if (meth->is_default_method()) {
941
tty->print("default ");
942
}
943
tty->cr();
944
}
945
}
946
put_method_at(mirandas.at(i), initialized);
947
++initialized;
948
}
949
return initialized;
950
}
951
952
// Copy this class's vtable to the vtable beginning at start.
953
// Used to copy superclass vtable to prefix of subclass's vtable.
954
void klassVtable::copy_vtable_to(vtableEntry* start) {
955
Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size());
956
}
957
958
#if INCLUDE_JVMTI
959
bool klassVtable::adjust_default_method(int vtable_index, Method* old_method, Method* new_method) {
960
// If old_method is default, find this vtable index in default_vtable_indices
961
// and replace that method in the _default_methods list
962
bool updated = false;
963
964
Array<Method*>* default_methods = ik()->default_methods();
965
if (default_methods != NULL) {
966
int len = default_methods->length();
967
for (int idx = 0; idx < len; idx++) {
968
if (vtable_index == ik()->default_vtable_indices()->at(idx)) {
969
if (default_methods->at(idx) == old_method) {
970
default_methods->at_put(idx, new_method);
971
updated = true;
972
}
973
break;
974
}
975
}
976
}
977
return updated;
978
}
979
980
// search the vtable for uses of either obsolete or EMCP methods
981
void klassVtable::adjust_method_entries(InstanceKlass* holder, bool * trace_name_printed) {
982
int prn_enabled = 0;
983
for (int index = 0; index < length(); index++) {
984
Method* old_method = unchecked_method_at(index);
985
if (old_method == NULL || old_method->method_holder() != holder || !old_method->is_old()) {
986
continue; // skip uninteresting entries
987
}
988
assert(!old_method->is_deleted(), "vtable methods may not be deleted");
989
990
Method* new_method = holder->method_with_idnum(old_method->orig_method_idnum());
991
992
assert(new_method != NULL, "method_with_idnum() should not be NULL");
993
assert(old_method != new_method, "sanity check");
994
995
put_method_at(new_method, index);
996
// For default methods, need to update the _default_methods array
997
// which can only have one method entry for a given signature
998
bool updated_default = false;
999
if (old_method->is_default_method()) {
1000
updated_default = adjust_default_method(index, old_method, new_method);
1001
}
1002
1003
if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
1004
if (!(*trace_name_printed)) {
1005
// RC_TRACE_MESG macro has an embedded ResourceMark
1006
RC_TRACE_MESG(("adjust: klassname=%s for methods from name=%s",
1007
klass()->external_name(),
1008
old_method->method_holder()->external_name()));
1009
*trace_name_printed = true;
1010
}
1011
// RC_TRACE macro has an embedded ResourceMark
1012
RC_TRACE(0x00100000, ("vtable method update: %s(%s), updated default = %s",
1013
new_method->name()->as_C_string(),
1014
new_method->signature()->as_C_string(),
1015
updated_default ? "true" : "false"));
1016
}
1017
}
1018
}
1019
1020
// a vtable should never contain old or obsolete methods
1021
bool klassVtable::check_no_old_or_obsolete_entries() {
1022
for (int i = 0; i < length(); i++) {
1023
Method* m = unchecked_method_at(i);
1024
if (m != NULL &&
1025
(NOT_PRODUCT(!m->is_valid() ||) m->is_old() || m->is_obsolete())) {
1026
return false;
1027
}
1028
}
1029
return true;
1030
}
1031
1032
void klassVtable::dump_vtable() {
1033
tty->print_cr("vtable dump --");
1034
for (int i = 0; i < length(); i++) {
1035
Method* m = unchecked_method_at(i);
1036
if (m != NULL) {
1037
tty->print(" (%5d) ", i);
1038
m->access_flags().print_on(tty);
1039
if (m->is_default_method()) {
1040
tty->print("default ");
1041
}
1042
if (m->is_overpass()) {
1043
tty->print("overpass");
1044
}
1045
tty->print(" -- ");
1046
m->print_name(tty);
1047
tty->cr();
1048
}
1049
}
1050
}
1051
#endif // INCLUDE_JVMTI
1052
1053
// CDS/RedefineClasses support - clear vtables so they can be reinitialized
1054
void klassVtable::clear_vtable() {
1055
for (int i = 0; i < _length; i++) table()[i].clear();
1056
}
1057
1058
bool klassVtable::is_initialized() {
1059
return _length == 0 || table()[0].method() != NULL;
1060
}
1061
1062
//-----------------------------------------------------------------------------------------
1063
// Itable code
1064
1065
// Initialize a itableMethodEntry
1066
void itableMethodEntry::initialize(Method* m) {
1067
if (m == NULL) return;
1068
1069
if (MetaspaceShared::is_in_shared_space((void*)&_method) &&
1070
!MetaspaceShared::remapped_readwrite()) {
1071
// At runtime initialize_itable is rerun as part of link_class_impl()
1072
// for a shared class loaded by the non-boot loader.
1073
// The dumptime itable method entry should be the same as the runtime entry.
1074
assert(_method == m, "sanity");
1075
} else {
1076
_method = m;
1077
}
1078
}
1079
1080
klassItable::klassItable(instanceKlassHandle klass) {
1081
_klass = klass;
1082
1083
if (klass->itable_length() > 0) {
1084
itableOffsetEntry* offset_entry = (itableOffsetEntry*)klass->start_of_itable();
1085
if (offset_entry != NULL && offset_entry->interface_klass() != NULL) { // Check that itable is initialized
1086
// First offset entry points to the first method_entry
1087
intptr_t* method_entry = (intptr_t *)(((address)klass()) + offset_entry->offset());
1088
intptr_t* end = klass->end_of_itable();
1089
1090
_table_offset = (intptr_t*)offset_entry - (intptr_t*)klass();
1091
_size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size();
1092
_size_method_table = (end - method_entry) / itableMethodEntry::size();
1093
assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation");
1094
return;
1095
}
1096
}
1097
1098
// The length of the itable was either zero, or it has not yet been initialized.
1099
_table_offset = 0;
1100
_size_offset_table = 0;
1101
_size_method_table = 0;
1102
}
1103
1104
static int initialize_count = 0;
1105
1106
// Initialization
1107
void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
1108
if (_klass->is_interface()) {
1109
// This needs to go after vtable indices are assigned but
1110
// before implementors need to know the number of itable indices.
1111
assign_itable_indices_for_interface(_klass());
1112
}
1113
1114
// Cannot be setup doing bootstrapping, interfaces don't have
1115
// itables, and klass with only ones entry have empty itables
1116
if (Universe::is_bootstrapping() ||
1117
_klass->is_interface() ||
1118
_klass->itable_length() == itableOffsetEntry::size()) return;
1119
1120
// There's alway an extra itable entry so we can null-terminate it.
1121
guarantee(size_offset_table() >= 1, "too small");
1122
int num_interfaces = size_offset_table() - 1;
1123
if (num_interfaces > 0) {
1124
if (TraceItables) tty->print_cr("%3d: Initializing itables for %s", ++initialize_count,
1125
_klass->name()->as_C_string());
1126
1127
1128
// Iterate through all interfaces
1129
int i;
1130
for(i = 0; i < num_interfaces; i++) {
1131
itableOffsetEntry* ioe = offset_entry(i);
1132
HandleMark hm(THREAD);
1133
KlassHandle interf_h (THREAD, ioe->interface_klass());
1134
assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable");
1135
initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK);
1136
}
1137
1138
}
1139
// Check that the last entry is empty
1140
itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
1141
guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
1142
}
1143
1144
1145
inline bool interface_method_needs_itable_index(Method* m) {
1146
if (m->is_static()) return false; // e.g., Stream.empty
1147
if (m->is_initializer()) return false; // <init> or <clinit>
1148
// If an interface redeclares a method from java.lang.Object,
1149
// it should already have a vtable index, don't touch it.
1150
// e.g., CharSequence.toString (from initialize_vtable)
1151
// if (m->has_vtable_index()) return false; // NO!
1152
return true;
1153
}
1154
1155
int klassItable::assign_itable_indices_for_interface(Klass* klass) {
1156
// an interface does not have an itable, but its methods need to be numbered
1157
if (TraceItables) tty->print_cr("%3d: Initializing itable indices for interface %s", ++initialize_count,
1158
klass->name()->as_C_string());
1159
Array<Method*>* methods = InstanceKlass::cast(klass)->methods();
1160
int nof_methods = methods->length();
1161
int ime_num = 0;
1162
for (int i = 0; i < nof_methods; i++) {
1163
Method* m = methods->at(i);
1164
if (interface_method_needs_itable_index(m)) {
1165
assert(!m->is_final_method(), "no final interface methods");
1166
// If m is already assigned a vtable index, do not disturb it.
1167
if (TraceItables && Verbose) {
1168
ResourceMark rm;
1169
const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
1170
if (m->has_vtable_index()) {
1171
tty->print("vtable index %d for method: %s, flags: ", m->vtable_index(), sig);
1172
} else {
1173
tty->print("itable index %d for method: %s, flags: ", ime_num, sig);
1174
}
1175
if (m != NULL) {
1176
m->access_flags().print_on(tty);
1177
if (m->is_default_method()) {
1178
tty->print("default ");
1179
}
1180
if (m->is_overpass()) {
1181
tty->print("overpass");
1182
}
1183
}
1184
tty->cr();
1185
}
1186
if (!m->has_vtable_index()) {
1187
// A shared method could have an initialized itable_index that
1188
// is < 0.
1189
assert(m->vtable_index() == Method::pending_itable_index ||
1190
m->is_shared(),
1191
"set by initialize_vtable");
1192
m->set_itable_index(ime_num);
1193
// Progress to next itable entry
1194
ime_num++;
1195
}
1196
}
1197
}
1198
assert(ime_num == method_count_for_interface(klass), "proper sizing");
1199
return ime_num;
1200
}
1201
1202
int klassItable::method_count_for_interface(Klass* interf) {
1203
assert(interf->oop_is_instance(), "must be");
1204
assert(interf->is_interface(), "must be");
1205
Array<Method*>* methods = InstanceKlass::cast(interf)->methods();
1206
int nof_methods = methods->length();
1207
int length = 0;
1208
while (nof_methods > 0) {
1209
Method* m = methods->at(nof_methods-1);
1210
if (m->has_itable_index()) {
1211
length = m->itable_index() + 1;
1212
break;
1213
}
1214
nof_methods -= 1;
1215
}
1216
#ifdef ASSERT
1217
int nof_methods_copy = nof_methods;
1218
while (nof_methods_copy > 0) {
1219
Method* mm = methods->at(--nof_methods_copy);
1220
assert(!mm->has_itable_index() || mm->itable_index() < length, "");
1221
}
1222
#endif //ASSERT
1223
// return the rightmost itable index, plus one; or 0 if no methods have
1224
// itable indices
1225
return length;
1226
}
1227
1228
1229
void klassItable::initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS) {
1230
Array<Method*>* methods = InstanceKlass::cast(interf_h())->methods();
1231
int nof_methods = methods->length();
1232
HandleMark hm;
1233
Handle interface_loader (THREAD, InstanceKlass::cast(interf_h())->class_loader());
1234
1235
int ime_count = method_count_for_interface(interf_h());
1236
for (int i = 0; i < nof_methods; i++) {
1237
Method* m = methods->at(i);
1238
methodHandle target;
1239
if (m->has_itable_index()) {
1240
// This search must match the runtime resolution, i.e. selection search for invokeinterface
1241
// to correctly enforce loader constraints for interface method inheritance
1242
LinkResolver::lookup_instance_method_in_klasses(target, _klass, m->name(), m->signature(), CHECK);
1243
}
1244
if (target == NULL || !target->is_public() || target->is_abstract()) {
1245
// Entry does not resolve. Leave it empty for AbstractMethodError.
1246
if (!(target == NULL) && !target->is_public()) {
1247
// Stuff an IllegalAccessError throwing method in there instead.
1248
itableOffsetEntry::method_entry(_klass(), method_table_offset)[m->itable_index()].
1249
initialize(Universe::throw_illegal_access_error());
1250
}
1251
} else {
1252
// Entry did resolve, check loader constraints before initializing
1253
// if checkconstraints requested
1254
if (checkconstraints) {
1255
Handle method_holder_loader (THREAD, target->method_holder()->class_loader());
1256
if (method_holder_loader() != interface_loader()) {
1257
ResourceMark rm(THREAD);
1258
Symbol* failed_type_symbol =
1259
SystemDictionary::check_signature_loaders(m->signature(),
1260
method_holder_loader,
1261
interface_loader,
1262
true, CHECK);
1263
if (failed_type_symbol != NULL) {
1264
const char* msg = "loader constraint violation in interface "
1265
"itable initialization: when resolving method \"%s\" the class"
1266
" loader (instance of %s) of the current class, %s, "
1267
"and the class loader (instance of %s) for interface "
1268
"%s have different Class objects for the type %s "
1269
"used in the signature";
1270
char* sig = target()->name_and_sig_as_C_string();
1271
const char* loader1 = SystemDictionary::loader_name(method_holder_loader());
1272
char* current = _klass->name()->as_C_string();
1273
const char* loader2 = SystemDictionary::loader_name(interface_loader());
1274
char* iface = InstanceKlass::cast(interf_h())->name()->as_C_string();
1275
char* failed_type_name = failed_type_symbol->as_C_string();
1276
size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
1277
strlen(current) + strlen(loader2) + strlen(iface) +
1278
strlen(failed_type_name);
1279
char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
1280
jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
1281
iface, failed_type_name);
1282
THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
1283
}
1284
}
1285
}
1286
1287
// ime may have moved during GC so recalculate address
1288
int ime_num = m->itable_index();
1289
assert(ime_num < ime_count, "oob");
1290
itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target());
1291
if (TraceItables && Verbose) {
1292
ResourceMark rm(THREAD);
1293
if (target() != NULL) {
1294
char* sig = target()->name_and_sig_as_C_string();
1295
tty->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ",
1296
interf_h()->internal_name(), ime_num, sig,
1297
target()->method_holder()->internal_name());
1298
tty->print("target_method flags: ");
1299
target()->access_flags().print_on(tty);
1300
if (target()->is_default_method()) {
1301
tty->print("default ");
1302
}
1303
tty->cr();
1304
}
1305
}
1306
}
1307
}
1308
}
1309
1310
// Update entry for specific Method*
1311
void klassItable::initialize_with_method(Method* m) {
1312
itableMethodEntry* ime = method_entry(0);
1313
for(int i = 0; i < _size_method_table; i++) {
1314
if (ime->method() == m) {
1315
ime->initialize(m);
1316
}
1317
ime++;
1318
}
1319
}
1320
1321
#if INCLUDE_JVMTI
1322
// search the itable for uses of either obsolete or EMCP methods
1323
void klassItable::adjust_method_entries(InstanceKlass* holder, bool * trace_name_printed) {
1324
1325
itableMethodEntry* ime = method_entry(0);
1326
for (int i = 0; i < _size_method_table; i++, ime++) {
1327
Method* old_method = ime->method();
1328
if (old_method == NULL || old_method->method_holder() != holder || !old_method->is_old()) {
1329
continue; // skip uninteresting entries
1330
}
1331
assert(!old_method->is_deleted(), "itable methods may not be deleted");
1332
1333
Method* new_method = holder->method_with_idnum(old_method->orig_method_idnum());
1334
1335
assert(new_method != NULL, "method_with_idnum() should not be NULL");
1336
assert(old_method != new_method, "sanity check");
1337
1338
ime->initialize(new_method);
1339
1340
if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
1341
if (!(*trace_name_printed)) {
1342
// RC_TRACE_MESG macro has an embedded ResourceMark
1343
RC_TRACE_MESG(("adjust: name=%s",
1344
old_method->method_holder()->external_name()));
1345
*trace_name_printed = true;
1346
}
1347
// RC_TRACE macro has an embedded ResourceMark
1348
RC_TRACE(0x00200000, ("itable method update: %s(%s)",
1349
new_method->name()->as_C_string(),
1350
new_method->signature()->as_C_string()));
1351
}
1352
}
1353
}
1354
1355
// an itable should never contain old or obsolete methods
1356
bool klassItable::check_no_old_or_obsolete_entries() {
1357
itableMethodEntry* ime = method_entry(0);
1358
for (int i = 0; i < _size_method_table; i++) {
1359
Method* m = ime->method();
1360
if (m != NULL &&
1361
(NOT_PRODUCT(!m->is_valid() ||) m->is_old() || m->is_obsolete())) {
1362
return false;
1363
}
1364
ime++;
1365
}
1366
return true;
1367
}
1368
1369
void klassItable::dump_itable() {
1370
itableMethodEntry* ime = method_entry(0);
1371
tty->print_cr("itable dump --");
1372
for (int i = 0; i < _size_method_table; i++) {
1373
Method* m = ime->method();
1374
if (m != NULL) {
1375
tty->print(" (%5d) ", i);
1376
m->access_flags().print_on(tty);
1377
if (m->is_default_method()) {
1378
tty->print("default ");
1379
}
1380
tty->print(" -- ");
1381
m->print_name(tty);
1382
tty->cr();
1383
}
1384
ime++;
1385
}
1386
}
1387
#endif // INCLUDE_JVMTI
1388
1389
// Setup
1390
class InterfaceVisiterClosure : public StackObj {
1391
public:
1392
virtual void doit(Klass* intf, int method_count) = 0;
1393
};
1394
1395
// Visit all interfaces with at least one itable method
1396
void visit_all_interfaces(Array<Klass*>* transitive_intf, InterfaceVisiterClosure *blk) {
1397
// Handle array argument
1398
for(int i = 0; i < transitive_intf->length(); i++) {
1399
Klass* intf = transitive_intf->at(i);
1400
assert(intf->is_interface(), "sanity check");
1401
1402
// Find no. of itable methods
1403
int method_count = 0;
1404
// method_count = klassItable::method_count_for_interface(intf);
1405
Array<Method*>* methods = InstanceKlass::cast(intf)->methods();
1406
if (methods->length() > 0) {
1407
for (int i = methods->length(); --i >= 0; ) {
1408
if (interface_method_needs_itable_index(methods->at(i))) {
1409
method_count++;
1410
}
1411
}
1412
}
1413
1414
// Visit all interfaces which either have any methods or can participate in receiver type check.
1415
// We do not bother to count methods in transitive interfaces, although that would allow us to skip
1416
// this step in the rare case of a zero-method interface extending another zero-method interface.
1417
if (method_count > 0 || InstanceKlass::cast(intf)->transitive_interfaces()->length() > 0) {
1418
blk->doit(intf, method_count);
1419
}
1420
}
1421
}
1422
1423
class CountInterfacesClosure : public InterfaceVisiterClosure {
1424
private:
1425
int _nof_methods;
1426
int _nof_interfaces;
1427
public:
1428
CountInterfacesClosure() { _nof_methods = 0; _nof_interfaces = 0; }
1429
1430
int nof_methods() const { return _nof_methods; }
1431
int nof_interfaces() const { return _nof_interfaces; }
1432
1433
void doit(Klass* intf, int method_count) { _nof_methods += method_count; _nof_interfaces++; }
1434
};
1435
1436
class SetupItableClosure : public InterfaceVisiterClosure {
1437
private:
1438
itableOffsetEntry* _offset_entry;
1439
itableMethodEntry* _method_entry;
1440
address _klass_begin;
1441
public:
1442
SetupItableClosure(address klass_begin, itableOffsetEntry* offset_entry, itableMethodEntry* method_entry) {
1443
_klass_begin = klass_begin;
1444
_offset_entry = offset_entry;
1445
_method_entry = method_entry;
1446
}
1447
1448
itableMethodEntry* method_entry() const { return _method_entry; }
1449
1450
void doit(Klass* intf, int method_count) {
1451
int offset = ((address)_method_entry) - _klass_begin;
1452
_offset_entry->initialize(intf, offset);
1453
_offset_entry++;
1454
_method_entry += method_count;
1455
}
1456
};
1457
1458
int klassItable::compute_itable_size(Array<Klass*>* transitive_interfaces) {
1459
// Count no of interfaces and total number of interface methods
1460
CountInterfacesClosure cic;
1461
visit_all_interfaces(transitive_interfaces, &cic);
1462
1463
// There's alway an extra itable entry so we can null-terminate it.
1464
int itable_size = calc_itable_size(cic.nof_interfaces() + 1, cic.nof_methods());
1465
1466
// Statistics
1467
update_stats(itable_size * HeapWordSize);
1468
1469
return itable_size;
1470
}
1471
1472
1473
// Fill out offset table and interface klasses into the itable space
1474
void klassItable::setup_itable_offset_table(instanceKlassHandle klass) {
1475
if (klass->itable_length() == 0) return;
1476
assert(!klass->is_interface(), "Should have zero length itable");
1477
1478
// Count no of interfaces and total number of interface methods
1479
CountInterfacesClosure cic;
1480
visit_all_interfaces(klass->transitive_interfaces(), &cic);
1481
int nof_methods = cic.nof_methods();
1482
int nof_interfaces = cic.nof_interfaces();
1483
1484
// Add one extra entry so we can null-terminate the table
1485
nof_interfaces++;
1486
1487
assert(compute_itable_size(klass->transitive_interfaces()) ==
1488
calc_itable_size(nof_interfaces, nof_methods),
1489
"mismatch calculation of itable size");
1490
1491
// Fill-out offset table
1492
itableOffsetEntry* ioe = (itableOffsetEntry*)klass->start_of_itable();
1493
itableMethodEntry* ime = (itableMethodEntry*)(ioe + nof_interfaces);
1494
intptr_t* end = klass->end_of_itable();
1495
assert((oop*)(ime + nof_methods) <= (oop*)klass->start_of_nonstatic_oop_maps(), "wrong offset calculation (1)");
1496
assert((oop*)(end) == (oop*)(ime + nof_methods), "wrong offset calculation (2)");
1497
1498
// Visit all interfaces and initialize itable offset table
1499
SetupItableClosure sic((address)klass(), ioe, ime);
1500
visit_all_interfaces(klass->transitive_interfaces(), &sic);
1501
1502
#ifdef ASSERT
1503
ime = sic.method_entry();
1504
oop* v = (oop*) klass->end_of_itable();
1505
assert( (oop*)(ime) == v, "wrong offset calculation (2)");
1506
#endif
1507
}
1508
1509
1510
// inverse to itable_index
1511
Method* klassItable::method_for_itable_index(Klass* intf, int itable_index) {
1512
assert(InstanceKlass::cast(intf)->is_interface(), "sanity check");
1513
assert(intf->verify_itable_index(itable_index), "");
1514
Array<Method*>* methods = InstanceKlass::cast(intf)->methods();
1515
1516
if (itable_index < 0 || itable_index >= method_count_for_interface(intf))
1517
return NULL; // help caller defend against bad indices
1518
1519
int index = itable_index;
1520
Method* m = methods->at(index);
1521
int index2 = -1;
1522
while (!m->has_itable_index() ||
1523
(index2 = m->itable_index()) != itable_index) {
1524
assert(index2 < itable_index, "monotonic");
1525
if (++index == methods->length())
1526
return NULL;
1527
m = methods->at(index);
1528
}
1529
assert(m->itable_index() == itable_index, "correct inverse");
1530
1531
return m;
1532
}
1533
1534
void klassVtable::verify(outputStream* st, bool forced) {
1535
// make sure table is initialized
1536
if (!Universe::is_fully_initialized()) return;
1537
#ifndef PRODUCT
1538
// avoid redundant verifies
1539
if (!forced && _verify_count == Universe::verify_count()) return;
1540
_verify_count = Universe::verify_count();
1541
#endif
1542
oop* end_of_obj = (oop*)_klass() + _klass()->size();
1543
oop* end_of_vtable = (oop *)&table()[_length];
1544
if (end_of_vtable > end_of_obj) {
1545
fatal(err_msg("klass %s: klass object too short (vtable extends beyond "
1546
"end)", _klass->internal_name()));
1547
}
1548
1549
for (int i = 0; i < _length; i++) table()[i].verify(this, st);
1550
// verify consistency with superKlass vtable
1551
Klass* super = _klass->super();
1552
if (super != NULL) {
1553
InstanceKlass* sk = InstanceKlass::cast(super);
1554
klassVtable* vt = sk->vtable();
1555
for (int i = 0; i < vt->length(); i++) {
1556
verify_against(st, vt, i);
1557
}
1558
}
1559
}
1560
1561
void klassVtable::verify_against(outputStream* st, klassVtable* vt, int index) {
1562
vtableEntry* vte = &vt->table()[index];
1563
if (vte->method()->name() != table()[index].method()->name() ||
1564
vte->method()->signature() != table()[index].method()->signature()) {
1565
fatal("mismatched name/signature of vtable entries");
1566
}
1567
}
1568
1569
#ifndef PRODUCT
1570
void klassVtable::print() {
1571
ResourceMark rm;
1572
tty->print("klassVtable for klass %s (length %d):\n", _klass->internal_name(), length());
1573
for (int i = 0; i < length(); i++) {
1574
table()[i].print();
1575
tty->cr();
1576
}
1577
}
1578
#endif
1579
1580
void vtableEntry::verify(klassVtable* vt, outputStream* st) {
1581
NOT_PRODUCT(FlagSetting fs(IgnoreLockingAssertions, true));
1582
assert(method() != NULL, "must have set method");
1583
method()->verify();
1584
// we sub_type, because it could be a miranda method
1585
if (!vt->klass()->is_subtype_of(method()->method_holder())) {
1586
#ifndef PRODUCT
1587
print();
1588
#endif
1589
fatal(err_msg("vtableEntry " PTR_FORMAT ": method is from subclass", this));
1590
}
1591
}
1592
1593
#ifndef PRODUCT
1594
1595
void vtableEntry::print() {
1596
ResourceMark rm;
1597
tty->print("vtableEntry %s: ", method()->name()->as_C_string());
1598
if (Verbose) {
1599
tty->print("m %#lx ", (address)method());
1600
}
1601
}
1602
1603
class VtableStats : AllStatic {
1604
public:
1605
static int no_klasses; // # classes with vtables
1606
static int no_array_klasses; // # array classes
1607
static int no_instance_klasses; // # instanceKlasses
1608
static int sum_of_vtable_len; // total # of vtable entries
1609
static int sum_of_array_vtable_len; // total # of vtable entries in array klasses only
1610
static int fixed; // total fixed overhead in bytes
1611
static int filler; // overhead caused by filler bytes
1612
static int entries; // total bytes consumed by vtable entries
1613
static int array_entries; // total bytes consumed by array vtable entries
1614
1615
static void do_class(Klass* k) {
1616
Klass* kl = k;
1617
klassVtable* vt = kl->vtable();
1618
if (vt == NULL) return;
1619
no_klasses++;
1620
if (kl->oop_is_instance()) {
1621
no_instance_klasses++;
1622
kl->array_klasses_do(do_class);
1623
}
1624
if (kl->oop_is_array()) {
1625
no_array_klasses++;
1626
sum_of_array_vtable_len += vt->length();
1627
}
1628
sum_of_vtable_len += vt->length();
1629
}
1630
1631
static void compute() {
1632
SystemDictionary::classes_do(do_class);
1633
fixed = no_klasses * oopSize; // vtable length
1634
// filler size is a conservative approximation
1635
filler = oopSize * (no_klasses - no_instance_klasses) * (sizeof(InstanceKlass) - sizeof(ArrayKlass) - 1);
1636
entries = sizeof(vtableEntry) * sum_of_vtable_len;
1637
array_entries = sizeof(vtableEntry) * sum_of_array_vtable_len;
1638
}
1639
};
1640
1641
int VtableStats::no_klasses = 0;
1642
int VtableStats::no_array_klasses = 0;
1643
int VtableStats::no_instance_klasses = 0;
1644
int VtableStats::sum_of_vtable_len = 0;
1645
int VtableStats::sum_of_array_vtable_len = 0;
1646
int VtableStats::fixed = 0;
1647
int VtableStats::filler = 0;
1648
int VtableStats::entries = 0;
1649
int VtableStats::array_entries = 0;
1650
1651
void klassVtable::print_statistics() {
1652
ResourceMark rm;
1653
HandleMark hm;
1654
VtableStats::compute();
1655
tty->print_cr("vtable statistics:");
1656
tty->print_cr("%6d classes (%d instance, %d array)", VtableStats::no_klasses, VtableStats::no_instance_klasses, VtableStats::no_array_klasses);
1657
int total = VtableStats::fixed + VtableStats::filler + VtableStats::entries;
1658
tty->print_cr("%6d bytes fixed overhead (refs + vtable object header)", VtableStats::fixed);
1659
tty->print_cr("%6d bytes filler overhead", VtableStats::filler);
1660
tty->print_cr("%6d bytes for vtable entries (%d for arrays)", VtableStats::entries, VtableStats::array_entries);
1661
tty->print_cr("%6d bytes total", total);
1662
}
1663
1664
int klassItable::_total_classes; // Total no. of classes with itables
1665
long klassItable::_total_size; // Total no. of bytes used for itables
1666
1667
void klassItable::print_statistics() {
1668
tty->print_cr("itable statistics:");
1669
tty->print_cr("%6d classes with itables", _total_classes);
1670
tty->print_cr("%6d K uses for itables (average by class: %d bytes)", _total_size / K, _total_size / _total_classes);
1671
}
1672
1673
#endif // PRODUCT
1674
1675