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