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/oopFactory.hpp"
  35 #include "memory/resourceArea.hpp"
  36 #include "oops/instanceKlass.hpp"
  37 #include "oops/klass.inline.hpp"
  38 #include "oops/oop.inline.hpp"
  39 #include "runtime/access.inline.hpp"
  40 #include "runtime/atomic.hpp"
  41 #include "runtime/orderAccess.inline.hpp"
  42 #include "trace/traceMacros.hpp"
  43 #include "utilities/macros.hpp"
  44 #include "utilities/stack.inline.hpp"
  45 
  46 bool Klass::is_cloneable() const {
  47   return _access_flags.is_cloneable_fast() ||
  48          is_subtype_of(SystemDictionary::Cloneable_klass());
  49 }
  50 
  51 void Klass::set_is_cloneable() {
  52   if (name() != vmSymbols::java_lang_invoke_MemberName()) {
  53     _access_flags.set_is_cloneable_fast();
  54   } else {
  55     assert(is_final(), "no subclasses allowed");
  56     // MemberName cloning should not be intrinsified and always happen in JVM_Clone.
  57   }
  58 }
  59 
  60 void Klass::set_name(Symbol* n) {
  61   _name = n;
  62   if (_name != NULL) _name->increment_refcount();
  63 }
  64 
  65 bool Klass::is_subclass_of(const Klass* k) const {
  66   // Run up the super chain and check
  67   if (this == k) return true;
  68 
  69   Klass* t = const_cast<Klass*>(this)->super();
  70 
  71   while (t != NULL) {
  72     if (t == k) return true;
  73     t = t->super();
  74   }
  75   return false;
  76 }
  77 
  78 bool Klass::search_secondary_supers(Klass* k) const {
  79   // Put some extra logic here out-of-line, before the search proper.
  80   // This cuts down the size of the inline method.
  81 
  82   // This is necessary, since I am never in my own secondary_super list.
  83   if (this == k)
  84     return true;
  85   // Scan the array-of-objects for a match
  86   int cnt = secondary_supers()->length();
  87   for (int i = 0; i < cnt; i++) {
  88     if (secondary_supers()->at(i) == k) {
  89       ((Klass*)this)->set_secondary_super_cache(k);
  90       return true;
  91     }
  92   }
  93   return false;
  94 }
  95 
  96 // Return self, except for abstract classes with exactly 1
  97 // implementor.  Then return the 1 concrete implementation.
  98 Klass *Klass::up_cast_abstract() {
  99   Klass *r = this;
 100   while( r->is_abstract() ) {   // Receiver is abstract?
 101     Klass *s = r->subklass();   // Check for exactly 1 subklass
 102     if( !s || s->next_sibling() ) // Oops; wrong count; give up
 103       return this;              // Return 'this' as a no-progress flag
 104     r = s;                    // Loop till find concrete class
 105   }
 106   return r;                   // Return the 1 concrete class
 107 }
 108 
 109 // Find LCA in class hierarchy
 110 Klass *Klass::LCA( Klass *k2 ) {
 111   Klass *k1 = this;
 112   while( 1 ) {
 113     if( k1->is_subtype_of(k2) ) return k2;
 114     if( k2->is_subtype_of(k1) ) return k1;
 115     k1 = k1->super();
 116     k2 = k2->super();
 117   }
 118 }
 119 
 120 
 121 void Klass::check_valid_for_instantiation(bool throwError, TRAPS) {
 122   ResourceMark rm(THREAD);
 123   THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
 124             : vmSymbols::java_lang_InstantiationException(), external_name());
 125 }
 126 
 127 
 128 void Klass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) {
 129   THROW(vmSymbols::java_lang_ArrayStoreException());
 130 }
 131 
 132 
 133 void Klass::initialize(TRAPS) {
 134   ShouldNotReachHere();
 135 }
 136 
 137 bool Klass::compute_is_subtype_of(Klass* k) {
 138   assert(k->is_klass(), "argument must be a class");
 139   return is_subclass_of(k);
 140 }
 141 
 142 Klass* Klass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
 143 #ifdef ASSERT
 144   tty->print_cr("Error: find_field called on a klass oop."
 145                 " Likely error: reflection method does not correctly"
 146                 " wrap return value in a mirror object.");
 147 #endif
 148   ShouldNotReachHere();
 149   return NULL;
 150 }
 151 
 152 Method* Klass::uncached_lookup_method(const Symbol* name, const Symbol* signature, OverpassLookupMode overpass_mode) const {
 153 #ifdef ASSERT
 154   tty->print_cr("Error: uncached_lookup_method called on a klass oop."
 155                 " Likely error: reflection method does not correctly"
 156                 " wrap return value in a mirror object.");
 157 #endif
 158   ShouldNotReachHere();
 159   return NULL;
 160 }
 161 
 162 void* Klass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) throw() {
 163   return Metaspace::allocate(loader_data, word_size, /*read_only*/false,
 164                              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::set_java_mirror(oop m) {
 442   KlassAccess<MO_RELEASE>::oop_store_at(this, in_bytes(java_mirror_offset()), m);
 443 }
 444 
 445 void Klass::oops_do(OopClosure* cl) {
 446   cl->do_oop(&_java_mirror);
 447 }
 448 
 449 void Klass::remove_unshareable_info() {
 450   assert (DumpSharedSpaces, "only called for DumpSharedSpaces");
 451   TRACE_REMOVE_ID(this);
 452 
 453   set_subklass(NULL);
 454   set_next_sibling(NULL);
 455   // Clear the java mirror
 456   set_java_mirror(NULL);
 457   set_next_link(NULL);
 458 
 459   // Null out class_loader_data because we don't share that yet.
 460   set_class_loader_data(NULL);
 461 }
 462 
 463 void Klass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
 464   assert(is_klass(), "ensure C++ vtable is restored");
 465   TRACE_RESTORE_ID(this);
 466 
 467   // If an exception happened during CDS restore, some of these fields may already be
 468   // set.  We leave the class on the CLD list, even if incomplete so that we don't
 469   // modify the CLD list outside a safepoint.
 470   if (class_loader_data() == NULL) {
 471     // Restore class_loader_data to the null class loader data
 472     set_class_loader_data(loader_data);
 473 
 474     // Add to null class loader list first before creating the mirror
 475     // (same order as class file parsing)
 476     loader_data->add_class(this);
 477   }
 478 
 479   // Recreate the class mirror.
 480   // Only recreate it if not present.  A previous attempt to restore may have
 481   // gotten an OOM later but keep the mirror if it was created.
 482   if (java_mirror() == NULL) {
 483     Handle loader(THREAD, loader_data->class_loader());
 484     ModuleEntry* module_entry = NULL;
 485     Klass* k = this;
 486     if (k->is_objArray_klass()) {
 487       k = ObjArrayKlass::cast(k)->bottom_klass();
 488     }
 489     // Obtain klass' module.
 490     if (k->is_instance_klass()) {
 491       InstanceKlass* ik = (InstanceKlass*) k;
 492       module_entry = ik->module();
 493     } else {
 494       module_entry = ModuleEntryTable::javabase_moduleEntry();
 495     }
 496     // Obtain java.lang.Module, if available
 497     Handle module_handle(THREAD, ((module_entry != NULL) ? JNIHandles::resolve(module_entry->module()) : (oop)NULL));
 498     java_lang_Class::create_mirror(this, loader, module_handle, protection_domain, CHECK);
 499   }
 500 }
 501 
 502 Klass* Klass::array_klass_or_null(int rank) {
 503   EXCEPTION_MARK;
 504   // No exception can be thrown by array_klass_impl when called with or_null == true.
 505   // (In anycase, the execption mark will fail if it do so)
 506   return array_klass_impl(true, rank, THREAD);
 507 }
 508 
 509 
 510 Klass* Klass::array_klass_or_null() {
 511   EXCEPTION_MARK;
 512   // No exception can be thrown by array_klass_impl when called with or_null == true.
 513   // (In anycase, the execption mark will fail if it do so)
 514   return array_klass_impl(true, THREAD);
 515 }
 516 
 517 
 518 Klass* Klass::array_klass_impl(bool or_null, int rank, TRAPS) {
 519   fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass");
 520   return NULL;
 521 }
 522 
 523 
 524 Klass* Klass::array_klass_impl(bool or_null, TRAPS) {
 525   fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass");
 526   return NULL;
 527 }
 528 
 529 oop Klass::class_loader() const       { return class_loader_data()->class_loader(); }
 530 
 531 oop* Klass::klass_holder_addr() const {
 532   return class_loader_data()->class_loader_addr();
 533 }
 534 
 535 oop Klass::klass_holder() const {
 536   return RootAccess<GC_ACCESS_ON_PHANTOM>::oop_load(klass_holder_addr());
 537 }
 538 
 539 // In product mode, this function doesn't have virtual function calls so
 540 // there might be some performance advantage to handling InstanceKlass here.
 541 const char* Klass::external_name() const {
 542   if (is_instance_klass()) {
 543     const InstanceKlass* ik = static_cast<const InstanceKlass*>(this);
 544     if (ik->is_anonymous()) {
 545       intptr_t hash = 0;
 546       if (ik->java_mirror() != NULL) {
 547         // java_mirror might not be created yet, return 0 as hash.
 548         hash = ik->java_mirror()->identity_hash();
 549       }
 550       char     hash_buf[40];
 551       sprintf(hash_buf, "/" UINTX_FORMAT, (uintx)hash);
 552       size_t   hash_len = strlen(hash_buf);
 553 
 554       size_t result_len = name()->utf8_length();
 555       char*  result     = NEW_RESOURCE_ARRAY(char, result_len + hash_len + 1);
 556       name()->as_klass_external_name(result, (int) result_len + 1);
 557       assert(strlen(result) == result_len, "");
 558       strcpy(result + result_len, hash_buf);
 559       assert(strlen(result) == result_len + hash_len, "");
 560       return result;
 561     }
 562   }
 563   if (name() == NULL)  return "<unknown>";
 564   return name()->as_klass_external_name();
 565 }
 566 
 567 
 568 const char* Klass::signature_name() const {
 569   if (name() == NULL)  return "<unknown>";
 570   return name()->as_C_string();
 571 }
 572 
 573 // Unless overridden, modifier_flags is 0.
 574 jint Klass::compute_modifier_flags(TRAPS) const {
 575   return 0;
 576 }
 577 
 578 int Klass::atomic_incr_biased_lock_revocation_count() {
 579   return (int) Atomic::add(1, &_biased_lock_revocation_count);
 580 }
 581 
 582 // Unless overridden, jvmti_class_status has no flags set.
 583 jint Klass::jvmti_class_status() const {
 584   return 0;
 585 }
 586 
 587 
 588 // Printing
 589 
 590 void Klass::print_on(outputStream* st) const {
 591   ResourceMark rm;
 592   // print title
 593   st->print("%s", internal_name());
 594   print_address_on(st);
 595   st->cr();
 596 }
 597 
 598 void Klass::oop_print_on(oop obj, outputStream* st) {
 599   ResourceMark rm;
 600   // print title
 601   st->print_cr("%s ", internal_name());
 602   obj->print_address_on(st);
 603 
 604   if (WizardMode) {
 605      // print header
 606      obj->mark()->print_on(st);
 607   }
 608 
 609   // print class
 610   st->print(" - klass: ");
 611   obj->klass()->print_value_on(st);
 612   st->cr();
 613 }
 614 
 615 void Klass::oop_print_value_on(oop obj, outputStream* st) {
 616   // print title
 617   ResourceMark rm;              // Cannot print in debug mode without this
 618   st->print("%s", internal_name());
 619   obj->print_address_on(st);
 620 }
 621 
 622 #if INCLUDE_SERVICES
 623 // Size Statistics
 624 void Klass::collect_statistics(KlassSizeStats *sz) const {
 625   sz->_klass_bytes = sz->count(this);
 626   sz->_mirror_bytes = sz->count(java_mirror());
 627   sz->_secondary_supers_bytes = sz->count_array(secondary_supers());
 628 
 629   sz->_ro_bytes += sz->_secondary_supers_bytes;
 630   sz->_rw_bytes += sz->_klass_bytes + sz->_mirror_bytes;
 631 }
 632 #endif // INCLUDE_SERVICES
 633 
 634 // Verification
 635 
 636 void Klass::verify_on(outputStream* st) {
 637 
 638   // This can be expensive, but it is worth checking that this klass is actually
 639   // in the CLD graph but not in production.
 640   assert(Metaspace::contains((address)this), "Should be");
 641 
 642   guarantee(this->is_klass(),"should be klass");
 643 
 644   if (super() != NULL) {
 645     guarantee(super()->is_klass(), "should be klass");
 646   }
 647   if (secondary_super_cache() != NULL) {
 648     Klass* ko = secondary_super_cache();
 649     guarantee(ko->is_klass(), "should be klass");
 650   }
 651   for ( uint i = 0; i < primary_super_limit(); i++ ) {
 652     Klass* ko = _primary_supers[i];
 653     if (ko != NULL) {
 654       guarantee(ko->is_klass(), "should be klass");
 655     }
 656   }
 657 
 658   if (java_mirror() != NULL) {
 659     guarantee(java_mirror()->is_oop(), "should be instance");
 660   }
 661 }
 662 
 663 void Klass::oop_verify_on(oop obj, outputStream* st) {
 664   guarantee(obj->is_oop(),  "should be oop");
 665   guarantee(obj->klass()->is_klass(), "klass field is not a klass");
 666 }
 667 
 668 klassVtable Klass::vtable() const {
 669   return klassVtable(const_cast<Klass*>(this), start_of_vtable(), vtable_length() / vtableEntry::size());
 670 }
 671 
 672 vtableEntry* Klass::start_of_vtable() const {
 673   return (vtableEntry*) ((address)this + in_bytes(vtable_start_offset()));
 674 }
 675 
 676 Method* Klass::method_at_vtable(int index)  {
 677 #ifndef PRODUCT
 678   assert(index >= 0, "valid vtable index");
 679   if (DebugVtables) {
 680     verify_vtable_index(index);
 681   }
 682 #endif
 683   return start_of_vtable()[index].method();
 684 }
 685 
 686 ByteSize Klass::vtable_start_offset() {
 687   return in_ByteSize(InstanceKlass::header_size() * wordSize);
 688 }
 689 
 690 #ifndef PRODUCT
 691 
 692 bool Klass::verify_vtable_index(int i) {
 693   int limit = vtable_length()/vtableEntry::size();
 694   assert(i >= 0 && i < limit, "index %d out of bounds %d", i, limit);
 695   return true;
 696 }
 697 
 698 bool Klass::verify_itable_index(int i) {
 699   assert(is_instance_klass(), "");
 700   int method_count = klassItable::method_count_for_interface(this);
 701   assert(i >= 0 && i < method_count, "index out of bounds");
 702   return true;
 703 }
 704 
 705 #endif