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