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