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