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