< prev index next >

src/hotspot/share/cds/heapShared.cpp

Print this page

  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/heapShared.hpp"
  34 #include "cds/metaspaceShared.hpp"
  35 #include "classfile/classLoaderData.hpp"
  36 #include "classfile/javaClasses.inline.hpp"
  37 #include "classfile/modules.hpp"
  38 #include "classfile/stringTable.hpp"
  39 #include "classfile/symbolTable.hpp"
  40 #include "classfile/systemDictionary.hpp"
  41 #include "classfile/systemDictionaryShared.hpp"
  42 #include "classfile/vmClasses.hpp"
  43 #include "classfile/vmSymbols.hpp"
  44 #include "gc/shared/collectedHeap.hpp"
  45 #include "gc/shared/gcLocker.hpp"
  46 #include "gc/shared/gcVMOperations.hpp"
  47 #include "logging/log.hpp"
  48 #include "logging/logStream.hpp"
  49 #include "memory/iterator.inline.hpp"
  50 #include "memory/resourceArea.hpp"
  51 #include "memory/universe.hpp"
  52 #include "oops/compressedOops.inline.hpp"

  66 #include "gc/g1/g1CollectedHeap.hpp"
  67 #endif
  68 
  69 #if INCLUDE_CDS_JAVA_HEAP
  70 
  71 struct ArchivableStaticFieldInfo {
  72   const char* klass_name;
  73   const char* field_name;
  74   InstanceKlass* klass;
  75   int offset;
  76   BasicType type;
  77 
  78   ArchivableStaticFieldInfo(const char* k, const char* f)
  79   : klass_name(k), field_name(f), klass(nullptr), offset(0), type(T_ILLEGAL) {}
  80 
  81   bool valid() {
  82     return klass_name != nullptr;
  83   }
  84 };
  85 





















  86 bool HeapShared::_disable_writing = false;
  87 DumpedInternedStrings *HeapShared::_dumped_interned_strings = nullptr;
  88 
  89 size_t HeapShared::_alloc_count[HeapShared::ALLOC_STAT_SLOTS];
  90 size_t HeapShared::_alloc_size[HeapShared::ALLOC_STAT_SLOTS];
  91 size_t HeapShared::_total_obj_count;
  92 size_t HeapShared::_total_obj_size;
  93 
  94 #ifndef PRODUCT
  95 #define ARCHIVE_TEST_FIELD_NAME "archivedObjects"
  96 static Array<char>* _archived_ArchiveHeapTestClass = nullptr;
  97 static const char* _test_class_name = nullptr;
  98 static const Klass* _test_class = nullptr;
  99 static const ArchivedKlassSubGraphInfoRecord* _test_class_record = nullptr;
 100 #endif
 101 
 102 
 103 //
 104 // If you add new entries to the following tables, you should know what you're doing!
 105 //
 106 
 107 static ArchivableStaticFieldInfo archive_subgraph_entry_fields[] = {

 108   {"java/lang/Integer$IntegerCache",              "archivedCache"},
 109   {"java/lang/Long$LongCache",                    "archivedCache"},
 110   {"java/lang/Byte$ByteCache",                    "archivedCache"},
 111   {"java/lang/Short$ShortCache",                  "archivedCache"},
 112   {"java/lang/Character$CharacterCache",          "archivedCache"},
 113   {"java/util/jar/Attributes$Name",               "KNOWN_NAMES"},
 114   {"sun/util/locale/BaseLocale",                  "constantBaseLocales"},
 115   {"jdk/internal/module/ArchivedModuleGraph",     "archivedModuleGraph"},
 116   {"java/util/ImmutableCollections",              "archivedObjects"},
 117   {"java/lang/ModuleLayer",                       "EMPTY_LAYER"},
 118   {"java/lang/module/Configuration",              "EMPTY_CONFIGURATION"},
 119   {"jdk/internal/math/FDBigInteger",              "archivedCaches"},




 120 #ifndef PRODUCT
 121   {nullptr, nullptr}, // Extra slot for -XX:ArchiveHeapTestClass
 122 #endif
 123   {nullptr, nullptr},
 124 };
 125 
 126 // full module graph
 127 static ArchivableStaticFieldInfo fmg_archive_subgraph_entry_fields[] = {
 128   {"jdk/internal/loader/ArchivedClassLoaders",    "archivedClassLoaders"},
 129   {ARCHIVED_BOOT_LAYER_CLASS,                     ARCHIVED_BOOT_LAYER_FIELD},
 130   {"java/lang/Module$ArchivedData",               "archivedData"},
 131   {nullptr, nullptr},
 132 };
 133 
 134 KlassSubGraphInfo* HeapShared::_default_subgraph_info;

 135 GrowableArrayCHeap<oop, mtClassShared>* HeapShared::_pending_roots = nullptr;


 136 OopHandle HeapShared::_roots;
 137 OopHandle HeapShared::_scratch_basic_type_mirrors[T_VOID+1];
 138 MetaspaceObjToOopHandleTable* HeapShared::_scratch_java_mirror_table = nullptr;
 139 MetaspaceObjToOopHandleTable* HeapShared::_scratch_references_table = nullptr;

 140 
 141 static bool is_subgraph_root_class_of(ArchivableStaticFieldInfo fields[], InstanceKlass* ik) {
 142   for (int i = 0; fields[i].valid(); i++) {
 143     if (fields[i].klass == ik) {
 144       return true;
 145     }
 146   }
 147   return false;
 148 }
 149 
 150 bool HeapShared::is_subgraph_root_class(InstanceKlass* ik) {
 151   return is_subgraph_root_class_of(archive_subgraph_entry_fields, ik) ||
 152          is_subgraph_root_class_of(fmg_archive_subgraph_entry_fields, ik);
 153 }
 154 
 155 unsigned HeapShared::oop_hash(oop const& p) {
 156   // Do not call p->identity_hash() as that will update the
 157   // object header.
 158   return primitive_hash(cast_from_oop<intptr_t>(p));
 159 }

 209 
 210 bool HeapShared::has_been_archived(oop obj) {
 211   assert(CDSConfig::is_dumping_heap(), "dump-time only");
 212   return archived_object_cache()->get(obj) != nullptr;
 213 }
 214 
 215 int HeapShared::append_root(oop obj) {
 216   assert(CDSConfig::is_dumping_heap(), "dump-time only");
 217 
 218   // No GC should happen since we aren't scanning _pending_roots.
 219   assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
 220 
 221   if (_pending_roots == nullptr) {
 222     _pending_roots = new GrowableArrayCHeap<oop, mtClassShared>(500);
 223   }
 224 
 225   return _pending_roots->append(obj);
 226 }
 227 
 228 objArrayOop HeapShared::roots() {
 229   if (CDSConfig::is_dumping_heap()) {
 230     assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
 231     if (!HeapShared::can_write()) {
 232       return nullptr;
 233     }
 234   } else {
 235     assert(CDSConfig::is_using_archive(), "must be");
 236   }
 237 
 238   objArrayOop roots = (objArrayOop)_roots.resolve();
 239   assert(roots != nullptr, "should have been initialized");
 240   return roots;
 241 }
 242 























































































































 243 // Returns an objArray that contains all the roots of the archived objects
 244 oop HeapShared::get_root(int index, bool clear) {
 245   assert(index >= 0, "sanity");
 246   assert(!CDSConfig::is_dumping_heap() && CDSConfig::is_using_archive(), "runtime only");
 247   assert(!_roots.is_empty(), "must have loaded shared heap");
 248   oop result = roots()->obj_at(index);
 249   if (clear) {
 250     clear_root(index);
 251   }
 252   return result;
 253 }
 254 
 255 void HeapShared::clear_root(int index) {
 256   assert(index >= 0, "sanity");
 257   assert(CDSConfig::is_using_archive(), "must be");
 258   if (ArchiveHeapLoader::is_in_use()) {
 259     if (log_is_enabled(Debug, cds, heap)) {
 260       oop old = roots()->obj_at(index);
 261       log_debug(cds, heap)("Clearing root %d: was " PTR_FORMAT, index, p2i(old));
 262     }

 312       return nullptr;
 313     }
 314   }
 315   void set_oop(MetaspaceObj* ptr, oop o) {
 316     MutexLocker ml(ScratchObjects_lock, Mutex::_no_safepoint_check_flag);
 317     OopHandle handle(Universe::vm_global(), o);
 318     bool is_new = put(ptr, handle);
 319     assert(is_new, "cannot set twice");
 320   }
 321   void remove_oop(MetaspaceObj* ptr) {
 322     MutexLocker ml(ScratchObjects_lock, Mutex::_no_safepoint_check_flag);
 323     OopHandle* handle = get(ptr);
 324     if (handle != nullptr) {
 325       handle->release(Universe::vm_global());
 326       remove(ptr);
 327     }
 328   }
 329 };
 330 
 331 void HeapShared::add_scratch_resolved_references(ConstantPool* src, objArrayOop dest) {



 332   _scratch_references_table->set_oop(src, dest);
 333 }
 334 
 335 objArrayOop HeapShared::scratch_resolved_references(ConstantPool* src) {
 336   return (objArrayOop)_scratch_references_table->get_oop(src);
 337 }
 338 
 339 void HeapShared::init_scratch_objects(TRAPS) {
 340   for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
 341     BasicType bt = (BasicType)i;
 342     if (!is_reference_type(bt)) {
 343       oop m = java_lang_Class::create_basic_type_mirror(type2name(bt), bt, CHECK);
 344       _scratch_basic_type_mirrors[i] = OopHandle(Universe::vm_global(), m);

 345     }
 346   }
 347   _scratch_java_mirror_table = new (mtClass)MetaspaceObjToOopHandleTable();
 348   _scratch_references_table = new (mtClass)MetaspaceObjToOopHandleTable();



























 349 }
 350 
 351 oop HeapShared::scratch_java_mirror(BasicType t) {
 352   assert((uint)t < T_VOID+1, "range check");
 353   assert(!is_reference_type(t), "sanity");
 354   return _scratch_basic_type_mirrors[t].resolve();
 355 }
 356 
 357 oop HeapShared::scratch_java_mirror(Klass* k) {
 358   return _scratch_java_mirror_table->get_oop(k);
 359 }
 360 
 361 void HeapShared::set_scratch_java_mirror(Klass* k, oop mirror) {

 362   _scratch_java_mirror_table->set_oop(k, mirror);
 363 }
 364 
 365 void HeapShared::remove_scratch_objects(Klass* k) {
 366   _scratch_java_mirror_table->remove_oop(k);
 367   if (k->is_instance_klass()) {
 368     _scratch_references_table->remove(InstanceKlass::cast(k)->constants());
 369   }
 370 }
 371 



























































































 372 void HeapShared::archive_java_mirrors() {


 373   for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
 374     BasicType bt = (BasicType)i;
 375     if (!is_reference_type(bt)) {

 376       oop m = _scratch_basic_type_mirrors[i].resolve();
 377       assert(m != nullptr, "sanity");
 378       bool success = archive_reachable_objects_from(1, _default_subgraph_info, m);

 379       assert(success, "sanity");
 380 
 381       log_trace(cds, heap, mirror)(
 382         "Archived %s mirror object from " PTR_FORMAT,
 383         type2name(bt), p2i(m));
 384 
 385       Universe::set_archived_basic_type_mirror_index(bt, append_root(m));
 386     }
 387   }
 388 
 389   GrowableArray<Klass*>* klasses = ArchiveBuilder::current()->klasses();
 390   assert(klasses != nullptr, "sanity");
 391   for (int i = 0; i < klasses->length(); i++) {
 392     Klass* orig_k = klasses->at(i);

 393     oop m = scratch_java_mirror(orig_k);
 394     if (m != nullptr) {








 395       Klass* buffered_k = ArchiveBuilder::get_buffered_klass(orig_k);
 396       bool success = archive_reachable_objects_from(1, _default_subgraph_info, m);
 397       guarantee(success, "scratch mirrors must point to only archivable objects");
 398       buffered_k->set_archived_java_mirror(append_root(m));
 399       ResourceMark rm;
 400       log_trace(cds, heap, mirror)(
 401         "Archived %s mirror object from " PTR_FORMAT,
 402         buffered_k->external_name(), p2i(m));
 403 
 404       // archive the resolved_referenes array
 405       if (buffered_k->is_instance_klass()) {
 406         InstanceKlass* ik = InstanceKlass::cast(buffered_k);
 407         oop rr = ik->constants()->prepare_resolved_references_for_archiving();
 408         if (rr != nullptr && !ArchiveHeapWriter::is_too_large_to_archive(rr)) {
 409           bool success = HeapShared::archive_reachable_objects_from(1, _default_subgraph_info, rr);
 410           assert(success, "must be");
 411           int root_index = append_root(rr);
 412           ik->constants()->cache()->set_archived_references(root_index);
 413         }
 414       }
 415     }
 416   }

 419 void HeapShared::archive_strings() {
 420   oop shared_strings_array = StringTable::init_shared_table(_dumped_interned_strings);
 421   bool success = archive_reachable_objects_from(1, _default_subgraph_info, shared_strings_array);
 422   // We must succeed because:
 423   // - _dumped_interned_strings do not contain any large strings.
 424   // - StringTable::init_shared_table() doesn't create any large arrays.
 425   assert(success, "shared strings array must not point to arrays or strings that are too large to archive");
 426   StringTable::set_shared_strings_array_index(append_root(shared_strings_array));
 427 }
 428 
 429 int HeapShared::archive_exception_instance(oop exception) {
 430   bool success = archive_reachable_objects_from(1, _default_subgraph_info, exception);
 431   assert(success, "sanity");
 432   return append_root(exception);
 433 }
 434 
 435 void HeapShared::mark_native_pointers(oop orig_obj) {
 436   if (java_lang_Class::is_instance(orig_obj)) {
 437     ArchiveHeapWriter::mark_native_pointer(orig_obj, java_lang_Class::klass_offset());
 438     ArchiveHeapWriter::mark_native_pointer(orig_obj, java_lang_Class::array_klass_offset());


 439   }
 440 }
 441 
 442 void HeapShared::get_pointer_info(oop src_obj, bool& has_oop_pointers, bool& has_native_pointers) {
 443   CachedOopInfo* info = archived_object_cache()->get(src_obj);
 444   assert(info != nullptr, "must be");
 445   has_oop_pointers = info->has_oop_pointers();
 446   has_native_pointers = info->has_native_pointers();
 447 }
 448 
 449 void HeapShared::set_has_native_pointers(oop src_obj) {
 450   CachedOopInfo* info = archived_object_cache()->get(src_obj);
 451   assert(info != nullptr, "must be");
 452   info->set_has_native_pointers();
 453 }
 454 





























































































 455 void HeapShared::archive_objects(ArchiveHeapInfo *heap_info) {
 456   {
 457     NoSafepointVerifier nsv;
 458 
 459     _default_subgraph_info = init_subgraph_info(vmClasses::Object_klass(), false);


 460 
 461     // Cache for recording where the archived objects are copied to
 462     create_archived_object_cache();
 463 
 464     log_info(cds)("Heap range = [" PTR_FORMAT " - "  PTR_FORMAT "]",
 465                    UseCompressedOops ? p2i(CompressedOops::begin()) :
 466                                        p2i((address)G1CollectedHeap::heap()->reserved().start()),
 467                    UseCompressedOops ? p2i(CompressedOops::end()) :
 468                                        p2i((address)G1CollectedHeap::heap()->reserved().end()));


 469     copy_objects();
 470 
 471     CDSHeapVerifier::verify();


 472     check_default_subgraph_classes();
 473   }
 474 
 475   ArchiveHeapWriter::write(_pending_roots, heap_info);
 476 }
 477 
 478 void HeapShared::copy_interned_strings() {
 479   init_seen_objects_table();
 480 
 481   auto copier = [&] (oop s, bool value_ignored) {
 482     assert(s != nullptr, "sanity");
 483     assert(!ArchiveHeapWriter::is_string_too_large_to_archive(s), "large strings must have been filtered");
 484     bool success = archive_reachable_objects_from(1, _default_subgraph_info, s);
 485     assert(success, "must be");
 486     // Prevent string deduplication from changing the value field to
 487     // something not in the archive.
 488     java_lang_String::set_deduplication_forbidden(s);
 489   };
 490   _dumped_interned_strings->iterate_all(copier);
 491 

 558 // Add the Klass* for an object in the current KlassSubGraphInfo's subgraphs.
 559 // Only objects of boot classes can be included in sub-graph.
 560 void KlassSubGraphInfo::add_subgraph_object_klass(Klass* orig_k) {
 561   assert(CDSConfig::is_dumping_heap(), "dump time only");
 562   Klass* buffered_k = ArchiveBuilder::get_buffered_klass(orig_k);
 563 
 564   if (_subgraph_object_klasses == nullptr) {
 565     _subgraph_object_klasses =
 566       new (mtClass) GrowableArray<Klass*>(50, mtClass);
 567   }
 568 
 569   assert(ArchiveBuilder::current()->is_in_buffer_space(buffered_k), "must be a shared class");
 570 
 571   if (_k == buffered_k) {
 572     // Don't add the Klass containing the sub-graph to it's own klass
 573     // initialization list.
 574     return;
 575   }
 576 
 577   if (buffered_k->is_instance_klass()) {
 578     assert(InstanceKlass::cast(buffered_k)->is_shared_boot_class(),
 579           "must be boot class");



 580     // vmClasses::xxx_klass() are not updated, need to check
 581     // the original Klass*
 582     if (orig_k == vmClasses::String_klass() ||
 583         orig_k == vmClasses::Object_klass()) {
 584       // Initialized early during VM initialization. No need to be added
 585       // to the sub-graph object class list.
 586       return;
 587     }
 588     check_allowed_klass(InstanceKlass::cast(orig_k));
 589   } else if (buffered_k->is_objArray_klass()) {
 590     Klass* abk = ObjArrayKlass::cast(buffered_k)->bottom_klass();
 591     if (abk->is_instance_klass()) {
 592       assert(InstanceKlass::cast(abk)->is_shared_boot_class(),
 593             "must be boot class");
 594       check_allowed_klass(InstanceKlass::cast(ObjArrayKlass::cast(orig_k)->bottom_klass()));
 595     }
 596     if (buffered_k == Universe::objectArrayKlass()) {
 597       // Initialized early during Universe::genesis. No need to be added
 598       // to the list.
 599       return;
 600     }
 601   } else {
 602     assert(buffered_k->is_typeArray_klass(), "must be");
 603     // Primitive type arrays are created early during Universe::genesis.
 604     return;
 605   }
 606 
 607   if (log_is_enabled(Debug, cds, heap)) {
 608     if (!_subgraph_object_klasses->contains(buffered_k)) {
 609       ResourceMark rm;
 610       log_debug(cds, heap)("Adding klass %s", orig_k->external_name());
 611     }
 612   }
 613 
 614   _subgraph_object_klasses->append_if_missing(buffered_k);
 615   _has_non_early_klasses |= is_non_early_klass(orig_k);
 616 }
 617 
 618 void KlassSubGraphInfo::check_allowed_klass(InstanceKlass* ik) {




 619   if (ik->module()->name() == vmSymbols::java_base()) {
 620     assert(ik->package() != nullptr, "classes in java.base cannot be in unnamed package");
 621     return;
 622   }
 623 
 624 #ifndef PRODUCT
 625   if (!ik->module()->is_named() && ik->package() == nullptr) {
 626     // This class is loaded by ArchiveHeapTestClass
 627     return;
 628   }
 629   const char* extra_msg = ", or in an unnamed package of an unnamed module";
 630 #else
 631   const char* extra_msg = "";
 632 #endif
 633 
 634   ResourceMark rm;
 635   log_error(cds, heap)("Class %s not allowed in archive heap. Must be in java.base%s",
 636                        ik->external_name(), extra_msg);
 637   MetaspaceShared::unrecoverable_writing_error();
 638 }

 696     _subgraph_object_klasses =
 697       ArchiveBuilder::new_ro_array<Klass*>(num_subgraphs_klasses);
 698     for (int i = 0; i < num_subgraphs_klasses; i++) {
 699       Klass* subgraph_k = subgraph_object_klasses->at(i);
 700       if (log_is_enabled(Info, cds, heap)) {
 701         ResourceMark rm;
 702         log_info(cds, heap)(
 703           "Archived object klass %s (%2d) => %s",
 704           _k->external_name(), i, subgraph_k->external_name());
 705       }
 706       _subgraph_object_klasses->at_put(i, subgraph_k);
 707       ArchivePtrMarker::mark_pointer(_subgraph_object_klasses->adr_at(i));
 708     }
 709   }
 710 
 711   ArchivePtrMarker::mark_pointer(&_k);
 712   ArchivePtrMarker::mark_pointer(&_entry_field_records);
 713   ArchivePtrMarker::mark_pointer(&_subgraph_object_klasses);
 714 }
 715 
 716 struct CopyKlassSubGraphInfoToArchive : StackObj {

 717   CompactHashtableWriter* _writer;
 718   CopyKlassSubGraphInfoToArchive(CompactHashtableWriter* writer) : _writer(writer) {}
 719 
 720   bool do_entry(Klass* klass, KlassSubGraphInfo& info) {
 721     if (info.subgraph_object_klasses() != nullptr || info.subgraph_entry_fields() != nullptr) {
 722       ArchivedKlassSubGraphInfoRecord* record =
 723         (ArchivedKlassSubGraphInfoRecord*)ArchiveBuilder::ro_region_alloc(sizeof(ArchivedKlassSubGraphInfoRecord));
 724       record->init(&info);
 725 
 726       Klass* buffered_k = ArchiveBuilder::get_buffered_klass(klass);
 727       unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary((address)buffered_k);
 728       u4 delta = ArchiveBuilder::current()->any_to_offset_u4(record);
 729       _writer->add(hash, delta);
 730     }
 731     return true; // keep on iterating
 732   }
 733 };
 734 







 735 // Build the records of archived subgraph infos, which include:
 736 // - Entry points to all subgraphs from the containing class mirror. The entry
 737 //   points are static fields in the mirror. For each entry point, the field
 738 //   offset, and value are recorded in the sub-graph
 739 //   info. The value is stored back to the corresponding field at runtime.
 740 // - A list of klasses that need to be loaded/initialized before archived
 741 //   java object sub-graph can be accessed at runtime.
 742 void HeapShared::write_subgraph_info_table() {
 743   // Allocate the contents of the hashtable(s) inside the RO region of the CDS archive.
 744   DumpTimeKlassSubGraphInfoTable* d_table = _dump_time_subgraph_info_table;
 745   CompactHashtableStats stats;
 746 
 747   _run_time_subgraph_info_table.reset();
 748 
 749   CompactHashtableWriter writer(d_table->_count, &stats);
 750   CopyKlassSubGraphInfoToArchive copy(&writer);
 751   d_table->iterate(&copy);
 752   writer.dump(&_run_time_subgraph_info_table, "subgraphs");
 753 


 754 #ifndef PRODUCT
 755   if (ArchiveHeapTestClass != nullptr) {
 756     size_t len = strlen(ArchiveHeapTestClass) + 1;
 757     Array<char>* array = ArchiveBuilder::new_ro_array<char>((int)len);
 758     strncpy(array->adr_at(0), ArchiveHeapTestClass, len);
 759     _archived_ArchiveHeapTestClass = array;
 760   }
 761 #endif
 762   if (log_is_enabled(Info, cds, heap)) {
 763     print_stats();
 764   }
 765 }
 766 




 767 void HeapShared::init_roots(oop roots_oop) {
 768   if (roots_oop != nullptr) {
 769     assert(ArchiveHeapLoader::is_in_use(), "must be");
 770     _roots = OopHandle(Universe::vm_global(), roots_oop);
 771   }
 772 }
 773 
 774 void HeapShared::serialize_tables(SerializeClosure* soc) {
 775 
 776 #ifndef PRODUCT
 777   soc->do_ptr(&_archived_ArchiveHeapTestClass);
 778   if (soc->reading() && _archived_ArchiveHeapTestClass != nullptr) {
 779     _test_class_name = _archived_ArchiveHeapTestClass->adr_at(0);
 780     setup_test_class(_test_class_name);
 781   }
 782 #endif
 783 
 784   _run_time_subgraph_info_table.serialize_header(soc);


 785 }
 786 
 787 static void verify_the_heap(Klass* k, const char* which) {
 788   if (VerifyArchivedFields > 0) {
 789     ResourceMark rm;
 790     log_info(cds, heap)("Verify heap %s initializing static field(s) in %s",
 791                         which, k->external_name());
 792 
 793     VM_Verify verify_op;
 794     VMThread::execute(&verify_op);
 795 
 796     if (VerifyArchivedFields > 1 && is_init_completed()) {
 797       // At this time, the oop->klass() of some archived objects in the heap may not
 798       // have been loaded into the system dictionary yet. Nevertheless, oop->klass() should
 799       // have enough information (object size, oop maps, etc) so that a GC can be safely
 800       // performed.
 801       //
 802       // -XX:VerifyArchivedFields=2 force a GC to happen in such an early stage
 803       // to check for GC safety.
 804       log_info(cds, heap)("Trigger GC %s initializing static field(s) in %s",
 805                           which, k->external_name());
 806       FlagSetting fs1(VerifyBeforeGC, true);
 807       FlagSetting fs2(VerifyDuringGC, true);
 808       FlagSetting fs3(VerifyAfterGC,  true);
 809       Universe::heap()->collect(GCCause::_java_lang_system_gc);
 810     }
 811   }
 812 }
 813 
 814 // Before GC can execute, we must ensure that all oops reachable from HeapShared::roots()
 815 // have a valid klass. I.e., oopDesc::klass() must have already been resolved.
 816 //
 817 // Note: if a ArchivedKlassSubGraphInfoRecord contains non-early classes, and JVMTI
 818 // ClassFileLoadHook is enabled, it's possible for this class to be dynamically replaced. In
 819 // this case, we will not load the ArchivedKlassSubGraphInfoRecord and will clear its roots.
 820 void HeapShared::resolve_classes(JavaThread* current) {
 821   assert(CDSConfig::is_using_archive(), "runtime only!");
 822   if (!ArchiveHeapLoader::is_in_use()) {
 823     return; // nothing to do
 824   }













 825   resolve_classes_for_subgraphs(current, archive_subgraph_entry_fields);
 826   resolve_classes_for_subgraphs(current, fmg_archive_subgraph_entry_fields);
 827 }
 828 
 829 void HeapShared::resolve_classes_for_subgraphs(JavaThread* current, ArchivableStaticFieldInfo fields[]) {
 830   for (int i = 0; fields[i].valid(); i++) {
 831     ArchivableStaticFieldInfo* info = &fields[i];
 832     TempNewSymbol klass_name = SymbolTable::new_symbol(info->klass_name);
 833     InstanceKlass* k = SystemDictionaryShared::find_builtin_class(klass_name);
 834     assert(k != nullptr && k->is_shared_boot_class(), "sanity");
 835     resolve_classes_for_subgraph_of(current, k);
 836   }
 837 }
 838 
 839 void HeapShared::resolve_classes_for_subgraph_of(JavaThread* current, Klass* k) {
 840   JavaThread* THREAD = current;
 841   ExceptionMark em(THREAD);
 842   const ArchivedKlassSubGraphInfoRecord* record =
 843    resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/false, THREAD);
 844   if (HAS_PENDING_EXCEPTION) {
 845    CLEAR_PENDING_EXCEPTION;
 846   }
 847   if (record == nullptr) {
 848    clear_archived_roots_of(k);
 849   }
 850 }
 851 









































 852 void HeapShared::initialize_from_archived_subgraph(JavaThread* current, Klass* k) {
 853   JavaThread* THREAD = current;
 854   if (!ArchiveHeapLoader::is_in_use()) {
 855     return; // nothing to do
 856   }
 857 
 858   ExceptionMark em(THREAD);
 859   const ArchivedKlassSubGraphInfoRecord* record =
 860     resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/true, THREAD);
 861 
 862   if (HAS_PENDING_EXCEPTION) {
 863     CLEAR_PENDING_EXCEPTION;
 864     // None of the field value will be set if there was an exception when initializing the classes.
 865     // The java code will not see any of the archived objects in the
 866     // subgraphs referenced from k in this case.
 867     return;
 868   }
 869 
 870   if (record != nullptr) {
 871     init_archived_fields_for(k, record);

 905         log_info(cds, heap)("subgraph %s cannot be used because full module graph is disabled",
 906                             k->external_name());
 907       }
 908       return nullptr;
 909     }
 910 
 911     if (record->has_non_early_klasses() && JvmtiExport::should_post_class_file_load_hook()) {
 912       if (log_is_enabled(Info, cds, heap)) {
 913         ResourceMark rm(THREAD);
 914         log_info(cds, heap)("subgraph %s cannot be used because JVMTI ClassFileLoadHook is enabled",
 915                             k->external_name());
 916       }
 917       return nullptr;
 918     }
 919 
 920     if (log_is_enabled(Info, cds, heap)) {
 921       ResourceMark rm;
 922       log_info(cds, heap)("%s subgraph %s ", do_init ? "init" : "resolve", k->external_name());
 923     }
 924 




















































 925     resolve_or_init(k, do_init, CHECK_NULL);
 926 
 927     // Load/link/initialize the klasses of the objects in the subgraph.
 928     // nullptr class loader is used.
 929     Array<Klass*>* klasses = record->subgraph_object_klasses();
 930     if (klasses != nullptr) {
 931       for (int i = 0; i < klasses->length(); i++) {
 932         Klass* klass = klasses->at(i);
 933         if (!klass->is_shared()) {
 934           return nullptr;
 935         }
 936         resolve_or_init(klass, do_init, CHECK_NULL);
 937       }
 938     }
 939   }
 940 
 941   return record;
 942 }
 943 













 944 void HeapShared::resolve_or_init(Klass* k, bool do_init, TRAPS) {
 945   if (!do_init) {
 946     if (k->class_loader_data() == nullptr) {
 947       Klass* resolved_k = SystemDictionary::resolve_or_null(k->name(), CHECK);
 948       assert(resolved_k == k, "classes used by archived heap must not be replaced by JVMTI ClassFileLoadHook");
 949     }
 950   } else {
 951     assert(k->class_loader_data() != nullptr, "must have been resolved by HeapShared::resolve_classes");
 952     if (k->is_instance_klass()) {
 953       InstanceKlass* ik = InstanceKlass::cast(k);
 954       ik->initialize(CHECK);
 955     } else if (k->is_objArray_klass()) {
 956       ObjArrayKlass* oak = ObjArrayKlass::cast(k);
 957       oak->initialize(CHECK);
 958     }
 959   }
 960 }
 961 
 962 void HeapShared::init_archived_fields_for(Klass* k, const ArchivedKlassSubGraphInfoRecord* record) {
 963   verify_the_heap(k, "before");

 993   unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(k);
 994   const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0);
 995   if (record != nullptr) {
 996     Array<int>* entry_field_records = record->entry_field_records();
 997     if (entry_field_records != nullptr) {
 998       int efr_len = entry_field_records->length();
 999       assert(efr_len % 2 == 0, "sanity");
1000       for (int i = 0; i < efr_len; i += 2) {
1001         int root_index = entry_field_records->at(i+1);
1002         clear_root(root_index);
1003       }
1004     }
1005   }
1006 }
1007 
1008 class WalkOopAndArchiveClosure: public BasicOopIterateClosure {
1009   int _level;
1010   bool _record_klasses_only;
1011   KlassSubGraphInfo* _subgraph_info;
1012   oop _referencing_obj;
1013 
1014   // The following are for maintaining a stack for determining
1015   // CachedOopInfo::_referrer
1016   static WalkOopAndArchiveClosure* _current;
1017   WalkOopAndArchiveClosure* _last;
1018  public:
1019   WalkOopAndArchiveClosure(int level,
1020                            bool record_klasses_only,
1021                            KlassSubGraphInfo* subgraph_info,
1022                            oop orig) :
1023     _level(level),
1024     _record_klasses_only(record_klasses_only),
1025     _subgraph_info(subgraph_info),
1026     _referencing_obj(orig) {
1027     _last = _current;
1028     _current = this;
1029   }
1030   ~WalkOopAndArchiveClosure() {
1031     _current = _last;
1032   }
1033   void do_oop(narrowOop *p) { WalkOopAndArchiveClosure::do_oop_work(p); }
1034   void do_oop(      oop *p) { WalkOopAndArchiveClosure::do_oop_work(p); }
1035 
1036  protected:
1037   template <class T> void do_oop_work(T *p) {
1038     oop obj = RawAccess<>::oop_load(p);
1039     if (!CompressedOops::is_null(obj)) {
1040       size_t field_delta = pointer_delta(p, _referencing_obj, sizeof(char));
1041 
1042       if (!_record_klasses_only && log_is_enabled(Debug, cds, heap)) {
1043         ResourceMark rm;
1044         log_debug(cds, heap)("(%d) %s[" SIZE_FORMAT "] ==> " PTR_FORMAT " size " SIZE_FORMAT " %s", _level,
1045                              _referencing_obj->klass()->external_name(), field_delta,
1046                              p2i(obj), obj->size() * HeapWordSize, obj->klass()->external_name());
1047         if (log_is_enabled(Trace, cds, heap)) {
1048           LogTarget(Trace, cds, heap) log;
1049           LogStream out(log);
1050           obj->print_on(&out);
1051         }
1052       }
1053 
1054       bool success = HeapShared::archive_reachable_objects_from(
1055           _level + 1, _subgraph_info, obj);
1056       assert(success, "VM should have exited with unarchivable objects for _level > 1");
1057     }
1058   }
1059 
1060  public:
1061   static WalkOopAndArchiveClosure* current()  { return _current;              }

1071 
1072   template <class T> void check(T *p) {
1073     _result |= (HeapAccess<>::oop_load(p) != nullptr);
1074   }
1075 
1076 public:
1077   PointsToOopsChecker() : _result(false) {}
1078   void do_oop(narrowOop *p) { check(p); }
1079   void do_oop(      oop *p) { check(p); }
1080   bool result() { return _result; }
1081 };
1082 
1083 HeapShared::CachedOopInfo HeapShared::make_cached_oop_info(oop obj) {
1084   WalkOopAndArchiveClosure* walker = WalkOopAndArchiveClosure::current();
1085   oop referrer = (walker == nullptr) ? nullptr : walker->referencing_obj();
1086   PointsToOopsChecker points_to_oops_checker;
1087   obj->oop_iterate(&points_to_oops_checker);
1088   return CachedOopInfo(referrer, points_to_oops_checker.result());
1089 }
1090 






















































1091 // (1) If orig_obj has not been archived yet, archive it.
1092 // (2) If orig_obj has not been seen yet (since start_recording_subgraph() was called),
1093 //     trace all  objects that are reachable from it, and make sure these objects are archived.
1094 // (3) Record the klasses of all orig_obj and all reachable objects.
1095 bool HeapShared::archive_reachable_objects_from(int level,
1096                                                 KlassSubGraphInfo* subgraph_info,
1097                                                 oop orig_obj) {

1098   assert(orig_obj != nullptr, "must be");
1099 
1100   if (!JavaClasses::is_supported_for_archiving(orig_obj)) {
1101     // This object has injected fields that cannot be supported easily, so we disallow them for now.
1102     // If you get an error here, you probably made a change in the JDK library that has added
1103     // these objects that are referenced (directly or indirectly) by static fields.
1104     ResourceMark rm;
1105     log_error(cds, heap)("Cannot archive object of class %s", orig_obj->klass()->external_name());
1106     MetaspaceShared::unrecoverable_writing_error();
1107   }
1108 
1109   // java.lang.Class instances cannot be included in an archived object sub-graph. We only support
1110   // them as Klass::_archived_mirror because they need to be specially restored at run time.
1111   //
1112   // If you get an error here, you probably made a change in the JDK library that has added a Class
1113   // object that is referenced (directly or indirectly) by static fields.
1114   if (java_lang_Class::is_instance(orig_obj) && subgraph_info != _default_subgraph_info) {
1115     log_error(cds, heap)("(%d) Unknown java.lang.Class object is in the archived sub-graph", level);
1116     MetaspaceShared::unrecoverable_writing_error();









1117   }






1118 
1119   if (has_been_seen_during_subgraph_recording(orig_obj)) {
1120     // orig_obj has already been archived and traced. Nothing more to do.
1121     return true;
1122   } else {
1123     set_has_been_seen_during_subgraph_recording(orig_obj);
1124   }
1125 
1126   bool already_archived = has_been_archived(orig_obj);
1127   bool record_klasses_only = already_archived;
1128   if (!already_archived) {
1129     ++_num_new_archived_objs;
1130     if (!archive_object(orig_obj)) {
1131       // Skip archiving the sub-graph referenced from the current entry field.
1132       ResourceMark rm;
1133       log_error(cds, heap)(
1134         "Cannot archive the sub-graph referenced from %s object ("
1135         PTR_FORMAT ") size " SIZE_FORMAT ", skipped.",
1136         orig_obj->klass()->external_name(), p2i(orig_obj), orig_obj->size() * HeapWordSize);
1137       if (level == 1) {
1138         // Don't archive a subgraph root that's too big. For archives static fields, that's OK
1139         // as the Java code will take care of initializing this field dynamically.
1140         return false;
1141       } else {
1142         // We don't know how to handle an object that has been archived, but some of its reachable
1143         // objects cannot be archived. Bail out for now. We might need to fix this in the future if
1144         // we have a real use case.
1145         MetaspaceShared::unrecoverable_writing_error();
1146       }
1147     }
1148   }
1149 
1150   Klass *orig_k = orig_obj->klass();
1151   subgraph_info->add_subgraph_object_klass(orig_k);
1152 
1153   WalkOopAndArchiveClosure walker(level, record_klasses_only, subgraph_info, orig_obj);
1154   orig_obj->oop_iterate(&walker);
1155 
1156   if (CDSEnumKlass::is_enum_obj(orig_obj)) {
1157     CDSEnumKlass::handle_enum_obj(level + 1, subgraph_info, orig_obj);
1158   }
1159   return true;
1160 }
1161 
1162 //
1163 // Start from the given static field in a java mirror and archive the
1164 // complete sub-graph of java heap objects that are reached directly
1165 // or indirectly from the starting object by following references.

1256   if (!CompressedOops::is_null(f)) {
1257     verify_subgraph_from(f);
1258   }
1259 }
1260 
1261 void HeapShared::verify_subgraph_from(oop orig_obj) {
1262   if (!has_been_archived(orig_obj)) {
1263     // It's OK for the root of a subgraph to be not archived. See comments in
1264     // archive_reachable_objects_from().
1265     return;
1266   }
1267 
1268   // Verify that all objects reachable from orig_obj are archived.
1269   init_seen_objects_table();
1270   verify_reachable_objects_from(orig_obj);
1271   delete_seen_objects_table();
1272 }
1273 
1274 void HeapShared::verify_reachable_objects_from(oop obj) {
1275   _num_total_verifications ++;




1276   if (!has_been_seen_during_subgraph_recording(obj)) {
1277     set_has_been_seen_during_subgraph_recording(obj);
1278     assert(has_been_archived(obj), "must be");
1279     VerifySharedOopClosure walker;
1280     obj->oop_iterate(&walker);
1281   }
1282 }
1283 #endif
1284 
1285 // The "default subgraph" contains special objects (see heapShared.hpp) that
1286 // can be accessed before we load any Java classes (including java/lang/Class).
1287 // Make sure that these are only instances of the very few specific types
1288 // that we can handle.
1289 void HeapShared::check_default_subgraph_classes() {
1290   GrowableArray<Klass*>* klasses = _default_subgraph_info->subgraph_object_klasses();
1291   int num = klasses->length();

1292   for (int i = 0; i < num; i++) {
1293     Klass* subgraph_k = klasses->at(i);
1294     if (log_is_enabled(Info, cds, heap)) {
1295       ResourceMark rm;
1296       log_info(cds, heap)(
1297           "Archived object klass (default subgraph %d) => %s",
1298           i, subgraph_k->external_name());
1299     }
1300 
1301     Symbol* name = ArchiveBuilder::current()->get_source_addr(subgraph_k->name());
1302     guarantee(name == vmSymbols::java_lang_Class() ||
1303               name == vmSymbols::java_lang_String() ||
1304               name == vmSymbols::java_lang_ArithmeticException() ||
1305               name == vmSymbols::java_lang_NullPointerException() ||
1306               name == vmSymbols::java_lang_InternalError() ||
1307               name == vmSymbols::object_array_signature() ||
1308               name == vmSymbols::byte_array_signature() ||
1309               name == vmSymbols::char_array_signature(),
1310               "default subgraph can have only these objects");








1311   }
1312 }
1313 
1314 HeapShared::SeenObjectsTable* HeapShared::_seen_objects_table = nullptr;
1315 int HeapShared::_num_new_walked_objs;
1316 int HeapShared::_num_new_archived_objs;
1317 int HeapShared::_num_old_recorded_klasses;
1318 
1319 int HeapShared::_num_total_subgraph_recordings = 0;
1320 int HeapShared::_num_total_walked_objs = 0;
1321 int HeapShared::_num_total_archived_objs = 0;
1322 int HeapShared::_num_total_recorded_klasses = 0;
1323 int HeapShared::_num_total_verifications = 0;
1324 
1325 bool HeapShared::has_been_seen_during_subgraph_recording(oop obj) {
1326   return _seen_objects_table->get(obj) != nullptr;
1327 }
1328 
1329 void HeapShared::set_has_been_seen_during_subgraph_recording(oop obj) {
1330   assert(!has_been_seen_during_subgraph_recording(obj), "sanity");

1376       }
1377     }
1378   }
1379   bool found()     { return _found;  }
1380   int offset()     { return _offset; }
1381 };
1382 
1383 void HeapShared::init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[],
1384                                             TRAPS) {
1385   for (int i = 0; fields[i].valid(); i++) {
1386     ArchivableStaticFieldInfo* info = &fields[i];
1387     TempNewSymbol klass_name =  SymbolTable::new_symbol(info->klass_name);
1388     TempNewSymbol field_name =  SymbolTable::new_symbol(info->field_name);
1389     ResourceMark rm; // for stringStream::as_string() etc.
1390 
1391 #ifndef PRODUCT
1392     bool is_test_class = (ArchiveHeapTestClass != nullptr) && (strcmp(info->klass_name, ArchiveHeapTestClass) == 0);
1393     const char* test_class_name = ArchiveHeapTestClass;
1394 #else
1395     bool is_test_class = false;
1396     const char* test_class_name = ""; // avoid C++ printf checks warnings.
1397 #endif
1398 
1399     if (is_test_class) {
1400       log_warning(cds)("Loading ArchiveHeapTestClass %s ...", test_class_name);
1401     }
1402 
1403     Klass* k = SystemDictionary::resolve_or_fail(klass_name, true, THREAD);
1404     if (HAS_PENDING_EXCEPTION) {
1405       CLEAR_PENDING_EXCEPTION;
1406       stringStream st;
1407       st.print("Fail to initialize archive heap: %s cannot be loaded by the boot loader", info->klass_name);
1408       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
1409     }
1410 
1411     if (!k->is_instance_klass()) {
1412       stringStream st;
1413       st.print("Fail to initialize archive heap: %s is not an instance class", info->klass_name);
1414       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
1415     }
1416 

1543 void HeapShared::archive_object_subgraphs(ArchivableStaticFieldInfo fields[],
1544                                           bool is_full_module_graph) {
1545   _num_total_subgraph_recordings = 0;
1546   _num_total_walked_objs = 0;
1547   _num_total_archived_objs = 0;
1548   _num_total_recorded_klasses = 0;
1549   _num_total_verifications = 0;
1550 
1551   // For each class X that has one or more archived fields:
1552   // [1] Dump the subgraph of each archived field
1553   // [2] Create a list of all the class of the objects that can be reached
1554   //     by any of these static fields.
1555   //     At runtime, these classes are initialized before X's archived fields
1556   //     are restored by HeapShared::initialize_from_archived_subgraph().
1557   int i;
1558   for (int i = 0; fields[i].valid(); ) {
1559     ArchivableStaticFieldInfo* info = &fields[i];
1560     const char* klass_name = info->klass_name;
1561     start_recording_subgraph(info->klass, klass_name, is_full_module_graph);
1562 

1563     // If you have specified consecutive fields of the same klass in
1564     // fields[], these will be archived in the same
1565     // {start_recording_subgraph ... done_recording_subgraph} pass to
1566     // save time.
1567     for (; fields[i].valid(); i++) {
1568       ArchivableStaticFieldInfo* f = &fields[i];
1569       if (f->klass_name != klass_name) {
1570         break;
1571       }
1572 

1573       archive_reachable_objects_from_static_field(f->klass, f->klass_name,
1574                                                   f->offset, f->field_name);
1575     }
1576     done_recording_subgraph(info->klass, klass_name);
1577   }
1578 
1579   log_info(cds, heap)("Archived subgraph records = %d",
1580                       _num_total_subgraph_recordings);
1581   log_info(cds, heap)("  Walked %d objects", _num_total_walked_objs);
1582   log_info(cds, heap)("  Archived %d objects", _num_total_archived_objs);
1583   log_info(cds, heap)("  Recorded %d klasses", _num_total_recorded_klasses);
1584 
1585 #ifndef PRODUCT
1586   for (int i = 0; fields[i].valid(); i++) {
1587     ArchivableStaticFieldInfo* f = &fields[i];
1588     verify_subgraph_from_static_field(f->klass, f->offset);
1589   }
1590   log_info(cds, heap)("  Verified %d references", _num_total_verifications);
1591 #endif
1592 }

  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"

  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 }

 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(CDSConfig::is_using_archive(), "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() && CDSConfig::is_using_archive(), "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(CDSConfig::is_using_archive(), "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     }

 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 CDSConfig::is_dumping_invokedynamic() && (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 (CDSConfig::is_dumping_reflection_data() && 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   }

 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::start_finding_archivable_hidden_classes() {
 744   NoSafepointVerifier nsv;
 745 
 746   init_seen_objects_table();
 747 
 748   find_archivable_hidden_classes_helper(archive_subgraph_entry_fields);
 749   if (CDSConfig::is_dumping_full_module_graph()) {
 750     find_archivable_hidden_classes_helper(fmg_archive_subgraph_entry_fields);
 751   }
 752 }
 753 
 754 void HeapShared::end_finding_archivable_hidden_classes() {
 755   NoSafepointVerifier nsv;
 756 
 757   delete_seen_objects_table();
 758 }
 759 
 760 void HeapShared::find_archivable_hidden_classes_helper(ArchivableStaticFieldInfo fields[]) {
 761   if (!CDSConfig::is_dumping_heap()) {
 762     return;
 763   }
 764   int i;
 765   for (int i = 0; fields[i].valid(); ) {
 766     ArchivableStaticFieldInfo* info = &fields[i];
 767     const char* klass_name = info->klass_name;
 768     for (; fields[i].valid(); i++) {
 769       ArchivableStaticFieldInfo* f = &fields[i];
 770       if (f->klass_name != klass_name) {
 771         break;
 772       }
 773 
 774       InstanceKlass* k = f->klass;
 775       oop m = k->java_mirror();
 776       oop o = m->obj_field(f->offset);
 777       if (o != nullptr) {
 778         find_archivable_hidden_classes_in_object(o);
 779       }
 780     }
 781   }
 782 }
 783 
 784 class HeapShared::FindHiddenClassesOopClosure: public BasicOopIterateClosure {
 785   GrowableArray<oop> _stack;
 786   template <class T> void do_oop_work(T *p) {
 787     // Recurse on a GrowableArray to avoid overflowing the C stack.
 788     oop o = RawAccess<>::oop_load(p);
 789     if (o != nullptr) {
 790       _stack.append(o);
 791     }
 792   }
 793 
 794  public:
 795 
 796   void do_oop(narrowOop *p) { FindHiddenClassesOopClosure::do_oop_work(p); }
 797   void do_oop(      oop *p) { FindHiddenClassesOopClosure::do_oop_work(p); }
 798 
 799   FindHiddenClassesOopClosure(oop o) {
 800     _stack.append(o);
 801   }
 802   oop pop() {
 803     if (_stack.length() == 0) {
 804       return nullptr;
 805     } else {
 806       return _stack.pop();
 807     }
 808   }
 809 };
 810 
 811 void HeapShared::find_archivable_hidden_classes_in_object(oop root) {
 812   ResourceMark rm;
 813   FindHiddenClassesOopClosure c(root);
 814   oop o;
 815   while ((o = c.pop()) != nullptr) {
 816     if (!has_been_seen_during_subgraph_recording(o)) {
 817       set_has_been_seen_during_subgraph_recording(o);
 818 
 819       if (java_lang_Class::is_instance(o)) {
 820         Klass* k = java_lang_Class::as_Klass(o);
 821         if (k != nullptr && k->is_instance_klass()) {
 822           SystemDictionaryShared::mark_required_class(InstanceKlass::cast(k));
 823         }
 824       } else if (java_lang_invoke_ResolvedMethodName::is_instance(o)) {
 825         Method* m = java_lang_invoke_ResolvedMethodName::vmtarget(o);
 826         if (m != nullptr && m->method_holder() != nullptr) {
 827           SystemDictionaryShared::mark_required_class(m->method_holder());
 828         }
 829       }
 830 
 831       o->oop_iterate(&c);
 832     }
 833   }
 834 }
 835 
 836 void HeapShared::archive_objects(ArchiveHeapInfo *heap_info) {
 837   {
 838     NoSafepointVerifier nsv;
 839 
 840     _default_subgraph_info = init_subgraph_info(vmClasses::Object_klass(), false);
 841     _trace = new GrowableArrayCHeap<oop, mtClassShared>(250);
 842     _context = new GrowableArrayCHeap<const char*, mtClassShared>(250);
 843 
 844     // Cache for recording where the archived objects are copied to
 845     create_archived_object_cache();
 846 
 847     if (UseCompressedOops || UseG1GC) {
 848       log_info(cds)("Heap range = [" PTR_FORMAT " - "  PTR_FORMAT "]",
 849                     UseCompressedOops ? p2i(CompressedOops::begin()) :
 850                                          p2i((address)G1CollectedHeap::heap()->reserved().start()),
 851                     UseCompressedOops ? p2i(CompressedOops::end()) :
 852                                          p2i((address)G1CollectedHeap::heap()->reserved().end()));
 853     }
 854     copy_objects();
 855 
 856     if (!SkipArchiveHeapVerification) {
 857       CDSHeapVerifier::verify();
 858     }
 859     check_default_subgraph_classes();
 860   }
 861 
 862   ArchiveHeapWriter::write(_pending_roots, heap_info);
 863 }
 864 
 865 void HeapShared::copy_interned_strings() {
 866   init_seen_objects_table();
 867 
 868   auto copier = [&] (oop s, bool value_ignored) {
 869     assert(s != nullptr, "sanity");
 870     assert(!ArchiveHeapWriter::is_string_too_large_to_archive(s), "large strings must have been filtered");
 871     bool success = archive_reachable_objects_from(1, _default_subgraph_info, s);
 872     assert(success, "must be");
 873     // Prevent string deduplication from changing the value field to
 874     // something not in the archive.
 875     java_lang_String::set_deduplication_forbidden(s);
 876   };
 877   _dumped_interned_strings->iterate_all(copier);
 878 

 945 // Add the Klass* for an object in the current KlassSubGraphInfo's subgraphs.
 946 // Only objects of boot classes can be included in sub-graph.
 947 void KlassSubGraphInfo::add_subgraph_object_klass(Klass* orig_k) {
 948   assert(CDSConfig::is_dumping_heap(), "dump time only");
 949   Klass* buffered_k = ArchiveBuilder::get_buffered_klass(orig_k);
 950 
 951   if (_subgraph_object_klasses == nullptr) {
 952     _subgraph_object_klasses =
 953       new (mtClass) GrowableArray<Klass*>(50, mtClass);
 954   }
 955 
 956   assert(ArchiveBuilder::current()->is_in_buffer_space(buffered_k), "must be a shared class");
 957 
 958   if (_k == buffered_k) {
 959     // Don't add the Klass containing the sub-graph to it's own klass
 960     // initialization list.
 961     return;
 962   }
 963 
 964   if (buffered_k->is_instance_klass()) {
 965     if (!CDSConfig::is_dumping_invokedynamic()) {
 966       // FIXME: this supports Lambda Proxy classes
 967       assert(InstanceKlass::cast(buffered_k)->is_shared_boot_class(),
 968              "must be boot class");
 969     }
 970     // vmClasses::xxx_klass() are not updated, need to check
 971     // the original Klass*
 972     if (orig_k == vmClasses::String_klass() ||
 973         orig_k == vmClasses::Object_klass()) {
 974       // Initialized early during VM initialization. No need to be added
 975       // to the sub-graph object class list.
 976       return;
 977     }
 978     check_allowed_klass(InstanceKlass::cast(orig_k));
 979   } else if (buffered_k->is_objArray_klass()) {
 980     Klass* abk = ObjArrayKlass::cast(buffered_k)->bottom_klass();
 981     if (abk->is_instance_klass()) {
 982       assert(InstanceKlass::cast(abk)->is_shared_boot_class(),
 983             "must be boot class");
 984       check_allowed_klass(InstanceKlass::cast(ObjArrayKlass::cast(orig_k)->bottom_klass()));
 985     }
 986     if (buffered_k == Universe::objectArrayKlass()) {
 987       // Initialized early during Universe::genesis. No need to be added
 988       // to the list.
 989       return;
 990     }
 991   } else {
 992     assert(buffered_k->is_typeArray_klass(), "must be");
 993     // Primitive type arrays are created early during Universe::genesis.
 994     return;
 995   }
 996 
 997   if (log_is_enabled(Debug, cds, heap)) {
 998     if (!_subgraph_object_klasses->contains(buffered_k)) {
 999       ResourceMark rm;
1000       log_debug(cds, heap)("Adding klass %s", orig_k->external_name());
1001     }
1002   }
1003 
1004   _subgraph_object_klasses->append_if_missing(buffered_k);
1005   _has_non_early_klasses |= is_non_early_klass(orig_k);
1006 }
1007 
1008 void KlassSubGraphInfo::check_allowed_klass(InstanceKlass* ik) {
1009   if (CDSConfig::is_dumping_invokedynamic()) {
1010     // FIXME -- this allows LambdaProxy classes
1011     return;
1012   }
1013   if (ik->module()->name() == vmSymbols::java_base()) {
1014     assert(ik->package() != nullptr, "classes in java.base cannot be in unnamed package");
1015     return;
1016   }
1017 
1018 #ifndef PRODUCT
1019   if (!ik->module()->is_named() && ik->package() == nullptr) {
1020     // This class is loaded by ArchiveHeapTestClass
1021     return;
1022   }
1023   const char* extra_msg = ", or in an unnamed package of an unnamed module";
1024 #else
1025   const char* extra_msg = "";
1026 #endif
1027 
1028   ResourceMark rm;
1029   log_error(cds, heap)("Class %s not allowed in archive heap. Must be in java.base%s",
1030                        ik->external_name(), extra_msg);
1031   MetaspaceShared::unrecoverable_writing_error();
1032 }

1090     _subgraph_object_klasses =
1091       ArchiveBuilder::new_ro_array<Klass*>(num_subgraphs_klasses);
1092     for (int i = 0; i < num_subgraphs_klasses; i++) {
1093       Klass* subgraph_k = subgraph_object_klasses->at(i);
1094       if (log_is_enabled(Info, cds, heap)) {
1095         ResourceMark rm;
1096         log_info(cds, heap)(
1097           "Archived object klass %s (%2d) => %s",
1098           _k->external_name(), i, subgraph_k->external_name());
1099       }
1100       _subgraph_object_klasses->at_put(i, subgraph_k);
1101       ArchivePtrMarker::mark_pointer(_subgraph_object_klasses->adr_at(i));
1102     }
1103   }
1104 
1105   ArchivePtrMarker::mark_pointer(&_k);
1106   ArchivePtrMarker::mark_pointer(&_entry_field_records);
1107   ArchivePtrMarker::mark_pointer(&_subgraph_object_klasses);
1108 }
1109 
1110 class HeapShared::CopyKlassSubGraphInfoToArchive : StackObj {
1111 public:
1112   CompactHashtableWriter* _writer;
1113   CopyKlassSubGraphInfoToArchive(CompactHashtableWriter* writer) : _writer(writer) {}
1114 
1115   bool do_entry(Klass* klass, KlassSubGraphInfo& info) {
1116     if (info.subgraph_object_klasses() != nullptr || info.subgraph_entry_fields() != nullptr) {
1117       ArchivedKlassSubGraphInfoRecord* record = HeapShared::archive_subgraph_info(&info);



1118       Klass* buffered_k = ArchiveBuilder::get_buffered_klass(klass);
1119       unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary((address)buffered_k);
1120       u4 delta = ArchiveBuilder::current()->any_to_offset_u4(record);
1121       _writer->add(hash, delta);
1122     }
1123     return true; // keep on iterating
1124   }
1125 };
1126 
1127 ArchivedKlassSubGraphInfoRecord* HeapShared::archive_subgraph_info(KlassSubGraphInfo* info) {
1128   ArchivedKlassSubGraphInfoRecord* record =
1129       (ArchivedKlassSubGraphInfoRecord*)ArchiveBuilder::ro_region_alloc(sizeof(ArchivedKlassSubGraphInfoRecord));
1130   record->init(info);
1131   return record;
1132 }
1133 
1134 // Build the records of archived subgraph infos, which include:
1135 // - Entry points to all subgraphs from the containing class mirror. The entry
1136 //   points are static fields in the mirror. For each entry point, the field
1137 //   offset, and value are recorded in the sub-graph
1138 //   info. The value is stored back to the corresponding field at runtime.
1139 // - A list of klasses that need to be loaded/initialized before archived
1140 //   java object sub-graph can be accessed at runtime.
1141 void HeapShared::write_subgraph_info_table() {
1142   // Allocate the contents of the hashtable(s) inside the RO region of the CDS archive.
1143   DumpTimeKlassSubGraphInfoTable* d_table = _dump_time_subgraph_info_table;
1144   CompactHashtableStats stats;
1145 
1146   _run_time_subgraph_info_table.reset();
1147 
1148   CompactHashtableWriter writer(d_table->_count, &stats);
1149   CopyKlassSubGraphInfoToArchive copy(&writer);
1150   d_table->iterate(&copy);
1151   writer.dump(&_run_time_subgraph_info_table, "subgraphs");
1152 
1153   _runtime_default_subgraph_info = archive_subgraph_info(_default_subgraph_info);
1154 
1155 #ifndef PRODUCT
1156   if (ArchiveHeapTestClass != nullptr) {
1157     size_t len = strlen(ArchiveHeapTestClass) + 1;
1158     Array<char>* array = ArchiveBuilder::new_ro_array<char>((int)len);
1159     strncpy(array->adr_at(0), ArchiveHeapTestClass, len);
1160     _archived_ArchiveHeapTestClass = array;
1161   }
1162 #endif
1163   if (log_is_enabled(Info, cds, heap)) {
1164     print_stats();
1165   }
1166 }
1167 
1168 void HeapShared::serialize_misc_info(SerializeClosure* soc) {
1169   soc->do_int(&_permobj_segments);
1170 }
1171 
1172 void HeapShared::init_roots(oop roots_oop) {
1173   if (roots_oop != nullptr) {
1174     assert(ArchiveHeapLoader::is_in_use(), "must be");
1175     _roots = OopHandle(Universe::vm_global(), roots_oop);
1176   }
1177 }
1178 
1179 void HeapShared::serialize_tables(SerializeClosure* soc) {
1180 
1181 #ifndef PRODUCT
1182   soc->do_ptr(&_archived_ArchiveHeapTestClass);
1183   if (soc->reading() && _archived_ArchiveHeapTestClass != nullptr) {
1184     _test_class_name = _archived_ArchiveHeapTestClass->adr_at(0);
1185     setup_test_class(_test_class_name);
1186   }
1187 #endif
1188 
1189   _run_time_subgraph_info_table.serialize_header(soc);
1190   soc->do_ptr(&_runtime_default_subgraph_info);
1191 
1192 }
1193 
1194 static void verify_the_heap(Klass* k, const char* which) {
1195   if (VerifyArchivedFields > 0) {
1196     ResourceMark rm;
1197     log_info(cds, heap)("Verify heap %s initializing static field(s) in %s",
1198                         which, k->external_name());
1199 
1200     VM_Verify verify_op;
1201     VMThread::execute(&verify_op);
1202 
1203     if (VerifyArchivedFields > 1 && is_init_completed()) {
1204       // At this time, the oop->klass() of some archived objects in the heap may not
1205       // have been loaded into the system dictionary yet. Nevertheless, oop->klass() should
1206       // have enough information (object size, oop maps, etc) so that a GC can be safely
1207       // performed.
1208       //
1209       // -XX:VerifyArchivedFields=2 force a GC to happen in such an early stage
1210       // to check for GC safety.
1211       log_info(cds, heap)("Trigger GC %s initializing static field(s) in %s",
1212                           which, k->external_name());
1213       FlagSetting fs1(VerifyBeforeGC, true);
1214       FlagSetting fs2(VerifyDuringGC, true);
1215       FlagSetting fs3(VerifyAfterGC,  true);
1216       Universe::heap()->collect(GCCause::_java_lang_system_gc);
1217     }
1218   }
1219 }
1220 
1221 // Before GC can execute, we must ensure that all oops reachable from HeapShared::roots()
1222 // have a valid klass. I.e., oopDesc::klass() must have already been resolved.
1223 //
1224 // Note: if a ArchivedKlassSubGraphInfoRecord contains non-early classes, and JVMTI
1225 // ClassFileLoadHook is enabled, it's possible for this class to be dynamically replaced. In
1226 // this case, we will not load the ArchivedKlassSubGraphInfoRecord and will clear its roots.
1227 void HeapShared::resolve_classes(JavaThread* current) {
1228   assert(CDSConfig::is_using_archive(), "runtime only!");
1229   if (!ArchiveHeapLoader::is_in_use()) {
1230     return; // nothing to do
1231   }
1232 
1233   if (!CDSConfig::has_preloaded_classes()) {
1234     assert( _runtime_default_subgraph_info != nullptr, "must be");
1235     Array<Klass*>* klasses = _runtime_default_subgraph_info->subgraph_object_klasses();
1236     if (klasses != nullptr) {
1237       for (int i = 0; i < klasses->length(); i++) {
1238         Klass* k = klasses->at(i);
1239         ExceptionMark em(current); // no exception can happen here
1240         resolve_or_init(k, /*do_init*/false, current);
1241       }
1242     }
1243   }
1244 
1245   resolve_classes_for_subgraphs(current, archive_subgraph_entry_fields);
1246   resolve_classes_for_subgraphs(current, fmg_archive_subgraph_entry_fields);
1247 }
1248 
1249 void HeapShared::resolve_classes_for_subgraphs(JavaThread* current, ArchivableStaticFieldInfo fields[]) {
1250   for (int i = 0; fields[i].valid(); i++) {
1251     ArchivableStaticFieldInfo* info = &fields[i];
1252     TempNewSymbol klass_name = SymbolTable::new_symbol(info->klass_name);
1253     InstanceKlass* k = SystemDictionaryShared::find_builtin_class(klass_name);
1254     assert(k != nullptr && k->is_shared_boot_class(), "sanity");
1255     resolve_classes_for_subgraph_of(current, k);
1256   }
1257 }
1258 
1259 void HeapShared::resolve_classes_for_subgraph_of(JavaThread* current, Klass* k) {
1260   JavaThread* THREAD = current;
1261   ExceptionMark em(THREAD);
1262   const ArchivedKlassSubGraphInfoRecord* record =
1263    resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/false, THREAD);
1264   if (HAS_PENDING_EXCEPTION) {
1265    CLEAR_PENDING_EXCEPTION;
1266   }
1267   if (record == nullptr) {
1268    clear_archived_roots_of(k);
1269   }
1270 }
1271 
1272 void HeapShared::initialize_java_lang_invoke(TRAPS) {
1273   if (CDSConfig::is_loading_invokedynamic() || CDSConfig::is_dumping_invokedynamic()) {
1274     resolve_or_init("java/lang/invoke/Invokers$Holder", true, CHECK);
1275     resolve_or_init("java/lang/invoke/MethodHandle", true, CHECK);
1276     resolve_or_init("java/lang/invoke/MethodHandleNatives", true, CHECK);
1277     resolve_or_init("java/lang/invoke/DirectMethodHandle$Holder", true, CHECK);
1278     resolve_or_init("java/lang/invoke/DelegatingMethodHandle$Holder", true, CHECK);
1279     resolve_or_init("java/lang/invoke/LambdaForm$Holder", true, CHECK);
1280     resolve_or_init("java/lang/invoke/BoundMethodHandle$Species_L", true, CHECK);
1281   }
1282 }
1283 
1284 void HeapShared::initialize_default_subgraph_classes(Handle loader, TRAPS) {
1285   if (!ArchiveHeapLoader::is_in_use()) {
1286     return;
1287   }
1288 
1289   assert( _runtime_default_subgraph_info != nullptr, "must be");
1290   Array<Klass*>* klasses = _runtime_default_subgraph_info->subgraph_object_klasses();
1291   if (klasses != nullptr) {
1292     for (int pass = 0; pass < 2; pass ++) {
1293       for (int i = 0; i < klasses->length(); i++) {
1294         Klass* k = klasses->at(i);
1295         if (k->class_loader_data() == nullptr) {
1296           // This class is not yet loaded. We will initialize it in a later phase.
1297           continue;
1298         }
1299         if (k->class_loader() == loader()) {
1300           if (pass == 0) {
1301             if (k->is_instance_klass()) {
1302               InstanceKlass::cast(k)->link_class(CHECK);
1303             }
1304           } else {
1305             resolve_or_init(k, /*do_init*/true, CHECK);
1306           }
1307         }
1308       }
1309     }
1310   }
1311 }
1312 
1313 void HeapShared::initialize_from_archived_subgraph(JavaThread* current, Klass* k) {
1314   JavaThread* THREAD = current;
1315   if (!ArchiveHeapLoader::is_in_use()) {
1316     return; // nothing to do
1317   }
1318 
1319   ExceptionMark em(THREAD);
1320   const ArchivedKlassSubGraphInfoRecord* record =
1321     resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/true, THREAD);
1322 
1323   if (HAS_PENDING_EXCEPTION) {
1324     CLEAR_PENDING_EXCEPTION;
1325     // None of the field value will be set if there was an exception when initializing the classes.
1326     // The java code will not see any of the archived objects in the
1327     // subgraphs referenced from k in this case.
1328     return;
1329   }
1330 
1331   if (record != nullptr) {
1332     init_archived_fields_for(k, record);

1366         log_info(cds, heap)("subgraph %s cannot be used because full module graph is disabled",
1367                             k->external_name());
1368       }
1369       return nullptr;
1370     }
1371 
1372     if (record->has_non_early_klasses() && JvmtiExport::should_post_class_file_load_hook()) {
1373       if (log_is_enabled(Info, cds, heap)) {
1374         ResourceMark rm(THREAD);
1375         log_info(cds, heap)("subgraph %s cannot be used because JVMTI ClassFileLoadHook is enabled",
1376                             k->external_name());
1377       }
1378       return nullptr;
1379     }
1380 
1381     if (log_is_enabled(Info, cds, heap)) {
1382       ResourceMark rm;
1383       log_info(cds, heap)("%s subgraph %s ", do_init ? "init" : "resolve", k->external_name());
1384     }
1385 
1386     if (do_init && k->name() == vmSymbols::java_lang_invoke_MethodType()) {
1387       // FIXME - hack.
1388       //
1389       // (The real fix would be to archive the MethodType class in its already initialized state. That
1390       //  way we don't need to re-execute the <clinit> methods)
1391       //
1392       // We need to do this to break a cycle in the way the archived subgraphs are restored. Without this block, we
1393       // have the following sequence
1394       //
1395       // MethodType.<clinit>()
1396       //  -> CDS.initializeFromArchive(MethodType.class);
1397       //   -> (this "if" block)
1398       //   -> resolve_or_init("MethodType", ...); // this does nothing because MethodType.<clinit> is already executing
1399       //   -> resolve_or_init("DirectMethodHandle", ...); // this class is in record->subgraph_object_klasses();
1400       //      -> DirectMethodHandle.<clinit>()
1401       //          -> MethodType.methodType()
1402       //             -> MethodType.genericMethodType()
1403       //               -> aaload MethodType.objectOnlyTypes[n]; <<<< here
1404       //
1405       // We need to restore MethodType.objectOnlyTypes here, or else the above aaload will
1406       // get an NPE.
1407       Array<int>* entry_field_records = record->entry_field_records();
1408       assert(entry_field_records != nullptr, "must be");
1409       int efr_len = entry_field_records->length();
1410       assert(efr_len == 2, "must be");
1411       int root_index = entry_field_records->at(1);
1412       oop obj = get_root(root_index, /*clear=*/false);
1413       if (obj != nullptr) {
1414         objArrayOop archivedObjects = objArrayOop(obj);
1415         InstanceKlass* ik = InstanceKlass::cast(k);
1416         oop m = ik->java_mirror();
1417 
1418         {
1419           fieldDescriptor fd;
1420           TempNewSymbol name = SymbolTable::new_symbol("archivedMethodTypes");
1421           TempNewSymbol sig  = SymbolTable::new_symbol("Ljava/util/HashMap;");
1422           Klass* result = ik->find_field(name, sig, true, &fd);
1423           assert(result != nullptr, "must be");
1424           m->obj_field_put(fd.offset(), archivedObjects->obj_at(0));
1425         }
1426 
1427         {
1428           fieldDescriptor fd;
1429           TempNewSymbol name = SymbolTable::new_symbol("objectOnlyTypes");
1430           TempNewSymbol sig  = SymbolTable::new_symbol("[Ljava/lang/invoke/MethodType;");
1431           Klass* result = ik->find_field(name, sig, true, &fd);
1432           assert(result != nullptr, "must be");
1433           m->obj_field_put(fd.offset(), archivedObjects->obj_at(1));
1434         }
1435       }
1436     }
1437 
1438     resolve_or_init(k, do_init, CHECK_NULL);
1439 
1440     // Load/link/initialize the klasses of the objects in the subgraph.
1441     // nullptr class loader is used.
1442     Array<Klass*>* klasses = record->subgraph_object_klasses();
1443     if (klasses != nullptr) {
1444       for (int i = 0; i < klasses->length(); i++) {
1445         Klass* klass = klasses->at(i);
1446         if (!klass->is_shared()) {
1447           return nullptr;
1448         }
1449         resolve_or_init(klass, do_init, CHECK_NULL);
1450       }
1451     }
1452   }
1453 
1454   return record;
1455 }
1456 
1457 void HeapShared::resolve_or_init(const char* klass_name, bool do_init, TRAPS) {
1458   TempNewSymbol klass_name_sym =  SymbolTable::new_symbol(klass_name);
1459   InstanceKlass* k = SystemDictionaryShared::find_builtin_class(klass_name_sym);
1460   if (k == nullptr) {
1461     return;
1462   }
1463   assert(k->is_shared_boot_class(), "sanity");
1464   resolve_or_init(k, false, CHECK);
1465   if (do_init) {
1466     resolve_or_init(k, true, CHECK);
1467   }
1468 }
1469 
1470 void HeapShared::resolve_or_init(Klass* k, bool do_init, TRAPS) {
1471   if (!do_init) {
1472     if (k->class_loader_data() == nullptr) {
1473       Klass* resolved_k = SystemDictionary::resolve_or_null(k->name(), CHECK);
1474       assert(resolved_k == k, "classes used by archived heap must not be replaced by JVMTI ClassFileLoadHook");
1475     }
1476   } else {
1477     assert(k->class_loader_data() != nullptr, "must have been resolved by HeapShared::resolve_classes");
1478     if (k->is_instance_klass()) {
1479       InstanceKlass* ik = InstanceKlass::cast(k);
1480       ik->initialize(CHECK);
1481     } else if (k->is_objArray_klass()) {
1482       ObjArrayKlass* oak = ObjArrayKlass::cast(k);
1483       oak->initialize(CHECK);
1484     }
1485   }
1486 }
1487 
1488 void HeapShared::init_archived_fields_for(Klass* k, const ArchivedKlassSubGraphInfoRecord* record) {
1489   verify_the_heap(k, "before");

1519   unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(k);
1520   const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0);
1521   if (record != nullptr) {
1522     Array<int>* entry_field_records = record->entry_field_records();
1523     if (entry_field_records != nullptr) {
1524       int efr_len = entry_field_records->length();
1525       assert(efr_len % 2 == 0, "sanity");
1526       for (int i = 0; i < efr_len; i += 2) {
1527         int root_index = entry_field_records->at(i+1);
1528         clear_root(root_index);
1529       }
1530     }
1531   }
1532 }
1533 
1534 class WalkOopAndArchiveClosure: public BasicOopIterateClosure {
1535   int _level;
1536   bool _record_klasses_only;
1537   KlassSubGraphInfo* _subgraph_info;
1538   oop _referencing_obj;

1539   // The following are for maintaining a stack for determining
1540   // CachedOopInfo::_referrer
1541   static WalkOopAndArchiveClosure* _current;
1542   WalkOopAndArchiveClosure* _last;
1543  public:
1544   WalkOopAndArchiveClosure(int level,
1545                            bool record_klasses_only,
1546                            KlassSubGraphInfo* subgraph_info,
1547                            oop orig) :
1548     _level(level),
1549     _record_klasses_only(record_klasses_only),
1550     _subgraph_info(subgraph_info),
1551     _referencing_obj(orig) {
1552     _last = _current;
1553     _current = this;
1554   }
1555   ~WalkOopAndArchiveClosure() {
1556     _current = _last;
1557   }
1558   void do_oop(narrowOop *p) { WalkOopAndArchiveClosure::do_oop_work(p); }
1559   void do_oop(      oop *p) { WalkOopAndArchiveClosure::do_oop_work(p); }
1560 
1561  protected:
1562   template <class T> void do_oop_work(T *p) {
1563     oop obj = RawAccess<>::oop_load(p);
1564     if (!CompressedOops::is_null(obj)) {
1565       size_t field_delta = pointer_delta(p, _referencing_obj, sizeof(char));

1566       if (!_record_klasses_only && log_is_enabled(Debug, cds, heap)) {
1567         ResourceMark rm;
1568         log_debug(cds, heap)("(%d) %s[" SIZE_FORMAT "] ==> " PTR_FORMAT " size " SIZE_FORMAT " %s", _level,
1569                              _referencing_obj->klass()->external_name(), field_delta,
1570                              p2i(obj), obj->size() * HeapWordSize, obj->klass()->external_name());
1571         if (log_is_enabled(Trace, cds, heap)) {
1572           LogTarget(Trace, cds, heap) log;
1573           LogStream out(log);
1574           obj->print_on(&out);
1575         }
1576       }
1577 
1578       bool success = HeapShared::archive_reachable_objects_from(
1579           _level + 1, _subgraph_info, obj);
1580       assert(success, "VM should have exited with unarchivable objects for _level > 1");
1581     }
1582   }
1583 
1584  public:
1585   static WalkOopAndArchiveClosure* current()  { return _current;              }

1595 
1596   template <class T> void check(T *p) {
1597     _result |= (HeapAccess<>::oop_load(p) != nullptr);
1598   }
1599 
1600 public:
1601   PointsToOopsChecker() : _result(false) {}
1602   void do_oop(narrowOop *p) { check(p); }
1603   void do_oop(      oop *p) { check(p); }
1604   bool result() { return _result; }
1605 };
1606 
1607 HeapShared::CachedOopInfo HeapShared::make_cached_oop_info(oop obj) {
1608   WalkOopAndArchiveClosure* walker = WalkOopAndArchiveClosure::current();
1609   oop referrer = (walker == nullptr) ? nullptr : walker->referencing_obj();
1610   PointsToOopsChecker points_to_oops_checker;
1611   obj->oop_iterate(&points_to_oops_checker);
1612   return CachedOopInfo(referrer, points_to_oops_checker.result());
1613 }
1614 
1615 // We currently allow only the box classes, which are initialized very early by
1616 // HeapShared::init_box_classes().
1617 bool HeapShared::can_mirror_be_used_in_subgraph(oop orig_java_mirror) {
1618   return java_lang_Class::is_primitive(orig_java_mirror)
1619     || orig_java_mirror == vmClasses::Boolean_klass()->java_mirror()
1620     || orig_java_mirror == vmClasses::Character_klass()->java_mirror()
1621     || orig_java_mirror == vmClasses::Float_klass()->java_mirror()
1622     || orig_java_mirror == vmClasses::Double_klass()->java_mirror()
1623     || orig_java_mirror == vmClasses::Byte_klass()->java_mirror()
1624     || orig_java_mirror == vmClasses::Short_klass()->java_mirror()
1625     || orig_java_mirror == vmClasses::Integer_klass()->java_mirror()
1626     || orig_java_mirror == vmClasses::Long_klass()->java_mirror()
1627     || orig_java_mirror == vmClasses::Void_klass()->java_mirror()
1628     || orig_java_mirror == vmClasses::Object_klass()->java_mirror();
1629 }
1630 
1631 void HeapShared::init_box_classes(TRAPS) {
1632   if (ArchiveHeapLoader::is_in_use()) {
1633     vmClasses::Boolean_klass()->initialize(CHECK);
1634     vmClasses::Character_klass()->initialize(CHECK);
1635     vmClasses::Float_klass()->initialize(CHECK);
1636     vmClasses::Double_klass()->initialize(CHECK);
1637     vmClasses::Byte_klass()->initialize(CHECK);
1638     vmClasses::Short_klass()->initialize(CHECK);
1639     vmClasses::Integer_klass()->initialize(CHECK);
1640     vmClasses::Long_klass()->initialize(CHECK);
1641     vmClasses::Void_klass()->initialize(CHECK);
1642   }
1643 }
1644 
1645 void HeapShared::exit_on_error() {
1646   if (_context != nullptr) {
1647     ResourceMark rm;
1648     LogStream ls(Log(cds, heap)::error());
1649     ls.print_cr("Context");
1650     for (int i = 0; i < _context->length(); i++) {
1651       const char* s = _context->at(i);
1652       ls.print_cr("- %s", s);
1653     }
1654   }
1655   if (_trace != nullptr) {
1656     ResourceMark rm;
1657     LogStream ls(Log(cds, heap)::error());
1658     ls.print_cr("Reference trace");
1659     for (int i = 0; i < _trace->length(); i++) {
1660       oop orig_obj = _trace->at(i);
1661       ls.print_cr("[%d] ========================================", i);
1662       orig_obj->print_on(&ls);
1663       ls.cr();
1664     }
1665   }
1666   MetaspaceShared::unrecoverable_writing_error();
1667 }
1668 
1669 // (1) If orig_obj has not been archived yet, archive it.
1670 // (2) If orig_obj has not been seen yet (since start_recording_subgraph() was called),
1671 //     trace all  objects that are reachable from it, and make sure these objects are archived.
1672 // (3) Record the klasses of all orig_obj and all reachable objects.
1673 bool HeapShared::archive_reachable_objects_from(int level,
1674                                                 KlassSubGraphInfo* subgraph_info,
1675                                                 oop orig_obj) {
1676   ArchivingObjectMark mark(orig_obj);
1677   assert(orig_obj != nullptr, "must be");
1678 
1679   if (!JavaClasses::is_supported_for_archiving(orig_obj)) {
1680     // This object has injected fields that cannot be supported easily, so we disallow them for now.
1681     // If you get an error here, you probably made a change in the JDK library that has added
1682     // these objects that are referenced (directly or indirectly) by static fields.
1683     ResourceMark rm;
1684     log_error(cds, heap)("Cannot archive object of class %s", orig_obj->klass()->external_name());
1685     exit_on_error();
1686   }
1687 
1688 #if 0




1689   if (java_lang_Class::is_instance(orig_obj) && subgraph_info != _default_subgraph_info) {
1690     if (can_mirror_be_used_in_subgraph(orig_obj)) {
1691       orig_obj = scratch_java_mirror(orig_obj);
1692       assert(orig_obj != nullptr, "must be archived");
1693     } else {
1694       // Don't follow the fields -- they will be nulled out when the mirror was copied
1695 
1696       // FIXME - we should preserve the static fields of LambdaForm classes (and other hidden classes?)
1697       // so we need to walk the oop fields.
1698       orig_obj = scratch_java_mirror(orig_obj);
1699       assert(orig_obj != nullptr, "must be archived");
1700     }
1701   }
1702 #else
1703   if (java_lang_Class::is_instance(orig_obj)) {
1704     orig_obj = scratch_java_mirror(orig_obj);
1705     assert(orig_obj != nullptr, "must be archived");
1706   }
1707 #endif
1708 
1709   if (has_been_seen_during_subgraph_recording(orig_obj)) {
1710     // orig_obj has already been archived and traced. Nothing more to do.
1711     return true;
1712   } else {
1713     set_has_been_seen_during_subgraph_recording(orig_obj);
1714   }
1715 
1716   bool already_archived = has_been_archived(orig_obj);
1717   bool record_klasses_only = already_archived;
1718   if (!already_archived) {
1719     ++_num_new_archived_objs;
1720     if (!archive_object(orig_obj)) {
1721       // Skip archiving the sub-graph referenced from the current entry field.
1722       ResourceMark rm;
1723       log_error(cds, heap)(
1724         "Cannot archive the sub-graph referenced from %s object ("
1725         PTR_FORMAT ") size " SIZE_FORMAT ", skipped.",
1726         orig_obj->klass()->external_name(), p2i(orig_obj), orig_obj->size() * HeapWordSize);
1727       if (level == 1) {
1728         // Don't archive a subgraph root that's too big. For archives static fields, that's OK
1729         // as the Java code will take care of initializing this field dynamically.
1730         return false;
1731       } else {
1732         // We don't know how to handle an object that has been archived, but some of its reachable
1733         // objects cannot be archived. Bail out for now. We might need to fix this in the future if
1734         // we have a real use case.
1735         exit_on_error();
1736       }
1737     }
1738   }
1739 
1740   Klass *orig_k = orig_obj->klass();
1741   subgraph_info->add_subgraph_object_klass(orig_k);
1742 
1743   WalkOopAndArchiveClosure walker(level, record_klasses_only, subgraph_info, orig_obj);
1744   orig_obj->oop_iterate(&walker);
1745 
1746   if (CDSEnumKlass::is_enum_obj(orig_obj)) {
1747     CDSEnumKlass::handle_enum_obj(level + 1, subgraph_info, orig_obj);
1748   }
1749   return true;
1750 }
1751 
1752 //
1753 // Start from the given static field in a java mirror and archive the
1754 // complete sub-graph of java heap objects that are reached directly
1755 // or indirectly from the starting object by following references.

1846   if (!CompressedOops::is_null(f)) {
1847     verify_subgraph_from(f);
1848   }
1849 }
1850 
1851 void HeapShared::verify_subgraph_from(oop orig_obj) {
1852   if (!has_been_archived(orig_obj)) {
1853     // It's OK for the root of a subgraph to be not archived. See comments in
1854     // archive_reachable_objects_from().
1855     return;
1856   }
1857 
1858   // Verify that all objects reachable from orig_obj are archived.
1859   init_seen_objects_table();
1860   verify_reachable_objects_from(orig_obj);
1861   delete_seen_objects_table();
1862 }
1863 
1864 void HeapShared::verify_reachable_objects_from(oop obj) {
1865   _num_total_verifications ++;
1866   if (java_lang_Class::is_instance(obj)) {
1867     obj = scratch_java_mirror(obj);
1868     assert(obj != nullptr, "must be");
1869   }
1870   if (!has_been_seen_during_subgraph_recording(obj)) {
1871     set_has_been_seen_during_subgraph_recording(obj);
1872     assert(has_been_archived(obj), "must be");
1873     VerifySharedOopClosure walker;
1874     obj->oop_iterate(&walker);
1875   }
1876 }
1877 #endif
1878 
1879 // The "default subgraph" contains special objects (see heapShared.hpp) that
1880 // can be accessed before we load any Java classes (including java/lang/Class).
1881 // Make sure that these are only instances of the very few specific types
1882 // that we can handle.
1883 void HeapShared::check_default_subgraph_classes() {
1884   GrowableArray<Klass*>* klasses = _default_subgraph_info->subgraph_object_klasses();
1885   int num = klasses->length();
1886   int warned = 0;
1887   for (int i = 0; i < num; i++) {
1888     Klass* subgraph_k = klasses->at(i);
1889     if (log_is_enabled(Info, cds, heap)) {
1890       ResourceMark rm;
1891       log_info(cds, heap)(
1892           "Archived object klass (default subgraph %d) => %s",
1893           i, subgraph_k->external_name());
1894     }
1895 
1896     if (subgraph_k->is_instance_klass()) {
1897       InstanceKlass* ik = InstanceKlass::cast(subgraph_k);
1898       Symbol* name = ArchiveBuilder::current()->get_source_addr(ik->name());
1899       if (name != vmSymbols::java_lang_Class() &&
1900           name != vmSymbols::java_lang_String() &&
1901           name != vmSymbols::java_lang_ArithmeticException() &&
1902           name != vmSymbols::java_lang_ArrayIndexOutOfBoundsException() &&
1903           name != vmSymbols::java_lang_ArrayStoreException() &&
1904           name != vmSymbols::java_lang_ClassCastException() &&
1905           name != vmSymbols::java_lang_InternalError() &&
1906           name != vmSymbols::java_lang_NullPointerException() &&
1907           !is_archivable_hidden_klass(ik)) {
1908         ResourceMark rm;
1909         const char* category = ArchiveUtils::class_category(ik);
1910         log_info(cds)("TODO: Archived unusual klass (default subgraph %2d) => %-5s %s",
1911                       ++warned, category, ik->external_name());
1912       }
1913     }
1914   }
1915 }
1916 
1917 HeapShared::SeenObjectsTable* HeapShared::_seen_objects_table = nullptr;
1918 int HeapShared::_num_new_walked_objs;
1919 int HeapShared::_num_new_archived_objs;
1920 int HeapShared::_num_old_recorded_klasses;
1921 
1922 int HeapShared::_num_total_subgraph_recordings = 0;
1923 int HeapShared::_num_total_walked_objs = 0;
1924 int HeapShared::_num_total_archived_objs = 0;
1925 int HeapShared::_num_total_recorded_klasses = 0;
1926 int HeapShared::_num_total_verifications = 0;
1927 
1928 bool HeapShared::has_been_seen_during_subgraph_recording(oop obj) {
1929   return _seen_objects_table->get(obj) != nullptr;
1930 }
1931 
1932 void HeapShared::set_has_been_seen_during_subgraph_recording(oop obj) {
1933   assert(!has_been_seen_during_subgraph_recording(obj), "sanity");

1979       }
1980     }
1981   }
1982   bool found()     { return _found;  }
1983   int offset()     { return _offset; }
1984 };
1985 
1986 void HeapShared::init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[],
1987                                             TRAPS) {
1988   for (int i = 0; fields[i].valid(); i++) {
1989     ArchivableStaticFieldInfo* info = &fields[i];
1990     TempNewSymbol klass_name =  SymbolTable::new_symbol(info->klass_name);
1991     TempNewSymbol field_name =  SymbolTable::new_symbol(info->field_name);
1992     ResourceMark rm; // for stringStream::as_string() etc.
1993 
1994 #ifndef PRODUCT
1995     bool is_test_class = (ArchiveHeapTestClass != nullptr) && (strcmp(info->klass_name, ArchiveHeapTestClass) == 0);
1996     const char* test_class_name = ArchiveHeapTestClass;
1997 #else
1998     bool is_test_class = false;
1999     const char* test_class_name = "";
2000 #endif
2001 
2002     if (is_test_class) {
2003       log_warning(cds)("Loading ArchiveHeapTestClass %s ...", test_class_name);
2004     }
2005 
2006     Klass* k = SystemDictionary::resolve_or_fail(klass_name, true, THREAD);
2007     if (HAS_PENDING_EXCEPTION) {
2008       CLEAR_PENDING_EXCEPTION;
2009       stringStream st;
2010       st.print("Fail to initialize archive heap: %s cannot be loaded by the boot loader", info->klass_name);
2011       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
2012     }
2013 
2014     if (!k->is_instance_klass()) {
2015       stringStream st;
2016       st.print("Fail to initialize archive heap: %s is not an instance class", info->klass_name);
2017       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
2018     }
2019 

2146 void HeapShared::archive_object_subgraphs(ArchivableStaticFieldInfo fields[],
2147                                           bool is_full_module_graph) {
2148   _num_total_subgraph_recordings = 0;
2149   _num_total_walked_objs = 0;
2150   _num_total_archived_objs = 0;
2151   _num_total_recorded_klasses = 0;
2152   _num_total_verifications = 0;
2153 
2154   // For each class X that has one or more archived fields:
2155   // [1] Dump the subgraph of each archived field
2156   // [2] Create a list of all the class of the objects that can be reached
2157   //     by any of these static fields.
2158   //     At runtime, these classes are initialized before X's archived fields
2159   //     are restored by HeapShared::initialize_from_archived_subgraph().
2160   int i;
2161   for (int i = 0; fields[i].valid(); ) {
2162     ArchivableStaticFieldInfo* info = &fields[i];
2163     const char* klass_name = info->klass_name;
2164     start_recording_subgraph(info->klass, klass_name, is_full_module_graph);
2165 
2166     ContextMark cm(klass_name);
2167     // If you have specified consecutive fields of the same klass in
2168     // fields[], these will be archived in the same
2169     // {start_recording_subgraph ... done_recording_subgraph} pass to
2170     // save time.
2171     for (; fields[i].valid(); i++) {
2172       ArchivableStaticFieldInfo* f = &fields[i];
2173       if (f->klass_name != klass_name) {
2174         break;
2175       }
2176 
2177       ContextMark cm(f->field_name);
2178       archive_reachable_objects_from_static_field(f->klass, f->klass_name,
2179                                                   f->offset, f->field_name);
2180     }
2181     done_recording_subgraph(info->klass, klass_name);
2182   }
2183 
2184   log_info(cds, heap)("Archived subgraph records = %d",
2185                       _num_total_subgraph_recordings);
2186   log_info(cds, heap)("  Walked %d objects", _num_total_walked_objs);
2187   log_info(cds, heap)("  Archived %d objects", _num_total_archived_objs);
2188   log_info(cds, heap)("  Recorded %d klasses", _num_total_recorded_klasses);
2189 
2190 #ifndef PRODUCT
2191   for (int i = 0; fields[i].valid(); i++) {
2192     ArchivableStaticFieldInfo* f = &fields[i];
2193     verify_subgraph_from_static_field(f->klass, f->offset);
2194   }
2195   log_info(cds, heap)("  Verified %d references", _num_total_verifications);
2196 #endif
2197 }
< prev index next >