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 static const char* java_lang_invoke_core_klasses[] = {
1375   "java/lang/invoke/Invokers$Holder",
1376   "java/lang/invoke/MethodHandle",
1377   "java/lang/invoke/MethodHandleNatives",
1378   "java/lang/invoke/DirectMethodHandle$Holder",
1379   "java/lang/invoke/DelegatingMethodHandle$Holder",
1380   "java/lang/invoke/LambdaForm$Holder",
1381   "java/lang/invoke/BoundMethodHandle$Species_L",
1382 };
1383 
1384 void HeapShared::initialize_java_lang_invoke(TRAPS) {
1385   if (CDSConfig::is_using_aot_linked_classes() || CDSConfig::is_dumping_method_handles()) {
1386     int len = sizeof(java_lang_invoke_core_klasses)/sizeof(char*);
1387     for (int i = 0; i < len; i++) {
1388       resolve_or_init(java_lang_invoke_core_klasses[i], true, CHECK);
1389     }
1390   }
1391 }
1392 
1393 bool HeapShared::is_core_java_lang_invoke_klass(InstanceKlass* klass) {
1394   // TODO: Crude, rewrite using Symbols or vmClasses instead
1395   ResourceMark rm;
1396   char* s2 = klass->name()->as_C_string();
1397   int len = sizeof(java_lang_invoke_core_klasses)/sizeof(char*);
1398   for (int i = 0; i < len; i++) {
1399     if (strcmp(java_lang_invoke_core_klasses[i], s2) == 0) {
1400       return true;
1401     }
1402   }
1403   return false;
1404 }
1405 
1406 // Initialize the InstanceKlasses of objects that are reachable from the following roots:
1407 //   - interned strings
1408 //   - Klass::java_mirror() -- including aot-initialized mirrors such as those of Enum klasses.
1409 //   - ConstantPool::resolved_references()
1410 //   - Universe::<xxx>_exception_instance()
1411 //
1412 // For example, if this enum class is initialized at AOT cache assembly time:
1413 //
1414 //    enum Fruit {
1415 //       APPLE, ORANGE, BANANA;
1416 //       static final Set<Fruit> HAVE_SEEDS = new HashSet<>(Arrays.asList(APPLE, ORANGE));
1417 //   }
1418 //
1419 // the aot-initialized mirror of Fruit has a static field that references HashSet, which
1420 // should be initialized before any Java code can access the Fruit class. Note that
1421 // HashSet itself doesn't necessary need to be an aot-initialized class.
1422 void HeapShared::init_classes_for_special_subgraph(Handle class_loader, TRAPS) {
1423   if (!ArchiveHeapLoader::is_in_use()) {
1424     return;
1425   }
1426 
1427   assert( _run_time_special_subgraph != nullptr, "must be");
1428   Array<Klass*>* klasses = _run_time_special_subgraph->subgraph_object_klasses();
1429   if (klasses != nullptr) {
1430     for (int pass = 0; pass < 2; pass ++) {
1431       for (int i = 0; i < klasses->length(); i++) {
1432         Klass* k = klasses->at(i);
1433         if (k->class_loader_data() == nullptr) {
1434           // This class is not yet loaded. We will initialize it in a later phase.
1435           // For example, we have loaded only AOTLinkedClassCategory::BOOT1 classes
1436           // but k is part of AOTLinkedClassCategory::BOOT2.
1437           continue;
1438         }
1439         if (k->class_loader() == class_loader()) {
1440           if (pass == 0) {
1441             if (k->is_instance_klass()) {
1442               InstanceKlass::cast(k)->link_class(CHECK);
1443             }
1444           } else {
1445             resolve_or_init(k, /*do_init*/true, CHECK);
1446           }
1447         }
1448       }
1449     }
1450   }
1451 }
1452 
1453 void HeapShared::initialize_from_archived_subgraph(JavaThread* current, Klass* k) {
1454   JavaThread* THREAD = current;
1455   if (!ArchiveHeapLoader::is_in_use()) {
1456     return; // nothing to do
1457   }
1458 
1459   if (k->name()->equals("jdk/internal/module/ArchivedModuleGraph") &&
1460       !CDSConfig::is_using_optimized_module_handling() &&
1461       // archive was created with --module-path
1462       AOTClassLocationConfig::runtime()->num_module_paths() > 0) {
1463     // ArchivedModuleGraph was created with a --module-path that's different than the runtime --module-path.
1464     // Thus, it might contain references to modules that do not exist at runtime. We cannot use it.
1465     log_info(aot, heap)("Skip initializing ArchivedModuleGraph subgraph: is_using_optimized_module_handling=%s num_module_paths=%d",
1466                         BOOL_TO_STR(CDSConfig::is_using_optimized_module_handling()),
1467                         AOTClassLocationConfig::runtime()->num_module_paths());
1468     return;
1469   }
1470 
1471   ExceptionMark em(THREAD);
1472   const ArchivedKlassSubGraphInfoRecord* record =
1473     resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/true, THREAD);
1474 
1475   if (HAS_PENDING_EXCEPTION) {
1476     CLEAR_PENDING_EXCEPTION;
1477     // None of the field value will be set if there was an exception when initializing the classes.
1478     // The java code will not see any of the archived objects in the
1479     // subgraphs referenced from k in this case.
1480     return;
1481   }
1482 
1483   if (record != nullptr) {
1484     init_archived_fields_for(k, record);
1485   }
1486 }
1487 
1488 const ArchivedKlassSubGraphInfoRecord*
1489 HeapShared::resolve_or_init_classes_for_subgraph_of(Klass* k, bool do_init, TRAPS) {
1490   assert(!CDSConfig::is_dumping_heap(), "Should not be called when dumping heap");
1491 
1492   if (!k->in_aot_cache()) {
1493     return nullptr;
1494   }
1495   unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(k);
1496   const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0);
1497 
1498 #ifndef PRODUCT
1499   if (_test_class_name != nullptr && k->name()->equals(_test_class_name) && record != nullptr) {
1500     _test_class = k;
1501     _test_class_record = record;
1502   }
1503 #endif
1504 
1505   // Initialize from archived data. Currently this is done only
1506   // during VM initialization time. No lock is needed.
1507   if (record == nullptr) {
1508     if (log_is_enabled(Info, aot, heap)) {
1509       ResourceMark rm(THREAD);
1510       log_info(aot, heap)("subgraph %s is not recorded",
1511                           k->external_name());
1512     }
1513     return nullptr;
1514   } else {
1515     if (record->is_full_module_graph() && !CDSConfig::is_using_full_module_graph()) {
1516       if (log_is_enabled(Info, aot, heap)) {
1517         ResourceMark rm(THREAD);
1518         log_info(aot, heap)("subgraph %s cannot be used because full module graph is disabled",
1519                             k->external_name());
1520       }
1521       return nullptr;
1522     }
1523 
1524     if (record->has_non_early_klasses() && JvmtiExport::should_post_class_file_load_hook()) {
1525       if (log_is_enabled(Info, aot, heap)) {
1526         ResourceMark rm(THREAD);
1527         log_info(aot, heap)("subgraph %s cannot be used because JVMTI ClassFileLoadHook is enabled",
1528                             k->external_name());
1529       }
1530       return nullptr;
1531     }
1532 
1533     if (log_is_enabled(Info, aot, heap)) {
1534       ResourceMark rm;
1535       log_info(aot, heap)("%s subgraph %s ", do_init ? "init" : "resolve", k->external_name());
1536     }
1537 
1538     resolve_or_init(k, do_init, CHECK_NULL);
1539 
1540     // Load/link/initialize the klasses of the objects in the subgraph.
1541     // nullptr class loader is used.
1542     Array<Klass*>* klasses = record->subgraph_object_klasses();
1543     if (klasses != nullptr) {
1544       for (int i = 0; i < klasses->length(); i++) {
1545         Klass* klass = klasses->at(i);
1546         if (!klass->in_aot_cache()) {
1547           return nullptr;
1548         }
1549         resolve_or_init(klass, do_init, CHECK_NULL);
1550       }
1551     }
1552   }
1553 
1554   return record;
1555 }
1556 
1557 void HeapShared::resolve_or_init(const char* klass_name, bool do_init, TRAPS) {
1558   TempNewSymbol klass_name_sym =  SymbolTable::new_symbol(klass_name);
1559   InstanceKlass* k = SystemDictionaryShared::find_builtin_class(klass_name_sym);
1560   if (k == nullptr) {
1561     return;
1562   }
1563   assert(k->defined_by_boot_loader(), "sanity");
1564   resolve_or_init(k, false, CHECK);
1565   if (do_init) {
1566     resolve_or_init(k, true, CHECK);
1567   }
1568 }
1569 
1570 void HeapShared::resolve_or_init(Klass* k, bool do_init, TRAPS) {
1571   if (!do_init) {
1572     if (k->class_loader_data() == nullptr) {
1573       Klass* resolved_k = SystemDictionary::resolve_or_null(k->name(), CHECK);
1574       assert(resolved_k == k, "classes used by archived heap must not be replaced by JVMTI ClassFileLoadHook");
1575     }
1576   } else {
1577     assert(k->class_loader_data() != nullptr, "must have been resolved by HeapShared::resolve_classes");
1578     if (k->is_instance_klass()) {
1579       InstanceKlass* ik = InstanceKlass::cast(k);
1580       ik->initialize(CHECK);
1581     } else if (k->is_objArray_klass()) {
1582       ObjArrayKlass* oak = ObjArrayKlass::cast(k);
1583       oak->initialize(CHECK);
1584     }
1585   }
1586 }
1587 
1588 void HeapShared::init_archived_fields_for(Klass* k, const ArchivedKlassSubGraphInfoRecord* record) {
1589   verify_the_heap(k, "before");
1590 
1591   // Load the subgraph entry fields from the record and store them back to
1592   // the corresponding fields within the mirror.
1593   oop m = k->java_mirror();
1594   Array<int>* entry_field_records = record->entry_field_records();
1595   if (entry_field_records != nullptr) {
1596     int efr_len = entry_field_records->length();
1597     assert(efr_len % 2 == 0, "sanity");
1598     for (int i = 0; i < efr_len; i += 2) {
1599       int field_offset = entry_field_records->at(i);
1600       int root_index = entry_field_records->at(i+1);
1601       oop v = get_root(root_index, /*clear=*/true);
1602       if (k->has_aot_initialized_mirror()) {
1603         assert(v == m->obj_field(field_offset), "must be aot-initialized");
1604       } else {
1605         m->obj_field_put(field_offset, v);
1606       }
1607       log_debug(aot, heap)("  " PTR_FORMAT " init field @ %2d = " PTR_FORMAT, p2i(k), field_offset, p2i(v));
1608     }
1609 
1610     // Done. Java code can see the archived sub-graphs referenced from k's
1611     // mirror after this point.
1612     if (log_is_enabled(Info, aot, heap)) {
1613       ResourceMark rm;
1614       log_info(aot, heap)("initialize_from_archived_subgraph %s " PTR_FORMAT "%s%s",
1615                           k->external_name(), p2i(k), JvmtiExport::is_early_phase() ? " (early)" : "",
1616                           k->has_aot_initialized_mirror() ? " (aot-inited)" : "");
1617     }
1618   }
1619 
1620   verify_the_heap(k, "after ");
1621 }
1622 
1623 void HeapShared::clear_archived_roots_of(Klass* k) {
1624   unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(k);
1625   const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0);
1626   if (record != nullptr) {
1627     Array<int>* entry_field_records = record->entry_field_records();
1628     if (entry_field_records != nullptr) {
1629       int efr_len = entry_field_records->length();
1630       assert(efr_len % 2 == 0, "sanity");
1631       for (int i = 0; i < efr_len; i += 2) {
1632         int root_index = entry_field_records->at(i+1);
1633         clear_root(root_index);
1634       }
1635     }
1636   }
1637 }
1638 
1639 // Push all oop fields (or oop array elemenets in case of an objArray) in
1640 // _referencing_obj onto the _stack.
1641 class HeapShared::OopFieldPusher: public BasicOopIterateClosure {
1642   PendingOopStack* _stack;
1643   GrowableArray<oop> _found_oop_fields;
1644   int _level;
1645   bool _record_klasses_only;
1646   KlassSubGraphInfo* _subgraph_info;
1647   oop _referencing_obj;
1648   bool _is_java_lang_ref;
1649  public:
1650   OopFieldPusher(PendingOopStack* stack,
1651                  int level,
1652                  bool record_klasses_only,
1653                  KlassSubGraphInfo* subgraph_info,
1654                  oop orig) :
1655     _stack(stack),
1656     _found_oop_fields(),
1657     _level(level),
1658     _record_klasses_only(record_klasses_only),
1659     _subgraph_info(subgraph_info),
1660     _referencing_obj(orig) {
1661     _is_java_lang_ref = AOTReferenceObjSupport::check_if_ref_obj(orig);
1662   }
1663   void do_oop(narrowOop *p) { OopFieldPusher::do_oop_work(p); }
1664   void do_oop(      oop *p) { OopFieldPusher::do_oop_work(p); }
1665 
1666   ~OopFieldPusher() {
1667     while (_found_oop_fields.length() > 0) {
1668       // This produces the exact same traversal order as the previous version
1669       // of OopFieldPusher that recurses on the C stack -- a depth-first search,
1670       // walking the oop fields in _referencing_obj by ascending field offsets.
1671       oop obj = _found_oop_fields.pop();
1672       _stack->push(PendingOop(obj, _referencing_obj, _level + 1));
1673     }
1674   }
1675 
1676  protected:
1677   template <class T> void do_oop_work(T *p) {
1678     int field_offset = pointer_delta_as_int((char*)p, cast_from_oop<char*>(_referencing_obj));
1679     oop obj = HeapAccess<ON_UNKNOWN_OOP_REF>::oop_load_at(_referencing_obj, field_offset);
1680     if (!CompressedOops::is_null(obj)) {
1681       if (_is_java_lang_ref && AOTReferenceObjSupport::skip_field(field_offset)) {
1682         // Do not follow these fields. They will be cleared to null.
1683         return;
1684       }
1685 
1686       if (!_record_klasses_only && log_is_enabled(Debug, aot, heap)) {
1687         ResourceMark rm;
1688         log_debug(aot, heap)("(%d) %s[%d] ==> " PTR_FORMAT " size %zu %s", _level,
1689                              _referencing_obj->klass()->external_name(), field_offset,
1690                              p2i(obj), obj->size() * HeapWordSize, obj->klass()->external_name());
1691         if (log_is_enabled(Trace, aot, heap)) {
1692           LogTarget(Trace, aot, heap) log;
1693           LogStream out(log);
1694           obj->print_on(&out);
1695         }
1696       }
1697 
1698       _found_oop_fields.push(obj);
1699     }
1700   }
1701 
1702  public:
1703   oop referencing_obj()                       { return _referencing_obj;      }
1704   KlassSubGraphInfo* subgraph_info()          { return _subgraph_info;        }
1705 };
1706 
1707 // Checks if an oop has any non-null oop fields
1708 class PointsToOopsChecker : public BasicOopIterateClosure {
1709   bool _result;
1710 
1711   template <class T> void check(T *p) {
1712     _result |= (HeapAccess<>::oop_load(p) != nullptr);
1713   }
1714 
1715 public:
1716   PointsToOopsChecker() : _result(false) {}
1717   void do_oop(narrowOop *p) { check(p); }
1718   void do_oop(      oop *p) { check(p); }
1719   bool result() { return _result; }
1720 };
1721 
1722 HeapShared::CachedOopInfo HeapShared::make_cached_oop_info(oop obj, oop referrer) {
1723   PointsToOopsChecker points_to_oops_checker;
1724   obj->oop_iterate(&points_to_oops_checker);
1725   return CachedOopInfo(OopHandle(Universe::vm_global(), referrer), points_to_oops_checker.result());
1726 }
1727 
1728 void HeapShared::init_box_classes(TRAPS) {
1729   if (ArchiveHeapLoader::is_in_use()) {
1730     vmClasses::Boolean_klass()->initialize(CHECK);
1731     vmClasses::Character_klass()->initialize(CHECK);
1732     vmClasses::Float_klass()->initialize(CHECK);
1733     vmClasses::Double_klass()->initialize(CHECK);
1734     vmClasses::Byte_klass()->initialize(CHECK);
1735     vmClasses::Short_klass()->initialize(CHECK);
1736     vmClasses::Integer_klass()->initialize(CHECK);
1737     vmClasses::Long_klass()->initialize(CHECK);
1738     vmClasses::Void_klass()->initialize(CHECK);
1739   }
1740 }
1741 
1742 void HeapShared::exit_on_error() {
1743   if (_context != nullptr) {
1744     ResourceMark rm;
1745     LogStream ls(Log(cds, heap)::error());
1746     ls.print_cr("Context");
1747     for (int i = 0; i < _context->length(); i++) {
1748       const char* s = _context->at(i);
1749       ls.print_cr("- %s", s);
1750     }
1751   }
1752   debug_trace();
1753   AOTMetaspace::unrecoverable_writing_error();
1754 }
1755 
1756 // (1) If orig_obj has not been archived yet, archive it.
1757 // (2) If orig_obj has not been seen yet (since start_recording_subgraph() was called),
1758 //     trace all  objects that are reachable from it, and make sure these objects are archived.
1759 // (3) Record the klasses of all objects that are reachable from orig_obj (including those that
1760 //     were already archived when this function is called)
1761 bool HeapShared::archive_reachable_objects_from(int level,
1762                                                 KlassSubGraphInfo* subgraph_info,
1763                                                 oop orig_obj) {
1764   assert(orig_obj != nullptr, "must be");
1765   PendingOopStack stack;
1766   stack.push(PendingOop(orig_obj, nullptr, level));
1767 
1768   while (stack.length() > 0) {
1769     PendingOop po = stack.pop();
1770     _object_being_archived = po;
1771     bool status = walk_one_object(&stack, po.level(), subgraph_info, po.obj(), po.referrer());
1772     _object_being_archived = PendingOop();
1773 
1774     if (!status) {
1775       // Don't archive a subgraph root that's too big. For archives static fields, that's OK
1776       // as the Java code will take care of initializing this field dynamically.
1777       assert(level == 1, "VM should have exited with unarchivable objects for _level > 1");
1778       return false;
1779     }
1780   }
1781 
1782   return true;
1783 }
1784 
1785 bool HeapShared::walk_one_object(PendingOopStack* stack, int level, KlassSubGraphInfo* subgraph_info,
1786                                  oop orig_obj, oop referrer) {
1787   assert(orig_obj != nullptr, "must be");
1788   if (!JavaClasses::is_supported_for_archiving(orig_obj)) {
1789     // This object has injected fields that cannot be supported easily, so we disallow them for now.
1790     // If you get an error here, you probably made a change in the JDK library that has added
1791     // these objects that are referenced (directly or indirectly) by static fields.
1792     ResourceMark rm;
1793     log_error(aot, heap)("Cannot archive object " PTR_FORMAT " of class %s", p2i(orig_obj), orig_obj->klass()->external_name());
1794     debug_trace();
1795     AOTMetaspace::unrecoverable_writing_error();
1796   }
1797 
1798   if (log_is_enabled(Debug, aot, heap) && java_lang_Class::is_instance(orig_obj)) {
1799     ResourceMark rm;
1800     LogTarget(Debug, aot, heap) log;
1801     LogStream out(log);
1802     out.print("Found java mirror " PTR_FORMAT " ", p2i(orig_obj));
1803     Klass* k = java_lang_Class::as_Klass(orig_obj);
1804     if (k != nullptr) {
1805       out.print("%s", k->external_name());
1806     } else {
1807       out.print("primitive");
1808     }
1809     out.print_cr("; scratch mirror = "  PTR_FORMAT,
1810                  p2i(scratch_java_mirror(orig_obj)));
1811   }
1812 
1813   if (java_lang_Class::is_instance(orig_obj)) {
1814     Klass* k = java_lang_Class::as_Klass(orig_obj);
1815     if (RegeneratedClasses::has_been_regenerated(k)) {
1816       orig_obj = RegeneratedClasses::get_regenerated_object(k)->java_mirror();
1817     }
1818   }
1819 
1820   if (CDSConfig::is_initing_classes_at_dump_time()) {
1821     if (java_lang_Class::is_instance(orig_obj)) {
1822       orig_obj = scratch_java_mirror(orig_obj);
1823       assert(orig_obj != nullptr, "must be archived");
1824     }
1825   } else if (java_lang_Class::is_instance(orig_obj) && subgraph_info != _dump_time_special_subgraph) {
1826     // Without CDSConfig::is_initing_classes_at_dump_time(), we only allow archived objects to
1827     // point to the mirrors of (1) j.l.Object, (2) primitive classes, and (3) box classes. These are initialized
1828     // very early by HeapShared::init_box_classes().
1829     if (orig_obj == vmClasses::Object_klass()->java_mirror()
1830         || java_lang_Class::is_primitive(orig_obj)
1831         || orig_obj == vmClasses::Boolean_klass()->java_mirror()
1832         || orig_obj == vmClasses::Character_klass()->java_mirror()
1833         || orig_obj == vmClasses::Float_klass()->java_mirror()
1834         || orig_obj == vmClasses::Double_klass()->java_mirror()
1835         || orig_obj == vmClasses::Byte_klass()->java_mirror()
1836         || orig_obj == vmClasses::Short_klass()->java_mirror()
1837         || orig_obj == vmClasses::Integer_klass()->java_mirror()
1838         || orig_obj == vmClasses::Long_klass()->java_mirror()
1839         || orig_obj == vmClasses::Void_klass()->java_mirror()) {
1840       orig_obj = scratch_java_mirror(orig_obj);
1841       assert(orig_obj != nullptr, "must be archived");
1842     } else {
1843       // If you get an error here, you probably made a change in the JDK library that has added a Class
1844       // object that is referenced (directly or indirectly) by an ArchivableStaticFieldInfo
1845       // defined at the top of this file.
1846       log_error(aot, heap)("(%d) Unknown java.lang.Class object is in the archived sub-graph", level);
1847       debug_trace();
1848       AOTMetaspace::unrecoverable_writing_error();
1849     }
1850   }
1851 
1852   if (has_been_seen_during_subgraph_recording(orig_obj)) {
1853     // orig_obj has already been archived and traced. Nothing more to do.
1854     return true;
1855   } else {
1856     set_has_been_seen_during_subgraph_recording(orig_obj);
1857   }
1858 
1859   bool already_archived = has_been_archived(orig_obj);
1860   bool record_klasses_only = already_archived;
1861   if (!already_archived) {
1862     ++_num_new_archived_objs;
1863     if (!archive_object(orig_obj, referrer, subgraph_info)) {
1864       // Skip archiving the sub-graph referenced from the current entry field.
1865       ResourceMark rm;
1866       log_error(aot, heap)(
1867         "Cannot archive the sub-graph referenced from %s object ("
1868         PTR_FORMAT ") size %zu, skipped.",
1869         orig_obj->klass()->external_name(), p2i(orig_obj), orig_obj->size() * HeapWordSize);
1870       if (level == 1) {
1871         // Don't archive a subgraph root that's too big. For archives static fields, that's OK
1872         // as the Java code will take care of initializing this field dynamically.
1873         return false;
1874       } else {
1875         // We don't know how to handle an object that has been archived, but some of its reachable
1876         // objects cannot be archived. Bail out for now. We might need to fix this in the future if
1877         // we have a real use case.
1878         AOTMetaspace::unrecoverable_writing_error();
1879       }
1880     }
1881   }
1882 
1883   Klass *orig_k = orig_obj->klass();
1884   subgraph_info->add_subgraph_object_klass(orig_k);
1885 
1886   {
1887     // Find all the oops that are referenced by orig_obj, push them onto the stack
1888     // so we can work on them next.
1889     ResourceMark rm;
1890     OopFieldPusher pusher(stack, level, record_klasses_only, subgraph_info, orig_obj);
1891     orig_obj->oop_iterate(&pusher);
1892   }
1893 
1894   if (CDSConfig::is_initing_classes_at_dump_time()) {
1895     // The classes of all archived enum instances have been marked as aot-init,
1896     // so there's nothing else to be done in the production run.
1897   } else {
1898     // This is legacy support for enum classes before JEP 483 -- we cannot rerun
1899     // the enum's <clinit> in the production run, so special handling is needed.
1900     if (CDSEnumKlass::is_enum_obj(orig_obj)) {
1901       CDSEnumKlass::handle_enum_obj(level + 1, subgraph_info, orig_obj);
1902     }
1903   }
1904 
1905   return true;
1906 }
1907 
1908 //
1909 // Start from the given static field in a java mirror and archive the
1910 // complete sub-graph of java heap objects that are reached directly
1911 // or indirectly from the starting object by following references.
1912 // Sub-graph archiving restrictions (current):
1913 //
1914 // - All classes of objects in the archived sub-graph (including the
1915 //   entry class) must be boot class only.
1916 // - No java.lang.Class instance (java mirror) can be included inside
1917 //   an archived sub-graph. Mirror can only be the sub-graph entry object.
1918 //
1919 // The Java heap object sub-graph archiving process (see OopFieldPusher):
1920 //
1921 // 1) Java object sub-graph archiving starts from a given static field
1922 // within a Class instance (java mirror). If the static field is a
1923 // reference field and points to a non-null java object, proceed to
1924 // the next step.
1925 //
1926 // 2) Archives the referenced java object. If an archived copy of the
1927 // current object already exists, updates the pointer in the archived
1928 // copy of the referencing object to point to the current archived object.
1929 // Otherwise, proceed to the next step.
1930 //
1931 // 3) Follows all references within the current java object and recursively
1932 // archive the sub-graph of objects starting from each reference.
1933 //
1934 // 4) Updates the pointer in the archived copy of referencing object to
1935 // point to the current archived object.
1936 //
1937 // 5) The Klass of the current java object is added to the list of Klasses
1938 // for loading and initializing before any object in the archived graph can
1939 // be accessed at runtime.
1940 //
1941 void HeapShared::archive_reachable_objects_from_static_field(InstanceKlass *k,
1942                                                              const char* klass_name,
1943                                                              int field_offset,
1944                                                              const char* field_name) {
1945   assert(CDSConfig::is_dumping_heap(), "dump time only");
1946   assert(k->defined_by_boot_loader(), "must be boot class");
1947 
1948   oop m = k->java_mirror();
1949 
1950   KlassSubGraphInfo* subgraph_info = get_subgraph_info(k);
1951   oop f = m->obj_field(field_offset);
1952 
1953   log_debug(aot, heap)("Start archiving from: %s::%s (" PTR_FORMAT ")", klass_name, field_name, p2i(f));
1954 
1955   if (!CompressedOops::is_null(f)) {
1956     if (log_is_enabled(Trace, aot, heap)) {
1957       LogTarget(Trace, aot, heap) log;
1958       LogStream out(log);
1959       f->print_on(&out);
1960     }
1961 
1962     bool success = archive_reachable_objects_from(1, subgraph_info, f);
1963     if (!success) {
1964       log_error(aot, heap)("Archiving failed %s::%s (some reachable objects cannot be archived)",
1965                            klass_name, field_name);
1966     } else {
1967       // Note: the field value is not preserved in the archived mirror.
1968       // Record the field as a new subGraph entry point. The recorded
1969       // information is restored from the archive at runtime.
1970       subgraph_info->add_subgraph_entry_field(field_offset, f);
1971       log_info(aot, heap)("Archived field %s::%s => " PTR_FORMAT, klass_name, field_name, p2i(f));
1972     }
1973   } else {
1974     // The field contains null, we still need to record the entry point,
1975     // so it can be restored at runtime.
1976     subgraph_info->add_subgraph_entry_field(field_offset, nullptr);
1977   }
1978 }
1979 
1980 #ifndef PRODUCT
1981 class VerifySharedOopClosure: public BasicOopIterateClosure {
1982  public:
1983   void do_oop(narrowOop *p) { VerifySharedOopClosure::do_oop_work(p); }
1984   void do_oop(      oop *p) { VerifySharedOopClosure::do_oop_work(p); }
1985 
1986  protected:
1987   template <class T> void do_oop_work(T *p) {
1988     oop obj = RawAccess<>::oop_load(p);
1989     if (!CompressedOops::is_null(obj)) {
1990       HeapShared::verify_reachable_objects_from(obj);
1991     }
1992   }
1993 };
1994 
1995 void HeapShared::verify_subgraph_from_static_field(InstanceKlass* k, int field_offset) {
1996   assert(CDSConfig::is_dumping_heap(), "dump time only");
1997   assert(k->defined_by_boot_loader(), "must be boot class");
1998 
1999   oop m = k->java_mirror();
2000   oop f = m->obj_field(field_offset);
2001   if (!CompressedOops::is_null(f)) {
2002     verify_subgraph_from(f);
2003   }
2004 }
2005 
2006 void HeapShared::verify_subgraph_from(oop orig_obj) {
2007   if (!has_been_archived(orig_obj)) {
2008     // It's OK for the root of a subgraph to be not archived. See comments in
2009     // archive_reachable_objects_from().
2010     return;
2011   }
2012 
2013   // Verify that all objects reachable from orig_obj are archived.
2014   init_seen_objects_table();
2015   verify_reachable_objects_from(orig_obj);
2016   delete_seen_objects_table();
2017 }
2018 
2019 void HeapShared::verify_reachable_objects_from(oop obj) {
2020   _num_total_verifications ++;
2021   if (java_lang_Class::is_instance(obj)) {
2022     Klass* k = java_lang_Class::as_Klass(obj);
2023     if (RegeneratedClasses::has_been_regenerated(k)) {
2024       k = RegeneratedClasses::get_regenerated_object(k);
2025       obj = k->java_mirror();
2026     }
2027     obj = scratch_java_mirror(obj);
2028     assert(obj != nullptr, "must be");
2029   }
2030   if (!has_been_seen_during_subgraph_recording(obj)) {
2031     set_has_been_seen_during_subgraph_recording(obj);
2032     assert(has_been_archived(obj), "must be");
2033     VerifySharedOopClosure walker;
2034     obj->oop_iterate(&walker);
2035   }
2036 }
2037 #endif
2038 
2039 void HeapShared::check_special_subgraph_classes() {
2040   if (CDSConfig::is_initing_classes_at_dump_time()) {
2041     // We can have aot-initialized classes (such as Enums) that can reference objects
2042     // of arbitrary types. Currently, we trust the JEP 483 implementation to only
2043     // aot-initialize classes that are "safe".
2044     //
2045     // TODO: we need an automatic tool that checks the safety of aot-initialized
2046     // classes (when we extend the set of aot-initialized classes beyond JEP 483)
2047     return;
2048   } else {
2049     // In this case, the special subgraph should contain a few specific types
2050     GrowableArray<Klass*>* klasses = _dump_time_special_subgraph->subgraph_object_klasses();
2051     int num = klasses->length();
2052     for (int i = 0; i < num; i++) {
2053       Klass* subgraph_k = klasses->at(i);
2054       Symbol* name = subgraph_k->name();
2055       if (subgraph_k->is_instance_klass() &&
2056           name != vmSymbols::java_lang_Class() &&
2057           name != vmSymbols::java_lang_String() &&
2058           name != vmSymbols::java_lang_ArithmeticException() &&
2059           name != vmSymbols::java_lang_ArrayIndexOutOfBoundsException() &&
2060           name != vmSymbols::java_lang_ArrayStoreException() &&
2061           name != vmSymbols::java_lang_ClassCastException() &&
2062           name != vmSymbols::java_lang_InternalError() &&
2063           name != vmSymbols::java_lang_NullPointerException()) {
2064         ResourceMark rm;
2065         fatal("special subgraph cannot have objects of type %s", subgraph_k->external_name());
2066       }
2067     }
2068   }
2069 }
2070 
2071 HeapShared::SeenObjectsTable* HeapShared::_seen_objects_table = nullptr;
2072 HeapShared::PendingOop HeapShared::_object_being_archived;
2073 int HeapShared::_num_new_walked_objs;
2074 int HeapShared::_num_new_archived_objs;
2075 int HeapShared::_num_old_recorded_klasses;
2076 
2077 int HeapShared::_num_total_subgraph_recordings = 0;
2078 int HeapShared::_num_total_walked_objs = 0;
2079 int HeapShared::_num_total_archived_objs = 0;
2080 int HeapShared::_num_total_recorded_klasses = 0;
2081 int HeapShared::_num_total_verifications = 0;
2082 
2083 bool HeapShared::has_been_seen_during_subgraph_recording(oop obj) {
2084   return _seen_objects_table->get(obj) != nullptr;
2085 }
2086 
2087 void HeapShared::set_has_been_seen_during_subgraph_recording(oop obj) {
2088   assert(!has_been_seen_during_subgraph_recording(obj), "sanity");
2089   _seen_objects_table->put_when_absent(obj, true);
2090   _seen_objects_table->maybe_grow();
2091   ++ _num_new_walked_objs;
2092 }
2093 
2094 void HeapShared::start_recording_subgraph(InstanceKlass *k, const char* class_name, bool is_full_module_graph) {
2095   log_info(aot, heap)("Start recording subgraph(s) for archived fields in %s", class_name);
2096   init_subgraph_info(k, is_full_module_graph);
2097   init_seen_objects_table();
2098   _num_new_walked_objs = 0;
2099   _num_new_archived_objs = 0;
2100   _num_old_recorded_klasses = get_subgraph_info(k)->num_subgraph_object_klasses();
2101 }
2102 
2103 void HeapShared::done_recording_subgraph(InstanceKlass *k, const char* class_name) {
2104   int num_new_recorded_klasses = get_subgraph_info(k)->num_subgraph_object_klasses() -
2105     _num_old_recorded_klasses;
2106   log_info(aot, heap)("Done recording subgraph(s) for archived fields in %s: "
2107                       "walked %d objs, archived %d new objs, recorded %d classes",
2108                       class_name, _num_new_walked_objs, _num_new_archived_objs,
2109                       num_new_recorded_klasses);
2110 
2111   delete_seen_objects_table();
2112 
2113   _num_total_subgraph_recordings ++;
2114   _num_total_walked_objs      += _num_new_walked_objs;
2115   _num_total_archived_objs    += _num_new_archived_objs;
2116   _num_total_recorded_klasses +=  num_new_recorded_klasses;
2117 }
2118 
2119 class ArchivableStaticFieldFinder: public FieldClosure {
2120   InstanceKlass* _ik;
2121   Symbol* _field_name;
2122   bool _found;
2123   int _offset;
2124 public:
2125   ArchivableStaticFieldFinder(InstanceKlass* ik, Symbol* field_name) :
2126     _ik(ik), _field_name(field_name), _found(false), _offset(-1) {}
2127 
2128   virtual void do_field(fieldDescriptor* fd) {
2129     if (fd->name() == _field_name) {
2130       assert(!_found, "fields can never be overloaded");
2131       if (is_reference_type(fd->field_type())) {
2132         _found = true;
2133         _offset = fd->offset();
2134       }
2135     }
2136   }
2137   bool found()     { return _found;  }
2138   int offset()     { return _offset; }
2139 };
2140 
2141 void HeapShared::init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[],
2142                                             TRAPS) {
2143   for (int i = 0; fields[i].valid(); i++) {
2144     ArchivableStaticFieldInfo* info = &fields[i];
2145     TempNewSymbol klass_name =  SymbolTable::new_symbol(info->klass_name);
2146     TempNewSymbol field_name =  SymbolTable::new_symbol(info->field_name);
2147     ResourceMark rm; // for stringStream::as_string() etc.
2148 
2149 #ifndef PRODUCT
2150     bool is_test_class = (ArchiveHeapTestClass != nullptr) && (strcmp(info->klass_name, ArchiveHeapTestClass) == 0);
2151     const char* test_class_name = ArchiveHeapTestClass;
2152 #else
2153     bool is_test_class = false;
2154     const char* test_class_name = ""; // avoid C++ printf checks warnings.
2155 #endif
2156 
2157     if (is_test_class) {
2158       log_warning(aot)("Loading ArchiveHeapTestClass %s ...", test_class_name);
2159     }
2160 
2161     Klass* k = SystemDictionary::resolve_or_fail(klass_name, true, THREAD);
2162     if (HAS_PENDING_EXCEPTION) {
2163       CLEAR_PENDING_EXCEPTION;
2164       stringStream st;
2165       st.print("Fail to initialize archive heap: %s cannot be loaded by the boot loader", info->klass_name);
2166       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
2167     }
2168 
2169     if (!k->is_instance_klass()) {
2170       stringStream st;
2171       st.print("Fail to initialize archive heap: %s is not an instance class", info->klass_name);
2172       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
2173     }
2174 
2175     InstanceKlass* ik = InstanceKlass::cast(k);
2176     assert(InstanceKlass::cast(ik)->defined_by_boot_loader(),
2177            "Only support boot classes");
2178 
2179     if (is_test_class) {
2180       if (ik->module()->is_named()) {
2181         // We don't want ArchiveHeapTestClass to be abused to easily load/initialize arbitrary
2182         // core-lib classes. You need to at least append to the bootclasspath.
2183         stringStream st;
2184         st.print("ArchiveHeapTestClass %s is not in unnamed module", test_class_name);
2185         THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
2186       }
2187 
2188       if (ik->package() != nullptr) {
2189         // This restriction makes HeapShared::is_a_test_class_in_unnamed_module() easy.
2190         stringStream st;
2191         st.print("ArchiveHeapTestClass %s is not in unnamed package", test_class_name);
2192         THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
2193       }
2194     } else {
2195       if (ik->module()->name() != vmSymbols::java_base()) {
2196         // We don't want to deal with cases when a module is unavailable at runtime.
2197         // FUTURE -- load from archived heap only when module graph has not changed
2198         //           between dump and runtime.
2199         stringStream st;
2200         st.print("%s is not in java.base module", info->klass_name);
2201         THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
2202       }
2203     }
2204 
2205     if (is_test_class) {
2206       log_warning(aot)("Initializing ArchiveHeapTestClass %s ...", test_class_name);
2207     }
2208     ik->initialize(CHECK);
2209 
2210     ArchivableStaticFieldFinder finder(ik, field_name);
2211     ik->do_local_static_fields(&finder);
2212     if (!finder.found()) {
2213       stringStream st;
2214       st.print("Unable to find the static T_OBJECT field %s::%s", info->klass_name, info->field_name);
2215       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
2216     }
2217 
2218     info->klass = ik;
2219     info->offset = finder.offset();
2220   }
2221 }
2222 
2223 void HeapShared::init_subgraph_entry_fields(TRAPS) {
2224   assert(CDSConfig::is_dumping_heap(), "must be");
2225   _dump_time_subgraph_info_table = new (mtClass)DumpTimeKlassSubGraphInfoTable();
2226   init_subgraph_entry_fields(archive_subgraph_entry_fields, CHECK);
2227   if (CDSConfig::is_dumping_full_module_graph()) {
2228     init_subgraph_entry_fields(fmg_archive_subgraph_entry_fields, CHECK);
2229   }
2230 }
2231 
2232 #ifndef PRODUCT
2233 void HeapShared::setup_test_class(const char* test_class_name) {
2234   ArchivableStaticFieldInfo* p = archive_subgraph_entry_fields;
2235   int num_slots = sizeof(archive_subgraph_entry_fields) / sizeof(ArchivableStaticFieldInfo);
2236   assert(p[num_slots - 2].klass_name == nullptr, "must have empty slot that's patched below");
2237   assert(p[num_slots - 1].klass_name == nullptr, "must have empty slot that marks the end of the list");
2238 
2239   if (test_class_name != nullptr) {
2240     p[num_slots - 2].klass_name = test_class_name;
2241     p[num_slots - 2].field_name = ARCHIVE_TEST_FIELD_NAME;
2242   }
2243 }
2244 
2245 // See if ik is one of the test classes that are pulled in by -XX:ArchiveHeapTestClass
2246 // during runtime. This may be called before the module system is initialized so
2247 // we cannot rely on InstanceKlass::module(), etc.
2248 bool HeapShared::is_a_test_class_in_unnamed_module(Klass* ik) {
2249   if (_test_class != nullptr) {
2250     if (ik == _test_class) {
2251       return true;
2252     }
2253     Array<Klass*>* klasses = _test_class_record->subgraph_object_klasses();
2254     if (klasses == nullptr) {
2255       return false;
2256     }
2257 
2258     for (int i = 0; i < klasses->length(); i++) {
2259       Klass* k = klasses->at(i);
2260       if (k == ik) {
2261         Symbol* name;
2262         if (k->is_instance_klass()) {
2263           name = InstanceKlass::cast(k)->name();
2264         } else if (k->is_objArray_klass()) {
2265           Klass* bk = ObjArrayKlass::cast(k)->bottom_klass();
2266           if (!bk->is_instance_klass()) {
2267             return false;
2268           }
2269           name = bk->name();
2270         } else {
2271           return false;
2272         }
2273 
2274         // See KlassSubGraphInfo::check_allowed_klass() - we only allow test classes
2275         // to be:
2276         //   (A) java.base classes (which must not be in the unnamed module)
2277         //   (B) test classes which must be in the unnamed package of the unnamed module.
2278         // So if we see a '/' character in the class name, it must be in (A);
2279         // otherwise it must be in (B).
2280         if (name->index_of_at(0, "/", 1)  >= 0) {
2281           return false; // (A)
2282         }
2283 
2284         return true; // (B)
2285       }
2286     }
2287   }
2288 
2289   return false;
2290 }
2291 
2292 void HeapShared::initialize_test_class_from_archive(JavaThread* current) {
2293   Klass* k = _test_class;
2294   if (k != nullptr && ArchiveHeapLoader::is_in_use()) {
2295     JavaThread* THREAD = current;
2296     ExceptionMark em(THREAD);
2297     const ArchivedKlassSubGraphInfoRecord* record =
2298       resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/false, THREAD);
2299 
2300     // The _test_class is in the unnamed module, so it can't call CDS.initializeFromArchive()
2301     // from its <clinit> method. So we set up its "archivedObjects" field first, before
2302     // calling its <clinit>. This is not strictly clean, but it's a convenient way to write unit
2303     // test cases (see test/hotspot/jtreg/runtime/cds/appcds/cacheObject/ArchiveHeapTestClass.java).
2304     if (record != nullptr) {
2305       init_archived_fields_for(k, record);
2306     }
2307     resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/true, THREAD);
2308   }
2309 }
2310 #endif
2311 
2312 void HeapShared::init_for_dumping(TRAPS) {
2313   if (CDSConfig::is_dumping_heap()) {
2314     setup_test_class(ArchiveHeapTestClass);
2315     _dumped_interned_strings = new (mtClass)DumpedInternedStrings(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE);
2316     init_subgraph_entry_fields(CHECK);
2317   }
2318 }
2319 
2320 void HeapShared::archive_object_subgraphs(ArchivableStaticFieldInfo fields[],
2321                                           bool is_full_module_graph) {
2322   _num_total_subgraph_recordings = 0;
2323   _num_total_walked_objs = 0;
2324   _num_total_archived_objs = 0;
2325   _num_total_recorded_klasses = 0;
2326   _num_total_verifications = 0;
2327 
2328   // For each class X that has one or more archived fields:
2329   // [1] Dump the subgraph of each archived field
2330   // [2] Create a list of all the class of the objects that can be reached
2331   //     by any of these static fields.
2332   //     At runtime, these classes are initialized before X's archived fields
2333   //     are restored by HeapShared::initialize_from_archived_subgraph().
2334   for (int i = 0; fields[i].valid(); ) {
2335     ArchivableStaticFieldInfo* info = &fields[i];
2336     const char* klass_name = info->klass_name;
2337     start_recording_subgraph(info->klass, klass_name, is_full_module_graph);
2338 
2339     ContextMark cm(klass_name);
2340     // If you have specified consecutive fields of the same klass in
2341     // fields[], these will be archived in the same
2342     // {start_recording_subgraph ... done_recording_subgraph} pass to
2343     // save time.
2344     for (; fields[i].valid(); i++) {
2345       ArchivableStaticFieldInfo* f = &fields[i];
2346       if (f->klass_name != klass_name) {
2347         break;
2348       }
2349 
2350       ContextMark cm(f->field_name);
2351       archive_reachable_objects_from_static_field(f->klass, f->klass_name,
2352                                                   f->offset, f->field_name);
2353     }
2354     done_recording_subgraph(info->klass, klass_name);
2355   }
2356 
2357   log_info(aot, heap)("Archived subgraph records = %d",
2358                       _num_total_subgraph_recordings);
2359   log_info(aot, heap)("  Walked %d objects", _num_total_walked_objs);
2360   log_info(aot, heap)("  Archived %d objects", _num_total_archived_objs);
2361   log_info(aot, heap)("  Recorded %d klasses", _num_total_recorded_klasses);
2362 
2363 #ifndef PRODUCT
2364   for (int i = 0; fields[i].valid(); i++) {
2365     ArchivableStaticFieldInfo* f = &fields[i];
2366     verify_subgraph_from_static_field(f->klass, f->offset);
2367   }
2368   log_info(aot, heap)("  Verified %d references", _num_total_verifications);
2369 #endif
2370 }
2371 
2372 // Keep track of the contents of the archived interned string table. This table
2373 // is used only by CDSHeapVerifier.
2374 void HeapShared::add_to_dumped_interned_strings(oop string) {
2375   assert_at_safepoint(); // DumpedInternedStrings uses raw oops
2376   assert(!ArchiveHeapWriter::is_string_too_large_to_archive(string), "must be");
2377   bool created;
2378   _dumped_interned_strings->put_if_absent(string, true, &created);
2379   if (created) {
2380     // Prevent string deduplication from changing the value field to
2381     // something not in the archive.
2382     java_lang_String::set_deduplication_forbidden(string);
2383     _dumped_interned_strings->maybe_grow();
2384   }
2385 }
2386 
2387 bool HeapShared::is_dumped_interned_string(oop o) {
2388   return _dumped_interned_strings->get(o) != nullptr;
2389 }
2390 
2391 // These tables should be used only within the CDS safepoint, so
2392 // delete them before we exit the safepoint. Otherwise the table will
2393 // contain bad oops after a GC.
2394 void HeapShared::delete_tables_with_raw_oops() {
2395   assert(_seen_objects_table == nullptr, "should have been deleted");
2396 
2397   delete _dumped_interned_strings;
2398   _dumped_interned_strings = nullptr;
2399 
2400   ArchiveHeapWriter::delete_tables_with_raw_oops();
2401 }
2402 
2403 void HeapShared::debug_trace() {
2404   ResourceMark rm;
2405   oop referrer = _object_being_archived.referrer();
2406   if (referrer != nullptr) {
2407     LogStream ls(Log(aot, heap)::error());
2408     ls.print_cr("Reference trace");
2409     CDSHeapVerifier::trace_to_root(&ls, referrer);
2410   }
2411 }
2412 
2413 #ifndef PRODUCT
2414 // At dump-time, find the location of all the non-null oop pointers in an archived heap
2415 // region. This way we can quickly relocate all the pointers without using
2416 // BasicOopIterateClosure at runtime.
2417 class FindEmbeddedNonNullPointers: public BasicOopIterateClosure {
2418   void* _start;
2419   BitMap *_oopmap;
2420   int _num_total_oops;
2421   int _num_null_oops;
2422  public:
2423   FindEmbeddedNonNullPointers(void* start, BitMap* oopmap)
2424     : _start(start), _oopmap(oopmap), _num_total_oops(0),  _num_null_oops(0) {}
2425 
2426   virtual void do_oop(narrowOop* p) {
2427     assert(UseCompressedOops, "sanity");
2428     _num_total_oops ++;
2429     narrowOop v = *p;
2430     if (!CompressedOops::is_null(v)) {
2431       size_t idx = p - (narrowOop*)_start;
2432       _oopmap->set_bit(idx);
2433     } else {
2434       _num_null_oops ++;
2435     }
2436   }
2437   virtual void do_oop(oop* p) {
2438     assert(!UseCompressedOops, "sanity");
2439     _num_total_oops ++;
2440     if ((*p) != nullptr) {
2441       size_t idx = p - (oop*)_start;
2442       _oopmap->set_bit(idx);
2443     } else {
2444       _num_null_oops ++;
2445     }
2446   }
2447   int num_total_oops() const { return _num_total_oops; }
2448   int num_null_oops()  const { return _num_null_oops; }
2449 };
2450 #endif
2451 
2452 void HeapShared::count_allocation(size_t size) {
2453   _total_obj_count ++;
2454   _total_obj_size += size;
2455   for (int i = 0; i < ALLOC_STAT_SLOTS; i++) {
2456     if (size <= (size_t(1) << i)) {
2457       _alloc_count[i] ++;
2458       _alloc_size[i] += size;
2459       return;
2460     }
2461   }
2462 }
2463 
2464 static double avg_size(size_t size, size_t count) {
2465   double avg = 0;
2466   if (count > 0) {
2467     avg = double(size * HeapWordSize) / double(count);
2468   }
2469   return avg;
2470 }
2471 
2472 void HeapShared::print_stats() {
2473   size_t huge_count = _total_obj_count;
2474   size_t huge_size = _total_obj_size;
2475 
2476   for (int i = 0; i < ALLOC_STAT_SLOTS; i++) {
2477     size_t byte_size_limit = (size_t(1) << i) * HeapWordSize;
2478     size_t count = _alloc_count[i];
2479     size_t size = _alloc_size[i];
2480     log_info(aot, heap)("%8zu objects are <= %-6zu"
2481                         " bytes (total %8zu bytes, avg %8.1f bytes)",
2482                         count, byte_size_limit, size * HeapWordSize, avg_size(size, count));
2483     huge_count -= count;
2484     huge_size -= size;
2485   }
2486 
2487   log_info(aot, heap)("%8zu huge  objects               (total %8zu bytes"
2488                       ", avg %8.1f bytes)",
2489                       huge_count, huge_size * HeapWordSize,
2490                       avg_size(huge_size, huge_count));
2491   log_info(aot, heap)("%8zu total objects               (total %8zu bytes"
2492                       ", avg %8.1f bytes)",
2493                       _total_obj_count, _total_obj_size * HeapWordSize,
2494                       avg_size(_total_obj_size, _total_obj_count));
2495 }
2496 
2497 bool HeapShared::is_archived_boot_layer_available(JavaThread* current) {
2498   TempNewSymbol klass_name = SymbolTable::new_symbol(ARCHIVED_BOOT_LAYER_CLASS);
2499   InstanceKlass* k = SystemDictionary::find_instance_klass(current, klass_name, Handle());
2500   if (k == nullptr) {
2501     return false;
2502   } else {
2503     TempNewSymbol field_name = SymbolTable::new_symbol(ARCHIVED_BOOT_LAYER_FIELD);
2504     TempNewSymbol field_signature = SymbolTable::new_symbol("Ljdk/internal/module/ArchivedBootLayer;");
2505     fieldDescriptor fd;
2506     if (k->find_field(field_name, field_signature, true, &fd) != nullptr) {
2507       oop m = k->java_mirror();
2508       oop f = m->obj_field(fd.offset());
2509       if (CompressedOops::is_null(f)) {
2510         return false;
2511       }
2512     } else {
2513       return false;
2514     }
2515   }
2516   return true;
2517 }
2518 
2519 #endif // INCLUDE_CDS_JAVA_HEAP