1 /* 2 * Copyright (c) 1997, 2024, 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 "cds/archiveHeapLoader.hpp" 27 #include "cds/cdsConfig.hpp" 28 #include "cds/dynamicArchive.hpp" 29 #include "cds/heapShared.hpp" 30 #include "cds/metaspaceShared.hpp" 31 #include "classfile/classLoader.hpp" 32 #include "classfile/classLoaderDataGraph.hpp" 33 #include "classfile/javaClasses.hpp" 34 #include "classfile/stringTable.hpp" 35 #include "classfile/symbolTable.hpp" 36 #include "classfile/systemDictionary.hpp" 37 #include "classfile/vmClasses.hpp" 38 #include "classfile/vmSymbols.hpp" 39 #include "code/codeBehaviours.hpp" 40 #include "code/codeCache.hpp" 41 #include "compiler/oopMap.hpp" 42 #include "gc/shared/collectedHeap.inline.hpp" 43 #include "gc/shared/gcArguments.hpp" 44 #include "gc/shared/gcConfig.hpp" 45 #include "gc/shared/gcLogPrecious.hpp" 46 #include "gc/shared/gcTraceTime.inline.hpp" 47 #include "gc/shared/oopStorageSet.hpp" 48 #include "gc/shared/plab.hpp" 49 #include "gc/shared/stringdedup/stringDedup.hpp" 50 #include "gc/shared/tlab_globals.hpp" 51 #include "logging/log.hpp" 52 #include "logging/logStream.hpp" 53 #include "memory/metadataFactory.hpp" 54 #include "memory/metaspaceClosure.hpp" 55 #include "memory/metaspaceCounters.hpp" 56 #include "memory/metaspaceUtils.hpp" 57 #include "memory/oopFactory.hpp" 58 #include "memory/resourceArea.hpp" 59 #include "memory/universe.hpp" 60 #include "oops/compressedOops.hpp" 61 #include "oops/instanceKlass.hpp" 62 #include "oops/instanceMirrorKlass.hpp" 63 #include "oops/klass.inline.hpp" 64 #include "oops/objArrayOop.inline.hpp" 65 #include "oops/oop.inline.hpp" 66 #include "oops/oopHandle.inline.hpp" 67 #include "oops/trainingData.hpp" 68 #include "oops/typeArrayKlass.hpp" 69 #include "prims/resolvedMethodTable.hpp" 70 #include "runtime/arguments.hpp" 71 #include "runtime/atomic.hpp" 72 #include "runtime/cpuTimeCounters.hpp" 73 #include "runtime/flags/jvmFlagLimit.hpp" 74 #include "runtime/handles.inline.hpp" 75 #include "runtime/init.hpp" 76 #include "runtime/java.hpp" 77 #include "runtime/javaThread.hpp" 78 #include "runtime/jniHandles.hpp" 79 #include "runtime/threads.hpp" 80 #include "runtime/timerTrace.hpp" 81 #include "sanitizers/leak.hpp" 82 #include "services/memoryService.hpp" 83 #include "utilities/align.hpp" 84 #include "utilities/autoRestore.hpp" 85 #include "utilities/debug.hpp" 86 #include "utilities/formatBuffer.hpp" 87 #include "utilities/macros.hpp" 88 #include "utilities/ostream.hpp" 89 #include "utilities/preserveException.hpp" 90 91 // A helper class for caching a Method* when the user of the cache 92 // only cares about the latest version of the Method*. This cache safely 93 // interacts with the RedefineClasses API. 94 class LatestMethodCache { 95 // We save the InstanceKlass* and the idnum of Method* in order to get 96 // the current Method*. 97 InstanceKlass* _klass; 98 int _method_idnum; 99 100 public: 101 LatestMethodCache() { _klass = nullptr; _method_idnum = -1; } 102 103 void init(JavaThread* current, InstanceKlass* ik, const char* method, 104 Symbol* signature, bool is_static); 105 Method* get_method(); 106 }; 107 108 static LatestMethodCache _finalizer_register_cache; // Finalizer.register() 109 static LatestMethodCache _loader_addClass_cache; // ClassLoader.addClass() 110 static LatestMethodCache _throw_illegal_access_error_cache; // Unsafe.throwIllegalAccessError() 111 static LatestMethodCache _throw_no_such_method_error_cache; // Unsafe.throwNoSuchMethodError() 112 static LatestMethodCache _do_stack_walk_cache; // AbstractStackWalker.doStackWalk() 113 114 // Known objects 115 TypeArrayKlass* Universe::_typeArrayKlasses[T_LONG+1] = { nullptr /*, nullptr...*/ }; 116 ObjArrayKlass* Universe::_objectArrayKlass = nullptr; 117 Klass* Universe::_fillerArrayKlass = nullptr; 118 OopHandle Universe::_basic_type_mirrors[T_VOID+1]; 119 #if INCLUDE_CDS_JAVA_HEAP 120 int Universe::_archived_basic_type_mirror_indices[T_VOID+1]; 121 #endif 122 123 OopHandle Universe::_main_thread_group; 124 OopHandle Universe::_system_thread_group; 125 OopHandle Universe::_the_empty_class_array; 126 OopHandle Universe::_the_null_string; 127 OopHandle Universe::_the_min_jint_string; 128 129 OopHandle Universe::_the_null_sentinel; 130 131 // _out_of_memory_errors is an objArray 132 enum OutOfMemoryInstance { _oom_java_heap, 133 _oom_c_heap, 134 _oom_metaspace, 135 _oom_class_metaspace, 136 _oom_array_size, 137 _oom_gc_overhead_limit, 138 _oom_realloc_objects, 139 _oom_count }; 140 141 OopHandle Universe::_out_of_memory_errors; 142 OopHandle Universe:: _class_init_stack_overflow_error; 143 OopHandle Universe::_delayed_stack_overflow_error_message; 144 OopHandle Universe::_preallocated_out_of_memory_error_array; 145 volatile jint Universe::_preallocated_out_of_memory_error_avail_count = 0; 146 147 // Message details for OOME objects, preallocate these objects since they could be 148 // used when throwing OOME, we should try to avoid further allocation in such case 149 OopHandle Universe::_msg_metaspace; 150 OopHandle Universe::_msg_class_metaspace; 151 152 OopHandle Universe::_reference_pending_list; 153 154 Array<Klass*>* Universe::_the_array_interfaces_array = nullptr; 155 156 long Universe::verify_flags = Universe::Verify_All; 157 158 Array<int>* Universe::_the_empty_int_array = nullptr; 159 Array<u2>* Universe::_the_empty_short_array = nullptr; 160 Array<Klass*>* Universe::_the_empty_klass_array = nullptr; 161 Array<InstanceKlass*>* Universe::_the_empty_instance_klass_array = nullptr; 162 Array<Method*>* Universe::_the_empty_method_array = nullptr; 163 164 uintx Universe::_the_array_interfaces_bitmap = 0; 165 uintx Universe::_the_empty_klass_bitmap = 0; 166 167 // These variables are guarded by FullGCALot_lock. 168 debug_only(OopHandle Universe::_fullgc_alot_dummy_array;) 169 debug_only(int Universe::_fullgc_alot_dummy_next = 0;) 170 171 // Heap 172 int Universe::_verify_count = 0; 173 174 // Oop verification (see MacroAssembler::verify_oop) 175 uintptr_t Universe::_verify_oop_mask = 0; 176 uintptr_t Universe::_verify_oop_bits = (uintptr_t) -1; 177 178 int Universe::_base_vtable_size = 0; 179 bool Universe::_bootstrapping = false; 180 bool Universe::_module_initialized = false; 181 bool Universe::_fully_initialized = false; 182 183 OopStorage* Universe::_vm_weak = nullptr; 184 OopStorage* Universe::_vm_global = nullptr; 185 186 CollectedHeap* Universe::_collectedHeap = nullptr; 187 188 // These are the exceptions that are always created and are guatanteed to exist. 189 // If possible, they can be stored as CDS archived objects to speed up AOT code. 190 class BuiltinException { 191 OopHandle _instance; 192 CDS_JAVA_HEAP_ONLY(int _archived_root_index;) 193 194 public: 195 BuiltinException() : _instance() { 196 CDS_JAVA_HEAP_ONLY(_archived_root_index = 0); 197 } 198 199 void init_if_empty(Symbol* symbol, TRAPS) { 200 if (_instance.is_empty()) { 201 Klass* k = SystemDictionary::resolve_or_fail(symbol, true, CHECK); 202 oop obj = InstanceKlass::cast(k)->allocate_instance(CHECK); 203 _instance = OopHandle(Universe::vm_global(), obj); 204 } 205 } 206 207 oop instance() { 208 return _instance.resolve(); 209 } 210 211 #if INCLUDE_CDS_JAVA_HEAP 212 void store_in_cds() { 213 _archived_root_index = HeapShared::archive_exception_instance(instance()); 214 } 215 216 void load_from_cds() { 217 if (_archived_root_index >= 0) { 218 oop obj = HeapShared::get_root(_archived_root_index); 219 assert(obj != nullptr, "must be"); 220 _instance = OopHandle(Universe::vm_global(), obj); 221 } 222 } 223 224 void serialize(SerializeClosure *f) { 225 f->do_int(&_archived_root_index); 226 } 227 #endif 228 }; 229 230 static BuiltinException _null_ptr_exception; 231 static BuiltinException _arithmetic_exception; 232 static BuiltinException _internal_error; 233 static BuiltinException _array_index_oob_exception; 234 static BuiltinException _array_store_exception; 235 static BuiltinException _class_cast_exception; 236 237 objArrayOop Universe::the_empty_class_array () { 238 return (objArrayOop)_the_empty_class_array.resolve(); 239 } 240 241 oop Universe::main_thread_group() { return _main_thread_group.resolve(); } 242 void Universe::set_main_thread_group(oop group) { _main_thread_group = OopHandle(vm_global(), group); } 243 244 oop Universe::system_thread_group() { return _system_thread_group.resolve(); } 245 void Universe::set_system_thread_group(oop group) { _system_thread_group = OopHandle(vm_global(), group); } 246 247 oop Universe::the_null_string() { return _the_null_string.resolve(); } 248 oop Universe::the_min_jint_string() { return _the_min_jint_string.resolve(); } 249 250 oop Universe::null_ptr_exception_instance() { return _null_ptr_exception.instance(); } 251 oop Universe::arithmetic_exception_instance() { return _arithmetic_exception.instance(); } 252 oop Universe::internal_error_instance() { return _internal_error.instance(); } 253 254 oop Universe::array_index_oob_exception_instance() { return _array_index_oob_exception.instance(); } 255 oop Universe::array_store_exception_instance() { return _array_store_exception.instance(); } 256 oop Universe::class_cast_exception_instance() { return _class_cast_exception.instance(); } 257 258 oop Universe::the_null_sentinel() { return _the_null_sentinel.resolve(); } 259 260 oop Universe::int_mirror() { return check_mirror(_basic_type_mirrors[T_INT].resolve()); } 261 oop Universe::float_mirror() { return check_mirror(_basic_type_mirrors[T_FLOAT].resolve()); } 262 oop Universe::double_mirror() { return check_mirror(_basic_type_mirrors[T_DOUBLE].resolve()); } 263 oop Universe::byte_mirror() { return check_mirror(_basic_type_mirrors[T_BYTE].resolve()); } 264 oop Universe::bool_mirror() { return check_mirror(_basic_type_mirrors[T_BOOLEAN].resolve()); } 265 oop Universe::char_mirror() { return check_mirror(_basic_type_mirrors[T_CHAR].resolve()); } 266 oop Universe::long_mirror() { return check_mirror(_basic_type_mirrors[T_LONG].resolve()); } 267 oop Universe::short_mirror() { return check_mirror(_basic_type_mirrors[T_SHORT].resolve()); } 268 oop Universe::void_mirror() { return check_mirror(_basic_type_mirrors[T_VOID].resolve()); } 269 270 oop Universe::java_mirror(BasicType t) { 271 assert((uint)t < T_VOID+1, "range check"); 272 assert(!is_reference_type(t), "sanity"); 273 return check_mirror(_basic_type_mirrors[t].resolve()); 274 } 275 276 void Universe::basic_type_classes_do(KlassClosure *closure) { 277 for (int i = T_BOOLEAN; i < T_LONG+1; i++) { 278 closure->do_klass(_typeArrayKlasses[i]); 279 } 280 // We don't do the following because it will confuse JVMTI. 281 // _fillerArrayKlass is used only by GC, which doesn't need to see 282 // this klass from basic_type_classes_do(). 283 // 284 // closure->do_klass(_fillerArrayKlass); 285 } 286 287 void Universe::metaspace_pointers_do(MetaspaceClosure* it) { 288 it->push(&_fillerArrayKlass); 289 for (int i = 0; i < T_LONG+1; i++) { 290 it->push(&_typeArrayKlasses[i]); 291 } 292 it->push(&_objectArrayKlass); 293 294 it->push(&_the_empty_int_array); 295 it->push(&_the_empty_short_array); 296 it->push(&_the_empty_klass_array); 297 it->push(&_the_empty_instance_klass_array); 298 it->push(&_the_empty_method_array); 299 it->push(&_the_array_interfaces_array); 300 } 301 302 #if INCLUDE_CDS_JAVA_HEAP 303 void Universe::set_archived_basic_type_mirror_index(BasicType t, int index) { 304 assert(CDSConfig::is_dumping_heap(), "sanity"); 305 assert(!is_reference_type(t), "sanity"); 306 _archived_basic_type_mirror_indices[t] = index; 307 } 308 309 void Universe::archive_exception_instances() { 310 _null_ptr_exception.store_in_cds(); 311 _arithmetic_exception.store_in_cds(); 312 _internal_error.store_in_cds(); 313 _array_index_oob_exception.store_in_cds(); 314 _array_store_exception.store_in_cds(); 315 _class_cast_exception.store_in_cds(); 316 } 317 318 void Universe::load_archived_object_instances() { 319 if (ArchiveHeapLoader::is_in_use()) { 320 for (int i = T_BOOLEAN; i < T_VOID+1; i++) { 321 int index = _archived_basic_type_mirror_indices[i]; 322 if (!is_reference_type((BasicType)i) && index >= 0) { 323 oop mirror_oop = HeapShared::get_root(index); 324 assert(mirror_oop != nullptr, "must be"); 325 _basic_type_mirrors[i] = OopHandle(vm_global(), mirror_oop); 326 } 327 } 328 329 _null_ptr_exception.load_from_cds(); 330 _arithmetic_exception.load_from_cds(); 331 _internal_error.load_from_cds(); 332 _array_index_oob_exception.load_from_cds(); 333 _array_store_exception.load_from_cds(); 334 _class_cast_exception.load_from_cds(); 335 } 336 } 337 #endif 338 339 void Universe::serialize(SerializeClosure* f) { 340 341 #if INCLUDE_CDS_JAVA_HEAP 342 for (int i = T_BOOLEAN; i < T_VOID+1; i++) { 343 f->do_int(&_archived_basic_type_mirror_indices[i]); 344 // if f->reading(): We can't call HeapShared::get_root() yet, as the heap 345 // contents may need to be relocated. _basic_type_mirrors[i] will be 346 // updated later in Universe::load_archived_object_instances(). 347 } 348 _null_ptr_exception.serialize(f); 349 _arithmetic_exception.serialize(f); 350 _internal_error.serialize(f); 351 _array_index_oob_exception.serialize(f); 352 _array_store_exception.serialize(f); 353 _class_cast_exception.serialize(f); 354 #endif 355 356 f->do_ptr(&_fillerArrayKlass); 357 for (int i = 0; i < T_LONG+1; i++) { 358 f->do_ptr(&_typeArrayKlasses[i]); 359 } 360 361 f->do_ptr(&_objectArrayKlass); 362 f->do_ptr(&_the_array_interfaces_array); 363 f->do_ptr(&_the_empty_int_array); 364 f->do_ptr(&_the_empty_short_array); 365 f->do_ptr(&_the_empty_method_array); 366 f->do_ptr(&_the_empty_klass_array); 367 f->do_ptr(&_the_empty_instance_klass_array); 368 } 369 370 371 void Universe::check_alignment(uintx size, uintx alignment, const char* name) { 372 if (size < alignment || size % alignment != 0) { 373 vm_exit_during_initialization( 374 err_msg("Size of %s (" UINTX_FORMAT " bytes) must be aligned to " UINTX_FORMAT " bytes", name, size, alignment)); 375 } 376 } 377 378 static void initialize_basic_type_klass(Klass* k, TRAPS) { 379 Klass* ok = vmClasses::Object_klass(); 380 #if INCLUDE_CDS 381 if (UseSharedSpaces) { 382 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 383 assert(k->super() == ok, "u3"); 384 if (k->is_instance_klass()) { 385 InstanceKlass::cast(k)->restore_unshareable_info(loader_data, Handle(), nullptr, CHECK); 386 } else { 387 ArrayKlass::cast(k)->restore_unshareable_info(loader_data, Handle(), CHECK); 388 } 389 } else 390 #endif 391 { 392 k->initialize_supers(ok, nullptr, CHECK); 393 } 394 k->append_to_sibling_list(); 395 } 396 397 void Universe::genesis(TRAPS) { 398 ResourceMark rm(THREAD); 399 HandleMark hm(THREAD); 400 401 // Explicit null checks are needed if these offsets are not smaller than the page size 402 assert(oopDesc::klass_offset_in_bytes() < static_cast<intptr_t>(os::vm_page_size()), 403 "Klass offset is expected to be less than the page size"); 404 assert(arrayOopDesc::length_offset_in_bytes() < static_cast<intptr_t>(os::vm_page_size()), 405 "Array length offset is expected to be less than the page size"); 406 407 { AutoModifyRestore<bool> temporarily(_bootstrapping, true); 408 409 java_lang_Class::allocate_fixup_lists(); 410 411 // determine base vtable size; without that we cannot create the array klasses 412 compute_base_vtable_size(); 413 414 if (!UseSharedSpaces) { 415 // Initialization of the fillerArrayKlass must come before regular 416 // int-TypeArrayKlass so that the int-Array mirror points to the 417 // int-TypeArrayKlass. 418 _fillerArrayKlass = TypeArrayKlass::create_klass(T_INT, "[Ljdk/internal/vm/FillerElement;", CHECK); 419 for (int i = T_BOOLEAN; i < T_LONG+1; i++) { 420 _typeArrayKlasses[i] = TypeArrayKlass::create_klass((BasicType)i, CHECK); 421 } 422 423 ClassLoaderData* null_cld = ClassLoaderData::the_null_class_loader_data(); 424 425 _the_array_interfaces_array = MetadataFactory::new_array<Klass*>(null_cld, 2, nullptr, CHECK); 426 _the_empty_int_array = MetadataFactory::new_array<int>(null_cld, 0, CHECK); 427 _the_empty_short_array = MetadataFactory::new_array<u2>(null_cld, 0, CHECK); 428 _the_empty_method_array = MetadataFactory::new_array<Method*>(null_cld, 0, CHECK); 429 _the_empty_klass_array = MetadataFactory::new_array<Klass*>(null_cld, 0, CHECK); 430 _the_empty_instance_klass_array = MetadataFactory::new_array<InstanceKlass*>(null_cld, 0, CHECK); 431 } 432 433 vmSymbols::initialize(); 434 435 SystemDictionary::initialize(CHECK); 436 437 // Create string constants 438 oop s = StringTable::intern("null", CHECK); 439 _the_null_string = OopHandle(vm_global(), s); 440 s = StringTable::intern("-2147483648", CHECK); 441 _the_min_jint_string = OopHandle(vm_global(), s); 442 443 444 #if INCLUDE_CDS 445 if (UseSharedSpaces) { 446 // Verify shared interfaces array. 447 assert(_the_array_interfaces_array->at(0) == 448 vmClasses::Cloneable_klass(), "u3"); 449 assert(_the_array_interfaces_array->at(1) == 450 vmClasses::Serializable_klass(), "u3"); 451 } else 452 #endif 453 { 454 // Set up shared interfaces array. (Do this before supers are set up.) 455 _the_array_interfaces_array->at_put(0, vmClasses::Cloneable_klass()); 456 _the_array_interfaces_array->at_put(1, vmClasses::Serializable_klass()); 457 } 458 459 if (UseSecondarySupersTable) { 460 Universe::_the_array_interfaces_bitmap = Klass::compute_secondary_supers_bitmap(_the_array_interfaces_array); 461 Universe::_the_empty_klass_bitmap = Klass::compute_secondary_supers_bitmap(_the_empty_klass_array); 462 } 463 464 initialize_basic_type_klass(_fillerArrayKlass, CHECK); 465 466 initialize_basic_type_klass(boolArrayKlass(), CHECK); 467 initialize_basic_type_klass(charArrayKlass(), CHECK); 468 initialize_basic_type_klass(floatArrayKlass(), CHECK); 469 initialize_basic_type_klass(doubleArrayKlass(), CHECK); 470 initialize_basic_type_klass(byteArrayKlass(), CHECK); 471 initialize_basic_type_klass(shortArrayKlass(), CHECK); 472 initialize_basic_type_klass(intArrayKlass(), CHECK); 473 initialize_basic_type_klass(longArrayKlass(), CHECK); 474 475 assert(_fillerArrayKlass != intArrayKlass(), 476 "Internal filler array klass should be different to int array Klass"); 477 } // end of core bootstrapping 478 479 { 480 Handle tns = java_lang_String::create_from_str("<null_sentinel>", CHECK); 481 _the_null_sentinel = OopHandle(vm_global(), tns()); 482 } 483 484 // Create a handle for reference_pending_list 485 _reference_pending_list = OopHandle(vm_global(), nullptr); 486 487 // Maybe this could be lifted up now that object array can be initialized 488 // during the bootstrapping. 489 490 // OLD 491 // Initialize _objectArrayKlass after core bootstraping to make 492 // sure the super class is set up properly for _objectArrayKlass. 493 // --- 494 // NEW 495 // Since some of the old system object arrays have been converted to 496 // ordinary object arrays, _objectArrayKlass will be loaded when 497 // SystemDictionary::initialize(CHECK); is run. See the extra check 498 // for Object_klass_loaded in objArrayKlassKlass::allocate_objArray_klass_impl. 499 { 500 Klass* oak = vmClasses::Object_klass()->array_klass(CHECK); 501 _objectArrayKlass = ObjArrayKlass::cast(oak); 502 } 503 // OLD 504 // Add the class to the class hierarchy manually to make sure that 505 // its vtable is initialized after core bootstrapping is completed. 506 // --- 507 // New 508 // Have already been initialized. 509 _objectArrayKlass->append_to_sibling_list(); 510 511 #ifdef ASSERT 512 if (FullGCALot) { 513 // Allocate an array of dummy objects. 514 // We'd like these to be at the bottom of the old generation, 515 // so that when we free one and then collect, 516 // (almost) the whole heap moves 517 // and we find out if we actually update all the oops correctly. 518 // But we can't allocate directly in the old generation, 519 // so we allocate wherever, and hope that the first collection 520 // moves these objects to the bottom of the old generation. 521 int size = FullGCALotDummies * 2; 522 523 objArrayOop naked_array = oopFactory::new_objArray(vmClasses::Object_klass(), size, CHECK); 524 objArrayHandle dummy_array(THREAD, naked_array); 525 int i = 0; 526 while (i < size) { 527 // Allocate dummy in old generation 528 oop dummy = vmClasses::Object_klass()->allocate_instance(CHECK); 529 dummy_array->obj_at_put(i++, dummy); 530 } 531 { 532 // Only modify the global variable inside the mutex. 533 // If we had a race to here, the other dummy_array instances 534 // and their elements just get dropped on the floor, which is fine. 535 MutexLocker ml(THREAD, FullGCALot_lock); 536 if (_fullgc_alot_dummy_array.is_empty()) { 537 _fullgc_alot_dummy_array = OopHandle(vm_global(), dummy_array()); 538 } 539 } 540 assert(i == ((objArrayOop)_fullgc_alot_dummy_array.resolve())->length(), "just checking"); 541 } 542 #endif 543 } 544 545 void Universe::initialize_basic_type_mirrors(TRAPS) { 546 #if INCLUDE_CDS_JAVA_HEAP 547 if (UseSharedSpaces && 548 ArchiveHeapLoader::is_in_use() && 549 _basic_type_mirrors[T_INT].resolve() != nullptr) { 550 assert(ArchiveHeapLoader::can_use(), "Sanity"); 551 552 // check that all basic type mirrors are mapped also 553 for (int i = T_BOOLEAN; i < T_VOID+1; i++) { 554 if (!is_reference_type((BasicType)i)) { 555 oop m = _basic_type_mirrors[i].resolve(); 556 assert(m != nullptr, "archived mirrors should not be null"); 557 } 558 } 559 } else 560 // _basic_type_mirrors[T_INT], etc, are null if archived heap is not mapped. 561 #endif 562 { 563 for (int i = T_BOOLEAN; i < T_VOID+1; i++) { 564 BasicType bt = (BasicType)i; 565 if (!is_reference_type(bt)) { 566 oop m = java_lang_Class::create_basic_type_mirror(type2name(bt), bt, CHECK); 567 _basic_type_mirrors[i] = OopHandle(vm_global(), m); 568 } 569 CDS_JAVA_HEAP_ONLY(_archived_basic_type_mirror_indices[i] = -1); 570 } 571 } 572 if (CDSConfig::is_dumping_heap()) { 573 HeapShared::init_scratch_objects(CHECK); 574 } 575 } 576 577 void Universe::fixup_mirrors(TRAPS) { 578 // Bootstrap problem: all classes gets a mirror (java.lang.Class instance) assigned eagerly, 579 // but we cannot do that for classes created before java.lang.Class is loaded. Here we simply 580 // walk over permanent objects created so far (mostly classes) and fixup their mirrors. Note 581 // that the number of objects allocated at this point is very small. 582 assert(vmClasses::Class_klass_loaded(), "java.lang.Class should be loaded"); 583 HandleMark hm(THREAD); 584 585 if (!UseSharedSpaces) { 586 // Cache the start of the static fields 587 InstanceMirrorKlass::init_offset_of_static_fields(); 588 } 589 590 GrowableArray <Klass*>* list = java_lang_Class::fixup_mirror_list(); 591 int list_length = list->length(); 592 for (int i = 0; i < list_length; i++) { 593 Klass* k = list->at(i); 594 assert(k->is_klass(), "List should only hold classes"); 595 java_lang_Class::fixup_mirror(k, CATCH); 596 } 597 delete java_lang_Class::fixup_mirror_list(); 598 java_lang_Class::set_fixup_mirror_list(nullptr); 599 } 600 601 #define assert_pll_locked(test) \ 602 assert(Heap_lock->test(), "Reference pending list access requires lock") 603 604 #define assert_pll_ownership() assert_pll_locked(owned_by_self) 605 606 oop Universe::reference_pending_list() { 607 if (Thread::current()->is_VM_thread()) { 608 assert_pll_locked(is_locked); 609 } else { 610 assert_pll_ownership(); 611 } 612 return _reference_pending_list.resolve(); 613 } 614 615 void Universe::clear_reference_pending_list() { 616 assert_pll_ownership(); 617 _reference_pending_list.replace(nullptr); 618 } 619 620 bool Universe::has_reference_pending_list() { 621 assert_pll_ownership(); 622 return _reference_pending_list.peek() != nullptr; 623 } 624 625 oop Universe::swap_reference_pending_list(oop list) { 626 assert_pll_locked(is_locked); 627 return _reference_pending_list.xchg(list); 628 } 629 630 #undef assert_pll_locked 631 #undef assert_pll_ownership 632 633 static void reinitialize_vtables() { 634 // The vtables are initialized by starting at java.lang.Object and 635 // initializing through the subclass links, so that the super 636 // classes are always initialized first. 637 for (ClassHierarchyIterator iter(vmClasses::Object_klass()); !iter.done(); iter.next()) { 638 Klass* sub = iter.klass(); 639 sub->vtable().initialize_vtable(); 640 } 641 } 642 643 static void reinitialize_itables() { 644 645 class ReinitTableClosure : public KlassClosure { 646 public: 647 void do_klass(Klass* k) { 648 if (k->is_instance_klass()) { 649 InstanceKlass::cast(k)->itable().initialize_itable(); 650 } 651 } 652 }; 653 654 MutexLocker mcld(ClassLoaderDataGraph_lock); 655 ReinitTableClosure cl; 656 ClassLoaderDataGraph::classes_do(&cl); 657 } 658 659 bool Universe::on_page_boundary(void* addr) { 660 return is_aligned(addr, os::vm_page_size()); 661 } 662 663 // the array of preallocated errors with backtraces 664 objArrayOop Universe::preallocated_out_of_memory_errors() { 665 return (objArrayOop)_preallocated_out_of_memory_error_array.resolve(); 666 } 667 668 objArrayOop Universe::out_of_memory_errors() { return (objArrayOop)_out_of_memory_errors.resolve(); } 669 670 oop Universe::out_of_memory_error_java_heap() { 671 return gen_out_of_memory_error(out_of_memory_errors()->obj_at(_oom_java_heap)); 672 } 673 674 oop Universe::out_of_memory_error_java_heap_without_backtrace() { 675 return out_of_memory_errors()->obj_at(_oom_java_heap); 676 } 677 678 oop Universe::out_of_memory_error_c_heap() { 679 return gen_out_of_memory_error(out_of_memory_errors()->obj_at(_oom_c_heap)); 680 } 681 682 oop Universe::out_of_memory_error_metaspace() { 683 return gen_out_of_memory_error(out_of_memory_errors()->obj_at(_oom_metaspace)); 684 } 685 686 oop Universe::out_of_memory_error_class_metaspace() { 687 return gen_out_of_memory_error(out_of_memory_errors()->obj_at(_oom_class_metaspace)); 688 } 689 690 oop Universe::out_of_memory_error_array_size() { 691 return gen_out_of_memory_error(out_of_memory_errors()->obj_at(_oom_array_size)); 692 } 693 694 oop Universe::out_of_memory_error_gc_overhead_limit() { 695 return gen_out_of_memory_error(out_of_memory_errors()->obj_at(_oom_gc_overhead_limit)); 696 } 697 698 oop Universe::out_of_memory_error_realloc_objects() { 699 return gen_out_of_memory_error(out_of_memory_errors()->obj_at(_oom_realloc_objects)); 700 } 701 702 oop Universe::class_init_out_of_memory_error() { return out_of_memory_errors()->obj_at(_oom_java_heap); } 703 oop Universe::class_init_stack_overflow_error() { return _class_init_stack_overflow_error.resolve(); } 704 oop Universe::delayed_stack_overflow_error_message() { return _delayed_stack_overflow_error_message.resolve(); } 705 706 707 bool Universe::should_fill_in_stack_trace(Handle throwable) { 708 // never attempt to fill in the stack trace of preallocated errors that do not have 709 // backtrace. These errors are kept alive forever and may be "re-used" when all 710 // preallocated errors with backtrace have been consumed. Also need to avoid 711 // a potential loop which could happen if an out of memory occurs when attempting 712 // to allocate the backtrace. 713 objArrayOop preallocated_oom = out_of_memory_errors(); 714 for (int i = 0; i < _oom_count; i++) { 715 if (throwable() == preallocated_oom->obj_at(i)) { 716 return false; 717 } 718 } 719 return true; 720 } 721 722 723 oop Universe::gen_out_of_memory_error(oop default_err) { 724 // generate an out of memory error: 725 // - if there is a preallocated error and stack traces are available 726 // (j.l.Throwable is initialized), then return the preallocated 727 // error with a filled in stack trace, and with the message 728 // provided by the default error. 729 // - otherwise, return the default error, without a stack trace. 730 int next; 731 if ((_preallocated_out_of_memory_error_avail_count > 0) && 732 vmClasses::Throwable_klass()->is_initialized()) { 733 next = (int)Atomic::add(&_preallocated_out_of_memory_error_avail_count, -1); 734 assert(next < (int)PreallocatedOutOfMemoryErrorCount, "avail count is corrupt"); 735 } else { 736 next = -1; 737 } 738 if (next < 0) { 739 // all preallocated errors have been used. 740 // return default 741 return default_err; 742 } else { 743 JavaThread* current = JavaThread::current(); 744 Handle default_err_h(current, default_err); 745 // get the error object at the slot and set set it to null so that the 746 // array isn't keeping it alive anymore. 747 Handle exc(current, preallocated_out_of_memory_errors()->obj_at(next)); 748 assert(exc() != nullptr, "slot has been used already"); 749 preallocated_out_of_memory_errors()->obj_at_put(next, nullptr); 750 751 // use the message from the default error 752 oop msg = java_lang_Throwable::message(default_err_h()); 753 assert(msg != nullptr, "no message"); 754 java_lang_Throwable::set_message(exc(), msg); 755 756 // populate the stack trace and return it. 757 java_lang_Throwable::fill_in_stack_trace_of_preallocated_backtrace(exc); 758 return exc(); 759 } 760 } 761 762 bool Universe::is_out_of_memory_error_metaspace(oop ex_obj) { 763 return java_lang_Throwable::message(ex_obj) == _msg_metaspace.resolve(); 764 } 765 766 bool Universe::is_out_of_memory_error_class_metaspace(oop ex_obj) { 767 return java_lang_Throwable::message(ex_obj) == _msg_class_metaspace.resolve(); 768 } 769 770 // Setup preallocated OutOfMemoryError errors 771 void Universe::create_preallocated_out_of_memory_errors(TRAPS) { 772 InstanceKlass* ik = vmClasses::OutOfMemoryError_klass(); 773 objArrayOop oa = oopFactory::new_objArray(ik, _oom_count, CHECK); 774 objArrayHandle oom_array(THREAD, oa); 775 776 for (int i = 0; i < _oom_count; i++) { 777 oop oom_obj = ik->allocate_instance(CHECK); 778 oom_array->obj_at_put(i, oom_obj); 779 } 780 _out_of_memory_errors = OopHandle(vm_global(), oom_array()); 781 782 Handle msg = java_lang_String::create_from_str("Java heap space", CHECK); 783 java_lang_Throwable::set_message(oom_array->obj_at(_oom_java_heap), msg()); 784 785 msg = java_lang_String::create_from_str("C heap space", CHECK); 786 java_lang_Throwable::set_message(oom_array->obj_at(_oom_c_heap), msg()); 787 788 msg = java_lang_String::create_from_str("Metaspace", CHECK); 789 _msg_metaspace = OopHandle(vm_global(), msg()); 790 java_lang_Throwable::set_message(oom_array->obj_at(_oom_metaspace), msg()); 791 792 msg = java_lang_String::create_from_str("Compressed class space", CHECK); 793 _msg_class_metaspace = OopHandle(vm_global(), msg()); 794 java_lang_Throwable::set_message(oom_array->obj_at(_oom_class_metaspace), msg()); 795 796 msg = java_lang_String::create_from_str("Requested array size exceeds VM limit", CHECK); 797 java_lang_Throwable::set_message(oom_array->obj_at(_oom_array_size), msg()); 798 799 msg = java_lang_String::create_from_str("GC overhead limit exceeded", CHECK); 800 java_lang_Throwable::set_message(oom_array->obj_at(_oom_gc_overhead_limit), msg()); 801 802 msg = java_lang_String::create_from_str("Java heap space: failed reallocation of scalar replaced objects", CHECK); 803 java_lang_Throwable::set_message(oom_array->obj_at(_oom_realloc_objects), msg()); 804 805 // Setup the array of errors that have preallocated backtrace 806 int len = (StackTraceInThrowable) ? (int)PreallocatedOutOfMemoryErrorCount : 0; 807 objArrayOop instance = oopFactory::new_objArray(ik, len, CHECK); 808 _preallocated_out_of_memory_error_array = OopHandle(vm_global(), instance); 809 objArrayHandle preallocated_oom_array(THREAD, instance); 810 811 for (int i=0; i<len; i++) { 812 oop err = ik->allocate_instance(CHECK); 813 Handle err_h(THREAD, err); 814 java_lang_Throwable::allocate_backtrace(err_h, CHECK); 815 preallocated_oom_array->obj_at_put(i, err_h()); 816 } 817 _preallocated_out_of_memory_error_avail_count = (jint)len; 818 } 819 820 intptr_t Universe::_non_oop_bits = 0; 821 822 void* Universe::non_oop_word() { 823 // Neither the high bits nor the low bits of this value is allowed 824 // to look like (respectively) the high or low bits of a real oop. 825 // 826 // High and low are CPU-specific notions, but low always includes 827 // the low-order bit. Since oops are always aligned at least mod 4, 828 // setting the low-order bit will ensure that the low half of the 829 // word will never look like that of a real oop. 830 // 831 // Using the OS-supplied non-memory-address word (usually 0 or -1) 832 // will take care of the high bits, however many there are. 833 834 if (_non_oop_bits == 0) { 835 _non_oop_bits = (intptr_t)os::non_memory_address_word() | 1; 836 } 837 838 return (void*)_non_oop_bits; 839 } 840 841 bool Universe::contains_non_oop_word(void* p) { 842 return *(void**)p == non_oop_word(); 843 } 844 845 static void initialize_global_behaviours() { 846 DefaultICProtectionBehaviour* protection_behavior = new DefaultICProtectionBehaviour(); 847 // Ignore leak of DefaultICProtectionBehaviour. It is overriden by some GC implementations and the 848 // pointer is leaked once. 849 LSAN_IGNORE_OBJECT(protection_behavior); 850 CompiledICProtectionBehaviour::set_current(protection_behavior); 851 } 852 853 jint universe_init() { 854 assert(!Universe::_fully_initialized, "called after initialize_vtables"); 855 guarantee(1 << LogHeapWordSize == sizeof(HeapWord), 856 "LogHeapWordSize is incorrect."); 857 guarantee(sizeof(oop) >= sizeof(HeapWord), "HeapWord larger than oop?"); 858 guarantee(sizeof(oop) % sizeof(HeapWord) == 0, 859 "oop size is not not a multiple of HeapWord size"); 860 861 TraceTime timer("Genesis", TRACETIME_LOG(Info, startuptime)); 862 863 initialize_global_behaviours(); 864 865 GCLogPrecious::initialize(); 866 867 // Initialize CPUTimeCounters object, which must be done before creation of the heap. 868 CPUTimeCounters::initialize(); 869 870 #ifdef _LP64 871 MetaspaceShared::adjust_heap_sizes_for_dumping(); 872 #endif // _LP64 873 874 GCConfig::arguments()->initialize_heap_sizes(); 875 876 jint status = Universe::initialize_heap(); 877 if (status != JNI_OK) { 878 return status; 879 } 880 881 Universe::initialize_tlab(); 882 883 Metaspace::global_initialize(); 884 885 // Initialize performance counters for metaspaces 886 MetaspaceCounters::initialize_performance_counters(); 887 888 // Checks 'AfterMemoryInit' constraints. 889 if (!JVMFlagLimit::check_all_constraints(JVMFlagConstraintPhase::AfterMemoryInit)) { 890 return JNI_EINVAL; 891 } 892 893 ClassLoaderData::init_null_class_loader_data(); 894 895 #if INCLUDE_CDS 896 DynamicArchive::check_for_dynamic_dump(); 897 if (UseSharedSpaces) { 898 // Read the data structures supporting the shared spaces (shared 899 // system dictionary, symbol table, etc.) 900 MetaspaceShared::initialize_shared_spaces(); 901 } 902 if (CDSConfig::is_dumping_archive()) { 903 MetaspaceShared::prepare_for_dumping(); 904 } 905 #endif 906 907 SymbolTable::create_table(); 908 StringTable::create_table(); 909 910 if (strlen(VerifySubSet) > 0) { 911 Universe::initialize_verify_flags(); 912 } 913 914 ResolvedMethodTable::create_table(); 915 916 return JNI_OK; 917 } 918 919 jint Universe::initialize_heap() { 920 assert(_collectedHeap == nullptr, "Heap already created"); 921 _collectedHeap = GCConfig::arguments()->create_heap(); 922 923 log_info(gc)("Using %s", _collectedHeap->name()); 924 return _collectedHeap->initialize(); 925 } 926 927 void Universe::initialize_tlab() { 928 ThreadLocalAllocBuffer::set_max_size(Universe::heap()->max_tlab_size()); 929 PLAB::startup_initialization(); 930 if (UseTLAB) { 931 ThreadLocalAllocBuffer::startup_initialization(); 932 } 933 } 934 935 ReservedHeapSpace Universe::reserve_heap(size_t heap_size, size_t alignment) { 936 937 assert(alignment <= Arguments::conservative_max_heap_alignment(), 938 "actual alignment " SIZE_FORMAT " must be within maximum heap alignment " SIZE_FORMAT, 939 alignment, Arguments::conservative_max_heap_alignment()); 940 941 size_t total_reserved = align_up(heap_size, alignment); 942 assert(!UseCompressedOops || (total_reserved <= (OopEncodingHeapMax - os::vm_page_size())), 943 "heap size is too big for compressed oops"); 944 945 size_t page_size = os::vm_page_size(); 946 if (UseLargePages && is_aligned(alignment, os::large_page_size())) { 947 page_size = os::large_page_size(); 948 } else { 949 // Parallel is the only collector that might opt out of using large pages 950 // for the heap. 951 assert(!UseLargePages || UseParallelGC , "Wrong alignment to use large pages"); 952 } 953 954 // Now create the space. 955 ReservedHeapSpace total_rs(total_reserved, alignment, page_size, AllocateHeapAt); 956 957 if (total_rs.is_reserved()) { 958 assert((total_reserved == total_rs.size()) && ((uintptr_t)total_rs.base() % alignment == 0), 959 "must be exactly of required size and alignment"); 960 // We are good. 961 962 if (AllocateHeapAt != nullptr) { 963 log_info(gc,heap)("Successfully allocated Java heap at location %s", AllocateHeapAt); 964 } 965 966 if (UseCompressedOops) { 967 CompressedOops::initialize(total_rs); 968 } 969 970 Universe::calculate_verify_data((HeapWord*)total_rs.base(), (HeapWord*)total_rs.end()); 971 972 return total_rs; 973 } 974 975 vm_exit_during_initialization( 976 err_msg("Could not reserve enough space for " SIZE_FORMAT "KB object heap", 977 total_reserved/K)); 978 979 // satisfy compiler 980 ShouldNotReachHere(); 981 return ReservedHeapSpace(0, 0, os::vm_page_size()); 982 } 983 984 OopStorage* Universe::vm_weak() { 985 return Universe::_vm_weak; 986 } 987 988 OopStorage* Universe::vm_global() { 989 return Universe::_vm_global; 990 } 991 992 void Universe::oopstorage_init() { 993 Universe::_vm_global = OopStorageSet::create_strong("VM Global", mtInternal); 994 Universe::_vm_weak = OopStorageSet::create_weak("VM Weak", mtInternal); 995 } 996 997 void universe_oopstorage_init() { 998 Universe::oopstorage_init(); 999 } 1000 1001 void LatestMethodCache::init(JavaThread* current, InstanceKlass* ik, 1002 const char* method, Symbol* signature, bool is_static) 1003 { 1004 TempNewSymbol name = SymbolTable::new_symbol(method); 1005 Method* m = nullptr; 1006 // The klass must be linked before looking up the method. 1007 if (!ik->link_class_or_fail(current) || 1008 ((m = ik->find_method(name, signature)) == nullptr) || 1009 is_static != m->is_static()) { 1010 ResourceMark rm(current); 1011 // NoSuchMethodException doesn't actually work because it tries to run the 1012 // <init> function before java_lang_Class is linked. Print error and exit. 1013 vm_exit_during_initialization(err_msg("Unable to link/verify %s.%s method", 1014 ik->name()->as_C_string(), method)); 1015 } 1016 1017 _klass = ik; 1018 _method_idnum = m->method_idnum(); 1019 assert(_method_idnum >= 0, "sanity check"); 1020 } 1021 1022 Method* LatestMethodCache::get_method() { 1023 if (_klass == nullptr) { 1024 return nullptr; 1025 } else { 1026 Method* m = _klass->method_with_idnum(_method_idnum); 1027 assert(m != nullptr, "sanity check"); 1028 return m; 1029 } 1030 } 1031 1032 Method* Universe::finalizer_register_method() { return _finalizer_register_cache.get_method(); } 1033 Method* Universe::loader_addClass_method() { return _loader_addClass_cache.get_method(); } 1034 Method* Universe::throw_illegal_access_error() { return _throw_illegal_access_error_cache.get_method(); } 1035 Method* Universe::throw_no_such_method_error() { return _throw_no_such_method_error_cache.get_method(); } 1036 Method* Universe::do_stack_walk_method() { return _do_stack_walk_cache.get_method(); } 1037 1038 void Universe::initialize_known_methods(JavaThread* current) { 1039 // Set up static method for registering finalizers 1040 _finalizer_register_cache.init(current, 1041 vmClasses::Finalizer_klass(), 1042 "register", 1043 vmSymbols::object_void_signature(), true); 1044 1045 _throw_illegal_access_error_cache.init(current, 1046 vmClasses::internal_Unsafe_klass(), 1047 "throwIllegalAccessError", 1048 vmSymbols::void_method_signature(), true); 1049 1050 _throw_no_such_method_error_cache.init(current, 1051 vmClasses::internal_Unsafe_klass(), 1052 "throwNoSuchMethodError", 1053 vmSymbols::void_method_signature(), true); 1054 1055 // Set up method for registering loaded classes in class loader vector 1056 _loader_addClass_cache.init(current, 1057 vmClasses::ClassLoader_klass(), 1058 "addClass", 1059 vmSymbols::class_void_signature(), false); 1060 1061 // Set up method for stack walking 1062 _do_stack_walk_cache.init(current, 1063 vmClasses::AbstractStackWalker_klass(), 1064 "doStackWalk", 1065 vmSymbols::doStackWalk_signature(), false); 1066 } 1067 1068 void universe2_init() { 1069 EXCEPTION_MARK; 1070 Universe::genesis(CATCH); 1071 } 1072 1073 // Set after initialization of the module runtime, call_initModuleRuntime 1074 void universe_post_module_init() { 1075 Universe::_module_initialized = true; 1076 } 1077 1078 bool universe_post_init() { 1079 assert(!is_init_completed(), "Error: initialization not yet completed!"); 1080 Universe::_fully_initialized = true; 1081 EXCEPTION_MARK; 1082 if (!UseSharedSpaces) { 1083 reinitialize_vtables(); 1084 reinitialize_itables(); 1085 } 1086 1087 HandleMark hm(THREAD); 1088 // Setup preallocated empty java.lang.Class array for Method reflection. 1089 1090 objArrayOop the_empty_class_array = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_false); 1091 Universe::_the_empty_class_array = OopHandle(Universe::vm_global(), the_empty_class_array); 1092 1093 // Setup preallocated OutOfMemoryError errors 1094 Universe::create_preallocated_out_of_memory_errors(CHECK_false); 1095 1096 oop instance; 1097 // Setup preallocated cause message for delayed StackOverflowError 1098 if (StackReservedPages > 0) { 1099 instance = java_lang_String::create_oop_from_str("Delayed StackOverflowError due to ReservedStackAccess annotated method", CHECK_false); 1100 Universe::_delayed_stack_overflow_error_message = OopHandle(Universe::vm_global(), instance); 1101 } 1102 1103 // Setup preallocated NullPointerException/ArithmeticException 1104 // (used for a cheap & dirty solution in compiler exception handling) 1105 _null_ptr_exception.init_if_empty(vmSymbols::java_lang_NullPointerException(), CHECK_false); 1106 _arithmetic_exception.init_if_empty(vmSymbols::java_lang_ArithmeticException(), CHECK_false); 1107 _array_index_oob_exception.init_if_empty(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), CHECK_false); 1108 _array_store_exception.init_if_empty(vmSymbols::java_lang_ArrayStoreException(), CHECK_false); 1109 _class_cast_exception.init_if_empty(vmSymbols::java_lang_ClassCastException(), CHECK_false); 1110 1111 // Virtual Machine Error for when we get into a situation we can't resolve 1112 Klass* k = vmClasses::InternalError_klass(); 1113 bool linked = InstanceKlass::cast(k)->link_class_or_fail(CHECK_false); 1114 if (!linked) { 1115 tty->print_cr("Unable to link/verify InternalError class"); 1116 return false; // initialization failed 1117 } 1118 _internal_error.init_if_empty(vmSymbols::java_lang_InternalError(), CHECK_false); 1119 1120 Handle msg = java_lang_String::create_from_str("/ by zero", CHECK_false); 1121 java_lang_Throwable::set_message(Universe::arithmetic_exception_instance(), msg()); 1122 1123 // Setup preallocated StackOverflowError for use with class initialization failure 1124 k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_StackOverflowError(), true, CHECK_false); 1125 instance = InstanceKlass::cast(k)->allocate_instance(CHECK_false); 1126 Universe::_class_init_stack_overflow_error = OopHandle(Universe::vm_global(), instance); 1127 1128 Universe::initialize_known_methods(THREAD); 1129 1130 // This needs to be done before the first scavenge/gc, since 1131 // it's an input to soft ref clearing policy. 1132 { 1133 MutexLocker x(THREAD, Heap_lock); 1134 Universe::heap()->update_capacity_and_used_at_gc(); 1135 } 1136 1137 // ("weak") refs processing infrastructure initialization 1138 Universe::heap()->post_initialize(); 1139 1140 MemoryService::add_metaspace_memory_pools(); 1141 1142 MemoryService::set_universe_heap(Universe::heap()); 1143 #if INCLUDE_CDS 1144 MetaspaceShared::post_initialize(CHECK_false); 1145 #endif 1146 return true; 1147 } 1148 1149 1150 void Universe::compute_base_vtable_size() { 1151 _base_vtable_size = ClassLoader::compute_Object_vtable(); 1152 } 1153 1154 void Universe::print_on(outputStream* st) { 1155 GCMutexLocker hl(Heap_lock); // Heap_lock might be locked by caller thread. 1156 st->print_cr("Heap"); 1157 heap()->print_on(st); 1158 } 1159 1160 void Universe::print_heap_at_SIGBREAK() { 1161 if (PrintHeapAtSIGBREAK) { 1162 print_on(tty); 1163 tty->cr(); 1164 tty->flush(); 1165 } 1166 } 1167 1168 void Universe::initialize_verify_flags() { 1169 verify_flags = 0; 1170 const char delimiter[] = " ,"; 1171 1172 size_t length = strlen(VerifySubSet); 1173 char* subset_list = NEW_C_HEAP_ARRAY(char, length + 1, mtInternal); 1174 strncpy(subset_list, VerifySubSet, length + 1); 1175 char* save_ptr; 1176 1177 char* token = strtok_r(subset_list, delimiter, &save_ptr); 1178 while (token != nullptr) { 1179 if (strcmp(token, "threads") == 0) { 1180 verify_flags |= Verify_Threads; 1181 } else if (strcmp(token, "heap") == 0) { 1182 verify_flags |= Verify_Heap; 1183 } else if (strcmp(token, "symbol_table") == 0) { 1184 verify_flags |= Verify_SymbolTable; 1185 } else if (strcmp(token, "string_table") == 0) { 1186 verify_flags |= Verify_StringTable; 1187 } else if (strcmp(token, "codecache") == 0) { 1188 verify_flags |= Verify_CodeCache; 1189 } else if (strcmp(token, "dictionary") == 0) { 1190 verify_flags |= Verify_SystemDictionary; 1191 } else if (strcmp(token, "classloader_data_graph") == 0) { 1192 verify_flags |= Verify_ClassLoaderDataGraph; 1193 } else if (strcmp(token, "metaspace") == 0) { 1194 verify_flags |= Verify_MetaspaceUtils; 1195 } else if (strcmp(token, "jni_handles") == 0) { 1196 verify_flags |= Verify_JNIHandles; 1197 } else if (strcmp(token, "codecache_oops") == 0) { 1198 verify_flags |= Verify_CodeCacheOops; 1199 } else if (strcmp(token, "resolved_method_table") == 0) { 1200 verify_flags |= Verify_ResolvedMethodTable; 1201 } else if (strcmp(token, "stringdedup") == 0) { 1202 verify_flags |= Verify_StringDedup; 1203 } else { 1204 vm_exit_during_initialization(err_msg("VerifySubSet: \'%s\' memory sub-system is unknown, please correct it", token)); 1205 } 1206 token = strtok_r(nullptr, delimiter, &save_ptr); 1207 } 1208 FREE_C_HEAP_ARRAY(char, subset_list); 1209 } 1210 1211 bool Universe::should_verify_subset(uint subset) { 1212 if (verify_flags & subset) { 1213 return true; 1214 } 1215 return false; 1216 } 1217 1218 void Universe::verify(VerifyOption option, const char* prefix) { 1219 COMPILER2_PRESENT( 1220 assert(!DerivedPointerTable::is_active(), 1221 "DPT should not be active during verification " 1222 "(of thread stacks below)"); 1223 ) 1224 1225 Thread* thread = Thread::current(); 1226 ResourceMark rm(thread); 1227 HandleMark hm(thread); // Handles created during verification can be zapped 1228 _verify_count++; 1229 1230 FormatBuffer<> title("Verifying %s", prefix); 1231 GCTraceTime(Info, gc, verify) tm(title.buffer()); 1232 if (should_verify_subset(Verify_Threads)) { 1233 log_debug(gc, verify)("Threads"); 1234 Threads::verify(); 1235 } 1236 if (should_verify_subset(Verify_Heap)) { 1237 log_debug(gc, verify)("Heap"); 1238 heap()->verify(option); 1239 } 1240 if (should_verify_subset(Verify_SymbolTable)) { 1241 log_debug(gc, verify)("SymbolTable"); 1242 SymbolTable::verify(); 1243 } 1244 if (should_verify_subset(Verify_StringTable)) { 1245 log_debug(gc, verify)("StringTable"); 1246 StringTable::verify(); 1247 } 1248 if (should_verify_subset(Verify_CodeCache)) { 1249 log_debug(gc, verify)("CodeCache"); 1250 CodeCache::verify(); 1251 } 1252 if (should_verify_subset(Verify_SystemDictionary)) { 1253 log_debug(gc, verify)("SystemDictionary"); 1254 SystemDictionary::verify(); 1255 } 1256 if (should_verify_subset(Verify_ClassLoaderDataGraph)) { 1257 log_debug(gc, verify)("ClassLoaderDataGraph"); 1258 ClassLoaderDataGraph::verify(); 1259 } 1260 if (should_verify_subset(Verify_MetaspaceUtils)) { 1261 log_debug(gc, verify)("MetaspaceUtils"); 1262 DEBUG_ONLY(MetaspaceUtils::verify();) 1263 } 1264 if (should_verify_subset(Verify_JNIHandles)) { 1265 log_debug(gc, verify)("JNIHandles"); 1266 JNIHandles::verify(); 1267 } 1268 if (should_verify_subset(Verify_CodeCacheOops)) { 1269 log_debug(gc, verify)("CodeCache Oops"); 1270 CodeCache::verify_oops(); 1271 } 1272 if (should_verify_subset(Verify_ResolvedMethodTable)) { 1273 log_debug(gc, verify)("ResolvedMethodTable Oops"); 1274 ResolvedMethodTable::verify(); 1275 } 1276 if (should_verify_subset(Verify_StringDedup)) { 1277 log_debug(gc, verify)("String Deduplication"); 1278 StringDedup::verify(); 1279 } 1280 } 1281 1282 1283 #ifndef PRODUCT 1284 void Universe::calculate_verify_data(HeapWord* low_boundary, HeapWord* high_boundary) { 1285 assert(low_boundary < high_boundary, "bad interval"); 1286 1287 // decide which low-order bits we require to be clear: 1288 size_t alignSize = MinObjAlignmentInBytes; 1289 size_t min_object_size = CollectedHeap::min_fill_size(); 1290 1291 // make an inclusive limit: 1292 uintptr_t max = (uintptr_t)high_boundary - min_object_size*wordSize; 1293 uintptr_t min = (uintptr_t)low_boundary; 1294 assert(min < max, "bad interval"); 1295 uintptr_t diff = max ^ min; 1296 1297 // throw away enough low-order bits to make the diff vanish 1298 uintptr_t mask = (uintptr_t)(-1); 1299 while ((mask & diff) != 0) 1300 mask <<= 1; 1301 uintptr_t bits = (min & mask); 1302 assert(bits == (max & mask), "correct mask"); 1303 // check an intermediate value between min and max, just to make sure: 1304 assert(bits == ((min + (max-min)/2) & mask), "correct mask"); 1305 1306 // require address alignment, too: 1307 mask |= (alignSize - 1); 1308 1309 if (!(_verify_oop_mask == 0 && _verify_oop_bits == (uintptr_t)-1)) { 1310 assert(_verify_oop_mask == mask && _verify_oop_bits == bits, "mask stability"); 1311 } 1312 _verify_oop_mask = mask; 1313 _verify_oop_bits = bits; 1314 } 1315 1316 void Universe::set_verify_data(uintptr_t mask, uintptr_t bits) { 1317 _verify_oop_mask = mask; 1318 _verify_oop_bits = bits; 1319 } 1320 1321 // Oop verification (see MacroAssembler::verify_oop) 1322 1323 uintptr_t Universe::verify_oop_mask() { 1324 return _verify_oop_mask; 1325 } 1326 1327 uintptr_t Universe::verify_oop_bits() { 1328 return _verify_oop_bits; 1329 } 1330 1331 uintptr_t Universe::verify_mark_mask() { 1332 return markWord::lock_mask_in_place; 1333 } 1334 1335 uintptr_t Universe::verify_mark_bits() { 1336 intptr_t mask = verify_mark_mask(); 1337 intptr_t bits = (intptr_t)markWord::prototype().value(); 1338 assert((bits & ~mask) == 0, "no stray header bits"); 1339 return bits; 1340 } 1341 #endif // PRODUCT 1342 1343 #ifdef ASSERT 1344 // Release dummy object(s) at bottom of heap 1345 bool Universe::release_fullgc_alot_dummy() { 1346 MutexLocker ml(FullGCALot_lock); 1347 objArrayOop fullgc_alot_dummy_array = (objArrayOop)_fullgc_alot_dummy_array.resolve(); 1348 if (fullgc_alot_dummy_array != nullptr) { 1349 if (_fullgc_alot_dummy_next >= fullgc_alot_dummy_array->length()) { 1350 // No more dummies to release, release entire array instead 1351 _fullgc_alot_dummy_array.release(Universe::vm_global()); 1352 _fullgc_alot_dummy_array = OopHandle(); // null out OopStorage pointer. 1353 return false; 1354 } 1355 1356 // Release dummy at bottom of old generation 1357 fullgc_alot_dummy_array->obj_at_put(_fullgc_alot_dummy_next++, nullptr); 1358 } 1359 return true; 1360 } 1361 1362 bool Universe::is_stw_gc_active() { 1363 return heap()->is_stw_gc_active(); 1364 } 1365 1366 bool Universe::is_in_heap(const void* p) { 1367 return heap()->is_in(p); 1368 } 1369 1370 #endif // ASSERT