1 /*
   2  * Copyright (c) 1997, 2017, 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/dictionary.hpp"
  27 #include "classfile/javaClasses.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "gc/shared/collectedHeap.inline.hpp"
  31 #include "logging/log.hpp"
  32 #include "memory/heapInspection.hpp"
  33 #include "memory/metadataFactory.hpp"
  34 #include "memory/metaspaceClosure.hpp"
  35 #include "memory/metaspaceShared.hpp"
  36 #include "memory/oopFactory.hpp"
  37 #include "memory/resourceArea.hpp"
  38 #include "oops/instanceKlass.hpp"
  39 #include "oops/klass.inline.hpp"
  40 #include "oops/oop.inline.hpp"
  41 #include "runtime/atomic.hpp"
  42 #include "runtime/orderAccess.inline.hpp"
  43 #include "trace/traceMacros.hpp"
  44 #include "utilities/macros.hpp"
  45 #include "utilities/stack.inline.hpp"
  46 
  47 bool Klass::is_cloneable() const {
  48   return _access_flags.is_cloneable_fast() ||
  49          is_subtype_of(SystemDictionary::Cloneable_klass());
  50 }
  51 
  52 void Klass::set_is_cloneable() {
  53   if (name() != vmSymbols::java_lang_invoke_MemberName()) {
  54     _access_flags.set_is_cloneable_fast();
  55   } else {
  56     assert(is_final(), "no subclasses allowed");
  57     // MemberName cloning should not be intrinsified and always happen in JVM_Clone.
  58   }
  59 }
  60 
  61 void Klass::set_name(Symbol* n) {
  62   _name = n;
  63   if (_name != NULL) _name->increment_refcount();
  64 }
  65 
  66 bool Klass::is_subclass_of(const Klass* k) const {
  67   // Run up the super chain and check
  68   if (this == k) return true;
  69 
  70   Klass* t = const_cast<Klass*>(this)->super();
  71 
  72   while (t != NULL) {
  73     if (t == k) return true;
  74     t = t->super();
  75   }
  76   return false;
  77 }
  78 
  79 bool Klass::search_secondary_supers(Klass* k) const {
  80   // Put some extra logic here out-of-line, before the search proper.
  81   // This cuts down the size of the inline method.
  82 
  83   // This is necessary, since I am never in my own secondary_super list.
  84   if (this == k)
  85     return true;
  86   // Scan the array-of-objects for a match
  87   int cnt = secondary_supers()->length();
  88   for (int i = 0; i < cnt; i++) {
  89     if (secondary_supers()->at(i) == k) {
  90       ((Klass*)this)->set_secondary_super_cache(k);
  91       return true;
  92     }
  93   }
  94   return false;
  95 }
  96 
  97 // Return self, except for abstract classes with exactly 1
  98 // implementor.  Then return the 1 concrete implementation.
  99 Klass *Klass::up_cast_abstract() {
 100   Klass *r = this;
 101   while( r->is_abstract() ) {   // Receiver is abstract?
 102     Klass *s = r->subklass();   // Check for exactly 1 subklass
 103     if( !s || s->next_sibling() ) // Oops; wrong count; give up
 104       return this;              // Return 'this' as a no-progress flag
 105     r = s;                    // Loop till find concrete class
 106   }
 107   return r;                   // Return the 1 concrete class
 108 }
 109 
 110 // Find LCA in class hierarchy
 111 Klass *Klass::LCA( Klass *k2 ) {
 112   Klass *k1 = this;
 113   while( 1 ) {
 114     if( k1->is_subtype_of(k2) ) return k2;
 115     if( k2->is_subtype_of(k1) ) return k1;
 116     k1 = k1->super();
 117     k2 = k2->super();
 118   }
 119 }
 120 
 121 
 122 void Klass::check_valid_for_instantiation(bool throwError, TRAPS) {
 123   ResourceMark rm(THREAD);
 124   THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
 125             : vmSymbols::java_lang_InstantiationException(), external_name());
 126 }
 127 
 128 
 129 void Klass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) {
 130   THROW(vmSymbols::java_lang_ArrayStoreException());
 131 }
 132 
 133 
 134 void Klass::initialize(TRAPS) {
 135   ShouldNotReachHere();
 136 }
 137 
 138 bool Klass::compute_is_subtype_of(Klass* k) {
 139   assert(k->is_klass(), "argument must be a class");
 140   return is_subclass_of(k);
 141 }
 142 
 143 Klass* Klass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
 144 #ifdef ASSERT
 145   tty->print_cr("Error: find_field called on a klass oop."
 146                 " Likely error: reflection method does not correctly"
 147                 " wrap return value in a mirror object.");
 148 #endif
 149   ShouldNotReachHere();
 150   return NULL;
 151 }
 152 
 153 Method* Klass::uncached_lookup_method(const Symbol* name, const Symbol* signature, OverpassLookupMode overpass_mode) const {
 154 #ifdef ASSERT
 155   tty->print_cr("Error: uncached_lookup_method called on a klass oop."
 156                 " Likely error: reflection method does not correctly"
 157                 " wrap return value in a mirror object.");
 158 #endif
 159   ShouldNotReachHere();
 160   return NULL;
 161 }
 162 
 163 void* Klass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) throw() {
 164   return Metaspace::allocate(loader_data, word_size, MetaspaceObj::ClassType, THREAD);
 165 }
 166 
 167 // "Normal" instantiation is preceeded by a MetaspaceObj allocation
 168 // which zeros out memory - calloc equivalent.
 169 // The constructor is also used from CppVtableCloner,
 170 // which doesn't zero out the memory before calling the constructor.
 171 // Need to set the _java_mirror field explicitly to not hit an assert that the field
 172 // should be NULL before setting it.
 173 Klass::Klass() : _prototype_header(markOopDesc::prototype()),
 174                  _shared_class_path_index(-1),
 175                  _java_mirror(NULL) {
 176 
 177   _primary_supers[0] = this;
 178   set_super_check_offset(in_bytes(primary_supers_offset()));
 179 }
 180 
 181 jint Klass::array_layout_helper(BasicType etype) {
 182   assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype");
 183   // Note that T_ARRAY is not allowed here.
 184   int  hsize = arrayOopDesc::base_offset_in_bytes(etype);
 185   int  esize = type2aelembytes(etype);
 186   bool isobj = (etype == T_OBJECT);
 187   int  tag   =  isobj ? _lh_array_tag_obj_value : _lh_array_tag_type_value;
 188   int lh = array_layout_helper(tag, hsize, etype, exact_log2(esize));
 189 
 190   assert(lh < (int)_lh_neutral_value, "must look like an array layout");
 191   assert(layout_helper_is_array(lh), "correct kind");
 192   assert(layout_helper_is_objArray(lh) == isobj, "correct kind");
 193   assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind");
 194   assert(layout_helper_header_size(lh) == hsize, "correct decode");
 195   assert(layout_helper_element_type(lh) == etype, "correct decode");
 196   assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode");
 197 
 198   return lh;
 199 }
 200 
 201 bool Klass::can_be_primary_super_slow() const {
 202   if (super() == NULL)
 203     return true;
 204   else if (super()->super_depth() >= primary_super_limit()-1)
 205     return false;
 206   else
 207     return true;
 208 }
 209 
 210 void Klass::initialize_supers(Klass* k, TRAPS) {
 211   if (FastSuperclassLimit == 0) {
 212     // None of the other machinery matters.
 213     set_super(k);
 214     return;
 215   }
 216   if (k == NULL) {
 217     set_super(NULL);
 218     _primary_supers[0] = this;
 219     assert(super_depth() == 0, "Object must already be initialized properly");
 220   } else if (k != super() || k == SystemDictionary::Object_klass()) {
 221     assert(super() == NULL || super() == SystemDictionary::Object_klass(),
 222            "initialize this only once to a non-trivial value");
 223     set_super(k);
 224     Klass* sup = k;
 225     int sup_depth = sup->super_depth();
 226     juint my_depth  = MIN2(sup_depth + 1, (int)primary_super_limit());
 227     if (!can_be_primary_super_slow())
 228       my_depth = primary_super_limit();
 229     for (juint i = 0; i < my_depth; i++) {
 230       _primary_supers[i] = sup->_primary_supers[i];
 231     }
 232     Klass* *super_check_cell;
 233     if (my_depth < primary_super_limit()) {
 234       _primary_supers[my_depth] = this;
 235       super_check_cell = &_primary_supers[my_depth];
 236     } else {
 237       // Overflow of the primary_supers array forces me to be secondary.
 238       super_check_cell = &_secondary_super_cache;
 239     }
 240     set_super_check_offset((address)super_check_cell - (address) this);
 241 
 242 #ifdef ASSERT
 243     {
 244       juint j = super_depth();
 245       assert(j == my_depth, "computed accessor gets right answer");
 246       Klass* t = this;
 247       while (!t->can_be_primary_super()) {
 248         t = t->super();
 249         j = t->super_depth();
 250       }
 251       for (juint j1 = j+1; j1 < primary_super_limit(); j1++) {
 252         assert(primary_super_of_depth(j1) == NULL, "super list padding");
 253       }
 254       while (t != NULL) {
 255         assert(primary_super_of_depth(j) == t, "super list initialization");
 256         t = t->super();
 257         --j;
 258       }
 259       assert(j == (juint)-1, "correct depth count");
 260     }
 261 #endif
 262   }
 263 
 264   if (secondary_supers() == NULL) {
 265 
 266     // Now compute the list of secondary supertypes.
 267     // Secondaries can occasionally be on the super chain,
 268     // if the inline "_primary_supers" array overflows.
 269     int extras = 0;
 270     Klass* p;
 271     for (p = super(); !(p == NULL || p->can_be_primary_super()); p = p->super()) {
 272       ++extras;
 273     }
 274 
 275     ResourceMark rm(THREAD);  // need to reclaim GrowableArrays allocated below
 276 
 277     // Compute the "real" non-extra secondaries.
 278     GrowableArray<Klass*>* secondaries = compute_secondary_supers(extras);
 279     if (secondaries == NULL) {
 280       // secondary_supers set by compute_secondary_supers
 281       return;
 282     }
 283 
 284     GrowableArray<Klass*>* primaries = new GrowableArray<Klass*>(extras);
 285 
 286     for (p = super(); !(p == NULL || p->can_be_primary_super()); p = p->super()) {
 287       int i;                    // Scan for overflow primaries being duplicates of 2nd'arys
 288 
 289       // This happens frequently for very deeply nested arrays: the
 290       // primary superclass chain overflows into the secondary.  The
 291       // secondary list contains the element_klass's secondaries with
 292       // an extra array dimension added.  If the element_klass's
 293       // secondary list already contains some primary overflows, they
 294       // (with the extra level of array-ness) will collide with the
 295       // normal primary superclass overflows.
 296       for( i = 0; i < secondaries->length(); i++ ) {
 297         if( secondaries->at(i) == p )
 298           break;
 299       }
 300       if( i < secondaries->length() )
 301         continue;               // It's a dup, don't put it in
 302       primaries->push(p);
 303     }
 304     // Combine the two arrays into a metadata object to pack the array.
 305     // The primaries are added in the reverse order, then the secondaries.
 306     int new_length = primaries->length() + secondaries->length();
 307     Array<Klass*>* s2 = MetadataFactory::new_array<Klass*>(
 308                                        class_loader_data(), new_length, CHECK);
 309     int fill_p = primaries->length();
 310     for (int j = 0; j < fill_p; j++) {
 311       s2->at_put(j, primaries->pop());  // add primaries in reverse order.
 312     }
 313     for( int j = 0; j < secondaries->length(); j++ ) {
 314       s2->at_put(j+fill_p, secondaries->at(j));  // add secondaries on the end.
 315     }
 316 
 317   #ifdef ASSERT
 318       // We must not copy any NULL placeholders left over from bootstrap.
 319     for (int j = 0; j < s2->length(); j++) {
 320       assert(s2->at(j) != NULL, "correct bootstrapping order");
 321     }
 322   #endif
 323 
 324     set_secondary_supers(s2);
 325   }
 326 }
 327 
 328 GrowableArray<Klass*>* Klass::compute_secondary_supers(int num_extra_slots) {
 329   assert(num_extra_slots == 0, "override for complex klasses");
 330   set_secondary_supers(Universe::the_empty_klass_array());
 331   return NULL;
 332 }
 333 
 334 
 335 InstanceKlass* Klass::superklass() const {
 336   assert(super() == NULL || super()->is_instance_klass(), "must be instance klass");
 337   return _super == NULL ? NULL : InstanceKlass::cast(_super);
 338 }
 339 
 340 void Klass::set_subklass(Klass* s) {
 341   assert(s != this, "sanity check");
 342   _subklass = s;
 343 }
 344 
 345 void Klass::set_next_sibling(Klass* s) {
 346   assert(s != this, "sanity check");
 347   _next_sibling = s;
 348 }
 349 
 350 void Klass::append_to_sibling_list() {
 351   debug_only(verify();)
 352   // add ourselves to superklass' subklass list
 353   InstanceKlass* super = superklass();
 354   if (super == NULL) return;        // special case: class Object
 355   assert((!super->is_interface()    // interfaces cannot be supers
 356           && (super->superklass() == NULL || !is_interface())),
 357          "an interface can only be a subklass of Object");
 358   Klass* prev_first_subklass = super->subklass();
 359   if (prev_first_subklass != NULL) {
 360     // set our sibling to be the superklass' previous first subklass
 361     set_next_sibling(prev_first_subklass);
 362   }
 363   // make ourselves the superklass' first subklass
 364   super->set_subklass(this);
 365   debug_only(verify();)
 366 }
 367 
 368 bool Klass::is_loader_alive(BoolObjectClosure* is_alive) {
 369 #ifdef ASSERT
 370   // The class is alive iff the class loader is alive.
 371   oop loader = class_loader();
 372   bool loader_alive = (loader == NULL) || is_alive->do_object_b(loader);
 373 #endif // ASSERT
 374 
 375   // The class is alive if it's mirror is alive (which should be marked if the
 376   // loader is alive) unless it's an anoymous class.
 377   bool mirror_alive = is_alive->do_object_b(java_mirror());
 378   assert(!mirror_alive || loader_alive, "loader must be alive if the mirror is"
 379                         " but not the other way around with anonymous classes");
 380   return mirror_alive;
 381 }
 382 
 383 void Klass::clean_weak_klass_links(BoolObjectClosure* is_alive, bool clean_alive_klasses) {
 384   if (!ClassUnloading) {
 385     return;
 386   }
 387 
 388   Klass* root = SystemDictionary::Object_klass();
 389   Stack<Klass*, mtGC> stack;
 390 
 391   stack.push(root);
 392   while (!stack.is_empty()) {
 393     Klass* current = stack.pop();
 394 
 395     assert(current->is_loader_alive(is_alive), "just checking, this should be live");
 396 
 397     // Find and set the first alive subklass
 398     Klass* sub = current->subklass();
 399     while (sub != NULL && !sub->is_loader_alive(is_alive)) {
 400 #ifndef PRODUCT
 401       if (log_is_enabled(Trace, class, unload)) {
 402         ResourceMark rm;
 403         log_trace(class, unload)("unlinking class (subclass): %s", sub->external_name());
 404       }
 405 #endif
 406       sub = sub->next_sibling();
 407     }
 408     current->set_subklass(sub);
 409     if (sub != NULL) {
 410       stack.push(sub);
 411     }
 412 
 413     // Find and set the first alive sibling
 414     Klass* sibling = current->next_sibling();
 415     while (sibling != NULL && !sibling->is_loader_alive(is_alive)) {
 416       if (log_is_enabled(Trace, class, unload)) {
 417         ResourceMark rm;
 418         log_trace(class, unload)("[Unlinking class (sibling) %s]", sibling->external_name());
 419       }
 420       sibling = sibling->next_sibling();
 421     }
 422     current->set_next_sibling(sibling);
 423     if (sibling != NULL) {
 424       stack.push(sibling);
 425     }
 426 
 427     // Clean the implementors list and method data.
 428     if (clean_alive_klasses && current->is_instance_klass()) {
 429       InstanceKlass* ik = InstanceKlass::cast(current);
 430       ik->clean_weak_instanceklass_links(is_alive);
 431 
 432       // JVMTI RedefineClasses creates previous versions that are not in
 433       // the class hierarchy, so process them here.
 434       while ((ik = ik->previous_versions()) != NULL) {
 435         ik->clean_weak_instanceklass_links(is_alive);
 436       }
 437     }
 438   }
 439 }
 440 
 441 void Klass::klass_update_barrier_set(oop v) {
 442   record_modified_oops();
 443 }
 444 
 445 // This barrier is used by G1 to remember the old oop values, so
 446 // that we don't forget any objects that were live at the snapshot at
 447 // the beginning. This function is only used when we write oops into Klasses.
 448 void Klass::klass_update_barrier_set_pre(oop* p, oop v) {
 449 #if INCLUDE_ALL_GCS
 450   oop obj = oopDesc::load_heap_oop(p);
 451   if (! oopDesc::is_null(obj)) {
 452     oopDesc::bs()->keep_alive_barrier(obj);
 453   }
 454 #endif
 455 }
 456 
 457 void Klass::klass_oop_store(oop* p, oop v) {
 458   assert(!Universe::heap()->is_in_reserved((void*)p), "Should store pointer into metadata");
 459   assert(v == NULL || Universe::heap()->is_in_reserved((void*)v), "Should store pointer to an object");
 460 
 461   // do the store
 462   if (always_do_update_barrier) {
 463     klass_oop_store((volatile oop*)p, v);
 464   } else {
 465     klass_update_barrier_set_pre(p, v);
 466     *p = v;
 467     klass_update_barrier_set(v);
 468   }
 469 }
 470 
 471 void Klass::klass_oop_store(volatile oop* p, oop v) {
 472   assert(!Universe::heap()->is_in_reserved((void*)p), "Should store pointer into metadata");
 473   assert(v == NULL || Universe::heap()->is_in_reserved((void*)v), "Should store pointer to an object");
 474 
 475   klass_update_barrier_set_pre((oop*)p, v); // Cast away volatile.
 476   OrderAccess::release_store_ptr(p, v);
 477   klass_update_barrier_set(v);
 478 }
 479 
 480 void Klass::oops_do(OopClosure* cl) {
 481   cl->do_oop(&_java_mirror);
 482 }
 483 
 484 void Klass::metaspace_pointers_do(MetaspaceClosure* it) {
 485   if (log_is_enabled(Trace, cds)) {
 486     ResourceMark rm;
 487     log_trace(cds)("Iter(Klass): %p (%s)", this, external_name());
 488   }
 489 
 490   it->push(&_name);
 491   it->push(&_secondary_super_cache);
 492   it->push(&_secondary_supers);
 493   for (int i = 0; i < _primary_super_limit; i++) {
 494     it->push(&_primary_supers[i]);
 495   }
 496   it->push(&_super);
 497   it->push(&_subklass);
 498   it->push(&_next_sibling);
 499   it->push(&_next_link);
 500 
 501   vtableEntry* vt = start_of_vtable();
 502   for (int i=0; i<vtable_length(); i++) {
 503     it->push(vt[i].method_addr());
 504   }
 505 }
 506 
 507 void Klass::remove_unshareable_info() {
 508   assert (DumpSharedSpaces, "only called for DumpSharedSpaces");
 509   TRACE_REMOVE_ID(this);
 510 
 511   set_subklass(NULL);
 512   set_next_sibling(NULL);
 513   // Clear the java mirror
 514   set_java_mirror(NULL);
 515   set_next_link(NULL);
 516 
 517   // Null out class_loader_data because we don't share that yet.
 518   set_class_loader_data(NULL);
 519   set_is_shared();
 520 }
 521 
 522 void Klass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
 523   assert(is_klass(), "ensure C++ vtable is restored");
 524   assert(is_shared(), "must be set");
 525   TRACE_RESTORE_ID(this);
 526 
 527   // If an exception happened during CDS restore, some of these fields may already be
 528   // set.  We leave the class on the CLD list, even if incomplete so that we don't
 529   // modify the CLD list outside a safepoint.
 530   if (class_loader_data() == NULL) {
 531     // Restore class_loader_data to the null class loader data
 532     set_class_loader_data(loader_data);
 533 
 534     // Add to null class loader list first before creating the mirror
 535     // (same order as class file parsing)
 536     loader_data->add_class(this);
 537   }
 538 
 539   // Recreate the class mirror.
 540   // Only recreate it if not present.  A previous attempt to restore may have
 541   // gotten an OOM later but keep the mirror if it was created.
 542   if (java_mirror() == NULL) {
 543     Handle loader(THREAD, loader_data->class_loader());
 544     ModuleEntry* module_entry = NULL;
 545     Klass* k = this;
 546     if (k->is_objArray_klass()) {
 547       k = ObjArrayKlass::cast(k)->bottom_klass();
 548     }
 549     // Obtain klass' module.
 550     if (k->is_instance_klass()) {
 551       InstanceKlass* ik = (InstanceKlass*) k;
 552       module_entry = ik->module();
 553     } else {
 554       module_entry = ModuleEntryTable::javabase_moduleEntry();
 555     }
 556     // Obtain java.lang.Module, if available
 557     Handle module_handle(THREAD, ((module_entry != NULL) ? module_entry->module() : (oop)NULL));
 558     java_lang_Class::create_mirror(this, loader, module_handle, protection_domain, CHECK);
 559   }
 560 }
 561 
 562 Klass* Klass::array_klass_or_null(int rank) {
 563   EXCEPTION_MARK;
 564   // No exception can be thrown by array_klass_impl when called with or_null == true.
 565   // (In anycase, the execption mark will fail if it do so)
 566   return array_klass_impl(true, rank, THREAD);
 567 }
 568 
 569 
 570 Klass* Klass::array_klass_or_null() {
 571   EXCEPTION_MARK;
 572   // No exception can be thrown by array_klass_impl when called with or_null == true.
 573   // (In anycase, the execption mark will fail if it do so)
 574   return array_klass_impl(true, THREAD);
 575 }
 576 
 577 
 578 Klass* Klass::array_klass_impl(bool or_null, int rank, TRAPS) {
 579   fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass");
 580   return NULL;
 581 }
 582 
 583 
 584 Klass* Klass::array_klass_impl(bool or_null, TRAPS) {
 585   fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass");
 586   return NULL;
 587 }
 588 
 589 oop Klass::class_loader() const { return class_loader_data()->class_loader(); }
 590 
 591 // In product mode, this function doesn't have virtual function calls so
 592 // there might be some performance advantage to handling InstanceKlass here.
 593 const char* Klass::external_name() const {
 594   if (is_instance_klass()) {
 595     const InstanceKlass* ik = static_cast<const InstanceKlass*>(this);
 596     if (ik->is_anonymous()) {
 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 klassVtable Klass::vtable() const {
 721   return klassVtable(const_cast<Klass*>(this), start_of_vtable(), vtable_length() / vtableEntry::size());
 722 }
 723 
 724 vtableEntry* Klass::start_of_vtable() const {
 725   return (vtableEntry*) ((address)this + in_bytes(vtable_start_offset()));
 726 }
 727 
 728 Method* Klass::method_at_vtable(int index)  {
 729 #ifndef PRODUCT
 730   assert(index >= 0, "valid vtable index");
 731   if (DebugVtables) {
 732     verify_vtable_index(index);
 733   }
 734 #endif
 735   return start_of_vtable()[index].method();
 736 }
 737 
 738 ByteSize Klass::vtable_start_offset() {
 739   return in_ByteSize(InstanceKlass::header_size() * wordSize);
 740 }
 741 
 742 #ifndef PRODUCT
 743 
 744 bool Klass::verify_vtable_index(int i) {
 745   int limit = vtable_length()/vtableEntry::size();
 746   assert(i >= 0 && i < limit, "index %d out of bounds %d", i, limit);
 747   return true;
 748 }
 749 
 750 bool Klass::verify_itable_index(int i) {
 751   assert(is_instance_klass(), "");
 752   int method_count = klassItable::method_count_for_interface(this);
 753   assert(i >= 0 && i < method_count, "index out of bounds");
 754   return true;
 755 }
 756 
 757 #endif