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