1 /* 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/javaClasses.hpp" 27 #include "classfile/systemDictionary.hpp" 28 #include "classfile/verifier.hpp" 29 #include "classfile/vmSymbols.hpp" 30 #include "compiler/compileBroker.hpp" 31 #include "gc_implementation/shared/markSweep.inline.hpp" 32 #include "gc_interface/collectedHeap.inline.hpp" 33 #include "interpreter/oopMapCache.hpp" 34 #include "interpreter/rewriter.hpp" 35 #include "jvmtifiles/jvmti.h" 36 #include "memory/genOopClosures.inline.hpp" 37 #include "memory/heapInspection.hpp" 38 #include "memory/metadataFactory.hpp" 39 #include "memory/oopFactory.hpp" 40 #include "oops/fieldStreams.hpp" 41 #include "oops/instanceClassLoaderKlass.hpp" 42 #include "oops/instanceKlass.hpp" 43 #include "oops/instanceMirrorKlass.hpp" 44 #include "oops/instanceOop.hpp" 45 #include "oops/klass.inline.hpp" 46 #include "oops/method.hpp" 47 #include "oops/oop.inline.hpp" 48 #include "oops/symbol.hpp" 49 #include "prims/jvmtiExport.hpp" 50 #include "prims/jvmtiRedefineClassesTrace.hpp" 51 #include "prims/jvmtiRedefineClasses.hpp" 52 #include "prims/jvmtiThreadState.hpp" 53 #include "prims/methodComparator.hpp" 54 #include "runtime/fieldDescriptor.hpp" 55 #include "runtime/handles.inline.hpp" 56 #include "runtime/javaCalls.hpp" 57 #include "runtime/mutexLocker.hpp" 58 #include "runtime/thread.inline.hpp" 59 #include "services/classLoadingService.hpp" 60 #include "services/threadService.hpp" 61 #include "utilities/dtrace.hpp" 62 #include "utilities/macros.hpp" 63 #if INCLUDE_ALL_GCS 64 #include "gc_implementation/concurrentMarkSweep/cmsOopClosures.inline.hpp" 65 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" 66 #include "gc_implementation/g1/g1OopClosures.inline.hpp" 67 #include "gc_implementation/g1/g1RemSet.inline.hpp" 68 #include "gc_implementation/g1/heapRegionSeq.inline.hpp" 69 #include "gc_implementation/parNew/parOopClosures.inline.hpp" 70 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.inline.hpp" 71 #include "gc_implementation/parallelScavenge/psPromotionManager.inline.hpp" 72 #include "gc_implementation/parallelScavenge/psScavenge.inline.hpp" 73 #include "oops/oop.pcgc.inline.hpp" 74 #endif // INCLUDE_ALL_GCS 75 #ifdef COMPILER1 76 #include "c1/c1_Compiler.hpp" 77 #endif 78 79 #ifdef DTRACE_ENABLED 80 81 82 #define HOTSPOT_CLASS_INITIALIZATION_required HOTSPOT_CLASS_INITIALIZATION_REQUIRED 83 #define HOTSPOT_CLASS_INITIALIZATION_recursive HOTSPOT_CLASS_INITIALIZATION_RECURSIVE 84 #define HOTSPOT_CLASS_INITIALIZATION_concurrent HOTSPOT_CLASS_INITIALIZATION_CONCURRENT 85 #define HOTSPOT_CLASS_INITIALIZATION_erroneous HOTSPOT_CLASS_INITIALIZATION_ERRONEOUS 86 #define HOTSPOT_CLASS_INITIALIZATION_super__failed HOTSPOT_CLASS_INITIALIZATION_SUPER_FAILED 87 #define HOTSPOT_CLASS_INITIALIZATION_clinit HOTSPOT_CLASS_INITIALIZATION_CLINIT 88 #define HOTSPOT_CLASS_INITIALIZATION_error HOTSPOT_CLASS_INITIALIZATION_ERROR 89 #define HOTSPOT_CLASS_INITIALIZATION_end HOTSPOT_CLASS_INITIALIZATION_END 90 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type) \ 91 { \ 92 char* data = NULL; \ 93 int len = 0; \ 94 Symbol* name = (clss)->name(); \ 95 if (name != NULL) { \ 96 data = (char*)name->bytes(); \ 97 len = name->utf8_length(); \ 98 } \ 99 HOTSPOT_CLASS_INITIALIZATION_##type( \ 100 data, len, (clss)->class_loader(), thread_type); \ 101 } 102 103 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait) \ 104 { \ 105 char* data = NULL; \ 106 int len = 0; \ 107 Symbol* name = (clss)->name(); \ 108 if (name != NULL) { \ 109 data = (char*)name->bytes(); \ 110 len = name->utf8_length(); \ 111 } \ 112 HOTSPOT_CLASS_INITIALIZATION_##type( \ 113 data, len, (clss)->class_loader(), thread_type, wait); \ 114 } 115 116 #else // ndef DTRACE_ENABLED 117 118 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type) 119 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait) 120 121 #endif // ndef DTRACE_ENABLED 122 123 volatile int InstanceKlass::_total_instanceKlass_count = 0; 124 125 InstanceKlass* InstanceKlass::allocate_instance_klass( 126 ClassLoaderData* loader_data, 127 int vtable_len, 128 int itable_len, 129 int static_field_size, 130 int nonstatic_oop_map_size, 131 ReferenceType rt, 132 AccessFlags access_flags, 133 Symbol* name, 134 Klass* super_klass, 135 bool is_anonymous, 136 TRAPS) { 137 138 int size = InstanceKlass::size(vtable_len, itable_len, nonstatic_oop_map_size, 139 access_flags.is_interface(), is_anonymous); 140 141 // Allocation 142 InstanceKlass* ik; 143 if (rt == REF_NONE) { 144 if (name == vmSymbols::java_lang_Class()) { 145 ik = new (loader_data, size, THREAD) InstanceMirrorKlass( 146 vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt, 147 access_flags, is_anonymous); 148 } else if (name == vmSymbols::java_lang_ClassLoader() || 149 (SystemDictionary::ClassLoader_klass_loaded() && 150 super_klass != NULL && 151 super_klass->is_subtype_of(SystemDictionary::ClassLoader_klass()))) { 152 ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass( 153 vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt, 154 access_flags, is_anonymous); 155 } else { 156 // normal class 157 ik = new (loader_data, size, THREAD) InstanceKlass( 158 vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt, 159 access_flags, is_anonymous); 160 } 161 } else { 162 // reference klass 163 ik = new (loader_data, size, THREAD) InstanceRefKlass( 164 vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt, 165 access_flags, is_anonymous); 166 } 167 168 // Check for pending exception before adding to the loader data and incrementing 169 // class count. Can get OOM here. 170 if (HAS_PENDING_EXCEPTION) { 171 return NULL; 172 } 173 174 // Add all classes to our internal class loader list here, 175 // including classes in the bootstrap (NULL) class loader. 176 loader_data->add_class(ik); 177 178 Atomic::inc(&_total_instanceKlass_count); 179 return ik; 180 } 181 182 183 // copy method ordering from resource area to Metaspace 184 void InstanceKlass::copy_method_ordering(intArray* m, TRAPS) { 185 if (m != NULL) { 186 // allocate a new array and copy contents (memcpy?) 187 _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK); 188 for (int i = 0; i < m->length(); i++) { 189 _method_ordering->at_put(i, m->at(i)); 190 } 191 } else { 192 _method_ordering = Universe::the_empty_int_array(); 193 } 194 } 195 196 // create a new array of vtable_indices for default methods 197 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) { 198 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL); 199 assert(default_vtable_indices() == NULL, "only create once"); 200 set_default_vtable_indices(vtable_indices); 201 return vtable_indices; 202 } 203 204 InstanceKlass::InstanceKlass(int vtable_len, 205 int itable_len, 206 int static_field_size, 207 int nonstatic_oop_map_size, 208 ReferenceType rt, 209 AccessFlags access_flags, 210 bool is_anonymous) { 211 No_Safepoint_Verifier no_safepoint; // until k becomes parsable 212 213 int iksize = InstanceKlass::size(vtable_len, itable_len, nonstatic_oop_map_size, 214 access_flags.is_interface(), is_anonymous); 215 216 set_vtable_length(vtable_len); 217 set_itable_length(itable_len); 218 set_static_field_size(static_field_size); 219 set_nonstatic_oop_map_size(nonstatic_oop_map_size); 220 set_access_flags(access_flags); 221 _misc_flags = 0; // initialize to zero 222 set_is_anonymous(is_anonymous); 223 assert(size() == iksize, "wrong size for object"); 224 225 set_array_klasses(NULL); 226 set_methods(NULL); 227 set_method_ordering(NULL); 228 set_default_methods(NULL); 229 set_default_vtable_indices(NULL); 230 set_local_interfaces(NULL); 231 set_transitive_interfaces(NULL); 232 init_implementor(); 233 set_fields(NULL, 0); 234 set_constants(NULL); 235 set_class_loader_data(NULL); 236 set_source_file_name_index(0); 237 set_source_debug_extension(NULL, 0); 238 set_array_name(NULL); 239 set_inner_classes(NULL); 240 set_static_oop_field_count(0); 241 set_nonstatic_field_size(0); 242 set_is_marked_dependent(false); 243 set_init_state(InstanceKlass::allocated); 244 set_init_thread(NULL); 245 set_reference_type(rt); 246 set_oop_map_cache(NULL); 247 set_jni_ids(NULL); 248 set_osr_nmethods_head(NULL); 249 set_breakpoints(NULL); 250 init_previous_versions(); 251 set_generic_signature_index(0); 252 release_set_methods_jmethod_ids(NULL); 253 set_annotations(NULL); 254 set_jvmti_cached_class_field_map(NULL); 255 set_initial_method_idnum(0); 256 _dependencies = NULL; 257 set_jvmti_cached_class_field_map(NULL); 258 set_cached_class_file(NULL); 259 set_initial_method_idnum(0); 260 set_minor_version(0); 261 set_major_version(0); 262 NOT_PRODUCT(_verify_count = 0;) 263 264 // initialize the non-header words to zero 265 intptr_t* p = (intptr_t*)this; 266 for (int index = InstanceKlass::header_size(); index < iksize; index++) { 267 p[index] = NULL_WORD; 268 } 269 270 // Set temporary value until parseClassFile updates it with the real instance 271 // size. 272 set_layout_helper(Klass::instance_layout_helper(0, true)); 273 } 274 275 276 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data, 277 Array<Method*>* methods) { 278 if (methods != NULL && methods != Universe::the_empty_method_array() && 279 !methods->is_shared()) { 280 for (int i = 0; i < methods->length(); i++) { 281 Method* method = methods->at(i); 282 if (method == NULL) continue; // maybe null if error processing 283 // Only want to delete methods that are not executing for RedefineClasses. 284 // The previous version will point to them so they're not totally dangling 285 assert (!method->on_stack(), "shouldn't be called with methods on stack"); 286 MetadataFactory::free_metadata(loader_data, method); 287 } 288 MetadataFactory::free_array<Method*>(loader_data, methods); 289 } 290 } 291 292 void InstanceKlass::deallocate_interfaces(ClassLoaderData* loader_data, 293 Klass* super_klass, 294 Array<Klass*>* local_interfaces, 295 Array<Klass*>* transitive_interfaces) { 296 // Only deallocate transitive interfaces if not empty, same as super class 297 // or same as local interfaces. See code in parseClassFile. 298 Array<Klass*>* ti = transitive_interfaces; 299 if (ti != Universe::the_empty_klass_array() && ti != local_interfaces) { 300 // check that the interfaces don't come from super class 301 Array<Klass*>* sti = (super_klass == NULL) ? NULL : 302 InstanceKlass::cast(super_klass)->transitive_interfaces(); 303 if (ti != sti && ti != NULL && !ti->is_shared()) { 304 MetadataFactory::free_array<Klass*>(loader_data, ti); 305 } 306 } 307 308 // local interfaces can be empty 309 if (local_interfaces != Universe::the_empty_klass_array() && 310 local_interfaces != NULL && !local_interfaces->is_shared()) { 311 MetadataFactory::free_array<Klass*>(loader_data, local_interfaces); 312 } 313 } 314 315 // This function deallocates the metadata and C heap pointers that the 316 // InstanceKlass points to. 317 void InstanceKlass::deallocate_contents(ClassLoaderData* loader_data) { 318 319 // Orphan the mirror first, CMS thinks it's still live. 320 if (java_mirror() != NULL) { 321 java_lang_Class::set_klass(java_mirror(), NULL); 322 } 323 324 // Need to take this class off the class loader data list. 325 loader_data->remove_class(this); 326 327 // The array_klass for this class is created later, after error handling. 328 // For class redefinition, we keep the original class so this scratch class 329 // doesn't have an array class. Either way, assert that there is nothing 330 // to deallocate. 331 assert(array_klasses() == NULL, "array classes shouldn't be created for this class yet"); 332 333 // Release C heap allocated data that this might point to, which includes 334 // reference counting symbol names. 335 release_C_heap_structures(); 336 337 deallocate_methods(loader_data, methods()); 338 set_methods(NULL); 339 340 if (method_ordering() != NULL && 341 method_ordering() != Universe::the_empty_int_array() && 342 !method_ordering()->is_shared()) { 343 MetadataFactory::free_array<int>(loader_data, method_ordering()); 344 } 345 set_method_ordering(NULL); 346 347 // default methods can be empty 348 if (default_methods() != NULL && 349 default_methods() != Universe::the_empty_method_array() && 350 !default_methods()->is_shared()) { 351 MetadataFactory::free_array<Method*>(loader_data, default_methods()); 352 } 353 // Do NOT deallocate the default methods, they are owned by superinterfaces. 354 set_default_methods(NULL); 355 356 // default methods vtable indices can be empty 357 if (default_vtable_indices() != NULL && 358 !default_vtable_indices()->is_shared()) { 359 MetadataFactory::free_array<int>(loader_data, default_vtable_indices()); 360 } 361 set_default_vtable_indices(NULL); 362 363 364 // This array is in Klass, but remove it with the InstanceKlass since 365 // this place would be the only caller and it can share memory with transitive 366 // interfaces. 367 if (secondary_supers() != NULL && 368 secondary_supers() != Universe::the_empty_klass_array() && 369 secondary_supers() != transitive_interfaces() && 370 !secondary_supers()->is_shared()) { 371 MetadataFactory::free_array<Klass*>(loader_data, secondary_supers()); 372 } 373 set_secondary_supers(NULL); 374 375 deallocate_interfaces(loader_data, super(), local_interfaces(), transitive_interfaces()); 376 set_transitive_interfaces(NULL); 377 set_local_interfaces(NULL); 378 379 if (fields() != NULL && !fields()->is_shared()) { 380 MetadataFactory::free_array<jushort>(loader_data, fields()); 381 } 382 set_fields(NULL, 0); 383 384 // If a method from a redefined class is using this constant pool, don't 385 // delete it, yet. The new class's previous version will point to this. 386 if (constants() != NULL) { 387 assert (!constants()->on_stack(), "shouldn't be called if anything is onstack"); 388 if (!constants()->is_shared()) { 389 MetadataFactory::free_metadata(loader_data, constants()); 390 } 391 set_constants(NULL); 392 } 393 394 if (inner_classes() != NULL && 395 inner_classes() != Universe::the_empty_short_array() && 396 !inner_classes()->is_shared()) { 397 MetadataFactory::free_array<jushort>(loader_data, inner_classes()); 398 } 399 set_inner_classes(NULL); 400 401 // We should deallocate the Annotations instance if it's not in shared spaces. 402 if (annotations() != NULL && !annotations()->is_shared()) { 403 MetadataFactory::free_metadata(loader_data, annotations()); 404 } 405 set_annotations(NULL); 406 } 407 408 bool InstanceKlass::should_be_initialized() const { 409 return !is_initialized(); 410 } 411 412 klassVtable* InstanceKlass::vtable() const { 413 return new klassVtable(this, start_of_vtable(), vtable_length() / vtableEntry::size()); 414 } 415 416 klassItable* InstanceKlass::itable() const { 417 return new klassItable(instanceKlassHandle(this)); 418 } 419 420 void InstanceKlass::eager_initialize(Thread *thread) { 421 if (!EagerInitialization) return; 422 423 if (this->is_not_initialized()) { 424 // abort if the the class has a class initializer 425 if (this->class_initializer() != NULL) return; 426 427 // abort if it is java.lang.Object (initialization is handled in genesis) 428 Klass* super = this->super(); 429 if (super == NULL) return; 430 431 // abort if the super class should be initialized 432 if (!InstanceKlass::cast(super)->is_initialized()) return; 433 434 // call body to expose the this pointer 435 instanceKlassHandle this_k(thread, this); 436 eager_initialize_impl(this_k); 437 } 438 } 439 440 // JVMTI spec thinks there are signers and protection domain in the 441 // instanceKlass. These accessors pretend these fields are there. 442 // The hprof specification also thinks these fields are in InstanceKlass. 443 oop InstanceKlass::protection_domain() const { 444 // return the protection_domain from the mirror 445 return java_lang_Class::protection_domain(java_mirror()); 446 } 447 448 // To remove these from requires an incompatible change and CCC request. 449 objArrayOop InstanceKlass::signers() const { 450 // return the signers from the mirror 451 return java_lang_Class::signers(java_mirror()); 452 } 453 454 oop InstanceKlass::init_lock() const { 455 // return the init lock from the mirror 456 oop lock = java_lang_Class::init_lock(java_mirror()); 457 assert((oop)lock != NULL || !is_not_initialized(), // initialized or in_error state 458 "only fully initialized state can have a null lock"); 459 return lock; 460 } 461 462 // Set the initialization lock to null so the object can be GC'ed. Any racing 463 // threads to get this lock will see a null lock and will not lock. 464 // That's okay because they all check for initialized state after getting 465 // the lock and return. 466 void InstanceKlass::fence_and_clear_init_lock() { 467 // make sure previous stores are all done, notably the init_state. 468 OrderAccess::storestore(); 469 java_lang_Class::set_init_lock(java_mirror(), NULL); 470 assert(!is_not_initialized(), "class must be initialized now"); 471 } 472 473 void InstanceKlass::eager_initialize_impl(instanceKlassHandle this_k) { 474 EXCEPTION_MARK; 475 oop init_lock = this_k->init_lock(); 476 ObjectLocker ol(init_lock, THREAD, init_lock != NULL); 477 478 // abort if someone beat us to the initialization 479 if (!this_k->is_not_initialized()) return; // note: not equivalent to is_initialized() 480 481 ClassState old_state = this_k->init_state(); 482 link_class_impl(this_k, true, THREAD); 483 if (HAS_PENDING_EXCEPTION) { 484 CLEAR_PENDING_EXCEPTION; 485 // Abort if linking the class throws an exception. 486 487 // Use a test to avoid redundantly resetting the state if there's 488 // no change. Set_init_state() asserts that state changes make 489 // progress, whereas here we might just be spinning in place. 490 if( old_state != this_k->_init_state ) 491 this_k->set_init_state (old_state); 492 } else { 493 // linking successfull, mark class as initialized 494 this_k->set_init_state (fully_initialized); 495 this_k->fence_and_clear_init_lock(); 496 // trace 497 if (TraceClassInitialization) { 498 ResourceMark rm(THREAD); 499 tty->print_cr("[Initialized %s without side effects]", this_k->external_name()); 500 } 501 } 502 } 503 504 505 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization 506 // process. The step comments refers to the procedure described in that section. 507 // Note: implementation moved to static method to expose the this pointer. 508 void InstanceKlass::initialize(TRAPS) { 509 if (this->should_be_initialized()) { 510 HandleMark hm(THREAD); 511 instanceKlassHandle this_k(THREAD, this); 512 initialize_impl(this_k, CHECK); 513 // Note: at this point the class may be initialized 514 // OR it may be in the state of being initialized 515 // in case of recursive initialization! 516 } else { 517 assert(is_initialized(), "sanity check"); 518 } 519 } 520 521 522 bool InstanceKlass::verify_code( 523 instanceKlassHandle this_k, bool throw_verifyerror, TRAPS) { 524 // 1) Verify the bytecodes 525 Verifier::Mode mode = 526 throw_verifyerror ? Verifier::ThrowException : Verifier::NoException; 527 return Verifier::verify(this_k, mode, this_k->should_verify_class(), CHECK_false); 528 } 529 530 531 // Used exclusively by the shared spaces dump mechanism to prevent 532 // classes mapped into the shared regions in new VMs from appearing linked. 533 534 void InstanceKlass::unlink_class() { 535 assert(is_linked(), "must be linked"); 536 _init_state = loaded; 537 } 538 539 void InstanceKlass::link_class(TRAPS) { 540 assert(is_loaded(), "must be loaded"); 541 if (!is_linked()) { 542 HandleMark hm(THREAD); 543 instanceKlassHandle this_k(THREAD, this); 544 link_class_impl(this_k, true, CHECK); 545 } 546 } 547 548 // Called to verify that a class can link during initialization, without 549 // throwing a VerifyError. 550 bool InstanceKlass::link_class_or_fail(TRAPS) { 551 assert(is_loaded(), "must be loaded"); 552 if (!is_linked()) { 553 HandleMark hm(THREAD); 554 instanceKlassHandle this_k(THREAD, this); 555 link_class_impl(this_k, false, CHECK_false); 556 } 557 return is_linked(); 558 } 559 560 bool InstanceKlass::link_class_impl( 561 instanceKlassHandle this_k, bool throw_verifyerror, TRAPS) { 562 // check for error state 563 if (this_k->is_in_error_state()) { 564 ResourceMark rm(THREAD); 565 THROW_MSG_(vmSymbols::java_lang_NoClassDefFoundError(), 566 this_k->external_name(), false); 567 } 568 // return if already verified 569 if (this_k->is_linked()) { 570 return true; 571 } 572 573 // Timing 574 // timer handles recursion 575 assert(THREAD->is_Java_thread(), "non-JavaThread in link_class_impl"); 576 JavaThread* jt = (JavaThread*)THREAD; 577 578 // link super class before linking this class 579 instanceKlassHandle super(THREAD, this_k->super()); 580 if (super.not_null()) { 581 if (super->is_interface()) { // check if super class is an interface 582 ResourceMark rm(THREAD); 583 Exceptions::fthrow( 584 THREAD_AND_LOCATION, 585 vmSymbols::java_lang_IncompatibleClassChangeError(), 586 "class %s has interface %s as super class", 587 this_k->external_name(), 588 super->external_name() 589 ); 590 return false; 591 } 592 593 link_class_impl(super, throw_verifyerror, CHECK_false); 594 } 595 596 // link all interfaces implemented by this class before linking this class 597 Array<Klass*>* interfaces = this_k->local_interfaces(); 598 int num_interfaces = interfaces->length(); 599 for (int index = 0; index < num_interfaces; index++) { 600 HandleMark hm(THREAD); 601 instanceKlassHandle ih(THREAD, interfaces->at(index)); 602 link_class_impl(ih, throw_verifyerror, CHECK_false); 603 } 604 605 // in case the class is linked in the process of linking its superclasses 606 if (this_k->is_linked()) { 607 return true; 608 } 609 610 // trace only the link time for this klass that includes 611 // the verification time 612 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(), 613 ClassLoader::perf_class_link_selftime(), 614 ClassLoader::perf_classes_linked(), 615 jt->get_thread_stat()->perf_recursion_counts_addr(), 616 jt->get_thread_stat()->perf_timers_addr(), 617 PerfClassTraceTime::CLASS_LINK); 618 619 // verification & rewriting 620 { 621 oop init_lock = this_k->init_lock(); 622 ObjectLocker ol(init_lock, THREAD, init_lock != NULL); 623 // rewritten will have been set if loader constraint error found 624 // on an earlier link attempt 625 // don't verify or rewrite if already rewritten 626 627 if (!this_k->is_linked()) { 628 if (!this_k->is_rewritten()) { 629 { 630 // Timer includes any side effects of class verification (resolution, 631 // etc), but not recursive entry into verify_code(). 632 PerfClassTraceTime timer(ClassLoader::perf_class_verify_time(), 633 ClassLoader::perf_class_verify_selftime(), 634 ClassLoader::perf_classes_verified(), 635 jt->get_thread_stat()->perf_recursion_counts_addr(), 636 jt->get_thread_stat()->perf_timers_addr(), 637 PerfClassTraceTime::CLASS_VERIFY); 638 bool verify_ok = verify_code(this_k, throw_verifyerror, THREAD); 639 if (!verify_ok) { 640 return false; 641 } 642 } 643 644 // Just in case a side-effect of verify linked this class already 645 // (which can sometimes happen since the verifier loads classes 646 // using custom class loaders, which are free to initialize things) 647 if (this_k->is_linked()) { 648 return true; 649 } 650 651 // also sets rewritten 652 this_k->rewrite_class(CHECK_false); 653 } 654 655 // relocate jsrs and link methods after they are all rewritten 656 this_k->link_methods(CHECK_false); 657 658 // Initialize the vtable and interface table after 659 // methods have been rewritten since rewrite may 660 // fabricate new Method*s. 661 // also does loader constraint checking 662 if (!this_k()->is_shared()) { 663 ResourceMark rm(THREAD); 664 this_k->vtable()->initialize_vtable(true, CHECK_false); 665 this_k->itable()->initialize_itable(true, CHECK_false); 666 } 667 #ifdef ASSERT 668 else { 669 ResourceMark rm(THREAD); 670 this_k->vtable()->verify(tty, true); 671 // In case itable verification is ever added. 672 // this_k->itable()->verify(tty, true); 673 } 674 #endif 675 this_k->set_init_state(linked); 676 if (JvmtiExport::should_post_class_prepare()) { 677 Thread *thread = THREAD; 678 assert(thread->is_Java_thread(), "thread->is_Java_thread()"); 679 JvmtiExport::post_class_prepare((JavaThread *) thread, this_k()); 680 } 681 } 682 } 683 return true; 684 } 685 686 687 // Rewrite the byte codes of all of the methods of a class. 688 // The rewriter must be called exactly once. Rewriting must happen after 689 // verification but before the first method of the class is executed. 690 void InstanceKlass::rewrite_class(TRAPS) { 691 assert(is_loaded(), "must be loaded"); 692 instanceKlassHandle this_k(THREAD, this); 693 if (this_k->is_rewritten()) { 694 assert(this_k()->is_shared(), "rewriting an unshared class?"); 695 return; 696 } 697 Rewriter::rewrite(this_k, CHECK); 698 this_k->set_rewritten(); 699 } 700 701 // Now relocate and link method entry points after class is rewritten. 702 // This is outside is_rewritten flag. In case of an exception, it can be 703 // executed more than once. 704 void InstanceKlass::link_methods(TRAPS) { 705 int len = methods()->length(); 706 for (int i = len-1; i >= 0; i--) { 707 methodHandle m(THREAD, methods()->at(i)); 708 709 // Set up method entry points for compiler and interpreter . 710 m->link_method(m, CHECK); 711 712 // This is for JVMTI and unrelated to relocator but the last thing we do 713 #ifdef ASSERT 714 if (StressMethodComparator) { 715 ResourceMark rm(THREAD); 716 static int nmc = 0; 717 for (int j = i; j >= 0 && j >= i-4; j--) { 718 if ((++nmc % 1000) == 0) tty->print_cr("Have run MethodComparator %d times...", nmc); 719 bool z = MethodComparator::methods_EMCP(m(), 720 methods()->at(j)); 721 if (j == i && !z) { 722 tty->print("MethodComparator FAIL: "); m->print(); m->print_codes(); 723 assert(z, "method must compare equal to itself"); 724 } 725 } 726 } 727 #endif //ASSERT 728 } 729 } 730 731 732 void InstanceKlass::initialize_impl(instanceKlassHandle this_k, TRAPS) { 733 // Make sure klass is linked (verified) before initialization 734 // A class could already be verified, since it has been reflected upon. 735 this_k->link_class(CHECK); 736 737 DTRACE_CLASSINIT_PROBE(required, InstanceKlass::cast(this_k()), -1); 738 739 bool wait = false; 740 741 // refer to the JVM book page 47 for description of steps 742 // Step 1 743 { 744 oop init_lock = this_k->init_lock(); 745 ObjectLocker ol(init_lock, THREAD, init_lock != NULL); 746 747 Thread *self = THREAD; // it's passed the current thread 748 749 // Step 2 750 // If we were to use wait() instead of waitInterruptibly() then 751 // we might end up throwing IE from link/symbol resolution sites 752 // that aren't expected to throw. This would wreak havoc. See 6320309. 753 while(this_k->is_being_initialized() && !this_k->is_reentrant_initialization(self)) { 754 wait = true; 755 ol.waitUninterruptibly(CHECK); 756 } 757 758 // Step 3 759 if (this_k->is_being_initialized() && this_k->is_reentrant_initialization(self)) { 760 DTRACE_CLASSINIT_PROBE_WAIT(recursive, InstanceKlass::cast(this_k()), -1,wait); 761 return; 762 } 763 764 // Step 4 765 if (this_k->is_initialized()) { 766 DTRACE_CLASSINIT_PROBE_WAIT(concurrent, InstanceKlass::cast(this_k()), -1,wait); 767 return; 768 } 769 770 // Step 5 771 if (this_k->is_in_error_state()) { 772 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, InstanceKlass::cast(this_k()), -1,wait); 773 ResourceMark rm(THREAD); 774 const char* desc = "Could not initialize class "; 775 const char* className = this_k->external_name(); 776 size_t msglen = strlen(desc) + strlen(className) + 1; 777 char* message = NEW_RESOURCE_ARRAY(char, msglen); 778 if (NULL == message) { 779 // Out of memory: can't create detailed error message 780 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), className); 781 } else { 782 jio_snprintf(message, msglen, "%s%s", desc, className); 783 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), message); 784 } 785 } 786 787 // Step 6 788 this_k->set_init_state(being_initialized); 789 this_k->set_init_thread(self); 790 } 791 792 // Step 7 793 Klass* super_klass = this_k->super(); 794 if (super_klass != NULL && !this_k->is_interface() && super_klass->should_be_initialized()) { 795 super_klass->initialize(THREAD); 796 797 if (HAS_PENDING_EXCEPTION) { 798 Handle e(THREAD, PENDING_EXCEPTION); 799 CLEAR_PENDING_EXCEPTION; 800 { 801 EXCEPTION_MARK; 802 this_k->set_initialization_state_and_notify(initialization_error, THREAD); // Locks object, set state, and notify all waiting threads 803 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, superclass initialization error is thrown below 804 } 805 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, InstanceKlass::cast(this_k()), -1,wait); 806 THROW_OOP(e()); 807 } 808 } 809 810 if (this_k->has_default_methods()) { 811 // Step 7.5: initialize any interfaces which have default methods 812 for (int i = 0; i < this_k->local_interfaces()->length(); ++i) { 813 Klass* iface = this_k->local_interfaces()->at(i); 814 InstanceKlass* ik = InstanceKlass::cast(iface); 815 if (ik->has_default_methods() && ik->should_be_initialized()) { 816 ik->initialize(THREAD); 817 818 if (HAS_PENDING_EXCEPTION) { 819 Handle e(THREAD, PENDING_EXCEPTION); 820 CLEAR_PENDING_EXCEPTION; 821 { 822 EXCEPTION_MARK; 823 // Locks object, set state, and notify all waiting threads 824 this_k->set_initialization_state_and_notify( 825 initialization_error, THREAD); 826 827 // ignore any exception thrown, superclass initialization error is 828 // thrown below 829 CLEAR_PENDING_EXCEPTION; 830 } 831 DTRACE_CLASSINIT_PROBE_WAIT( 832 super__failed, InstanceKlass::cast(this_k()), -1, wait); 833 THROW_OOP(e()); 834 } 835 } 836 } 837 } 838 839 // Step 8 840 { 841 assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl"); 842 JavaThread* jt = (JavaThread*)THREAD; 843 DTRACE_CLASSINIT_PROBE_WAIT(clinit, InstanceKlass::cast(this_k()), -1,wait); 844 // Timer includes any side effects of class initialization (resolution, 845 // etc), but not recursive entry into call_class_initializer(). 846 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(), 847 ClassLoader::perf_class_init_selftime(), 848 ClassLoader::perf_classes_inited(), 849 jt->get_thread_stat()->perf_recursion_counts_addr(), 850 jt->get_thread_stat()->perf_timers_addr(), 851 PerfClassTraceTime::CLASS_CLINIT); 852 this_k->call_class_initializer(THREAD); 853 } 854 855 // Step 9 856 if (!HAS_PENDING_EXCEPTION) { 857 this_k->set_initialization_state_and_notify(fully_initialized, CHECK); 858 { ResourceMark rm(THREAD); 859 debug_only(this_k->vtable()->verify(tty, true);) 860 } 861 } 862 else { 863 // Step 10 and 11 864 Handle e(THREAD, PENDING_EXCEPTION); 865 CLEAR_PENDING_EXCEPTION; 866 // JVMTI has already reported the pending exception 867 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError 868 JvmtiExport::clear_detected_exception((JavaThread*)THREAD); 869 { 870 EXCEPTION_MARK; 871 this_k->set_initialization_state_and_notify(initialization_error, THREAD); 872 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below 873 // JVMTI has already reported the pending exception 874 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError 875 JvmtiExport::clear_detected_exception((JavaThread*)THREAD); 876 } 877 DTRACE_CLASSINIT_PROBE_WAIT(error, InstanceKlass::cast(this_k()), -1,wait); 878 if (e->is_a(SystemDictionary::Error_klass())) { 879 THROW_OOP(e()); 880 } else { 881 JavaCallArguments args(e); 882 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(), 883 vmSymbols::throwable_void_signature(), 884 &args); 885 } 886 } 887 DTRACE_CLASSINIT_PROBE_WAIT(end, InstanceKlass::cast(this_k()), -1,wait); 888 } 889 890 891 // Note: implementation moved to static method to expose the this pointer. 892 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) { 893 instanceKlassHandle kh(THREAD, this); 894 set_initialization_state_and_notify_impl(kh, state, CHECK); 895 } 896 897 void InstanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_k, ClassState state, TRAPS) { 898 oop init_lock = this_k->init_lock(); 899 ObjectLocker ol(init_lock, THREAD, init_lock != NULL); 900 this_k->set_init_state(state); 901 this_k->fence_and_clear_init_lock(); 902 ol.notify_all(CHECK); 903 } 904 905 // The embedded _implementor field can only record one implementor. 906 // When there are more than one implementors, the _implementor field 907 // is set to the interface Klass* itself. Following are the possible 908 // values for the _implementor field: 909 // NULL - no implementor 910 // implementor Klass* - one implementor 911 // self - more than one implementor 912 // 913 // The _implementor field only exists for interfaces. 914 void InstanceKlass::add_implementor(Klass* k) { 915 assert(Compile_lock->owned_by_self(), ""); 916 assert(is_interface(), "not interface"); 917 // Filter out my subinterfaces. 918 // (Note: Interfaces are never on the subklass list.) 919 if (InstanceKlass::cast(k)->is_interface()) return; 920 921 // Filter out subclasses whose supers already implement me. 922 // (Note: CHA must walk subclasses of direct implementors 923 // in order to locate indirect implementors.) 924 Klass* sk = InstanceKlass::cast(k)->super(); 925 if (sk != NULL && InstanceKlass::cast(sk)->implements_interface(this)) 926 // We only need to check one immediate superclass, since the 927 // implements_interface query looks at transitive_interfaces. 928 // Any supers of the super have the same (or fewer) transitive_interfaces. 929 return; 930 931 Klass* ik = implementor(); 932 if (ik == NULL) { 933 set_implementor(k); 934 } else if (ik != this) { 935 // There is already an implementor. Use itself as an indicator of 936 // more than one implementors. 937 set_implementor(this); 938 } 939 940 // The implementor also implements the transitive_interfaces 941 for (int index = 0; index < local_interfaces()->length(); index++) { 942 InstanceKlass::cast(local_interfaces()->at(index))->add_implementor(k); 943 } 944 } 945 946 void InstanceKlass::init_implementor() { 947 if (is_interface()) { 948 set_implementor(NULL); 949 } 950 } 951 952 953 void InstanceKlass::process_interfaces(Thread *thread) { 954 // link this class into the implementors list of every interface it implements 955 for (int i = local_interfaces()->length() - 1; i >= 0; i--) { 956 assert(local_interfaces()->at(i)->is_klass(), "must be a klass"); 957 InstanceKlass* interf = InstanceKlass::cast(local_interfaces()->at(i)); 958 assert(interf->is_interface(), "expected interface"); 959 interf->add_implementor(this); 960 } 961 } 962 963 bool InstanceKlass::can_be_primary_super_slow() const { 964 if (is_interface()) 965 return false; 966 else 967 return Klass::can_be_primary_super_slow(); 968 } 969 970 GrowableArray<Klass*>* InstanceKlass::compute_secondary_supers(int num_extra_slots) { 971 // The secondaries are the implemented interfaces. 972 InstanceKlass* ik = InstanceKlass::cast(this); 973 Array<Klass*>* interfaces = ik->transitive_interfaces(); 974 int num_secondaries = num_extra_slots + interfaces->length(); 975 if (num_secondaries == 0) { 976 // Must share this for correct bootstrapping! 977 set_secondary_supers(Universe::the_empty_klass_array()); 978 return NULL; 979 } else if (num_extra_slots == 0) { 980 // The secondary super list is exactly the same as the transitive interfaces. 981 // Redefine classes has to be careful not to delete this! 982 set_secondary_supers(interfaces); 983 return NULL; 984 } else { 985 // Copy transitive interfaces to a temporary growable array to be constructed 986 // into the secondary super list with extra slots. 987 GrowableArray<Klass*>* secondaries = new GrowableArray<Klass*>(interfaces->length()); 988 for (int i = 0; i < interfaces->length(); i++) { 989 secondaries->push(interfaces->at(i)); 990 } 991 return secondaries; 992 } 993 } 994 995 bool InstanceKlass::compute_is_subtype_of(Klass* k) { 996 if (k->is_interface()) { 997 return implements_interface(k); 998 } else { 999 return Klass::compute_is_subtype_of(k); 1000 } 1001 } 1002 1003 bool InstanceKlass::implements_interface(Klass* k) const { 1004 if (this == k) return true; 1005 assert(k->is_interface(), "should be an interface class"); 1006 for (int i = 0; i < transitive_interfaces()->length(); i++) { 1007 if (transitive_interfaces()->at(i) == k) { 1008 return true; 1009 } 1010 } 1011 return false; 1012 } 1013 1014 bool InstanceKlass::is_same_or_direct_interface(Klass *k) const { 1015 // Verify direct super interface 1016 if (this == k) return true; 1017 assert(k->is_interface(), "should be an interface class"); 1018 for (int i = 0; i < local_interfaces()->length(); i++) { 1019 if (local_interfaces()->at(i) == k) { 1020 return true; 1021 } 1022 } 1023 return false; 1024 } 1025 1026 objArrayOop InstanceKlass::allocate_objArray(int n, int length, TRAPS) { 1027 if (length < 0) THROW_0(vmSymbols::java_lang_NegativeArraySizeException()); 1028 if (length > arrayOopDesc::max_array_length(T_OBJECT)) { 1029 report_java_out_of_memory("Requested array size exceeds VM limit"); 1030 JvmtiExport::post_array_size_exhausted(); 1031 THROW_OOP_0(Universe::out_of_memory_error_array_size()); 1032 } 1033 int size = objArrayOopDesc::object_size(length); 1034 Klass* ak = array_klass(n, CHECK_NULL); 1035 KlassHandle h_ak (THREAD, ak); 1036 objArrayOop o = 1037 (objArrayOop)CollectedHeap::array_allocate(h_ak, size, length, CHECK_NULL); 1038 return o; 1039 } 1040 1041 instanceOop InstanceKlass::register_finalizer(instanceOop i, TRAPS) { 1042 if (TraceFinalizerRegistration) { 1043 tty->print("Registered "); 1044 i->print_value_on(tty); 1045 tty->print_cr(" (" INTPTR_FORMAT ") as finalizable", (address)i); 1046 } 1047 instanceHandle h_i(THREAD, i); 1048 // Pass the handle as argument, JavaCalls::call expects oop as jobjects 1049 JavaValue result(T_VOID); 1050 JavaCallArguments args(h_i); 1051 methodHandle mh (THREAD, Universe::finalizer_register_method()); 1052 JavaCalls::call(&result, mh, &args, CHECK_NULL); 1053 return h_i(); 1054 } 1055 1056 instanceOop InstanceKlass::allocate_instance(TRAPS) { 1057 bool has_finalizer_flag = has_finalizer(); // Query before possible GC 1058 int size = size_helper(); // Query before forming handle. 1059 1060 KlassHandle h_k(THREAD, this); 1061 1062 instanceOop i; 1063 1064 i = (instanceOop)CollectedHeap::obj_allocate(h_k, size, CHECK_NULL); 1065 if (has_finalizer_flag && !RegisterFinalizersAtInit) { 1066 i = register_finalizer(i, CHECK_NULL); 1067 } 1068 return i; 1069 } 1070 1071 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) { 1072 if (is_interface() || is_abstract()) { 1073 ResourceMark rm(THREAD); 1074 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError() 1075 : vmSymbols::java_lang_InstantiationException(), external_name()); 1076 } 1077 if (this == SystemDictionary::Class_klass()) { 1078 ResourceMark rm(THREAD); 1079 THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError() 1080 : vmSymbols::java_lang_IllegalAccessException(), external_name()); 1081 } 1082 } 1083 1084 Klass* InstanceKlass::array_klass_impl(bool or_null, int n, TRAPS) { 1085 instanceKlassHandle this_k(THREAD, this); 1086 return array_klass_impl(this_k, or_null, n, THREAD); 1087 } 1088 1089 Klass* InstanceKlass::array_klass_impl(instanceKlassHandle this_k, bool or_null, int n, TRAPS) { 1090 if (this_k->array_klasses() == NULL) { 1091 if (or_null) return NULL; 1092 1093 ResourceMark rm; 1094 JavaThread *jt = (JavaThread *)THREAD; 1095 { 1096 // Atomic creation of array_klasses 1097 MutexLocker mc(Compile_lock, THREAD); // for vtables 1098 MutexLocker ma(MultiArray_lock, THREAD); 1099 1100 // Check if update has already taken place 1101 if (this_k->array_klasses() == NULL) { 1102 Klass* k = ObjArrayKlass::allocate_objArray_klass(this_k->class_loader_data(), 1, this_k, CHECK_NULL); 1103 this_k->set_array_klasses(k); 1104 } 1105 } 1106 } 1107 // _this will always be set at this point 1108 ObjArrayKlass* oak = (ObjArrayKlass*)this_k->array_klasses(); 1109 if (or_null) { 1110 return oak->array_klass_or_null(n); 1111 } 1112 return oak->array_klass(n, CHECK_NULL); 1113 } 1114 1115 Klass* InstanceKlass::array_klass_impl(bool or_null, TRAPS) { 1116 return array_klass_impl(or_null, 1, THREAD); 1117 } 1118 1119 void InstanceKlass::call_class_initializer(TRAPS) { 1120 instanceKlassHandle ik (THREAD, this); 1121 call_class_initializer_impl(ik, THREAD); 1122 } 1123 1124 static int call_class_initializer_impl_counter = 0; // for debugging 1125 1126 Method* InstanceKlass::class_initializer() { 1127 Method* clinit = find_method( 1128 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature()); 1129 if (clinit != NULL && clinit->has_valid_initializer_flags()) { 1130 return clinit; 1131 } 1132 return NULL; 1133 } 1134 1135 void InstanceKlass::call_class_initializer_impl(instanceKlassHandle this_k, TRAPS) { 1136 if (ReplayCompiles && 1137 (ReplaySuppressInitializers == 1 || 1138 ReplaySuppressInitializers >= 2 && this_k->class_loader() != NULL)) { 1139 // Hide the existence of the initializer for the purpose of replaying the compile 1140 return; 1141 } 1142 1143 methodHandle h_method(THREAD, this_k->class_initializer()); 1144 assert(!this_k->is_initialized(), "we cannot initialize twice"); 1145 if (TraceClassInitialization) { 1146 tty->print("%d Initializing ", call_class_initializer_impl_counter++); 1147 this_k->name()->print_value(); 1148 tty->print_cr("%s (" INTPTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", (address)this_k()); 1149 } 1150 if (h_method() != NULL) { 1151 JavaCallArguments args; // No arguments 1152 JavaValue result(T_VOID); 1153 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args) 1154 } 1155 } 1156 1157 1158 void InstanceKlass::mask_for(methodHandle method, int bci, 1159 InterpreterOopMap* entry_for) { 1160 // Dirty read, then double-check under a lock. 1161 if (_oop_map_cache == NULL) { 1162 // Otherwise, allocate a new one. 1163 MutexLocker x(OopMapCacheAlloc_lock); 1164 // First time use. Allocate a cache in C heap 1165 if (_oop_map_cache == NULL) { 1166 // Release stores from OopMapCache constructor before assignment 1167 // to _oop_map_cache. C++ compilers on ppc do not emit the 1168 // required memory barrier only because of the volatile 1169 // qualifier of _oop_map_cache. 1170 OrderAccess::release_store_ptr(&_oop_map_cache, new OopMapCache()); 1171 } 1172 } 1173 // _oop_map_cache is constant after init; lookup below does is own locking. 1174 _oop_map_cache->lookup(method, bci, entry_for); 1175 } 1176 1177 1178 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const { 1179 for (JavaFieldStream fs(this); !fs.done(); fs.next()) { 1180 Symbol* f_name = fs.name(); 1181 Symbol* f_sig = fs.signature(); 1182 if (f_name == name && f_sig == sig) { 1183 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index()); 1184 return true; 1185 } 1186 } 1187 return false; 1188 } 1189 1190 1191 Klass* InstanceKlass::find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const { 1192 const int n = local_interfaces()->length(); 1193 for (int i = 0; i < n; i++) { 1194 Klass* intf1 = local_interfaces()->at(i); 1195 assert(intf1->is_interface(), "just checking type"); 1196 // search for field in current interface 1197 if (InstanceKlass::cast(intf1)->find_local_field(name, sig, fd)) { 1198 assert(fd->is_static(), "interface field must be static"); 1199 return intf1; 1200 } 1201 // search for field in direct superinterfaces 1202 Klass* intf2 = InstanceKlass::cast(intf1)->find_interface_field(name, sig, fd); 1203 if (intf2 != NULL) return intf2; 1204 } 1205 // otherwise field lookup fails 1206 return NULL; 1207 } 1208 1209 1210 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const { 1211 // search order according to newest JVM spec (5.4.3.2, p.167). 1212 // 1) search for field in current klass 1213 if (find_local_field(name, sig, fd)) { 1214 return const_cast<InstanceKlass*>(this); 1215 } 1216 // 2) search for field recursively in direct superinterfaces 1217 { Klass* intf = find_interface_field(name, sig, fd); 1218 if (intf != NULL) return intf; 1219 } 1220 // 3) apply field lookup recursively if superclass exists 1221 { Klass* supr = super(); 1222 if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, fd); 1223 } 1224 // 4) otherwise field lookup fails 1225 return NULL; 1226 } 1227 1228 1229 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const { 1230 // search order according to newest JVM spec (5.4.3.2, p.167). 1231 // 1) search for field in current klass 1232 if (find_local_field(name, sig, fd)) { 1233 if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this); 1234 } 1235 // 2) search for field recursively in direct superinterfaces 1236 if (is_static) { 1237 Klass* intf = find_interface_field(name, sig, fd); 1238 if (intf != NULL) return intf; 1239 } 1240 // 3) apply field lookup recursively if superclass exists 1241 { Klass* supr = super(); 1242 if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd); 1243 } 1244 // 4) otherwise field lookup fails 1245 return NULL; 1246 } 1247 1248 1249 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const { 1250 for (JavaFieldStream fs(this); !fs.done(); fs.next()) { 1251 if (fs.offset() == offset) { 1252 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index()); 1253 if (fd->is_static() == is_static) return true; 1254 } 1255 } 1256 return false; 1257 } 1258 1259 1260 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const { 1261 Klass* klass = const_cast<InstanceKlass*>(this); 1262 while (klass != NULL) { 1263 if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) { 1264 return true; 1265 } 1266 klass = klass->super(); 1267 } 1268 return false; 1269 } 1270 1271 1272 void InstanceKlass::methods_do(void f(Method* method)) { 1273 int len = methods()->length(); 1274 for (int index = 0; index < len; index++) { 1275 Method* m = methods()->at(index); 1276 assert(m->is_method(), "must be method"); 1277 f(m); 1278 } 1279 } 1280 1281 1282 void InstanceKlass::do_local_static_fields(FieldClosure* cl) { 1283 for (JavaFieldStream fs(this); !fs.done(); fs.next()) { 1284 if (fs.access_flags().is_static()) { 1285 fieldDescriptor& fd = fs.field_descriptor(); 1286 cl->do_field(&fd); 1287 } 1288 } 1289 } 1290 1291 1292 void InstanceKlass::do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle mirror, TRAPS) { 1293 instanceKlassHandle h_this(THREAD, this); 1294 do_local_static_fields_impl(h_this, f, mirror, CHECK); 1295 } 1296 1297 1298 void InstanceKlass::do_local_static_fields_impl(instanceKlassHandle this_k, 1299 void f(fieldDescriptor* fd, Handle, TRAPS), Handle mirror, TRAPS) { 1300 for (JavaFieldStream fs(this_k()); !fs.done(); fs.next()) { 1301 if (fs.access_flags().is_static()) { 1302 fieldDescriptor& fd = fs.field_descriptor(); 1303 f(&fd, mirror, CHECK); 1304 } 1305 } 1306 } 1307 1308 1309 static int compare_fields_by_offset(int* a, int* b) { 1310 return a[0] - b[0]; 1311 } 1312 1313 void InstanceKlass::do_nonstatic_fields(FieldClosure* cl) { 1314 InstanceKlass* super = superklass(); 1315 if (super != NULL) { 1316 super->do_nonstatic_fields(cl); 1317 } 1318 fieldDescriptor fd; 1319 int length = java_fields_count(); 1320 // In DebugInfo nonstatic fields are sorted by offset. 1321 int* fields_sorted = NEW_C_HEAP_ARRAY(int, 2*(length+1), mtClass); 1322 int j = 0; 1323 for (int i = 0; i < length; i += 1) { 1324 fd.reinitialize(this, i); 1325 if (!fd.is_static()) { 1326 fields_sorted[j + 0] = fd.offset(); 1327 fields_sorted[j + 1] = i; 1328 j += 2; 1329 } 1330 } 1331 if (j > 0) { 1332 length = j; 1333 // _sort_Fn is defined in growableArray.hpp. 1334 qsort(fields_sorted, length/2, 2*sizeof(int), (_sort_Fn)compare_fields_by_offset); 1335 for (int i = 0; i < length; i += 2) { 1336 fd.reinitialize(this, fields_sorted[i + 1]); 1337 assert(!fd.is_static() && fd.offset() == fields_sorted[i], "only nonstatic fields"); 1338 cl->do_field(&fd); 1339 } 1340 } 1341 FREE_C_HEAP_ARRAY(int, fields_sorted, mtClass); 1342 } 1343 1344 1345 void InstanceKlass::array_klasses_do(void f(Klass* k, TRAPS), TRAPS) { 1346 if (array_klasses() != NULL) 1347 ArrayKlass::cast(array_klasses())->array_klasses_do(f, THREAD); 1348 } 1349 1350 void InstanceKlass::array_klasses_do(void f(Klass* k)) { 1351 if (array_klasses() != NULL) 1352 ArrayKlass::cast(array_klasses())->array_klasses_do(f); 1353 } 1354 1355 #ifdef ASSERT 1356 static int linear_search(Array<Method*>* methods, Symbol* name, Symbol* signature) { 1357 int len = methods->length(); 1358 for (int index = 0; index < len; index++) { 1359 Method* m = methods->at(index); 1360 assert(m->is_method(), "must be method"); 1361 if (m->signature() == signature && m->name() == name) { 1362 return index; 1363 } 1364 } 1365 return -1; 1366 } 1367 #endif 1368 1369 static int binary_search(Array<Method*>* methods, Symbol* name) { 1370 int len = methods->length(); 1371 // methods are sorted, so do binary search 1372 int l = 0; 1373 int h = len - 1; 1374 while (l <= h) { 1375 int mid = (l + h) >> 1; 1376 Method* m = methods->at(mid); 1377 assert(m->is_method(), "must be method"); 1378 int res = m->name()->fast_compare(name); 1379 if (res == 0) { 1380 return mid; 1381 } else if (res < 0) { 1382 l = mid + 1; 1383 } else { 1384 h = mid - 1; 1385 } 1386 } 1387 return -1; 1388 } 1389 1390 // find_method looks up the name/signature in the local methods array 1391 Method* InstanceKlass::find_method(Symbol* name, Symbol* signature) const { 1392 return InstanceKlass::find_method(methods(), name, signature); 1393 } 1394 1395 // find_instance_method looks up the name/signature in the local methods array 1396 // and skips over static methods 1397 Method* InstanceKlass::find_instance_method( 1398 Array<Method*>* methods, Symbol* name, Symbol* signature) { 1399 Method* meth = InstanceKlass::find_method(methods, name, signature); 1400 if (meth != NULL && meth->is_static()) { 1401 meth = NULL; 1402 } 1403 return meth; 1404 } 1405 1406 // find_method looks up the name/signature in the local methods array 1407 Method* InstanceKlass::find_method( 1408 Array<Method*>* methods, Symbol* name, Symbol* signature) { 1409 int hit = find_method_index(methods, name, signature); 1410 return hit >= 0 ? methods->at(hit): NULL; 1411 } 1412 1413 // Used directly for default_methods to find the index into the 1414 // default_vtable_indices, and indirectly by find_method 1415 // find_method_index looks in the local methods array to return the index 1416 // of the matching name/signature 1417 int InstanceKlass::find_method_index( 1418 Array<Method*>* methods, Symbol* name, Symbol* signature) { 1419 int hit = binary_search(methods, name); 1420 if (hit != -1) { 1421 Method* m = methods->at(hit); 1422 // Do linear search to find matching signature. First, quick check 1423 // for common case 1424 if (m->signature() == signature) return hit; 1425 // search downwards through overloaded methods 1426 int i; 1427 for (i = hit - 1; i >= 0; --i) { 1428 Method* m = methods->at(i); 1429 assert(m->is_method(), "must be method"); 1430 if (m->name() != name) break; 1431 if (m->signature() == signature) return i; 1432 } 1433 // search upwards 1434 for (i = hit + 1; i < methods->length(); ++i) { 1435 Method* m = methods->at(i); 1436 assert(m->is_method(), "must be method"); 1437 if (m->name() != name) break; 1438 if (m->signature() == signature) return i; 1439 } 1440 // not found 1441 #ifdef ASSERT 1442 int index = linear_search(methods, name, signature); 1443 assert(index == -1, err_msg("binary search should have found entry %d", index)); 1444 #endif 1445 } 1446 return -1; 1447 } 1448 int InstanceKlass::find_method_by_name(Symbol* name, int* end) { 1449 return find_method_by_name(methods(), name, end); 1450 } 1451 1452 int InstanceKlass::find_method_by_name( 1453 Array<Method*>* methods, Symbol* name, int* end_ptr) { 1454 assert(end_ptr != NULL, "just checking"); 1455 int start = binary_search(methods, name); 1456 int end = start + 1; 1457 if (start != -1) { 1458 while (start - 1 >= 0 && (methods->at(start - 1))->name() == name) --start; 1459 while (end < methods->length() && (methods->at(end))->name() == name) ++end; 1460 *end_ptr = end; 1461 return start; 1462 } 1463 return -1; 1464 } 1465 1466 // uncached_lookup_method searches both the local class methods array and all 1467 // superclasses methods arrays, skipping any overpass methods in superclasses. 1468 Method* InstanceKlass::uncached_lookup_method(Symbol* name, Symbol* signature) const { 1469 Klass* klass = const_cast<InstanceKlass*>(this); 1470 bool dont_ignore_overpasses = true; // For the class being searched, find its overpasses. 1471 while (klass != NULL) { 1472 Method* method = InstanceKlass::cast(klass)->find_method(name, signature); 1473 if ((method != NULL) && (dont_ignore_overpasses || !method->is_overpass())) { 1474 return method; 1475 } 1476 klass = InstanceKlass::cast(klass)->super(); 1477 dont_ignore_overpasses = false; // Ignore overpass methods in all superclasses. 1478 } 1479 return NULL; 1480 } 1481 1482 #ifdef ASSERT 1483 // search through class hierarchy and return true if this class or 1484 // one of the superclasses was redefined 1485 bool InstanceKlass::has_redefined_this_or_super() const { 1486 const InstanceKlass* klass = this; 1487 while (klass != NULL) { 1488 if (klass->has_been_redefined()) { 1489 return true; 1490 } 1491 klass = InstanceKlass::cast(klass->super()); 1492 } 1493 return false; 1494 } 1495 #endif 1496 1497 // lookup a method in the default methods list then in all transitive interfaces 1498 // Do NOT return private or static methods 1499 Method* InstanceKlass::lookup_method_in_ordered_interfaces(Symbol* name, 1500 Symbol* signature) const { 1501 Method* m = NULL; 1502 if (default_methods() != NULL) { 1503 m = find_method(default_methods(), name, signature); 1504 } 1505 // Look up interfaces 1506 if (m == NULL) { 1507 m = lookup_method_in_all_interfaces(name, signature, false); 1508 } 1509 return m; 1510 } 1511 1512 // lookup a method in all the interfaces that this class implements 1513 // Do NOT return private or static methods, new in JDK8 which are not externally visible 1514 // They should only be found in the initial InterfaceMethodRef 1515 Method* InstanceKlass::lookup_method_in_all_interfaces(Symbol* name, 1516 Symbol* signature, 1517 bool skip_default_methods) const { 1518 Array<Klass*>* all_ifs = transitive_interfaces(); 1519 int num_ifs = all_ifs->length(); 1520 InstanceKlass *ik = NULL; 1521 for (int i = 0; i < num_ifs; i++) { 1522 ik = InstanceKlass::cast(all_ifs->at(i)); 1523 Method* m = ik->lookup_method(name, signature); 1524 if (m != NULL && m->is_public() && !m->is_static() && 1525 (!skip_default_methods || !m->is_default_method())) { 1526 return m; 1527 } 1528 } 1529 return NULL; 1530 } 1531 1532 /* jni_id_for_impl for jfieldIds only */ 1533 JNIid* InstanceKlass::jni_id_for_impl(instanceKlassHandle this_k, int offset) { 1534 MutexLocker ml(JfieldIdCreation_lock); 1535 // Retry lookup after we got the lock 1536 JNIid* probe = this_k->jni_ids() == NULL ? NULL : this_k->jni_ids()->find(offset); 1537 if (probe == NULL) { 1538 // Slow case, allocate new static field identifier 1539 probe = new JNIid(this_k(), offset, this_k->jni_ids()); 1540 this_k->set_jni_ids(probe); 1541 } 1542 return probe; 1543 } 1544 1545 1546 /* jni_id_for for jfieldIds only */ 1547 JNIid* InstanceKlass::jni_id_for(int offset) { 1548 JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset); 1549 if (probe == NULL) { 1550 probe = jni_id_for_impl(this, offset); 1551 } 1552 return probe; 1553 } 1554 1555 u2 InstanceKlass::enclosing_method_data(int offset) { 1556 Array<jushort>* inner_class_list = inner_classes(); 1557 if (inner_class_list == NULL) { 1558 return 0; 1559 } 1560 int length = inner_class_list->length(); 1561 if (length % inner_class_next_offset == 0) { 1562 return 0; 1563 } else { 1564 int index = length - enclosing_method_attribute_size; 1565 assert(offset < enclosing_method_attribute_size, "invalid offset"); 1566 return inner_class_list->at(index + offset); 1567 } 1568 } 1569 1570 void InstanceKlass::set_enclosing_method_indices(u2 class_index, 1571 u2 method_index) { 1572 Array<jushort>* inner_class_list = inner_classes(); 1573 assert (inner_class_list != NULL, "_inner_classes list is not set up"); 1574 int length = inner_class_list->length(); 1575 if (length % inner_class_next_offset == enclosing_method_attribute_size) { 1576 int index = length - enclosing_method_attribute_size; 1577 inner_class_list->at_put( 1578 index + enclosing_method_class_index_offset, class_index); 1579 inner_class_list->at_put( 1580 index + enclosing_method_method_index_offset, method_index); 1581 } 1582 } 1583 1584 // Lookup or create a jmethodID. 1585 // This code is called by the VMThread and JavaThreads so the 1586 // locking has to be done very carefully to avoid deadlocks 1587 // and/or other cache consistency problems. 1588 // 1589 jmethodID InstanceKlass::get_jmethod_id(instanceKlassHandle ik_h, methodHandle method_h) { 1590 size_t idnum = (size_t)method_h->method_idnum(); 1591 jmethodID* jmeths = ik_h->methods_jmethod_ids_acquire(); 1592 size_t length = 0; 1593 jmethodID id = NULL; 1594 1595 // We use a double-check locking idiom here because this cache is 1596 // performance sensitive. In the normal system, this cache only 1597 // transitions from NULL to non-NULL which is safe because we use 1598 // release_set_methods_jmethod_ids() to advertise the new cache. 1599 // A partially constructed cache should never be seen by a racing 1600 // thread. We also use release_store_ptr() to save a new jmethodID 1601 // in the cache so a partially constructed jmethodID should never be 1602 // seen either. Cache reads of existing jmethodIDs proceed without a 1603 // lock, but cache writes of a new jmethodID requires uniqueness and 1604 // creation of the cache itself requires no leaks so a lock is 1605 // generally acquired in those two cases. 1606 // 1607 // If the RedefineClasses() API has been used, then this cache can 1608 // grow and we'll have transitions from non-NULL to bigger non-NULL. 1609 // Cache creation requires no leaks and we require safety between all 1610 // cache accesses and freeing of the old cache so a lock is generally 1611 // acquired when the RedefineClasses() API has been used. 1612 1613 if (jmeths != NULL) { 1614 // the cache already exists 1615 if (!ik_h->idnum_can_increment()) { 1616 // the cache can't grow so we can just get the current values 1617 get_jmethod_id_length_value(jmeths, idnum, &length, &id); 1618 } else { 1619 // cache can grow so we have to be more careful 1620 if (Threads::number_of_threads() == 0 || 1621 SafepointSynchronize::is_at_safepoint()) { 1622 // we're single threaded or at a safepoint - no locking needed 1623 get_jmethod_id_length_value(jmeths, idnum, &length, &id); 1624 } else { 1625 MutexLocker ml(JmethodIdCreation_lock); 1626 get_jmethod_id_length_value(jmeths, idnum, &length, &id); 1627 } 1628 } 1629 } 1630 // implied else: 1631 // we need to allocate a cache so default length and id values are good 1632 1633 if (jmeths == NULL || // no cache yet 1634 length <= idnum || // cache is too short 1635 id == NULL) { // cache doesn't contain entry 1636 1637 // This function can be called by the VMThread so we have to do all 1638 // things that might block on a safepoint before grabbing the lock. 1639 // Otherwise, we can deadlock with the VMThread or have a cache 1640 // consistency issue. These vars keep track of what we might have 1641 // to free after the lock is dropped. 1642 jmethodID to_dealloc_id = NULL; 1643 jmethodID* to_dealloc_jmeths = NULL; 1644 1645 // may not allocate new_jmeths or use it if we allocate it 1646 jmethodID* new_jmeths = NULL; 1647 if (length <= idnum) { 1648 // allocate a new cache that might be used 1649 size_t size = MAX2(idnum+1, (size_t)ik_h->idnum_allocated_count()); 1650 new_jmeths = NEW_C_HEAP_ARRAY(jmethodID, size+1, mtClass); 1651 memset(new_jmeths, 0, (size+1)*sizeof(jmethodID)); 1652 // cache size is stored in element[0], other elements offset by one 1653 new_jmeths[0] = (jmethodID)size; 1654 } 1655 1656 // allocate a new jmethodID that might be used 1657 jmethodID new_id = NULL; 1658 if (method_h->is_old() && !method_h->is_obsolete()) { 1659 // The method passed in is old (but not obsolete), we need to use the current version 1660 Method* current_method = ik_h->method_with_idnum((int)idnum); 1661 assert(current_method != NULL, "old and but not obsolete, so should exist"); 1662 new_id = Method::make_jmethod_id(ik_h->class_loader_data(), current_method); 1663 } else { 1664 // It is the current version of the method or an obsolete method, 1665 // use the version passed in 1666 new_id = Method::make_jmethod_id(ik_h->class_loader_data(), method_h()); 1667 } 1668 1669 if (Threads::number_of_threads() == 0 || 1670 SafepointSynchronize::is_at_safepoint()) { 1671 // we're single threaded or at a safepoint - no locking needed 1672 id = get_jmethod_id_fetch_or_update(ik_h, idnum, new_id, new_jmeths, 1673 &to_dealloc_id, &to_dealloc_jmeths); 1674 } else { 1675 MutexLocker ml(JmethodIdCreation_lock); 1676 id = get_jmethod_id_fetch_or_update(ik_h, idnum, new_id, new_jmeths, 1677 &to_dealloc_id, &to_dealloc_jmeths); 1678 } 1679 1680 // The lock has been dropped so we can free resources. 1681 // Free up either the old cache or the new cache if we allocated one. 1682 if (to_dealloc_jmeths != NULL) { 1683 FreeHeap(to_dealloc_jmeths); 1684 } 1685 // free up the new ID since it wasn't needed 1686 if (to_dealloc_id != NULL) { 1687 Method::destroy_jmethod_id(ik_h->class_loader_data(), to_dealloc_id); 1688 } 1689 } 1690 return id; 1691 } 1692 1693 1694 // Common code to fetch the jmethodID from the cache or update the 1695 // cache with the new jmethodID. This function should never do anything 1696 // that causes the caller to go to a safepoint or we can deadlock with 1697 // the VMThread or have cache consistency issues. 1698 // 1699 jmethodID InstanceKlass::get_jmethod_id_fetch_or_update( 1700 instanceKlassHandle ik_h, size_t idnum, jmethodID new_id, 1701 jmethodID* new_jmeths, jmethodID* to_dealloc_id_p, 1702 jmethodID** to_dealloc_jmeths_p) { 1703 assert(new_id != NULL, "sanity check"); 1704 assert(to_dealloc_id_p != NULL, "sanity check"); 1705 assert(to_dealloc_jmeths_p != NULL, "sanity check"); 1706 assert(Threads::number_of_threads() == 0 || 1707 SafepointSynchronize::is_at_safepoint() || 1708 JmethodIdCreation_lock->owned_by_self(), "sanity check"); 1709 1710 // reacquire the cache - we are locked, single threaded or at a safepoint 1711 jmethodID* jmeths = ik_h->methods_jmethod_ids_acquire(); 1712 jmethodID id = NULL; 1713 size_t length = 0; 1714 1715 if (jmeths == NULL || // no cache yet 1716 (length = (size_t)jmeths[0]) <= idnum) { // cache is too short 1717 if (jmeths != NULL) { 1718 // copy any existing entries from the old cache 1719 for (size_t index = 0; index < length; index++) { 1720 new_jmeths[index+1] = jmeths[index+1]; 1721 } 1722 *to_dealloc_jmeths_p = jmeths; // save old cache for later delete 1723 } 1724 ik_h->release_set_methods_jmethod_ids(jmeths = new_jmeths); 1725 } else { 1726 // fetch jmethodID (if any) from the existing cache 1727 id = jmeths[idnum+1]; 1728 *to_dealloc_jmeths_p = new_jmeths; // save new cache for later delete 1729 } 1730 if (id == NULL) { 1731 // No matching jmethodID in the existing cache or we have a new 1732 // cache or we just grew the cache. This cache write is done here 1733 // by the first thread to win the foot race because a jmethodID 1734 // needs to be unique once it is generally available. 1735 id = new_id; 1736 1737 // The jmethodID cache can be read while unlocked so we have to 1738 // make sure the new jmethodID is complete before installing it 1739 // in the cache. 1740 OrderAccess::release_store_ptr(&jmeths[idnum+1], id); 1741 } else { 1742 *to_dealloc_id_p = new_id; // save new id for later delete 1743 } 1744 return id; 1745 } 1746 1747 1748 // Common code to get the jmethodID cache length and the jmethodID 1749 // value at index idnum if there is one. 1750 // 1751 void InstanceKlass::get_jmethod_id_length_value(jmethodID* cache, 1752 size_t idnum, size_t *length_p, jmethodID* id_p) { 1753 assert(cache != NULL, "sanity check"); 1754 assert(length_p != NULL, "sanity check"); 1755 assert(id_p != NULL, "sanity check"); 1756 1757 // cache size is stored in element[0], other elements offset by one 1758 *length_p = (size_t)cache[0]; 1759 if (*length_p <= idnum) { // cache is too short 1760 *id_p = NULL; 1761 } else { 1762 *id_p = cache[idnum+1]; // fetch jmethodID (if any) 1763 } 1764 } 1765 1766 1767 // Lookup a jmethodID, NULL if not found. Do no blocking, no allocations, no handles 1768 jmethodID InstanceKlass::jmethod_id_or_null(Method* method) { 1769 size_t idnum = (size_t)method->method_idnum(); 1770 jmethodID* jmeths = methods_jmethod_ids_acquire(); 1771 size_t length; // length assigned as debugging crumb 1772 jmethodID id = NULL; 1773 if (jmeths != NULL && // If there is a cache 1774 (length = (size_t)jmeths[0]) > idnum) { // and if it is long enough, 1775 id = jmeths[idnum+1]; // Look up the id (may be NULL) 1776 } 1777 return id; 1778 } 1779 1780 1781 // 1782 // Walk the list of dependent nmethods searching for nmethods which 1783 // are dependent on the changes that were passed in and mark them for 1784 // deoptimization. Returns the number of nmethods found. 1785 // 1786 int InstanceKlass::mark_dependent_nmethods(DepChange& changes) { 1787 assert_locked_or_safepoint(CodeCache_lock); 1788 int found = 0; 1789 nmethodBucket* b = _dependencies; 1790 while (b != NULL) { 1791 nmethod* nm = b->get_nmethod(); 1792 // since dependencies aren't removed until an nmethod becomes a zombie, 1793 // the dependency list may contain nmethods which aren't alive. 1794 if (nm->is_alive() && !nm->is_marked_for_deoptimization() && nm->check_dependency_on(changes)) { 1795 if (TraceDependencies) { 1796 ResourceMark rm; 1797 tty->print_cr("Marked for deoptimization"); 1798 tty->print_cr(" context = %s", this->external_name()); 1799 changes.print(); 1800 nm->print(); 1801 nm->print_dependencies(); 1802 } 1803 nm->mark_for_deoptimization(); 1804 found++; 1805 } 1806 b = b->next(); 1807 } 1808 return found; 1809 } 1810 1811 1812 // 1813 // Add an nmethodBucket to the list of dependencies for this nmethod. 1814 // It's possible that an nmethod has multiple dependencies on this klass 1815 // so a count is kept for each bucket to guarantee that creation and 1816 // deletion of dependencies is consistent. 1817 // 1818 void InstanceKlass::add_dependent_nmethod(nmethod* nm) { 1819 assert_locked_or_safepoint(CodeCache_lock); 1820 nmethodBucket* b = _dependencies; 1821 nmethodBucket* last = NULL; 1822 while (b != NULL) { 1823 if (nm == b->get_nmethod()) { 1824 b->increment(); 1825 return; 1826 } 1827 b = b->next(); 1828 } 1829 _dependencies = new nmethodBucket(nm, _dependencies); 1830 } 1831 1832 1833 // 1834 // Decrement count of the nmethod in the dependency list and remove 1835 // the bucket competely when the count goes to 0. This method must 1836 // find a corresponding bucket otherwise there's a bug in the 1837 // recording of dependecies. 1838 // 1839 void InstanceKlass::remove_dependent_nmethod(nmethod* nm) { 1840 assert_locked_or_safepoint(CodeCache_lock); 1841 nmethodBucket* b = _dependencies; 1842 nmethodBucket* last = NULL; 1843 while (b != NULL) { 1844 if (nm == b->get_nmethod()) { 1845 if (b->decrement() == 0) { 1846 if (last == NULL) { 1847 _dependencies = b->next(); 1848 } else { 1849 last->set_next(b->next()); 1850 } 1851 delete b; 1852 } 1853 return; 1854 } 1855 last = b; 1856 b = b->next(); 1857 } 1858 #ifdef ASSERT 1859 tty->print_cr("### %s can't find dependent nmethod:", this->external_name()); 1860 nm->print(); 1861 #endif // ASSERT 1862 ShouldNotReachHere(); 1863 } 1864 1865 1866 #ifndef PRODUCT 1867 void InstanceKlass::print_dependent_nmethods(bool verbose) { 1868 nmethodBucket* b = _dependencies; 1869 int idx = 0; 1870 while (b != NULL) { 1871 nmethod* nm = b->get_nmethod(); 1872 tty->print("[%d] count=%d { ", idx++, b->count()); 1873 if (!verbose) { 1874 nm->print_on(tty, "nmethod"); 1875 tty->print_cr(" } "); 1876 } else { 1877 nm->print(); 1878 nm->print_dependencies(); 1879 tty->print_cr("--- } "); 1880 } 1881 b = b->next(); 1882 } 1883 } 1884 1885 1886 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) { 1887 nmethodBucket* b = _dependencies; 1888 while (b != NULL) { 1889 if (nm == b->get_nmethod()) { 1890 return true; 1891 } 1892 b = b->next(); 1893 } 1894 return false; 1895 } 1896 #endif //PRODUCT 1897 1898 1899 // Garbage collection 1900 1901 #ifdef ASSERT 1902 template <class T> void assert_is_in(T *p) { 1903 T heap_oop = oopDesc::load_heap_oop(p); 1904 if (!oopDesc::is_null(heap_oop)) { 1905 oop o = oopDesc::decode_heap_oop_not_null(heap_oop); 1906 assert(Universe::heap()->is_in(o), "should be in heap"); 1907 } 1908 } 1909 template <class T> void assert_is_in_closed_subset(T *p) { 1910 T heap_oop = oopDesc::load_heap_oop(p); 1911 if (!oopDesc::is_null(heap_oop)) { 1912 oop o = oopDesc::decode_heap_oop_not_null(heap_oop); 1913 assert(Universe::heap()->is_in_closed_subset(o), 1914 err_msg("should be in closed *p " INTPTR_FORMAT " " INTPTR_FORMAT, (address)p, (address)o)); 1915 } 1916 } 1917 template <class T> void assert_is_in_reserved(T *p) { 1918 T heap_oop = oopDesc::load_heap_oop(p); 1919 if (!oopDesc::is_null(heap_oop)) { 1920 oop o = oopDesc::decode_heap_oop_not_null(heap_oop); 1921 assert(Universe::heap()->is_in_reserved(o), "should be in reserved"); 1922 } 1923 } 1924 template <class T> void assert_nothing(T *p) {} 1925 1926 #else 1927 template <class T> void assert_is_in(T *p) {} 1928 template <class T> void assert_is_in_closed_subset(T *p) {} 1929 template <class T> void assert_is_in_reserved(T *p) {} 1930 template <class T> void assert_nothing(T *p) {} 1931 #endif // ASSERT 1932 1933 // 1934 // Macros that iterate over areas of oops which are specialized on type of 1935 // oop pointer either narrow or wide, depending on UseCompressedOops 1936 // 1937 // Parameters are: 1938 // T - type of oop to point to (either oop or narrowOop) 1939 // start_p - starting pointer for region to iterate over 1940 // count - number of oops or narrowOops to iterate over 1941 // do_oop - action to perform on each oop (it's arbitrary C code which 1942 // makes it more efficient to put in a macro rather than making 1943 // it a template function) 1944 // assert_fn - assert function which is template function because performance 1945 // doesn't matter when enabled. 1946 #define InstanceKlass_SPECIALIZED_OOP_ITERATE( \ 1947 T, start_p, count, do_oop, \ 1948 assert_fn) \ 1949 { \ 1950 T* p = (T*)(start_p); \ 1951 T* const end = p + (count); \ 1952 while (p < end) { \ 1953 (assert_fn)(p); \ 1954 do_oop; \ 1955 ++p; \ 1956 } \ 1957 } 1958 1959 #define InstanceKlass_SPECIALIZED_OOP_REVERSE_ITERATE( \ 1960 T, start_p, count, do_oop, \ 1961 assert_fn) \ 1962 { \ 1963 T* const start = (T*)(start_p); \ 1964 T* p = start + (count); \ 1965 while (start < p) { \ 1966 --p; \ 1967 (assert_fn)(p); \ 1968 do_oop; \ 1969 } \ 1970 } 1971 1972 #define InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE( \ 1973 T, start_p, count, low, high, \ 1974 do_oop, assert_fn) \ 1975 { \ 1976 T* const l = (T*)(low); \ 1977 T* const h = (T*)(high); \ 1978 assert(mask_bits((intptr_t)l, sizeof(T)-1) == 0 && \ 1979 mask_bits((intptr_t)h, sizeof(T)-1) == 0, \ 1980 "bounded region must be properly aligned"); \ 1981 T* p = (T*)(start_p); \ 1982 T* end = p + (count); \ 1983 if (p < l) p = l; \ 1984 if (end > h) end = h; \ 1985 while (p < end) { \ 1986 (assert_fn)(p); \ 1987 do_oop; \ 1988 ++p; \ 1989 } \ 1990 } 1991 1992 1993 // The following macros call specialized macros, passing either oop or 1994 // narrowOop as the specialization type. These test the UseCompressedOops 1995 // flag. 1996 #define InstanceKlass_OOP_MAP_ITERATE(obj, do_oop, assert_fn) \ 1997 { \ 1998 /* Compute oopmap block range. The common case \ 1999 is nonstatic_oop_map_size == 1. */ \ 2000 OopMapBlock* map = start_of_nonstatic_oop_maps(); \ 2001 OopMapBlock* const end_map = map + nonstatic_oop_map_count(); \ 2002 if (UseCompressedOops) { \ 2003 while (map < end_map) { \ 2004 InstanceKlass_SPECIALIZED_OOP_ITERATE(narrowOop, \ 2005 obj->obj_field_addr<narrowOop>(map->offset()), map->count(), \ 2006 do_oop, assert_fn) \ 2007 ++map; \ 2008 } \ 2009 } else { \ 2010 while (map < end_map) { \ 2011 InstanceKlass_SPECIALIZED_OOP_ITERATE(oop, \ 2012 obj->obj_field_addr<oop>(map->offset()), map->count(), \ 2013 do_oop, assert_fn) \ 2014 ++map; \ 2015 } \ 2016 } \ 2017 } 2018 2019 #define InstanceKlass_OOP_MAP_REVERSE_ITERATE(obj, do_oop, assert_fn) \ 2020 { \ 2021 OopMapBlock* const start_map = start_of_nonstatic_oop_maps(); \ 2022 OopMapBlock* map = start_map + nonstatic_oop_map_count(); \ 2023 if (UseCompressedOops) { \ 2024 while (start_map < map) { \ 2025 --map; \ 2026 InstanceKlass_SPECIALIZED_OOP_REVERSE_ITERATE(narrowOop, \ 2027 obj->obj_field_addr<narrowOop>(map->offset()), map->count(), \ 2028 do_oop, assert_fn) \ 2029 } \ 2030 } else { \ 2031 while (start_map < map) { \ 2032 --map; \ 2033 InstanceKlass_SPECIALIZED_OOP_REVERSE_ITERATE(oop, \ 2034 obj->obj_field_addr<oop>(map->offset()), map->count(), \ 2035 do_oop, assert_fn) \ 2036 } \ 2037 } \ 2038 } 2039 2040 #define InstanceKlass_BOUNDED_OOP_MAP_ITERATE(obj, low, high, do_oop, \ 2041 assert_fn) \ 2042 { \ 2043 /* Compute oopmap block range. The common case is \ 2044 nonstatic_oop_map_size == 1, so we accept the \ 2045 usually non-existent extra overhead of examining \ 2046 all the maps. */ \ 2047 OopMapBlock* map = start_of_nonstatic_oop_maps(); \ 2048 OopMapBlock* const end_map = map + nonstatic_oop_map_count(); \ 2049 if (UseCompressedOops) { \ 2050 while (map < end_map) { \ 2051 InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE(narrowOop, \ 2052 obj->obj_field_addr<narrowOop>(map->offset()), map->count(), \ 2053 low, high, \ 2054 do_oop, assert_fn) \ 2055 ++map; \ 2056 } \ 2057 } else { \ 2058 while (map < end_map) { \ 2059 InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE(oop, \ 2060 obj->obj_field_addr<oop>(map->offset()), map->count(), \ 2061 low, high, \ 2062 do_oop, assert_fn) \ 2063 ++map; \ 2064 } \ 2065 } \ 2066 } 2067 2068 void InstanceKlass::oop_follow_contents(oop obj) { 2069 assert(obj != NULL, "can't follow the content of NULL object"); 2070 MarkSweep::follow_klass(obj->klass()); 2071 InstanceKlass_OOP_MAP_ITERATE( \ 2072 obj, \ 2073 MarkSweep::mark_and_push(p), \ 2074 assert_is_in_closed_subset) 2075 } 2076 2077 #if INCLUDE_ALL_GCS 2078 void InstanceKlass::oop_follow_contents(ParCompactionManager* cm, 2079 oop obj) { 2080 assert(obj != NULL, "can't follow the content of NULL object"); 2081 PSParallelCompact::follow_klass(cm, obj->klass()); 2082 // Only mark the header and let the scan of the meta-data mark 2083 // everything else. 2084 InstanceKlass_OOP_MAP_ITERATE( \ 2085 obj, \ 2086 PSParallelCompact::mark_and_push(cm, p), \ 2087 assert_is_in) 2088 } 2089 #endif // INCLUDE_ALL_GCS 2090 2091 // closure's do_metadata() method dictates whether the given closure should be 2092 // applied to the klass ptr in the object header. 2093 2094 #define if_do_metadata_checked(closure, nv_suffix) \ 2095 /* Make sure the non-virtual and the virtual versions match. */ \ 2096 assert(closure->do_metadata##nv_suffix() == closure->do_metadata(), \ 2097 "Inconsistency in do_metadata"); \ 2098 if (closure->do_metadata##nv_suffix()) 2099 2100 #define InstanceKlass_OOP_OOP_ITERATE_DEFN(OopClosureType, nv_suffix) \ 2101 \ 2102 int InstanceKlass::oop_oop_iterate##nv_suffix(oop obj, OopClosureType* closure) { \ 2103 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik);\ 2104 /* header */ \ 2105 if_do_metadata_checked(closure, nv_suffix) { \ 2106 closure->do_klass##nv_suffix(obj->klass()); \ 2107 } \ 2108 InstanceKlass_OOP_MAP_ITERATE( \ 2109 obj, \ 2110 SpecializationStats:: \ 2111 record_do_oop_call##nv_suffix(SpecializationStats::ik); \ 2112 (closure)->do_oop##nv_suffix(p), \ 2113 assert_is_in_closed_subset) \ 2114 return size_helper(); \ 2115 } 2116 2117 #if INCLUDE_ALL_GCS 2118 #define InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix) \ 2119 \ 2120 int InstanceKlass::oop_oop_iterate_backwards##nv_suffix(oop obj, \ 2121 OopClosureType* closure) { \ 2122 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik); \ 2123 /* header */ \ 2124 if_do_metadata_checked(closure, nv_suffix) { \ 2125 closure->do_klass##nv_suffix(obj->klass()); \ 2126 } \ 2127 /* instance variables */ \ 2128 InstanceKlass_OOP_MAP_REVERSE_ITERATE( \ 2129 obj, \ 2130 SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::ik);\ 2131 (closure)->do_oop##nv_suffix(p), \ 2132 assert_is_in_closed_subset) \ 2133 return size_helper(); \ 2134 } 2135 #endif // INCLUDE_ALL_GCS 2136 2137 #define InstanceKlass_OOP_OOP_ITERATE_DEFN_m(OopClosureType, nv_suffix) \ 2138 \ 2139 int InstanceKlass::oop_oop_iterate##nv_suffix##_m(oop obj, \ 2140 OopClosureType* closure, \ 2141 MemRegion mr) { \ 2142 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik);\ 2143 if_do_metadata_checked(closure, nv_suffix) { \ 2144 if (mr.contains(obj)) { \ 2145 closure->do_klass##nv_suffix(obj->klass()); \ 2146 } \ 2147 } \ 2148 InstanceKlass_BOUNDED_OOP_MAP_ITERATE( \ 2149 obj, mr.start(), mr.end(), \ 2150 (closure)->do_oop##nv_suffix(p), \ 2151 assert_is_in_closed_subset) \ 2152 return size_helper(); \ 2153 } 2154 2155 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_DEFN) 2156 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceKlass_OOP_OOP_ITERATE_DEFN) 2157 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_DEFN_m) 2158 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceKlass_OOP_OOP_ITERATE_DEFN_m) 2159 #if INCLUDE_ALL_GCS 2160 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN) 2161 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN) 2162 #endif // INCLUDE_ALL_GCS 2163 2164 int InstanceKlass::oop_adjust_pointers(oop obj) { 2165 int size = size_helper(); 2166 InstanceKlass_OOP_MAP_ITERATE( \ 2167 obj, \ 2168 MarkSweep::adjust_pointer(p), \ 2169 assert_is_in) 2170 return size; 2171 } 2172 2173 #if INCLUDE_ALL_GCS 2174 void InstanceKlass::oop_push_contents(PSPromotionManager* pm, oop obj) { 2175 InstanceKlass_OOP_MAP_REVERSE_ITERATE( \ 2176 obj, \ 2177 if (PSScavenge::should_scavenge(p)) { \ 2178 pm->claim_or_forward_depth(p); \ 2179 }, \ 2180 assert_nothing ) 2181 } 2182 2183 int InstanceKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) { 2184 int size = size_helper(); 2185 InstanceKlass_OOP_MAP_ITERATE( \ 2186 obj, \ 2187 PSParallelCompact::adjust_pointer(p), \ 2188 assert_is_in) 2189 return size; 2190 } 2191 2192 #endif // INCLUDE_ALL_GCS 2193 2194 void InstanceKlass::clean_implementors_list(BoolObjectClosure* is_alive) { 2195 assert(is_loader_alive(is_alive), "this klass should be live"); 2196 if (is_interface()) { 2197 if (ClassUnloading) { 2198 Klass* impl = implementor(); 2199 if (impl != NULL) { 2200 if (!impl->is_loader_alive(is_alive)) { 2201 // remove this guy 2202 Klass** klass = adr_implementor(); 2203 assert(klass != NULL, "null klass"); 2204 if (klass != NULL) { 2205 *klass = NULL; 2206 } 2207 } 2208 } 2209 } 2210 } 2211 } 2212 2213 void InstanceKlass::clean_method_data(BoolObjectClosure* is_alive) { 2214 for (int m = 0; m < methods()->length(); m++) { 2215 MethodData* mdo = methods()->at(m)->method_data(); 2216 if (mdo != NULL) { 2217 mdo->clean_method_data(is_alive); 2218 } 2219 } 2220 } 2221 2222 2223 static void remove_unshareable_in_class(Klass* k) { 2224 // remove klass's unshareable info 2225 k->remove_unshareable_info(); 2226 } 2227 2228 void InstanceKlass::remove_unshareable_info() { 2229 Klass::remove_unshareable_info(); 2230 // Unlink the class 2231 if (is_linked()) { 2232 unlink_class(); 2233 } 2234 init_implementor(); 2235 2236 constants()->remove_unshareable_info(); 2237 2238 for (int i = 0; i < methods()->length(); i++) { 2239 Method* m = methods()->at(i); 2240 m->remove_unshareable_info(); 2241 } 2242 2243 // do array classes also. 2244 array_klasses_do(remove_unshareable_in_class); 2245 } 2246 2247 void restore_unshareable_in_class(Klass* k, TRAPS) { 2248 k->restore_unshareable_info(CHECK); 2249 } 2250 2251 void InstanceKlass::restore_unshareable_info(TRAPS) { 2252 Klass::restore_unshareable_info(CHECK); 2253 instanceKlassHandle ik(THREAD, this); 2254 2255 Array<Method*>* methods = ik->methods(); 2256 int num_methods = methods->length(); 2257 for (int index2 = 0; index2 < num_methods; ++index2) { 2258 methodHandle m(THREAD, methods->at(index2)); 2259 m->restore_unshareable_info(CHECK); 2260 } 2261 if (JvmtiExport::has_redefined_a_class()) { 2262 // Reinitialize vtable because RedefineClasses may have changed some 2263 // entries in this vtable for super classes so the CDS vtable might 2264 // point to old or obsolete entries. RedefineClasses doesn't fix up 2265 // vtables in the shared system dictionary, only the main one. 2266 // It also redefines the itable too so fix that too. 2267 ResourceMark rm(THREAD); 2268 ik->vtable()->initialize_vtable(false, CHECK); 2269 ik->itable()->initialize_itable(false, CHECK); 2270 } 2271 2272 // restore constant pool resolved references 2273 ik->constants()->restore_unshareable_info(CHECK); 2274 2275 ik->array_klasses_do(restore_unshareable_in_class, CHECK); 2276 } 2277 2278 static void clear_all_breakpoints(Method* m) { 2279 m->clear_all_breakpoints(); 2280 } 2281 2282 2283 void InstanceKlass::notify_unload_class(InstanceKlass* ik) { 2284 // notify the debugger 2285 if (JvmtiExport::should_post_class_unload()) { 2286 JvmtiExport::post_class_unload(ik); 2287 } 2288 2289 // notify ClassLoadingService of class unload 2290 ClassLoadingService::notify_class_unloaded(ik); 2291 } 2292 2293 void InstanceKlass::release_C_heap_structures(InstanceKlass* ik) { 2294 // Clean up C heap 2295 ik->release_C_heap_structures(); 2296 ik->constants()->release_C_heap_structures(); 2297 } 2298 2299 void InstanceKlass::release_C_heap_structures() { 2300 2301 // Can't release the constant pool here because the constant pool can be 2302 // deallocated separately from the InstanceKlass for default methods and 2303 // redefine classes. 2304 2305 // Deallocate oop map cache 2306 if (_oop_map_cache != NULL) { 2307 delete _oop_map_cache; 2308 _oop_map_cache = NULL; 2309 } 2310 2311 // Deallocate JNI identifiers for jfieldIDs 2312 JNIid::deallocate(jni_ids()); 2313 set_jni_ids(NULL); 2314 2315 jmethodID* jmeths = methods_jmethod_ids_acquire(); 2316 if (jmeths != (jmethodID*)NULL) { 2317 release_set_methods_jmethod_ids(NULL); 2318 FreeHeap(jmeths); 2319 } 2320 2321 // Deallocate MemberNameTable 2322 { 2323 Mutex* lock_or_null = SafepointSynchronize::is_at_safepoint() ? NULL : MemberNameTable_lock; 2324 MutexLockerEx ml(lock_or_null, Mutex::_no_safepoint_check_flag); 2325 MemberNameTable* mnt = member_names(); 2326 if (mnt != NULL) { 2327 delete mnt; 2328 set_member_names(NULL); 2329 } 2330 } 2331 2332 // release dependencies 2333 nmethodBucket* b = _dependencies; 2334 _dependencies = NULL; 2335 while (b != NULL) { 2336 nmethodBucket* next = b->next(); 2337 delete b; 2338 b = next; 2339 } 2340 2341 // Deallocate breakpoint records 2342 if (breakpoints() != 0x0) { 2343 methods_do(clear_all_breakpoints); 2344 assert(breakpoints() == 0x0, "should have cleared breakpoints"); 2345 } 2346 2347 // deallocate information about previous versions 2348 if (_previous_versions != NULL) { 2349 for (int i = _previous_versions->length() - 1; i >= 0; i--) { 2350 PreviousVersionNode * pv_node = _previous_versions->at(i); 2351 delete pv_node; 2352 } 2353 delete _previous_versions; 2354 _previous_versions = NULL; 2355 } 2356 2357 // deallocate the cached class file 2358 if (_cached_class_file != NULL) { 2359 os::free(_cached_class_file, mtClass); 2360 _cached_class_file = NULL; 2361 } 2362 2363 // Decrement symbol reference counts associated with the unloaded class. 2364 if (_name != NULL) _name->decrement_refcount(); 2365 // unreference array name derived from this class name (arrays of an unloaded 2366 // class can't be referenced anymore). 2367 if (_array_name != NULL) _array_name->decrement_refcount(); 2368 if (_source_debug_extension != NULL) FREE_C_HEAP_ARRAY(char, _source_debug_extension, mtClass); 2369 2370 assert(_total_instanceKlass_count >= 1, "Sanity check"); 2371 Atomic::dec(&_total_instanceKlass_count); 2372 } 2373 2374 void InstanceKlass::set_source_debug_extension(char* array, int length) { 2375 if (array == NULL) { 2376 _source_debug_extension = NULL; 2377 } else { 2378 // Adding one to the attribute length in order to store a null terminator 2379 // character could cause an overflow because the attribute length is 2380 // already coded with an u4 in the classfile, but in practice, it's 2381 // unlikely to happen. 2382 assert((length+1) > length, "Overflow checking"); 2383 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass); 2384 for (int i = 0; i < length; i++) { 2385 sde[i] = array[i]; 2386 } 2387 sde[length] = '\0'; 2388 _source_debug_extension = sde; 2389 } 2390 } 2391 2392 address InstanceKlass::static_field_addr(int offset) { 2393 return (address)(offset + InstanceMirrorKlass::offset_of_static_fields() + cast_from_oop<intptr_t>(java_mirror())); 2394 } 2395 2396 2397 const char* InstanceKlass::signature_name() const { 2398 int hash_len = 0; 2399 char hash_buf[40]; 2400 2401 // If this is an anonymous class, append a hash to make the name unique 2402 if (is_anonymous()) { 2403 assert(EnableInvokeDynamic, "EnableInvokeDynamic was not set."); 2404 intptr_t hash = (java_mirror() != NULL) ? java_mirror()->identity_hash() : 0; 2405 sprintf(hash_buf, "/" UINTX_FORMAT, (uintx)hash); 2406 hash_len = (int)strlen(hash_buf); 2407 } 2408 2409 // Get the internal name as a c string 2410 const char* src = (const char*) (name()->as_C_string()); 2411 const int src_length = (int)strlen(src); 2412 2413 char* dest = NEW_RESOURCE_ARRAY(char, src_length + hash_len + 3); 2414 2415 // Add L as type indicator 2416 int dest_index = 0; 2417 dest[dest_index++] = 'L'; 2418 2419 // Add the actual class name 2420 for (int src_index = 0; src_index < src_length; ) { 2421 dest[dest_index++] = src[src_index++]; 2422 } 2423 2424 // If we have a hash, append it 2425 for (int hash_index = 0; hash_index < hash_len; ) { 2426 dest[dest_index++] = hash_buf[hash_index++]; 2427 } 2428 2429 // Add the semicolon and the NULL 2430 dest[dest_index++] = ';'; 2431 dest[dest_index] = '\0'; 2432 return dest; 2433 } 2434 2435 // different verisons of is_same_class_package 2436 bool InstanceKlass::is_same_class_package(Klass* class2) { 2437 Klass* class1 = this; 2438 oop classloader1 = InstanceKlass::cast(class1)->class_loader(); 2439 Symbol* classname1 = class1->name(); 2440 2441 if (class2->oop_is_objArray()) { 2442 class2 = ObjArrayKlass::cast(class2)->bottom_klass(); 2443 } 2444 oop classloader2; 2445 if (class2->oop_is_instance()) { 2446 classloader2 = InstanceKlass::cast(class2)->class_loader(); 2447 } else { 2448 assert(class2->oop_is_typeArray(), "should be type array"); 2449 classloader2 = NULL; 2450 } 2451 Symbol* classname2 = class2->name(); 2452 2453 return InstanceKlass::is_same_class_package(classloader1, classname1, 2454 classloader2, classname2); 2455 } 2456 2457 bool InstanceKlass::is_same_class_package(oop classloader2, Symbol* classname2) { 2458 Klass* class1 = this; 2459 oop classloader1 = InstanceKlass::cast(class1)->class_loader(); 2460 Symbol* classname1 = class1->name(); 2461 2462 return InstanceKlass::is_same_class_package(classloader1, classname1, 2463 classloader2, classname2); 2464 } 2465 2466 // return true if two classes are in the same package, classloader 2467 // and classname information is enough to determine a class's package 2468 bool InstanceKlass::is_same_class_package(oop class_loader1, Symbol* class_name1, 2469 oop class_loader2, Symbol* class_name2) { 2470 if (class_loader1 != class_loader2) { 2471 return false; 2472 } else if (class_name1 == class_name2) { 2473 return true; // skip painful bytewise comparison 2474 } else { 2475 ResourceMark rm; 2476 2477 // The Symbol*'s are in UTF8 encoding. Since we only need to check explicitly 2478 // for ASCII characters ('/', 'L', '['), we can keep them in UTF8 encoding. 2479 // Otherwise, we just compare jbyte values between the strings. 2480 const jbyte *name1 = class_name1->base(); 2481 const jbyte *name2 = class_name2->base(); 2482 2483 const jbyte *last_slash1 = UTF8::strrchr(name1, class_name1->utf8_length(), '/'); 2484 const jbyte *last_slash2 = UTF8::strrchr(name2, class_name2->utf8_length(), '/'); 2485 2486 if ((last_slash1 == NULL) || (last_slash2 == NULL)) { 2487 // One of the two doesn't have a package. Only return true 2488 // if the other one also doesn't have a package. 2489 return last_slash1 == last_slash2; 2490 } else { 2491 // Skip over '['s 2492 if (*name1 == '[') { 2493 do { 2494 name1++; 2495 } while (*name1 == '['); 2496 if (*name1 != 'L') { 2497 // Something is terribly wrong. Shouldn't be here. 2498 return false; 2499 } 2500 } 2501 if (*name2 == '[') { 2502 do { 2503 name2++; 2504 } while (*name2 == '['); 2505 if (*name2 != 'L') { 2506 // Something is terribly wrong. Shouldn't be here. 2507 return false; 2508 } 2509 } 2510 2511 // Check that package part is identical 2512 int length1 = last_slash1 - name1; 2513 int length2 = last_slash2 - name2; 2514 2515 return UTF8::equal(name1, length1, name2, length2); 2516 } 2517 } 2518 } 2519 2520 // Returns true iff super_method can be overridden by a method in targetclassname 2521 // See JSL 3rd edition 8.4.6.1 2522 // Assumes name-signature match 2523 // "this" is InstanceKlass of super_method which must exist 2524 // note that the InstanceKlass of the method in the targetclassname has not always been created yet 2525 bool InstanceKlass::is_override(methodHandle super_method, Handle targetclassloader, Symbol* targetclassname, TRAPS) { 2526 // Private methods can not be overridden 2527 if (super_method->is_private()) { 2528 return false; 2529 } 2530 // If super method is accessible, then override 2531 if ((super_method->is_protected()) || 2532 (super_method->is_public())) { 2533 return true; 2534 } 2535 // Package-private methods are not inherited outside of package 2536 assert(super_method->is_package_private(), "must be package private"); 2537 return(is_same_class_package(targetclassloader(), targetclassname)); 2538 } 2539 2540 /* defined for now in jvm.cpp, for historical reasons *-- 2541 Klass* InstanceKlass::compute_enclosing_class_impl(instanceKlassHandle self, 2542 Symbol*& simple_name_result, TRAPS) { 2543 ... 2544 } 2545 */ 2546 2547 // tell if two classes have the same enclosing class (at package level) 2548 bool InstanceKlass::is_same_package_member_impl(instanceKlassHandle class1, 2549 Klass* class2_oop, TRAPS) { 2550 if (class2_oop == class1()) return true; 2551 if (!class2_oop->oop_is_instance()) return false; 2552 instanceKlassHandle class2(THREAD, class2_oop); 2553 2554 // must be in same package before we try anything else 2555 if (!class1->is_same_class_package(class2->class_loader(), class2->name())) 2556 return false; 2557 2558 // As long as there is an outer1.getEnclosingClass, 2559 // shift the search outward. 2560 instanceKlassHandle outer1 = class1; 2561 for (;;) { 2562 // As we walk along, look for equalities between outer1 and class2. 2563 // Eventually, the walks will terminate as outer1 stops 2564 // at the top-level class around the original class. 2565 bool ignore_inner_is_member; 2566 Klass* next = outer1->compute_enclosing_class(&ignore_inner_is_member, 2567 CHECK_false); 2568 if (next == NULL) break; 2569 if (next == class2()) return true; 2570 outer1 = instanceKlassHandle(THREAD, next); 2571 } 2572 2573 // Now do the same for class2. 2574 instanceKlassHandle outer2 = class2; 2575 for (;;) { 2576 bool ignore_inner_is_member; 2577 Klass* next = outer2->compute_enclosing_class(&ignore_inner_is_member, 2578 CHECK_false); 2579 if (next == NULL) break; 2580 // Might as well check the new outer against all available values. 2581 if (next == class1()) return true; 2582 if (next == outer1()) return true; 2583 outer2 = instanceKlassHandle(THREAD, next); 2584 } 2585 2586 // If by this point we have not found an equality between the 2587 // two classes, we know they are in separate package members. 2588 return false; 2589 } 2590 2591 2592 jint InstanceKlass::compute_modifier_flags(TRAPS) const { 2593 jint access = access_flags().as_int(); 2594 2595 // But check if it happens to be member class. 2596 instanceKlassHandle ik(THREAD, this); 2597 InnerClassesIterator iter(ik); 2598 for (; !iter.done(); iter.next()) { 2599 int ioff = iter.inner_class_info_index(); 2600 // Inner class attribute can be zero, skip it. 2601 // Strange but true: JVM spec. allows null inner class refs. 2602 if (ioff == 0) continue; 2603 2604 // only look at classes that are already loaded 2605 // since we are looking for the flags for our self. 2606 Symbol* inner_name = ik->constants()->klass_name_at(ioff); 2607 if ((ik->name() == inner_name)) { 2608 // This is really a member class. 2609 access = iter.inner_access_flags(); 2610 break; 2611 } 2612 } 2613 // Remember to strip ACC_SUPER bit 2614 return (access & (~JVM_ACC_SUPER)) & JVM_ACC_WRITTEN_FLAGS; 2615 } 2616 2617 jint InstanceKlass::jvmti_class_status() const { 2618 jint result = 0; 2619 2620 if (is_linked()) { 2621 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED; 2622 } 2623 2624 if (is_initialized()) { 2625 assert(is_linked(), "Class status is not consistent"); 2626 result |= JVMTI_CLASS_STATUS_INITIALIZED; 2627 } 2628 if (is_in_error_state()) { 2629 result |= JVMTI_CLASS_STATUS_ERROR; 2630 } 2631 return result; 2632 } 2633 2634 Method* InstanceKlass::method_at_itable(Klass* holder, int index, TRAPS) { 2635 itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable(); 2636 int method_table_offset_in_words = ioe->offset()/wordSize; 2637 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words()) 2638 / itableOffsetEntry::size(); 2639 2640 for (int cnt = 0 ; ; cnt ++, ioe ++) { 2641 // If the interface isn't implemented by the receiver class, 2642 // the VM should throw IncompatibleClassChangeError. 2643 if (cnt >= nof_interfaces) { 2644 THROW_NULL(vmSymbols::java_lang_IncompatibleClassChangeError()); 2645 } 2646 2647 Klass* ik = ioe->interface_klass(); 2648 if (ik == holder) break; 2649 } 2650 2651 itableMethodEntry* ime = ioe->first_method_entry(this); 2652 Method* m = ime[index].method(); 2653 if (m == NULL) { 2654 THROW_NULL(vmSymbols::java_lang_AbstractMethodError()); 2655 } 2656 return m; 2657 } 2658 2659 2660 #if INCLUDE_JVMTI 2661 // update default_methods for redefineclasses for methods that are 2662 // not yet in the vtable due to concurrent subclass define and superinterface 2663 // redefinition 2664 // Note: those in the vtable, should have been updated via adjust_method_entries 2665 void InstanceKlass::adjust_default_methods(Method** old_methods, Method** new_methods, 2666 int methods_length, bool* trace_name_printed) { 2667 // search the default_methods for uses of either obsolete or EMCP methods 2668 if (default_methods() != NULL) { 2669 for (int j = 0; j < methods_length; j++) { 2670 Method* old_method = old_methods[j]; 2671 Method* new_method = new_methods[j]; 2672 2673 for (int index = 0; index < default_methods()->length(); index ++) { 2674 if (default_methods()->at(index) == old_method) { 2675 default_methods()->at_put(index, new_method); 2676 if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) { 2677 if (!(*trace_name_printed)) { 2678 // RC_TRACE_MESG macro has an embedded ResourceMark 2679 RC_TRACE_MESG(("adjust: klassname=%s default methods from name=%s", 2680 external_name(), 2681 old_method->method_holder()->external_name())); 2682 *trace_name_printed = true; 2683 } 2684 RC_TRACE(0x00100000, ("default method update: %s(%s) ", 2685 new_method->name()->as_C_string(), 2686 new_method->signature()->as_C_string())); 2687 } 2688 } 2689 } 2690 } 2691 } 2692 } 2693 #endif // INCLUDE_JVMTI 2694 2695 // On-stack replacement stuff 2696 void InstanceKlass::add_osr_nmethod(nmethod* n) { 2697 // only one compilation can be active 2698 NEEDS_CLEANUP 2699 // This is a short non-blocking critical region, so the no safepoint check is ok. 2700 OsrList_lock->lock_without_safepoint_check(); 2701 assert(n->is_osr_method(), "wrong kind of nmethod"); 2702 n->set_osr_link(osr_nmethods_head()); 2703 set_osr_nmethods_head(n); 2704 // Raise the highest osr level if necessary 2705 if (TieredCompilation) { 2706 Method* m = n->method(); 2707 m->set_highest_osr_comp_level(MAX2(m->highest_osr_comp_level(), n->comp_level())); 2708 } 2709 // Remember to unlock again 2710 OsrList_lock->unlock(); 2711 2712 // Get rid of the osr methods for the same bci that have lower levels. 2713 if (TieredCompilation) { 2714 for (int l = CompLevel_limited_profile; l < n->comp_level(); l++) { 2715 nmethod *inv = lookup_osr_nmethod(n->method(), n->osr_entry_bci(), l, true); 2716 if (inv != NULL && inv->is_in_use()) { 2717 inv->make_not_entrant(); 2718 } 2719 } 2720 } 2721 } 2722 2723 2724 void InstanceKlass::remove_osr_nmethod(nmethod* n) { 2725 // This is a short non-blocking critical region, so the no safepoint check is ok. 2726 OsrList_lock->lock_without_safepoint_check(); 2727 assert(n->is_osr_method(), "wrong kind of nmethod"); 2728 nmethod* last = NULL; 2729 nmethod* cur = osr_nmethods_head(); 2730 int max_level = CompLevel_none; // Find the max comp level excluding n 2731 Method* m = n->method(); 2732 // Search for match 2733 while(cur != NULL && cur != n) { 2734 if (TieredCompilation && m == cur->method()) { 2735 // Find max level before n 2736 max_level = MAX2(max_level, cur->comp_level()); 2737 } 2738 last = cur; 2739 cur = cur->osr_link(); 2740 } 2741 nmethod* next = NULL; 2742 if (cur == n) { 2743 next = cur->osr_link(); 2744 if (last == NULL) { 2745 // Remove first element 2746 set_osr_nmethods_head(next); 2747 } else { 2748 last->set_osr_link(next); 2749 } 2750 } 2751 n->set_osr_link(NULL); 2752 if (TieredCompilation) { 2753 cur = next; 2754 while (cur != NULL) { 2755 // Find max level after n 2756 if (m == cur->method()) { 2757 max_level = MAX2(max_level, cur->comp_level()); 2758 } 2759 cur = cur->osr_link(); 2760 } 2761 m->set_highest_osr_comp_level(max_level); 2762 } 2763 // Remember to unlock again 2764 OsrList_lock->unlock(); 2765 } 2766 2767 nmethod* InstanceKlass::lookup_osr_nmethod(const Method* m, int bci, int comp_level, bool match_level) const { 2768 // This is a short non-blocking critical region, so the no safepoint check is ok. 2769 OsrList_lock->lock_without_safepoint_check(); 2770 nmethod* osr = osr_nmethods_head(); 2771 nmethod* best = NULL; 2772 while (osr != NULL) { 2773 assert(osr->is_osr_method(), "wrong kind of nmethod found in chain"); 2774 // There can be a time when a c1 osr method exists but we are waiting 2775 // for a c2 version. When c2 completes its osr nmethod we will trash 2776 // the c1 version and only be able to find the c2 version. However 2777 // while we overflow in the c1 code at back branches we don't want to 2778 // try and switch to the same code as we are already running 2779 2780 if (osr->method() == m && 2781 (bci == InvocationEntryBci || osr->osr_entry_bci() == bci)) { 2782 if (match_level) { 2783 if (osr->comp_level() == comp_level) { 2784 // Found a match - return it. 2785 OsrList_lock->unlock(); 2786 return osr; 2787 } 2788 } else { 2789 if (best == NULL || (osr->comp_level() > best->comp_level())) { 2790 if (osr->comp_level() == CompLevel_highest_tier) { 2791 // Found the best possible - return it. 2792 OsrList_lock->unlock(); 2793 return osr; 2794 } 2795 best = osr; 2796 } 2797 } 2798 } 2799 osr = osr->osr_link(); 2800 } 2801 OsrList_lock->unlock(); 2802 if (best != NULL && best->comp_level() >= comp_level && match_level == false) { 2803 return best; 2804 } 2805 return NULL; 2806 } 2807 2808 void InstanceKlass::add_member_name(int index, Handle mem_name) { 2809 jweak mem_name_wref = JNIHandles::make_weak_global(mem_name); 2810 MutexLocker ml(MemberNameTable_lock); 2811 assert(0 <= index && index < idnum_allocated_count(), "index is out of bounds"); 2812 DEBUG_ONLY(No_Safepoint_Verifier nsv); 2813 2814 if (_member_names == NULL) { 2815 _member_names = new (ResourceObj::C_HEAP, mtClass) MemberNameTable(idnum_allocated_count()); 2816 } 2817 _member_names->add_member_name(index, mem_name_wref); 2818 } 2819 2820 oop InstanceKlass::get_member_name(int index) { 2821 MutexLocker ml(MemberNameTable_lock); 2822 assert(0 <= index && index < idnum_allocated_count(), "index is out of bounds"); 2823 DEBUG_ONLY(No_Safepoint_Verifier nsv); 2824 2825 if (_member_names == NULL) { 2826 return NULL; 2827 } 2828 oop mem_name =_member_names->get_member_name(index); 2829 return mem_name; 2830 } 2831 2832 // ----------------------------------------------------------------------------------------------------- 2833 // Printing 2834 2835 #ifndef PRODUCT 2836 2837 #define BULLET " - " 2838 2839 static const char* state_names[] = { 2840 "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error" 2841 }; 2842 2843 static void print_vtable(intptr_t* start, int len, outputStream* st) { 2844 for (int i = 0; i < len; i++) { 2845 intptr_t e = start[i]; 2846 st->print("%d : " INTPTR_FORMAT, i, e); 2847 if (e != 0 && ((Metadata*)e)->is_metaspace_object()) { 2848 st->print(" "); 2849 ((Metadata*)e)->print_value_on(st); 2850 } 2851 st->cr(); 2852 } 2853 } 2854 2855 void InstanceKlass::print_on(outputStream* st) const { 2856 assert(is_klass(), "must be klass"); 2857 Klass::print_on(st); 2858 2859 st->print(BULLET"instance size: %d", size_helper()); st->cr(); 2860 st->print(BULLET"klass size: %d", size()); st->cr(); 2861 st->print(BULLET"access: "); access_flags().print_on(st); st->cr(); 2862 st->print(BULLET"state: "); st->print_cr(state_names[_init_state]); 2863 st->print(BULLET"name: "); name()->print_value_on(st); st->cr(); 2864 st->print(BULLET"super: "); super()->print_value_on_maybe_null(st); st->cr(); 2865 st->print(BULLET"sub: "); 2866 Klass* sub = subklass(); 2867 int n; 2868 for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) { 2869 if (n < MaxSubklassPrintSize) { 2870 sub->print_value_on(st); 2871 st->print(" "); 2872 } 2873 } 2874 if (n >= MaxSubklassPrintSize) st->print("(%d more klasses...)", n - MaxSubklassPrintSize); 2875 st->cr(); 2876 2877 if (is_interface()) { 2878 st->print_cr(BULLET"nof implementors: %d", nof_implementors()); 2879 if (nof_implementors() == 1) { 2880 st->print_cr(BULLET"implementor: "); 2881 st->print(" "); 2882 implementor()->print_value_on(st); 2883 st->cr(); 2884 } 2885 } 2886 2887 st->print(BULLET"arrays: "); array_klasses()->print_value_on_maybe_null(st); st->cr(); 2888 st->print(BULLET"methods: "); methods()->print_value_on(st); st->cr(); 2889 if (Verbose || WizardMode) { 2890 Array<Method*>* method_array = methods(); 2891 for (int i = 0; i < method_array->length(); i++) { 2892 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr(); 2893 } 2894 } 2895 st->print(BULLET"method ordering: "); method_ordering()->print_value_on(st); st->cr(); 2896 st->print(BULLET"default_methods: "); default_methods()->print_value_on(st); st->cr(); 2897 if (Verbose && default_methods() != NULL) { 2898 Array<Method*>* method_array = default_methods(); 2899 for (int i = 0; i < method_array->length(); i++) { 2900 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr(); 2901 } 2902 } 2903 if (default_vtable_indices() != NULL) { 2904 st->print(BULLET"default vtable indices: "); default_vtable_indices()->print_value_on(st); st->cr(); 2905 } 2906 st->print(BULLET"local interfaces: "); local_interfaces()->print_value_on(st); st->cr(); 2907 st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr(); 2908 st->print(BULLET"constants: "); constants()->print_value_on(st); st->cr(); 2909 if (class_loader_data() != NULL) { 2910 st->print(BULLET"class loader data: "); 2911 class_loader_data()->print_value_on(st); 2912 st->cr(); 2913 } 2914 st->print(BULLET"host class: "); host_klass()->print_value_on_maybe_null(st); st->cr(); 2915 if (source_file_name() != NULL) { 2916 st->print(BULLET"source file: "); 2917 source_file_name()->print_value_on(st); 2918 st->cr(); 2919 } 2920 if (source_debug_extension() != NULL) { 2921 st->print(BULLET"source debug extension: "); 2922 st->print("%s", source_debug_extension()); 2923 st->cr(); 2924 } 2925 st->print(BULLET"class annotations: "); class_annotations()->print_value_on(st); st->cr(); 2926 st->print(BULLET"class type annotations: "); class_type_annotations()->print_value_on(st); st->cr(); 2927 st->print(BULLET"field annotations: "); fields_annotations()->print_value_on(st); st->cr(); 2928 st->print(BULLET"field type annotations: "); fields_type_annotations()->print_value_on(st); st->cr(); 2929 { 2930 bool have_pv = false; 2931 PreviousVersionWalker pvw(Thread::current(), (InstanceKlass*)this); 2932 for (PreviousVersionNode * pv_node = pvw.next_previous_version(); 2933 pv_node != NULL; pv_node = pvw.next_previous_version()) { 2934 if (!have_pv) 2935 st->print(BULLET"previous version: "); 2936 have_pv = true; 2937 pv_node->prev_constant_pool()->print_value_on(st); 2938 } 2939 if (have_pv) st->cr(); 2940 } // pvw is cleaned up 2941 2942 if (generic_signature() != NULL) { 2943 st->print(BULLET"generic signature: "); 2944 generic_signature()->print_value_on(st); 2945 st->cr(); 2946 } 2947 st->print(BULLET"inner classes: "); inner_classes()->print_value_on(st); st->cr(); 2948 st->print(BULLET"java mirror: "); java_mirror()->print_value_on(st); st->cr(); 2949 st->print(BULLET"vtable length %d (start addr: " INTPTR_FORMAT ")", vtable_length(), start_of_vtable()); st->cr(); 2950 if (vtable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_vtable(), vtable_length(), st); 2951 st->print(BULLET"itable length %d (start addr: " INTPTR_FORMAT ")", itable_length(), start_of_itable()); st->cr(); 2952 if (itable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_itable(), itable_length(), st); 2953 st->print_cr(BULLET"---- static fields (%d words):", static_field_size()); 2954 FieldPrinter print_static_field(st); 2955 ((InstanceKlass*)this)->do_local_static_fields(&print_static_field); 2956 st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size()); 2957 FieldPrinter print_nonstatic_field(st); 2958 ((InstanceKlass*)this)->do_nonstatic_fields(&print_nonstatic_field); 2959 2960 st->print(BULLET"non-static oop maps: "); 2961 OopMapBlock* map = start_of_nonstatic_oop_maps(); 2962 OopMapBlock* end_map = map + nonstatic_oop_map_count(); 2963 while (map < end_map) { 2964 st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1)); 2965 map++; 2966 } 2967 st->cr(); 2968 } 2969 2970 #endif //PRODUCT 2971 2972 void InstanceKlass::print_value_on(outputStream* st) const { 2973 assert(is_klass(), "must be klass"); 2974 if (Verbose || WizardMode) access_flags().print_on(st); 2975 name()->print_value_on(st); 2976 } 2977 2978 #ifndef PRODUCT 2979 2980 void FieldPrinter::do_field(fieldDescriptor* fd) { 2981 _st->print(BULLET); 2982 if (_obj == NULL) { 2983 fd->print_on(_st); 2984 _st->cr(); 2985 } else { 2986 fd->print_on_for(_st, _obj); 2987 _st->cr(); 2988 } 2989 } 2990 2991 2992 void InstanceKlass::oop_print_on(oop obj, outputStream* st) { 2993 Klass::oop_print_on(obj, st); 2994 2995 if (this == SystemDictionary::String_klass()) { 2996 typeArrayOop value = java_lang_String::value(obj); 2997 juint offset = java_lang_String::offset(obj); 2998 juint length = java_lang_String::length(obj); 2999 if (value != NULL && 3000 value->is_typeArray() && 3001 offset <= (juint) value->length() && 3002 offset + length <= (juint) value->length()) { 3003 st->print(BULLET"string: "); 3004 java_lang_String::print(obj, st); 3005 st->cr(); 3006 if (!WizardMode) return; // that is enough 3007 } 3008 } 3009 3010 st->print_cr(BULLET"---- fields (total size %d words):", oop_size(obj)); 3011 FieldPrinter print_field(st, obj); 3012 do_nonstatic_fields(&print_field); 3013 3014 if (this == SystemDictionary::Class_klass()) { 3015 st->print(BULLET"signature: "); 3016 java_lang_Class::print_signature(obj, st); 3017 st->cr(); 3018 Klass* mirrored_klass = java_lang_Class::as_Klass(obj); 3019 st->print(BULLET"fake entry for mirror: "); 3020 mirrored_klass->print_value_on_maybe_null(st); 3021 st->cr(); 3022 Klass* array_klass = java_lang_Class::array_klass(obj); 3023 st->print(BULLET"fake entry for array: "); 3024 array_klass->print_value_on_maybe_null(st); 3025 st->cr(); 3026 st->print_cr(BULLET"fake entry for oop_size: %d", java_lang_Class::oop_size(obj)); 3027 st->print_cr(BULLET"fake entry for static_oop_field_count: %d", java_lang_Class::static_oop_field_count(obj)); 3028 Klass* real_klass = java_lang_Class::as_Klass(obj); 3029 if (real_klass != NULL && real_klass->oop_is_instance()) { 3030 InstanceKlass::cast(real_klass)->do_local_static_fields(&print_field); 3031 } 3032 } else if (this == SystemDictionary::MethodType_klass()) { 3033 st->print(BULLET"signature: "); 3034 java_lang_invoke_MethodType::print_signature(obj, st); 3035 st->cr(); 3036 } 3037 } 3038 3039 #endif //PRODUCT 3040 3041 void InstanceKlass::oop_print_value_on(oop obj, outputStream* st) { 3042 st->print("a "); 3043 name()->print_value_on(st); 3044 obj->print_address_on(st); 3045 if (this == SystemDictionary::String_klass() 3046 && java_lang_String::value(obj) != NULL) { 3047 ResourceMark rm; 3048 int len = java_lang_String::length(obj); 3049 int plen = (len < 24 ? len : 12); 3050 char* str = java_lang_String::as_utf8_string(obj, 0, plen); 3051 st->print(" = \"%s\"", str); 3052 if (len > plen) 3053 st->print("...[%d]", len); 3054 } else if (this == SystemDictionary::Class_klass()) { 3055 Klass* k = java_lang_Class::as_Klass(obj); 3056 st->print(" = "); 3057 if (k != NULL) { 3058 k->print_value_on(st); 3059 } else { 3060 const char* tname = type2name(java_lang_Class::primitive_type(obj)); 3061 st->print("%s", tname ? tname : "type?"); 3062 } 3063 } else if (this == SystemDictionary::MethodType_klass()) { 3064 st->print(" = "); 3065 java_lang_invoke_MethodType::print_signature(obj, st); 3066 } else if (java_lang_boxing_object::is_instance(obj)) { 3067 st->print(" = "); 3068 java_lang_boxing_object::print(obj, st); 3069 } else if (this == SystemDictionary::LambdaForm_klass()) { 3070 oop vmentry = java_lang_invoke_LambdaForm::vmentry(obj); 3071 if (vmentry != NULL) { 3072 st->print(" => "); 3073 vmentry->print_value_on(st); 3074 } 3075 } else if (this == SystemDictionary::MemberName_klass()) { 3076 Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(obj); 3077 if (vmtarget != NULL) { 3078 st->print(" = "); 3079 vmtarget->print_value_on(st); 3080 } else { 3081 java_lang_invoke_MemberName::clazz(obj)->print_value_on(st); 3082 st->print("."); 3083 java_lang_invoke_MemberName::name(obj)->print_value_on(st); 3084 } 3085 } 3086 } 3087 3088 const char* InstanceKlass::internal_name() const { 3089 return external_name(); 3090 } 3091 3092 #if INCLUDE_SERVICES 3093 // Size Statistics 3094 void InstanceKlass::collect_statistics(KlassSizeStats *sz) const { 3095 Klass::collect_statistics(sz); 3096 3097 sz->_inst_size = HeapWordSize * size_helper(); 3098 sz->_vtab_bytes = HeapWordSize * align_object_offset(vtable_length()); 3099 sz->_itab_bytes = HeapWordSize * align_object_offset(itable_length()); 3100 sz->_nonstatic_oopmap_bytes = HeapWordSize * 3101 ((is_interface() || is_anonymous()) ? 3102 align_object_offset(nonstatic_oop_map_size()) : 3103 nonstatic_oop_map_size()); 3104 3105 int n = 0; 3106 n += (sz->_methods_array_bytes = sz->count_array(methods())); 3107 n += (sz->_method_ordering_bytes = sz->count_array(method_ordering())); 3108 n += (sz->_local_interfaces_bytes = sz->count_array(local_interfaces())); 3109 n += (sz->_transitive_interfaces_bytes = sz->count_array(transitive_interfaces())); 3110 n += (sz->_fields_bytes = sz->count_array(fields())); 3111 n += (sz->_inner_classes_bytes = sz->count_array(inner_classes())); 3112 sz->_ro_bytes += n; 3113 3114 const ConstantPool* cp = constants(); 3115 if (cp) { 3116 cp->collect_statistics(sz); 3117 } 3118 3119 const Annotations* anno = annotations(); 3120 if (anno) { 3121 anno->collect_statistics(sz); 3122 } 3123 3124 const Array<Method*>* methods_array = methods(); 3125 if (methods()) { 3126 for (int i = 0; i < methods_array->length(); i++) { 3127 Method* method = methods_array->at(i); 3128 if (method) { 3129 sz->_method_count ++; 3130 method->collect_statistics(sz); 3131 } 3132 } 3133 } 3134 } 3135 #endif // INCLUDE_SERVICES 3136 3137 // Verification 3138 3139 class VerifyFieldClosure: public OopClosure { 3140 protected: 3141 template <class T> void do_oop_work(T* p) { 3142 oop obj = oopDesc::load_decode_heap_oop(p); 3143 if (!obj->is_oop_or_null()) { 3144 tty->print_cr("Failed: " PTR_FORMAT " -> " PTR_FORMAT, p, (address)obj); 3145 Universe::print(); 3146 guarantee(false, "boom"); 3147 } 3148 } 3149 public: 3150 virtual void do_oop(oop* p) { VerifyFieldClosure::do_oop_work(p); } 3151 virtual void do_oop(narrowOop* p) { VerifyFieldClosure::do_oop_work(p); } 3152 }; 3153 3154 void InstanceKlass::verify_on(outputStream* st) { 3155 #ifndef PRODUCT 3156 // Avoid redundant verifies, this really should be in product. 3157 if (_verify_count == Universe::verify_count()) return; 3158 _verify_count = Universe::verify_count(); 3159 #endif 3160 3161 // Verify Klass 3162 Klass::verify_on(st); 3163 3164 // Verify that klass is present in ClassLoaderData 3165 guarantee(class_loader_data()->contains_klass(this), 3166 "this class isn't found in class loader data"); 3167 3168 // Verify vtables 3169 if (is_linked()) { 3170 ResourceMark rm; 3171 // $$$ This used to be done only for m/s collections. Doing it 3172 // always seemed a valid generalization. (DLD -- 6/00) 3173 vtable()->verify(st); 3174 } 3175 3176 // Verify first subklass 3177 if (subklass() != NULL) { 3178 guarantee(subklass()->is_klass(), "should be klass"); 3179 } 3180 3181 // Verify siblings 3182 Klass* super = this->super(); 3183 Klass* sib = next_sibling(); 3184 if (sib != NULL) { 3185 if (sib == this) { 3186 fatal(err_msg("subclass points to itself " PTR_FORMAT, sib)); 3187 } 3188 3189 guarantee(sib->is_klass(), "should be klass"); 3190 guarantee(sib->super() == super, "siblings should have same superklass"); 3191 } 3192 3193 // Verify implementor fields 3194 Klass* im = implementor(); 3195 if (im != NULL) { 3196 guarantee(is_interface(), "only interfaces should have implementor set"); 3197 guarantee(im->is_klass(), "should be klass"); 3198 guarantee(!im->is_interface() || im == this, 3199 "implementors cannot be interfaces"); 3200 } 3201 3202 // Verify local interfaces 3203 if (local_interfaces()) { 3204 Array<Klass*>* local_interfaces = this->local_interfaces(); 3205 for (int j = 0; j < local_interfaces->length(); j++) { 3206 Klass* e = local_interfaces->at(j); 3207 guarantee(e->is_klass() && e->is_interface(), "invalid local interface"); 3208 } 3209 } 3210 3211 // Verify transitive interfaces 3212 if (transitive_interfaces() != NULL) { 3213 Array<Klass*>* transitive_interfaces = this->transitive_interfaces(); 3214 for (int j = 0; j < transitive_interfaces->length(); j++) { 3215 Klass* e = transitive_interfaces->at(j); 3216 guarantee(e->is_klass() && e->is_interface(), "invalid transitive interface"); 3217 } 3218 } 3219 3220 // Verify methods 3221 if (methods() != NULL) { 3222 Array<Method*>* methods = this->methods(); 3223 for (int j = 0; j < methods->length(); j++) { 3224 guarantee(methods->at(j)->is_method(), "non-method in methods array"); 3225 } 3226 for (int j = 0; j < methods->length() - 1; j++) { 3227 Method* m1 = methods->at(j); 3228 Method* m2 = methods->at(j + 1); 3229 guarantee(m1->name()->fast_compare(m2->name()) <= 0, "methods not sorted correctly"); 3230 } 3231 } 3232 3233 // Verify method ordering 3234 if (method_ordering() != NULL) { 3235 Array<int>* method_ordering = this->method_ordering(); 3236 int length = method_ordering->length(); 3237 if (JvmtiExport::can_maintain_original_method_order() || 3238 ((UseSharedSpaces || DumpSharedSpaces) && length != 0)) { 3239 guarantee(length == methods()->length(), "invalid method ordering length"); 3240 jlong sum = 0; 3241 for (int j = 0; j < length; j++) { 3242 int original_index = method_ordering->at(j); 3243 guarantee(original_index >= 0, "invalid method ordering index"); 3244 guarantee(original_index < length, "invalid method ordering index"); 3245 sum += original_index; 3246 } 3247 // Verify sum of indices 0,1,...,length-1 3248 guarantee(sum == ((jlong)length*(length-1))/2, "invalid method ordering sum"); 3249 } else { 3250 guarantee(length == 0, "invalid method ordering length"); 3251 } 3252 } 3253 3254 // Verify default methods 3255 if (default_methods() != NULL) { 3256 Array<Method*>* methods = this->default_methods(); 3257 for (int j = 0; j < methods->length(); j++) { 3258 guarantee(methods->at(j)->is_method(), "non-method in methods array"); 3259 } 3260 for (int j = 0; j < methods->length() - 1; j++) { 3261 Method* m1 = methods->at(j); 3262 Method* m2 = methods->at(j + 1); 3263 guarantee(m1->name()->fast_compare(m2->name()) <= 0, "methods not sorted correctly"); 3264 } 3265 } 3266 3267 // Verify JNI static field identifiers 3268 if (jni_ids() != NULL) { 3269 jni_ids()->verify(this); 3270 } 3271 3272 // Verify other fields 3273 if (array_klasses() != NULL) { 3274 guarantee(array_klasses()->is_klass(), "should be klass"); 3275 } 3276 if (constants() != NULL) { 3277 guarantee(constants()->is_constantPool(), "should be constant pool"); 3278 } 3279 const Klass* host = host_klass(); 3280 if (host != NULL) { 3281 guarantee(host->is_klass(), "should be klass"); 3282 } 3283 } 3284 3285 void InstanceKlass::oop_verify_on(oop obj, outputStream* st) { 3286 Klass::oop_verify_on(obj, st); 3287 VerifyFieldClosure blk; 3288 obj->oop_iterate_no_header(&blk); 3289 } 3290 3291 3292 // JNIid class for jfieldIDs only 3293 // Note to reviewers: 3294 // These JNI functions are just moved over to column 1 and not changed 3295 // in the compressed oops workspace. 3296 JNIid::JNIid(Klass* holder, int offset, JNIid* next) { 3297 _holder = holder; 3298 _offset = offset; 3299 _next = next; 3300 debug_only(_is_static_field_id = false;) 3301 } 3302 3303 3304 JNIid* JNIid::find(int offset) { 3305 JNIid* current = this; 3306 while (current != NULL) { 3307 if (current->offset() == offset) return current; 3308 current = current->next(); 3309 } 3310 return NULL; 3311 } 3312 3313 void JNIid::deallocate(JNIid* current) { 3314 while (current != NULL) { 3315 JNIid* next = current->next(); 3316 delete current; 3317 current = next; 3318 } 3319 } 3320 3321 3322 void JNIid::verify(Klass* holder) { 3323 int first_field_offset = InstanceMirrorKlass::offset_of_static_fields(); 3324 int end_field_offset; 3325 end_field_offset = first_field_offset + (InstanceKlass::cast(holder)->static_field_size() * wordSize); 3326 3327 JNIid* current = this; 3328 while (current != NULL) { 3329 guarantee(current->holder() == holder, "Invalid klass in JNIid"); 3330 #ifdef ASSERT 3331 int o = current->offset(); 3332 if (current->is_static_field_id()) { 3333 guarantee(o >= first_field_offset && o < end_field_offset, "Invalid static field offset in JNIid"); 3334 } 3335 #endif 3336 current = current->next(); 3337 } 3338 } 3339 3340 3341 #ifdef ASSERT 3342 void InstanceKlass::set_init_state(ClassState state) { 3343 bool good_state = is_shared() ? (_init_state <= state) 3344 : (_init_state < state); 3345 assert(good_state || state == allocated, "illegal state transition"); 3346 _init_state = (u1)state; 3347 } 3348 #endif 3349 3350 3351 // RedefineClasses() support for previous versions: 3352 3353 // Purge previous versions 3354 static void purge_previous_versions_internal(InstanceKlass* ik, int emcp_method_count) { 3355 if (ik->previous_versions() != NULL) { 3356 // This klass has previous versions so see what we can cleanup 3357 // while it is safe to do so. 3358 3359 int deleted_count = 0; // leave debugging breadcrumbs 3360 int live_count = 0; 3361 ClassLoaderData* loader_data = ik->class_loader_data() == NULL ? 3362 ClassLoaderData::the_null_class_loader_data() : 3363 ik->class_loader_data(); 3364 3365 // RC_TRACE macro has an embedded ResourceMark 3366 RC_TRACE(0x00000200, ("purge: %s: previous version length=%d", 3367 ik->external_name(), ik->previous_versions()->length())); 3368 3369 for (int i = ik->previous_versions()->length() - 1; i >= 0; i--) { 3370 // check the previous versions array 3371 PreviousVersionNode * pv_node = ik->previous_versions()->at(i); 3372 ConstantPool* cp_ref = pv_node->prev_constant_pool(); 3373 assert(cp_ref != NULL, "cp ref was unexpectedly cleared"); 3374 3375 ConstantPool* pvcp = cp_ref; 3376 if (!pvcp->on_stack()) { 3377 // If the constant pool isn't on stack, none of the methods 3378 // are executing. Delete all the methods, the constant pool and 3379 // and this previous version node. 3380 GrowableArray<Method*>* method_refs = pv_node->prev_EMCP_methods(); 3381 if (method_refs != NULL) { 3382 for (int j = method_refs->length() - 1; j >= 0; j--) { 3383 Method* method = method_refs->at(j); 3384 assert(method != NULL, "method ref was unexpectedly cleared"); 3385 method_refs->remove_at(j); 3386 // method will be freed with associated class. 3387 } 3388 } 3389 // Remove the constant pool 3390 delete pv_node; 3391 // Since we are traversing the array backwards, we don't have to 3392 // do anything special with the index. 3393 ik->previous_versions()->remove_at(i); 3394 deleted_count++; 3395 continue; 3396 } else { 3397 RC_TRACE(0x00000200, ("purge: previous version @%d is alive", i)); 3398 assert(pvcp->pool_holder() != NULL, "Constant pool with no holder"); 3399 guarantee (!loader_data->is_unloading(), "unloaded classes can't be on the stack"); 3400 live_count++; 3401 } 3402 3403 // At least one method is live in this previous version, clean out 3404 // the others or mark them as obsolete. 3405 GrowableArray<Method*>* method_refs = pv_node->prev_EMCP_methods(); 3406 if (method_refs != NULL) { 3407 RC_TRACE(0x00000200, ("purge: previous methods length=%d", 3408 method_refs->length())); 3409 for (int j = method_refs->length() - 1; j >= 0; j--) { 3410 Method* method = method_refs->at(j); 3411 assert(method != NULL, "method ref was unexpectedly cleared"); 3412 3413 // Remove the emcp method if it's not executing 3414 // If it's been made obsolete by a redefinition of a non-emcp 3415 // method, mark it as obsolete but leave it to clean up later. 3416 if (!method->on_stack()) { 3417 method_refs->remove_at(j); 3418 } else if (emcp_method_count == 0) { 3419 method->set_is_obsolete(); 3420 } else { 3421 // RC_TRACE macro has an embedded ResourceMark 3422 RC_TRACE(0x00000200, 3423 ("purge: %s(%s): prev method @%d in version @%d is alive", 3424 method->name()->as_C_string(), 3425 method->signature()->as_C_string(), j, i)); 3426 if (method->method_data() != NULL) { 3427 // Clean out any weak method links 3428 method->method_data()->clean_weak_method_links(); 3429 } 3430 } 3431 } 3432 } 3433 } 3434 assert(ik->previous_versions()->length() == live_count, "sanity check"); 3435 RC_TRACE(0x00000200, 3436 ("purge: previous version stats: live=%d, deleted=%d", live_count, 3437 deleted_count)); 3438 } 3439 3440 Array<Method*>* methods = ik->methods(); 3441 int num_methods = methods->length(); 3442 for (int index2 = 0; index2 < num_methods; ++index2) { 3443 if (methods->at(index2)->method_data() != NULL) { 3444 methods->at(index2)->method_data()->clean_weak_method_links(); 3445 } 3446 } 3447 } 3448 3449 // External interface for use during class unloading. 3450 void InstanceKlass::purge_previous_versions(InstanceKlass* ik) { 3451 // Call with >0 emcp methods since they are not currently being redefined. 3452 purge_previous_versions_internal(ik, 1); 3453 } 3454 3455 3456 // Potentially add an information node that contains pointers to the 3457 // interesting parts of the previous version of the_class. 3458 // This is also where we clean out any unused references. 3459 // Note that while we delete nodes from the _previous_versions 3460 // array, we never delete the array itself until the klass is 3461 // unloaded. The has_been_redefined() query depends on that fact. 3462 // 3463 void InstanceKlass::add_previous_version(instanceKlassHandle ikh, 3464 BitMap* emcp_methods, int emcp_method_count) { 3465 assert(Thread::current()->is_VM_thread(), 3466 "only VMThread can add previous versions"); 3467 3468 if (_previous_versions == NULL) { 3469 // This is the first previous version so make some space. 3470 // Start with 2 elements under the assumption that the class 3471 // won't be redefined much. 3472 _previous_versions = new (ResourceObj::C_HEAP, mtClass) 3473 GrowableArray<PreviousVersionNode *>(2, true); 3474 } 3475 3476 ConstantPool* cp_ref = ikh->constants(); 3477 3478 // RC_TRACE macro has an embedded ResourceMark 3479 RC_TRACE(0x00000400, ("adding previous version ref for %s @%d, EMCP_cnt=%d " 3480 "on_stack=%d", 3481 ikh->external_name(), _previous_versions->length(), emcp_method_count, 3482 cp_ref->on_stack())); 3483 3484 // If the constant pool for this previous version of the class 3485 // is not marked as being on the stack, then none of the methods 3486 // in this previous version of the class are on the stack so 3487 // we don't need to create a new PreviousVersionNode. However, 3488 // we still need to examine older previous versions below. 3489 Array<Method*>* old_methods = ikh->methods(); 3490 3491 if (cp_ref->on_stack()) { 3492 PreviousVersionNode * pv_node = NULL; 3493 if (emcp_method_count == 0) { 3494 // non-shared ConstantPool gets a reference 3495 pv_node = new PreviousVersionNode(cp_ref, NULL); 3496 RC_TRACE(0x00000400, 3497 ("add: all methods are obsolete; flushing any EMCP refs")); 3498 } else { 3499 int local_count = 0; 3500 GrowableArray<Method*>* method_refs = new (ResourceObj::C_HEAP, mtClass) 3501 GrowableArray<Method*>(emcp_method_count, true); 3502 for (int i = 0; i < old_methods->length(); i++) { 3503 if (emcp_methods->at(i)) { 3504 // this old method is EMCP. Save it only if it's on the stack 3505 Method* old_method = old_methods->at(i); 3506 if (old_method->on_stack()) { 3507 method_refs->append(old_method); 3508 } 3509 if (++local_count >= emcp_method_count) { 3510 // no more EMCP methods so bail out now 3511 break; 3512 } 3513 } 3514 } 3515 // non-shared ConstantPool gets a reference 3516 pv_node = new PreviousVersionNode(cp_ref, method_refs); 3517 } 3518 // append new previous version. 3519 _previous_versions->append(pv_node); 3520 } 3521 3522 // Since the caller is the VMThread and we are at a safepoint, this 3523 // is a good time to clear out unused references. 3524 3525 RC_TRACE(0x00000400, ("add: previous version length=%d", 3526 _previous_versions->length())); 3527 3528 // Purge previous versions not executing on the stack 3529 purge_previous_versions_internal(this, emcp_method_count); 3530 3531 int obsolete_method_count = old_methods->length() - emcp_method_count; 3532 3533 if (emcp_method_count != 0 && obsolete_method_count != 0 && 3534 _previous_versions->length() > 0) { 3535 // We have a mix of obsolete and EMCP methods so we have to 3536 // clear out any matching EMCP method entries the hard way. 3537 int local_count = 0; 3538 for (int i = 0; i < old_methods->length(); i++) { 3539 if (!emcp_methods->at(i)) { 3540 // only obsolete methods are interesting 3541 Method* old_method = old_methods->at(i); 3542 Symbol* m_name = old_method->name(); 3543 Symbol* m_signature = old_method->signature(); 3544 3545 // we might not have added the last entry 3546 for (int j = _previous_versions->length() - 1; j >= 0; j--) { 3547 // check the previous versions array for non executing obsolete methods 3548 PreviousVersionNode * pv_node = _previous_versions->at(j); 3549 3550 GrowableArray<Method*>* method_refs = pv_node->prev_EMCP_methods(); 3551 if (method_refs == NULL) { 3552 // We have run into a PreviousVersion generation where 3553 // all methods were made obsolete during that generation's 3554 // RedefineClasses() operation. At the time of that 3555 // operation, all EMCP methods were flushed so we don't 3556 // have to go back any further. 3557 // 3558 // A NULL method_refs is different than an empty method_refs. 3559 // We cannot infer any optimizations about older generations 3560 // from an empty method_refs for the current generation. 3561 break; 3562 } 3563 3564 for (int k = method_refs->length() - 1; k >= 0; k--) { 3565 Method* method = method_refs->at(k); 3566 3567 if (!method->is_obsolete() && 3568 method->name() == m_name && 3569 method->signature() == m_signature) { 3570 // The current RedefineClasses() call has made all EMCP 3571 // versions of this method obsolete so mark it as obsolete 3572 // and remove the reference. 3573 RC_TRACE(0x00000400, 3574 ("add: %s(%s): flush obsolete method @%d in version @%d", 3575 m_name->as_C_string(), m_signature->as_C_string(), k, j)); 3576 3577 method->set_is_obsolete(); 3578 // Leave obsolete methods on the previous version list to 3579 // clean up later. 3580 break; 3581 } 3582 } 3583 3584 // The previous loop may not find a matching EMCP method, but 3585 // that doesn't mean that we can optimize and not go any 3586 // further back in the PreviousVersion generations. The EMCP 3587 // method for this generation could have already been deleted, 3588 // but there still may be an older EMCP method that has not 3589 // been deleted. 3590 } 3591 3592 if (++local_count >= obsolete_method_count) { 3593 // no more obsolete methods so bail out now 3594 break; 3595 } 3596 } 3597 } 3598 } 3599 } // end add_previous_version() 3600 3601 3602 // Determine if InstanceKlass has a previous version. 3603 bool InstanceKlass::has_previous_version() const { 3604 return (_previous_versions != NULL && _previous_versions->length() > 0); 3605 } // end has_previous_version() 3606 3607 3608 Method* InstanceKlass::method_with_idnum(int idnum) { 3609 Method* m = NULL; 3610 if (idnum < methods()->length()) { 3611 m = methods()->at(idnum); 3612 } 3613 if (m == NULL || m->method_idnum() != idnum) { 3614 for (int index = 0; index < methods()->length(); ++index) { 3615 m = methods()->at(index); 3616 if (m->method_idnum() == idnum) { 3617 return m; 3618 } 3619 } 3620 // None found, return null for the caller to handle. 3621 return NULL; 3622 } 3623 return m; 3624 } 3625 3626 jint InstanceKlass::get_cached_class_file_len() { 3627 return VM_RedefineClasses::get_cached_class_file_len(_cached_class_file); 3628 } 3629 3630 unsigned char * InstanceKlass::get_cached_class_file_bytes() { 3631 return VM_RedefineClasses::get_cached_class_file_bytes(_cached_class_file); 3632 } 3633 3634 3635 // Construct a PreviousVersionNode entry for the array hung off 3636 // the InstanceKlass. 3637 PreviousVersionNode::PreviousVersionNode(ConstantPool* prev_constant_pool, 3638 GrowableArray<Method*>* prev_EMCP_methods) { 3639 3640 _prev_constant_pool = prev_constant_pool; 3641 _prev_EMCP_methods = prev_EMCP_methods; 3642 } 3643 3644 3645 // Destroy a PreviousVersionNode 3646 PreviousVersionNode::~PreviousVersionNode() { 3647 if (_prev_constant_pool != NULL) { 3648 _prev_constant_pool = NULL; 3649 } 3650 3651 if (_prev_EMCP_methods != NULL) { 3652 delete _prev_EMCP_methods; 3653 } 3654 } 3655 3656 // Construct a helper for walking the previous versions array 3657 PreviousVersionWalker::PreviousVersionWalker(Thread* thread, InstanceKlass *ik) { 3658 _thread = thread; 3659 _previous_versions = ik->previous_versions(); 3660 _current_index = 0; 3661 _current_p = NULL; 3662 _current_constant_pool_handle = constantPoolHandle(thread, ik->constants()); 3663 } 3664 3665 3666 // Return the interesting information for the next previous version 3667 // of the klass. Returns NULL if there are no more previous versions. 3668 PreviousVersionNode* PreviousVersionWalker::next_previous_version() { 3669 if (_previous_versions == NULL) { 3670 // no previous versions so nothing to return 3671 return NULL; 3672 } 3673 3674 _current_p = NULL; // reset to NULL 3675 _current_constant_pool_handle = NULL; 3676 3677 int length = _previous_versions->length(); 3678 3679 while (_current_index < length) { 3680 PreviousVersionNode * pv_node = _previous_versions->at(_current_index++); 3681 3682 // Save a handle to the constant pool for this previous version, 3683 // which keeps all the methods from being deallocated. 3684 _current_constant_pool_handle = constantPoolHandle(_thread, pv_node->prev_constant_pool()); 3685 _current_p = pv_node; 3686 return pv_node; 3687 } 3688 3689 return NULL; 3690 } // end next_previous_version()