1 /*
   2  * Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "cds/archiveBuilder.hpp"
  27 #include "cds/archiveHeapLoader.hpp"
  28 #include "cds/archiveHeapWriter.hpp"
  29 #include "cds/archiveUtils.hpp"
  30 #include "cds/cdsConfig.hpp"
  31 #include "cds/cdsEnumKlass.hpp"
  32 #include "cds/cdsHeapVerifier.hpp"
  33 #include "cds/heapShared.hpp"
  34 #include "cds/metaspaceShared.hpp"
  35 #include "classfile/classLoaderData.hpp"
  36 #include "classfile/classLoaderExt.hpp"
  37 #include "classfile/javaClasses.inline.hpp"
  38 #include "classfile/modules.hpp"
  39 #include "classfile/stringTable.hpp"
  40 #include "classfile/symbolTable.hpp"
  41 #include "classfile/systemDictionary.hpp"
  42 #include "classfile/systemDictionaryShared.hpp"
  43 #include "classfile/vmClasses.hpp"
  44 #include "classfile/vmSymbols.hpp"
  45 #include "gc/shared/collectedHeap.hpp"
  46 #include "gc/shared/gcLocker.hpp"
  47 #include "gc/shared/gcVMOperations.hpp"
  48 #include "logging/log.hpp"
  49 #include "logging/logStream.hpp"
  50 #include "memory/iterator.inline.hpp"
  51 #include "memory/resourceArea.hpp"
  52 #include "memory/universe.hpp"
  53 #include "oops/compressedOops.inline.hpp"
  54 #include "oops/fieldStreams.inline.hpp"
  55 #include "oops/objArrayOop.inline.hpp"
  56 #include "oops/oop.inline.hpp"
  57 #include "oops/typeArrayOop.inline.hpp"
  58 #include "prims/jvmtiExport.hpp"
  59 #include "runtime/arguments.hpp"
  60 #include "runtime/fieldDescriptor.inline.hpp"
  61 #include "runtime/init.hpp"
  62 #include "runtime/javaCalls.hpp"
  63 #include "runtime/mutexLocker.hpp"
  64 #include "runtime/safepointVerifiers.hpp"
  65 #include "utilities/bitMap.inline.hpp"
  66 #include "utilities/copy.hpp"
  67 #if INCLUDE_G1GC
  68 #include "gc/g1/g1CollectedHeap.hpp"
  69 #endif
  70 
  71 #if INCLUDE_CDS_JAVA_HEAP
  72 
  73 struct ArchivableStaticFieldInfo {
  74   const char* klass_name;
  75   const char* field_name;
  76   InstanceKlass* klass;
  77   int offset;
  78   BasicType type;
  79 
  80   ArchivableStaticFieldInfo(const char* k, const char* f)
  81   : klass_name(k), field_name(f), klass(nullptr), offset(0), type(T_ILLEGAL) {}
  82 
  83   bool valid() {
  84     return klass_name != nullptr;
  85   }
  86 };
  87 
  88 bool HeapShared::_disable_writing = false;
  89 DumpedInternedStrings *HeapShared::_dumped_interned_strings = nullptr;
  90 
  91 size_t HeapShared::_alloc_count[HeapShared::ALLOC_STAT_SLOTS];
  92 size_t HeapShared::_alloc_size[HeapShared::ALLOC_STAT_SLOTS];
  93 size_t HeapShared::_total_obj_count;
  94 size_t HeapShared::_total_obj_size;
  95 
  96 #ifndef PRODUCT
  97 #define ARCHIVE_TEST_FIELD_NAME "archivedObjects"
  98 static Array<char>* _archived_ArchiveHeapTestClass = nullptr;
  99 static const char* _test_class_name = nullptr;
 100 static const Klass* _test_class = nullptr;
 101 static const ArchivedKlassSubGraphInfoRecord* _test_class_record = nullptr;
 102 #endif
 103 
 104 
 105 //
 106 // If you add new entries to the following tables, you should know what you're doing!
 107 //
 108 
 109 static ArchivableStaticFieldInfo archive_subgraph_entry_fields[] = {
 110   {"java/lang/Integer$IntegerCache",              "archivedCache"},
 111   {"java/lang/Long$LongCache",                    "archivedCache"},
 112   {"java/lang/Byte$ByteCache",                    "archivedCache"},
 113   {"java/lang/Short$ShortCache",                  "archivedCache"},
 114   {"java/lang/Character$CharacterCache",          "archivedCache"},
 115   {"java/util/jar/Attributes$Name",               "KNOWN_NAMES"},
 116   {"sun/util/locale/BaseLocale",                  "constantBaseLocales"},
 117   {"jdk/internal/module/ArchivedModuleGraph",     "archivedModuleGraph"},
 118   {"java/util/ImmutableCollections",              "archivedObjects"},
 119   {"java/lang/ModuleLayer",                       "EMPTY_LAYER"},
 120   {"java/lang/module/Configuration",              "EMPTY_CONFIGURATION"},
 121   {"jdk/internal/math/FDBigInteger",              "archivedCaches"},
 122 #ifndef PRODUCT
 123   {nullptr, nullptr}, // Extra slot for -XX:ArchiveHeapTestClass
 124 #endif
 125   {nullptr, nullptr},
 126 };
 127 
 128 // full module graph
 129 static ArchivableStaticFieldInfo fmg_archive_subgraph_entry_fields[] = {
 130   {"jdk/internal/loader/ArchivedClassLoaders",    "archivedClassLoaders"},
 131   {ARCHIVED_BOOT_LAYER_CLASS,                     ARCHIVED_BOOT_LAYER_FIELD},
 132   {"java/lang/Module$ArchivedData",               "archivedData"},
 133   {nullptr, nullptr},
 134 };
 135 
 136 KlassSubGraphInfo* HeapShared::_default_subgraph_info;
 137 GrowableArrayCHeap<oop, mtClassShared>* HeapShared::_pending_roots = nullptr;
 138 GrowableArrayCHeap<OopHandle, mtClassShared>* HeapShared::_root_segments;
 139 int HeapShared::_root_segment_max_size_elems;
 140 OopHandle HeapShared::_scratch_basic_type_mirrors[T_VOID+1];
 141 MetaspaceObjToOopHandleTable* HeapShared::_scratch_java_mirror_table = nullptr;
 142 MetaspaceObjToOopHandleTable* HeapShared::_scratch_references_table = nullptr;
 143 
 144 static bool is_subgraph_root_class_of(ArchivableStaticFieldInfo fields[], InstanceKlass* ik) {
 145   for (int i = 0; fields[i].valid(); i++) {
 146     if (fields[i].klass == ik) {
 147       return true;
 148     }
 149   }
 150   return false;
 151 }
 152 
 153 bool HeapShared::is_subgraph_root_class(InstanceKlass* ik) {
 154   return is_subgraph_root_class_of(archive_subgraph_entry_fields, ik) ||
 155          is_subgraph_root_class_of(fmg_archive_subgraph_entry_fields, ik);
 156 }
 157 
 158 unsigned HeapShared::oop_hash(oop const& p) {
 159   // Do not call p->identity_hash() as that will update the
 160   // object header.
 161   return primitive_hash(cast_from_oop<intptr_t>(p));
 162 }
 163 
 164 static void reset_states(oop obj, TRAPS) {
 165   Handle h_obj(THREAD, obj);
 166   InstanceKlass* klass = InstanceKlass::cast(obj->klass());
 167   TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates");
 168   Symbol* method_sig = vmSymbols::void_method_signature();
 169 
 170   while (klass != nullptr) {
 171     Method* method = klass->find_method(method_name, method_sig);
 172     if (method != nullptr) {
 173       assert(method->is_private(), "must be");
 174       if (log_is_enabled(Debug, cds)) {
 175         ResourceMark rm(THREAD);
 176         log_debug(cds)("  calling %s", method->name_and_sig_as_C_string());
 177       }
 178       JavaValue result(T_VOID);
 179       JavaCalls::call_special(&result, h_obj, klass,
 180                               method_name, method_sig, CHECK);
 181     }
 182     klass = klass->java_super();
 183   }
 184 }
 185 
 186 void HeapShared::reset_archived_object_states(TRAPS) {
 187   assert(CDSConfig::is_dumping_heap(), "dump-time only");
 188   log_debug(cds)("Resetting platform loader");
 189   reset_states(SystemDictionary::java_platform_loader(), CHECK);
 190   log_debug(cds)("Resetting system loader");
 191   reset_states(SystemDictionary::java_system_loader(), CHECK);
 192 
 193   // Clean up jdk.internal.loader.ClassLoaders::bootLoader(), which is not
 194   // directly used for class loading, but rather is used by the core library
 195   // to keep track of resources, etc, loaded by the null class loader.
 196   //
 197   // Note, this object is non-null, and is not the same as
 198   // ClassLoaderData::the_null_class_loader_data()->class_loader(),
 199   // which is null.
 200   log_debug(cds)("Resetting boot loader");
 201   JavaValue result(T_OBJECT);
 202   JavaCalls::call_static(&result,
 203                          vmClasses::jdk_internal_loader_ClassLoaders_klass(),
 204                          vmSymbols::bootLoader_name(),
 205                          vmSymbols::void_BuiltinClassLoader_signature(),
 206                          CHECK);
 207   Handle boot_loader(THREAD, result.get_oop());
 208   reset_states(boot_loader(), CHECK);
 209 }
 210 
 211 HeapShared::ArchivedObjectCache* HeapShared::_archived_object_cache = nullptr;
 212 
 213 bool HeapShared::has_been_archived(oop obj) {
 214   assert(CDSConfig::is_dumping_heap(), "dump-time only");
 215   return archived_object_cache()->get(obj) != nullptr;
 216 }
 217 
 218 int HeapShared::append_root(oop obj) {
 219   assert(CDSConfig::is_dumping_heap(), "dump-time only");
 220 
 221   // No GC should happen since we aren't scanning _pending_roots.
 222   assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
 223 
 224   if (_pending_roots == nullptr) {
 225     _pending_roots = new GrowableArrayCHeap<oop, mtClassShared>(500);
 226   }
 227 
 228   return _pending_roots->append(obj);
 229 }
 230 
 231 objArrayOop HeapShared::root_segment(int segment_idx) {
 232   if (CDSConfig::is_dumping_heap()) {
 233     assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
 234     if (!HeapShared::can_write()) {
 235       return nullptr;
 236     }
 237   } else {
 238     assert(CDSConfig::is_using_archive(), "must be");
 239   }
 240 
 241   objArrayOop segment = (objArrayOop)_root_segments->at(segment_idx).resolve();
 242   assert(segment != nullptr, "should have been initialized");
 243   return segment;
 244 }
 245 
 246 void HeapShared::get_segment_indexes(int idx, int& seg_idx, int& int_idx) {
 247   assert(_root_segment_max_size_elems > 0, "sanity");
 248 
 249   // Try to avoid divisions for the common case.
 250   if (idx < _root_segment_max_size_elems) {
 251     seg_idx = 0;
 252     int_idx = idx;
 253   } else {
 254     seg_idx = idx / _root_segment_max_size_elems;
 255     int_idx = idx % _root_segment_max_size_elems;
 256   }
 257 
 258   assert(idx == seg_idx * _root_segment_max_size_elems + int_idx,
 259          "sanity: %d index maps to %d segment and %d internal", idx, seg_idx, int_idx);
 260 }
 261 
 262 // Returns an objArray that contains all the roots of the archived objects
 263 oop HeapShared::get_root(int index, bool clear) {
 264   assert(index >= 0, "sanity");
 265   assert(!CDSConfig::is_dumping_heap() && CDSConfig::is_using_archive(), "runtime only");
 266   assert(!_root_segments->is_empty(), "must have loaded shared heap");
 267   int seg_idx, int_idx;
 268   get_segment_indexes(index, seg_idx, int_idx);
 269   oop result = root_segment(seg_idx)->obj_at(int_idx);
 270   if (clear) {
 271     clear_root(index);
 272   }
 273   return result;
 274 }
 275 
 276 void HeapShared::clear_root(int index) {
 277   assert(index >= 0, "sanity");
 278   assert(CDSConfig::is_using_archive(), "must be");
 279   if (ArchiveHeapLoader::is_in_use()) {
 280     int seg_idx, int_idx;
 281     get_segment_indexes(index, seg_idx, int_idx);
 282     if (log_is_enabled(Debug, cds, heap)) {
 283       oop old = root_segment(seg_idx)->obj_at(int_idx);
 284       log_debug(cds, heap)("Clearing root %d: was " PTR_FORMAT, index, p2i(old));
 285     }
 286     root_segment(seg_idx)->obj_at_put(int_idx, nullptr);
 287   }
 288 }
 289 
 290 bool HeapShared::archive_object(oop obj) {
 291   assert(CDSConfig::is_dumping_heap(), "dump-time only");
 292 
 293   assert(!obj->is_stackChunk(), "do not archive stack chunks");
 294   if (has_been_archived(obj)) {
 295     return true;
 296   }
 297 
 298   if (ArchiveHeapWriter::is_too_large_to_archive(obj->size())) {
 299     log_debug(cds, heap)("Cannot archive, object (" PTR_FORMAT ") is too large: " SIZE_FORMAT,
 300                          p2i(obj), obj->size());
 301     return false;
 302   } else {
 303     count_allocation(obj->size());
 304     ArchiveHeapWriter::add_source_obj(obj);
 305     CachedOopInfo info = make_cached_oop_info(obj);
 306     archived_object_cache()->put_when_absent(obj, info);
 307     archived_object_cache()->maybe_grow();
 308     mark_native_pointers(obj);
 309 
 310     if (log_is_enabled(Debug, cds, heap)) {
 311       ResourceMark rm;
 312       log_debug(cds, heap)("Archived heap object " PTR_FORMAT " : %s",
 313                            p2i(obj), obj->klass()->external_name());
 314     }
 315 
 316     if (java_lang_Module::is_instance(obj) && Modules::check_archived_module_oop(obj)) {
 317       Modules::update_oops_in_archived_module(obj, append_root(obj));
 318     }
 319 
 320     return true;
 321   }
 322 }
 323 
 324 class MetaspaceObjToOopHandleTable: public ResourceHashtable<MetaspaceObj*, OopHandle,
 325     36137, // prime number
 326     AnyObj::C_HEAP,
 327     mtClassShared> {
 328 public:
 329   oop get_oop(MetaspaceObj* ptr) {
 330     MutexLocker ml(ScratchObjects_lock, Mutex::_no_safepoint_check_flag);
 331     OopHandle* handle = get(ptr);
 332     if (handle != nullptr) {
 333       return handle->resolve();
 334     } else {
 335       return nullptr;
 336     }
 337   }
 338   void set_oop(MetaspaceObj* ptr, oop o) {
 339     MutexLocker ml(ScratchObjects_lock, Mutex::_no_safepoint_check_flag);
 340     OopHandle handle(Universe::vm_global(), o);
 341     bool is_new = put(ptr, handle);
 342     assert(is_new, "cannot set twice");
 343   }
 344   void remove_oop(MetaspaceObj* ptr) {
 345     MutexLocker ml(ScratchObjects_lock, Mutex::_no_safepoint_check_flag);
 346     OopHandle* handle = get(ptr);
 347     if (handle != nullptr) {
 348       handle->release(Universe::vm_global());
 349       remove(ptr);
 350     }
 351   }
 352 };
 353 
 354 void HeapShared::add_scratch_resolved_references(ConstantPool* src, objArrayOop dest) {
 355   _scratch_references_table->set_oop(src, dest);
 356 }
 357 
 358 objArrayOop HeapShared::scratch_resolved_references(ConstantPool* src) {
 359   return (objArrayOop)_scratch_references_table->get_oop(src);
 360 }
 361 
 362 void HeapShared::init_scratch_objects(TRAPS) {
 363   for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
 364     BasicType bt = (BasicType)i;
 365     if (!is_reference_type(bt)) {
 366       oop m = java_lang_Class::create_basic_type_mirror(type2name(bt), bt, CHECK);
 367       _scratch_basic_type_mirrors[i] = OopHandle(Universe::vm_global(), m);
 368     }
 369   }
 370   _scratch_java_mirror_table = new (mtClass)MetaspaceObjToOopHandleTable();
 371   _scratch_references_table = new (mtClass)MetaspaceObjToOopHandleTable();
 372 }
 373 
 374 oop HeapShared::scratch_java_mirror(BasicType t) {
 375   assert((uint)t < T_VOID+1, "range check");
 376   assert(!is_reference_type(t), "sanity");
 377   return _scratch_basic_type_mirrors[t].resolve();
 378 }
 379 
 380 oop HeapShared::scratch_java_mirror(Klass* k) {
 381   return _scratch_java_mirror_table->get_oop(k);
 382 }
 383 
 384 void HeapShared::set_scratch_java_mirror(Klass* k, oop mirror) {
 385   _scratch_java_mirror_table->set_oop(k, mirror);
 386 }
 387 
 388 void HeapShared::remove_scratch_objects(Klass* k) {
 389   // Klass is being deallocated. Java mirror can still be alive, and it should not
 390   // point to dead klass. We need to break the link from mirror to the Klass.
 391   // See how InstanceKlass::deallocate_contents does it for normal mirrors.
 392   oop mirror = _scratch_java_mirror_table->get_oop(k);
 393   if (mirror != nullptr) {
 394     java_lang_Class::set_klass(mirror, nullptr);
 395   }
 396   _scratch_java_mirror_table->remove_oop(k);
 397   if (k->is_instance_klass()) {
 398     _scratch_references_table->remove(InstanceKlass::cast(k)->constants());
 399   }
 400 }
 401 
 402 void HeapShared::archive_java_mirrors() {
 403   for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
 404     BasicType bt = (BasicType)i;
 405     if (!is_reference_type(bt)) {
 406       oop m = _scratch_basic_type_mirrors[i].resolve();
 407       assert(m != nullptr, "sanity");
 408       bool success = archive_reachable_objects_from(1, _default_subgraph_info, m);
 409       assert(success, "sanity");
 410 
 411       log_trace(cds, heap, mirror)(
 412         "Archived %s mirror object from " PTR_FORMAT,
 413         type2name(bt), p2i(m));
 414 
 415       Universe::set_archived_basic_type_mirror_index(bt, append_root(m));
 416     }
 417   }
 418 
 419   GrowableArray<Klass*>* klasses = ArchiveBuilder::current()->klasses();
 420   assert(klasses != nullptr, "sanity");
 421   for (int i = 0; i < klasses->length(); i++) {
 422     Klass* orig_k = klasses->at(i);
 423     oop m = scratch_java_mirror(orig_k);
 424     if (m != nullptr) {
 425       Klass* buffered_k = ArchiveBuilder::get_buffered_klass(orig_k);
 426       bool success = archive_reachable_objects_from(1, _default_subgraph_info, m);
 427       guarantee(success, "scratch mirrors must point to only archivable objects");
 428       buffered_k->set_archived_java_mirror(append_root(m));
 429       ResourceMark rm;
 430       log_trace(cds, heap, mirror)(
 431         "Archived %s mirror object from " PTR_FORMAT,
 432         buffered_k->external_name(), p2i(m));
 433 
 434       // archive the resolved_referenes array
 435       if (buffered_k->is_instance_klass()) {
 436         InstanceKlass* ik = InstanceKlass::cast(buffered_k);
 437         oop rr = ik->constants()->prepare_resolved_references_for_archiving();
 438         if (rr != nullptr && !ArchiveHeapWriter::is_too_large_to_archive(rr)) {
 439           bool success = HeapShared::archive_reachable_objects_from(1, _default_subgraph_info, rr);
 440           assert(success, "must be");
 441           int root_index = append_root(rr);
 442           ik->constants()->cache()->set_archived_references(root_index);
 443         }
 444       }
 445     }
 446   }
 447 }
 448 
 449 void HeapShared::archive_strings() {
 450   oop shared_strings_array = StringTable::init_shared_table(_dumped_interned_strings);
 451   bool success = archive_reachable_objects_from(1, _default_subgraph_info, shared_strings_array);
 452   // We must succeed because:
 453   // - _dumped_interned_strings do not contain any large strings.
 454   // - StringTable::init_shared_table() doesn't create any large arrays.
 455   assert(success, "shared strings array must not point to arrays or strings that are too large to archive");
 456   StringTable::set_shared_strings_array_index(append_root(shared_strings_array));
 457 }
 458 
 459 int HeapShared::archive_exception_instance(oop exception) {
 460   bool success = archive_reachable_objects_from(1, _default_subgraph_info, exception);
 461   assert(success, "sanity");
 462   return append_root(exception);
 463 }
 464 
 465 void HeapShared::mark_native_pointers(oop orig_obj) {
 466   if (java_lang_Class::is_instance(orig_obj)) {
 467     ArchiveHeapWriter::mark_native_pointer(orig_obj, java_lang_Class::klass_offset());
 468     ArchiveHeapWriter::mark_native_pointer(orig_obj, java_lang_Class::array_klass_offset());
 469   }
 470 }
 471 
 472 void HeapShared::get_pointer_info(oop src_obj, bool& has_oop_pointers, bool& has_native_pointers) {
 473   CachedOopInfo* info = archived_object_cache()->get(src_obj);
 474   assert(info != nullptr, "must be");
 475   has_oop_pointers = info->has_oop_pointers();
 476   has_native_pointers = info->has_native_pointers();
 477 }
 478 
 479 void HeapShared::set_has_native_pointers(oop src_obj) {
 480   CachedOopInfo* info = archived_object_cache()->get(src_obj);
 481   assert(info != nullptr, "must be");
 482   info->set_has_native_pointers();
 483 }
 484 
 485 void HeapShared::archive_objects(ArchiveHeapInfo *heap_info) {
 486   {
 487     NoSafepointVerifier nsv;
 488 
 489     _default_subgraph_info = init_subgraph_info(vmClasses::Object_klass(), false);
 490 
 491     // Cache for recording where the archived objects are copied to
 492     create_archived_object_cache();
 493 
 494     if (UseCompressedOops || UseG1GC) {
 495       log_info(cds)("Heap range = [" PTR_FORMAT " - "  PTR_FORMAT "]",
 496                     UseCompressedOops ? p2i(CompressedOops::begin()) :
 497                                         p2i((address)G1CollectedHeap::heap()->reserved().start()),
 498                     UseCompressedOops ? p2i(CompressedOops::end()) :
 499                                         p2i((address)G1CollectedHeap::heap()->reserved().end()));
 500     }
 501     copy_objects();
 502 
 503     CDSHeapVerifier::verify();
 504     check_default_subgraph_classes();
 505   }
 506 
 507   ArchiveHeapWriter::write(_pending_roots, heap_info);
 508 }
 509 
 510 void HeapShared::copy_interned_strings() {
 511   init_seen_objects_table();
 512 
 513   auto copier = [&] (oop s, bool value_ignored) {
 514     assert(s != nullptr, "sanity");
 515     assert(!ArchiveHeapWriter::is_string_too_large_to_archive(s), "large strings must have been filtered");
 516     bool success = archive_reachable_objects_from(1, _default_subgraph_info, s);
 517     assert(success, "must be");
 518     // Prevent string deduplication from changing the value field to
 519     // something not in the archive.
 520     java_lang_String::set_deduplication_forbidden(s);
 521   };
 522   _dumped_interned_strings->iterate_all(copier);
 523 
 524   delete_seen_objects_table();
 525 }
 526 
 527 void HeapShared::copy_special_objects() {
 528   // Archive special objects that do not belong to any subgraphs
 529   init_seen_objects_table();
 530   archive_java_mirrors();
 531   archive_strings();
 532   Universe::archive_exception_instances();
 533   delete_seen_objects_table();
 534 }
 535 
 536 void HeapShared::copy_objects() {
 537   assert(HeapShared::can_write(), "must be");
 538 
 539   copy_interned_strings();
 540   copy_special_objects();
 541 
 542   archive_object_subgraphs(archive_subgraph_entry_fields,
 543                            false /* is_full_module_graph */);
 544 
 545   if (CDSConfig::is_dumping_full_module_graph()) {
 546     archive_object_subgraphs(fmg_archive_subgraph_entry_fields,
 547                              true /* is_full_module_graph */);
 548     Modules::verify_archived_modules();
 549   }
 550 }
 551 
 552 //
 553 // Subgraph archiving support
 554 //
 555 HeapShared::DumpTimeKlassSubGraphInfoTable* HeapShared::_dump_time_subgraph_info_table = nullptr;
 556 HeapShared::RunTimeKlassSubGraphInfoTable   HeapShared::_run_time_subgraph_info_table;
 557 
 558 // Get the subgraph_info for Klass k. A new subgraph_info is created if
 559 // there is no existing one for k. The subgraph_info records the "buffered"
 560 // address of the class.
 561 KlassSubGraphInfo* HeapShared::init_subgraph_info(Klass* k, bool is_full_module_graph) {
 562   assert(CDSConfig::is_dumping_heap(), "dump time only");
 563   bool created;
 564   Klass* buffered_k = ArchiveBuilder::get_buffered_klass(k);
 565   KlassSubGraphInfo* info =
 566     _dump_time_subgraph_info_table->put_if_absent(k, KlassSubGraphInfo(buffered_k, is_full_module_graph),
 567                                                   &created);
 568   assert(created, "must not initialize twice");
 569   return info;
 570 }
 571 
 572 KlassSubGraphInfo* HeapShared::get_subgraph_info(Klass* k) {
 573   assert(CDSConfig::is_dumping_heap(), "dump time only");
 574   KlassSubGraphInfo* info = _dump_time_subgraph_info_table->get(k);
 575   assert(info != nullptr, "must have been initialized");
 576   return info;
 577 }
 578 
 579 // Add an entry field to the current KlassSubGraphInfo.
 580 void KlassSubGraphInfo::add_subgraph_entry_field(int static_field_offset, oop v) {
 581   assert(CDSConfig::is_dumping_heap(), "dump time only");
 582   if (_subgraph_entry_fields == nullptr) {
 583     _subgraph_entry_fields =
 584       new (mtClass) GrowableArray<int>(10, mtClass);
 585   }
 586   _subgraph_entry_fields->append(static_field_offset);
 587   _subgraph_entry_fields->append(HeapShared::append_root(v));
 588 }
 589 
 590 // Add the Klass* for an object in the current KlassSubGraphInfo's subgraphs.
 591 // Only objects of boot classes can be included in sub-graph.
 592 void KlassSubGraphInfo::add_subgraph_object_klass(Klass* orig_k) {
 593   assert(CDSConfig::is_dumping_heap(), "dump time only");
 594   Klass* buffered_k = ArchiveBuilder::get_buffered_klass(orig_k);
 595 
 596   if (_subgraph_object_klasses == nullptr) {
 597     _subgraph_object_klasses =
 598       new (mtClass) GrowableArray<Klass*>(50, mtClass);
 599   }
 600 
 601   assert(ArchiveBuilder::current()->is_in_buffer_space(buffered_k), "must be a shared class");
 602 
 603   if (_k == buffered_k) {
 604     // Don't add the Klass containing the sub-graph to it's own klass
 605     // initialization list.
 606     return;
 607   }
 608 
 609   if (buffered_k->is_instance_klass()) {
 610     assert(InstanceKlass::cast(buffered_k)->is_shared_boot_class(),
 611           "must be boot class");
 612     // vmClasses::xxx_klass() are not updated, need to check
 613     // the original Klass*
 614     if (orig_k == vmClasses::String_klass() ||
 615         orig_k == vmClasses::Object_klass()) {
 616       // Initialized early during VM initialization. No need to be added
 617       // to the sub-graph object class list.
 618       return;
 619     }
 620     check_allowed_klass(InstanceKlass::cast(orig_k));
 621   } else if (buffered_k->is_objArray_klass()) {
 622     Klass* abk = ObjArrayKlass::cast(buffered_k)->bottom_klass();
 623     if (abk->is_instance_klass()) {
 624       assert(InstanceKlass::cast(abk)->is_shared_boot_class(),
 625             "must be boot class");
 626       check_allowed_klass(InstanceKlass::cast(ObjArrayKlass::cast(orig_k)->bottom_klass()));
 627     }
 628     if (buffered_k == Universe::objectArrayKlass()) {
 629       // Initialized early during Universe::genesis. No need to be added
 630       // to the list.
 631       return;
 632     }
 633   } else {
 634     assert(buffered_k->is_typeArray_klass(), "must be");
 635     // Primitive type arrays are created early during Universe::genesis.
 636     return;
 637   }
 638 
 639   if (log_is_enabled(Debug, cds, heap)) {
 640     if (!_subgraph_object_klasses->contains(buffered_k)) {
 641       ResourceMark rm;
 642       log_debug(cds, heap)("Adding klass %s", orig_k->external_name());
 643     }
 644   }
 645 
 646   _subgraph_object_klasses->append_if_missing(buffered_k);
 647   _has_non_early_klasses |= is_non_early_klass(orig_k);
 648 }
 649 
 650 void KlassSubGraphInfo::check_allowed_klass(InstanceKlass* ik) {
 651   if (ik->module()->name() == vmSymbols::java_base()) {
 652     assert(ik->package() != nullptr, "classes in java.base cannot be in unnamed package");
 653     return;
 654   }
 655 
 656 #ifndef PRODUCT
 657   if (!ik->module()->is_named() && ik->package() == nullptr) {
 658     // This class is loaded by ArchiveHeapTestClass
 659     return;
 660   }
 661   const char* extra_msg = ", or in an unnamed package of an unnamed module";
 662 #else
 663   const char* extra_msg = "";
 664 #endif
 665 
 666   ResourceMark rm;
 667   log_error(cds, heap)("Class %s not allowed in archive heap. Must be in java.base%s",
 668                        ik->external_name(), extra_msg);
 669   MetaspaceShared::unrecoverable_writing_error();
 670 }
 671 
 672 bool KlassSubGraphInfo::is_non_early_klass(Klass* k) {
 673   if (k->is_objArray_klass()) {
 674     k = ObjArrayKlass::cast(k)->bottom_klass();
 675   }
 676   if (k->is_instance_klass()) {
 677     if (!SystemDictionaryShared::is_early_klass(InstanceKlass::cast(k))) {
 678       ResourceMark rm;
 679       log_info(cds, heap)("non-early: %s", k->external_name());
 680       return true;
 681     } else {
 682       return false;
 683     }
 684   } else {
 685     return false;
 686   }
 687 }
 688 
 689 // Initialize an archived subgraph_info_record from the given KlassSubGraphInfo.
 690 void ArchivedKlassSubGraphInfoRecord::init(KlassSubGraphInfo* info) {
 691   _k = info->klass();
 692   _entry_field_records = nullptr;
 693   _subgraph_object_klasses = nullptr;
 694   _is_full_module_graph = info->is_full_module_graph();
 695 
 696   if (_is_full_module_graph) {
 697     // Consider all classes referenced by the full module graph as early -- we will be
 698     // allocating objects of these classes during JVMTI early phase, so they cannot
 699     // be processed by (non-early) JVMTI ClassFileLoadHook
 700     _has_non_early_klasses = false;
 701   } else {
 702     _has_non_early_klasses = info->has_non_early_klasses();
 703   }
 704 
 705   if (_has_non_early_klasses) {
 706     ResourceMark rm;
 707     log_info(cds, heap)(
 708           "Subgraph of klass %s has non-early klasses and cannot be used when JVMTI ClassFileLoadHook is enabled",
 709           _k->external_name());
 710   }
 711 
 712   // populate the entry fields
 713   GrowableArray<int>* entry_fields = info->subgraph_entry_fields();
 714   if (entry_fields != nullptr) {
 715     int num_entry_fields = entry_fields->length();
 716     assert(num_entry_fields % 2 == 0, "sanity");
 717     _entry_field_records =
 718       ArchiveBuilder::new_ro_array<int>(num_entry_fields);
 719     for (int i = 0 ; i < num_entry_fields; i++) {
 720       _entry_field_records->at_put(i, entry_fields->at(i));
 721     }
 722   }
 723 
 724   // the Klasses of the objects in the sub-graphs
 725   GrowableArray<Klass*>* subgraph_object_klasses = info->subgraph_object_klasses();
 726   if (subgraph_object_klasses != nullptr) {
 727     int num_subgraphs_klasses = subgraph_object_klasses->length();
 728     _subgraph_object_klasses =
 729       ArchiveBuilder::new_ro_array<Klass*>(num_subgraphs_klasses);
 730     for (int i = 0; i < num_subgraphs_klasses; i++) {
 731       Klass* subgraph_k = subgraph_object_klasses->at(i);
 732       if (log_is_enabled(Info, cds, heap)) {
 733         ResourceMark rm;
 734         log_info(cds, heap)(
 735           "Archived object klass %s (%2d) => %s",
 736           _k->external_name(), i, subgraph_k->external_name());
 737       }
 738       _subgraph_object_klasses->at_put(i, subgraph_k);
 739       ArchivePtrMarker::mark_pointer(_subgraph_object_klasses->adr_at(i));
 740     }
 741   }
 742 
 743   ArchivePtrMarker::mark_pointer(&_k);
 744   ArchivePtrMarker::mark_pointer(&_entry_field_records);
 745   ArchivePtrMarker::mark_pointer(&_subgraph_object_klasses);
 746 }
 747 
 748 struct CopyKlassSubGraphInfoToArchive : StackObj {
 749   CompactHashtableWriter* _writer;
 750   CopyKlassSubGraphInfoToArchive(CompactHashtableWriter* writer) : _writer(writer) {}
 751 
 752   bool do_entry(Klass* klass, KlassSubGraphInfo& info) {
 753     if (info.subgraph_object_klasses() != nullptr || info.subgraph_entry_fields() != nullptr) {
 754       ArchivedKlassSubGraphInfoRecord* record =
 755         (ArchivedKlassSubGraphInfoRecord*)ArchiveBuilder::ro_region_alloc(sizeof(ArchivedKlassSubGraphInfoRecord));
 756       record->init(&info);
 757 
 758       Klass* buffered_k = ArchiveBuilder::get_buffered_klass(klass);
 759       unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary((address)buffered_k);
 760       u4 delta = ArchiveBuilder::current()->any_to_offset_u4(record);
 761       _writer->add(hash, delta);
 762     }
 763     return true; // keep on iterating
 764   }
 765 };
 766 
 767 // Build the records of archived subgraph infos, which include:
 768 // - Entry points to all subgraphs from the containing class mirror. The entry
 769 //   points are static fields in the mirror. For each entry point, the field
 770 //   offset, and value are recorded in the sub-graph
 771 //   info. The value is stored back to the corresponding field at runtime.
 772 // - A list of klasses that need to be loaded/initialized before archived
 773 //   java object sub-graph can be accessed at runtime.
 774 void HeapShared::write_subgraph_info_table() {
 775   // Allocate the contents of the hashtable(s) inside the RO region of the CDS archive.
 776   DumpTimeKlassSubGraphInfoTable* d_table = _dump_time_subgraph_info_table;
 777   CompactHashtableStats stats;
 778 
 779   _run_time_subgraph_info_table.reset();
 780 
 781   CompactHashtableWriter writer(d_table->_count, &stats);
 782   CopyKlassSubGraphInfoToArchive copy(&writer);
 783   d_table->iterate(&copy);
 784   writer.dump(&_run_time_subgraph_info_table, "subgraphs");
 785 
 786 #ifndef PRODUCT
 787   if (ArchiveHeapTestClass != nullptr) {
 788     size_t len = strlen(ArchiveHeapTestClass) + 1;
 789     Array<char>* array = ArchiveBuilder::new_ro_array<char>((int)len);
 790     strncpy(array->adr_at(0), ArchiveHeapTestClass, len);
 791     _archived_ArchiveHeapTestClass = array;
 792   }
 793 #endif
 794   if (log_is_enabled(Info, cds, heap)) {
 795     print_stats();
 796   }
 797 }
 798 
 799 void HeapShared::add_root_segment(objArrayOop segment_oop) {
 800   assert(segment_oop != nullptr, "must be");
 801   assert(ArchiveHeapLoader::is_in_use(), "must be");
 802   if (_root_segments == nullptr) {
 803     _root_segments = new GrowableArrayCHeap<OopHandle, mtClassShared>(10);
 804   }
 805   _root_segments->push(OopHandle(Universe::vm_global(), segment_oop));
 806 }
 807 
 808 void HeapShared::init_root_segment_sizes(int max_size_elems) {
 809   _root_segment_max_size_elems = max_size_elems;
 810 }
 811 
 812 void HeapShared::serialize_tables(SerializeClosure* soc) {
 813 
 814 #ifndef PRODUCT
 815   soc->do_ptr(&_archived_ArchiveHeapTestClass);
 816   if (soc->reading() && _archived_ArchiveHeapTestClass != nullptr) {
 817     _test_class_name = _archived_ArchiveHeapTestClass->adr_at(0);
 818     setup_test_class(_test_class_name);
 819   }
 820 #endif
 821 
 822   _run_time_subgraph_info_table.serialize_header(soc);
 823 }
 824 
 825 static void verify_the_heap(Klass* k, const char* which) {
 826   if (VerifyArchivedFields > 0) {
 827     ResourceMark rm;
 828     log_info(cds, heap)("Verify heap %s initializing static field(s) in %s",
 829                         which, k->external_name());
 830 
 831     VM_Verify verify_op;
 832     VMThread::execute(&verify_op);
 833 
 834     if (VerifyArchivedFields > 1 && is_init_completed()) {
 835       // At this time, the oop->klass() of some archived objects in the heap may not
 836       // have been loaded into the system dictionary yet. Nevertheless, oop->klass() should
 837       // have enough information (object size, oop maps, etc) so that a GC can be safely
 838       // performed.
 839       //
 840       // -XX:VerifyArchivedFields=2 force a GC to happen in such an early stage
 841       // to check for GC safety.
 842       log_info(cds, heap)("Trigger GC %s initializing static field(s) in %s",
 843                           which, k->external_name());
 844       FlagSetting fs1(VerifyBeforeGC, true);
 845       FlagSetting fs2(VerifyDuringGC, true);
 846       FlagSetting fs3(VerifyAfterGC,  true);
 847       Universe::heap()->collect(GCCause::_java_lang_system_gc);
 848     }
 849   }
 850 }
 851 
 852 // Before GC can execute, we must ensure that all oops reachable from HeapShared::roots()
 853 // have a valid klass. I.e., oopDesc::klass() must have already been resolved.
 854 //
 855 // Note: if a ArchivedKlassSubGraphInfoRecord contains non-early classes, and JVMTI
 856 // ClassFileLoadHook is enabled, it's possible for this class to be dynamically replaced. In
 857 // this case, we will not load the ArchivedKlassSubGraphInfoRecord and will clear its roots.
 858 void HeapShared::resolve_classes(JavaThread* current) {
 859   assert(CDSConfig::is_using_archive(), "runtime only!");
 860   if (!ArchiveHeapLoader::is_in_use()) {
 861     return; // nothing to do
 862   }
 863   resolve_classes_for_subgraphs(current, archive_subgraph_entry_fields);
 864   resolve_classes_for_subgraphs(current, fmg_archive_subgraph_entry_fields);
 865 }
 866 
 867 void HeapShared::resolve_classes_for_subgraphs(JavaThread* current, ArchivableStaticFieldInfo fields[]) {
 868   for (int i = 0; fields[i].valid(); i++) {
 869     ArchivableStaticFieldInfo* info = &fields[i];
 870     TempNewSymbol klass_name = SymbolTable::new_symbol(info->klass_name);
 871     InstanceKlass* k = SystemDictionaryShared::find_builtin_class(klass_name);
 872     assert(k != nullptr && k->is_shared_boot_class(), "sanity");
 873     resolve_classes_for_subgraph_of(current, k);
 874   }
 875 }
 876 
 877 void HeapShared::resolve_classes_for_subgraph_of(JavaThread* current, Klass* k) {
 878   JavaThread* THREAD = current;
 879   ExceptionMark em(THREAD);
 880   const ArchivedKlassSubGraphInfoRecord* record =
 881    resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/false, THREAD);
 882   if (HAS_PENDING_EXCEPTION) {
 883    CLEAR_PENDING_EXCEPTION;
 884   }
 885   if (record == nullptr) {
 886    clear_archived_roots_of(k);
 887   }
 888 }
 889 
 890 void HeapShared::initialize_from_archived_subgraph(JavaThread* current, Klass* k) {
 891   JavaThread* THREAD = current;
 892   if (!ArchiveHeapLoader::is_in_use()) {
 893     return; // nothing to do
 894   }
 895 
 896   if (k->name()->equals("jdk/internal/module/ArchivedModuleGraph") &&
 897       !CDSConfig::is_using_optimized_module_handling() &&
 898       // archive was created with --module-path
 899       ClassLoaderExt::num_module_paths() > 0) {
 900     // ArchivedModuleGraph was created with a --module-path that's different than the runtime --module-path.
 901     // Thus, it might contain references to modules that do not exist at runtime. We cannot use it.
 902     log_info(cds, heap)("Skip initializing ArchivedModuleGraph subgraph: is_using_optimized_module_handling=%s num_module_paths=%d",
 903                         BOOL_TO_STR(CDSConfig::is_using_optimized_module_handling()), ClassLoaderExt::num_module_paths());
 904     return;
 905   }
 906 
 907   ExceptionMark em(THREAD);
 908   const ArchivedKlassSubGraphInfoRecord* record =
 909     resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/true, THREAD);
 910 
 911   if (HAS_PENDING_EXCEPTION) {
 912     CLEAR_PENDING_EXCEPTION;
 913     // None of the field value will be set if there was an exception when initializing the classes.
 914     // The java code will not see any of the archived objects in the
 915     // subgraphs referenced from k in this case.
 916     return;
 917   }
 918 
 919   if (record != nullptr) {
 920     init_archived_fields_for(k, record);
 921   }
 922 }
 923 
 924 const ArchivedKlassSubGraphInfoRecord*
 925 HeapShared::resolve_or_init_classes_for_subgraph_of(Klass* k, bool do_init, TRAPS) {
 926   assert(!CDSConfig::is_dumping_heap(), "Should not be called when dumping heap");
 927 
 928   if (!k->is_shared()) {
 929     return nullptr;
 930   }
 931   unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(k);
 932   const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0);
 933 
 934 #ifndef PRODUCT
 935   if (_test_class_name != nullptr && k->name()->equals(_test_class_name) && record != nullptr) {
 936     _test_class = k;
 937     _test_class_record = record;
 938   }
 939 #endif
 940 
 941   // Initialize from archived data. Currently this is done only
 942   // during VM initialization time. No lock is needed.
 943   if (record == nullptr) {
 944     if (log_is_enabled(Info, cds, heap)) {
 945       ResourceMark rm(THREAD);
 946       log_info(cds, heap)("subgraph %s is not recorded",
 947                           k->external_name());
 948     }
 949     return nullptr;
 950   } else {
 951     if (record->is_full_module_graph() && !CDSConfig::is_using_full_module_graph()) {
 952       if (log_is_enabled(Info, cds, heap)) {
 953         ResourceMark rm(THREAD);
 954         log_info(cds, heap)("subgraph %s cannot be used because full module graph is disabled",
 955                             k->external_name());
 956       }
 957       return nullptr;
 958     }
 959 
 960     if (record->has_non_early_klasses() && JvmtiExport::should_post_class_file_load_hook()) {
 961       if (log_is_enabled(Info, cds, heap)) {
 962         ResourceMark rm(THREAD);
 963         log_info(cds, heap)("subgraph %s cannot be used because JVMTI ClassFileLoadHook is enabled",
 964                             k->external_name());
 965       }
 966       return nullptr;
 967     }
 968 
 969     if (log_is_enabled(Info, cds, heap)) {
 970       ResourceMark rm;
 971       log_info(cds, heap)("%s subgraph %s ", do_init ? "init" : "resolve", k->external_name());
 972     }
 973 
 974     resolve_or_init(k, do_init, CHECK_NULL);
 975 
 976     // Load/link/initialize the klasses of the objects in the subgraph.
 977     // nullptr class loader is used.
 978     Array<Klass*>* klasses = record->subgraph_object_klasses();
 979     if (klasses != nullptr) {
 980       for (int i = 0; i < klasses->length(); i++) {
 981         Klass* klass = klasses->at(i);
 982         if (!klass->is_shared()) {
 983           return nullptr;
 984         }
 985         resolve_or_init(klass, do_init, CHECK_NULL);
 986       }
 987     }
 988   }
 989 
 990   return record;
 991 }
 992 
 993 void HeapShared::resolve_or_init(Klass* k, bool do_init, TRAPS) {
 994   if (!do_init) {
 995     if (k->class_loader_data() == nullptr) {
 996       Klass* resolved_k = SystemDictionary::resolve_or_null(k->name(), CHECK);
 997       assert(resolved_k == k, "classes used by archived heap must not be replaced by JVMTI ClassFileLoadHook");
 998     }
 999   } else {
1000     assert(k->class_loader_data() != nullptr, "must have been resolved by HeapShared::resolve_classes");
1001     if (k->is_instance_klass()) {
1002       InstanceKlass* ik = InstanceKlass::cast(k);
1003       ik->initialize(CHECK);
1004     } else if (k->is_objArray_klass()) {
1005       ObjArrayKlass* oak = ObjArrayKlass::cast(k);
1006       oak->initialize(CHECK);
1007     }
1008   }
1009 }
1010 
1011 void HeapShared::init_archived_fields_for(Klass* k, const ArchivedKlassSubGraphInfoRecord* record) {
1012   verify_the_heap(k, "before");
1013 
1014   // Load the subgraph entry fields from the record and store them back to
1015   // the corresponding fields within the mirror.
1016   oop m = k->java_mirror();
1017   Array<int>* entry_field_records = record->entry_field_records();
1018   if (entry_field_records != nullptr) {
1019     int efr_len = entry_field_records->length();
1020     assert(efr_len % 2 == 0, "sanity");
1021     for (int i = 0; i < efr_len; i += 2) {
1022       int field_offset = entry_field_records->at(i);
1023       int root_index = entry_field_records->at(i+1);
1024       oop v = get_root(root_index, /*clear=*/true);
1025       m->obj_field_put(field_offset, v);
1026       log_debug(cds, heap)("  " PTR_FORMAT " init field @ %2d = " PTR_FORMAT, p2i(k), field_offset, p2i(v));
1027     }
1028 
1029     // Done. Java code can see the archived sub-graphs referenced from k's
1030     // mirror after this point.
1031     if (log_is_enabled(Info, cds, heap)) {
1032       ResourceMark rm;
1033       log_info(cds, heap)("initialize_from_archived_subgraph %s " PTR_FORMAT "%s",
1034                           k->external_name(), p2i(k), JvmtiExport::is_early_phase() ? " (early)" : "");
1035     }
1036   }
1037 
1038   verify_the_heap(k, "after ");
1039 }
1040 
1041 void HeapShared::clear_archived_roots_of(Klass* k) {
1042   unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(k);
1043   const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0);
1044   if (record != nullptr) {
1045     Array<int>* entry_field_records = record->entry_field_records();
1046     if (entry_field_records != nullptr) {
1047       int efr_len = entry_field_records->length();
1048       assert(efr_len % 2 == 0, "sanity");
1049       for (int i = 0; i < efr_len; i += 2) {
1050         int root_index = entry_field_records->at(i+1);
1051         clear_root(root_index);
1052       }
1053     }
1054   }
1055 }
1056 
1057 class WalkOopAndArchiveClosure: public BasicOopIterateClosure {
1058   int _level;
1059   bool _record_klasses_only;
1060   KlassSubGraphInfo* _subgraph_info;
1061   oop _referencing_obj;
1062 
1063   // The following are for maintaining a stack for determining
1064   // CachedOopInfo::_referrer
1065   static WalkOopAndArchiveClosure* _current;
1066   WalkOopAndArchiveClosure* _last;
1067  public:
1068   WalkOopAndArchiveClosure(int level,
1069                            bool record_klasses_only,
1070                            KlassSubGraphInfo* subgraph_info,
1071                            oop orig) :
1072     _level(level),
1073     _record_klasses_only(record_klasses_only),
1074     _subgraph_info(subgraph_info),
1075     _referencing_obj(orig) {
1076     _last = _current;
1077     _current = this;
1078   }
1079   ~WalkOopAndArchiveClosure() {
1080     _current = _last;
1081   }
1082   void do_oop(narrowOop *p) { WalkOopAndArchiveClosure::do_oop_work(p); }
1083   void do_oop(      oop *p) { WalkOopAndArchiveClosure::do_oop_work(p); }
1084 
1085  protected:
1086   template <class T> void do_oop_work(T *p) {
1087     oop obj = RawAccess<>::oop_load(p);
1088     if (!CompressedOops::is_null(obj)) {
1089       size_t field_delta = pointer_delta(p, _referencing_obj, sizeof(char));
1090 
1091       if (!_record_klasses_only && log_is_enabled(Debug, cds, heap)) {
1092         ResourceMark rm;
1093         log_debug(cds, heap)("(%d) %s[" SIZE_FORMAT "] ==> " PTR_FORMAT " size " SIZE_FORMAT " %s", _level,
1094                              _referencing_obj->klass()->external_name(), field_delta,
1095                              p2i(obj), obj->size() * HeapWordSize, obj->klass()->external_name());
1096         if (log_is_enabled(Trace, cds, heap)) {
1097           LogTarget(Trace, cds, heap) log;
1098           LogStream out(log);
1099           obj->print_on(&out);
1100         }
1101       }
1102 
1103       bool success = HeapShared::archive_reachable_objects_from(
1104           _level + 1, _subgraph_info, obj);
1105       assert(success, "VM should have exited with unarchivable objects for _level > 1");
1106     }
1107   }
1108 
1109  public:
1110   static WalkOopAndArchiveClosure* current()  { return _current;              }
1111   oop referencing_obj()                       { return _referencing_obj;      }
1112   KlassSubGraphInfo* subgraph_info()          { return _subgraph_info;        }
1113 };
1114 
1115 WalkOopAndArchiveClosure* WalkOopAndArchiveClosure::_current = nullptr;
1116 
1117 // Checks if an oop has any non-null oop fields
1118 class PointsToOopsChecker : public BasicOopIterateClosure {
1119   bool _result;
1120 
1121   template <class T> void check(T *p) {
1122     _result |= (HeapAccess<>::oop_load(p) != nullptr);
1123   }
1124 
1125 public:
1126   PointsToOopsChecker() : _result(false) {}
1127   void do_oop(narrowOop *p) { check(p); }
1128   void do_oop(      oop *p) { check(p); }
1129   bool result() { return _result; }
1130 };
1131 
1132 HeapShared::CachedOopInfo HeapShared::make_cached_oop_info(oop obj) {
1133   WalkOopAndArchiveClosure* walker = WalkOopAndArchiveClosure::current();
1134   oop referrer = (walker == nullptr) ? nullptr : walker->referencing_obj();
1135   PointsToOopsChecker points_to_oops_checker;
1136   obj->oop_iterate(&points_to_oops_checker);
1137   return CachedOopInfo(referrer, points_to_oops_checker.result());
1138 }
1139 
1140 // (1) If orig_obj has not been archived yet, archive it.
1141 // (2) If orig_obj has not been seen yet (since start_recording_subgraph() was called),
1142 //     trace all  objects that are reachable from it, and make sure these objects are archived.
1143 // (3) Record the klasses of all orig_obj and all reachable objects.
1144 bool HeapShared::archive_reachable_objects_from(int level,
1145                                                 KlassSubGraphInfo* subgraph_info,
1146                                                 oop orig_obj) {
1147   assert(orig_obj != nullptr, "must be");
1148 
1149   if (!JavaClasses::is_supported_for_archiving(orig_obj)) {
1150     // This object has injected fields that cannot be supported easily, so we disallow them for now.
1151     // If you get an error here, you probably made a change in the JDK library that has added
1152     // these objects that are referenced (directly or indirectly) by static fields.
1153     ResourceMark rm;
1154     log_error(cds, heap)("Cannot archive object of class %s", orig_obj->klass()->external_name());
1155     if (log_is_enabled(Trace, cds, heap)) {
1156       WalkOopAndArchiveClosure* walker = WalkOopAndArchiveClosure::current();
1157       if (walker != nullptr) {
1158         LogStream ls(Log(cds, heap)::trace());
1159         CDSHeapVerifier::trace_to_root(&ls, walker->referencing_obj());
1160       }
1161     }
1162     MetaspaceShared::unrecoverable_writing_error();
1163   }
1164 
1165   // java.lang.Class instances cannot be included in an archived object sub-graph. We only support
1166   // them as Klass::_archived_mirror because they need to be specially restored at run time.
1167   //
1168   // If you get an error here, you probably made a change in the JDK library that has added a Class
1169   // object that is referenced (directly or indirectly) by static fields.
1170   if (java_lang_Class::is_instance(orig_obj) && subgraph_info != _default_subgraph_info) {
1171     log_error(cds, heap)("(%d) Unknown java.lang.Class object is in the archived sub-graph", level);
1172     MetaspaceShared::unrecoverable_writing_error();
1173   }
1174 
1175   if (has_been_seen_during_subgraph_recording(orig_obj)) {
1176     // orig_obj has already been archived and traced. Nothing more to do.
1177     return true;
1178   } else {
1179     set_has_been_seen_during_subgraph_recording(orig_obj);
1180   }
1181 
1182   bool already_archived = has_been_archived(orig_obj);
1183   bool record_klasses_only = already_archived;
1184   if (!already_archived) {
1185     ++_num_new_archived_objs;
1186     if (!archive_object(orig_obj)) {
1187       // Skip archiving the sub-graph referenced from the current entry field.
1188       ResourceMark rm;
1189       log_error(cds, heap)(
1190         "Cannot archive the sub-graph referenced from %s object ("
1191         PTR_FORMAT ") size " SIZE_FORMAT ", skipped.",
1192         orig_obj->klass()->external_name(), p2i(orig_obj), orig_obj->size() * HeapWordSize);
1193       if (level == 1) {
1194         // Don't archive a subgraph root that's too big. For archives static fields, that's OK
1195         // as the Java code will take care of initializing this field dynamically.
1196         return false;
1197       } else {
1198         // We don't know how to handle an object that has been archived, but some of its reachable
1199         // objects cannot be archived. Bail out for now. We might need to fix this in the future if
1200         // we have a real use case.
1201         MetaspaceShared::unrecoverable_writing_error();
1202       }
1203     }
1204   }
1205 
1206   Klass *orig_k = orig_obj->klass();
1207   subgraph_info->add_subgraph_object_klass(orig_k);
1208 
1209   WalkOopAndArchiveClosure walker(level, record_klasses_only, subgraph_info, orig_obj);
1210   orig_obj->oop_iterate(&walker);
1211 
1212   if (CDSEnumKlass::is_enum_obj(orig_obj)) {
1213     CDSEnumKlass::handle_enum_obj(level + 1, subgraph_info, orig_obj);
1214   }
1215   return true;
1216 }
1217 
1218 //
1219 // Start from the given static field in a java mirror and archive the
1220 // complete sub-graph of java heap objects that are reached directly
1221 // or indirectly from the starting object by following references.
1222 // Sub-graph archiving restrictions (current):
1223 //
1224 // - All classes of objects in the archived sub-graph (including the
1225 //   entry class) must be boot class only.
1226 // - No java.lang.Class instance (java mirror) can be included inside
1227 //   an archived sub-graph. Mirror can only be the sub-graph entry object.
1228 //
1229 // The Java heap object sub-graph archiving process (see
1230 // WalkOopAndArchiveClosure):
1231 //
1232 // 1) Java object sub-graph archiving starts from a given static field
1233 // within a Class instance (java mirror). If the static field is a
1234 // reference field and points to a non-null java object, proceed to
1235 // the next step.
1236 //
1237 // 2) Archives the referenced java object. If an archived copy of the
1238 // current object already exists, updates the pointer in the archived
1239 // copy of the referencing object to point to the current archived object.
1240 // Otherwise, proceed to the next step.
1241 //
1242 // 3) Follows all references within the current java object and recursively
1243 // archive the sub-graph of objects starting from each reference.
1244 //
1245 // 4) Updates the pointer in the archived copy of referencing object to
1246 // point to the current archived object.
1247 //
1248 // 5) The Klass of the current java object is added to the list of Klasses
1249 // for loading and initializing before any object in the archived graph can
1250 // be accessed at runtime.
1251 //
1252 void HeapShared::archive_reachable_objects_from_static_field(InstanceKlass *k,
1253                                                              const char* klass_name,
1254                                                              int field_offset,
1255                                                              const char* field_name) {
1256   assert(CDSConfig::is_dumping_heap(), "dump time only");
1257   assert(k->is_shared_boot_class(), "must be boot class");
1258 
1259   oop m = k->java_mirror();
1260 
1261   KlassSubGraphInfo* subgraph_info = get_subgraph_info(k);
1262   oop f = m->obj_field(field_offset);
1263 
1264   log_debug(cds, heap)("Start archiving from: %s::%s (" PTR_FORMAT ")", klass_name, field_name, p2i(f));
1265 
1266   if (!CompressedOops::is_null(f)) {
1267     if (log_is_enabled(Trace, cds, heap)) {
1268       LogTarget(Trace, cds, heap) log;
1269       LogStream out(log);
1270       f->print_on(&out);
1271     }
1272 
1273     bool success = archive_reachable_objects_from(1, subgraph_info, f);
1274     if (!success) {
1275       log_error(cds, heap)("Archiving failed %s::%s (some reachable objects cannot be archived)",
1276                            klass_name, field_name);
1277     } else {
1278       // Note: the field value is not preserved in the archived mirror.
1279       // Record the field as a new subGraph entry point. The recorded
1280       // information is restored from the archive at runtime.
1281       subgraph_info->add_subgraph_entry_field(field_offset, f);
1282       log_info(cds, heap)("Archived field %s::%s => " PTR_FORMAT, klass_name, field_name, p2i(f));
1283     }
1284   } else {
1285     // The field contains null, we still need to record the entry point,
1286     // so it can be restored at runtime.
1287     subgraph_info->add_subgraph_entry_field(field_offset, nullptr);
1288   }
1289 }
1290 
1291 #ifndef PRODUCT
1292 class VerifySharedOopClosure: public BasicOopIterateClosure {
1293  public:
1294   void do_oop(narrowOop *p) { VerifySharedOopClosure::do_oop_work(p); }
1295   void do_oop(      oop *p) { VerifySharedOopClosure::do_oop_work(p); }
1296 
1297  protected:
1298   template <class T> void do_oop_work(T *p) {
1299     oop obj = RawAccess<>::oop_load(p);
1300     if (!CompressedOops::is_null(obj)) {
1301       HeapShared::verify_reachable_objects_from(obj);
1302     }
1303   }
1304 };
1305 
1306 void HeapShared::verify_subgraph_from_static_field(InstanceKlass* k, int field_offset) {
1307   assert(CDSConfig::is_dumping_heap(), "dump time only");
1308   assert(k->is_shared_boot_class(), "must be boot class");
1309 
1310   oop m = k->java_mirror();
1311   oop f = m->obj_field(field_offset);
1312   if (!CompressedOops::is_null(f)) {
1313     verify_subgraph_from(f);
1314   }
1315 }
1316 
1317 void HeapShared::verify_subgraph_from(oop orig_obj) {
1318   if (!has_been_archived(orig_obj)) {
1319     // It's OK for the root of a subgraph to be not archived. See comments in
1320     // archive_reachable_objects_from().
1321     return;
1322   }
1323 
1324   // Verify that all objects reachable from orig_obj are archived.
1325   init_seen_objects_table();
1326   verify_reachable_objects_from(orig_obj);
1327   delete_seen_objects_table();
1328 }
1329 
1330 void HeapShared::verify_reachable_objects_from(oop obj) {
1331   _num_total_verifications ++;
1332   if (!has_been_seen_during_subgraph_recording(obj)) {
1333     set_has_been_seen_during_subgraph_recording(obj);
1334     assert(has_been_archived(obj), "must be");
1335     VerifySharedOopClosure walker;
1336     obj->oop_iterate(&walker);
1337   }
1338 }
1339 #endif
1340 
1341 // The "default subgraph" contains special objects (see heapShared.hpp) that
1342 // can be accessed before we load any Java classes (including java/lang/Class).
1343 // Make sure that these are only instances of the very few specific types
1344 // that we can handle.
1345 void HeapShared::check_default_subgraph_classes() {
1346   GrowableArray<Klass*>* klasses = _default_subgraph_info->subgraph_object_klasses();
1347   int num = klasses->length();
1348   for (int i = 0; i < num; i++) {
1349     Klass* subgraph_k = klasses->at(i);
1350     if (log_is_enabled(Info, cds, heap)) {
1351       ResourceMark rm;
1352       log_info(cds, heap)(
1353           "Archived object klass (default subgraph %d) => %s",
1354           i, subgraph_k->external_name());
1355     }
1356 
1357     Symbol* name = ArchiveBuilder::current()->get_source_addr(subgraph_k->name());
1358     guarantee(name == vmSymbols::java_lang_Class() ||
1359               name == vmSymbols::java_lang_String() ||
1360               name == vmSymbols::java_lang_ArithmeticException() ||
1361               name == vmSymbols::java_lang_NullPointerException() ||
1362               name == vmSymbols::java_lang_InternalError() ||
1363               name == vmSymbols::java_lang_ArrayIndexOutOfBoundsException() ||
1364               name == vmSymbols::java_lang_ArrayStoreException() ||
1365               name == vmSymbols::java_lang_ClassCastException() ||
1366               name == vmSymbols::object_array_signature() ||
1367               name == vmSymbols::byte_array_signature() ||
1368               name == vmSymbols::char_array_signature(),
1369               "default subgraph can have only these objects");
1370   }
1371 }
1372 
1373 HeapShared::SeenObjectsTable* HeapShared::_seen_objects_table = nullptr;
1374 int HeapShared::_num_new_walked_objs;
1375 int HeapShared::_num_new_archived_objs;
1376 int HeapShared::_num_old_recorded_klasses;
1377 
1378 int HeapShared::_num_total_subgraph_recordings = 0;
1379 int HeapShared::_num_total_walked_objs = 0;
1380 int HeapShared::_num_total_archived_objs = 0;
1381 int HeapShared::_num_total_recorded_klasses = 0;
1382 int HeapShared::_num_total_verifications = 0;
1383 
1384 bool HeapShared::has_been_seen_during_subgraph_recording(oop obj) {
1385   return _seen_objects_table->get(obj) != nullptr;
1386 }
1387 
1388 void HeapShared::set_has_been_seen_during_subgraph_recording(oop obj) {
1389   assert(!has_been_seen_during_subgraph_recording(obj), "sanity");
1390   _seen_objects_table->put_when_absent(obj, true);
1391   _seen_objects_table->maybe_grow();
1392   ++ _num_new_walked_objs;
1393 }
1394 
1395 void HeapShared::start_recording_subgraph(InstanceKlass *k, const char* class_name, bool is_full_module_graph) {
1396   log_info(cds, heap)("Start recording subgraph(s) for archived fields in %s", class_name);
1397   init_subgraph_info(k, is_full_module_graph);
1398   init_seen_objects_table();
1399   _num_new_walked_objs = 0;
1400   _num_new_archived_objs = 0;
1401   _num_old_recorded_klasses = get_subgraph_info(k)->num_subgraph_object_klasses();
1402 }
1403 
1404 void HeapShared::done_recording_subgraph(InstanceKlass *k, const char* class_name) {
1405   int num_new_recorded_klasses = get_subgraph_info(k)->num_subgraph_object_klasses() -
1406     _num_old_recorded_klasses;
1407   log_info(cds, heap)("Done recording subgraph(s) for archived fields in %s: "
1408                       "walked %d objs, archived %d new objs, recorded %d classes",
1409                       class_name, _num_new_walked_objs, _num_new_archived_objs,
1410                       num_new_recorded_klasses);
1411 
1412   delete_seen_objects_table();
1413 
1414   _num_total_subgraph_recordings ++;
1415   _num_total_walked_objs      += _num_new_walked_objs;
1416   _num_total_archived_objs    += _num_new_archived_objs;
1417   _num_total_recorded_klasses +=  num_new_recorded_klasses;
1418 }
1419 
1420 class ArchivableStaticFieldFinder: public FieldClosure {
1421   InstanceKlass* _ik;
1422   Symbol* _field_name;
1423   bool _found;
1424   int _offset;
1425 public:
1426   ArchivableStaticFieldFinder(InstanceKlass* ik, Symbol* field_name) :
1427     _ik(ik), _field_name(field_name), _found(false), _offset(-1) {}
1428 
1429   virtual void do_field(fieldDescriptor* fd) {
1430     if (fd->name() == _field_name) {
1431       assert(!_found, "fields can never be overloaded");
1432       if (is_reference_type(fd->field_type())) {
1433         _found = true;
1434         _offset = fd->offset();
1435       }
1436     }
1437   }
1438   bool found()     { return _found;  }
1439   int offset()     { return _offset; }
1440 };
1441 
1442 void HeapShared::init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[],
1443                                             TRAPS) {
1444   for (int i = 0; fields[i].valid(); i++) {
1445     ArchivableStaticFieldInfo* info = &fields[i];
1446     TempNewSymbol klass_name =  SymbolTable::new_symbol(info->klass_name);
1447     TempNewSymbol field_name =  SymbolTable::new_symbol(info->field_name);
1448     ResourceMark rm; // for stringStream::as_string() etc.
1449 
1450 #ifndef PRODUCT
1451     bool is_test_class = (ArchiveHeapTestClass != nullptr) && (strcmp(info->klass_name, ArchiveHeapTestClass) == 0);
1452     const char* test_class_name = ArchiveHeapTestClass;
1453 #else
1454     bool is_test_class = false;
1455     const char* test_class_name = ""; // avoid C++ printf checks warnings.
1456 #endif
1457 
1458     if (is_test_class) {
1459       log_warning(cds)("Loading ArchiveHeapTestClass %s ...", test_class_name);
1460     }
1461 
1462     Klass* k = SystemDictionary::resolve_or_fail(klass_name, true, THREAD);
1463     if (HAS_PENDING_EXCEPTION) {
1464       CLEAR_PENDING_EXCEPTION;
1465       stringStream st;
1466       st.print("Fail to initialize archive heap: %s cannot be loaded by the boot loader", info->klass_name);
1467       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
1468     }
1469 
1470     if (!k->is_instance_klass()) {
1471       stringStream st;
1472       st.print("Fail to initialize archive heap: %s is not an instance class", info->klass_name);
1473       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
1474     }
1475 
1476     InstanceKlass* ik = InstanceKlass::cast(k);
1477     assert(InstanceKlass::cast(ik)->is_shared_boot_class(),
1478            "Only support boot classes");
1479 
1480     if (is_test_class) {
1481       if (ik->module()->is_named()) {
1482         // We don't want ArchiveHeapTestClass to be abused to easily load/initialize arbitrary
1483         // core-lib classes. You need to at least append to the bootclasspath.
1484         stringStream st;
1485         st.print("ArchiveHeapTestClass %s is not in unnamed module", test_class_name);
1486         THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
1487       }
1488 
1489       if (ik->package() != nullptr) {
1490         // This restriction makes HeapShared::is_a_test_class_in_unnamed_module() easy.
1491         stringStream st;
1492         st.print("ArchiveHeapTestClass %s is not in unnamed package", test_class_name);
1493         THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
1494       }
1495     } else {
1496       if (ik->module()->name() != vmSymbols::java_base()) {
1497         // We don't want to deal with cases when a module is unavailable at runtime.
1498         // FUTURE -- load from archived heap only when module graph has not changed
1499         //           between dump and runtime.
1500         stringStream st;
1501         st.print("%s is not in java.base module", info->klass_name);
1502         THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
1503       }
1504     }
1505 
1506     if (is_test_class) {
1507       log_warning(cds)("Initializing ArchiveHeapTestClass %s ...", test_class_name);
1508     }
1509     ik->initialize(CHECK);
1510 
1511     ArchivableStaticFieldFinder finder(ik, field_name);
1512     ik->do_local_static_fields(&finder);
1513     if (!finder.found()) {
1514       stringStream st;
1515       st.print("Unable to find the static T_OBJECT field %s::%s", info->klass_name, info->field_name);
1516       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
1517     }
1518 
1519     info->klass = ik;
1520     info->offset = finder.offset();
1521   }
1522 }
1523 
1524 void HeapShared::init_subgraph_entry_fields(TRAPS) {
1525   assert(HeapShared::can_write(), "must be");
1526   _dump_time_subgraph_info_table = new (mtClass)DumpTimeKlassSubGraphInfoTable();
1527   init_subgraph_entry_fields(archive_subgraph_entry_fields, CHECK);
1528   if (CDSConfig::is_dumping_full_module_graph()) {
1529     init_subgraph_entry_fields(fmg_archive_subgraph_entry_fields, CHECK);
1530   }
1531 }
1532 
1533 #ifndef PRODUCT
1534 void HeapShared::setup_test_class(const char* test_class_name) {
1535   ArchivableStaticFieldInfo* p = archive_subgraph_entry_fields;
1536   int num_slots = sizeof(archive_subgraph_entry_fields) / sizeof(ArchivableStaticFieldInfo);
1537   assert(p[num_slots - 2].klass_name == nullptr, "must have empty slot that's patched below");
1538   assert(p[num_slots - 1].klass_name == nullptr, "must have empty slot that marks the end of the list");
1539 
1540   if (test_class_name != nullptr) {
1541     p[num_slots - 2].klass_name = test_class_name;
1542     p[num_slots - 2].field_name = ARCHIVE_TEST_FIELD_NAME;
1543   }
1544 }
1545 
1546 // See if ik is one of the test classes that are pulled in by -XX:ArchiveHeapTestClass
1547 // during runtime. This may be called before the module system is initialized so
1548 // we cannot rely on InstanceKlass::module(), etc.
1549 bool HeapShared::is_a_test_class_in_unnamed_module(Klass* ik) {
1550   if (_test_class != nullptr) {
1551     if (ik == _test_class) {
1552       return true;
1553     }
1554     Array<Klass*>* klasses = _test_class_record->subgraph_object_klasses();
1555     if (klasses == nullptr) {
1556       return false;
1557     }
1558 
1559     for (int i = 0; i < klasses->length(); i++) {
1560       Klass* k = klasses->at(i);
1561       if (k == ik) {
1562         Symbol* name;
1563         if (k->is_instance_klass()) {
1564           name = InstanceKlass::cast(k)->name();
1565         } else if (k->is_objArray_klass()) {
1566           Klass* bk = ObjArrayKlass::cast(k)->bottom_klass();
1567           if (!bk->is_instance_klass()) {
1568             return false;
1569           }
1570           name = bk->name();
1571         } else {
1572           return false;
1573         }
1574 
1575         // See KlassSubGraphInfo::check_allowed_klass() - only two types of
1576         // classes are allowed:
1577         //   (A) java.base classes (which must not be in the unnamed module)
1578         //   (B) test classes which must be in the unnamed package of the unnamed module.
1579         // So if we see a '/' character in the class name, it must be in (A);
1580         // otherwise it must be in (B).
1581         if (name->index_of_at(0, "/", 1)  >= 0) {
1582           return false; // (A)
1583         }
1584 
1585         return true; // (B)
1586       }
1587     }
1588   }
1589 
1590   return false;
1591 }
1592 #endif
1593 
1594 void HeapShared::init_for_dumping(TRAPS) {
1595   if (HeapShared::can_write()) {
1596     setup_test_class(ArchiveHeapTestClass);
1597     _dumped_interned_strings = new (mtClass)DumpedInternedStrings(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE);
1598     init_subgraph_entry_fields(CHECK);
1599   }
1600 }
1601 
1602 void HeapShared::archive_object_subgraphs(ArchivableStaticFieldInfo fields[],
1603                                           bool is_full_module_graph) {
1604   _num_total_subgraph_recordings = 0;
1605   _num_total_walked_objs = 0;
1606   _num_total_archived_objs = 0;
1607   _num_total_recorded_klasses = 0;
1608   _num_total_verifications = 0;
1609 
1610   // For each class X that has one or more archived fields:
1611   // [1] Dump the subgraph of each archived field
1612   // [2] Create a list of all the class of the objects that can be reached
1613   //     by any of these static fields.
1614   //     At runtime, these classes are initialized before X's archived fields
1615   //     are restored by HeapShared::initialize_from_archived_subgraph().
1616   for (int i = 0; fields[i].valid(); ) {
1617     ArchivableStaticFieldInfo* info = &fields[i];
1618     const char* klass_name = info->klass_name;
1619 
1620     if (CDSConfig::is_valhalla_preview() && strcmp(klass_name, "jdk/internal/module/ArchivedModuleGraph") == 0) {
1621       // FIXME -- ArchivedModuleGraph doesn't work when java.base is patched with valhalla classes.
1622       i++;
1623       continue;
1624     }
1625 
1626     start_recording_subgraph(info->klass, klass_name, is_full_module_graph);
1627 
1628     // If you have specified consecutive fields of the same klass in
1629     // fields[], these will be archived in the same
1630     // {start_recording_subgraph ... done_recording_subgraph} pass to
1631     // save time.
1632     for (; fields[i].valid(); i++) {
1633       ArchivableStaticFieldInfo* f = &fields[i];
1634       if (f->klass_name != klass_name) {
1635         break;
1636       }
1637 
1638       archive_reachable_objects_from_static_field(f->klass, f->klass_name,
1639                                                   f->offset, f->field_name);
1640     }
1641     done_recording_subgraph(info->klass, klass_name);
1642   }
1643 
1644   log_info(cds, heap)("Archived subgraph records = %d",
1645                       _num_total_subgraph_recordings);
1646   log_info(cds, heap)("  Walked %d objects", _num_total_walked_objs);
1647   log_info(cds, heap)("  Archived %d objects", _num_total_archived_objs);
1648   log_info(cds, heap)("  Recorded %d klasses", _num_total_recorded_klasses);
1649 
1650 #ifndef PRODUCT
1651   for (int i = 0; fields[i].valid(); i++) {
1652     ArchivableStaticFieldInfo* f = &fields[i];
1653     verify_subgraph_from_static_field(f->klass, f->offset);
1654   }
1655   log_info(cds, heap)("  Verified %d references", _num_total_verifications);
1656 #endif
1657 }
1658 
1659 // Not all the strings in the global StringTable are dumped into the archive, because
1660 // some of those strings may be only referenced by classes that are excluded from
1661 // the archive. We need to explicitly mark the strings that are:
1662 //   [1] used by classes that WILL be archived;
1663 //   [2] included in the SharedArchiveConfigFile.
1664 void HeapShared::add_to_dumped_interned_strings(oop string) {
1665   assert_at_safepoint(); // DumpedInternedStrings uses raw oops
1666   assert(!ArchiveHeapWriter::is_string_too_large_to_archive(string), "must be");
1667   bool created;
1668   _dumped_interned_strings->put_if_absent(string, true, &created);
1669   if (created) {
1670     _dumped_interned_strings->maybe_grow();
1671   }
1672 }
1673 
1674 #ifndef PRODUCT
1675 // At dump-time, find the location of all the non-null oop pointers in an archived heap
1676 // region. This way we can quickly relocate all the pointers without using
1677 // BasicOopIterateClosure at runtime.
1678 class FindEmbeddedNonNullPointers: public BasicOopIterateClosure {
1679   void* _start;
1680   BitMap *_oopmap;
1681   int _num_total_oops;
1682   int _num_null_oops;
1683  public:
1684   FindEmbeddedNonNullPointers(void* start, BitMap* oopmap)
1685     : _start(start), _oopmap(oopmap), _num_total_oops(0),  _num_null_oops(0) {}
1686 
1687   virtual void do_oop(narrowOop* p) {
1688     assert(UseCompressedOops, "sanity");
1689     _num_total_oops ++;
1690     narrowOop v = *p;
1691     if (!CompressedOops::is_null(v)) {
1692       size_t idx = p - (narrowOop*)_start;
1693       _oopmap->set_bit(idx);
1694     } else {
1695       _num_null_oops ++;
1696     }
1697   }
1698   virtual void do_oop(oop* p) {
1699     assert(!UseCompressedOops, "sanity");
1700     _num_total_oops ++;
1701     if ((*p) != nullptr) {
1702       size_t idx = p - (oop*)_start;
1703       _oopmap->set_bit(idx);
1704     } else {
1705       _num_null_oops ++;
1706     }
1707   }
1708   int num_total_oops() const { return _num_total_oops; }
1709   int num_null_oops()  const { return _num_null_oops; }
1710 };
1711 #endif
1712 
1713 void HeapShared::count_allocation(size_t size) {
1714   _total_obj_count ++;
1715   _total_obj_size += size;
1716   for (int i = 0; i < ALLOC_STAT_SLOTS; i++) {
1717     if (size <= (size_t(1) << i)) {
1718       _alloc_count[i] ++;
1719       _alloc_size[i] += size;
1720       return;
1721     }
1722   }
1723 }
1724 
1725 static double avg_size(size_t size, size_t count) {
1726   double avg = 0;
1727   if (count > 0) {
1728     avg = double(size * HeapWordSize) / double(count);
1729   }
1730   return avg;
1731 }
1732 
1733 void HeapShared::print_stats() {
1734   size_t huge_count = _total_obj_count;
1735   size_t huge_size = _total_obj_size;
1736 
1737   for (int i = 0; i < ALLOC_STAT_SLOTS; i++) {
1738     size_t byte_size_limit = (size_t(1) << i) * HeapWordSize;
1739     size_t count = _alloc_count[i];
1740     size_t size = _alloc_size[i];
1741     log_info(cds, heap)(SIZE_FORMAT_W(8) " objects are <= " SIZE_FORMAT_W(-6)
1742                         " bytes (total " SIZE_FORMAT_W(8) " bytes, avg %8.1f bytes)",
1743                         count, byte_size_limit, size * HeapWordSize, avg_size(size, count));
1744     huge_count -= count;
1745     huge_size -= size;
1746   }
1747 
1748   log_info(cds, heap)(SIZE_FORMAT_W(8) " huge  objects               (total "  SIZE_FORMAT_W(8) " bytes"
1749                       ", avg %8.1f bytes)",
1750                       huge_count, huge_size * HeapWordSize,
1751                       avg_size(huge_size, huge_count));
1752   log_info(cds, heap)(SIZE_FORMAT_W(8) " total objects               (total "  SIZE_FORMAT_W(8) " bytes"
1753                       ", avg %8.1f bytes)",
1754                       _total_obj_count, _total_obj_size * HeapWordSize,
1755                       avg_size(_total_obj_size, _total_obj_count));
1756 }
1757 
1758 bool HeapShared::is_archived_boot_layer_available(JavaThread* current) {
1759   TempNewSymbol klass_name = SymbolTable::new_symbol(ARCHIVED_BOOT_LAYER_CLASS);
1760   InstanceKlass* k = SystemDictionary::find_instance_klass(current, klass_name, Handle(), Handle());
1761   if (k == nullptr) {
1762     return false;
1763   } else {
1764     TempNewSymbol field_name = SymbolTable::new_symbol(ARCHIVED_BOOT_LAYER_FIELD);
1765     TempNewSymbol field_signature = SymbolTable::new_symbol("Ljdk/internal/module/ArchivedBootLayer;");
1766     fieldDescriptor fd;
1767     if (k->find_field(field_name, field_signature, true, &fd) != nullptr) {
1768       oop m = k->java_mirror();
1769       oop f = m->obj_field(fd.offset());
1770       if (CompressedOops::is_null(f)) {
1771         return false;
1772       }
1773     } else {
1774       return false;
1775     }
1776   }
1777   return true;
1778 }
1779 
1780 #endif // INCLUDE_CDS_JAVA_HEAP