1 /*
   2  * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.inline.hpp"
  27 #include "classfile/stringTable.hpp"
  28 #include "classfile/symbolTable.hpp"
  29 #include "classfile/systemDictionaryShared.hpp"
  30 #include "classfile/vmSymbols.hpp"
  31 #include "gc/shared/gcLocker.hpp"
  32 #include "logging/log.hpp"
  33 #include "logging/logMessage.hpp"
  34 #include "logging/logStream.hpp"
  35 #include "memory/archiveUtils.hpp"
  36 #include "memory/filemap.hpp"
  37 #include "memory/heapShared.inline.hpp"
  38 #include "memory/iterator.inline.hpp"
  39 #include "memory/metadataFactory.hpp"
  40 #include "memory/metaspaceClosure.hpp"
  41 #include "memory/metaspaceShared.hpp"
  42 #include "memory/resourceArea.hpp"
  43 #include "memory/universe.hpp"
  44 #include "oops/compressedOops.inline.hpp"
  45 #include "oops/fieldStreams.inline.hpp"
  46 #include "oops/oop.inline.hpp"
  47 #include "runtime/fieldDescriptor.inline.hpp"
  48 #include "runtime/safepointVerifiers.hpp"
  49 #include "utilities/bitMap.inline.hpp"
  50 #if INCLUDE_G1GC
  51 #include "gc/g1/g1CollectedHeap.hpp"
  52 #endif
  53 
  54 #if INCLUDE_CDS_JAVA_HEAP
  55 
  56 bool HeapShared::_closed_archive_heap_region_mapped = false;
  57 bool HeapShared::_open_archive_heap_region_mapped = false;
  58 bool HeapShared::_archive_heap_region_fixed = false;
  59 
  60 address   HeapShared::_narrow_oop_base;
  61 int       HeapShared::_narrow_oop_shift;
  62 
  63 //
  64 // If you add new entries to the following tables, you should know what you're doing!
  65 //
  66 
  67 // Entry fields for shareable subgraphs archived in the closed archive heap
  68 // region. Warning: Objects in the subgraphs should not have reference fields
  69 // assigned at runtime.
  70 static ArchivableStaticFieldInfo closed_archive_subgraph_entry_fields[] = {
  71   {"java/lang/Integer$IntegerCache",           "archivedCache"},
  72   {"java/lang/Long$LongCache",                 "archivedCache"},
  73   {"java/lang/Byte$ByteCache",                 "archivedCache"},
  74   {"java/lang/Short$ShortCache",               "archivedCache"},
  75   {"java/lang/Character$CharacterCache",       "archivedCache"},
  76   {"java/util/jar/Attributes$Name",            "KNOWN_NAMES"},
  77   {"sun/util/locale/BaseLocale",               "constantBaseLocales"},
  78 };
  79 // Entry fields for subgraphs archived in the open archive heap region.
  80 static ArchivableStaticFieldInfo open_archive_subgraph_entry_fields[] = {
  81   {"jdk/internal/module/ArchivedModuleGraph",  "archivedModuleGraph"},
  82   {"java/util/ImmutableCollections",           "archivedObjects"},
  83   {"java/lang/module/Configuration",           "EMPTY_CONFIGURATION"},
  84   {"jdk/internal/math/FDBigInteger",           "archivedCaches"},
  85 };
  86 
  87 const static int num_closed_archive_subgraph_entry_fields =
  88   sizeof(closed_archive_subgraph_entry_fields) / sizeof(ArchivableStaticFieldInfo);
  89 const static int num_open_archive_subgraph_entry_fields =
  90   sizeof(open_archive_subgraph_entry_fields) / sizeof(ArchivableStaticFieldInfo);
  91 
  92 ////////////////////////////////////////////////////////////////
  93 //
  94 // Java heap object archiving support
  95 //
  96 ////////////////////////////////////////////////////////////////
  97 void HeapShared::fixup_mapped_heap_regions() {
  98   FileMapInfo *mapinfo = FileMapInfo::current_info();
  99   mapinfo->fixup_mapped_heap_regions();
 100   set_archive_heap_region_fixed();
 101   SystemDictionaryShared::update_archived_mirror_native_pointers();
 102 }
 103 
 104 unsigned HeapShared::oop_hash(oop const& p) {
 105   assert(!p->mark().has_bias_pattern(),
 106          "this object should never have been locked");  // so identity_hash won't safepoin
 107   unsigned hash = (unsigned)p->identity_hash();
 108   return hash;
 109 }
 110 
 111 HeapShared::ArchivedObjectCache* HeapShared::_archived_object_cache = NULL;
 112 oop HeapShared::find_archived_heap_object(oop obj) {
 113   assert(DumpSharedSpaces, "dump-time only");
 114   ArchivedObjectCache* cache = archived_object_cache();
 115   oop* p = cache->get(obj);
 116   if (p != NULL) {
 117     return *p;
 118   } else {
 119     return NULL;
 120   }
 121 }
 122 
 123 oop HeapShared::archive_heap_object(oop obj, Thread* THREAD) {
 124   assert(DumpSharedSpaces, "dump-time only");
 125 
 126   oop ao = find_archived_heap_object(obj);
 127   if (ao != NULL) {
 128     // already archived
 129     return ao;
 130   }
 131 
 132   int len = obj->size();
 133   if (G1CollectedHeap::heap()->is_archive_alloc_too_large(len)) {
 134     log_debug(cds, heap)("Cannot archive, object (" PTR_FORMAT ") is too large: " SIZE_FORMAT,
 135                          p2i(obj), (size_t)obj->size());
 136     return NULL;
 137   }
 138 
 139   oop archived_oop = (oop)G1CollectedHeap::heap()->archive_mem_allocate(len);
 140   if (archived_oop != NULL) {
 141     Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(obj), cast_from_oop<HeapWord*>(archived_oop), len);
 142     MetaspaceShared::relocate_klass_ptr(archived_oop);
 143     // Reinitialize markword to remove age/marking/locking/etc.
 144     //
 145     // We need to retain the identity_hash, because it may have been used by some hashtables
 146     // in the shared heap. This also has the side effect of pre-initializing the
 147     // identity_hash for all shared objects, so they are less likely to be written
 148     // into during run time, increasing the potential of memory sharing.
 149     int hash_original = obj->identity_hash();
 150     archived_oop->set_mark_raw(markWord::prototype().copy_set_hash(hash_original));
 151     assert(archived_oop->mark().is_unlocked(), "sanity");
 152 
 153     DEBUG_ONLY(int hash_archived = archived_oop->identity_hash());
 154     assert(hash_original == hash_archived, "Different hash codes: original %x, archived %x", hash_original, hash_archived);
 155 
 156     ArchivedObjectCache* cache = archived_object_cache();
 157     cache->put(obj, archived_oop);
 158     log_debug(cds, heap)("Archived heap object " PTR_FORMAT " ==> " PTR_FORMAT,
 159                          p2i(obj), p2i(archived_oop));
 160   } else {
 161     log_error(cds, heap)(
 162       "Cannot allocate space for object " PTR_FORMAT " in archived heap region",
 163       p2i(obj));
 164     vm_exit(1);
 165   }
 166   return archived_oop;
 167 }
 168 
 169 oop HeapShared::materialize_archived_object(narrowOop v) {
 170   assert(archive_heap_region_fixed(),
 171          "must be called after archive heap regions are fixed");
 172   if (!CompressedOops::is_null(v)) {
 173     oop obj = HeapShared::decode_from_archive(v);
 174     return G1CollectedHeap::heap()->materialize_archived_object(obj);
 175   }
 176   return NULL;
 177 }
 178 
 179 void HeapShared::archive_klass_objects(Thread* THREAD) {
 180   GrowableArray<Klass*>* klasses = MetaspaceShared::collected_klasses();
 181   assert(klasses != NULL, "sanity");
 182   for (int i = 0; i < klasses->length(); i++) {
 183     Klass* k = klasses->at(i);
 184 
 185     // archive mirror object
 186     java_lang_Class::archive_mirror(k, CHECK);
 187 
 188     // archive the resolved_referenes array
 189     if (k->is_instance_klass()) {
 190       InstanceKlass* ik = InstanceKlass::cast(k);
 191       ik->constants()->archive_resolved_references(THREAD);
 192     }
 193   }
 194 }
 195 
 196 void HeapShared::run_full_gc_in_vm_thread() {
 197   if (is_heap_object_archiving_allowed()) {
 198     // Avoid fragmentation while archiving heap objects.
 199     // We do this inside a safepoint, so that no further allocation can happen after GC
 200     // has finished.
 201     if (GCLocker::is_active()) {
 202       // Just checking for safety ...
 203       // This should not happen during -Xshare:dump. If you see this, probably the Java core lib
 204       // has been modified such that JNI code is executed in some clean up threads after
 205       // we have finished class loading.
 206       log_warning(cds)("GC locker is held, unable to start extra compacting GC. This may produce suboptimal results.");
 207     } else {
 208       log_info(cds)("Run GC ...");
 209       Universe::heap()->collect_as_vm_thread(GCCause::_archive_time_gc);
 210       log_info(cds)("Run GC done");
 211     }
 212   }
 213 }
 214 
 215 void HeapShared::archive_java_heap_objects(GrowableArray<MemRegion> *closed,
 216                                            GrowableArray<MemRegion> *open) {
 217   if (!is_heap_object_archiving_allowed()) {
 218     log_info(cds)(
 219       "Archived java heap is not supported as UseG1GC, "
 220       "UseCompressedOops and UseCompressedClassPointers are required."
 221       "Current settings: UseG1GC=%s, UseCompressedOops=%s, UseCompressedClassPointers=%s.",
 222       BOOL_TO_STR(UseG1GC), BOOL_TO_STR(UseCompressedOops),
 223       BOOL_TO_STR(UseCompressedClassPointers));
 224     return;
 225   }
 226 
 227   G1HeapVerifier::verify_ready_for_archiving();
 228 
 229   {
 230     NoSafepointVerifier nsv;
 231 
 232     // Cache for recording where the archived objects are copied to
 233     create_archived_object_cache();
 234 
 235     log_info(cds)("Dumping objects to closed archive heap region ...");
 236     NOT_PRODUCT(StringTable::verify());
 237     copy_closed_archive_heap_objects(closed);
 238 
 239     log_info(cds)("Dumping objects to open archive heap region ...");
 240     copy_open_archive_heap_objects(open);
 241 
 242     destroy_archived_object_cache();
 243   }
 244 
 245   G1HeapVerifier::verify_archive_regions();
 246 }
 247 
 248 void HeapShared::copy_closed_archive_heap_objects(
 249                                     GrowableArray<MemRegion> * closed_archive) {
 250   assert(is_heap_object_archiving_allowed(), "Cannot archive java heap objects");
 251 
 252   Thread* THREAD = Thread::current();
 253   G1CollectedHeap::heap()->begin_archive_alloc_range();
 254 
 255   // Archive interned string objects
 256   StringTable::write_to_archive();
 257 
 258   archive_object_subgraphs(closed_archive_subgraph_entry_fields,
 259                            num_closed_archive_subgraph_entry_fields,
 260                            true /* is_closed_archive */, THREAD);
 261 
 262   G1CollectedHeap::heap()->end_archive_alloc_range(closed_archive,
 263                                                    os::vm_allocation_granularity());
 264 }
 265 
 266 void HeapShared::copy_open_archive_heap_objects(
 267                                     GrowableArray<MemRegion> * open_archive) {
 268   assert(is_heap_object_archiving_allowed(), "Cannot archive java heap objects");
 269 
 270   Thread* THREAD = Thread::current();
 271   G1CollectedHeap::heap()->begin_archive_alloc_range(true /* open */);
 272 
 273   java_lang_Class::archive_basic_type_mirrors(THREAD);
 274 
 275   archive_klass_objects(THREAD);
 276 
 277   archive_object_subgraphs(open_archive_subgraph_entry_fields,
 278                            num_open_archive_subgraph_entry_fields,
 279                            false /* is_closed_archive */,
 280                            THREAD);
 281 
 282   G1CollectedHeap::heap()->end_archive_alloc_range(open_archive,
 283                                                    os::vm_allocation_granularity());
 284 }
 285 
 286 void HeapShared::init_narrow_oop_decoding(address base, int shift) {
 287   _narrow_oop_base = base;
 288   _narrow_oop_shift = shift;
 289 }
 290 
 291 //
 292 // Subgraph archiving support
 293 //
 294 HeapShared::DumpTimeKlassSubGraphInfoTable* HeapShared::_dump_time_subgraph_info_table = NULL;
 295 HeapShared::RunTimeKlassSubGraphInfoTable   HeapShared::_run_time_subgraph_info_table;
 296 
 297 // Get the subgraph_info for Klass k. A new subgraph_info is created if
 298 // there is no existing one for k. The subgraph_info records the relocated
 299 // Klass* of the original k.
 300 KlassSubGraphInfo* HeapShared::get_subgraph_info(Klass* k) {
 301   assert(DumpSharedSpaces, "dump time only");
 302   Klass* relocated_k = MetaspaceShared::get_relocated_klass(k);
 303   KlassSubGraphInfo* info = _dump_time_subgraph_info_table->get(relocated_k);
 304   if (info == NULL) {
 305     _dump_time_subgraph_info_table->put(relocated_k, KlassSubGraphInfo(relocated_k));
 306     info = _dump_time_subgraph_info_table->get(relocated_k);
 307     ++ _dump_time_subgraph_info_table->_count;
 308   }
 309   return info;
 310 }
 311 
 312 // Add an entry field to the current KlassSubGraphInfo.
 313 void KlassSubGraphInfo::add_subgraph_entry_field(
 314       int static_field_offset, oop v, bool is_closed_archive) {
 315   assert(DumpSharedSpaces, "dump time only");
 316   if (_subgraph_entry_fields == NULL) {
 317     _subgraph_entry_fields =
 318       new(ResourceObj::C_HEAP, mtClass) GrowableArray<juint>(10, mtClass);
 319   }
 320   _subgraph_entry_fields->append((juint)static_field_offset);
 321   _subgraph_entry_fields->append(CompressedOops::encode(v));
 322   _subgraph_entry_fields->append(is_closed_archive ? 1 : 0);
 323 }
 324 
 325 // Add the Klass* for an object in the current KlassSubGraphInfo's subgraphs.
 326 // Only objects of boot classes can be included in sub-graph.
 327 void KlassSubGraphInfo::add_subgraph_object_klass(Klass* orig_k, Klass *relocated_k) {
 328   assert(DumpSharedSpaces, "dump time only");
 329   assert(relocated_k == MetaspaceShared::get_relocated_klass(orig_k),
 330          "must be the relocated Klass in the shared space");
 331 
 332   if (_subgraph_object_klasses == NULL) {
 333     _subgraph_object_klasses =
 334       new(ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(50, mtClass);
 335   }
 336 
 337   assert(relocated_k->is_shared(), "must be a shared class");
 338 
 339   if (_k == relocated_k) {
 340     // Don't add the Klass containing the sub-graph to it's own klass
 341     // initialization list.
 342     return;
 343   }
 344 
 345   if (relocated_k->is_instance_klass()) {
 346     assert(InstanceKlass::cast(relocated_k)->is_shared_boot_class(),
 347           "must be boot class");
 348     // SystemDictionary::xxx_klass() are not updated, need to check
 349     // the original Klass*
 350     if (orig_k == SystemDictionary::String_klass() ||
 351         orig_k == SystemDictionary::Object_klass()) {
 352       // Initialized early during VM initialization. No need to be added
 353       // to the sub-graph object class list.
 354       return;
 355     }
 356   } else if (relocated_k->is_objArray_klass()) {
 357     Klass* abk = ObjArrayKlass::cast(relocated_k)->bottom_klass();
 358     if (abk->is_instance_klass()) {
 359       assert(InstanceKlass::cast(abk)->is_shared_boot_class(),
 360             "must be boot class");
 361     }
 362     if (relocated_k == Universe::objectArrayKlassObj()) {
 363       // Initialized early during Universe::genesis. No need to be added
 364       // to the list.
 365       return;
 366     }
 367   } else {
 368     assert(relocated_k->is_typeArray_klass(), "must be");
 369     // Primitive type arrays are created early during Universe::genesis.
 370     return;
 371   }
 372 
 373   if (log_is_enabled(Debug, cds, heap)) {
 374     if (!_subgraph_object_klasses->contains(relocated_k)) {
 375       ResourceMark rm;
 376       log_debug(cds, heap)("Adding klass %s", orig_k->external_name());
 377     }
 378   }
 379 
 380   _subgraph_object_klasses->append_if_missing(relocated_k);
 381 }
 382 
 383 // Initialize an archived subgraph_info_record from the given KlassSubGraphInfo.
 384 void ArchivedKlassSubGraphInfoRecord::init(KlassSubGraphInfo* info) {
 385   _k = info->klass();
 386   _entry_field_records = NULL;
 387   _subgraph_object_klasses = NULL;
 388 
 389   // populate the entry fields
 390   GrowableArray<juint>* entry_fields = info->subgraph_entry_fields();
 391   if (entry_fields != NULL) {
 392     int num_entry_fields = entry_fields->length();
 393     assert(num_entry_fields % 3 == 0, "sanity");
 394     _entry_field_records =
 395       MetaspaceShared::new_ro_array<juint>(num_entry_fields);
 396     for (int i = 0 ; i < num_entry_fields; i++) {
 397       _entry_field_records->at_put(i, entry_fields->at(i));
 398     }
 399   }
 400 
 401   // the Klasses of the objects in the sub-graphs
 402   GrowableArray<Klass*>* subgraph_object_klasses = info->subgraph_object_klasses();
 403   if (subgraph_object_klasses != NULL) {
 404     int num_subgraphs_klasses = subgraph_object_klasses->length();
 405     _subgraph_object_klasses =
 406       MetaspaceShared::new_ro_array<Klass*>(num_subgraphs_klasses);
 407     for (int i = 0; i < num_subgraphs_klasses; i++) {
 408       Klass* subgraph_k = subgraph_object_klasses->at(i);
 409       if (log_is_enabled(Info, cds, heap)) {
 410         ResourceMark rm;
 411         log_info(cds, heap)(
 412           "Archived object klass %s (%2d) => %s",
 413           _k->external_name(), i, subgraph_k->external_name());
 414       }
 415       _subgraph_object_klasses->at_put(i, subgraph_k);
 416       ArchivePtrMarker::mark_pointer(_subgraph_object_klasses->adr_at(i));
 417     }
 418   }
 419 
 420   ArchivePtrMarker::mark_pointer(&_k);
 421   ArchivePtrMarker::mark_pointer(&_entry_field_records);
 422   ArchivePtrMarker::mark_pointer(&_subgraph_object_klasses);
 423 }
 424 
 425 struct CopyKlassSubGraphInfoToArchive : StackObj {
 426   CompactHashtableWriter* _writer;
 427   CopyKlassSubGraphInfoToArchive(CompactHashtableWriter* writer) : _writer(writer) {}
 428 
 429   bool do_entry(Klass* klass, KlassSubGraphInfo& info) {
 430     if (info.subgraph_object_klasses() != NULL || info.subgraph_entry_fields() != NULL) {
 431       ArchivedKlassSubGraphInfoRecord* record =
 432         (ArchivedKlassSubGraphInfoRecord*)MetaspaceShared::read_only_space_alloc(sizeof(ArchivedKlassSubGraphInfoRecord));
 433       record->init(&info);
 434 
 435       unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary(klass);
 436       u4 delta = MetaspaceShared::object_delta_u4(record);
 437       _writer->add(hash, delta);
 438     }
 439     return true; // keep on iterating
 440   }
 441 };
 442 
 443 // Build the records of archived subgraph infos, which include:
 444 // - Entry points to all subgraphs from the containing class mirror. The entry
 445 //   points are static fields in the mirror. For each entry point, the field
 446 //   offset, value and is_closed_archive flag are recorded in the sub-graph
 447 //   info. The value is stored back to the corresponding field at runtime.
 448 // - A list of klasses that need to be loaded/initialized before archived
 449 //   java object sub-graph can be accessed at runtime.
 450 void HeapShared::write_subgraph_info_table() {
 451   // Allocate the contents of the hashtable(s) inside the RO region of the CDS archive.
 452   DumpTimeKlassSubGraphInfoTable* d_table = _dump_time_subgraph_info_table;
 453   CompactHashtableStats stats;
 454 
 455   _run_time_subgraph_info_table.reset();
 456 
 457   CompactHashtableWriter writer(d_table->_count, &stats);
 458   CopyKlassSubGraphInfoToArchive copy(&writer);
 459   d_table->iterate(&copy);
 460 
 461   writer.dump(&_run_time_subgraph_info_table, "subgraphs");
 462 }
 463 
 464 void HeapShared::serialize_subgraph_info_table_header(SerializeClosure* soc) {
 465   _run_time_subgraph_info_table.serialize_header(soc);
 466 }
 467 
 468 void HeapShared::initialize_from_archived_subgraph(Klass* k) {
 469   if (!open_archive_heap_region_mapped()) {
 470     return; // nothing to do
 471   }
 472   assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces");
 473 
 474   unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary(k);
 475   const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0);
 476 
 477   // Initialize from archived data. Currently this is done only
 478   // during VM initialization time. No lock is needed.
 479   if (record != NULL) {
 480     Thread* THREAD = Thread::current();
 481 
 482     int i;
 483     // Load/link/initialize the klasses of the objects in the subgraph.
 484     // NULL class loader is used.
 485     Array<Klass*>* klasses = record->subgraph_object_klasses();
 486     if (klasses != NULL) {
 487       for (i = 0; i < klasses->length(); i++) {
 488         Klass* obj_k = klasses->at(i);
 489         Klass* resolved_k = SystemDictionary::resolve_or_null(
 490                                               (obj_k)->name(), THREAD);
 491         if (resolved_k != obj_k) {
 492           assert(!SystemDictionary::is_well_known_klass(resolved_k),
 493                  "shared well-known classes must not be replaced by JVMTI ClassFileLoadHook");
 494           ResourceMark rm(THREAD);
 495           log_info(cds, heap)("Failed to load subgraph because %s was not loaded from archive",
 496                               resolved_k->external_name());
 497           return;
 498         }
 499         if ((obj_k)->is_instance_klass()) {
 500           InstanceKlass* ik = InstanceKlass::cast(obj_k);
 501           ik->initialize(THREAD);
 502         } else if ((obj_k)->is_objArray_klass()) {
 503           ObjArrayKlass* oak = ObjArrayKlass::cast(obj_k);
 504           oak->initialize(THREAD);
 505         }
 506       }
 507     }
 508 
 509     if (HAS_PENDING_EXCEPTION) {
 510       CLEAR_PENDING_EXCEPTION;
 511       // None of the field value will be set if there was an exception.
 512       // The java code will not see any of the archived objects in the
 513       // subgraphs referenced from k in this case.
 514       return;
 515     }
 516 
 517     // Load the subgraph entry fields from the record and store them back to
 518     // the corresponding fields within the mirror.
 519     oop m = k->java_mirror();
 520     Array<juint>* entry_field_records = record->entry_field_records();
 521     if (entry_field_records != NULL) {
 522       int efr_len = entry_field_records->length();
 523       assert(efr_len % 3 == 0, "sanity");
 524       for (i = 0; i < efr_len;) {
 525         int field_offset = entry_field_records->at(i);
 526         narrowOop nv = entry_field_records->at(i+1);
 527         int is_closed_archive = entry_field_records->at(i+2);
 528         oop v;
 529         if (is_closed_archive == 0) {
 530           // It's an archived object in the open archive heap regions, not shared.
 531           // The object refereced by the field becomes 'known' by GC from this
 532           // point. All objects in the subgraph reachable from the object are
 533           // also 'known' by GC.
 534           v = materialize_archived_object(nv);
 535         } else {
 536           // Shared object in the closed archive heap regions. Decode directly.
 537           assert(!CompressedOops::is_null(nv), "shared object is null");
 538           v = HeapShared::decode_from_archive(nv);
 539         }
 540         m->obj_field_put(field_offset, v);
 541         i += 3;
 542 
 543         log_debug(cds, heap)("  " PTR_FORMAT " init field @ %2d = " PTR_FORMAT, p2i(k), field_offset, p2i(v));
 544       }
 545 
 546       // Done. Java code can see the archived sub-graphs referenced from k's
 547       // mirror after this point.
 548       if (log_is_enabled(Info, cds, heap)) {
 549         ResourceMark rm;
 550         log_info(cds, heap)("initialize_from_archived_subgraph %s " PTR_FORMAT,
 551                             k->external_name(), p2i(k));
 552       }
 553     }
 554   }
 555 }
 556 
 557 class WalkOopAndArchiveClosure: public BasicOopIterateClosure {
 558   int _level;
 559   bool _is_closed_archive;
 560   bool _record_klasses_only;
 561   KlassSubGraphInfo* _subgraph_info;
 562   oop _orig_referencing_obj;
 563   oop _archived_referencing_obj;
 564   Thread* _thread;
 565  public:
 566   WalkOopAndArchiveClosure(int level,
 567                            bool is_closed_archive,
 568                            bool record_klasses_only,
 569                            KlassSubGraphInfo* subgraph_info,
 570                            oop orig, oop archived, TRAPS) :
 571     _level(level), _is_closed_archive(is_closed_archive),
 572     _record_klasses_only(record_klasses_only),
 573     _subgraph_info(subgraph_info),
 574     _orig_referencing_obj(orig), _archived_referencing_obj(archived),
 575     _thread(THREAD) {}
 576   void do_oop(narrowOop *p) { WalkOopAndArchiveClosure::do_oop_work(p); }
 577   void do_oop(      oop *p) { WalkOopAndArchiveClosure::do_oop_work(p); }
 578 
 579  protected:
 580   template <class T> void do_oop_work(T *p) {
 581     oop obj = RawAccess<>::oop_load(p);
 582     if (!CompressedOops::is_null(obj)) {
 583       assert(!HeapShared::is_archived_object(obj),
 584              "original objects must not point to archived objects");
 585 
 586       size_t field_delta = pointer_delta(p, _orig_referencing_obj, sizeof(char));
 587       T* new_p = (T*)(cast_from_oop<address>(_archived_referencing_obj) + field_delta);
 588       Thread* THREAD = _thread;
 589 
 590       if (!_record_klasses_only && log_is_enabled(Debug, cds, heap)) {
 591         ResourceMark rm;
 592         log_debug(cds, heap)("(%d) %s[" SIZE_FORMAT "] ==> " PTR_FORMAT " size %d %s", _level,
 593                              _orig_referencing_obj->klass()->external_name(), field_delta,
 594                              p2i(obj), obj->size() * HeapWordSize, obj->klass()->external_name());
 595         LogTarget(Trace, cds, heap) log;
 596         LogStream out(log);
 597         obj->print_on(&out);
 598       }
 599 
 600       oop archived = HeapShared::archive_reachable_objects_from(
 601           _level + 1, _subgraph_info, obj, _is_closed_archive, THREAD);
 602       assert(archived != NULL, "VM should have exited with unarchivable objects for _level > 1");
 603       assert(HeapShared::is_archived_object(archived), "must be");
 604 
 605       if (!_record_klasses_only) {
 606         // Update the reference in the archived copy of the referencing object.
 607         log_debug(cds, heap)("(%d) updating oop @[" PTR_FORMAT "] " PTR_FORMAT " ==> " PTR_FORMAT,
 608                              _level, p2i(new_p), p2i(obj), p2i(archived));
 609         RawAccess<IS_NOT_NULL>::oop_store(new_p, archived);
 610       }
 611     }
 612   }
 613 };
 614 
 615 void HeapShared::check_closed_archive_heap_region_object(InstanceKlass* k,
 616                                                          Thread* THREAD) {
 617   // Check fields in the object
 618   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
 619     if (!fs.access_flags().is_static()) {
 620       BasicType ft = fs.field_descriptor().field_type();
 621       if (!fs.access_flags().is_final() && is_reference_type(ft)) {
 622         ResourceMark rm(THREAD);
 623         log_warning(cds, heap)(
 624           "Please check reference field in %s instance in closed archive heap region: %s %s",
 625           k->external_name(), (fs.name())->as_C_string(),
 626           (fs.signature())->as_C_string());
 627       }
 628     }
 629   }
 630 }
 631 
 632 // (1) If orig_obj has not been archived yet, archive it.
 633 // (2) If orig_obj has not been seen yet (since start_recording_subgraph() was called),
 634 //     trace all  objects that are reachable from it, and make sure these objects are archived.
 635 // (3) Record the klasses of all orig_obj and all reachable objects.
 636 oop HeapShared::archive_reachable_objects_from(int level,
 637                                                KlassSubGraphInfo* subgraph_info,
 638                                                oop orig_obj,
 639                                                bool is_closed_archive,
 640                                                TRAPS) {
 641   assert(orig_obj != NULL, "must be");
 642   assert(!is_archived_object(orig_obj), "sanity");
 643 
 644   if (!JavaClasses::is_supported_for_archiving(orig_obj)) {
 645     // This object has injected fields that cannot be supported easily, so we disallow them for now.
 646     // If you get an error here, you probably made a change in the JDK library that has added
 647     // these objects that are referenced (directly or indirectly) by static fields.
 648     ResourceMark rm;
 649     log_error(cds, heap)("Cannot archive object of class %s", orig_obj->klass()->external_name());
 650     vm_exit(1);
 651   }
 652 
 653   // java.lang.Class instances cannot be included in an archived object sub-graph. We only support
 654   // them as Klass::_archived_mirror because they need to be specially restored at run time.
 655   //
 656   // If you get an error here, you probably made a change in the JDK library that has added a Class
 657   // object that is referenced (directly or indirectly) by static fields.
 658   if (java_lang_Class::is_instance(orig_obj)) {
 659     log_error(cds, heap)("(%d) Unknown java.lang.Class object is in the archived sub-graph", level);
 660     vm_exit(1);
 661   }
 662 
 663   oop archived_obj = find_archived_heap_object(orig_obj);
 664   if (java_lang_String::is_instance(orig_obj) && archived_obj != NULL) {
 665     // To save time, don't walk strings that are already archived. They just contain
 666     // pointers to a type array, whose klass doesn't need to be recorded.
 667     return archived_obj;
 668   }
 669 
 670   if (has_been_seen_during_subgraph_recording(orig_obj)) {
 671     // orig_obj has already been archived and traced. Nothing more to do.
 672     return archived_obj;
 673   } else {
 674     set_has_been_seen_during_subgraph_recording(orig_obj);
 675   }
 676 
 677   bool record_klasses_only = (archived_obj != NULL);
 678   if (archived_obj == NULL) {
 679     ++_num_new_archived_objs;
 680     archived_obj = archive_heap_object(orig_obj, THREAD);
 681     if (archived_obj == NULL) {
 682       // Skip archiving the sub-graph referenced from the current entry field.
 683       ResourceMark rm;
 684       log_error(cds, heap)(
 685         "Cannot archive the sub-graph referenced from %s object ("
 686         PTR_FORMAT ") size %d, skipped.",
 687         orig_obj->klass()->external_name(), p2i(orig_obj), orig_obj->size() * HeapWordSize);
 688       if (level == 1) {
 689         // Don't archive a subgraph root that's too big. For archives static fields, that's OK
 690         // as the Java code will take care of initializing this field dynamically.
 691         return NULL;
 692       } else {
 693         // We don't know how to handle an object that has been archived, but some of its reachable
 694         // objects cannot be archived. Bail out for now. We might need to fix this in the future if
 695         // we have a real use case.
 696         vm_exit(1);
 697       }
 698     }
 699   }
 700 
 701   assert(archived_obj != NULL, "must be");
 702   Klass *orig_k = orig_obj->klass();
 703   Klass *relocated_k = archived_obj->klass();
 704   subgraph_info->add_subgraph_object_klass(orig_k, relocated_k);
 705 
 706   WalkOopAndArchiveClosure walker(level, is_closed_archive, record_klasses_only,
 707                                   subgraph_info, orig_obj, archived_obj, THREAD);
 708   orig_obj->oop_iterate(&walker);
 709   if (is_closed_archive && orig_k->is_instance_klass()) {
 710     check_closed_archive_heap_region_object(InstanceKlass::cast(orig_k), THREAD);
 711   }
 712   return archived_obj;
 713 }
 714 
 715 //
 716 // Start from the given static field in a java mirror and archive the
 717 // complete sub-graph of java heap objects that are reached directly
 718 // or indirectly from the starting object by following references.
 719 // Sub-graph archiving restrictions (current):
 720 //
 721 // - All classes of objects in the archived sub-graph (including the
 722 //   entry class) must be boot class only.
 723 // - No java.lang.Class instance (java mirror) can be included inside
 724 //   an archived sub-graph. Mirror can only be the sub-graph entry object.
 725 //
 726 // The Java heap object sub-graph archiving process (see
 727 // WalkOopAndArchiveClosure):
 728 //
 729 // 1) Java object sub-graph archiving starts from a given static field
 730 // within a Class instance (java mirror). If the static field is a
 731 // refererence field and points to a non-null java object, proceed to
 732 // the next step.
 733 //
 734 // 2) Archives the referenced java object. If an archived copy of the
 735 // current object already exists, updates the pointer in the archived
 736 // copy of the referencing object to point to the current archived object.
 737 // Otherwise, proceed to the next step.
 738 //
 739 // 3) Follows all references within the current java object and recursively
 740 // archive the sub-graph of objects starting from each reference.
 741 //
 742 // 4) Updates the pointer in the archived copy of referencing object to
 743 // point to the current archived object.
 744 //
 745 // 5) The Klass of the current java object is added to the list of Klasses
 746 // for loading and initialzing before any object in the archived graph can
 747 // be accessed at runtime.
 748 //
 749 void HeapShared::archive_reachable_objects_from_static_field(InstanceKlass *k,
 750                                                              const char* klass_name,
 751                                                              int field_offset,
 752                                                              const char* field_name,
 753                                                              bool is_closed_archive,
 754                                                              TRAPS) {
 755   assert(DumpSharedSpaces, "dump time only");
 756   assert(k->is_shared_boot_class(), "must be boot class");
 757 
 758   oop m = k->java_mirror();
 759 
 760   KlassSubGraphInfo* subgraph_info = get_subgraph_info(k);
 761   oop f = m->obj_field(field_offset);
 762 
 763   log_debug(cds, heap)("Start archiving from: %s::%s (" PTR_FORMAT ")", klass_name, field_name, p2i(f));
 764 
 765   if (!CompressedOops::is_null(f)) {
 766     if (log_is_enabled(Trace, cds, heap)) {
 767       LogTarget(Trace, cds, heap) log;
 768       LogStream out(log);
 769       f->print_on(&out);
 770     }
 771 
 772     oop af = archive_reachable_objects_from(1, subgraph_info, f,
 773                                             is_closed_archive, CHECK);
 774 
 775     if (af == NULL) {
 776       log_error(cds, heap)("Archiving failed %s::%s (some reachable objects cannot be archived)",
 777                            klass_name, field_name);
 778     } else {
 779       // Note: the field value is not preserved in the archived mirror.
 780       // Record the field as a new subGraph entry point. The recorded
 781       // information is restored from the archive at runtime.
 782       subgraph_info->add_subgraph_entry_field(field_offset, af, is_closed_archive);
 783       log_info(cds, heap)("Archived field %s::%s => " PTR_FORMAT, klass_name, field_name, p2i(af));
 784     }
 785   } else {
 786     // The field contains null, we still need to record the entry point,
 787     // so it can be restored at runtime.
 788     subgraph_info->add_subgraph_entry_field(field_offset, NULL, false);
 789   }
 790 }
 791 
 792 #ifndef PRODUCT
 793 class VerifySharedOopClosure: public BasicOopIterateClosure {
 794  private:
 795   bool _is_archived;
 796 
 797  public:
 798   VerifySharedOopClosure(bool is_archived) : _is_archived(is_archived) {}
 799 
 800   void do_oop(narrowOop *p) { VerifySharedOopClosure::do_oop_work(p); }
 801   void do_oop(      oop *p) { VerifySharedOopClosure::do_oop_work(p); }
 802 
 803  protected:
 804   template <class T> void do_oop_work(T *p) {
 805     oop obj = RawAccess<>::oop_load(p);
 806     if (!CompressedOops::is_null(obj)) {
 807       HeapShared::verify_reachable_objects_from(obj, _is_archived);
 808     }
 809   }
 810 };
 811 
 812 void HeapShared::verify_subgraph_from_static_field(InstanceKlass* k, int field_offset) {
 813   assert(DumpSharedSpaces, "dump time only");
 814   assert(k->is_shared_boot_class(), "must be boot class");
 815 
 816   oop m = k->java_mirror();
 817   oop f = m->obj_field(field_offset);
 818   if (!CompressedOops::is_null(f)) {
 819     verify_subgraph_from(f);
 820   }
 821 }
 822 
 823 void HeapShared::verify_subgraph_from(oop orig_obj) {
 824   oop archived_obj = find_archived_heap_object(orig_obj);
 825   if (archived_obj == NULL) {
 826     // It's OK for the root of a subgraph to be not archived. See comments in
 827     // archive_reachable_objects_from().
 828     return;
 829   }
 830 
 831   // Verify that all objects reachable from orig_obj are archived.
 832   init_seen_objects_table();
 833   verify_reachable_objects_from(orig_obj, false);
 834   delete_seen_objects_table();
 835 
 836   // Note: we could also verify that all objects reachable from the archived
 837   // copy of orig_obj can only point to archived objects, with:
 838   //      init_seen_objects_table();
 839   //      verify_reachable_objects_from(archived_obj, true);
 840   //      init_seen_objects_table();
 841   // but that's already done in G1HeapVerifier::verify_archive_regions so we
 842   // won't do it here.
 843 }
 844 
 845 void HeapShared::verify_reachable_objects_from(oop obj, bool is_archived) {
 846   _num_total_verifications ++;
 847   if (!has_been_seen_during_subgraph_recording(obj)) {
 848     set_has_been_seen_during_subgraph_recording(obj);
 849 
 850     if (is_archived) {
 851       assert(is_archived_object(obj), "must be");
 852       assert(find_archived_heap_object(obj) == NULL, "must be");
 853     } else {
 854       assert(!is_archived_object(obj), "must be");
 855       assert(find_archived_heap_object(obj) != NULL, "must be");
 856     }
 857 
 858     VerifySharedOopClosure walker(is_archived);
 859     obj->oop_iterate(&walker);
 860   }
 861 }
 862 #endif
 863 
 864 HeapShared::SeenObjectsTable* HeapShared::_seen_objects_table = NULL;
 865 int HeapShared::_num_new_walked_objs;
 866 int HeapShared::_num_new_archived_objs;
 867 int HeapShared::_num_old_recorded_klasses;
 868 
 869 int HeapShared::_num_total_subgraph_recordings = 0;
 870 int HeapShared::_num_total_walked_objs = 0;
 871 int HeapShared::_num_total_archived_objs = 0;
 872 int HeapShared::_num_total_recorded_klasses = 0;
 873 int HeapShared::_num_total_verifications = 0;
 874 
 875 bool HeapShared::has_been_seen_during_subgraph_recording(oop obj) {
 876   return _seen_objects_table->get(obj) != NULL;
 877 }
 878 
 879 void HeapShared::set_has_been_seen_during_subgraph_recording(oop obj) {
 880   assert(!has_been_seen_during_subgraph_recording(obj), "sanity");
 881   _seen_objects_table->put(obj, true);
 882   ++ _num_new_walked_objs;
 883 }
 884 
 885 void HeapShared::start_recording_subgraph(InstanceKlass *k, const char* class_name) {
 886   log_info(cds, heap)("Start recording subgraph(s) for archived fields in %s", class_name);
 887   init_seen_objects_table();
 888   _num_new_walked_objs = 0;
 889   _num_new_archived_objs = 0;
 890   _num_old_recorded_klasses = get_subgraph_info(k)->num_subgraph_object_klasses();
 891 }
 892 
 893 void HeapShared::done_recording_subgraph(InstanceKlass *k, const char* class_name) {
 894   int num_new_recorded_klasses = get_subgraph_info(k)->num_subgraph_object_klasses() -
 895     _num_old_recorded_klasses;
 896   log_info(cds, heap)("Done recording subgraph(s) for archived fields in %s: "
 897                       "walked %d objs, archived %d new objs, recorded %d classes",
 898                       class_name, _num_new_walked_objs, _num_new_archived_objs,
 899                       num_new_recorded_klasses);
 900 
 901   delete_seen_objects_table();
 902 
 903   _num_total_subgraph_recordings ++;
 904   _num_total_walked_objs      += _num_new_walked_objs;
 905   _num_total_archived_objs    += _num_new_archived_objs;
 906   _num_total_recorded_klasses +=  num_new_recorded_klasses;
 907 }
 908 
 909 class ArchivableStaticFieldFinder: public FieldClosure {
 910   InstanceKlass* _ik;
 911   Symbol* _field_name;
 912   bool _found;
 913   int _offset;
 914 public:
 915   ArchivableStaticFieldFinder(InstanceKlass* ik, Symbol* field_name) :
 916     _ik(ik), _field_name(field_name), _found(false), _offset(-1) {}
 917 
 918   virtual void do_field(fieldDescriptor* fd) {
 919     if (fd->name() == _field_name) {
 920       assert(!_found, "fields cannot be overloaded");
 921       assert(is_reference_type(fd->field_type()), "can archive only fields that are references");
 922       _found = true;
 923       _offset = fd->offset();
 924     }
 925   }
 926   bool found()     { return _found;  }
 927   int offset()     { return _offset; }
 928 };
 929 
 930 void HeapShared::init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[],
 931                                             int num, Thread* THREAD) {
 932   for (int i = 0; i < num; i++) {
 933     ArchivableStaticFieldInfo* info = &fields[i];
 934     TempNewSymbol klass_name =  SymbolTable::new_symbol(info->klass_name);
 935     TempNewSymbol field_name =  SymbolTable::new_symbol(info->field_name);
 936 
 937     Klass* k = SystemDictionary::resolve_or_null(klass_name, THREAD);
 938     assert(k != NULL && !HAS_PENDING_EXCEPTION, "class must exist");
 939     InstanceKlass* ik = InstanceKlass::cast(k);
 940     assert(InstanceKlass::cast(ik)->is_shared_boot_class(),
 941            "Only support boot classes");
 942     ik->initialize(THREAD);
 943     guarantee(!HAS_PENDING_EXCEPTION, "exception in initialize");
 944 
 945     ArchivableStaticFieldFinder finder(ik, field_name);
 946     ik->do_local_static_fields(&finder);
 947     assert(finder.found(), "field must exist");
 948 
 949     info->klass = ik;
 950     info->offset = finder.offset();
 951   }
 952 }
 953 
 954 void HeapShared::init_subgraph_entry_fields(Thread* THREAD) {
 955   _dump_time_subgraph_info_table = new (ResourceObj::C_HEAP, mtClass)DumpTimeKlassSubGraphInfoTable();
 956 
 957   init_subgraph_entry_fields(closed_archive_subgraph_entry_fields,
 958                              num_closed_archive_subgraph_entry_fields,
 959                              THREAD);
 960   init_subgraph_entry_fields(open_archive_subgraph_entry_fields,
 961                              num_open_archive_subgraph_entry_fields,
 962                              THREAD);
 963 }
 964 
 965 void HeapShared::archive_object_subgraphs(ArchivableStaticFieldInfo fields[],
 966                                           int num, bool is_closed_archive,
 967                                           Thread* THREAD) {
 968   _num_total_subgraph_recordings = 0;
 969   _num_total_walked_objs = 0;
 970   _num_total_archived_objs = 0;
 971   _num_total_recorded_klasses = 0;
 972   _num_total_verifications = 0;
 973 
 974   // For each class X that has one or more archived fields:
 975   // [1] Dump the subgraph of each archived field
 976   // [2] Create a list of all the class of the objects that can be reached
 977   //     by any of these static fields.
 978   //     At runtime, these classes are initialized before X's archived fields
 979   //     are restored by HeapShared::initialize_from_archived_subgraph().
 980   int i;
 981   for (i = 0; i < num; ) {
 982     ArchivableStaticFieldInfo* info = &fields[i];
 983     const char* klass_name = info->klass_name;
 984     start_recording_subgraph(info->klass, klass_name);
 985 
 986     // If you have specified consecutive fields of the same klass in
 987     // fields[], these will be archived in the same
 988     // {start_recording_subgraph ... done_recording_subgraph} pass to
 989     // save time.
 990     for (; i < num; i++) {
 991       ArchivableStaticFieldInfo* f = &fields[i];
 992       if (f->klass_name != klass_name) {
 993         break;
 994       }
 995       archive_reachable_objects_from_static_field(f->klass, f->klass_name,
 996                                                   f->offset, f->field_name,
 997                                                   is_closed_archive, CHECK);
 998     }
 999     done_recording_subgraph(info->klass, klass_name);
1000   }
1001 
1002   log_info(cds, heap)("Archived subgraph records in %s archive heap region = %d",
1003                       is_closed_archive ? "closed" : "open",
1004                       _num_total_subgraph_recordings);
1005   log_info(cds, heap)("  Walked %d objects", _num_total_walked_objs);
1006   log_info(cds, heap)("  Archived %d objects", _num_total_archived_objs);
1007   log_info(cds, heap)("  Recorded %d klasses", _num_total_recorded_klasses);
1008 
1009 #ifndef PRODUCT
1010   for (int i = 0; i < num; i++) {
1011     ArchivableStaticFieldInfo* f = &fields[i];
1012     verify_subgraph_from_static_field(f->klass, f->offset);
1013   }
1014   log_info(cds, heap)("  Verified %d references", _num_total_verifications);
1015 #endif
1016 }
1017 
1018 // At dump-time, find the location of all the non-null oop pointers in an archived heap
1019 // region. This way we can quickly relocate all the pointers without using
1020 // BasicOopIterateClosure at runtime.
1021 class FindEmbeddedNonNullPointers: public BasicOopIterateClosure {
1022   narrowOop* _start;
1023   BitMap *_oopmap;
1024   int _num_total_oops;
1025   int _num_null_oops;
1026  public:
1027   FindEmbeddedNonNullPointers(narrowOop* start, BitMap* oopmap)
1028     : _start(start), _oopmap(oopmap), _num_total_oops(0),  _num_null_oops(0) {}
1029 
1030   virtual bool should_verify_oops(void) {
1031     return false;
1032   }
1033   virtual void do_oop(narrowOop* p) {
1034     _num_total_oops ++;
1035     narrowOop v = *p;
1036     if (!CompressedOops::is_null(v)) {
1037       size_t idx = p - _start;
1038       _oopmap->set_bit(idx);
1039     } else {
1040       _num_null_oops ++;
1041     }
1042   }
1043   virtual void do_oop(oop *p) {
1044     ShouldNotReachHere();
1045   }
1046   int num_total_oops() const { return _num_total_oops; }
1047   int num_null_oops()  const { return _num_null_oops; }
1048 };
1049 
1050 ResourceBitMap HeapShared::calculate_oopmap(MemRegion region) {
1051   assert(UseCompressedOops, "must be");
1052   size_t num_bits = region.byte_size() / sizeof(narrowOop);
1053   ResourceBitMap oopmap(num_bits);
1054 
1055   HeapWord* p   = region.start();
1056   HeapWord* end = region.end();
1057   FindEmbeddedNonNullPointers finder((narrowOop*)p, &oopmap);
1058 
1059   int num_objs = 0;
1060   while (p < end) {
1061     oop o = (oop)p;
1062     o->oop_iterate(&finder);
1063     p += o->size();
1064     ++ num_objs;
1065   }
1066 
1067   log_info(cds, heap)("calculate_oopmap: objects = %6d, embedded oops = %7d, nulls = %7d",
1068                       num_objs, finder.num_total_oops(), finder.num_null_oops());
1069   return oopmap;
1070 }
1071 
1072 // Patch all the embedded oop pointers inside an archived heap region,
1073 // to be consistent with the runtime oop encoding.
1074 class PatchEmbeddedPointers: public BitMapClosure {
1075   narrowOop* _start;
1076 
1077  public:
1078   PatchEmbeddedPointers(narrowOop* start) : _start(start) {}
1079 
1080   bool do_bit(size_t offset) {
1081     narrowOop* p = _start + offset;
1082     narrowOop v = *p;
1083     assert(!CompressedOops::is_null(v), "null oops should have been filtered out at dump time");
1084     oop o = HeapShared::decode_from_archive(v);
1085     RawAccess<IS_NOT_NULL>::oop_store(p, o);
1086     return true;
1087   }
1088 };
1089 
1090 void HeapShared::patch_archived_heap_embedded_pointers(MemRegion region, address oopmap,
1091                                                        size_t oopmap_size_in_bits) {
1092   BitMapView bm((BitMap::bm_word_t*)oopmap, oopmap_size_in_bits);
1093 
1094 #ifndef PRODUCT
1095   ResourceMark rm;
1096   ResourceBitMap checkBm = calculate_oopmap(region);
1097   assert(bm.is_same(checkBm), "sanity");
1098 #endif
1099 
1100   PatchEmbeddedPointers patcher((narrowOop*)region.start());
1101   bm.iterate(&patcher);
1102 }
1103 
1104 #endif // INCLUDE_CDS_JAVA_HEAP