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