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