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