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