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