< prev index next >

src/hotspot/share/cds/heapShared.cpp

Print this page
*** 26,11 ***
--- 26,13 ---
  #include "cds/archiveBuilder.hpp"
  #include "cds/archiveHeapLoader.hpp"
  #include "cds/archiveHeapWriter.hpp"
  #include "cds/archiveUtils.hpp"
  #include "cds/cdsConfig.hpp"
+ #include "cds/cdsEnumKlass.hpp"
  #include "cds/cdsHeapVerifier.hpp"
+ #include "cds/classPrelinker.hpp"
  #include "cds/heapShared.hpp"
  #include "cds/metaspaceShared.hpp"
  #include "classfile/classLoaderData.hpp"
  #include "classfile/javaClasses.inline.hpp"
  #include "classfile/modules.hpp"

*** 80,11 ***
--- 82,33 ---
    bool valid() {
      return klass_name != nullptr;
    }
  };
  
+ class HeapShared::ArchivingObjectMark : public StackObj {
+ public:
+   ArchivingObjectMark(oop obj) {
+     _trace->push(obj);
+   }
+   ~ArchivingObjectMark() {
+     _trace->pop();
+   }
+ };
+ 
+ class HeapShared::ContextMark : public StackObj {
+   ResourceMark rm;
+ public:
+   ContextMark(const char* c) : rm{} {
+     _context->push(c);
+   }
+   ~ContextMark() {
+     _context->pop();
+   }
+ };
+ 
  bool HeapShared::_disable_writing = false;
+ bool HeapShared::_box_classes_inited = false;
  DumpedInternedStrings *HeapShared::_dumped_interned_strings = nullptr;
  
  size_t HeapShared::_alloc_count[HeapShared::ALLOC_STAT_SLOTS];
  size_t HeapShared::_alloc_size[HeapShared::ALLOC_STAT_SLOTS];
  size_t HeapShared::_total_obj_count;

*** 114,10 ***
--- 138,14 ---
    {"jdk/internal/module/ArchivedModuleGraph",     "archivedModuleGraph"},
    {"java/util/ImmutableCollections",              "archivedObjects"},
    {"java/lang/ModuleLayer",                       "EMPTY_LAYER"},
    {"java/lang/module/Configuration",              "EMPTY_CONFIGURATION"},
    {"jdk/internal/math/FDBigInteger",              "archivedCaches"},
+   {"java/lang/invoke/DirectMethodHandle",         "archivedObjects"},
+   {"java/lang/invoke/MethodType",                 "archivedObjects"},
+   {"java/lang/invoke/LambdaForm$NamedFunction",   "archivedObjects"},
+   {"java/lang/reflect/Proxy$ProxyBuilder",        "archivedData"},
  #ifndef PRODUCT
    {nullptr, nullptr}, // Extra slot for -XX:ArchiveHeapTestClass
  #endif
    {nullptr, nullptr},
  };

*** 129,15 ***
--- 157,27 ---
    {"java/lang/Module$ArchivedData",               "archivedData"},
    {nullptr, nullptr},
  };
  
  KlassSubGraphInfo* HeapShared::_default_subgraph_info;
+ ArchivedKlassSubGraphInfoRecord* HeapShared::_runtime_default_subgraph_info;
  GrowableArrayCHeap<oop, mtClassShared>* HeapShared::_pending_roots = nullptr;
+ GrowableArrayCHeap<oop, mtClassShared>* HeapShared::_trace = nullptr;
+ GrowableArrayCHeap<const char*, mtClassShared>* HeapShared::_context = nullptr;
  OopHandle HeapShared::_roots;
  OopHandle HeapShared::_scratch_basic_type_mirrors[T_VOID+1];
+ 
+ OopHandle HeapShared::_scratch_null_ptr_exception_instance;
+ OopHandle HeapShared::_scratch_arithmetic_exception_instance;
+ OopHandle HeapShared::_scratch_virtual_machine_error_instance;
+ OopHandle HeapShared::_scratch_array_index_oob_exception_instance;
+ OopHandle HeapShared::_scratch_array_store_exception_instance;
+ OopHandle HeapShared::_scratch_class_cast_exception_instance;
+ 
  MetaspaceObjToOopHandleTable* HeapShared::_scratch_java_mirror_table = nullptr;
  MetaspaceObjToOopHandleTable* HeapShared::_scratch_references_table = nullptr;
+ int HeapShared::_permobj_segments = 0;
  
  static bool is_subgraph_root_class_of(ArchivableStaticFieldInfo fields[], InstanceKlass* ik) {
    for (int i = 0; fields[i].valid(); i++) {
      if (fields[i].klass == ik) {
        return true;

*** 223,11 ***
  
    return _pending_roots->append(obj);
  }
  
  objArrayOop HeapShared::roots() {
!   if (CDSConfig::is_dumping_heap()) {
      assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
      if (!HeapShared::can_write()) {
        return nullptr;
      }
    } else {
--- 263,11 ---
  
    return _pending_roots->append(obj);
  }
  
  objArrayOop HeapShared::roots() {
!   if (CDSConfig::is_dumping_heap() && !CDSConfig::is_dumping_final_static_archive()) {
      assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
      if (!HeapShared::can_write()) {
        return nullptr;
      }
    } else {

*** 237,10 ***
--- 277,129 ---
    objArrayOop roots = (objArrayOop)_roots.resolve();
    assert(roots != nullptr, "should have been initialized");
    return roots;
  }
  
+ static unsigned int oop_handle_hash(const OopHandle& oh) {
+   oop o = oh.resolve();
+   if (o == nullptr) {
+     return 0;
+   } else {
+     return o->identity_hash();
+   }
+ }
+ 
+ static bool oop_handle_equals(const OopHandle& a, const OopHandle& b) {
+   return a.resolve() == b.resolve();
+ }
+ 
+ class OrigToScratchObjectTable: public ResourceHashtable<OopHandle, OopHandle,
+     36137, // prime number
+     AnyObj::C_HEAP,
+     mtClassShared,
+     oop_handle_hash,
+     oop_handle_equals> {};
+ 
+ static OrigToScratchObjectTable* _orig_to_scratch_object_table = nullptr;
+ 
+ void HeapShared::track_scratch_object(oop orig_obj, oop scratch_obj) {
+   MutexLocker ml(ArchivedObjectTables_lock, Mutex::_no_safepoint_check_flag);
+   if (_orig_to_scratch_object_table == nullptr) {
+     _orig_to_scratch_object_table = new (mtClass)OrigToScratchObjectTable();
+   }
+ 
+   OopHandle orig_h(Universe::vm_global(), orig_obj);
+   OopHandle scratch_h(Universe::vm_global(), scratch_obj);
+   _orig_to_scratch_object_table->put_when_absent(orig_h, scratch_h);
+ }
+ 
+ oop HeapShared::orig_to_scratch_object(oop orig_obj) {
+   MutexLocker ml(ArchivedObjectTables_lock, Mutex::_no_safepoint_check_flag);
+   if (_orig_to_scratch_object_table != nullptr) {
+     OopHandle orig(&orig_obj);
+     OopHandle* v = _orig_to_scratch_object_table->get(orig);
+     if (v != nullptr) {
+       return v->resolve();
+     }
+   }
+   return nullptr;
+ }
+ 
+ class ArchivedObjectPermanentIndexTable: public ResourceHashtable<OopHandle, int,
+     36137, // prime number
+     AnyObj::C_HEAP,
+     mtClassShared,
+     oop_handle_hash,
+     oop_handle_equals> {};
+ 
+ static ArchivedObjectPermanentIndexTable* _permanent_index_table = nullptr;
+ 
+ void HeapShared::add_to_permanent_index_table(oop obj, int index) {
+   assert_locked_or_safepoint(ArchivedObjectTables_lock);
+ 
+   if (_permanent_index_table == nullptr) {
+     _permanent_index_table = new (mtClass)ArchivedObjectPermanentIndexTable();
+   }
+   OopHandle oh(Universe::vm_global(), obj);
+   _permanent_index_table->put(oh, index);
+ }
+ 
+ int HeapShared::get_archived_object_permanent_index(oop obj) {
+   if (!UsePermanentHeapObjects) {
+     return -1;
+   }
+   if (!CDSConfig::is_dumping_heap() && _permobj_segments <= 0) {
+     return -1;
+   }
+ 
+   MutexLocker ml(ArchivedObjectTables_lock, Mutex::_no_safepoint_check_flag);
+ 
+   if (!CDSConfig::is_dumping_heap() && _permanent_index_table == nullptr) {
+     int first_permobj_segment = roots()->length() - _permobj_segments;
+     for (int i = 0; i < _permobj_segments; i++) {
+       objArrayOop a = (objArrayOop)roots()->obj_at(i + first_permobj_segment);
+       for (int j = 0; j < a->length(); j++) {
+         int index = (i << ArchiveHeapWriter::PERMOBJ_SEGMENT_MAX_SHIFT) + j;
+         add_to_permanent_index_table(a->obj_at(j), index);
+       }
+     }
+   }
+ 
+   if (_permanent_index_table != nullptr) {
+     if (_orig_to_scratch_object_table != nullptr) {
+       OopHandle orig(&obj);
+       OopHandle* v = _orig_to_scratch_object_table->get(orig);
+       if (v != nullptr) {
+         obj = v->resolve();
+       }
+     }
+     OopHandle tmp(&obj);
+     int* v = _permanent_index_table->get(tmp);
+     if (v != nullptr) {
+       int n = *v;
+       return n;
+     }
+   }
+ 
+   return -1;
+ }
+ 
+ oop HeapShared::get_archived_object(int permanent_index) {
+   if (ArchiveHeapLoader::is_in_use()) {
+     assert(_permobj_segments > 0, "must be");
+ 
+     int first_permobj_segment = roots()->length() - _permobj_segments;
+     int upper = permanent_index >> ArchiveHeapWriter::PERMOBJ_SEGMENT_MAX_SHIFT;
+     int lower = permanent_index &  ArchiveHeapWriter::PERMOBJ_SEGMENT_MAX_MASK;
+     objArrayOop a = (objArrayOop)roots()->obj_at(upper + first_permobj_segment);
+     return a->obj_at(lower);
+   } else {
+     assert(CDSConfig::is_dumping_heap(), "must be");
+     return ArchiveHeapWriter::get_perm_object_by_index(permanent_index);
+   }
+ }
+ 
  // Returns an objArray that contains all the roots of the archived objects
  oop HeapShared::get_root(int index, bool clear) {
    assert(index >= 0, "sanity");
    assert(!CDSConfig::is_dumping_heap() && UseSharedSpaces, "runtime only");
    assert(!_roots.is_empty(), "must have loaded shared heap");

*** 331,10 ***
--- 490,13 ---
      }
    }
  };
  
  void HeapShared::add_scratch_resolved_references(ConstantPool* src, objArrayOop dest) {
+   if (_scratch_references_table == nullptr) {
+     _scratch_references_table = new (mtClass)MetaspaceObjToOopHandleTable();
+   }
    _scratch_references_table->set_oop(src, dest);
  }
  
  objArrayOop HeapShared::scratch_resolved_references(ConstantPool* src) {
    return (objArrayOop)_scratch_references_table->get_oop(src);

*** 344,14 ***
    for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
      BasicType bt = (BasicType)i;
      if (!is_reference_type(bt)) {
        oop m = java_lang_Class::create_basic_type_mirror(type2name(bt), bt, CHECK);
        _scratch_basic_type_mirrors[i] = OopHandle(Universe::vm_global(), m);
      }
    }
    _scratch_java_mirror_table = new (mtClass)MetaspaceObjToOopHandleTable();
!   _scratch_references_table = new (mtClass)MetaspaceObjToOopHandleTable();
  }
  
  oop HeapShared::scratch_java_mirror(BasicType t) {
    assert((uint)t < T_VOID+1, "range check");
    assert(!is_reference_type(t), "sanity");
--- 506,57 ---
    for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
      BasicType bt = (BasicType)i;
      if (!is_reference_type(bt)) {
        oop m = java_lang_Class::create_basic_type_mirror(type2name(bt), bt, CHECK);
        _scratch_basic_type_mirrors[i] = OopHandle(Universe::vm_global(), m);
+       track_scratch_object(Universe::java_mirror(bt), m);
      }
    }
    _scratch_java_mirror_table = new (mtClass)MetaspaceObjToOopHandleTable();
!   if (_scratch_references_table == nullptr) {
+     _scratch_references_table = new (mtClass)MetaspaceObjToOopHandleTable();
+   }
+ }
+ 
+ OopHandle HeapShared::init_scratch_exception(oop orig_exception_obj, TRAPS) {
+   Symbol* klass_name = orig_exception_obj->klass()->name();
+   oop scratch_obj = java_lang_Throwable::create_exception_instance(klass_name, CHECK_(OopHandle()));
+   track_scratch_object(orig_exception_obj, scratch_obj);
+   return OopHandle(Universe::vm_global(), scratch_obj);
+ }
+ 
+ void HeapShared::init_scratch_exceptions(TRAPS) {
+   _scratch_null_ptr_exception_instance        = init_scratch_exception(Universe::null_ptr_exception_instance(),        CHECK);
+   _scratch_arithmetic_exception_instance      = init_scratch_exception(Universe::arithmetic_exception_instance(),      CHECK);
+   _scratch_virtual_machine_error_instance     = init_scratch_exception(Universe::virtual_machine_error_instance(),     CHECK);
+   _scratch_array_index_oob_exception_instance = init_scratch_exception(Universe::array_index_oob_exception_instance(), CHECK);
+   _scratch_array_store_exception_instance     = init_scratch_exception(Universe::array_store_exception_instance(),     CHECK);
+   _scratch_class_cast_exception_instance      = init_scratch_exception(Universe::class_cast_exception_instance(),      CHECK);
+ }
+ // Given java_mirror that represents a (primitive or reference) type T,
+ // return the "scratch" version that represents the same type T.
+ // Note that if java_mirror will be returned if it's already a
+ // scratch mirror.
+ //
+ // See java_lang_Class::create_scratch_mirror() for more info.
+ oop HeapShared::scratch_java_mirror(oop java_mirror) {
+   assert(java_lang_Class::is_instance(java_mirror), "must be");
+ 
+   for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
+     BasicType bt = (BasicType)i;
+     if (!is_reference_type(bt)) {
+       if (_scratch_basic_type_mirrors[i].resolve() == java_mirror) {
+         return java_mirror;
+       }
+     }
+   }
+ 
+   if (java_lang_Class::is_primitive(java_mirror)) {
+     return scratch_java_mirror(java_lang_Class::as_BasicType(java_mirror));
+   } else {
+     return scratch_java_mirror(java_lang_Class::as_Klass(java_mirror));
+   }
  }
  
  oop HeapShared::scratch_java_mirror(BasicType t) {
    assert((uint)t < T_VOID+1, "range check");
    assert(!is_reference_type(t), "sanity");

*** 361,27 ***
  oop HeapShared::scratch_java_mirror(Klass* k) {
    return _scratch_java_mirror_table->get_oop(k);
  }
  
  void HeapShared::set_scratch_java_mirror(Klass* k, oop mirror) {
    _scratch_java_mirror_table->set_oop(k, mirror);
  }
  
  void HeapShared::remove_scratch_objects(Klass* k) {
    _scratch_java_mirror_table->remove_oop(k);
    if (k->is_instance_klass()) {
      _scratch_references_table->remove(InstanceKlass::cast(k)->constants());
    }
  }
  
  void HeapShared::archive_java_mirrors() {
    for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
      BasicType bt = (BasicType)i;
      if (!is_reference_type(bt)) {
        oop m = _scratch_basic_type_mirrors[i].resolve();
        assert(m != nullptr, "sanity");
!       bool success = archive_reachable_objects_from(1, _default_subgraph_info, m);
        assert(success, "sanity");
  
        log_trace(cds, heap, mirror)(
          "Archived %s mirror object from " PTR_FORMAT,
          type2name(bt), p2i(m));
--- 566,123 ---
  oop HeapShared::scratch_java_mirror(Klass* k) {
    return _scratch_java_mirror_table->get_oop(k);
  }
  
  void HeapShared::set_scratch_java_mirror(Klass* k, oop mirror) {
+   track_scratch_object(k->java_mirror(), mirror);
    _scratch_java_mirror_table->set_oop(k, mirror);
  }
  
  void HeapShared::remove_scratch_objects(Klass* k) {
    _scratch_java_mirror_table->remove_oop(k);
    if (k->is_instance_klass()) {
      _scratch_references_table->remove(InstanceKlass::cast(k)->constants());
    }
  }
  
+ bool HeapShared::is_lambda_form_klass(InstanceKlass* ik) {
+   return ik->is_hidden() &&
+     (ik->name()->starts_with("java/lang/invoke/LambdaForm$MH+") ||
+      ik->name()->starts_with("java/lang/invoke/LambdaForm$DMH+") ||
+      ik->name()->starts_with("java/lang/invoke/LambdaForm$BMH+"));
+      
+ }
+ 
+ bool HeapShared::is_lambda_proxy_klass(InstanceKlass* ik) {
+   return ik->is_hidden() && (ik->name()->index_of_at(0, "$$Lambda+", 9) > 0);
+ }
+ 
+ bool HeapShared::is_archivable_hidden_klass(InstanceKlass* ik) {
+   return is_lambda_form_klass(ik) || is_lambda_proxy_klass(ik);
+ }
+ 
+ void HeapShared::copy_preinitialized_mirror(Klass* orig_k, oop orig_mirror, oop m) {
+   if (!orig_k->is_instance_klass()) {
+     return;
+   }
+   InstanceKlass* ik = InstanceKlass::cast(orig_k);
+ 
+   if (HeapShared::is_archivable_hidden_klass(ik)) {
+     // We can't rerun the <clinit> method of hidden classes as we don't save
+     // the classData, so we must archive its mirror in initialized state.
+     assert(ik->is_initialized(), "must be");
+   }
+ 
+   if (!ik->is_initialized() || !ClassPrelinker::can_archive_preinitialized_mirror(ik)) {
+     return;
+   }
+ 
+   int nfields = 0;
+   for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
+     if (fs.access_flags().is_static()) {
+       fieldDescriptor& fd = fs.field_descriptor();
+       int offset = fd.offset();
+       switch (fd.field_type()) {
+       case T_OBJECT:
+       case T_ARRAY:
+         m->obj_field_put(offset, orig_mirror->obj_field(offset));
+         break;
+       case T_BOOLEAN:
+         m->bool_field_put(offset, orig_mirror->bool_field(offset));
+         break;
+       case T_BYTE:
+         m->byte_field_put(offset, orig_mirror->byte_field(offset));
+         break;
+       case T_SHORT:
+         m->short_field_put(offset, orig_mirror->short_field(offset));
+         break;
+       case T_CHAR:
+         m->char_field_put(offset, orig_mirror->char_field(offset));
+         break;
+       case T_INT:
+         m->int_field_put(offset, orig_mirror->int_field(offset));
+         break;
+       case T_LONG:
+         m->long_field_put(offset, orig_mirror->long_field(offset));
+         break;
+       case T_FLOAT:
+         m->float_field_put(offset, orig_mirror->float_field(offset));
+         break;
+       case T_DOUBLE:
+         m->double_field_put(offset, orig_mirror->double_field(offset));
+         break;
+       default:
+         ShouldNotReachHere();
+       }
+       nfields ++;
+     }
+   }
+   if (log_is_enabled(Info, cds, init)) {
+     ResourceMark rm;
+     log_debug(cds, init)("copied %3d field(s) in preinitialized mirror %s%s", nfields, ik->external_name(),
+                          ik->is_hidden() ? " (hidden)" : "");
+   }
+ 
+   InstanceKlass* buffered_ik = ArchiveBuilder::current()->get_buffered_addr(ik);
+   buffered_ik->set_has_preinitialized_mirror();
+ }
+ 
+ static void copy_java_mirror_hashcode(oop orig_mirror, oop scratch_m) {
+   int src_hash = orig_mirror->identity_hash();
+   scratch_m->set_mark(markWord::prototype().copy_set_hash(src_hash));
+   assert(scratch_m->mark().is_unlocked(), "sanity");
+ 
+   DEBUG_ONLY(int archived_hash = scratch_m->identity_hash());
+   assert(src_hash == archived_hash, "Java mirror wrong hash: original %x, scratch %x", src_hash, archived_hash);
+ }
+ 
  void HeapShared::archive_java_mirrors() {
+   SystemDictionaryShared::reset_preinit_check();
+ 
    for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
      BasicType bt = (BasicType)i;
      if (!is_reference_type(bt)) {
+       oop orig_mirror = Universe::java_mirror(bt);
        oop m = _scratch_basic_type_mirrors[i].resolve();
        assert(m != nullptr, "sanity");
!       copy_java_mirror_hashcode(orig_mirror, m);
+       bool success = archive_reachable_objects_from(1, _default_subgraph_info, orig_mirror);
        assert(success, "sanity");
  
        log_trace(cds, heap, mirror)(
          "Archived %s mirror object from " PTR_FORMAT,
          type2name(bt), p2i(m));

*** 392,14 ***
  
    GrowableArray<Klass*>* klasses = ArchiveBuilder::current()->klasses();
    assert(klasses != nullptr, "sanity");
    for (int i = 0; i < klasses->length(); i++) {
      Klass* orig_k = klasses->at(i);
      oop m = scratch_java_mirror(orig_k);
      if (m != nullptr) {
        Klass* buffered_k = ArchiveBuilder::get_buffered_klass(orig_k);
!       bool success = archive_reachable_objects_from(1, _default_subgraph_info, m);
        guarantee(success, "scratch mirrors must point to only archivable objects");
        buffered_k->set_archived_java_mirror(append_root(m));
        ResourceMark rm;
        log_trace(cds, heap, mirror)(
          "Archived %s mirror object from " PTR_FORMAT,
--- 693,23 ---
  
    GrowableArray<Klass*>* klasses = ArchiveBuilder::current()->klasses();
    assert(klasses != nullptr, "sanity");
    for (int i = 0; i < klasses->length(); i++) {
      Klass* orig_k = klasses->at(i);
+     oop orig_mirror = orig_k->java_mirror();
      oop m = scratch_java_mirror(orig_k);
      if (m != nullptr) {
+       copy_java_mirror_hashcode(orig_mirror, m);
+       copy_preinitialized_mirror(orig_k, orig_mirror, m);
+       if (ArchiveReflectionData && java_lang_Class::has_reflection_data(orig_mirror)) {
+         oop reflection_data = java_lang_Class::reflection_data(orig_mirror);
+         bool success = archive_reachable_objects_from(1, _default_subgraph_info, reflection_data);
+         guarantee(success, "");
+         java_lang_Class::set_reflection_data(m, reflection_data);
+       }
        Klass* buffered_k = ArchiveBuilder::get_buffered_klass(orig_k);
!       bool success = archive_reachable_objects_from(1, _default_subgraph_info, orig_mirror);
        guarantee(success, "scratch mirrors must point to only archivable objects");
        buffered_k->set_archived_java_mirror(append_root(m));
        ResourceMark rm;
        log_trace(cds, heap, mirror)(
          "Archived %s mirror object from " PTR_FORMAT,

*** 428,111 ***
    // - StringTable::init_shared_table() doesn't create any large arrays.
    assert(success, "shared strings array must not point to arrays or strings that are too large to archive");
    StringTable::set_shared_strings_array_index(append_root(shared_strings_array));
  }
  
! void HeapShared::mark_native_pointers(oop orig_obj) {
!   if (java_lang_Class::is_instance(orig_obj)) {
!     ArchiveHeapWriter::mark_native_pointer(orig_obj, java_lang_Class::klass_offset());
!     ArchiveHeapWriter::mark_native_pointer(orig_obj, java_lang_Class::array_klass_offset());
    }
! }
! 
! // -- Handling of Enum objects
! // Java Enum classes have synthetic <clinit> methods that look like this
! //     enum MyEnum {FOO, BAR}
- //     MyEnum::<clinint> {
- //        /*static final MyEnum*/ MyEnum::FOO = new MyEnum("FOO");
- //        /*static final MyEnum*/ MyEnum::BAR = new MyEnum("BAR");
- //     }
- //
- // If MyEnum::FOO object is referenced by any of the archived subgraphs, we must
- // ensure the archived value equals (in object address) to the runtime value of
- // MyEnum::FOO.
- //
- // However, since MyEnum::<clinint> is synthetically generated by javac, there's
- // no way of programmatically handling this inside the Java code (as you would handle
- // ModuleLayer::EMPTY_LAYER, for example).
- //
- // Instead, we archive all static field of such Enum classes. At runtime,
- // HeapShared::initialize_enum_klass() will skip the <clinit> method and pull
- // the static fields out of the archived heap.
- void HeapShared::check_enum_obj(int level,
-                                 KlassSubGraphInfo* subgraph_info,
-                                 oop orig_obj) {
-   assert(level > 1, "must never be called at the first (outermost) level");
-   Klass* k = orig_obj->klass();
-   Klass* buffered_k = ArchiveBuilder::get_buffered_klass(k);
-   if (!k->is_instance_klass()) {
-     return;
    }
!   InstanceKlass* ik = InstanceKlass::cast(k);
!   if (ik->java_super() == vmClasses::Enum_klass() && !ik->has_archived_enum_objs()) {
!     ResourceMark rm;
!     ik->set_has_archived_enum_objs();
!     buffered_k->set_has_archived_enum_objs();
-     oop mirror = ik->java_mirror();
- 
-     for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
-       if (fs.access_flags().is_static()) {
-         fieldDescriptor& fd = fs.field_descriptor();
-         if (fd.field_type() != T_OBJECT && fd.field_type() != T_ARRAY) {
-           guarantee(false, "static field %s::%s must be T_OBJECT or T_ARRAY",
-                     ik->external_name(), fd.name()->as_C_string());
-         }
-         oop oop_field = mirror->obj_field(fd.offset());
-         if (oop_field == nullptr) {
-           guarantee(false, "static field %s::%s must not be null",
-                     ik->external_name(), fd.name()->as_C_string());
-         } else if (oop_field->klass() != ik && oop_field->klass() != ik->array_klass_or_null()) {
-           guarantee(false, "static field %s::%s is of the wrong type",
-                     ik->external_name(), fd.name()->as_C_string());
-         }
-         bool success = archive_reachable_objects_from(level, subgraph_info, oop_field);
-         assert(success, "VM should have exited with unarchivable objects for _level > 1");
-         int root_index = append_root(oop_field);
-         log_info(cds, heap)("Archived enum obj @%d %s::%s (" INTPTR_FORMAT ")",
-                             root_index, ik->external_name(), fd.name()->as_C_string(),
-                             p2i((oopDesc*)oop_field));
-         SystemDictionaryShared::add_enum_klass_static_field(ik, root_index);
-       }
-     }
    }
! }
! 
! // See comments in HeapShared::check_enum_obj()
! bool HeapShared::initialize_enum_klass(InstanceKlass* k, TRAPS) {
!   if (!ArchiveHeapLoader::is_in_use()) {
-     return false;
    }
! 
!   RunTimeClassInfo* info = RunTimeClassInfo::get_for(k);
!   assert(info != nullptr, "sanity");
! 
!   if (log_is_enabled(Info, cds, heap)) {
-     ResourceMark rm;
-     log_info(cds, heap)("Initializing Enum class: %s", k->external_name());
    }
  
!   oop mirror = k->java_mirror();
!   int i = 0;
!   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
!     if (fs.access_flags().is_static()) {
!       int root_index = info->enum_klass_static_field_root_index_at(i++);
!       fieldDescriptor& fd = fs.field_descriptor();
-       assert(fd.field_type() == T_OBJECT || fd.field_type() == T_ARRAY, "must be");
-       mirror->obj_field_put(fd.offset(), get_root(root_index, /*clear=*/true));
-     }
    }
-   return true;
  }
  
  void HeapShared::archive_objects(ArchiveHeapInfo *heap_info) {
    {
      NoSafepointVerifier nsv;
  
      _default_subgraph_info = init_subgraph_info(vmClasses::Object_klass(), false);
  
      // Cache for recording where the archived objects are copied to
      create_archived_object_cache();
  
      log_info(cds)("Heap range = [" PTR_FORMAT " - "  PTR_FORMAT "]",
--- 738,65 ---
    // - StringTable::init_shared_table() doesn't create any large arrays.
    assert(success, "shared strings array must not point to arrays or strings that are too large to archive");
    StringTable::set_shared_strings_array_index(append_root(shared_strings_array));
  }
  
! void HeapShared::archive_exception_instances() {
!   {
!     oop m = _scratch_null_ptr_exception_instance.resolve();
!     bool success = archive_reachable_objects_from(1, _default_subgraph_info, m /*Universe::null_ptr_exception_instance()*/);
+     assert(success, "sanity");
+     Universe::set_archived_null_ptr_exception_instance_index(append_root(m));
    }
!   {
!     oop m = _scratch_arithmetic_exception_instance.resolve();
!     bool success = archive_reachable_objects_from(1, _default_subgraph_info, m /*Universe::arithmetic_exception_instance()*/);
!     assert(success, "sanity");
!     Universe::set_archived_arithmetic_exception_instance_index(append_root(m));
    }
!   {
!     oop m = _scratch_virtual_machine_error_instance.resolve();
!     bool success = archive_reachable_objects_from(1, _default_subgraph_info, m /*Universe::virtual_machine_error_instance()*/);
!     assert(success, "sanity");
!     Universe::set_archived_virtual_machine_error_instance_index(append_root(m));
    }
!   {
!     oop m = _scratch_array_index_oob_exception_instance.resolve();
!     bool success = archive_reachable_objects_from(1, _default_subgraph_info, m /*Universe::array_index_oob_exception_instance()*/);
!     assert(success, "sanity");
!     Universe::set_archived_array_index_oob_exception_instance_index(append_root(m));
    }
!   {
!     oop m = _scratch_array_store_exception_instance.resolve();
!     bool success = archive_reachable_objects_from(1, _default_subgraph_info, m /*Universe::array_store_exception_instance()*/);
!     assert(success, "sanity");
!     Universe::set_archived_array_store_exception_instance_index(append_root(m));
    }
+   {
+     oop m = _scratch_class_cast_exception_instance.resolve();
+     bool success = archive_reachable_objects_from(1, _default_subgraph_info, m /*Universe::class_cast_exception_instance()*/);
+     assert(success, "sanity");
+     Universe::set_archived_class_cast_exception_instance_index(append_root(m));
+   }
+ }
  
! void HeapShared::mark_native_pointers(oop orig_obj) {
!   if (java_lang_Class::is_instance(orig_obj)) {
!     ArchiveHeapWriter::mark_native_pointer(orig_obj, java_lang_Class::klass_offset());
!     ArchiveHeapWriter::mark_native_pointer(orig_obj, java_lang_Class::array_klass_offset());
!   } else if (java_lang_invoke_ResolvedMethodName::is_instance(orig_obj)) {
!     ArchiveHeapWriter::mark_native_pointer(orig_obj, java_lang_invoke_ResolvedMethodName::vmtarget_offset());
    }
  }
  
  void HeapShared::archive_objects(ArchiveHeapInfo *heap_info) {
    {
      NoSafepointVerifier nsv;
  
      _default_subgraph_info = init_subgraph_info(vmClasses::Object_klass(), false);
+     _trace = new GrowableArrayCHeap<oop, mtClassShared>(250);
+     _context = new GrowableArrayCHeap<const char*, mtClassShared>(250);
  
      // Cache for recording where the archived objects are copied to
      create_archived_object_cache();
  
      log_info(cds)("Heap range = [" PTR_FORMAT " - "  PTR_FORMAT "]",

*** 569,10 ***
--- 833,11 ---
  void HeapShared::copy_special_objects() {
    // Archive special objects that do not belong to any subgraphs
    init_seen_objects_table();
    archive_java_mirrors();
    archive_strings();
+   archive_exception_instances();
    delete_seen_objects_table();
  }
  
  void HeapShared::copy_objects() {
    assert(HeapShared::can_write(), "must be");

*** 646,12 ***
      // initialization list.
      return;
    }
  
    if (buffered_k->is_instance_klass()) {
!     assert(InstanceKlass::cast(buffered_k)->is_shared_boot_class(),
!           "must be boot class");
      // vmClasses::xxx_klass() are not updated, need to check
      // the original Klass*
      if (orig_k == vmClasses::String_klass() ||
          orig_k == vmClasses::Object_klass()) {
        // Initialized early during VM initialization. No need to be added
--- 911,15 ---
      // initialization list.
      return;
    }
  
    if (buffered_k->is_instance_klass()) {
!     if (!ArchiveInvokeDynamic) {
!       // FIXME: this supports Lambda Proxy classes
+       assert(InstanceKlass::cast(buffered_k)->is_shared_boot_class(),
+              "must be boot class");
+     }
      // vmClasses::xxx_klass() are not updated, need to check
      // the original Klass*
      if (orig_k == vmClasses::String_klass() ||
          orig_k == vmClasses::Object_klass()) {
        // Initialized early during VM initialization. No need to be added

*** 687,10 ***
--- 955,14 ---
    _subgraph_object_klasses->append_if_missing(buffered_k);
    _has_non_early_klasses |= is_non_early_klass(orig_k);
  }
  
  void KlassSubGraphInfo::check_allowed_klass(InstanceKlass* ik) {
+   if (ArchiveInvokeDynamic) {
+     // FIXME -- this allows LambdaProxy classes
+     return;
+   }
    if (ik->module()->name() == vmSymbols::java_base()) {
      assert(ik->package() != nullptr, "classes in java.base cannot be in unnamed package");
      return;
    }
  

*** 784,29 ***
    ArchivePtrMarker::mark_pointer(&_k);
    ArchivePtrMarker::mark_pointer(&_entry_field_records);
    ArchivePtrMarker::mark_pointer(&_subgraph_object_klasses);
  }
  
! struct CopyKlassSubGraphInfoToArchive : StackObj {
    CompactHashtableWriter* _writer;
    CopyKlassSubGraphInfoToArchive(CompactHashtableWriter* writer) : _writer(writer) {}
  
    bool do_entry(Klass* klass, KlassSubGraphInfo& info) {
      if (info.subgraph_object_klasses() != nullptr || info.subgraph_entry_fields() != nullptr) {
!       ArchivedKlassSubGraphInfoRecord* record =
-         (ArchivedKlassSubGraphInfoRecord*)ArchiveBuilder::ro_region_alloc(sizeof(ArchivedKlassSubGraphInfoRecord));
-       record->init(&info);
- 
        Klass* buffered_k = ArchiveBuilder::get_buffered_klass(klass);
        unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary((address)buffered_k);
        u4 delta = ArchiveBuilder::current()->any_to_offset_u4(record);
        _writer->add(hash, delta);
      }
      return true; // keep on iterating
    }
  };
  
  // Build the records of archived subgraph infos, which include:
  // - Entry points to all subgraphs from the containing class mirror. The entry
  //   points are static fields in the mirror. For each entry point, the field
  //   offset, and value are recorded in the sub-graph
  //   info. The value is stored back to the corresponding field at runtime.
--- 1056,34 ---
    ArchivePtrMarker::mark_pointer(&_k);
    ArchivePtrMarker::mark_pointer(&_entry_field_records);
    ArchivePtrMarker::mark_pointer(&_subgraph_object_klasses);
  }
  
! class HeapShared::CopyKlassSubGraphInfoToArchive : StackObj {
+ public:
    CompactHashtableWriter* _writer;
    CopyKlassSubGraphInfoToArchive(CompactHashtableWriter* writer) : _writer(writer) {}
  
    bool do_entry(Klass* klass, KlassSubGraphInfo& info) {
      if (info.subgraph_object_klasses() != nullptr || info.subgraph_entry_fields() != nullptr) {
!       ArchivedKlassSubGraphInfoRecord* record = HeapShared::archive_subgraph_info(&info);
        Klass* buffered_k = ArchiveBuilder::get_buffered_klass(klass);
        unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary((address)buffered_k);
        u4 delta = ArchiveBuilder::current()->any_to_offset_u4(record);
        _writer->add(hash, delta);
      }
      return true; // keep on iterating
    }
  };
  
+ ArchivedKlassSubGraphInfoRecord* HeapShared::archive_subgraph_info(KlassSubGraphInfo* info) {
+   ArchivedKlassSubGraphInfoRecord* record =
+       (ArchivedKlassSubGraphInfoRecord*)ArchiveBuilder::ro_region_alloc(sizeof(ArchivedKlassSubGraphInfoRecord));
+   record->init(info);
+   return record;
+ }
+ 
  // Build the records of archived subgraph infos, which include:
  // - Entry points to all subgraphs from the containing class mirror. The entry
  //   points are static fields in the mirror. For each entry point, the field
  //   offset, and value are recorded in the sub-graph
  //   info. The value is stored back to the corresponding field at runtime.

*** 822,10 ***
--- 1099,12 ---
    CompactHashtableWriter writer(d_table->_count, &stats);
    CopyKlassSubGraphInfoToArchive copy(&writer);
    d_table->iterate(&copy);
    writer.dump(&_run_time_subgraph_info_table, "subgraphs");
  
+   _runtime_default_subgraph_info = archive_subgraph_info(_default_subgraph_info);
+ 
  #ifndef PRODUCT
    if (ArchiveHeapTestClass != nullptr) {
      size_t len = strlen(ArchiveHeapTestClass) + 1;
      Array<char>* array = ArchiveBuilder::new_ro_array<char>((int)len);
      strncpy(array->adr_at(0), ArchiveHeapTestClass, len);

*** 835,10 ***
--- 1114,14 ---
    if (log_is_enabled(Info, cds, heap)) {
      print_stats();
    }
  }
  
+ void HeapShared::serialize_misc_info(SerializeClosure* soc) {
+   soc->do_int(&_permobj_segments);
+ }
+ 
  void HeapShared::init_roots(oop roots_oop) {
    if (roots_oop != nullptr) {
      assert(ArchiveHeapLoader::is_in_use(), "must be");
      _roots = OopHandle(Universe::vm_global(), roots_oop);
    }

*** 853,10 ***
--- 1136,12 ---
      setup_test_class(_test_class_name);
    }
  #endif
  
    _run_time_subgraph_info_table.serialize_header(soc);
+   soc->do_ptr(&_runtime_default_subgraph_info);
+ 
  }
  
  static void verify_the_heap(Klass* k, const char* which) {
    if (VerifyArchivedFields > 0) {
      ResourceMark rm;

*** 920,16 ***
--- 1205,62 ---
    if (record == nullptr) {
     clear_archived_roots_of(k);
    }
  }
  
+ void HeapShared::initialize_java_lang_invoke(TRAPS) {
+   if (CDSConfig::is_loading_invokedynamic() || ArchiveInvokeDynamic) {
+     resolve_or_init("java/lang/invoke/Invokers$Holder", true, CHECK);
+     resolve_or_init("java/lang/invoke/MethodHandle", true, CHECK);
+     resolve_or_init("java/lang/invoke/MethodHandleNatives", true, CHECK);
+     resolve_or_init("java/lang/invoke/DirectMethodHandle$Holder", true, CHECK);
+     resolve_or_init("java/lang/invoke/DelegatingMethodHandle$Holder", true, CHECK);
+     resolve_or_init("java/lang/invoke/LambdaForm$Holder", true, CHECK);
+     resolve_or_init("java/lang/invoke/BoundMethodHandle$Species_L", true, CHECK);
+   }
+ }
+ 
+ void HeapShared::initialize_default_subgraph_classes(Handle loader, TRAPS) {
+   if (!ArchiveHeapLoader::is_in_use()) {
+     return;
+   }
+ 
+   assert( _runtime_default_subgraph_info != nullptr, "must be");
+   Array<Klass*>* klasses = _runtime_default_subgraph_info->subgraph_object_klasses();
+   if (klasses != nullptr) {
+     for (int pass = 0; pass < 2; pass ++) {
+       for (int i = 0; i < klasses->length(); i++) {
+         Klass* k = klasses->at(i);
+         if (k->class_loader_data() == nullptr) {
+           // This class is not yet loaded. We will initialize it in a later phase.
+           continue;
+         }
+         if (k->class_loader() == loader()) {
+           if (pass == 0) {
+             if (k->is_instance_klass()) {
+               InstanceKlass::cast(k)->link_class(CHECK);
+             }
+           } else {
+             resolve_or_init(k, /*do_init*/true, CHECK);
+           }
+         }
+       }
+     }
+   }
+ }
+ 
  void HeapShared::initialize_from_archived_subgraph(JavaThread* current, Klass* k) {
    JavaThread* THREAD = current;
    if (!ArchiveHeapLoader::is_in_use()) {
      return; // nothing to do
    }
  
+   // The subgraphs may reference java_mirrors of the box classes like
+   // java/lang/Boolean. It may not be necessary, but for sanity, we force
+   // the box classes to be initialized before any subgraph can be initialized.
+   assert(_box_classes_inited, "must be");
+ 
    ExceptionMark em(THREAD);
    const ArchivedKlassSubGraphInfoRecord* record =
      resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/true, THREAD);
  
    if (HAS_PENDING_EXCEPTION) {

*** 993,10 ***
--- 1324,62 ---
      if (log_is_enabled(Info, cds, heap)) {
        ResourceMark rm;
        log_info(cds, heap)("%s subgraph %s ", do_init ? "init" : "resolve", k->external_name());
      }
  
+     if (do_init && k->name() == vmSymbols::java_lang_invoke_MethodType()) {
+       // FIXME - hack.
+       //
+       // (The real fix would be to archive the MethodType class in its already initialized state. That
+       //  way we don't need to re-execute the <clinit> methods)
+       //
+       // We need to do this to break a cycle in the way the archived subgraphs are restored. Without this block, we
+       // have the following sequence
+       //
+       // MethodType.<clinit>()
+       //  -> CDS.initializeFromArchive(MethodType.class);
+       //   -> (this "if" block)
+       //   -> resolve_or_init("MethodType", ...); // this does nothing because MethodType.<clinit> is already executing
+       //   -> resolve_or_init("DirectMethodHandle", ...); // this class is in record->subgraph_object_klasses();
+       //      -> DirectMethodHandle.<clinit>()
+       //          -> MethodType.methodType()
+       //             -> MethodType.genericMethodType()
+       //               -> aaload MethodType.objectOnlyTypes[n]; <<<< here
+       //
+       // We need to restore MethodType.objectOnlyTypes here, or else the above aaload will
+       // get an NPE.
+       Array<int>* entry_field_records = record->entry_field_records();
+       assert(entry_field_records != nullptr, "must be");
+       int efr_len = entry_field_records->length();
+       assert(efr_len == 2, "must be");
+       int root_index = entry_field_records->at(1);
+       oop obj = get_root(root_index, /*clear=*/false);
+       if (obj != nullptr) {
+         objArrayOop archivedObjects = objArrayOop(obj);
+         InstanceKlass* ik = InstanceKlass::cast(k);
+         oop m = ik->java_mirror();
+ 
+         {
+           fieldDescriptor fd;
+           TempNewSymbol name = SymbolTable::new_symbol("archivedMethodTypes");
+           TempNewSymbol sig  = SymbolTable::new_symbol("Ljava/util/HashMap;");
+           Klass* result = ik->find_field(name, sig, true, &fd);
+           assert(result != nullptr, "must be");
+           m->obj_field_put(fd.offset(), archivedObjects->obj_at(0));
+         }
+ 
+         {
+           fieldDescriptor fd;
+           TempNewSymbol name = SymbolTable::new_symbol("objectOnlyTypes");
+           TempNewSymbol sig  = SymbolTable::new_symbol("[Ljava/lang/invoke/MethodType;");
+           Klass* result = ik->find_field(name, sig, true, &fd);
+           assert(result != nullptr, "must be");
+           m->obj_field_put(fd.offset(), archivedObjects->obj_at(1));
+         }
+       }
+     }
+ 
      resolve_or_init(k, do_init, CHECK_NULL);
  
      // Load/link/initialize the klasses of the objects in the subgraph.
      // nullptr class loader is used.
      Array<Klass*>* klasses = record->subgraph_object_klasses();

*** 1012,10 ***
--- 1395,23 ---
    }
  
    return record;
  }
  
+ void HeapShared::resolve_or_init(const char* klass_name, bool do_init, TRAPS) {
+   TempNewSymbol klass_name_sym =  SymbolTable::new_symbol(klass_name);
+   InstanceKlass* k = SystemDictionaryShared::find_builtin_class(klass_name_sym);
+   if (k == nullptr) {
+     return;
+   }
+   assert(k->is_shared_boot_class(), "sanity");
+   resolve_or_init(k, false, CHECK);
+   if (do_init) {
+     resolve_or_init(k, true, CHECK);
+   }
+ }
+ 
  void HeapShared::resolve_or_init(Klass* k, bool do_init, TRAPS) {
    if (!do_init) {
      if (k->class_loader_data() == nullptr) {
        Klass* resolved_k = SystemDictionary::resolve_or_null(k->name(), CHECK);
        assert(resolved_k == k, "classes used by archived heap must not be replaced by JVMTI ClassFileLoadHook");

*** 1081,11 ***
  class WalkOopAndArchiveClosure: public BasicOopIterateClosure {
    int _level;
    bool _record_klasses_only;
    KlassSubGraphInfo* _subgraph_info;
    oop _referencing_obj;
- 
    // The following are for maintaining a stack for determining
    // CachedOopInfo::_referrer
    static WalkOopAndArchiveClosure* _current;
    WalkOopAndArchiveClosure* _last;
   public:
--- 1477,10 ---

*** 1109,11 ***
   protected:
    template <class T> void do_oop_work(T *p) {
      oop obj = RawAccess<>::oop_load(p);
      if (!CompressedOops::is_null(obj)) {
        size_t field_delta = pointer_delta(p, _referencing_obj, sizeof(char));
- 
        if (!_record_klasses_only && log_is_enabled(Debug, cds, heap)) {
          ResourceMark rm;
          log_debug(cds, heap)("(%d) %s[" SIZE_FORMAT "] ==> " PTR_FORMAT " size " SIZE_FORMAT " %s", _level,
                               _referencing_obj->klass()->external_name(), field_delta,
                               p2i(obj), obj->size() * HeapWordSize, obj->klass()->external_name());
--- 1504,10 ---

*** 1142,37 ***
    WalkOopAndArchiveClosure* walker = WalkOopAndArchiveClosure::current();
    oop referrer = (walker == nullptr) ? nullptr : walker->referencing_obj();
    return CachedOopInfo(referrer);
  }
  
  // (1) If orig_obj has not been archived yet, archive it.
  // (2) If orig_obj has not been seen yet (since start_recording_subgraph() was called),
  //     trace all  objects that are reachable from it, and make sure these objects are archived.
  // (3) Record the klasses of all orig_obj and all reachable objects.
  bool HeapShared::archive_reachable_objects_from(int level,
                                                  KlassSubGraphInfo* subgraph_info,
                                                  oop orig_obj) {
    assert(orig_obj != nullptr, "must be");
  
    if (!JavaClasses::is_supported_for_archiving(orig_obj)) {
      // This object has injected fields that cannot be supported easily, so we disallow them for now.
      // If you get an error here, you probably made a change in the JDK library that has added
      // these objects that are referenced (directly or indirectly) by static fields.
      ResourceMark rm;
      log_error(cds, heap)("Cannot archive object of class %s", orig_obj->klass()->external_name());
!     MetaspaceShared::unrecoverable_writing_error();
    }
  
!   // java.lang.Class instances cannot be included in an archived object sub-graph. We only support
-   // them as Klass::_archived_mirror because they need to be specially restored at run time.
-   //
-   // If you get an error here, you probably made a change in the JDK library that has added a Class
-   // object that is referenced (directly or indirectly) by static fields.
    if (java_lang_Class::is_instance(orig_obj) && subgraph_info != _default_subgraph_info) {
!     log_error(cds, heap)("(%d) Unknown java.lang.Class object is in the archived sub-graph", level);
!     MetaspaceShared::unrecoverable_writing_error();
    }
  
    if (has_been_seen_during_subgraph_recording(orig_obj)) {
      // orig_obj has already been archived and traced. Nothing more to do.
      return true;
    } else {
--- 1536,104 ---
    WalkOopAndArchiveClosure* walker = WalkOopAndArchiveClosure::current();
    oop referrer = (walker == nullptr) ? nullptr : walker->referencing_obj();
    return CachedOopInfo(referrer);
  }
  
+ // We currently allow only the box classes, which are initialized very early by
+ // HeapShared::init_box_classes().
+ bool HeapShared::can_mirror_be_used_in_subgraph(oop orig_java_mirror) {
+   return java_lang_Class::is_primitive(orig_java_mirror)
+     || orig_java_mirror == vmClasses::Boolean_klass()->java_mirror()
+     || orig_java_mirror == vmClasses::Character_klass()->java_mirror()
+     || orig_java_mirror == vmClasses::Float_klass()->java_mirror()
+     || orig_java_mirror == vmClasses::Double_klass()->java_mirror()
+     || orig_java_mirror == vmClasses::Byte_klass()->java_mirror()
+     || orig_java_mirror == vmClasses::Short_klass()->java_mirror()
+     || orig_java_mirror == vmClasses::Integer_klass()->java_mirror()
+     || orig_java_mirror == vmClasses::Long_klass()->java_mirror()
+     || orig_java_mirror == vmClasses::Void_klass()->java_mirror()
+     || orig_java_mirror == vmClasses::Object_klass()->java_mirror();
+ }
+ 
+ void HeapShared::init_box_classes(TRAPS) {
+   if (ArchiveHeapLoader::is_in_use()) {
+     vmClasses::Boolean_klass()->initialize(CHECK);
+     vmClasses::Character_klass()->initialize(CHECK);
+     vmClasses::Float_klass()->initialize(CHECK);
+     vmClasses::Double_klass()->initialize(CHECK);
+     vmClasses::Byte_klass()->initialize(CHECK);
+     vmClasses::Short_klass()->initialize(CHECK);
+     vmClasses::Integer_klass()->initialize(CHECK);
+     vmClasses::Long_klass()->initialize(CHECK);
+     vmClasses::Void_klass()->initialize(CHECK);
+     _box_classes_inited = true;
+   }
+ }
+ 
+ void HeapShared::exit_on_error() {
+   if (_context != nullptr) {
+     ResourceMark rm;
+     LogStream ls(Log(cds, heap)::error());
+     ls.print_cr("Context");
+     for (int i = 0; i < _context->length(); i++) {
+       const char* s = _context->at(i);
+       ls.print_cr("- %s", s);
+     }
+   }
+   if (_trace != nullptr) {
+     ResourceMark rm;
+     LogStream ls(Log(cds, heap)::error());
+     ls.print_cr("Reference trace");
+     for (int i = 0; i < _trace->length(); i++) {
+       oop orig_obj = _trace->at(i);
+       ls.print_cr("[%d] ========================================", i);
+       orig_obj->print_on(&ls);
+       ls.cr();
+     }
+   }
+   MetaspaceShared::unrecoverable_writing_error();
+ }
+ 
  // (1) If orig_obj has not been archived yet, archive it.
  // (2) If orig_obj has not been seen yet (since start_recording_subgraph() was called),
  //     trace all  objects that are reachable from it, and make sure these objects are archived.
  // (3) Record the klasses of all orig_obj and all reachable objects.
  bool HeapShared::archive_reachable_objects_from(int level,
                                                  KlassSubGraphInfo* subgraph_info,
                                                  oop orig_obj) {
+   ArchivingObjectMark mark(orig_obj);
    assert(orig_obj != nullptr, "must be");
  
    if (!JavaClasses::is_supported_for_archiving(orig_obj)) {
      // This object has injected fields that cannot be supported easily, so we disallow them for now.
      // If you get an error here, you probably made a change in the JDK library that has added
      // these objects that are referenced (directly or indirectly) by static fields.
      ResourceMark rm;
      log_error(cds, heap)("Cannot archive object of class %s", orig_obj->klass()->external_name());
!     exit_on_error();
    }
  
! #if 0
    if (java_lang_Class::is_instance(orig_obj) && subgraph_info != _default_subgraph_info) {
!     if (can_mirror_be_used_in_subgraph(orig_obj)) {
!       orig_obj = scratch_java_mirror(orig_obj);
+       assert(orig_obj != nullptr, "must be archived");
+     } else {
+       // Don't follow the fields -- they will be nulled out when the mirror was copied
+ 
+       // FIXME - we should preserve the static fields of LambdaForm classes (and other hidden classes?)
+       // so we need to walk the oop fields.
+       orig_obj = scratch_java_mirror(orig_obj);
+       assert(orig_obj != nullptr, "must be archived");
+     }
+   }
+ #else
+   if (java_lang_Class::is_instance(orig_obj)) {
+     orig_obj = scratch_java_mirror(orig_obj);
+     assert(orig_obj != nullptr, "must be archived");
    }
+ #endif
  
    if (has_been_seen_during_subgraph_recording(orig_obj)) {
      // orig_obj has already been archived and traced. Nothing more to do.
      return true;
    } else {

*** 1196,22 ***
          return false;
        } else {
          // We don't know how to handle an object that has been archived, but some of its reachable
          // objects cannot be archived. Bail out for now. We might need to fix this in the future if
          // we have a real use case.
!         MetaspaceShared::unrecoverable_writing_error();
        }
      }
    }
  
    Klass *orig_k = orig_obj->klass();
    subgraph_info->add_subgraph_object_klass(orig_k);
  
    WalkOopAndArchiveClosure walker(level, record_klasses_only, subgraph_info, orig_obj);
    orig_obj->oop_iterate(&walker);
  
!   check_enum_obj(level + 1, subgraph_info, orig_obj);
    return true;
  }
  
  //
  // Start from the given static field in a java mirror and archive the
--- 1657,24 ---
          return false;
        } else {
          // We don't know how to handle an object that has been archived, but some of its reachable
          // objects cannot be archived. Bail out for now. We might need to fix this in the future if
          // we have a real use case.
!         exit_on_error();
        }
      }
    }
  
    Klass *orig_k = orig_obj->klass();
    subgraph_info->add_subgraph_object_klass(orig_k);
  
    WalkOopAndArchiveClosure walker(level, record_klasses_only, subgraph_info, orig_obj);
    orig_obj->oop_iterate(&walker);
  
!   if (CDSEnumKlass::is_enum_obj(orig_obj)) {
+     CDSEnumKlass::handle_enum_obj(level + 1, subgraph_info, orig_obj);
+   }
    return true;
  }
  
  //
  // Start from the given static field in a java mirror and archive the

*** 1325,10 ***
--- 1788,14 ---
    delete_seen_objects_table();
  }
  
  void HeapShared::verify_reachable_objects_from(oop obj) {
    _num_total_verifications ++;
+   if (java_lang_Class::is_instance(obj)) {
+     obj = scratch_java_mirror(obj);
+     assert(obj != nullptr, "must be");
+   }
    if (!has_been_seen_during_subgraph_recording(obj)) {
      set_has_been_seen_during_subgraph_recording(obj);
      assert(has_been_archived(obj), "must be");
      VerifySharedOopClosure walker;
      obj->oop_iterate(&walker);

*** 1341,25 ***
  // Make sure that these are only instances of the very few specific types
  // that we can handle.
  void HeapShared::check_default_subgraph_classes() {
    GrowableArray<Klass*>* klasses = _default_subgraph_info->subgraph_object_klasses();
    int num = klasses->length();
    for (int i = 0; i < num; i++) {
      Klass* subgraph_k = klasses->at(i);
      if (log_is_enabled(Info, cds, heap)) {
        ResourceMark rm;
        log_info(cds, heap)(
            "Archived object klass (default subgraph %d) => %s",
            i, subgraph_k->external_name());
      }
  
!     guarantee(subgraph_k->name()->equals("java/lang/Class") ||
!               subgraph_k->name()->equals("java/lang/String") ||
!               subgraph_k->name()->equals("[Ljava/lang/Object;") ||
!               subgraph_k->name()->equals("[C") ||
!               subgraph_k->name()->equals("[B"),
!               "default subgraph can have only these objects");
    }
  }
  
  HeapShared::SeenObjectsTable* HeapShared::_seen_objects_table = nullptr;
  int HeapShared::_num_new_walked_objs;
--- 1808,32 ---
  // Make sure that these are only instances of the very few specific types
  // that we can handle.
  void HeapShared::check_default_subgraph_classes() {
    GrowableArray<Klass*>* klasses = _default_subgraph_info->subgraph_object_klasses();
    int num = klasses->length();
+   int warned = 0;
    for (int i = 0; i < num; i++) {
      Klass* subgraph_k = klasses->at(i);
      if (log_is_enabled(Info, cds, heap)) {
        ResourceMark rm;
        log_info(cds, heap)(
            "Archived object klass (default subgraph %d) => %s",
            i, subgraph_k->external_name());
      }
  
!     if (subgraph_k->is_instance_klass()) {
!       InstanceKlass* ik = InstanceKlass::cast(subgraph_k);
!       Symbol* name = ik->name();
!       if (!name->equals("java/lang/Class") &&
!           !name->equals("java/lang/String") &&
!           !is_archivable_hidden_klass(ik)) {
+         ResourceMark rm;
+         const char* category = ArchiveUtils::class_category(ik);
+         log_info(cds)("TODO: Archived unusual klass (default subgraph %2d) => %-5s %s",
+                       ++warned, category, ik->external_name());
+       }
+     }
    }
  }
  
  HeapShared::SeenObjectsTable* HeapShared::_seen_objects_table = nullptr;
  int HeapShared::_num_new_walked_objs;

*** 1437,16 ***
      TempNewSymbol field_name =  SymbolTable::new_symbol(info->field_name);
      ResourceMark rm; // for stringStream::as_string() etc.
  
  #ifndef PRODUCT
      bool is_test_class = (ArchiveHeapTestClass != nullptr) && (strcmp(info->klass_name, ArchiveHeapTestClass) == 0);
  #else
      bool is_test_class = false;
  #endif
  
      if (is_test_class) {
!       log_warning(cds)("Loading ArchiveHeapTestClass %s ...", ArchiveHeapTestClass);
      }
  
      Klass* k = SystemDictionary::resolve_or_fail(klass_name, true, THREAD);
      if (HAS_PENDING_EXCEPTION) {
        CLEAR_PENDING_EXCEPTION;
--- 1911,18 ---
      TempNewSymbol field_name =  SymbolTable::new_symbol(info->field_name);
      ResourceMark rm; // for stringStream::as_string() etc.
  
  #ifndef PRODUCT
      bool is_test_class = (ArchiveHeapTestClass != nullptr) && (strcmp(info->klass_name, ArchiveHeapTestClass) == 0);
+     const char* test_class_name = ArchiveHeapTestClass;
  #else
      bool is_test_class = false;
+     const char* test_class_name = "";
  #endif
  
      if (is_test_class) {
!       log_warning(cds)("Loading ArchiveHeapTestClass %s ...", test_class_name);
      }
  
      Klass* k = SystemDictionary::resolve_or_fail(klass_name, true, THREAD);
      if (HAS_PENDING_EXCEPTION) {
        CLEAR_PENDING_EXCEPTION;

*** 1468,18 ***
      if (is_test_class) {
        if (ik->module()->is_named()) {
          // We don't want ArchiveHeapTestClass to be abused to easily load/initialize arbitrary
          // core-lib classes. You need to at least append to the bootclasspath.
          stringStream st;
!         st.print("ArchiveHeapTestClass %s is not in unnamed module", ArchiveHeapTestClass);
          THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
        }
  
        if (ik->package() != nullptr) {
          // This restriction makes HeapShared::is_a_test_class_in_unnamed_module() easy.
          stringStream st;
!         st.print("ArchiveHeapTestClass %s is not in unnamed package", ArchiveHeapTestClass);
          THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
        }
      } else {
        if (ik->module()->name() != vmSymbols::java_base()) {
          // We don't want to deal with cases when a module is unavailable at runtime.
--- 1944,18 ---
      if (is_test_class) {
        if (ik->module()->is_named()) {
          // We don't want ArchiveHeapTestClass to be abused to easily load/initialize arbitrary
          // core-lib classes. You need to at least append to the bootclasspath.
          stringStream st;
!         st.print("ArchiveHeapTestClass %s is not in unnamed module", test_class_name);
          THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
        }
  
        if (ik->package() != nullptr) {
          // This restriction makes HeapShared::is_a_test_class_in_unnamed_module() easy.
          stringStream st;
!         st.print("ArchiveHeapTestClass %s is not in unnamed package", test_class_name);
          THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
        }
      } else {
        if (ik->module()->name() != vmSymbols::java_base()) {
          // We don't want to deal with cases when a module is unavailable at runtime.

*** 1490,11 ***
          THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
        }
      }
  
      if (is_test_class) {
!       log_warning(cds)("Initializing ArchiveHeapTestClass %s ...", ArchiveHeapTestClass);
      }
      ik->initialize(CHECK);
  
      ArchivableStaticFieldFinder finder(ik, field_name);
      ik->do_local_static_fields(&finder);
--- 1966,11 ---
          THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
        }
      }
  
      if (is_test_class) {
!       log_warning(cds)("Initializing ArchiveHeapTestClass %s ...", test_class_name);
      }
      ik->initialize(CHECK);
  
      ArchivableStaticFieldFinder finder(ik, field_name);
      ik->do_local_static_fields(&finder);

*** 1582,10 ***
--- 2058,11 ---
  void HeapShared::init_for_dumping(TRAPS) {
    if (HeapShared::can_write()) {
      setup_test_class(ArchiveHeapTestClass);
      _dumped_interned_strings = new (mtClass)DumpedInternedStrings();
      init_subgraph_entry_fields(CHECK);
+     init_scratch_exceptions(CHECK);
    }
  }
  
  void HeapShared::archive_object_subgraphs(ArchivableStaticFieldInfo fields[],
                                            bool is_full_module_graph) {

*** 1605,20 ***
--- 2082,22 ---
    for (int i = 0; fields[i].valid(); ) {
      ArchivableStaticFieldInfo* info = &fields[i];
      const char* klass_name = info->klass_name;
      start_recording_subgraph(info->klass, klass_name, is_full_module_graph);
  
+     ContextMark cm(klass_name);
      // If you have specified consecutive fields of the same klass in
      // fields[], these will be archived in the same
      // {start_recording_subgraph ... done_recording_subgraph} pass to
      // save time.
      for (; fields[i].valid(); i++) {
        ArchivableStaticFieldInfo* f = &fields[i];
        if (f->klass_name != klass_name) {
          break;
        }
  
+       ContextMark cm(f->field_name);
        archive_reachable_objects_from_static_field(f->klass, f->klass_name,
                                                    f->offset, f->field_name);
      }
      done_recording_subgraph(info->klass, klass_name);
    }
< prev index next >