1 /* 2 * Copyright (c) 2018, 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/archiveBuilder.hpp" 27 #include "cds/archiveHeapLoader.hpp" 28 #include "cds/archiveHeapWriter.hpp" 29 #include "cds/archiveUtils.hpp" 30 #include "cds/cdsConfig.hpp" 31 #include "cds/cdsEnumKlass.hpp" 32 #include "cds/cdsHeapVerifier.hpp" 33 #include "cds/classPreinitializer.hpp" 34 #include "cds/heapShared.hpp" 35 #include "cds/metaspaceShared.hpp" 36 #include "classfile/classLoaderData.hpp" 37 #include "classfile/javaClasses.inline.hpp" 38 #include "classfile/modules.hpp" 39 #include "classfile/stringTable.hpp" 40 #include "classfile/symbolTable.hpp" 41 #include "classfile/systemDictionary.hpp" 42 #include "classfile/systemDictionaryShared.hpp" 43 #include "classfile/vmClasses.hpp" 44 #include "classfile/vmSymbols.hpp" 45 #include "gc/shared/collectedHeap.hpp" 46 #include "gc/shared/gcLocker.hpp" 47 #include "gc/shared/gcVMOperations.hpp" 48 #include "logging/log.hpp" 49 #include "logging/logStream.hpp" 50 #include "memory/iterator.inline.hpp" 51 #include "memory/resourceArea.hpp" 52 #include "memory/universe.hpp" 53 #include "oops/compressedOops.inline.hpp" 54 #include "oops/fieldStreams.inline.hpp" 55 #include "oops/objArrayOop.inline.hpp" 56 #include "oops/oop.inline.hpp" 57 #include "oops/typeArrayOop.inline.hpp" 58 #include "prims/jvmtiExport.hpp" 59 #include "runtime/fieldDescriptor.inline.hpp" 60 #include "runtime/init.hpp" 61 #include "runtime/javaCalls.hpp" 62 #include "runtime/mutexLocker.hpp" 63 #include "runtime/safepointVerifiers.hpp" 64 #include "utilities/bitMap.inline.hpp" 65 #include "utilities/copy.hpp" 66 #if INCLUDE_G1GC 67 #include "gc/g1/g1CollectedHeap.hpp" 68 #endif 69 70 #if INCLUDE_CDS_JAVA_HEAP 71 72 struct ArchivableStaticFieldInfo { 73 const char* klass_name; 74 const char* field_name; 75 InstanceKlass* klass; 76 int offset; 77 BasicType type; 78 79 ArchivableStaticFieldInfo(const char* k, const char* f) 80 : klass_name(k), field_name(f), klass(nullptr), offset(0), type(T_ILLEGAL) {} 81 82 bool valid() { 83 return klass_name != nullptr; 84 } 85 }; 86 87 class HeapShared::ArchivingObjectMark : public StackObj { 88 public: 89 ArchivingObjectMark(oop obj) { 90 _trace->push(obj); 91 } 92 ~ArchivingObjectMark() { 93 _trace->pop(); 94 } 95 }; 96 97 class HeapShared::ContextMark : public StackObj { 98 ResourceMark rm; 99 public: 100 ContextMark(const char* c) : rm{} { 101 _context->push(c); 102 } 103 ~ContextMark() { 104 _context->pop(); 105 } 106 }; 107 108 bool HeapShared::_disable_writing = false; 109 DumpedInternedStrings *HeapShared::_dumped_interned_strings = nullptr; 110 111 size_t HeapShared::_alloc_count[HeapShared::ALLOC_STAT_SLOTS]; 112 size_t HeapShared::_alloc_size[HeapShared::ALLOC_STAT_SLOTS]; 113 size_t HeapShared::_total_obj_count; 114 size_t HeapShared::_total_obj_size; 115 116 #ifndef PRODUCT 117 #define ARCHIVE_TEST_FIELD_NAME "archivedObjects" 118 static Array<char>* _archived_ArchiveHeapTestClass = nullptr; 119 static const char* _test_class_name = nullptr; 120 static const Klass* _test_class = nullptr; 121 static const ArchivedKlassSubGraphInfoRecord* _test_class_record = nullptr; 122 #endif 123 124 125 // 126 // If you add new entries to the following tables, you should know what you're doing! 127 // 128 129 static ArchivableStaticFieldInfo archive_subgraph_entry_fields[] = { 130 {"java/lang/Boolean", "archivedCache"}, 131 {"java/lang/Integer$IntegerCache", "archivedCache"}, 132 {"java/lang/Long$LongCache", "archivedCache"}, 133 {"java/lang/Byte$ByteCache", "archivedCache"}, 134 {"java/lang/Short$ShortCache", "archivedCache"}, 135 {"java/lang/Character$CharacterCache", "archivedCache"}, 136 {"java/util/jar/Attributes$Name", "KNOWN_NAMES"}, 137 {"sun/util/locale/BaseLocale", "constantBaseLocales"}, 138 {"jdk/internal/module/ArchivedModuleGraph", "archivedModuleGraph"}, 139 {"java/util/ImmutableCollections", "archivedObjects"}, 140 {"java/lang/ModuleLayer", "EMPTY_LAYER"}, 141 {"java/lang/module/Configuration", "EMPTY_CONFIGURATION"}, 142 {"jdk/internal/math/FDBigInteger", "archivedCaches"}, 143 {"java/lang/invoke/DirectMethodHandle", "archivedObjects"}, // FIXME -- requires PreloadSharedClasses 144 {"java/lang/invoke/MethodType", "archivedObjects"}, // FIXME -- requires PreloadSharedClasses 145 {"java/lang/invoke/LambdaForm$NamedFunction", "archivedObjects"}, // FIXME -- requires PreloadSharedClasses 146 {"java/lang/reflect/Proxy$ProxyBuilder", "archivedData"}, // FIXME -- requires PreloadSharedClasses 147 #ifndef PRODUCT 148 {nullptr, nullptr}, // Extra slot for -XX:ArchiveHeapTestClass 149 #endif 150 {nullptr, nullptr}, 151 }; 152 153 // full module graph 154 static ArchivableStaticFieldInfo fmg_archive_subgraph_entry_fields[] = { 155 {"jdk/internal/loader/ArchivedClassLoaders", "archivedClassLoaders"}, 156 {ARCHIVED_BOOT_LAYER_CLASS, ARCHIVED_BOOT_LAYER_FIELD}, 157 {"java/lang/Module$ArchivedData", "archivedData"}, 158 {nullptr, nullptr}, 159 }; 160 161 KlassSubGraphInfo* HeapShared::_default_subgraph_info; 162 ArchivedKlassSubGraphInfoRecord* HeapShared::_runtime_default_subgraph_info; 163 GrowableArrayCHeap<oop, mtClassShared>* HeapShared::_pending_roots = nullptr; 164 GrowableArrayCHeap<oop, mtClassShared>* HeapShared::_trace = nullptr; 165 GrowableArrayCHeap<const char*, mtClassShared>* HeapShared::_context = nullptr; 166 OopHandle HeapShared::_roots; 167 OopHandle HeapShared::_scratch_basic_type_mirrors[T_VOID+1]; 168 MetaspaceObjToOopHandleTable* HeapShared::_scratch_java_mirror_table = nullptr; 169 MetaspaceObjToOopHandleTable* HeapShared::_scratch_references_table = nullptr; 170 int HeapShared::_permobj_segments = 0; 171 172 static bool is_subgraph_root_class_of(ArchivableStaticFieldInfo fields[], InstanceKlass* ik) { 173 for (int i = 0; fields[i].valid(); i++) { 174 if (fields[i].klass == ik) { 175 return true; 176 } 177 } 178 return false; 179 } 180 181 bool HeapShared::is_subgraph_root_class(InstanceKlass* ik) { 182 return is_subgraph_root_class_of(archive_subgraph_entry_fields, ik) || 183 is_subgraph_root_class_of(fmg_archive_subgraph_entry_fields, ik); 184 } 185 186 unsigned HeapShared::oop_hash(oop const& p) { 187 // Do not call p->identity_hash() as that will update the 188 // object header. 189 return primitive_hash(cast_from_oop<intptr_t>(p)); 190 } 191 192 static void reset_states(oop obj, TRAPS) { 193 Handle h_obj(THREAD, obj); 194 InstanceKlass* klass = InstanceKlass::cast(obj->klass()); 195 TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates"); 196 Symbol* method_sig = vmSymbols::void_method_signature(); 197 198 while (klass != nullptr) { 199 Method* method = klass->find_method(method_name, method_sig); 200 if (method != nullptr) { 201 assert(method->is_private(), "must be"); 202 if (log_is_enabled(Debug, cds)) { 203 ResourceMark rm(THREAD); 204 log_debug(cds)(" calling %s", method->name_and_sig_as_C_string()); 205 } 206 JavaValue result(T_VOID); 207 JavaCalls::call_special(&result, h_obj, klass, 208 method_name, method_sig, CHECK); 209 } 210 klass = klass->java_super(); 211 } 212 } 213 214 void HeapShared::reset_archived_object_states(TRAPS) { 215 assert(CDSConfig::is_dumping_heap(), "dump-time only"); 216 log_debug(cds)("Resetting platform loader"); 217 reset_states(SystemDictionary::java_platform_loader(), CHECK); 218 log_debug(cds)("Resetting system loader"); 219 reset_states(SystemDictionary::java_system_loader(), CHECK); 220 221 // Clean up jdk.internal.loader.ClassLoaders::bootLoader(), which is not 222 // directly used for class loading, but rather is used by the core library 223 // to keep track of resources, etc, loaded by the null class loader. 224 // 225 // Note, this object is non-null, and is not the same as 226 // ClassLoaderData::the_null_class_loader_data()->class_loader(), 227 // which is null. 228 log_debug(cds)("Resetting boot loader"); 229 JavaValue result(T_OBJECT); 230 JavaCalls::call_static(&result, 231 vmClasses::jdk_internal_loader_ClassLoaders_klass(), 232 vmSymbols::bootLoader_name(), 233 vmSymbols::void_BuiltinClassLoader_signature(), 234 CHECK); 235 Handle boot_loader(THREAD, result.get_oop()); 236 reset_states(boot_loader(), CHECK); 237 } 238 239 HeapShared::ArchivedObjectCache* HeapShared::_archived_object_cache = nullptr; 240 241 bool HeapShared::has_been_archived(oop obj) { 242 assert(CDSConfig::is_dumping_heap(), "dump-time only"); 243 return archived_object_cache()->get(obj) != nullptr; 244 } 245 246 int HeapShared::append_root(oop obj) { 247 assert(CDSConfig::is_dumping_heap(), "dump-time only"); 248 249 // No GC should happen since we aren't scanning _pending_roots. 250 assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread"); 251 252 if (_pending_roots == nullptr) { 253 _pending_roots = new GrowableArrayCHeap<oop, mtClassShared>(500); 254 } 255 256 return _pending_roots->append(obj); 257 } 258 259 objArrayOop HeapShared::roots() { 260 if (CDSConfig::is_dumping_heap() && !CDSConfig::is_dumping_final_static_archive()) { 261 assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread"); 262 if (!HeapShared::can_write()) { 263 return nullptr; 264 } 265 } else { 266 assert(CDSConfig::is_using_archive(), "must be"); 267 } 268 269 objArrayOop roots = (objArrayOop)_roots.resolve(); 270 assert(roots != nullptr, "should have been initialized"); 271 return roots; 272 } 273 274 static unsigned int oop_handle_hash(const OopHandle& oh) { 275 oop o = oh.resolve(); 276 if (o == nullptr) { 277 return 0; 278 } else { 279 return o->identity_hash(); 280 } 281 } 282 283 static bool oop_handle_equals(const OopHandle& a, const OopHandle& b) { 284 return a.resolve() == b.resolve(); 285 } 286 287 class OrigToScratchObjectTable: public ResourceHashtable<OopHandle, OopHandle, 288 36137, // prime number 289 AnyObj::C_HEAP, 290 mtClassShared, 291 oop_handle_hash, 292 oop_handle_equals> {}; 293 294 static OrigToScratchObjectTable* _orig_to_scratch_object_table = nullptr; 295 296 void HeapShared::track_scratch_object(oop orig_obj, oop scratch_obj) { 297 MutexLocker ml(ArchivedObjectTables_lock, Mutex::_no_safepoint_check_flag); 298 if (_orig_to_scratch_object_table == nullptr) { 299 _orig_to_scratch_object_table = new (mtClass)OrigToScratchObjectTable(); 300 } 301 302 OopHandle orig_h(Universe::vm_global(), orig_obj); 303 OopHandle scratch_h(Universe::vm_global(), scratch_obj); 304 _orig_to_scratch_object_table->put_when_absent(orig_h, scratch_h); 305 } 306 307 oop HeapShared::orig_to_scratch_object(oop orig_obj) { 308 MutexLocker ml(ArchivedObjectTables_lock, Mutex::_no_safepoint_check_flag); 309 if (_orig_to_scratch_object_table != nullptr) { 310 OopHandle orig(&orig_obj); 311 OopHandle* v = _orig_to_scratch_object_table->get(orig); 312 if (v != nullptr) { 313 return v->resolve(); 314 } 315 } 316 return nullptr; 317 } 318 319 class ArchivedObjectPermanentIndexTable: public ResourceHashtable<OopHandle, int, 320 36137, // prime number 321 AnyObj::C_HEAP, 322 mtClassShared, 323 oop_handle_hash, 324 oop_handle_equals> {}; 325 326 static ArchivedObjectPermanentIndexTable* _permanent_index_table = nullptr; 327 328 void HeapShared::add_to_permanent_index_table(oop obj, int index) { 329 assert_locked_or_safepoint(ArchivedObjectTables_lock); 330 331 if (_permanent_index_table == nullptr) { 332 _permanent_index_table = new (mtClass)ArchivedObjectPermanentIndexTable(); 333 } 334 OopHandle oh(Universe::vm_global(), obj); 335 _permanent_index_table->put(oh, index); 336 } 337 338 int HeapShared::get_archived_object_permanent_index(oop obj) { 339 if (!UsePermanentHeapObjects) { 340 return -1; 341 } 342 if (!CDSConfig::is_dumping_heap() && _permobj_segments <= 0) { 343 return -1; 344 } 345 346 MutexLocker ml(ArchivedObjectTables_lock, Mutex::_no_safepoint_check_flag); 347 348 if (!CDSConfig::is_dumping_heap() && _permanent_index_table == nullptr) { 349 int first_permobj_segment = roots()->length() - _permobj_segments; 350 for (int i = 0; i < _permobj_segments; i++) { 351 objArrayOop a = (objArrayOop)roots()->obj_at(i + first_permobj_segment); 352 for (int j = 0; j < a->length(); j++) { 353 int index = (i << ArchiveHeapWriter::PERMOBJ_SEGMENT_MAX_SHIFT) + j; 354 add_to_permanent_index_table(a->obj_at(j), index); 355 } 356 } 357 } 358 359 if (_permanent_index_table != nullptr) { 360 if (_orig_to_scratch_object_table != nullptr) { 361 OopHandle orig(&obj); 362 OopHandle* v = _orig_to_scratch_object_table->get(orig); 363 if (v != nullptr) { 364 obj = v->resolve(); 365 } 366 } 367 OopHandle tmp(&obj); 368 int* v = _permanent_index_table->get(tmp); 369 if (v != nullptr) { 370 int n = *v; 371 return n; 372 } 373 } 374 375 return -1; 376 } 377 378 oop HeapShared::get_archived_object(int permanent_index) { 379 if (ArchiveHeapLoader::is_in_use()) { 380 assert(_permobj_segments > 0, "must be"); 381 382 int first_permobj_segment = roots()->length() - _permobj_segments; 383 int upper = permanent_index >> ArchiveHeapWriter::PERMOBJ_SEGMENT_MAX_SHIFT; 384 int lower = permanent_index & ArchiveHeapWriter::PERMOBJ_SEGMENT_MAX_MASK; 385 objArrayOop a = (objArrayOop)roots()->obj_at(upper + first_permobj_segment); 386 return a->obj_at(lower); 387 } else { 388 assert(CDSConfig::is_dumping_heap(), "must be"); 389 return ArchiveHeapWriter::get_perm_object_by_index(permanent_index); 390 } 391 } 392 393 // Returns an objArray that contains all the roots of the archived objects 394 oop HeapShared::get_root(int index, bool clear) { 395 assert(index >= 0, "sanity"); 396 assert(!CDSConfig::is_dumping_heap() && CDSConfig::is_using_archive(), "runtime only"); 397 assert(!_roots.is_empty(), "must have loaded shared heap"); 398 oop result = roots()->obj_at(index); 399 if (clear) { 400 clear_root(index); 401 } 402 return result; 403 } 404 405 void HeapShared::clear_root(int index) { 406 assert(index >= 0, "sanity"); 407 assert(CDSConfig::is_using_archive(), "must be"); 408 if (ArchiveHeapLoader::is_in_use()) { 409 if (log_is_enabled(Debug, cds, heap)) { 410 oop old = roots()->obj_at(index); 411 log_debug(cds, heap)("Clearing root %d: was " PTR_FORMAT, index, p2i(old)); 412 } 413 roots()->obj_at_put(index, nullptr); 414 } 415 } 416 417 bool HeapShared::archive_object(oop obj) { 418 assert(CDSConfig::is_dumping_heap(), "dump-time only"); 419 420 assert(!obj->is_stackChunk(), "do not archive stack chunks"); 421 if (has_been_archived(obj)) { 422 return true; 423 } 424 425 if (ArchiveHeapWriter::is_too_large_to_archive(obj->size())) { 426 log_debug(cds, heap)("Cannot archive, object (" PTR_FORMAT ") is too large: " SIZE_FORMAT, 427 p2i(obj), obj->size()); 428 return false; 429 } else { 430 count_allocation(obj->size()); 431 ArchiveHeapWriter::add_source_obj(obj); 432 CachedOopInfo info = make_cached_oop_info(obj); 433 archived_object_cache()->put_when_absent(obj, info); 434 archived_object_cache()->maybe_grow(); 435 mark_native_pointers(obj); 436 437 if (log_is_enabled(Debug, cds, heap)) { 438 ResourceMark rm; 439 log_debug(cds, heap)("Archived heap object " PTR_FORMAT " : %s", 440 p2i(obj), obj->klass()->external_name()); 441 } 442 443 if (java_lang_Module::is_instance(obj) && Modules::check_archived_module_oop(obj)) { 444 Modules::update_oops_in_archived_module(obj, append_root(obj)); 445 } 446 447 return true; 448 } 449 } 450 451 class MetaspaceObjToOopHandleTable: public ResourceHashtable<MetaspaceObj*, OopHandle, 452 36137, // prime number 453 AnyObj::C_HEAP, 454 mtClassShared> { 455 public: 456 oop get_oop(MetaspaceObj* ptr) { 457 MutexLocker ml(ScratchObjects_lock, Mutex::_no_safepoint_check_flag); 458 OopHandle* handle = get(ptr); 459 if (handle != nullptr) { 460 return handle->resolve(); 461 } else { 462 return nullptr; 463 } 464 } 465 void set_oop(MetaspaceObj* ptr, oop o) { 466 MutexLocker ml(ScratchObjects_lock, Mutex::_no_safepoint_check_flag); 467 OopHandle handle(Universe::vm_global(), o); 468 bool is_new = put(ptr, handle); 469 assert(is_new, "cannot set twice"); 470 } 471 void remove_oop(MetaspaceObj* ptr) { 472 MutexLocker ml(ScratchObjects_lock, Mutex::_no_safepoint_check_flag); 473 OopHandle* handle = get(ptr); 474 if (handle != nullptr) { 475 handle->release(Universe::vm_global()); 476 remove(ptr); 477 } 478 } 479 }; 480 481 void HeapShared::add_scratch_resolved_references(ConstantPool* src, objArrayOop dest) { 482 if (_scratch_references_table == nullptr) { 483 _scratch_references_table = new (mtClass)MetaspaceObjToOopHandleTable(); 484 } 485 _scratch_references_table->set_oop(src, dest); 486 } 487 488 objArrayOop HeapShared::scratch_resolved_references(ConstantPool* src) { 489 return (objArrayOop)_scratch_references_table->get_oop(src); 490 } 491 492 void HeapShared::init_scratch_objects(TRAPS) { 493 for (int i = T_BOOLEAN; i < T_VOID+1; i++) { 494 BasicType bt = (BasicType)i; 495 if (!is_reference_type(bt)) { 496 oop m = java_lang_Class::create_basic_type_mirror(type2name(bt), bt, CHECK); 497 _scratch_basic_type_mirrors[i] = OopHandle(Universe::vm_global(), m); 498 track_scratch_object(Universe::java_mirror(bt), m); 499 } 500 } 501 _scratch_java_mirror_table = new (mtClass)MetaspaceObjToOopHandleTable(); 502 if (_scratch_references_table == nullptr) { 503 _scratch_references_table = new (mtClass)MetaspaceObjToOopHandleTable(); 504 } 505 } 506 507 // Given java_mirror that represents a (primitive or reference) type T, 508 // return the "scratch" version that represents the same type T. 509 // Note that if java_mirror will be returned if it's already a 510 // scratch mirror. 511 // 512 // See java_lang_Class::create_scratch_mirror() for more info. 513 oop HeapShared::scratch_java_mirror(oop java_mirror) { 514 assert(java_lang_Class::is_instance(java_mirror), "must be"); 515 516 for (int i = T_BOOLEAN; i < T_VOID+1; i++) { 517 BasicType bt = (BasicType)i; 518 if (!is_reference_type(bt)) { 519 if (_scratch_basic_type_mirrors[i].resolve() == java_mirror) { 520 return java_mirror; 521 } 522 } 523 } 524 525 if (java_lang_Class::is_primitive(java_mirror)) { 526 return scratch_java_mirror(java_lang_Class::as_BasicType(java_mirror)); 527 } else { 528 return scratch_java_mirror(java_lang_Class::as_Klass(java_mirror)); 529 } 530 } 531 532 oop HeapShared::scratch_java_mirror(BasicType t) { 533 assert((uint)t < T_VOID+1, "range check"); 534 assert(!is_reference_type(t), "sanity"); 535 return _scratch_basic_type_mirrors[t].resolve(); 536 } 537 538 oop HeapShared::scratch_java_mirror(Klass* k) { 539 return _scratch_java_mirror_table->get_oop(k); 540 } 541 542 void HeapShared::set_scratch_java_mirror(Klass* k, oop mirror) { 543 track_scratch_object(k->java_mirror(), mirror); 544 _scratch_java_mirror_table->set_oop(k, mirror); 545 } 546 547 void HeapShared::remove_scratch_objects(Klass* k) { 548 _scratch_java_mirror_table->remove_oop(k); 549 if (k->is_instance_klass()) { 550 _scratch_references_table->remove(InstanceKlass::cast(k)->constants()); 551 } 552 } 553 554 bool HeapShared::is_lambda_form_klass(InstanceKlass* ik) { 555 return ik->is_hidden() && 556 (ik->name()->starts_with("java/lang/invoke/LambdaForm$MH+") || 557 ik->name()->starts_with("java/lang/invoke/LambdaForm$DMH+") || 558 ik->name()->starts_with("java/lang/invoke/LambdaForm$BMH+")); 559 560 } 561 562 bool HeapShared::is_lambda_proxy_klass(InstanceKlass* ik) { 563 return ik->is_hidden() && (ik->name()->index_of_at(0, "$$Lambda+", 9) > 0); 564 } 565 566 bool HeapShared::is_archivable_hidden_klass(InstanceKlass* ik) { 567 return CDSConfig::is_dumping_invokedynamic() && (is_lambda_form_klass(ik) || is_lambda_proxy_klass(ik)); 568 } 569 570 void HeapShared::copy_preinitialized_mirror(Klass* orig_k, oop orig_mirror, oop m) { 571 if (!orig_k->is_instance_klass()) { 572 return; 573 } 574 InstanceKlass* ik = InstanceKlass::cast(orig_k); 575 576 if (HeapShared::is_archivable_hidden_klass(ik)) { 577 // We can't rerun the <clinit> method of hidden classes as we don't save 578 // the classData, so we must archive its mirror in initialized state. 579 assert(ik->is_initialized(), "must be"); 580 } 581 582 if (!ik->is_initialized() || !ClassPreinitializer::can_archive_preinitialized_mirror(ik)) { 583 return; 584 } 585 586 int nfields = 0; 587 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) { 588 if (fs.access_flags().is_static()) { 589 fieldDescriptor& fd = fs.field_descriptor(); 590 int offset = fd.offset(); 591 switch (fd.field_type()) { 592 case T_OBJECT: 593 case T_ARRAY: 594 m->obj_field_put(offset, orig_mirror->obj_field(offset)); 595 break; 596 case T_BOOLEAN: 597 m->bool_field_put(offset, orig_mirror->bool_field(offset)); 598 break; 599 case T_BYTE: 600 m->byte_field_put(offset, orig_mirror->byte_field(offset)); 601 break; 602 case T_SHORT: 603 m->short_field_put(offset, orig_mirror->short_field(offset)); 604 break; 605 case T_CHAR: 606 m->char_field_put(offset, orig_mirror->char_field(offset)); 607 break; 608 case T_INT: 609 m->int_field_put(offset, orig_mirror->int_field(offset)); 610 break; 611 case T_LONG: 612 m->long_field_put(offset, orig_mirror->long_field(offset)); 613 break; 614 case T_FLOAT: 615 m->float_field_put(offset, orig_mirror->float_field(offset)); 616 break; 617 case T_DOUBLE: 618 m->double_field_put(offset, orig_mirror->double_field(offset)); 619 break; 620 default: 621 ShouldNotReachHere(); 622 } 623 nfields ++; 624 } 625 } 626 if (log_is_enabled(Info, cds, init)) { 627 ResourceMark rm; 628 log_debug(cds, init)("copied %3d field(s) in preinitialized mirror %s%s", nfields, ik->external_name(), 629 ik->is_hidden() ? " (hidden)" : ""); 630 } 631 632 InstanceKlass* buffered_ik = ArchiveBuilder::current()->get_buffered_addr(ik); 633 buffered_ik->set_has_preinitialized_mirror(); 634 } 635 636 static void copy_java_mirror_hashcode(oop orig_mirror, oop scratch_m) { 637 int src_hash = orig_mirror->identity_hash(); 638 scratch_m->set_mark(markWord::prototype().copy_set_hash(src_hash)); 639 assert(scratch_m->mark().is_unlocked(), "sanity"); 640 641 DEBUG_ONLY(int archived_hash = scratch_m->identity_hash()); 642 assert(src_hash == archived_hash, "Java mirror wrong hash: original %x, scratch %x", src_hash, archived_hash); 643 } 644 645 void HeapShared::archive_java_mirrors() { 646 ClassPreinitializer::reset_preinit_check(); 647 648 for (int i = T_BOOLEAN; i < T_VOID+1; i++) { 649 BasicType bt = (BasicType)i; 650 if (!is_reference_type(bt)) { 651 oop orig_mirror = Universe::java_mirror(bt); 652 oop m = _scratch_basic_type_mirrors[i].resolve(); 653 assert(m != nullptr, "sanity"); 654 copy_java_mirror_hashcode(orig_mirror, m); 655 bool success = archive_reachable_objects_from(1, _default_subgraph_info, orig_mirror); 656 assert(success, "sanity"); 657 658 log_trace(cds, heap, mirror)( 659 "Archived %s mirror object from " PTR_FORMAT, 660 type2name(bt), p2i(m)); 661 662 Universe::set_archived_basic_type_mirror_index(bt, append_root(m)); 663 } 664 } 665 666 GrowableArray<Klass*>* klasses = ArchiveBuilder::current()->klasses(); 667 assert(klasses != nullptr, "sanity"); 668 for (int i = 0; i < klasses->length(); i++) { 669 Klass* orig_k = klasses->at(i); 670 oop orig_mirror = orig_k->java_mirror(); 671 oop m = scratch_java_mirror(orig_k); 672 if (m != nullptr) { 673 copy_java_mirror_hashcode(orig_mirror, m); 674 copy_preinitialized_mirror(orig_k, orig_mirror, m); 675 if (CDSConfig::is_dumping_reflection_data() && java_lang_Class::has_reflection_data(orig_mirror)) { 676 oop reflection_data = java_lang_Class::reflection_data(orig_mirror); 677 bool success = archive_reachable_objects_from(1, _default_subgraph_info, reflection_data); 678 guarantee(success, ""); 679 java_lang_Class::set_reflection_data(m, reflection_data); 680 } 681 Klass* buffered_k = ArchiveBuilder::get_buffered_klass(orig_k); 682 bool success = archive_reachable_objects_from(1, _default_subgraph_info, orig_mirror); 683 guarantee(success, "scratch mirrors must point to only archivable objects"); 684 buffered_k->set_archived_java_mirror(append_root(m)); 685 ResourceMark rm; 686 log_trace(cds, heap, mirror)( 687 "Archived %s mirror object from " PTR_FORMAT, 688 buffered_k->external_name(), p2i(m)); 689 690 // archive the resolved_referenes array 691 if (buffered_k->is_instance_klass()) { 692 InstanceKlass* ik = InstanceKlass::cast(buffered_k); 693 oop rr = ik->constants()->prepare_resolved_references_for_archiving(); 694 if (rr != nullptr && !ArchiveHeapWriter::is_too_large_to_archive(rr)) { 695 bool success = HeapShared::archive_reachable_objects_from(1, _default_subgraph_info, rr); 696 assert(success, "must be"); 697 int root_index = append_root(rr); 698 ik->constants()->cache()->set_archived_references(root_index); 699 } 700 } 701 } 702 } 703 } 704 705 void HeapShared::archive_strings() { 706 oop shared_strings_array = StringTable::init_shared_table(_dumped_interned_strings); 707 bool success = archive_reachable_objects_from(1, _default_subgraph_info, shared_strings_array); 708 // We must succeed because: 709 // - _dumped_interned_strings do not contain any large strings. 710 // - StringTable::init_shared_table() doesn't create any large arrays. 711 assert(success, "shared strings array must not point to arrays or strings that are too large to archive"); 712 StringTable::set_shared_strings_array_index(append_root(shared_strings_array)); 713 } 714 715 int HeapShared::archive_exception_instance(oop exception) { 716 bool success = archive_reachable_objects_from(1, _default_subgraph_info, exception); 717 assert(success, "sanity"); 718 return append_root(exception); 719 } 720 721 void HeapShared::mark_native_pointers(oop orig_obj) { 722 if (java_lang_Class::is_instance(orig_obj)) { 723 ArchiveHeapWriter::mark_native_pointer(orig_obj, java_lang_Class::klass_offset()); 724 ArchiveHeapWriter::mark_native_pointer(orig_obj, java_lang_Class::array_klass_offset()); 725 } else if (java_lang_invoke_ResolvedMethodName::is_instance(orig_obj)) { 726 ArchiveHeapWriter::mark_native_pointer(orig_obj, java_lang_invoke_ResolvedMethodName::vmtarget_offset()); 727 } 728 } 729 730 void HeapShared::get_pointer_info(oop src_obj, bool& has_oop_pointers, bool& has_native_pointers) { 731 CachedOopInfo* info = archived_object_cache()->get(src_obj); 732 assert(info != nullptr, "must be"); 733 has_oop_pointers = info->has_oop_pointers(); 734 has_native_pointers = info->has_native_pointers(); 735 } 736 737 void HeapShared::set_has_native_pointers(oop src_obj) { 738 CachedOopInfo* info = archived_object_cache()->get(src_obj); 739 assert(info != nullptr, "must be"); 740 info->set_has_native_pointers(); 741 } 742 743 void HeapShared::start_finding_archivable_hidden_classes() { 744 NoSafepointVerifier nsv; 745 746 init_seen_objects_table(); 747 748 find_archivable_hidden_classes_helper(archive_subgraph_entry_fields); 749 if (CDSConfig::is_dumping_full_module_graph()) { 750 find_archivable_hidden_classes_helper(fmg_archive_subgraph_entry_fields); 751 } 752 } 753 754 void HeapShared::end_finding_archivable_hidden_classes() { 755 NoSafepointVerifier nsv; 756 757 delete_seen_objects_table(); 758 } 759 760 void HeapShared::find_archivable_hidden_classes_helper(ArchivableStaticFieldInfo fields[]) { 761 if (!CDSConfig::is_dumping_heap()) { 762 return; 763 } 764 int i; 765 for (int i = 0; fields[i].valid(); ) { 766 ArchivableStaticFieldInfo* info = &fields[i]; 767 const char* klass_name = info->klass_name; 768 for (; fields[i].valid(); i++) { 769 ArchivableStaticFieldInfo* f = &fields[i]; 770 if (f->klass_name != klass_name) { 771 break; 772 } 773 774 InstanceKlass* k = f->klass; 775 oop m = k->java_mirror(); 776 oop o = m->obj_field(f->offset); 777 if (o != nullptr) { 778 find_archivable_hidden_classes_in_object(o); 779 } 780 } 781 } 782 } 783 784 class HeapShared::FindHiddenClassesOopClosure: public BasicOopIterateClosure { 785 GrowableArray<oop> _stack; 786 template <class T> void do_oop_work(T *p) { 787 // Recurse on a GrowableArray to avoid overflowing the C stack. 788 oop o = RawAccess<>::oop_load(p); 789 if (o != nullptr) { 790 _stack.append(o); 791 } 792 } 793 794 public: 795 796 void do_oop(narrowOop *p) { FindHiddenClassesOopClosure::do_oop_work(p); } 797 void do_oop( oop *p) { FindHiddenClassesOopClosure::do_oop_work(p); } 798 799 FindHiddenClassesOopClosure(oop o) { 800 _stack.append(o); 801 } 802 oop pop() { 803 if (_stack.length() == 0) { 804 return nullptr; 805 } else { 806 return _stack.pop(); 807 } 808 } 809 }; 810 811 void HeapShared::find_archivable_hidden_classes_in_object(oop root) { 812 ResourceMark rm; 813 FindHiddenClassesOopClosure c(root); 814 oop o; 815 while ((o = c.pop()) != nullptr) { 816 if (!has_been_seen_during_subgraph_recording(o)) { 817 set_has_been_seen_during_subgraph_recording(o); 818 819 if (java_lang_Class::is_instance(o)) { 820 Klass* k = java_lang_Class::as_Klass(o); 821 if (k != nullptr && k->is_instance_klass()) { 822 SystemDictionaryShared::mark_required_class(InstanceKlass::cast(k)); 823 } 824 } else if (java_lang_invoke_ResolvedMethodName::is_instance(o)) { 825 Method* m = java_lang_invoke_ResolvedMethodName::vmtarget(o); 826 if (m != nullptr && m->method_holder() != nullptr) { 827 SystemDictionaryShared::mark_required_class(m->method_holder()); 828 } 829 } 830 831 o->oop_iterate(&c); 832 } 833 } 834 } 835 836 void HeapShared::archive_objects(ArchiveHeapInfo *heap_info) { 837 { 838 NoSafepointVerifier nsv; 839 840 _default_subgraph_info = init_subgraph_info(vmClasses::Object_klass(), false); 841 _trace = new GrowableArrayCHeap<oop, mtClassShared>(250); 842 _context = new GrowableArrayCHeap<const char*, mtClassShared>(250); 843 844 // Cache for recording where the archived objects are copied to 845 create_archived_object_cache(); 846 847 if (UseCompressedOops || UseG1GC) { 848 log_info(cds)("Heap range = [" PTR_FORMAT " - " PTR_FORMAT "]", 849 UseCompressedOops ? p2i(CompressedOops::begin()) : 850 p2i((address)G1CollectedHeap::heap()->reserved().start()), 851 UseCompressedOops ? p2i(CompressedOops::end()) : 852 p2i((address)G1CollectedHeap::heap()->reserved().end())); 853 } 854 copy_objects(); 855 856 if (!SkipArchiveHeapVerification) { 857 CDSHeapVerifier::verify(); 858 } 859 check_default_subgraph_classes(); 860 } 861 862 ArchiveHeapWriter::write(_pending_roots, heap_info); 863 } 864 865 void HeapShared::copy_interned_strings() { 866 init_seen_objects_table(); 867 868 auto copier = [&] (oop s, bool value_ignored) { 869 assert(s != nullptr, "sanity"); 870 assert(!ArchiveHeapWriter::is_string_too_large_to_archive(s), "large strings must have been filtered"); 871 bool success = archive_reachable_objects_from(1, _default_subgraph_info, s); 872 assert(success, "must be"); 873 // Prevent string deduplication from changing the value field to 874 // something not in the archive. 875 java_lang_String::set_deduplication_forbidden(s); 876 }; 877 _dumped_interned_strings->iterate_all(copier); 878 879 delete_seen_objects_table(); 880 } 881 882 void HeapShared::copy_special_objects() { 883 // Archive special objects that do not belong to any subgraphs 884 init_seen_objects_table(); 885 archive_java_mirrors(); 886 archive_strings(); 887 Universe::archive_exception_instances(); 888 delete_seen_objects_table(); 889 } 890 891 void HeapShared::copy_objects() { 892 assert(HeapShared::can_write(), "must be"); 893 894 copy_interned_strings(); 895 copy_special_objects(); 896 897 archive_object_subgraphs(archive_subgraph_entry_fields, 898 false /* is_full_module_graph */); 899 900 if (CDSConfig::is_dumping_full_module_graph()) { 901 archive_object_subgraphs(fmg_archive_subgraph_entry_fields, 902 true /* is_full_module_graph */); 903 Modules::verify_archived_modules(); 904 } 905 } 906 907 // 908 // Subgraph archiving support 909 // 910 HeapShared::DumpTimeKlassSubGraphInfoTable* HeapShared::_dump_time_subgraph_info_table = nullptr; 911 HeapShared::RunTimeKlassSubGraphInfoTable HeapShared::_run_time_subgraph_info_table; 912 913 // Get the subgraph_info for Klass k. A new subgraph_info is created if 914 // there is no existing one for k. The subgraph_info records the "buffered" 915 // address of the class. 916 KlassSubGraphInfo* HeapShared::init_subgraph_info(Klass* k, bool is_full_module_graph) { 917 assert(CDSConfig::is_dumping_heap(), "dump time only"); 918 bool created; 919 Klass* buffered_k = ArchiveBuilder::get_buffered_klass(k); 920 KlassSubGraphInfo* info = 921 _dump_time_subgraph_info_table->put_if_absent(k, KlassSubGraphInfo(buffered_k, is_full_module_graph), 922 &created); 923 assert(created, "must not initialize twice"); 924 return info; 925 } 926 927 KlassSubGraphInfo* HeapShared::get_subgraph_info(Klass* k) { 928 assert(CDSConfig::is_dumping_heap(), "dump time only"); 929 KlassSubGraphInfo* info = _dump_time_subgraph_info_table->get(k); 930 assert(info != nullptr, "must have been initialized"); 931 return info; 932 } 933 934 // Add an entry field to the current KlassSubGraphInfo. 935 void KlassSubGraphInfo::add_subgraph_entry_field(int static_field_offset, oop v) { 936 assert(CDSConfig::is_dumping_heap(), "dump time only"); 937 if (_subgraph_entry_fields == nullptr) { 938 _subgraph_entry_fields = 939 new (mtClass) GrowableArray<int>(10, mtClass); 940 } 941 _subgraph_entry_fields->append(static_field_offset); 942 _subgraph_entry_fields->append(HeapShared::append_root(v)); 943 } 944 945 // Add the Klass* for an object in the current KlassSubGraphInfo's subgraphs. 946 // Only objects of boot classes can be included in sub-graph. 947 void KlassSubGraphInfo::add_subgraph_object_klass(Klass* orig_k) { 948 assert(CDSConfig::is_dumping_heap(), "dump time only"); 949 Klass* buffered_k = ArchiveBuilder::get_buffered_klass(orig_k); 950 951 if (_subgraph_object_klasses == nullptr) { 952 _subgraph_object_klasses = 953 new (mtClass) GrowableArray<Klass*>(50, mtClass); 954 } 955 956 assert(ArchiveBuilder::current()->is_in_buffer_space(buffered_k), "must be a shared class"); 957 958 if (_k == buffered_k) { 959 // Don't add the Klass containing the sub-graph to it's own klass 960 // initialization list. 961 return; 962 } 963 964 if (buffered_k->is_instance_klass()) { 965 if (!CDSConfig::is_dumping_invokedynamic()) { 966 // FIXME: this supports Lambda Proxy classes 967 assert(InstanceKlass::cast(buffered_k)->is_shared_boot_class(), 968 "must be boot class"); 969 } 970 // vmClasses::xxx_klass() are not updated, need to check 971 // the original Klass* 972 if (orig_k == vmClasses::String_klass() || 973 orig_k == vmClasses::Object_klass()) { 974 // Initialized early during VM initialization. No need to be added 975 // to the sub-graph object class list. 976 return; 977 } 978 check_allowed_klass(InstanceKlass::cast(orig_k)); 979 } else if (buffered_k->is_objArray_klass()) { 980 Klass* abk = ObjArrayKlass::cast(buffered_k)->bottom_klass(); 981 if (abk->is_instance_klass()) { 982 assert(InstanceKlass::cast(abk)->is_shared_boot_class(), 983 "must be boot class"); 984 check_allowed_klass(InstanceKlass::cast(ObjArrayKlass::cast(orig_k)->bottom_klass())); 985 } 986 if (buffered_k == Universe::objectArrayKlass()) { 987 // Initialized early during Universe::genesis. No need to be added 988 // to the list. 989 return; 990 } 991 } else { 992 assert(buffered_k->is_typeArray_klass(), "must be"); 993 // Primitive type arrays are created early during Universe::genesis. 994 return; 995 } 996 997 if (log_is_enabled(Debug, cds, heap)) { 998 if (!_subgraph_object_klasses->contains(buffered_k)) { 999 ResourceMark rm; 1000 log_debug(cds, heap)("Adding klass %s", orig_k->external_name()); 1001 } 1002 } 1003 1004 _subgraph_object_klasses->append_if_missing(buffered_k); 1005 _has_non_early_klasses |= is_non_early_klass(orig_k); 1006 } 1007 1008 void KlassSubGraphInfo::check_allowed_klass(InstanceKlass* ik) { 1009 if (CDSConfig::is_dumping_invokedynamic()) { 1010 // FIXME -- this allows LambdaProxy classes 1011 return; 1012 } 1013 if (ik->module()->name() == vmSymbols::java_base()) { 1014 assert(ik->package() != nullptr, "classes in java.base cannot be in unnamed package"); 1015 return; 1016 } 1017 1018 #ifndef PRODUCT 1019 if (!ik->module()->is_named() && ik->package() == nullptr) { 1020 // This class is loaded by ArchiveHeapTestClass 1021 return; 1022 } 1023 const char* extra_msg = ", or in an unnamed package of an unnamed module"; 1024 #else 1025 const char* extra_msg = ""; 1026 #endif 1027 1028 ResourceMark rm; 1029 log_error(cds, heap)("Class %s not allowed in archive heap. Must be in java.base%s", 1030 ik->external_name(), extra_msg); 1031 MetaspaceShared::unrecoverable_writing_error(); 1032 } 1033 1034 bool KlassSubGraphInfo::is_non_early_klass(Klass* k) { 1035 if (k->is_objArray_klass()) { 1036 k = ObjArrayKlass::cast(k)->bottom_klass(); 1037 } 1038 if (k->is_instance_klass()) { 1039 if (!SystemDictionaryShared::is_early_klass(InstanceKlass::cast(k))) { 1040 ResourceMark rm; 1041 log_info(cds, heap)("non-early: %s", k->external_name()); 1042 return true; 1043 } else { 1044 return false; 1045 } 1046 } else { 1047 return false; 1048 } 1049 } 1050 1051 // Initialize an archived subgraph_info_record from the given KlassSubGraphInfo. 1052 void ArchivedKlassSubGraphInfoRecord::init(KlassSubGraphInfo* info) { 1053 _k = info->klass(); 1054 _entry_field_records = nullptr; 1055 _subgraph_object_klasses = nullptr; 1056 _is_full_module_graph = info->is_full_module_graph(); 1057 1058 if (_is_full_module_graph) { 1059 // Consider all classes referenced by the full module graph as early -- we will be 1060 // allocating objects of these classes during JVMTI early phase, so they cannot 1061 // be processed by (non-early) JVMTI ClassFileLoadHook 1062 _has_non_early_klasses = false; 1063 } else { 1064 _has_non_early_klasses = info->has_non_early_klasses(); 1065 } 1066 1067 if (_has_non_early_klasses) { 1068 ResourceMark rm; 1069 log_info(cds, heap)( 1070 "Subgraph of klass %s has non-early klasses and cannot be used when JVMTI ClassFileLoadHook is enabled", 1071 _k->external_name()); 1072 } 1073 1074 // populate the entry fields 1075 GrowableArray<int>* entry_fields = info->subgraph_entry_fields(); 1076 if (entry_fields != nullptr) { 1077 int num_entry_fields = entry_fields->length(); 1078 assert(num_entry_fields % 2 == 0, "sanity"); 1079 _entry_field_records = 1080 ArchiveBuilder::new_ro_array<int>(num_entry_fields); 1081 for (int i = 0 ; i < num_entry_fields; i++) { 1082 _entry_field_records->at_put(i, entry_fields->at(i)); 1083 } 1084 } 1085 1086 // the Klasses of the objects in the sub-graphs 1087 GrowableArray<Klass*>* subgraph_object_klasses = info->subgraph_object_klasses(); 1088 if (subgraph_object_klasses != nullptr) { 1089 int num_subgraphs_klasses = subgraph_object_klasses->length(); 1090 _subgraph_object_klasses = 1091 ArchiveBuilder::new_ro_array<Klass*>(num_subgraphs_klasses); 1092 for (int i = 0; i < num_subgraphs_klasses; i++) { 1093 Klass* subgraph_k = subgraph_object_klasses->at(i); 1094 if (log_is_enabled(Info, cds, heap)) { 1095 ResourceMark rm; 1096 log_info(cds, heap)( 1097 "Archived object klass %s (%2d) => %s", 1098 _k->external_name(), i, subgraph_k->external_name()); 1099 } 1100 _subgraph_object_klasses->at_put(i, subgraph_k); 1101 ArchivePtrMarker::mark_pointer(_subgraph_object_klasses->adr_at(i)); 1102 } 1103 } 1104 1105 ArchivePtrMarker::mark_pointer(&_k); 1106 ArchivePtrMarker::mark_pointer(&_entry_field_records); 1107 ArchivePtrMarker::mark_pointer(&_subgraph_object_klasses); 1108 } 1109 1110 class HeapShared::CopyKlassSubGraphInfoToArchive : StackObj { 1111 public: 1112 CompactHashtableWriter* _writer; 1113 CopyKlassSubGraphInfoToArchive(CompactHashtableWriter* writer) : _writer(writer) {} 1114 1115 bool do_entry(Klass* klass, KlassSubGraphInfo& info) { 1116 if (info.subgraph_object_klasses() != nullptr || info.subgraph_entry_fields() != nullptr) { 1117 ArchivedKlassSubGraphInfoRecord* record = HeapShared::archive_subgraph_info(&info); 1118 Klass* buffered_k = ArchiveBuilder::get_buffered_klass(klass); 1119 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary((address)buffered_k); 1120 u4 delta = ArchiveBuilder::current()->any_to_offset_u4(record); 1121 _writer->add(hash, delta); 1122 } 1123 return true; // keep on iterating 1124 } 1125 }; 1126 1127 ArchivedKlassSubGraphInfoRecord* HeapShared::archive_subgraph_info(KlassSubGraphInfo* info) { 1128 ArchivedKlassSubGraphInfoRecord* record = 1129 (ArchivedKlassSubGraphInfoRecord*)ArchiveBuilder::ro_region_alloc(sizeof(ArchivedKlassSubGraphInfoRecord)); 1130 record->init(info); 1131 return record; 1132 } 1133 1134 // Build the records of archived subgraph infos, which include: 1135 // - Entry points to all subgraphs from the containing class mirror. The entry 1136 // points are static fields in the mirror. For each entry point, the field 1137 // offset, and value are recorded in the sub-graph 1138 // info. The value is stored back to the corresponding field at runtime. 1139 // - A list of klasses that need to be loaded/initialized before archived 1140 // java object sub-graph can be accessed at runtime. 1141 void HeapShared::write_subgraph_info_table() { 1142 // Allocate the contents of the hashtable(s) inside the RO region of the CDS archive. 1143 DumpTimeKlassSubGraphInfoTable* d_table = _dump_time_subgraph_info_table; 1144 CompactHashtableStats stats; 1145 1146 _run_time_subgraph_info_table.reset(); 1147 1148 CompactHashtableWriter writer(d_table->_count, &stats); 1149 CopyKlassSubGraphInfoToArchive copy(&writer); 1150 d_table->iterate(©); 1151 writer.dump(&_run_time_subgraph_info_table, "subgraphs"); 1152 1153 _runtime_default_subgraph_info = archive_subgraph_info(_default_subgraph_info); 1154 1155 #ifndef PRODUCT 1156 if (ArchiveHeapTestClass != nullptr) { 1157 size_t len = strlen(ArchiveHeapTestClass) + 1; 1158 Array<char>* array = ArchiveBuilder::new_ro_array<char>((int)len); 1159 strncpy(array->adr_at(0), ArchiveHeapTestClass, len); 1160 _archived_ArchiveHeapTestClass = array; 1161 } 1162 #endif 1163 if (log_is_enabled(Info, cds, heap)) { 1164 print_stats(); 1165 } 1166 } 1167 1168 void HeapShared::serialize_misc_info(SerializeClosure* soc) { 1169 soc->do_int(&_permobj_segments); 1170 } 1171 1172 void HeapShared::init_roots(oop roots_oop) { 1173 if (roots_oop != nullptr) { 1174 assert(ArchiveHeapLoader::is_in_use(), "must be"); 1175 _roots = OopHandle(Universe::vm_global(), roots_oop); 1176 } 1177 } 1178 1179 void HeapShared::serialize_tables(SerializeClosure* soc) { 1180 1181 #ifndef PRODUCT 1182 soc->do_ptr(&_archived_ArchiveHeapTestClass); 1183 if (soc->reading() && _archived_ArchiveHeapTestClass != nullptr) { 1184 _test_class_name = _archived_ArchiveHeapTestClass->adr_at(0); 1185 setup_test_class(_test_class_name); 1186 } 1187 #endif 1188 1189 _run_time_subgraph_info_table.serialize_header(soc); 1190 soc->do_ptr(&_runtime_default_subgraph_info); 1191 1192 } 1193 1194 static void verify_the_heap(Klass* k, const char* which) { 1195 if (VerifyArchivedFields > 0) { 1196 ResourceMark rm; 1197 log_info(cds, heap)("Verify heap %s initializing static field(s) in %s", 1198 which, k->external_name()); 1199 1200 VM_Verify verify_op; 1201 VMThread::execute(&verify_op); 1202 1203 if (VerifyArchivedFields > 1 && is_init_completed()) { 1204 // At this time, the oop->klass() of some archived objects in the heap may not 1205 // have been loaded into the system dictionary yet. Nevertheless, oop->klass() should 1206 // have enough information (object size, oop maps, etc) so that a GC can be safely 1207 // performed. 1208 // 1209 // -XX:VerifyArchivedFields=2 force a GC to happen in such an early stage 1210 // to check for GC safety. 1211 log_info(cds, heap)("Trigger GC %s initializing static field(s) in %s", 1212 which, k->external_name()); 1213 FlagSetting fs1(VerifyBeforeGC, true); 1214 FlagSetting fs2(VerifyDuringGC, true); 1215 FlagSetting fs3(VerifyAfterGC, true); 1216 Universe::heap()->collect(GCCause::_java_lang_system_gc); 1217 } 1218 } 1219 } 1220 1221 // Before GC can execute, we must ensure that all oops reachable from HeapShared::roots() 1222 // have a valid klass. I.e., oopDesc::klass() must have already been resolved. 1223 // 1224 // Note: if a ArchivedKlassSubGraphInfoRecord contains non-early classes, and JVMTI 1225 // ClassFileLoadHook is enabled, it's possible for this class to be dynamically replaced. In 1226 // this case, we will not load the ArchivedKlassSubGraphInfoRecord and will clear its roots. 1227 void HeapShared::resolve_classes(JavaThread* current) { 1228 assert(CDSConfig::is_using_archive(), "runtime only!"); 1229 if (!ArchiveHeapLoader::is_in_use()) { 1230 return; // nothing to do 1231 } 1232 1233 if (!CDSConfig::has_preloaded_classes()) { 1234 assert( _runtime_default_subgraph_info != nullptr, "must be"); 1235 Array<Klass*>* klasses = _runtime_default_subgraph_info->subgraph_object_klasses(); 1236 if (klasses != nullptr) { 1237 for (int i = 0; i < klasses->length(); i++) { 1238 Klass* k = klasses->at(i); 1239 ExceptionMark em(current); // no exception can happen here 1240 resolve_or_init(k, /*do_init*/false, current); 1241 } 1242 } 1243 } 1244 1245 resolve_classes_for_subgraphs(current, archive_subgraph_entry_fields); 1246 resolve_classes_for_subgraphs(current, fmg_archive_subgraph_entry_fields); 1247 } 1248 1249 void HeapShared::resolve_classes_for_subgraphs(JavaThread* current, ArchivableStaticFieldInfo fields[]) { 1250 for (int i = 0; fields[i].valid(); i++) { 1251 ArchivableStaticFieldInfo* info = &fields[i]; 1252 TempNewSymbol klass_name = SymbolTable::new_symbol(info->klass_name); 1253 InstanceKlass* k = SystemDictionaryShared::find_builtin_class(klass_name); 1254 assert(k != nullptr && k->is_shared_boot_class(), "sanity"); 1255 resolve_classes_for_subgraph_of(current, k); 1256 } 1257 } 1258 1259 void HeapShared::resolve_classes_for_subgraph_of(JavaThread* current, Klass* k) { 1260 JavaThread* THREAD = current; 1261 ExceptionMark em(THREAD); 1262 const ArchivedKlassSubGraphInfoRecord* record = 1263 resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/false, THREAD); 1264 if (HAS_PENDING_EXCEPTION) { 1265 CLEAR_PENDING_EXCEPTION; 1266 } 1267 if (record == nullptr) { 1268 clear_archived_roots_of(k); 1269 } 1270 } 1271 1272 void HeapShared::initialize_java_lang_invoke(TRAPS) { 1273 if (CDSConfig::is_loading_invokedynamic() || CDSConfig::is_dumping_invokedynamic()) { 1274 resolve_or_init("java/lang/invoke/Invokers$Holder", true, CHECK); 1275 resolve_or_init("java/lang/invoke/MethodHandle", true, CHECK); 1276 resolve_or_init("java/lang/invoke/MethodHandleNatives", true, CHECK); 1277 resolve_or_init("java/lang/invoke/DirectMethodHandle$Holder", true, CHECK); 1278 resolve_or_init("java/lang/invoke/DelegatingMethodHandle$Holder", true, CHECK); 1279 resolve_or_init("java/lang/invoke/LambdaForm$Holder", true, CHECK); 1280 resolve_or_init("java/lang/invoke/BoundMethodHandle$Species_L", true, CHECK); 1281 } 1282 } 1283 1284 void HeapShared::initialize_default_subgraph_classes(Handle loader, TRAPS) { 1285 if (!ArchiveHeapLoader::is_in_use()) { 1286 return; 1287 } 1288 1289 assert( _runtime_default_subgraph_info != nullptr, "must be"); 1290 Array<Klass*>* klasses = _runtime_default_subgraph_info->subgraph_object_klasses(); 1291 if (klasses != nullptr) { 1292 for (int pass = 0; pass < 2; pass ++) { 1293 for (int i = 0; i < klasses->length(); i++) { 1294 Klass* k = klasses->at(i); 1295 if (k->class_loader_data() == nullptr) { 1296 // This class is not yet loaded. We will initialize it in a later phase. 1297 continue; 1298 } 1299 if (k->class_loader() == loader()) { 1300 if (pass == 0) { 1301 if (k->is_instance_klass()) { 1302 InstanceKlass::cast(k)->link_class(CHECK); 1303 } 1304 } else { 1305 resolve_or_init(k, /*do_init*/true, CHECK); 1306 } 1307 } 1308 } 1309 } 1310 } 1311 } 1312 1313 void HeapShared::initialize_from_archived_subgraph(JavaThread* current, Klass* k) { 1314 JavaThread* THREAD = current; 1315 if (!ArchiveHeapLoader::is_in_use()) { 1316 return; // nothing to do 1317 } 1318 1319 ExceptionMark em(THREAD); 1320 const ArchivedKlassSubGraphInfoRecord* record = 1321 resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/true, THREAD); 1322 1323 if (HAS_PENDING_EXCEPTION) { 1324 CLEAR_PENDING_EXCEPTION; 1325 // None of the field value will be set if there was an exception when initializing the classes. 1326 // The java code will not see any of the archived objects in the 1327 // subgraphs referenced from k in this case. 1328 return; 1329 } 1330 1331 if (record != nullptr) { 1332 init_archived_fields_for(k, record); 1333 } 1334 } 1335 1336 const ArchivedKlassSubGraphInfoRecord* 1337 HeapShared::resolve_or_init_classes_for_subgraph_of(Klass* k, bool do_init, TRAPS) { 1338 assert(!CDSConfig::is_dumping_heap(), "Should not be called when dumping heap"); 1339 1340 if (!k->is_shared()) { 1341 return nullptr; 1342 } 1343 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(k); 1344 const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0); 1345 1346 #ifndef PRODUCT 1347 if (_test_class_name != nullptr && k->name()->equals(_test_class_name) && record != nullptr) { 1348 _test_class = k; 1349 _test_class_record = record; 1350 } 1351 #endif 1352 1353 // Initialize from archived data. Currently this is done only 1354 // during VM initialization time. No lock is needed. 1355 if (record == nullptr) { 1356 if (log_is_enabled(Info, cds, heap)) { 1357 ResourceMark rm(THREAD); 1358 log_info(cds, heap)("subgraph %s is not recorded", 1359 k->external_name()); 1360 } 1361 return nullptr; 1362 } else { 1363 if (record->is_full_module_graph() && !CDSConfig::is_using_full_module_graph()) { 1364 if (log_is_enabled(Info, cds, heap)) { 1365 ResourceMark rm(THREAD); 1366 log_info(cds, heap)("subgraph %s cannot be used because full module graph is disabled", 1367 k->external_name()); 1368 } 1369 return nullptr; 1370 } 1371 1372 if (record->has_non_early_klasses() && JvmtiExport::should_post_class_file_load_hook()) { 1373 if (log_is_enabled(Info, cds, heap)) { 1374 ResourceMark rm(THREAD); 1375 log_info(cds, heap)("subgraph %s cannot be used because JVMTI ClassFileLoadHook is enabled", 1376 k->external_name()); 1377 } 1378 return nullptr; 1379 } 1380 1381 if (log_is_enabled(Info, cds, heap)) { 1382 ResourceMark rm; 1383 log_info(cds, heap)("%s subgraph %s ", do_init ? "init" : "resolve", k->external_name()); 1384 } 1385 1386 if (do_init && k->name() == vmSymbols::java_lang_invoke_MethodType()) { 1387 // FIXME - hack. 1388 // 1389 // (The real fix would be to archive the MethodType class in its already initialized state. That 1390 // way we don't need to re-execute the <clinit> methods) 1391 // 1392 // We need to do this to break a cycle in the way the archived subgraphs are restored. Without this block, we 1393 // have the following sequence 1394 // 1395 // MethodType.<clinit>() 1396 // -> CDS.initializeFromArchive(MethodType.class); 1397 // -> (this "if" block) 1398 // -> resolve_or_init("MethodType", ...); // this does nothing because MethodType.<clinit> is already executing 1399 // -> resolve_or_init("DirectMethodHandle", ...); // this class is in record->subgraph_object_klasses(); 1400 // -> DirectMethodHandle.<clinit>() 1401 // -> MethodType.methodType() 1402 // -> MethodType.genericMethodType() 1403 // -> aaload MethodType.objectOnlyTypes[n]; <<<< here 1404 // 1405 // We need to restore MethodType.objectOnlyTypes here, or else the above aaload will 1406 // get an NPE. 1407 Array<int>* entry_field_records = record->entry_field_records(); 1408 assert(entry_field_records != nullptr, "must be"); 1409 int efr_len = entry_field_records->length(); 1410 assert(efr_len == 2, "must be"); 1411 int root_index = entry_field_records->at(1); 1412 oop obj = get_root(root_index, /*clear=*/false); 1413 if (obj != nullptr) { 1414 objArrayOop archivedObjects = objArrayOop(obj); 1415 InstanceKlass* ik = InstanceKlass::cast(k); 1416 oop m = ik->java_mirror(); 1417 1418 { 1419 fieldDescriptor fd; 1420 TempNewSymbol name = SymbolTable::new_symbol("archivedMethodTypes"); 1421 TempNewSymbol sig = SymbolTable::new_symbol("Ljava/util/HashMap;"); 1422 Klass* result = ik->find_field(name, sig, true, &fd); 1423 assert(result != nullptr, "must be"); 1424 m->obj_field_put(fd.offset(), archivedObjects->obj_at(0)); 1425 } 1426 1427 { 1428 fieldDescriptor fd; 1429 TempNewSymbol name = SymbolTable::new_symbol("objectOnlyTypes"); 1430 TempNewSymbol sig = SymbolTable::new_symbol("[Ljava/lang/invoke/MethodType;"); 1431 Klass* result = ik->find_field(name, sig, true, &fd); 1432 assert(result != nullptr, "must be"); 1433 m->obj_field_put(fd.offset(), archivedObjects->obj_at(1)); 1434 } 1435 } 1436 } 1437 1438 resolve_or_init(k, do_init, CHECK_NULL); 1439 1440 // Load/link/initialize the klasses of the objects in the subgraph. 1441 // nullptr class loader is used. 1442 Array<Klass*>* klasses = record->subgraph_object_klasses(); 1443 if (klasses != nullptr) { 1444 for (int i = 0; i < klasses->length(); i++) { 1445 Klass* klass = klasses->at(i); 1446 if (!klass->is_shared()) { 1447 return nullptr; 1448 } 1449 resolve_or_init(klass, do_init, CHECK_NULL); 1450 } 1451 } 1452 } 1453 1454 return record; 1455 } 1456 1457 void HeapShared::resolve_or_init(const char* klass_name, bool do_init, TRAPS) { 1458 TempNewSymbol klass_name_sym = SymbolTable::new_symbol(klass_name); 1459 InstanceKlass* k = SystemDictionaryShared::find_builtin_class(klass_name_sym); 1460 if (k == nullptr) { 1461 return; 1462 } 1463 assert(k->is_shared_boot_class(), "sanity"); 1464 resolve_or_init(k, false, CHECK); 1465 if (do_init) { 1466 resolve_or_init(k, true, CHECK); 1467 } 1468 } 1469 1470 void HeapShared::resolve_or_init(Klass* k, bool do_init, TRAPS) { 1471 if (!do_init) { 1472 if (k->class_loader_data() == nullptr) { 1473 Klass* resolved_k = SystemDictionary::resolve_or_null(k->name(), CHECK); 1474 assert(resolved_k == k, "classes used by archived heap must not be replaced by JVMTI ClassFileLoadHook"); 1475 } 1476 } else { 1477 assert(k->class_loader_data() != nullptr, "must have been resolved by HeapShared::resolve_classes"); 1478 if (k->is_instance_klass()) { 1479 InstanceKlass* ik = InstanceKlass::cast(k); 1480 ik->initialize(CHECK); 1481 } else if (k->is_objArray_klass()) { 1482 ObjArrayKlass* oak = ObjArrayKlass::cast(k); 1483 oak->initialize(CHECK); 1484 } 1485 } 1486 } 1487 1488 void HeapShared::init_archived_fields_for(Klass* k, const ArchivedKlassSubGraphInfoRecord* record) { 1489 verify_the_heap(k, "before"); 1490 1491 // Load the subgraph entry fields from the record and store them back to 1492 // the corresponding fields within the mirror. 1493 oop m = k->java_mirror(); 1494 Array<int>* entry_field_records = record->entry_field_records(); 1495 if (entry_field_records != nullptr) { 1496 int efr_len = entry_field_records->length(); 1497 assert(efr_len % 2 == 0, "sanity"); 1498 for (int i = 0; i < efr_len; i += 2) { 1499 int field_offset = entry_field_records->at(i); 1500 int root_index = entry_field_records->at(i+1); 1501 oop v = get_root(root_index, /*clear=*/true); 1502 m->obj_field_put(field_offset, v); 1503 log_debug(cds, heap)(" " PTR_FORMAT " init field @ %2d = " PTR_FORMAT, p2i(k), field_offset, p2i(v)); 1504 } 1505 1506 // Done. Java code can see the archived sub-graphs referenced from k's 1507 // mirror after this point. 1508 if (log_is_enabled(Info, cds, heap)) { 1509 ResourceMark rm; 1510 log_info(cds, heap)("initialize_from_archived_subgraph %s " PTR_FORMAT "%s", 1511 k->external_name(), p2i(k), JvmtiExport::is_early_phase() ? " (early)" : ""); 1512 } 1513 } 1514 1515 verify_the_heap(k, "after "); 1516 } 1517 1518 void HeapShared::clear_archived_roots_of(Klass* k) { 1519 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(k); 1520 const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0); 1521 if (record != nullptr) { 1522 Array<int>* entry_field_records = record->entry_field_records(); 1523 if (entry_field_records != nullptr) { 1524 int efr_len = entry_field_records->length(); 1525 assert(efr_len % 2 == 0, "sanity"); 1526 for (int i = 0; i < efr_len; i += 2) { 1527 int root_index = entry_field_records->at(i+1); 1528 clear_root(root_index); 1529 } 1530 } 1531 } 1532 } 1533 1534 class WalkOopAndArchiveClosure: public BasicOopIterateClosure { 1535 int _level; 1536 bool _record_klasses_only; 1537 KlassSubGraphInfo* _subgraph_info; 1538 oop _referencing_obj; 1539 // The following are for maintaining a stack for determining 1540 // CachedOopInfo::_referrer 1541 static WalkOopAndArchiveClosure* _current; 1542 WalkOopAndArchiveClosure* _last; 1543 public: 1544 WalkOopAndArchiveClosure(int level, 1545 bool record_klasses_only, 1546 KlassSubGraphInfo* subgraph_info, 1547 oop orig) : 1548 _level(level), 1549 _record_klasses_only(record_klasses_only), 1550 _subgraph_info(subgraph_info), 1551 _referencing_obj(orig) { 1552 _last = _current; 1553 _current = this; 1554 } 1555 ~WalkOopAndArchiveClosure() { 1556 _current = _last; 1557 } 1558 void do_oop(narrowOop *p) { WalkOopAndArchiveClosure::do_oop_work(p); } 1559 void do_oop( oop *p) { WalkOopAndArchiveClosure::do_oop_work(p); } 1560 1561 protected: 1562 template <class T> void do_oop_work(T *p) { 1563 oop obj = RawAccess<>::oop_load(p); 1564 if (!CompressedOops::is_null(obj)) { 1565 size_t field_delta = pointer_delta(p, _referencing_obj, sizeof(char)); 1566 if (!_record_klasses_only && log_is_enabled(Debug, cds, heap)) { 1567 ResourceMark rm; 1568 log_debug(cds, heap)("(%d) %s[" SIZE_FORMAT "] ==> " PTR_FORMAT " size " SIZE_FORMAT " %s", _level, 1569 _referencing_obj->klass()->external_name(), field_delta, 1570 p2i(obj), obj->size() * HeapWordSize, obj->klass()->external_name()); 1571 if (log_is_enabled(Trace, cds, heap)) { 1572 LogTarget(Trace, cds, heap) log; 1573 LogStream out(log); 1574 obj->print_on(&out); 1575 } 1576 } 1577 1578 bool success = HeapShared::archive_reachable_objects_from( 1579 _level + 1, _subgraph_info, obj); 1580 assert(success, "VM should have exited with unarchivable objects for _level > 1"); 1581 } 1582 } 1583 1584 public: 1585 static WalkOopAndArchiveClosure* current() { return _current; } 1586 oop referencing_obj() { return _referencing_obj; } 1587 KlassSubGraphInfo* subgraph_info() { return _subgraph_info; } 1588 }; 1589 1590 WalkOopAndArchiveClosure* WalkOopAndArchiveClosure::_current = nullptr; 1591 1592 // Checks if an oop has any non-null oop fields 1593 class PointsToOopsChecker : public BasicOopIterateClosure { 1594 bool _result; 1595 1596 template <class T> void check(T *p) { 1597 _result |= (HeapAccess<>::oop_load(p) != nullptr); 1598 } 1599 1600 public: 1601 PointsToOopsChecker() : _result(false) {} 1602 void do_oop(narrowOop *p) { check(p); } 1603 void do_oop( oop *p) { check(p); } 1604 bool result() { return _result; } 1605 }; 1606 1607 HeapShared::CachedOopInfo HeapShared::make_cached_oop_info(oop obj) { 1608 WalkOopAndArchiveClosure* walker = WalkOopAndArchiveClosure::current(); 1609 oop referrer = (walker == nullptr) ? nullptr : walker->referencing_obj(); 1610 PointsToOopsChecker points_to_oops_checker; 1611 obj->oop_iterate(&points_to_oops_checker); 1612 return CachedOopInfo(referrer, points_to_oops_checker.result()); 1613 } 1614 1615 // We currently allow only the box classes, which are initialized very early by 1616 // HeapShared::init_box_classes(). 1617 bool HeapShared::can_mirror_be_used_in_subgraph(oop orig_java_mirror) { 1618 return java_lang_Class::is_primitive(orig_java_mirror) 1619 || orig_java_mirror == vmClasses::Boolean_klass()->java_mirror() 1620 || orig_java_mirror == vmClasses::Character_klass()->java_mirror() 1621 || orig_java_mirror == vmClasses::Float_klass()->java_mirror() 1622 || orig_java_mirror == vmClasses::Double_klass()->java_mirror() 1623 || orig_java_mirror == vmClasses::Byte_klass()->java_mirror() 1624 || orig_java_mirror == vmClasses::Short_klass()->java_mirror() 1625 || orig_java_mirror == vmClasses::Integer_klass()->java_mirror() 1626 || orig_java_mirror == vmClasses::Long_klass()->java_mirror() 1627 || orig_java_mirror == vmClasses::Void_klass()->java_mirror() 1628 || orig_java_mirror == vmClasses::Object_klass()->java_mirror(); 1629 } 1630 1631 void HeapShared::init_box_classes(TRAPS) { 1632 if (ArchiveHeapLoader::is_in_use()) { 1633 vmClasses::Boolean_klass()->initialize(CHECK); 1634 vmClasses::Character_klass()->initialize(CHECK); 1635 vmClasses::Float_klass()->initialize(CHECK); 1636 vmClasses::Double_klass()->initialize(CHECK); 1637 vmClasses::Byte_klass()->initialize(CHECK); 1638 vmClasses::Short_klass()->initialize(CHECK); 1639 vmClasses::Integer_klass()->initialize(CHECK); 1640 vmClasses::Long_klass()->initialize(CHECK); 1641 vmClasses::Void_klass()->initialize(CHECK); 1642 } 1643 } 1644 1645 void HeapShared::exit_on_error() { 1646 if (_context != nullptr) { 1647 ResourceMark rm; 1648 LogStream ls(Log(cds, heap)::error()); 1649 ls.print_cr("Context"); 1650 for (int i = 0; i < _context->length(); i++) { 1651 const char* s = _context->at(i); 1652 ls.print_cr("- %s", s); 1653 } 1654 } 1655 if (_trace != nullptr) { 1656 ResourceMark rm; 1657 LogStream ls(Log(cds, heap)::error()); 1658 ls.print_cr("Reference trace"); 1659 for (int i = 0; i < _trace->length(); i++) { 1660 oop orig_obj = _trace->at(i); 1661 ls.print_cr("[%d] ========================================", i); 1662 orig_obj->print_on(&ls); 1663 ls.cr(); 1664 } 1665 } 1666 MetaspaceShared::unrecoverable_writing_error(); 1667 } 1668 1669 // (1) If orig_obj has not been archived yet, archive it. 1670 // (2) If orig_obj has not been seen yet (since start_recording_subgraph() was called), 1671 // trace all objects that are reachable from it, and make sure these objects are archived. 1672 // (3) Record the klasses of all orig_obj and all reachable objects. 1673 bool HeapShared::archive_reachable_objects_from(int level, 1674 KlassSubGraphInfo* subgraph_info, 1675 oop orig_obj) { 1676 ArchivingObjectMark mark(orig_obj); 1677 assert(orig_obj != nullptr, "must be"); 1678 1679 if (!JavaClasses::is_supported_for_archiving(orig_obj)) { 1680 // This object has injected fields that cannot be supported easily, so we disallow them for now. 1681 // If you get an error here, you probably made a change in the JDK library that has added 1682 // these objects that are referenced (directly or indirectly) by static fields. 1683 ResourceMark rm; 1684 log_error(cds, heap)("Cannot archive object of class %s", orig_obj->klass()->external_name()); 1685 exit_on_error(); 1686 } 1687 1688 #if 0 1689 if (java_lang_Class::is_instance(orig_obj) && subgraph_info != _default_subgraph_info) { 1690 if (can_mirror_be_used_in_subgraph(orig_obj)) { 1691 orig_obj = scratch_java_mirror(orig_obj); 1692 assert(orig_obj != nullptr, "must be archived"); 1693 } else { 1694 // Don't follow the fields -- they will be nulled out when the mirror was copied 1695 1696 // FIXME - we should preserve the static fields of LambdaForm classes (and other hidden classes?) 1697 // so we need to walk the oop fields. 1698 orig_obj = scratch_java_mirror(orig_obj); 1699 assert(orig_obj != nullptr, "must be archived"); 1700 } 1701 } 1702 #else 1703 if (java_lang_Class::is_instance(orig_obj)) { 1704 orig_obj = scratch_java_mirror(orig_obj); 1705 assert(orig_obj != nullptr, "must be archived"); 1706 } 1707 #endif 1708 1709 if (has_been_seen_during_subgraph_recording(orig_obj)) { 1710 // orig_obj has already been archived and traced. Nothing more to do. 1711 return true; 1712 } else { 1713 set_has_been_seen_during_subgraph_recording(orig_obj); 1714 } 1715 1716 bool already_archived = has_been_archived(orig_obj); 1717 bool record_klasses_only = already_archived; 1718 if (!already_archived) { 1719 ++_num_new_archived_objs; 1720 if (!archive_object(orig_obj)) { 1721 // Skip archiving the sub-graph referenced from the current entry field. 1722 ResourceMark rm; 1723 log_error(cds, heap)( 1724 "Cannot archive the sub-graph referenced from %s object (" 1725 PTR_FORMAT ") size " SIZE_FORMAT ", skipped.", 1726 orig_obj->klass()->external_name(), p2i(orig_obj), orig_obj->size() * HeapWordSize); 1727 if (level == 1) { 1728 // Don't archive a subgraph root that's too big. For archives static fields, that's OK 1729 // as the Java code will take care of initializing this field dynamically. 1730 return false; 1731 } else { 1732 // We don't know how to handle an object that has been archived, but some of its reachable 1733 // objects cannot be archived. Bail out for now. We might need to fix this in the future if 1734 // we have a real use case. 1735 exit_on_error(); 1736 } 1737 } 1738 } 1739 1740 Klass *orig_k = orig_obj->klass(); 1741 subgraph_info->add_subgraph_object_klass(orig_k); 1742 1743 WalkOopAndArchiveClosure walker(level, record_klasses_only, subgraph_info, orig_obj); 1744 orig_obj->oop_iterate(&walker); 1745 1746 if (CDSEnumKlass::is_enum_obj(orig_obj)) { 1747 CDSEnumKlass::archive_enum_obj(level + 1, subgraph_info, orig_obj); 1748 } 1749 return true; 1750 } 1751 1752 // 1753 // Start from the given static field in a java mirror and archive the 1754 // complete sub-graph of java heap objects that are reached directly 1755 // or indirectly from the starting object by following references. 1756 // Sub-graph archiving restrictions (current): 1757 // 1758 // - All classes of objects in the archived sub-graph (including the 1759 // entry class) must be boot class only. 1760 // - No java.lang.Class instance (java mirror) can be included inside 1761 // an archived sub-graph. Mirror can only be the sub-graph entry object. 1762 // 1763 // The Java heap object sub-graph archiving process (see 1764 // WalkOopAndArchiveClosure): 1765 // 1766 // 1) Java object sub-graph archiving starts from a given static field 1767 // within a Class instance (java mirror). If the static field is a 1768 // reference field and points to a non-null java object, proceed to 1769 // the next step. 1770 // 1771 // 2) Archives the referenced java object. If an archived copy of the 1772 // current object already exists, updates the pointer in the archived 1773 // copy of the referencing object to point to the current archived object. 1774 // Otherwise, proceed to the next step. 1775 // 1776 // 3) Follows all references within the current java object and recursively 1777 // archive the sub-graph of objects starting from each reference. 1778 // 1779 // 4) Updates the pointer in the archived copy of referencing object to 1780 // point to the current archived object. 1781 // 1782 // 5) The Klass of the current java object is added to the list of Klasses 1783 // for loading and initializing before any object in the archived graph can 1784 // be accessed at runtime. 1785 // 1786 void HeapShared::archive_reachable_objects_from_static_field(InstanceKlass *k, 1787 const char* klass_name, 1788 int field_offset, 1789 const char* field_name) { 1790 assert(CDSConfig::is_dumping_heap(), "dump time only"); 1791 assert(k->is_shared_boot_class(), "must be boot class"); 1792 1793 oop m = k->java_mirror(); 1794 1795 KlassSubGraphInfo* subgraph_info = get_subgraph_info(k); 1796 oop f = m->obj_field(field_offset); 1797 1798 log_debug(cds, heap)("Start archiving from: %s::%s (" PTR_FORMAT ")", klass_name, field_name, p2i(f)); 1799 1800 if (!CompressedOops::is_null(f)) { 1801 if (log_is_enabled(Trace, cds, heap)) { 1802 LogTarget(Trace, cds, heap) log; 1803 LogStream out(log); 1804 f->print_on(&out); 1805 } 1806 1807 bool success = archive_reachable_objects_from(1, subgraph_info, f); 1808 if (!success) { 1809 log_error(cds, heap)("Archiving failed %s::%s (some reachable objects cannot be archived)", 1810 klass_name, field_name); 1811 } else { 1812 // Note: the field value is not preserved in the archived mirror. 1813 // Record the field as a new subGraph entry point. The recorded 1814 // information is restored from the archive at runtime. 1815 subgraph_info->add_subgraph_entry_field(field_offset, f); 1816 log_info(cds, heap)("Archived field %s::%s => " PTR_FORMAT, klass_name, field_name, p2i(f)); 1817 } 1818 } else { 1819 // The field contains null, we still need to record the entry point, 1820 // so it can be restored at runtime. 1821 subgraph_info->add_subgraph_entry_field(field_offset, nullptr); 1822 } 1823 } 1824 1825 #ifndef PRODUCT 1826 class VerifySharedOopClosure: public BasicOopIterateClosure { 1827 public: 1828 void do_oop(narrowOop *p) { VerifySharedOopClosure::do_oop_work(p); } 1829 void do_oop( oop *p) { VerifySharedOopClosure::do_oop_work(p); } 1830 1831 protected: 1832 template <class T> void do_oop_work(T *p) { 1833 oop obj = RawAccess<>::oop_load(p); 1834 if (!CompressedOops::is_null(obj)) { 1835 HeapShared::verify_reachable_objects_from(obj); 1836 } 1837 } 1838 }; 1839 1840 void HeapShared::verify_subgraph_from_static_field(InstanceKlass* k, int field_offset) { 1841 assert(CDSConfig::is_dumping_heap(), "dump time only"); 1842 assert(k->is_shared_boot_class(), "must be boot class"); 1843 1844 oop m = k->java_mirror(); 1845 oop f = m->obj_field(field_offset); 1846 if (!CompressedOops::is_null(f)) { 1847 verify_subgraph_from(f); 1848 } 1849 } 1850 1851 void HeapShared::verify_subgraph_from(oop orig_obj) { 1852 if (!has_been_archived(orig_obj)) { 1853 // It's OK for the root of a subgraph to be not archived. See comments in 1854 // archive_reachable_objects_from(). 1855 return; 1856 } 1857 1858 // Verify that all objects reachable from orig_obj are archived. 1859 init_seen_objects_table(); 1860 verify_reachable_objects_from(orig_obj); 1861 delete_seen_objects_table(); 1862 } 1863 1864 void HeapShared::verify_reachable_objects_from(oop obj) { 1865 _num_total_verifications ++; 1866 if (java_lang_Class::is_instance(obj)) { 1867 obj = scratch_java_mirror(obj); 1868 assert(obj != nullptr, "must be"); 1869 } 1870 if (!has_been_seen_during_subgraph_recording(obj)) { 1871 set_has_been_seen_during_subgraph_recording(obj); 1872 assert(has_been_archived(obj), "must be"); 1873 VerifySharedOopClosure walker; 1874 obj->oop_iterate(&walker); 1875 } 1876 } 1877 #endif 1878 1879 // The "default subgraph" contains special objects (see heapShared.hpp) that 1880 // can be accessed before we load any Java classes (including java/lang/Class). 1881 // Make sure that these are only instances of the very few specific types 1882 // that we can handle. 1883 void HeapShared::check_default_subgraph_classes() { 1884 GrowableArray<Klass*>* klasses = _default_subgraph_info->subgraph_object_klasses(); 1885 int num = klasses->length(); 1886 int warned = 0; 1887 for (int i = 0; i < num; i++) { 1888 Klass* subgraph_k = klasses->at(i); 1889 if (log_is_enabled(Info, cds, heap)) { 1890 ResourceMark rm; 1891 log_info(cds, heap)( 1892 "Archived object klass (default subgraph %d) => %s", 1893 i, subgraph_k->external_name()); 1894 } 1895 1896 if (subgraph_k->is_instance_klass()) { 1897 InstanceKlass* ik = InstanceKlass::cast(subgraph_k); 1898 Symbol* name = ArchiveBuilder::current()->get_source_addr(ik->name()); 1899 if (name != vmSymbols::java_lang_Class() && 1900 name != vmSymbols::java_lang_String() && 1901 name != vmSymbols::java_lang_ArithmeticException() && 1902 name != vmSymbols::java_lang_ArrayIndexOutOfBoundsException() && 1903 name != vmSymbols::java_lang_ArrayStoreException() && 1904 name != vmSymbols::java_lang_ClassCastException() && 1905 name != vmSymbols::java_lang_InternalError() && 1906 name != vmSymbols::java_lang_NullPointerException() && 1907 !is_archivable_hidden_klass(ik)) { 1908 ResourceMark rm; 1909 const char* category = ArchiveUtils::class_category(ik); 1910 log_info(cds)("TODO: Archived unusual klass (default subgraph %2d) => %-5s %s", 1911 ++warned, category, ik->external_name()); 1912 } 1913 } 1914 } 1915 } 1916 1917 HeapShared::SeenObjectsTable* HeapShared::_seen_objects_table = nullptr; 1918 int HeapShared::_num_new_walked_objs; 1919 int HeapShared::_num_new_archived_objs; 1920 int HeapShared::_num_old_recorded_klasses; 1921 1922 int HeapShared::_num_total_subgraph_recordings = 0; 1923 int HeapShared::_num_total_walked_objs = 0; 1924 int HeapShared::_num_total_archived_objs = 0; 1925 int HeapShared::_num_total_recorded_klasses = 0; 1926 int HeapShared::_num_total_verifications = 0; 1927 1928 bool HeapShared::has_been_seen_during_subgraph_recording(oop obj) { 1929 return _seen_objects_table->get(obj) != nullptr; 1930 } 1931 1932 void HeapShared::set_has_been_seen_during_subgraph_recording(oop obj) { 1933 assert(!has_been_seen_during_subgraph_recording(obj), "sanity"); 1934 _seen_objects_table->put_when_absent(obj, true); 1935 _seen_objects_table->maybe_grow(); 1936 ++ _num_new_walked_objs; 1937 } 1938 1939 void HeapShared::start_recording_subgraph(InstanceKlass *k, const char* class_name, bool is_full_module_graph) { 1940 log_info(cds, heap)("Start recording subgraph(s) for archived fields in %s", class_name); 1941 init_subgraph_info(k, is_full_module_graph); 1942 init_seen_objects_table(); 1943 _num_new_walked_objs = 0; 1944 _num_new_archived_objs = 0; 1945 _num_old_recorded_klasses = get_subgraph_info(k)->num_subgraph_object_klasses(); 1946 } 1947 1948 void HeapShared::done_recording_subgraph(InstanceKlass *k, const char* class_name) { 1949 int num_new_recorded_klasses = get_subgraph_info(k)->num_subgraph_object_klasses() - 1950 _num_old_recorded_klasses; 1951 log_info(cds, heap)("Done recording subgraph(s) for archived fields in %s: " 1952 "walked %d objs, archived %d new objs, recorded %d classes", 1953 class_name, _num_new_walked_objs, _num_new_archived_objs, 1954 num_new_recorded_klasses); 1955 1956 delete_seen_objects_table(); 1957 1958 _num_total_subgraph_recordings ++; 1959 _num_total_walked_objs += _num_new_walked_objs; 1960 _num_total_archived_objs += _num_new_archived_objs; 1961 _num_total_recorded_klasses += num_new_recorded_klasses; 1962 } 1963 1964 class ArchivableStaticFieldFinder: public FieldClosure { 1965 InstanceKlass* _ik; 1966 Symbol* _field_name; 1967 bool _found; 1968 int _offset; 1969 public: 1970 ArchivableStaticFieldFinder(InstanceKlass* ik, Symbol* field_name) : 1971 _ik(ik), _field_name(field_name), _found(false), _offset(-1) {} 1972 1973 virtual void do_field(fieldDescriptor* fd) { 1974 if (fd->name() == _field_name) { 1975 assert(!_found, "fields can never be overloaded"); 1976 if (is_reference_type(fd->field_type())) { 1977 _found = true; 1978 _offset = fd->offset(); 1979 } 1980 } 1981 } 1982 bool found() { return _found; } 1983 int offset() { return _offset; } 1984 }; 1985 1986 void HeapShared::init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[], 1987 TRAPS) { 1988 for (int i = 0; fields[i].valid(); i++) { 1989 ArchivableStaticFieldInfo* info = &fields[i]; 1990 TempNewSymbol klass_name = SymbolTable::new_symbol(info->klass_name); 1991 TempNewSymbol field_name = SymbolTable::new_symbol(info->field_name); 1992 ResourceMark rm; // for stringStream::as_string() etc. 1993 1994 #ifndef PRODUCT 1995 bool is_test_class = (ArchiveHeapTestClass != nullptr) && (strcmp(info->klass_name, ArchiveHeapTestClass) == 0); 1996 const char* test_class_name = ArchiveHeapTestClass; 1997 #else 1998 bool is_test_class = false; 1999 const char* test_class_name = ""; 2000 #endif 2001 2002 if (is_test_class) { 2003 log_warning(cds)("Loading ArchiveHeapTestClass %s ...", test_class_name); 2004 } 2005 2006 Klass* k = SystemDictionary::resolve_or_fail(klass_name, true, THREAD); 2007 if (HAS_PENDING_EXCEPTION) { 2008 CLEAR_PENDING_EXCEPTION; 2009 stringStream st; 2010 st.print("Fail to initialize archive heap: %s cannot be loaded by the boot loader", info->klass_name); 2011 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string()); 2012 } 2013 2014 if (!k->is_instance_klass()) { 2015 stringStream st; 2016 st.print("Fail to initialize archive heap: %s is not an instance class", info->klass_name); 2017 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string()); 2018 } 2019 2020 InstanceKlass* ik = InstanceKlass::cast(k); 2021 assert(InstanceKlass::cast(ik)->is_shared_boot_class(), 2022 "Only support boot classes"); 2023 2024 if (is_test_class) { 2025 if (ik->module()->is_named()) { 2026 // We don't want ArchiveHeapTestClass to be abused to easily load/initialize arbitrary 2027 // core-lib classes. You need to at least append to the bootclasspath. 2028 stringStream st; 2029 st.print("ArchiveHeapTestClass %s is not in unnamed module", test_class_name); 2030 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string()); 2031 } 2032 2033 if (ik->package() != nullptr) { 2034 // This restriction makes HeapShared::is_a_test_class_in_unnamed_module() easy. 2035 stringStream st; 2036 st.print("ArchiveHeapTestClass %s is not in unnamed package", test_class_name); 2037 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string()); 2038 } 2039 } else { 2040 if (ik->module()->name() != vmSymbols::java_base()) { 2041 // We don't want to deal with cases when a module is unavailable at runtime. 2042 // FUTURE -- load from archived heap only when module graph has not changed 2043 // between dump and runtime. 2044 stringStream st; 2045 st.print("%s is not in java.base module", info->klass_name); 2046 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string()); 2047 } 2048 } 2049 2050 if (is_test_class) { 2051 log_warning(cds)("Initializing ArchiveHeapTestClass %s ...", test_class_name); 2052 } 2053 ik->initialize(CHECK); 2054 2055 ArchivableStaticFieldFinder finder(ik, field_name); 2056 ik->do_local_static_fields(&finder); 2057 if (!finder.found()) { 2058 stringStream st; 2059 st.print("Unable to find the static T_OBJECT field %s::%s", info->klass_name, info->field_name); 2060 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string()); 2061 } 2062 2063 info->klass = ik; 2064 info->offset = finder.offset(); 2065 } 2066 } 2067 2068 void HeapShared::init_subgraph_entry_fields(TRAPS) { 2069 assert(HeapShared::can_write(), "must be"); 2070 _dump_time_subgraph_info_table = new (mtClass)DumpTimeKlassSubGraphInfoTable(); 2071 init_subgraph_entry_fields(archive_subgraph_entry_fields, CHECK); 2072 if (CDSConfig::is_dumping_full_module_graph()) { 2073 init_subgraph_entry_fields(fmg_archive_subgraph_entry_fields, CHECK); 2074 } 2075 } 2076 2077 #ifndef PRODUCT 2078 void HeapShared::setup_test_class(const char* test_class_name) { 2079 ArchivableStaticFieldInfo* p = archive_subgraph_entry_fields; 2080 int num_slots = sizeof(archive_subgraph_entry_fields) / sizeof(ArchivableStaticFieldInfo); 2081 assert(p[num_slots - 2].klass_name == nullptr, "must have empty slot that's patched below"); 2082 assert(p[num_slots - 1].klass_name == nullptr, "must have empty slot that marks the end of the list"); 2083 2084 if (test_class_name != nullptr) { 2085 p[num_slots - 2].klass_name = test_class_name; 2086 p[num_slots - 2].field_name = ARCHIVE_TEST_FIELD_NAME; 2087 } 2088 } 2089 2090 // See if ik is one of the test classes that are pulled in by -XX:ArchiveHeapTestClass 2091 // during runtime. This may be called before the module system is initialized so 2092 // we cannot rely on InstanceKlass::module(), etc. 2093 bool HeapShared::is_a_test_class_in_unnamed_module(Klass* ik) { 2094 if (_test_class != nullptr) { 2095 if (ik == _test_class) { 2096 return true; 2097 } 2098 Array<Klass*>* klasses = _test_class_record->subgraph_object_klasses(); 2099 if (klasses == nullptr) { 2100 return false; 2101 } 2102 2103 for (int i = 0; i < klasses->length(); i++) { 2104 Klass* k = klasses->at(i); 2105 if (k == ik) { 2106 Symbol* name; 2107 if (k->is_instance_klass()) { 2108 name = InstanceKlass::cast(k)->name(); 2109 } else if (k->is_objArray_klass()) { 2110 Klass* bk = ObjArrayKlass::cast(k)->bottom_klass(); 2111 if (!bk->is_instance_klass()) { 2112 return false; 2113 } 2114 name = bk->name(); 2115 } else { 2116 return false; 2117 } 2118 2119 // See KlassSubGraphInfo::check_allowed_klass() - only two types of 2120 // classes are allowed: 2121 // (A) java.base classes (which must not be in the unnamed module) 2122 // (B) test classes which must be in the unnamed package of the unnamed module. 2123 // So if we see a '/' character in the class name, it must be in (A); 2124 // otherwise it must be in (B). 2125 if (name->index_of_at(0, "/", 1) >= 0) { 2126 return false; // (A) 2127 } 2128 2129 return true; // (B) 2130 } 2131 } 2132 } 2133 2134 return false; 2135 } 2136 #endif 2137 2138 void HeapShared::init_for_dumping(TRAPS) { 2139 if (HeapShared::can_write()) { 2140 setup_test_class(ArchiveHeapTestClass); 2141 _dumped_interned_strings = new (mtClass)DumpedInternedStrings(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE); 2142 init_subgraph_entry_fields(CHECK); 2143 } 2144 } 2145 2146 void HeapShared::archive_object_subgraphs(ArchivableStaticFieldInfo fields[], 2147 bool is_full_module_graph) { 2148 _num_total_subgraph_recordings = 0; 2149 _num_total_walked_objs = 0; 2150 _num_total_archived_objs = 0; 2151 _num_total_recorded_klasses = 0; 2152 _num_total_verifications = 0; 2153 2154 // For each class X that has one or more archived fields: 2155 // [1] Dump the subgraph of each archived field 2156 // [2] Create a list of all the class of the objects that can be reached 2157 // by any of these static fields. 2158 // At runtime, these classes are initialized before X's archived fields 2159 // are restored by HeapShared::initialize_from_archived_subgraph(). 2160 int i; 2161 for (int i = 0; fields[i].valid(); ) { 2162 ArchivableStaticFieldInfo* info = &fields[i]; 2163 const char* klass_name = info->klass_name; 2164 start_recording_subgraph(info->klass, klass_name, is_full_module_graph); 2165 2166 ContextMark cm(klass_name); 2167 // If you have specified consecutive fields of the same klass in 2168 // fields[], these will be archived in the same 2169 // {start_recording_subgraph ... done_recording_subgraph} pass to 2170 // save time. 2171 for (; fields[i].valid(); i++) { 2172 ArchivableStaticFieldInfo* f = &fields[i]; 2173 if (f->klass_name != klass_name) { 2174 break; 2175 } 2176 2177 ContextMark cm(f->field_name); 2178 archive_reachable_objects_from_static_field(f->klass, f->klass_name, 2179 f->offset, f->field_name); 2180 } 2181 done_recording_subgraph(info->klass, klass_name); 2182 } 2183 2184 log_info(cds, heap)("Archived subgraph records = %d", 2185 _num_total_subgraph_recordings); 2186 log_info(cds, heap)(" Walked %d objects", _num_total_walked_objs); 2187 log_info(cds, heap)(" Archived %d objects", _num_total_archived_objs); 2188 log_info(cds, heap)(" Recorded %d klasses", _num_total_recorded_klasses); 2189 2190 #ifndef PRODUCT 2191 for (int i = 0; fields[i].valid(); i++) { 2192 ArchivableStaticFieldInfo* f = &fields[i]; 2193 verify_subgraph_from_static_field(f->klass, f->offset); 2194 } 2195 log_info(cds, heap)(" Verified %d references", _num_total_verifications); 2196 #endif 2197 } 2198 2199 // Not all the strings in the global StringTable are dumped into the archive, because 2200 // some of those strings may be only referenced by classes that are excluded from 2201 // the archive. We need to explicitly mark the strings that are: 2202 // [1] used by classes that WILL be archived; 2203 // [2] included in the SharedArchiveConfigFile. 2204 void HeapShared::add_to_dumped_interned_strings(oop string) { 2205 assert_at_safepoint(); // DumpedInternedStrings uses raw oops 2206 assert(!ArchiveHeapWriter::is_string_too_large_to_archive(string), "must be"); 2207 bool created; 2208 _dumped_interned_strings->put_if_absent(string, true, &created); 2209 if (created) { 2210 _dumped_interned_strings->maybe_grow(); 2211 } 2212 } 2213 2214 #ifndef PRODUCT 2215 // At dump-time, find the location of all the non-null oop pointers in an archived heap 2216 // region. This way we can quickly relocate all the pointers without using 2217 // BasicOopIterateClosure at runtime. 2218 class FindEmbeddedNonNullPointers: public BasicOopIterateClosure { 2219 void* _start; 2220 BitMap *_oopmap; 2221 int _num_total_oops; 2222 int _num_null_oops; 2223 public: 2224 FindEmbeddedNonNullPointers(void* start, BitMap* oopmap) 2225 : _start(start), _oopmap(oopmap), _num_total_oops(0), _num_null_oops(0) {} 2226 2227 virtual void do_oop(narrowOop* p) { 2228 assert(UseCompressedOops, "sanity"); 2229 _num_total_oops ++; 2230 narrowOop v = *p; 2231 if (!CompressedOops::is_null(v)) { 2232 size_t idx = p - (narrowOop*)_start; 2233 _oopmap->set_bit(idx); 2234 } else { 2235 _num_null_oops ++; 2236 } 2237 } 2238 virtual void do_oop(oop* p) { 2239 assert(!UseCompressedOops, "sanity"); 2240 _num_total_oops ++; 2241 if ((*p) != nullptr) { 2242 size_t idx = p - (oop*)_start; 2243 _oopmap->set_bit(idx); 2244 } else { 2245 _num_null_oops ++; 2246 } 2247 } 2248 int num_total_oops() const { return _num_total_oops; } 2249 int num_null_oops() const { return _num_null_oops; } 2250 }; 2251 #endif 2252 2253 #ifndef PRODUCT 2254 ResourceBitMap HeapShared::calculate_oopmap(MemRegion region) { 2255 size_t num_bits = region.byte_size() / (UseCompressedOops ? sizeof(narrowOop) : sizeof(oop)); 2256 ResourceBitMap oopmap(num_bits); 2257 2258 HeapWord* p = region.start(); 2259 HeapWord* end = region.end(); 2260 FindEmbeddedNonNullPointers finder((void*)p, &oopmap); 2261 2262 int num_objs = 0; 2263 while (p < end) { 2264 oop o = cast_to_oop(p); 2265 o->oop_iterate(&finder); 2266 p += o->size(); 2267 ++ num_objs; 2268 } 2269 2270 log_info(cds, heap)("calculate_oopmap: objects = %6d, oop fields = %7d (nulls = %7d)", 2271 num_objs, finder.num_total_oops(), finder.num_null_oops()); 2272 return oopmap; 2273 } 2274 2275 #endif // !PRODUCT 2276 2277 void HeapShared::count_allocation(size_t size) { 2278 _total_obj_count ++; 2279 _total_obj_size += size; 2280 for (int i = 0; i < ALLOC_STAT_SLOTS; i++) { 2281 if (size <= (size_t(1) << i)) { 2282 _alloc_count[i] ++; 2283 _alloc_size[i] += size; 2284 return; 2285 } 2286 } 2287 } 2288 2289 static double avg_size(size_t size, size_t count) { 2290 double avg = 0; 2291 if (count > 0) { 2292 avg = double(size * HeapWordSize) / double(count); 2293 } 2294 return avg; 2295 } 2296 2297 void HeapShared::print_stats() { 2298 size_t huge_count = _total_obj_count; 2299 size_t huge_size = _total_obj_size; 2300 2301 for (int i = 0; i < ALLOC_STAT_SLOTS; i++) { 2302 size_t byte_size_limit = (size_t(1) << i) * HeapWordSize; 2303 size_t count = _alloc_count[i]; 2304 size_t size = _alloc_size[i]; 2305 log_info(cds, heap)(SIZE_FORMAT_W(8) " objects are <= " SIZE_FORMAT_W(-6) 2306 " bytes (total " SIZE_FORMAT_W(8) " bytes, avg %8.1f bytes)", 2307 count, byte_size_limit, size * HeapWordSize, avg_size(size, count)); 2308 huge_count -= count; 2309 huge_size -= size; 2310 } 2311 2312 log_info(cds, heap)(SIZE_FORMAT_W(8) " huge objects (total " SIZE_FORMAT_W(8) " bytes" 2313 ", avg %8.1f bytes)", 2314 huge_count, huge_size * HeapWordSize, 2315 avg_size(huge_size, huge_count)); 2316 log_info(cds, heap)(SIZE_FORMAT_W(8) " total objects (total " SIZE_FORMAT_W(8) " bytes" 2317 ", avg %8.1f bytes)", 2318 _total_obj_count, _total_obj_size * HeapWordSize, 2319 avg_size(_total_obj_size, _total_obj_count)); 2320 } 2321 2322 bool HeapShared::is_archived_boot_layer_available(JavaThread* current) { 2323 TempNewSymbol klass_name = SymbolTable::new_symbol(ARCHIVED_BOOT_LAYER_CLASS); 2324 InstanceKlass* k = SystemDictionary::find_instance_klass(current, klass_name, Handle(), Handle()); 2325 if (k == nullptr) { 2326 return false; 2327 } else { 2328 TempNewSymbol field_name = SymbolTable::new_symbol(ARCHIVED_BOOT_LAYER_FIELD); 2329 TempNewSymbol field_signature = SymbolTable::new_symbol("Ljdk/internal/module/ArchivedBootLayer;"); 2330 fieldDescriptor fd; 2331 if (k->find_field(field_name, field_signature, true, &fd) != nullptr) { 2332 oop m = k->java_mirror(); 2333 oop f = m->obj_field(fd.offset()); 2334 if (CompressedOops::is_null(f)) { 2335 return false; 2336 } 2337 } else { 2338 return false; 2339 } 2340 } 2341 return true; 2342 } 2343 2344 #endif // INCLUDE_CDS_JAVA_HEAP --- EOF ---