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