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