1 /*
   2  * Copyright (c) 1997, 2018, 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/dictionary.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "gc_implementation/shared/markSweep.inline.hpp"
  31 #include "gc_interface/collectedHeap.inline.hpp"
  32 #include "memory/heapInspection.hpp"
  33 #include "memory/metadataFactory.hpp"
  34 #include "memory/oopFactory.hpp"
  35 #include "memory/resourceArea.hpp"
  36 #include "oops/instanceKlass.hpp"
  37 #include "oops/klass.inline.hpp"
  38 #include "oops/oop.inline2.hpp"
  39 #include "runtime/atomic.inline.hpp"
  40 #include "runtime/orderAccess.inline.hpp"
  41 #include "utilities/stack.hpp"
  42 #include "utilities/macros.hpp"
  43 #if INCLUDE_ALL_GCS
  44 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
  45 #include "gc_implementation/parallelScavenge/psParallelCompact.hpp"
  46 #include "gc_implementation/parallelScavenge/psPromotionManager.hpp"
  47 #include "gc_implementation/parallelScavenge/psScavenge.hpp"
  48 #endif // INCLUDE_ALL_GCS
  49 #if INCLUDE_JFR
  50 #include "jfr/support/jfrTraceIdExtension.hpp"
  51 #endif
  52 
  53 bool Klass::is_cloneable() const {
  54   return _access_flags.is_cloneable() ||
  55          is_subtype_of(SystemDictionary::Cloneable_klass());
  56 }
  57 
  58 void Klass::set_is_cloneable() {
  59   if (oop_is_instance() && InstanceKlass::cast(this)->reference_type() != REF_NONE) {
  60     // Reference cloning should not be intrinsified and always happen in JVM_Clone.
  61   } else {
  62     _access_flags.set_is_cloneable();
  63   }
  64 }
  65 
  66 void Klass::set_name(Symbol* n) {
  67   _name = n;
  68   if (_name != NULL) _name->increment_refcount();
  69 }
  70 
  71 bool Klass::is_subclass_of(const Klass* k) const {
  72   // Run up the super chain and check
  73   if (this == k) return true;
  74 
  75   Klass* t = const_cast<Klass*>(this)->super();
  76 
  77   while (t != NULL) {
  78     if (t == k) return true;
  79     t = t->super();
  80   }
  81   return false;
  82 }
  83 
  84 bool Klass::search_secondary_supers(Klass* k) const {
  85   // Put some extra logic here out-of-line, before the search proper.
  86   // This cuts down the size of the inline method.
  87 
  88   // This is necessary, since I am never in my own secondary_super list.
  89   if (this == k)
  90     return true;
  91   // Scan the array-of-objects for a match
  92   int cnt = secondary_supers()->length();
  93   for (int i = 0; i < cnt; i++) {
  94     if (secondary_supers()->at(i) == k) {
  95       ((Klass*)this)->set_secondary_super_cache(k);
  96       return true;
  97     }
  98   }
  99   return false;
 100 }
 101 
 102 // Return self, except for abstract classes with exactly 1
 103 // implementor.  Then return the 1 concrete implementation.
 104 Klass *Klass::up_cast_abstract() {
 105   Klass *r = this;
 106   while( r->is_abstract() ) {   // Receiver is abstract?
 107     Klass *s = r->subklass();   // Check for exactly 1 subklass
 108     if( !s || s->next_sibling() ) // Oops; wrong count; give up
 109       return this;              // Return 'this' as a no-progress flag
 110     r = s;                    // Loop till find concrete class
 111   }
 112   return r;                   // Return the 1 concrete class
 113 }
 114 
 115 // Find LCA in class hierarchy
 116 Klass *Klass::LCA( Klass *k2 ) {
 117   Klass *k1 = this;
 118   while( 1 ) {
 119     if( k1->is_subtype_of(k2) ) return k2;
 120     if( k2->is_subtype_of(k1) ) return k1;
 121     k1 = k1->super();
 122     k2 = k2->super();
 123   }
 124 }
 125 
 126 
 127 void Klass::check_valid_for_instantiation(bool throwError, TRAPS) {
 128   ResourceMark rm(THREAD);
 129   THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
 130             : vmSymbols::java_lang_InstantiationException(), external_name());
 131 }
 132 
 133 
 134 void Klass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) {
 135   THROW(vmSymbols::java_lang_ArrayStoreException());
 136 }
 137 
 138 
 139 void Klass::initialize(TRAPS) {
 140   ShouldNotReachHere();
 141 }
 142 
 143 bool Klass::compute_is_subtype_of(Klass* k) {
 144   assert(k->is_klass(), "argument must be a class");
 145   return is_subclass_of(k);
 146 }
 147 
 148 Klass* Klass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
 149 #ifdef ASSERT
 150   tty->print_cr("Error: find_field called on a klass oop."
 151                 " Likely error: reflection method does not correctly"
 152                 " wrap return value in a mirror object.");
 153 #endif
 154   ShouldNotReachHere();
 155   return NULL;
 156 }
 157 
 158 Method* Klass::uncached_lookup_method(Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode) const {
 159 #ifdef ASSERT
 160   tty->print_cr("Error: uncached_lookup_method called on a klass oop."
 161                 " Likely error: reflection method does not correctly"
 162                 " wrap return value in a mirror object.");
 163 #endif
 164   ShouldNotReachHere();
 165   return NULL;
 166 }
 167 
 168 void* Klass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) throw() {
 169   return Metaspace::allocate(loader_data, word_size, /*read_only*/false,
 170                              MetaspaceObj::ClassType, CHECK_NULL);
 171 }
 172 
 173 Klass::Klass() {
 174   Klass* k = this;
 175 
 176   // Preinitialize supertype information.
 177   // A later call to initialize_supers() may update these settings:
 178   set_super(NULL);
 179   for (juint i = 0; i < Klass::primary_super_limit(); i++) {
 180     _primary_supers[i] = NULL;
 181   }
 182   set_secondary_supers(NULL);
 183   set_secondary_super_cache(NULL);
 184   _primary_supers[0] = k;
 185   set_super_check_offset(in_bytes(primary_supers_offset()));
 186 
 187   // The constructor is used from init_self_patching_vtbl_list,
 188   // which doesn't zero out the memory before calling the constructor.
 189   // Need to set the field explicitly to not hit an assert that the field
 190   // should be NULL before setting it.
 191   _java_mirror = NULL;
 192 
 193   set_modifier_flags(0);
 194   set_layout_helper(Klass::_lh_neutral_value);
 195   set_name(NULL);
 196   AccessFlags af;
 197   af.set_flags(0);
 198   set_access_flags(af);
 199   set_subklass(NULL);
 200   set_next_sibling(NULL);
 201   set_next_link(NULL);
 202 
 203   set_prototype_header(markOopDesc::prototype());
 204   set_biased_lock_revocation_count(0);
 205   set_last_biased_lock_bulk_revocation_time(0);
 206 
 207   // The klass doesn't have any references at this point.
 208   clear_modified_oops();
 209   clear_accumulated_modified_oops();
 210   _shared_class_path_index = -1;
 211 }
 212 
 213 jint Klass::array_layout_helper(BasicType etype) {
 214   assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype");
 215   // Note that T_ARRAY is not allowed here.
 216   int  hsize = arrayOopDesc::base_offset_in_bytes(etype);
 217   int  esize = type2aelembytes(etype);
 218   bool isobj = (etype == T_OBJECT);
 219   int  tag   =  isobj ? _lh_array_tag_obj_value : _lh_array_tag_type_value;
 220   int lh = array_layout_helper(tag, hsize, etype, exact_log2(esize));
 221 
 222   assert(lh < (int)_lh_neutral_value, "must look like an array layout");
 223   assert(layout_helper_is_array(lh), "correct kind");
 224   assert(layout_helper_is_objArray(lh) == isobj, "correct kind");
 225   assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind");
 226   assert(layout_helper_header_size(lh) == hsize, "correct decode");
 227   assert(layout_helper_element_type(lh) == etype, "correct decode");
 228   assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode");
 229 
 230   return lh;
 231 }
 232 
 233 bool Klass::can_be_primary_super_slow() const {
 234   if (super() == NULL)
 235     return true;
 236   else if (super()->super_depth() >= primary_super_limit()-1)
 237     return false;
 238   else
 239     return true;
 240 }
 241 
 242 void Klass::initialize_supers(Klass* k, TRAPS) {
 243   if (FastSuperclassLimit == 0) {
 244     // None of the other machinery matters.
 245     set_super(k);
 246     return;
 247   }
 248   if (k == NULL) {
 249     set_super(NULL);
 250     _primary_supers[0] = this;
 251     assert(super_depth() == 0, "Object must already be initialized properly");
 252   } else if (k != super() || k == SystemDictionary::Object_klass()) {
 253     assert(super() == NULL || super() == SystemDictionary::Object_klass(),
 254            "initialize this only once to a non-trivial value");
 255     set_super(k);
 256     Klass* sup = k;
 257     int sup_depth = sup->super_depth();
 258     juint my_depth  = MIN2(sup_depth + 1, (int)primary_super_limit());
 259     if (!can_be_primary_super_slow())
 260       my_depth = primary_super_limit();
 261     for (juint i = 0; i < my_depth; i++) {
 262       _primary_supers[i] = sup->_primary_supers[i];
 263     }
 264     Klass* *super_check_cell;
 265     if (my_depth < primary_super_limit()) {
 266       _primary_supers[my_depth] = this;
 267       super_check_cell = &_primary_supers[my_depth];
 268     } else {
 269       // Overflow of the primary_supers array forces me to be secondary.
 270       super_check_cell = &_secondary_super_cache;
 271     }
 272     set_super_check_offset((address)super_check_cell - (address) this);
 273 
 274 #ifdef ASSERT
 275     {
 276       juint j = super_depth();
 277       assert(j == my_depth, "computed accessor gets right answer");
 278       Klass* t = this;
 279       while (!t->can_be_primary_super()) {
 280         t = t->super();
 281         j = t->super_depth();
 282       }
 283       for (juint j1 = j+1; j1 < primary_super_limit(); j1++) {
 284         assert(primary_super_of_depth(j1) == NULL, "super list padding");
 285       }
 286       while (t != NULL) {
 287         assert(primary_super_of_depth(j) == t, "super list initialization");
 288         t = t->super();
 289         --j;
 290       }
 291       assert(j == (juint)-1, "correct depth count");
 292     }
 293 #endif
 294   }
 295 
 296   if (secondary_supers() == NULL) {
 297     KlassHandle this_kh (THREAD, this);
 298 
 299     // Now compute the list of secondary supertypes.
 300     // Secondaries can occasionally be on the super chain,
 301     // if the inline "_primary_supers" array overflows.
 302     int extras = 0;
 303     Klass* p;
 304     for (p = super(); !(p == NULL || p->can_be_primary_super()); p = p->super()) {
 305       ++extras;
 306     }
 307 
 308     ResourceMark rm(THREAD);  // need to reclaim GrowableArrays allocated below
 309 
 310     // Compute the "real" non-extra secondaries.
 311     GrowableArray<Klass*>* secondaries = compute_secondary_supers(extras);
 312     if (secondaries == NULL) {
 313       // secondary_supers set by compute_secondary_supers
 314       return;
 315     }
 316 
 317     GrowableArray<Klass*>* primaries = new GrowableArray<Klass*>(extras);
 318 
 319     for (p = this_kh->super(); !(p == NULL || p->can_be_primary_super()); p = p->super()) {
 320       int i;                    // Scan for overflow primaries being duplicates of 2nd'arys
 321 
 322       // This happens frequently for very deeply nested arrays: the
 323       // primary superclass chain overflows into the secondary.  The
 324       // secondary list contains the element_klass's secondaries with
 325       // an extra array dimension added.  If the element_klass's
 326       // secondary list already contains some primary overflows, they
 327       // (with the extra level of array-ness) will collide with the
 328       // normal primary superclass overflows.
 329       for( i = 0; i < secondaries->length(); i++ ) {
 330         if( secondaries->at(i) == p )
 331           break;
 332       }
 333       if( i < secondaries->length() )
 334         continue;               // It's a dup, don't put it in
 335       primaries->push(p);
 336     }
 337     // Combine the two arrays into a metadata object to pack the array.
 338     // The primaries are added in the reverse order, then the secondaries.
 339     int new_length = primaries->length() + secondaries->length();
 340     Array<Klass*>* s2 = MetadataFactory::new_array<Klass*>(
 341                                        class_loader_data(), new_length, CHECK);
 342     int fill_p = primaries->length();
 343     for (int j = 0; j < fill_p; j++) {
 344       s2->at_put(j, primaries->pop());  // add primaries in reverse order.
 345     }
 346     for( int j = 0; j < secondaries->length(); j++ ) {
 347       s2->at_put(j+fill_p, secondaries->at(j));  // add secondaries on the end.
 348     }
 349 
 350   #ifdef ASSERT
 351       // We must not copy any NULL placeholders left over from bootstrap.
 352     for (int j = 0; j < s2->length(); j++) {
 353       assert(s2->at(j) != NULL, "correct bootstrapping order");
 354     }
 355   #endif
 356 
 357     this_kh->set_secondary_supers(s2);
 358   }
 359 }
 360 
 361 GrowableArray<Klass*>* Klass::compute_secondary_supers(int num_extra_slots) {
 362   assert(num_extra_slots == 0, "override for complex klasses");
 363   set_secondary_supers(Universe::the_empty_klass_array());
 364   return NULL;
 365 }
 366 
 367 
 368 Klass* Klass::subklass() const {
 369   return _subklass == NULL ? NULL : _subklass;
 370 }
 371 
 372 InstanceKlass* Klass::superklass() const {
 373   assert(super() == NULL || super()->oop_is_instance(), "must be instance klass");
 374   return _super == NULL ? NULL : InstanceKlass::cast(_super);
 375 }
 376 
 377 Klass* Klass::next_sibling() const {
 378   return _next_sibling == NULL ? NULL : _next_sibling;
 379 }
 380 
 381 void Klass::set_subklass(Klass* s) {
 382   assert(s != this, "sanity check");
 383   _subklass = s;
 384 }
 385 
 386 void Klass::set_next_sibling(Klass* s) {
 387   assert(s != this, "sanity check");
 388   _next_sibling = s;
 389 }
 390 
 391 void Klass::append_to_sibling_list() {
 392   debug_only(verify();)
 393   // add ourselves to superklass' subklass list
 394   InstanceKlass* super = superklass();
 395   if (super == NULL) return;        // special case: class Object
 396   assert((!super->is_interface()    // interfaces cannot be supers
 397           && (super->superklass() == NULL || !is_interface())),
 398          "an interface can only be a subklass of Object");
 399   Klass* prev_first_subklass = super->subklass_oop();
 400   if (prev_first_subklass != NULL) {
 401     // set our sibling to be the superklass' previous first subklass
 402     set_next_sibling(prev_first_subklass);
 403   }
 404   // make ourselves the superklass' first subklass
 405   super->set_subklass(this);
 406   debug_only(verify();)
 407 }
 408 
 409 bool Klass::is_loader_alive(BoolObjectClosure* is_alive) {
 410 #ifdef ASSERT
 411   // The class is alive iff the class loader is alive.
 412   oop loader = class_loader();
 413   bool loader_alive = (loader == NULL) || is_alive->do_object_b(loader);
 414 #endif // ASSERT
 415 
 416   // The class is alive if it's mirror is alive (which should be marked if the
 417   // loader is alive) unless it's an anoymous class.
 418   bool mirror_alive = is_alive->do_object_b(java_mirror());
 419   assert(!mirror_alive || loader_alive, "loader must be alive if the mirror is"
 420                         " but not the other way around with anonymous classes");
 421   return mirror_alive;
 422 }
 423 
 424 void Klass::clean_weak_klass_links(BoolObjectClosure* is_alive, bool clean_alive_klasses) {
 425   if (!ClassUnloading) {
 426     return;
 427   }
 428 
 429   Klass* root = SystemDictionary::Object_klass();
 430   Stack<Klass*, mtGC> stack;
 431 
 432   stack.push(root);
 433   while (!stack.is_empty()) {
 434     Klass* current = stack.pop();
 435 
 436     assert(current->is_loader_alive(is_alive), "just checking, this should be live");
 437 
 438     // Find and set the first alive subklass
 439     Klass* sub = current->subklass_oop();
 440     while (sub != NULL && !sub->is_loader_alive(is_alive)) {
 441 #ifndef PRODUCT
 442       if (TraceClassUnloading && WizardMode) {
 443         ResourceMark rm;
 444         tty->print_cr("[Unlinking class (subclass) %s]", sub->external_name());
 445       }
 446 #endif
 447       sub = sub->next_sibling_oop();
 448     }
 449     current->set_subklass(sub);
 450     if (sub != NULL) {
 451       stack.push(sub);
 452     }
 453 
 454     // Find and set the first alive sibling
 455     Klass* sibling = current->next_sibling_oop();
 456     while (sibling != NULL && !sibling->is_loader_alive(is_alive)) {
 457       if (TraceClassUnloading && WizardMode) {
 458         ResourceMark rm;
 459         tty->print_cr("[Unlinking class (sibling) %s]", sibling->external_name());
 460       }
 461       sibling = sibling->next_sibling_oop();
 462     }
 463     current->set_next_sibling(sibling);
 464     if (sibling != NULL) {
 465       stack.push(sibling);
 466     }
 467 
 468     // Clean the implementors list and method data.
 469     if (clean_alive_klasses && current->oop_is_instance()) {
 470       InstanceKlass* ik = InstanceKlass::cast(current);
 471       ik->clean_weak_instanceklass_links(is_alive);
 472 
 473       // JVMTI RedefineClasses creates previous versions that are not in
 474       // the class hierarchy, so process them here.
 475       while ((ik = ik->previous_versions()) != NULL) {
 476         ik->clean_weak_instanceklass_links(is_alive);
 477       }
 478     }
 479   }
 480 }
 481 
 482 void Klass::klass_update_barrier_set(oop v) {
 483   record_modified_oops();
 484 }
 485 
 486 // This barrier is used by G1 to remember the old oop values, so
 487 // that we don't forget any objects that were live at the snapshot at
 488 // the beginning. This function is only used when we write oops into Klasses.
 489 void Klass::klass_update_barrier_set_pre(oop* p, oop v) {
 490 #if INCLUDE_ALL_GCS
 491   if (UseG1GC) {
 492     oop obj = *p;
 493     if (obj != NULL) {
 494       G1SATBCardTableModRefBS::enqueue(obj);
 495     }
 496   }
 497 #endif
 498 }
 499 
 500 void Klass::klass_oop_store(oop* p, oop v) {
 501   assert(!Universe::heap()->is_in_reserved((void*)p), "Should store pointer into metadata");
 502   assert(v == NULL || Universe::heap()->is_in_reserved((void*)v), "Should store pointer to an object");
 503 
 504   // do the store
 505   if (always_do_update_barrier) {
 506     klass_oop_store((volatile oop*)p, v);
 507   } else {
 508     klass_update_barrier_set_pre(p, v);
 509     *p = v;
 510     klass_update_barrier_set(v);
 511   }
 512 }
 513 
 514 void Klass::klass_oop_store(volatile oop* p, oop v) {
 515   assert(!Universe::heap()->is_in_reserved((void*)p), "Should store pointer into metadata");
 516   assert(v == NULL || Universe::heap()->is_in_reserved((void*)v), "Should store pointer to an object");
 517 
 518   klass_update_barrier_set_pre((oop*)p, v); // Cast away volatile.
 519   OrderAccess::release_store_ptr(p, v);
 520   klass_update_barrier_set(v);
 521 }
 522 
 523 void Klass::oops_do(OopClosure* cl) {
 524   cl->do_oop(&_java_mirror);
 525 }
 526 
 527 void Klass::remove_unshareable_info() {
 528   assert (DumpSharedSpaces, "only called for DumpSharedSpaces");
 529 
 530   JFR_ONLY(REMOVE_ID(this);)
 531   set_subklass(NULL);
 532   set_next_sibling(NULL);
 533   // Clear the java mirror
 534   set_java_mirror(NULL);
 535   set_next_link(NULL);
 536 
 537   // Null out class_loader_data because we don't share that yet.
 538   set_class_loader_data(NULL);
 539 }
 540 
 541 void Klass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
 542   JFR_ONLY(RESTORE_ID(this);)
 543   // If an exception happened during CDS restore, some of these fields may already be
 544   // set.  We leave the class on the CLD list, even if incomplete so that we don't
 545   // modify the CLD list outside a safepoint.
 546   if (class_loader_data() == NULL) {
 547     // Restore class_loader_data
 548     set_class_loader_data(loader_data);
 549 
 550     // Add to class loader list first before creating the mirror
 551     // (same order as class file parsing)
 552     loader_data->add_class(this);
 553   }
 554 
 555   // Recreate the class mirror.
 556   // Only recreate it if not present.  A previous attempt to restore may have
 557   // gotten an OOM later but keep the mirror if it was created.
 558   if (java_mirror() == NULL) {
 559     java_lang_Class::create_mirror(this, class_loader(), protection_domain, CHECK);
 560   }
 561 }
 562 
 563 Klass* Klass::array_klass_or_null(int rank) {
 564   EXCEPTION_MARK;
 565   // No exception can be thrown by array_klass_impl when called with or_null == true.
 566   // (In anycase, the execption mark will fail if it do so)
 567   return array_klass_impl(true, rank, THREAD);
 568 }
 569 
 570 
 571 Klass* Klass::array_klass_or_null() {
 572   EXCEPTION_MARK;
 573   // No exception can be thrown by array_klass_impl when called with or_null == true.
 574   // (In anycase, the execption mark will fail if it do so)
 575   return array_klass_impl(true, THREAD);
 576 }
 577 
 578 
 579 Klass* Klass::array_klass_impl(bool or_null, int rank, TRAPS) {
 580   fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass");
 581   return NULL;
 582 }
 583 
 584 
 585 Klass* Klass::array_klass_impl(bool or_null, TRAPS) {
 586   fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass");
 587   return NULL;
 588 }
 589 
 590 oop Klass::class_loader() const { return class_loader_data()->class_loader(); }
 591 
 592 const char* Klass::external_name() const {
 593   if (oop_is_instance()) {
 594     InstanceKlass* ik = (InstanceKlass*) this;
 595     if (ik->is_anonymous()) {
 596       assert(EnableInvokeDynamic, "");
 597       intptr_t hash = 0;
 598       if (ik->java_mirror() != NULL) {
 599         // java_mirror might not be created yet, return 0 as hash.
 600         hash = ik->java_mirror()->identity_hash();
 601       }
 602       char     hash_buf[40];
 603       sprintf(hash_buf, "/" UINTX_FORMAT, (uintx)hash);
 604       size_t   hash_len = strlen(hash_buf);
 605 
 606       size_t result_len = name()->utf8_length();
 607       char*  result     = NEW_RESOURCE_ARRAY(char, result_len + hash_len + 1);
 608       name()->as_klass_external_name(result, (int) result_len + 1);
 609       assert(strlen(result) == result_len, "");
 610       strcpy(result + result_len, hash_buf);
 611       assert(strlen(result) == result_len + hash_len, "");
 612       return result;
 613     }
 614   }
 615   if (name() == NULL)  return "<unknown>";
 616   return name()->as_klass_external_name();
 617 }
 618 
 619 
 620 const char* Klass::signature_name() const {
 621   if (name() == NULL)  return "<unknown>";
 622   return name()->as_C_string();
 623 }
 624 
 625 // Unless overridden, modifier_flags is 0.
 626 jint Klass::compute_modifier_flags(TRAPS) const {
 627   return 0;
 628 }
 629 
 630 int Klass::atomic_incr_biased_lock_revocation_count() {
 631   return (int) Atomic::add(1, &_biased_lock_revocation_count);
 632 }
 633 
 634 // Unless overridden, jvmti_class_status has no flags set.
 635 jint Klass::jvmti_class_status() const {
 636   return 0;
 637 }
 638 
 639 
 640 // Printing
 641 
 642 void Klass::print_on(outputStream* st) const {
 643   ResourceMark rm;
 644   // print title
 645   st->print("%s", internal_name());
 646   print_address_on(st);
 647   st->cr();
 648 }
 649 
 650 void Klass::oop_print_on(oop obj, outputStream* st) {
 651   ResourceMark rm;
 652   // print title
 653   st->print_cr("%s ", internal_name());
 654   obj->print_address_on(st);
 655 
 656   if (WizardMode) {
 657      // print header
 658      obj->mark()->print_on(st);
 659   }
 660 
 661   // print class
 662   st->print(" - klass: ");
 663   obj->klass()->print_value_on(st);
 664   st->cr();
 665 }
 666 
 667 void Klass::oop_print_value_on(oop obj, outputStream* st) {
 668   // print title
 669   ResourceMark rm;              // Cannot print in debug mode without this
 670   st->print("%s", internal_name());
 671   obj->print_address_on(st);
 672 }
 673 
 674 #if INCLUDE_SERVICES
 675 // Size Statistics
 676 void Klass::collect_statistics(KlassSizeStats *sz) const {
 677   sz->_klass_bytes = sz->count(this);
 678   sz->_mirror_bytes = sz->count(java_mirror());
 679   sz->_secondary_supers_bytes = sz->count_array(secondary_supers());
 680 
 681   sz->_ro_bytes += sz->_secondary_supers_bytes;
 682   sz->_rw_bytes += sz->_klass_bytes + sz->_mirror_bytes;
 683 }
 684 #endif // INCLUDE_SERVICES
 685 
 686 // Verification
 687 
 688 void Klass::verify_on(outputStream* st) {
 689 
 690   // This can be expensive, but it is worth checking that this klass is actually
 691   // in the CLD graph but not in production.
 692   assert(Metaspace::contains((address)this), "Should be");
 693 
 694   guarantee(this->is_klass(),"should be klass");
 695 
 696   if (super() != NULL) {
 697     guarantee(super()->is_klass(), "should be klass");
 698   }
 699   if (secondary_super_cache() != NULL) {
 700     Klass* ko = secondary_super_cache();
 701     guarantee(ko->is_klass(), "should be klass");
 702   }
 703   for ( uint i = 0; i < primary_super_limit(); i++ ) {
 704     Klass* ko = _primary_supers[i];
 705     if (ko != NULL) {
 706       guarantee(ko->is_klass(), "should be klass");
 707     }
 708   }
 709 
 710   if (java_mirror() != NULL) {
 711     guarantee(java_mirror()->is_oop(), "should be instance");
 712   }
 713 }
 714 
 715 void Klass::oop_verify_on(oop obj, outputStream* st) {
 716   guarantee(obj->is_oop(),  "should be oop");
 717   guarantee(obj->klass()->is_klass(), "klass field is not a klass");
 718 }
 719 
 720 #ifndef PRODUCT
 721 
 722 bool Klass::verify_vtable_index(int i) {
 723   if (oop_is_instance()) {
 724     int limit = ((InstanceKlass*)this)->vtable_length()/vtableEntry::size();
 725     assert(i >= 0 && i < limit, err_msg("index %d out of bounds %d", i, limit));
 726   } else {
 727     assert(oop_is_array(), "Must be");
 728     int limit = ((ArrayKlass*)this)->vtable_length()/vtableEntry::size();
 729     assert(i >= 0 && i < limit, err_msg("index %d out of bounds %d", i, limit));
 730   }
 731   return true;
 732 }
 733 
 734 bool Klass::verify_itable_index(int i) {
 735   assert(oop_is_instance(), "");
 736   int method_count = klassItable::method_count_for_interface(this);
 737   assert(i >= 0 && i < method_count, "index out of bounds");
 738   return true;
 739 }
 740 
 741 #endif
 742 
 743 /////////////// Unit tests ///////////////
 744 
 745 #ifndef PRODUCT
 746 
 747 class TestKlass {
 748  public:
 749   static void test_oop_is_instanceClassLoader() {
 750     assert(SystemDictionary::ClassLoader_klass()->oop_is_instanceClassLoader(), "assert");
 751     assert(!SystemDictionary::String_klass()->oop_is_instanceClassLoader(), "assert");
 752   }
 753 };
 754 
 755 void TestKlass_test() {
 756   TestKlass::test_oop_is_instanceClassLoader();
 757 }
 758 
 759 #endif