1 /*
   2  * Copyright (c) 2003, 2021, 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 "cds/archiveBuilder.hpp"
  28 #include "cds/archiveUtils.inline.hpp"
  29 #include "cds/dynamicArchive.hpp"
  30 #include "cds/filemap.hpp"
  31 #include "cds/heapShared.inline.hpp"
  32 #include "cds/metaspaceShared.hpp"
  33 #include "classfile/altHashing.hpp"
  34 #include "classfile/classFileStream.hpp"
  35 #include "classfile/classLoader.hpp"
  36 #include "classfile/classLoader.inline.hpp"
  37 #include "classfile/classLoaderData.inline.hpp"
  38 #include "classfile/classLoaderExt.hpp"
  39 #include "classfile/symbolTable.hpp"
  40 #include "classfile/systemDictionaryShared.hpp"
  41 #include "classfile/vmClasses.hpp"
  42 #include "classfile/vmSymbols.hpp"
  43 #include "logging/log.hpp"
  44 #include "logging/logStream.hpp"
  45 #include "logging/logMessage.hpp"
  46 #include "memory/iterator.inline.hpp"
  47 #include "memory/metadataFactory.hpp"
  48 #include "memory/metaspaceClosure.hpp"
  49 #include "memory/oopFactory.hpp"
  50 #include "memory/universe.hpp"
  51 #include "oops/compressedOops.hpp"
  52 #include "oops/compressedOops.inline.hpp"
  53 #include "oops/objArrayOop.hpp"
  54 #include "oops/oop.inline.hpp"
  55 #include "prims/jvmtiExport.hpp"
  56 #include "runtime/arguments.hpp"
  57 #include "runtime/globals_extension.hpp"
  58 #include "runtime/java.hpp"
  59 #include "runtime/mutexLocker.hpp"
  60 #include "runtime/os.hpp"
  61 #include "runtime/vm_version.hpp"
  62 #include "services/memTracker.hpp"
  63 #include "utilities/align.hpp"
  64 #include "utilities/bitMap.inline.hpp"
  65 #include "utilities/classpathStream.hpp"
  66 #include "utilities/defaultStream.hpp"
  67 #include "utilities/ostream.hpp"
  68 #if INCLUDE_G1GC
  69 #include "gc/g1/g1CollectedHeap.hpp"
  70 #include "gc/g1/heapRegion.hpp"
  71 #endif
  72 
  73 # include <sys/stat.h>
  74 # include <errno.h>
  75 
  76 #ifndef O_BINARY       // if defined (Win32) use binary files.
  77 #define O_BINARY 0     // otherwise do nothing.
  78 #endif
  79 
  80 // Complain and stop. All error conditions occurring during the writing of
  81 // an archive file should stop the process.  Unrecoverable errors during
  82 // the reading of the archive file should stop the process.
  83 
  84 static void fail_exit(const char *msg, va_list ap) {
  85   // This occurs very early during initialization: tty is not initialized.
  86   jio_fprintf(defaultStream::error_stream(),
  87               "An error has occurred while processing the"
  88               " shared archive file.\n");
  89   jio_vfprintf(defaultStream::error_stream(), msg, ap);
  90   jio_fprintf(defaultStream::error_stream(), "\n");
  91   // Do not change the text of the below message because some tests check for it.
  92   vm_exit_during_initialization("Unable to use shared archive.", NULL);
  93 }
  94 
  95 
  96 void FileMapInfo::fail_stop(const char *msg, ...) {
  97         va_list ap;
  98   va_start(ap, msg);
  99   fail_exit(msg, ap);   // Never returns.
 100   va_end(ap);           // for completeness.
 101 }
 102 
 103 
 104 // Complain and continue.  Recoverable errors during the reading of the
 105 // archive file may continue (with sharing disabled).
 106 //
 107 // If we continue, then disable shared spaces and close the file.
 108 
 109 void FileMapInfo::fail_continue(const char *msg, ...) {
 110   va_list ap;
 111   va_start(ap, msg);
 112   if (PrintSharedArchiveAndExit && _validating_shared_path_table) {
 113     // If we are doing PrintSharedArchiveAndExit and some of the classpath entries
 114     // do not validate, we can still continue "limping" to validate the remaining
 115     // entries. No need to quit.
 116     tty->print("[");
 117     tty->vprint(msg, ap);
 118     tty->print_cr("]");
 119   } else {
 120     if (RequireSharedSpaces) {
 121       fail_exit(msg, ap);
 122     } else {
 123       if (log_is_enabled(Info, cds)) {
 124         ResourceMark rm;
 125         LogStream ls(Log(cds)::info());
 126         ls.print("UseSharedSpaces: ");
 127         ls.vprint_cr(msg, ap);
 128       }
 129     }
 130   }
 131   va_end(ap);
 132 }
 133 
 134 inline void CDSMustMatchFlags::do_print(outputStream* st, bool v) {
 135   st->print("%s", v ? "true" : "false");
 136 }
 137 
 138 inline void CDSMustMatchFlags::do_print(outputStream* st, intx v) {
 139   st->print(INTX_FORMAT, v);
 140 }
 141 
 142 inline void CDSMustMatchFlags::do_print(outputStream* st, uintx v) {
 143   st->print(UINTX_FORMAT, v);
 144 }
 145 
 146 inline void CDSMustMatchFlags::do_print(outputStream* st, double v) {
 147   st->print("%f", v);
 148 }
 149 
 150 void CDSMustMatchFlags::init() {
 151   Arguments::assert_is_dumping_archive();
 152   _max_name_width = 0;
 153 
 154 #define INIT_CDS_MUST_MATCH_FLAG(n) \
 155   _v_##n = n; \
 156   _max_name_width = MAX2(_max_name_width,strlen(#n));
 157   CDS_MUST_MATCH_FLAGS_DO(INIT_CDS_MUST_MATCH_FLAG);
 158 #undef INIT_CDS_MUST_MATCH_FLAG
 159 }
 160 
 161 bool CDSMustMatchFlags::runtime_check() const {
 162 #define CHECK_CDS_MUST_MATCH_FLAG(n) \
 163   if (_v_##n != n) { \
 164     ResourceMark rm; \
 165     stringStream ss; \
 166     ss.print("VM option %s is different between dumptime (", #n);  \
 167     do_print(&ss, _v_ ## n); \
 168     ss.print(") and runtime ("); \
 169     do_print(&ss, n); \
 170     ss.print(")"); \
 171     FileMapInfo::fail_continue("%s", ss.as_string()); \
 172     return false; \
 173   }
 174   CDS_MUST_MATCH_FLAGS_DO(CHECK_CDS_MUST_MATCH_FLAG);
 175 #undef CHECK_CDS_MUST_MATCH_FLAG
 176 
 177   return true;
 178 }
 179 
 180 void CDSMustMatchFlags::print_info() const {
 181   LogTarget(Info, cds) lt;
 182   if (lt.is_enabled()) {
 183     LogStream ls(lt);
 184     ls.print_cr("Recorded VM flags during dumptime:");
 185     print(&ls);
 186   }
 187 }
 188 
 189 void CDSMustMatchFlags::print(outputStream* st) const {
 190 #define PRINT_CDS_MUST_MATCH_FLAG(n) \
 191   st->print("- %-s ", #n);                   \
 192   st->sp(int(_max_name_width - strlen(#n))); \
 193   do_print(st, _v_##n);                      \
 194   st->cr();
 195   CDS_MUST_MATCH_FLAGS_DO(PRINT_CDS_MUST_MATCH_FLAG);
 196 #undef PRINT_CDS_MUST_MATCH_FLAG
 197 }
 198 
 199 // Fill in the fileMapInfo structure with data about this VM instance.
 200 
 201 // This method copies the vm version info into header_version.  If the version is too
 202 // long then a truncated version, which has a hash code appended to it, is copied.
 203 //
 204 // Using a template enables this method to verify that header_version is an array of
 205 // length JVM_IDENT_MAX.  This ensures that the code that writes to the CDS file and
 206 // the code that reads the CDS file will both use the same size buffer.  Hence, will
 207 // use identical truncation.  This is necessary for matching of truncated versions.
 208 template <int N> static void get_header_version(char (&header_version) [N]) {
 209   assert(N == JVM_IDENT_MAX, "Bad header_version size");
 210 
 211   const char *vm_version = VM_Version::internal_vm_info_string();
 212   const int version_len = (int)strlen(vm_version);
 213 
 214   memset(header_version, 0, JVM_IDENT_MAX);
 215 
 216   if (version_len < (JVM_IDENT_MAX-1)) {
 217     strcpy(header_version, vm_version);
 218 
 219   } else {
 220     // Get the hash value.  Use a static seed because the hash needs to return the same
 221     // value over multiple jvm invocations.
 222     uint32_t hash = AltHashing::halfsiphash_32(8191, (const uint8_t*)vm_version, version_len);
 223 
 224     // Truncate the ident, saving room for the 8 hex character hash value.
 225     strncpy(header_version, vm_version, JVM_IDENT_MAX-9);
 226 
 227     // Append the hash code as eight hex digits.
 228     sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash);
 229     header_version[JVM_IDENT_MAX-1] = 0;  // Null terminate.
 230   }
 231 
 232   assert(header_version[JVM_IDENT_MAX-1] == 0, "must be");
 233 }
 234 
 235 FileMapInfo::FileMapInfo(bool is_static) {
 236   memset((void*)this, 0, sizeof(FileMapInfo));
 237   _is_static = is_static;
 238   if (_is_static) {
 239     assert(_current_info == NULL, "must be singleton"); // not thread safe
 240     _current_info = this;
 241   } else {
 242     assert(_dynamic_archive_info == NULL, "must be singleton"); // not thread safe
 243     _dynamic_archive_info = this;
 244   }
 245   _file_offset = 0;
 246   _file_open = false;
 247 }
 248 
 249 FileMapInfo::~FileMapInfo() {
 250   if (_is_static) {
 251     assert(_current_info == this, "must be singleton"); // not thread safe
 252     _current_info = NULL;
 253   } else {
 254     assert(_dynamic_archive_info == this, "must be singleton"); // not thread safe
 255     _dynamic_archive_info = NULL;
 256   }
 257 }
 258 
 259 void FileMapInfo::populate_header(size_t core_region_alignment) {
 260   assert(_header == NULL, "Sanity check");
 261   size_t c_header_size;
 262   size_t header_size;
 263   size_t base_archive_name_size = 0;
 264   size_t base_archive_name_offset = 0;
 265   if (is_static()) {
 266     c_header_size = sizeof(FileMapHeader);
 267     header_size = c_header_size;
 268   } else {
 269     // dynamic header including base archive name for non-default base archive
 270     c_header_size = sizeof(DynamicArchiveHeader);
 271     header_size = c_header_size;
 272     if (!FLAG_IS_DEFAULT(SharedArchiveFile)) {
 273       base_archive_name_size = strlen(Arguments::GetSharedArchivePath()) + 1;
 274       header_size += base_archive_name_size;
 275       base_archive_name_offset = c_header_size;
 276     }
 277   }
 278   _header = (FileMapHeader*)os::malloc(header_size, mtInternal);
 279   memset((void*)_header, 0, header_size);
 280   _header->populate(this,
 281                     core_region_alignment,
 282                     header_size,
 283                     base_archive_name_size,
 284                     base_archive_name_offset);
 285 }
 286 
 287 void FileMapHeader::populate(FileMapInfo *info, size_t core_region_alignment,
 288                              size_t header_size, size_t base_archive_name_size,
 289                              size_t base_archive_name_offset) {
 290   // 1. We require _generic_header._magic to be at the beginning of the file
 291   // 2. FileMapHeader also assumes that _generic_header is at the beginning of the file
 292   assert(offset_of(FileMapHeader, _generic_header) == 0, "must be");
 293   set_header_size((unsigned int)header_size);
 294   set_base_archive_name_offset((unsigned int)base_archive_name_offset);
 295   set_base_archive_name_size((unsigned int)base_archive_name_size);
 296   set_magic(DynamicDumpSharedSpaces ? CDS_DYNAMIC_ARCHIVE_MAGIC : CDS_ARCHIVE_MAGIC);
 297   set_version(CURRENT_CDS_ARCHIVE_VERSION);
 298 
 299   if (!info->is_static() && base_archive_name_size != 0) {
 300     // copy base archive name
 301     copy_base_archive_name(Arguments::GetSharedArchivePath());
 302   }
 303   _core_region_alignment = core_region_alignment;
 304   _obj_alignment = ObjectAlignmentInBytes;
 305   _compact_strings = CompactStrings;
 306   if (DumpSharedSpaces && HeapShared::can_write()) {
 307     _narrow_oop_mode = CompressedOops::mode();
 308     _narrow_oop_base = CompressedOops::base();
 309     _narrow_oop_shift = CompressedOops::shift();
 310     _heap_begin = CompressedOops::begin();
 311     _heap_end = CompressedOops::end();
 312   }
 313   _compressed_oops = UseCompressedOops;
 314   _compressed_class_ptrs = UseCompressedClassPointers;
 315   _max_heap_size = MaxHeapSize;
 316   _narrow_klass_shift = CompressedKlassPointers::shift();
 317   _use_optimized_module_handling = MetaspaceShared::use_optimized_module_handling();
 318   _use_full_module_graph = MetaspaceShared::use_full_module_graph();
 319 
 320   // The following fields are for sanity checks for whether this archive
 321   // will function correctly with this JVM and the bootclasspath it's
 322   // invoked with.
 323 
 324   // JVM version string ... changes on each build.
 325   get_header_version(_jvm_ident);
 326 
 327   _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
 328   _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
 329   _num_module_paths = ClassLoader::num_module_path_entries();
 330   _max_used_path_index = ClassLoaderExt::max_used_path_index();
 331 
 332   _verify_local = BytecodeVerificationLocal;
 333   _verify_remote = BytecodeVerificationRemote;
 334   _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
 335   _has_non_jar_in_classpath = ClassLoaderExt::has_non_jar_in_classpath();
 336   _requested_base_address = (char*)SharedBaseAddress;
 337   _mapped_base_address = (char*)SharedBaseAddress;
 338   _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;
 339   _must_match.init();
 340 
 341   if (!DynamicDumpSharedSpaces) {
 342     set_shared_path_table(info->_shared_path_table);
 343     CDS_JAVA_HEAP_ONLY(_heap_obj_roots = CompressedOops::encode(HeapShared::roots());)
 344   }
 345 }
 346 
 347 void FileMapHeader::copy_base_archive_name(const char* archive) {
 348   assert(base_archive_name_size() != 0, "_base_archive_name_size not set");
 349   assert(base_archive_name_offset() != 0, "_base_archive_name_offset not set");
 350   assert(header_size() > sizeof(*this), "_base_archive_name_size not included in header size?");
 351   memcpy((char*)this + base_archive_name_offset(), archive, base_archive_name_size());
 352 }
 353 
 354 void FileMapHeader::print(outputStream* st) {
 355   ResourceMark rm;
 356 
 357   st->print_cr("- magic:                          0x%08x", magic());
 358   st->print_cr("- crc:                            0x%08x", crc());
 359   st->print_cr("- version:                        %d", version());
 360   st->print_cr("- header_size:                    " UINT32_FORMAT, header_size());
 361   st->print_cr("- base_archive_name_offset:       " UINT32_FORMAT, base_archive_name_offset());
 362   st->print_cr("- base_archive_name_size:         " UINT32_FORMAT, base_archive_name_size());
 363 
 364   for (int i = 0; i < NUM_CDS_REGIONS; i++) {
 365     FileMapRegion* si = space_at(i);
 366     si->print(st, i);
 367   }
 368   st->print_cr("============ end regions ======== ");
 369 
 370   st->print_cr("- core_region_alignment:          " SIZE_FORMAT, _core_region_alignment);
 371   st->print_cr("- obj_alignment:                  %d", _obj_alignment);
 372   st->print_cr("- narrow_oop_base:                " INTPTR_FORMAT, p2i(_narrow_oop_base));
 373   st->print_cr("- narrow_oop_base:                " INTPTR_FORMAT, p2i(_narrow_oop_base));
 374   st->print_cr("- narrow_oop_shift                %d", _narrow_oop_shift);
 375   st->print_cr("- compact_strings:                %d", _compact_strings);
 376   st->print_cr("- max_heap_size:                  " UINTX_FORMAT, _max_heap_size);
 377   st->print_cr("- narrow_oop_mode:                %d", _narrow_oop_mode);
 378   st->print_cr("- narrow_klass_shift:             %d", _narrow_klass_shift);
 379   st->print_cr("- compressed_oops:                %d", _compressed_oops);
 380   st->print_cr("- compressed_class_ptrs:          %d", _compressed_class_ptrs);
 381   st->print_cr("- cloned_vtables_offset:          " SIZE_FORMAT_HEX, _cloned_vtables_offset);
 382   st->print_cr("- serialized_data_offset:         " SIZE_FORMAT_HEX, _serialized_data_offset);
 383   st->print_cr("- heap_end:                       " INTPTR_FORMAT, p2i(_heap_end));
 384   st->print_cr("- jvm_ident:                      %s", _jvm_ident);
 385   st->print_cr("- shared_path_table_offset:       " SIZE_FORMAT_HEX, _shared_path_table_offset);
 386   st->print_cr("- shared_path_table_size:         %d", _shared_path_table_size);
 387   st->print_cr("- app_class_paths_start_index:    %d", _app_class_paths_start_index);
 388   st->print_cr("- app_module_paths_start_index:   %d", _app_module_paths_start_index);
 389   st->print_cr("- num_module_paths:               %d", _num_module_paths);
 390   st->print_cr("- max_used_path_index:            %d", _max_used_path_index);
 391   st->print_cr("- verify_local:                   %d", _verify_local);
 392   st->print_cr("- verify_remote:                  %d", _verify_remote);
 393   st->print_cr("- has_platform_or_app_classes:    %d", _has_platform_or_app_classes);
 394   st->print_cr("- has_non_jar_in_classpath:       %d", _has_non_jar_in_classpath);
 395   st->print_cr("- requested_base_address:         " INTPTR_FORMAT, p2i(_requested_base_address));
 396   st->print_cr("- mapped_base_address:            " INTPTR_FORMAT, p2i(_mapped_base_address));
 397   st->print_cr("- allow_archiving_with_java_agent:%d", _allow_archiving_with_java_agent);
 398   st->print_cr("- use_optimized_module_handling:  %d", _use_optimized_module_handling);
 399   st->print_cr("- use_full_module_graph           %d", _use_full_module_graph);
 400   st->print_cr("- ptrmap_size_in_bits:            " SIZE_FORMAT, _ptrmap_size_in_bits);
 401   _must_match.print(st);
 402 }
 403 
 404 void SharedClassPathEntry::init_as_non_existent(const char* path, TRAPS) {
 405   _type = non_existent_entry;
 406   set_name(path, CHECK);
 407 }
 408 
 409 void SharedClassPathEntry::init(bool is_modules_image,
 410                                 bool is_module_path,
 411                                 ClassPathEntry* cpe, TRAPS) {
 412   Arguments::assert_is_dumping_archive();
 413   _timestamp = 0;
 414   _filesize  = 0;
 415   _from_class_path_attr = false;
 416 
 417   struct stat st;
 418   if (os::stat(cpe->name(), &st) == 0) {
 419     if ((st.st_mode & S_IFMT) == S_IFDIR) {
 420       _type = dir_entry;
 421     } else {
 422       // The timestamp of the modules_image is not checked at runtime.
 423       if (is_modules_image) {
 424         _type = modules_image_entry;
 425       } else {
 426         _type = jar_entry;
 427         _timestamp = st.st_mtime;
 428         _from_class_path_attr = cpe->from_class_path_attr();
 429       }
 430       _filesize = st.st_size;
 431       _is_module_path = is_module_path;
 432     }
 433   } else {
 434     // The file/dir must exist, or it would not have been added
 435     // into ClassLoader::classpath_entry().
 436     //
 437     // If we can't access a jar file in the boot path, then we can't
 438     // make assumptions about where classes get loaded from.
 439     FileMapInfo::fail_stop("Unable to open file %s.", cpe->name());
 440   }
 441 
 442   // No need to save the name of the module file, as it will be computed at run time
 443   // to allow relocation of the JDK directory.
 444   const char* name = is_modules_image  ? "" : cpe->name();
 445   set_name(name, CHECK);
 446 }
 447 
 448 void SharedClassPathEntry::set_name(const char* name, TRAPS) {
 449   size_t len = strlen(name) + 1;
 450   _name = MetadataFactory::new_array<char>(ClassLoaderData::the_null_class_loader_data(), (int)len, CHECK);
 451   strcpy(_name->data(), name);
 452 }
 453 
 454 void SharedClassPathEntry::copy_from(SharedClassPathEntry* ent, ClassLoaderData* loader_data, TRAPS) {
 455   _type = ent->_type;
 456   _is_module_path = ent->_is_module_path;
 457   _timestamp = ent->_timestamp;
 458   _filesize = ent->_filesize;
 459   _from_class_path_attr = ent->_from_class_path_attr;
 460   set_name(ent->name(), CHECK);
 461 
 462   if (ent->is_jar() && !ent->is_signed() && ent->manifest() != NULL) {
 463     Array<u1>* buf = MetadataFactory::new_array<u1>(loader_data,
 464                                                     ent->manifest_size(),
 465                                                     CHECK);
 466     char* p = (char*)(buf->data());
 467     memcpy(p, ent->manifest(), ent->manifest_size());
 468     set_manifest(buf);
 469   }
 470 }
 471 
 472 const char* SharedClassPathEntry::name() const {
 473   if (UseSharedSpaces && is_modules_image()) {
 474     // In order to validate the runtime modules image file size against the archived
 475     // size information, we need to obtain the runtime modules image path. The recorded
 476     // dump time modules image path in the archive may be different from the runtime path
 477     // if the JDK image has beed moved after generating the archive.
 478     return ClassLoader::get_jrt_entry()->name();
 479   } else {
 480     return _name->data();
 481   }
 482 }
 483 
 484 bool SharedClassPathEntry::validate(bool is_class_path) const {
 485   assert(UseSharedSpaces, "runtime only");
 486 
 487   struct stat st;
 488   const char* name = this->name();
 489 
 490   bool ok = true;
 491   log_info(class, path)("checking shared classpath entry: %s", name);
 492   if (os::stat(name, &st) != 0 && is_class_path) {
 493     // If the archived module path entry does not exist at runtime, it is not fatal
 494     // (no need to invalid the shared archive) because the shared runtime visibility check
 495     // filters out any archived module classes that do not have a matching runtime
 496     // module path location.
 497     FileMapInfo::fail_continue("Required classpath entry does not exist: %s", name);
 498     ok = false;
 499   } else if (is_dir()) {
 500     if (!os::dir_is_empty(name)) {
 501       FileMapInfo::fail_continue("directory is not empty: %s", name);
 502       ok = false;
 503     }
 504   } else if ((has_timestamp() && _timestamp != st.st_mtime) ||
 505              _filesize != st.st_size) {
 506     ok = false;
 507     if (PrintSharedArchiveAndExit) {
 508       FileMapInfo::fail_continue(_timestamp != st.st_mtime ?
 509                                  "Timestamp mismatch" :
 510                                  "File size mismatch");
 511     } else {
 512       FileMapInfo::fail_continue("A jar file is not the one used while building"
 513                                  " the shared archive file: %s", name);
 514     }
 515   }
 516 
 517   if (PrintSharedArchiveAndExit && !ok) {
 518     // If PrintSharedArchiveAndExit is enabled, don't report failure to the
 519     // caller. Please see above comments for more details.
 520     ok = true;
 521     MetaspaceShared::set_archive_loading_failed();
 522   }
 523   return ok;
 524 }
 525 
 526 bool SharedClassPathEntry::check_non_existent() const {
 527   assert(_type == non_existent_entry, "must be");
 528   log_info(class, path)("should be non-existent: %s", name());
 529   struct stat st;
 530   if (os::stat(name(), &st) != 0) {
 531     log_info(class, path)("ok");
 532     return true; // file doesn't exist
 533   } else {
 534     return false;
 535   }
 536 }
 537 
 538 
 539 void SharedClassPathEntry::metaspace_pointers_do(MetaspaceClosure* it) {
 540   it->push(&_name);
 541   it->push(&_manifest);
 542 }
 543 
 544 void SharedPathTable::metaspace_pointers_do(MetaspaceClosure* it) {
 545   it->push(&_table);
 546   for (int i=0; i<_size; i++) {
 547     path_at(i)->metaspace_pointers_do(it);
 548   }
 549 }
 550 
 551 void SharedPathTable::dumptime_init(ClassLoaderData* loader_data, TRAPS) {
 552   size_t entry_size = sizeof(SharedClassPathEntry);
 553   int num_entries = 0;
 554   num_entries += ClassLoader::num_boot_classpath_entries();
 555   num_entries += ClassLoader::num_app_classpath_entries();
 556   num_entries += ClassLoader::num_module_path_entries();
 557   num_entries += FileMapInfo::num_non_existent_class_paths();
 558   size_t bytes = entry_size * num_entries;
 559 
 560   _table = MetadataFactory::new_array<u8>(loader_data, (int)bytes, CHECK);
 561   _size = num_entries;
 562 }
 563 
 564 // Make a copy of the _shared_path_table for use during dynamic CDS dump.
 565 // It is needed because some Java code continues to execute after dynamic dump has finished.
 566 // However, during dynamic dump, we have modified FileMapInfo::_shared_path_table so
 567 // FileMapInfo::shared_path(i) returns incorrect information in ClassLoader::record_result().
 568 void FileMapInfo::copy_shared_path_table(ClassLoaderData* loader_data, TRAPS) {
 569   size_t entry_size = sizeof(SharedClassPathEntry);
 570   size_t bytes = entry_size * _shared_path_table.size();
 571 
 572   Array<u8>* array = MetadataFactory::new_array<u8>(loader_data, (int)bytes, CHECK);
 573   _saved_shared_path_table = SharedPathTable(array, _shared_path_table.size());
 574 
 575   for (int i = 0; i < _shared_path_table.size(); i++) {
 576     _saved_shared_path_table.path_at(i)->copy_from(shared_path(i), loader_data, CHECK);
 577   }
 578   _saved_shared_path_table_array = array;
 579 }
 580 
 581 void FileMapInfo::clone_shared_path_table(TRAPS) {
 582   Arguments::assert_is_dumping_archive();
 583 
 584   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
 585   ClassPathEntry* jrt = ClassLoader::get_jrt_entry();
 586 
 587   assert(jrt != NULL,
 588          "No modular java runtime image present when allocating the CDS classpath entry table");
 589 
 590   if (_saved_shared_path_table_array != NULL) {
 591     MetadataFactory::free_array<u8>(loader_data, _saved_shared_path_table_array);
 592     _saved_shared_path_table_array = NULL;
 593   }
 594 
 595   copy_shared_path_table(loader_data, CHECK);
 596 }
 597 
 598 void FileMapInfo::allocate_shared_path_table(TRAPS) {
 599   Arguments::assert_is_dumping_archive();
 600 
 601   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
 602   ClassPathEntry* jrt = ClassLoader::get_jrt_entry();
 603 
 604   assert(jrt != NULL,
 605          "No modular java runtime image present when allocating the CDS classpath entry table");
 606 
 607   _shared_path_table.dumptime_init(loader_data, CHECK);
 608 
 609   // 1. boot class path
 610   int i = 0;
 611   i = add_shared_classpaths(i, "boot",   jrt, CHECK);
 612   i = add_shared_classpaths(i, "app",    ClassLoader::app_classpath_entries(), CHECK);
 613   i = add_shared_classpaths(i, "module", ClassLoader::module_path_entries(), CHECK);
 614 
 615   for (int x = 0; x < num_non_existent_class_paths(); x++, i++) {
 616     const char* path = _non_existent_class_paths->at(x);
 617     shared_path(i)->init_as_non_existent(path, CHECK);
 618   }
 619 
 620   assert(i == _shared_path_table.size(), "number of shared path entry mismatch");
 621   clone_shared_path_table(CHECK);
 622 }
 623 
 624 int FileMapInfo::add_shared_classpaths(int i, const char* which, ClassPathEntry *cpe, TRAPS) {
 625   while (cpe != NULL) {
 626     bool is_jrt = (cpe == ClassLoader::get_jrt_entry());
 627     bool is_module_path = i >= ClassLoaderExt::app_module_paths_start_index();
 628     const char* type = (is_jrt ? "jrt" : (cpe->is_jar_file() ? "jar" : "dir"));
 629     log_info(class, path)("add %s shared path (%s) %s", which, type, cpe->name());
 630     SharedClassPathEntry* ent = shared_path(i);
 631     ent->init(is_jrt, is_module_path, cpe, CHECK_0);
 632     if (cpe->is_jar_file()) {
 633       update_jar_manifest(cpe, ent, CHECK_0);
 634     }
 635     if (is_jrt) {
 636       cpe = ClassLoader::get_next_boot_classpath_entry(cpe);
 637     } else {
 638       cpe = cpe->next();
 639     }
 640     i++;
 641   }
 642 
 643   return i;
 644 }
 645 
 646 void FileMapInfo::check_nonempty_dir_in_shared_path_table() {
 647   Arguments::assert_is_dumping_archive();
 648 
 649   bool has_nonempty_dir = false;
 650 
 651   int last = _shared_path_table.size() - 1;
 652   if (last > ClassLoaderExt::max_used_path_index()) {
 653      // no need to check any path beyond max_used_path_index
 654      last = ClassLoaderExt::max_used_path_index();
 655   }
 656 
 657   for (int i = 0; i <= last; i++) {
 658     SharedClassPathEntry *e = shared_path(i);
 659     if (e->is_dir()) {
 660       const char* path = e->name();
 661       if (!os::dir_is_empty(path)) {
 662         log_error(cds)("Error: non-empty directory '%s'", path);
 663         has_nonempty_dir = true;
 664       }
 665     }
 666   }
 667 
 668   if (has_nonempty_dir) {
 669     ClassLoader::exit_with_path_failure("Cannot have non-empty directory in paths", NULL);
 670   }
 671 }
 672 
 673 void FileMapInfo::record_non_existent_class_path_entry(const char* path) {
 674   Arguments::assert_is_dumping_archive();
 675   log_info(class, path)("non-existent Class-Path entry %s", path);
 676   if (_non_existent_class_paths == NULL) {
 677     _non_existent_class_paths = new (ResourceObj::C_HEAP, mtClass)GrowableArray<const char*>(10, mtClass);
 678   }
 679   _non_existent_class_paths->append(os::strdup(path));
 680 }
 681 
 682 int FileMapInfo::num_non_existent_class_paths() {
 683   Arguments::assert_is_dumping_archive();
 684   if (_non_existent_class_paths != NULL) {
 685     return _non_existent_class_paths->length();
 686   } else {
 687     return 0;
 688   }
 689 }
 690 
 691 int FileMapInfo::get_module_shared_path_index(Symbol* location) {
 692   if (location->starts_with("jrt:", 4) && get_number_of_shared_paths() > 0) {
 693     assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
 694     return 0;
 695   }
 696 
 697   if (ClassLoaderExt::app_module_paths_start_index() >= get_number_of_shared_paths()) {
 698     // The archive(s) were created without --module-path option
 699     return -1;
 700   }
 701 
 702   if (!location->starts_with("file:", 5)) {
 703     return -1;
 704   }
 705 
 706   // skip_uri_protocol was also called during dump time -- see ClassLoaderExt::process_module_table()
 707   ResourceMark rm;
 708   const char* file = ClassLoader::skip_uri_protocol(location->as_C_string());
 709   for (int i = ClassLoaderExt::app_module_paths_start_index(); i < get_number_of_shared_paths(); i++) {
 710     SharedClassPathEntry* ent = shared_path(i);
 711     assert(ent->in_named_module(), "must be");
 712     bool cond = strcmp(file, ent->name()) == 0;
 713     log_debug(class, path)("get_module_shared_path_index (%d) %s : %s = %s", i,
 714                            location->as_C_string(), ent->name(), cond ? "same" : "different");
 715     if (cond) {
 716       return i;
 717     }
 718   }
 719 
 720   return -1;
 721 }
 722 
 723 class ManifestStream: public ResourceObj {
 724   private:
 725   u1*   _buffer_start; // Buffer bottom
 726   u1*   _buffer_end;   // Buffer top (one past last element)
 727   u1*   _current;      // Current buffer position
 728 
 729  public:
 730   // Constructor
 731   ManifestStream(u1* buffer, int length) : _buffer_start(buffer),
 732                                            _current(buffer) {
 733     _buffer_end = buffer + length;
 734   }
 735 
 736   static bool is_attr(u1* attr, const char* name) {
 737     return strncmp((const char*)attr, name, strlen(name)) == 0;
 738   }
 739 
 740   static char* copy_attr(u1* value, size_t len) {
 741     char* buf = NEW_RESOURCE_ARRAY(char, len + 1);
 742     strncpy(buf, (char*)value, len);
 743     buf[len] = 0;
 744     return buf;
 745   }
 746 
 747   // The return value indicates if the JAR is signed or not
 748   bool check_is_signed() {
 749     u1* attr = _current;
 750     bool isSigned = false;
 751     while (_current < _buffer_end) {
 752       if (*_current == '\n') {
 753         *_current = '\0';
 754         u1* value = (u1*)strchr((char*)attr, ':');
 755         if (value != NULL) {
 756           assert(*(value+1) == ' ', "Unrecognized format" );
 757           if (strstr((char*)attr, "-Digest") != NULL) {
 758             isSigned = true;
 759             break;
 760           }
 761         }
 762         *_current = '\n'; // restore
 763         attr = _current + 1;
 764       }
 765       _current ++;
 766     }
 767     return isSigned;
 768   }
 769 };
 770 
 771 void FileMapInfo::update_jar_manifest(ClassPathEntry *cpe, SharedClassPathEntry* ent, TRAPS) {
 772   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
 773   ResourceMark rm(THREAD);
 774   jint manifest_size;
 775 
 776   assert(cpe->is_jar_file() && ent->is_jar(), "the shared class path entry is not a JAR file");
 777   char* manifest = ClassLoaderExt::read_manifest(THREAD, cpe, &manifest_size);
 778   if (manifest != NULL) {
 779     ManifestStream* stream = new ManifestStream((u1*)manifest,
 780                                                 manifest_size);
 781     if (stream->check_is_signed()) {
 782       ent->set_is_signed();
 783     } else {
 784       // Copy the manifest into the shared archive
 785       manifest = ClassLoaderExt::read_raw_manifest(THREAD, cpe, &manifest_size);
 786       Array<u1>* buf = MetadataFactory::new_array<u1>(loader_data,
 787                                                       manifest_size,
 788                                                       CHECK);
 789       char* p = (char*)(buf->data());
 790       memcpy(p, manifest, manifest_size);
 791       ent->set_manifest(buf);
 792     }
 793   }
 794 }
 795 
 796 char* FileMapInfo::skip_first_path_entry(const char* path) {
 797   size_t path_sep_len = strlen(os::path_separator());
 798   char* p = strstr((char*)path, os::path_separator());
 799   if (p != NULL) {
 800     debug_only( {
 801       size_t image_name_len = strlen(MODULES_IMAGE_NAME);
 802       assert(strncmp(p - image_name_len, MODULES_IMAGE_NAME, image_name_len) == 0,
 803              "first entry must be the modules image");
 804     } );
 805     p += path_sep_len;
 806   } else {
 807     debug_only( {
 808       assert(ClassLoader::string_ends_with(path, MODULES_IMAGE_NAME),
 809              "first entry must be the modules image");
 810     } );
 811   }
 812   return p;
 813 }
 814 
 815 int FileMapInfo::num_paths(const char* path) {
 816   if (path == NULL) {
 817     return 0;
 818   }
 819   int npaths = 1;
 820   char* p = (char*)path;
 821   while (p != NULL) {
 822     char* prev = p;
 823     p = strstr((char*)p, os::path_separator());
 824     if (p != NULL) {
 825       p++;
 826       // don't count empty path
 827       if ((p - prev) > 1) {
 828        npaths++;
 829       }
 830     }
 831   }
 832   return npaths;
 833 }
 834 
 835 GrowableArray<const char*>* FileMapInfo::create_path_array(const char* paths) {
 836   GrowableArray<const char*>* path_array = new GrowableArray<const char*>(10);
 837   JavaThread* current = JavaThread::current();
 838   ClasspathStream cp_stream(paths);
 839   bool non_jar_in_cp = header()->has_non_jar_in_classpath();
 840   while (cp_stream.has_next()) {
 841     const char* path = cp_stream.get_next();
 842     if (!non_jar_in_cp) {
 843       struct stat st;
 844       if (os::stat(path, &st) == 0) {
 845         path_array->append(path);
 846       }
 847     } else {
 848       const char* canonical_path = ClassLoader::get_canonical_path(path, current);
 849       if (canonical_path != NULL) {
 850         char* error_msg = NULL;
 851         jzfile* zip = ClassLoader::open_zip_file(canonical_path, &error_msg, current);
 852         if (zip != NULL && error_msg == NULL) {
 853           path_array->append(path);
 854         }
 855       }
 856     }
 857   }
 858   return path_array;
 859 }
 860 
 861 bool FileMapInfo::classpath_failure(const char* msg, const char* name) {
 862   ClassLoader::trace_class_path(msg, name);
 863   if (PrintSharedArchiveAndExit) {
 864     MetaspaceShared::set_archive_loading_failed();
 865   }
 866   return false;
 867 }
 868 
 869 bool FileMapInfo::check_paths(int shared_path_start_idx, int num_paths, GrowableArray<const char*>* rp_array) {
 870   int i = 0;
 871   int j = shared_path_start_idx;
 872   bool mismatch = false;
 873   while (i < num_paths && !mismatch) {
 874     while (shared_path(j)->from_class_path_attr()) {
 875       // shared_path(j) was expanded from the JAR file attribute "Class-Path:"
 876       // during dump time. It's not included in the -classpath VM argument.
 877       j++;
 878     }
 879     if (!os::same_files(shared_path(j)->name(), rp_array->at(i))) {
 880       mismatch = true;
 881     }
 882     i++;
 883     j++;
 884   }
 885   return mismatch;
 886 }
 887 
 888 bool FileMapInfo::validate_boot_class_paths() {
 889   //
 890   // - Archive contains boot classes only - relaxed boot path check:
 891   //   Extra path elements appended to the boot path at runtime are allowed.
 892   //
 893   // - Archive contains application or platform classes - strict boot path check:
 894   //   Validate the entire runtime boot path, which must be compatible
 895   //   with the dump time boot path. Appending boot path at runtime is not
 896   //   allowed.
 897   //
 898 
 899   // The first entry in boot path is the modules_image (guaranteed by
 900   // ClassLoader::setup_boot_search_path()). Skip the first entry. The
 901   // path of the runtime modules_image may be different from the dump
 902   // time path (e.g. the JDK image is copied to a different location
 903   // after generating the shared archive), which is acceptable. For most
 904   // common cases, the dump time boot path might contain modules_image only.
 905   char* runtime_boot_path = Arguments::get_sysclasspath();
 906   char* rp = skip_first_path_entry(runtime_boot_path);
 907   assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
 908   int dp_len = header()->app_class_paths_start_index() - 1; // ignore the first path to the module image
 909   bool mismatch = false;
 910 
 911   bool relaxed_check = !header()->has_platform_or_app_classes();
 912   if (dp_len == 0 && rp == NULL) {
 913     return true;   // ok, both runtime and dump time boot paths have modules_images only
 914   } else if (dp_len == 0 && rp != NULL) {
 915     if (relaxed_check) {
 916       return true;   // ok, relaxed check, runtime has extra boot append path entries
 917     } else {
 918       mismatch = true;
 919     }
 920   } else if (dp_len > 0 && rp != NULL) {
 921     int num;
 922     ResourceMark rm;
 923     GrowableArray<const char*>* rp_array = create_path_array(rp);
 924     int rp_len = rp_array->length();
 925     if (rp_len >= dp_len) {
 926       if (relaxed_check) {
 927         // only check the leading entries in the runtime boot path, up to
 928         // the length of the dump time boot path
 929         num = dp_len;
 930       } else {
 931         // check the full runtime boot path, must match with dump time
 932         num = rp_len;
 933       }
 934       mismatch = check_paths(1, num, rp_array);
 935     } else {
 936       // create_path_array() ignores non-existing paths. Although the dump time and runtime boot classpath lengths
 937       // are the same initially, after the call to create_path_array(), the runtime boot classpath length could become
 938       // shorter. We consider boot classpath mismatch in this case.
 939       mismatch = true;
 940     }
 941   }
 942 
 943   if (mismatch) {
 944     // The paths are different
 945     return classpath_failure("[BOOT classpath mismatch, actual =", runtime_boot_path);
 946   }
 947   return true;
 948 }
 949 
 950 bool FileMapInfo::validate_app_class_paths(int shared_app_paths_len) {
 951   const char *appcp = Arguments::get_appclasspath();
 952   assert(appcp != NULL, "NULL app classpath");
 953   int rp_len = num_paths(appcp);
 954   bool mismatch = false;
 955   if (rp_len < shared_app_paths_len) {
 956     return classpath_failure("Run time APP classpath is shorter than the one at dump time: ", appcp);
 957   }
 958   if (shared_app_paths_len != 0 && rp_len != 0) {
 959     // Prefix is OK: E.g., dump with -cp foo.jar, but run with -cp foo.jar:bar.jar.
 960     ResourceMark rm;
 961     GrowableArray<const char*>* rp_array = create_path_array(appcp);
 962     if (rp_array->length() == 0) {
 963       // None of the jar file specified in the runtime -cp exists.
 964       return classpath_failure("None of the jar file specified in the runtime -cp exists: -Djava.class.path=", appcp);
 965     }
 966     if (rp_array->length() < shared_app_paths_len) {
 967       // create_path_array() ignores non-existing paths. Although the dump time and runtime app classpath lengths
 968       // are the same initially, after the call to create_path_array(), the runtime app classpath length could become
 969       // shorter. We consider app classpath mismatch in this case.
 970       return classpath_failure("[APP classpath mismatch, actual: -Djava.class.path=", appcp);
 971     }
 972 
 973     // Handling of non-existent entries in the classpath: we eliminate all the non-existent
 974     // entries from both the dump time classpath (ClassLoader::update_class_path_entry_list)
 975     // and the runtime classpath (FileMapInfo::create_path_array), and check the remaining
 976     // entries. E.g.:
 977     //
 978     // dump : -cp a.jar:NE1:NE2:b.jar  -> a.jar:b.jar -> recorded in archive.
 979     // run 1: -cp NE3:a.jar:NE4:b.jar  -> a.jar:b.jar -> matched
 980     // run 2: -cp x.jar:NE4:b.jar      -> x.jar:b.jar -> mismatched
 981 
 982     int j = header()->app_class_paths_start_index();
 983     mismatch = check_paths(j, shared_app_paths_len, rp_array);
 984     if (mismatch) {
 985       return classpath_failure("[APP classpath mismatch, actual: -Djava.class.path=", appcp);
 986     }
 987   }
 988   return true;
 989 }
 990 
 991 void FileMapInfo::log_paths(const char* msg, int start_idx, int end_idx) {
 992   LogTarget(Info, class, path) lt;
 993   if (lt.is_enabled()) {
 994     LogStream ls(lt);
 995     ls.print("%s", msg);
 996     const char* prefix = "";
 997     for (int i = start_idx; i < end_idx; i++) {
 998       ls.print("%s%s", prefix, shared_path(i)->name());
 999       prefix = os::path_separator();
1000     }
1001     ls.cr();
1002   }
1003 }
1004 
1005 bool FileMapInfo::validate_shared_path_table() {
1006   assert(UseSharedSpaces, "runtime only");
1007 
1008   _validating_shared_path_table = true;
1009 
1010   // Load the shared path table info from the archive header
1011   _shared_path_table = header()->shared_path_table();
1012   if (DynamicDumpSharedSpaces) {
1013     // Only support dynamic dumping with the usage of the default CDS archive
1014     // or a simple base archive.
1015     // If the base layer archive contains additional path component besides
1016     // the runtime image and the -cp, dynamic dumping is disabled.
1017     //
1018     // When dynamic archiving is enabled, the _shared_path_table is overwritten
1019     // to include the application path and stored in the top layer archive.
1020     assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
1021     if (header()->app_class_paths_start_index() > 1) {
1022       DynamicDumpSharedSpaces = false;
1023       warning(
1024         "Dynamic archiving is disabled because base layer archive has appended boot classpath");
1025     }
1026     if (header()->num_module_paths() > 0) {
1027       DynamicDumpSharedSpaces = false;
1028       warning(
1029         "Dynamic archiving is disabled because base layer archive has module path");
1030     }
1031   }
1032 
1033   log_paths("Expecting BOOT path=", 0, header()->app_class_paths_start_index());
1034   log_paths("Expecting -Djava.class.path=", header()->app_class_paths_start_index(), header()->app_module_paths_start_index());
1035 
1036   int module_paths_start_index = header()->app_module_paths_start_index();
1037   int shared_app_paths_len = 0;
1038 
1039   // validate the path entries up to the _max_used_path_index
1040   for (int i=0; i < header()->max_used_path_index() + 1; i++) {
1041     if (i < module_paths_start_index) {
1042       if (shared_path(i)->validate()) {
1043         // Only count the app class paths not from the "Class-path" attribute of a jar manifest.
1044         if (!shared_path(i)->from_class_path_attr() && i >= header()->app_class_paths_start_index()) {
1045           shared_app_paths_len++;
1046         }
1047         log_info(class, path)("ok");
1048       } else {
1049         if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) {
1050           assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
1051         }
1052         return false;
1053       }
1054     } else if (i >= module_paths_start_index) {
1055       if (shared_path(i)->validate(false /* not a class path entry */)) {
1056         log_info(class, path)("ok");
1057       } else {
1058         if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) {
1059           assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
1060         }
1061         return false;
1062       }
1063     }
1064   }
1065 
1066   if (header()->max_used_path_index() == 0) {
1067     // default archive only contains the module image in the bootclasspath
1068     assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
1069   } else {
1070     if (!validate_boot_class_paths() || !validate_app_class_paths(shared_app_paths_len)) {
1071       fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)");
1072       return false;
1073     }
1074   }
1075 
1076   validate_non_existent_class_paths();
1077 
1078   _validating_shared_path_table = false;
1079 
1080 #if INCLUDE_JVMTI
1081   if (_classpath_entries_for_jvmti != NULL) {
1082     os::free(_classpath_entries_for_jvmti);
1083   }
1084   size_t sz = sizeof(ClassPathEntry*) * get_number_of_shared_paths();
1085   _classpath_entries_for_jvmti = (ClassPathEntry**)os::malloc(sz, mtClass);
1086   memset((void*)_classpath_entries_for_jvmti, 0, sz);
1087 #endif
1088 
1089   return true;
1090 }
1091 
1092 void FileMapInfo::validate_non_existent_class_paths() {
1093   // All of the recorded non-existent paths came from the Class-Path: attribute from the JAR
1094   // files on the app classpath. If any of these are found to exist during runtime,
1095   // it will change how classes are loading for the app loader. For safety, disable
1096   // loading of archived platform/app classes (currently there's no way to disable just the
1097   // app classes).
1098 
1099   assert(UseSharedSpaces, "runtime only");
1100   for (int i = header()->app_module_paths_start_index() + header()->num_module_paths();
1101        i < get_number_of_shared_paths();
1102        i++) {
1103     SharedClassPathEntry* ent = shared_path(i);
1104     if (!ent->check_non_existent()) {
1105       warning("Archived non-system classes are disabled because the "
1106               "file %s exists", ent->name());
1107       header()->set_has_platform_or_app_classes(false);
1108     }
1109   }
1110 }
1111 
1112 // a utility class for checking file header
1113 class FileHeaderHelper {
1114   int _fd;
1115   GenericCDSFileMapHeader _header;
1116 
1117 public:
1118   FileHeaderHelper() {
1119     _fd = -1;
1120   }
1121 
1122   ~FileHeaderHelper() {
1123     if (_fd != -1) {
1124       os::close(_fd);
1125     }
1126   }
1127 
1128   bool initialize(const char* archive_name) {
1129     _fd = os::open(archive_name, O_RDONLY | O_BINARY, 0);
1130     if (_fd < 0) {
1131       return false;
1132     }
1133     return initialize(_fd);
1134   }
1135 
1136   // for an already opened file, do not set _fd
1137   bool initialize(int fd) {
1138     assert(fd != -1, "Archive should be opened");
1139     size_t size = sizeof(GenericCDSFileMapHeader);
1140     lseek(fd, 0, SEEK_SET);
1141     size_t n = os::read(fd, (void*)&_header, (unsigned int)size);
1142     if (n != size) {
1143       vm_exit_during_initialization("Unable to read generic CDS file map header from shared archive");
1144       return false;
1145     }
1146     return true;
1147   }
1148 
1149   GenericCDSFileMapHeader* get_generic_file_header() {
1150     return &_header;
1151   }
1152 
1153   char* read_base_archive_name() {
1154     assert(_fd != -1, "Archive should be open");
1155     size_t name_size = _header._base_archive_name_size;
1156     assert(name_size != 0, "For non-default base archive, name size should be non-zero!");
1157     char* base_name = NEW_C_HEAP_ARRAY(char, name_size, mtInternal);
1158     lseek(_fd, _header._base_archive_name_offset, SEEK_SET); // position to correct offset.
1159     size_t n = os::read(_fd, base_name, (unsigned int)name_size);
1160     if (n != name_size) {
1161       log_info(cds)("Unable to read base archive name from archive");
1162       FREE_C_HEAP_ARRAY(char, base_name);
1163       return nullptr;
1164     }
1165     if (base_name[name_size - 1] != '\0' || strlen(base_name) != name_size - 1) {
1166       log_info(cds)("Base archive name is damaged");
1167       FREE_C_HEAP_ARRAY(char, base_name);
1168       return nullptr;
1169     }
1170     if (!os::file_exists(base_name)) {
1171       log_info(cds)("Base archive %s does not exist", base_name);
1172       FREE_C_HEAP_ARRAY(char, base_name);
1173       return nullptr;
1174     }
1175     return base_name;
1176   }
1177 };
1178 
1179 bool FileMapInfo::check_archive(const char* archive_name, bool is_static) {
1180   FileHeaderHelper file_helper;
1181   if (!file_helper.initialize(archive_name)) {
1182     // do not vm_exit_during_initialization here because Arguments::init_shared_archive_paths()
1183     // requires a shared archive name. The open_for_read() function will log a message regarding
1184     // failure in opening a shared archive.
1185     return false;
1186   }
1187 
1188   GenericCDSFileMapHeader* header = file_helper.get_generic_file_header();
1189   if (is_static) {
1190     if (header->_magic != CDS_ARCHIVE_MAGIC) {
1191       vm_exit_during_initialization("Not a base shared archive", archive_name);
1192       return false;
1193     }
1194     if (header->_base_archive_name_offset != 0) {
1195       log_info(cds)("_base_archive_name_offset should be 0");
1196       log_info(cds)("_base_archive_name_offset = " UINT32_FORMAT, header->_base_archive_name_offset);
1197       return false;
1198     }
1199   } else {
1200     if (header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
1201       vm_exit_during_initialization("Not a top shared archive", archive_name);
1202       return false;
1203     }
1204     unsigned int name_size = header->_base_archive_name_size;
1205     unsigned int name_offset = header->_base_archive_name_offset;
1206     unsigned int header_size = header->_header_size;
1207     if (name_offset + name_size != header_size) {
1208       log_info(cds)("_header_size should be equal to _base_archive_name_offset plus _base_archive_name_size");
1209       log_info(cds)("  _base_archive_name_size   = " UINT32_FORMAT, name_size);
1210       log_info(cds)("  _base_archive_name_offset = " UINT32_FORMAT, name_offset);
1211       log_info(cds)("  _header_size              = " UINT32_FORMAT, header_size);
1212       return false;
1213     }
1214     char* base_name = file_helper.read_base_archive_name();
1215     if (base_name == nullptr) {
1216       return false;
1217     }
1218     FREE_C_HEAP_ARRAY(char, base_name);
1219   }
1220   return true;
1221 }
1222 
1223 bool FileMapInfo::get_base_archive_name_from_header(const char* archive_name,
1224                                                     char** base_archive_name) {
1225   FileHeaderHelper file_helper;
1226   if (!file_helper.initialize(archive_name)) {
1227     return false;
1228   }
1229   GenericCDSFileMapHeader* header = file_helper.get_generic_file_header();
1230   if (header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
1231     // Not a dynamic header, no need to proceed further.
1232     return false;
1233   }
1234 
1235   if ((header->_base_archive_name_size == 0 && header->_base_archive_name_offset != 0) ||
1236       (header->_base_archive_name_size != 0 && header->_base_archive_name_offset == 0)) {
1237     fail_continue("Default base archive not set correct");
1238     return false;
1239   }
1240   if (header->_base_archive_name_size == 0 &&
1241       header->_base_archive_name_offset == 0) {
1242     *base_archive_name = Arguments::get_default_shared_archive_path();
1243   } else {
1244     // read the base archive name
1245     *base_archive_name = file_helper.read_base_archive_name();
1246     if (*base_archive_name == nullptr) {
1247       return false;
1248     }
1249   }
1250   return true;
1251 }
1252 
1253 // Read the FileMapInfo information from the file.
1254 
1255 bool FileMapInfo::init_from_file(int fd) {
1256   FileHeaderHelper file_helper;
1257   if (!file_helper.initialize(fd)) {
1258     fail_continue("Unable to read the file header.");
1259     return false;
1260   }
1261   GenericCDSFileMapHeader* gen_header = file_helper.get_generic_file_header();
1262 
1263   unsigned int expected_magic = is_static() ? CDS_ARCHIVE_MAGIC : CDS_DYNAMIC_ARCHIVE_MAGIC;
1264   if (gen_header->_magic != expected_magic) {
1265     log_info(cds)("_magic expected: 0x%08x", expected_magic);
1266     log_info(cds)("         actual: 0x%08x", gen_header->_magic);
1267     FileMapInfo::fail_continue("The shared archive file has a bad magic number.");
1268     return false;
1269   }
1270 
1271   _header = (FileMapHeader*)os::malloc(gen_header->_header_size, mtInternal);
1272   lseek(fd, 0, SEEK_SET); // reset to begin of the archive
1273   size_t size = gen_header->_header_size;
1274   size_t n = os::read(fd, (void*)_header, (unsigned int)size);
1275   if (n != size) {
1276     fail_continue("Failed to read file header from the top archive file\n");
1277     return false;
1278   }
1279 
1280   if (header()->version() != CURRENT_CDS_ARCHIVE_VERSION) {
1281     log_info(cds)("_version expected: %d", CURRENT_CDS_ARCHIVE_VERSION);
1282     log_info(cds)("           actual: %d", header()->version());
1283     fail_continue("The shared archive file has the wrong version.");
1284     return false;
1285   }
1286 
1287   unsigned int base_offset = header()->base_archive_name_offset();
1288   unsigned int name_size = header()->base_archive_name_size();
1289   unsigned int header_size = header()->header_size();
1290   if (base_offset != 0 && name_size != 0) {
1291     if (header_size != base_offset + name_size) {
1292       log_info(cds)("_header_size: " UINT32_FORMAT, header_size);
1293       log_info(cds)("base_archive_name_size: " UINT32_FORMAT, name_size);
1294       log_info(cds)("base_archive_name_offset: " UINT32_FORMAT, base_offset);
1295       FileMapInfo::fail_continue("The shared archive file has an incorrect header size.");
1296       return false;
1297     }
1298   }
1299 
1300   const char* actual_ident = header()->jvm_ident();
1301 
1302   if (actual_ident[JVM_IDENT_MAX-1] != 0) {
1303     FileMapInfo::fail_continue("JVM version identifier is corrupted.");
1304     return false;
1305   }
1306 
1307   char expected_ident[JVM_IDENT_MAX];
1308   get_header_version(expected_ident);
1309   if (strncmp(actual_ident, expected_ident, JVM_IDENT_MAX-1) != 0) {
1310     log_info(cds)("_jvm_ident expected: %s", expected_ident);
1311     log_info(cds)("             actual: %s", actual_ident);
1312     FileMapInfo::fail_continue("The shared archive file was created by a different"
1313                   " version or build of HotSpot");
1314     return false;
1315   }
1316 
1317   if (VerifySharedSpaces) {
1318     int expected_crc = header()->compute_crc();
1319     if (expected_crc != header()->crc()) {
1320       log_info(cds)("_crc expected: %d", expected_crc);
1321       log_info(cds)("       actual: %d", header()->crc());
1322       FileMapInfo::fail_continue("Header checksum verification failed.");
1323       return false;
1324     }
1325   }
1326 
1327   _file_offset = header()->header_size(); // accounts for the size of _base_archive_name
1328 
1329   if (is_static()) {
1330     // just checking the last region is sufficient since the archive is written
1331     // in sequential order
1332     size_t len = lseek(fd, 0, SEEK_END);
1333     FileMapRegion* si = space_at(MetaspaceShared::last_valid_region);
1334     // The last space might be empty
1335     if (si->file_offset() > len || len - si->file_offset() < si->used()) {
1336       fail_continue("The shared archive file has been truncated.");
1337       return false;
1338     }
1339   }
1340 
1341   if (!header()->check_must_match_flags()) {
1342     return false;
1343   }
1344 
1345   return true;
1346 }
1347 
1348 void FileMapInfo::seek_to_position(size_t pos) {
1349   if (lseek(_fd, (long)pos, SEEK_SET) < 0) {
1350     fail_stop("Unable to seek to position " SIZE_FORMAT, pos);
1351   }
1352 }
1353 
1354 // Read the FileMapInfo information from the file.
1355 bool FileMapInfo::open_for_read() {
1356   if (_file_open) {
1357     return true;
1358   }
1359   if (is_static()) {
1360     _full_path = Arguments::GetSharedArchivePath();
1361   } else {
1362     _full_path = Arguments::GetSharedDynamicArchivePath();
1363   }
1364   log_info(cds)("trying to map %s", _full_path);
1365   int fd = os::open(_full_path, O_RDONLY | O_BINARY, 0);
1366   if (fd < 0) {
1367     if (errno == ENOENT) {
1368       fail_continue("Specified shared archive not found (%s).", _full_path);
1369     } else {
1370       fail_continue("Failed to open shared archive file (%s).",
1371                     os::strerror(errno));
1372     }
1373     return false;
1374   } else {
1375     log_info(cds)("Opened archive %s.", _full_path);
1376   }
1377 
1378   _fd = fd;
1379   _file_open = true;
1380   return true;
1381 }
1382 
1383 // Write the FileMapInfo information to the file.
1384 
1385 void FileMapInfo::open_for_write(const char* path) {
1386   if (path == NULL) {
1387     _full_path = Arguments::GetSharedArchivePath();
1388   } else {
1389     _full_path = path;
1390   }
1391   LogMessage(cds) msg;
1392   if (msg.is_info()) {
1393     msg.info("Dumping shared data to file: ");
1394     msg.info("   %s", _full_path);
1395   }
1396 
1397 #ifdef _WINDOWS  // On Windows, need WRITE permission to remove the file.
1398     chmod(_full_path, _S_IREAD | _S_IWRITE);
1399 #endif
1400 
1401   // Use remove() to delete the existing file because, on Unix, this will
1402   // allow processes that have it open continued access to the file.
1403   remove(_full_path);
1404   int fd = os::open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444);
1405   if (fd < 0) {
1406     fail_stop("Unable to create shared archive file %s: (%s).", _full_path,
1407               os::strerror(errno));
1408   }
1409   _fd = fd;
1410   _file_open = true;
1411 
1412   // Seek past the header. We will write the header after all regions are written
1413   // and their CRCs computed.
1414   size_t header_bytes = header()->header_size();
1415 
1416   header_bytes = align_up(header_bytes, MetaspaceShared::core_region_alignment());
1417   _file_offset = header_bytes;
1418   seek_to_position(_file_offset);
1419 }
1420 
1421 // Write the header to the file, seek to the next allocation boundary.
1422 
1423 void FileMapInfo::write_header() {
1424   _file_offset = 0;
1425   seek_to_position(_file_offset);
1426   assert(is_file_position_aligned(), "must be");
1427   write_bytes(header(), header()->header_size());
1428 }
1429 
1430 size_t FileMapRegion::used_aligned() const {
1431   return align_up(used(), MetaspaceShared::core_region_alignment());
1432 }
1433 
1434 void FileMapRegion::init(int region_index, size_t mapping_offset, size_t size, bool read_only,
1435                          bool allow_exec, int crc) {
1436   _is_heap_region = HeapShared::is_heap_region(region_index);
1437   _is_bitmap_region = (region_index == MetaspaceShared::bm);
1438   _mapping_offset = mapping_offset;
1439   _used = size;
1440   _read_only = read_only;
1441   _allow_exec = allow_exec;
1442   _crc = crc;
1443   _mapped_from_file = false;
1444   _mapped_base = NULL;
1445 }
1446 
1447 
1448 static const char* region_name(int region_index) {
1449   static const char* names[] = {
1450     "rw", "ro", "bm", "ca0", "ca1", "oa0", "oa1"
1451   };
1452   const int num_regions = sizeof(names)/sizeof(names[0]);
1453   assert(0 <= region_index && region_index < num_regions, "sanity");
1454 
1455   return names[region_index];
1456 }
1457 
1458 void FileMapRegion::print(outputStream* st, int region_index) {
1459   st->print_cr("============ region ============= %d \"%s\"", region_index, region_name(region_index));
1460   st->print_cr("- crc:                            0x%08x", _crc);
1461   st->print_cr("- read_only:                      %d", _read_only);
1462   st->print_cr("- allow_exec:                     %d", _allow_exec);
1463   st->print_cr("- is_heap_region:                 %d", _is_heap_region);
1464   st->print_cr("- is_bitmap_region:               %d", _is_bitmap_region);
1465   st->print_cr("- mapped_from_file:               %d", _mapped_from_file);
1466   st->print_cr("- file_offset:                    " SIZE_FORMAT_HEX, _file_offset);
1467   st->print_cr("- mapping_offset:                 " SIZE_FORMAT_HEX, _mapping_offset);
1468   st->print_cr("- used:                           " SIZE_FORMAT, _used);
1469   st->print_cr("- oopmap_offset:                  " SIZE_FORMAT_HEX, _oopmap_offset);
1470   st->print_cr("- oopmap_size_in_bits:            " SIZE_FORMAT, _oopmap_size_in_bits);
1471   st->print_cr("- mapped_base:                    " INTPTR_FORMAT, p2i(_mapped_base));
1472 }
1473 
1474 void FileMapInfo::write_region(int region, char* base, size_t size,
1475                                bool read_only, bool allow_exec) {
1476   Arguments::assert_is_dumping_archive();
1477 
1478   FileMapRegion* si = space_at(region);
1479   char* requested_base;
1480   size_t mapping_offset = 0;
1481 
1482   if (region == MetaspaceShared::bm) {
1483     requested_base = NULL; // always NULL for bm region
1484   } else if (size == 0) {
1485     // This is an unused region (e.g., a heap region when !INCLUDE_CDS_JAVA_HEAP)
1486     requested_base = NULL;
1487   } else if (HeapShared::is_heap_region(region)) {
1488     assert(!DynamicDumpSharedSpaces, "must be");
1489     requested_base = base;
1490     mapping_offset = (size_t)CompressedOops::encode_not_null(cast_to_oop(base));
1491     assert(mapping_offset == (size_t)(uint32_t)mapping_offset, "must be 32-bit only");
1492   } else {
1493     char* requested_SharedBaseAddress = (char*)MetaspaceShared::requested_base_address();
1494     requested_base = ArchiveBuilder::current()->to_requested(base);
1495     assert(requested_base >= requested_SharedBaseAddress, "must be");
1496     mapping_offset = requested_base - requested_SharedBaseAddress;
1497   }
1498 
1499   si->set_file_offset(_file_offset);
1500   int crc = ClassLoader::crc32(0, base, (jint)size);
1501   if (size > 0) {
1502     log_info(cds)("Shared file region (%-3s)  %d: " SIZE_FORMAT_W(8)
1503                    " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08)
1504                    " crc 0x%08x",
1505                    region_name(region), region, size, p2i(requested_base), _file_offset, crc);
1506   }
1507   si->init(region, mapping_offset, size, read_only, allow_exec, crc);
1508 
1509   if (base != NULL) {
1510     write_bytes_aligned(base, size);
1511   }
1512 }
1513 
1514 size_t FileMapInfo::set_oopmaps_offset(GrowableArray<ArchiveHeapOopmapInfo>* oopmaps, size_t curr_size) {
1515   for (int i = 0; i < oopmaps->length(); i++) {
1516     oopmaps->at(i)._offset = curr_size;
1517     curr_size += oopmaps->at(i)._oopmap_size_in_bytes;
1518   }
1519   return curr_size;
1520 }
1521 
1522 size_t FileMapInfo::write_oopmaps(GrowableArray<ArchiveHeapOopmapInfo>* oopmaps, size_t curr_offset, char* buffer) {
1523   for (int i = 0; i < oopmaps->length(); i++) {
1524     memcpy(buffer + curr_offset, oopmaps->at(i)._oopmap, oopmaps->at(i)._oopmap_size_in_bytes);
1525     curr_offset += oopmaps->at(i)._oopmap_size_in_bytes;
1526   }
1527   return curr_offset;
1528 }
1529 
1530 char* FileMapInfo::write_bitmap_region(const CHeapBitMap* ptrmap,
1531                                        GrowableArray<ArchiveHeapOopmapInfo>* closed_oopmaps,
1532                                        GrowableArray<ArchiveHeapOopmapInfo>* open_oopmaps,
1533                                        size_t &size_in_bytes) {
1534   size_t size_in_bits = ptrmap->size();
1535   size_in_bytes = ptrmap->size_in_bytes();
1536 
1537   if (closed_oopmaps != NULL && open_oopmaps != NULL) {
1538     size_in_bytes = set_oopmaps_offset(closed_oopmaps, size_in_bytes);
1539     size_in_bytes = set_oopmaps_offset(open_oopmaps, size_in_bytes);
1540   }
1541 
1542   char* buffer = NEW_C_HEAP_ARRAY(char, size_in_bytes, mtClassShared);
1543   ptrmap->write_to((BitMap::bm_word_t*)buffer, ptrmap->size_in_bytes());
1544   header()->set_ptrmap_size_in_bits(size_in_bits);
1545 
1546   if (closed_oopmaps != NULL && open_oopmaps != NULL) {
1547     size_t curr_offset = write_oopmaps(closed_oopmaps, ptrmap->size_in_bytes(), buffer);
1548     write_oopmaps(open_oopmaps, curr_offset, buffer);
1549   }
1550 
1551   write_region(MetaspaceShared::bm, (char*)buffer, size_in_bytes, /*read_only=*/true, /*allow_exec=*/false);
1552   return buffer;
1553 }
1554 
1555 // Write out the given archive heap memory regions.  GC code combines multiple
1556 // consecutive archive GC regions into one MemRegion whenever possible and
1557 // produces the 'regions' array.
1558 //
1559 // If the archive heap memory size is smaller than a single dump time GC region
1560 // size, there is only one MemRegion in the array.
1561 //
1562 // If the archive heap memory size is bigger than one dump time GC region size,
1563 // the 'regions' array may contain more than one consolidated MemRegions. When
1564 // the first/bottom archive GC region is a partial GC region (with the empty
1565 // portion at the higher address within the region), one MemRegion is used for
1566 // the bottom partial archive GC region. The rest of the consecutive archive
1567 // GC regions are combined into another MemRegion.
1568 //
1569 // Here's the mapping from (archive heap GC regions) -> (GrowableArray<MemRegion> *regions).
1570 //   + We have 1 or more archive heap regions: ah0, ah1, ah2 ..... ahn
1571 //   + We have 1 or 2 consolidated heap memory regions: r0 and r1
1572 //
1573 // If there's a single archive GC region (ah0), then r0 == ah0, and r1 is empty.
1574 // Otherwise:
1575 //
1576 // "X" represented space that's occupied by heap objects.
1577 // "_" represented unused spaced in the heap region.
1578 //
1579 //
1580 //    |ah0       | ah1 | ah2| ...... | ahn|
1581 //    |XXXXXX|__ |XXXXX|XXXX|XXXXXXXX|XXXX|
1582 //    |<-r0->|   |<- r1 ----------------->|
1583 //            ^^^
1584 //             |
1585 //             +-- gap
1586 size_t FileMapInfo::write_heap_regions(GrowableArray<MemRegion>* regions,
1587                                        GrowableArray<ArchiveHeapOopmapInfo>* oopmaps,
1588                                        int first_region_id, int max_num_regions) {
1589   assert(max_num_regions <= 2, "Only support maximum 2 memory regions");
1590 
1591   int arr_len = regions == NULL ? 0 : regions->length();
1592   if (arr_len > max_num_regions) {
1593     fail_stop("Unable to write archive heap memory regions: "
1594               "number of memory regions exceeds maximum due to fragmentation. "
1595               "Please increase java heap size "
1596               "(current MaxHeapSize is " SIZE_FORMAT ", InitialHeapSize is " SIZE_FORMAT ").",
1597               MaxHeapSize, InitialHeapSize);
1598   }
1599 
1600   size_t total_size = 0;
1601   for (int i = 0; i < max_num_regions; i++) {
1602     char* start = NULL;
1603     size_t size = 0;
1604     if (i < arr_len) {
1605       start = (char*)regions->at(i).start();
1606       size = regions->at(i).byte_size();
1607       total_size += size;
1608     }
1609 
1610     int region_idx = i + first_region_id;
1611     write_region(region_idx, start, size, false, false);
1612     if (size > 0) {
1613       space_at(region_idx)->init_oopmap(oopmaps->at(i)._offset,
1614                                         oopmaps->at(i)._oopmap_size_in_bits);
1615     }
1616   }
1617   return total_size;
1618 }
1619 
1620 // Dump bytes to file -- at the current file position.
1621 
1622 void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) {
1623   assert(_file_open, "must be");
1624   size_t n = os::write(_fd, buffer, (unsigned int)nbytes);
1625   if (n != nbytes) {
1626     // If the shared archive is corrupted, close it and remove it.
1627     close();
1628     remove(_full_path);
1629     fail_stop("Unable to write to shared archive file.");
1630   }
1631   _file_offset += nbytes;
1632 }
1633 
1634 bool FileMapInfo::is_file_position_aligned() const {
1635   return _file_offset == align_up(_file_offset,
1636                                   MetaspaceShared::core_region_alignment());
1637 }
1638 
1639 // Align file position to an allocation unit boundary.
1640 
1641 void FileMapInfo::align_file_position() {
1642   assert(_file_open, "must be");
1643   size_t new_file_offset = align_up(_file_offset,
1644                                     MetaspaceShared::core_region_alignment());
1645   if (new_file_offset != _file_offset) {
1646     _file_offset = new_file_offset;
1647     // Seek one byte back from the target and write a byte to insure
1648     // that the written file is the correct length.
1649     _file_offset -= 1;
1650     seek_to_position(_file_offset);
1651     char zero = 0;
1652     write_bytes(&zero, 1);
1653   }
1654 }
1655 
1656 
1657 // Dump bytes to file -- at the current file position.
1658 
1659 void FileMapInfo::write_bytes_aligned(const void* buffer, size_t nbytes) {
1660   align_file_position();
1661   write_bytes(buffer, nbytes);
1662   align_file_position();
1663 }
1664 
1665 // Close the shared archive file.  This does NOT unmap mapped regions.
1666 
1667 void FileMapInfo::close() {
1668   if (_file_open) {
1669     if (::close(_fd) < 0) {
1670       fail_stop("Unable to close the shared archive file.");
1671     }
1672     _file_open = false;
1673     _fd = -1;
1674   }
1675 }
1676 
1677 
1678 // JVM/TI RedefineClasses() support:
1679 // Remap the shared readonly space to shared readwrite, private.
1680 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
1681   int idx = MetaspaceShared::ro;
1682   FileMapRegion* si = space_at(idx);
1683   if (!si->read_only()) {
1684     // the space is already readwrite so we are done
1685     return true;
1686   }
1687   size_t size = si->used_aligned();
1688   if (!open_for_read()) {
1689     return false;
1690   }
1691   char *addr = region_addr(idx);
1692   char *base = os::remap_memory(_fd, _full_path, si->file_offset(),
1693                                 addr, size, false /* !read_only */,
1694                                 si->allow_exec());
1695   close();
1696   // These have to be errors because the shared region is now unmapped.
1697   if (base == NULL) {
1698     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1699     vm_exit(1);
1700   }
1701   if (base != addr) {
1702     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1703     vm_exit(1);
1704   }
1705   si->set_read_only(false);
1706   return true;
1707 }
1708 
1709 // Memory map a region in the address space.
1710 static const char* shared_region_name[] = { "ReadWrite", "ReadOnly", "Bitmap",
1711                                             "String1", "String2", "OpenArchive1", "OpenArchive2" };
1712 
1713 MapArchiveResult FileMapInfo::map_regions(int regions[], int num_regions, char* mapped_base_address, ReservedSpace rs) {
1714   DEBUG_ONLY(FileMapRegion* last_region = NULL);
1715   intx addr_delta = mapped_base_address - header()->requested_base_address();
1716 
1717   // Make sure we don't attempt to use header()->mapped_base_address() unless
1718   // it's been successfully mapped.
1719   DEBUG_ONLY(header()->set_mapped_base_address((char*)(uintptr_t)0xdeadbeef);)
1720 
1721   for (int r = 0; r < num_regions; r++) {
1722     int idx = regions[r];
1723     MapArchiveResult result = map_region(idx, addr_delta, mapped_base_address, rs);
1724     if (result != MAP_ARCHIVE_SUCCESS) {
1725       return result;
1726     }
1727     FileMapRegion* si = space_at(idx);
1728     DEBUG_ONLY(if (last_region != NULL) {
1729         // Ensure that the OS won't be able to allocate new memory spaces between any mapped
1730         // regions, or else it would mess up the simple comparision in MetaspaceObj::is_shared().
1731         assert(si->mapped_base() == last_region->mapped_end(), "must have no gaps");
1732       }
1733       last_region = si;)
1734     log_info(cds)("Mapped %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)", is_static() ? "static " : "dynamic",
1735                   idx, p2i(si->mapped_base()), p2i(si->mapped_end()),
1736                   shared_region_name[idx]);
1737 
1738   }
1739 
1740   header()->set_mapped_base_address(header()->requested_base_address() + addr_delta);
1741   if (addr_delta != 0 && !relocate_pointers_in_core_regions(addr_delta)) {
1742     return MAP_ARCHIVE_OTHER_FAILURE;
1743   }
1744 
1745   return MAP_ARCHIVE_SUCCESS;
1746 }
1747 
1748 bool FileMapInfo::read_region(int i, char* base, size_t size, bool do_commit) {
1749   FileMapRegion* si = space_at(i);
1750   if (do_commit) {
1751     log_info(cds)("Commit %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)%s",
1752                   is_static() ? "static " : "dynamic", i, p2i(base), p2i(base + size),
1753                   shared_region_name[i], si->allow_exec() ? " exec" : "");
1754     if (!os::commit_memory(base, size, si->allow_exec())) {
1755       log_error(cds)("Failed to commit %s region #%d (%s)", is_static() ? "static " : "dynamic",
1756                      i, shared_region_name[i]);
1757       return false;
1758     }
1759   }
1760   if (lseek(_fd, (long)si->file_offset(), SEEK_SET) != (int)si->file_offset() ||
1761       read_bytes(base, size) != size) {
1762     return false;
1763   }
1764 
1765   if (VerifySharedSpaces && !region_crc_check(base, si->used(), si->crc())) {
1766     return false;
1767   }
1768 
1769   return true;
1770 }
1771 
1772 MapArchiveResult FileMapInfo::map_region(int i, intx addr_delta, char* mapped_base_address, ReservedSpace rs) {
1773   assert(!HeapShared::is_heap_region(i), "sanity");
1774   FileMapRegion* si = space_at(i);
1775   size_t size = si->used_aligned();
1776   char *requested_addr = mapped_base_address + si->mapping_offset();
1777   assert(si->mapped_base() == NULL, "must be not mapped yet");
1778   assert(requested_addr != NULL, "must be specified");
1779 
1780   si->set_mapped_from_file(false);
1781 
1782   if (MetaspaceShared::use_windows_memory_mapping()) {
1783     // Windows cannot remap read-only shared memory to read-write when required for
1784     // RedefineClasses, which is also used by JFR.  Always map windows regions as RW.
1785     si->set_read_only(false);
1786   } else if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space() ||
1787              Arguments::has_jfr_option()) {
1788     // If a tool agent is in use (debugging enabled), or JFR, we must map the address space RW
1789     si->set_read_only(false);
1790   } else if (addr_delta != 0) {
1791     si->set_read_only(false); // Need to patch the pointers
1792   }
1793 
1794   if (MetaspaceShared::use_windows_memory_mapping() && rs.is_reserved()) {
1795     // This is the second time we try to map the archive(s). We have already created a ReservedSpace
1796     // that covers all the FileMapRegions to ensure all regions can be mapped. However, Windows
1797     // can't mmap into a ReservedSpace, so we just os::read() the data. We're going to patch all the
1798     // regions anyway, so there's no benefit for mmap anyway.
1799     if (!read_region(i, requested_addr, size, /* do_commit = */ true)) {
1800       log_info(cds)("Failed to read %s shared space into reserved space at " INTPTR_FORMAT,
1801                     shared_region_name[i], p2i(requested_addr));
1802       return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
1803     }
1804   } else {
1805     // Note that this may either be a "fresh" mapping into unreserved address
1806     // space (Windows, first mapping attempt), or a mapping into pre-reserved
1807     // space (Posix). See also comment in MetaspaceShared::map_archives().
1808     char* base = os::map_memory(_fd, _full_path, si->file_offset(),
1809                                 requested_addr, size, si->read_only(),
1810                                 si->allow_exec(), mtClassShared);
1811     if (base != requested_addr) {
1812       log_info(cds)("Unable to map %s shared space at " INTPTR_FORMAT,
1813                     shared_region_name[i], p2i(requested_addr));
1814       _memory_mapping_failed = true;
1815       return MAP_ARCHIVE_MMAP_FAILURE;
1816     }
1817     si->set_mapped_from_file(true);
1818   }
1819   si->set_mapped_base(requested_addr);
1820 
1821   if (VerifySharedSpaces && !verify_region_checksum(i)) {
1822     return MAP_ARCHIVE_OTHER_FAILURE;
1823   }
1824 
1825   return MAP_ARCHIVE_SUCCESS;
1826 }
1827 
1828 // The return value is the location of the archive relocation bitmap.
1829 char* FileMapInfo::map_bitmap_region() {
1830   FileMapRegion* si = space_at(MetaspaceShared::bm);
1831   if (si->mapped_base() != NULL) {
1832     return si->mapped_base();
1833   }
1834   bool read_only = true, allow_exec = false;
1835   char* requested_addr = NULL; // allow OS to pick any location
1836   char* bitmap_base = os::map_memory(_fd, _full_path, si->file_offset(),
1837                                      requested_addr, si->used_aligned(), read_only, allow_exec, mtClassShared);
1838   if (bitmap_base == NULL) {
1839     log_info(cds)("failed to map relocation bitmap");
1840     return NULL;
1841   }
1842 
1843   if (VerifySharedSpaces && !region_crc_check(bitmap_base, si->used(), si->crc())) {
1844     log_error(cds)("relocation bitmap CRC error");
1845     if (!os::unmap_memory(bitmap_base, si->used_aligned())) {
1846       fatal("os::unmap_memory of relocation bitmap failed");
1847     }
1848     return NULL;
1849   }
1850 
1851   si->set_mapped_base(bitmap_base);
1852   si->set_mapped_from_file(true);
1853   log_info(cds)("Mapped %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
1854                 is_static() ? "static " : "dynamic",
1855                 MetaspaceShared::bm, p2i(si->mapped_base()), p2i(si->mapped_end()),
1856                 shared_region_name[MetaspaceShared::bm]);
1857   return bitmap_base;
1858 }
1859 
1860 // This is called when we cannot map the archive at the requested[ base address (usually 0x800000000).
1861 // We relocate all pointers in the 2 core regions (ro, rw).
1862 bool FileMapInfo::relocate_pointers_in_core_regions(intx addr_delta) {
1863   log_debug(cds, reloc)("runtime archive relocation start");
1864   char* bitmap_base = map_bitmap_region();
1865 
1866   if (bitmap_base == NULL) {
1867     return false; // OOM, or CRC check failure
1868   } else {
1869     size_t ptrmap_size_in_bits = header()->ptrmap_size_in_bits();
1870     log_debug(cds, reloc)("mapped relocation bitmap @ " INTPTR_FORMAT " (" SIZE_FORMAT " bits)",
1871                           p2i(bitmap_base), ptrmap_size_in_bits);
1872 
1873     BitMapView ptrmap((BitMap::bm_word_t*)bitmap_base, ptrmap_size_in_bits);
1874 
1875     // Patch all pointers in the the mapped region that are marked by ptrmap.
1876     address patch_base = (address)mapped_base();
1877     address patch_end  = (address)mapped_end();
1878 
1879     // the current value of the pointers to be patched must be within this
1880     // range (i.e., must be between the requesed base address, and the of the current archive).
1881     // Note: top archive may point to objects in the base archive, but not the other way around.
1882     address valid_old_base = (address)header()->requested_base_address();
1883     address valid_old_end  = valid_old_base + mapping_end_offset();
1884 
1885     // after patching, the pointers must point inside this range
1886     // (the requested location of the archive, as mapped at runtime).
1887     address valid_new_base = (address)header()->mapped_base_address();
1888     address valid_new_end  = (address)mapped_end();
1889 
1890     SharedDataRelocator patcher((address*)patch_base, (address*)patch_end, valid_old_base, valid_old_end,
1891                                 valid_new_base, valid_new_end, addr_delta);
1892     ptrmap.iterate(&patcher);
1893 
1894     // The MetaspaceShared::bm region will be unmapped in MetaspaceShared::initialize_shared_spaces().
1895 
1896     log_debug(cds, reloc)("runtime archive relocation done");
1897     return true;
1898   }
1899 }
1900 
1901 size_t FileMapInfo::read_bytes(void* buffer, size_t count) {
1902   assert(_file_open, "Archive file is not open");
1903   size_t n = os::read(_fd, buffer, (unsigned int)count);
1904   if (n != count) {
1905     // Close the file if there's a problem reading it.
1906     close();
1907     return 0;
1908   }
1909   _file_offset += count;
1910   return count;
1911 }
1912 
1913 address FileMapInfo::decode_start_address(FileMapRegion* spc, bool with_current_oop_encoding_mode) {
1914   size_t offset = spc->mapping_offset();
1915   narrowOop n = CompressedOops::narrow_oop_cast(offset);
1916   if (with_current_oop_encoding_mode) {
1917     return cast_from_oop<address>(CompressedOops::decode_raw_not_null(n));
1918   } else {
1919     return cast_from_oop<address>(HeapShared::decode_from_archive(n));
1920   }
1921 }
1922 
1923 static MemRegion *closed_heap_regions = NULL;
1924 static MemRegion *open_heap_regions = NULL;
1925 static int num_closed_heap_regions = 0;
1926 static int num_open_heap_regions = 0;
1927 
1928 #if INCLUDE_CDS_JAVA_HEAP
1929 bool FileMapInfo::has_heap_regions() {
1930   return (space_at(MetaspaceShared::first_closed_heap_region)->used() > 0);
1931 }
1932 
1933 // Returns the address range of the archived heap regions computed using the
1934 // current oop encoding mode. This range may be different than the one seen at
1935 // dump time due to encoding mode differences. The result is used in determining
1936 // if/how these regions should be relocated at run time.
1937 MemRegion FileMapInfo::get_heap_regions_range_with_current_oop_encoding_mode() {
1938   address start = (address) max_uintx;
1939   address end   = NULL;
1940 
1941   for (int i = MetaspaceShared::first_closed_heap_region;
1942            i <= MetaspaceShared::last_valid_region;
1943            i++) {
1944     FileMapRegion* si = space_at(i);
1945     size_t size = si->used();
1946     if (size > 0) {
1947       address s = start_address_as_decoded_with_current_oop_encoding_mode(si);
1948       address e = s + size;
1949       if (start > s) {
1950         start = s;
1951       }
1952       if (end < e) {
1953         end = e;
1954       }
1955     }
1956   }
1957   assert(end != NULL, "must have at least one used heap region");
1958   return MemRegion((HeapWord*)start, (HeapWord*)end);
1959 }
1960 
1961 void FileMapInfo::map_or_load_heap_regions() {
1962   bool success = false;
1963 
1964   if (can_use_heap_regions()) {
1965     if (HeapShared::can_map()) {
1966       success = map_heap_regions();
1967     } else if (HeapShared::can_load()) {
1968       success = HeapShared::load_heap_regions(this);
1969     } else {
1970       log_info(cds)("Cannot use CDS heap data. UseEpsilonGC, UseG1GC or UseSerialGC are required.");
1971     }
1972   }
1973 
1974   if (!success) {
1975     MetaspaceShared::disable_full_module_graph();
1976   }
1977 }
1978 
1979 bool FileMapInfo::can_use_heap_regions() {
1980   if (!has_heap_regions()) {
1981     return false;
1982   }
1983   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
1984     ShouldNotReachHere(); // CDS should have been disabled.
1985     // The archived objects are mapped at JVM start-up, but we don't know if
1986     // j.l.String or j.l.Class might be replaced by the ClassFileLoadHook,
1987     // which would make the archived String or mirror objects invalid. Let's be safe and not
1988     // use the archived objects. These 2 classes are loaded during the JVMTI "early" stage.
1989     //
1990     // If JvmtiExport::has_early_class_hook_env() is false, the classes of some objects
1991     // in the archived subgraphs may be replaced by the ClassFileLoadHook. But that's OK
1992     // because we won't install an archived object subgraph if the klass of any of the
1993     // referenced objects are replaced. See HeapShared::initialize_from_archived_subgraph().
1994   }
1995 
1996   log_info(cds)("CDS archive was created with max heap size = " SIZE_FORMAT "M, and the following configuration:",
1997                 max_heap_size()/M);
1998   log_info(cds)("    narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
1999                 p2i(narrow_klass_base()), narrow_klass_shift());
2000   log_info(cds)("    narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
2001                 narrow_oop_mode(), p2i(narrow_oop_base()), narrow_oop_shift());
2002   log_info(cds)("    heap range = [" PTR_FORMAT " - "  PTR_FORMAT "]",
2003                 p2i(header()->heap_begin()), p2i(header()->heap_end()));
2004 
2005   log_info(cds)("The current max heap size = " SIZE_FORMAT "M, HeapRegion::GrainBytes = " SIZE_FORMAT,
2006                 MaxHeapSize/M, HeapRegion::GrainBytes);
2007   log_info(cds)("    narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
2008                 p2i(CompressedKlassPointers::base()), CompressedKlassPointers::shift());
2009   log_info(cds)("    narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
2010                 CompressedOops::mode(), p2i(CompressedOops::base()), CompressedOops::shift());
2011   log_info(cds)("    heap range = [" PTR_FORMAT " - "  PTR_FORMAT "]",
2012                 p2i(CompressedOops::begin()), p2i(CompressedOops::end()));
2013 
2014   if (narrow_klass_base() != CompressedKlassPointers::base() ||
2015       narrow_klass_shift() != CompressedKlassPointers::shift()) {
2016     log_info(cds)("CDS heap data cannot be used because the archive was created with an incompatible narrow klass encoding mode.");
2017     return false;
2018   }
2019   return true;
2020 }
2021 
2022 
2023 //
2024 // Map the closed and open archive heap objects to the runtime java heap.
2025 //
2026 // The shared objects are mapped at (or close to ) the java heap top in
2027 // closed archive regions. The mapped objects contain no out-going
2028 // references to any other java heap regions. GC does not write into the
2029 // mapped closed archive heap region.
2030 //
2031 // The open archive heap objects are mapped below the shared objects in
2032 // the runtime java heap. The mapped open archive heap data only contains
2033 // references to the shared objects and open archive objects initially.
2034 // During runtime execution, out-going references to any other java heap
2035 // regions may be added. GC may mark and update references in the mapped
2036 // open archive objects.
2037 void FileMapInfo::map_heap_regions_impl() {
2038   if (narrow_oop_mode() != CompressedOops::mode() ||
2039       narrow_oop_base() != CompressedOops::base() ||
2040       narrow_oop_shift() != CompressedOops::shift()) {
2041     log_info(cds)("CDS heap data needs to be relocated because the archive was created with an incompatible oop encoding mode.");
2042     _heap_pointers_need_patching = true;
2043   } else {
2044     MemRegion range = get_heap_regions_range_with_current_oop_encoding_mode();
2045     if (!CompressedOops::is_in(range)) {
2046       log_info(cds)("CDS heap data needs to be relocated because");
2047       log_info(cds)("the desired range " PTR_FORMAT " - "  PTR_FORMAT, p2i(range.start()), p2i(range.end()));
2048       log_info(cds)("is outside of the heap " PTR_FORMAT " - "  PTR_FORMAT, p2i(CompressedOops::begin()), p2i(CompressedOops::end()));
2049       _heap_pointers_need_patching = true;
2050     } else if (header()->heap_end() != CompressedOops::end()) {
2051       log_info(cds)("CDS heap data needs to be relocated to the end of the runtime heap to reduce fragmentation");
2052       _heap_pointers_need_patching = true;
2053     }
2054   }
2055 
2056   ptrdiff_t delta = 0;
2057   if (_heap_pointers_need_patching) {
2058     //   dumptime heap end  ------------v
2059     //   [      |archived heap regions| ]         runtime heap end ------v
2060     //                                       [   |archived heap regions| ]
2061     //                                  |<-----delta-------------------->|
2062     //
2063     // At dump time, the archived heap regions were near the top of the heap.
2064     // At run time, they may not be inside the heap, so we move them so
2065     // that they are now near the top of the runtime time. This can be done by
2066     // the simple math of adding the delta as shown above.
2067     address dumptime_heap_end = header()->heap_end();
2068     address runtime_heap_end = CompressedOops::end();
2069     delta = runtime_heap_end - dumptime_heap_end;
2070   }
2071 
2072   log_info(cds)("CDS heap data relocation delta = " INTX_FORMAT " bytes", delta);
2073   HeapShared::init_narrow_oop_decoding(narrow_oop_base() + delta, narrow_oop_shift());
2074 
2075   FileMapRegion* si = space_at(MetaspaceShared::first_closed_heap_region);
2076   address relocated_closed_heap_region_bottom = start_address_as_decoded_from_archive(si);
2077   if (!is_aligned(relocated_closed_heap_region_bottom, HeapRegion::GrainBytes)) {
2078     // Align the bottom of the closed archive heap regions at G1 region boundary.
2079     // This will avoid the situation where the highest open region and the lowest
2080     // closed region sharing the same G1 region. Otherwise we will fail to map the
2081     // open regions.
2082     size_t align = size_t(relocated_closed_heap_region_bottom) % HeapRegion::GrainBytes;
2083     delta -= align;
2084     log_info(cds)("CDS heap data needs to be relocated lower by a further " SIZE_FORMAT
2085                   " bytes to " INTX_FORMAT " to be aligned with HeapRegion::GrainBytes",
2086                   align, delta);
2087     HeapShared::init_narrow_oop_decoding(narrow_oop_base() + delta, narrow_oop_shift());
2088     _heap_pointers_need_patching = true;
2089     relocated_closed_heap_region_bottom = start_address_as_decoded_from_archive(si);
2090   }
2091   assert(is_aligned(relocated_closed_heap_region_bottom, HeapRegion::GrainBytes),
2092          "must be");
2093 
2094   // Map the closed heap regions: GC does not write into these regions.
2095   if (map_heap_regions(MetaspaceShared::first_closed_heap_region,
2096                        MetaspaceShared::max_num_closed_heap_regions,
2097                        /*is_open_archive=*/ false,
2098                        &closed_heap_regions, &num_closed_heap_regions)) {
2099     HeapShared::set_closed_regions_mapped();
2100 
2101     // Now, map the open heap regions: GC can write into these regions.
2102     if (map_heap_regions(MetaspaceShared::first_open_heap_region,
2103                          MetaspaceShared::max_num_open_heap_regions,
2104                          /*is_open_archive=*/ true,
2105                          &open_heap_regions, &num_open_heap_regions)) {
2106       HeapShared::set_open_regions_mapped();
2107       HeapShared::set_roots(header()->heap_obj_roots());
2108     }
2109   }
2110 }
2111 
2112 bool FileMapInfo::map_heap_regions() {
2113   map_heap_regions_impl();
2114 
2115   if (!HeapShared::closed_regions_mapped()) {
2116     assert(closed_heap_regions == NULL &&
2117            num_closed_heap_regions == 0, "sanity");
2118   }
2119 
2120   if (!HeapShared::open_regions_mapped()) {
2121     assert(open_heap_regions == NULL && num_open_heap_regions == 0, "sanity");
2122     return false;
2123   } else {
2124     return true;
2125   }
2126 }
2127 
2128 bool FileMapInfo::map_heap_regions(int first, int max,  bool is_open_archive,
2129                                    MemRegion** regions_ret, int* num_regions_ret) {
2130   MemRegion* regions = MemRegion::create_array(max, mtInternal);
2131 
2132   struct Cleanup {
2133     MemRegion* _regions;
2134     uint _length;
2135     bool _aborted;
2136     Cleanup(MemRegion* regions, uint length) : _regions(regions), _length(length), _aborted(true) { }
2137     ~Cleanup() { if (_aborted) { MemRegion::destroy_array(_regions, _length); } }
2138   } cleanup(regions, max);
2139 
2140   FileMapRegion* si;
2141   int num_regions = 0;
2142 
2143   for (int i = first;
2144            i < first + max; i++) {
2145     si = space_at(i);
2146     size_t size = si->used();
2147     if (size > 0) {
2148       HeapWord* start = (HeapWord*)start_address_as_decoded_from_archive(si);
2149       regions[num_regions] = MemRegion(start, size / HeapWordSize);
2150       num_regions ++;
2151       log_info(cds)("Trying to map heap data: region[%d] at " INTPTR_FORMAT ", size = " SIZE_FORMAT_W(8) " bytes",
2152                     i, p2i(start), size);
2153     }
2154   }
2155 
2156   if (num_regions == 0) {
2157     return false; // no archived java heap data
2158   }
2159 
2160   // Check that regions are within the java heap
2161   if (!G1CollectedHeap::heap()->check_archive_addresses(regions, num_regions)) {
2162     log_info(cds)("UseSharedSpaces: Unable to allocate region, range is not within java heap.");
2163     return false;
2164   }
2165 
2166   // allocate from java heap
2167   if (!G1CollectedHeap::heap()->alloc_archive_regions(
2168              regions, num_regions, is_open_archive)) {
2169     log_info(cds)("UseSharedSpaces: Unable to allocate region, java heap range is already in use.");
2170     return false;
2171   }
2172 
2173   // Map the archived heap data. No need to call MemTracker::record_virtual_memory_type()
2174   // for mapped regions as they are part of the reserved java heap, which is
2175   // already recorded.
2176   for (int i = 0; i < num_regions; i++) {
2177     si = space_at(first + i);
2178     char* addr = (char*)regions[i].start();
2179     char* base = os::map_memory(_fd, _full_path, si->file_offset(),
2180                                 addr, regions[i].byte_size(), si->read_only(),
2181                                 si->allow_exec());
2182     if (base == NULL || base != addr) {
2183       // dealloc the regions from java heap
2184       dealloc_heap_regions(regions, num_regions);
2185       log_info(cds)("UseSharedSpaces: Unable to map at required address in java heap. "
2186                     INTPTR_FORMAT ", size = " SIZE_FORMAT " bytes",
2187                     p2i(addr), regions[i].byte_size());
2188       return false;
2189     }
2190 
2191     if (VerifySharedSpaces && !region_crc_check(addr, regions[i].byte_size(), si->crc())) {
2192       // dealloc the regions from java heap
2193       dealloc_heap_regions(regions, num_regions);
2194       log_info(cds)("UseSharedSpaces: mapped heap regions are corrupt");
2195       return false;
2196     }
2197   }
2198 
2199   cleanup._aborted = false;
2200   // the shared heap data is mapped successfully
2201   *regions_ret = regions;
2202   *num_regions_ret = num_regions;
2203   return true;
2204 }
2205 
2206 void FileMapInfo::patch_heap_embedded_pointers() {
2207   if (!_heap_pointers_need_patching) {
2208     return;
2209   }
2210 
2211   log_info(cds)("patching heap embedded pointers");
2212   patch_heap_embedded_pointers(closed_heap_regions,
2213                                num_closed_heap_regions,
2214                                MetaspaceShared::first_closed_heap_region);
2215 
2216   patch_heap_embedded_pointers(open_heap_regions,
2217                                num_open_heap_regions,
2218                                MetaspaceShared::first_open_heap_region);
2219 }
2220 
2221 void FileMapInfo::patch_heap_embedded_pointers(MemRegion* regions, int num_regions,
2222                                                int first_region_idx) {
2223   char* bitmap_base = map_bitmap_region();
2224   if (bitmap_base == NULL) {
2225     return;
2226   }
2227   for (int i=0; i<num_regions; i++) {
2228     FileMapRegion* si = space_at(i + first_region_idx);
2229     HeapShared::patch_embedded_pointers(
2230       regions[i],
2231       (address)(space_at(MetaspaceShared::bm)->mapped_base()) + si->oopmap_offset(),
2232       si->oopmap_size_in_bits());
2233   }
2234 }
2235 
2236 // This internally allocates objects using vmClasses::Object_klass(), so it
2237 // must be called after the Object_klass is loaded
2238 void FileMapInfo::fixup_mapped_heap_regions() {
2239   assert(vmClasses::Object_klass_loaded(), "must be");
2240   // If any closed regions were found, call the fill routine to make them parseable.
2241   // Note that closed_heap_regions may be non-NULL even if no regions were found.
2242   if (num_closed_heap_regions != 0) {
2243     assert(closed_heap_regions != NULL,
2244            "Null closed_heap_regions array with non-zero count");
2245     G1CollectedHeap::heap()->fill_archive_regions(closed_heap_regions,
2246                                                   num_closed_heap_regions);
2247   }
2248 
2249   // do the same for mapped open archive heap regions
2250   if (num_open_heap_regions != 0) {
2251     assert(open_heap_regions != NULL, "NULL open_heap_regions array with non-zero count");
2252     G1CollectedHeap::heap()->fill_archive_regions(open_heap_regions,
2253                                                   num_open_heap_regions);
2254 
2255     // Populate the open archive regions' G1BlockOffsetTableParts. That ensures
2256     // fast G1BlockOffsetTablePart::block_start operations for any given address
2257     // within the open archive regions when trying to find start of an object
2258     // (e.g. during card table scanning).
2259     //
2260     // This is only needed for open archive regions but not the closed archive
2261     // regions, because objects in closed archive regions never reference objects
2262     // outside the closed archive regions and they are immutable. So we never
2263     // need their BOT during garbage collection.
2264     G1CollectedHeap::heap()->populate_archive_regions_bot_part(open_heap_regions,
2265                                                                num_open_heap_regions);
2266   }
2267 }
2268 
2269 // dealloc the archive regions from java heap
2270 void FileMapInfo::dealloc_heap_regions(MemRegion* regions, int num) {
2271   if (num > 0) {
2272     assert(regions != NULL, "Null archive regions array with non-zero count");
2273     G1CollectedHeap::heap()->dealloc_archive_regions(regions, num);
2274   }
2275 }
2276 #endif // INCLUDE_CDS_JAVA_HEAP
2277 
2278 bool FileMapInfo::region_crc_check(char* buf, size_t size, int expected_crc) {
2279   int crc = ClassLoader::crc32(0, buf, (jint)size);
2280   if (crc != expected_crc) {
2281     fail_continue("Checksum verification failed.");
2282     return false;
2283   }
2284   return true;
2285 }
2286 
2287 bool FileMapInfo::verify_region_checksum(int i) {
2288   assert(VerifySharedSpaces, "sanity");
2289   size_t sz = space_at(i)->used();
2290 
2291   if (sz == 0) {
2292     return true; // no data
2293   } else {
2294     return region_crc_check(region_addr(i), sz, space_at(i)->crc());
2295   }
2296 }
2297 
2298 void FileMapInfo::unmap_regions(int regions[], int num_regions) {
2299   for (int r = 0; r < num_regions; r++) {
2300     int idx = regions[r];
2301     unmap_region(idx);
2302   }
2303 }
2304 
2305 // Unmap a memory region in the address space.
2306 
2307 void FileMapInfo::unmap_region(int i) {
2308   assert(!HeapShared::is_heap_region(i), "sanity");
2309   FileMapRegion* si = space_at(i);
2310   char* mapped_base = si->mapped_base();
2311   size_t size = si->used_aligned();
2312 
2313   if (mapped_base != NULL) {
2314     if (size > 0 && si->mapped_from_file()) {
2315       log_info(cds)("Unmapping region #%d at base " INTPTR_FORMAT " (%s)", i, p2i(mapped_base),
2316                     shared_region_name[i]);
2317       if (!os::unmap_memory(mapped_base, size)) {
2318         fatal("os::unmap_memory failed");
2319       }
2320     }
2321     si->set_mapped_base(NULL);
2322   }
2323 }
2324 
2325 void FileMapInfo::assert_mark(bool check) {
2326   if (!check) {
2327     fail_stop("Mark mismatch while restoring from shared file.");
2328   }
2329 }
2330 
2331 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it, bool use_copy) {
2332   if (use_copy) {
2333     _saved_shared_path_table.metaspace_pointers_do(it);
2334   } else {
2335     _shared_path_table.metaspace_pointers_do(it);
2336   }
2337 }
2338 
2339 FileMapInfo* FileMapInfo::_current_info = NULL;
2340 FileMapInfo* FileMapInfo::_dynamic_archive_info = NULL;
2341 bool FileMapInfo::_heap_pointers_need_patching = false;
2342 SharedPathTable FileMapInfo::_shared_path_table;
2343 SharedPathTable FileMapInfo::_saved_shared_path_table;
2344 Array<u8>*      FileMapInfo::_saved_shared_path_table_array = NULL;
2345 bool FileMapInfo::_validating_shared_path_table = false;
2346 bool FileMapInfo::_memory_mapping_failed = false;
2347 GrowableArray<const char*>* FileMapInfo::_non_existent_class_paths = NULL;
2348 
2349 // Open the shared archive file, read and validate the header
2350 // information (version, boot classpath, etc.).  If initialization
2351 // fails, shared spaces are disabled and the file is closed. [See
2352 // fail_continue.]
2353 //
2354 // Validation of the archive is done in two steps:
2355 //
2356 // [1] validate_header() - done here.
2357 // [2] validate_shared_path_table - this is done later, because the table is in the RW
2358 //     region of the archive, which is not mapped yet.
2359 bool FileMapInfo::initialize() {
2360   assert(UseSharedSpaces, "UseSharedSpaces expected.");
2361 
2362   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
2363     // CDS assumes that no classes resolved in vmClasses::resolve_all()
2364     // are replaced at runtime by JVMTI ClassFileLoadHook. All of those classes are resolved
2365     // during the JVMTI "early" stage, so we can still use CDS if
2366     // JvmtiExport::has_early_class_hook_env() is false.
2367     FileMapInfo::fail_continue("CDS is disabled because early JVMTI ClassFileLoadHook is in use.");
2368     return false;
2369   }
2370 
2371   if (!Arguments::has_jimage()) {
2372     FileMapInfo::fail_continue("The shared archive file cannot be used with an exploded module build.");
2373     return false;
2374   }
2375 
2376   if (!open_for_read()) {
2377     return false;
2378   }
2379   if (!init_from_file(_fd)) {
2380     return false;
2381   }
2382   if (!validate_header()) {
2383     return false;
2384   }
2385   return true;
2386 }
2387 
2388 char* FileMapInfo::region_addr(int idx) {
2389   FileMapRegion* si = space_at(idx);
2390   if (HeapShared::is_heap_region(idx)) {
2391     assert(DumpSharedSpaces, "The following doesn't work at runtime");
2392     return si->used() > 0 ?
2393           (char*)start_address_as_decoded_with_current_oop_encoding_mode(si) : NULL;
2394   } else {
2395     return si->mapped_base();
2396   }
2397 }
2398 
2399 // The 2 core spaces are RW->RO
2400 FileMapRegion* FileMapInfo::first_core_space() const {
2401   return space_at(MetaspaceShared::rw);
2402 }
2403 
2404 FileMapRegion* FileMapInfo::last_core_space() const {
2405   return space_at(MetaspaceShared::ro);
2406 }
2407 
2408 void FileMapHeader::set_as_offset(char* p, size_t *offset) {
2409   *offset = ArchiveBuilder::current()->any_to_offset((address)p);
2410 }
2411 
2412 int FileMapHeader::compute_crc() {
2413   char* start = (char*)this;
2414   // start computing from the field after _crc to end of base archive name.
2415   char* buf = (char*)&(_generic_header._crc) + sizeof(_generic_header._crc);
2416   size_t sz = header_size() - (buf - start);
2417   int crc = ClassLoader::crc32(0, buf, (jint)sz);
2418   return crc;
2419 }
2420 
2421 // This function should only be called during run time with UseSharedSpaces enabled.
2422 bool FileMapHeader::validate() {
2423   if (_obj_alignment != ObjectAlignmentInBytes) {
2424     FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d"
2425                   " does not equal the current ObjectAlignmentInBytes of " INTX_FORMAT ".",
2426                   _obj_alignment, ObjectAlignmentInBytes);
2427     return false;
2428   }
2429   if (_compact_strings != CompactStrings) {
2430     FileMapInfo::fail_continue("The shared archive file's CompactStrings setting (%s)"
2431                   " does not equal the current CompactStrings setting (%s).",
2432                   _compact_strings ? "enabled" : "disabled",
2433                   CompactStrings   ? "enabled" : "disabled");
2434     return false;
2435   }
2436 
2437   // This must be done after header validation because it might change the
2438   // header data
2439   const char* prop = Arguments::get_property("java.system.class.loader");
2440   if (prop != NULL) {
2441     warning("Archived non-system classes are disabled because the "
2442             "java.system.class.loader property is specified (value = \"%s\"). "
2443             "To use archived non-system classes, this property must not be set", prop);
2444     _has_platform_or_app_classes = false;
2445   }
2446 
2447 
2448   if (!_verify_local && BytecodeVerificationLocal) {
2449     //  we cannot load boot classes, so there's no point of using the CDS archive
2450     FileMapInfo::fail_continue("The shared archive file's BytecodeVerificationLocal setting (%s)"
2451                                " does not equal the current BytecodeVerificationLocal setting (%s).",
2452                                _verify_local ? "enabled" : "disabled",
2453                                BytecodeVerificationLocal ? "enabled" : "disabled");
2454     return false;
2455   }
2456 
2457   // For backwards compatibility, we don't check the BytecodeVerificationRemote setting
2458   // if the archive only contains system classes.
2459   if (_has_platform_or_app_classes
2460       && !_verify_remote // we didn't verify the archived platform/app classes
2461       && BytecodeVerificationRemote) { // but we want to verify all loaded platform/app classes
2462     FileMapInfo::fail_continue("The shared archive file was created with less restrictive "
2463                                "verification setting than the current setting.");
2464     // Pretend that we didn't have any archived platform/app classes, so they won't be loaded
2465     // by SystemDictionaryShared.
2466     _has_platform_or_app_classes = false;
2467   }
2468 
2469   // Java agents are allowed during run time. Therefore, the following condition is not
2470   // checked: (!_allow_archiving_with_java_agent && AllowArchivingWithJavaAgent)
2471   // Note: _allow_archiving_with_java_agent is set in the shared archive during dump time
2472   // while AllowArchivingWithJavaAgent is set during the current run.
2473   if (_allow_archiving_with_java_agent && !AllowArchivingWithJavaAgent) {
2474     FileMapInfo::fail_continue("The setting of the AllowArchivingWithJavaAgent is different "
2475                                "from the setting in the shared archive.");
2476     return false;
2477   }
2478 
2479   if (_allow_archiving_with_java_agent) {
2480     warning("This archive was created with AllowArchivingWithJavaAgent. It should be used "
2481             "for testing purposes only and should not be used in a production environment");
2482   }
2483 
2484   log_info(cds)("Archive was created with UseCompressedOops = %d, UseCompressedClassPointers = %d",
2485                           compressed_oops(), compressed_class_pointers());
2486   if (compressed_oops() != UseCompressedOops || compressed_class_pointers() != UseCompressedClassPointers) {
2487     FileMapInfo::fail_continue("Unable to use shared archive.\nThe saved state of UseCompressedOops and UseCompressedClassPointers is "
2488                                "different from runtime, CDS will be disabled.");
2489     return false;
2490   }
2491 
2492   if (!_use_optimized_module_handling) {
2493     MetaspaceShared::disable_optimized_module_handling();
2494     log_info(cds)("optimized module handling: disabled because archive was created without optimized module handling");
2495   }
2496 
2497   if (!_use_full_module_graph) {
2498     MetaspaceShared::disable_full_module_graph();
2499     log_info(cds)("full module graph: disabled because archive was created without full module graph");
2500   }
2501 
2502   return true;
2503 }
2504 
2505 bool FileMapInfo::validate_header() {
2506   if (!header()->validate()) {
2507     return false;
2508   }
2509   if (_is_static) {
2510     return true;
2511   } else {
2512     return DynamicArchive::validate(this);
2513   }
2514 }
2515 
2516 // Check if a given address is within one of the shared regions
2517 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
2518   assert(idx == MetaspaceShared::ro ||
2519          idx == MetaspaceShared::rw, "invalid region index");
2520   char* base = region_addr(idx);
2521   if (p >= base && p < base + space_at(idx)->used()) {
2522     return true;
2523   }
2524   return false;
2525 }
2526 
2527 // Unmap mapped regions of shared space.
2528 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
2529   MetaspaceShared::set_shared_metaspace_range(NULL, NULL, NULL);
2530 
2531   FileMapInfo *map_info = FileMapInfo::current_info();
2532   if (map_info) {
2533     map_info->fail_continue("%s", msg);
2534     for (int i = 0; i < MetaspaceShared::num_non_heap_regions; i++) {
2535       if (!HeapShared::is_heap_region(i)) {
2536         map_info->unmap_region(i);
2537       }
2538     }
2539     // Dealloc the archive heap regions only without unmapping. The regions are part
2540     // of the java heap. Unmapping of the heap regions are managed by GC.
2541     map_info->dealloc_heap_regions(open_heap_regions,
2542                                    num_open_heap_regions);
2543     map_info->dealloc_heap_regions(closed_heap_regions,
2544                                    num_closed_heap_regions);
2545   } else if (DumpSharedSpaces) {
2546     fail_stop("%s", msg);
2547   }
2548 }
2549 
2550 #if INCLUDE_JVMTI
2551 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = NULL;
2552 
2553 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
2554   ClassPathEntry* ent = _classpath_entries_for_jvmti[i];
2555   if (ent == NULL) {
2556     if (i == 0) {
2557       ent = ClassLoader::get_jrt_entry();
2558       assert(ent != NULL, "must be");
2559     } else {
2560       SharedClassPathEntry* scpe = shared_path(i);
2561       assert(scpe->is_jar(), "must be"); // other types of scpe will not produce archived classes
2562 
2563       const char* path = scpe->name();
2564       struct stat st;
2565       if (os::stat(path, &st) != 0) {
2566         char *msg = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, strlen(path) + 128);
2567         jio_snprintf(msg, strlen(path) + 127, "error in finding JAR file %s", path);
2568         THROW_MSG_(vmSymbols::java_io_IOException(), msg, NULL);
2569       } else {
2570         ent = ClassLoader::create_class_path_entry(THREAD, path, &st, false, false);
2571         if (ent == NULL) {
2572           char *msg = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, strlen(path) + 128);
2573           jio_snprintf(msg, strlen(path) + 127, "error in opening JAR file %s", path);
2574           THROW_MSG_(vmSymbols::java_io_IOException(), msg, NULL);
2575         }
2576       }
2577     }
2578 
2579     MutexLocker mu(THREAD, CDSClassFileStream_lock);
2580     if (_classpath_entries_for_jvmti[i] == NULL) {
2581       _classpath_entries_for_jvmti[i] = ent;
2582     } else {
2583       // Another thread has beat me to creating this entry
2584       delete ent;
2585       ent = _classpath_entries_for_jvmti[i];
2586     }
2587   }
2588 
2589   return ent;
2590 }
2591 
2592 ClassFileStream* FileMapInfo::open_stream_for_jvmti(InstanceKlass* ik, Handle class_loader, TRAPS) {
2593   int path_index = ik->shared_classpath_index();
2594   assert(path_index >= 0, "should be called for shared built-in classes only");
2595   assert(path_index < (int)get_number_of_shared_paths(), "sanity");
2596 
2597   ClassPathEntry* cpe = get_classpath_entry_for_jvmti(path_index, CHECK_NULL);
2598   assert(cpe != NULL, "must be");
2599 
2600   Symbol* name = ik->name();
2601   const char* const class_name = name->as_C_string();
2602   const char* const file_name = ClassLoader::file_name_for_class_name(class_name,
2603                                                                       name->utf8_length());
2604   ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader());
2605   ClassFileStream* cfs = cpe->open_stream_for_loader(THREAD, file_name, loader_data);
2606   assert(cfs != NULL, "must be able to read the classfile data of shared classes for built-in loaders.");
2607   log_debug(cds, jvmti)("classfile data for %s [%d: %s] = %d bytes", class_name, path_index,
2608                         cfs->source(), cfs->length());
2609   return cfs;
2610 }
2611 
2612 #endif