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 "gc/shared/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 inline InstanceKlass* klassVtable::ik() const { 42 Klass* k = _klass(); 43 assert(k->oop_is_instance(), "not an InstanceKlass"); 44 return (InstanceKlass*)k; 45 } 46 47 48 // this function computes the vtable size (including the size needed for miranda 49 // methods) and the number of miranda methods in this class. 50 // Note on Miranda methods: Let's say there is a class C that implements 51 // interface I, and none of C's superclasses implements I. 52 // Let's say there is an abstract method m in I that neither C 53 // nor any of its super classes implement (i.e there is no method of any access, 54 // with the same name and signature as m), then m is a Miranda method which is 55 // entered as a public abstract method in C's vtable. From then on it should 56 // treated as any other public method in C for method over-ride purposes. 57 void klassVtable::compute_vtable_size_and_num_mirandas( 58 int* vtable_length_ret, int* num_new_mirandas, 59 GrowableArray<Method*>* all_mirandas, Klass* super, 60 Array<Method*>* methods, AccessFlags class_flags, 61 Handle classloader, Symbol* classname, Array<Klass*>* local_interfaces, 62 TRAPS) { 63 No_Safepoint_Verifier nsv; 64 65 // set up default result values 66 int vtable_length = 0; 67 68 // start off with super's vtable length 69 InstanceKlass* sk = (InstanceKlass*)super; 70 vtable_length = super == NULL ? 0 : sk->vtable_length(); 71 72 // go thru each method in the methods table to see if it needs a new entry 73 int len = methods->length(); 74 for (int i = 0; i < len; i++) { 75 assert(methods->at(i)->is_method(), "must be a Method*"); 76 methodHandle mh(THREAD, methods->at(i)); 77 78 if (needs_new_vtable_entry(mh, super, classloader, classname, class_flags, THREAD)) { 79 vtable_length += vtableEntry::size(); // we need a new entry 80 } 81 } 82 83 GrowableArray<Method*> new_mirandas(20); 84 // compute the number of mirandas methods that must be added to the end 85 get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces); 86 *num_new_mirandas = new_mirandas.length(); 87 88 // Interfaces do not need interface methods in their vtables 89 // This includes miranda methods and during later processing, default methods 90 if (!class_flags.is_interface()) { 91 vtable_length += *num_new_mirandas * vtableEntry::size(); 92 } 93 94 if (Universe::is_bootstrapping() && vtable_length == 0) { 95 // array classes don't have their superclass set correctly during 96 // bootstrapping 97 vtable_length = Universe::base_vtable_size(); 98 } 99 100 if (super == NULL && vtable_length != Universe::base_vtable_size()) { 101 if (Universe::is_bootstrapping()) { 102 // Someone is attempting to override java.lang.Object incorrectly on the 103 // bootclasspath. The JVM cannot recover from this error including throwing 104 // an exception 105 vm_exit_during_initialization("Incompatible definition of java.lang.Object"); 106 } else { 107 // Someone is attempting to redefine java.lang.Object incorrectly. The 108 // only way this should happen is from 109 // SystemDictionary::resolve_from_stream(), which will detect this later 110 // and throw a security exception. So don't assert here to let 111 // the exception occur. 112 vtable_length = Universe::base_vtable_size(); 113 } 114 } 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 // private methods are also never overridden 406 if (!super_method->is_private() && 407 (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 if (is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super())) { 685 return true; 686 } 687 } 688 return false; 689 } 690 691 // Check if a method is a miranda method, given a class's methods array, 692 // its default_method table and its super class. 693 // "Miranda" means an abstract non-private method that would not be 694 // overridden for the local class. 695 // A "miranda" method should only include non-private interface 696 // instance methods, i.e. not private methods, not static methods, 697 // not default methods (concrete interface methods), not overpass methods. 698 // If a given class already has a local (including overpass) method, a 699 // default method, or any of its superclasses has the same which would have 700 // overridden an abstract method, then this is not a miranda method. 701 // 702 // Miranda methods are checked multiple times. 703 // Pass 1: during class load/class file parsing: before vtable size calculation: 704 // include superinterface abstract and default methods (non-private instance). 705 // We include potential default methods to give them space in the vtable. 706 // During the first run, the current instanceKlass has not yet been 707 // created, the superclasses and superinterfaces do have instanceKlasses 708 // but may not have vtables, the default_methods list is empty, no overpasses. 709 // This is seen by default method creation. 710 // 711 // Pass 2: recalculated during vtable initialization: only include abstract methods. 712 // The goal of pass 2 is to walk through the superinterfaces to see if any of 713 // the superinterface methods (which were all abstract pre-default methods) 714 // need to be added to the vtable. 715 // With the addition of default methods, we have three new challenges: 716 // overpasses, static interface methods and private interface methods. 717 // Static and private interface methods do not get added to the vtable and 718 // are not seen by the method resolution process, so we skip those. 719 // Overpass methods are already in the vtable, so vtable lookup will 720 // find them and we don't need to add a miranda method to the end of 721 // the vtable. So we look for overpass methods and if they are found we 722 // return false. Note that we inherit our superclasses vtable, so 723 // the superclass' search also needs to use find_overpass so that if 724 // one is found we return false. 725 // False means - we don't need a miranda method added to the vtable. 726 // 727 // During the second run, default_methods is set up, so concrete methods from 728 // superinterfaces with matching names/signatures to default_methods are already 729 // in the default_methods list and do not need to be appended to the vtable 730 // as mirandas. Abstract methods may already have been handled via 731 // overpasses - either local or superclass overpasses, which may be 732 // in the vtable already. 733 // 734 // Pass 3: They are also checked by link resolution and selection, 735 // for invocation on a method (not interface method) reference that 736 // resolves to a method with an interface as its method_holder. 737 // Used as part of walking from the bottom of the vtable to find 738 // the vtable index for the miranda method. 739 // 740 // Part of the Miranda Rights in the US mean that if you do not have 741 // an attorney one will be appointed for you. 742 bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods, 743 Array<Method*>* default_methods, Klass* super) { 744 if (m->is_static() || m->is_private() || m->is_overpass()) { 745 return false; 746 } 747 Symbol* name = m->name(); 748 Symbol* signature = m->signature(); 749 750 // First look in local methods to see if already covered 751 if (InstanceKlass::find_local_method(class_methods, name, signature, 752 Klass::find_overpass, Klass::skip_static, Klass::skip_private) != NULL) 753 { 754 return false; 755 } 756 757 // Check local default methods 758 if ((default_methods != NULL) && 759 (InstanceKlass::find_method(default_methods, name, signature) != NULL)) 760 { 761 return false; 762 } 763 764 InstanceKlass* cursuper; 765 // Iterate on all superclasses, which should have instanceKlasses 766 // Note that we explicitly look for overpasses at each level. 767 // Overpasses may or may not exist for supers for pass 1, 768 // they should have been created for pass 2 and later. 769 770 for (cursuper = InstanceKlass::cast(super); cursuper != NULL; cursuper = (InstanceKlass*)cursuper->super()) 771 { 772 if (cursuper->find_local_method(name, signature, 773 Klass::find_overpass, Klass::skip_static, Klass::skip_private) != NULL) { 774 return false; 775 } 776 } 777 778 return true; 779 } 780 781 // Scans current_interface_methods for miranda methods that do not 782 // already appear in new_mirandas, or default methods, and are also not defined-and-non-private 783 // in super (superclass). These mirandas are added to all_mirandas if it is 784 // not null; in addition, those that are not duplicates of miranda methods 785 // inherited by super from its interfaces are added to new_mirandas. 786 // Thus, new_mirandas will be the set of mirandas that this class introduces, 787 // all_mirandas will be the set of all mirandas applicable to this class 788 // including all defined in superclasses. 789 void klassVtable::add_new_mirandas_to_lists( 790 GrowableArray<Method*>* new_mirandas, GrowableArray<Method*>* all_mirandas, 791 Array<Method*>* current_interface_methods, Array<Method*>* class_methods, 792 Array<Method*>* default_methods, Klass* super) { 793 794 // iterate thru the current interface's method to see if it a miranda 795 int num_methods = current_interface_methods->length(); 796 for (int i = 0; i < num_methods; i++) { 797 Method* im = current_interface_methods->at(i); 798 bool is_duplicate = false; 799 int num_of_current_mirandas = new_mirandas->length(); 800 // check for duplicate mirandas in different interfaces we implement 801 for (int j = 0; j < num_of_current_mirandas; j++) { 802 Method* miranda = new_mirandas->at(j); 803 if ((im->name() == miranda->name()) && 804 (im->signature() == miranda->signature())) { 805 is_duplicate = true; 806 break; 807 } 808 } 809 810 if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable 811 if (is_miranda(im, class_methods, default_methods, super)) { // is it a miranda at all? 812 InstanceKlass *sk = InstanceKlass::cast(super); 813 // check if it is a duplicate of a super's miranda 814 if (sk->lookup_method_in_all_interfaces(im->name(), im->signature(), Klass::find_defaults) == NULL) { 815 new_mirandas->append(im); 816 } 817 if (all_mirandas != NULL) { 818 all_mirandas->append(im); 819 } 820 } 821 } 822 } 823 } 824 825 void klassVtable::get_mirandas(GrowableArray<Method*>* new_mirandas, 826 GrowableArray<Method*>* all_mirandas, 827 Klass* super, Array<Method*>* class_methods, 828 Array<Method*>* default_methods, 829 Array<Klass*>* local_interfaces) { 830 assert((new_mirandas->length() == 0) , "current mirandas must be 0"); 831 832 // iterate thru the local interfaces looking for a miranda 833 int num_local_ifs = local_interfaces->length(); 834 for (int i = 0; i < num_local_ifs; i++) { 835 InstanceKlass *ik = InstanceKlass::cast(local_interfaces->at(i)); 836 add_new_mirandas_to_lists(new_mirandas, all_mirandas, 837 ik->methods(), class_methods, 838 default_methods, super); 839 // iterate thru each local's super interfaces 840 Array<Klass*>* super_ifs = ik->transitive_interfaces(); 841 int num_super_ifs = super_ifs->length(); 842 for (int j = 0; j < num_super_ifs; j++) { 843 InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j)); 844 add_new_mirandas_to_lists(new_mirandas, all_mirandas, 845 sik->methods(), class_methods, 846 default_methods, super); 847 } 848 } 849 } 850 851 // Discover miranda methods ("miranda" = "interface abstract, no binding"), 852 // and append them into the vtable starting at index initialized, 853 // return the new value of initialized. 854 // Miranda methods use vtable entries, but do not get assigned a vtable_index 855 // The vtable_index is discovered by searching from the end of the vtable 856 int klassVtable::fill_in_mirandas(int initialized) { 857 GrowableArray<Method*> mirandas(20); 858 get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(), 859 ik()->default_methods(), ik()->local_interfaces()); 860 for (int i = 0; i < mirandas.length(); i++) { 861 if (PrintVtables && Verbose) { 862 Method* meth = mirandas.at(i); 863 ResourceMark rm(Thread::current()); 864 if (meth != NULL) { 865 char* sig = meth->name_and_sig_as_C_string(); 866 tty->print("fill in mirandas with %s index %d, flags: ", 867 sig, initialized); 868 meth->access_flags().print_on(tty); 869 if (meth->is_default_method()) { 870 tty->print("default "); 871 } 872 tty->cr(); 873 } 874 } 875 put_method_at(mirandas.at(i), initialized); 876 ++initialized; 877 } 878 return initialized; 879 } 880 881 // Copy this class's vtable to the vtable beginning at start. 882 // Used to copy superclass vtable to prefix of subclass's vtable. 883 void klassVtable::copy_vtable_to(vtableEntry* start) { 884 Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size()); 885 } 886 887 #if INCLUDE_JVMTI 888 bool klassVtable::adjust_default_method(int vtable_index, Method* old_method, Method* new_method) { 889 // If old_method is default, find this vtable index in default_vtable_indices 890 // and replace that method in the _default_methods list 891 bool updated = false; 892 893 Array<Method*>* default_methods = ik()->default_methods(); 894 if (default_methods != NULL) { 895 int len = default_methods->length(); 896 for (int idx = 0; idx < len; idx++) { 897 if (vtable_index == ik()->default_vtable_indices()->at(idx)) { 898 if (default_methods->at(idx) == old_method) { 899 default_methods->at_put(idx, new_method); 900 updated = true; 901 } 902 break; 903 } 904 } 905 } 906 return updated; 907 } 908 909 // search the vtable for uses of either obsolete or EMCP methods 910 void klassVtable::adjust_method_entries(InstanceKlass* holder, bool * trace_name_printed) { 911 int prn_enabled = 0; 912 for (int index = 0; index < length(); index++) { 913 Method* old_method = unchecked_method_at(index); 914 if (old_method == NULL || old_method->method_holder() != holder || !old_method->is_old()) { 915 continue; // skip uninteresting entries 916 } 917 assert(!old_method->is_deleted(), "vtable methods may not be deleted"); 918 919 Method* new_method = holder->method_with_idnum(old_method->orig_method_idnum()); 920 921 assert(new_method != NULL, "method_with_idnum() should not be NULL"); 922 assert(old_method != new_method, "sanity check"); 923 924 put_method_at(new_method, index); 925 // For default methods, need to update the _default_methods array 926 // which can only have one method entry for a given signature 927 bool updated_default = false; 928 if (old_method->is_default_method()) { 929 updated_default = adjust_default_method(index, old_method, new_method); 930 } 931 932 if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) { 933 if (!(*trace_name_printed)) { 934 // RC_TRACE_MESG macro has an embedded ResourceMark 935 RC_TRACE_MESG(("adjust: klassname=%s for methods from name=%s", 936 klass()->external_name(), 937 old_method->method_holder()->external_name())); 938 *trace_name_printed = true; 939 } 940 // RC_TRACE macro has an embedded ResourceMark 941 RC_TRACE(0x00100000, ("vtable method update: %s(%s), updated default = %s", 942 new_method->name()->as_C_string(), 943 new_method->signature()->as_C_string(), 944 updated_default ? "true" : "false")); 945 } 946 } 947 } 948 949 // a vtable should never contain old or obsolete methods 950 bool klassVtable::check_no_old_or_obsolete_entries() { 951 for (int i = 0; i < length(); i++) { 952 Method* m = unchecked_method_at(i); 953 if (m != NULL && 954 (NOT_PRODUCT(!m->is_valid() ||) m->is_old() || m->is_obsolete())) { 955 return false; 956 } 957 } 958 return true; 959 } 960 961 void klassVtable::dump_vtable() { 962 tty->print_cr("vtable dump --"); 963 for (int i = 0; i < length(); i++) { 964 Method* m = unchecked_method_at(i); 965 if (m != NULL) { 966 tty->print(" (%5d) ", i); 967 m->access_flags().print_on(tty); 968 if (m->is_default_method()) { 969 tty->print("default "); 970 } 971 if (m->is_overpass()) { 972 tty->print("overpass"); 973 } 974 tty->print(" -- "); 975 m->print_name(tty); 976 tty->cr(); 977 } 978 } 979 } 980 #endif // INCLUDE_JVMTI 981 982 // CDS/RedefineClasses support - clear vtables so they can be reinitialized 983 void klassVtable::clear_vtable() { 984 for (int i = 0; i < _length; i++) table()[i].clear(); 985 } 986 987 bool klassVtable::is_initialized() { 988 return _length == 0 || table()[0].method() != NULL; 989 } 990 991 //----------------------------------------------------------------------------------------- 992 // Itable code 993 994 // Initialize a itableMethodEntry 995 void itableMethodEntry::initialize(Method* m) { 996 if (m == NULL) return; 997 998 _method = m; 999 } 1000 1001 klassItable::klassItable(instanceKlassHandle klass) { 1002 _klass = klass; 1003 1004 if (klass->itable_length() > 0) { 1005 itableOffsetEntry* offset_entry = (itableOffsetEntry*)klass->start_of_itable(); 1006 if (offset_entry != NULL && offset_entry->interface_klass() != NULL) { // Check that itable is initialized 1007 // First offset entry points to the first method_entry 1008 intptr_t* method_entry = (intptr_t *)(((address)klass()) + offset_entry->offset()); 1009 intptr_t* end = klass->end_of_itable(); 1010 1011 _table_offset = (intptr_t*)offset_entry - (intptr_t*)klass(); 1012 _size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size(); 1013 _size_method_table = (end - method_entry) / itableMethodEntry::size(); 1014 assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation"); 1015 return; 1016 } 1017 } 1018 1019 // The length of the itable was either zero, or it has not yet been initialized. 1020 _table_offset = 0; 1021 _size_offset_table = 0; 1022 _size_method_table = 0; 1023 } 1024 1025 static int initialize_count = 0; 1026 1027 // Initialization 1028 void klassItable::initialize_itable(bool checkconstraints, TRAPS) { 1029 if (_klass->is_interface()) { 1030 // This needs to go after vtable indices are assigned but 1031 // before implementors need to know the number of itable indices. 1032 assign_itable_indices_for_interface(_klass()); 1033 } 1034 1035 // Cannot be setup doing bootstrapping, interfaces don't have 1036 // itables, and klass with only ones entry have empty itables 1037 if (Universe::is_bootstrapping() || 1038 _klass->is_interface() || 1039 _klass->itable_length() == itableOffsetEntry::size()) return; 1040 1041 // There's alway an extra itable entry so we can null-terminate it. 1042 guarantee(size_offset_table() >= 1, "too small"); 1043 int num_interfaces = size_offset_table() - 1; 1044 if (num_interfaces > 0) { 1045 if (TraceItables) tty->print_cr("%3d: Initializing itables for %s", ++initialize_count, 1046 _klass->name()->as_C_string()); 1047 1048 1049 // Iterate through all interfaces 1050 int i; 1051 for(i = 0; i < num_interfaces; i++) { 1052 itableOffsetEntry* ioe = offset_entry(i); 1053 HandleMark hm(THREAD); 1054 KlassHandle interf_h (THREAD, ioe->interface_klass()); 1055 assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable"); 1056 initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK); 1057 } 1058 1059 } 1060 // Check that the last entry is empty 1061 itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1); 1062 guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing"); 1063 } 1064 1065 1066 inline bool interface_method_needs_itable_index(Method* m) { 1067 if (m->is_static()) return false; // e.g., Stream.empty 1068 if (m->is_initializer()) return false; // <init> or <clinit> 1069 // If an interface redeclares a method from java.lang.Object, 1070 // it should already have a vtable index, don't touch it. 1071 // e.g., CharSequence.toString (from initialize_vtable) 1072 // if (m->has_vtable_index()) return false; // NO! 1073 return true; 1074 } 1075 1076 int klassItable::assign_itable_indices_for_interface(Klass* klass) { 1077 // an interface does not have an itable, but its methods need to be numbered 1078 if (TraceItables) tty->print_cr("%3d: Initializing itable indices for interface %s", ++initialize_count, 1079 klass->name()->as_C_string()); 1080 Array<Method*>* methods = InstanceKlass::cast(klass)->methods(); 1081 int nof_methods = methods->length(); 1082 int ime_num = 0; 1083 for (int i = 0; i < nof_methods; i++) { 1084 Method* m = methods->at(i); 1085 if (interface_method_needs_itable_index(m)) { 1086 assert(!m->is_final_method(), "no final interface methods"); 1087 // If m is already assigned a vtable index, do not disturb it. 1088 if (TraceItables && Verbose) { 1089 ResourceMark rm; 1090 const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>"; 1091 if (m->has_vtable_index()) { 1092 tty->print("vtable index %d for method: %s, flags: ", m->vtable_index(), sig); 1093 } else { 1094 tty->print("itable index %d for method: %s, flags: ", ime_num, sig); 1095 } 1096 if (m != NULL) { 1097 m->access_flags().print_on(tty); 1098 if (m->is_default_method()) { 1099 tty->print("default "); 1100 } 1101 if (m->is_overpass()) { 1102 tty->print("overpass"); 1103 } 1104 } 1105 tty->cr(); 1106 } 1107 if (!m->has_vtable_index()) { 1108 assert(m->vtable_index() == Method::pending_itable_index, "set by initialize_vtable"); 1109 m->set_itable_index(ime_num); 1110 // Progress to next itable entry 1111 ime_num++; 1112 } 1113 } 1114 } 1115 assert(ime_num == method_count_for_interface(klass), "proper sizing"); 1116 return ime_num; 1117 } 1118 1119 int klassItable::method_count_for_interface(Klass* interf) { 1120 assert(interf->oop_is_instance(), "must be"); 1121 assert(interf->is_interface(), "must be"); 1122 Array<Method*>* methods = InstanceKlass::cast(interf)->methods(); 1123 int nof_methods = methods->length(); 1124 int length = 0; 1125 while (nof_methods > 0) { 1126 Method* m = methods->at(nof_methods-1); 1127 if (m->has_itable_index()) { 1128 length = m->itable_index() + 1; 1129 break; 1130 } 1131 nof_methods -= 1; 1132 } 1133 #ifdef ASSERT 1134 int nof_methods_copy = nof_methods; 1135 while (nof_methods_copy > 0) { 1136 Method* mm = methods->at(--nof_methods_copy); 1137 assert(!mm->has_itable_index() || mm->itable_index() < length, ""); 1138 } 1139 #endif //ASSERT 1140 // return the rightmost itable index, plus one; or 0 if no methods have 1141 // itable indices 1142 return length; 1143 } 1144 1145 1146 void klassItable::initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS) { 1147 Array<Method*>* methods = InstanceKlass::cast(interf_h())->methods(); 1148 int nof_methods = methods->length(); 1149 HandleMark hm; 1150 assert(nof_methods > 0, "at least one method must exist for interface to be in vtable"); 1151 Handle interface_loader (THREAD, InstanceKlass::cast(interf_h())->class_loader()); 1152 1153 int ime_count = method_count_for_interface(interf_h()); 1154 for (int i = 0; i < nof_methods; i++) { 1155 Method* m = methods->at(i); 1156 methodHandle target; 1157 if (m->has_itable_index()) { 1158 // This search must match the runtime resolution, i.e. selection search for invokeinterface 1159 // to correctly enforce loader constraints for interface method inheritance 1160 target = LinkResolver::lookup_instance_method_in_klasses(_klass, m->name(), m->signature(), CHECK); 1161 } 1162 if (target == NULL || !target->is_public() || target->is_abstract()) { 1163 // Entry does not resolve. Leave it empty for AbstractMethodError. 1164 if (!(target == NULL) && !target->is_public()) { 1165 // Stuff an IllegalAccessError throwing method in there instead. 1166 itableOffsetEntry::method_entry(_klass(), method_table_offset)[m->itable_index()]. 1167 initialize(Universe::throw_illegal_access_error()); 1168 } 1169 } else { 1170 // Entry did resolve, check loader constraints before initializing 1171 // if checkconstraints requested 1172 if (checkconstraints) { 1173 Handle method_holder_loader (THREAD, target->method_holder()->class_loader()); 1174 if (method_holder_loader() != interface_loader()) { 1175 ResourceMark rm(THREAD); 1176 Symbol* failed_type_symbol = 1177 SystemDictionary::check_signature_loaders(m->signature(), 1178 method_holder_loader, 1179 interface_loader, 1180 true, CHECK); 1181 if (failed_type_symbol != NULL) { 1182 const char* msg = "loader constraint violation in interface " 1183 "itable initialization: when resolving method \"%s\" the class" 1184 " loader (instance of %s) of the current class, %s, " 1185 "and the class loader (instance of %s) for interface " 1186 "%s have different Class objects for the type %s " 1187 "used in the signature"; 1188 char* sig = target()->name_and_sig_as_C_string(); 1189 const char* loader1 = SystemDictionary::loader_name(method_holder_loader()); 1190 char* current = _klass->name()->as_C_string(); 1191 const char* loader2 = SystemDictionary::loader_name(interface_loader()); 1192 char* iface = InstanceKlass::cast(interf_h())->name()->as_C_string(); 1193 char* failed_type_name = failed_type_symbol->as_C_string(); 1194 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) + 1195 strlen(current) + strlen(loader2) + strlen(iface) + 1196 strlen(failed_type_name); 1197 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen); 1198 jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2, 1199 iface, failed_type_name); 1200 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf); 1201 } 1202 } 1203 } 1204 1205 // ime may have moved during GC so recalculate address 1206 int ime_num = m->itable_index(); 1207 assert(ime_num < ime_count, "oob"); 1208 itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target()); 1209 if (TraceItables && Verbose) { 1210 ResourceMark rm(THREAD); 1211 if (target() != NULL) { 1212 char* sig = target()->name_and_sig_as_C_string(); 1213 tty->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ", 1214 interf_h()->internal_name(), ime_num, sig, 1215 target()->method_holder()->internal_name()); 1216 tty->print("target_method flags: "); 1217 target()->access_flags().print_on(tty); 1218 if (target()->is_default_method()) { 1219 tty->print("default "); 1220 } 1221 tty->cr(); 1222 } 1223 } 1224 } 1225 } 1226 } 1227 1228 // Update entry for specific Method* 1229 void klassItable::initialize_with_method(Method* m) { 1230 itableMethodEntry* ime = method_entry(0); 1231 for(int i = 0; i < _size_method_table; i++) { 1232 if (ime->method() == m) { 1233 ime->initialize(m); 1234 } 1235 ime++; 1236 } 1237 } 1238 1239 #if INCLUDE_JVMTI 1240 // search the itable for uses of either obsolete or EMCP methods 1241 void klassItable::adjust_method_entries(InstanceKlass* holder, bool * trace_name_printed) { 1242 1243 itableMethodEntry* ime = method_entry(0); 1244 for (int i = 0; i < _size_method_table; i++, ime++) { 1245 Method* old_method = ime->method(); 1246 if (old_method == NULL || old_method->method_holder() != holder || !old_method->is_old()) { 1247 continue; // skip uninteresting entries 1248 } 1249 assert(!old_method->is_deleted(), "itable methods may not be deleted"); 1250 1251 Method* new_method = holder->method_with_idnum(old_method->orig_method_idnum()); 1252 1253 assert(new_method != NULL, "method_with_idnum() should not be NULL"); 1254 assert(old_method != new_method, "sanity check"); 1255 1256 ime->initialize(new_method); 1257 1258 if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) { 1259 if (!(*trace_name_printed)) { 1260 // RC_TRACE_MESG macro has an embedded ResourceMark 1261 RC_TRACE_MESG(("adjust: name=%s", 1262 old_method->method_holder()->external_name())); 1263 *trace_name_printed = true; 1264 } 1265 // RC_TRACE macro has an embedded ResourceMark 1266 RC_TRACE(0x00200000, ("itable method update: %s(%s)", 1267 new_method->name()->as_C_string(), 1268 new_method->signature()->as_C_string())); 1269 } 1270 } 1271 } 1272 1273 // an itable should never contain old or obsolete methods 1274 bool klassItable::check_no_old_or_obsolete_entries() { 1275 itableMethodEntry* ime = method_entry(0); 1276 for (int i = 0; i < _size_method_table; i++) { 1277 Method* m = ime->method(); 1278 if (m != NULL && 1279 (NOT_PRODUCT(!m->is_valid() ||) m->is_old() || m->is_obsolete())) { 1280 return false; 1281 } 1282 ime++; 1283 } 1284 return true; 1285 } 1286 1287 void klassItable::dump_itable() { 1288 itableMethodEntry* ime = method_entry(0); 1289 tty->print_cr("itable dump --"); 1290 for (int i = 0; i < _size_method_table; i++) { 1291 Method* m = ime->method(); 1292 if (m != NULL) { 1293 tty->print(" (%5d) ", i); 1294 m->access_flags().print_on(tty); 1295 if (m->is_default_method()) { 1296 tty->print("default "); 1297 } 1298 tty->print(" -- "); 1299 m->print_name(tty); 1300 tty->cr(); 1301 } 1302 ime++; 1303 } 1304 } 1305 #endif // INCLUDE_JVMTI 1306 1307 1308 // Setup 1309 class InterfaceVisiterClosure : public StackObj { 1310 public: 1311 virtual void doit(Klass* intf, int method_count) = 0; 1312 }; 1313 1314 // Visit all interfaces with at least one itable method 1315 void visit_all_interfaces(Array<Klass*>* transitive_intf, InterfaceVisiterClosure *blk) { 1316 // Handle array argument 1317 for(int i = 0; i < transitive_intf->length(); i++) { 1318 Klass* intf = transitive_intf->at(i); 1319 assert(intf->is_interface(), "sanity check"); 1320 1321 // Find no. of itable methods 1322 int method_count = 0; 1323 // method_count = klassItable::method_count_for_interface(intf); 1324 Array<Method*>* methods = InstanceKlass::cast(intf)->methods(); 1325 if (methods->length() > 0) { 1326 for (int i = methods->length(); --i >= 0; ) { 1327 if (interface_method_needs_itable_index(methods->at(i))) { 1328 method_count++; 1329 } 1330 } 1331 } 1332 1333 // Only count interfaces with at least one method 1334 if (method_count > 0) { 1335 blk->doit(intf, method_count); 1336 } 1337 } 1338 } 1339 1340 class CountInterfacesClosure : public InterfaceVisiterClosure { 1341 private: 1342 int _nof_methods; 1343 int _nof_interfaces; 1344 public: 1345 CountInterfacesClosure() { _nof_methods = 0; _nof_interfaces = 0; } 1346 1347 int nof_methods() const { return _nof_methods; } 1348 int nof_interfaces() const { return _nof_interfaces; } 1349 1350 void doit(Klass* intf, int method_count) { _nof_methods += method_count; _nof_interfaces++; } 1351 }; 1352 1353 class SetupItableClosure : public InterfaceVisiterClosure { 1354 private: 1355 itableOffsetEntry* _offset_entry; 1356 itableMethodEntry* _method_entry; 1357 address _klass_begin; 1358 public: 1359 SetupItableClosure(address klass_begin, itableOffsetEntry* offset_entry, itableMethodEntry* method_entry) { 1360 _klass_begin = klass_begin; 1361 _offset_entry = offset_entry; 1362 _method_entry = method_entry; 1363 } 1364 1365 itableMethodEntry* method_entry() const { return _method_entry; } 1366 1367 void doit(Klass* intf, int method_count) { 1368 int offset = ((address)_method_entry) - _klass_begin; 1369 _offset_entry->initialize(intf, offset); 1370 _offset_entry++; 1371 _method_entry += method_count; 1372 } 1373 }; 1374 1375 int klassItable::compute_itable_size(Array<Klass*>* transitive_interfaces) { 1376 // Count no of interfaces and total number of interface methods 1377 CountInterfacesClosure cic; 1378 visit_all_interfaces(transitive_interfaces, &cic); 1379 1380 // There's alway an extra itable entry so we can null-terminate it. 1381 int itable_size = calc_itable_size(cic.nof_interfaces() + 1, cic.nof_methods()); 1382 1383 // Statistics 1384 update_stats(itable_size * HeapWordSize); 1385 1386 return itable_size; 1387 } 1388 1389 1390 // Fill out offset table and interface klasses into the itable space 1391 void klassItable::setup_itable_offset_table(instanceKlassHandle klass) { 1392 if (klass->itable_length() == 0) return; 1393 assert(!klass->is_interface(), "Should have zero length itable"); 1394 1395 // Count no of interfaces and total number of interface methods 1396 CountInterfacesClosure cic; 1397 visit_all_interfaces(klass->transitive_interfaces(), &cic); 1398 int nof_methods = cic.nof_methods(); 1399 int nof_interfaces = cic.nof_interfaces(); 1400 1401 // Add one extra entry so we can null-terminate the table 1402 nof_interfaces++; 1403 1404 assert(compute_itable_size(klass->transitive_interfaces()) == 1405 calc_itable_size(nof_interfaces, nof_methods), 1406 "mismatch calculation of itable size"); 1407 1408 // Fill-out offset table 1409 itableOffsetEntry* ioe = (itableOffsetEntry*)klass->start_of_itable(); 1410 itableMethodEntry* ime = (itableMethodEntry*)(ioe + nof_interfaces); 1411 intptr_t* end = klass->end_of_itable(); 1412 assert((oop*)(ime + nof_methods) <= (oop*)klass->start_of_nonstatic_oop_maps(), "wrong offset calculation (1)"); 1413 assert((oop*)(end) == (oop*)(ime + nof_methods), "wrong offset calculation (2)"); 1414 1415 // Visit all interfaces and initialize itable offset table 1416 SetupItableClosure sic((address)klass(), ioe, ime); 1417 visit_all_interfaces(klass->transitive_interfaces(), &sic); 1418 1419 #ifdef ASSERT 1420 ime = sic.method_entry(); 1421 oop* v = (oop*) klass->end_of_itable(); 1422 assert( (oop*)(ime) == v, "wrong offset calculation (2)"); 1423 #endif 1424 } 1425 1426 1427 // inverse to itable_index 1428 Method* klassItable::method_for_itable_index(Klass* intf, int itable_index) { 1429 assert(InstanceKlass::cast(intf)->is_interface(), "sanity check"); 1430 assert(intf->verify_itable_index(itable_index), ""); 1431 Array<Method*>* methods = InstanceKlass::cast(intf)->methods(); 1432 1433 if (itable_index < 0 || itable_index >= method_count_for_interface(intf)) 1434 return NULL; // help caller defend against bad indices 1435 1436 int index = itable_index; 1437 Method* m = methods->at(index); 1438 int index2 = -1; 1439 while (!m->has_itable_index() || 1440 (index2 = m->itable_index()) != itable_index) { 1441 assert(index2 < itable_index, "monotonic"); 1442 if (++index == methods->length()) 1443 return NULL; 1444 m = methods->at(index); 1445 } 1446 assert(m->itable_index() == itable_index, "correct inverse"); 1447 1448 return m; 1449 } 1450 1451 void klassVtable::verify(outputStream* st, bool forced) { 1452 // make sure table is initialized 1453 if (!Universe::is_fully_initialized()) return; 1454 #ifndef PRODUCT 1455 // avoid redundant verifies 1456 if (!forced && _verify_count == Universe::verify_count()) return; 1457 _verify_count = Universe::verify_count(); 1458 #endif 1459 oop* end_of_obj = (oop*)_klass() + _klass()->size(); 1460 oop* end_of_vtable = (oop *)&table()[_length]; 1461 if (end_of_vtable > end_of_obj) { 1462 fatal("klass %s: klass object too short (vtable extends beyond end)", 1463 _klass->internal_name()); 1464 } 1465 1466 for (int i = 0; i < _length; i++) table()[i].verify(this, st); 1467 // verify consistency with superKlass vtable 1468 Klass* super = _klass->super(); 1469 if (super != NULL) { 1470 InstanceKlass* sk = InstanceKlass::cast(super); 1471 klassVtable* vt = sk->vtable(); 1472 for (int i = 0; i < vt->length(); i++) { 1473 verify_against(st, vt, i); 1474 } 1475 } 1476 } 1477 1478 void klassVtable::verify_against(outputStream* st, klassVtable* vt, int index) { 1479 vtableEntry* vte = &vt->table()[index]; 1480 if (vte->method()->name() != table()[index].method()->name() || 1481 vte->method()->signature() != table()[index].method()->signature()) { 1482 fatal("mismatched name/signature of vtable entries"); 1483 } 1484 } 1485 1486 #ifndef PRODUCT 1487 void klassVtable::print() { 1488 ResourceMark rm; 1489 tty->print("klassVtable for klass %s (length %d):\n", _klass->internal_name(), length()); 1490 for (int i = 0; i < length(); i++) { 1491 table()[i].print(); 1492 tty->cr(); 1493 } 1494 } 1495 #endif 1496 1497 void vtableEntry::verify(klassVtable* vt, outputStream* st) { 1498 NOT_PRODUCT(FlagSetting fs(IgnoreLockingAssertions, true)); 1499 assert(method() != NULL, "must have set method"); 1500 method()->verify(); 1501 // we sub_type, because it could be a miranda method 1502 if (!vt->klass()->is_subtype_of(method()->method_holder())) { 1503 #ifndef PRODUCT 1504 print(); 1505 #endif 1506 fatal("vtableEntry " PTR_FORMAT ": method is from subclass", p2i(this)); 1507 } 1508 } 1509 1510 #ifndef PRODUCT 1511 1512 void vtableEntry::print() { 1513 ResourceMark rm; 1514 tty->print("vtableEntry %s: ", method()->name()->as_C_string()); 1515 if (Verbose) { 1516 tty->print("m " INTPTR_FORMAT_W(#) " ", p2i(method())); 1517 } 1518 } 1519 1520 class VtableStats : AllStatic { 1521 public: 1522 static int no_klasses; // # classes with vtables 1523 static int no_array_klasses; // # array classes 1524 static int no_instance_klasses; // # instanceKlasses 1525 static int sum_of_vtable_len; // total # of vtable entries 1526 static int sum_of_array_vtable_len; // total # of vtable entries in array klasses only 1527 static int fixed; // total fixed overhead in bytes 1528 static int filler; // overhead caused by filler bytes 1529 static int entries; // total bytes consumed by vtable entries 1530 static int array_entries; // total bytes consumed by array vtable entries 1531 1532 static void do_class(Klass* k) { 1533 Klass* kl = k; 1534 klassVtable* vt = kl->vtable(); 1535 if (vt == NULL) return; 1536 no_klasses++; 1537 if (kl->oop_is_instance()) { 1538 no_instance_klasses++; 1539 kl->array_klasses_do(do_class); 1540 } 1541 if (kl->oop_is_array()) { 1542 no_array_klasses++; 1543 sum_of_array_vtable_len += vt->length(); 1544 } 1545 sum_of_vtable_len += vt->length(); 1546 } 1547 1548 static void compute() { 1549 SystemDictionary::classes_do(do_class); 1550 fixed = no_klasses * oopSize; // vtable length 1551 // filler size is a conservative approximation 1552 filler = oopSize * (no_klasses - no_instance_klasses) * (sizeof(InstanceKlass) - sizeof(ArrayKlass) - 1); 1553 entries = sizeof(vtableEntry) * sum_of_vtable_len; 1554 array_entries = sizeof(vtableEntry) * sum_of_array_vtable_len; 1555 } 1556 }; 1557 1558 int VtableStats::no_klasses = 0; 1559 int VtableStats::no_array_klasses = 0; 1560 int VtableStats::no_instance_klasses = 0; 1561 int VtableStats::sum_of_vtable_len = 0; 1562 int VtableStats::sum_of_array_vtable_len = 0; 1563 int VtableStats::fixed = 0; 1564 int VtableStats::filler = 0; 1565 int VtableStats::entries = 0; 1566 int VtableStats::array_entries = 0; 1567 1568 void klassVtable::print_statistics() { 1569 ResourceMark rm; 1570 HandleMark hm; 1571 VtableStats::compute(); 1572 tty->print_cr("vtable statistics:"); 1573 tty->print_cr("%6d classes (%d instance, %d array)", VtableStats::no_klasses, VtableStats::no_instance_klasses, VtableStats::no_array_klasses); 1574 int total = VtableStats::fixed + VtableStats::filler + VtableStats::entries; 1575 tty->print_cr("%6d bytes fixed overhead (refs + vtable object header)", VtableStats::fixed); 1576 tty->print_cr("%6d bytes filler overhead", VtableStats::filler); 1577 tty->print_cr("%6d bytes for vtable entries (%d for arrays)", VtableStats::entries, VtableStats::array_entries); 1578 tty->print_cr("%6d bytes total", total); 1579 } 1580 1581 int klassItable::_total_classes; // Total no. of classes with itables 1582 long klassItable::_total_size; // Total no. of bytes used for itables 1583 1584 void klassItable::print_statistics() { 1585 tty->print_cr("itable statistics:"); 1586 tty->print_cr("%6d classes with itables", _total_classes); 1587 tty->print_cr("%6ld K uses for itables (average by class: %ld bytes)", _total_size / K, _total_size / _total_classes); 1588 } 1589 1590 #endif // PRODUCT