1 /*
   2  * Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "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/javaClasses.inline.hpp"
  33 #include "classfile/placeholders.hpp"
  34 #include "classfile/symbolTable.hpp"
  35 #include "classfile/stringTable.hpp"
  36 #include "classfile/systemDictionary.hpp"
  37 #include "classfile/systemDictionaryShared.hpp"
  38 #include "code/codeCache.hpp"
  39 #include "gc/shared/softRefPolicy.hpp"
  40 #include "interpreter/bytecodeStream.hpp"
  41 #include "interpreter/bytecodes.hpp"
  42 #include "logging/log.hpp"
  43 #include "logging/logMessage.hpp"
  44 #include "memory/archiveUtils.inline.hpp"
  45 #include "memory/dynamicArchive.hpp"
  46 #include "memory/filemap.hpp"
  47 #include "memory/heapShared.inline.hpp"
  48 #include "memory/metaspace.hpp"
  49 #include "memory/metaspaceClosure.hpp"
  50 #include "memory/metaspaceShared.hpp"
  51 #include "memory/resourceArea.hpp"
  52 #include "memory/universe.hpp"
  53 #include "oops/compressedOops.inline.hpp"
  54 #include "oops/instanceClassLoaderKlass.hpp"
  55 #include "oops/instanceMirrorKlass.hpp"
  56 #include "oops/instanceRefKlass.hpp"
  57 #include "oops/methodData.hpp"
  58 #include "oops/objArrayKlass.hpp"
  59 #include "oops/objArrayOop.hpp"
  60 #include "oops/oop.inline.hpp"
  61 #include "oops/typeArrayKlass.hpp"
  62 #include "prims/jvmtiRedefineClasses.hpp"
  63 #include "runtime/handles.inline.hpp"
  64 #include "runtime/os.hpp"
  65 #include "runtime/safepointVerifiers.hpp"
  66 #include "runtime/signature.hpp"
  67 #include "runtime/timerTrace.hpp"
  68 #include "runtime/vmThread.hpp"
  69 #include "runtime/vmOperations.hpp"
  70 #include "utilities/align.hpp"
  71 #include "utilities/bitMap.inline.hpp"
  72 #include "utilities/ostream.hpp"
  73 #include "utilities/defaultStream.hpp"
  74 #include "utilities/hashtable.inline.hpp"
  75 #if INCLUDE_G1GC
  76 #include "gc/g1/g1CollectedHeap.hpp"
  77 #endif
  78 
  79 ReservedSpace MetaspaceShared::_shared_rs;
  80 VirtualSpace MetaspaceShared::_shared_vs;
  81 ReservedSpace MetaspaceShared::_symbol_rs;
  82 VirtualSpace MetaspaceShared::_symbol_vs;
  83 MetaspaceSharedStats MetaspaceShared::_stats;
  84 bool MetaspaceShared::_has_error_classes;
  85 bool MetaspaceShared::_archive_loading_failed = false;
  86 bool MetaspaceShared::_remapped_readwrite = false;
  87 address MetaspaceShared::_i2i_entry_code_buffers = NULL;
  88 size_t MetaspaceShared::_i2i_entry_code_buffers_size = 0;
  89 void* MetaspaceShared::_shared_metaspace_static_top = NULL;
  90 intx MetaspaceShared::_relocation_delta;
  91 char* MetaspaceShared::_requested_base_address;
  92 bool MetaspaceShared::_use_optimized_module_handling = true;
  93 
  94 // The CDS archive is divided into the following regions:
  95 //     mc  - misc code (the method entry trampolines, c++ vtables)
  96 //     rw  - read-write metadata
  97 //     ro  - read-only metadata and read-only tables
  98 //
  99 //     ca0 - closed archive heap space #0
 100 //     ca1 - closed archive heap space #1 (may be empty)
 101 //     oa0 - open archive heap space #0
 102 //     oa1 - open archive heap space #1 (may be empty)
 103 //
 104 // The mc, rw, and ro regions are linearly allocated, starting from
 105 // SharedBaseAddress, in the order of mc->rw->ro. The size of these 3 regions
 106 // are page-aligned, and there's no gap between any consecutive regions.
 107 //
 108 // These 3 regions are populated in the following steps:
 109 // [1] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
 110 //     temporarily allocated outside of the shared regions. Only the method entry
 111 //     trampolines are written into the mc region.
 112 // [2] C++ vtables are copied into the mc region.
 113 // [3] ArchiveCompactor copies RW metadata into the rw region.
 114 // [4] ArchiveCompactor copies RO metadata into the ro region.
 115 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
 116 //     are copied into the ro region as read-only tables.
 117 //
 118 // The s0/s1 and oa0/oa1 regions are populated inside HeapShared::archive_java_heap_objects.
 119 // Their layout is independent of the other 4 regions.
 120 
 121 char* DumpRegion::expand_top_to(char* newtop) {
 122   assert(is_allocatable(), "must be initialized and not packed");
 123   assert(newtop >= _top, "must not grow backwards");
 124   if (newtop > _end) {
 125     MetaspaceShared::report_out_of_space(_name, newtop - _top);
 126     ShouldNotReachHere();
 127   }
 128 
 129   if (_rs == MetaspaceShared::shared_rs()) {
 130     uintx delta;
 131     if (DynamicDumpSharedSpaces) {
 132       delta = DynamicArchive::object_delta_uintx(newtop);
 133     } else {
 134       delta = MetaspaceShared::object_delta_uintx(newtop);
 135     }
 136     if (delta > MAX_SHARED_DELTA) {
 137       // This is just a sanity check and should not appear in any real world usage. This
 138       // happens only if you allocate more than 2GB of shared objects and would require
 139       // millions of shared classes.
 140       vm_exit_during_initialization("Out of memory in the CDS archive",
 141                                     "Please reduce the number of shared classes.");
 142     }
 143   }
 144 
 145   MetaspaceShared::commit_to(_rs, _vs, newtop);
 146   _top = newtop;
 147   return _top;
 148 }
 149 
 150 char* DumpRegion::allocate(size_t num_bytes, size_t alignment) {
 151   char* p = (char*)align_up(_top, alignment);
 152   char* newtop = p + align_up(num_bytes, alignment);
 153   expand_top_to(newtop);
 154   memset(p, 0, newtop - p);
 155   return p;
 156 }
 157 
 158 void DumpRegion::append_intptr_t(intptr_t n, bool need_to_mark) {
 159   assert(is_aligned(_top, sizeof(intptr_t)), "bad alignment");
 160   intptr_t *p = (intptr_t*)_top;
 161   char* newtop = _top + sizeof(intptr_t);
 162   expand_top_to(newtop);
 163   *p = n;
 164   if (need_to_mark) {
 165     ArchivePtrMarker::mark_pointer(p);
 166   }
 167 }
 168 
 169 void DumpRegion::print(size_t total_bytes) const {
 170   log_debug(cds)("%-3s space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used] at " INTPTR_FORMAT,
 171                  _name, used(), percent_of(used(), total_bytes), reserved(), percent_of(used(), reserved()),
 172                  p2i(_base + MetaspaceShared::final_delta()));
 173 }
 174 
 175 void DumpRegion::print_out_of_space_msg(const char* failing_region, size_t needed_bytes) {
 176   log_error(cds)("[%-8s] " PTR_FORMAT " - " PTR_FORMAT " capacity =%9d, allocated =%9d",
 177                  _name, p2i(_base), p2i(_top), int(_end - _base), int(_top - _base));
 178   if (strcmp(_name, failing_region) == 0) {
 179     log_error(cds)(" required = %d", int(needed_bytes));
 180   }
 181 }
 182 
 183 void DumpRegion::init(ReservedSpace* rs, VirtualSpace* vs) {
 184   _rs = rs;
 185   _vs = vs;
 186   // Start with 0 committed bytes. The memory will be committed as needed by
 187   // MetaspaceShared::commit_to().
 188   if (!_vs->initialize(*_rs, 0)) {
 189     fatal("Unable to allocate memory for shared space");
 190   }
 191   _base = _top = _rs->base();
 192   _end = _rs->end();
 193 }
 194 
 195 void DumpRegion::pack(DumpRegion* next) {
 196   assert(!is_packed(), "sanity");
 197   _end = (char*)align_up(_top, MetaspaceShared::reserved_space_alignment());
 198   _is_packed = true;
 199   if (next != NULL) {
 200     next->_rs = _rs;
 201     next->_vs = _vs;
 202     next->_base = next->_top = this->_end;
 203     next->_end = _rs->end();
 204   }
 205 }
 206 
 207 static DumpRegion _mc_region("mc"), _ro_region("ro"), _rw_region("rw"), _symbol_region("symbols");
 208 static size_t _total_closed_archive_region_size = 0, _total_open_archive_region_size = 0;
 209 
 210 void MetaspaceShared::init_shared_dump_space(DumpRegion* first_space) {
 211   first_space->init(&_shared_rs, &_shared_vs);
 212 }
 213 
 214 DumpRegion* MetaspaceShared::misc_code_dump_space() {
 215   return &_mc_region;
 216 }
 217 
 218 DumpRegion* MetaspaceShared::read_write_dump_space() {
 219   return &_rw_region;
 220 }
 221 
 222 DumpRegion* MetaspaceShared::read_only_dump_space() {
 223   return &_ro_region;
 224 }
 225 
 226 void MetaspaceShared::pack_dump_space(DumpRegion* current, DumpRegion* next,
 227                                       ReservedSpace* rs) {
 228   current->pack(next);
 229 }
 230 
 231 char* MetaspaceShared::symbol_space_alloc(size_t num_bytes) {
 232   return _symbol_region.allocate(num_bytes);
 233 }
 234 
 235 char* MetaspaceShared::misc_code_space_alloc(size_t num_bytes) {
 236   return _mc_region.allocate(num_bytes);
 237 }
 238 
 239 char* MetaspaceShared::read_only_space_alloc(size_t num_bytes) {
 240   return _ro_region.allocate(num_bytes);
 241 }
 242 
 243 size_t MetaspaceShared::reserved_space_alignment() { return os::vm_allocation_granularity(); }
 244 
 245 static bool shared_base_valid(char* shared_base) {
 246 #ifdef _LP64
 247   return CompressedKlassPointers::is_valid_base((address)shared_base);
 248 #else
 249   return true;
 250 #endif
 251 }
 252 
 253 static bool shared_base_too_high(char* shared_base, size_t cds_total) {
 254   if (SharedBaseAddress != 0 && shared_base < (char*)SharedBaseAddress) {
 255     // SharedBaseAddress is very high (e.g., 0xffffffffffffff00) so
 256     // align_up(SharedBaseAddress, MetaspaceShared::reserved_space_alignment()) has wrapped around.
 257     return true;
 258   }
 259   if (max_uintx - uintx(shared_base) < uintx(cds_total)) {
 260     // The end of the archive will wrap around
 261     return true;
 262   }
 263 
 264   return false;
 265 }
 266 
 267 static char* compute_shared_base(size_t cds_total) {
 268   char* shared_base = (char*)align_up((char*)SharedBaseAddress, MetaspaceShared::reserved_space_alignment());
 269   const char* err = NULL;
 270   if (shared_base_too_high(shared_base, cds_total)) {
 271     err = "too high";
 272   } else if (!shared_base_valid(shared_base)) {
 273     err = "invalid for this platform";
 274   }
 275   if (err) {
 276     log_warning(cds)("SharedBaseAddress (" INTPTR_FORMAT ") is %s. Reverted to " INTPTR_FORMAT,
 277                      p2i((void*)SharedBaseAddress), err,
 278                      p2i((void*)Arguments::default_SharedBaseAddress()));
 279     SharedBaseAddress = Arguments::default_SharedBaseAddress();
 280     shared_base = (char*)align_up((char*)SharedBaseAddress, MetaspaceShared::reserved_space_alignment());
 281   }
 282   assert(!shared_base_too_high(shared_base, cds_total) && shared_base_valid(shared_base), "Sanity");
 283   return shared_base;
 284 }
 285 
 286 void MetaspaceShared::initialize_dumptime_shared_and_meta_spaces() {
 287   assert(DumpSharedSpaces, "should be called for dump time only");
 288 
 289   const size_t reserve_alignment = MetaspaceShared::reserved_space_alignment();
 290 
 291 #ifdef _LP64
 292   // On 64-bit VM we reserve a 4G range and, if UseCompressedClassPointers=1,
 293   //  will use that to house both the archives and the ccs. See below for
 294   //  details.
 295   const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
 296   const size_t cds_total = align_down(UnscaledClassSpaceMax, reserve_alignment);
 297 #else
 298   // We don't support archives larger than 256MB on 32-bit due to limited
 299   //  virtual address space.
 300   size_t cds_total = align_down(256*M, reserve_alignment);
 301 #endif
 302 
 303   char* shared_base = compute_shared_base(cds_total);
 304   _requested_base_address = shared_base;
 305 
 306   // Whether to use SharedBaseAddress as attach address.
 307   bool use_requested_base = true;
 308 
 309   if (shared_base == NULL) {
 310     use_requested_base = false;
 311   }
 312 
 313   if (ArchiveRelocationMode == 1) {
 314     log_info(cds)("ArchiveRelocationMode == 1: always allocate class space at an alternative address");
 315     use_requested_base = false;
 316   }
 317 
 318   // First try to reserve the space at the specified SharedBaseAddress.
 319   assert(!_shared_rs.is_reserved(), "must be");
 320   if (use_requested_base) {
 321     _shared_rs = ReservedSpace(cds_total, reserve_alignment,
 322                                false /* large */, (char*)shared_base);
 323     if (_shared_rs.is_reserved()) {
 324       assert(_shared_rs.base() == shared_base, "should match");
 325     } else {
 326       log_info(cds)("dumptime space reservation: failed to map at "
 327                     "SharedBaseAddress " PTR_FORMAT, p2i(shared_base));
 328     }
 329   }
 330   if (!_shared_rs.is_reserved()) {
 331     // Get a reserved space anywhere if attaching at the SharedBaseAddress
 332     //  fails:
 333     if (UseCompressedClassPointers) {
 334       // If we need to reserve class space as well, let the platform handle
 335       //  the reservation.
 336       LP64_ONLY(_shared_rs =
 337                 Metaspace::reserve_address_space_for_compressed_classes(cds_total);)
 338       NOT_LP64(ShouldNotReachHere();)
 339     } else {
 340       // anywhere is fine.
 341       _shared_rs = ReservedSpace(cds_total, reserve_alignment,
 342                                  false /* large */, (char*)NULL);
 343     }
 344   }
 345 
 346   if (!_shared_rs.is_reserved()) {
 347     vm_exit_during_initialization("Unable to reserve memory for shared space",
 348                                   err_msg(SIZE_FORMAT " bytes.", cds_total));
 349   }
 350 
 351 #ifdef _LP64
 352 
 353   if (UseCompressedClassPointers) {
 354 
 355     assert(CompressedKlassPointers::is_valid_base((address)_shared_rs.base()), "Sanity");
 356 
 357     // On 64-bit VM, if UseCompressedClassPointers=1, the compressed class space
 358     //  must be allocated near the cds such as that the compressed Klass pointer
 359     //  encoding can be used to en/decode pointers from both cds and ccs. Since
 360     //  Metaspace cannot do this (it knows nothing about cds), we do it for
 361     //  Metaspace here and pass it the space to use for ccs.
 362     //
 363     // We do this by reserving space for the ccs behind the archives. Note
 364     //  however that ccs follows a different alignment
 365     //  (Metaspace::reserve_alignment), so there may be a gap between ccs and
 366     //  cds.
 367     // We use a similar layout at runtime, see reserve_address_space_for_archives().
 368     //
 369     //                              +-- SharedBaseAddress (default = 0x800000000)
 370     //                              v
 371     // +-..---------+---------+ ... +----+----+----+--------+-----------------+
 372     // |    Heap    | Archive |     | MC | RW | RO | [gap]  |    class space  |
 373     // +-..---------+---------+ ... +----+----+----+--------+-----------------+
 374     // |<--   MaxHeapSize  -->|     |<-- UnscaledClassSpaceMax = 4GB -->|
 375     //
 376     // Note: ccs must follow the archives, and the archives must start at the
 377     //  encoding base. However, the exact placement of ccs does not matter as
 378     //  long as it it resides in the encoding range of CompressedKlassPointers
 379     //  and comes after the archive.
 380     //
 381     // We do this by splitting up the allocated 4G into 3G of archive space,
 382     //  followed by 1G for the ccs:
 383     // + The upper 1 GB is used as the "temporary compressed class space"
 384     //   -- preload_classes() will store Klasses into this space.
 385     // + The lower 3 GB is used for the archive -- when preload_classes()
 386     //   is done, ArchiveCompactor will copy the class metadata into this
 387     //   space, first the RW parts, then the RO parts.
 388 
 389     // Starting address of ccs must be aligned to Metaspace::reserve_alignment()...
 390     size_t class_space_size = align_down(_shared_rs.size() / 4, Metaspace::reserve_alignment());
 391     address class_space_start = (address)align_down(_shared_rs.end() - class_space_size, Metaspace::reserve_alignment());
 392     size_t archive_size = class_space_start - (address)_shared_rs.base();
 393 
 394     ReservedSpace tmp_class_space = _shared_rs.last_part(archive_size);
 395     _shared_rs = _shared_rs.first_part(archive_size);
 396 
 397     // ... as does the size of ccs.
 398     tmp_class_space = tmp_class_space.first_part(class_space_size);
 399     CompressedClassSpaceSize = class_space_size;
 400 
 401     // Let Metaspace initialize ccs
 402     Metaspace::initialize_class_space(tmp_class_space);
 403 
 404     // and set up CompressedKlassPointers encoding.
 405     CompressedKlassPointers::initialize((address)_shared_rs.base(), cds_total);
 406 
 407     log_info(cds)("narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
 408                   p2i(CompressedKlassPointers::base()), CompressedKlassPointers::shift());
 409 
 410     log_info(cds)("Allocated temporary class space: " SIZE_FORMAT " bytes at " PTR_FORMAT,
 411                   CompressedClassSpaceSize, p2i(tmp_class_space.base()));
 412 
 413     assert(_shared_rs.end() == tmp_class_space.base() &&
 414            is_aligned(_shared_rs.base(), MetaspaceShared::reserved_space_alignment()) &&
 415            is_aligned(tmp_class_space.base(), Metaspace::reserve_alignment()) &&
 416            is_aligned(tmp_class_space.size(), Metaspace::reserve_alignment()), "Sanity");
 417   }
 418 
 419 #endif
 420 
 421   init_shared_dump_space(&_mc_region);
 422   SharedBaseAddress = (size_t)_shared_rs.base();
 423   log_info(cds)("Allocated shared space: " SIZE_FORMAT " bytes at " PTR_FORMAT,
 424                 _shared_rs.size(), p2i(_shared_rs.base()));
 425 
 426   // We don't want any valid object to be at the very bottom of the archive.
 427   // See ArchivePtrMarker::mark_pointer().
 428   MetaspaceShared::misc_code_space_alloc(16);
 429 
 430   size_t symbol_rs_size = LP64_ONLY(3 * G) NOT_LP64(128 * M);
 431   _symbol_rs = ReservedSpace(symbol_rs_size);
 432   if (!_symbol_rs.is_reserved()) {
 433     vm_exit_during_initialization("Unable to reserve memory for symbols",
 434                                   err_msg(SIZE_FORMAT " bytes.", symbol_rs_size));
 435   }
 436   _symbol_region.init(&_symbol_rs, &_symbol_vs);
 437 }
 438 
 439 // Called by universe_post_init()
 440 void MetaspaceShared::post_initialize(TRAPS) {
 441   if (UseSharedSpaces) {
 442     int size = FileMapInfo::get_number_of_shared_paths();
 443     if (size > 0) {
 444       SystemDictionaryShared::allocate_shared_data_arrays(size, THREAD);
 445       if (!DynamicDumpSharedSpaces) {
 446         FileMapInfo* info;
 447         if (FileMapInfo::dynamic_info() == NULL) {
 448           info = FileMapInfo::current_info();
 449         } else {
 450           info = FileMapInfo::dynamic_info();
 451         }
 452         ClassLoaderExt::init_paths_start_index(info->app_class_paths_start_index());
 453         ClassLoaderExt::init_app_module_paths_start_index(info->app_module_paths_start_index());
 454       }
 455     }
 456   }
 457 }
 458 
 459 static GrowableArrayCHeap<Handle, mtClassShared>* _extra_interned_strings = NULL;
 460 
 461 void MetaspaceShared::read_extra_data(const char* filename, TRAPS) {
 462   _extra_interned_strings = new GrowableArrayCHeap<Handle, mtClassShared>(10000);
 463 
 464   HashtableTextDump reader(filename);
 465   reader.check_version("VERSION: 1.0");
 466 
 467   while (reader.remain() > 0) {
 468     int utf8_length;
 469     int prefix_type = reader.scan_prefix(&utf8_length);
 470     ResourceMark rm(THREAD);
 471     if (utf8_length == 0x7fffffff) {
 472       // buf_len will overflown 32-bit value.
 473       vm_exit_during_initialization(err_msg("string length too large: %d", utf8_length));
 474     }
 475     int buf_len = utf8_length+1;
 476     char* utf8_buffer = NEW_RESOURCE_ARRAY(char, buf_len);
 477     reader.get_utf8(utf8_buffer, utf8_length);
 478     utf8_buffer[utf8_length] = '\0';
 479 
 480     if (prefix_type == HashtableTextDump::SymbolPrefix) {
 481       SymbolTable::new_permanent_symbol(utf8_buffer);
 482     } else{
 483       assert(prefix_type == HashtableTextDump::StringPrefix, "Sanity");
 484       oop s = StringTable::intern(utf8_buffer, THREAD);
 485 
 486       if (HAS_PENDING_EXCEPTION) {
 487         log_warning(cds, heap)("[line %d] extra interned string allocation failed; size too large: %d",
 488                                reader.last_line_no(), utf8_length);
 489         CLEAR_PENDING_EXCEPTION;
 490       } else {
 491 #if INCLUDE_G1GC
 492         if (UseG1GC) {
 493           typeArrayOop body = java_lang_String::value(s);
 494           const HeapRegion* hr = G1CollectedHeap::heap()->heap_region_containing(body);
 495           if (hr->is_humongous()) {
 496             // Don't keep it alive, so it will be GC'ed before we dump the strings, in order
 497             // to maximize free heap space and minimize fragmentation.
 498             log_warning(cds, heap)("[line %d] extra interned string ignored; size too large: %d",
 499                                 reader.last_line_no(), utf8_length);
 500             continue;
 501           }
 502         }
 503 #endif
 504         // Interned strings are GC'ed if there are no references to it, so let's
 505         // add a reference to keep this string alive.
 506         assert(s != NULL, "must succeed");
 507         Handle h(THREAD, s);
 508         _extra_interned_strings->append(h);
 509       }
 510     }
 511   }
 512 }
 513 
 514 void MetaspaceShared::commit_to(ReservedSpace* rs, VirtualSpace* vs, char* newtop) {
 515   Arguments::assert_is_dumping_archive();
 516   char* base = rs->base();
 517   size_t need_committed_size = newtop - base;
 518   size_t has_committed_size = vs->committed_size();
 519   if (need_committed_size < has_committed_size) {
 520     return;
 521   }
 522 
 523   size_t min_bytes = need_committed_size - has_committed_size;
 524   size_t preferred_bytes = 1 * M;
 525   size_t uncommitted = vs->reserved_size() - has_committed_size;
 526 
 527   size_t commit =MAX2(min_bytes, preferred_bytes);
 528   commit = MIN2(commit, uncommitted);
 529   assert(commit <= uncommitted, "sanity");
 530 
 531   bool result = vs->expand_by(commit, false);
 532   if (rs == &_shared_rs) {
 533     ArchivePtrMarker::expand_ptr_end((address*)vs->high());
 534   }
 535 
 536   if (!result) {
 537     vm_exit_during_initialization(err_msg("Failed to expand shared space to " SIZE_FORMAT " bytes",
 538                                           need_committed_size));
 539   }
 540 
 541   assert(rs == &_shared_rs || rs == &_symbol_rs, "must be");
 542   const char* which = (rs == &_shared_rs) ? "shared" : "symbol";
 543   log_debug(cds)("Expanding %s spaces by " SIZE_FORMAT_W(7) " bytes [total " SIZE_FORMAT_W(9)  " bytes ending at %p]",
 544                  which, commit, vs->actual_committed_size(), vs->high());
 545 }
 546 
 547 void MetaspaceShared::initialize_ptr_marker(CHeapBitMap* ptrmap) {
 548   ArchivePtrMarker::initialize(ptrmap, (address*)_shared_vs.low(), (address*)_shared_vs.high());
 549 }
 550 
 551 // Read/write a data stream for restoring/preserving metadata pointers and
 552 // miscellaneous data from/to the shared archive file.
 553 
 554 void MetaspaceShared::serialize(SerializeClosure* soc) {
 555   int tag = 0;
 556   soc->do_tag(--tag);
 557 
 558   // Verify the sizes of various metadata in the system.
 559   soc->do_tag(sizeof(Method));
 560   soc->do_tag(sizeof(ConstMethod));
 561   soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE));
 562   soc->do_tag(sizeof(ConstantPool));
 563   soc->do_tag(sizeof(ConstantPoolCache));
 564   soc->do_tag(objArrayOopDesc::base_offset_in_bytes());
 565   soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE));
 566   soc->do_tag(sizeof(Symbol));
 567 
 568   // Dump/restore miscellaneous metadata.
 569   JavaClasses::serialize_offsets(soc);
 570   Universe::serialize(soc);
 571   soc->do_tag(--tag);
 572 
 573   // Dump/restore references to commonly used names and signatures.
 574   vmSymbols::serialize(soc);
 575   soc->do_tag(--tag);
 576 
 577   // Dump/restore the symbol/string/subgraph_info tables
 578   SymbolTable::serialize_shared_table_header(soc);
 579   StringTable::serialize_shared_table_header(soc);
 580   HeapShared::serialize_subgraph_info_table_header(soc);
 581   SystemDictionaryShared::serialize_dictionary_headers(soc);
 582 
 583   InstanceMirrorKlass::serialize_offsets(soc);
 584 
 585   // Dump/restore well known classes (pointers)
 586   SystemDictionaryShared::serialize_well_known_klasses(soc);
 587   soc->do_tag(--tag);
 588 
 589   serialize_cloned_cpp_vtptrs(soc);
 590   soc->do_tag(--tag);
 591 
 592   soc->do_tag(666);
 593 }
 594 
 595 address MetaspaceShared::i2i_entry_code_buffers(size_t total_size) {
 596   if (DumpSharedSpaces) {
 597     if (_i2i_entry_code_buffers == NULL) {
 598       _i2i_entry_code_buffers = (address)misc_code_space_alloc(total_size);
 599       _i2i_entry_code_buffers_size = total_size;
 600     }
 601   } else if (UseSharedSpaces) {
 602     assert(_i2i_entry_code_buffers != NULL, "must already been initialized");
 603   } else {
 604     return NULL;
 605   }
 606 
 607   assert(_i2i_entry_code_buffers_size == total_size, "must not change");
 608   return _i2i_entry_code_buffers;
 609 }
 610 
 611 uintx MetaspaceShared::object_delta_uintx(void* obj) {
 612   Arguments::assert_is_dumping_archive();
 613   if (DumpSharedSpaces) {
 614     assert(shared_rs()->contains(obj), "must be");
 615   } else {
 616     assert(is_in_shared_metaspace(obj) || DynamicArchive::is_in_target_space(obj), "must be");
 617   }
 618   address base_address = address(SharedBaseAddress);
 619   uintx deltax = address(obj) - base_address;
 620   return deltax;
 621 }
 622 
 623 // Global object for holding classes that have been loaded.  Since this
 624 // is run at a safepoint just before exit, this is the entire set of classes.
 625 static GrowableArray<Klass*>* _global_klass_objects;
 626 
 627 static int global_klass_compare(Klass** a, Klass **b) {
 628   return a[0]->name()->fast_compare(b[0]->name());
 629 }
 630 
 631 GrowableArray<Klass*>* MetaspaceShared::collected_klasses() {
 632   return _global_klass_objects;
 633 }
 634 
 635 static void collect_array_classes(Klass* k) {
 636   _global_klass_objects->append_if_missing(k);
 637   if (k->is_array_klass()) {
 638     // Add in the array classes too
 639     ArrayKlass* ak = ArrayKlass::cast(k);
 640     Klass* h = ak->higher_dimension();
 641     if (h != NULL) {
 642       h->array_klasses_do(collect_array_classes);
 643     }
 644   }
 645 }
 646 
 647 class CollectClassesClosure : public KlassClosure {
 648   void do_klass(Klass* k) {
 649     if (k->is_instance_klass() &&
 650         SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(k))) {
 651       // Don't add to the _global_klass_objects
 652     } else {
 653       _global_klass_objects->append_if_missing(k);
 654     }
 655     if (k->is_array_klass()) {
 656       // Add in the array classes too
 657       ArrayKlass* ak = ArrayKlass::cast(k);
 658       Klass* h = ak->higher_dimension();
 659       if (h != NULL) {
 660         h->array_klasses_do(collect_array_classes);
 661       }
 662     }
 663   }
 664 };
 665 
 666 // Global object for holding symbols that created during class loading. See SymbolTable::new_symbol
 667 static GrowableArray<Symbol*>* _global_symbol_objects = NULL;
 668 
 669 static int compare_symbols_by_address(Symbol** a, Symbol** b) {
 670   if (a[0] < b[0]) {
 671     return -1;
 672   } else if (a[0] == b[0]) {
 673     ResourceMark rm;
 674     log_warning(cds)("Duplicated symbol %s unexpected", (*a)->as_C_string());
 675     return 0;
 676   } else {
 677     return 1;
 678   }
 679 }
 680 
 681 void MetaspaceShared::add_symbol(Symbol* sym) {
 682   MutexLocker ml(CDSAddSymbol_lock, Mutex::_no_safepoint_check_flag);
 683   if (_global_symbol_objects == NULL) {
 684     _global_symbol_objects = new (ResourceObj::C_HEAP, mtSymbol) GrowableArray<Symbol*>(2048, mtSymbol);
 685   }
 686   _global_symbol_objects->append(sym);
 687 }
 688 
 689 GrowableArray<Symbol*>* MetaspaceShared::collected_symbols() {
 690   return _global_symbol_objects;
 691 }
 692 
 693 static void remove_unshareable_in_classes() {
 694   for (int i = 0; i < _global_klass_objects->length(); i++) {
 695     Klass* k = _global_klass_objects->at(i);
 696     if (!k->is_objArray_klass()) {
 697       // InstanceKlass and TypeArrayKlass will in turn call remove_unshareable_info
 698       // on their array classes.
 699       assert(k->is_instance_klass() || k->is_typeArray_klass(), "must be");
 700       k->remove_unshareable_info();
 701     }
 702   }
 703 }
 704 
 705 static void remove_java_mirror_in_classes() {
 706   for (int i = 0; i < _global_klass_objects->length(); i++) {
 707     Klass* k = _global_klass_objects->at(i);
 708     if (!k->is_objArray_klass()) {
 709       // InstanceKlass and TypeArrayKlass will in turn call remove_unshareable_info
 710       // on their array classes.
 711       assert(k->is_instance_klass() || k->is_typeArray_klass(), "must be");
 712       k->remove_java_mirror();
 713     }
 714   }
 715 }
 716 
 717 static void rewrite_nofast_bytecode(const methodHandle& method) {
 718   BytecodeStream bcs(method);
 719   while (!bcs.is_last_bytecode()) {
 720     Bytecodes::Code opcode = bcs.next();
 721     switch (opcode) {
 722     case Bytecodes::_getfield:      *bcs.bcp() = Bytecodes::_nofast_getfield;      break;
 723     case Bytecodes::_putfield:      *bcs.bcp() = Bytecodes::_nofast_putfield;      break;
 724     case Bytecodes::_aload_0:       *bcs.bcp() = Bytecodes::_nofast_aload_0;       break;
 725     case Bytecodes::_iload: {
 726       if (!bcs.is_wide()) {
 727         *bcs.bcp() = Bytecodes::_nofast_iload;
 728       }
 729       break;
 730     }
 731     default: break;
 732     }
 733   }
 734 }
 735 
 736 // Walk all methods in the class list to ensure that they won't be modified at
 737 // run time. This includes:
 738 // [1] Rewrite all bytecodes as needed, so that the ConstMethod* will not be modified
 739 //     at run time by RewriteBytecodes/RewriteFrequentPairs
 740 // [2] Assign a fingerprint, so one doesn't need to be assigned at run-time.
 741 static void rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread* thread) {
 742   for (int i = 0; i < _global_klass_objects->length(); i++) {
 743     Klass* k = _global_klass_objects->at(i);
 744     if (k->is_instance_klass()) {
 745       InstanceKlass* ik = InstanceKlass::cast(k);
 746       MetaspaceShared::rewrite_nofast_bytecodes_and_calculate_fingerprints(thread, ik);
 747     }
 748   }
 749 }
 750 
 751 void MetaspaceShared::rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread* thread, InstanceKlass* ik) {
 752   for (int i = 0; i < ik->methods()->length(); i++) {
 753     methodHandle m(thread, ik->methods()->at(i));
 754     rewrite_nofast_bytecode(m);
 755     Fingerprinter fp(m);
 756     // The side effect of this call sets method's fingerprint field.
 757     fp.fingerprint();
 758   }
 759 }
 760 
 761 // Objects of the Metadata types (such as Klass and ConstantPool) have C++ vtables.
 762 // (In GCC this is the field <Type>::_vptr, i.e., first word in the object.)
 763 //
 764 // Addresses of the vtables and the methods may be different across JVM runs,
 765 // if libjvm.so is dynamically loaded at a different base address.
 766 //
 767 // To ensure that the Metadata objects in the CDS archive always have the correct vtable:
 768 //
 769 // + at dump time:  we redirect the _vptr to point to our own vtables inside
 770 //                  the CDS image
 771 // + at run time:   we clone the actual contents of the vtables from libjvm.so
 772 //                  into our own tables.
 773 
 774 // Currently, the archive contain ONLY the following types of objects that have C++ vtables.
 775 #define CPP_VTABLE_PATCH_TYPES_DO(f) \
 776   f(ConstantPool) \
 777   f(InstanceKlass) \
 778   f(InstanceClassLoaderKlass) \
 779   f(InstanceMirrorKlass) \
 780   f(InstanceRefKlass) \
 781   f(Method) \
 782   f(ObjArrayKlass) \
 783   f(TypeArrayKlass)
 784 
 785 class CppVtableInfo {
 786   intptr_t _vtable_size;
 787   intptr_t _cloned_vtable[1];
 788 public:
 789   static int num_slots(int vtable_size) {
 790     return 1 + vtable_size; // Need to add the space occupied by _vtable_size;
 791   }
 792   int vtable_size()           { return int(uintx(_vtable_size)); }
 793   void set_vtable_size(int n) { _vtable_size = intptr_t(n); }
 794   intptr_t* cloned_vtable()   { return &_cloned_vtable[0]; }
 795   void zero()                 { memset(_cloned_vtable, 0, sizeof(intptr_t) * vtable_size()); }
 796   // Returns the address of the next CppVtableInfo that can be placed immediately after this CppVtableInfo
 797   static size_t byte_size(int vtable_size) {
 798     CppVtableInfo i;
 799     return pointer_delta(&i._cloned_vtable[vtable_size], &i, sizeof(u1));
 800   }
 801 };
 802 
 803 static inline intptr_t* vtable_of(Metadata* m) {
 804   return *((intptr_t**)m);
 805 }
 806 
 807 template <class T> class CppVtableCloner : public T {
 808   static CppVtableInfo* _info;
 809 
 810   static int get_vtable_length(const char* name);
 811 
 812 public:
 813   // Allocate and initialize the C++ vtable, starting from top, but do not go past end.
 814   static intptr_t* allocate(const char* name);
 815 
 816   // Clone the vtable to ...
 817   static intptr_t* clone_vtable(const char* name, CppVtableInfo* info);
 818 
 819   static void zero_vtable_clone() {
 820     assert(DumpSharedSpaces, "dump-time only");
 821     _info->zero();
 822   }
 823 
 824   static bool is_valid_shared_object(const T* obj) {
 825     intptr_t* vptr = *(intptr_t**)obj;
 826     return vptr == _info->cloned_vtable();
 827   }
 828 
 829   static void init_orig_cpp_vtptr(int kind);
 830 };
 831 
 832 template <class T> CppVtableInfo* CppVtableCloner<T>::_info = NULL;
 833 
 834 template <class T>
 835 intptr_t* CppVtableCloner<T>::allocate(const char* name) {
 836   assert(is_aligned(_mc_region.top(), sizeof(intptr_t)), "bad alignment");
 837   int n = get_vtable_length(name);
 838   _info = (CppVtableInfo*)_mc_region.allocate(CppVtableInfo::byte_size(n), sizeof(intptr_t));
 839   _info->set_vtable_size(n);
 840 
 841   intptr_t* p = clone_vtable(name, _info);
 842   assert((char*)p == _mc_region.top(), "must be");
 843 
 844   return _info->cloned_vtable();
 845 }
 846 
 847 template <class T>
 848 intptr_t* CppVtableCloner<T>::clone_vtable(const char* name, CppVtableInfo* info) {
 849   if (!DumpSharedSpaces) {
 850     assert(_info == 0, "_info is initialized only at dump time");
 851     _info = info; // Remember it -- it will be used by MetaspaceShared::is_valid_shared_method()
 852   }
 853   T tmp; // Allocate temporary dummy metadata object to get to the original vtable.
 854   int n = info->vtable_size();
 855   intptr_t* srcvtable = vtable_of(&tmp);
 856   intptr_t* dstvtable = info->cloned_vtable();
 857 
 858   // We already checked (and, if necessary, adjusted n) when the vtables were allocated, so we are
 859   // safe to do memcpy.
 860   log_debug(cds, vtables)("Copying %3d vtable entries for %s", n, name);
 861   memcpy(dstvtable, srcvtable, sizeof(intptr_t) * n);
 862   return dstvtable + n;
 863 }
 864 
 865 // To determine the size of the vtable for each type, we use the following
 866 // trick by declaring 2 subclasses:
 867 //
 868 //   class CppVtableTesterA: public InstanceKlass {virtual int   last_virtual_method() {return 1;}    };
 869 //   class CppVtableTesterB: public InstanceKlass {virtual void* last_virtual_method() {return NULL}; };
 870 //
 871 // CppVtableTesterA and CppVtableTesterB's vtables have the following properties:
 872 // - Their size (N+1) is exactly one more than the size of InstanceKlass's vtable (N)
 873 // - The first N entries have are exactly the same as in InstanceKlass's vtable.
 874 // - Their last entry is different.
 875 //
 876 // So to determine the value of N, we just walk CppVtableTesterA and CppVtableTesterB's tables
 877 // and find the first entry that's different.
 878 //
 879 // This works on all C++ compilers supported by Oracle, but you may need to tweak it for more
 880 // esoteric compilers.
 881 
 882 template <class T> class CppVtableTesterB: public T {
 883 public:
 884   virtual int last_virtual_method() {return 1;}
 885 };
 886 
 887 template <class T> class CppVtableTesterA : public T {
 888 public:
 889   virtual void* last_virtual_method() {
 890     // Make this different than CppVtableTesterB::last_virtual_method so the C++
 891     // compiler/linker won't alias the two functions.
 892     return NULL;
 893   }
 894 };
 895 
 896 template <class T>
 897 int CppVtableCloner<T>::get_vtable_length(const char* name) {
 898   CppVtableTesterA<T> a;
 899   CppVtableTesterB<T> b;
 900 
 901   intptr_t* avtable = vtable_of(&a);
 902   intptr_t* bvtable = vtable_of(&b);
 903 
 904   // Start at slot 1, because slot 0 may be RTTI (on Solaris/Sparc)
 905   int vtable_len = 1;
 906   for (; ; vtable_len++) {
 907     if (avtable[vtable_len] != bvtable[vtable_len]) {
 908       break;
 909     }
 910   }
 911   log_debug(cds, vtables)("Found   %3d vtable entries for %s", vtable_len, name);
 912 
 913   return vtable_len;
 914 }
 915 
 916 #define ALLOC_CPP_VTABLE_CLONE(c) \
 917   _cloned_cpp_vtptrs[c##_Kind] = CppVtableCloner<c>::allocate(#c); \
 918   ArchivePtrMarker::mark_pointer(&_cloned_cpp_vtptrs[c##_Kind]);
 919 
 920 #define CLONE_CPP_VTABLE(c) \
 921   p = CppVtableCloner<c>::clone_vtable(#c, (CppVtableInfo*)p);
 922 
 923 #define ZERO_CPP_VTABLE(c) \
 924  CppVtableCloner<c>::zero_vtable_clone();
 925 
 926 #define INIT_ORIG_CPP_VTPTRS(c) \
 927   CppVtableCloner<c>::init_orig_cpp_vtptr(c##_Kind);
 928 
 929 #define DECLARE_CLONED_VTABLE_KIND(c) c ## _Kind,
 930 
 931 enum ClonedVtableKind {
 932   // E.g., ConstantPool_Kind == 0, InstanceKlass_Kind == 1, etc.
 933   CPP_VTABLE_PATCH_TYPES_DO(DECLARE_CLONED_VTABLE_KIND)
 934   _num_cloned_vtable_kinds
 935 };
 936 
 937 // This is a map of all the original vtptrs. E.g., for
 938 //     ConstantPool *cp = new (...) ConstantPool(...) ; // a dynamically allocated constant pool
 939 // the following holds true:
 940 //     _orig_cpp_vtptrs[ConstantPool_Kind] ==  ((intptr_t**)cp)[0]
 941 static intptr_t* _orig_cpp_vtptrs[_num_cloned_vtable_kinds];
 942 static bool _orig_cpp_vtptrs_inited = false;
 943 
 944 template <class T>
 945 void CppVtableCloner<T>::init_orig_cpp_vtptr(int kind) {
 946   assert(kind < _num_cloned_vtable_kinds, "sanity");
 947   T tmp; // Allocate temporary dummy metadata object to get to the original vtable.
 948   intptr_t* srcvtable = vtable_of(&tmp);
 949   _orig_cpp_vtptrs[kind] = srcvtable;
 950 }
 951 
 952 // This is the index of all the cloned vtables. E.g., for
 953 //     ConstantPool* cp = ....; // an archived constant pool
 954 //     InstanceKlass* ik = ....;// an archived class
 955 // the following holds true:
 956 //     _cloned_cpp_vtptrs[ConstantPool_Kind]  == ((intptr_t**)cp)[0]
 957 //     _cloned_cpp_vtptrs[InstanceKlass_Kind] == ((intptr_t**)ik)[0]
 958 static intptr_t** _cloned_cpp_vtptrs = NULL;
 959 
 960 void MetaspaceShared::allocate_cloned_cpp_vtptrs() {
 961   assert(DumpSharedSpaces, "must");
 962   size_t vtptrs_bytes = _num_cloned_vtable_kinds * sizeof(intptr_t*);
 963   _cloned_cpp_vtptrs = (intptr_t**)_mc_region.allocate(vtptrs_bytes, sizeof(intptr_t*));
 964 }
 965 
 966 void MetaspaceShared::serialize_cloned_cpp_vtptrs(SerializeClosure* soc) {
 967   soc->do_ptr((void**)&_cloned_cpp_vtptrs);
 968 }
 969 
 970 intptr_t* MetaspaceShared::get_archived_cpp_vtable(MetaspaceObj::Type msotype, address obj) {
 971   if (!_orig_cpp_vtptrs_inited) {
 972     CPP_VTABLE_PATCH_TYPES_DO(INIT_ORIG_CPP_VTPTRS);
 973     _orig_cpp_vtptrs_inited = true;
 974   }
 975 
 976   Arguments::assert_is_dumping_archive();
 977   int kind = -1;
 978   switch (msotype) {
 979   case MetaspaceObj::SymbolType:
 980   case MetaspaceObj::TypeArrayU1Type:
 981   case MetaspaceObj::TypeArrayU2Type:
 982   case MetaspaceObj::TypeArrayU4Type:
 983   case MetaspaceObj::TypeArrayU8Type:
 984   case MetaspaceObj::TypeArrayOtherType:
 985   case MetaspaceObj::ConstMethodType:
 986   case MetaspaceObj::ConstantPoolCacheType:
 987   case MetaspaceObj::AnnotationsType:
 988   case MetaspaceObj::MethodCountersType:
 989   case MetaspaceObj::RecordComponentType:
 990     // These have no vtables.
 991     break;
 992   case MetaspaceObj::MethodDataType:
 993     // We don't archive MethodData <-- should have been removed in removed_unsharable_info
 994     ShouldNotReachHere();
 995     break;
 996   default:
 997     for (kind = 0; kind < _num_cloned_vtable_kinds; kind ++) {
 998       if (vtable_of((Metadata*)obj) == _orig_cpp_vtptrs[kind]) {
 999         break;
1000       }
1001     }
1002     if (kind >= _num_cloned_vtable_kinds) {
1003       fatal("Cannot find C++ vtable for " INTPTR_FORMAT " -- you probably added"
1004             " a new subtype of Klass or MetaData without updating CPP_VTABLE_PATCH_TYPES_DO",
1005             p2i(obj));
1006     }
1007   }
1008 
1009   if (kind >= 0) {
1010     assert(kind < _num_cloned_vtable_kinds, "must be");
1011     return _cloned_cpp_vtptrs[kind];
1012   } else {
1013     return NULL;
1014   }
1015 }
1016 
1017 // This can be called at both dump time and run time:
1018 // - clone the contents of the c++ vtables into the space
1019 //   allocated by allocate_cpp_vtable_clones()
1020 void MetaspaceShared::clone_cpp_vtables(intptr_t* p) {
1021   assert(DumpSharedSpaces || UseSharedSpaces, "sanity");
1022   CPP_VTABLE_PATCH_TYPES_DO(CLONE_CPP_VTABLE);
1023 }
1024 
1025 void MetaspaceShared::zero_cpp_vtable_clones_for_writing() {
1026   assert(DumpSharedSpaces, "dump-time only");
1027   CPP_VTABLE_PATCH_TYPES_DO(ZERO_CPP_VTABLE);
1028 }
1029 
1030 // Allocate and initialize the C++ vtables, starting from top, but do not go past end.
1031 char* MetaspaceShared::allocate_cpp_vtable_clones() {
1032   char* cloned_vtables = _mc_region.top(); // This is the beginning of all the cloned vtables
1033 
1034   assert(DumpSharedSpaces, "dump-time only");
1035   // Layout (each slot is a intptr_t):
1036   //   [number of slots in the first vtable = n1]
1037   //   [ <n1> slots for the first vtable]
1038   //   [number of slots in the first second = n2]
1039   //   [ <n2> slots for the second vtable]
1040   //   ...
1041   // The order of the vtables is the same as the CPP_VTAB_PATCH_TYPES_DO macro.
1042   CPP_VTABLE_PATCH_TYPES_DO(ALLOC_CPP_VTABLE_CLONE);
1043 
1044   return cloned_vtables;
1045 }
1046 
1047 bool MetaspaceShared::is_valid_shared_method(const Method* m) {
1048   assert(is_in_shared_metaspace(m), "must be");
1049   return CppVtableCloner<Method>::is_valid_shared_object(m);
1050 }
1051 
1052 void WriteClosure::do_oop(oop* o) {
1053   if (*o == NULL) {
1054     _dump_region->append_intptr_t(0);
1055   } else {
1056     assert(HeapShared::is_heap_object_archiving_allowed(),
1057            "Archiving heap object is not allowed");
1058     _dump_region->append_intptr_t(
1059       (intptr_t)CompressedOops::encode_not_null(*o));
1060   }
1061 }
1062 
1063 void WriteClosure::do_region(u_char* start, size_t size) {
1064   assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
1065   assert(size % sizeof(intptr_t) == 0, "bad size");
1066   do_tag((int)size);
1067   while (size > 0) {
1068     _dump_region->append_intptr_t(*(intptr_t*)start, true);
1069     start += sizeof(intptr_t);
1070     size -= sizeof(intptr_t);
1071   }
1072 }
1073 
1074 // This is for dumping detailed statistics for the allocations
1075 // in the shared spaces.
1076 class DumpAllocStats : public ResourceObj {
1077 public:
1078 
1079   // Here's poor man's enum inheritance
1080 #define SHAREDSPACE_OBJ_TYPES_DO(f) \
1081   METASPACE_OBJ_TYPES_DO(f) \
1082   f(SymbolHashentry) \
1083   f(SymbolBucket) \
1084   f(StringHashentry) \
1085   f(StringBucket) \
1086   f(Other)
1087 
1088   enum Type {
1089     // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc
1090     SHAREDSPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_DECLARE)
1091     _number_of_types
1092   };
1093 
1094   static const char * type_name(Type type) {
1095     switch(type) {
1096     SHAREDSPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_NAME_CASE)
1097     default:
1098       ShouldNotReachHere();
1099       return NULL;
1100     }
1101   }
1102 
1103 public:
1104   enum { RO = 0, RW = 1 };
1105 
1106   int _counts[2][_number_of_types];
1107   int _bytes [2][_number_of_types];
1108 
1109   DumpAllocStats() {
1110     memset(_counts, 0, sizeof(_counts));
1111     memset(_bytes,  0, sizeof(_bytes));
1112   };
1113 
1114   void record(MetaspaceObj::Type type, int byte_size, bool read_only) {
1115     assert(int(type) >= 0 && type < MetaspaceObj::_number_of_types, "sanity");
1116     int which = (read_only) ? RO : RW;
1117     _counts[which][type] ++;
1118     _bytes [which][type] += byte_size;
1119   }
1120 
1121   void record_other_type(int byte_size, bool read_only) {
1122     int which = (read_only) ? RO : RW;
1123     _bytes [which][OtherType] += byte_size;
1124   }
1125   void print_stats(int ro_all, int rw_all, int mc_all);
1126 };
1127 
1128 void DumpAllocStats::print_stats(int ro_all, int rw_all, int mc_all) {
1129   // Calculate size of data that was not allocated by Metaspace::allocate()
1130   MetaspaceSharedStats *stats = MetaspaceShared::stats();
1131 
1132   // symbols
1133   _counts[RO][SymbolHashentryType] = stats->symbol.hashentry_count;
1134   _bytes [RO][SymbolHashentryType] = stats->symbol.hashentry_bytes;
1135 
1136   _counts[RO][SymbolBucketType] = stats->symbol.bucket_count;
1137   _bytes [RO][SymbolBucketType] = stats->symbol.bucket_bytes;
1138 
1139   // strings
1140   _counts[RO][StringHashentryType] = stats->string.hashentry_count;
1141   _bytes [RO][StringHashentryType] = stats->string.hashentry_bytes;
1142 
1143   _counts[RO][StringBucketType] = stats->string.bucket_count;
1144   _bytes [RO][StringBucketType] = stats->string.bucket_bytes;
1145 
1146   // TODO: count things like dictionary, vtable, etc
1147   _bytes[RW][OtherType] += mc_all;
1148   rw_all += mc_all; // mc is mapped Read/Write
1149 
1150   // prevent divide-by-zero
1151   if (ro_all < 1) {
1152     ro_all = 1;
1153   }
1154   if (rw_all < 1) {
1155     rw_all = 1;
1156   }
1157 
1158   int all_ro_count = 0;
1159   int all_ro_bytes = 0;
1160   int all_rw_count = 0;
1161   int all_rw_bytes = 0;
1162 
1163 // To make fmt_stats be a syntactic constant (for format warnings), use #define.
1164 #define fmt_stats "%-20s: %8d %10d %5.1f | %8d %10d %5.1f | %8d %10d %5.1f"
1165   const char *sep = "--------------------+---------------------------+---------------------------+--------------------------";
1166   const char *hdr = "                        ro_cnt   ro_bytes     % |   rw_cnt   rw_bytes     % |  all_cnt  all_bytes     %";
1167 
1168   LogMessage(cds) msg;
1169 
1170   msg.debug("Detailed metadata info (excluding st regions; rw stats include mc regions):");
1171   msg.debug("%s", hdr);
1172   msg.debug("%s", sep);
1173   for (int type = 0; type < int(_number_of_types); type ++) {
1174     const char *name = type_name((Type)type);
1175     int ro_count = _counts[RO][type];
1176     int ro_bytes = _bytes [RO][type];
1177     int rw_count = _counts[RW][type];
1178     int rw_bytes = _bytes [RW][type];
1179     int count = ro_count + rw_count;
1180     int bytes = ro_bytes + rw_bytes;
1181 
1182     double ro_perc = percent_of(ro_bytes, ro_all);
1183     double rw_perc = percent_of(rw_bytes, rw_all);
1184     double perc    = percent_of(bytes, ro_all + rw_all);
1185 
1186     msg.debug(fmt_stats, name,
1187                          ro_count, ro_bytes, ro_perc,
1188                          rw_count, rw_bytes, rw_perc,
1189                          count, bytes, perc);
1190 
1191     all_ro_count += ro_count;
1192     all_ro_bytes += ro_bytes;
1193     all_rw_count += rw_count;
1194     all_rw_bytes += rw_bytes;
1195   }
1196 
1197   int all_count = all_ro_count + all_rw_count;
1198   int all_bytes = all_ro_bytes + all_rw_bytes;
1199 
1200   double all_ro_perc = percent_of(all_ro_bytes, ro_all);
1201   double all_rw_perc = percent_of(all_rw_bytes, rw_all);
1202   double all_perc    = percent_of(all_bytes, ro_all + rw_all);
1203 
1204   msg.debug("%s", sep);
1205   msg.debug(fmt_stats, "Total",
1206                        all_ro_count, all_ro_bytes, all_ro_perc,
1207                        all_rw_count, all_rw_bytes, all_rw_perc,
1208                        all_count, all_bytes, all_perc);
1209 
1210   assert(all_ro_bytes == ro_all, "everything should have been counted");
1211   assert(all_rw_bytes == rw_all, "everything should have been counted");
1212 
1213 #undef fmt_stats
1214 }
1215 
1216 // Populate the shared space.
1217 
1218 class VM_PopulateDumpSharedSpace: public VM_Operation {
1219 private:
1220   GrowableArray<MemRegion> *_closed_archive_heap_regions;
1221   GrowableArray<MemRegion> *_open_archive_heap_regions;
1222 
1223   GrowableArray<ArchiveHeapOopmapInfo> *_closed_archive_heap_oopmaps;
1224   GrowableArray<ArchiveHeapOopmapInfo> *_open_archive_heap_oopmaps;
1225 
1226   void dump_java_heap_objects() NOT_CDS_JAVA_HEAP_RETURN;
1227   void dump_archive_heap_oopmaps() NOT_CDS_JAVA_HEAP_RETURN;
1228   void dump_archive_heap_oopmaps(GrowableArray<MemRegion>* regions,
1229                                  GrowableArray<ArchiveHeapOopmapInfo>* oopmaps);
1230   void dump_symbols();
1231   char* dump_read_only_tables();
1232   void print_class_stats();
1233   void print_region_stats(FileMapInfo* map_info);
1234   void print_bitmap_region_stats(size_t size, size_t total_size);
1235   void print_heap_region_stats(GrowableArray<MemRegion> *heap_mem,
1236                                const char *name, size_t total_size);
1237   void relocate_to_requested_base_address(CHeapBitMap* ptrmap);
1238 
1239 public:
1240 
1241   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
1242   void doit();   // outline because gdb sucks
1243   bool allow_nested_vm_operations() const { return true; }
1244 }; // class VM_PopulateDumpSharedSpace
1245 
1246 // ArchiveCompactor --
1247 //
1248 // This class is the central piece of shared archive compaction -- all metaspace data are
1249 // initially allocated outside of the shared regions. ArchiveCompactor copies the
1250 // metaspace data into their final location in the shared regions.
1251 
1252 class ArchiveCompactor : AllStatic {
1253   static const int INITIAL_TABLE_SIZE = 8087;
1254   static const int MAX_TABLE_SIZE     = 1000000;
1255 
1256   static DumpAllocStats* _alloc_stats;
1257 
1258   typedef KVHashtable<address, address, mtInternal> RelocationTable;
1259   static RelocationTable* _new_loc_table;
1260 
1261 public:
1262   static void initialize() {
1263     _alloc_stats = new(ResourceObj::C_HEAP, mtInternal)DumpAllocStats;
1264     _new_loc_table = new RelocationTable(INITIAL_TABLE_SIZE);
1265   }
1266   static DumpAllocStats* alloc_stats() {
1267     return _alloc_stats;
1268   }
1269 
1270   // Use this when you allocate space with MetaspaceShare::read_only_space_alloc()
1271   // outside of ArchiveCompactor::allocate(). These are usually for misc tables
1272   // that are allocated in the RO space.
1273   class OtherROAllocMark {
1274     char* _oldtop;
1275   public:
1276     OtherROAllocMark() {
1277       _oldtop = _ro_region.top();
1278     }
1279     ~OtherROAllocMark() {
1280       char* newtop = _ro_region.top();
1281       ArchiveCompactor::alloc_stats()->record_other_type(int(newtop - _oldtop), true);
1282     }
1283   };
1284 
1285   static void allocate(MetaspaceClosure::Ref* ref, bool read_only) {
1286     address obj = ref->obj();
1287     int bytes = ref->size() * BytesPerWord;
1288     char* p;
1289     size_t alignment = BytesPerWord;
1290     char* oldtop;
1291     char* newtop;
1292 
1293     if (read_only) {
1294       oldtop = _ro_region.top();
1295       p = _ro_region.allocate(bytes, alignment);
1296       newtop = _ro_region.top();
1297     } else {
1298       oldtop = _rw_region.top();
1299       if (ref->msotype() == MetaspaceObj::ClassType) {
1300         // Save a pointer immediate in front of an InstanceKlass, so
1301         // we can do a quick lookup from InstanceKlass* -> RunTimeSharedClassInfo*
1302         // without building another hashtable. See RunTimeSharedClassInfo::get_for()
1303         // in systemDictionaryShared.cpp.
1304         Klass* klass = (Klass*)obj;
1305         if (klass->is_instance_klass()) {
1306           SystemDictionaryShared::validate_before_archiving(InstanceKlass::cast(klass));
1307           _rw_region.allocate(sizeof(address), BytesPerWord);
1308         }
1309       }
1310       p = _rw_region.allocate(bytes, alignment);
1311       newtop = _rw_region.top();
1312     }
1313     memcpy(p, obj, bytes);
1314 
1315     intptr_t* archived_vtable = MetaspaceShared::get_archived_cpp_vtable(ref->msotype(), (address)p);
1316     if (archived_vtable != NULL) {
1317       *(address*)p = (address)archived_vtable;
1318       ArchivePtrMarker::mark_pointer((address*)p);
1319     }
1320 
1321     assert(_new_loc_table->lookup(obj) == NULL, "each object can be relocated at most once");
1322     _new_loc_table->add(obj, (address)p);
1323     log_trace(cds)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(obj), p2i(p), bytes);
1324     if (_new_loc_table->maybe_grow(MAX_TABLE_SIZE)) {
1325       log_info(cds, hashtables)("Expanded _new_loc_table to %d", _new_loc_table->table_size());
1326     }
1327     _alloc_stats->record(ref->msotype(), int(newtop - oldtop), read_only);
1328   }
1329 
1330   static address get_new_loc(MetaspaceClosure::Ref* ref) {
1331     address* pp = _new_loc_table->lookup(ref->obj());
1332     assert(pp != NULL, "must be");
1333     return *pp;
1334   }
1335 
1336 private:
1337   // Makes a shallow copy of visited MetaspaceObj's
1338   class ShallowCopier: public UniqueMetaspaceClosure {
1339     bool _read_only;
1340   public:
1341     ShallowCopier(bool read_only) : _read_only(read_only) {}
1342 
1343     virtual bool do_unique_ref(Ref* ref, bool read_only) {
1344       if (read_only == _read_only) {
1345         allocate(ref, read_only);
1346       }
1347       return true; // recurse into ref.obj()
1348     }
1349   };
1350 
1351   // Relocate embedded pointers within a MetaspaceObj's shallow copy
1352   class ShallowCopyEmbeddedRefRelocator: public UniqueMetaspaceClosure {
1353   public:
1354     virtual bool do_unique_ref(Ref* ref, bool read_only) {
1355       address new_loc = get_new_loc(ref);
1356       RefRelocator refer;
1357       ref->metaspace_pointers_do_at(&refer, new_loc);
1358       return true; // recurse into ref.obj()
1359     }
1360     virtual void push_special(SpecialRef type, Ref* ref, intptr_t* p) {
1361       assert(type == _method_entry_ref, "only special type allowed for now");
1362       address obj = ref->obj();
1363       address new_obj = get_new_loc(ref);
1364       size_t offset = pointer_delta(p, obj,  sizeof(u1));
1365       intptr_t* new_p = (intptr_t*)(new_obj + offset);
1366       assert(*p == *new_p, "must be a copy");
1367       ArchivePtrMarker::mark_pointer((address*)new_p);
1368     }
1369   };
1370 
1371   // Relocate a reference to point to its shallow copy
1372   class RefRelocator: public MetaspaceClosure {
1373   public:
1374     virtual bool do_ref(Ref* ref, bool read_only) {
1375       if (ref->not_null()) {
1376         ref->update(get_new_loc(ref));
1377         ArchivePtrMarker::mark_pointer(ref->addr());
1378       }
1379       return false; // Do not recurse.
1380     }
1381   };
1382 
1383 #ifdef ASSERT
1384   class IsRefInArchiveChecker: public MetaspaceClosure {
1385   public:
1386     virtual bool do_ref(Ref* ref, bool read_only) {
1387       if (ref->not_null()) {
1388         char* obj = (char*)ref->obj();
1389         assert(_ro_region.contains(obj) || _rw_region.contains(obj),
1390                "must be relocated to point to CDS archive");
1391       }
1392       return false; // Do not recurse.
1393     }
1394   };
1395 #endif
1396 
1397 public:
1398   static void copy_and_compact() {
1399     ResourceMark rm;
1400 
1401     log_info(cds)("Scanning all metaspace objects ... ");
1402     {
1403       // allocate and shallow-copy RW objects, immediately following the MC region
1404       log_info(cds)("Allocating RW objects ... ");
1405       _mc_region.pack(&_rw_region);
1406 
1407       ResourceMark rm;
1408       ShallowCopier rw_copier(false);
1409       iterate_roots(&rw_copier);
1410     }
1411     {
1412       // allocate and shallow-copy of RO object, immediately following the RW region
1413       log_info(cds)("Allocating RO objects ... ");
1414       _rw_region.pack(&_ro_region);
1415 
1416       ResourceMark rm;
1417       ShallowCopier ro_copier(true);
1418       iterate_roots(&ro_copier);
1419     }
1420     {
1421       log_info(cds)("Relocating embedded pointers ... ");
1422       ResourceMark rm;
1423       ShallowCopyEmbeddedRefRelocator emb_reloc;
1424       iterate_roots(&emb_reloc);
1425     }
1426     {
1427       log_info(cds)("Relocating external roots ... ");
1428       ResourceMark rm;
1429       RefRelocator ext_reloc;
1430       iterate_roots(&ext_reloc);
1431     }
1432     {
1433       log_info(cds)("Fixing symbol identity hash ... ");
1434       os::init_random(0x12345678);
1435       GrowableArray<Symbol*>* all_symbols = MetaspaceShared::collected_symbols();
1436       all_symbols->sort(compare_symbols_by_address);
1437       for (int i = 0; i < all_symbols->length(); i++) {
1438         assert(all_symbols->at(i)->is_permanent(), "archived symbols must be permanent");
1439         all_symbols->at(i)->update_identity_hash();
1440       }
1441     }
1442 #ifdef ASSERT
1443     {
1444       log_info(cds)("Verifying external roots ... ");
1445       ResourceMark rm;
1446       IsRefInArchiveChecker checker;
1447       iterate_roots(&checker);
1448     }
1449 #endif
1450   }
1451 
1452   // We must relocate the System::_well_known_klasses only after we have copied the
1453   // java objects in during dump_java_heap_objects(): during the object copy, we operate on
1454   // old objects which assert that their klass is the original klass.
1455   static void relocate_well_known_klasses() {
1456     {
1457       log_info(cds)("Relocating SystemDictionary::_well_known_klasses[] ... ");
1458       ResourceMark rm;
1459       RefRelocator ext_reloc;
1460       SystemDictionary::well_known_klasses_do(&ext_reloc);
1461     }
1462     // NOTE: after this point, we shouldn't have any globals that can reach the old
1463     // objects.
1464 
1465     // We cannot use any of the objects in the heap anymore (except for the
1466     // shared strings) because their headers no longer point to valid Klasses.
1467   }
1468 
1469   static void iterate_roots(MetaspaceClosure* it) {
1470     // To ensure deterministic contents in the archive, we just need to ensure that
1471     // we iterate the MetsapceObjs in a deterministic order. It doesn't matter where
1472     // the MetsapceObjs are located originally, as they are copied sequentially into
1473     // the archive during the iteration.
1474     //
1475     // The only issue here is that the symbol table and the system directories may be
1476     // randomly ordered, so we copy the symbols and klasses into two arrays and sort
1477     // them deterministically.
1478     //
1479     // During -Xshare:dump, the order of Symbol creation is strictly determined by
1480     // the SharedClassListFile (class loading is done in a single thread and the JIT
1481     // is disabled). Also, Symbols are allocated in monotonically increasing addresses
1482     // (see Symbol::operator new(size_t, int)). So if we iterate the Symbols by
1483     // ascending address order, we ensure that all Symbols are copied into deterministic
1484     // locations in the archive.
1485     GrowableArray<Symbol*>* symbols = _global_symbol_objects;
1486     for (int i = 0; i < symbols->length(); i++) {
1487       it->push(symbols->adr_at(i));
1488     }
1489     if (_global_klass_objects != NULL) {
1490       // Need to fix up the pointers
1491       for (int i = 0; i < _global_klass_objects->length(); i++) {
1492         // NOTE -- this requires that the vtable is NOT yet patched, or else we are hosed.
1493         it->push(_global_klass_objects->adr_at(i));
1494       }
1495     }
1496     FileMapInfo::metaspace_pointers_do(it, false);
1497     SystemDictionaryShared::dumptime_classes_do(it);
1498     Universe::metaspace_pointers_do(it);
1499     SymbolTable::metaspace_pointers_do(it);
1500     vmSymbols::metaspace_pointers_do(it);
1501 
1502     it->finish();
1503   }
1504 
1505   static Klass* get_relocated_klass(Klass* orig_klass) {
1506     assert(DumpSharedSpaces, "dump time only");
1507     address* pp = _new_loc_table->lookup((address)orig_klass);
1508     assert(pp != NULL, "must be");
1509     Klass* klass = (Klass*)(*pp);
1510     assert(klass->is_klass(), "must be");
1511     return klass;
1512   }
1513 };
1514 
1515 DumpAllocStats* ArchiveCompactor::_alloc_stats;
1516 ArchiveCompactor::RelocationTable* ArchiveCompactor::_new_loc_table;
1517 
1518 void VM_PopulateDumpSharedSpace::dump_symbols() {
1519   log_info(cds)("Dumping symbol table ...");
1520 
1521   NOT_PRODUCT(SymbolTable::verify());
1522   SymbolTable::write_to_archive();
1523 }
1524 
1525 char* VM_PopulateDumpSharedSpace::dump_read_only_tables() {
1526   ArchiveCompactor::OtherROAllocMark mark;
1527 
1528   log_info(cds)("Removing java_mirror ... ");
1529   if (!HeapShared::is_heap_object_archiving_allowed()) {
1530     Universe::clear_basic_type_mirrors();
1531   }
1532   remove_java_mirror_in_classes();
1533   log_info(cds)("done. ");
1534 
1535   SystemDictionaryShared::write_to_archive();
1536 
1537   // Write the other data to the output array.
1538   char* start = _ro_region.top();
1539   WriteClosure wc(&_ro_region);
1540   MetaspaceShared::serialize(&wc);
1541 
1542   // Write the bitmaps for patching the archive heap regions
1543   _closed_archive_heap_oopmaps = NULL;
1544   _open_archive_heap_oopmaps = NULL;
1545   dump_archive_heap_oopmaps();
1546 
1547   return start;
1548 }
1549 
1550 void VM_PopulateDumpSharedSpace::print_class_stats() {
1551   log_info(cds)("Number of classes %d", _global_klass_objects->length());
1552   {
1553     int num_type_array = 0, num_obj_array = 0, num_inst = 0;
1554     for (int i = 0; i < _global_klass_objects->length(); i++) {
1555       Klass* k = _global_klass_objects->at(i);
1556       if (k->is_instance_klass()) {
1557         num_inst ++;
1558       } else if (k->is_objArray_klass()) {
1559         num_obj_array ++;
1560       } else {
1561         assert(k->is_typeArray_klass(), "sanity");
1562         num_type_array ++;
1563       }
1564     }
1565     log_info(cds)("    instance classes   = %5d", num_inst);
1566     log_info(cds)("    obj array classes  = %5d", num_obj_array);
1567     log_info(cds)("    type array classes = %5d", num_type_array);
1568   }
1569 }
1570 
1571 void VM_PopulateDumpSharedSpace::relocate_to_requested_base_address(CHeapBitMap* ptrmap) {
1572   intx addr_delta = MetaspaceShared::final_delta();
1573   if (addr_delta == 0) {
1574     ArchivePtrMarker::compact((address)SharedBaseAddress, (address)_ro_region.top());
1575   } else {
1576     // We are not able to reserve space at MetaspaceShared::requested_base_address() (due to ASLR).
1577     // This means that the current content of the archive is based on a random
1578     // address. Let's relocate all the pointers, so that it can be mapped to
1579     // MetaspaceShared::requested_base_address() without runtime relocation.
1580     //
1581     // Note: both the base and dynamic archive are written with
1582     // FileMapHeader::_requested_base_address == MetaspaceShared::requested_base_address()
1583 
1584     // Patch all pointers that are marked by ptrmap within this region,
1585     // where we have just dumped all the metaspace data.
1586     address patch_base = (address)SharedBaseAddress;
1587     address patch_end  = (address)_ro_region.top();
1588     size_t size = patch_end - patch_base;
1589 
1590     // the current value of the pointers to be patched must be within this
1591     // range (i.e., must point to valid metaspace objects)
1592     address valid_old_base = patch_base;
1593     address valid_old_end  = patch_end;
1594 
1595     // after patching, the pointers must point inside this range
1596     // (the requested location of the archive, as mapped at runtime).
1597     address valid_new_base = (address)MetaspaceShared::requested_base_address();
1598     address valid_new_end  = valid_new_base + size;
1599 
1600     log_debug(cds)("Relocating archive from [" INTPTR_FORMAT " - " INTPTR_FORMAT " ] to "
1601                    "[" INTPTR_FORMAT " - " INTPTR_FORMAT " ]", p2i(patch_base), p2i(patch_end),
1602                    p2i(valid_new_base), p2i(valid_new_end));
1603 
1604     SharedDataRelocator<true> patcher((address*)patch_base, (address*)patch_end, valid_old_base, valid_old_end,
1605                                       valid_new_base, valid_new_end, addr_delta, ptrmap);
1606     ptrmap->iterate(&patcher);
1607     ArchivePtrMarker::compact(patcher.max_non_null_offset());
1608   }
1609 }
1610 
1611 void VM_PopulateDumpSharedSpace::doit() {
1612   HeapShared::run_full_gc_in_vm_thread();
1613   CHeapBitMap ptrmap;
1614   MetaspaceShared::initialize_ptr_marker(&ptrmap);
1615 
1616   // We should no longer allocate anything from the metaspace, so that:
1617   //
1618   // (1) Metaspace::allocate might trigger GC if we have run out of
1619   //     committed metaspace, but we can't GC because we're running
1620   //     in the VM thread.
1621   // (2) ArchiveCompactor needs to work with a stable set of MetaspaceObjs.
1622   Metaspace::freeze();
1623   DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm);
1624 
1625   Thread* THREAD = VMThread::vm_thread();
1626 
1627   FileMapInfo::check_nonempty_dir_in_shared_path_table();
1628 
1629   NOT_PRODUCT(SystemDictionary::verify();)
1630   // The following guarantee is meant to ensure that no loader constraints
1631   // exist yet, since the constraints table is not shared.  This becomes
1632   // more important now that we don't re-initialize vtables/itables for
1633   // shared classes at runtime, where constraints were previously created.
1634   guarantee(SystemDictionary::constraints()->number_of_entries() == 0,
1635             "loader constraints are not saved");
1636   guarantee(SystemDictionary::placeholders()->number_of_entries() == 0,
1637           "placeholders are not saved");
1638 
1639   // At this point, many classes have been loaded.
1640   // Gather systemDictionary classes in a global array and do everything to
1641   // that so we don't have to walk the SystemDictionary again.
1642   SystemDictionaryShared::check_excluded_classes();
1643   _global_klass_objects = new GrowableArray<Klass*>(1000);
1644   CollectClassesClosure collect_classes;
1645   ClassLoaderDataGraph::loaded_classes_do(&collect_classes);
1646   _global_klass_objects->sort(global_klass_compare);
1647 
1648   print_class_stats();
1649 
1650   // Ensure the ConstMethods won't be modified at run-time
1651   log_info(cds)("Updating ConstMethods ... ");
1652   rewrite_nofast_bytecodes_and_calculate_fingerprints(THREAD);
1653   log_info(cds)("done. ");
1654 
1655   // Remove all references outside the metadata
1656   log_info(cds)("Removing unshareable information ... ");
1657   remove_unshareable_in_classes();
1658   log_info(cds)("done. ");
1659 
1660   MetaspaceShared::allocate_cloned_cpp_vtptrs();
1661   char* cloned_vtables = _mc_region.top();
1662   MetaspaceShared::allocate_cpp_vtable_clones();
1663 
1664   ArchiveCompactor::initialize();
1665   ArchiveCompactor::copy_and_compact();
1666 
1667   dump_symbols();
1668 
1669   // Dump supported java heap objects
1670   _closed_archive_heap_regions = NULL;
1671   _open_archive_heap_regions = NULL;
1672   dump_java_heap_objects();
1673 
1674   ArchiveCompactor::relocate_well_known_klasses();
1675 
1676   char* serialized_data = dump_read_only_tables();
1677   _ro_region.pack();
1678 
1679   // The vtable clones contain addresses of the current process.
1680   // We don't want to write these addresses into the archive. Same for i2i buffer.
1681   MetaspaceShared::zero_cpp_vtable_clones_for_writing();
1682   memset(MetaspaceShared::i2i_entry_code_buffers(), 0,
1683          MetaspaceShared::i2i_entry_code_buffers_size());
1684 
1685   // relocate the data so that it can be mapped to MetaspaceShared::requested_base_address()
1686   // without runtime relocation.
1687   relocate_to_requested_base_address(&ptrmap);
1688 
1689   // Create and write the archive file that maps the shared spaces.
1690 
1691   FileMapInfo* mapinfo = new FileMapInfo(true);
1692   mapinfo->populate_header(os::vm_allocation_granularity());
1693   mapinfo->set_serialized_data(serialized_data);
1694   mapinfo->set_cloned_vtables(cloned_vtables);
1695   mapinfo->set_i2i_entry_code_buffers(MetaspaceShared::i2i_entry_code_buffers(),
1696                                       MetaspaceShared::i2i_entry_code_buffers_size());
1697   mapinfo->open_for_write();
1698   MetaspaceShared::write_core_archive_regions(mapinfo, _closed_archive_heap_oopmaps, _open_archive_heap_oopmaps);
1699   _total_closed_archive_region_size = mapinfo->write_archive_heap_regions(
1700                                         _closed_archive_heap_regions,
1701                                         _closed_archive_heap_oopmaps,
1702                                         MetaspaceShared::first_closed_archive_heap_region,
1703                                         MetaspaceShared::max_closed_archive_heap_region);
1704   _total_open_archive_region_size = mapinfo->write_archive_heap_regions(
1705                                         _open_archive_heap_regions,
1706                                         _open_archive_heap_oopmaps,
1707                                         MetaspaceShared::first_open_archive_heap_region,
1708                                         MetaspaceShared::max_open_archive_heap_region);
1709 
1710   mapinfo->set_final_requested_base((char*)MetaspaceShared::requested_base_address());
1711   mapinfo->set_header_crc(mapinfo->compute_header_crc());
1712   mapinfo->write_header();
1713   print_region_stats(mapinfo);
1714   mapinfo->close();
1715 
1716   if (log_is_enabled(Info, cds)) {
1717     ArchiveCompactor::alloc_stats()->print_stats(int(_ro_region.used()), int(_rw_region.used()),
1718                                                  int(_mc_region.used()));
1719   }
1720 
1721   if (PrintSystemDictionaryAtExit) {
1722     SystemDictionary::print();
1723   }
1724 
1725   if (AllowArchivingWithJavaAgent) {
1726     warning("This archive was created with AllowArchivingWithJavaAgent. It should be used "
1727             "for testing purposes only and should not be used in a production environment");
1728   }
1729 
1730   // There may be other pending VM operations that operate on the InstanceKlasses,
1731   // which will fail because InstanceKlasses::remove_unshareable_info()
1732   // has been called. Forget these operations and exit the VM directly.
1733   vm_direct_exit(0);
1734 }
1735 
1736 void VM_PopulateDumpSharedSpace::print_region_stats(FileMapInfo *map_info) {
1737   // Print statistics of all the regions
1738   const size_t bitmap_used = map_info->space_at(MetaspaceShared::bm)->used();
1739   const size_t bitmap_reserved = map_info->space_at(MetaspaceShared::bm)->used_aligned();
1740   const size_t total_reserved = _ro_region.reserved()  + _rw_region.reserved() +
1741                                 _mc_region.reserved()  +
1742                                 bitmap_reserved +
1743                                 _total_closed_archive_region_size +
1744                                 _total_open_archive_region_size;
1745   const size_t total_bytes = _ro_region.used()  + _rw_region.used() +
1746                              _mc_region.used()  +
1747                              bitmap_used +
1748                              _total_closed_archive_region_size +
1749                              _total_open_archive_region_size;
1750   const double total_u_perc = percent_of(total_bytes, total_reserved);
1751 
1752   _mc_region.print(total_reserved);
1753   _rw_region.print(total_reserved);
1754   _ro_region.print(total_reserved);
1755   print_bitmap_region_stats(bitmap_used, total_reserved);
1756   print_heap_region_stats(_closed_archive_heap_regions, "ca", total_reserved);
1757   print_heap_region_stats(_open_archive_heap_regions, "oa", total_reserved);
1758 
1759   log_debug(cds)("total    : " SIZE_FORMAT_W(9) " [100.0%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used]",
1760                  total_bytes, total_reserved, total_u_perc);
1761 }
1762 
1763 void VM_PopulateDumpSharedSpace::print_bitmap_region_stats(size_t size, size_t total_size) {
1764   log_debug(cds)("bm  space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used]",
1765                  size, size/double(total_size)*100.0, size);
1766 }
1767 
1768 void VM_PopulateDumpSharedSpace::print_heap_region_stats(GrowableArray<MemRegion> *heap_mem,
1769                                                          const char *name, size_t total_size) {
1770   int arr_len = heap_mem == NULL ? 0 : heap_mem->length();
1771   for (int i = 0; i < arr_len; i++) {
1772       char* start = (char*)heap_mem->at(i).start();
1773       size_t size = heap_mem->at(i).byte_size();
1774       char* top = start + size;
1775       log_debug(cds)("%s%d space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used] at " INTPTR_FORMAT,
1776                      name, i, size, size/double(total_size)*100.0, size, p2i(start));
1777 
1778   }
1779 }
1780 
1781 void MetaspaceShared::write_core_archive_regions(FileMapInfo* mapinfo,
1782                                                  GrowableArray<ArchiveHeapOopmapInfo>* closed_oopmaps,
1783                                                  GrowableArray<ArchiveHeapOopmapInfo>* open_oopmaps) {
1784   // Make sure NUM_CDS_REGIONS (exported in cds.h) agrees with
1785   // MetaspaceShared::n_regions (internal to hotspot).
1786   assert(NUM_CDS_REGIONS == MetaspaceShared::n_regions, "sanity");
1787 
1788   // mc contains the trampoline code for method entries, which are patched at run time,
1789   // so it needs to be read/write.
1790   write_region(mapinfo, mc, &_mc_region, /*read_only=*/false,/*allow_exec=*/true);
1791   write_region(mapinfo, rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false);
1792   write_region(mapinfo, ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false);
1793   mapinfo->write_bitmap_region(ArchivePtrMarker::ptrmap(), closed_oopmaps, open_oopmaps);
1794 }
1795 
1796 void MetaspaceShared::write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region, bool read_only,  bool allow_exec) {
1797   mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec);
1798 }
1799 
1800 // Update a Java object to point its Klass* to the new location after
1801 // shared archive has been compacted.
1802 void MetaspaceShared::relocate_klass_ptr(oop o) {
1803   assert(DumpSharedSpaces, "sanity");
1804   Klass* k = ArchiveCompactor::get_relocated_klass(o->klass());
1805   o->set_klass(k);
1806 }
1807 
1808 Klass* MetaspaceShared::get_relocated_klass(Klass *k, bool is_final) {
1809   assert(DumpSharedSpaces, "sanity");
1810   k = ArchiveCompactor::get_relocated_klass(k);
1811   if (is_final) {
1812     k = (Klass*)(address(k) + final_delta());
1813   }
1814   return k;
1815 }
1816 
1817 class LinkSharedClassesClosure : public KlassClosure {
1818   Thread* THREAD;
1819   bool    _made_progress;
1820  public:
1821   LinkSharedClassesClosure(Thread* thread) : THREAD(thread), _made_progress(false) {}
1822 
1823   void reset()               { _made_progress = false; }
1824   bool made_progress() const { return _made_progress; }
1825 
1826   void do_klass(Klass* k) {
1827     if (k->is_instance_klass()) {
1828       InstanceKlass* ik = InstanceKlass::cast(k);
1829       // For dynamic CDS dump, only link classes loaded by the builtin class loaders.
1830       bool do_linking = DumpSharedSpaces ? true : !ik->is_shared_unregistered_class();
1831       if (do_linking) {
1832         // Link the class to cause the bytecodes to be rewritten and the
1833         // cpcache to be created. Class verification is done according
1834         // to -Xverify setting.
1835         _made_progress |= MetaspaceShared::try_link_class(ik, THREAD);
1836         guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1837 
1838         if (DumpSharedSpaces) {
1839           // The following function is used to resolve all Strings in the statically
1840           // dumped classes to archive all the Strings. The archive heap is not supported
1841           // for the dynamic archive.
1842           ik->constants()->resolve_class_constants(THREAD);
1843         }
1844       }
1845     }
1846   }
1847 };
1848 
1849 void MetaspaceShared::link_and_cleanup_shared_classes(TRAPS) {
1850   // We need to iterate because verification may cause additional classes
1851   // to be loaded.
1852   LinkSharedClassesClosure link_closure(THREAD);
1853   do {
1854     link_closure.reset();
1855     ClassLoaderDataGraph::unlocked_loaded_classes_do(&link_closure);
1856     guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1857   } while (link_closure.made_progress());
1858 }
1859 
1860 void MetaspaceShared::prepare_for_dumping() {
1861   Arguments::check_unsupported_dumping_properties();
1862   ClassLoader::initialize_shared_path();
1863 }
1864 
1865 // Preload classes from a list, populate the shared spaces and dump to a
1866 // file.
1867 void MetaspaceShared::preload_and_dump(TRAPS) {
1868   { TraceTime timer("Dump Shared Spaces", TRACETIME_LOG(Info, startuptime));
1869     ResourceMark rm(THREAD);
1870     char class_list_path_str[JVM_MAXPATHLEN];
1871     // Preload classes to be shared.
1872     const char* class_list_path;
1873     if (SharedClassListFile == NULL) {
1874       // Construct the path to the class list (in jre/lib)
1875       // Walk up two directories from the location of the VM and
1876       // optionally tack on "lib" (depending on platform)
1877       os::jvm_path(class_list_path_str, sizeof(class_list_path_str));
1878       for (int i = 0; i < 3; i++) {
1879         char *end = strrchr(class_list_path_str, *os::file_separator());
1880         if (end != NULL) *end = '\0';
1881       }
1882       int class_list_path_len = (int)strlen(class_list_path_str);
1883       if (class_list_path_len >= 3) {
1884         if (strcmp(class_list_path_str + class_list_path_len - 3, "lib") != 0) {
1885           if (class_list_path_len < JVM_MAXPATHLEN - 4) {
1886             jio_snprintf(class_list_path_str + class_list_path_len,
1887                          sizeof(class_list_path_str) - class_list_path_len,
1888                          "%slib", os::file_separator());
1889             class_list_path_len += 4;
1890           }
1891         }
1892       }
1893       if (class_list_path_len < JVM_MAXPATHLEN - 10) {
1894         jio_snprintf(class_list_path_str + class_list_path_len,
1895                      sizeof(class_list_path_str) - class_list_path_len,
1896                      "%sclasslist", os::file_separator());
1897       }
1898       class_list_path = class_list_path_str;
1899     } else {
1900       class_list_path = SharedClassListFile;
1901     }
1902 
1903     log_info(cds)("Loading classes to share ...");
1904     _has_error_classes = false;
1905     int class_count = preload_classes(class_list_path, THREAD);
1906     if (ExtraSharedClassListFile) {
1907       class_count += preload_classes(ExtraSharedClassListFile, THREAD);
1908     }
1909     log_info(cds)("Loading classes to share: done.");
1910 
1911     log_info(cds)("Shared spaces: preloaded %d classes", class_count);
1912 
1913     if (SharedArchiveConfigFile) {
1914       log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
1915       read_extra_data(SharedArchiveConfigFile, THREAD);
1916     }
1917     log_info(cds)("Reading extra data: done.");
1918 
1919     HeapShared::init_subgraph_entry_fields(THREAD);
1920 
1921     // Rewrite and link classes
1922     log_info(cds)("Rewriting and linking classes ...");
1923 
1924     // Link any classes which got missed. This would happen if we have loaded classes that
1925     // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1926     // fails verification, all other interfaces that were not specified in the classlist but
1927     // are implemented by K are not verified.
1928     link_and_cleanup_shared_classes(CATCH);
1929     log_info(cds)("Rewriting and linking classes: done");
1930 
1931     VM_PopulateDumpSharedSpace op;
1932     MutexLocker ml(THREAD, HeapShared::is_heap_object_archiving_allowed() ?
1933                    Heap_lock : NULL);     // needed by HeapShared::run_gc()
1934     VMThread::execute(&op);
1935   }
1936 }
1937 
1938 
1939 int MetaspaceShared::preload_classes(const char* class_list_path, TRAPS) {
1940   ClassListParser parser(class_list_path);
1941   int class_count = 0;
1942 
1943   while (parser.parse_one_line()) {
1944     Klass* klass = parser.load_current_class(THREAD);
1945     if (HAS_PENDING_EXCEPTION) {
1946       if (klass == NULL &&
1947           (PENDING_EXCEPTION->klass()->name() == vmSymbols::java_lang_ClassNotFoundException())) {
1948         // print a warning only when the pending exception is class not found
1949         log_warning(cds)("Preload Warning: Cannot find %s", parser.current_class_name());
1950       }
1951       CLEAR_PENDING_EXCEPTION;
1952     }
1953     if (klass != NULL) {
1954       if (log_is_enabled(Trace, cds)) {
1955         ResourceMark rm(THREAD);
1956         log_trace(cds)("Shared spaces preloaded: %s", klass->external_name());
1957       }
1958 
1959       if (klass->is_instance_klass()) {
1960         InstanceKlass* ik = InstanceKlass::cast(klass);
1961 
1962         // Link the class to cause the bytecodes to be rewritten and the
1963         // cpcache to be created. The linking is done as soon as classes
1964         // are loaded in order that the related data structures (klass and
1965         // cpCache) are located together.
1966         try_link_class(ik, THREAD);
1967         guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1968       }
1969 
1970       class_count++;
1971     }
1972   }
1973 
1974   return class_count;
1975 }
1976 
1977 // Returns true if the class's status has changed
1978 bool MetaspaceShared::try_link_class(InstanceKlass* ik, TRAPS) {
1979   Arguments::assert_is_dumping_archive();
1980   if (ik->init_state() < InstanceKlass::linked &&
1981       !SystemDictionaryShared::has_class_failed_verification(ik)) {
1982     bool saved = BytecodeVerificationLocal;
1983     if (ik->is_shared_unregistered_class() && ik->class_loader() == NULL) {
1984       // The verification decision is based on BytecodeVerificationRemote
1985       // for non-system classes. Since we are using the NULL classloader
1986       // to load non-system classes for customized class loaders during dumping,
1987       // we need to temporarily change BytecodeVerificationLocal to be the same as
1988       // BytecodeVerificationRemote. Note this can cause the parent system
1989       // classes also being verified. The extra overhead is acceptable during
1990       // dumping.
1991       BytecodeVerificationLocal = BytecodeVerificationRemote;
1992     }
1993     ik->link_class(THREAD);
1994     if (HAS_PENDING_EXCEPTION) {
1995       ResourceMark rm(THREAD);
1996       log_warning(cds)("Preload Warning: Verification failed for %s",
1997                     ik->external_name());
1998       CLEAR_PENDING_EXCEPTION;
1999       SystemDictionaryShared::set_class_has_failed_verification(ik);
2000       _has_error_classes = true;
2001     }
2002     BytecodeVerificationLocal = saved;
2003     return true;
2004   } else {
2005     return false;
2006   }
2007 }
2008 
2009 #if INCLUDE_CDS_JAVA_HEAP
2010 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
2011   // The closed and open archive heap space has maximum two regions.
2012   // See FileMapInfo::write_archive_heap_regions() for details.
2013   _closed_archive_heap_regions = new GrowableArray<MemRegion>(2);
2014   _open_archive_heap_regions = new GrowableArray<MemRegion>(2);
2015   HeapShared::archive_java_heap_objects(_closed_archive_heap_regions,
2016                                         _open_archive_heap_regions);
2017   ArchiveCompactor::OtherROAllocMark mark;
2018   HeapShared::write_subgraph_info_table();
2019 }
2020 
2021 void VM_PopulateDumpSharedSpace::dump_archive_heap_oopmaps() {
2022   if (HeapShared::is_heap_object_archiving_allowed()) {
2023     _closed_archive_heap_oopmaps = new GrowableArray<ArchiveHeapOopmapInfo>(2);
2024     dump_archive_heap_oopmaps(_closed_archive_heap_regions, _closed_archive_heap_oopmaps);
2025 
2026     _open_archive_heap_oopmaps = new GrowableArray<ArchiveHeapOopmapInfo>(2);
2027     dump_archive_heap_oopmaps(_open_archive_heap_regions, _open_archive_heap_oopmaps);
2028   }
2029 }
2030 
2031 void VM_PopulateDumpSharedSpace::dump_archive_heap_oopmaps(GrowableArray<MemRegion>* regions,
2032                                                            GrowableArray<ArchiveHeapOopmapInfo>* oopmaps) {
2033   for (int i=0; i<regions->length(); i++) {
2034     ResourceBitMap oopmap = HeapShared::calculate_oopmap(regions->at(i));
2035     size_t size_in_bits = oopmap.size();
2036     size_t size_in_bytes = oopmap.size_in_bytes();
2037     uintptr_t* buffer = (uintptr_t*)NEW_C_HEAP_ARRAY(char, size_in_bytes, mtInternal);
2038     oopmap.write_to(buffer, size_in_bytes);
2039     log_info(cds, heap)("Oopmap = " INTPTR_FORMAT " (" SIZE_FORMAT_W(6) " bytes) for heap region "
2040                         INTPTR_FORMAT " (" SIZE_FORMAT_W(8) " bytes)",
2041                         p2i(buffer), size_in_bytes,
2042                         p2i(regions->at(i).start()), regions->at(i).byte_size());
2043 
2044     ArchiveHeapOopmapInfo info;
2045     info._oopmap = (address)buffer;
2046     info._oopmap_size_in_bits = size_in_bits;
2047     info._oopmap_size_in_bytes = size_in_bytes;
2048     oopmaps->append(info);
2049   }
2050 }
2051 #endif // INCLUDE_CDS_JAVA_HEAP
2052 
2053 void ReadClosure::do_ptr(void** p) {
2054   assert(*p == NULL, "initializing previous initialized pointer.");
2055   intptr_t obj = nextPtr();
2056   assert((intptr_t)obj >= 0 || (intptr_t)obj < -100,
2057          "hit tag while initializing ptrs.");
2058   *p = (void*)obj;
2059 }
2060 
2061 void ReadClosure::do_u4(u4* p) {
2062   intptr_t obj = nextPtr();
2063   *p = (u4)(uintx(obj));
2064 }
2065 
2066 void ReadClosure::do_bool(bool* p) {
2067   intptr_t obj = nextPtr();
2068   *p = (bool)(uintx(obj));
2069 }
2070 
2071 void ReadClosure::do_tag(int tag) {
2072   int old_tag;
2073   old_tag = (int)(intptr_t)nextPtr();
2074   // do_int(&old_tag);
2075   assert(tag == old_tag, "old tag doesn't match");
2076   FileMapInfo::assert_mark(tag == old_tag);
2077 }
2078 
2079 void ReadClosure::do_oop(oop *p) {
2080   narrowOop o = (narrowOop)nextPtr();
2081   if (o == 0 || !HeapShared::open_archive_heap_region_mapped()) {
2082     *p = NULL;
2083   } else {
2084     assert(HeapShared::is_heap_object_archiving_allowed(),
2085            "Archived heap object is not allowed");
2086     assert(HeapShared::open_archive_heap_region_mapped(),
2087            "Open archive heap region is not mapped");
2088     *p = HeapShared::decode_from_archive(o);
2089   }
2090 }
2091 
2092 void ReadClosure::do_region(u_char* start, size_t size) {
2093   assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
2094   assert(size % sizeof(intptr_t) == 0, "bad size");
2095   do_tag((int)size);
2096   while (size > 0) {
2097     *(intptr_t*)start = nextPtr();
2098     start += sizeof(intptr_t);
2099     size -= sizeof(intptr_t);
2100   }
2101 }
2102 
2103 void MetaspaceShared::set_shared_metaspace_range(void* base, void *static_top, void* top) {
2104   assert(base <= static_top && static_top <= top, "must be");
2105   _shared_metaspace_static_top = static_top;
2106   MetaspaceObj::set_shared_metaspace_range(base, top);
2107 }
2108 
2109 // Return true if given address is in the misc data region
2110 bool MetaspaceShared::is_in_shared_region(const void* p, int idx) {
2111   return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_region(p, idx);
2112 }
2113 
2114 bool MetaspaceShared::is_in_trampoline_frame(address addr) {
2115   if (UseSharedSpaces && is_in_shared_region(addr, MetaspaceShared::mc)) {
2116     return true;
2117   }
2118   return false;
2119 }
2120 
2121 bool MetaspaceShared::is_shared_dynamic(void* p) {
2122   if ((p < MetaspaceObj::shared_metaspace_top()) &&
2123       (p >= _shared_metaspace_static_top)) {
2124     return true;
2125   } else {
2126     return false;
2127   }
2128 }
2129 
2130 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
2131   assert(UseSharedSpaces, "Must be called when UseSharedSpaces is enabled");
2132   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
2133 
2134   FileMapInfo* static_mapinfo = open_static_archive();
2135   FileMapInfo* dynamic_mapinfo = NULL;
2136 
2137   if (static_mapinfo != NULL) {
2138     dynamic_mapinfo = open_dynamic_archive();
2139 
2140     // First try to map at the requested address
2141     result = map_archives(static_mapinfo, dynamic_mapinfo, true);
2142     if (result == MAP_ARCHIVE_MMAP_FAILURE) {
2143       // Mapping has failed (probably due to ASLR). Let's map at an address chosen
2144       // by the OS.
2145       log_info(cds)("Try to map archive(s) at an alternative address");
2146       result = map_archives(static_mapinfo, dynamic_mapinfo, false);
2147     }
2148   }
2149 
2150   if (result == MAP_ARCHIVE_SUCCESS) {
2151     bool dynamic_mapped = (dynamic_mapinfo != NULL && dynamic_mapinfo->is_mapped());
2152     char* cds_base = static_mapinfo->mapped_base();
2153     char* cds_end =  dynamic_mapped ? dynamic_mapinfo->mapped_end() : static_mapinfo->mapped_end();
2154     set_shared_metaspace_range(cds_base, static_mapinfo->mapped_end(), cds_end);
2155     _relocation_delta = static_mapinfo->relocation_delta();
2156     if (dynamic_mapped) {
2157       FileMapInfo::set_shared_path_table(dynamic_mapinfo);
2158     } else {
2159       FileMapInfo::set_shared_path_table(static_mapinfo);
2160     }
2161     _requested_base_address = static_mapinfo->requested_base_address();
2162   } else {
2163     set_shared_metaspace_range(NULL, NULL, NULL);
2164     UseSharedSpaces = false;
2165     FileMapInfo::fail_continue("Unable to map shared spaces");
2166     if (PrintSharedArchiveAndExit) {
2167       vm_exit_during_initialization("Unable to use shared archive.");
2168     }
2169   }
2170 
2171   if (static_mapinfo != NULL && !static_mapinfo->is_mapped()) {
2172     delete static_mapinfo;
2173   }
2174   if (dynamic_mapinfo != NULL && !dynamic_mapinfo->is_mapped()) {
2175     delete dynamic_mapinfo;
2176   }
2177 }
2178 
2179 FileMapInfo* MetaspaceShared::open_static_archive() {
2180   FileMapInfo* mapinfo = new FileMapInfo(true);
2181   if (!mapinfo->initialize()) {
2182     delete(mapinfo);
2183     return NULL;
2184   }
2185   return mapinfo;
2186 }
2187 
2188 FileMapInfo* MetaspaceShared::open_dynamic_archive() {
2189   if (DynamicDumpSharedSpaces) {
2190     return NULL;
2191   }
2192   if (Arguments::GetSharedDynamicArchivePath() == NULL) {
2193     return NULL;
2194   }
2195 
2196   FileMapInfo* mapinfo = new FileMapInfo(false);
2197   if (!mapinfo->initialize()) {
2198     delete(mapinfo);
2199     return NULL;
2200   }
2201   return mapinfo;
2202 }
2203 
2204 // use_requested_addr:
2205 //  true  = map at FileMapHeader::_requested_base_address
2206 //  false = map at an alternative address picked by OS.
2207 MapArchiveResult MetaspaceShared::map_archives(FileMapInfo* static_mapinfo, FileMapInfo* dynamic_mapinfo,
2208                                                bool use_requested_addr) {
2209   if (use_requested_addr && static_mapinfo->requested_base_address() == NULL) {
2210     log_info(cds)("Archive(s) were created with -XX:SharedBaseAddress=0. Always map at os-selected address.");
2211     return MAP_ARCHIVE_MMAP_FAILURE;
2212   }
2213 
2214   PRODUCT_ONLY(if (ArchiveRelocationMode == 1 && use_requested_addr) {
2215       // For product build only -- this is for benchmarking the cost of doing relocation.
2216       // For debug builds, the check is done below, after reserving the space, for better test coverage
2217       // (see comment below).
2218       log_info(cds)("ArchiveRelocationMode == 1: always map archive(s) at an alternative address");
2219       return MAP_ARCHIVE_MMAP_FAILURE;
2220     });
2221 
2222   if (ArchiveRelocationMode == 2 && !use_requested_addr) {
2223     log_info(cds)("ArchiveRelocationMode == 2: never map archive(s) at an alternative address");
2224     return MAP_ARCHIVE_MMAP_FAILURE;
2225   };
2226 
2227   if (dynamic_mapinfo != NULL) {
2228     // Ensure that the OS won't be able to allocate new memory spaces between the two
2229     // archives, or else it would mess up the simple comparision in MetaspaceObj::is_shared().
2230     assert(static_mapinfo->mapping_end_offset() == dynamic_mapinfo->mapping_base_offset(), "no gap");
2231   }
2232 
2233   ReservedSpace archive_space_rs, class_space_rs;
2234   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
2235   char* mapped_base_address = reserve_address_space_for_archives(static_mapinfo, dynamic_mapinfo,
2236                                                                  use_requested_addr, archive_space_rs,
2237                                                                  class_space_rs);
2238   if (mapped_base_address == NULL) {
2239     result = MAP_ARCHIVE_MMAP_FAILURE;
2240     log_debug(cds)("Failed to reserve spaces (use_requested_addr=%u)", (unsigned)use_requested_addr);
2241   } else {
2242 
2243 #ifdef ASSERT
2244     // Some sanity checks after reserving address spaces for archives
2245     //  and class space.
2246     assert(archive_space_rs.is_reserved(), "Sanity");
2247     if (Metaspace::using_class_space()) {
2248       // Class space must closely follow the archive space. Both spaces
2249       //  must be aligned correctly.
2250       assert(class_space_rs.is_reserved(),
2251              "A class space should have been reserved");
2252       assert(class_space_rs.base() >= archive_space_rs.end(),
2253              "class space should follow the cds archive space");
2254       assert(is_aligned(archive_space_rs.base(),
2255                         MetaspaceShared::reserved_space_alignment()),
2256              "Archive space misaligned");
2257       assert(is_aligned(class_space_rs.base(),
2258                         Metaspace::reserve_alignment()),
2259              "class space misaligned");
2260     }
2261 #endif // ASSERT
2262 
2263     log_debug(cds)("Reserved archive_space_rs     [" INTPTR_FORMAT " - " INTPTR_FORMAT "] (" SIZE_FORMAT ") bytes",
2264                    p2i(archive_space_rs.base()), p2i(archive_space_rs.end()), archive_space_rs.size());
2265     log_debug(cds)("Reserved class_space_rs [" INTPTR_FORMAT " - " INTPTR_FORMAT "] (" SIZE_FORMAT ") bytes",
2266                    p2i(class_space_rs.base()), p2i(class_space_rs.end()), class_space_rs.size());
2267 
2268     if (MetaspaceShared::use_windows_memory_mapping()) {
2269       // We have now reserved address space for the archives, and will map in
2270       //  the archive files into this space.
2271       //
2272       // Special handling for Windows: on Windows we cannot map a file view
2273       //  into an existing memory mapping. So, we unmap the address range we
2274       //  just reserved again, which will make it available for mapping the
2275       //  archives.
2276       // Reserving this range has not been for naught however since it makes
2277       //  us reasonably sure the address range is available.
2278       //
2279       // But still it may fail, since between unmapping the range and mapping
2280       //  in the archive someone else may grab the address space. Therefore
2281       //  there is a fallback in FileMap::map_region() where we just read in
2282       //  the archive files sequentially instead of mapping it in. We couple
2283       //  this with use_requested_addr, since we're going to patch all the
2284       //  pointers anyway so there's no benefit to mmap.
2285       if (use_requested_addr) {
2286         log_info(cds)("Windows mmap workaround: releasing archive space.");
2287         archive_space_rs.release();
2288       }
2289     }
2290     MapArchiveResult static_result = map_archive(static_mapinfo, mapped_base_address, archive_space_rs);
2291     MapArchiveResult dynamic_result = (static_result == MAP_ARCHIVE_SUCCESS) ?
2292                                      map_archive(dynamic_mapinfo, mapped_base_address, archive_space_rs) : MAP_ARCHIVE_OTHER_FAILURE;
2293 
2294     DEBUG_ONLY(if (ArchiveRelocationMode == 1 && use_requested_addr) {
2295       // This is for simulating mmap failures at the requested address. In
2296       //  debug builds, we do it here (after all archives have possibly been
2297       //  mapped), so we can thoroughly test the code for failure handling
2298       //  (releasing all allocated resource, etc).
2299       log_info(cds)("ArchiveRelocationMode == 1: always map archive(s) at an alternative address");
2300       if (static_result == MAP_ARCHIVE_SUCCESS) {
2301         static_result = MAP_ARCHIVE_MMAP_FAILURE;
2302       }
2303       if (dynamic_result == MAP_ARCHIVE_SUCCESS) {
2304         dynamic_result = MAP_ARCHIVE_MMAP_FAILURE;
2305       }
2306     });
2307 
2308     if (static_result == MAP_ARCHIVE_SUCCESS) {
2309       if (dynamic_result == MAP_ARCHIVE_SUCCESS) {
2310         result = MAP_ARCHIVE_SUCCESS;
2311       } else if (dynamic_result == MAP_ARCHIVE_OTHER_FAILURE) {
2312         assert(dynamic_mapinfo != NULL && !dynamic_mapinfo->is_mapped(), "must have failed");
2313         // No need to retry mapping the dynamic archive again, as it will never succeed
2314         // (bad file, etc) -- just keep the base archive.
2315         log_warning(cds, dynamic)("Unable to use shared archive. The top archive failed to load: %s",
2316                                   dynamic_mapinfo->full_path());
2317         result = MAP_ARCHIVE_SUCCESS;
2318         // TODO, we can give the unused space for the dynamic archive to class_space_rs, but there's no
2319         // easy API to do that right now.
2320       } else {
2321         result = MAP_ARCHIVE_MMAP_FAILURE;
2322       }
2323     } else if (static_result == MAP_ARCHIVE_OTHER_FAILURE) {
2324       result = MAP_ARCHIVE_OTHER_FAILURE;
2325     } else {
2326       result = MAP_ARCHIVE_MMAP_FAILURE;
2327     }
2328   }
2329 
2330   if (result == MAP_ARCHIVE_SUCCESS) {
2331     SharedBaseAddress = (size_t)mapped_base_address;
2332     LP64_ONLY({
2333         if (Metaspace::using_class_space()) {
2334           // Set up ccs in metaspace.
2335           Metaspace::initialize_class_space(class_space_rs);
2336 
2337           // Set up compressed Klass pointer encoding: the encoding range must
2338           //  cover both archive and class space.
2339           address cds_base = (address)static_mapinfo->mapped_base();
2340           address ccs_end = (address)class_space_rs.end();
2341           CompressedKlassPointers::initialize(cds_base, ccs_end - cds_base);
2342 
2343           // map_heap_regions() compares the current narrow oop and klass encodings
2344           // with the archived ones, so it must be done after all encodings are determined.
2345           static_mapinfo->map_heap_regions();
2346         }
2347       });
2348     log_info(cds)("Using optimized module handling %s", MetaspaceShared::use_optimized_module_handling() ? "enabled" : "disabled");
2349   } else {
2350     unmap_archive(static_mapinfo);
2351     unmap_archive(dynamic_mapinfo);
2352     release_reserved_spaces(archive_space_rs, class_space_rs);
2353   }
2354 
2355   return result;
2356 }
2357 
2358 
2359 // This will reserve two address spaces suitable to house Klass structures, one
2360 //  for the cds archives (static archive and optionally dynamic archive) and
2361 //  optionally one move for ccs.
2362 //
2363 // Since both spaces must fall within the compressed class pointer encoding
2364 //  range, they are allocated close to each other.
2365 //
2366 // Space for archives will be reserved first, followed by a potential gap,
2367 //  followed by the space for ccs:
2368 //
2369 // +-- Base address             A        B                     End
2370 // |                            |        |                      |
2371 // v                            v        v                      v
2372 // +-------------+--------------+        +----------------------+
2373 // | static arc  | [dyn. arch]  | [gap]  | compr. class space   |
2374 // +-------------+--------------+        +----------------------+
2375 //
2376 // (The gap may result from different alignment requirements between metaspace
2377 //  and CDS)
2378 //
2379 // If UseCompressedClassPointers is disabled, only one address space will be
2380 //  reserved:
2381 //
2382 // +-- Base address             End
2383 // |                            |
2384 // v                            v
2385 // +-------------+--------------+
2386 // | static arc  | [dyn. arch]  |
2387 // +-------------+--------------+
2388 //
2389 // Base address: If use_archive_base_addr address is true, the Base address is
2390 //  determined by the address stored in the static archive. If
2391 //  use_archive_base_addr address is false, this base address is determined
2392 //  by the platform.
2393 //
2394 // If UseCompressedClassPointers=1, the range encompassing both spaces will be
2395 //  suitable to en/decode narrow Klass pointers: the base will be valid for
2396 //  encoding, the range [Base, End) not surpass KlassEncodingMetaspaceMax.
2397 //
2398 // Return:
2399 //
2400 // - On success:
2401 //    - archive_space_rs will be reserved and large enough to host static and
2402 //      if needed dynamic archive: [Base, A).
2403 //      archive_space_rs.base and size will be aligned to CDS reserve
2404 //      granularity.
2405 //    - class_space_rs: If UseCompressedClassPointers=1, class_space_rs will
2406 //      be reserved. Its start address will be aligned to metaspace reserve
2407 //      alignment, which may differ from CDS alignment. It will follow the cds
2408 //      archive space, close enough such that narrow class pointer encoding
2409 //      covers both spaces.
2410 //      If UseCompressedClassPointers=0, class_space_rs remains unreserved.
2411 // - On error: NULL is returned and the spaces remain unreserved.
2412 char* MetaspaceShared::reserve_address_space_for_archives(FileMapInfo* static_mapinfo,
2413                                                           FileMapInfo* dynamic_mapinfo,
2414                                                           bool use_archive_base_addr,
2415                                                           ReservedSpace& archive_space_rs,
2416                                                           ReservedSpace& class_space_rs) {
2417 
2418   address const base_address = (address) (use_archive_base_addr ? static_mapinfo->requested_base_address() : NULL);
2419   const size_t archive_space_alignment = MetaspaceShared::reserved_space_alignment();
2420 
2421   // Size and requested location of the archive_space_rs (for both static and dynamic archives)
2422   assert(static_mapinfo->mapping_base_offset() == 0, "Must be");
2423   size_t archive_end_offset  = (dynamic_mapinfo == NULL) ? static_mapinfo->mapping_end_offset() : dynamic_mapinfo->mapping_end_offset();
2424   size_t archive_space_size = align_up(archive_end_offset, archive_space_alignment);
2425 
2426   // If a base address is given, it must have valid alignment and be suitable as encoding base.
2427   if (base_address != NULL) {
2428     assert(is_aligned(base_address, archive_space_alignment),
2429            "Archive base address invalid: " PTR_FORMAT ".", p2i(base_address));
2430     if (Metaspace::using_class_space()) {
2431       assert(CompressedKlassPointers::is_valid_base(base_address),
2432              "Archive base address invalid: " PTR_FORMAT ".", p2i(base_address));
2433     }
2434   }
2435 
2436   if (!Metaspace::using_class_space()) {
2437     // Get the simple case out of the way first:
2438     // no compressed class space, simple allocation.
2439     archive_space_rs = ReservedSpace(archive_space_size, archive_space_alignment,
2440                                      false /* bool large */, (char*)base_address);
2441     if (archive_space_rs.is_reserved()) {
2442       assert(base_address == NULL ||
2443              (address)archive_space_rs.base() == base_address, "Sanity");
2444       // Register archive space with NMT.
2445       MemTracker::record_virtual_memory_type(archive_space_rs.base(), mtClassShared);
2446       return archive_space_rs.base();
2447     }
2448     return NULL;
2449   }
2450 
2451 #ifdef _LP64
2452 
2453   // Complex case: two spaces adjacent to each other, both to be addressable
2454   //  with narrow class pointers.
2455   // We reserve the whole range spanning both spaces, then split that range up.
2456 
2457   const size_t class_space_alignment = Metaspace::reserve_alignment();
2458 
2459   // To simplify matters, lets assume that metaspace alignment will always be
2460   //  equal or a multiple of archive alignment.
2461   assert(is_power_of_2(class_space_alignment) &&
2462                        is_power_of_2(archive_space_alignment) &&
2463                        class_space_alignment >= archive_space_alignment,
2464                        "Sanity");
2465 
2466   const size_t class_space_size = CompressedClassSpaceSize;
2467   assert(CompressedClassSpaceSize > 0 &&
2468          is_aligned(CompressedClassSpaceSize, class_space_alignment),
2469          "CompressedClassSpaceSize malformed: "
2470          SIZE_FORMAT, CompressedClassSpaceSize);
2471 
2472   const size_t ccs_begin_offset = align_up(base_address + archive_space_size,
2473                                            class_space_alignment) - base_address;
2474   const size_t gap_size = ccs_begin_offset - archive_space_size;
2475 
2476   const size_t total_range_size =
2477       align_up(archive_space_size + gap_size + class_space_size,
2478                os::vm_allocation_granularity());
2479 
2480   ReservedSpace total_rs;
2481   if (base_address != NULL) {
2482     // Reserve at the given archive base address, or not at all.
2483     total_rs = ReservedSpace(total_range_size, archive_space_alignment,
2484                              false /* bool large */, (char*) base_address);
2485   } else {
2486     // Reserve at any address, but leave it up to the platform to choose a good one.
2487     total_rs = Metaspace::reserve_address_space_for_compressed_classes(total_range_size);
2488   }
2489 
2490   if (!total_rs.is_reserved()) {
2491     return NULL;
2492   }
2493 
2494   // Paranoid checks:
2495   assert(base_address == NULL || (address)total_rs.base() == base_address,
2496          "Sanity (" PTR_FORMAT " vs " PTR_FORMAT ")", p2i(base_address), p2i(total_rs.base()));
2497   assert(is_aligned(total_rs.base(), archive_space_alignment), "Sanity");
2498   assert(total_rs.size() == total_range_size, "Sanity");
2499   assert(CompressedKlassPointers::is_valid_base((address)total_rs.base()), "Sanity");
2500 
2501   // Now split up the space into ccs and cds archive. For simplicity, just leave
2502   //  the gap reserved at the end of the archive space.
2503   archive_space_rs = total_rs.first_part(ccs_begin_offset,
2504                                          (size_t)os::vm_allocation_granularity(),
2505                                          /*split=*/true);
2506   class_space_rs = total_rs.last_part(ccs_begin_offset);
2507 
2508   assert(is_aligned(archive_space_rs.base(), archive_space_alignment), "Sanity");
2509   assert(is_aligned(archive_space_rs.size(), archive_space_alignment), "Sanity");
2510   assert(is_aligned(class_space_rs.base(), class_space_alignment), "Sanity");
2511   assert(is_aligned(class_space_rs.size(), class_space_alignment), "Sanity");
2512 
2513   // NMT: fix up the space tags
2514   MemTracker::record_virtual_memory_type(archive_space_rs.base(), mtClassShared);
2515   MemTracker::record_virtual_memory_type(class_space_rs.base(), mtClass);
2516 
2517   return archive_space_rs.base();
2518 
2519 #else
2520   ShouldNotReachHere();
2521   return NULL;
2522 #endif
2523 
2524 }
2525 
2526 void MetaspaceShared::release_reserved_spaces(ReservedSpace& archive_space_rs,
2527                                               ReservedSpace& class_space_rs) {
2528   if (archive_space_rs.is_reserved()) {
2529     log_debug(cds)("Released shared space (archive) " INTPTR_FORMAT, p2i(archive_space_rs.base()));
2530     archive_space_rs.release();
2531   }
2532   if (class_space_rs.is_reserved()) {
2533     log_debug(cds)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
2534     class_space_rs.release();
2535   }
2536 }
2537 
2538 static int archive_regions[]  = {MetaspaceShared::mc,
2539                                  MetaspaceShared::rw,
2540                                  MetaspaceShared::ro};
2541 static int archive_regions_count  = 3;
2542 
2543 MapArchiveResult MetaspaceShared::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
2544   assert(UseSharedSpaces, "must be runtime");
2545   if (mapinfo == NULL) {
2546     return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
2547   }
2548 
2549   mapinfo->set_is_mapped(false);
2550 
2551   if (mapinfo->alignment() != (size_t)os::vm_allocation_granularity()) {
2552     log_error(cds)("Unable to map CDS archive -- os::vm_allocation_granularity() expected: " SIZE_FORMAT
2553                    " actual: %d", mapinfo->alignment(), os::vm_allocation_granularity());
2554     return MAP_ARCHIVE_OTHER_FAILURE;
2555   }
2556 
2557   MapArchiveResult result =
2558     mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
2559 
2560   if (result != MAP_ARCHIVE_SUCCESS) {
2561     unmap_archive(mapinfo);
2562     return result;
2563   }
2564 
2565   if (!mapinfo->validate_shared_path_table()) {
2566     unmap_archive(mapinfo);
2567     return MAP_ARCHIVE_OTHER_FAILURE;
2568   }
2569 
2570   mapinfo->set_is_mapped(true);
2571   return MAP_ARCHIVE_SUCCESS;
2572 }
2573 
2574 void MetaspaceShared::unmap_archive(FileMapInfo* mapinfo) {
2575   assert(UseSharedSpaces, "must be runtime");
2576   if (mapinfo != NULL) {
2577     mapinfo->unmap_regions(archive_regions, archive_regions_count);
2578     mapinfo->set_is_mapped(false);
2579   }
2580 }
2581 
2582 // Read the miscellaneous data from the shared file, and
2583 // serialize it out to its various destinations.
2584 
2585 void MetaspaceShared::initialize_shared_spaces() {
2586   FileMapInfo *static_mapinfo = FileMapInfo::current_info();
2587   _i2i_entry_code_buffers = static_mapinfo->i2i_entry_code_buffers();
2588   _i2i_entry_code_buffers_size = static_mapinfo->i2i_entry_code_buffers_size();
2589   char* buffer = static_mapinfo->cloned_vtables();
2590   clone_cpp_vtables((intptr_t*)buffer);
2591 
2592   // Verify various attributes of the archive, plus initialize the
2593   // shared string/symbol tables
2594   buffer = static_mapinfo->serialized_data();
2595   intptr_t* array = (intptr_t*)buffer;
2596   ReadClosure rc(&array);
2597   serialize(&rc);
2598 
2599   // Initialize the run-time symbol table.
2600   SymbolTable::create_table();
2601 
2602   static_mapinfo->patch_archived_heap_embedded_pointers();
2603 
2604   // Close the mapinfo file
2605   static_mapinfo->close();
2606 
2607   static_mapinfo->unmap_region(MetaspaceShared::bm);
2608 
2609   FileMapInfo *dynamic_mapinfo = FileMapInfo::dynamic_info();
2610   if (dynamic_mapinfo != NULL) {
2611     intptr_t* buffer = (intptr_t*)dynamic_mapinfo->serialized_data();
2612     ReadClosure rc(&buffer);
2613     SymbolTable::serialize_shared_table_header(&rc, false);
2614     SystemDictionaryShared::serialize_dictionary_headers(&rc, false);
2615     dynamic_mapinfo->close();
2616   }
2617 
2618   if (PrintSharedArchiveAndExit) {
2619     if (PrintSharedDictionary) {
2620       tty->print_cr("\nShared classes:\n");
2621       SystemDictionaryShared::print_on(tty);
2622     }
2623     if (FileMapInfo::current_info() == NULL || _archive_loading_failed) {
2624       tty->print_cr("archive is invalid");
2625       vm_exit(1);
2626     } else {
2627       tty->print_cr("archive is valid");
2628       vm_exit(0);
2629     }
2630   }
2631 }
2632 
2633 // JVM/TI RedefineClasses() support:
2634 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
2635   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2636 
2637   if (UseSharedSpaces) {
2638     // remap the shared readonly space to shared readwrite, private
2639     FileMapInfo* mapinfo = FileMapInfo::current_info();
2640     if (!mapinfo->remap_shared_readonly_as_readwrite()) {
2641       return false;
2642     }
2643     if (FileMapInfo::dynamic_info() != NULL) {
2644       mapinfo = FileMapInfo::dynamic_info();
2645       if (!mapinfo->remap_shared_readonly_as_readwrite()) {
2646         return false;
2647       }
2648     }
2649     _remapped_readwrite = true;
2650   }
2651   return true;
2652 }
2653 
2654 void MetaspaceShared::report_out_of_space(const char* name, size_t needed_bytes) {
2655   // This is highly unlikely to happen on 64-bits because we have reserved a 4GB space.
2656   // On 32-bit we reserve only 256MB so you could run out of space with 100,000 classes
2657   // or so.
2658   _mc_region.print_out_of_space_msg(name, needed_bytes);
2659   _rw_region.print_out_of_space_msg(name, needed_bytes);
2660   _ro_region.print_out_of_space_msg(name, needed_bytes);
2661 
2662   vm_exit_during_initialization(err_msg("Unable to allocate from '%s' region", name),
2663                                 "Please reduce the number of shared classes.");
2664 }
2665 
2666 // This is used to relocate the pointers so that the base archive can be mapped at
2667 // MetaspaceShared::requested_base_address() without runtime relocation.
2668 intx MetaspaceShared::final_delta() {
2669   return intx(MetaspaceShared::requested_base_address())  // We want the base archive to be mapped to here at runtime
2670        - intx(SharedBaseAddress);                         // .. but the base archive is mapped at here at dump time
2671 }
2672 
2673 void MetaspaceShared::print_on(outputStream* st) {
2674   if (UseSharedSpaces || DumpSharedSpaces) {
2675     st->print("CDS archive(s) mapped at: ");
2676     address base;
2677     address top;
2678     if (UseSharedSpaces) { // Runtime
2679       base = (address)MetaspaceObj::shared_metaspace_base();
2680       address static_top = (address)_shared_metaspace_static_top;
2681       top = (address)MetaspaceObj::shared_metaspace_top();
2682       st->print("[" PTR_FORMAT "-" PTR_FORMAT "-" PTR_FORMAT "), ", p2i(base), p2i(static_top), p2i(top));
2683     } else if (DumpSharedSpaces) { // Dump Time
2684       base = (address)_shared_rs.base();
2685       top = (address)_shared_rs.end();
2686       st->print("[" PTR_FORMAT "-" PTR_FORMAT "), ", p2i(base), p2i(top));
2687     }
2688     st->print("size " SIZE_FORMAT ", ", top - base);
2689     st->print("SharedBaseAddress: " PTR_FORMAT ", ArchiveRelocationMode: %d.", SharedBaseAddress, (int)ArchiveRelocationMode);
2690   } else {
2691     st->print("CDS disabled.");
2692   }
2693   st->cr();
2694 }
2695 
2696 
2697 
2698 
2699