Mon, 12 Aug 2019 18:30:40 +0300
8223147: JFR Backport
8199712: Flight Recorder
8203346: JFR: Inconsistent signature of jfr_add_string_constant
8195817: JFR.stop should require name of recording
8195818: JFR.start should increase autogenerated name by one
8195819: Remove recording=x from jcmd JFR.check output
8203921: JFR thread sampling is missing fixes from JDK-8194552
8203929: Limit amount of data for JFR.dump
8203664: JFR start failure after AppCDS archive created with JFR StartFlightRecording
8003209: JFR events for network utilization
8207392: [PPC64] Implement JFR profiling
8202835: jfr/event/os/TestSystemProcess.java fails on missing events
Summary: Backport JFR from JDK11. Initial integration
Reviewed-by: neugens
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 */
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"
43 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
45 inline InstanceKlass* klassVtable::ik() const {
46 Klass* k = _klass();
47 assert(k->oop_is_instance(), "not an InstanceKlass");
48 return (InstanceKlass*)k;
49 }
51 bool klassVtable::is_preinitialized_vtable() {
52 return _klass->is_shared() && !MetaspaceShared::remapped_readwrite();
53 }
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;
73 // set up default result values
74 int vtable_length = 0;
76 // start off with super's vtable length
77 InstanceKlass* sk = (InstanceKlass*)super;
78 vtable_length = super == NULL ? 0 : sk->vtable_length();
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));
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 }
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();
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 }
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 }
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");
122 *vtable_length_ret = vtable_length;
123 }
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 }
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 }
163 //
164 // Revised lookup semantics introduced 1.3 (Kestrel beta)
165 void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
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;
171 bool is_shared = _klass->is_shared();
173 if (PrintVtables && !klass()->oop_is_array()) {
174 ResourceMark rm(THREAD);
175 tty->print_cr("Initializing: %s", _klass->name()->as_C_string());
176 }
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
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 }
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");
197 Array<Method*>* methods = ik()->methods();
198 int len = methods->length();
199 int initialized = super_vtable_len;
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));
209 bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, -1, checkconstraints, CHECK);
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 }
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));
235 bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, i, checkconstraints, CHECK);
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 }
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 }
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 }
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 InstanceKlass* supersuperklass = InstanceKlass::cast(superk->super());
293 klassVtable* ssVtable = supersuperklass->vtable();
294 if (vtable_index < ssVtable->length()) {
295 Method* super_method = ssVtable->method_at(vtable_index);
296 #ifndef PRODUCT
297 Symbol* name= target_method()->name();
298 Symbol* signature = target_method()->signature();
299 assert(super_method->name() == name && super_method->signature() == signature, "vtable entry name/sig mismatch");
300 #endif
301 if (supersuperklass->is_override(super_method, target_loader, target_classname, THREAD)) {
302 #ifndef PRODUCT
303 if (PrintVtables && Verbose) {
304 ResourceMark rm(THREAD);
305 char* sig = target_method()->name_and_sig_as_C_string();
306 tty->print("transitive overriding superclass %s with %s::%s index %d, original flags: ",
307 supersuperklass->internal_name(),
308 _klass->internal_name(), sig, vtable_index);
309 super_method->access_flags().print_on(tty);
310 if (super_method->is_default_method()) {
311 tty->print("default ");
312 }
313 tty->print("overriders flags: ");
314 target_method->access_flags().print_on(tty);
315 if (target_method->is_default_method()) {
316 tty->print("default ");
317 }
318 }
319 #endif /*PRODUCT*/
320 break; // return found superk
321 }
322 } else {
323 // super class has no vtable entry here, stop transitive search
324 superk = (InstanceKlass*)NULL;
325 break;
326 }
327 // if no override found yet, continue to search up
328 superk = InstanceKlass::cast(superk->super());
329 }
331 return superk;
332 }
334 // Update child's copy of super vtable for overrides
335 // OR return true if a new vtable entry is required.
336 // Only called for InstanceKlass's, i.e. not for arrays
337 // If that changed, could not use _klass as handle for klass
338 bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method,
339 int super_vtable_len, int default_index,
340 bool checkconstraints, TRAPS) {
341 ResourceMark rm;
342 bool allocate_new = true;
343 assert(klass->oop_is_instance(), "must be InstanceKlass");
345 Array<int>* def_vtable_indices = NULL;
346 bool is_default = false;
347 // default methods are concrete methods in superinterfaces which are added to the vtable
348 // with their real method_holder
349 // Since vtable and itable indices share the same storage, don't touch
350 // the default method's real vtable/itable index
351 // default_vtable_indices stores the vtable value relative to this inheritor
352 if (default_index >= 0 ) {
353 is_default = true;
354 def_vtable_indices = klass->default_vtable_indices();
355 assert(def_vtable_indices != NULL, "def vtable alloc?");
356 assert(default_index <= def_vtable_indices->length(), "def vtable len?");
357 } else {
358 assert(klass == target_method()->method_holder(), "caller resp.");
359 // Initialize the method's vtable index to "nonvirtual".
360 // If we allocate a vtable entry, we will update it to a non-negative number.
361 target_method()->set_vtable_index(Method::nonvirtual_vtable_index);
362 }
364 // Static and <init> methods are never in
365 if (target_method()->is_static() || target_method()->name() == vmSymbols::object_initializer_name()) {
366 return false;
367 }
369 if (target_method->is_final_method(klass->access_flags())) {
370 // a final method never needs a new entry; final methods can be statically
371 // resolved and they have to be present in the vtable only if they override
372 // a super's method, in which case they re-use its entry
373 allocate_new = false;
374 } else if (klass->is_interface()) {
375 allocate_new = false; // see note below in needs_new_vtable_entry
376 // An interface never allocates new vtable slots, only inherits old ones.
377 // This method will either be assigned its own itable index later,
378 // or be assigned an inherited vtable index in the loop below.
379 // default methods inherited by classes store their vtable indices
380 // in the inheritor's default_vtable_indices
381 // default methods inherited by interfaces may already have a
382 // valid itable index, if so, don't change it
383 // overpass methods in an interface will be assigned an itable index later
384 // by an inheriting class
385 if (!is_default || !target_method()->has_itable_index()) {
386 target_method()->set_vtable_index(Method::pending_itable_index);
387 }
388 }
390 // we need a new entry if there is no superclass
391 Klass* super = klass->super();
392 if (super == NULL) {
393 return allocate_new;
394 }
396 // private methods in classes always have a new entry in the vtable
397 // specification interpretation since classic has
398 // private methods not overriding
399 // JDK8 adds private methods in interfaces which require invokespecial
400 if (target_method()->is_private()) {
401 return allocate_new;
402 }
404 // search through the vtable and update overridden entries
405 // Since check_signature_loaders acquires SystemDictionary_lock
406 // which can block for gc, once we are in this loop, use handles
407 // For classfiles built with >= jdk7, we now look for transitive overrides
409 Symbol* name = target_method()->name();
410 Symbol* signature = target_method()->signature();
412 KlassHandle target_klass(THREAD, target_method()->method_holder());
413 if (target_klass == NULL) {
414 target_klass = _klass;
415 }
417 Handle target_loader(THREAD, target_klass->class_loader());
419 Symbol* target_classname = target_klass->name();
420 for(int i = 0; i < super_vtable_len; i++) {
421 Method* super_method;
422 if (is_preinitialized_vtable()) {
423 // If this is a shared class, the vtable is already in the final state (fully
424 // initialized). Need to look at the super's vtable.
425 klassVtable* superVtable = super->vtable();
426 super_method = superVtable->method_at(i);
427 } else {
428 super_method = method_at(i);
429 }
430 // Check if method name matches
431 if (super_method->name() == name && super_method->signature() == signature) {
433 // get super_klass for method_holder for the found method
434 InstanceKlass* super_klass = super_method->method_holder();
436 // private methods are also never overridden
437 if (!super_method->is_private() &&
438 (is_default
439 || ((super_klass->is_override(super_method, target_loader, target_classname, THREAD))
440 || ((klass->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION)
441 && ((super_klass = find_transitive_override(super_klass,
442 target_method, i, target_loader,
443 target_classname, THREAD))
444 != (InstanceKlass*)NULL)))))
445 {
446 // Package private methods always need a new entry to root their own
447 // overriding. They may also override other methods.
448 if (!target_method()->is_package_private()) {
449 allocate_new = false;
450 }
452 if (checkconstraints) {
453 // Override vtable entry if passes loader constraint check
454 // if loader constraint checking requested
455 // No need to visit his super, since he and his super
456 // have already made any needed loader constraints.
457 // Since loader constraints are transitive, it is enough
458 // to link to the first super, and we get all the others.
459 Handle super_loader(THREAD, super_klass->class_loader());
461 if (target_loader() != super_loader()) {
462 ResourceMark rm(THREAD);
463 Symbol* failed_type_symbol =
464 SystemDictionary::check_signature_loaders(signature, target_loader,
465 super_loader, true,
466 CHECK_(false));
467 if (failed_type_symbol != NULL) {
468 const char* msg = "loader constraint violation: when resolving "
469 "overridden method \"%s\" the class loader (instance"
470 " of %s) of the current class, %s, and its superclass loader "
471 "(instance of %s), have different Class objects for the type "
472 "%s used in the signature";
473 char* sig = target_method()->name_and_sig_as_C_string();
474 const char* loader1 = SystemDictionary::loader_name(target_loader());
475 char* current = target_klass->name()->as_C_string();
476 const char* loader2 = SystemDictionary::loader_name(super_loader());
477 char* failed_type_name = failed_type_symbol->as_C_string();
478 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
479 strlen(current) + strlen(loader2) + strlen(failed_type_name);
480 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
481 jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
482 failed_type_name);
483 THROW_MSG_(vmSymbols::java_lang_LinkageError(), buf, false);
484 }
485 }
486 }
488 put_method_at(target_method(), i);
489 if (!is_default) {
490 target_method()->set_vtable_index(i);
491 } else {
492 if (def_vtable_indices != NULL) {
493 if (is_preinitialized_vtable()) {
494 // At runtime initialize_vtable is rerun as part of link_class_impl()
495 // for a shared class loaded by the non-boot loader.
496 // The dumptime vtable index should be the same as the runtime index.
497 assert(def_vtable_indices->at(default_index) == i,
498 "dump time vtable index is different from runtime index");
499 } else {
500 def_vtable_indices->at_put(default_index, i);
501 }
502 }
503 assert(super_method->is_default_method() || super_method->is_overpass()
504 || super_method->is_abstract(), "default override error");
505 }
508 #ifndef PRODUCT
509 if (PrintVtables && Verbose) {
510 ResourceMark rm(THREAD);
511 char* sig = target_method()->name_and_sig_as_C_string();
512 tty->print("overriding with %s::%s index %d, original flags: ",
513 target_klass->internal_name(), sig, i);
514 super_method->access_flags().print_on(tty);
515 if (super_method->is_default_method()) {
516 tty->print("default ");
517 }
518 if (super_method->is_overpass()) {
519 tty->print("overpass");
520 }
521 tty->print("overriders flags: ");
522 target_method->access_flags().print_on(tty);
523 if (target_method->is_default_method()) {
524 tty->print("default ");
525 }
526 if (target_method->is_overpass()) {
527 tty->print("overpass");
528 }
529 tty->cr();
530 }
531 #endif /*PRODUCT*/
532 } else {
533 // allocate_new = true; default. We might override one entry,
534 // but not override another. Once we override one, not need new
535 #ifndef PRODUCT
536 if (PrintVtables && Verbose) {
537 ResourceMark rm(THREAD);
538 char* sig = target_method()->name_and_sig_as_C_string();
539 tty->print("NOT overriding with %s::%s index %d, original flags: ",
540 target_klass->internal_name(), sig,i);
541 super_method->access_flags().print_on(tty);
542 if (super_method->is_default_method()) {
543 tty->print("default ");
544 }
545 if (super_method->is_overpass()) {
546 tty->print("overpass");
547 }
548 tty->print("overriders flags: ");
549 target_method->access_flags().print_on(tty);
550 if (target_method->is_default_method()) {
551 tty->print("default ");
552 }
553 if (target_method->is_overpass()) {
554 tty->print("overpass");
555 }
556 tty->cr();
557 }
558 #endif /*PRODUCT*/
559 }
560 }
561 }
562 return allocate_new;
563 }
565 void klassVtable::put_method_at(Method* m, int index) {
566 if (is_preinitialized_vtable()) {
567 // At runtime initialize_vtable is rerun as part of link_class_impl()
568 // for shared class loaded by the non-boot loader to obtain the loader
569 // constraints based on the runtime classloaders' context. The dumptime
570 // method at the vtable index should be the same as the runtime method.
571 assert(table()[index].method() == m,
572 "archived method is different from the runtime method");
573 } else {
574 #ifndef PRODUCT
575 if (PrintVtables && Verbose) {
576 ResourceMark rm;
577 const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
578 tty->print("adding %s at index %d, flags: ", sig, index);
579 if (m != NULL) {
580 m->access_flags().print_on(tty);
581 if (m->is_default_method()) {
582 tty->print("default ");
583 }
584 if (m->is_overpass()) {
585 tty->print("overpass");
586 }
587 }
588 tty->cr();
589 }
590 #endif
591 table()[index].set(m);
592 }
593 }
595 // Find out if a method "m" with superclass "super", loader "classloader" and
596 // name "classname" needs a new vtable entry. Let P be a class package defined
597 // by "classloader" and "classname".
598 // NOTE: The logic used here is very similar to the one used for computing
599 // the vtables indices for a method. We cannot directly use that function because,
600 // we allocate the InstanceKlass at load time, and that requires that the
601 // superclass has been loaded.
602 // However, the vtable entries are filled in at link time, and therefore
603 // the superclass' vtable may not yet have been filled in.
604 bool klassVtable::needs_new_vtable_entry(methodHandle target_method,
605 Klass* super,
606 Handle classloader,
607 Symbol* classname,
608 AccessFlags class_flags,
609 TRAPS) {
610 if (class_flags.is_interface()) {
611 // Interfaces do not use vtables, except for java.lang.Object methods,
612 // so there is no point to assigning
613 // a vtable index to any of their local methods. If we refrain from doing this,
614 // we can use Method::_vtable_index to hold the itable index
615 return false;
616 }
618 if (target_method->is_final_method(class_flags) ||
619 // a final method never needs a new entry; final methods can be statically
620 // resolved and they have to be present in the vtable only if they override
621 // a super's method, in which case they re-use its entry
622 (target_method()->is_static()) ||
623 // static methods don't need to be in vtable
624 (target_method()->name() == vmSymbols::object_initializer_name())
625 // <init> is never called dynamically-bound
626 ) {
627 return false;
628 }
630 // Concrete interface methods do not need new entries, they override
631 // abstract method entries using default inheritance rules
632 if (target_method()->method_holder() != NULL &&
633 target_method()->method_holder()->is_interface() &&
634 !target_method()->is_abstract() ) {
635 return false;
636 }
638 // we need a new entry if there is no superclass
639 if (super == NULL) {
640 return true;
641 }
643 // private methods in classes always have a new entry in the vtable
644 // specification interpretation since classic has
645 // private methods not overriding
646 // JDK8 adds private methods in interfaces which require invokespecial
647 if (target_method()->is_private()) {
648 return true;
649 }
651 // Package private methods always need a new entry to root their own
652 // overriding. This allows transitive overriding to work.
653 if (target_method()->is_package_private()) {
654 return true;
655 }
657 // search through the super class hierarchy to see if we need
658 // a new entry
659 ResourceMark rm;
660 Symbol* name = target_method()->name();
661 Symbol* signature = target_method()->signature();
662 Klass* k = super;
663 Method* super_method = NULL;
664 InstanceKlass *holder = NULL;
665 Method* recheck_method = NULL;
666 while (k != NULL) {
667 // lookup through the hierarchy for a method with matching name and sign.
668 super_method = InstanceKlass::cast(k)->lookup_method(name, signature);
669 if (super_method == NULL) {
670 break; // we still have to search for a matching miranda method
671 }
672 // get the class holding the matching method
673 // make sure you use that class for is_override
674 InstanceKlass* superk = super_method->method_holder();
675 // we want only instance method matches
676 // pretend private methods are not in the super vtable
677 // since we do override around them: e.g. a.m pub/b.m private/c.m pub,
678 // ignore private, c.m pub does override a.m pub
679 // For classes that were not javac'd together, we also do transitive overriding around
680 // methods that have less accessibility
681 if ((!super_method->is_static()) &&
682 (!super_method->is_private())) {
683 if (superk->is_override(super_method, classloader, classname, THREAD)) {
684 return false;
685 // else keep looking for transitive overrides
686 }
687 }
689 // Start with lookup result and continue to search up
690 k = superk->super(); // haven't found an override match yet; continue to look
691 }
693 // if the target method is public or protected it may have a matching
694 // miranda method in the super, whose entry it should re-use.
695 // Actually, to handle cases that javac would not generate, we need
696 // this check for all access permissions.
697 InstanceKlass *sk = InstanceKlass::cast(super);
698 if (sk->has_miranda_methods()) {
699 if (sk->lookup_method_in_all_interfaces(name, signature, Klass::find_defaults) != NULL) {
700 return false; // found a matching miranda; we do not need a new entry
701 }
702 }
703 return true; // found no match; we need a new entry
704 }
706 // Support for miranda methods
708 // get the vtable index of a miranda method with matching "name" and "signature"
709 int klassVtable::index_of_miranda(Symbol* name, Symbol* signature) {
710 // search from the bottom, might be faster
711 for (int i = (length() - 1); i >= 0; i--) {
712 Method* m = table()[i].method();
713 if (is_miranda_entry_at(i) &&
714 m->name() == name && m->signature() == signature) {
715 return i;
716 }
717 }
718 return Method::invalid_vtable_index;
719 }
721 // check if an entry at an index is miranda
722 // requires that method m at entry be declared ("held") by an interface.
723 bool klassVtable::is_miranda_entry_at(int i) {
724 Method* m = method_at(i);
725 Klass* method_holder = m->method_holder();
726 InstanceKlass *mhk = InstanceKlass::cast(method_holder);
728 // miranda methods are public abstract instance interface methods in a class's vtable
729 if (mhk->is_interface()) {
730 assert(m->is_public(), "should be public");
731 assert(ik()->implements_interface(method_holder) , "this class should implement the interface");
732 if (is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super())) {
733 return true;
734 }
735 }
736 return false;
737 }
739 // Check if a method is a miranda method, given a class's methods array,
740 // its default_method table and its super class.
741 // "Miranda" means an abstract non-private method that would not be
742 // overridden for the local class.
743 // A "miranda" method should only include non-private interface
744 // instance methods, i.e. not private methods, not static methods,
745 // not default methods (concrete interface methods), not overpass methods.
746 // If a given class already has a local (including overpass) method, a
747 // default method, or any of its superclasses has the same which would have
748 // overridden an abstract method, then this is not a miranda method.
749 //
750 // Miranda methods are checked multiple times.
751 // Pass 1: during class load/class file parsing: before vtable size calculation:
752 // include superinterface abstract and default methods (non-private instance).
753 // We include potential default methods to give them space in the vtable.
754 // During the first run, the current instanceKlass has not yet been
755 // created, the superclasses and superinterfaces do have instanceKlasses
756 // but may not have vtables, the default_methods list is empty, no overpasses.
757 // This is seen by default method creation.
758 //
759 // Pass 2: recalculated during vtable initialization: only include abstract methods.
760 // The goal of pass 2 is to walk through the superinterfaces to see if any of
761 // the superinterface methods (which were all abstract pre-default methods)
762 // need to be added to the vtable.
763 // With the addition of default methods, we have three new challenges:
764 // overpasses, static interface methods and private interface methods.
765 // Static and private interface methods do not get added to the vtable and
766 // are not seen by the method resolution process, so we skip those.
767 // Overpass methods are already in the vtable, so vtable lookup will
768 // find them and we don't need to add a miranda method to the end of
769 // the vtable. So we look for overpass methods and if they are found we
770 // return false. Note that we inherit our superclasses vtable, so
771 // the superclass' search also needs to use find_overpass so that if
772 // one is found we return false.
773 // False means - we don't need a miranda method added to the vtable.
774 //
775 // During the second run, default_methods is set up, so concrete methods from
776 // superinterfaces with matching names/signatures to default_methods are already
777 // in the default_methods list and do not need to be appended to the vtable
778 // as mirandas. Abstract methods may already have been handled via
779 // overpasses - either local or superclass overpasses, which may be
780 // in the vtable already.
781 //
782 // Pass 3: They are also checked by link resolution and selection,
783 // for invocation on a method (not interface method) reference that
784 // resolves to a method with an interface as its method_holder.
785 // Used as part of walking from the bottom of the vtable to find
786 // the vtable index for the miranda method.
787 //
788 // Part of the Miranda Rights in the US mean that if you do not have
789 // an attorney one will be appointed for you.
790 bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods,
791 Array<Method*>* default_methods, Klass* super) {
792 if (m->is_static() || m->is_private() || m->is_overpass()) {
793 return false;
794 }
795 Symbol* name = m->name();
796 Symbol* signature = m->signature();
798 // First look in local methods to see if already covered
799 if (InstanceKlass::find_local_method(class_methods, name, signature,
800 Klass::find_overpass, Klass::skip_static, Klass::skip_private) != NULL)
801 {
802 return false;
803 }
805 // Check local default methods
806 if ((default_methods != NULL) &&
807 (InstanceKlass::find_method(default_methods, name, signature) != NULL))
808 {
809 return false;
810 }
812 InstanceKlass* cursuper;
813 // Iterate on all superclasses, which should have instanceKlasses
814 // Note that we explicitly look for overpasses at each level.
815 // Overpasses may or may not exist for supers for pass 1,
816 // they should have been created for pass 2 and later.
818 for (cursuper = InstanceKlass::cast(super); cursuper != NULL; cursuper = (InstanceKlass*)cursuper->super())
819 {
820 if (cursuper->find_local_method(name, signature,
821 Klass::find_overpass, Klass::skip_static, Klass::skip_private) != NULL) {
822 return false;
823 }
824 }
826 return true;
827 }
829 // Scans current_interface_methods for miranda methods that do not
830 // already appear in new_mirandas, or default methods, and are also not defined-and-non-private
831 // in super (superclass). These mirandas are added to all_mirandas if it is
832 // not null; in addition, those that are not duplicates of miranda methods
833 // inherited by super from its interfaces are added to new_mirandas.
834 // Thus, new_mirandas will be the set of mirandas that this class introduces,
835 // all_mirandas will be the set of all mirandas applicable to this class
836 // including all defined in superclasses.
837 void klassVtable::add_new_mirandas_to_lists(
838 GrowableArray<Method*>* new_mirandas, GrowableArray<Method*>* all_mirandas,
839 Array<Method*>* current_interface_methods, Array<Method*>* class_methods,
840 Array<Method*>* default_methods, Klass* super) {
842 // iterate thru the current interface's method to see if it a miranda
843 int num_methods = current_interface_methods->length();
844 for (int i = 0; i < num_methods; i++) {
845 Method* im = current_interface_methods->at(i);
846 bool is_duplicate = false;
847 int num_of_current_mirandas = new_mirandas->length();
848 // check for duplicate mirandas in different interfaces we implement
849 for (int j = 0; j < num_of_current_mirandas; j++) {
850 Method* miranda = new_mirandas->at(j);
851 if ((im->name() == miranda->name()) &&
852 (im->signature() == miranda->signature())) {
853 is_duplicate = true;
854 break;
855 }
856 }
858 if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable
859 if (is_miranda(im, class_methods, default_methods, super)) { // is it a miranda at all?
860 InstanceKlass *sk = InstanceKlass::cast(super);
861 // check if it is a duplicate of a super's miranda
862 if (sk->lookup_method_in_all_interfaces(im->name(), im->signature(), Klass::find_defaults) == NULL) {
863 new_mirandas->append(im);
864 }
865 if (all_mirandas != NULL) {
866 all_mirandas->append(im);
867 }
868 }
869 }
870 }
871 }
873 void klassVtable::get_mirandas(GrowableArray<Method*>* new_mirandas,
874 GrowableArray<Method*>* all_mirandas,
875 Klass* super, Array<Method*>* class_methods,
876 Array<Method*>* default_methods,
877 Array<Klass*>* local_interfaces) {
878 assert((new_mirandas->length() == 0) , "current mirandas must be 0");
880 // iterate thru the local interfaces looking for a miranda
881 int num_local_ifs = local_interfaces->length();
882 for (int i = 0; i < num_local_ifs; i++) {
883 InstanceKlass *ik = InstanceKlass::cast(local_interfaces->at(i));
884 add_new_mirandas_to_lists(new_mirandas, all_mirandas,
885 ik->methods(), class_methods,
886 default_methods, super);
887 // iterate thru each local's super interfaces
888 Array<Klass*>* super_ifs = ik->transitive_interfaces();
889 int num_super_ifs = super_ifs->length();
890 for (int j = 0; j < num_super_ifs; j++) {
891 InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j));
892 add_new_mirandas_to_lists(new_mirandas, all_mirandas,
893 sik->methods(), class_methods,
894 default_methods, super);
895 }
896 }
897 }
899 // Discover miranda methods ("miranda" = "interface abstract, no binding"),
900 // and append them into the vtable starting at index initialized,
901 // return the new value of initialized.
902 // Miranda methods use vtable entries, but do not get assigned a vtable_index
903 // The vtable_index is discovered by searching from the end of the vtable
904 int klassVtable::fill_in_mirandas(int initialized) {
905 GrowableArray<Method*> mirandas(20);
906 get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
907 ik()->default_methods(), ik()->local_interfaces());
908 for (int i = 0; i < mirandas.length(); i++) {
909 if (PrintVtables && Verbose) {
910 Method* meth = mirandas.at(i);
911 ResourceMark rm(Thread::current());
912 if (meth != NULL) {
913 char* sig = meth->name_and_sig_as_C_string();
914 tty->print("fill in mirandas with %s index %d, flags: ",
915 sig, initialized);
916 meth->access_flags().print_on(tty);
917 if (meth->is_default_method()) {
918 tty->print("default ");
919 }
920 tty->cr();
921 }
922 }
923 put_method_at(mirandas.at(i), initialized);
924 ++initialized;
925 }
926 return initialized;
927 }
929 // Copy this class's vtable to the vtable beginning at start.
930 // Used to copy superclass vtable to prefix of subclass's vtable.
931 void klassVtable::copy_vtable_to(vtableEntry* start) {
932 Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size());
933 }
935 #if INCLUDE_JVMTI
936 bool klassVtable::adjust_default_method(int vtable_index, Method* old_method, Method* new_method) {
937 // If old_method is default, find this vtable index in default_vtable_indices
938 // and replace that method in the _default_methods list
939 bool updated = false;
941 Array<Method*>* default_methods = ik()->default_methods();
942 if (default_methods != NULL) {
943 int len = default_methods->length();
944 for (int idx = 0; idx < len; idx++) {
945 if (vtable_index == ik()->default_vtable_indices()->at(idx)) {
946 if (default_methods->at(idx) == old_method) {
947 default_methods->at_put(idx, new_method);
948 updated = true;
949 }
950 break;
951 }
952 }
953 }
954 return updated;
955 }
957 // search the vtable for uses of either obsolete or EMCP methods
958 void klassVtable::adjust_method_entries(InstanceKlass* holder, bool * trace_name_printed) {
959 int prn_enabled = 0;
960 for (int index = 0; index < length(); index++) {
961 Method* old_method = unchecked_method_at(index);
962 if (old_method == NULL || old_method->method_holder() != holder || !old_method->is_old()) {
963 continue; // skip uninteresting entries
964 }
965 assert(!old_method->is_deleted(), "vtable methods may not be deleted");
967 Method* new_method = holder->method_with_idnum(old_method->orig_method_idnum());
969 assert(new_method != NULL, "method_with_idnum() should not be NULL");
970 assert(old_method != new_method, "sanity check");
972 put_method_at(new_method, index);
973 // For default methods, need to update the _default_methods array
974 // which can only have one method entry for a given signature
975 bool updated_default = false;
976 if (old_method->is_default_method()) {
977 updated_default = adjust_default_method(index, old_method, new_method);
978 }
980 if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
981 if (!(*trace_name_printed)) {
982 // RC_TRACE_MESG macro has an embedded ResourceMark
983 RC_TRACE_MESG(("adjust: klassname=%s for methods from name=%s",
984 klass()->external_name(),
985 old_method->method_holder()->external_name()));
986 *trace_name_printed = true;
987 }
988 // RC_TRACE macro has an embedded ResourceMark
989 RC_TRACE(0x00100000, ("vtable method update: %s(%s), updated default = %s",
990 new_method->name()->as_C_string(),
991 new_method->signature()->as_C_string(),
992 updated_default ? "true" : "false"));
993 }
994 }
995 }
997 // a vtable should never contain old or obsolete methods
998 bool klassVtable::check_no_old_or_obsolete_entries() {
999 for (int i = 0; i < length(); i++) {
1000 Method* m = unchecked_method_at(i);
1001 if (m != NULL &&
1002 (NOT_PRODUCT(!m->is_valid() ||) m->is_old() || m->is_obsolete())) {
1003 return false;
1004 }
1005 }
1006 return true;
1007 }
1009 void klassVtable::dump_vtable() {
1010 tty->print_cr("vtable dump --");
1011 for (int i = 0; i < length(); i++) {
1012 Method* m = unchecked_method_at(i);
1013 if (m != NULL) {
1014 tty->print(" (%5d) ", i);
1015 m->access_flags().print_on(tty);
1016 if (m->is_default_method()) {
1017 tty->print("default ");
1018 }
1019 if (m->is_overpass()) {
1020 tty->print("overpass");
1021 }
1022 tty->print(" -- ");
1023 m->print_name(tty);
1024 tty->cr();
1025 }
1026 }
1027 }
1028 #endif // INCLUDE_JVMTI
1030 // CDS/RedefineClasses support - clear vtables so they can be reinitialized
1031 void klassVtable::clear_vtable() {
1032 for (int i = 0; i < _length; i++) table()[i].clear();
1033 }
1035 bool klassVtable::is_initialized() {
1036 return _length == 0 || table()[0].method() != NULL;
1037 }
1039 //-----------------------------------------------------------------------------------------
1040 // Itable code
1042 // Initialize a itableMethodEntry
1043 void itableMethodEntry::initialize(Method* m) {
1044 if (m == NULL) return;
1046 if (MetaspaceShared::is_in_shared_space((void*)&_method) &&
1047 !MetaspaceShared::remapped_readwrite()) {
1048 // At runtime initialize_itable is rerun as part of link_class_impl()
1049 // for a shared class loaded by the non-boot loader.
1050 // The dumptime itable method entry should be the same as the runtime entry.
1051 assert(_method == m, "sanity");
1052 } else {
1053 _method = m;
1054 }
1055 }
1057 klassItable::klassItable(instanceKlassHandle klass) {
1058 _klass = klass;
1060 if (klass->itable_length() > 0) {
1061 itableOffsetEntry* offset_entry = (itableOffsetEntry*)klass->start_of_itable();
1062 if (offset_entry != NULL && offset_entry->interface_klass() != NULL) { // Check that itable is initialized
1063 // First offset entry points to the first method_entry
1064 intptr_t* method_entry = (intptr_t *)(((address)klass()) + offset_entry->offset());
1065 intptr_t* end = klass->end_of_itable();
1067 _table_offset = (intptr_t*)offset_entry - (intptr_t*)klass();
1068 _size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size();
1069 _size_method_table = (end - method_entry) / itableMethodEntry::size();
1070 assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation");
1071 return;
1072 }
1073 }
1075 // The length of the itable was either zero, or it has not yet been initialized.
1076 _table_offset = 0;
1077 _size_offset_table = 0;
1078 _size_method_table = 0;
1079 }
1081 static int initialize_count = 0;
1083 // Initialization
1084 void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
1085 if (_klass->is_interface()) {
1086 // This needs to go after vtable indices are assigned but
1087 // before implementors need to know the number of itable indices.
1088 assign_itable_indices_for_interface(_klass());
1089 }
1091 // Cannot be setup doing bootstrapping, interfaces don't have
1092 // itables, and klass with only ones entry have empty itables
1093 if (Universe::is_bootstrapping() ||
1094 _klass->is_interface() ||
1095 _klass->itable_length() == itableOffsetEntry::size()) return;
1097 // There's alway an extra itable entry so we can null-terminate it.
1098 guarantee(size_offset_table() >= 1, "too small");
1099 int num_interfaces = size_offset_table() - 1;
1100 if (num_interfaces > 0) {
1101 if (TraceItables) tty->print_cr("%3d: Initializing itables for %s", ++initialize_count,
1102 _klass->name()->as_C_string());
1105 // Iterate through all interfaces
1106 int i;
1107 for(i = 0; i < num_interfaces; i++) {
1108 itableOffsetEntry* ioe = offset_entry(i);
1109 HandleMark hm(THREAD);
1110 KlassHandle interf_h (THREAD, ioe->interface_klass());
1111 assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable");
1112 initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK);
1113 }
1115 }
1116 // Check that the last entry is empty
1117 itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
1118 guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
1119 }
1122 inline bool interface_method_needs_itable_index(Method* m) {
1123 if (m->is_static()) return false; // e.g., Stream.empty
1124 if (m->is_initializer()) return false; // <init> or <clinit>
1125 // If an interface redeclares a method from java.lang.Object,
1126 // it should already have a vtable index, don't touch it.
1127 // e.g., CharSequence.toString (from initialize_vtable)
1128 // if (m->has_vtable_index()) return false; // NO!
1129 return true;
1130 }
1132 int klassItable::assign_itable_indices_for_interface(Klass* klass) {
1133 // an interface does not have an itable, but its methods need to be numbered
1134 if (TraceItables) tty->print_cr("%3d: Initializing itable for interface %s", ++initialize_count,
1135 klass->name()->as_C_string());
1136 Array<Method*>* methods = InstanceKlass::cast(klass)->methods();
1137 int nof_methods = methods->length();
1138 int ime_num = 0;
1139 for (int i = 0; i < nof_methods; i++) {
1140 Method* m = methods->at(i);
1141 if (interface_method_needs_itable_index(m)) {
1142 assert(!m->is_final_method(), "no final interface methods");
1143 // If m is already assigned a vtable index, do not disturb it.
1144 if (TraceItables && Verbose) {
1145 ResourceMark rm;
1146 const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
1147 if (m->has_vtable_index()) {
1148 tty->print("itable index %d for method: %s, flags: ", m->vtable_index(), sig);
1149 } else {
1150 tty->print("itable index %d for method: %s, flags: ", ime_num, sig);
1151 }
1152 if (m != NULL) {
1153 m->access_flags().print_on(tty);
1154 if (m->is_default_method()) {
1155 tty->print("default ");
1156 }
1157 if (m->is_overpass()) {
1158 tty->print("overpass");
1159 }
1160 }
1161 tty->cr();
1162 }
1163 if (!m->has_vtable_index()) {
1164 // A shared method could have an initialized itable_index that
1165 // is < 0.
1166 assert(m->vtable_index() == Method::pending_itable_index ||
1167 m->is_shared(),
1168 "set by initialize_vtable");
1169 m->set_itable_index(ime_num);
1170 // Progress to next itable entry
1171 ime_num++;
1172 }
1173 }
1174 }
1175 assert(ime_num == method_count_for_interface(klass), "proper sizing");
1176 return ime_num;
1177 }
1179 int klassItable::method_count_for_interface(Klass* interf) {
1180 assert(interf->oop_is_instance(), "must be");
1181 assert(interf->is_interface(), "must be");
1182 Array<Method*>* methods = InstanceKlass::cast(interf)->methods();
1183 int nof_methods = methods->length();
1184 while (nof_methods > 0) {
1185 Method* m = methods->at(nof_methods-1);
1186 if (m->has_itable_index()) {
1187 int length = m->itable_index() + 1;
1188 #ifdef ASSERT
1189 while (nof_methods = 0) {
1190 m = methods->at(--nof_methods);
1191 assert(!m->has_itable_index() || m->itable_index() < length, "");
1192 }
1193 #endif //ASSERT
1194 return length; // return the rightmost itable index, plus one
1195 }
1196 nof_methods -= 1;
1197 }
1198 // no methods have itable indices
1199 return 0;
1200 }
1203 void klassItable::initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS) {
1204 Array<Method*>* methods = InstanceKlass::cast(interf_h())->methods();
1205 int nof_methods = methods->length();
1206 HandleMark hm;
1207 Handle interface_loader (THREAD, InstanceKlass::cast(interf_h())->class_loader());
1209 int ime_count = method_count_for_interface(interf_h());
1210 for (int i = 0; i < nof_methods; i++) {
1211 Method* m = methods->at(i);
1212 methodHandle target;
1213 if (m->has_itable_index()) {
1214 // This search must match the runtime resolution, i.e. selection search for invokeinterface
1215 // to correctly enforce loader constraints for interface method inheritance
1216 LinkResolver::lookup_instance_method_in_klasses(target, _klass, m->name(), m->signature(), CHECK);
1217 }
1218 if (target == NULL || !target->is_public() || target->is_abstract()) {
1219 // Entry does not resolve. Leave it empty for AbstractMethodError.
1220 if (!(target == NULL) && !target->is_public()) {
1221 // Stuff an IllegalAccessError throwing method in there instead.
1222 itableOffsetEntry::method_entry(_klass(), method_table_offset)[m->itable_index()].
1223 initialize(Universe::throw_illegal_access_error());
1224 }
1225 } else {
1226 // Entry did resolve, check loader constraints before initializing
1227 // if checkconstraints requested
1228 if (checkconstraints) {
1229 Handle method_holder_loader (THREAD, target->method_holder()->class_loader());
1230 if (method_holder_loader() != interface_loader()) {
1231 ResourceMark rm(THREAD);
1232 Symbol* failed_type_symbol =
1233 SystemDictionary::check_signature_loaders(m->signature(),
1234 method_holder_loader,
1235 interface_loader,
1236 true, CHECK);
1237 if (failed_type_symbol != NULL) {
1238 const char* msg = "loader constraint violation in interface "
1239 "itable initialization: when resolving method \"%s\" the class"
1240 " loader (instance of %s) of the current class, %s, "
1241 "and the class loader (instance of %s) for interface "
1242 "%s have different Class objects for the type %s "
1243 "used in the signature";
1244 char* sig = target()->name_and_sig_as_C_string();
1245 const char* loader1 = SystemDictionary::loader_name(method_holder_loader());
1246 char* current = _klass->name()->as_C_string();
1247 const char* loader2 = SystemDictionary::loader_name(interface_loader());
1248 char* iface = InstanceKlass::cast(interf_h())->name()->as_C_string();
1249 char* failed_type_name = failed_type_symbol->as_C_string();
1250 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
1251 strlen(current) + strlen(loader2) + strlen(iface) +
1252 strlen(failed_type_name);
1253 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
1254 jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
1255 iface, failed_type_name);
1256 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
1257 }
1258 }
1259 }
1261 // ime may have moved during GC so recalculate address
1262 int ime_num = m->itable_index();
1263 assert(ime_num < ime_count, "oob");
1264 itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target());
1265 if (TraceItables && Verbose) {
1266 ResourceMark rm(THREAD);
1267 if (target() != NULL) {
1268 char* sig = target()->name_and_sig_as_C_string();
1269 tty->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ",
1270 interf_h()->internal_name(), ime_num, sig,
1271 target()->method_holder()->internal_name());
1272 tty->print("target_method flags: ");
1273 target()->access_flags().print_on(tty);
1274 if (target()->is_default_method()) {
1275 tty->print("default ");
1276 }
1277 tty->cr();
1278 }
1279 }
1280 }
1281 }
1282 }
1284 // Update entry for specific Method*
1285 void klassItable::initialize_with_method(Method* m) {
1286 itableMethodEntry* ime = method_entry(0);
1287 for(int i = 0; i < _size_method_table; i++) {
1288 if (ime->method() == m) {
1289 ime->initialize(m);
1290 }
1291 ime++;
1292 }
1293 }
1295 #if INCLUDE_JVMTI
1296 // search the itable for uses of either obsolete or EMCP methods
1297 void klassItable::adjust_method_entries(InstanceKlass* holder, bool * trace_name_printed) {
1299 itableMethodEntry* ime = method_entry(0);
1300 for (int i = 0; i < _size_method_table; i++, ime++) {
1301 Method* old_method = ime->method();
1302 if (old_method == NULL || old_method->method_holder() != holder || !old_method->is_old()) {
1303 continue; // skip uninteresting entries
1304 }
1305 assert(!old_method->is_deleted(), "itable methods may not be deleted");
1307 Method* new_method = holder->method_with_idnum(old_method->orig_method_idnum());
1309 assert(new_method != NULL, "method_with_idnum() should not be NULL");
1310 assert(old_method != new_method, "sanity check");
1312 ime->initialize(new_method);
1314 if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
1315 if (!(*trace_name_printed)) {
1316 // RC_TRACE_MESG macro has an embedded ResourceMark
1317 RC_TRACE_MESG(("adjust: name=%s",
1318 old_method->method_holder()->external_name()));
1319 *trace_name_printed = true;
1320 }
1321 // RC_TRACE macro has an embedded ResourceMark
1322 RC_TRACE(0x00200000, ("itable method update: %s(%s)",
1323 new_method->name()->as_C_string(),
1324 new_method->signature()->as_C_string()));
1325 }
1326 }
1327 }
1329 // an itable should never contain old or obsolete methods
1330 bool klassItable::check_no_old_or_obsolete_entries() {
1331 itableMethodEntry* ime = method_entry(0);
1332 for (int i = 0; i < _size_method_table; i++) {
1333 Method* m = ime->method();
1334 if (m != NULL &&
1335 (NOT_PRODUCT(!m->is_valid() ||) m->is_old() || m->is_obsolete())) {
1336 return false;
1337 }
1338 ime++;
1339 }
1340 return true;
1341 }
1343 void klassItable::dump_itable() {
1344 itableMethodEntry* ime = method_entry(0);
1345 tty->print_cr("itable dump --");
1346 for (int i = 0; i < _size_method_table; i++) {
1347 Method* m = ime->method();
1348 if (m != NULL) {
1349 tty->print(" (%5d) ", i);
1350 m->access_flags().print_on(tty);
1351 if (m->is_default_method()) {
1352 tty->print("default ");
1353 }
1354 tty->print(" -- ");
1355 m->print_name(tty);
1356 tty->cr();
1357 }
1358 ime++;
1359 }
1360 }
1361 #endif // INCLUDE_JVMTI
1363 // Setup
1364 class InterfaceVisiterClosure : public StackObj {
1365 public:
1366 virtual void doit(Klass* intf, int method_count) = 0;
1367 };
1369 // Visit all interfaces with at least one itable method
1370 void visit_all_interfaces(Array<Klass*>* transitive_intf, InterfaceVisiterClosure *blk) {
1371 // Handle array argument
1372 for(int i = 0; i < transitive_intf->length(); i++) {
1373 Klass* intf = transitive_intf->at(i);
1374 assert(intf->is_interface(), "sanity check");
1376 // Find no. of itable methods
1377 int method_count = 0;
1378 // method_count = klassItable::method_count_for_interface(intf);
1379 Array<Method*>* methods = InstanceKlass::cast(intf)->methods();
1380 if (methods->length() > 0) {
1381 for (int i = methods->length(); --i >= 0; ) {
1382 if (interface_method_needs_itable_index(methods->at(i))) {
1383 method_count++;
1384 }
1385 }
1386 }
1388 // Visit all interfaces which either have any methods or can participate in receiver type check.
1389 // We do not bother to count methods in transitive interfaces, although that would allow us to skip
1390 // this step in the rare case of a zero-method interface extending another zero-method interface.
1391 if (method_count > 0 || InstanceKlass::cast(intf)->transitive_interfaces()->length() > 0) {
1392 blk->doit(intf, method_count);
1393 }
1394 }
1395 }
1397 class CountInterfacesClosure : public InterfaceVisiterClosure {
1398 private:
1399 int _nof_methods;
1400 int _nof_interfaces;
1401 public:
1402 CountInterfacesClosure() { _nof_methods = 0; _nof_interfaces = 0; }
1404 int nof_methods() const { return _nof_methods; }
1405 int nof_interfaces() const { return _nof_interfaces; }
1407 void doit(Klass* intf, int method_count) { _nof_methods += method_count; _nof_interfaces++; }
1408 };
1410 class SetupItableClosure : public InterfaceVisiterClosure {
1411 private:
1412 itableOffsetEntry* _offset_entry;
1413 itableMethodEntry* _method_entry;
1414 address _klass_begin;
1415 public:
1416 SetupItableClosure(address klass_begin, itableOffsetEntry* offset_entry, itableMethodEntry* method_entry) {
1417 _klass_begin = klass_begin;
1418 _offset_entry = offset_entry;
1419 _method_entry = method_entry;
1420 }
1422 itableMethodEntry* method_entry() const { return _method_entry; }
1424 void doit(Klass* intf, int method_count) {
1425 int offset = ((address)_method_entry) - _klass_begin;
1426 _offset_entry->initialize(intf, offset);
1427 _offset_entry++;
1428 _method_entry += method_count;
1429 }
1430 };
1432 int klassItable::compute_itable_size(Array<Klass*>* transitive_interfaces) {
1433 // Count no of interfaces and total number of interface methods
1434 CountInterfacesClosure cic;
1435 visit_all_interfaces(transitive_interfaces, &cic);
1437 // There's alway an extra itable entry so we can null-terminate it.
1438 int itable_size = calc_itable_size(cic.nof_interfaces() + 1, cic.nof_methods());
1440 // Statistics
1441 update_stats(itable_size * HeapWordSize);
1443 return itable_size;
1444 }
1447 // Fill out offset table and interface klasses into the itable space
1448 void klassItable::setup_itable_offset_table(instanceKlassHandle klass) {
1449 if (klass->itable_length() == 0) return;
1450 assert(!klass->is_interface(), "Should have zero length itable");
1452 // Count no of interfaces and total number of interface methods
1453 CountInterfacesClosure cic;
1454 visit_all_interfaces(klass->transitive_interfaces(), &cic);
1455 int nof_methods = cic.nof_methods();
1456 int nof_interfaces = cic.nof_interfaces();
1458 // Add one extra entry so we can null-terminate the table
1459 nof_interfaces++;
1461 assert(compute_itable_size(klass->transitive_interfaces()) ==
1462 calc_itable_size(nof_interfaces, nof_methods),
1463 "mismatch calculation of itable size");
1465 // Fill-out offset table
1466 itableOffsetEntry* ioe = (itableOffsetEntry*)klass->start_of_itable();
1467 itableMethodEntry* ime = (itableMethodEntry*)(ioe + nof_interfaces);
1468 intptr_t* end = klass->end_of_itable();
1469 assert((oop*)(ime + nof_methods) <= (oop*)klass->start_of_nonstatic_oop_maps(), "wrong offset calculation (1)");
1470 assert((oop*)(end) == (oop*)(ime + nof_methods), "wrong offset calculation (2)");
1472 // Visit all interfaces and initialize itable offset table
1473 SetupItableClosure sic((address)klass(), ioe, ime);
1474 visit_all_interfaces(klass->transitive_interfaces(), &sic);
1476 #ifdef ASSERT
1477 ime = sic.method_entry();
1478 oop* v = (oop*) klass->end_of_itable();
1479 assert( (oop*)(ime) == v, "wrong offset calculation (2)");
1480 #endif
1481 }
1484 // inverse to itable_index
1485 Method* klassItable::method_for_itable_index(Klass* intf, int itable_index) {
1486 assert(InstanceKlass::cast(intf)->is_interface(), "sanity check");
1487 assert(intf->verify_itable_index(itable_index), "");
1488 Array<Method*>* methods = InstanceKlass::cast(intf)->methods();
1490 if (itable_index < 0 || itable_index >= method_count_for_interface(intf))
1491 return NULL; // help caller defend against bad indices
1493 int index = itable_index;
1494 Method* m = methods->at(index);
1495 int index2 = -1;
1496 while (!m->has_itable_index() ||
1497 (index2 = m->itable_index()) != itable_index) {
1498 assert(index2 < itable_index, "monotonic");
1499 if (++index == methods->length())
1500 return NULL;
1501 m = methods->at(index);
1502 }
1503 assert(m->itable_index() == itable_index, "correct inverse");
1505 return m;
1506 }
1508 void klassVtable::verify(outputStream* st, bool forced) {
1509 // make sure table is initialized
1510 if (!Universe::is_fully_initialized()) return;
1511 #ifndef PRODUCT
1512 // avoid redundant verifies
1513 if (!forced && _verify_count == Universe::verify_count()) return;
1514 _verify_count = Universe::verify_count();
1515 #endif
1516 oop* end_of_obj = (oop*)_klass() + _klass()->size();
1517 oop* end_of_vtable = (oop *)&table()[_length];
1518 if (end_of_vtable > end_of_obj) {
1519 fatal(err_msg("klass %s: klass object too short (vtable extends beyond "
1520 "end)", _klass->internal_name()));
1521 }
1523 for (int i = 0; i < _length; i++) table()[i].verify(this, st);
1524 // verify consistency with superKlass vtable
1525 Klass* super = _klass->super();
1526 if (super != NULL) {
1527 InstanceKlass* sk = InstanceKlass::cast(super);
1528 klassVtable* vt = sk->vtable();
1529 for (int i = 0; i < vt->length(); i++) {
1530 verify_against(st, vt, i);
1531 }
1532 }
1533 }
1535 void klassVtable::verify_against(outputStream* st, klassVtable* vt, int index) {
1536 vtableEntry* vte = &vt->table()[index];
1537 if (vte->method()->name() != table()[index].method()->name() ||
1538 vte->method()->signature() != table()[index].method()->signature()) {
1539 fatal("mismatched name/signature of vtable entries");
1540 }
1541 }
1543 #ifndef PRODUCT
1544 void klassVtable::print() {
1545 ResourceMark rm;
1546 tty->print("klassVtable for klass %s (length %d):\n", _klass->internal_name(), length());
1547 for (int i = 0; i < length(); i++) {
1548 table()[i].print();
1549 tty->cr();
1550 }
1551 }
1552 #endif
1554 void vtableEntry::verify(klassVtable* vt, outputStream* st) {
1555 NOT_PRODUCT(FlagSetting fs(IgnoreLockingAssertions, true));
1556 assert(method() != NULL, "must have set method");
1557 method()->verify();
1558 // we sub_type, because it could be a miranda method
1559 if (!vt->klass()->is_subtype_of(method()->method_holder())) {
1560 #ifndef PRODUCT
1561 print();
1562 #endif
1563 fatal(err_msg("vtableEntry " PTR_FORMAT ": method is from subclass", this));
1564 }
1565 }
1567 #ifndef PRODUCT
1569 void vtableEntry::print() {
1570 ResourceMark rm;
1571 tty->print("vtableEntry %s: ", method()->name()->as_C_string());
1572 if (Verbose) {
1573 tty->print("m %#lx ", (address)method());
1574 }
1575 }
1577 class VtableStats : AllStatic {
1578 public:
1579 static int no_klasses; // # classes with vtables
1580 static int no_array_klasses; // # array classes
1581 static int no_instance_klasses; // # instanceKlasses
1582 static int sum_of_vtable_len; // total # of vtable entries
1583 static int sum_of_array_vtable_len; // total # of vtable entries in array klasses only
1584 static int fixed; // total fixed overhead in bytes
1585 static int filler; // overhead caused by filler bytes
1586 static int entries; // total bytes consumed by vtable entries
1587 static int array_entries; // total bytes consumed by array vtable entries
1589 static void do_class(Klass* k) {
1590 Klass* kl = k;
1591 klassVtable* vt = kl->vtable();
1592 if (vt == NULL) return;
1593 no_klasses++;
1594 if (kl->oop_is_instance()) {
1595 no_instance_klasses++;
1596 kl->array_klasses_do(do_class);
1597 }
1598 if (kl->oop_is_array()) {
1599 no_array_klasses++;
1600 sum_of_array_vtable_len += vt->length();
1601 }
1602 sum_of_vtable_len += vt->length();
1603 }
1605 static void compute() {
1606 SystemDictionary::classes_do(do_class);
1607 fixed = no_klasses * oopSize; // vtable length
1608 // filler size is a conservative approximation
1609 filler = oopSize * (no_klasses - no_instance_klasses) * (sizeof(InstanceKlass) - sizeof(ArrayKlass) - 1);
1610 entries = sizeof(vtableEntry) * sum_of_vtable_len;
1611 array_entries = sizeof(vtableEntry) * sum_of_array_vtable_len;
1612 }
1613 };
1615 int VtableStats::no_klasses = 0;
1616 int VtableStats::no_array_klasses = 0;
1617 int VtableStats::no_instance_klasses = 0;
1618 int VtableStats::sum_of_vtable_len = 0;
1619 int VtableStats::sum_of_array_vtable_len = 0;
1620 int VtableStats::fixed = 0;
1621 int VtableStats::filler = 0;
1622 int VtableStats::entries = 0;
1623 int VtableStats::array_entries = 0;
1625 void klassVtable::print_statistics() {
1626 ResourceMark rm;
1627 HandleMark hm;
1628 VtableStats::compute();
1629 tty->print_cr("vtable statistics:");
1630 tty->print_cr("%6d classes (%d instance, %d array)", VtableStats::no_klasses, VtableStats::no_instance_klasses, VtableStats::no_array_klasses);
1631 int total = VtableStats::fixed + VtableStats::filler + VtableStats::entries;
1632 tty->print_cr("%6d bytes fixed overhead (refs + vtable object header)", VtableStats::fixed);
1633 tty->print_cr("%6d bytes filler overhead", VtableStats::filler);
1634 tty->print_cr("%6d bytes for vtable entries (%d for arrays)", VtableStats::entries, VtableStats::array_entries);
1635 tty->print_cr("%6d bytes total", total);
1636 }
1638 int klassItable::_total_classes; // Total no. of classes with itables
1639 long klassItable::_total_size; // Total no. of bytes used for itables
1641 void klassItable::print_statistics() {
1642 tty->print_cr("itable statistics:");
1643 tty->print_cr("%6d classes with itables", _total_classes);
1644 tty->print_cr("%6d K uses for itables (average by class: %d bytes)", _total_size / K, _total_size / _total_classes);
1645 }
1647 #endif // PRODUCT