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