1 /*
   2  * Copyright (c) 1997, 2020, 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 "aot/aotLoader.hpp"
  27 #include "classfile/classLoader.hpp"
  28 #include "classfile/classLoaderDataGraph.hpp"
  29 #include "classfile/javaClasses.hpp"
  30 #include "classfile/stringTable.hpp"
  31 #include "classfile/symbolTable.hpp"
  32 #include "classfile/systemDictionary.hpp"
  33 #include "classfile/vmSymbols.hpp"
  34 #include "code/codeBehaviours.hpp"
  35 #include "code/codeCache.hpp"
  36 #include "code/dependencies.hpp"
  37 #include "gc/shared/collectedHeap.inline.hpp"
  38 #include "gc/shared/gcArguments.hpp"
  39 #include "gc/shared/gcConfig.hpp"
  40 #include "gc/shared/gcTraceTime.inline.hpp"
  41 #include "interpreter/interpreter.hpp"
  42 #include "logging/log.hpp"
  43 #include "logging/logStream.hpp"
  44 #include "memory/heapShared.hpp"
  45 #include "memory/filemap.hpp"
  46 #include "memory/metadataFactory.hpp"
  47 #include "memory/metaspaceClosure.hpp"
  48 #include "memory/metaspaceCounters.hpp"
  49 #include "memory/metaspaceShared.hpp"
  50 #include "memory/oopFactory.hpp"
  51 #include "memory/resourceArea.hpp"
  52 #include "memory/universe.hpp"
  53 #include "oops/compressedOops.hpp"
  54 #include "oops/constantPool.hpp"
  55 #include "oops/instanceClassLoaderKlass.hpp"
  56 #include "oops/instanceKlass.hpp"
  57 #include "oops/instanceMirrorKlass.hpp"
  58 #include "oops/instanceRefKlass.hpp"
  59 #include "oops/objArrayOop.inline.hpp"
  60 #include "oops/oop.inline.hpp"
  61 #include "oops/typeArrayKlass.hpp"
  62 #include "prims/resolvedMethodTable.hpp"
  63 #include "runtime/arguments.hpp"
  64 #include "runtime/atomic.hpp"
  65 #include "runtime/deoptimization.hpp"
  66 #include "runtime/flags/flagSetting.hpp"
  67 #include "runtime/flags/jvmFlagConstraintList.hpp"
  68 #include "runtime/handles.inline.hpp"
  69 #include "runtime/init.hpp"
  70 #include "runtime/java.hpp"
  71 #include "runtime/javaCalls.hpp"
  72 #include "runtime/sharedRuntime.hpp"
  73 #include "runtime/synchronizer.hpp"
  74 #include "runtime/thread.inline.hpp"
  75 #include "runtime/timerTrace.hpp"
  76 #include "runtime/vmOperations.hpp"
  77 #include "services/memoryService.hpp"
  78 #include "utilities/align.hpp"
  79 #include "utilities/copy.hpp"
  80 #include "utilities/debug.hpp"
  81 #include "utilities/events.hpp"
  82 #include "utilities/formatBuffer.hpp"
  83 #include "utilities/hashtable.inline.hpp"
  84 #include "utilities/macros.hpp"
  85 #include "utilities/ostream.hpp"
  86 #include "utilities/preserveException.hpp"
  87 
  88 #define PRIMITIVE_MIRRORS_DO(func) \
  89   func(_int_mirror)    \
  90   func(_float_mirror)  \
  91   func(_double_mirror) \
  92   func(_byte_mirror)   \
  93   func(_bool_mirror)   \
  94   func(_char_mirror)   \
  95   func(_long_mirror)   \
  96   func(_short_mirror)  \
  97   func(_void_mirror)
  98 
  99 #define DEFINE_PRIMITIVE_MIRROR(m) \
 100     oop Universe::m  = NULL;
 101 
 102 // Known objects
 103 PRIMITIVE_MIRRORS_DO(DEFINE_PRIMITIVE_MIRROR)
 104 Klass* Universe::_typeArrayKlassObjs[T_LONG+1]        = { NULL /*, NULL...*/ };
 105 Klass* Universe::_objectArrayKlassObj                 = NULL;
 106 oop Universe::_mirrors[T_VOID+1]                      = { NULL /*, NULL...*/ };
 107 oop Universe::_main_thread_group                      = NULL;
 108 oop Universe::_system_thread_group                    = NULL;
 109 objArrayOop Universe::_the_empty_class_klass_array    = NULL;
 110 Array<Klass*>* Universe::_the_array_interfaces_array = NULL;
 111 oop Universe::_the_null_sentinel                      = NULL;
 112 oop Universe::_the_null_string                        = NULL;
 113 oop Universe::_the_min_jint_string                   = NULL;
 114 LatestMethodCache* Universe::_finalizer_register_cache = NULL;
 115 LatestMethodCache* Universe::_loader_addClass_cache    = NULL;
 116 LatestMethodCache* Universe::_throw_illegal_access_error_cache = NULL;
 117 LatestMethodCache* Universe::_throw_no_such_method_error_cache = NULL;
 118 LatestMethodCache* Universe::_do_stack_walk_cache     = NULL;
 119 oop Universe::_out_of_memory_error_java_heap          = NULL;
 120 oop Universe::_out_of_memory_error_metaspace          = NULL;
 121 oop Universe::_out_of_memory_error_class_metaspace    = NULL;
 122 oop Universe::_out_of_memory_error_array_size         = NULL;
 123 oop Universe::_out_of_memory_error_gc_overhead_limit  = NULL;
 124 oop Universe::_out_of_memory_error_realloc_objects    = NULL;
 125 oop Universe::_out_of_memory_error_retry              = NULL;
 126 oop Universe::_delayed_stack_overflow_error_message   = NULL;
 127 objArrayOop Universe::_preallocated_out_of_memory_error_array = NULL;
 128 volatile jint Universe::_preallocated_out_of_memory_error_avail_count = 0;
 129 bool Universe::_verify_in_progress                    = false;
 130 long Universe::verify_flags                           = Universe::Verify_All;
 131 oop Universe::_null_ptr_exception_instance            = NULL;
 132 oop Universe::_arithmetic_exception_instance          = NULL;
 133 oop Universe::_virtual_machine_error_instance         = NULL;
 134 oop Universe::_vm_exception                           = NULL;
 135 oop Universe::_reference_pending_list                 = NULL;
 136 
 137 Array<int>* Universe::_the_empty_int_array            = NULL;
 138 Array<u2>* Universe::_the_empty_short_array           = NULL;
 139 Array<Klass*>* Universe::_the_empty_klass_array     = NULL;
 140 Array<InstanceKlass*>* Universe::_the_empty_instance_klass_array  = NULL;
 141 Array<Method*>* Universe::_the_empty_method_array   = NULL;
 142 
 143 // These variables are guarded by FullGCALot_lock.
 144 debug_only(objArrayOop Universe::_fullgc_alot_dummy_array = NULL;)
 145 debug_only(int Universe::_fullgc_alot_dummy_next      = 0;)
 146 
 147 // Heap
 148 int             Universe::_verify_count = 0;
 149 
 150 // Oop verification (see MacroAssembler::verify_oop)
 151 uintptr_t       Universe::_verify_oop_mask = 0;
 152 uintptr_t       Universe::_verify_oop_bits = (uintptr_t) -1;
 153 
 154 int             Universe::_base_vtable_size = 0;
 155 bool            Universe::_bootstrapping = false;
 156 bool            Universe::_module_initialized = false;
 157 bool            Universe::_fully_initialized = false;
 158 
 159 size_t          Universe::_heap_capacity_at_last_gc;
 160 size_t          Universe::_heap_used_at_last_gc = 0;
 161 
 162 CollectedHeap*  Universe::_collectedHeap = NULL;
 163 
 164 void Universe::basic_type_classes_do(void f(Klass*)) {
 165   for (int i = T_BOOLEAN; i < T_LONG+1; i++) {
 166     f(_typeArrayKlassObjs[i]);
 167   }
 168 }
 169 
 170 void Universe::basic_type_classes_do(KlassClosure *closure) {
 171   for (int i = T_BOOLEAN; i < T_LONG+1; i++) {
 172     closure->do_klass(_typeArrayKlassObjs[i]);
 173   }
 174 }
 175 
 176 #define DO_PRIMITIVE_MIRROR(m) \
 177   f->do_oop((oop*) &m);
 178 
 179 void Universe::oops_do(OopClosure* f) {
 180   PRIMITIVE_MIRRORS_DO(DO_PRIMITIVE_MIRROR);
 181 
 182   for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
 183     f->do_oop((oop*) &_mirrors[i]);
 184   }
 185   assert(_mirrors[0] == NULL && _mirrors[T_BOOLEAN - 1] == NULL, "checking");
 186 
 187   f->do_oop((oop*)&_the_empty_class_klass_array);
 188   f->do_oop((oop*)&_the_null_sentinel);
 189   f->do_oop((oop*)&_the_null_string);
 190   f->do_oop((oop*)&_the_min_jint_string);
 191   f->do_oop((oop*)&_out_of_memory_error_java_heap);
 192   f->do_oop((oop*)&_out_of_memory_error_metaspace);
 193   f->do_oop((oop*)&_out_of_memory_error_class_metaspace);
 194   f->do_oop((oop*)&_out_of_memory_error_array_size);
 195   f->do_oop((oop*)&_out_of_memory_error_gc_overhead_limit);
 196   f->do_oop((oop*)&_out_of_memory_error_realloc_objects);
 197   f->do_oop((oop*)&_out_of_memory_error_retry);
 198   f->do_oop((oop*)&_delayed_stack_overflow_error_message);
 199   f->do_oop((oop*)&_preallocated_out_of_memory_error_array);
 200   f->do_oop((oop*)&_null_ptr_exception_instance);
 201   f->do_oop((oop*)&_arithmetic_exception_instance);
 202   f->do_oop((oop*)&_virtual_machine_error_instance);
 203   f->do_oop((oop*)&_main_thread_group);
 204   f->do_oop((oop*)&_system_thread_group);
 205   f->do_oop((oop*)&_vm_exception);
 206   f->do_oop((oop*)&_reference_pending_list);
 207   debug_only(f->do_oop((oop*)&_fullgc_alot_dummy_array);)
 208   ThreadsSMRSupport::exiting_threads_oops_do(f);
 209 }
 210 
 211 void LatestMethodCache::metaspace_pointers_do(MetaspaceClosure* it) {
 212   it->push(&_klass);
 213 }
 214 
 215 void Universe::metaspace_pointers_do(MetaspaceClosure* it) {
 216   for (int i = 0; i < T_LONG+1; i++) {
 217     it->push(&_typeArrayKlassObjs[i]);
 218   }
 219   it->push(&_objectArrayKlassObj);
 220 
 221   it->push(&_the_empty_int_array);
 222   it->push(&_the_empty_short_array);
 223   it->push(&_the_empty_klass_array);
 224   it->push(&_the_empty_instance_klass_array);
 225   it->push(&_the_empty_method_array);
 226   it->push(&_the_array_interfaces_array);
 227 
 228   _finalizer_register_cache->metaspace_pointers_do(it);
 229   _loader_addClass_cache->metaspace_pointers_do(it);
 230   _throw_illegal_access_error_cache->metaspace_pointers_do(it);
 231   _throw_no_such_method_error_cache->metaspace_pointers_do(it);
 232   _do_stack_walk_cache->metaspace_pointers_do(it);
 233 }
 234 
 235 #define ASSERT_MIRROR_NULL(m) \
 236   assert(m == NULL, "archived mirrors should be NULL");
 237 
 238 #define SERIALIZE_MIRROR(m) \
 239   f->do_oop(&m); \
 240   if (m != NULL) { java_lang_Class::update_archived_primitive_mirror_native_pointers(m); }
 241 
 242 // Serialize metadata and pointers to primitive type mirrors in and out of CDS archive
 243 void Universe::serialize(SerializeClosure* f) {
 244 
 245   for (int i = 0; i < T_LONG+1; i++) {
 246     f->do_ptr((void**)&_typeArrayKlassObjs[i]);
 247   }
 248 
 249   f->do_ptr((void**)&_objectArrayKlassObj);
 250 
 251 #if INCLUDE_CDS_JAVA_HEAP
 252   DEBUG_ONLY(if (DumpSharedSpaces && !HeapShared::is_heap_object_archiving_allowed()) {
 253       PRIMITIVE_MIRRORS_DO(ASSERT_MIRROR_NULL);
 254     });
 255   PRIMITIVE_MIRRORS_DO(SERIALIZE_MIRROR);
 256 #endif
 257 
 258   f->do_ptr((void**)&_the_array_interfaces_array);
 259   f->do_ptr((void**)&_the_empty_int_array);
 260   f->do_ptr((void**)&_the_empty_short_array);
 261   f->do_ptr((void**)&_the_empty_method_array);
 262   f->do_ptr((void**)&_the_empty_klass_array);
 263   f->do_ptr((void**)&_the_empty_instance_klass_array);
 264   _finalizer_register_cache->serialize(f);
 265   _loader_addClass_cache->serialize(f);
 266   _throw_illegal_access_error_cache->serialize(f);
 267   _throw_no_such_method_error_cache->serialize(f);
 268   _do_stack_walk_cache->serialize(f);
 269 }
 270 
 271 void Universe::check_alignment(uintx size, uintx alignment, const char* name) {
 272   if (size < alignment || size % alignment != 0) {
 273     vm_exit_during_initialization(
 274       err_msg("Size of %s (" UINTX_FORMAT " bytes) must be aligned to " UINTX_FORMAT " bytes", name, size, alignment));
 275   }
 276 }
 277 
 278 void initialize_basic_type_klass(Klass* k, TRAPS) {
 279   Klass* ok = SystemDictionary::Object_klass();
 280 #if INCLUDE_CDS
 281   if (UseSharedSpaces) {
 282     ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
 283     assert(k->super() == ok, "u3");
 284     if (k->is_instance_klass()) {
 285       InstanceKlass::cast(k)->restore_unshareable_info(loader_data, Handle(), NULL, CHECK);
 286     } else {
 287       ArrayKlass::cast(k)->restore_unshareable_info(loader_data, Handle(), CHECK);
 288     }
 289   } else
 290 #endif
 291   {
 292     k->initialize_supers(ok, NULL, CHECK);
 293   }
 294   k->append_to_sibling_list();
 295 }
 296 
 297 void Universe::genesis(TRAPS) {
 298   ResourceMark rm(THREAD);
 299 
 300   { FlagSetting fs(_bootstrapping, true);
 301 
 302     { MutexLocker mc(THREAD, Compile_lock);
 303 
 304       java_lang_Class::allocate_fixup_lists();
 305 
 306       // determine base vtable size; without that we cannot create the array klasses
 307       compute_base_vtable_size();
 308 
 309       if (!UseSharedSpaces) {
 310         for (int i = T_BOOLEAN; i < T_LONG+1; i++) {
 311           _typeArrayKlassObjs[i] = TypeArrayKlass::create_klass((BasicType)i, CHECK);
 312         }
 313 
 314         ClassLoaderData* null_cld = ClassLoaderData::the_null_class_loader_data();
 315 
 316         _the_array_interfaces_array     = MetadataFactory::new_array<Klass*>(null_cld, 2, NULL, CHECK);
 317         _the_empty_int_array            = MetadataFactory::new_array<int>(null_cld, 0, CHECK);
 318         _the_empty_short_array          = MetadataFactory::new_array<u2>(null_cld, 0, CHECK);
 319         _the_empty_method_array         = MetadataFactory::new_array<Method*>(null_cld, 0, CHECK);
 320         _the_empty_klass_array          = MetadataFactory::new_array<Klass*>(null_cld, 0, CHECK);
 321         _the_empty_instance_klass_array = MetadataFactory::new_array<InstanceKlass*>(null_cld, 0, CHECK);
 322       }
 323     }
 324 
 325     vmSymbols::initialize(CHECK);
 326 
 327     SystemDictionary::initialize(CHECK);
 328 
 329     Klass* ok = SystemDictionary::Object_klass();
 330 
 331     _the_null_string            = StringTable::intern("null", CHECK);
 332     _the_min_jint_string       = StringTable::intern("-2147483648", CHECK);
 333 
 334 #if INCLUDE_CDS
 335     if (UseSharedSpaces) {
 336       // Verify shared interfaces array.
 337       assert(_the_array_interfaces_array->at(0) ==
 338              SystemDictionary::Cloneable_klass(), "u3");
 339       assert(_the_array_interfaces_array->at(1) ==
 340              SystemDictionary::Serializable_klass(), "u3");
 341     } else
 342 #endif
 343     {
 344       // Set up shared interfaces array.  (Do this before supers are set up.)
 345       _the_array_interfaces_array->at_put(0, SystemDictionary::Cloneable_klass());
 346       _the_array_interfaces_array->at_put(1, SystemDictionary::Serializable_klass());
 347     }
 348 
 349     initialize_basic_type_klass(boolArrayKlassObj(), CHECK);
 350     initialize_basic_type_klass(charArrayKlassObj(), CHECK);
 351     initialize_basic_type_klass(floatArrayKlassObj(), CHECK);
 352     initialize_basic_type_klass(doubleArrayKlassObj(), CHECK);
 353     initialize_basic_type_klass(byteArrayKlassObj(), CHECK);
 354     initialize_basic_type_klass(shortArrayKlassObj(), CHECK);
 355     initialize_basic_type_klass(intArrayKlassObj(), CHECK);
 356     initialize_basic_type_klass(longArrayKlassObj(), CHECK);
 357   } // end of core bootstrapping
 358 
 359   {
 360     Handle tns = java_lang_String::create_from_str("<null_sentinel>", CHECK);
 361     _the_null_sentinel = tns();
 362   }
 363 
 364   // Maybe this could be lifted up now that object array can be initialized
 365   // during the bootstrapping.
 366 
 367   // OLD
 368   // Initialize _objectArrayKlass after core bootstraping to make
 369   // sure the super class is set up properly for _objectArrayKlass.
 370   // ---
 371   // NEW
 372   // Since some of the old system object arrays have been converted to
 373   // ordinary object arrays, _objectArrayKlass will be loaded when
 374   // SystemDictionary::initialize(CHECK); is run. See the extra check
 375   // for Object_klass_loaded in objArrayKlassKlass::allocate_objArray_klass_impl.
 376   _objectArrayKlassObj = InstanceKlass::
 377     cast(SystemDictionary::Object_klass())->array_klass(1, CHECK);
 378   // OLD
 379   // Add the class to the class hierarchy manually to make sure that
 380   // its vtable is initialized after core bootstrapping is completed.
 381   // ---
 382   // New
 383   // Have already been initialized.
 384   _objectArrayKlassObj->append_to_sibling_list();
 385 
 386   #ifdef ASSERT
 387   if (FullGCALot) {
 388     // Allocate an array of dummy objects.
 389     // We'd like these to be at the bottom of the old generation,
 390     // so that when we free one and then collect,
 391     // (almost) the whole heap moves
 392     // and we find out if we actually update all the oops correctly.
 393     // But we can't allocate directly in the old generation,
 394     // so we allocate wherever, and hope that the first collection
 395     // moves these objects to the bottom of the old generation.
 396     // We can allocate directly in the permanent generation, so we do.
 397     int size = FullGCALotDummies * 2;
 398 
 399     objArrayOop    naked_array = oopFactory::new_objArray(SystemDictionary::Object_klass(), size, CHECK);
 400     objArrayHandle dummy_array(THREAD, naked_array);
 401     int i = 0;
 402     while (i < size) {
 403         // Allocate dummy in old generation
 404       oop dummy = SystemDictionary::Object_klass()->allocate_instance(CHECK);
 405       dummy_array->obj_at_put(i++, dummy);
 406     }
 407     {
 408       // Only modify the global variable inside the mutex.
 409       // If we had a race to here, the other dummy_array instances
 410       // and their elements just get dropped on the floor, which is fine.
 411       MutexLocker ml(THREAD, FullGCALot_lock);
 412       if (_fullgc_alot_dummy_array == NULL) {
 413         _fullgc_alot_dummy_array = dummy_array();
 414       }
 415     }
 416     assert(i == _fullgc_alot_dummy_array->length(), "just checking");
 417   }
 418   #endif
 419 }
 420 
 421 #define ASSERT_MIRROR_NOT_NULL(m) \
 422   assert(m != NULL, "archived mirrors should not be NULL");
 423 
 424 void Universe::initialize_basic_type_mirrors(TRAPS) {
 425 #if INCLUDE_CDS_JAVA_HEAP
 426     if (UseSharedSpaces &&
 427         HeapShared::open_archive_heap_region_mapped() &&
 428         _int_mirror != NULL) {
 429       assert(HeapShared::is_heap_object_archiving_allowed(), "Sanity");
 430       PRIMITIVE_MIRRORS_DO(ASSERT_MIRROR_NOT_NULL);
 431     } else
 432       // _int_mirror could be NULL if archived heap is not mapped.
 433 #endif
 434     {
 435       _int_mirror     =
 436         java_lang_Class::create_basic_type_mirror("int",    T_INT, CHECK);
 437       _float_mirror   =
 438         java_lang_Class::create_basic_type_mirror("float",  T_FLOAT,   CHECK);
 439       _double_mirror  =
 440         java_lang_Class::create_basic_type_mirror("double", T_DOUBLE,  CHECK);
 441       _byte_mirror    =
 442         java_lang_Class::create_basic_type_mirror("byte",   T_BYTE, CHECK);
 443       _bool_mirror    =
 444         java_lang_Class::create_basic_type_mirror("boolean",T_BOOLEAN, CHECK);
 445       _char_mirror    =
 446         java_lang_Class::create_basic_type_mirror("char",   T_CHAR, CHECK);
 447       _long_mirror    =
 448         java_lang_Class::create_basic_type_mirror("long",   T_LONG, CHECK);
 449       _short_mirror   =
 450         java_lang_Class::create_basic_type_mirror("short",  T_SHORT,   CHECK);
 451       _void_mirror    =
 452         java_lang_Class::create_basic_type_mirror("void",   T_VOID, CHECK);
 453     }
 454 
 455     _mirrors[T_INT]     = _int_mirror;
 456     _mirrors[T_FLOAT]   = _float_mirror;
 457     _mirrors[T_DOUBLE]  = _double_mirror;
 458     _mirrors[T_BYTE]    = _byte_mirror;
 459     _mirrors[T_BOOLEAN] = _bool_mirror;
 460     _mirrors[T_CHAR]    = _char_mirror;
 461     _mirrors[T_LONG]    = _long_mirror;
 462     _mirrors[T_SHORT]   = _short_mirror;
 463     _mirrors[T_VOID]    = _void_mirror;
 464   //_mirrors[T_OBJECT]  = _object_klass->java_mirror();
 465   //_mirrors[T_ARRAY]   = _object_klass->java_mirror();
 466 }
 467 
 468 void Universe::fixup_mirrors(TRAPS) {
 469   // Bootstrap problem: all classes gets a mirror (java.lang.Class instance) assigned eagerly,
 470   // but we cannot do that for classes created before java.lang.Class is loaded. Here we simply
 471   // walk over permanent objects created so far (mostly classes) and fixup their mirrors. Note
 472   // that the number of objects allocated at this point is very small.
 473   assert(SystemDictionary::Class_klass_loaded(), "java.lang.Class should be loaded");
 474   HandleMark hm(THREAD);
 475 
 476   if (!UseSharedSpaces) {
 477     // Cache the start of the static fields
 478     InstanceMirrorKlass::init_offset_of_static_fields();
 479   }
 480 
 481   GrowableArray <Klass*>* list = java_lang_Class::fixup_mirror_list();
 482   int list_length = list->length();
 483   for (int i = 0; i < list_length; i++) {
 484     Klass* k = list->at(i);
 485     assert(k->is_klass(), "List should only hold classes");
 486     EXCEPTION_MARK;
 487     java_lang_Class::fixup_mirror(k, CATCH);
 488   }
 489   delete java_lang_Class::fixup_mirror_list();
 490   java_lang_Class::set_fixup_mirror_list(NULL);
 491 }
 492 
 493 #define assert_pll_locked(test) \
 494   assert(Heap_lock->test(), "Reference pending list access requires lock")
 495 
 496 #define assert_pll_ownership() assert_pll_locked(owned_by_self)
 497 
 498 oop Universe::reference_pending_list() {
 499   if (Thread::current()->is_VM_thread()) {
 500     assert_pll_locked(is_locked);
 501   } else {
 502     assert_pll_ownership();
 503   }
 504   return _reference_pending_list;
 505 }
 506 
 507 void Universe::set_reference_pending_list(oop list) {
 508   assert_pll_ownership();
 509   _reference_pending_list = list;
 510 }
 511 
 512 bool Universe::has_reference_pending_list() {
 513   assert_pll_ownership();
 514   return _reference_pending_list != NULL;
 515 }
 516 
 517 oop Universe::swap_reference_pending_list(oop list) {
 518   assert_pll_locked(is_locked);
 519   return Atomic::xchg(&_reference_pending_list, list);
 520 }
 521 
 522 #undef assert_pll_locked
 523 #undef assert_pll_ownership
 524 
 525 void Universe::reinitialize_vtable_of(Klass* ko, TRAPS) {
 526   // init vtable of k and all subclasses
 527   ko->vtable().initialize_vtable(false, CHECK);
 528   if (ko->is_instance_klass()) {
 529     for (Klass* sk = ko->subklass();
 530          sk != NULL;
 531          sk = sk->next_sibling()) {
 532       reinitialize_vtable_of(sk, CHECK);
 533     }
 534   }
 535 }
 536 
 537 void Universe::reinitialize_vtables(TRAPS) {
 538   // The vtables are initialized by starting at java.lang.Object and
 539   // initializing through the subclass links, so that the super
 540   // classes are always initialized first.
 541   Klass* ok = SystemDictionary::Object_klass();
 542   Universe::reinitialize_vtable_of(ok, THREAD);
 543 }
 544 
 545 
 546 void initialize_itable_for_klass(InstanceKlass* k, TRAPS) {
 547   k->itable().initialize_itable(false, CHECK);
 548 }
 549 
 550 
 551 void Universe::reinitialize_itables(TRAPS) {
 552   MutexLocker mcld(THREAD, ClassLoaderDataGraph_lock);
 553   ClassLoaderDataGraph::dictionary_classes_do(initialize_itable_for_klass, CHECK);
 554 }
 555 
 556 
 557 bool Universe::on_page_boundary(void* addr) {
 558   return is_aligned(addr, os::vm_page_size());
 559 }
 560 
 561 
 562 bool Universe::should_fill_in_stack_trace(Handle throwable) {
 563   // never attempt to fill in the stack trace of preallocated errors that do not have
 564   // backtrace. These errors are kept alive forever and may be "re-used" when all
 565   // preallocated errors with backtrace have been consumed. Also need to avoid
 566   // a potential loop which could happen if an out of memory occurs when attempting
 567   // to allocate the backtrace.
 568   return ((throwable() != Universe::_out_of_memory_error_java_heap) &&
 569           (throwable() != Universe::_out_of_memory_error_metaspace)  &&
 570           (throwable() != Universe::_out_of_memory_error_class_metaspace)  &&
 571           (throwable() != Universe::_out_of_memory_error_array_size) &&
 572           (throwable() != Universe::_out_of_memory_error_gc_overhead_limit) &&
 573           (throwable() != Universe::_out_of_memory_error_realloc_objects) &&
 574           (throwable() != Universe::_out_of_memory_error_retry));
 575 }
 576 
 577 
 578 oop Universe::gen_out_of_memory_error(oop default_err) {
 579   // generate an out of memory error:
 580   // - if there is a preallocated error and stack traces are available
 581   //   (j.l.Throwable is initialized), then return the preallocated
 582   //   error with a filled in stack trace, and with the message
 583   //   provided by the default error.
 584   // - otherwise, return the default error, without a stack trace.
 585   int next;
 586   if ((_preallocated_out_of_memory_error_avail_count > 0) &&
 587       SystemDictionary::Throwable_klass()->is_initialized()) {
 588     next = (int)Atomic::add(&_preallocated_out_of_memory_error_avail_count, -1);
 589     assert(next < (int)PreallocatedOutOfMemoryErrorCount, "avail count is corrupt");
 590   } else {
 591     next = -1;
 592   }
 593   if (next < 0) {
 594     // all preallocated errors have been used.
 595     // return default
 596     return default_err;
 597   } else {
 598     Thread* THREAD = Thread::current();
 599     Handle default_err_h(THREAD, default_err);
 600     // get the error object at the slot and set set it to NULL so that the
 601     // array isn't keeping it alive anymore.
 602     Handle exc(THREAD, preallocated_out_of_memory_errors()->obj_at(next));
 603     assert(exc() != NULL, "slot has been used already");
 604     preallocated_out_of_memory_errors()->obj_at_put(next, NULL);
 605 
 606     // use the message from the default error
 607     oop msg = java_lang_Throwable::message(default_err_h());
 608     assert(msg != NULL, "no message");
 609     java_lang_Throwable::set_message(exc(), msg);
 610 
 611     // populate the stack trace and return it.
 612     java_lang_Throwable::fill_in_stack_trace_of_preallocated_backtrace(exc);
 613     return exc();
 614   }
 615 }
 616 
 617 intptr_t Universe::_non_oop_bits = 0;
 618 
 619 void* Universe::non_oop_word() {
 620   // Neither the high bits nor the low bits of this value is allowed
 621   // to look like (respectively) the high or low bits of a real oop.
 622   //
 623   // High and low are CPU-specific notions, but low always includes
 624   // the low-order bit.  Since oops are always aligned at least mod 4,
 625   // setting the low-order bit will ensure that the low half of the
 626   // word will never look like that of a real oop.
 627   //
 628   // Using the OS-supplied non-memory-address word (usually 0 or -1)
 629   // will take care of the high bits, however many there are.
 630 
 631   if (_non_oop_bits == 0) {
 632     _non_oop_bits = (intptr_t)os::non_memory_address_word() | 1;
 633   }
 634 
 635   return (void*)_non_oop_bits;
 636 }
 637 
 638 static void initialize_global_behaviours() {
 639   CompiledICProtectionBehaviour::set_current(new DefaultICProtectionBehaviour());
 640 }
 641 
 642 jint universe_init() {
 643   assert(!Universe::_fully_initialized, "called after initialize_vtables");
 644   guarantee(1 << LogHeapWordSize == sizeof(HeapWord),
 645          "LogHeapWordSize is incorrect.");
 646   guarantee(sizeof(oop) >= sizeof(HeapWord), "HeapWord larger than oop?");
 647   guarantee(sizeof(oop) % sizeof(HeapWord) == 0,
 648             "oop size is not not a multiple of HeapWord size");
 649 
 650   TraceTime timer("Genesis", TRACETIME_LOG(Info, startuptime));
 651 
 652   JavaClasses::compute_hard_coded_offsets();
 653 
 654   initialize_global_behaviours();
 655 
 656   GCConfig::arguments()->initialize_heap_sizes();
 657 
 658   jint status = Universe::initialize_heap();
 659   if (status != JNI_OK) {
 660     return status;
 661   }
 662 
 663   Universe::initialize_tlab();
 664 
 665   Metaspace::global_initialize();
 666 
 667   // Initialize performance counters for metaspaces
 668   MetaspaceCounters::initialize_performance_counters();
 669   CompressedClassSpaceCounters::initialize_performance_counters();
 670 
 671   AOTLoader::universe_init();
 672 
 673   // Checks 'AfterMemoryInit' constraints.
 674   if (!JVMFlagConstraintList::check_constraints(JVMFlagConstraint::AfterMemoryInit)) {
 675     return JNI_EINVAL;
 676   }
 677 
 678   // Create memory for metadata.  Must be after initializing heap for
 679   // DumpSharedSpaces.
 680   ClassLoaderData::init_null_class_loader_data();
 681 
 682   // We have a heap so create the Method* caches before
 683   // Metaspace::initialize_shared_spaces() tries to populate them.
 684   Universe::_finalizer_register_cache = new LatestMethodCache();
 685   Universe::_loader_addClass_cache    = new LatestMethodCache();
 686   Universe::_throw_illegal_access_error_cache = new LatestMethodCache();
 687   Universe::_throw_no_such_method_error_cache = new LatestMethodCache();
 688   Universe::_do_stack_walk_cache = new LatestMethodCache();
 689 
 690 #if INCLUDE_CDS
 691   if (UseSharedSpaces) {
 692     // Read the data structures supporting the shared spaces (shared
 693     // system dictionary, symbol table, etc.).  After that, access to
 694     // the file (other than the mapped regions) is no longer needed, and
 695     // the file is closed. Closing the file does not affect the
 696     // currently mapped regions.
 697     MetaspaceShared::initialize_shared_spaces();
 698     StringTable::create_table();
 699   } else
 700 #endif
 701   {
 702     SymbolTable::create_table();
 703     StringTable::create_table();
 704   }
 705 
 706 #if INCLUDE_CDS
 707   if (Arguments::is_dumping_archive()) {
 708     MetaspaceShared::prepare_for_dumping();
 709   }
 710 #endif
 711 
 712   if (strlen(VerifySubSet) > 0) {
 713     Universe::initialize_verify_flags();
 714   }
 715 
 716   ResolvedMethodTable::create_table();
 717 
 718   return JNI_OK;
 719 }
 720 
 721 jint Universe::initialize_heap() {
 722   assert(_collectedHeap == NULL, "Heap already created");
 723   _collectedHeap = GCConfig::arguments()->create_heap();
 724   jint status = _collectedHeap->initialize();
 725 
 726   if (status == JNI_OK) {
 727     log_info(gc)("Using %s", _collectedHeap->name());
 728   }
 729 
 730   return status;
 731 }
 732 
 733 void Universe::initialize_tlab() {
 734   ThreadLocalAllocBuffer::set_max_size(Universe::heap()->max_tlab_size());
 735   if (UseTLAB) {
 736     assert(Universe::heap()->supports_tlab_allocation(),
 737            "Should support thread-local allocation buffers");
 738     ThreadLocalAllocBuffer::startup_initialization();
 739   }
 740 }
 741 
 742 ReservedHeapSpace Universe::reserve_heap(size_t heap_size, size_t alignment) {
 743 
 744   assert(alignment <= Arguments::conservative_max_heap_alignment(),
 745          "actual alignment " SIZE_FORMAT " must be within maximum heap alignment " SIZE_FORMAT,
 746          alignment, Arguments::conservative_max_heap_alignment());
 747 
 748   size_t total_reserved = align_up(heap_size, alignment);
 749   assert(!UseCompressedOops || (total_reserved <= (OopEncodingHeapMax - os::vm_page_size())),
 750       "heap size is too big for compressed oops");
 751 
 752   bool use_large_pages = UseLargePages && is_aligned(alignment, os::large_page_size());
 753   assert(!UseLargePages
 754       || UseParallelGC
 755       || use_large_pages, "Wrong alignment to use large pages");
 756 
 757   // Now create the space.
 758   ReservedHeapSpace total_rs(total_reserved, alignment, use_large_pages, AllocateHeapAt);
 759 
 760   if (total_rs.is_reserved()) {
 761     assert((total_reserved == total_rs.size()) && ((uintptr_t)total_rs.base() % alignment == 0),
 762            "must be exactly of required size and alignment");
 763     // We are good.
 764 
 765     if (AllocateHeapAt != NULL) {
 766       log_info(gc,heap)("Successfully allocated Java heap at location %s", AllocateHeapAt);
 767     }
 768 
 769     if (UseCompressedOops) {
 770       CompressedOops::initialize(total_rs);
 771     }
 772 
 773     Universe::calculate_verify_data((HeapWord*)total_rs.base(), (HeapWord*)total_rs.end());
 774 
 775     return total_rs;
 776   }
 777 
 778   vm_exit_during_initialization(
 779     err_msg("Could not reserve enough space for " SIZE_FORMAT "KB object heap",
 780             total_reserved/K));
 781 
 782   // satisfy compiler
 783   ShouldNotReachHere();
 784   return ReservedHeapSpace(0, 0, false);
 785 }
 786 
 787 
 788 // It's the caller's responsibility to ensure glitch-freedom
 789 // (if required).
 790 void Universe::update_heap_info_at_gc() {
 791   _heap_capacity_at_last_gc = heap()->capacity();
 792   _heap_used_at_last_gc     = heap()->used();
 793 }
 794 
 795 void initialize_known_method(LatestMethodCache* method_cache,
 796                              InstanceKlass* ik,
 797                              const char* method,
 798                              Symbol* signature,
 799                              bool is_static, TRAPS)
 800 {
 801   TempNewSymbol name = SymbolTable::new_symbol(method);
 802   Method* m = NULL;
 803   // The klass must be linked before looking up the method.
 804   if (!ik->link_class_or_fail(THREAD) ||
 805       ((m = ik->find_method(name, signature)) == NULL) ||
 806       is_static != m->is_static()) {
 807     ResourceMark rm(THREAD);
 808     // NoSuchMethodException doesn't actually work because it tries to run the
 809     // <init> function before java_lang_Class is linked. Print error and exit.
 810     vm_exit_during_initialization(err_msg("Unable to link/verify %s.%s method",
 811                                  ik->name()->as_C_string(), method));
 812   }
 813   method_cache->init(ik, m);
 814 }
 815 
 816 void Universe::initialize_known_methods(TRAPS) {
 817   // Set up static method for registering finalizers
 818   initialize_known_method(_finalizer_register_cache,
 819                           SystemDictionary::Finalizer_klass(),
 820                           "register",
 821                           vmSymbols::object_void_signature(), true, CHECK);
 822 
 823   initialize_known_method(_throw_illegal_access_error_cache,
 824                           SystemDictionary::internal_Unsafe_klass(),
 825                           "throwIllegalAccessError",
 826                           vmSymbols::void_method_signature(), true, CHECK);
 827 
 828   initialize_known_method(_throw_no_such_method_error_cache,
 829                           SystemDictionary::internal_Unsafe_klass(),
 830                           "throwNoSuchMethodError",
 831                           vmSymbols::void_method_signature(), true, CHECK);
 832 
 833   // Set up method for registering loaded classes in class loader vector
 834   initialize_known_method(_loader_addClass_cache,
 835                           SystemDictionary::ClassLoader_klass(),
 836                           "addClass",
 837                           vmSymbols::class_void_signature(), false, CHECK);
 838 
 839   // Set up method for stack walking
 840   initialize_known_method(_do_stack_walk_cache,
 841                           SystemDictionary::AbstractStackWalker_klass(),
 842                           "doStackWalk",
 843                           vmSymbols::doStackWalk_signature(), false, CHECK);
 844 }
 845 
 846 void universe2_init() {
 847   EXCEPTION_MARK;
 848   Universe::genesis(CATCH);
 849 }
 850 
 851 // Set after initialization of the module runtime, call_initModuleRuntime
 852 void universe_post_module_init() {
 853   Universe::_module_initialized = true;
 854 }
 855 
 856 bool universe_post_init() {
 857   assert(!is_init_completed(), "Error: initialization not yet completed!");
 858   Universe::_fully_initialized = true;
 859   EXCEPTION_MARK;
 860   if (!UseSharedSpaces) {
 861     ResourceMark rm;
 862     Universe::reinitialize_vtables(CHECK_false);
 863     Universe::reinitialize_itables(CHECK_false);
 864   }
 865 
 866   HandleMark hm(THREAD);
 867   // Setup preallocated empty java.lang.Class array
 868   Universe::_the_empty_class_klass_array = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_false);
 869 
 870   // Setup preallocated OutOfMemoryError errors
 871   Klass* k = SystemDictionary::OutOfMemoryError_klass();
 872   InstanceKlass* ik = InstanceKlass::cast(k);
 873   Universe::_out_of_memory_error_java_heap = ik->allocate_instance(CHECK_false);
 874   Universe::_out_of_memory_error_metaspace = ik->allocate_instance(CHECK_false);
 875   Universe::_out_of_memory_error_class_metaspace = ik->allocate_instance(CHECK_false);
 876   Universe::_out_of_memory_error_array_size = ik->allocate_instance(CHECK_false);
 877   Universe::_out_of_memory_error_gc_overhead_limit =
 878     ik->allocate_instance(CHECK_false);
 879   Universe::_out_of_memory_error_realloc_objects = ik->allocate_instance(CHECK_false);
 880   Universe::_out_of_memory_error_retry = ik->allocate_instance(CHECK_false);
 881 
 882   // Setup preallocated cause message for delayed StackOverflowError
 883   if (StackReservedPages > 0) {
 884     Universe::_delayed_stack_overflow_error_message =
 885       java_lang_String::create_oop_from_str("Delayed StackOverflowError due to ReservedStackAccess annotated method", CHECK_false);
 886   }
 887 
 888   // Setup preallocated NullPointerException
 889   // (this is currently used for a cheap & dirty solution in compiler exception handling)
 890   k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_NullPointerException(), true, CHECK_false);
 891   Universe::_null_ptr_exception_instance = InstanceKlass::cast(k)->allocate_instance(CHECK_false);
 892   // Setup preallocated ArithmeticException
 893   // (this is currently used for a cheap & dirty solution in compiler exception handling)
 894   k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ArithmeticException(), true, CHECK_false);
 895   Universe::_arithmetic_exception_instance = InstanceKlass::cast(k)->allocate_instance(CHECK_false);
 896   // Virtual Machine Error for when we get into a situation we can't resolve
 897   k = SystemDictionary::VirtualMachineError_klass();
 898   bool linked = InstanceKlass::cast(k)->link_class_or_fail(CHECK_false);
 899   if (!linked) {
 900      tty->print_cr("Unable to link/verify VirtualMachineError class");
 901      return false; // initialization failed
 902   }
 903   Universe::_virtual_machine_error_instance =
 904     InstanceKlass::cast(k)->allocate_instance(CHECK_false);
 905 
 906   Universe::_vm_exception = InstanceKlass::cast(k)->allocate_instance(CHECK_false);
 907 
 908   Handle msg = java_lang_String::create_from_str("Java heap space", CHECK_false);
 909   java_lang_Throwable::set_message(Universe::_out_of_memory_error_java_heap, msg());
 910 
 911   msg = java_lang_String::create_from_str("Metaspace", CHECK_false);
 912   java_lang_Throwable::set_message(Universe::_out_of_memory_error_metaspace, msg());
 913   msg = java_lang_String::create_from_str("Compressed class space", CHECK_false);
 914   java_lang_Throwable::set_message(Universe::_out_of_memory_error_class_metaspace, msg());
 915 
 916   msg = java_lang_String::create_from_str("Requested array size exceeds VM limit", CHECK_false);
 917   java_lang_Throwable::set_message(Universe::_out_of_memory_error_array_size, msg());
 918 
 919   msg = java_lang_String::create_from_str("GC overhead limit exceeded", CHECK_false);
 920   java_lang_Throwable::set_message(Universe::_out_of_memory_error_gc_overhead_limit, msg());
 921 
 922   msg = java_lang_String::create_from_str("Java heap space: failed reallocation of scalar replaced objects", CHECK_false);
 923   java_lang_Throwable::set_message(Universe::_out_of_memory_error_realloc_objects, msg());
 924 
 925   msg = java_lang_String::create_from_str("Java heap space: failed retryable allocation", CHECK_false);
 926   java_lang_Throwable::set_message(Universe::_out_of_memory_error_retry, msg());
 927 
 928   msg = java_lang_String::create_from_str("/ by zero", CHECK_false);
 929   java_lang_Throwable::set_message(Universe::_arithmetic_exception_instance, msg());
 930 
 931   // Setup the array of errors that have preallocated backtrace
 932   k = Universe::_out_of_memory_error_java_heap->klass();
 933   assert(k->name() == vmSymbols::java_lang_OutOfMemoryError(), "should be out of memory error");
 934   ik = InstanceKlass::cast(k);
 935 
 936   int len = (StackTraceInThrowable) ? (int)PreallocatedOutOfMemoryErrorCount : 0;
 937   Universe::_preallocated_out_of_memory_error_array = oopFactory::new_objArray(ik, len, CHECK_false);
 938   for (int i=0; i<len; i++) {
 939     oop err = ik->allocate_instance(CHECK_false);
 940     Handle err_h = Handle(THREAD, err);
 941     java_lang_Throwable::allocate_backtrace(err_h, CHECK_false);
 942     Universe::preallocated_out_of_memory_errors()->obj_at_put(i, err_h());
 943   }
 944   Universe::_preallocated_out_of_memory_error_avail_count = (jint)len;
 945 
 946   Universe::initialize_known_methods(CHECK_false);
 947 
 948   // This needs to be done before the first scavenge/gc, since
 949   // it's an input to soft ref clearing policy.
 950   {
 951     MutexLocker x(THREAD, Heap_lock);
 952     Universe::update_heap_info_at_gc();
 953   }
 954 
 955   // ("weak") refs processing infrastructure initialization
 956   Universe::heap()->post_initialize();
 957 
 958   MemoryService::add_metaspace_memory_pools();
 959 
 960   MemoryService::set_universe_heap(Universe::heap());
 961 #if INCLUDE_CDS
 962   MetaspaceShared::post_initialize(CHECK_false);
 963 #endif
 964   return true;
 965 }
 966 
 967 
 968 void Universe::compute_base_vtable_size() {
 969   _base_vtable_size = ClassLoader::compute_Object_vtable();
 970 }
 971 
 972 void Universe::print_on(outputStream* st) {
 973   GCMutexLocker hl(Heap_lock); // Heap_lock might be locked by caller thread.
 974   st->print_cr("Heap");
 975   heap()->print_on(st);
 976 }
 977 
 978 void Universe::print_heap_at_SIGBREAK() {
 979   if (PrintHeapAtSIGBREAK) {
 980     print_on(tty);
 981     tty->cr();
 982     tty->flush();
 983   }
 984 }
 985 
 986 void Universe::print_heap_before_gc() {
 987   LogTarget(Debug, gc, heap) lt;
 988   if (lt.is_enabled()) {
 989     LogStream ls(lt);
 990     ls.print("Heap before GC invocations=%u (full %u):", heap()->total_collections(), heap()->total_full_collections());
 991     ResourceMark rm;
 992     heap()->print_on(&ls);
 993   }
 994 }
 995 
 996 void Universe::print_heap_after_gc() {
 997   LogTarget(Debug, gc, heap) lt;
 998   if (lt.is_enabled()) {
 999     LogStream ls(lt);
1000     ls.print("Heap after GC invocations=%u (full %u):", heap()->total_collections(), heap()->total_full_collections());
1001     ResourceMark rm;
1002     heap()->print_on(&ls);
1003   }
1004 }
1005 
1006 void Universe::initialize_verify_flags() {
1007   verify_flags = 0;
1008   const char delimiter[] = " ,";
1009 
1010   size_t length = strlen(VerifySubSet);
1011   char* subset_list = NEW_C_HEAP_ARRAY(char, length + 1, mtInternal);
1012   strncpy(subset_list, VerifySubSet, length + 1);
1013   char* save_ptr;
1014 
1015   char* token = strtok_r(subset_list, delimiter, &save_ptr);
1016   while (token != NULL) {
1017     if (strcmp(token, "threads") == 0) {
1018       verify_flags |= Verify_Threads;
1019     } else if (strcmp(token, "heap") == 0) {
1020       verify_flags |= Verify_Heap;
1021     } else if (strcmp(token, "symbol_table") == 0) {
1022       verify_flags |= Verify_SymbolTable;
1023     } else if (strcmp(token, "string_table") == 0) {
1024       verify_flags |= Verify_StringTable;
1025     } else if (strcmp(token, "codecache") == 0) {
1026       verify_flags |= Verify_CodeCache;
1027     } else if (strcmp(token, "dictionary") == 0) {
1028       verify_flags |= Verify_SystemDictionary;
1029     } else if (strcmp(token, "classloader_data_graph") == 0) {
1030       verify_flags |= Verify_ClassLoaderDataGraph;
1031     } else if (strcmp(token, "metaspace") == 0) {
1032       verify_flags |= Verify_MetaspaceUtils;
1033     } else if (strcmp(token, "jni_handles") == 0) {
1034       verify_flags |= Verify_JNIHandles;
1035     } else if (strcmp(token, "codecache_oops") == 0) {
1036       verify_flags |= Verify_CodeCacheOops;
1037     } else if (strcmp(token, "resolved_method_table") == 0) {
1038       verify_flags |= Verify_ResolvedMethodTable;
1039     } else {
1040       vm_exit_during_initialization(err_msg("VerifySubSet: \'%s\' memory sub-system is unknown, please correct it", token));
1041     }
1042     token = strtok_r(NULL, delimiter, &save_ptr);
1043   }
1044   FREE_C_HEAP_ARRAY(char, subset_list);
1045 }
1046 
1047 bool Universe::should_verify_subset(uint subset) {
1048   if (verify_flags & subset) {
1049     return true;
1050   }
1051   return false;
1052 }
1053 
1054 void Universe::verify(VerifyOption option, const char* prefix) {
1055   // The use of _verify_in_progress is a temporary work around for
1056   // 6320749.  Don't bother with a creating a class to set and clear
1057   // it since it is only used in this method and the control flow is
1058   // straight forward.
1059   _verify_in_progress = true;
1060 
1061   COMPILER2_PRESENT(
1062     assert(!DerivedPointerTable::is_active(),
1063          "DPT should not be active during verification "
1064          "(of thread stacks below)");
1065   )
1066 
1067   ResourceMark rm;
1068   HandleMark hm;  // Handles created during verification can be zapped
1069   _verify_count++;
1070 
1071   FormatBuffer<> title("Verifying %s", prefix);
1072   GCTraceTime(Info, gc, verify) tm(title.buffer());
1073   if (should_verify_subset(Verify_Threads)) {
1074     log_debug(gc, verify)("Threads");
1075     Threads::verify();
1076   }
1077   if (should_verify_subset(Verify_Heap)) {
1078     log_debug(gc, verify)("Heap");
1079     heap()->verify(option);
1080   }
1081   if (should_verify_subset(Verify_SymbolTable)) {
1082     log_debug(gc, verify)("SymbolTable");
1083     SymbolTable::verify();
1084   }
1085   if (should_verify_subset(Verify_StringTable)) {
1086     log_debug(gc, verify)("StringTable");
1087     StringTable::verify();
1088   }
1089   if (should_verify_subset(Verify_CodeCache)) {
1090   {
1091     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1092     log_debug(gc, verify)("CodeCache");
1093     CodeCache::verify();
1094   }
1095   }
1096   if (should_verify_subset(Verify_SystemDictionary)) {
1097     log_debug(gc, verify)("SystemDictionary");
1098     SystemDictionary::verify();
1099   }
1100 #ifndef PRODUCT
1101   if (should_verify_subset(Verify_ClassLoaderDataGraph)) {
1102     log_debug(gc, verify)("ClassLoaderDataGraph");
1103     ClassLoaderDataGraph::verify();
1104   }
1105 #endif
1106   if (should_verify_subset(Verify_MetaspaceUtils)) {
1107     log_debug(gc, verify)("MetaspaceUtils");
1108     MetaspaceUtils::verify_free_chunks();
1109   }
1110   if (should_verify_subset(Verify_JNIHandles)) {
1111     log_debug(gc, verify)("JNIHandles");
1112     JNIHandles::verify();
1113   }
1114   if (should_verify_subset(Verify_CodeCacheOops)) {
1115     log_debug(gc, verify)("CodeCache Oops");
1116     CodeCache::verify_oops();
1117   }
1118   if (should_verify_subset(Verify_ResolvedMethodTable)) {
1119     log_debug(gc, verify)("ResolvedMethodTable Oops");
1120     ResolvedMethodTable::verify();
1121   }
1122 
1123   _verify_in_progress = false;
1124 }
1125 
1126 
1127 #ifndef PRODUCT
1128 void Universe::calculate_verify_data(HeapWord* low_boundary, HeapWord* high_boundary) {
1129   assert(low_boundary < high_boundary, "bad interval");
1130 
1131   // decide which low-order bits we require to be clear:
1132   size_t alignSize = MinObjAlignmentInBytes;
1133   size_t min_object_size = CollectedHeap::min_fill_size();
1134 
1135   // make an inclusive limit:
1136   uintptr_t max = (uintptr_t)high_boundary - min_object_size*wordSize;
1137   uintptr_t min = (uintptr_t)low_boundary;
1138   assert(min < max, "bad interval");
1139   uintptr_t diff = max ^ min;
1140 
1141   // throw away enough low-order bits to make the diff vanish
1142   uintptr_t mask = (uintptr_t)(-1);
1143   while ((mask & diff) != 0)
1144     mask <<= 1;
1145   uintptr_t bits = (min & mask);
1146   assert(bits == (max & mask), "correct mask");
1147   // check an intermediate value between min and max, just to make sure:
1148   assert(bits == ((min + (max-min)/2) & mask), "correct mask");
1149 
1150   // require address alignment, too:
1151   mask |= (alignSize - 1);
1152 
1153   if (!(_verify_oop_mask == 0 && _verify_oop_bits == (uintptr_t)-1)) {
1154     assert(_verify_oop_mask == mask && _verify_oop_bits == bits, "mask stability");
1155   }
1156   _verify_oop_mask = mask;
1157   _verify_oop_bits = bits;
1158 }
1159 
1160 // Oop verification (see MacroAssembler::verify_oop)
1161 
1162 uintptr_t Universe::verify_oop_mask() {
1163   return _verify_oop_mask;
1164 }
1165 
1166 uintptr_t Universe::verify_oop_bits() {
1167   return _verify_oop_bits;
1168 }
1169 
1170 uintptr_t Universe::verify_mark_mask() {
1171   return markWord::lock_mask_in_place;
1172 }
1173 
1174 uintptr_t Universe::verify_mark_bits() {
1175   intptr_t mask = verify_mark_mask();
1176   intptr_t bits = (intptr_t)markWord::prototype().value();
1177   assert((bits & ~mask) == 0, "no stray header bits");
1178   return bits;
1179 }
1180 #endif // PRODUCT
1181 
1182 
1183 void LatestMethodCache::init(Klass* k, Method* m) {
1184   if (!UseSharedSpaces) {
1185     _klass = k;
1186   }
1187 #ifndef PRODUCT
1188   else {
1189     // sharing initilization should have already set up _klass
1190     assert(_klass != NULL, "just checking");
1191   }
1192 #endif
1193 
1194   _method_idnum = m->method_idnum();
1195   assert(_method_idnum >= 0, "sanity check");
1196 }
1197 
1198 
1199 Method* LatestMethodCache::get_method() {
1200   if (klass() == NULL) return NULL;
1201   InstanceKlass* ik = InstanceKlass::cast(klass());
1202   Method* m = ik->method_with_idnum(method_idnum());
1203   assert(m != NULL, "sanity check");
1204   return m;
1205 }
1206 
1207 
1208 #ifdef ASSERT
1209 // Release dummy object(s) at bottom of heap
1210 bool Universe::release_fullgc_alot_dummy() {
1211   MutexLocker ml(FullGCALot_lock);
1212   if (_fullgc_alot_dummy_array != NULL) {
1213     if (_fullgc_alot_dummy_next >= _fullgc_alot_dummy_array->length()) {
1214       // No more dummies to release, release entire array instead
1215       _fullgc_alot_dummy_array = NULL;
1216       return false;
1217     }
1218 
1219     // Release dummy at bottom of old generation
1220     _fullgc_alot_dummy_array->obj_at_put(_fullgc_alot_dummy_next++, NULL);
1221 
1222     // Release dummy at bottom of permanent generation
1223     _fullgc_alot_dummy_array->obj_at_put(_fullgc_alot_dummy_next++, NULL);
1224   }
1225   return true;
1226 }
1227 
1228 #endif // ASSERT