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