< prev index next >

src/hotspot/share/cds/heapShared.cpp

Print this page
*** 28,10 ***
--- 28,11 ---
  #include "cds/archiveHeapWriter.hpp"
  #include "cds/archiveUtils.hpp"
  #include "cds/cdsConfig.hpp"
  #include "cds/cdsEnumKlass.hpp"
  #include "cds/cdsHeapVerifier.hpp"
+ #include "cds/classPreinitializer.hpp"
  #include "cds/heapShared.hpp"
  #include "cds/metaspaceShared.hpp"
  #include "classfile/classLoaderData.hpp"
  #include "classfile/javaClasses.inline.hpp"
  #include "classfile/modules.hpp"

*** 81,10 ***
--- 82,31 ---
    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;
  DumpedInternedStrings *HeapShared::_dumped_interned_strings = nullptr;
  
  size_t HeapShared::_alloc_count[HeapShared::ALLOC_STAT_SLOTS];
  size_t HeapShared::_alloc_size[HeapShared::ALLOC_STAT_SLOTS];

*** 103,10 ***
--- 125,11 ---
  //
  // If you add new entries to the following tables, you should know what you're doing!
  //
  
  static ArchivableStaticFieldInfo archive_subgraph_entry_fields[] = {
+   {"java/lang/Boolean",                           "archivedCache"},
    {"java/lang/Integer$IntegerCache",              "archivedCache"},
    {"java/lang/Long$LongCache",                    "archivedCache"},
    {"java/lang/Byte$ByteCache",                    "archivedCache"},
    {"java/lang/Short$ShortCache",                  "archivedCache"},
    {"java/lang/Character$CharacterCache",          "archivedCache"},

*** 115,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"},  // FIXME -- requires PreloadSharedClasses
+   {"java/lang/invoke/MethodType",                 "archivedObjects"},  // FIXME -- requires PreloadSharedClasses
+   {"java/lang/invoke/LambdaForm$NamedFunction",   "archivedObjects"},  // FIXME -- requires PreloadSharedClasses
+   {"java/lang/reflect/Proxy$ProxyBuilder",        "archivedData"},     // FIXME -- requires PreloadSharedClasses
  #ifndef PRODUCT
    {nullptr, nullptr}, // Extra slot for -XX:ArchiveHeapTestClass
  #endif
    {nullptr, nullptr},
  };

*** 130,15 ***
--- 157,19 ---
    {"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];
  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;

*** 224,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 {
--- 255,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 {

*** 238,10 ***
--- 269,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() && CDSConfig::is_using_archive(), "runtime only");
    assert(!_roots.is_empty(), "must have loaded shared heap");

*** 327,10 ***
--- 477,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);

*** 340,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");
--- 493,42 ---
    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();
+   }
+ }
+ 
+ // 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");

*** 357,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));
--- 538,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 CDSConfig::is_dumping_invokedynamic() && (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() || !ClassPreinitializer::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() {
+   ClassPreinitializer::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));

*** 388,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,
--- 665,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 (CDSConfig::is_dumping_reflection_data() && 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,

*** 434,10 ***
--- 720,12 ---
  
  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::get_pointer_info(oop src_obj, bool& has_oop_pointers, bool& has_native_pointers) {
    CachedOopInfo* info = archived_object_cache()->get(src_obj);

*** 450,27 ***
    CachedOopInfo* info = archived_object_cache()->get(src_obj);
    assert(info != nullptr, "must be");
    info->set_has_native_pointers();
  }
  
  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 "]",
!                    UseCompressedOops ? p2i(CompressedOops::begin()) :
!                                        p2i((address)G1CollectedHeap::heap()->reserved().start()),
!                    UseCompressedOops ? p2i(CompressedOops::end()) :
!                                        p2i((address)G1CollectedHeap::heap()->reserved().end()));
      copy_objects();
  
!     CDSHeapVerifier::verify();
      check_default_subgraph_classes();
    }
  
    ArchiveHeapWriter::write(_pending_roots, heap_info);
  }
--- 738,126 ---
    CachedOopInfo* info = archived_object_cache()->get(src_obj);
    assert(info != nullptr, "must be");
    info->set_has_native_pointers();
  }
  
+ void HeapShared::start_finding_archivable_hidden_classes() {
+   NoSafepointVerifier nsv;
+ 
+   init_seen_objects_table();
+ 
+   find_archivable_hidden_classes_helper(archive_subgraph_entry_fields);
+   if (CDSConfig::is_dumping_full_module_graph()) {
+     find_archivable_hidden_classes_helper(fmg_archive_subgraph_entry_fields);
+   }
+ }
+ 
+ void HeapShared::end_finding_archivable_hidden_classes() {
+   NoSafepointVerifier nsv;
+ 
+   delete_seen_objects_table();
+ }
+ 
+ void HeapShared::find_archivable_hidden_classes_helper(ArchivableStaticFieldInfo fields[]) {
+   if (!CDSConfig::is_dumping_heap()) {
+     return;
+   }
+   int i;
+   for (int i = 0; fields[i].valid(); ) {
+     ArchivableStaticFieldInfo* info = &fields[i];
+     const char* klass_name = info->klass_name;
+     for (; fields[i].valid(); i++) {
+       ArchivableStaticFieldInfo* f = &fields[i];
+       if (f->klass_name != klass_name) {
+         break;
+       }
+ 
+       InstanceKlass* k = f->klass;
+       oop m = k->java_mirror();
+       oop o = m->obj_field(f->offset);
+       if (o != nullptr) {
+         find_archivable_hidden_classes_in_object(o);
+       }
+     }
+   }
+ }
+ 
+ class HeapShared::FindHiddenClassesOopClosure: public BasicOopIterateClosure {
+   GrowableArray<oop> _stack;
+   template <class T> void do_oop_work(T *p) {
+     // Recurse on a GrowableArray to avoid overflowing the C stack.
+     oop o = RawAccess<>::oop_load(p);
+     if (o != nullptr) {
+       _stack.append(o);
+     }
+   }
+ 
+  public:
+ 
+   void do_oop(narrowOop *p) { FindHiddenClassesOopClosure::do_oop_work(p); }
+   void do_oop(      oop *p) { FindHiddenClassesOopClosure::do_oop_work(p); }
+ 
+   FindHiddenClassesOopClosure(oop o) {
+     _stack.append(o);
+   }
+   oop pop() {
+     if (_stack.length() == 0) {
+       return nullptr;
+     } else {
+       return _stack.pop();
+     }
+   }
+ };
+ 
+ void HeapShared::find_archivable_hidden_classes_in_object(oop root) {
+   ResourceMark rm;
+   FindHiddenClassesOopClosure c(root);
+   oop o;
+   while ((o = c.pop()) != nullptr) {
+     if (!has_been_seen_during_subgraph_recording(o)) {
+       set_has_been_seen_during_subgraph_recording(o);
+ 
+       if (java_lang_Class::is_instance(o)) {
+         Klass* k = java_lang_Class::as_Klass(o);
+         if (k != nullptr && k->is_instance_klass()) {
+           SystemDictionaryShared::mark_required_class(InstanceKlass::cast(k));
+         }
+       } else if (java_lang_invoke_ResolvedMethodName::is_instance(o)) {
+         Method* m = java_lang_invoke_ResolvedMethodName::vmtarget(o);
+         if (m != nullptr && m->method_holder() != nullptr) {
+           SystemDictionaryShared::mark_required_class(m->method_holder());
+         }
+       }
+ 
+       o->oop_iterate(&c);
+     }
+   }
+ }
+ 
  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();
  
!     if (UseCompressedOops || UseG1GC) {
!       log_info(cds)("Heap range = [" PTR_FORMAT " - "  PTR_FORMAT "]",
!                     UseCompressedOops ? p2i(CompressedOops::begin()) :
!                                          p2i((address)G1CollectedHeap::heap()->reserved().start()),
!                     UseCompressedOops ? p2i(CompressedOops::end()) :
+                                          p2i((address)G1CollectedHeap::heap()->reserved().end()));
+     }
      copy_objects();
  
!     if (!SkipArchiveHeapVerification) {
+       CDSHeapVerifier::verify();
+     }
      check_default_subgraph_classes();
    }
  
    ArchiveHeapWriter::write(_pending_roots, heap_info);
  }

*** 573,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
--- 960,15 ---
      // initialization list.
      return;
    }
  
    if (buffered_k->is_instance_klass()) {
!     if (!CDSConfig::is_dumping_invokedynamic()) {
!       // 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

*** 614,10 ***
--- 1004,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 (CDSConfig::is_dumping_invokedynamic()) {
+     // 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;
    }
  

*** 711,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.
--- 1105,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.

*** 749,10 ***
--- 1148,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);

*** 762,10 ***
--- 1163,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);
    }

*** 780,10 ***
--- 1185,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;

*** 820,10 ***
--- 1227,23 ---
  void HeapShared::resolve_classes(JavaThread* current) {
    assert(CDSConfig::is_using_archive(), "runtime only!");
    if (!ArchiveHeapLoader::is_in_use()) {
      return; // nothing to do
    }
+ 
+   if (!CDSConfig::has_preloaded_classes()) {
+     assert( _runtime_default_subgraph_info != nullptr, "must be");
+     Array<Klass*>* klasses = _runtime_default_subgraph_info->subgraph_object_klasses();
+     if (klasses != nullptr) {
+       for (int i = 0; i < klasses->length(); i++) {
+         Klass* k = klasses->at(i);
+         ExceptionMark em(current); // no exception can happen here
+         resolve_or_init(k, /*do_init*/false, current);
+       }
+     }
+   }
+ 
    resolve_classes_for_subgraphs(current, archive_subgraph_entry_fields);
    resolve_classes_for_subgraphs(current, fmg_archive_subgraph_entry_fields);
  }
  
  void HeapShared::resolve_classes_for_subgraphs(JavaThread* current, ArchivableStaticFieldInfo fields[]) {

*** 847,10 ***
--- 1267,51 ---
    if (record == nullptr) {
     clear_archived_roots_of(k);
    }
  }
  
+ void HeapShared::initialize_java_lang_invoke(TRAPS) {
+   if (CDSConfig::is_loading_invokedynamic() || CDSConfig::is_dumping_invokedynamic()) {
+     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
    }

*** 920,10 ***
--- 1381,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();

*** 939,10 ***
--- 1452,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");

*** 1008,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:
--- 1534,10 ---

*** 1036,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());
--- 1561,10 ---

*** 1086,37 ***
    PointsToOopsChecker points_to_oops_checker;
    obj->oop_iterate(&points_to_oops_checker);
    return CachedOopInfo(referrer, points_to_oops_checker.result());
  }
  
  // (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 {
--- 1610,103 ---
    PointsToOopsChecker points_to_oops_checker;
    obj->oop_iterate(&points_to_oops_checker);
    return CachedOopInfo(referrer, points_to_oops_checker.result());
  }
  
+ // 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);
+   }
+ }
+ 
+ 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 {

*** 1140,11 ***
          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();
--- 1730,11 ---
          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();

*** 1271,10 ***
--- 1861,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);

*** 1287,29 ***
  // 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());
      }
  
!     Symbol* name = ArchiveBuilder::current()->get_source_addr(subgraph_k->name());
!     guarantee(name == vmSymbols::java_lang_Class() ||
!               name == vmSymbols::java_lang_String() ||
!               name == vmSymbols::java_lang_ArithmeticException() ||
!               name == vmSymbols::java_lang_NullPointerException() ||
!               name == vmSymbols::java_lang_InternalError() ||
!               name == vmSymbols::object_array_signature() ||
!               name == vmSymbols::byte_array_signature() ||
!               name == vmSymbols::char_array_signature(),
!               "default subgraph can have only these objects");
    }
  }
  
  HeapShared::SeenObjectsTable* HeapShared::_seen_objects_table = nullptr;
  int HeapShared::_num_new_walked_objs;
--- 1881,38 ---
  // 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 = ArchiveBuilder::current()->get_source_addr(ik->name());
!       if (name != vmSymbols::java_lang_Class() &&
!           name != vmSymbols::java_lang_String() &&
!           name != vmSymbols::java_lang_ArithmeticException() &&
!           name != vmSymbols::java_lang_ArrayIndexOutOfBoundsException() &&
!           name != vmSymbols::java_lang_ArrayStoreException() &&
!           name != vmSymbols::java_lang_ClassCastException() &&
!           name != vmSymbols::java_lang_InternalError() &&
+           name != vmSymbols::java_lang_NullPointerException() &&
+           !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;

*** 1391,11 ***
  #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 = ""; // avoid C++ printf checks warnings.
  #endif
  
      if (is_test_class) {
        log_warning(cds)("Loading ArchiveHeapTestClass %s ...", test_class_name);
      }
--- 1994,11 ---
  #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);
      }

*** 1558,20 ***
--- 2161,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 >