1 /*
   2  * Copyright (c) 1997, 2011, 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/systemDictionary.hpp"
  28 #include "gc_implementation/shared/markSweep.inline.hpp"
  29 #include "gc_interface/collectedHeap.inline.hpp"
  30 #include "interpreter/oopMapCache.hpp"
  31 #include "memory/gcLocker.hpp"
  32 #include "oops/constantPoolOop.hpp"
  33 #include "oops/instanceKlass.hpp"
  34 #include "oops/instanceMirrorKlass.hpp"
  35 #include "oops/instanceKlassKlass.hpp"
  36 #include "oops/instanceRefKlass.hpp"
  37 #include "oops/objArrayKlassKlass.hpp"
  38 #include "oops/objArrayOop.hpp"
  39 #include "oops/oop.inline.hpp"
  40 #include "oops/oop.inline2.hpp"
  41 #include "oops/symbol.hpp"
  42 #include "oops/typeArrayOop.hpp"
  43 #include "prims/jvmtiExport.hpp"
  44 #include "runtime/fieldDescriptor.hpp"
  45 #ifndef SERIALGC
  46 #include "gc_implementation/parNew/parOopClosures.inline.hpp"
  47 #include "gc_implementation/parallelScavenge/psPromotionManager.inline.hpp"
  48 #include "gc_implementation/parallelScavenge/psScavenge.inline.hpp"
  49 #include "memory/cardTableRS.hpp"
  50 #include "oops/oop.pcgc.inline.hpp"
  51 #endif
  52 
  53 klassOop instanceKlassKlass::create_klass(TRAPS) {
  54   instanceKlassKlass o;
  55   KlassHandle h_this_klass(THREAD, Universe::klassKlassObj());
  56   KlassHandle k = base_create_klass(h_this_klass, header_size(), o.vtbl_value(), CHECK_NULL);
  57   // Make sure size calculation is right
  58   assert(k()->size() == align_object_size(header_size()), "wrong size for object");
  59   java_lang_Class::create_mirror(k, CHECK_NULL); // Allocate mirror
  60   return k();
  61 }
  62 
  63 int instanceKlassKlass::oop_size(oop obj) const {
  64   assert(obj->is_klass(), "must be klass");
  65   return instanceKlass::cast(klassOop(obj))->object_size();
  66 }
  67 
  68 bool instanceKlassKlass::oop_is_parsable(oop obj) const {
  69   assert(obj->is_klass(), "must be klass");
  70   instanceKlass* ik = instanceKlass::cast(klassOop(obj));
  71   return (!ik->null_vtbl()) && ik->object_is_parsable();
  72 }
  73 
  74 void instanceKlassKlass::iterate_c_heap_oops(instanceKlass* ik,
  75                                              OopClosure* closure) {
  76   if (ik->oop_map_cache() != NULL) {
  77     ik->oop_map_cache()->oop_iterate(closure);
  78   }
  79 
  80   if (ik->jni_ids() != NULL) {
  81     ik->jni_ids()->oops_do(closure);
  82   }
  83 }
  84 
  85 void instanceKlassKlass::oop_follow_contents(oop obj) {
  86   assert(obj->is_klass(),"must be a klass");
  87   assert(klassOop(obj)->klass_part()->oop_is_instance_slow(), "must be instance klass");
  88 
  89   instanceKlass* ik = instanceKlass::cast(klassOop(obj));
  90   {
  91     HandleMark hm;
  92     ik->vtable()->oop_follow_contents();
  93     ik->itable()->oop_follow_contents();
  94   }
  95 
  96   MarkSweep::mark_and_push(ik->adr_array_klasses());
  97   MarkSweep::mark_and_push(ik->adr_methods());
  98   MarkSweep::mark_and_push(ik->adr_method_ordering());
  99   MarkSweep::mark_and_push(ik->adr_local_interfaces());
 100   MarkSweep::mark_and_push(ik->adr_transitive_interfaces());
 101   MarkSweep::mark_and_push(ik->adr_fields());
 102   MarkSweep::mark_and_push(ik->adr_constants());
 103   MarkSweep::mark_and_push(ik->adr_class_loader());
 104   MarkSweep::mark_and_push(ik->adr_inner_classes());
 105   MarkSweep::mark_and_push(ik->adr_protection_domain());
 106   MarkSweep::mark_and_push(ik->adr_host_klass());
 107   MarkSweep::mark_and_push(ik->adr_signers());
 108   MarkSweep::mark_and_push(ik->adr_class_annotations());
 109   MarkSweep::mark_and_push(ik->adr_fields_annotations());
 110   MarkSweep::mark_and_push(ik->adr_methods_annotations());
 111   MarkSweep::mark_and_push(ik->adr_methods_parameter_annotations());
 112   MarkSweep::mark_and_push(ik->adr_methods_default_annotations());
 113 
 114   // We do not follow adr_implementors() here. It is followed later
 115   // in instanceKlass::follow_weak_klass_links()
 116 
 117   klassKlass::oop_follow_contents(obj);
 118 
 119   iterate_c_heap_oops(ik, &MarkSweep::mark_and_push_closure);
 120 }
 121 
 122 #ifndef SERIALGC
 123 void instanceKlassKlass::oop_follow_contents(ParCompactionManager* cm,
 124                                              oop obj) {
 125   assert(obj->is_klass(),"must be a klass");
 126   assert(klassOop(obj)->klass_part()->oop_is_instance_slow(), "must be instance klass");
 127 
 128   instanceKlass* ik = instanceKlass::cast(klassOop(obj));
 129   ik->vtable()->oop_follow_contents(cm);
 130   ik->itable()->oop_follow_contents(cm);
 131 
 132   PSParallelCompact::mark_and_push(cm, ik->adr_array_klasses());
 133   PSParallelCompact::mark_and_push(cm, ik->adr_methods());
 134   PSParallelCompact::mark_and_push(cm, ik->adr_method_ordering());
 135   PSParallelCompact::mark_and_push(cm, ik->adr_local_interfaces());
 136   PSParallelCompact::mark_and_push(cm, ik->adr_transitive_interfaces());
 137   PSParallelCompact::mark_and_push(cm, ik->adr_fields());
 138   PSParallelCompact::mark_and_push(cm, ik->adr_constants());
 139   PSParallelCompact::mark_and_push(cm, ik->adr_class_loader());
 140   PSParallelCompact::mark_and_push(cm, ik->adr_inner_classes());
 141   PSParallelCompact::mark_and_push(cm, ik->adr_protection_domain());
 142   PSParallelCompact::mark_and_push(cm, ik->adr_host_klass());
 143   PSParallelCompact::mark_and_push(cm, ik->adr_signers());
 144   PSParallelCompact::mark_and_push(cm, ik->adr_class_annotations());
 145   PSParallelCompact::mark_and_push(cm, ik->adr_fields_annotations());
 146   PSParallelCompact::mark_and_push(cm, ik->adr_methods_annotations());
 147   PSParallelCompact::mark_and_push(cm, ik->adr_methods_parameter_annotations());
 148   PSParallelCompact::mark_and_push(cm, ik->adr_methods_default_annotations());
 149 
 150   // We do not follow adr_implementor() here. It is followed later
 151   // in instanceKlass::follow_weak_klass_links()
 152 
 153   klassKlass::oop_follow_contents(cm, obj);
 154 
 155   PSParallelCompact::MarkAndPushClosure mark_and_push_closure(cm);
 156   iterate_c_heap_oops(ik, &mark_and_push_closure);
 157 }
 158 #endif // SERIALGC
 159 
 160 int instanceKlassKlass::oop_oop_iterate(oop obj, OopClosure* blk) {
 161   assert(obj->is_klass(),"must be a klass");
 162   assert(klassOop(obj)->klass_part()->oop_is_instance_slow(), "must be instance klass");
 163   instanceKlass* ik = instanceKlass::cast(klassOop(obj));
 164   // Get size before changing pointers.
 165   // Don't call size() or oop_size() since that is a virtual call.
 166   int size = ik->object_size();
 167 
 168   ik->vtable()->oop_oop_iterate(blk);
 169   ik->itable()->oop_oop_iterate(blk);
 170 
 171   blk->do_oop(ik->adr_array_klasses());
 172   blk->do_oop(ik->adr_methods());
 173   blk->do_oop(ik->adr_method_ordering());
 174   blk->do_oop(ik->adr_local_interfaces());
 175   blk->do_oop(ik->adr_transitive_interfaces());
 176   blk->do_oop(ik->adr_fields());
 177   blk->do_oop(ik->adr_constants());
 178   blk->do_oop(ik->adr_class_loader());
 179   blk->do_oop(ik->adr_protection_domain());
 180   blk->do_oop(ik->adr_host_klass());
 181   blk->do_oop(ik->adr_signers());
 182   blk->do_oop(ik->adr_inner_classes());
 183   for (int i = 0; i < instanceKlass::implementors_limit; i++) {
 184     blk->do_oop(&ik->adr_implementors()[i]);
 185   }
 186   blk->do_oop(ik->adr_class_annotations());
 187   blk->do_oop(ik->adr_fields_annotations());
 188   blk->do_oop(ik->adr_methods_annotations());
 189   blk->do_oop(ik->adr_methods_parameter_annotations());
 190   blk->do_oop(ik->adr_methods_default_annotations());
 191 
 192   klassKlass::oop_oop_iterate(obj, blk);
 193 
 194   if(ik->oop_map_cache() != NULL) ik->oop_map_cache()->oop_iterate(blk);
 195   return size;
 196 }
 197 
 198 int instanceKlassKlass::oop_oop_iterate_m(oop obj, OopClosure* blk,
 199                                            MemRegion mr) {
 200   assert(obj->is_klass(),"must be a klass");
 201   assert(klassOop(obj)->klass_part()->oop_is_instance_slow(), "must be instance klass");
 202   instanceKlass* ik = instanceKlass::cast(klassOop(obj));
 203   // Get size before changing pointers.
 204   // Don't call size() or oop_size() since that is a virtual call.
 205   int size = ik->object_size();
 206 
 207   ik->vtable()->oop_oop_iterate_m(blk, mr);
 208   ik->itable()->oop_oop_iterate_m(blk, mr);
 209 
 210   oop* adr;
 211   adr = ik->adr_array_klasses();
 212   if (mr.contains(adr)) blk->do_oop(adr);
 213   adr = ik->adr_methods();
 214   if (mr.contains(adr)) blk->do_oop(adr);
 215   adr = ik->adr_method_ordering();
 216   if (mr.contains(adr)) blk->do_oop(adr);
 217   adr = ik->adr_local_interfaces();
 218   if (mr.contains(adr)) blk->do_oop(adr);
 219   adr = ik->adr_transitive_interfaces();
 220   if (mr.contains(adr)) blk->do_oop(adr);
 221   adr = ik->adr_fields();
 222   if (mr.contains(adr)) blk->do_oop(adr);
 223   adr = ik->adr_constants();
 224   if (mr.contains(adr)) blk->do_oop(adr);
 225   adr = ik->adr_class_loader();
 226   if (mr.contains(adr)) blk->do_oop(adr);
 227   adr = ik->adr_protection_domain();
 228   if (mr.contains(adr)) blk->do_oop(adr);
 229   adr = ik->adr_host_klass();
 230   if (mr.contains(adr)) blk->do_oop(adr);
 231   adr = ik->adr_signers();
 232   if (mr.contains(adr)) blk->do_oop(adr);
 233   adr = ik->adr_inner_classes();
 234   if (mr.contains(adr)) blk->do_oop(adr);
 235   adr = ik->adr_implementors();
 236   for (int i = 0; i < instanceKlass::implementors_limit; i++) {
 237     if (mr.contains(&adr[i])) blk->do_oop(&adr[i]);
 238   }
 239   adr = ik->adr_class_annotations();
 240   if (mr.contains(adr)) blk->do_oop(adr);
 241   adr = ik->adr_fields_annotations();
 242   if (mr.contains(adr)) blk->do_oop(adr);
 243   adr = ik->adr_methods_annotations();
 244   if (mr.contains(adr)) blk->do_oop(adr);
 245   adr = ik->adr_methods_parameter_annotations();
 246   if (mr.contains(adr)) blk->do_oop(adr);
 247   adr = ik->adr_methods_default_annotations();
 248   if (mr.contains(adr)) blk->do_oop(adr);
 249 
 250   klassKlass::oop_oop_iterate_m(obj, blk, mr);
 251 
 252   if(ik->oop_map_cache() != NULL) ik->oop_map_cache()->oop_iterate(blk, mr);
 253   return size;
 254 }
 255 
 256 int instanceKlassKlass::oop_adjust_pointers(oop obj) {
 257   assert(obj->is_klass(),"must be a klass");
 258   assert(klassOop(obj)->klass_part()->oop_is_instance_slow(), "must be instance klass");
 259 
 260   instanceKlass* ik = instanceKlass::cast(klassOop(obj));
 261   ik->vtable()->oop_adjust_pointers();
 262   ik->itable()->oop_adjust_pointers();
 263 
 264   MarkSweep::adjust_pointer(ik->adr_array_klasses());
 265   MarkSweep::adjust_pointer(ik->adr_methods());
 266   MarkSweep::adjust_pointer(ik->adr_method_ordering());
 267   MarkSweep::adjust_pointer(ik->adr_local_interfaces());
 268   MarkSweep::adjust_pointer(ik->adr_transitive_interfaces());
 269   MarkSweep::adjust_pointer(ik->adr_fields());
 270   MarkSweep::adjust_pointer(ik->adr_constants());
 271   MarkSweep::adjust_pointer(ik->adr_class_loader());
 272   MarkSweep::adjust_pointer(ik->adr_protection_domain());
 273   MarkSweep::adjust_pointer(ik->adr_host_klass());
 274   MarkSweep::adjust_pointer(ik->adr_signers());
 275   MarkSweep::adjust_pointer(ik->adr_inner_classes());
 276   for (int i = 0; i < instanceKlass::implementors_limit; i++) {
 277     MarkSweep::adjust_pointer(&ik->adr_implementors()[i]);
 278   }
 279   MarkSweep::adjust_pointer(ik->adr_class_annotations());
 280   MarkSweep::adjust_pointer(ik->adr_fields_annotations());
 281   MarkSweep::adjust_pointer(ik->adr_methods_annotations());
 282   MarkSweep::adjust_pointer(ik->adr_methods_parameter_annotations());
 283   MarkSweep::adjust_pointer(ik->adr_methods_default_annotations());
 284 
 285   iterate_c_heap_oops(ik, &MarkSweep::adjust_root_pointer_closure);
 286 
 287   return klassKlass::oop_adjust_pointers(obj);
 288 }
 289 
 290 #ifndef SERIALGC
 291 void instanceKlassKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
 292   instanceKlass* ik = instanceKlass::cast(klassOop(obj));
 293 
 294   oop* loader_addr = ik->adr_class_loader();
 295   if (PSScavenge::should_scavenge(loader_addr)) {
 296     pm->claim_or_forward_depth(loader_addr);
 297   }
 298 
 299   oop* pd_addr = ik->adr_protection_domain();
 300   if (PSScavenge::should_scavenge(pd_addr)) {
 301     pm->claim_or_forward_depth(pd_addr);
 302   }
 303 
 304   oop* hk_addr = ik->adr_host_klass();
 305   if (PSScavenge::should_scavenge(hk_addr)) {
 306     pm->claim_or_forward_depth(hk_addr);
 307   }
 308 
 309   oop* sg_addr = ik->adr_signers();
 310   if (PSScavenge::should_scavenge(sg_addr)) {
 311     pm->claim_or_forward_depth(sg_addr);
 312   }
 313 
 314   klassKlass::oop_push_contents(pm, obj);
 315 }
 316 
 317 int instanceKlassKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
 318   assert(obj->is_klass(),"must be a klass");
 319   assert(klassOop(obj)->klass_part()->oop_is_instance_slow(),
 320          "must be instance klass");
 321 
 322   instanceKlass* ik = instanceKlass::cast(klassOop(obj));
 323   ik->vtable()->oop_update_pointers(cm);
 324   ik->itable()->oop_update_pointers(cm);
 325 
 326   oop* const beg_oop = ik->oop_block_beg();
 327   oop* const end_oop = ik->oop_block_end();
 328   for (oop* cur_oop = beg_oop; cur_oop < end_oop; ++cur_oop) {
 329     PSParallelCompact::adjust_pointer(cur_oop);
 330   }
 331 
 332   OopClosure* closure = PSParallelCompact::adjust_root_pointer_closure();
 333   iterate_c_heap_oops(ik, closure);
 334 
 335   klassKlass::oop_update_pointers(cm, obj);
 336   return ik->object_size();
 337 }
 338 
 339 #endif // SERIALGC
 340 
 341 klassOop
 342 instanceKlassKlass::allocate_instance_klass(Symbol* name, int vtable_len, int itable_len,
 343                                             int static_field_size,
 344                                             unsigned nonstatic_oop_map_count,
 345                                             ReferenceType rt, TRAPS) {
 346 
 347   const int nonstatic_oop_map_size =
 348     instanceKlass::nonstatic_oop_map_size(nonstatic_oop_map_count);
 349   int size = instanceKlass::object_size(align_object_offset(vtable_len) + align_object_offset(itable_len) + nonstatic_oop_map_size);
 350 
 351   // Allocation
 352   KlassHandle h_this_klass(THREAD, as_klassOop());
 353   KlassHandle k;
 354   if (rt == REF_NONE) {
 355     if (name != vmSymbols::java_lang_Class()) {
 356       // regular klass
 357       instanceKlass o;
 358       k = base_create_klass(h_this_klass, size, o.vtbl_value(), CHECK_NULL);
 359     } else {
 360       // Class
 361       instanceMirrorKlass o;
 362       k = base_create_klass(h_this_klass, size, o.vtbl_value(), CHECK_NULL);
 363     }
 364   } else {
 365     // reference klass
 366     instanceRefKlass o;
 367     k = base_create_klass(h_this_klass, size, o.vtbl_value(), CHECK_NULL);
 368   }
 369   {
 370     No_Safepoint_Verifier no_safepoint; // until k becomes parsable
 371     instanceKlass* ik = (instanceKlass*) k()->klass_part();
 372     assert(!k()->is_parsable(), "not expecting parsability yet.");
 373 
 374     // The sizes of these these three variables are used for determining the
 375     // size of the instanceKlassOop. It is critical that these are set to the right
 376     // sizes before the first GC, i.e., when we allocate the mirror.
 377     ik->set_vtable_length(vtable_len);
 378     ik->set_itable_length(itable_len);
 379     ik->set_static_field_size(static_field_size);
 380     ik->set_nonstatic_oop_map_size(nonstatic_oop_map_size);
 381     assert(k()->size() == size, "wrong size for object");
 382 
 383     ik->set_array_klasses(NULL);
 384     ik->set_methods(NULL);
 385     ik->set_method_ordering(NULL);
 386     ik->set_local_interfaces(NULL);
 387     ik->set_transitive_interfaces(NULL);
 388     ik->init_implementor();
 389     ik->set_fields(NULL, 0);
 390     ik->set_constants(NULL);
 391     ik->set_class_loader(NULL);
 392     ik->set_protection_domain(NULL);
 393     ik->set_host_klass(NULL);
 394     ik->set_signers(NULL);
 395     ik->set_source_file_name(NULL);
 396     ik->set_source_debug_extension(NULL);
 397     ik->set_source_debug_extension(NULL);
 398     ik->set_array_name(NULL);
 399     ik->set_inner_classes(NULL);
 400     ik->set_static_oop_field_count(0);
 401     ik->set_nonstatic_field_size(0);
 402     ik->set_is_marked_dependent(false);
 403     ik->set_init_state(instanceKlass::allocated);
 404     ik->set_init_thread(NULL);
 405     ik->set_reference_type(rt);
 406     ik->set_oop_map_cache(NULL);
 407     ik->set_jni_ids(NULL);
 408     ik->set_osr_nmethods_head(NULL);
 409     ik->set_breakpoints(NULL);
 410     ik->init_previous_versions();
 411     ik->set_generic_signature(NULL);
 412     ik->release_set_methods_jmethod_ids(NULL);
 413     ik->release_set_methods_cached_itable_indices(NULL);
 414     ik->set_class_annotations(NULL);
 415     ik->set_fields_annotations(NULL);
 416     ik->set_methods_annotations(NULL);
 417     ik->set_methods_parameter_annotations(NULL);
 418     ik->set_methods_default_annotations(NULL);
 419     ik->set_jvmti_cached_class_field_map(NULL);
 420     ik->set_initial_method_idnum(0);
 421     assert(k()->is_parsable(), "should be parsable here.");
 422 
 423     // initialize the non-header words to zero
 424     intptr_t* p = (intptr_t*)k();
 425     for (int index = instanceKlass::header_size(); index < size; index++) {
 426       p[index] = NULL_WORD;
 427     }
 428 
 429     // To get verify to work - must be set to partial loaded before first GC point.
 430     k()->set_partially_loaded();
 431   }
 432   return k();
 433 }
 434 
 435 
 436 
 437 #ifndef PRODUCT
 438 
 439 // Printing
 440 
 441 #define BULLET  " - "
 442 
 443 static const char* state_names[] = {
 444   "unparseable_by_gc", "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error"
 445 };
 446 
 447 
 448 void instanceKlassKlass::oop_print_on(oop obj, outputStream* st) {
 449   assert(obj->is_klass(), "must be klass");
 450   instanceKlass* ik = instanceKlass::cast(klassOop(obj));
 451   klassKlass::oop_print_on(obj, st);
 452 
 453   st->print(BULLET"instance size:     %d", ik->size_helper());                        st->cr();
 454   st->print(BULLET"klass size:        %d", ik->object_size());                        st->cr();
 455   st->print(BULLET"access:            "); ik->access_flags().print_on(st);            st->cr();
 456   st->print(BULLET"state:             "); st->print_cr(state_names[ik->_init_state]);
 457   st->print(BULLET"name:              "); ik->name()->print_value_on(st);             st->cr();
 458   st->print(BULLET"super:             "); ik->super()->print_value_on(st);            st->cr();
 459   st->print(BULLET"sub:               ");
 460   Klass* sub = ik->subklass();
 461   int n;
 462   for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) {
 463     if (n < MaxSubklassPrintSize) {
 464       sub->as_klassOop()->print_value_on(st);
 465       st->print("   ");
 466     }
 467   }
 468   if (n >= MaxSubklassPrintSize) st->print("(%d more klasses...)", n - MaxSubklassPrintSize);
 469   st->cr();
 470 
 471   if (ik->is_interface()) {
 472     st->print_cr(BULLET"nof implementors:  %d", ik->nof_implementors());
 473     int print_impl = 0;
 474     for (int i = 0; i < instanceKlass::implementors_limit; i++) {
 475       if (ik->implementor(i) != NULL) {
 476         if (++print_impl == 1)
 477           st->print_cr(BULLET"implementor:    ");
 478         st->print("   ");
 479         ik->implementor(i)->print_value_on(st);
 480       }
 481     }
 482     if (print_impl > 0)  st->cr();
 483   }
 484 
 485   st->print(BULLET"arrays:            "); ik->array_klasses()->print_value_on(st);     st->cr();
 486   st->print(BULLET"methods:           "); ik->methods()->print_value_on(st);           st->cr();
 487   if (Verbose) {
 488     objArrayOop methods = ik->methods();
 489     for(int i = 0; i < methods->length(); i++) {
 490       tty->print("%d : ", i); methods->obj_at(i)->print_value(); tty->cr();
 491     }
 492   }
 493   st->print(BULLET"method ordering:   "); ik->method_ordering()->print_value_on(st);       st->cr();
 494   st->print(BULLET"local interfaces:  "); ik->local_interfaces()->print_value_on(st);      st->cr();
 495   st->print(BULLET"trans. interfaces: "); ik->transitive_interfaces()->print_value_on(st); st->cr();
 496   st->print(BULLET"constants:         "); ik->constants()->print_value_on(st);         st->cr();
 497   st->print(BULLET"class loader:      "); ik->class_loader()->print_value_on(st);      st->cr();
 498   st->print(BULLET"protection domain: "); ik->protection_domain()->print_value_on(st); st->cr();
 499   st->print(BULLET"host class:        "); ik->host_klass()->print_value_on(st);        st->cr();
 500   st->print(BULLET"signers:           "); ik->signers()->print_value_on(st);           st->cr();
 501   if (ik->source_file_name() != NULL) {
 502     st->print(BULLET"source file:       ");
 503     ik->source_file_name()->print_value_on(st);
 504     st->cr();
 505   }
 506   if (ik->source_debug_extension() != NULL) {
 507     st->print(BULLET"source debug extension:       ");
 508     ik->source_debug_extension()->print_value_on(st);
 509     st->cr();
 510   }
 511 
 512   {
 513     ResourceMark rm;
 514     // PreviousVersionInfo objects returned via PreviousVersionWalker
 515     // contain a GrowableArray of handles. We have to clean up the
 516     // GrowableArray _after_ the PreviousVersionWalker destructor
 517     // has destroyed the handles.
 518     {
 519       bool have_pv = false;
 520       PreviousVersionWalker pvw(ik);
 521       for (PreviousVersionInfo * pv_info = pvw.next_previous_version();
 522            pv_info != NULL; pv_info = pvw.next_previous_version()) {
 523         if (!have_pv)
 524           st->print(BULLET"previous version:  ");
 525         have_pv = true;
 526         pv_info->prev_constant_pool_handle()()->print_value_on(st);
 527       }
 528       if (have_pv)  st->cr();
 529     } // pvw is cleaned up
 530   } // rm is cleaned up
 531 
 532   if (ik->generic_signature() != NULL) {
 533     st->print(BULLET"generic signature: ");
 534     ik->generic_signature()->print_value_on(st);
 535     st->cr();
 536   }
 537   st->print(BULLET"inner classes:     "); ik->inner_classes()->print_value_on(st);     st->cr();
 538   st->print(BULLET"java mirror:       "); ik->java_mirror()->print_value_on(st);       st->cr();
 539   st->print(BULLET"vtable length      %d  (start addr: " INTPTR_FORMAT ")", ik->vtable_length(), ik->start_of_vtable());  st->cr();
 540   st->print(BULLET"itable length      %d (start addr: " INTPTR_FORMAT ")", ik->itable_length(), ik->start_of_itable()); st->cr();
 541   st->print_cr(BULLET"---- static fields (%d words):", ik->static_field_size());
 542   FieldPrinter print_static_field(st);
 543   ik->do_local_static_fields(&print_static_field);
 544   st->print_cr(BULLET"---- non-static fields (%d words):", ik->nonstatic_field_size());
 545   FieldPrinter print_nonstatic_field(st);
 546   ik->do_nonstatic_fields(&print_nonstatic_field);
 547 
 548   st->print(BULLET"non-static oop maps: ");
 549   OopMapBlock* map     = ik->start_of_nonstatic_oop_maps();
 550   OopMapBlock* end_map = map + ik->nonstatic_oop_map_count();
 551   while (map < end_map) {
 552     st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
 553     map++;
 554   }
 555   st->cr();
 556 }
 557 
 558 #endif //PRODUCT
 559 
 560 void instanceKlassKlass::oop_print_value_on(oop obj, outputStream* st) {
 561   assert(obj->is_klass(), "must be klass");
 562   instanceKlass* ik = instanceKlass::cast(klassOop(obj));
 563   ik->name()->print_value_on(st);
 564 }
 565 
 566 const char* instanceKlassKlass::internal_name() const {
 567   return "{instance class}";
 568 }
 569 
 570 // Verification
 571 
 572 class VerifyFieldClosure: public OopClosure {
 573  protected:
 574   template <class T> void do_oop_work(T* p) {
 575     guarantee(Universe::heap()->is_in(p), "should be in heap");
 576     oop obj = oopDesc::load_decode_heap_oop(p);
 577     guarantee(obj->is_oop_or_null(), "should be in heap");
 578   }
 579  public:
 580   virtual void do_oop(oop* p)       { VerifyFieldClosure::do_oop_work(p); }
 581   virtual void do_oop(narrowOop* p) { VerifyFieldClosure::do_oop_work(p); }
 582 };
 583 
 584 void instanceKlassKlass::oop_verify_on(oop obj, outputStream* st) {
 585   klassKlass::oop_verify_on(obj, st);
 586   if (!obj->partially_loaded()) {
 587     Thread *thread = Thread::current();
 588     instanceKlass* ik = instanceKlass::cast(klassOop(obj));
 589 
 590 #ifndef PRODUCT
 591     // Avoid redundant verifies
 592     if (ik->_verify_count == Universe::verify_count()) return;
 593     ik->_verify_count = Universe::verify_count();
 594 #endif
 595     // Verify that klass is present in SystemDictionary
 596     if (ik->is_loaded() && !ik->is_anonymous()) {
 597       Symbol* h_name = ik->name();
 598       Handle h_loader (thread, ik->class_loader());
 599       Handle h_obj(thread, obj);
 600       SystemDictionary::verify_obj_klass_present(h_obj, h_name, h_loader);
 601     }
 602 
 603     // Verify static fields
 604     VerifyFieldClosure blk;
 605 
 606     // Verify vtables
 607     if (ik->is_linked()) {
 608       ResourceMark rm(thread);
 609       // $$$ This used to be done only for m/s collections.  Doing it
 610       // always seemed a valid generalization.  (DLD -- 6/00)
 611       ik->vtable()->verify(st);
 612     }
 613 
 614     // Verify oop map cache
 615     if (ik->oop_map_cache() != NULL) {
 616       ik->oop_map_cache()->verify();
 617     }
 618 
 619     // Verify first subklass
 620     if (ik->subklass_oop() != NULL) {
 621       guarantee(ik->subklass_oop()->is_perm(),  "should be in permspace");
 622       guarantee(ik->subklass_oop()->is_klass(), "should be klass");
 623     }
 624 
 625     // Verify siblings
 626     klassOop super = ik->super();
 627     Klass* sib = ik->next_sibling();
 628     int sib_count = 0;
 629     while (sib != NULL) {
 630       if (sib == ik) {
 631         fatal(err_msg("subclass cycle of length %d", sib_count));
 632       }
 633       if (sib_count >= 100000) {
 634         fatal(err_msg("suspiciously long subclass list %d", sib_count));
 635       }
 636       guarantee(sib->as_klassOop()->is_klass(), "should be klass");
 637       guarantee(sib->as_klassOop()->is_perm(),  "should be in permspace");
 638       guarantee(sib->super() == super, "siblings should have same superklass");
 639       sib = sib->next_sibling();
 640     }
 641 
 642     // Verify implementor fields
 643     bool saw_null_impl = false;
 644     for (int i = 0; i < instanceKlass::implementors_limit; i++) {
 645       klassOop im = ik->implementor(i);
 646       if (im == NULL) { saw_null_impl = true; continue; }
 647       guarantee(!saw_null_impl, "non-nulls must preceded all nulls");
 648       guarantee(ik->is_interface(), "only interfaces should have implementor set");
 649       guarantee(i < ik->nof_implementors(), "should only have one implementor");
 650       guarantee(im->is_perm(),  "should be in permspace");
 651       guarantee(im->is_klass(), "should be klass");
 652       guarantee(!Klass::cast(klassOop(im))->is_interface(), "implementors cannot be interfaces");
 653     }
 654 
 655     // Verify local interfaces
 656     objArrayOop local_interfaces = ik->local_interfaces();
 657     guarantee(local_interfaces->is_perm(),          "should be in permspace");
 658     guarantee(local_interfaces->is_objArray(),      "should be obj array");
 659     int j;
 660     for (j = 0; j < local_interfaces->length(); j++) {
 661       oop e = local_interfaces->obj_at(j);
 662       guarantee(e->is_klass() && Klass::cast(klassOop(e))->is_interface(), "invalid local interface");
 663     }
 664 
 665     // Verify transitive interfaces
 666     objArrayOop transitive_interfaces = ik->transitive_interfaces();
 667     guarantee(transitive_interfaces->is_perm(),          "should be in permspace");
 668     guarantee(transitive_interfaces->is_objArray(),      "should be obj array");
 669     for (j = 0; j < transitive_interfaces->length(); j++) {
 670       oop e = transitive_interfaces->obj_at(j);
 671       guarantee(e->is_klass() && Klass::cast(klassOop(e))->is_interface(), "invalid transitive interface");
 672     }
 673 
 674     // Verify methods
 675     objArrayOop methods = ik->methods();
 676     guarantee(methods->is_perm(),              "should be in permspace");
 677     guarantee(methods->is_objArray(),          "should be obj array");
 678     for (j = 0; j < methods->length(); j++) {
 679       guarantee(methods->obj_at(j)->is_method(), "non-method in methods array");
 680     }
 681     for (j = 0; j < methods->length() - 1; j++) {
 682       methodOop m1 = methodOop(methods->obj_at(j));
 683       methodOop m2 = methodOop(methods->obj_at(j + 1));
 684       guarantee(m1->name()->fast_compare(m2->name()) <= 0, "methods not sorted correctly");
 685     }
 686 
 687     // Verify method ordering
 688     typeArrayOop method_ordering = ik->method_ordering();
 689     guarantee(method_ordering->is_perm(),              "should be in permspace");
 690     guarantee(method_ordering->is_typeArray(),         "should be type array");
 691     int length = method_ordering->length();
 692     if (JvmtiExport::can_maintain_original_method_order() ||
 693         (UseSharedSpaces && length != 0)) {
 694       guarantee(length == methods->length(),           "invalid method ordering length");
 695       jlong sum = 0;
 696       for (j = 0; j < length; j++) {
 697         int original_index = method_ordering->int_at(j);
 698         guarantee(original_index >= 0 && original_index < length, "invalid method ordering index");
 699         sum += original_index;
 700       }
 701       // Verify sum of indices 0,1,...,length-1
 702       guarantee(sum == ((jlong)length*(length-1))/2,   "invalid method ordering sum");
 703     } else {
 704       guarantee(length == 0,                           "invalid method ordering length");
 705     }
 706 
 707     // Verify JNI static field identifiers
 708     if (ik->jni_ids() != NULL) {
 709       ik->jni_ids()->verify(ik->as_klassOop());
 710     }
 711 
 712     // Verify other fields
 713     if (ik->array_klasses() != NULL) {
 714       guarantee(ik->array_klasses()->is_perm(),      "should be in permspace");
 715       guarantee(ik->array_klasses()->is_klass(),     "should be klass");
 716     }
 717     guarantee(ik->fields()->is_perm(),               "should be in permspace");
 718     guarantee(ik->fields()->is_typeArray(),          "should be type array");
 719     guarantee(ik->constants()->is_perm(),            "should be in permspace");
 720     guarantee(ik->constants()->is_constantPool(),    "should be constant pool");
 721     guarantee(ik->inner_classes()->is_perm(),        "should be in permspace");
 722     guarantee(ik->inner_classes()->is_typeArray(),   "should be type array");
 723     if (ik->protection_domain() != NULL) {
 724       guarantee(ik->protection_domain()->is_oop(),  "should be oop");
 725     }
 726     if (ik->host_klass() != NULL) {
 727       guarantee(ik->host_klass()->is_oop(),  "should be oop");
 728     }
 729     if (ik->signers() != NULL) {
 730       guarantee(ik->signers()->is_objArray(),       "should be obj array");
 731     }
 732     if (ik->class_annotations() != NULL) {
 733       guarantee(ik->class_annotations()->is_typeArray(), "should be type array");
 734     }
 735     if (ik->fields_annotations() != NULL) {
 736       guarantee(ik->fields_annotations()->is_objArray(), "should be obj array");
 737     }
 738     if (ik->methods_annotations() != NULL) {
 739       guarantee(ik->methods_annotations()->is_objArray(), "should be obj array");
 740     }
 741     if (ik->methods_parameter_annotations() != NULL) {
 742       guarantee(ik->methods_parameter_annotations()->is_objArray(), "should be obj array");
 743     }
 744     if (ik->methods_default_annotations() != NULL) {
 745       guarantee(ik->methods_default_annotations()->is_objArray(), "should be obj array");
 746     }
 747   }
 748 }
 749 
 750 
 751 bool instanceKlassKlass::oop_partially_loaded(oop obj) const {
 752   assert(obj->is_klass(), "object must be klass");
 753   instanceKlass* ik = instanceKlass::cast(klassOop(obj));
 754   assert(ik->oop_is_instance(), "object must be instanceKlass");
 755   return ik->transitive_interfaces() == (objArrayOop) obj;   // Check whether transitive_interfaces points to self
 756 }
 757 
 758 
 759 // The transitive_interfaces is the last field set when loading an object.
 760 void instanceKlassKlass::oop_set_partially_loaded(oop obj) {
 761   assert(obj->is_klass(), "object must be klass");
 762   instanceKlass* ik = instanceKlass::cast(klassOop(obj));
 763   // Set the layout helper to a place-holder value, until fuller initialization.
 764   // (This allows asserts in oop_is_instance to succeed.)
 765   ik->set_layout_helper(Klass::instance_layout_helper(0, true));
 766   assert(ik->oop_is_instance(), "object must be instanceKlass");
 767   assert(ik->transitive_interfaces() == NULL, "just checking");
 768   ik->set_transitive_interfaces((objArrayOop) obj);   // Temporarily set transitive_interfaces to point to self
 769 }