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