1 /*
   2  * Copyright (c) 2012, 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 "jvm.h"
  27 #include "classfile/classLoaderDataGraph.hpp"
  28 #include "classfile/classListParser.hpp"
  29 #include "classfile/classLoaderExt.hpp"
  30 #include "classfile/dictionary.hpp"
  31 #include "classfile/loaderConstraints.hpp"
  32 #include "classfile/placeholders.hpp"
  33 #include "classfile/symbolTable.hpp"
  34 #include "classfile/stringTable.hpp"
  35 #include "classfile/systemDictionary.hpp"
  36 #include "classfile/systemDictionaryShared.hpp"
  37 #include "code/codeCache.hpp"
  38 #include "interpreter/bytecodeStream.hpp"
  39 #include "interpreter/bytecodes.hpp"
  40 #include "logging/log.hpp"
  41 #include "logging/logMessage.hpp"
  42 #include "memory/filemap.hpp"
  43 #include "memory/heapShared.inline.hpp"
  44 #include "memory/metaspace.hpp"
  45 #include "memory/metaspaceClosure.hpp"
  46 #include "memory/metaspaceShared.hpp"
  47 #include "memory/resourceArea.hpp"
  48 #include "oops/compressedOops.inline.hpp"
  49 #include "oops/instanceClassLoaderKlass.hpp"
  50 #include "oops/instanceMirrorKlass.hpp"
  51 #include "oops/instanceRefKlass.hpp"
  52 #include "oops/objArrayKlass.hpp"
  53 #include "oops/objArrayOop.hpp"
  54 #include "oops/oop.inline.hpp"
  55 #include "oops/typeArrayKlass.hpp"
  56 #include "prims/jvmtiRedefineClasses.hpp"
  57 #include "runtime/handles.inline.hpp"
  58 #include "runtime/os.hpp"
  59 #include "runtime/safepointVerifiers.hpp"
  60 #include "runtime/signature.hpp"
  61 #include "runtime/timerTrace.hpp"
  62 #include "runtime/vmThread.hpp"
  63 #include "runtime/vm_operations.hpp"
  64 #include "utilities/align.hpp"
  65 #include "utilities/bitMap.hpp"
  66 #include "utilities/defaultStream.hpp"
  67 #include "utilities/hashtable.inline.hpp"
  68 #if INCLUDE_G1GC
  69 #include "gc/g1/g1CollectedHeap.hpp"
  70 #endif
  71 
  72 ReservedSpace MetaspaceShared::_shared_rs;
  73 VirtualSpace MetaspaceShared::_shared_vs;
  74 MetaspaceSharedStats MetaspaceShared::_stats;
  75 bool MetaspaceShared::_has_error_classes;
  76 bool MetaspaceShared::_archive_loading_failed = false;
  77 bool MetaspaceShared::_remapped_readwrite = false;
  78 address MetaspaceShared::_cds_i2i_entry_code_buffers = NULL;
  79 size_t MetaspaceShared::_cds_i2i_entry_code_buffers_size = 0;
  80 size_t MetaspaceShared::_core_spaces_size = 0;
  81 
  82 // The CDS archive is divided into the following regions:
  83 //     mc  - misc code (the method entry trampolines)
  84 //     rw  - read-write metadata
  85 //     ro  - read-only metadata and read-only tables
  86 //     md  - misc data (the c++ vtables)
  87 //     od  - optional data (original class files)
  88 //
  89 //     ca0 - closed archive heap space #0
  90 //     ca1 - closed archive heap space #1 (may be empty)
  91 //     oa0 - open archive heap space #0
  92 //     oa1 - open archive heap space #1 (may be empty)
  93 //
  94 // The mc, rw, ro, md and od regions are linearly allocated, starting from
  95 // SharedBaseAddress, in the order of mc->rw->ro->md->od. The size of these 5 regions
  96 // are page-aligned, and there's no gap between any consecutive regions.
  97 //
  98 // These 5 regions are populated in the following steps:
  99 // [1] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
 100 //     temporarily allocated outside of the shared regions. Only the method entry
 101 //     trampolines are written into the mc region.
 102 // [2] ArchiveCompactor copies RW metadata into the rw region.
 103 // [3] ArchiveCompactor copies RO metadata into the ro region.
 104 // [4] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
 105 //     are copied into the ro region as read-only tables.
 106 // [5] C++ vtables are copied into the md region.
 107 // [6] Original class files are copied into the od region.
 108 //
 109 // The s0/s1 and oa0/oa1 regions are populated inside HeapShared::archive_java_heap_objects.
 110 // Their layout is independent of the other 5 regions.
 111 
 112 class DumpRegion {
 113 private:
 114   const char* _name;
 115   char* _base;
 116   char* _top;
 117   char* _end;
 118   bool _is_packed;
 119 
 120   char* expand_top_to(char* newtop) {
 121     assert(is_allocatable(), "must be initialized and not packed");
 122     assert(newtop >= _top, "must not grow backwards");
 123     if (newtop > _end) {
 124       MetaspaceShared::report_out_of_space(_name, newtop - _top);
 125       ShouldNotReachHere();
 126     }
 127     uintx delta = MetaspaceShared::object_delta_uintx(newtop);
 128     if (delta > MAX_SHARED_DELTA) {
 129       // This is just a sanity check and should not appear in any real world usage. This
 130       // happens only if you allocate more than 2GB of shared objects and would require
 131       // millions of shared classes.
 132       vm_exit_during_initialization("Out of memory in the CDS archive",
 133                                     "Please reduce the number of shared classes.");
 134     }
 135 
 136     MetaspaceShared::commit_shared_space_to(newtop);
 137     _top = newtop;
 138     return _top;
 139   }
 140 
 141 public:
 142   DumpRegion(const char* name) : _name(name), _base(NULL), _top(NULL), _end(NULL), _is_packed(false) {}
 143 
 144   char* allocate(size_t num_bytes, size_t alignment=BytesPerWord) {
 145     char* p = (char*)align_up(_top, alignment);
 146     char* newtop = p + align_up(num_bytes, alignment);
 147     expand_top_to(newtop);
 148     memset(p, 0, newtop - p);
 149     return p;
 150   }
 151 
 152   void append_intptr_t(intptr_t n) {
 153     assert(is_aligned(_top, sizeof(intptr_t)), "bad alignment");
 154     intptr_t *p = (intptr_t*)_top;
 155     char* newtop = _top + sizeof(intptr_t);
 156     expand_top_to(newtop);
 157     *p = n;
 158   }
 159 
 160   char* base()      const { return _base;        }
 161   char* top()       const { return _top;         }
 162   char* end()       const { return _end;         }
 163   size_t reserved() const { return _end - _base; }
 164   size_t used()     const { return _top - _base; }
 165   bool is_packed()  const { return _is_packed;   }
 166   bool is_allocatable() const {
 167     return !is_packed() && _base != NULL;
 168   }
 169 
 170   void print(size_t total_bytes) const {
 171     tty->print_cr("%-3s space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used] at " INTPTR_FORMAT,
 172                   _name, used(), percent_of(used(), total_bytes), reserved(), percent_of(used(), reserved()), p2i(_base));
 173   }
 174   void print_out_of_space_msg(const char* failing_region, size_t needed_bytes) {
 175     tty->print("[%-8s] " PTR_FORMAT " - " PTR_FORMAT " capacity =%9d, allocated =%9d",
 176                _name, p2i(_base), p2i(_top), int(_end - _base), int(_top - _base));
 177     if (strcmp(_name, failing_region) == 0) {
 178       tty->print_cr(" required = %d", int(needed_bytes));
 179     } else {
 180       tty->cr();
 181     }
 182   }
 183 
 184   void init(const ReservedSpace* rs) {
 185     _base = _top = rs->base();
 186     _end = rs->end();
 187   }
 188   void init(char* b, char* t, char* e) {
 189     _base = b;
 190     _top = t;
 191     _end = e;
 192   }
 193 
 194   void pack(DumpRegion* next = NULL) {
 195     assert(!is_packed(), "sanity");
 196     _end = (char*)align_up(_top, Metaspace::reserve_alignment());
 197     _is_packed = true;
 198     if (next != NULL) {
 199       next->_base = next->_top = this->_end;
 200       next->_end = MetaspaceShared::shared_rs()->end();
 201     }
 202   }
 203   bool contains(char* p) {
 204     return base() <= p && p < top();
 205   }
 206 };
 207 
 208 
 209 DumpRegion _mc_region("mc"), _ro_region("ro"), _rw_region("rw"), _md_region("md"), _od_region("od");
 210 size_t _total_closed_archive_region_size = 0, _total_open_archive_region_size = 0;
 211 
 212 char* MetaspaceShared::misc_code_space_alloc(size_t num_bytes) {
 213   return _mc_region.allocate(num_bytes);
 214 }
 215 
 216 char* MetaspaceShared::read_only_space_alloc(size_t num_bytes) {
 217   return _ro_region.allocate(num_bytes);
 218 }
 219 
 220 char* MetaspaceShared::read_only_space_top() {
 221   return _ro_region.top();
 222 }
 223 
 224 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
 225   assert(UseSharedSpaces, "Must be called when UseSharedSpaces is enabled");
 226 
 227   // If using shared space, open the file that contains the shared space
 228   // and map in the memory before initializing the rest of metaspace (so
 229   // the addresses don't conflict)
 230   address cds_address = NULL;
 231   FileMapInfo* mapinfo = new FileMapInfo();
 232 
 233   // Open the shared archive file, read and validate the header. If
 234   // initialization fails, shared spaces [UseSharedSpaces] are
 235   // disabled and the file is closed.
 236   // Map in spaces now also
 237   if (mapinfo->initialize() && map_shared_spaces(mapinfo)) {
 238     size_t cds_total = core_spaces_size();
 239     cds_address = (address)mapinfo->region_addr(0);
 240 #ifdef _LP64
 241     if (Metaspace::using_class_space()) {
 242       char* cds_end = (char*)(cds_address + cds_total);
 243       cds_end = (char *)align_up(cds_end, Metaspace::reserve_alignment());
 244       // If UseCompressedClassPointers is set then allocate the metaspace area
 245       // above the heap and above the CDS area (if it exists).
 246       Metaspace::allocate_metaspace_compressed_klass_ptrs(cds_end, cds_address);
 247       // map_heap_regions() compares the current narrow oop and klass encodings
 248       // with the archived ones, so it must be done after all encodings are determined.
 249       mapinfo->map_heap_regions();
 250     }
 251     Universe::set_narrow_klass_range(CompressedClassSpaceSize);
 252 #endif // _LP64
 253   } else {
 254     assert(!mapinfo->is_open() && !UseSharedSpaces,
 255            "archive file not closed or shared spaces not disabled.");
 256   }
 257 }
 258 
 259 void MetaspaceShared::initialize_dumptime_shared_and_meta_spaces() {
 260   assert(DumpSharedSpaces, "should be called for dump time only");
 261   const size_t reserve_alignment = Metaspace::reserve_alignment();
 262   bool large_pages = false; // No large pages when dumping the CDS archive.
 263   char* shared_base = (char*)align_up((char*)SharedBaseAddress, reserve_alignment);
 264 
 265 #ifdef _LP64
 266   // On 64-bit VM, the heap and class space layout will be the same as if
 267   // you're running in -Xshare:on mode:
 268   //
 269   //                              +-- SharedBaseAddress (default = 0x800000000)
 270   //                              v
 271   // +-..---------+---------+ ... +----+----+----+----+----+---------------+
 272   // |    Heap    | Archive |     | MC | RW | RO | MD | OD | class space   |
 273   // +-..---------+---------+ ... +----+----+----+----+----+---------------+
 274   // |<--   MaxHeapSize  -->|     |<-- UnscaledClassSpaceMax = 4GB ------->|
 275   //
 276   const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
 277   const size_t cds_total = align_down(UnscaledClassSpaceMax, reserve_alignment);
 278 #else
 279   // We don't support archives larger than 256MB on 32-bit due to limited virtual address space.
 280   size_t cds_total = align_down(256*M, reserve_alignment);
 281 #endif
 282 
 283   // First try to reserve the space at the specified SharedBaseAddress.
 284   _shared_rs = ReservedSpace(cds_total, reserve_alignment, large_pages, shared_base);
 285   if (_shared_rs.is_reserved()) {
 286     assert(shared_base == 0 || _shared_rs.base() == shared_base, "should match");
 287   } else {
 288     // Get a mmap region anywhere if the SharedBaseAddress fails.
 289     _shared_rs = ReservedSpace(cds_total, reserve_alignment, large_pages);
 290   }
 291   if (!_shared_rs.is_reserved()) {
 292     vm_exit_during_initialization("Unable to reserve memory for shared space",
 293                                   err_msg(SIZE_FORMAT " bytes.", cds_total));
 294   }
 295 
 296 #ifdef _LP64
 297   // During dump time, we allocate 4GB (UnscaledClassSpaceMax) of space and split it up:
 298   // + The upper 1 GB is used as the "temporary compressed class space" -- preload_classes()
 299   //   will store Klasses into this space.
 300   // + The lower 3 GB is used for the archive -- when preload_classes() is done,
 301   //   ArchiveCompactor will copy the class metadata into this space, first the RW parts,
 302   //   then the RO parts.
 303 
 304   assert(UseCompressedOops && UseCompressedClassPointers,
 305       "UseCompressedOops and UseCompressedClassPointers must be set");
 306 
 307   size_t max_archive_size = align_down(cds_total * 3 / 4, reserve_alignment);
 308   ReservedSpace tmp_class_space = _shared_rs.last_part(max_archive_size);
 309   CompressedClassSpaceSize = align_down(tmp_class_space.size(), reserve_alignment);
 310   _shared_rs = _shared_rs.first_part(max_archive_size);
 311 
 312   // Set up compress class pointers.
 313   Universe::set_narrow_klass_base((address)_shared_rs.base());
 314   // Set narrow_klass_shift to be LogKlassAlignmentInBytes. This is consistent
 315   // with AOT.
 316   Universe::set_narrow_klass_shift(LogKlassAlignmentInBytes);
 317   // Set the range of klass addresses to 4GB.
 318   Universe::set_narrow_klass_range(cds_total);
 319 
 320   Metaspace::initialize_class_space(tmp_class_space);
 321   log_info(cds)("narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
 322                 p2i(Universe::narrow_klass_base()), Universe::narrow_klass_shift());
 323 
 324   log_info(cds)("Allocated temporary class space: " SIZE_FORMAT " bytes at " PTR_FORMAT,
 325                 CompressedClassSpaceSize, p2i(tmp_class_space.base()));
 326 #endif
 327 
 328   // Start with 0 committed bytes. The memory will be committed as needed by
 329   // MetaspaceShared::commit_shared_space_to().
 330   if (!_shared_vs.initialize(_shared_rs, 0)) {
 331     vm_exit_during_initialization("Unable to allocate memory for shared space");
 332   }
 333 
 334   _mc_region.init(&_shared_rs);
 335   SharedBaseAddress = (size_t)_shared_rs.base();
 336   tty->print_cr("Allocated shared space: " SIZE_FORMAT " bytes at " PTR_FORMAT,
 337                 _shared_rs.size(), p2i(_shared_rs.base()));
 338 }
 339 
 340 // Called by universe_post_init()
 341 void MetaspaceShared::post_initialize(TRAPS) {
 342   if (UseSharedSpaces) {
 343     int size = FileMapInfo::get_number_of_shared_paths();
 344     if (size > 0) {
 345       SystemDictionaryShared::allocate_shared_data_arrays(size, THREAD);
 346       FileMapHeader* header = FileMapInfo::current_info()->header();
 347       ClassLoaderExt::init_paths_start_index(header->_app_class_paths_start_index);
 348       ClassLoaderExt::init_app_module_paths_start_index(header->_app_module_paths_start_index);
 349     }
 350   }
 351 }
 352 
 353 void MetaspaceShared::read_extra_data(const char* filename, TRAPS) {
 354   HashtableTextDump reader(filename);
 355   reader.check_version("VERSION: 1.0");
 356 
 357   while (reader.remain() > 0) {
 358     int utf8_length;
 359     int prefix_type = reader.scan_prefix(&utf8_length);
 360     ResourceMark rm(THREAD);
 361     char* utf8_buffer = NEW_RESOURCE_ARRAY(char, utf8_length);
 362     reader.get_utf8(utf8_buffer, utf8_length);
 363 
 364     if (prefix_type == HashtableTextDump::SymbolPrefix) {
 365       SymbolTable::new_symbol(utf8_buffer, utf8_length, THREAD);
 366     } else{
 367       assert(prefix_type == HashtableTextDump::StringPrefix, "Sanity");
 368       utf8_buffer[utf8_length] = '\0';
 369       oop s = StringTable::intern(utf8_buffer, THREAD);
 370     }
 371   }
 372 }
 373 
 374 void MetaspaceShared::commit_shared_space_to(char* newtop) {
 375   assert(DumpSharedSpaces, "dump-time only");
 376   char* base = _shared_rs.base();
 377   size_t need_committed_size = newtop - base;
 378   size_t has_committed_size = _shared_vs.committed_size();
 379   if (need_committed_size < has_committed_size) {
 380     return;
 381   }
 382 
 383   size_t min_bytes = need_committed_size - has_committed_size;
 384   size_t preferred_bytes = 1 * M;
 385   size_t uncommitted = _shared_vs.reserved_size() - has_committed_size;
 386 
 387   size_t commit = MAX2(min_bytes, preferred_bytes);
 388   assert(commit <= uncommitted, "sanity");
 389 
 390   bool result = _shared_vs.expand_by(commit, false);
 391   if (!result) {
 392     vm_exit_during_initialization(err_msg("Failed to expand shared space to " SIZE_FORMAT " bytes",
 393                                           need_committed_size));
 394   }
 395 
 396   log_info(cds)("Expanding shared spaces by " SIZE_FORMAT_W(7) " bytes [total " SIZE_FORMAT_W(9)  " bytes ending at %p]",
 397                 commit, _shared_vs.actual_committed_size(), _shared_vs.high());
 398 }
 399 
 400 // Read/write a data stream for restoring/preserving metadata pointers and
 401 // miscellaneous data from/to the shared archive file.
 402 
 403 void MetaspaceShared::serialize(SerializeClosure* soc) {
 404   int tag = 0;
 405   soc->do_tag(--tag);
 406 
 407   // Verify the sizes of various metadata in the system.
 408   soc->do_tag(sizeof(Method));
 409   soc->do_tag(sizeof(ConstMethod));
 410   soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE));
 411   soc->do_tag(sizeof(ConstantPool));
 412   soc->do_tag(sizeof(ConstantPoolCache));
 413   soc->do_tag(objArrayOopDesc::base_offset_in_bytes());
 414   soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE));
 415   soc->do_tag(sizeof(Symbol));
 416 
 417   // Dump/restore miscellaneous metadata.
 418   Universe::serialize(soc);
 419   soc->do_tag(--tag);
 420 
 421   // Dump/restore references to commonly used names and signatures.
 422   vmSymbols::serialize(soc);
 423   soc->do_tag(--tag);
 424 
 425   // Dump/restore the symbol/string/subgraph_info tables
 426   SymbolTable::serialize_shared_table_header(soc);
 427   StringTable::serialize_shared_table_header(soc);
 428   HeapShared::serialize_subgraph_info_table_header(soc);
 429   SystemDictionaryShared::serialize_dictionary_headers(soc);
 430 
 431   JavaClasses::serialize_offsets(soc);
 432   InstanceMirrorKlass::serialize_offsets(soc);
 433   soc->do_tag(--tag);
 434 
 435   soc->do_tag(666);
 436 }
 437 
 438 address MetaspaceShared::cds_i2i_entry_code_buffers(size_t total_size) {
 439   if (DumpSharedSpaces) {
 440     if (_cds_i2i_entry_code_buffers == NULL) {
 441       _cds_i2i_entry_code_buffers = (address)misc_code_space_alloc(total_size);
 442       _cds_i2i_entry_code_buffers_size = total_size;
 443     }
 444   } else if (UseSharedSpaces) {
 445     assert(_cds_i2i_entry_code_buffers != NULL, "must already been initialized");
 446   } else {
 447     return NULL;
 448   }
 449 
 450   assert(_cds_i2i_entry_code_buffers_size == total_size, "must not change");
 451   return _cds_i2i_entry_code_buffers;
 452 }
 453 
 454 // CDS code for dumping shared archive.
 455 
 456 // Global object for holding classes that have been loaded.  Since this
 457 // is run at a safepoint just before exit, this is the entire set of classes.
 458 static GrowableArray<Klass*>* _global_klass_objects;
 459 
 460 GrowableArray<Klass*>* MetaspaceShared::collected_klasses() {
 461   return _global_klass_objects;
 462 }
 463 
 464 static void collect_array_classes(Klass* k) {
 465   _global_klass_objects->append_if_missing(k);
 466   if (k->is_array_klass()) {
 467     // Add in the array classes too
 468     ArrayKlass* ak = ArrayKlass::cast(k);
 469     Klass* h = ak->higher_dimension();
 470     if (h != NULL) {
 471       h->array_klasses_do(collect_array_classes);
 472     }
 473   }
 474 }
 475 
 476 class CollectClassesClosure : public KlassClosure {
 477   void do_klass(Klass* k) {
 478     if (k->is_instance_klass() &&
 479         SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(k))) {
 480       // Don't add to the _global_klass_objects
 481     } else {
 482       _global_klass_objects->append_if_missing(k);
 483     }
 484     if (k->is_array_klass()) {
 485       // Add in the array classes too
 486       ArrayKlass* ak = ArrayKlass::cast(k);
 487       Klass* h = ak->higher_dimension();
 488       if (h != NULL) {
 489         h->array_klasses_do(collect_array_classes);
 490       }
 491     }
 492   }
 493 };
 494 
 495 static void remove_unshareable_in_classes() {
 496   for (int i = 0; i < _global_klass_objects->length(); i++) {
 497     Klass* k = _global_klass_objects->at(i);
 498     if (!k->is_objArray_klass()) {
 499       // InstanceKlass and TypeArrayKlass will in turn call remove_unshareable_info
 500       // on their array classes.
 501       assert(k->is_instance_klass() || k->is_typeArray_klass(), "must be");
 502       k->remove_unshareable_info();
 503     }
 504   }
 505 }
 506 
 507 static void remove_java_mirror_in_classes() {
 508   for (int i = 0; i < _global_klass_objects->length(); i++) {
 509     Klass* k = _global_klass_objects->at(i);
 510     if (!k->is_objArray_klass()) {
 511       // InstanceKlass and TypeArrayKlass will in turn call remove_unshareable_info
 512       // on their array classes.
 513       assert(k->is_instance_klass() || k->is_typeArray_klass(), "must be");
 514       k->remove_java_mirror();
 515     }
 516   }
 517 }
 518 
 519 static void clear_basic_type_mirrors() {
 520   assert(!HeapShared::is_heap_object_archiving_allowed(), "Sanity");
 521   Universe::set_int_mirror(NULL);
 522   Universe::set_float_mirror(NULL);
 523   Universe::set_double_mirror(NULL);
 524   Universe::set_byte_mirror(NULL);
 525   Universe::set_bool_mirror(NULL);
 526   Universe::set_char_mirror(NULL);
 527   Universe::set_long_mirror(NULL);
 528   Universe::set_short_mirror(NULL);
 529   Universe::set_void_mirror(NULL);
 530 }
 531 
 532 static void rewrite_nofast_bytecode(Method* method) {
 533   BytecodeStream bcs(method);
 534   while (!bcs.is_last_bytecode()) {
 535     Bytecodes::Code opcode = bcs.next();
 536     switch (opcode) {
 537     case Bytecodes::_getfield:      *bcs.bcp() = Bytecodes::_nofast_getfield;      break;
 538     case Bytecodes::_putfield:      *bcs.bcp() = Bytecodes::_nofast_putfield;      break;
 539     case Bytecodes::_aload_0:       *bcs.bcp() = Bytecodes::_nofast_aload_0;       break;
 540     case Bytecodes::_iload: {
 541       if (!bcs.is_wide()) {
 542         *bcs.bcp() = Bytecodes::_nofast_iload;
 543       }
 544       break;
 545     }
 546     default: break;
 547     }
 548   }
 549 }
 550 
 551 // Walk all methods in the class list to ensure that they won't be modified at
 552 // run time. This includes:
 553 // [1] Rewrite all bytecodes as needed, so that the ConstMethod* will not be modified
 554 //     at run time by RewriteBytecodes/RewriteFrequentPairs
 555 // [2] Assign a fingerprint, so one doesn't need to be assigned at run-time.
 556 static void rewrite_nofast_bytecodes_and_calculate_fingerprints() {
 557   for (int i = 0; i < _global_klass_objects->length(); i++) {
 558     Klass* k = _global_klass_objects->at(i);
 559     if (k->is_instance_klass()) {
 560       InstanceKlass* ik = InstanceKlass::cast(k);
 561       for (int i = 0; i < ik->methods()->length(); i++) {
 562         Method* m = ik->methods()->at(i);
 563         rewrite_nofast_bytecode(m);
 564         Fingerprinter fp(m);
 565         // The side effect of this call sets method's fingerprint field.
 566         fp.fingerprint();
 567       }
 568     }
 569   }
 570 }
 571 
 572 static void relocate_cached_class_file() {
 573   for (int i = 0; i < _global_klass_objects->length(); i++) {
 574     Klass* k = _global_klass_objects->at(i);
 575     if (k->is_instance_klass()) {
 576       InstanceKlass* ik = InstanceKlass::cast(k);
 577       JvmtiCachedClassFileData* p = ik->get_archived_class_data();
 578       if (p != NULL) {
 579         int size = offset_of(JvmtiCachedClassFileData, data) + p->length;
 580         JvmtiCachedClassFileData* q = (JvmtiCachedClassFileData*)_od_region.allocate(size);
 581         q->length = p->length;
 582         memcpy(q->data, p->data, p->length);
 583         ik->set_archived_class_data(q);
 584       }
 585     }
 586   }
 587 }
 588 
 589 // Objects of the Metadata types (such as Klass and ConstantPool) have C++ vtables.
 590 // (In GCC this is the field <Type>::_vptr, i.e., first word in the object.)
 591 //
 592 // Addresses of the vtables and the methods may be different across JVM runs,
 593 // if libjvm.so is dynamically loaded at a different base address.
 594 //
 595 // To ensure that the Metadata objects in the CDS archive always have the correct vtable:
 596 //
 597 // + at dump time:  we redirect the _vptr to point to our own vtables inside
 598 //                  the CDS image
 599 // + at run time:   we clone the actual contents of the vtables from libjvm.so
 600 //                  into our own tables.
 601 
 602 // Currently, the archive contain ONLY the following types of objects that have C++ vtables.
 603 #define CPP_VTABLE_PATCH_TYPES_DO(f) \
 604   f(ConstantPool) \
 605   f(InstanceKlass) \
 606   f(InstanceClassLoaderKlass) \
 607   f(InstanceMirrorKlass) \
 608   f(InstanceRefKlass) \
 609   f(Method) \
 610   f(ObjArrayKlass) \
 611   f(TypeArrayKlass)
 612 
 613 class CppVtableInfo {
 614   intptr_t _vtable_size;
 615   intptr_t _cloned_vtable[1];
 616 public:
 617   static int num_slots(int vtable_size) {
 618     return 1 + vtable_size; // Need to add the space occupied by _vtable_size;
 619   }
 620   int vtable_size()           { return int(uintx(_vtable_size)); }
 621   void set_vtable_size(int n) { _vtable_size = intptr_t(n); }
 622   intptr_t* cloned_vtable()   { return &_cloned_vtable[0]; }
 623   void zero()                 { memset(_cloned_vtable, 0, sizeof(intptr_t) * vtable_size()); }
 624   // Returns the address of the next CppVtableInfo that can be placed immediately after this CppVtableInfo
 625   static size_t byte_size(int vtable_size) {
 626     CppVtableInfo i;
 627     return pointer_delta(&i._cloned_vtable[vtable_size], &i, sizeof(u1));
 628   }
 629 };
 630 
 631 template <class T> class CppVtableCloner : public T {
 632   static intptr_t* vtable_of(Metadata& m) {
 633     return *((intptr_t**)&m);
 634   }
 635   static CppVtableInfo* _info;
 636 
 637   static int get_vtable_length(const char* name);
 638 
 639 public:
 640   // Allocate and initialize the C++ vtable, starting from top, but do not go past end.
 641   static intptr_t* allocate(const char* name);
 642 
 643   // Clone the vtable to ...
 644   static intptr_t* clone_vtable(const char* name, CppVtableInfo* info);
 645 
 646   static void zero_vtable_clone() {
 647     assert(DumpSharedSpaces, "dump-time only");
 648     _info->zero();
 649   }
 650 
 651   // Switch the vtable pointer to point to the cloned vtable.
 652   static void patch(Metadata* obj) {
 653     assert(DumpSharedSpaces, "dump-time only");
 654     *(void**)obj = (void*)(_info->cloned_vtable());
 655   }
 656 
 657   static bool is_valid_shared_object(const T* obj) {
 658     intptr_t* vptr = *(intptr_t**)obj;
 659     return vptr == _info->cloned_vtable();
 660   }
 661 };
 662 
 663 template <class T> CppVtableInfo* CppVtableCloner<T>::_info = NULL;
 664 
 665 template <class T>
 666 intptr_t* CppVtableCloner<T>::allocate(const char* name) {
 667   assert(is_aligned(_md_region.top(), sizeof(intptr_t)), "bad alignment");
 668   int n = get_vtable_length(name);
 669   _info = (CppVtableInfo*)_md_region.allocate(CppVtableInfo::byte_size(n), sizeof(intptr_t));
 670   _info->set_vtable_size(n);
 671 
 672   intptr_t* p = clone_vtable(name, _info);
 673   assert((char*)p == _md_region.top(), "must be");
 674 
 675   return p;
 676 }
 677 
 678 template <class T>
 679 intptr_t* CppVtableCloner<T>::clone_vtable(const char* name, CppVtableInfo* info) {
 680   if (!DumpSharedSpaces) {
 681     assert(_info == 0, "_info is initialized only at dump time");
 682     _info = info; // Remember it -- it will be used by MetaspaceShared::is_valid_shared_method()
 683   }
 684   T tmp; // Allocate temporary dummy metadata object to get to the original vtable.
 685   int n = info->vtable_size();
 686   intptr_t* srcvtable = vtable_of(tmp);
 687   intptr_t* dstvtable = info->cloned_vtable();
 688 
 689   // We already checked (and, if necessary, adjusted n) when the vtables were allocated, so we are
 690   // safe to do memcpy.
 691   log_debug(cds, vtables)("Copying %3d vtable entries for %s", n, name);
 692   memcpy(dstvtable, srcvtable, sizeof(intptr_t) * n);
 693   return dstvtable + n;
 694 }
 695 
 696 // To determine the size of the vtable for each type, we use the following
 697 // trick by declaring 2 subclasses:
 698 //
 699 //   class CppVtableTesterA: public InstanceKlass {virtual int   last_virtual_method() {return 1;}    };
 700 //   class CppVtableTesterB: public InstanceKlass {virtual void* last_virtual_method() {return NULL}; };
 701 //
 702 // CppVtableTesterA and CppVtableTesterB's vtables have the following properties:
 703 // - Their size (N+1) is exactly one more than the size of InstanceKlass's vtable (N)
 704 // - The first N entries have are exactly the same as in InstanceKlass's vtable.
 705 // - Their last entry is different.
 706 //
 707 // So to determine the value of N, we just walk CppVtableTesterA and CppVtableTesterB's tables
 708 // and find the first entry that's different.
 709 //
 710 // This works on all C++ compilers supported by Oracle, but you may need to tweak it for more
 711 // esoteric compilers.
 712 
 713 template <class T> class CppVtableTesterB: public T {
 714 public:
 715   virtual int last_virtual_method() {return 1;}
 716 };
 717 
 718 template <class T> class CppVtableTesterA : public T {
 719 public:
 720   virtual void* last_virtual_method() {
 721     // Make this different than CppVtableTesterB::last_virtual_method so the C++
 722     // compiler/linker won't alias the two functions.
 723     return NULL;
 724   }
 725 };
 726 
 727 template <class T>
 728 int CppVtableCloner<T>::get_vtable_length(const char* name) {
 729   CppVtableTesterA<T> a;
 730   CppVtableTesterB<T> b;
 731 
 732   intptr_t* avtable = vtable_of(a);
 733   intptr_t* bvtable = vtable_of(b);
 734 
 735   // Start at slot 1, because slot 0 may be RTTI (on Solaris/Sparc)
 736   int vtable_len = 1;
 737   for (; ; vtable_len++) {
 738     if (avtable[vtable_len] != bvtable[vtable_len]) {
 739       break;
 740     }
 741   }
 742   log_debug(cds, vtables)("Found   %3d vtable entries for %s", vtable_len, name);
 743 
 744   return vtable_len;
 745 }
 746 
 747 #define ALLOC_CPP_VTABLE_CLONE(c) \
 748   CppVtableCloner<c>::allocate(#c);
 749 
 750 #define CLONE_CPP_VTABLE(c) \
 751   p = CppVtableCloner<c>::clone_vtable(#c, (CppVtableInfo*)p);
 752 
 753 #define ZERO_CPP_VTABLE(c) \
 754  CppVtableCloner<c>::zero_vtable_clone();
 755 
 756 // This can be called at both dump time and run time.
 757 intptr_t* MetaspaceShared::clone_cpp_vtables(intptr_t* p) {
 758   assert(DumpSharedSpaces || UseSharedSpaces, "sanity");
 759   CPP_VTABLE_PATCH_TYPES_DO(CLONE_CPP_VTABLE);
 760   return p;
 761 }
 762 
 763 void MetaspaceShared::zero_cpp_vtable_clones_for_writing() {
 764   assert(DumpSharedSpaces, "dump-time only");
 765   CPP_VTABLE_PATCH_TYPES_DO(ZERO_CPP_VTABLE);
 766 }
 767 
 768 // Allocate and initialize the C++ vtables, starting from top, but do not go past end.
 769 void MetaspaceShared::allocate_cpp_vtable_clones() {
 770   assert(DumpSharedSpaces, "dump-time only");
 771   // Layout (each slot is a intptr_t):
 772   //   [number of slots in the first vtable = n1]
 773   //   [ <n1> slots for the first vtable]
 774   //   [number of slots in the first second = n2]
 775   //   [ <n2> slots for the second vtable]
 776   //   ...
 777   // The order of the vtables is the same as the CPP_VTAB_PATCH_TYPES_DO macro.
 778   CPP_VTABLE_PATCH_TYPES_DO(ALLOC_CPP_VTABLE_CLONE);
 779 }
 780 
 781 // Switch the vtable pointer to point to the cloned vtable. We assume the
 782 // vtable pointer is in first slot in object.
 783 void MetaspaceShared::patch_cpp_vtable_pointers() {
 784   int n = _global_klass_objects->length();
 785   for (int i = 0; i < n; i++) {
 786     Klass* obj = _global_klass_objects->at(i);
 787     if (obj->is_instance_klass()) {
 788       InstanceKlass* ik = InstanceKlass::cast(obj);
 789       if (ik->is_class_loader_instance_klass()) {
 790         CppVtableCloner<InstanceClassLoaderKlass>::patch(ik);
 791       } else if (ik->is_reference_instance_klass()) {
 792         CppVtableCloner<InstanceRefKlass>::patch(ik);
 793       } else if (ik->is_mirror_instance_klass()) {
 794         CppVtableCloner<InstanceMirrorKlass>::patch(ik);
 795       } else {
 796         CppVtableCloner<InstanceKlass>::patch(ik);
 797       }
 798       ConstantPool* cp = ik->constants();
 799       CppVtableCloner<ConstantPool>::patch(cp);
 800       for (int j = 0; j < ik->methods()->length(); j++) {
 801         Method* m = ik->methods()->at(j);
 802         CppVtableCloner<Method>::patch(m);
 803         assert(CppVtableCloner<Method>::is_valid_shared_object(m), "must be");
 804       }
 805     } else if (obj->is_objArray_klass()) {
 806       CppVtableCloner<ObjArrayKlass>::patch(obj);
 807     } else {
 808       assert(obj->is_typeArray_klass(), "sanity");
 809       CppVtableCloner<TypeArrayKlass>::patch(obj);
 810     }
 811   }
 812 }
 813 
 814 bool MetaspaceShared::is_valid_shared_method(const Method* m) {
 815   assert(is_in_shared_metaspace(m), "must be");
 816   return CppVtableCloner<Method>::is_valid_shared_object(m);
 817 }
 818 
 819 // Closure for serializing initialization data out to a data area to be
 820 // written to the shared file.
 821 
 822 class WriteClosure : public SerializeClosure {
 823 private:
 824   DumpRegion* _dump_region;
 825 
 826 public:
 827   WriteClosure(DumpRegion* r) {
 828     _dump_region = r;
 829   }
 830 
 831   void do_ptr(void** p) {
 832     _dump_region->append_intptr_t((intptr_t)*p);
 833   }
 834 
 835   void do_u4(u4* p) {
 836     void* ptr = (void*)(uintx(*p));
 837     do_ptr(&ptr);
 838   }
 839 
 840   void do_tag(int tag) {
 841     _dump_region->append_intptr_t((intptr_t)tag);
 842   }
 843 
 844   void do_oop(oop* o) {
 845     if (*o == NULL) {
 846       _dump_region->append_intptr_t(0);
 847     } else {
 848       assert(HeapShared::is_heap_object_archiving_allowed(),
 849              "Archiving heap object is not allowed");
 850       _dump_region->append_intptr_t(
 851         (intptr_t)CompressedOops::encode_not_null(*o));
 852     }
 853   }
 854 
 855   void do_region(u_char* start, size_t size) {
 856     assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
 857     assert(size % sizeof(intptr_t) == 0, "bad size");
 858     do_tag((int)size);
 859     while (size > 0) {
 860       _dump_region->append_intptr_t(*(intptr_t*)start);
 861       start += sizeof(intptr_t);
 862       size -= sizeof(intptr_t);
 863     }
 864   }
 865 
 866   bool reading() const { return false; }
 867 };
 868 
 869 // This is for dumping detailed statistics for the allocations
 870 // in the shared spaces.
 871 class DumpAllocStats : public ResourceObj {
 872 public:
 873 
 874   // Here's poor man's enum inheritance
 875 #define SHAREDSPACE_OBJ_TYPES_DO(f) \
 876   METASPACE_OBJ_TYPES_DO(f) \
 877   f(SymbolHashentry) \
 878   f(SymbolBucket) \
 879   f(StringHashentry) \
 880   f(StringBucket) \
 881   f(Other)
 882 
 883   enum Type {
 884     // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc
 885     SHAREDSPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_DECLARE)
 886     _number_of_types
 887   };
 888 
 889   static const char * type_name(Type type) {
 890     switch(type) {
 891     SHAREDSPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_NAME_CASE)
 892     default:
 893       ShouldNotReachHere();
 894       return NULL;
 895     }
 896   }
 897 
 898 public:
 899   enum { RO = 0, RW = 1 };
 900 
 901   int _counts[2][_number_of_types];
 902   int _bytes [2][_number_of_types];
 903 
 904   DumpAllocStats() {
 905     memset(_counts, 0, sizeof(_counts));
 906     memset(_bytes,  0, sizeof(_bytes));
 907   };
 908 
 909   void record(MetaspaceObj::Type type, int byte_size, bool read_only) {
 910     assert(int(type) >= 0 && type < MetaspaceObj::_number_of_types, "sanity");
 911     int which = (read_only) ? RO : RW;
 912     _counts[which][type] ++;
 913     _bytes [which][type] += byte_size;
 914   }
 915 
 916   void record_other_type(int byte_size, bool read_only) {
 917     int which = (read_only) ? RO : RW;
 918     _bytes [which][OtherType] += byte_size;
 919   }
 920   void print_stats(int ro_all, int rw_all, int mc_all, int md_all);
 921 };
 922 
 923 void DumpAllocStats::print_stats(int ro_all, int rw_all, int mc_all, int md_all) {
 924   // Calculate size of data that was not allocated by Metaspace::allocate()
 925   MetaspaceSharedStats *stats = MetaspaceShared::stats();
 926 
 927   // symbols
 928   _counts[RO][SymbolHashentryType] = stats->symbol.hashentry_count;
 929   _bytes [RO][SymbolHashentryType] = stats->symbol.hashentry_bytes;
 930 
 931   _counts[RO][SymbolBucketType] = stats->symbol.bucket_count;
 932   _bytes [RO][SymbolBucketType] = stats->symbol.bucket_bytes;
 933 
 934   // strings
 935   _counts[RO][StringHashentryType] = stats->string.hashentry_count;
 936   _bytes [RO][StringHashentryType] = stats->string.hashentry_bytes;
 937 
 938   _counts[RO][StringBucketType] = stats->string.bucket_count;
 939   _bytes [RO][StringBucketType] = stats->string.bucket_bytes;
 940 
 941   // TODO: count things like dictionary, vtable, etc
 942   _bytes[RW][OtherType] += mc_all + md_all;
 943   rw_all += mc_all + md_all; // mc/md are mapped Read/Write
 944 
 945   // prevent divide-by-zero
 946   if (ro_all < 1) {
 947     ro_all = 1;
 948   }
 949   if (rw_all < 1) {
 950     rw_all = 1;
 951   }
 952 
 953   int all_ro_count = 0;
 954   int all_ro_bytes = 0;
 955   int all_rw_count = 0;
 956   int all_rw_bytes = 0;
 957 
 958 // To make fmt_stats be a syntactic constant (for format warnings), use #define.
 959 #define fmt_stats "%-20s: %8d %10d %5.1f | %8d %10d %5.1f | %8d %10d %5.1f"
 960   const char *sep = "--------------------+---------------------------+---------------------------+--------------------------";
 961   const char *hdr = "                        ro_cnt   ro_bytes     % |   rw_cnt   rw_bytes     % |  all_cnt  all_bytes     %";
 962 
 963   LogMessage(cds) msg;
 964 
 965   msg.info("Detailed metadata info (excluding od/st regions; rw stats include md/mc regions):");
 966   msg.info("%s", hdr);
 967   msg.info("%s", sep);
 968   for (int type = 0; type < int(_number_of_types); type ++) {
 969     const char *name = type_name((Type)type);
 970     int ro_count = _counts[RO][type];
 971     int ro_bytes = _bytes [RO][type];
 972     int rw_count = _counts[RW][type];
 973     int rw_bytes = _bytes [RW][type];
 974     int count = ro_count + rw_count;
 975     int bytes = ro_bytes + rw_bytes;
 976 
 977     double ro_perc = percent_of(ro_bytes, ro_all);
 978     double rw_perc = percent_of(rw_bytes, rw_all);
 979     double perc    = percent_of(bytes, ro_all + rw_all);
 980 
 981     msg.info(fmt_stats, name,
 982                          ro_count, ro_bytes, ro_perc,
 983                          rw_count, rw_bytes, rw_perc,
 984                          count, bytes, perc);
 985 
 986     all_ro_count += ro_count;
 987     all_ro_bytes += ro_bytes;
 988     all_rw_count += rw_count;
 989     all_rw_bytes += rw_bytes;
 990   }
 991 
 992   int all_count = all_ro_count + all_rw_count;
 993   int all_bytes = all_ro_bytes + all_rw_bytes;
 994 
 995   double all_ro_perc = percent_of(all_ro_bytes, ro_all);
 996   double all_rw_perc = percent_of(all_rw_bytes, rw_all);
 997   double all_perc    = percent_of(all_bytes, ro_all + rw_all);
 998 
 999   msg.info("%s", sep);
1000   msg.info(fmt_stats, "Total",
1001                        all_ro_count, all_ro_bytes, all_ro_perc,
1002                        all_rw_count, all_rw_bytes, all_rw_perc,
1003                        all_count, all_bytes, all_perc);
1004 
1005   assert(all_ro_bytes == ro_all, "everything should have been counted");
1006   assert(all_rw_bytes == rw_all, "everything should have been counted");
1007 
1008 #undef fmt_stats
1009 }
1010 
1011 // Populate the shared space.
1012 
1013 class VM_PopulateDumpSharedSpace: public VM_Operation {
1014 private:
1015   GrowableArray<MemRegion> *_closed_archive_heap_regions;
1016   GrowableArray<MemRegion> *_open_archive_heap_regions;
1017 
1018   GrowableArray<ArchiveHeapOopmapInfo> *_closed_archive_heap_oopmaps;
1019   GrowableArray<ArchiveHeapOopmapInfo> *_open_archive_heap_oopmaps;
1020 
1021   void dump_java_heap_objects() NOT_CDS_JAVA_HEAP_RETURN;
1022   void dump_archive_heap_oopmaps() NOT_CDS_JAVA_HEAP_RETURN;
1023   void dump_archive_heap_oopmaps(GrowableArray<MemRegion>* regions,
1024                                  GrowableArray<ArchiveHeapOopmapInfo>* oopmaps);
1025   void dump_symbols();
1026   char* dump_read_only_tables();
1027   void print_region_stats();
1028   void print_heap_region_stats(GrowableArray<MemRegion> *heap_mem,
1029                                const char *name, const size_t total_size);
1030 public:
1031 
1032   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
1033   void doit();   // outline because gdb sucks
1034   static void write_region(FileMapInfo* mapinfo, int region, DumpRegion* space, bool read_only,  bool allow_exec);
1035   bool allow_nested_vm_operations() const { return true; }
1036 }; // class VM_PopulateDumpSharedSpace
1037 
1038 class SortedSymbolClosure: public SymbolClosure {
1039   GrowableArray<Symbol*> _symbols;
1040   virtual void do_symbol(Symbol** sym) {
1041     assert((*sym)->is_permanent(), "archived symbols must be permanent");
1042     _symbols.append(*sym);
1043   }
1044   static int compare_symbols_by_address(Symbol** a, Symbol** b) {
1045     if (a[0] < b[0]) {
1046       return -1;
1047     } else if (a[0] == b[0]) {
1048       return 0;
1049     } else {
1050       return 1;
1051     }
1052   }
1053 
1054 public:
1055   SortedSymbolClosure() {
1056     SymbolTable::symbols_do(this);
1057     _symbols.sort(compare_symbols_by_address);
1058   }
1059   GrowableArray<Symbol*>* get_sorted_symbols() {
1060     return &_symbols;
1061   }
1062 };
1063 
1064 // ArchiveCompactor --
1065 //
1066 // This class is the central piece of shared archive compaction -- all metaspace data are
1067 // initially allocated outside of the shared regions. ArchiveCompactor copies the
1068 // metaspace data into their final location in the shared regions.
1069 
1070 class ArchiveCompactor : AllStatic {
1071   static DumpAllocStats* _alloc_stats;
1072   static SortedSymbolClosure* _ssc;
1073 
1074   typedef KVHashtable<address, address, mtInternal> RelocationTable;
1075   static RelocationTable* _new_loc_table;
1076 
1077 public:
1078   static void initialize() {
1079     _alloc_stats = new(ResourceObj::C_HEAP, mtInternal)DumpAllocStats;
1080     _new_loc_table = new RelocationTable(8087);
1081   }
1082   static DumpAllocStats* alloc_stats() {
1083     return _alloc_stats;
1084   }
1085 
1086   // Use this when you allocate space with MetaspaceShare::read_only_space_alloc()
1087   // outside of ArchiveCompactor::allocate(). These are usually for misc tables
1088   // that are allocated in the RO space.
1089   class OtherROAllocMark {
1090     char* _oldtop;
1091   public:
1092     OtherROAllocMark() {
1093       _oldtop = _ro_region.top();
1094     }
1095     ~OtherROAllocMark() {
1096       char* newtop = _ro_region.top();
1097       ArchiveCompactor::alloc_stats()->record_other_type(int(newtop - _oldtop), true);
1098     }
1099   };
1100 
1101   static void allocate(MetaspaceClosure::Ref* ref, bool read_only) {
1102     address obj = ref->obj();
1103     int bytes = ref->size() * BytesPerWord;
1104     char* p;
1105     size_t alignment = BytesPerWord;
1106     char* oldtop;
1107     char* newtop;
1108 
1109     if (read_only) {
1110       oldtop = _ro_region.top();
1111       p = _ro_region.allocate(bytes, alignment);
1112       newtop = _ro_region.top();
1113     } else {
1114       oldtop = _rw_region.top();
1115       if (ref->msotype() == MetaspaceObj::ClassType) {
1116         // Save a pointer immediate in front of an InstanceKlass, so
1117         // we can do a quick lookup from InstanceKlass* -> RunTimeSharedClassInfo*
1118         // without building another hashtable. See RunTimeSharedClassInfo::get_for()
1119         // in systemDictionaryShared.cpp.
1120         Klass* klass = (Klass*)obj;
1121         if (klass->is_instance_klass()) {
1122           SystemDictionaryShared::validate_before_archiving(InstanceKlass::cast(klass));
1123           _rw_region.allocate(sizeof(address), BytesPerWord);
1124         }
1125       }
1126       p = _rw_region.allocate(bytes, alignment);
1127       newtop = _rw_region.top();
1128     }
1129     memcpy(p, obj, bytes);
1130     assert(_new_loc_table->lookup(obj) == NULL, "each object can be relocated at most once");
1131     _new_loc_table->add(obj, (address)p);
1132     log_trace(cds)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(obj), p2i(p), bytes);
1133     if (_new_loc_table->maybe_grow()) {
1134       log_info(cds, hashtables)("Expanded _new_loc_table to %d", _new_loc_table->table_size());
1135     }
1136     _alloc_stats->record(ref->msotype(), int(newtop - oldtop), read_only);
1137   }
1138 
1139   static address get_new_loc(MetaspaceClosure::Ref* ref) {
1140     address* pp = _new_loc_table->lookup(ref->obj());
1141     assert(pp != NULL, "must be");
1142     return *pp;
1143   }
1144 
1145 private:
1146   // Makes a shallow copy of visited MetaspaceObj's
1147   class ShallowCopier: public UniqueMetaspaceClosure {
1148     bool _read_only;
1149   public:
1150     ShallowCopier(bool read_only) : _read_only(read_only) {}
1151 
1152     virtual void do_unique_ref(Ref* ref, bool read_only) {
1153       if (read_only == _read_only) {
1154         allocate(ref, read_only);
1155       }
1156     }
1157   };
1158 
1159   // Relocate embedded pointers within a MetaspaceObj's shallow copy
1160   class ShallowCopyEmbeddedRefRelocator: public UniqueMetaspaceClosure {
1161   public:
1162     virtual void do_unique_ref(Ref* ref, bool read_only) {
1163       address new_loc = get_new_loc(ref);
1164       RefRelocator refer;
1165       ref->metaspace_pointers_do_at(&refer, new_loc);
1166     }
1167   };
1168 
1169   // Relocate a reference to point to its shallow copy
1170   class RefRelocator: public MetaspaceClosure {
1171   public:
1172     virtual bool do_ref(Ref* ref, bool read_only) {
1173       if (ref->not_null()) {
1174         ref->update(get_new_loc(ref));
1175       }
1176       return false; // Do not recurse.
1177     }
1178   };
1179 
1180 #ifdef ASSERT
1181   class IsRefInArchiveChecker: public MetaspaceClosure {
1182   public:
1183     virtual bool do_ref(Ref* ref, bool read_only) {
1184       if (ref->not_null()) {
1185         char* obj = (char*)ref->obj();
1186         assert(_ro_region.contains(obj) || _rw_region.contains(obj),
1187                "must be relocated to point to CDS archive");
1188       }
1189       return false; // Do not recurse.
1190     }
1191   };
1192 #endif
1193 
1194 public:
1195   static void copy_and_compact() {
1196     ResourceMark rm;
1197     SortedSymbolClosure the_ssc; // StackObj
1198     _ssc = &the_ssc;
1199 
1200     tty->print_cr("Scanning all metaspace objects ... ");
1201     {
1202       // allocate and shallow-copy RW objects, immediately following the MC region
1203       tty->print_cr("Allocating RW objects ... ");
1204       _mc_region.pack(&_rw_region);
1205 
1206       ResourceMark rm;
1207       ShallowCopier rw_copier(false);
1208       iterate_roots(&rw_copier);
1209     }
1210     {
1211       // allocate and shallow-copy of RO object, immediately following the RW region
1212       tty->print_cr("Allocating RO objects ... ");
1213       _rw_region.pack(&_ro_region);
1214 
1215       ResourceMark rm;
1216       ShallowCopier ro_copier(true);
1217       iterate_roots(&ro_copier);
1218     }
1219     {
1220       tty->print_cr("Relocating embedded pointers ... ");
1221       ResourceMark rm;
1222       ShallowCopyEmbeddedRefRelocator emb_reloc;
1223       iterate_roots(&emb_reloc);
1224     }
1225     {
1226       tty->print_cr("Relocating external roots ... ");
1227       ResourceMark rm;
1228       RefRelocator ext_reloc;
1229       iterate_roots(&ext_reloc);
1230     }
1231 
1232 #ifdef ASSERT
1233     {
1234       tty->print_cr("Verifying external roots ... ");
1235       ResourceMark rm;
1236       IsRefInArchiveChecker checker;
1237       iterate_roots(&checker);
1238     }
1239 #endif
1240 
1241 
1242     // cleanup
1243     _ssc = NULL;
1244   }
1245 
1246   // We must relocate the System::_well_known_klasses only after we have copied the
1247   // java objects in during dump_java_heap_objects(): during the object copy, we operate on
1248   // old objects which assert that their klass is the original klass.
1249   static void relocate_well_known_klasses() {
1250     {
1251       tty->print_cr("Relocating SystemDictionary::_well_known_klasses[] ... ");
1252       ResourceMark rm;
1253       RefRelocator ext_reloc;
1254       SystemDictionary::well_known_klasses_do(&ext_reloc);
1255     }
1256     // NOTE: after this point, we shouldn't have any globals that can reach the old
1257     // objects.
1258 
1259     // We cannot use any of the objects in the heap anymore (except for the
1260     // shared strings) because their headers no longer point to valid Klasses.
1261   }
1262 
1263   static void iterate_roots(MetaspaceClosure* it) {
1264     GrowableArray<Symbol*>* symbols = _ssc->get_sorted_symbols();
1265     for (int i=0; i<symbols->length(); i++) {
1266       it->push(symbols->adr_at(i));
1267     }
1268     if (_global_klass_objects != NULL) {
1269       // Need to fix up the pointers
1270       for (int i = 0; i < _global_klass_objects->length(); i++) {
1271         // NOTE -- this requires that the vtable is NOT yet patched, or else we are hosed.
1272         it->push(_global_klass_objects->adr_at(i));
1273       }
1274     }
1275     FileMapInfo::metaspace_pointers_do(it);
1276     SystemDictionaryShared::dumptime_classes_do(it);
1277     Universe::metaspace_pointers_do(it);
1278     SymbolTable::metaspace_pointers_do(it);
1279     vmSymbols::metaspace_pointers_do(it);
1280   }
1281 
1282   static Klass* get_relocated_klass(Klass* orig_klass) {
1283     assert(DumpSharedSpaces, "dump time only");
1284     address* pp = _new_loc_table->lookup((address)orig_klass);
1285     assert(pp != NULL, "must be");
1286     Klass* klass = (Klass*)(*pp);
1287     assert(klass->is_klass(), "must be");
1288     return klass;
1289   }
1290 };
1291 
1292 DumpAllocStats* ArchiveCompactor::_alloc_stats;
1293 SortedSymbolClosure* ArchiveCompactor::_ssc;
1294 ArchiveCompactor::RelocationTable* ArchiveCompactor::_new_loc_table;
1295 
1296 void VM_PopulateDumpSharedSpace::write_region(FileMapInfo* mapinfo, int region_idx,
1297                                               DumpRegion* dump_region, bool read_only,  bool allow_exec) {
1298   mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec);
1299 }
1300 
1301 void VM_PopulateDumpSharedSpace::dump_symbols() {
1302   tty->print_cr("Dumping symbol table ...");
1303 
1304   NOT_PRODUCT(SymbolTable::verify());
1305   SymbolTable::write_to_archive();
1306 }
1307 
1308 char* VM_PopulateDumpSharedSpace::dump_read_only_tables() {
1309   ArchiveCompactor::OtherROAllocMark mark;
1310 
1311   tty->print("Removing java_mirror ... ");
1312   if (!HeapShared::is_heap_object_archiving_allowed()) {
1313     clear_basic_type_mirrors();
1314   }
1315   remove_java_mirror_in_classes();
1316   tty->print_cr("done. ");
1317 
1318   SystemDictionaryShared::write_to_archive();
1319 
1320   char* start = _ro_region.top();
1321 
1322   // Write the other data to the output array.
1323   WriteClosure wc(&_ro_region);
1324   MetaspaceShared::serialize(&wc);
1325 
1326   // Write the bitmaps for patching the archive heap regions
1327   dump_archive_heap_oopmaps();
1328 
1329   return start;
1330 }
1331 
1332 void VM_PopulateDumpSharedSpace::doit() {
1333   // We should no longer allocate anything from the metaspace, so that:
1334   //
1335   // (1) Metaspace::allocate might trigger GC if we have run out of
1336   //     committed metaspace, but we can't GC because we're running
1337   //     in the VM thread.
1338   // (2) ArchiveCompactor needs to work with a stable set of MetaspaceObjs.
1339   Metaspace::freeze();
1340 
1341   Thread* THREAD = VMThread::vm_thread();
1342 
1343   FileMapInfo::check_nonempty_dir_in_shared_path_table();
1344 
1345   NOT_PRODUCT(SystemDictionary::verify();)
1346   // The following guarantee is meant to ensure that no loader constraints
1347   // exist yet, since the constraints table is not shared.  This becomes
1348   // more important now that we don't re-initialize vtables/itables for
1349   // shared classes at runtime, where constraints were previously created.
1350   guarantee(SystemDictionary::constraints()->number_of_entries() == 0,
1351             "loader constraints are not saved");
1352   guarantee(SystemDictionary::placeholders()->number_of_entries() == 0,
1353           "placeholders are not saved");
1354 
1355   // At this point, many classes have been loaded.
1356   // Gather systemDictionary classes in a global array and do everything to
1357   // that so we don't have to walk the SystemDictionary again.
1358   SystemDictionaryShared::check_excluded_classes();
1359   _global_klass_objects = new GrowableArray<Klass*>(1000);
1360   CollectClassesClosure collect_classes;
1361   ClassLoaderDataGraph::loaded_classes_do(&collect_classes);
1362 
1363   tty->print_cr("Number of classes %d", _global_klass_objects->length());
1364   {
1365     int num_type_array = 0, num_obj_array = 0, num_inst = 0;
1366     for (int i = 0; i < _global_klass_objects->length(); i++) {
1367       Klass* k = _global_klass_objects->at(i);
1368       if (k->is_instance_klass()) {
1369         num_inst ++;
1370       } else if (k->is_objArray_klass()) {
1371         num_obj_array ++;
1372       } else {
1373         assert(k->is_typeArray_klass(), "sanity");
1374         num_type_array ++;
1375       }
1376     }
1377     tty->print_cr("    instance classes   = %5d", num_inst);
1378     tty->print_cr("    obj array classes  = %5d", num_obj_array);
1379     tty->print_cr("    type array classes = %5d", num_type_array);
1380   }
1381 
1382   // Ensure the ConstMethods won't be modified at run-time
1383   tty->print("Updating ConstMethods ... ");
1384   rewrite_nofast_bytecodes_and_calculate_fingerprints();
1385   tty->print_cr("done. ");
1386 
1387   // Remove all references outside the metadata
1388   tty->print("Removing unshareable information ... ");
1389   remove_unshareable_in_classes();
1390   tty->print_cr("done. ");
1391 
1392   ArchiveCompactor::initialize();
1393   ArchiveCompactor::copy_and_compact();
1394 
1395   dump_symbols();
1396 
1397   // Dump supported java heap objects
1398   _closed_archive_heap_regions = NULL;
1399   _open_archive_heap_regions = NULL;
1400   dump_java_heap_objects();
1401 
1402   ArchiveCompactor::relocate_well_known_klasses();
1403 
1404   char* read_only_tables_start = dump_read_only_tables();
1405   _ro_region.pack(&_md_region);
1406 
1407   char* vtbl_list = _md_region.top();
1408   MetaspaceShared::allocate_cpp_vtable_clones();
1409   _md_region.pack(&_od_region);
1410 
1411   // Relocate the archived class file data into the od region
1412   relocate_cached_class_file();
1413   _od_region.pack();
1414 
1415   // The 5 core spaces are allocated consecutively mc->rw->ro->md->od, so there total size
1416   // is just the spaces between the two ends.
1417   size_t core_spaces_size = _od_region.end() - _mc_region.base();
1418   assert(core_spaces_size == (size_t)align_up(core_spaces_size, Metaspace::reserve_alignment()),
1419          "should already be aligned");
1420 
1421   // During patching, some virtual methods may be called, so at this point
1422   // the vtables must contain valid methods (as filled in by CppVtableCloner::allocate).
1423   MetaspaceShared::patch_cpp_vtable_pointers();
1424 
1425   // The vtable clones contain addresses of the current process.
1426   // We don't want to write these addresses into the archive.
1427   MetaspaceShared::zero_cpp_vtable_clones_for_writing();
1428 
1429   // Create and write the archive file that maps the shared spaces.
1430 
1431   FileMapInfo* mapinfo = new FileMapInfo();
1432   mapinfo->populate_header(os::vm_allocation_granularity());
1433   mapinfo->set_read_only_tables_start(read_only_tables_start);
1434   mapinfo->set_misc_data_patching_start(vtbl_list);
1435   mapinfo->set_cds_i2i_entry_code_buffers(MetaspaceShared::cds_i2i_entry_code_buffers());
1436   mapinfo->set_cds_i2i_entry_code_buffers_size(MetaspaceShared::cds_i2i_entry_code_buffers_size());
1437   mapinfo->set_core_spaces_size(core_spaces_size);
1438 
1439   for (int pass=1; pass<=2; pass++) {
1440     bool print_archive_log = (pass==1);
1441     if (pass == 1) {
1442       // The first pass doesn't actually write the data to disk. All it
1443       // does is to update the fields in the mapinfo->_header.
1444     } else {
1445       // After the first pass, the contents of mapinfo->_header are finalized,
1446       // so we can compute the header's CRC, and write the contents of the header
1447       // and the regions into disk.
1448       mapinfo->open_for_write();
1449       mapinfo->set_header_crc(mapinfo->compute_header_crc());
1450     }
1451     mapinfo->write_header();
1452 
1453     // NOTE: md contains the trampoline code for method entries, which are patched at run time,
1454     // so it needs to be read/write.
1455     write_region(mapinfo, MetaspaceShared::mc, &_mc_region, /*read_only=*/false,/*allow_exec=*/true);
1456     write_region(mapinfo, MetaspaceShared::rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false);
1457     write_region(mapinfo, MetaspaceShared::ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false);
1458     write_region(mapinfo, MetaspaceShared::md, &_md_region, /*read_only=*/false,/*allow_exec=*/false);
1459     write_region(mapinfo, MetaspaceShared::od, &_od_region, /*read_only=*/true, /*allow_exec=*/false);
1460 
1461     _total_closed_archive_region_size = mapinfo->write_archive_heap_regions(
1462                                         _closed_archive_heap_regions,
1463                                         _closed_archive_heap_oopmaps,
1464                                         MetaspaceShared::first_closed_archive_heap_region,
1465                                         MetaspaceShared::max_closed_archive_heap_region,
1466                                         print_archive_log);
1467     _total_open_archive_region_size = mapinfo->write_archive_heap_regions(
1468                                         _open_archive_heap_regions,
1469                                         _open_archive_heap_oopmaps,
1470                                         MetaspaceShared::first_open_archive_heap_region,
1471                                         MetaspaceShared::max_open_archive_heap_region,
1472                                         print_archive_log);
1473   }
1474 
1475   mapinfo->close();
1476 
1477   // Restore the vtable in case we invoke any virtual methods.
1478   MetaspaceShared::clone_cpp_vtables((intptr_t*)vtbl_list);
1479 
1480   print_region_stats();
1481 
1482   if (log_is_enabled(Info, cds)) {
1483     ArchiveCompactor::alloc_stats()->print_stats(int(_ro_region.used()), int(_rw_region.used()),
1484                                                  int(_mc_region.used()), int(_md_region.used()));
1485   }
1486 
1487   if (PrintSystemDictionaryAtExit) {
1488     SystemDictionary::print();
1489   }
1490   // There may be other pending VM operations that operate on the InstanceKlasses,
1491   // which will fail because InstanceKlasses::remove_unshareable_info()
1492   // has been called. Forget these operations and exit the VM directly.
1493   vm_direct_exit(0);
1494 }
1495 
1496 void VM_PopulateDumpSharedSpace::print_region_stats() {
1497   // Print statistics of all the regions
1498   const size_t total_reserved = _ro_region.reserved()  + _rw_region.reserved() +
1499                                 _mc_region.reserved()  + _md_region.reserved() +
1500                                 _od_region.reserved()  +
1501                                 _total_closed_archive_region_size +
1502                                 _total_open_archive_region_size;
1503   const size_t total_bytes = _ro_region.used()  + _rw_region.used() +
1504                              _mc_region.used()  + _md_region.used() +
1505                              _od_region.used()  +
1506                              _total_closed_archive_region_size +
1507                              _total_open_archive_region_size;
1508   const double total_u_perc = percent_of(total_bytes, total_reserved);
1509 
1510   _mc_region.print(total_reserved);
1511   _rw_region.print(total_reserved);
1512   _ro_region.print(total_reserved);
1513   _md_region.print(total_reserved);
1514   _od_region.print(total_reserved);
1515   print_heap_region_stats(_closed_archive_heap_regions, "ca", total_reserved);
1516   print_heap_region_stats(_open_archive_heap_regions, "oa", total_reserved);
1517 
1518   tty->print_cr("total    : " SIZE_FORMAT_W(9) " [100.0%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used]",
1519                  total_bytes, total_reserved, total_u_perc);
1520 }
1521 
1522 void VM_PopulateDumpSharedSpace::print_heap_region_stats(GrowableArray<MemRegion> *heap_mem,
1523                                                          const char *name, const size_t total_size) {
1524   int arr_len = heap_mem == NULL ? 0 : heap_mem->length();
1525   for (int i = 0; i < arr_len; i++) {
1526       char* start = (char*)heap_mem->at(i).start();
1527       size_t size = heap_mem->at(i).byte_size();
1528       char* top = start + size;
1529       tty->print_cr("%s%d space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used] at " INTPTR_FORMAT,
1530                     name, i, size, size/double(total_size)*100.0, size, p2i(start));
1531 
1532   }
1533 }
1534 
1535 // Update a Java object to point its Klass* to the new location after
1536 // shared archive has been compacted.
1537 void MetaspaceShared::relocate_klass_ptr(oop o) {
1538   assert(DumpSharedSpaces, "sanity");
1539   Klass* k = ArchiveCompactor::get_relocated_klass(o->klass());
1540   o->set_klass(k);
1541 }
1542 
1543 Klass* MetaspaceShared::get_relocated_klass(Klass *k) {
1544   assert(DumpSharedSpaces, "sanity");
1545   return ArchiveCompactor::get_relocated_klass(k);
1546 }
1547 
1548 class LinkSharedClassesClosure : public KlassClosure {
1549   Thread* THREAD;
1550   bool    _made_progress;
1551  public:
1552   LinkSharedClassesClosure(Thread* thread) : THREAD(thread), _made_progress(false) {}
1553 
1554   void reset()               { _made_progress = false; }
1555   bool made_progress() const { return _made_progress; }
1556 
1557   void do_klass(Klass* k) {
1558     if (k->is_instance_klass()) {
1559       InstanceKlass* ik = InstanceKlass::cast(k);
1560       // Link the class to cause the bytecodes to be rewritten and the
1561       // cpcache to be created. Class verification is done according
1562       // to -Xverify setting.
1563       _made_progress |= MetaspaceShared::try_link_class(ik, THREAD);
1564       guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1565 
1566       ik->constants()->resolve_class_constants(THREAD);
1567     }
1568   }
1569 };
1570 
1571 class CheckSharedClassesClosure : public KlassClosure {
1572   bool    _made_progress;
1573  public:
1574   CheckSharedClassesClosure() : _made_progress(false) {}
1575 
1576   void reset()               { _made_progress = false; }
1577   bool made_progress() const { return _made_progress; }
1578   void do_klass(Klass* k) {
1579     if (k->is_instance_klass() && InstanceKlass::cast(k)->check_sharing_error_state()) {
1580       _made_progress = true;
1581     }
1582   }
1583 };
1584 
1585 void MetaspaceShared::link_and_cleanup_shared_classes(TRAPS) {
1586   // We need to iterate because verification may cause additional classes
1587   // to be loaded.
1588   LinkSharedClassesClosure link_closure(THREAD);
1589   do {
1590     link_closure.reset();
1591     ClassLoaderDataGraph::unlocked_loaded_classes_do(&link_closure);
1592     guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1593   } while (link_closure.made_progress());
1594 
1595   if (_has_error_classes) {
1596     // Mark all classes whose super class or interfaces failed verification.
1597     CheckSharedClassesClosure check_closure;
1598     do {
1599       // Not completely sure if we need to do this iteratively. Anyway,
1600       // we should come here only if there are unverifiable classes, which
1601       // shouldn't happen in normal cases. So better safe than sorry.
1602       check_closure.reset();
1603       ClassLoaderDataGraph::unlocked_loaded_classes_do(&check_closure);
1604     } while (check_closure.made_progress());
1605   }
1606 }
1607 
1608 void MetaspaceShared::prepare_for_dumping() {
1609   Arguments::check_unsupported_dumping_properties();
1610   ClassLoader::initialize_shared_path();
1611 }
1612 
1613 // Preload classes from a list, populate the shared spaces and dump to a
1614 // file.
1615 void MetaspaceShared::preload_and_dump(TRAPS) {
1616   { TraceTime timer("Dump Shared Spaces", TRACETIME_LOG(Info, startuptime));
1617     ResourceMark rm;
1618     char class_list_path_str[JVM_MAXPATHLEN];
1619     // Preload classes to be shared.
1620     // Should use some os:: method rather than fopen() here. aB.
1621     const char* class_list_path;
1622     if (SharedClassListFile == NULL) {
1623       // Construct the path to the class list (in jre/lib)
1624       // Walk up two directories from the location of the VM and
1625       // optionally tack on "lib" (depending on platform)
1626       os::jvm_path(class_list_path_str, sizeof(class_list_path_str));
1627       for (int i = 0; i < 3; i++) {
1628         char *end = strrchr(class_list_path_str, *os::file_separator());
1629         if (end != NULL) *end = '\0';
1630       }
1631       int class_list_path_len = (int)strlen(class_list_path_str);
1632       if (class_list_path_len >= 3) {
1633         if (strcmp(class_list_path_str + class_list_path_len - 3, "lib") != 0) {
1634           if (class_list_path_len < JVM_MAXPATHLEN - 4) {
1635             jio_snprintf(class_list_path_str + class_list_path_len,
1636                          sizeof(class_list_path_str) - class_list_path_len,
1637                          "%slib", os::file_separator());
1638             class_list_path_len += 4;
1639           }
1640         }
1641       }
1642       if (class_list_path_len < JVM_MAXPATHLEN - 10) {
1643         jio_snprintf(class_list_path_str + class_list_path_len,
1644                      sizeof(class_list_path_str) - class_list_path_len,
1645                      "%sclasslist", os::file_separator());
1646       }
1647       class_list_path = class_list_path_str;
1648     } else {
1649       class_list_path = SharedClassListFile;
1650     }
1651 
1652     tty->print_cr("Loading classes to share ...");
1653     _has_error_classes = false;
1654     int class_count = preload_classes(class_list_path, THREAD);
1655     if (ExtraSharedClassListFile) {
1656       class_count += preload_classes(ExtraSharedClassListFile, THREAD);
1657     }
1658     tty->print_cr("Loading classes to share: done.");
1659 
1660     log_info(cds)("Shared spaces: preloaded %d classes", class_count);
1661 
1662     if (SharedArchiveConfigFile) {
1663       tty->print_cr("Reading extra data from %s ...", SharedArchiveConfigFile);
1664       read_extra_data(SharedArchiveConfigFile, THREAD);
1665     }
1666     tty->print_cr("Reading extra data: done.");
1667 
1668     HeapShared::init_subgraph_entry_fields(THREAD);
1669 
1670     // Rewrite and link classes
1671     tty->print_cr("Rewriting and linking classes ...");
1672 
1673     // Link any classes which got missed. This would happen if we have loaded classes that
1674     // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1675     // fails verification, all other interfaces that were not specified in the classlist but
1676     // are implemented by K are not verified.
1677     link_and_cleanup_shared_classes(CATCH);
1678     tty->print_cr("Rewriting and linking classes: done");
1679 
1680     VM_PopulateDumpSharedSpace op;
1681     VMThread::execute(&op);
1682   }
1683 }
1684 
1685 
1686 int MetaspaceShared::preload_classes(const char* class_list_path, TRAPS) {
1687   ClassListParser parser(class_list_path);
1688   int class_count = 0;
1689 
1690   while (parser.parse_one_line()) {
1691     Klass* klass = parser.load_current_class(THREAD);
1692     if (HAS_PENDING_EXCEPTION) {
1693       if (klass == NULL &&
1694           (PENDING_EXCEPTION->klass()->name() == vmSymbols::java_lang_ClassNotFoundException())) {
1695         // print a warning only when the pending exception is class not found
1696         tty->print_cr("Preload Warning: Cannot find %s", parser.current_class_name());
1697       }
1698       CLEAR_PENDING_EXCEPTION;
1699     }
1700     if (klass != NULL) {
1701       if (log_is_enabled(Trace, cds)) {
1702         ResourceMark rm;
1703         log_trace(cds)("Shared spaces preloaded: %s", klass->external_name());
1704       }
1705 
1706       if (klass->is_instance_klass()) {
1707         InstanceKlass* ik = InstanceKlass::cast(klass);
1708 
1709         // Link the class to cause the bytecodes to be rewritten and the
1710         // cpcache to be created. The linking is done as soon as classes
1711         // are loaded in order that the related data structures (klass and
1712         // cpCache) are located together.
1713         try_link_class(ik, THREAD);
1714         guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1715       }
1716 
1717       class_count++;
1718     }
1719   }
1720 
1721   return class_count;
1722 }
1723 
1724 // Returns true if the class's status has changed
1725 bool MetaspaceShared::try_link_class(InstanceKlass* ik, TRAPS) {
1726   assert(DumpSharedSpaces, "should only be called during dumping");
1727   if (ik->init_state() < InstanceKlass::linked) {
1728     bool saved = BytecodeVerificationLocal;
1729     if (ik->loader_type() == 0 && ik->class_loader() == NULL) {
1730       // The verification decision is based on BytecodeVerificationRemote
1731       // for non-system classes. Since we are using the NULL classloader
1732       // to load non-system classes for customized class loaders during dumping,
1733       // we need to temporarily change BytecodeVerificationLocal to be the same as
1734       // BytecodeVerificationRemote. Note this can cause the parent system
1735       // classes also being verified. The extra overhead is acceptable during
1736       // dumping.
1737       BytecodeVerificationLocal = BytecodeVerificationRemote;
1738     }
1739     ik->link_class(THREAD);
1740     if (HAS_PENDING_EXCEPTION) {
1741       ResourceMark rm;
1742       tty->print_cr("Preload Warning: Verification failed for %s",
1743                     ik->external_name());
1744       CLEAR_PENDING_EXCEPTION;
1745       ik->set_in_error_state();
1746       _has_error_classes = true;
1747     }
1748     BytecodeVerificationLocal = saved;
1749     return true;
1750   } else {
1751     return false;
1752   }
1753 }
1754 
1755 #if INCLUDE_CDS_JAVA_HEAP
1756 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1757   // The closed and open archive heap space has maximum two regions.
1758   // See FileMapInfo::write_archive_heap_regions() for details.
1759   _closed_archive_heap_regions = new GrowableArray<MemRegion>(2);
1760   _open_archive_heap_regions = new GrowableArray<MemRegion>(2);
1761   HeapShared::archive_java_heap_objects(_closed_archive_heap_regions,
1762                                         _open_archive_heap_regions);
1763   ArchiveCompactor::OtherROAllocMark mark;
1764   HeapShared::write_subgraph_info_table();
1765 }
1766 
1767 void VM_PopulateDumpSharedSpace::dump_archive_heap_oopmaps() {
1768   if (HeapShared::is_heap_object_archiving_allowed()) {
1769     _closed_archive_heap_oopmaps = new GrowableArray<ArchiveHeapOopmapInfo>(2);
1770     dump_archive_heap_oopmaps(_closed_archive_heap_regions, _closed_archive_heap_oopmaps);
1771 
1772     _open_archive_heap_oopmaps = new GrowableArray<ArchiveHeapOopmapInfo>(2);
1773     dump_archive_heap_oopmaps(_open_archive_heap_regions, _open_archive_heap_oopmaps);
1774   }
1775 }
1776 
1777 void VM_PopulateDumpSharedSpace::dump_archive_heap_oopmaps(GrowableArray<MemRegion>* regions,
1778                                                            GrowableArray<ArchiveHeapOopmapInfo>* oopmaps) {
1779   for (int i=0; i<regions->length(); i++) {
1780     ResourceBitMap oopmap = HeapShared::calculate_oopmap(regions->at(i));
1781     size_t size_in_bits = oopmap.size();
1782     size_t size_in_bytes = oopmap.size_in_bytes();
1783     uintptr_t* buffer = (uintptr_t*)_ro_region.allocate(size_in_bytes, sizeof(intptr_t));
1784     oopmap.write_to(buffer, size_in_bytes);
1785     log_info(cds)("Oopmap = " INTPTR_FORMAT " (" SIZE_FORMAT_W(6) " bytes) for heap region "
1786                   INTPTR_FORMAT " (" SIZE_FORMAT_W(8) " bytes)",
1787                   p2i(buffer), size_in_bytes,
1788                   p2i(regions->at(i).start()), regions->at(i).byte_size());
1789 
1790     ArchiveHeapOopmapInfo info;
1791     info._oopmap = (address)buffer;
1792     info._oopmap_size_in_bits = size_in_bits;
1793     oopmaps->append(info);
1794   }
1795 }
1796 #endif // INCLUDE_CDS_JAVA_HEAP
1797 
1798 // Closure for serializing initialization data in from a data area
1799 // (ptr_array) read from the shared file.
1800 
1801 class ReadClosure : public SerializeClosure {
1802 private:
1803   intptr_t** _ptr_array;
1804 
1805   inline intptr_t nextPtr() {
1806     return *(*_ptr_array)++;
1807   }
1808 
1809 public:
1810   ReadClosure(intptr_t** ptr_array) { _ptr_array = ptr_array; }
1811 
1812   void do_ptr(void** p) {
1813     assert(*p == NULL, "initializing previous initialized pointer.");
1814     intptr_t obj = nextPtr();
1815     assert((intptr_t)obj >= 0 || (intptr_t)obj < -100,
1816            "hit tag while initializing ptrs.");
1817     *p = (void*)obj;
1818   }
1819 
1820   void do_u4(u4* p) {
1821     intptr_t obj = nextPtr();
1822     *p = (u4)(uintx(obj));
1823   }
1824 
1825   void do_tag(int tag) {
1826     int old_tag;
1827     old_tag = (int)(intptr_t)nextPtr();
1828     // do_int(&old_tag);
1829     assert(tag == old_tag, "old tag doesn't match");
1830     FileMapInfo::assert_mark(tag == old_tag);
1831   }
1832 
1833   void do_oop(oop *p) {
1834     narrowOop o = (narrowOop)nextPtr();
1835     if (o == 0 || !HeapShared::open_archive_heap_region_mapped()) {
1836       p = NULL;
1837     } else {
1838       assert(HeapShared::is_heap_object_archiving_allowed(),
1839              "Archived heap object is not allowed");
1840       assert(HeapShared::open_archive_heap_region_mapped(),
1841              "Open archive heap region is not mapped");
1842       *p = HeapShared::decode_from_archive(o);
1843     }
1844   }
1845 
1846   void do_region(u_char* start, size_t size) {
1847     assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
1848     assert(size % sizeof(intptr_t) == 0, "bad size");
1849     do_tag((int)size);
1850     while (size > 0) {
1851       *(intptr_t*)start = nextPtr();
1852       start += sizeof(intptr_t);
1853       size -= sizeof(intptr_t);
1854     }
1855   }
1856 
1857   bool reading() const { return true; }
1858 };
1859 
1860 // Return true if given address is in the misc data region
1861 bool MetaspaceShared::is_in_shared_region(const void* p, int idx) {
1862   return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_region(p, idx);
1863 }
1864 
1865 bool MetaspaceShared::is_in_trampoline_frame(address addr) {
1866   if (UseSharedSpaces && is_in_shared_region(addr, MetaspaceShared::mc)) {
1867     return true;
1868   }
1869   return false;
1870 }
1871 
1872 // Map shared spaces at requested addresses and return if succeeded.
1873 bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) {
1874   size_t image_alignment = mapinfo->alignment();
1875 
1876 #ifndef _WINDOWS
1877   // Map in the shared memory and then map the regions on top of it.
1878   // On Windows, don't map the memory here because it will cause the
1879   // mappings of the regions to fail.
1880   ReservedSpace shared_rs = mapinfo->reserve_shared_memory();
1881   if (!shared_rs.is_reserved()) return false;
1882 #endif
1883 
1884   assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces");
1885 
1886   char* ro_base = NULL; char* ro_top;
1887   char* rw_base = NULL; char* rw_top;
1888   char* mc_base = NULL; char* mc_top;
1889   char* md_base = NULL; char* md_top;
1890   char* od_base = NULL; char* od_top;
1891 
1892   // Map each shared region
1893   if ((mc_base = mapinfo->map_region(mc, &mc_top)) != NULL &&
1894       (rw_base = mapinfo->map_region(rw, &rw_top)) != NULL &&
1895       (ro_base = mapinfo->map_region(ro, &ro_top)) != NULL &&
1896       (md_base = mapinfo->map_region(md, &md_top)) != NULL &&
1897       (od_base = mapinfo->map_region(od, &od_top)) != NULL &&
1898       (image_alignment == (size_t)os::vm_allocation_granularity()) &&
1899       mapinfo->validate_shared_path_table()) {
1900     // Success -- set up MetaspaceObj::_shared_metaspace_{base,top} for
1901     // fast checking in MetaspaceShared::is_in_shared_metaspace() and
1902     // MetaspaceObj::is_shared().
1903     //
1904     // We require that mc->rw->ro->md->od to be laid out consecutively, with no
1905     // gaps between them. That way, we can ensure that the OS won't be able to
1906     // allocate any new memory spaces inside _shared_metaspace_{base,top}, which
1907     // would mess up the simple comparision in MetaspaceShared::is_in_shared_metaspace().
1908     assert(mc_base < ro_base && mc_base < rw_base && mc_base < md_base && mc_base < od_base, "must be");
1909     assert(od_top  > ro_top  && od_top  > rw_top  && od_top  > md_top  && od_top  > mc_top , "must be");
1910     assert(mc_top == rw_base, "must be");
1911     assert(rw_top == ro_base, "must be");
1912     assert(ro_top == md_base, "must be");
1913     assert(md_top == od_base, "must be");
1914 
1915     _core_spaces_size = mapinfo->core_spaces_size();
1916     MetaspaceObj::set_shared_metaspace_range((void*)mc_base, (void*)od_top);
1917     return true;
1918   } else {
1919     // If there was a failure in mapping any of the spaces, unmap the ones
1920     // that succeeded
1921     if (ro_base != NULL) mapinfo->unmap_region(ro);
1922     if (rw_base != NULL) mapinfo->unmap_region(rw);
1923     if (mc_base != NULL) mapinfo->unmap_region(mc);
1924     if (md_base != NULL) mapinfo->unmap_region(md);
1925     if (od_base != NULL) mapinfo->unmap_region(od);
1926 #ifndef _WINDOWS
1927     // Release the entire mapped region
1928     shared_rs.release();
1929 #endif
1930     // If -Xshare:on is specified, print out the error message and exit VM,
1931     // otherwise, set UseSharedSpaces to false and continue.
1932     if (RequireSharedSpaces || PrintSharedArchiveAndExit) {
1933       vm_exit_during_initialization("Unable to use shared archive.", "Failed map_region for using -Xshare:on.");
1934     } else {
1935       FLAG_SET_DEFAULT(UseSharedSpaces, false);
1936     }
1937     return false;
1938   }
1939 }
1940 
1941 // Read the miscellaneous data from the shared file, and
1942 // serialize it out to its various destinations.
1943 
1944 void MetaspaceShared::initialize_shared_spaces() {
1945   FileMapInfo *mapinfo = FileMapInfo::current_info();
1946   _cds_i2i_entry_code_buffers = mapinfo->cds_i2i_entry_code_buffers();
1947   _cds_i2i_entry_code_buffers_size = mapinfo->cds_i2i_entry_code_buffers_size();
1948   // _core_spaces_size is loaded from the shared archive immediatelly after mapping
1949   assert(_core_spaces_size == mapinfo->core_spaces_size(), "sanity");
1950   char* buffer = mapinfo->misc_data_patching_start();
1951   clone_cpp_vtables((intptr_t*)buffer);
1952 
1953   // The rest of the data is now stored in the RW region
1954   buffer = mapinfo->read_only_tables_start();
1955 
1956   // Verify various attributes of the archive, plus initialize the
1957   // shared string/symbol tables
1958   intptr_t* array = (intptr_t*)buffer;
1959   ReadClosure rc(&array);
1960   serialize(&rc);
1961 
1962   // Initialize the run-time symbol table.
1963   SymbolTable::create_table();
1964 
1965   mapinfo->patch_archived_heap_embedded_pointers();
1966 
1967   // Close the mapinfo file
1968   mapinfo->close();
1969 
1970   if (PrintSharedArchiveAndExit) {
1971     if (PrintSharedDictionary) {
1972       tty->print_cr("\nShared classes:\n");
1973       SystemDictionaryShared::print_on(tty);
1974     }
1975     if (_archive_loading_failed) {
1976       tty->print_cr("archive is invalid");
1977       vm_exit(1);
1978     } else {
1979       tty->print_cr("archive is valid");
1980       vm_exit(0);
1981     }
1982   }
1983 }
1984 
1985 // JVM/TI RedefineClasses() support:
1986 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
1987   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1988 
1989   if (UseSharedSpaces) {
1990     // remap the shared readonly space to shared readwrite, private
1991     FileMapInfo* mapinfo = FileMapInfo::current_info();
1992     if (!mapinfo->remap_shared_readonly_as_readwrite()) {
1993       return false;
1994     }
1995     _remapped_readwrite = true;
1996   }
1997   return true;
1998 }
1999 
2000 void MetaspaceShared::report_out_of_space(const char* name, size_t needed_bytes) {
2001   // This is highly unlikely to happen on 64-bits because we have reserved a 4GB space.
2002   // On 32-bit we reserve only 256MB so you could run out of space with 100,000 classes
2003   // or so.
2004   _mc_region.print_out_of_space_msg(name, needed_bytes);
2005   _rw_region.print_out_of_space_msg(name, needed_bytes);
2006   _ro_region.print_out_of_space_msg(name, needed_bytes);
2007   _md_region.print_out_of_space_msg(name, needed_bytes);
2008   _od_region.print_out_of_space_msg(name, needed_bytes);
2009 
2010   vm_exit_during_initialization(err_msg("Unable to allocate from '%s' region", name),
2011                                 "Please reduce the number of shared classes.");
2012 }