1 /* 2 * Copyright (c) 1997, 2024, 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 "cds/archiveHeapLoader.hpp" 27 #include "cds/cdsConfig.hpp" 28 #include "cds/heapShared.hpp" 29 #include "classfile/classLoader.hpp" 30 #include "classfile/classLoaderData.inline.hpp" 31 #include "classfile/classLoaderDataGraph.inline.hpp" 32 #include "classfile/javaClasses.inline.hpp" 33 #include "classfile/moduleEntry.hpp" 34 #include "classfile/systemDictionary.hpp" 35 #include "classfile/systemDictionaryShared.hpp" 36 #include "classfile/vmClasses.hpp" 37 #include "classfile/vmSymbols.hpp" 38 #include "gc/shared/collectedHeap.inline.hpp" 39 #include "jvm_io.h" 40 #include "logging/log.hpp" 41 #include "memory/metadataFactory.hpp" 42 #include "memory/metaspaceClosure.hpp" 43 #include "memory/oopFactory.hpp" 44 #include "memory/resourceArea.hpp" 45 #include "memory/universe.hpp" 46 #include "oops/compressedKlass.inline.hpp" 47 #include "oops/compressedOops.inline.hpp" 48 #include "oops/instanceKlass.hpp" 49 #include "oops/klass.inline.hpp" 50 #include "oops/objArrayKlass.hpp" 51 #include "oops/oop.inline.hpp" 52 #include "oops/oopHandle.inline.hpp" 53 #include "prims/jvmtiExport.hpp" 54 #include "runtime/atomic.hpp" 55 #include "runtime/handles.inline.hpp" 56 #include "runtime/perfData.hpp" 57 #include "utilities/macros.hpp" 58 #include "utilities/numberSeq.hpp" 59 #include "utilities/powerOfTwo.hpp" 60 #include "utilities/rotate_bits.hpp" 61 #include "utilities/stack.inline.hpp" 62 63 void Klass::set_java_mirror(Handle m) { 64 assert(!m.is_null(), "New mirror should never be null."); 65 assert(_java_mirror.is_empty(), "should only be used to initialize mirror"); 66 _java_mirror = class_loader_data()->add_handle(m); 67 } 68 69 bool Klass::is_cloneable() const { 70 return _misc_flags.is_cloneable_fast() || 71 is_subtype_of(vmClasses::Cloneable_klass()); 72 } 73 74 void Klass::set_is_cloneable() { 75 if (name() == vmSymbols::java_lang_invoke_MemberName()) { 76 assert(is_final(), "no subclasses allowed"); 77 // MemberName cloning should not be intrinsified and always happen in JVM_Clone. 78 } else if (is_instance_klass() && InstanceKlass::cast(this)->reference_type() != REF_NONE) { 79 // Reference cloning should not be intrinsified and always happen in JVM_Clone. 80 } else { 81 _misc_flags.set_is_cloneable_fast(true); 82 } 83 } 84 85 uint8_t Klass::compute_hash_slot(Symbol* n) { 86 uint hash_code; 87 // Special cases for the two superclasses of all Array instances. 88 // Code elsewhere assumes, for all instances of ArrayKlass, that 89 // these two interfaces will be in this order. 90 91 // We ensure there are some empty slots in the hash table between 92 // these two very common interfaces because if they were adjacent 93 // (e.g. Slots 0 and 1), then any other class which hashed to 0 or 1 94 // would result in a probe length of 3. 95 if (n == vmSymbols::java_lang_Cloneable()) { 96 hash_code = 0; 97 } else if (n == vmSymbols::java_io_Serializable()) { 98 hash_code = SECONDARY_SUPERS_TABLE_SIZE / 2; 99 } else { 100 auto s = (const jbyte*) n->bytes(); 101 hash_code = java_lang_String::hash_code(s, n->utf8_length()); 102 // We use String::hash_code here (rather than e.g. 103 // Symbol::identity_hash()) in order to have a hash code that 104 // does not change from run to run. We want that because the 105 // hash value for a secondary superclass appears in generated 106 // code as a constant. 107 108 // This constant is magic: see Knuth, "Fibonacci Hashing". 109 constexpr uint multiplier 110 = 2654435769; // (uint)(((u8)1 << 32) / ((1 + sqrt(5)) / 2 )) 111 constexpr uint hash_shift = sizeof(hash_code) * 8 - 6; 112 // The leading bits of the least significant half of the product. 113 hash_code = (hash_code * multiplier) >> hash_shift; 114 115 if (StressSecondarySupers) { 116 // Generate many hash collisions in order to stress-test the 117 // linear search fallback. 118 hash_code = hash_code % 3; 119 hash_code = hash_code * (SECONDARY_SUPERS_TABLE_SIZE / 3); 120 } 121 } 122 123 return (hash_code & SECONDARY_SUPERS_TABLE_MASK); 124 } 125 126 void Klass::set_name(Symbol* n) { 127 _name = n; 128 129 if (_name != nullptr) { 130 _name->increment_refcount(); 131 } 132 133 { 134 elapsedTimer selftime; 135 selftime.start(); 136 137 _hash_slot = compute_hash_slot(n); 138 assert(_hash_slot < SECONDARY_SUPERS_TABLE_SIZE, "required"); 139 140 selftime.stop(); 141 if (UsePerfData) { 142 ClassLoader::perf_secondary_hash_time()->inc(selftime.ticks()); 143 } 144 } 145 146 if (CDSConfig::is_dumping_archive() && is_instance_klass()) { 147 SystemDictionaryShared::init_dumptime_info(InstanceKlass::cast(this)); 148 } 149 } 150 151 bool Klass::is_subclass_of(const Klass* k) const { 152 // Run up the super chain and check 153 if (this == k) return true; 154 155 Klass* t = const_cast<Klass*>(this)->super(); 156 157 while (t != nullptr) { 158 if (t == k) return true; 159 t = t->super(); 160 } 161 return false; 162 } 163 164 void Klass::release_C_heap_structures(bool release_constant_pool) { 165 if (_name != nullptr) _name->decrement_refcount(); 166 } 167 168 bool Klass::linear_search_secondary_supers(const Klass* k) const { 169 // Scan the array-of-objects for a match 170 // FIXME: We could do something smarter here, maybe a vectorized 171 // comparison or a binary search, but is that worth any added 172 // complexity? 173 int cnt = secondary_supers()->length(); 174 for (int i = 0; i < cnt; i++) { 175 if (secondary_supers()->at(i) == k) { 176 return true; 177 } 178 } 179 return false; 180 } 181 182 // Given a secondary superklass k, an initial array index, and an 183 // occupancy bitmap rotated such that Bit 1 is the next bit to test, 184 // search for k. 185 bool Klass::fallback_search_secondary_supers(const Klass* k, int index, uintx rotated_bitmap) const { 186 // Once the occupancy bitmap is almost full, it's faster to use a 187 // linear search. 188 if (secondary_supers()->length() > SECONDARY_SUPERS_TABLE_SIZE - 2) { 189 return linear_search_secondary_supers(k); 190 } 191 192 // This is conventional linear probing, but instead of terminating 193 // when a null entry is found in the table, we maintain a bitmap 194 // in which a 0 indicates missing entries. 195 196 precond((int)population_count(rotated_bitmap) == secondary_supers()->length()); 197 198 // The check for secondary_supers()->length() <= SECONDARY_SUPERS_TABLE_SIZE - 2 199 // at the start of this function guarantees there are 0s in the 200 // bitmap, so this loop eventually terminates. 201 while ((rotated_bitmap & 2) != 0) { 202 if (++index == secondary_supers()->length()) { 203 index = 0; 204 } 205 if (secondary_supers()->at(index) == k) { 206 return true; 207 } 208 rotated_bitmap = rotate_right(rotated_bitmap, 1); 209 } 210 return false; 211 } 212 213 // Return self, except for abstract classes with exactly 1 214 // implementor. Then return the 1 concrete implementation. 215 Klass *Klass::up_cast_abstract() { 216 Klass *r = this; 217 while( r->is_abstract() ) { // Receiver is abstract? 218 Klass *s = r->subklass(); // Check for exactly 1 subklass 219 if (s == nullptr || s->next_sibling() != nullptr) // Oops; wrong count; give up 220 return this; // Return 'this' as a no-progress flag 221 r = s; // Loop till find concrete class 222 } 223 return r; // Return the 1 concrete class 224 } 225 226 // Find LCA in class hierarchy 227 Klass *Klass::LCA( Klass *k2 ) { 228 Klass *k1 = this; 229 while( 1 ) { 230 if( k1->is_subtype_of(k2) ) return k2; 231 if( k2->is_subtype_of(k1) ) return k1; 232 k1 = k1->super(); 233 k2 = k2->super(); 234 } 235 } 236 237 238 void Klass::check_valid_for_instantiation(bool throwError, TRAPS) { 239 ResourceMark rm(THREAD); 240 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError() 241 : vmSymbols::java_lang_InstantiationException(), external_name()); 242 } 243 244 245 void Klass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) { 246 ResourceMark rm(THREAD); 247 assert(s != nullptr, "Throw NPE!"); 248 THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), 249 err_msg("arraycopy: source type %s is not an array", s->klass()->external_name())); 250 } 251 252 253 void Klass::initialize(TRAPS) { 254 ShouldNotReachHere(); 255 } 256 257 Klass* Klass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const { 258 #ifdef ASSERT 259 tty->print_cr("Error: find_field called on a klass oop." 260 " Likely error: reflection method does not correctly" 261 " wrap return value in a mirror object."); 262 #endif 263 ShouldNotReachHere(); 264 return nullptr; 265 } 266 267 Method* Klass::uncached_lookup_method(const Symbol* name, const Symbol* signature, 268 OverpassLookupMode overpass_mode, 269 PrivateLookupMode private_mode) const { 270 #ifdef ASSERT 271 tty->print_cr("Error: uncached_lookup_method called on a klass oop." 272 " Likely error: reflection method does not correctly" 273 " wrap return value in a mirror object."); 274 #endif 275 ShouldNotReachHere(); 276 return nullptr; 277 } 278 279 static markWord make_prototype(const Klass* kls) { 280 markWord prototype = markWord::prototype(); 281 #ifdef _LP64 282 if (UseCompactObjectHeaders) { 283 // With compact object headers, the narrow Klass ID is part of the mark word. 284 // We therfore seed the mark word with the narrow Klass ID. 285 // Note that only those Klass that can be instantiated have a narrow Klass ID. 286 // For those who don't, we leave the klass bits empty and assert if someone 287 // tries to use those. 288 const narrowKlass nk = CompressedKlassPointers::is_encodable(kls) ? 289 CompressedKlassPointers::encode(const_cast<Klass*>(kls)) : 0; 290 prototype = prototype.set_narrow_klass(nk); 291 } 292 #endif 293 return prototype; 294 } 295 296 Klass::Klass() : _kind(UnknownKlassKind) { 297 assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for cds"); 298 } 299 300 // "Normal" instantiation is preceded by a MetaspaceObj allocation 301 // which zeros out memory - calloc equivalent. 302 // The constructor is also used from CppVtableCloner, 303 // which doesn't zero out the memory before calling the constructor. 304 Klass::Klass(KlassKind kind) : _kind(kind), 305 _prototype_header(make_prototype(this)), 306 _shared_class_path_index(-1) { 307 CDS_ONLY(_shared_class_flags = 0;) 308 CDS_JAVA_HEAP_ONLY(_archived_mirror_index = -1;) 309 _primary_supers[0] = this; 310 set_super_check_offset(in_bytes(primary_supers_offset())); 311 } 312 313 jint Klass::array_layout_helper(BasicType etype) { 314 assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype"); 315 // Note that T_ARRAY is not allowed here. 316 int hsize = arrayOopDesc::base_offset_in_bytes(etype); 317 int esize = type2aelembytes(etype); 318 bool isobj = (etype == T_OBJECT); 319 int tag = isobj ? _lh_array_tag_obj_value : _lh_array_tag_type_value; 320 int lh = array_layout_helper(tag, hsize, etype, exact_log2(esize)); 321 322 assert(lh < (int)_lh_neutral_value, "must look like an array layout"); 323 assert(layout_helper_is_array(lh), "correct kind"); 324 assert(layout_helper_is_objArray(lh) == isobj, "correct kind"); 325 assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind"); 326 assert(layout_helper_header_size(lh) == hsize, "correct decode"); 327 assert(layout_helper_element_type(lh) == etype, "correct decode"); 328 assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode"); 329 330 return lh; 331 } 332 333 bool Klass::can_be_primary_super_slow() const { 334 if (super() == nullptr) 335 return true; 336 else if (super()->super_depth() >= primary_super_limit()-1) 337 return false; 338 else 339 return true; 340 } 341 342 void Klass::set_secondary_supers(Array<Klass*>* secondaries, uintx bitmap) { 343 #ifdef ASSERT 344 if (secondaries != nullptr) { 345 uintx real_bitmap = compute_secondary_supers_bitmap(secondaries); 346 assert(bitmap == real_bitmap, "must be"); 347 assert(secondaries->length() >= (int)population_count(bitmap), "must be"); 348 } 349 #endif 350 _secondary_supers_bitmap = bitmap; 351 _secondary_supers = secondaries; 352 353 if (secondaries != nullptr) { 354 LogMessage(class, load) msg; 355 NonInterleavingLogStream log {LogLevel::Debug, msg}; 356 if (log.is_enabled()) { 357 ResourceMark rm; 358 log.print_cr("set_secondary_supers: hash_slot: %d; klass: %s", hash_slot(), external_name()); 359 print_secondary_supers_on(&log); 360 } 361 } 362 } 363 364 // Hashed secondary superclasses 365 // 366 // We use a compressed 64-entry hash table with linear probing. We 367 // start by creating a hash table in the usual way, followed by a pass 368 // that removes all the null entries. To indicate which entries would 369 // have been null we use a bitmap that contains a 1 in each position 370 // where an entry is present, 0 otherwise. This bitmap also serves as 371 // a kind of Bloom filter, which in many cases allows us quickly to 372 // eliminate the possibility that something is a member of a set of 373 // secondaries. 374 uintx Klass::hash_secondary_supers(Array<Klass*>* secondaries, bool rewrite) { 375 const int length = secondaries->length(); 376 377 if (length == 0) { 378 return SECONDARY_SUPERS_BITMAP_EMPTY; 379 } 380 381 if (length == 1) { 382 int hash_slot = secondaries->at(0)->hash_slot(); 383 return uintx(1) << hash_slot; 384 } 385 386 // Invariant: _secondary_supers.length >= population_count(_secondary_supers_bitmap) 387 388 // Don't attempt to hash a table that's completely full, because in 389 // the case of an absent interface linear probing would not 390 // terminate. 391 if (length >= SECONDARY_SUPERS_TABLE_SIZE) { 392 return SECONDARY_SUPERS_BITMAP_FULL; 393 } 394 395 { 396 PerfTraceTime ptt(ClassLoader::perf_secondary_hash_time()); 397 398 ResourceMark rm; 399 uintx bitmap = SECONDARY_SUPERS_BITMAP_EMPTY; 400 auto hashed_secondaries = new GrowableArray<Klass*>(SECONDARY_SUPERS_TABLE_SIZE, 401 SECONDARY_SUPERS_TABLE_SIZE, nullptr); 402 403 for (int j = 0; j < length; j++) { 404 Klass* k = secondaries->at(j); 405 hash_insert(k, hashed_secondaries, bitmap); 406 } 407 408 // Pack the hashed secondaries array by copying it into the 409 // secondaries array, sans nulls, if modification is allowed. 410 // Otherwise, validate the order. 411 int i = 0; 412 for (int slot = 0; slot < SECONDARY_SUPERS_TABLE_SIZE; slot++) { 413 bool has_element = ((bitmap >> slot) & 1) != 0; 414 assert(has_element == (hashed_secondaries->at(slot) != nullptr), ""); 415 if (has_element) { 416 Klass* k = hashed_secondaries->at(slot); 417 if (rewrite) { 418 secondaries->at_put(i, k); 419 } else if (secondaries->at(i) != k) { 420 assert(false, "broken secondary supers hash table"); 421 return SECONDARY_SUPERS_BITMAP_FULL; 422 } 423 i++; 424 } 425 } 426 assert(i == secondaries->length(), "mismatch"); 427 postcond((int)population_count(bitmap) == secondaries->length()); 428 429 return bitmap; 430 } 431 } 432 433 void Klass::hash_insert(Klass* klass, GrowableArray<Klass*>* secondaries, uintx& bitmap) { 434 assert(bitmap != SECONDARY_SUPERS_BITMAP_FULL, ""); 435 436 int dist = 0; 437 for (int slot = klass->hash_slot(); true; slot = (slot + 1) & SECONDARY_SUPERS_TABLE_MASK) { 438 Klass* existing = secondaries->at(slot); 439 assert(((bitmap >> slot) & 1) == (existing != nullptr), "mismatch"); 440 if (existing == nullptr) { // no conflict 441 secondaries->at_put(slot, klass); 442 bitmap |= uintx(1) << slot; 443 assert(bitmap != SECONDARY_SUPERS_BITMAP_FULL, ""); 444 return; 445 } else { 446 // Use Robin Hood hashing to minimize the worst case search. 447 // Also, every permutation of the insertion sequence produces 448 // the same final Robin Hood hash table, provided that a 449 // consistent tie breaker is used. 450 int existing_dist = (slot - existing->hash_slot()) & SECONDARY_SUPERS_TABLE_MASK; 451 if (existing_dist < dist 452 // This tie breaker ensures that the hash order is maintained. 453 || ((existing_dist == dist) 454 && (uintptr_t(existing) < uintptr_t(klass)))) { 455 Klass* tmp = secondaries->at(slot); 456 secondaries->at_put(slot, klass); 457 klass = tmp; 458 dist = existing_dist; 459 } 460 ++dist; 461 } 462 } 463 } 464 465 Array<Klass*>* Klass::pack_secondary_supers(ClassLoaderData* loader_data, 466 GrowableArray<Klass*>* primaries, 467 GrowableArray<Klass*>* secondaries, 468 uintx& bitmap, TRAPS) { 469 int new_length = primaries->length() + secondaries->length(); 470 Array<Klass*>* secondary_supers = MetadataFactory::new_array<Klass*>(loader_data, new_length, CHECK_NULL); 471 472 // Combine the two arrays into a metadata object to pack the array. 473 // The primaries are added in the reverse order, then the secondaries. 474 int fill_p = primaries->length(); 475 for (int j = 0; j < fill_p; j++) { 476 secondary_supers->at_put(j, primaries->pop()); // add primaries in reverse order. 477 } 478 for( int j = 0; j < secondaries->length(); j++ ) { 479 secondary_supers->at_put(j+fill_p, secondaries->at(j)); // add secondaries on the end. 480 } 481 #ifdef ASSERT 482 // We must not copy any null placeholders left over from bootstrap. 483 for (int j = 0; j < secondary_supers->length(); j++) { 484 assert(secondary_supers->at(j) != nullptr, "correct bootstrapping order"); 485 } 486 #endif 487 488 bitmap = hash_secondary_supers(secondary_supers, /*rewrite=*/true); // rewrites freshly allocated array 489 return secondary_supers; 490 } 491 492 uintx Klass::compute_secondary_supers_bitmap(Array<Klass*>* secondary_supers) { 493 return hash_secondary_supers(secondary_supers, /*rewrite=*/false); // no rewrites allowed 494 } 495 496 uint8_t Klass::compute_home_slot(Klass* k, uintx bitmap) { 497 uint8_t hash = k->hash_slot(); 498 if (hash > 0) { 499 return population_count(bitmap << (SECONDARY_SUPERS_TABLE_SIZE - hash)); 500 } 501 return 0; 502 } 503 504 505 void Klass::initialize_supers(Klass* k, Array<InstanceKlass*>* transitive_interfaces, TRAPS) { 506 if (k == nullptr) { 507 set_super(nullptr); 508 _primary_supers[0] = this; 509 assert(super_depth() == 0, "Object must already be initialized properly"); 510 } else if (k != super() || k == vmClasses::Object_klass()) { 511 assert(super() == nullptr || super() == vmClasses::Object_klass(), 512 "initialize this only once to a non-trivial value"); 513 set_super(k); 514 Klass* sup = k; 515 int sup_depth = sup->super_depth(); 516 juint my_depth = MIN2(sup_depth + 1, (int)primary_super_limit()); 517 if (!can_be_primary_super_slow()) 518 my_depth = primary_super_limit(); 519 for (juint i = 0; i < my_depth; i++) { 520 _primary_supers[i] = sup->_primary_supers[i]; 521 } 522 Klass* *super_check_cell; 523 if (my_depth < primary_super_limit()) { 524 _primary_supers[my_depth] = this; 525 super_check_cell = &_primary_supers[my_depth]; 526 } else { 527 // Overflow of the primary_supers array forces me to be secondary. 528 super_check_cell = &_secondary_super_cache; 529 } 530 set_super_check_offset(u4((address)super_check_cell - (address) this)); 531 532 #ifdef ASSERT 533 { 534 juint j = super_depth(); 535 assert(j == my_depth, "computed accessor gets right answer"); 536 Klass* t = this; 537 while (!t->can_be_primary_super()) { 538 t = t->super(); 539 j = t->super_depth(); 540 } 541 for (juint j1 = j+1; j1 < primary_super_limit(); j1++) { 542 assert(primary_super_of_depth(j1) == nullptr, "super list padding"); 543 } 544 while (t != nullptr) { 545 assert(primary_super_of_depth(j) == t, "super list initialization"); 546 t = t->super(); 547 --j; 548 } 549 assert(j == (juint)-1, "correct depth count"); 550 } 551 #endif 552 } 553 554 if (secondary_supers() == nullptr) { 555 556 // Now compute the list of secondary supertypes. 557 // Secondaries can occasionally be on the super chain, 558 // if the inline "_primary_supers" array overflows. 559 int extras = 0; 560 Klass* p; 561 for (p = super(); !(p == nullptr || p->can_be_primary_super()); p = p->super()) { 562 ++extras; 563 } 564 565 ResourceMark rm(THREAD); // need to reclaim GrowableArrays allocated below 566 567 // Compute the "real" non-extra secondaries. 568 GrowableArray<Klass*>* secondaries = compute_secondary_supers(extras, transitive_interfaces); 569 if (secondaries == nullptr) { 570 // secondary_supers set by compute_secondary_supers 571 return; 572 } 573 574 GrowableArray<Klass*>* primaries = new GrowableArray<Klass*>(extras); 575 576 for (p = super(); !(p == nullptr || p->can_be_primary_super()); p = p->super()) { 577 int i; // Scan for overflow primaries being duplicates of 2nd'arys 578 579 // This happens frequently for very deeply nested arrays: the 580 // primary superclass chain overflows into the secondary. The 581 // secondary list contains the element_klass's secondaries with 582 // an extra array dimension added. If the element_klass's 583 // secondary list already contains some primary overflows, they 584 // (with the extra level of array-ness) will collide with the 585 // normal primary superclass overflows. 586 for( i = 0; i < secondaries->length(); i++ ) { 587 if( secondaries->at(i) == p ) 588 break; 589 } 590 if( i < secondaries->length() ) 591 continue; // It's a dup, don't put it in 592 primaries->push(p); 593 } 594 // Combine the two arrays into a metadata object to pack the array. 595 uintx bitmap = 0; 596 Array<Klass*>* s2 = pack_secondary_supers(class_loader_data(), primaries, secondaries, bitmap, CHECK); 597 set_secondary_supers(s2, bitmap); 598 } 599 } 600 601 GrowableArray<Klass*>* Klass::compute_secondary_supers(int num_extra_slots, 602 Array<InstanceKlass*>* transitive_interfaces) { 603 assert(num_extra_slots == 0, "override for complex klasses"); 604 assert(transitive_interfaces == nullptr, "sanity"); 605 set_secondary_supers(Universe::the_empty_klass_array(), Universe::the_empty_klass_bitmap()); 606 return nullptr; 607 } 608 609 610 // superklass links 611 InstanceKlass* Klass::superklass() const { 612 assert(super() == nullptr || super()->is_instance_klass(), "must be instance klass"); 613 return _super == nullptr ? nullptr : InstanceKlass::cast(_super); 614 } 615 616 // subklass links. Used by the compiler (and vtable initialization) 617 // May be cleaned concurrently, so must use the Compile_lock. 618 // The log parameter is for clean_weak_klass_links to report unlinked classes. 619 Klass* Klass::subklass(bool log) const { 620 // Need load_acquire on the _subklass, because it races with inserts that 621 // publishes freshly initialized data. 622 for (Klass* chain = Atomic::load_acquire(&_subklass); 623 chain != nullptr; 624 // Do not need load_acquire on _next_sibling, because inserts never 625 // create _next_sibling edges to dead data. 626 chain = Atomic::load(&chain->_next_sibling)) 627 { 628 if (chain->is_loader_alive()) { 629 return chain; 630 } else if (log) { 631 if (log_is_enabled(Trace, class, unload)) { 632 ResourceMark rm; 633 log_trace(class, unload)("unlinking class (subclass): %s", chain->external_name()); 634 } 635 } 636 } 637 return nullptr; 638 } 639 640 Klass* Klass::next_sibling(bool log) const { 641 // Do not need load_acquire on _next_sibling, because inserts never 642 // create _next_sibling edges to dead data. 643 for (Klass* chain = Atomic::load(&_next_sibling); 644 chain != nullptr; 645 chain = Atomic::load(&chain->_next_sibling)) { 646 // Only return alive klass, there may be stale klass 647 // in this chain if cleaned concurrently. 648 if (chain->is_loader_alive()) { 649 return chain; 650 } else if (log) { 651 if (log_is_enabled(Trace, class, unload)) { 652 ResourceMark rm; 653 log_trace(class, unload)("unlinking class (sibling): %s", chain->external_name()); 654 } 655 } 656 } 657 return nullptr; 658 } 659 660 void Klass::set_subklass(Klass* s) { 661 assert(s != this, "sanity check"); 662 Atomic::release_store(&_subklass, s); 663 } 664 665 void Klass::set_next_sibling(Klass* s) { 666 assert(s != this, "sanity check"); 667 // Does not need release semantics. If used by cleanup, it will link to 668 // already safely published data, and if used by inserts, will be published 669 // safely using cmpxchg. 670 Atomic::store(&_next_sibling, s); 671 } 672 673 void Klass::append_to_sibling_list() { 674 if (Universe::is_fully_initialized()) { 675 assert_locked_or_safepoint(Compile_lock); 676 } 677 debug_only(verify();) 678 // add ourselves to superklass' subklass list 679 InstanceKlass* super = superklass(); 680 if (super == nullptr) return; // special case: class Object 681 assert((!super->is_interface() // interfaces cannot be supers 682 && (super->superklass() == nullptr || !is_interface())), 683 "an interface can only be a subklass of Object"); 684 685 // Make sure there is no stale subklass head 686 super->clean_subklass(); 687 688 for (;;) { 689 Klass* prev_first_subklass = Atomic::load_acquire(&_super->_subklass); 690 if (prev_first_subklass != nullptr) { 691 // set our sibling to be the superklass' previous first subklass 692 assert(prev_first_subklass->is_loader_alive(), "May not attach not alive klasses"); 693 set_next_sibling(prev_first_subklass); 694 } 695 // Note that the prev_first_subklass is always alive, meaning no sibling_next links 696 // are ever created to not alive klasses. This is an important invariant of the lock-free 697 // cleaning protocol, that allows us to safely unlink dead klasses from the sibling list. 698 if (Atomic::cmpxchg(&super->_subklass, prev_first_subklass, this) == prev_first_subklass) { 699 return; 700 } 701 } 702 debug_only(verify();) 703 } 704 705 void Klass::clean_subklass() { 706 for (;;) { 707 // Need load_acquire, due to contending with concurrent inserts 708 Klass* subklass = Atomic::load_acquire(&_subklass); 709 if (subklass == nullptr || subklass->is_loader_alive()) { 710 return; 711 } 712 // Try to fix _subklass until it points at something not dead. 713 Atomic::cmpxchg(&_subklass, subklass, subklass->next_sibling()); 714 } 715 } 716 717 void Klass::clean_weak_klass_links(bool unloading_occurred, bool clean_alive_klasses) { 718 if (!ClassUnloading || !unloading_occurred) { 719 return; 720 } 721 722 Klass* root = vmClasses::Object_klass(); 723 Stack<Klass*, mtGC> stack; 724 725 stack.push(root); 726 while (!stack.is_empty()) { 727 Klass* current = stack.pop(); 728 729 assert(current->is_loader_alive(), "just checking, this should be live"); 730 731 // Find and set the first alive subklass 732 Klass* sub = current->subklass(true); 733 current->clean_subklass(); 734 if (sub != nullptr) { 735 stack.push(sub); 736 } 737 738 // Find and set the first alive sibling 739 Klass* sibling = current->next_sibling(true); 740 current->set_next_sibling(sibling); 741 if (sibling != nullptr) { 742 stack.push(sibling); 743 } 744 745 // Clean the implementors list and method data. 746 if (clean_alive_klasses && current->is_instance_klass()) { 747 InstanceKlass* ik = InstanceKlass::cast(current); 748 ik->clean_weak_instanceklass_links(); 749 750 // JVMTI RedefineClasses creates previous versions that are not in 751 // the class hierarchy, so process them here. 752 while ((ik = ik->previous_versions()) != nullptr) { 753 ik->clean_weak_instanceklass_links(); 754 } 755 } 756 } 757 } 758 759 void Klass::metaspace_pointers_do(MetaspaceClosure* it) { 760 if (log_is_enabled(Trace, cds)) { 761 ResourceMark rm; 762 log_trace(cds)("Iter(Klass): %p (%s)", this, external_name()); 763 } 764 765 it->push(&_name); 766 it->push(&_secondary_supers); 767 for (int i = 0; i < _primary_super_limit; i++) { 768 it->push(&_primary_supers[i]); 769 } 770 it->push(&_super); 771 if (!CDSConfig::is_dumping_archive()) { 772 // If dumping archive, these may point to excluded classes. There's no need 773 // to follow these pointers anyway, as they will be set to null in 774 // remove_unshareable_info(). 775 it->push((Klass**)&_subklass); 776 it->push((Klass**)&_next_sibling); 777 it->push(&_next_link); 778 } 779 780 vtableEntry* vt = start_of_vtable(); 781 for (int i=0; i<vtable_length(); i++) { 782 it->push(vt[i].method_addr()); 783 } 784 } 785 786 #if INCLUDE_CDS 787 void Klass::remove_unshareable_info() { 788 assert(CDSConfig::is_dumping_archive(), 789 "only called during CDS dump time"); 790 JFR_ONLY(REMOVE_ID(this);) 791 if (log_is_enabled(Trace, cds, unshareable)) { 792 ResourceMark rm; 793 log_trace(cds, unshareable)("remove: %s", external_name()); 794 } 795 796 // _secondary_super_cache may be updated by an is_subtype_of() call 797 // while ArchiveBuilder is copying metaspace objects. Let's reset it to 798 // null and let it be repopulated at runtime. 799 set_secondary_super_cache(nullptr); 800 801 set_subklass(nullptr); 802 set_next_sibling(nullptr); 803 set_next_link(nullptr); 804 805 // Null out class_loader_data because we don't share that yet. 806 set_class_loader_data(nullptr); 807 set_is_shared(); 808 809 // FIXME: validation in Klass::hash_secondary_supers() may fail for shared klasses. 810 // Even though the bitmaps always match, the canonical order of elements in the table 811 // is not guaranteed to stay the same (see tie breaker during Robin Hood hashing in Klass::hash_insert). 812 //assert(compute_secondary_supers_bitmap(secondary_supers()) == _secondary_supers_bitmap, "broken table"); 813 } 814 815 void Klass::remove_java_mirror() { 816 assert(CDSConfig::is_dumping_archive(), "sanity"); 817 if (log_is_enabled(Trace, cds, unshareable)) { 818 ResourceMark rm; 819 log_trace(cds, unshareable)("remove java_mirror: %s", external_name()); 820 } 821 // Just null out the mirror. The class_loader_data() no longer exists. 822 clear_java_mirror_handle(); 823 } 824 825 void Klass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) { 826 assert(is_klass(), "ensure C++ vtable is restored"); 827 assert(is_shared(), "must be set"); 828 assert(secondary_supers()->length() >= (int)population_count(_secondary_supers_bitmap), "must be"); 829 JFR_ONLY(RESTORE_ID(this);) 830 if (log_is_enabled(Trace, cds, unshareable)) { 831 ResourceMark rm(THREAD); 832 oop class_loader = loader_data->class_loader(); 833 log_trace(cds, unshareable)("restore: %s with class loader: %s", external_name(), 834 class_loader != nullptr ? class_loader->klass()->external_name() : "boot"); 835 } 836 837 // If an exception happened during CDS restore, some of these fields may already be 838 // set. We leave the class on the CLD list, even if incomplete so that we don't 839 // modify the CLD list outside a safepoint. 840 if (class_loader_data() == nullptr) { 841 set_class_loader_data(loader_data); 842 843 // Add to class loader list first before creating the mirror 844 // (same order as class file parsing) 845 loader_data->add_class(this); 846 } 847 848 Handle loader(THREAD, loader_data->class_loader()); 849 ModuleEntry* module_entry = nullptr; 850 Klass* k = this; 851 if (k->is_objArray_klass()) { 852 k = ObjArrayKlass::cast(k)->bottom_klass(); 853 } 854 // Obtain klass' module. 855 if (k->is_instance_klass()) { 856 InstanceKlass* ik = (InstanceKlass*) k; 857 module_entry = ik->module(); 858 } else { 859 module_entry = ModuleEntryTable::javabase_moduleEntry(); 860 } 861 // Obtain java.lang.Module, if available 862 Handle module_handle(THREAD, ((module_entry != nullptr) ? module_entry->module() : (oop)nullptr)); 863 864 if (this->has_archived_mirror_index()) { 865 ResourceMark rm(THREAD); 866 log_debug(cds, mirror)("%s has raw archived mirror", external_name()); 867 if (ArchiveHeapLoader::is_in_use()) { 868 bool present = java_lang_Class::restore_archived_mirror(this, loader, module_handle, 869 protection_domain, 870 CHECK); 871 if (present) { 872 return; 873 } 874 } 875 876 // No archived mirror data 877 log_debug(cds, mirror)("No archived mirror data for %s", external_name()); 878 clear_java_mirror_handle(); 879 this->clear_archived_mirror_index(); 880 } 881 882 // Only recreate it if not present. A previous attempt to restore may have 883 // gotten an OOM later but keep the mirror if it was created. 884 if (java_mirror() == nullptr) { 885 ResourceMark rm(THREAD); 886 log_trace(cds, mirror)("Recreate mirror for %s", external_name()); 887 java_lang_Class::create_mirror(this, loader, module_handle, protection_domain, Handle(), CHECK); 888 } 889 } 890 #endif // INCLUDE_CDS 891 892 #if INCLUDE_CDS_JAVA_HEAP 893 oop Klass::archived_java_mirror() { 894 assert(has_archived_mirror_index(), "must have archived mirror"); 895 return HeapShared::get_root(_archived_mirror_index); 896 } 897 898 void Klass::clear_archived_mirror_index() { 899 if (_archived_mirror_index >= 0) { 900 HeapShared::clear_root(_archived_mirror_index); 901 } 902 _archived_mirror_index = -1; 903 } 904 905 // No GC barrier 906 void Klass::set_archived_java_mirror(int mirror_index) { 907 assert(CDSConfig::is_dumping_heap(), "sanity"); 908 _archived_mirror_index = mirror_index; 909 } 910 #endif // INCLUDE_CDS_JAVA_HEAP 911 912 void Klass::check_array_allocation_length(int length, int max_length, TRAPS) { 913 if (length > max_length) { 914 if (!THREAD->is_in_internal_oome_mark()) { 915 report_java_out_of_memory("Requested array size exceeds VM limit"); 916 JvmtiExport::post_array_size_exhausted(); 917 THROW_OOP(Universe::out_of_memory_error_array_size()); 918 } else { 919 THROW_OOP(Universe::out_of_memory_error_java_heap_without_backtrace()); 920 } 921 } else if (length < 0) { 922 THROW_MSG(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", length)); 923 } 924 } 925 926 // Replace the last '+' char with '/'. 927 static char* convert_hidden_name_to_java(Symbol* name) { 928 size_t name_len = name->utf8_length(); 929 char* result = NEW_RESOURCE_ARRAY(char, name_len + 1); 930 name->as_klass_external_name(result, (int)name_len + 1); 931 for (int index = (int)name_len; index > 0; index--) { 932 if (result[index] == '+') { 933 result[index] = JVM_SIGNATURE_SLASH; 934 break; 935 } 936 } 937 return result; 938 } 939 940 // In product mode, this function doesn't have virtual function calls so 941 // there might be some performance advantage to handling InstanceKlass here. 942 const char* Klass::external_name() const { 943 if (is_instance_klass()) { 944 const InstanceKlass* ik = static_cast<const InstanceKlass*>(this); 945 if (ik->is_hidden()) { 946 char* result = convert_hidden_name_to_java(name()); 947 return result; 948 } 949 } else if (is_objArray_klass() && ObjArrayKlass::cast(this)->bottom_klass()->is_hidden()) { 950 char* result = convert_hidden_name_to_java(name()); 951 return result; 952 } 953 if (name() == nullptr) return "<unknown>"; 954 return name()->as_klass_external_name(); 955 } 956 957 const char* Klass::signature_name() const { 958 if (name() == nullptr) return "<unknown>"; 959 if (is_objArray_klass() && ObjArrayKlass::cast(this)->bottom_klass()->is_hidden()) { 960 size_t name_len = name()->utf8_length(); 961 char* result = NEW_RESOURCE_ARRAY(char, name_len + 1); 962 name()->as_C_string(result, (int)name_len + 1); 963 for (int index = (int)name_len; index > 0; index--) { 964 if (result[index] == '+') { 965 result[index] = JVM_SIGNATURE_DOT; 966 break; 967 } 968 } 969 return result; 970 } 971 return name()->as_C_string(); 972 } 973 974 const char* Klass::external_kind() const { 975 if (is_interface()) return "interface"; 976 if (is_abstract()) return "abstract class"; 977 return "class"; 978 } 979 980 // Unless overridden, jvmti_class_status has no flags set. 981 jint Klass::jvmti_class_status() const { 982 return 0; 983 } 984 985 986 // Printing 987 988 void Klass::print_on(outputStream* st) const { 989 ResourceMark rm; 990 // print title 991 st->print("%s", internal_name()); 992 print_address_on(st); 993 st->cr(); 994 } 995 996 #define BULLET " - " 997 998 // Caller needs ResourceMark 999 void Klass::oop_print_on(oop obj, outputStream* st) { 1000 // print title 1001 st->print_cr("%s ", internal_name()); 1002 obj->print_address_on(st); 1003 1004 if (WizardMode) { 1005 // print header 1006 obj->mark().print_on(st); 1007 st->cr(); 1008 if (UseCompactObjectHeaders) { 1009 st->print(BULLET"prototype_header: " INTPTR_FORMAT, _prototype_header.value()); 1010 st->cr(); 1011 } 1012 } 1013 1014 // print class 1015 st->print(BULLET"klass: "); 1016 obj->klass()->print_value_on(st); 1017 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr(); 1018 st->cr(); 1019 } 1020 1021 void Klass::oop_print_value_on(oop obj, outputStream* st) { 1022 // print title 1023 ResourceMark rm; // Cannot print in debug mode without this 1024 st->print("%s", internal_name()); 1025 obj->print_address_on(st); 1026 } 1027 1028 // Verification 1029 1030 void Klass::verify_on(outputStream* st) { 1031 1032 // This can be expensive, but it is worth checking that this klass is actually 1033 // in the CLD graph but not in production. 1034 #ifdef ASSERT 1035 if (UseCompressedClassPointers && needs_narrow_id()) { 1036 // Stricter checks for both correct alignment and placement 1037 CompressedKlassPointers::check_encodable(this); 1038 } else { 1039 assert(Metaspace::contains((address)this), "Should be"); 1040 } 1041 #endif // ASSERT 1042 1043 guarantee(this->is_klass(),"should be klass"); 1044 1045 if (super() != nullptr) { 1046 guarantee(super()->is_klass(), "should be klass"); 1047 } 1048 if (secondary_super_cache() != nullptr) { 1049 Klass* ko = secondary_super_cache(); 1050 guarantee(ko->is_klass(), "should be klass"); 1051 } 1052 for ( uint i = 0; i < primary_super_limit(); i++ ) { 1053 Klass* ko = _primary_supers[i]; 1054 if (ko != nullptr) { 1055 guarantee(ko->is_klass(), "should be klass"); 1056 } 1057 } 1058 1059 if (java_mirror_no_keepalive() != nullptr) { 1060 guarantee(java_lang_Class::is_instance(java_mirror_no_keepalive()), "should be instance"); 1061 } 1062 } 1063 1064 void Klass::oop_verify_on(oop obj, outputStream* st) { 1065 guarantee(oopDesc::is_oop(obj), "should be oop"); 1066 guarantee(obj->klass()->is_klass(), "klass field is not a klass"); 1067 } 1068 1069 // Note: this function is called with an address that may or may not be a Klass. 1070 // The point is not to assert it is but to check if it could be. 1071 bool Klass::is_valid(Klass* k) { 1072 if (!is_aligned(k, sizeof(MetaWord))) return false; 1073 if ((size_t)k < os::min_page_size()) return false; 1074 1075 if (!os::is_readable_range(k, k + 1)) return false; 1076 if (!Metaspace::contains(k)) return false; 1077 1078 if (!Symbol::is_valid(k->name())) return false; 1079 return ClassLoaderDataGraph::is_valid(k->class_loader_data()); 1080 } 1081 1082 Method* Klass::method_at_vtable(int index) { 1083 #ifndef PRODUCT 1084 assert(index >= 0, "valid vtable index"); 1085 if (DebugVtables) { 1086 verify_vtable_index(index); 1087 } 1088 #endif 1089 return start_of_vtable()[index].method(); 1090 } 1091 1092 1093 #ifndef PRODUCT 1094 1095 bool Klass::verify_vtable_index(int i) { 1096 int limit = vtable_length()/vtableEntry::size(); 1097 assert(i >= 0 && i < limit, "index %d out of bounds %d", i, limit); 1098 return true; 1099 } 1100 1101 #endif // PRODUCT 1102 1103 // Caller needs ResourceMark 1104 // joint_in_module_of_loader provides an optimization if 2 classes are in 1105 // the same module to succinctly print out relevant information about their 1106 // module name and class loader's name_and_id for error messages. 1107 // Format: 1108 // <fully-qualified-external-class-name1> and <fully-qualified-external-class-name2> 1109 // are in module <module-name>[@<version>] 1110 // of loader <loader-name_and_id>[, parent loader <parent-loader-name_and_id>] 1111 const char* Klass::joint_in_module_of_loader(const Klass* class2, bool include_parent_loader) const { 1112 assert(module() == class2->module(), "classes do not have the same module"); 1113 const char* class1_name = external_name(); 1114 size_t len = strlen(class1_name) + 1; 1115 1116 const char* class2_description = class2->class_in_module_of_loader(true, include_parent_loader); 1117 len += strlen(class2_description); 1118 1119 len += strlen(" and "); 1120 1121 char* joint_description = NEW_RESOURCE_ARRAY_RETURN_NULL(char, len); 1122 1123 // Just return the FQN if error when allocating string 1124 if (joint_description == nullptr) { 1125 return class1_name; 1126 } 1127 1128 jio_snprintf(joint_description, len, "%s and %s", 1129 class1_name, 1130 class2_description); 1131 1132 return joint_description; 1133 } 1134 1135 // Caller needs ResourceMark 1136 // class_in_module_of_loader provides a standard way to include 1137 // relevant information about a class, such as its module name as 1138 // well as its class loader's name_and_id, in error messages and logging. 1139 // Format: 1140 // <fully-qualified-external-class-name> is in module <module-name>[@<version>] 1141 // of loader <loader-name_and_id>[, parent loader <parent-loader-name_and_id>] 1142 const char* Klass::class_in_module_of_loader(bool use_are, bool include_parent_loader) const { 1143 // 1. fully qualified external name of class 1144 const char* klass_name = external_name(); 1145 size_t len = strlen(klass_name) + 1; 1146 1147 // 2. module name + @version 1148 const char* module_name = ""; 1149 const char* version = ""; 1150 bool has_version = false; 1151 bool module_is_named = false; 1152 const char* module_name_phrase = ""; 1153 const Klass* bottom_klass = is_objArray_klass() ? 1154 ObjArrayKlass::cast(this)->bottom_klass() : this; 1155 if (bottom_klass->is_instance_klass()) { 1156 ModuleEntry* module = InstanceKlass::cast(bottom_klass)->module(); 1157 if (module->is_named()) { 1158 module_is_named = true; 1159 module_name_phrase = "module "; 1160 module_name = module->name()->as_C_string(); 1161 len += strlen(module_name); 1162 // Use version if exists and is not a jdk module 1163 if (module->should_show_version()) { 1164 has_version = true; 1165 version = module->version()->as_C_string(); 1166 // Include stlen(version) + 1 for the "@" 1167 len += strlen(version) + 1; 1168 } 1169 } else { 1170 module_name = UNNAMED_MODULE; 1171 len += UNNAMED_MODULE_LEN; 1172 } 1173 } else { 1174 // klass is an array of primitives, module is java.base 1175 module_is_named = true; 1176 module_name_phrase = "module "; 1177 module_name = JAVA_BASE_NAME; 1178 len += JAVA_BASE_NAME_LEN; 1179 } 1180 1181 // 3. class loader's name_and_id 1182 ClassLoaderData* cld = class_loader_data(); 1183 assert(cld != nullptr, "class_loader_data should not be null"); 1184 const char* loader_name_and_id = cld->loader_name_and_id(); 1185 len += strlen(loader_name_and_id); 1186 1187 // 4. include parent loader information 1188 const char* parent_loader_phrase = ""; 1189 const char* parent_loader_name_and_id = ""; 1190 if (include_parent_loader && 1191 !cld->is_builtin_class_loader_data()) { 1192 oop parent_loader = java_lang_ClassLoader::parent(class_loader()); 1193 ClassLoaderData *parent_cld = ClassLoaderData::class_loader_data_or_null(parent_loader); 1194 // The parent loader's ClassLoaderData could be null if it is 1195 // a delegating class loader that has never defined a class. 1196 // In this case the loader's name must be obtained via the parent loader's oop. 1197 if (parent_cld == nullptr) { 1198 oop cl_name_and_id = java_lang_ClassLoader::nameAndId(parent_loader); 1199 if (cl_name_and_id != nullptr) { 1200 parent_loader_name_and_id = java_lang_String::as_utf8_string(cl_name_and_id); 1201 } 1202 } else { 1203 parent_loader_name_and_id = parent_cld->loader_name_and_id(); 1204 } 1205 parent_loader_phrase = ", parent loader "; 1206 len += strlen(parent_loader_phrase) + strlen(parent_loader_name_and_id); 1207 } 1208 1209 // Start to construct final full class description string 1210 len += ((use_are) ? strlen(" are in ") : strlen(" is in ")); 1211 len += strlen(module_name_phrase) + strlen(" of loader "); 1212 1213 char* class_description = NEW_RESOURCE_ARRAY_RETURN_NULL(char, len); 1214 1215 // Just return the FQN if error when allocating string 1216 if (class_description == nullptr) { 1217 return klass_name; 1218 } 1219 1220 jio_snprintf(class_description, len, "%s %s in %s%s%s%s of loader %s%s%s", 1221 klass_name, 1222 (use_are) ? "are" : "is", 1223 module_name_phrase, 1224 module_name, 1225 (has_version) ? "@" : "", 1226 (has_version) ? version : "", 1227 loader_name_and_id, 1228 parent_loader_phrase, 1229 parent_loader_name_and_id); 1230 1231 return class_description; 1232 } 1233 1234 class LookupStats : StackObj { 1235 private: 1236 uint _no_of_samples; 1237 uint _worst; 1238 uint _worst_count; 1239 uint _average; 1240 uint _best; 1241 uint _best_count; 1242 public: 1243 LookupStats() : _no_of_samples(0), _worst(0), _worst_count(0), _average(0), _best(INT_MAX), _best_count(0) {} 1244 1245 ~LookupStats() { 1246 assert(_best <= _worst || _no_of_samples == 0, "sanity"); 1247 } 1248 1249 void sample(uint value) { 1250 ++_no_of_samples; 1251 _average += value; 1252 1253 if (_worst < value) { 1254 _worst = value; 1255 _worst_count = 1; 1256 } else if (_worst == value) { 1257 ++_worst_count; 1258 } 1259 1260 if (_best > value) { 1261 _best = value; 1262 _best_count = 1; 1263 } else if (_best == value) { 1264 ++_best_count; 1265 } 1266 } 1267 1268 void print_on(outputStream* st) const { 1269 st->print("best: %2d (%4.1f%%)", _best, (100.0 * _best_count) / _no_of_samples); 1270 if (_best_count < _no_of_samples) { 1271 st->print("; average: %4.1f; worst: %2d (%4.1f%%)", 1272 (1.0 * _average) / _no_of_samples, 1273 _worst, (100.0 * _worst_count) / _no_of_samples); 1274 } 1275 } 1276 }; 1277 1278 static void print_positive_lookup_stats(Array<Klass*>* secondary_supers, uintx bitmap, outputStream* st) { 1279 int num_of_supers = secondary_supers->length(); 1280 1281 LookupStats s; 1282 for (int i = 0; i < num_of_supers; i++) { 1283 Klass* secondary_super = secondary_supers->at(i); 1284 int home_slot = Klass::compute_home_slot(secondary_super, bitmap); 1285 uint score = 1 + ((i - home_slot) & Klass::SECONDARY_SUPERS_TABLE_MASK); 1286 s.sample(score); 1287 } 1288 st->print("positive_lookup: "); s.print_on(st); 1289 } 1290 1291 static uint compute_distance_to_nearest_zero(int slot, uintx bitmap) { 1292 assert(~bitmap != 0, "no zeroes"); 1293 uintx start = rotate_right(bitmap, slot); 1294 return count_trailing_zeros(~start); 1295 } 1296 1297 static void print_negative_lookup_stats(uintx bitmap, outputStream* st) { 1298 LookupStats s; 1299 for (int slot = 0; slot < Klass::SECONDARY_SUPERS_TABLE_SIZE; slot++) { 1300 uint score = compute_distance_to_nearest_zero(slot, bitmap); 1301 s.sample(score); 1302 } 1303 st->print("negative_lookup: "); s.print_on(st); 1304 } 1305 1306 void Klass::print_secondary_supers_on(outputStream* st) const { 1307 if (secondary_supers() != nullptr) { 1308 st->print(" - "); st->print("%d elements;", _secondary_supers->length()); 1309 st->print_cr(" bitmap: " UINTX_FORMAT_X_0 ";", _secondary_supers_bitmap); 1310 if (_secondary_supers_bitmap != SECONDARY_SUPERS_BITMAP_EMPTY && 1311 _secondary_supers_bitmap != SECONDARY_SUPERS_BITMAP_FULL) { 1312 st->print(" - "); print_positive_lookup_stats(secondary_supers(), 1313 _secondary_supers_bitmap, st); st->cr(); 1314 st->print(" - "); print_negative_lookup_stats(_secondary_supers_bitmap, st); st->cr(); 1315 } 1316 } else { 1317 st->print("null"); 1318 } 1319 } 1320 1321 void Klass::on_secondary_supers_verification_failure(Klass* super, Klass* sub, bool linear_result, bool table_result, const char* msg) { 1322 ResourceMark rm; 1323 super->print(); 1324 sub->print(); 1325 fatal("%s: %s implements %s: linear_search: %d; table_lookup: %d", 1326 msg, sub->external_name(), super->external_name(), linear_result, table_result); 1327 } 1328 1329 static int expanded = 0; 1330 static int not_expanded = 0; 1331 static NumberSeq seq = NumberSeq(); 1332 1333 bool Klass::expand_for_hash(oop obj) const { 1334 assert(UseCompactObjectHeaders, "only with compact i-hash"); 1335 assert((size_t)hash_offset_in_bytes(obj) <= (obj->base_size_given_klass(obj->mark(), this) * HeapWordSize), "hash offset must be eq or lt base size: hash offset: %d, base size: " SIZE_FORMAT, hash_offset_in_bytes(obj), obj->base_size_given_klass(obj->mark(), this) * HeapWordSize); 1336 return obj->base_size_given_klass(obj->mark(), this) * HeapWordSize - hash_offset_in_bytes(obj) < (int)sizeof(uint32_t); 1337 }