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