src/hotspot/share/oops/klass.cpp
branchihse-cflags-rewrite-branch
changeset 58665 30a5049a36bb
parent 56900 d5d542d50e3c
parent 58537 30a9612a657d
equal deleted inserted replaced
56900:d5d542d50e3c 58665:30a5049a36bb
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    22  *
    22  *
    23  */
    23  */
    24 
    24 
    25 #include "precompiled.hpp"
    25 #include "precompiled.hpp"
    26 #include "classfile/classLoaderData.inline.hpp"
    26 #include "classfile/classLoaderData.inline.hpp"
       
    27 #include "classfile/classLoaderDataGraph.inline.hpp"
    27 #include "classfile/dictionary.hpp"
    28 #include "classfile/dictionary.hpp"
    28 #include "classfile/javaClasses.hpp"
    29 #include "classfile/javaClasses.hpp"
       
    30 #include "classfile/moduleEntry.hpp"
    29 #include "classfile/systemDictionary.hpp"
    31 #include "classfile/systemDictionary.hpp"
    30 #include "classfile/vmSymbols.hpp"
    32 #include "classfile/vmSymbols.hpp"
    31 #include "gc/shared/collectedHeap.inline.hpp"
    33 #include "gc/shared/collectedHeap.inline.hpp"
    32 #include "logging/log.hpp"
    34 #include "logging/log.hpp"
    33 #include "memory/heapInspection.hpp"
    35 #include "memory/heapInspection.hpp"
       
    36 #include "memory/heapShared.hpp"
    34 #include "memory/metadataFactory.hpp"
    37 #include "memory/metadataFactory.hpp"
    35 #include "memory/metaspaceClosure.hpp"
    38 #include "memory/metaspaceClosure.hpp"
    36 #include "memory/metaspaceShared.hpp"
    39 #include "memory/metaspaceShared.hpp"
    37 #include "memory/oopFactory.hpp"
    40 #include "memory/oopFactory.hpp"
    38 #include "memory/resourceArea.hpp"
    41 #include "memory/resourceArea.hpp"
       
    42 #include "memory/universe.hpp"
    39 #include "oops/compressedOops.inline.hpp"
    43 #include "oops/compressedOops.inline.hpp"
    40 #include "oops/instanceKlass.hpp"
    44 #include "oops/instanceKlass.hpp"
    41 #include "oops/klass.inline.hpp"
    45 #include "oops/klass.inline.hpp"
    42 #include "oops/oop.inline.hpp"
    46 #include "oops/oop.inline.hpp"
    43 #include "oops/oopHandle.inline.hpp"
    47 #include "oops/oopHandle.inline.hpp"
    51   assert(!m.is_null(), "New mirror should never be null.");
    55   assert(!m.is_null(), "New mirror should never be null.");
    52   assert(_java_mirror.resolve() == NULL, "should only be used to initialize mirror");
    56   assert(_java_mirror.resolve() == NULL, "should only be used to initialize mirror");
    53   _java_mirror = class_loader_data()->add_handle(m);
    57   _java_mirror = class_loader_data()->add_handle(m);
    54 }
    58 }
    55 
    59 
    56 oop Klass::java_mirror() const {
    60 oop Klass::java_mirror_no_keepalive() const {
    57   return _java_mirror.resolve();
    61   return _java_mirror.peek();
    58 }
    62 }
    59 
    63 
    60 bool Klass::is_cloneable() const {
    64 bool Klass::is_cloneable() const {
    61   return _access_flags.is_cloneable_fast() ||
    65   return _access_flags.is_cloneable_fast() ||
    62          is_subtype_of(SystemDictionary::Cloneable_klass());
    66          is_subtype_of(SystemDictionary::Cloneable_klass());
   113 // implementor.  Then return the 1 concrete implementation.
   117 // implementor.  Then return the 1 concrete implementation.
   114 Klass *Klass::up_cast_abstract() {
   118 Klass *Klass::up_cast_abstract() {
   115   Klass *r = this;
   119   Klass *r = this;
   116   while( r->is_abstract() ) {   // Receiver is abstract?
   120   while( r->is_abstract() ) {   // Receiver is abstract?
   117     Klass *s = r->subklass();   // Check for exactly 1 subklass
   121     Klass *s = r->subklass();   // Check for exactly 1 subklass
   118     if( !s || s->next_sibling() ) // Oops; wrong count; give up
   122     if (s == NULL || s->next_sibling() != NULL) // Oops; wrong count; give up
   119       return this;              // Return 'this' as a no-progress flag
   123       return this;              // Return 'this' as a no-progress flag
   120     r = s;                    // Loop till find concrete class
   124     r = s;                    // Loop till find concrete class
   121   }
   125   }
   122   return r;                   // Return the 1 concrete class
   126   return r;                   // Return the 1 concrete class
   123 }
   127 }
   149 }
   153 }
   150 
   154 
   151 
   155 
   152 void Klass::initialize(TRAPS) {
   156 void Klass::initialize(TRAPS) {
   153   ShouldNotReachHere();
   157   ShouldNotReachHere();
   154 }
       
   155 
       
   156 bool Klass::compute_is_subtype_of(Klass* k) {
       
   157   assert(k->is_klass(), "argument must be a class");
       
   158   return is_subclass_of(k);
       
   159 }
   158 }
   160 
   159 
   161 Klass* Klass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
   160 Klass* Klass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
   162 #ifdef ASSERT
   161 #ifdef ASSERT
   163   tty->print_cr("Error: find_field called on a klass oop."
   162   tty->print_cr("Error: find_field called on a klass oop."
   190 // which doesn't zero out the memory before calling the constructor.
   189 // which doesn't zero out the memory before calling the constructor.
   191 // Need to set the _java_mirror field explicitly to not hit an assert that the field
   190 // Need to set the _java_mirror field explicitly to not hit an assert that the field
   192 // should be NULL before setting it.
   191 // should be NULL before setting it.
   193 Klass::Klass(KlassID id) : _id(id),
   192 Klass::Klass(KlassID id) : _id(id),
   194                            _java_mirror(NULL),
   193                            _java_mirror(NULL),
   195                            _prototype_header(markOopDesc::prototype()),
   194                            _prototype_header(markWord::prototype()),
   196                            _shared_class_path_index(-1) {
   195                            _shared_class_path_index(-1) {
   197   CDS_ONLY(_shared_class_flags = 0;)
   196   CDS_ONLY(_shared_class_flags = 0;)
   198   CDS_JAVA_HEAP_ONLY(_archived_mirror = 0;)
   197   CDS_JAVA_HEAP_ONLY(_archived_mirror = 0;)
   199   _primary_supers[0] = this;
   198   _primary_supers[0] = this;
   200   set_super_check_offset(in_bytes(primary_supers_offset()));
   199   set_super_check_offset(in_bytes(primary_supers_offset()));
   228   else
   227   else
   229     return true;
   228     return true;
   230 }
   229 }
   231 
   230 
   232 void Klass::initialize_supers(Klass* k, Array<InstanceKlass*>* transitive_interfaces, TRAPS) {
   231 void Klass::initialize_supers(Klass* k, Array<InstanceKlass*>* transitive_interfaces, TRAPS) {
   233   if (FastSuperclassLimit == 0) {
       
   234     // None of the other machinery matters.
       
   235     set_super(k);
       
   236     return;
       
   237   }
       
   238   if (k == NULL) {
   232   if (k == NULL) {
   239     set_super(NULL);
   233     set_super(NULL);
   240     _primary_supers[0] = this;
   234     _primary_supers[0] = this;
   241     assert(super_depth() == 0, "Object must already be initialized properly");
   235     assert(super_depth() == 0, "Object must already be initialized properly");
   242   } else if (k != super() || k == SystemDictionary::Object_klass()) {
   236   } else if (k != super() || k == SystemDictionary::Object_klass()) {
   354   set_secondary_supers(Universe::the_empty_klass_array());
   348   set_secondary_supers(Universe::the_empty_klass_array());
   355   return NULL;
   349   return NULL;
   356 }
   350 }
   357 
   351 
   358 
   352 
       
   353 // superklass links
   359 InstanceKlass* Klass::superklass() const {
   354 InstanceKlass* Klass::superklass() const {
   360   assert(super() == NULL || super()->is_instance_klass(), "must be instance klass");
   355   assert(super() == NULL || super()->is_instance_klass(), "must be instance klass");
   361   return _super == NULL ? NULL : InstanceKlass::cast(_super);
   356   return _super == NULL ? NULL : InstanceKlass::cast(_super);
   362 }
   357 }
   363 
   358 
       
   359 // subklass links.  Used by the compiler (and vtable initialization)
       
   360 // May be cleaned concurrently, so must use the Compile_lock.
       
   361 // The log parameter is for clean_weak_klass_links to report unlinked classes.
       
   362 Klass* Klass::subklass(bool log) const {
       
   363   // Need load_acquire on the _subklass, because it races with inserts that
       
   364   // publishes freshly initialized data.
       
   365   for (Klass* chain = OrderAccess::load_acquire(&_subklass);
       
   366        chain != NULL;
       
   367        // Do not need load_acquire on _next_sibling, because inserts never
       
   368        // create _next_sibling edges to dead data.
       
   369        chain = Atomic::load(&chain->_next_sibling))
       
   370   {
       
   371     if (chain->is_loader_alive()) {
       
   372       return chain;
       
   373     } else if (log) {
       
   374       if (log_is_enabled(Trace, class, unload)) {
       
   375         ResourceMark rm;
       
   376         log_trace(class, unload)("unlinking class (subclass): %s", chain->external_name());
       
   377       }
       
   378     }
       
   379   }
       
   380   return NULL;
       
   381 }
       
   382 
       
   383 Klass* Klass::next_sibling(bool log) const {
       
   384   // Do not need load_acquire on _next_sibling, because inserts never
       
   385   // create _next_sibling edges to dead data.
       
   386   for (Klass* chain = Atomic::load(&_next_sibling);
       
   387        chain != NULL;
       
   388        chain = Atomic::load(&chain->_next_sibling)) {
       
   389     // Only return alive klass, there may be stale klass
       
   390     // in this chain if cleaned concurrently.
       
   391     if (chain->is_loader_alive()) {
       
   392       return chain;
       
   393     } else if (log) {
       
   394       if (log_is_enabled(Trace, class, unload)) {
       
   395         ResourceMark rm;
       
   396         log_trace(class, unload)("unlinking class (sibling): %s", chain->external_name());
       
   397       }
       
   398     }
       
   399   }
       
   400   return NULL;
       
   401 }
       
   402 
   364 void Klass::set_subklass(Klass* s) {
   403 void Klass::set_subklass(Klass* s) {
   365   assert(s != this, "sanity check");
   404   assert(s != this, "sanity check");
   366   _subklass = s;
   405   OrderAccess::release_store(&_subklass, s);
   367 }
   406 }
   368 
   407 
   369 void Klass::set_next_sibling(Klass* s) {
   408 void Klass::set_next_sibling(Klass* s) {
   370   assert(s != this, "sanity check");
   409   assert(s != this, "sanity check");
   371   _next_sibling = s;
   410   // Does not need release semantics. If used by cleanup, it will link to
       
   411   // already safely published data, and if used by inserts, will be published
       
   412   // safely using cmpxchg.
       
   413   Atomic::store(s, &_next_sibling);
   372 }
   414 }
   373 
   415 
   374 void Klass::append_to_sibling_list() {
   416 void Klass::append_to_sibling_list() {
       
   417   assert_locked_or_safepoint(Compile_lock);
   375   debug_only(verify();)
   418   debug_only(verify();)
   376   // add ourselves to superklass' subklass list
   419   // add ourselves to superklass' subklass list
   377   InstanceKlass* super = superklass();
   420   InstanceKlass* super = superklass();
   378   if (super == NULL) return;        // special case: class Object
   421   if (super == NULL) return;        // special case: class Object
   379   assert((!super->is_interface()    // interfaces cannot be supers
   422   assert((!super->is_interface()    // interfaces cannot be supers
   380           && (super->superklass() == NULL || !is_interface())),
   423           && (super->superklass() == NULL || !is_interface())),
   381          "an interface can only be a subklass of Object");
   424          "an interface can only be a subklass of Object");
   382   Klass* prev_first_subklass = super->subklass();
   425 
   383   if (prev_first_subklass != NULL) {
   426   // Make sure there is no stale subklass head
   384     // set our sibling to be the superklass' previous first subklass
   427   super->clean_subklass();
   385     set_next_sibling(prev_first_subklass);
   428 
   386   }
   429   for (;;) {
   387   // make ourselves the superklass' first subklass
   430     Klass* prev_first_subklass = OrderAccess::load_acquire(&_super->_subklass);
   388   super->set_subklass(this);
   431     if (prev_first_subklass != NULL) {
       
   432       // set our sibling to be the superklass' previous first subklass
       
   433       assert(prev_first_subklass->is_loader_alive(), "May not attach not alive klasses");
       
   434       set_next_sibling(prev_first_subklass);
       
   435     }
       
   436     // Note that the prev_first_subklass is always alive, meaning no sibling_next links
       
   437     // are ever created to not alive klasses. This is an important invariant of the lock-free
       
   438     // cleaning protocol, that allows us to safely unlink dead klasses from the sibling list.
       
   439     if (Atomic::cmpxchg(this, &super->_subklass, prev_first_subklass) == prev_first_subklass) {
       
   440       return;
       
   441     }
       
   442   }
   389   debug_only(verify();)
   443   debug_only(verify();)
   390 }
   444 }
   391 
   445 
   392 oop Klass::holder_phantom() const {
   446 void Klass::clean_subklass() {
   393   return class_loader_data()->holder_phantom();
   447   for (;;) {
       
   448     // Need load_acquire, due to contending with concurrent inserts
       
   449     Klass* subklass = OrderAccess::load_acquire(&_subklass);
       
   450     if (subklass == NULL || subklass->is_loader_alive()) {
       
   451       return;
       
   452     }
       
   453     // Try to fix _subklass until it points at something not dead.
       
   454     Atomic::cmpxchg(subklass->next_sibling(), &_subklass, subklass);
       
   455   }
   394 }
   456 }
   395 
   457 
   396 void Klass::clean_weak_klass_links(bool unloading_occurred, bool clean_alive_klasses) {
   458 void Klass::clean_weak_klass_links(bool unloading_occurred, bool clean_alive_klasses) {
   397   if (!ClassUnloading || !unloading_occurred) {
   459   if (!ClassUnloading || !unloading_occurred) {
   398     return;
   460     return;
   406     Klass* current = stack.pop();
   468     Klass* current = stack.pop();
   407 
   469 
   408     assert(current->is_loader_alive(), "just checking, this should be live");
   470     assert(current->is_loader_alive(), "just checking, this should be live");
   409 
   471 
   410     // Find and set the first alive subklass
   472     // Find and set the first alive subklass
   411     Klass* sub = current->subklass();
   473     Klass* sub = current->subklass(true);
   412     while (sub != NULL && !sub->is_loader_alive()) {
   474     current->clean_subklass();
   413 #ifndef PRODUCT
       
   414       if (log_is_enabled(Trace, class, unload)) {
       
   415         ResourceMark rm;
       
   416         log_trace(class, unload)("unlinking class (subclass): %s", sub->external_name());
       
   417       }
       
   418 #endif
       
   419       sub = sub->next_sibling();
       
   420     }
       
   421     current->set_subklass(sub);
       
   422     if (sub != NULL) {
   475     if (sub != NULL) {
   423       stack.push(sub);
   476       stack.push(sub);
   424     }
   477     }
   425 
   478 
   426     // Find and set the first alive sibling
   479     // Find and set the first alive sibling
   427     Klass* sibling = current->next_sibling();
   480     Klass* sibling = current->next_sibling(true);
   428     while (sibling != NULL && !sibling->is_loader_alive()) {
       
   429       if (log_is_enabled(Trace, class, unload)) {
       
   430         ResourceMark rm;
       
   431         log_trace(class, unload)("[Unlinking class (sibling) %s]", sibling->external_name());
       
   432       }
       
   433       sibling = sibling->next_sibling();
       
   434     }
       
   435     current->set_next_sibling(sibling);
   481     current->set_next_sibling(sibling);
   436     if (sibling != NULL) {
   482     if (sibling != NULL) {
   437       stack.push(sibling);
   483       stack.push(sibling);
   438     }
   484     }
   439 
   485 
   462   it->push(&_secondary_supers);
   508   it->push(&_secondary_supers);
   463   for (int i = 0; i < _primary_super_limit; i++) {
   509   for (int i = 0; i < _primary_super_limit; i++) {
   464     it->push(&_primary_supers[i]);
   510     it->push(&_primary_supers[i]);
   465   }
   511   }
   466   it->push(&_super);
   512   it->push(&_super);
   467   it->push(&_subklass);
   513   it->push((Klass**)&_subklass);
   468   it->push(&_next_sibling);
   514   it->push((Klass**)&_next_sibling);
   469   it->push(&_next_link);
   515   it->push(&_next_link);
   470 
   516 
   471   vtableEntry* vt = start_of_vtable();
   517   vtableEntry* vt = start_of_vtable();
   472   for (int i=0; i<vtable_length(); i++) {
   518   for (int i=0; i<vtable_length(); i++) {
   473     it->push(vt[i].method_addr());
   519     it->push(vt[i].method_addr());
   474   }
   520   }
   475 }
   521 }
   476 
   522 
   477 void Klass::remove_unshareable_info() {
   523 void Klass::remove_unshareable_info() {
   478   assert (DumpSharedSpaces, "only called for DumpSharedSpaces");
   524   assert (Arguments::is_dumping_archive(),
       
   525           "only called during CDS dump time");
   479   JFR_ONLY(REMOVE_ID(this);)
   526   JFR_ONLY(REMOVE_ID(this);)
   480   if (log_is_enabled(Trace, cds, unshareable)) {
   527   if (log_is_enabled(Trace, cds, unshareable)) {
   481     ResourceMark rm;
   528     ResourceMark rm;
   482     log_trace(cds, unshareable)("remove: %s", external_name());
   529     log_trace(cds, unshareable)("remove: %s", external_name());
   483   }
   530   }
   490   set_class_loader_data(NULL);
   537   set_class_loader_data(NULL);
   491   set_is_shared();
   538   set_is_shared();
   492 }
   539 }
   493 
   540 
   494 void Klass::remove_java_mirror() {
   541 void Klass::remove_java_mirror() {
   495   assert (DumpSharedSpaces, "only called for DumpSharedSpaces");
   542   Arguments::assert_is_dumping_archive();
   496   if (log_is_enabled(Trace, cds, unshareable)) {
   543   if (log_is_enabled(Trace, cds, unshareable)) {
   497     ResourceMark rm;
   544     ResourceMark rm;
   498     log_trace(cds, unshareable)("remove java_mirror: %s", external_name());
   545     log_trace(cds, unshareable)("remove java_mirror: %s", external_name());
   499   }
   546   }
   500   // Just null out the mirror.  The class_loader_data() no longer exists.
   547   // Just null out the mirror.  The class_loader_data() no longer exists.
   515   // modify the CLD list outside a safepoint.
   562   // modify the CLD list outside a safepoint.
   516   if (class_loader_data() == NULL) {
   563   if (class_loader_data() == NULL) {
   517     // Restore class_loader_data to the null class loader data
   564     // Restore class_loader_data to the null class loader data
   518     set_class_loader_data(loader_data);
   565     set_class_loader_data(loader_data);
   519 
   566 
       
   567     // Workaround for suspected bug.  Make sure other threads see this assignment.
       
   568     // This shouldn't be necessary but the compiler thread seems to be behind
       
   569     // the times, even though this thread takes MethodCompileQueue_lock and the thread
       
   570     // that doesn't see this value also takes that lock.
       
   571     OrderAccess::fence();
       
   572 
   520     // Add to null class loader list first before creating the mirror
   573     // Add to null class loader list first before creating the mirror
   521     // (same order as class file parsing)
   574     // (same order as class file parsing)
   522     loader_data->add_class(this);
   575     loader_data->add_class(this);
   523   }
   576   }
   524 
   577 
   539   Handle module_handle(THREAD, ((module_entry != NULL) ? module_entry->module() : (oop)NULL));
   592   Handle module_handle(THREAD, ((module_entry != NULL) ? module_entry->module() : (oop)NULL));
   540 
   593 
   541   if (this->has_raw_archived_mirror()) {
   594   if (this->has_raw_archived_mirror()) {
   542     ResourceMark rm;
   595     ResourceMark rm;
   543     log_debug(cds, mirror)("%s has raw archived mirror", external_name());
   596     log_debug(cds, mirror)("%s has raw archived mirror", external_name());
   544     if (MetaspaceShared::open_archive_heap_region_mapped()) {
   597     if (HeapShared::open_archive_heap_region_mapped()) {
   545       bool present = java_lang_Class::restore_archived_mirror(this, loader, module_handle,
   598       bool present = java_lang_Class::restore_archived_mirror(this, loader, module_handle,
   546                                                               protection_domain,
   599                                                               protection_domain,
   547                                                               CHECK);
   600                                                               CHECK);
   548       if (present) {
   601       if (present) {
   549         return;
   602         return;
   608 Klass* Klass::array_klass_impl(bool or_null, TRAPS) {
   661 Klass* Klass::array_klass_impl(bool or_null, TRAPS) {
   609   fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass");
   662   fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass");
   610   return NULL;
   663   return NULL;
   611 }
   664 }
   612 
   665 
   613 oop Klass::class_loader() const { return class_loader_data()->class_loader(); }
   666 void Klass::check_array_allocation_length(int length, int max_length, TRAPS) {
       
   667   if (length > max_length) {
       
   668     if (!THREAD->in_retryable_allocation()) {
       
   669       report_java_out_of_memory("Requested array size exceeds VM limit");
       
   670       JvmtiExport::post_array_size_exhausted();
       
   671       THROW_OOP(Universe::out_of_memory_error_array_size());
       
   672     } else {
       
   673       THROW_OOP(Universe::out_of_memory_error_retry());
       
   674     }
       
   675   } else if (length < 0) {
       
   676     THROW_MSG(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", length));
       
   677   }
       
   678 }
   614 
   679 
   615 // In product mode, this function doesn't have virtual function calls so
   680 // In product mode, this function doesn't have virtual function calls so
   616 // there might be some performance advantage to handling InstanceKlass here.
   681 // there might be some performance advantage to handling InstanceKlass here.
   617 const char* Klass::external_name() const {
   682 const char* Klass::external_name() const {
   618   if (is_instance_klass()) {
   683   if (is_instance_klass()) {
   668   st->print("%s", internal_name());
   733   st->print("%s", internal_name());
   669   print_address_on(st);
   734   print_address_on(st);
   670   st->cr();
   735   st->cr();
   671 }
   736 }
   672 
   737 
       
   738 #define BULLET  " - "
       
   739 
   673 void Klass::oop_print_on(oop obj, outputStream* st) {
   740 void Klass::oop_print_on(oop obj, outputStream* st) {
   674   ResourceMark rm;
       
   675   // print title
   741   // print title
   676   st->print_cr("%s ", internal_name());
   742   st->print_cr("%s ", internal_name());
   677   obj->print_address_on(st);
   743   obj->print_address_on(st);
   678 
   744 
   679   if (WizardMode) {
   745   if (WizardMode) {
   680      // print header
   746      // print header
   681      obj->mark()->print_on(st);
   747      obj->mark().print_on(st);
       
   748      st->cr();
       
   749      st->print(BULLET"prototype_header: " INTPTR_FORMAT, _prototype_header.value());
       
   750      st->cr();
   682   }
   751   }
   683 
   752 
   684   // print class
   753   // print class
   685   st->print(" - klass: ");
   754   st->print(BULLET"klass: ");
   686   obj->klass()->print_value_on(st);
   755   obj->klass()->print_value_on(st);
   687   st->cr();
   756   st->cr();
   688 }
   757 }
   689 
   758 
   690 void Klass::oop_print_value_on(oop obj, outputStream* st) {
   759 void Klass::oop_print_value_on(oop obj, outputStream* st) {
   696 
   765 
   697 #if INCLUDE_SERVICES
   766 #if INCLUDE_SERVICES
   698 // Size Statistics
   767 // Size Statistics
   699 void Klass::collect_statistics(KlassSizeStats *sz) const {
   768 void Klass::collect_statistics(KlassSizeStats *sz) const {
   700   sz->_klass_bytes = sz->count(this);
   769   sz->_klass_bytes = sz->count(this);
   701   sz->_mirror_bytes = sz->count(java_mirror());
   770   sz->_mirror_bytes = sz->count(java_mirror_no_keepalive());
   702   sz->_secondary_supers_bytes = sz->count_array(secondary_supers());
   771   sz->_secondary_supers_bytes = sz->count_array(secondary_supers());
   703 
   772 
   704   sz->_ro_bytes += sz->_secondary_supers_bytes;
   773   sz->_ro_bytes += sz->_secondary_supers_bytes;
   705   sz->_rw_bytes += sz->_klass_bytes + sz->_mirror_bytes;
   774   sz->_rw_bytes += sz->_klass_bytes + sz->_mirror_bytes;
   706 }
   775 }
   728     if (ko != NULL) {
   797     if (ko != NULL) {
   729       guarantee(ko->is_klass(), "should be klass");
   798       guarantee(ko->is_klass(), "should be klass");
   730     }
   799     }
   731   }
   800   }
   732 
   801 
   733   if (java_mirror() != NULL) {
   802   if (java_mirror_no_keepalive() != NULL) {
   734     guarantee(oopDesc::is_oop(java_mirror()), "should be instance");
   803     guarantee(oopDesc::is_oop(java_mirror_no_keepalive()), "should be instance");
   735   }
   804   }
   736 }
   805 }
   737 
   806 
   738 void Klass::oop_verify_on(oop obj, outputStream* st) {
   807 void Klass::oop_verify_on(oop obj, outputStream* st) {
   739   guarantee(oopDesc::is_oop(obj),  "should be oop");
   808   guarantee(oopDesc::is_oop(obj),  "should be oop");
   740   guarantee(obj->klass()->is_klass(), "klass field is not a klass");
   809   guarantee(obj->klass()->is_klass(), "klass field is not a klass");
   741 }
   810 }
   742 
   811 
   743 klassVtable Klass::vtable() const {
   812 bool Klass::is_valid(Klass* k) {
   744   return klassVtable(const_cast<Klass*>(this), start_of_vtable(), vtable_length() / vtableEntry::size());
   813   if (!is_aligned(k, sizeof(MetaWord))) return false;
   745 }
   814   if ((size_t)k < os::min_page_size()) return false;
   746 
   815 
   747 vtableEntry* Klass::start_of_vtable() const {
   816   if (!os::is_readable_range(k, k + 1)) return false;
   748   return (vtableEntry*) ((address)this + in_bytes(vtable_start_offset()));
   817   if (!Metaspace::contains(k)) return false;
       
   818 
       
   819   if (!Symbol::is_valid(k->name())) return false;
       
   820   return ClassLoaderDataGraph::is_valid(k->class_loader_data());
   749 }
   821 }
   750 
   822 
   751 Method* Klass::method_at_vtable(int index)  {
   823 Method* Klass::method_at_vtable(int index)  {
   752 #ifndef PRODUCT
   824 #ifndef PRODUCT
   753   assert(index >= 0, "valid vtable index");
   825   assert(index >= 0, "valid vtable index");
   756   }
   828   }
   757 #endif
   829 #endif
   758   return start_of_vtable()[index].method();
   830   return start_of_vtable()[index].method();
   759 }
   831 }
   760 
   832 
   761 ByteSize Klass::vtable_start_offset() {
       
   762   return in_ByteSize(InstanceKlass::header_size() * wordSize);
       
   763 }
       
   764 
   833 
   765 #ifndef PRODUCT
   834 #ifndef PRODUCT
   766 
   835 
   767 bool Klass::verify_vtable_index(int i) {
   836 bool Klass::verify_vtable_index(int i) {
   768   int limit = vtable_length()/vtableEntry::size();
   837   int limit = vtable_length()/vtableEntry::size();
   860   const char* parent_loader_phrase = "";
   929   const char* parent_loader_phrase = "";
   861   const char* parent_loader_name_and_id = "";
   930   const char* parent_loader_name_and_id = "";
   862   if (include_parent_loader &&
   931   if (include_parent_loader &&
   863       !cld->is_builtin_class_loader_data()) {
   932       !cld->is_builtin_class_loader_data()) {
   864     oop parent_loader = java_lang_ClassLoader::parent(class_loader());
   933     oop parent_loader = java_lang_ClassLoader::parent(class_loader());
   865     ClassLoaderData *parent_cld = ClassLoaderData::class_loader_data(parent_loader);
   934     ClassLoaderData *parent_cld = ClassLoaderData::class_loader_data_or_null(parent_loader);
   866     assert(parent_cld != NULL, "parent's class loader data should not be null");
   935     // The parent loader's ClassLoaderData could be null if it is
   867     parent_loader_name_and_id = parent_cld->loader_name_and_id();
   936     // a delegating class loader that has never defined a class.
       
   937     // In this case the loader's name must be obtained via the parent loader's oop.
       
   938     if (parent_cld == NULL) {
       
   939       oop cl_name_and_id = java_lang_ClassLoader::nameAndId(parent_loader);
       
   940       if (cl_name_and_id != NULL) {
       
   941         parent_loader_name_and_id = java_lang_String::as_utf8_string(cl_name_and_id);
       
   942       }
       
   943     } else {
       
   944       parent_loader_name_and_id = parent_cld->loader_name_and_id();
       
   945     }
   868     parent_loader_phrase = ", parent loader ";
   946     parent_loader_phrase = ", parent loader ";
   869     len += strlen(parent_loader_phrase) + strlen(parent_loader_name_and_id);
   947     len += strlen(parent_loader_phrase) + strlen(parent_loader_name_and_id);
   870   }
   948   }
   871 
   949 
   872   // Start to construct final full class description string
   950   // Start to construct final full class description string