src/hotspot/share/memory/filemap.cpp
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 54927 1512d88b24c6
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
    53 #include "runtime/mutexLocker.hpp"
    53 #include "runtime/mutexLocker.hpp"
    54 #include "runtime/os.inline.hpp"
    54 #include "runtime/os.inline.hpp"
    55 #include "runtime/vm_version.hpp"
    55 #include "runtime/vm_version.hpp"
    56 #include "services/memTracker.hpp"
    56 #include "services/memTracker.hpp"
    57 #include "utilities/align.hpp"
    57 #include "utilities/align.hpp"
       
    58 #include "utilities/classpathStream.hpp"
    58 #include "utilities/defaultStream.hpp"
    59 #include "utilities/defaultStream.hpp"
    59 #if INCLUDE_G1GC
    60 #if INCLUDE_G1GC
    60 #include "gc/g1/g1CollectedHeap.hpp"
    61 #include "gc/g1/g1CollectedHeap.hpp"
    61 #include "gc/g1/heapRegion.hpp"
    62 #include "gc/g1/heapRegion.hpp"
    62 #endif
    63 #endif
    73 
    74 
    74 // Complain and stop. All error conditions occurring during the writing of
    75 // Complain and stop. All error conditions occurring during the writing of
    75 // an archive file should stop the process.  Unrecoverable errors during
    76 // an archive file should stop the process.  Unrecoverable errors during
    76 // the reading of the archive file should stop the process.
    77 // the reading of the archive file should stop the process.
    77 
    78 
    78 static void fail(const char *msg, va_list ap) {
    79 static void fail_exit(const char *msg, va_list ap) {
    79   // This occurs very early during initialization: tty is not initialized.
    80   // This occurs very early during initialization: tty is not initialized.
    80   jio_fprintf(defaultStream::error_stream(),
    81   jio_fprintf(defaultStream::error_stream(),
    81               "An error has occurred while processing the"
    82               "An error has occurred while processing the"
    82               " shared archive file.\n");
    83               " shared archive file.\n");
    83   jio_vfprintf(defaultStream::error_stream(), msg, ap);
    84   jio_vfprintf(defaultStream::error_stream(), msg, ap);
    88 
    89 
    89 
    90 
    90 void FileMapInfo::fail_stop(const char *msg, ...) {
    91 void FileMapInfo::fail_stop(const char *msg, ...) {
    91         va_list ap;
    92         va_list ap;
    92   va_start(ap, msg);
    93   va_start(ap, msg);
    93   fail(msg, ap);        // Never returns.
    94   fail_exit(msg, ap);   // Never returns.
    94   va_end(ap);           // for completeness.
    95   va_end(ap);           // for completeness.
    95 }
    96 }
    96 
    97 
    97 
    98 
    98 // Complain and continue.  Recoverable errors during the reading of the
    99 // Complain and continue.  Recoverable errors during the reading of the
   116     tty->print("[");
   117     tty->print("[");
   117     tty->vprint(msg, ap);
   118     tty->vprint(msg, ap);
   118     tty->print_cr("]");
   119     tty->print_cr("]");
   119   } else {
   120   } else {
   120     if (RequireSharedSpaces) {
   121     if (RequireSharedSpaces) {
   121       fail(msg, ap);
   122       fail_exit(msg, ap);
   122     } else {
   123     } else {
   123       if (log_is_enabled(Info, cds)) {
   124       if (log_is_enabled(Info, cds)) {
   124         ResourceMark rm;
   125         ResourceMark rm;
   125         LogStream ls(Log(cds)::info());
   126         LogStream ls(Log(cds)::info());
   126         ls.print("UseSharedSpaces: ");
   127         ls.print("UseSharedSpaces: ");
   153   assert(N == JVM_IDENT_MAX, "Bad header_version size");
   154   assert(N == JVM_IDENT_MAX, "Bad header_version size");
   154 
   155 
   155   const char *vm_version = VM_Version::internal_vm_info_string();
   156   const char *vm_version = VM_Version::internal_vm_info_string();
   156   const int version_len = (int)strlen(vm_version);
   157   const int version_len = (int)strlen(vm_version);
   157 
   158 
       
   159   memset(header_version, 0, JVM_IDENT_MAX);
       
   160 
   158   if (version_len < (JVM_IDENT_MAX-1)) {
   161   if (version_len < (JVM_IDENT_MAX-1)) {
   159     strcpy(header_version, vm_version);
   162     strcpy(header_version, vm_version);
   160 
   163 
   161   } else {
   164   } else {
   162     // Get the hash value.  Use a static seed because the hash needs to return the same
   165     // Get the hash value.  Use a static seed because the hash needs to return the same
   168 
   171 
   169     // Append the hash code as eight hex digits.
   172     // Append the hash code as eight hex digits.
   170     sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash);
   173     sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash);
   171     header_version[JVM_IDENT_MAX-1] = 0;  // Null terminate.
   174     header_version[JVM_IDENT_MAX-1] = 0;  // Null terminate.
   172   }
   175   }
       
   176 
       
   177   assert(header_version[JVM_IDENT_MAX-1] == 0, "must be");
   173 }
   178 }
   174 
   179 
   175 FileMapInfo::FileMapInfo(bool is_static) {
   180 FileMapInfo::FileMapInfo(bool is_static) {
   176   memset((void*)this, 0, sizeof(FileMapInfo));
   181   memset((void*)this, 0, sizeof(FileMapInfo));
   177   _is_static = is_static;
   182   _is_static = is_static;
   185     _dynamic_archive_info = this;
   190     _dynamic_archive_info = this;
   186     header_size = sizeof(DynamicArchiveHeader);
   191     header_size = sizeof(DynamicArchiveHeader);
   187   }
   192   }
   188   _header = (FileMapHeader*)os::malloc(header_size, mtInternal);
   193   _header = (FileMapHeader*)os::malloc(header_size, mtInternal);
   189   memset((void*)_header, 0, header_size);
   194   memset((void*)_header, 0, header_size);
   190   _header->_header_size = header_size;
   195   _header->set_header_size(header_size);
   191   _header->_version = INVALID_CDS_ARCHIVE_VERSION;
   196   _header->set_version(INVALID_CDS_ARCHIVE_VERSION);
   192   _header->_has_platform_or_app_classes = true;
   197   _header->set_has_platform_or_app_classes(true);
   193   _file_offset = 0;
   198   _file_offset = 0;
   194   _file_open = false;
   199   _file_open = false;
   195 }
   200 }
   196 
   201 
   197 FileMapInfo::~FileMapInfo() {
   202 FileMapInfo::~FileMapInfo() {
   203     _dynamic_archive_info = NULL;
   208     _dynamic_archive_info = NULL;
   204   }
   209   }
   205 }
   210 }
   206 
   211 
   207 void FileMapInfo::populate_header(size_t alignment) {
   212 void FileMapInfo::populate_header(size_t alignment) {
   208   _header->populate(this, alignment);
   213   header()->populate(this, alignment);
   209 }
   214 }
   210 
   215 
   211 void FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
   216 void FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
   212   if (DynamicDumpSharedSpaces) {
   217   if (DynamicDumpSharedSpaces) {
   213     _magic = CDS_DYNAMIC_ARCHIVE_MAGIC;
   218     _magic = CDS_DYNAMIC_ARCHIVE_MAGIC;
   224   _max_heap_size = MaxHeapSize;
   229   _max_heap_size = MaxHeapSize;
   225   _narrow_klass_base = CompressedKlassPointers::base();
   230   _narrow_klass_base = CompressedKlassPointers::base();
   226   _narrow_klass_shift = CompressedKlassPointers::shift();
   231   _narrow_klass_shift = CompressedKlassPointers::shift();
   227   _shared_path_table = mapinfo->_shared_path_table;
   232   _shared_path_table = mapinfo->_shared_path_table;
   228   if (HeapShared::is_heap_object_archiving_allowed()) {
   233   if (HeapShared::is_heap_object_archiving_allowed()) {
   229     _heap_reserved = Universe::heap()->reserved_region();
   234     _heap_end = CompressedOops::end();
   230   }
   235   }
   231 
   236 
   232   // The following fields are for sanity checks for whether this archive
   237   // The following fields are for sanity checks for whether this archive
   233   // will function correctly with this JVM and the bootclasspath it's
   238   // will function correctly with this JVM and the bootclasspath it's
   234   // invoked with.
   239   // invoked with.
   235 
   240 
   236   // JVM version string ... changes on each build.
   241   // JVM version string ... changes on each build.
   237   get_header_version(_jvm_ident);
   242   get_header_version(_jvm_ident);
   238 
   243 
   239   ClassLoaderExt::finalize_shared_paths_misc_info();
       
   240   _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
   244   _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
   241   _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
   245   _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
   242   _num_module_paths = ClassLoader::num_module_path_entries();
   246   _num_module_paths = ClassLoader::num_module_path_entries();
   243   _max_used_path_index = ClassLoaderExt::max_used_path_index();
   247   _max_used_path_index = ClassLoaderExt::max_used_path_index();
   244 
   248 
   250   // the following 2 fields will be set in write_header for dynamic archive header
   254   // the following 2 fields will be set in write_header for dynamic archive header
   251   _base_archive_name_size = 0;
   255   _base_archive_name_size = 0;
   252   _base_archive_is_default = false;
   256   _base_archive_is_default = false;
   253 }
   257 }
   254 
   258 
   255 void SharedClassPathEntry::init(const char* name, bool is_modules_image, TRAPS) {
   259 void SharedClassPathEntry::init_as_non_existent(const char* path, TRAPS) {
   256   assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only");
   260   _type = non_existent_entry;
       
   261   set_name(path, THREAD);
       
   262 }
       
   263 
       
   264 void SharedClassPathEntry::init(bool is_modules_image,
       
   265                                 ClassPathEntry* cpe, TRAPS) {
       
   266   Arguments::assert_is_dumping_archive();
   257   _timestamp = 0;
   267   _timestamp = 0;
   258   _filesize  = 0;
   268   _filesize  = 0;
       
   269   _from_class_path_attr = false;
   259 
   270 
   260   struct stat st;
   271   struct stat st;
   261   if (os::stat(name, &st) == 0) {
   272   if (os::stat(cpe->name(), &st) == 0) {
   262     if ((st.st_mode & S_IFMT) == S_IFDIR) {
   273     if ((st.st_mode & S_IFMT) == S_IFDIR) {
   263       _type = dir_entry;
   274       _type = dir_entry;
   264     } else {
   275     } else {
   265       // The timestamp of the modules_image is not checked at runtime.
   276       // The timestamp of the modules_image is not checked at runtime.
   266       if (is_modules_image) {
   277       if (is_modules_image) {
   267         _type = modules_image_entry;
   278         _type = modules_image_entry;
   268       } else {
   279       } else {
   269         _type = jar_entry;
   280         _type = jar_entry;
   270         _timestamp = st.st_mtime;
   281         _timestamp = st.st_mtime;
       
   282         _from_class_path_attr = cpe->from_class_path_attr();
   271       }
   283       }
   272       _filesize = st.st_size;
   284       _filesize = st.st_size;
   273     }
   285     }
   274   } else {
   286   } else {
   275     // The file/dir must exist, or it would not have been added
   287     // The file/dir must exist, or it would not have been added
   276     // into ClassLoader::classpath_entry().
   288     // into ClassLoader::classpath_entry().
   277     //
   289     //
   278     // If we can't access a jar file in the boot path, then we can't
   290     // If we can't access a jar file in the boot path, then we can't
   279     // make assumptions about where classes get loaded from.
   291     // make assumptions about where classes get loaded from.
   280     FileMapInfo::fail_stop("Unable to open file %s.", name);
   292     FileMapInfo::fail_stop("Unable to open file %s.", cpe->name());
   281   }
   293   }
   282 
   294 
       
   295   // No need to save the name of the module file, as it will be computed at run time
       
   296   // to allow relocation of the JDK directory.
       
   297   const char* name = is_modules_image  ? "" : cpe->name();
       
   298   set_name(name, THREAD);
       
   299 }
       
   300 
       
   301 void SharedClassPathEntry::set_name(const char* name, TRAPS) {
   283   size_t len = strlen(name) + 1;
   302   size_t len = strlen(name) + 1;
   284   _name = MetadataFactory::new_array<char>(ClassLoaderData::the_null_class_loader_data(), (int)len, THREAD);
   303   _name = MetadataFactory::new_array<char>(ClassLoaderData::the_null_class_loader_data(), (int)len, THREAD);
   285   strcpy(_name->data(), name);
   304   strcpy(_name->data(), name);
   286 }
   305 }
   287 
   306 
   288 bool SharedClassPathEntry::validate(bool is_class_path) {
   307 const char* SharedClassPathEntry::name() const {
       
   308   if (UseSharedSpaces && is_modules_image()) {
       
   309     // In order to validate the runtime modules image file size against the archived
       
   310     // size information, we need to obtain the runtime modules image path. The recorded
       
   311     // dump time modules image path in the archive may be different from the runtime path
       
   312     // if the JDK image has beed moved after generating the archive.
       
   313     return ClassLoader::get_jrt_entry()->name();
       
   314   } else {
       
   315     return _name->data();
       
   316   }
       
   317 }
       
   318 
       
   319 bool SharedClassPathEntry::validate(bool is_class_path) const {
   289   assert(UseSharedSpaces, "runtime only");
   320   assert(UseSharedSpaces, "runtime only");
   290 
   321 
   291   struct stat st;
   322   struct stat st;
   292   const char* name;
   323   const char* name = this->name();
   293 
       
   294   // In order to validate the runtime modules image file size against the archived
       
   295   // size information, we need to obtain the runtime modules image path. The recorded
       
   296   // dump time modules image path in the archive may be different from the runtime path
       
   297   // if the JDK image has beed moved after generating the archive.
       
   298   if (is_modules_image()) {
       
   299     name = ClassLoader::get_jrt_entry()->name();
       
   300   } else {
       
   301     name = this->name();
       
   302   }
       
   303 
   324 
   304   bool ok = true;
   325   bool ok = true;
   305   log_info(class, path)("checking shared classpath entry: %s", name);
   326   log_info(class, path)("checking shared classpath entry: %s", name);
   306   if (os::stat(name, &st) != 0 && is_class_path) {
   327   if (os::stat(name, &st) != 0 && is_class_path) {
   307     // If the archived module path entry does not exist at runtime, it is not fatal
   328     // If the archived module path entry does not exist at runtime, it is not fatal
   335     MetaspaceShared::set_archive_loading_failed();
   356     MetaspaceShared::set_archive_loading_failed();
   336   }
   357   }
   337   return ok;
   358   return ok;
   338 }
   359 }
   339 
   360 
       
   361 bool SharedClassPathEntry::check_non_existent() const {
       
   362   assert(_type == non_existent_entry, "must be");
       
   363   log_info(class, path)("should be non-existent: %s", name());
       
   364   struct stat st;
       
   365   if (os::stat(name(), &st) != 0) {
       
   366     log_info(class, path)("ok");
       
   367     return true; // file doesn't exist
       
   368   } else {
       
   369     return false;
       
   370   }
       
   371 }
       
   372 
       
   373 
   340 void SharedClassPathEntry::metaspace_pointers_do(MetaspaceClosure* it) {
   374 void SharedClassPathEntry::metaspace_pointers_do(MetaspaceClosure* it) {
   341   it->push(&_name);
   375   it->push(&_name);
   342   it->push(&_manifest);
   376   it->push(&_manifest);
   343 }
   377 }
   344 
   378 
   349   }
   383   }
   350 }
   384 }
   351 
   385 
   352 void SharedPathTable::dumptime_init(ClassLoaderData* loader_data, Thread* THREAD) {
   386 void SharedPathTable::dumptime_init(ClassLoaderData* loader_data, Thread* THREAD) {
   353   size_t entry_size = sizeof(SharedClassPathEntry);
   387   size_t entry_size = sizeof(SharedClassPathEntry);
   354   int num_boot_classpath_entries = ClassLoader::num_boot_classpath_entries();
   388   int num_entries = 0;
   355   int num_app_classpath_entries = ClassLoader::num_app_classpath_entries();
   389   num_entries += ClassLoader::num_boot_classpath_entries();
   356   int num_module_path_entries = ClassLoader::num_module_path_entries();
   390   num_entries += ClassLoader::num_app_classpath_entries();
   357   int num_entries = num_boot_classpath_entries + num_app_classpath_entries + num_module_path_entries;
   391   num_entries += ClassLoader::num_module_path_entries();
       
   392   num_entries += FileMapInfo::num_non_existent_class_paths();
   358   size_t bytes = entry_size * num_entries;
   393   size_t bytes = entry_size * num_entries;
   359 
   394 
   360   _table = MetadataFactory::new_array<u8>(loader_data, (int)(bytes + 7 / 8), THREAD);
   395   _table = MetadataFactory::new_array<u8>(loader_data, (int)(bytes + 7 / 8), THREAD);
   361   _size = num_entries;
   396   _size = num_entries;
   362 }
   397 }
   363 
   398 
   364 void FileMapInfo::allocate_shared_path_table() {
   399 void FileMapInfo::allocate_shared_path_table() {
   365   assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "Sanity");
   400   Arguments::assert_is_dumping_archive();
   366 
   401 
   367   Thread* THREAD = Thread::current();
   402   EXCEPTION_MARK; // The following calls should never throw, but would exit VM on error.
   368   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
   403   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
   369   ClassPathEntry* jrt = ClassLoader::get_jrt_entry();
   404   ClassPathEntry* jrt = ClassLoader::get_jrt_entry();
   370 
   405 
   371   assert(jrt != NULL,
   406   assert(jrt != NULL,
   372          "No modular java runtime image present when allocating the CDS classpath entry table");
   407          "No modular java runtime image present when allocating the CDS classpath entry table");
   373 
   408 
   374   _shared_path_table.dumptime_init(loader_data, THREAD);
   409   _shared_path_table.dumptime_init(loader_data, THREAD);
   375 
   410 
   376   // 1. boot class path
   411   // 1. boot class path
   377   int i = 0;
   412   int i = 0;
   378   ClassPathEntry* cpe = jrt;
   413   i = add_shared_classpaths(i, "boot",   jrt, THREAD);
       
   414   i = add_shared_classpaths(i, "app",    ClassLoader::app_classpath_entries(), THREAD);
       
   415   i = add_shared_classpaths(i, "module", ClassLoader::module_path_entries(), THREAD);
       
   416 
       
   417   for (int x = 0; x < num_non_existent_class_paths(); x++, i++) {
       
   418     const char* path = _non_existent_class_paths->at(x);
       
   419     shared_path(i)->init_as_non_existent(path, THREAD);
       
   420   }
       
   421 
       
   422   assert(i == _shared_path_table.size(), "number of shared path entry mismatch");
       
   423 }
       
   424 
       
   425 int FileMapInfo::add_shared_classpaths(int i, const char* which, ClassPathEntry *cpe, TRAPS) {
   379   while (cpe != NULL) {
   426   while (cpe != NULL) {
   380     bool is_jrt = (cpe == jrt);
   427     bool is_jrt = (cpe == ClassLoader::get_jrt_entry());
   381     const char* type = (is_jrt ? "jrt" : (cpe->is_jar_file() ? "jar" : "dir"));
   428     const char* type = (is_jrt ? "jrt" : (cpe->is_jar_file() ? "jar" : "dir"));
   382     log_info(class, path)("add main shared path (%s) %s", type, cpe->name());
   429     log_info(class, path)("add %s shared path (%s) %s", which, type, cpe->name());
   383     SharedClassPathEntry* ent = shared_path(i);
   430     SharedClassPathEntry* ent = shared_path(i);
   384     ent->init(cpe->name(), is_jrt, THREAD);
   431     ent->init(is_jrt, cpe, THREAD);
   385     if (!is_jrt) {    // No need to do the modules image.
   432     if (cpe->is_jar_file()) {
   386       EXCEPTION_MARK; // The following call should never throw, but would exit VM on error.
   433       update_jar_manifest(cpe, ent, THREAD);
   387       update_shared_classpath(cpe, ent, THREAD);
   434     }
   388     }
   435     if (is_jrt) {
   389     cpe = ClassLoader::get_next_boot_classpath_entry(cpe);
   436       cpe = ClassLoader::get_next_boot_classpath_entry(cpe);
       
   437     } else {
       
   438       cpe = cpe->next();
       
   439     }
   390     i++;
   440     i++;
   391   }
   441   }
   392   assert(i == ClassLoader::num_boot_classpath_entries(),
   442 
   393          "number of boot class path entry mismatch");
   443   return i;
   394 
       
   395   // 2. app class path
       
   396   ClassPathEntry *acpe = ClassLoader::app_classpath_entries();
       
   397   while (acpe != NULL) {
       
   398     log_info(class, path)("add app shared path %s", acpe->name());
       
   399     SharedClassPathEntry* ent = shared_path(i);
       
   400     ent->init(acpe->name(), false, THREAD);
       
   401     EXCEPTION_MARK;
       
   402     update_shared_classpath(acpe, ent, THREAD);
       
   403     acpe = acpe->next();
       
   404     i++;
       
   405   }
       
   406 
       
   407   // 3. module path
       
   408   ClassPathEntry *mpe = ClassLoader::module_path_entries();
       
   409   while (mpe != NULL) {
       
   410     log_info(class, path)("add module path %s",mpe->name());
       
   411     SharedClassPathEntry* ent = shared_path(i);
       
   412     ent->init(mpe->name(), false, THREAD);
       
   413     EXCEPTION_MARK;
       
   414     update_shared_classpath(mpe, ent, THREAD);
       
   415     mpe = mpe->next();
       
   416     i++;
       
   417   }
       
   418   assert(i == _shared_path_table.size(), "number of shared path entry mismatch");
       
   419 }
   444 }
   420 
   445 
   421 void FileMapInfo::check_nonempty_dir_in_shared_path_table() {
   446 void FileMapInfo::check_nonempty_dir_in_shared_path_table() {
   422   assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only");
   447   Arguments::assert_is_dumping_archive();
   423 
   448 
   424   bool has_nonempty_dir = false;
   449   bool has_nonempty_dir = false;
   425 
   450 
   426   int last = _shared_path_table.size() - 1;
   451   int last = _shared_path_table.size() - 1;
   427   if (last > ClassLoaderExt::max_used_path_index()) {
   452   if (last > ClassLoaderExt::max_used_path_index()) {
   432   for (int i = 0; i <= last; i++) {
   457   for (int i = 0; i <= last; i++) {
   433     SharedClassPathEntry *e = shared_path(i);
   458     SharedClassPathEntry *e = shared_path(i);
   434     if (e->is_dir()) {
   459     if (e->is_dir()) {
   435       const char* path = e->name();
   460       const char* path = e->name();
   436       if (!os::dir_is_empty(path)) {
   461       if (!os::dir_is_empty(path)) {
   437         tty->print_cr("Error: non-empty directory '%s'", path);
   462         log_error(cds)("Error: non-empty directory '%s'", path);
   438         has_nonempty_dir = true;
   463         has_nonempty_dir = true;
   439       }
   464       }
   440     }
   465     }
   441   }
   466   }
   442 
   467 
   443   if (has_nonempty_dir) {
   468   if (has_nonempty_dir) {
   444     ClassLoader::exit_with_path_failure("Cannot have non-empty directory in paths", NULL);
   469     ClassLoader::exit_with_path_failure("Cannot have non-empty directory in paths", NULL);
       
   470   }
       
   471 }
       
   472 
       
   473 void FileMapInfo::record_non_existent_class_path_entry(const char* path) {
       
   474   Arguments::assert_is_dumping_archive();
       
   475   log_info(class, path)("non-existent Class-Path entry %s", path);
       
   476   if (_non_existent_class_paths == NULL) {
       
   477     _non_existent_class_paths = new (ResourceObj::C_HEAP, mtInternal)GrowableArray<const char*>(10, true);
       
   478   }
       
   479   _non_existent_class_paths->append(os::strdup(path));
       
   480 }
       
   481 
       
   482 int FileMapInfo::num_non_existent_class_paths() {
       
   483   Arguments::assert_is_dumping_archive();
       
   484   if (_non_existent_class_paths != NULL) {
       
   485     return _non_existent_class_paths->length();
       
   486   } else {
       
   487     return 0;
   445   }
   488   }
   446 }
   489 }
   447 
   490 
   448 class ManifestStream: public ResourceObj {
   491 class ManifestStream: public ResourceObj {
   449   private:
   492   private:
   491     }
   534     }
   492     return isSigned;
   535     return isSigned;
   493   }
   536   }
   494 };
   537 };
   495 
   538 
   496 void FileMapInfo::update_shared_classpath(ClassPathEntry *cpe, SharedClassPathEntry* ent, TRAPS) {
   539 void FileMapInfo::update_jar_manifest(ClassPathEntry *cpe, SharedClassPathEntry* ent, TRAPS) {
   497   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
   540   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
   498   ResourceMark rm(THREAD);
   541   ResourceMark rm(THREAD);
   499   jint manifest_size;
   542   jint manifest_size;
   500 
   543 
   501   if (cpe->is_jar_file()) {
   544   assert(cpe->is_jar_file() && ent->is_jar(), "the shared class path entry is not a JAR file");
   502     assert(ent->is_jar(), "the shared class path entry is not a JAR file");
   545   char* manifest = ClassLoaderExt::read_manifest(cpe, &manifest_size, CHECK);
   503     char* manifest = ClassLoaderExt::read_manifest(cpe, &manifest_size, CHECK);
   546   if (manifest != NULL) {
   504     if (manifest != NULL) {
   547     ManifestStream* stream = new ManifestStream((u1*)manifest,
   505       ManifestStream* stream = new ManifestStream((u1*)manifest,
   548                                                 manifest_size);
   506                                                   manifest_size);
   549     if (stream->check_is_signed()) {
   507       if (stream->check_is_signed()) {
   550       ent->set_is_signed();
   508         ent->set_is_signed();
   551     } else {
       
   552       // Copy the manifest into the shared archive
       
   553       manifest = ClassLoaderExt::read_raw_manifest(cpe, &manifest_size, CHECK);
       
   554       Array<u1>* buf = MetadataFactory::new_array<u1>(loader_data,
       
   555                                                       manifest_size,
       
   556                                                       THREAD);
       
   557       char* p = (char*)(buf->data());
       
   558       memcpy(p, manifest, manifest_size);
       
   559       ent->set_manifest(buf);
       
   560     }
       
   561   }
       
   562 }
       
   563 
       
   564 char* FileMapInfo::skip_first_path_entry(const char* path) {
       
   565   size_t path_sep_len = strlen(os::path_separator());
       
   566   char* p = strstr((char*)path, os::path_separator());
       
   567   if (p != NULL) {
       
   568     debug_only( {
       
   569       size_t image_name_len = strlen(MODULES_IMAGE_NAME);
       
   570       assert(strncmp(p - image_name_len, MODULES_IMAGE_NAME, image_name_len) == 0,
       
   571              "first entry must be the modules image");
       
   572     } );
       
   573     p += path_sep_len;
       
   574   } else {
       
   575     debug_only( {
       
   576       assert(ClassLoader::string_ends_with(path, MODULES_IMAGE_NAME),
       
   577              "first entry must be the modules image");
       
   578     } );
       
   579   }
       
   580   return p;
       
   581 }
       
   582 
       
   583 int FileMapInfo::num_paths(const char* path) {
       
   584   if (path == NULL) {
       
   585     return 0;
       
   586   }
       
   587   int npaths = 1;
       
   588   char* p = (char*)path;
       
   589   while (p != NULL) {
       
   590     char* prev = p;
       
   591     p = strstr((char*)p, os::path_separator());
       
   592     if (p != NULL) {
       
   593       p++;
       
   594       // don't count empty path
       
   595       if ((p - prev) > 1) {
       
   596        npaths++;
       
   597       }
       
   598     }
       
   599   }
       
   600   return npaths;
       
   601 }
       
   602 
       
   603 GrowableArray<const char*>* FileMapInfo::create_path_array(const char* paths) {
       
   604   GrowableArray<const char*>* path_array =  new(ResourceObj::RESOURCE_AREA, mtInternal)
       
   605       GrowableArray<const char*>(10);
       
   606 
       
   607   ClasspathStream cp_stream(paths);
       
   608   while (cp_stream.has_next()) {
       
   609     const char* path = cp_stream.get_next();
       
   610     struct stat st;
       
   611     if (os::stat(path, &st) == 0) {
       
   612       path_array->append(path);
       
   613     }
       
   614   }
       
   615   return path_array;
       
   616 }
       
   617 
       
   618 bool FileMapInfo::fail(const char* msg, const char* name) {
       
   619   ClassLoader::trace_class_path(msg, name);
       
   620   MetaspaceShared::set_archive_loading_failed();
       
   621   return false;
       
   622 }
       
   623 
       
   624 bool FileMapInfo::check_paths(int shared_path_start_idx, int num_paths, GrowableArray<const char*>* rp_array) {
       
   625   int i = 0;
       
   626   int j = shared_path_start_idx;
       
   627   bool mismatch = false;
       
   628   while (i < num_paths && !mismatch) {
       
   629     while (shared_path(j)->from_class_path_attr()) {
       
   630       // shared_path(j) was expanded from the JAR file attribute "Class-Path:"
       
   631       // during dump time. It's not included in the -classpath VM argument.
       
   632       j++;
       
   633     }
       
   634     if (!os::same_files(shared_path(j)->name(), rp_array->at(i))) {
       
   635       mismatch = true;
       
   636     }
       
   637     i++;
       
   638     j++;
       
   639   }
       
   640   return mismatch;
       
   641 }
       
   642 
       
   643 bool FileMapInfo::validate_boot_class_paths() {
       
   644   //
       
   645   // - Archive contains boot classes only - relaxed boot path check:
       
   646   //   Extra path elements appended to the boot path at runtime are allowed.
       
   647   //
       
   648   // - Archive contains application or platform classes - strict boot path check:
       
   649   //   Validate the entire runtime boot path, which must be compatible
       
   650   //   with the dump time boot path. Appending boot path at runtime is not
       
   651   //   allowed.
       
   652   //
       
   653 
       
   654   // The first entry in boot path is the modules_image (guaranteed by
       
   655   // ClassLoader::setup_boot_search_path()). Skip the first entry. The
       
   656   // path of the runtime modules_image may be different from the dump
       
   657   // time path (e.g. the JDK image is copied to a different location
       
   658   // after generating the shared archive), which is acceptable. For most
       
   659   // common cases, the dump time boot path might contain modules_image only.
       
   660   char* runtime_boot_path = Arguments::get_sysclasspath();
       
   661   char* rp = skip_first_path_entry(runtime_boot_path);
       
   662   assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
       
   663   int dp_len = header()->app_class_paths_start_index() - 1; // ignore the first path to the module image
       
   664   bool mismatch = false;
       
   665 
       
   666   bool relaxed_check = !header()->has_platform_or_app_classes();
       
   667   if (dp_len == 0 && rp == NULL) {
       
   668     return true;   // ok, both runtime and dump time boot paths have modules_images only
       
   669   } else if (dp_len == 0 && rp != NULL) {
       
   670     if (relaxed_check) {
       
   671       return true;   // ok, relaxed check, runtime has extra boot append path entries
       
   672     } else {
       
   673       mismatch = true;
       
   674     }
       
   675   } else if (dp_len > 0 && rp != NULL) {
       
   676     int num;
       
   677     ResourceMark rm;
       
   678     GrowableArray<const char*>* rp_array = create_path_array(rp);
       
   679     int rp_len = rp_array->length();
       
   680     if (rp_len >= dp_len) {
       
   681       if (relaxed_check) {
       
   682         // only check the leading entries in the runtime boot path, up to
       
   683         // the length of the dump time boot path
       
   684         num = dp_len;
   509       } else {
   685       } else {
   510         // Copy the manifest into the shared archive
   686         // check the full runtime boot path, must match with dump time
   511         manifest = ClassLoaderExt::read_raw_manifest(cpe, &manifest_size, CHECK);
   687         num = rp_len;
   512         Array<u1>* buf = MetadataFactory::new_array<u1>(loader_data,
       
   513                                                         manifest_size,
       
   514                                                         THREAD);
       
   515         char* p = (char*)(buf->data());
       
   516         memcpy(p, manifest, manifest_size);
       
   517         ent->set_manifest(buf);
       
   518       }
   688       }
   519     }
   689       mismatch = check_paths(1, num, rp_array);
   520   }
   690     }
   521 }
   691   }
   522 
   692 
       
   693   if (mismatch) {
       
   694     // The paths are different
       
   695     return fail("[BOOT classpath mismatch, actual =", runtime_boot_path);
       
   696   }
       
   697   return true;
       
   698 }
       
   699 
       
   700 bool FileMapInfo::validate_app_class_paths(int shared_app_paths_len) {
       
   701   const char *appcp = Arguments::get_appclasspath();
       
   702   assert(appcp != NULL, "NULL app classpath");
       
   703   int rp_len = num_paths(appcp);
       
   704   bool mismatch = false;
       
   705   if (rp_len < shared_app_paths_len) {
       
   706     return fail("Run time APP classpath is shorter than the one at dump time: ", appcp);
       
   707   }
       
   708   if (shared_app_paths_len != 0 && rp_len != 0) {
       
   709     // Prefix is OK: E.g., dump with -cp foo.jar, but run with -cp foo.jar:bar.jar.
       
   710     ResourceMark rm;
       
   711     GrowableArray<const char*>* rp_array = create_path_array(appcp);
       
   712     if (rp_array->length() == 0) {
       
   713       // None of the jar file specified in the runtime -cp exists.
       
   714       return fail("None of the jar file specified in the runtime -cp exists: -Djava.class.path=", appcp);
       
   715     }
       
   716 
       
   717     // Handling of non-existent entries in the classpath: we eliminate all the non-existent
       
   718     // entries from both the dump time classpath (ClassLoader::update_class_path_entry_list)
       
   719     // and the runtime classpath (FileMapInfo::create_path_array), and check the remaining
       
   720     // entries. E.g.:
       
   721     //
       
   722     // dump : -cp a.jar:NE1:NE2:b.jar  -> a.jar:b.jar -> recorded in archive.
       
   723     // run 1: -cp NE3:a.jar:NE4:b.jar  -> a.jar:b.jar -> matched
       
   724     // run 2: -cp x.jar:NE4:b.jar      -> x.jar:b.jar -> mismatched
       
   725 
       
   726     int j = header()->app_class_paths_start_index();
       
   727     mismatch = check_paths(j, shared_app_paths_len, rp_array);
       
   728     if (mismatch) {
       
   729       return fail("[APP classpath mismatch, actual: -Djava.class.path=", appcp);
       
   730     }
       
   731   }
       
   732   return true;
       
   733 }
       
   734 
       
   735 void FileMapInfo::log_paths(const char* msg, int start_idx, int end_idx) {
       
   736   LogTarget(Info, class, path) lt;
       
   737   if (lt.is_enabled()) {
       
   738     LogStream ls(lt);
       
   739     ls.print("%s", msg);
       
   740     const char* prefix = "";
       
   741     for (int i = start_idx; i < end_idx; i++) {
       
   742       ls.print("%s%s", prefix, shared_path(i)->name());
       
   743       prefix = os::path_separator();
       
   744     }
       
   745     ls.cr();
       
   746   }
       
   747 }
   523 
   748 
   524 bool FileMapInfo::validate_shared_path_table() {
   749 bool FileMapInfo::validate_shared_path_table() {
   525   assert(UseSharedSpaces, "runtime only");
   750   assert(UseSharedSpaces, "runtime only");
   526 
   751 
   527   _validating_shared_path_table = true;
   752   _validating_shared_path_table = true;
   528 
   753 
   529   // Load the shared path table info from the archive header
   754   // Load the shared path table info from the archive header
   530   _shared_path_table = _header->_shared_path_table;
   755   _shared_path_table = header()->shared_path_table();
   531   if (DynamicDumpSharedSpaces) {
   756   if (DynamicDumpSharedSpaces) {
   532     // Only support dynamic dumping with the usage of the default CDS archive
   757     // Only support dynamic dumping with the usage of the default CDS archive
   533     // or a simple base archive.
   758     // or a simple base archive.
   534     // If the base layer archive contains additional path component besides
   759     // If the base layer archive contains additional path component besides
   535     // the runtime image and the -cp, dynamic dumping is disabled.
   760     // the runtime image and the -cp, dynamic dumping is disabled.
   536     //
   761     //
   537     // When dynamic archiving is enabled, the _shared_path_table is overwritten
   762     // When dynamic archiving is enabled, the _shared_path_table is overwritten
   538     // to include the application path and stored in the top layer archive.
   763     // to include the application path and stored in the top layer archive.
   539     assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
   764     assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
   540     if (_header->_app_class_paths_start_index > 1) {
   765     if (header()->app_class_paths_start_index() > 1) {
   541       DynamicDumpSharedSpaces = false;
   766       DynamicDumpSharedSpaces = false;
   542       warning(
   767       warning(
   543         "Dynamic archiving is disabled because base layer archive has appended boot classpath");
   768         "Dynamic archiving is disabled because base layer archive has appended boot classpath");
   544     }
   769     }
   545     if (_header->_num_module_paths > 0) {
   770     if (header()->num_module_paths() > 0) {
   546       DynamicDumpSharedSpaces = false;
   771       DynamicDumpSharedSpaces = false;
   547       warning(
   772       warning(
   548         "Dynamic archiving is disabled because base layer archive has module path");
   773         "Dynamic archiving is disabled because base layer archive has module path");
   549     }
   774     }
   550   }
   775   }
   551 
   776 
   552   int module_paths_start_index = _header->_app_module_paths_start_index;
   777   log_paths("Expecting BOOT path=", 0, header()->app_class_paths_start_index());
       
   778   log_paths("Expecting -Djava.class.path=", header()->app_class_paths_start_index(), header()->app_module_paths_start_index());
       
   779 
       
   780   int module_paths_start_index = header()->app_module_paths_start_index();
       
   781   int shared_app_paths_len = 0;
   553 
   782 
   554   // validate the path entries up to the _max_used_path_index
   783   // validate the path entries up to the _max_used_path_index
   555   for (int i=0; i < _header->_max_used_path_index + 1; i++) {
   784   for (int i=0; i < header()->max_used_path_index() + 1; i++) {
   556     if (i < module_paths_start_index) {
   785     if (i < module_paths_start_index) {
   557       if (shared_path(i)->validate()) {
   786       if (shared_path(i)->validate()) {
       
   787         // Only count the app class paths not from the "Class-path" attribute of a jar manifest.
       
   788         if (!shared_path(i)->from_class_path_attr() && i >= header()->app_class_paths_start_index()) {
       
   789           shared_app_paths_len++;
       
   790         }
   558         log_info(class, path)("ok");
   791         log_info(class, path)("ok");
   559       } else {
   792       } else {
   560         if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) {
   793         if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) {
   561           assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
   794           assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
   562         }
   795         }
   572         return false;
   805         return false;
   573       }
   806       }
   574     }
   807     }
   575   }
   808   }
   576 
   809 
       
   810   if (header()->max_used_path_index() == 0) {
       
   811     // default archive only contains the module image in the bootclasspath
       
   812     assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
       
   813   } else {
       
   814     if (!validate_boot_class_paths() || !validate_app_class_paths(shared_app_paths_len)) {
       
   815       fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)");
       
   816       return false;
       
   817     }
       
   818   }
       
   819 
       
   820   validate_non_existent_class_paths();
       
   821 
   577   _validating_shared_path_table = false;
   822   _validating_shared_path_table = false;
   578 
   823 
   579 #if INCLUDE_JVMTI
   824 #if INCLUDE_JVMTI
   580   if (_classpath_entries_for_jvmti != NULL) {
   825   if (_classpath_entries_for_jvmti != NULL) {
   581     os::free(_classpath_entries_for_jvmti);
   826     os::free(_classpath_entries_for_jvmti);
   586 #endif
   831 #endif
   587 
   832 
   588   return true;
   833   return true;
   589 }
   834 }
   590 
   835 
   591 bool FileMapInfo::same_files(const char* file1, const char* file2) {
   836 void FileMapInfo::validate_non_existent_class_paths() {
   592   if (strcmp(file1, file2) == 0) {
   837   // All of the recorded non-existent paths came from the Class-Path: attribute from the JAR
   593     return true;
   838   // files on the app classpath. If any of these are found to exist during runtime,
   594   }
   839   // it will change how classes are loading for the app loader. For safety, disable
   595 
   840   // loading of archived platform/app classes (currently there's no way to disable just the
   596   bool is_same = false;
   841   // app classes).
   597   // if the two paths diff only in case
   842 
   598   struct stat st1;
   843   assert(UseSharedSpaces, "runtime only");
   599   struct stat st2;
   844   for (int i = header()->app_module_paths_start_index() + header()->num_module_paths();
   600   int ret1;
   845        i < get_number_of_shared_paths();
   601   int ret2;
   846        i++) {
   602   ret1 = os::stat(file1, &st1);
   847     SharedClassPathEntry* ent = shared_path(i);
   603   ret2 = os::stat(file2, &st2);
   848     if (!ent->check_non_existent()) {
   604   if (ret1 < 0 || ret2 < 0) {
   849       warning("Archived non-system classes are disabled because the "
   605     // one of the files is invalid. So they are not the same.
   850               "file %s exists", ent->name());
   606     is_same = false;
   851       header()->set_has_platform_or_app_classes(false);
   607   } else if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) {
   852     }
   608     // different files
   853   }
   609     is_same = false;
       
   610 #ifndef _WINDOWS
       
   611   } else if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) {
       
   612     // same files
       
   613     is_same = true;
       
   614 #else
       
   615   } else if ((st1.st_size == st2.st_size) && (st1.st_ctime == st2.st_ctime) &&
       
   616              (st1.st_mtime == st2.st_mtime)) {
       
   617     // same files
       
   618     is_same = true;
       
   619 #endif
       
   620   }
       
   621   return is_same;
       
   622 }
   854 }
   623 
   855 
   624 bool FileMapInfo::check_archive(const char* archive_name, bool is_static) {
   856 bool FileMapInfo::check_archive(const char* archive_name, bool is_static) {
   625   int fd = os::open(archive_name, O_RDONLY | O_BINARY, 0);
   857   int fd = os::open(archive_name, O_RDONLY | O_BINARY, 0);
   626   if (fd < 0) {
   858   if (fd < 0) {
   640     vm_exit_during_initialization("Unable to read header from shared archive", archive_name);
   872     vm_exit_during_initialization("Unable to read header from shared archive", archive_name);
   641     return false;
   873     return false;
   642   }
   874   }
   643   if (is_static) {
   875   if (is_static) {
   644     FileMapHeader* static_header = (FileMapHeader*)header;
   876     FileMapHeader* static_header = (FileMapHeader*)header;
   645     if (static_header->_magic != CDS_ARCHIVE_MAGIC) {
   877     if (static_header->magic() != CDS_ARCHIVE_MAGIC) {
   646       os::free(header);
   878       os::free(header);
   647       os::close(fd);
   879       os::close(fd);
   648       vm_exit_during_initialization("Not a base shared archive", archive_name);
   880       vm_exit_during_initialization("Not a base shared archive", archive_name);
   649       return false;
   881       return false;
   650     }
   882     }
   651   } else {
   883   } else {
   652     DynamicArchiveHeader* dynamic_header = (DynamicArchiveHeader*)header;
   884     DynamicArchiveHeader* dynamic_header = (DynamicArchiveHeader*)header;
   653     if (dynamic_header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
   885     if (dynamic_header->magic() != CDS_DYNAMIC_ARCHIVE_MAGIC) {
   654       os::free(header);
   886       os::free(header);
   655       os::close(fd);
   887       os::close(fd);
   656       vm_exit_during_initialization("Not a top shared archive", archive_name);
   888       vm_exit_during_initialization("Not a top shared archive", archive_name);
   657       return false;
   889       return false;
   658     }
   890     }
   678     fail_continue("Unable to read the file header.");
   910     fail_continue("Unable to read the file header.");
   679     os::free(dynamic_header);
   911     os::free(dynamic_header);
   680     os::close(fd);
   912     os::close(fd);
   681     return false;
   913     return false;
   682   }
   914   }
   683   if (dynamic_header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
   915   if (dynamic_header->magic() != CDS_DYNAMIC_ARCHIVE_MAGIC) {
   684     // Not a dynamic header, no need to proceed further.
   916     // Not a dynamic header, no need to proceed further.
   685     *size = 0;
   917     *size = 0;
   686     os::free(dynamic_header);
   918     os::free(dynamic_header);
   687     os::close(fd);
   919     os::close(fd);
   688     return false;
   920     return false;
   689   }
   921   }
   690   if (dynamic_header->_base_archive_is_default) {
   922   if (dynamic_header->base_archive_is_default()) {
   691     *base_archive_name = Arguments::get_default_shared_archive_path();
   923     *base_archive_name = Arguments::get_default_shared_archive_path();
   692   } else {
   924   } else {
   693     // skip over the _paths_misc_info
       
   694     sz = dynamic_header->_paths_misc_info_size;
       
   695     lseek(fd, (long)sz, SEEK_CUR);
       
   696     // read the base archive name
   925     // read the base archive name
   697     size_t name_size = dynamic_header->_base_archive_name_size;
   926     size_t name_size = dynamic_header->base_archive_name_size();
   698     if (name_size == 0) {
   927     if (name_size == 0) {
   699       os::free(dynamic_header);
   928       os::free(dynamic_header);
   700       os::close(fd);
   929       os::close(fd);
   701       return false;
   930       return false;
   702     }
   931     }
   716   os::close(fd);
   945   os::close(fd);
   717   return true;
   946   return true;
   718 }
   947 }
   719 
   948 
   720 void FileMapInfo::restore_shared_path_table() {
   949 void FileMapInfo::restore_shared_path_table() {
   721   _shared_path_table = _current_info->_header->_shared_path_table;
   950   _shared_path_table = _current_info->header()->shared_path_table();
   722 }
   951 }
   723 
   952 
   724 // Read the FileMapInfo information from the file.
   953 // Read the FileMapInfo information from the file.
   725 
   954 
   726 bool FileMapInfo::init_from_file(int fd, bool is_static) {
   955 bool FileMapInfo::init_from_file(int fd, bool is_static) {
   727   size_t sz = is_static ? sizeof(FileMapHeader) : sizeof(DynamicArchiveHeader);
   956   size_t sz = is_static ? sizeof(FileMapHeader) : sizeof(DynamicArchiveHeader);
   728   size_t n = os::read(fd, _header, (unsigned int)sz);
   957   size_t n = os::read(fd, header(), (unsigned int)sz);
   729   if (n != sz) {
   958   if (n != sz) {
   730     fail_continue("Unable to read the file header.");
   959     fail_continue("Unable to read the file header.");
   731     return false;
   960     return false;
   732   }
   961   }
   733   if (_header->_version != CURRENT_CDS_ARCHIVE_VERSION) {
   962 
       
   963   if (!Arguments::has_jimage()) {
       
   964     FileMapInfo::fail_continue("The shared archive file cannot be used with an exploded module build.");
       
   965     return false;
       
   966   }
       
   967 
       
   968   unsigned int expected_magic = is_static ? CDS_ARCHIVE_MAGIC : CDS_DYNAMIC_ARCHIVE_MAGIC;
       
   969   if (header()->magic() != expected_magic) {
       
   970     log_info(cds)("_magic expected: 0x%08x", expected_magic);
       
   971     log_info(cds)("         actual: 0x%08x", header()->magic());
       
   972     FileMapInfo::fail_continue("The shared archive file has a bad magic number.");
       
   973     return false;
       
   974   }
       
   975 
       
   976   if (header()->version() != CURRENT_CDS_ARCHIVE_VERSION) {
       
   977     log_info(cds)("_version expected: %d", CURRENT_CDS_ARCHIVE_VERSION);
       
   978     log_info(cds)("           actual: %d", header()->version());
   734     fail_continue("The shared archive file has the wrong version.");
   979     fail_continue("The shared archive file has the wrong version.");
   735     return false;
   980     return false;
   736   }
   981   }
   737   _file_offset = n;
   982 
   738 
   983   if (header()->header_size() != sz) {
   739   size_t info_size = _header->_paths_misc_info_size;
   984     log_info(cds)("_header_size expected: " SIZE_FORMAT, sz);
   740   _paths_misc_info = NEW_C_HEAP_ARRAY(char, info_size, mtClass);
   985     log_info(cds)("               actual: " SIZE_FORMAT, header()->header_size());
   741   n = os::read(fd, _paths_misc_info, (unsigned int)info_size);
   986     FileMapInfo::fail_continue("The shared archive file has an incorrect header size.");
   742   if (n != info_size) {
   987     return false;
   743     fail_continue("Unable to read the shared path info header.");
   988   }
   744     FREE_C_HEAP_ARRAY(char, _paths_misc_info);
   989 
   745     _paths_misc_info = NULL;
   990   const char* actual_ident = header()->jvm_ident();
   746     return false;
   991 
   747   }
   992   if (actual_ident[JVM_IDENT_MAX-1] != 0) {
   748   _file_offset += n + _header->_base_archive_name_size; // accounts for the size of _base_archive_name
   993     FileMapInfo::fail_continue("JVM version identifier is corrupted.");
       
   994     return false;
       
   995   }
       
   996 
       
   997   char expected_ident[JVM_IDENT_MAX];
       
   998   get_header_version(expected_ident);
       
   999   if (strncmp(actual_ident, expected_ident, JVM_IDENT_MAX-1) != 0) {
       
  1000     log_info(cds)("_jvm_ident expected: %s", expected_ident);
       
  1001     log_info(cds)("             actual: %s", actual_ident);
       
  1002     FileMapInfo::fail_continue("The shared archive file was created by a different"
       
  1003                   " version or build of HotSpot");
       
  1004     return false;
       
  1005   }
       
  1006 
       
  1007   if (VerifySharedSpaces) {
       
  1008     int expected_crc = header()->compute_crc();
       
  1009     if (expected_crc != header()->crc()) {
       
  1010       log_info(cds)("_crc expected: %d", expected_crc);
       
  1011       log_info(cds)("       actual: %d", header()->crc());
       
  1012       FileMapInfo::fail_continue("Header checksum verification failed.");
       
  1013       return false;
       
  1014     }
       
  1015   }
       
  1016 
       
  1017   _file_offset = n + header()->base_archive_name_size(); // accounts for the size of _base_archive_name
   749 
  1018 
   750   if (is_static) {
  1019   if (is_static) {
   751     if (_header->_magic != CDS_ARCHIVE_MAGIC) {
       
   752       fail_continue("Incorrect static archive magic number");
       
   753       return false;
       
   754     }
       
   755     // just checking the last region is sufficient since the archive is written
  1020     // just checking the last region is sufficient since the archive is written
   756     // in sequential order
  1021     // in sequential order
   757     size_t len = lseek(fd, 0, SEEK_END);
  1022     size_t len = lseek(fd, 0, SEEK_END);
   758     CDSFileMapRegion* si = space_at(MetaspaceShared::last_valid_region);
  1023     FileMapRegion* si = space_at(MetaspaceShared::last_valid_region);
   759     // The last space might be empty
  1024     // The last space might be empty
   760     if (si->_file_offset > len || len - si->_file_offset < si->_used) {
  1025     if (si->file_offset() > len || len - si->file_offset() < si->used()) {
   761       fail_continue("The shared archive file has been truncated.");
  1026       fail_continue("The shared archive file has been truncated.");
   762       return false;
  1027       return false;
   763     }
  1028     }
   764 
  1029 
   765     SharedBaseAddress = _header->_shared_base_address;
  1030     SharedBaseAddress = header()->shared_base_address();
   766   }
  1031   }
   767 
  1032 
   768   return true;
  1033   return true;
   769 }
  1034 }
   770 
  1035 
       
  1036 void FileMapInfo::seek_to_position(size_t pos) {
       
  1037   if (lseek(_fd, (long)pos, SEEK_SET) < 0) {
       
  1038     fail_stop("Unable to seek to position " SIZE_FORMAT, pos);
       
  1039   }
       
  1040 }
   771 
  1041 
   772 // Read the FileMapInfo information from the file.
  1042 // Read the FileMapInfo information from the file.
   773 bool FileMapInfo::open_for_read(const char* path) {
  1043 bool FileMapInfo::open_for_read(const char* path) {
   774   if (_file_open) {
  1044   if (_file_open) {
   775     return true;
  1045     return true;
   821   if (fd < 0) {
  1091   if (fd < 0) {
   822     fail_stop("Unable to create shared archive file %s: (%s).", _full_path,
  1092     fail_stop("Unable to create shared archive file %s: (%s).", _full_path,
   823               os::strerror(errno));
  1093               os::strerror(errno));
   824   }
  1094   }
   825   _fd = fd;
  1095   _fd = fd;
       
  1096   _file_open = true;
       
  1097 
       
  1098   // Seek past the header. We will write the header after all regions are written
       
  1099   // and their CRCs computed.
       
  1100   size_t header_bytes = header()->header_size();
       
  1101   if (header()->magic() == CDS_DYNAMIC_ARCHIVE_MAGIC) {
       
  1102     header_bytes += strlen(Arguments::GetSharedArchivePath()) + 1;
       
  1103   }
       
  1104 
       
  1105   header_bytes = align_up(header_bytes, os::vm_allocation_granularity());
       
  1106   _file_offset = header_bytes;
       
  1107   seek_to_position(_file_offset);
       
  1108 }
       
  1109 
       
  1110 
       
  1111 // Write the header to the file, seek to the next allocation boundary.
       
  1112 
       
  1113 void FileMapInfo::write_header() {
   826   _file_offset = 0;
  1114   _file_offset = 0;
   827   _file_open = true;
  1115   seek_to_position(_file_offset);
   828 }
       
   829 
       
   830 
       
   831 // Write the header to the file, seek to the next allocation boundary.
       
   832 
       
   833 void FileMapInfo::write_header() {
       
   834   int info_size = ClassLoader::get_shared_paths_misc_info_size();
       
   835 
       
   836   _header->_paths_misc_info_size = info_size;
       
   837 
       
   838   char* base_archive_name = NULL;
  1116   char* base_archive_name = NULL;
   839   if (_header->_magic == CDS_DYNAMIC_ARCHIVE_MAGIC) {
  1117   if (header()->magic() == CDS_DYNAMIC_ARCHIVE_MAGIC) {
   840     base_archive_name = (char*)Arguments::GetSharedArchivePath();
  1118     base_archive_name = (char*)Arguments::GetSharedArchivePath();
   841     _header->_base_archive_name_size = (int)strlen(base_archive_name) + 1;
  1119     header()->set_base_archive_name_size(strlen(base_archive_name) + 1);
   842     _header->_base_archive_is_default = FLAG_IS_DEFAULT(SharedArchiveFile);
  1120     header()->set_base_archive_is_default(FLAG_IS_DEFAULT(SharedArchiveFile));
   843   }
  1121   }
   844 
  1122 
   845   assert(is_file_position_aligned(), "must be");
  1123   assert(is_file_position_aligned(), "must be");
   846   write_bytes(_header, _header->_header_size);
  1124   write_bytes(header(), header()->header_size());
   847   write_bytes(ClassLoader::get_shared_paths_misc_info(), (size_t)info_size);
       
   848   if (base_archive_name != NULL) {
  1125   if (base_archive_name != NULL) {
   849     write_bytes(base_archive_name, (size_t)_header->_base_archive_name_size);
  1126     write_bytes(base_archive_name, header()->base_archive_name_size());
   850   }
  1127   }
   851   align_file_position();
  1128 }
   852 }
  1129 
   853 
  1130 void FileMapRegion::init(bool is_heap_region, char* base, size_t size, bool read_only,
   854 // Dump region to file.
  1131                          bool allow_exec, int crc) {
   855 // This is called twice for each region during archiving, once before
  1132   _is_heap_region = is_heap_region;
   856 // the archive file is open (_file_open is false) and once after.
  1133 
       
  1134   if (is_heap_region) {
       
  1135     assert(!DynamicDumpSharedSpaces, "must be");
       
  1136     assert((base - (char*)CompressedKlassPointers::base()) % HeapWordSize == 0, "Sanity");
       
  1137     if (base != NULL) {
       
  1138       _addr._offset = (intx)CompressedOops::encode_not_null((oop)base);
       
  1139     } else {
       
  1140       _addr._offset = 0;
       
  1141     }
       
  1142   } else {
       
  1143     _addr._base = base;
       
  1144   }
       
  1145   _used = size;
       
  1146   _read_only = read_only;
       
  1147   _allow_exec = allow_exec;
       
  1148   _crc = crc;
       
  1149 }
       
  1150 
   857 void FileMapInfo::write_region(int region, char* base, size_t size,
  1151 void FileMapInfo::write_region(int region, char* base, size_t size,
   858                                bool read_only, bool allow_exec) {
  1152                                bool read_only, bool allow_exec) {
   859   assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "Dump time only");
  1153   Arguments::assert_is_dumping_archive();
   860 
  1154 
   861   CDSFileMapRegion* si = space_at(region);
  1155   FileMapRegion* si = space_at(region);
   862   char* target_base = base;
  1156   char* target_base = base;
   863   if (DynamicDumpSharedSpaces) {
  1157   if (DynamicDumpSharedSpaces) {
       
  1158     assert(!HeapShared::is_heap_region(region), "dynamic archive doesn't support heap regions");
   864     target_base = DynamicArchive::buffer_to_target(base);
  1159     target_base = DynamicArchive::buffer_to_target(base);
   865   }
  1160   }
   866 
  1161 
   867   if (_file_open) {
  1162   si->set_file_offset(_file_offset);
   868     guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
  1163   log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08)
   869     log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08)
  1164                 " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),
   870                   " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),
  1165                 region, size, p2i(target_base), _file_offset);
   871                   region, size, p2i(target_base), _file_offset);
  1166 
   872   } else {
  1167   int crc = ClassLoader::crc32(0, base, (jint)size);
   873     si->_file_offset = _file_offset;
  1168   si->init(HeapShared::is_heap_region(region), target_base, size, read_only, allow_exec, crc);
   874   }
  1169 
   875 
       
   876   if (HeapShared::is_heap_region(region)) {
       
   877     assert((target_base - (char*)CompressedKlassPointers::base()) % HeapWordSize == 0, "Sanity");
       
   878     if (target_base != NULL) {
       
   879       si->_addr._offset = (intx)CompressedOops::encode_not_null((oop)target_base);
       
   880     } else {
       
   881       si->_addr._offset = 0;
       
   882     }
       
   883   } else {
       
   884     si->_addr._base = target_base;
       
   885   }
       
   886   si->_used = size;
       
   887   si->_read_only = read_only;
       
   888   si->_allow_exec = allow_exec;
       
   889 
       
   890   // Use the current 'base' when computing the CRC value and writing out data
       
   891   si->_crc = ClassLoader::crc32(0, base, (jint)size);
       
   892   if (base != NULL) {
  1170   if (base != NULL) {
   893     write_bytes_aligned(base, size);
  1171     write_bytes_aligned(base, size);
   894   }
  1172   }
   895 }
  1173 }
   896 
  1174 
   925 //            ^^^
  1203 //            ^^^
   926 //             |
  1204 //             |
   927 //             +-- gap
  1205 //             +-- gap
   928 size_t FileMapInfo::write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem,
  1206 size_t FileMapInfo::write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem,
   929                                                GrowableArray<ArchiveHeapOopmapInfo> *oopmaps,
  1207                                                GrowableArray<ArchiveHeapOopmapInfo> *oopmaps,
   930                                                int first_region_id, int max_num_regions,
  1208                                                int first_region_id, int max_num_regions) {
   931                                                bool print_log) {
       
   932   assert(max_num_regions <= 2, "Only support maximum 2 memory regions");
  1209   assert(max_num_regions <= 2, "Only support maximum 2 memory regions");
   933 
  1210 
   934   int arr_len = heap_mem == NULL ? 0 : heap_mem->length();
  1211   int arr_len = heap_mem == NULL ? 0 : heap_mem->length();
   935   if(arr_len > max_num_regions) {
  1212   if(arr_len > max_num_regions) {
   936     fail_stop("Unable to write archive heap memory regions: "
  1213     fail_stop("Unable to write archive heap memory regions: "
   950       start = (char*)heap_mem->at(arr_idx).start();
  1227       start = (char*)heap_mem->at(arr_idx).start();
   951       size = heap_mem->at(arr_idx).byte_size();
  1228       size = heap_mem->at(arr_idx).byte_size();
   952       total_size += size;
  1229       total_size += size;
   953     }
  1230     }
   954 
  1231 
   955     if (print_log) {
  1232     log_info(cds)("Archive heap region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes",
   956       log_info(cds)("Archive heap region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes",
  1233                   i, p2i(start), p2i(start + size), size);
   957                     i, p2i(start), p2i(start + size), size);
       
   958     }
       
   959     write_region(i, start, size, false, false);
  1234     write_region(i, start, size, false, false);
   960     if (size > 0) {
  1235     if (size > 0) {
   961       space_at(i)->_oopmap = oopmaps->at(arr_idx)._oopmap;
  1236       space_at(i)->init_oopmap(oopmaps->at(arr_idx)._oopmap,
   962       space_at(i)->_oopmap_size_in_bits = oopmaps->at(arr_idx)._oopmap_size_in_bits;
  1237                                oopmaps->at(arr_idx)._oopmap_size_in_bits);
   963     }
  1238     }
   964   }
  1239   }
   965   return total_size;
  1240   return total_size;
   966 }
  1241 }
   967 
  1242 
   968 // Dump bytes to file -- at the current file position.
  1243 // Dump bytes to file -- at the current file position.
   969 
  1244 
   970 void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) {
  1245 void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) {
   971   if (_file_open) {
  1246   assert(_file_open, "must be");
   972     size_t n = os::write(_fd, buffer, (unsigned int)nbytes);
  1247   size_t n = os::write(_fd, buffer, (unsigned int)nbytes);
   973     if (n != nbytes) {
  1248   if (n != nbytes) {
   974       // If the shared archive is corrupted, close it and remove it.
  1249     // If the shared archive is corrupted, close it and remove it.
   975       close();
  1250     close();
   976       remove(_full_path);
  1251     remove(_full_path);
   977       fail_stop("Unable to write to shared archive file.");
  1252     fail_stop("Unable to write to shared archive file.");
   978     }
       
   979   }
  1253   }
   980   _file_offset += nbytes;
  1254   _file_offset += nbytes;
   981 }
  1255 }
   982 
  1256 
   983 bool FileMapInfo::is_file_position_aligned() const {
  1257 bool FileMapInfo::is_file_position_aligned() const {
   986 }
  1260 }
   987 
  1261 
   988 // Align file position to an allocation unit boundary.
  1262 // Align file position to an allocation unit boundary.
   989 
  1263 
   990 void FileMapInfo::align_file_position() {
  1264 void FileMapInfo::align_file_position() {
       
  1265   assert(_file_open, "must be");
   991   size_t new_file_offset = align_up(_file_offset,
  1266   size_t new_file_offset = align_up(_file_offset,
   992                                          os::vm_allocation_granularity());
  1267                                     os::vm_allocation_granularity());
   993   if (new_file_offset != _file_offset) {
  1268   if (new_file_offset != _file_offset) {
   994     _file_offset = new_file_offset;
  1269     _file_offset = new_file_offset;
   995     if (_file_open) {
  1270     // Seek one byte back from the target and write a byte to insure
   996       // Seek one byte back from the target and write a byte to insure
  1271     // that the written file is the correct length.
   997       // that the written file is the correct length.
  1272     _file_offset -= 1;
   998       _file_offset -= 1;
  1273     seek_to_position(_file_offset);
   999       if (lseek(_fd, (long)_file_offset, SEEK_SET) < 0) {
  1274     char zero = 0;
  1000         fail_stop("Unable to seek.");
  1275     write_bytes(&zero, 1);
  1001       }
       
  1002       char zero = 0;
       
  1003       write_bytes(&zero, 1);
       
  1004     }
       
  1005   }
  1276   }
  1006 }
  1277 }
  1007 
  1278 
  1008 
  1279 
  1009 // Dump bytes to file -- at the current file position.
  1280 // Dump bytes to file -- at the current file position.
  1030 
  1301 
  1031 // JVM/TI RedefineClasses() support:
  1302 // JVM/TI RedefineClasses() support:
  1032 // Remap the shared readonly space to shared readwrite, private.
  1303 // Remap the shared readonly space to shared readwrite, private.
  1033 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
  1304 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
  1034   int idx = MetaspaceShared::ro;
  1305   int idx = MetaspaceShared::ro;
  1035   CDSFileMapRegion* si = space_at(idx);
  1306   FileMapRegion* si = space_at(idx);
  1036   if (!si->_read_only) {
  1307   if (!si->read_only()) {
  1037     // the space is already readwrite so we are done
  1308     // the space is already readwrite so we are done
  1038     return true;
  1309     return true;
  1039   }
  1310   }
  1040   size_t used = si->_used;
  1311   size_t used = si->used();
  1041   size_t size = align_up(used, os::vm_allocation_granularity());
  1312   size_t size = align_up(used, os::vm_allocation_granularity());
  1042   if (!open_for_read()) {
  1313   if (!open_for_read()) {
  1043     return false;
  1314     return false;
  1044   }
  1315   }
  1045   char *addr = region_addr(idx);
  1316   char *addr = region_addr(idx);
  1046   char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
  1317   char *base = os::remap_memory(_fd, _full_path, si->file_offset(),
  1047                                 addr, size, false /* !read_only */,
  1318                                 addr, size, false /* !read_only */,
  1048                                 si->_allow_exec);
  1319                                 si->allow_exec());
  1049   close();
  1320   close();
  1050   // These have to be errors because the shared region is now unmapped.
  1321   // These have to be errors because the shared region is now unmapped.
  1051   if (base == NULL) {
  1322   if (base == NULL) {
  1052     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
  1323     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
  1053     vm_exit(1);
  1324     vm_exit(1);
  1054   }
  1325   }
  1055   if (base != addr) {
  1326   if (base != addr) {
  1056     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
  1327     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
  1057     vm_exit(1);
  1328     vm_exit(1);
  1058   }
  1329   }
  1059   si->_read_only = false;
  1330   si->set_read_only(false);
  1060   return true;
  1331   return true;
  1061 }
  1332 }
  1062 
  1333 
  1063 // Map the whole region at once, assumed to be allocated contiguously.
  1334 // Map the whole region at once, assumed to be allocated contiguously.
  1064 ReservedSpace FileMapInfo::reserve_shared_memory() {
  1335 ReservedSpace FileMapInfo::reserve_shared_memory() {
  1078 
  1349 
  1079   return rs;
  1350   return rs;
  1080 }
  1351 }
  1081 
  1352 
  1082 // Memory map a region in the address space.
  1353 // Memory map a region in the address space.
  1083 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "OptionalData",
  1354 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode",
  1084                                             "String1", "String2", "OpenArchive1", "OpenArchive2" };
  1355                                             "String1", "String2", "OpenArchive1", "OpenArchive2" };
  1085 
  1356 
  1086 char* FileMapInfo::map_regions(int regions[], char* saved_base[], size_t len) {
  1357 char* FileMapInfo::map_regions(int regions[], char* saved_base[], size_t len) {
  1087   char* prev_top = NULL;
  1358   char* prev_top = NULL;
  1088   char* curr_base;
  1359   char* curr_base;
  1092     curr_base = map_region(regions[i], &curr_top);
  1363     curr_base = map_region(regions[i], &curr_top);
  1093     if (curr_base == NULL) {
  1364     if (curr_base == NULL) {
  1094       return NULL;
  1365       return NULL;
  1095     }
  1366     }
  1096     if (i > 0) {
  1367     if (i > 0) {
  1097       // We require that mc->rw->ro->md->od to be laid out consecutively, with no
  1368       // We require that mc->rw->ro->md to be laid out consecutively, with no
  1098       // gaps between them. That way, we can ensure that the OS won't be able to
  1369       // gaps between them. That way, we can ensure that the OS won't be able to
  1099       // allocate any new memory spaces inside _shared_metaspace_{base,top}, which
  1370       // allocate any new memory spaces inside _shared_metaspace_{base,top}, which
  1100       // would mess up the simple comparision in MetaspaceShared::is_in_shared_metaspace().
  1371       // would mess up the simple comparision in MetaspaceShared::is_in_shared_metaspace().
  1101       assert(curr_base == prev_top, "must be");
  1372       assert(curr_base == prev_top, "must be");
  1102     }
  1373     }
  1107   return curr_top;
  1378   return curr_top;
  1108 }
  1379 }
  1109 
  1380 
  1110 char* FileMapInfo::map_region(int i, char** top_ret) {
  1381 char* FileMapInfo::map_region(int i, char** top_ret) {
  1111   assert(!HeapShared::is_heap_region(i), "sanity");
  1382   assert(!HeapShared::is_heap_region(i), "sanity");
  1112   CDSFileMapRegion* si = space_at(i);
  1383   FileMapRegion* si = space_at(i);
  1113   size_t used = si->_used;
  1384   size_t used = si->used();
  1114   size_t alignment = os::vm_allocation_granularity();
  1385   size_t alignment = os::vm_allocation_granularity();
  1115   size_t size = align_up(used, alignment);
  1386   size_t size = align_up(used, alignment);
  1116   char *requested_addr = region_addr(i);
  1387   char *requested_addr = region_addr(i);
  1117 
  1388 
  1118 #ifdef _WINDOWS
  1389 #ifdef _WINDOWS
  1119   // Windows cannot remap read-only shared memory to read-write when required for
  1390   // Windows cannot remap read-only shared memory to read-write when required for
  1120   // RedefineClasses, which is also used by JFR.  Always map windows regions as RW.
  1391   // RedefineClasses, which is also used by JFR.  Always map windows regions as RW.
  1121   si->_read_only = false;
  1392   si->set_read_only(false);
  1122 #else
  1393 #else
  1123   // If a tool agent is in use (debugging enabled), or JFR, we must map the address space RW
  1394   // If a tool agent is in use (debugging enabled), or JFR, we must map the address space RW
  1124   if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space() ||
  1395   if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space() ||
  1125       Arguments::has_jfr_option()) {
  1396       Arguments::has_jfr_option()) {
  1126     si->_read_only = false;
  1397     si->set_read_only(false);
  1127   }
  1398   }
  1128 #endif // _WINDOWS
  1399 #endif // _WINDOWS
  1129 
  1400 
  1130   // map the contents of the CDS archive in this memory
  1401   // map the contents of the CDS archive in this memory
  1131   char *base = os::map_memory(_fd, _full_path, si->_file_offset,
  1402   char *base = os::map_memory(_fd, _full_path, si->file_offset(),
  1132                               requested_addr, size, si->_read_only,
  1403                               requested_addr, size, si->read_only(),
  1133                               si->_allow_exec);
  1404                               si->allow_exec());
  1134   if (base == NULL || base != requested_addr) {
  1405   if (base == NULL || base != requested_addr) {
  1135     fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
  1406     fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
  1136     _memory_mapping_failed = true;
  1407     _memory_mapping_failed = true;
  1137     return NULL;
  1408     return NULL;
  1138   }
  1409   }
  1160   }
  1431   }
  1161   _file_offset += count;
  1432   _file_offset += count;
  1162   return count;
  1433   return count;
  1163 }
  1434 }
  1164 
  1435 
  1165 address FileMapInfo::decode_start_address(CDSFileMapRegion* spc, bool with_current_oop_encoding_mode) {
  1436 address FileMapInfo::decode_start_address(FileMapRegion* spc, bool with_current_oop_encoding_mode) {
  1166   if (with_current_oop_encoding_mode) {
  1437   if (with_current_oop_encoding_mode) {
  1167     return (address)CompressedOops::decode_not_null(offset_of_space(spc));
  1438     return (address)CompressedOops::decode_not_null(spc->offset());
  1168   } else {
  1439   } else {
  1169     return (address)HeapShared::decode_from_archive(offset_of_space(spc));
  1440     return (address)HeapShared::decode_from_archive(spc->offset());
  1170   }
  1441   }
  1171 }
  1442 }
  1172 
  1443 
  1173 static MemRegion *closed_archive_heap_ranges = NULL;
  1444 static MemRegion *closed_archive_heap_ranges = NULL;
  1174 static MemRegion *open_archive_heap_ranges = NULL;
  1445 static MemRegion *open_archive_heap_ranges = NULL;
  1175 static int num_closed_archive_heap_ranges = 0;
  1446 static int num_closed_archive_heap_ranges = 0;
  1176 static int num_open_archive_heap_ranges = 0;
  1447 static int num_open_archive_heap_ranges = 0;
  1177 
  1448 
  1178 #if INCLUDE_CDS_JAVA_HEAP
  1449 #if INCLUDE_CDS_JAVA_HEAP
  1179 bool FileMapInfo::has_heap_regions() {
  1450 bool FileMapInfo::has_heap_regions() {
  1180   return (_header->_space[MetaspaceShared::first_closed_archive_heap_region]._used > 0);
  1451   return (space_at(MetaspaceShared::first_closed_archive_heap_region)->used() > 0);
  1181 }
  1452 }
  1182 
  1453 
  1183 // Returns the address range of the archived heap regions computed using the
  1454 // Returns the address range of the archived heap regions computed using the
  1184 // current oop encoding mode. This range may be different than the one seen at
  1455 // current oop encoding mode. This range may be different than the one seen at
  1185 // dump time due to encoding mode differences. The result is used in determining
  1456 // dump time due to encoding mode differences. The result is used in determining
  1189   address end   = NULL;
  1460   address end   = NULL;
  1190 
  1461 
  1191   for (int i = MetaspaceShared::first_closed_archive_heap_region;
  1462   for (int i = MetaspaceShared::first_closed_archive_heap_region;
  1192            i <= MetaspaceShared::last_valid_region;
  1463            i <= MetaspaceShared::last_valid_region;
  1193            i++) {
  1464            i++) {
  1194     CDSFileMapRegion* si = space_at(i);
  1465     FileMapRegion* si = space_at(i);
  1195     size_t size = si->_used;
  1466     size_t size = si->used();
  1196     if (size > 0) {
  1467     if (size > 0) {
  1197       address s = start_address_as_decoded_with_current_oop_encoding_mode(si);
  1468       address s = start_address_as_decoded_with_current_oop_encoding_mode(si);
  1198       address e = s + size;
  1469       address e = s + size;
  1199       if (start > s) {
  1470       if (start > s) {
  1200         start = s;
  1471         start = s;
  1240     // in the archived subgraphs may be replaced by the ClassFileLoadHook. But that's OK
  1511     // in the archived subgraphs may be replaced by the ClassFileLoadHook. But that's OK
  1241     // because we won't install an archived object subgraph if the klass of any of the
  1512     // because we won't install an archived object subgraph if the klass of any of the
  1242     // referenced objects are replaced. See HeapShared::initialize_from_archived_subgraph().
  1513     // referenced objects are replaced. See HeapShared::initialize_from_archived_subgraph().
  1243   }
  1514   }
  1244 
  1515 
  1245   MemRegion heap_reserved = Universe::heap()->reserved_region();
       
  1246 
       
  1247   log_info(cds)("CDS archive was created with max heap size = " SIZE_FORMAT "M, and the following configuration:",
  1516   log_info(cds)("CDS archive was created with max heap size = " SIZE_FORMAT "M, and the following configuration:",
  1248                 max_heap_size()/M);
  1517                 max_heap_size()/M);
  1249   log_info(cds)("    narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
  1518   log_info(cds)("    narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
  1250                 p2i(narrow_klass_base()), narrow_klass_shift());
  1519                 p2i(narrow_klass_base()), narrow_klass_shift());
  1251   log_info(cds)("    narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
  1520   log_info(cds)("    narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
  1252                 narrow_oop_mode(), p2i(narrow_oop_base()), narrow_oop_shift());
  1521                 narrow_oop_mode(), p2i(narrow_oop_base()), narrow_oop_shift());
  1253 
  1522 
  1254   log_info(cds)("The current max heap size = " SIZE_FORMAT "M, HeapRegion::GrainBytes = " SIZE_FORMAT,
  1523   log_info(cds)("The current max heap size = " SIZE_FORMAT "M, HeapRegion::GrainBytes = " SIZE_FORMAT,
  1255                 heap_reserved.byte_size()/M, HeapRegion::GrainBytes);
  1524                 MaxHeapSize/M, HeapRegion::GrainBytes);
  1256   log_info(cds)("    narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
  1525   log_info(cds)("    narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
  1257                 p2i(CompressedKlassPointers::base()), CompressedKlassPointers::shift());
  1526                 p2i(CompressedKlassPointers::base()), CompressedKlassPointers::shift());
  1258   log_info(cds)("    narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
  1527   log_info(cds)("    narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
  1259                 CompressedOops::mode(), p2i(CompressedOops::base()), CompressedOops::shift());
  1528                 CompressedOops::mode(), p2i(CompressedOops::base()), CompressedOops::shift());
  1260 
  1529 
  1269       narrow_oop_shift() != CompressedOops::shift()) {
  1538       narrow_oop_shift() != CompressedOops::shift()) {
  1270     log_info(cds)("CDS heap data need to be relocated because the archive was created with an incompatible oop encoding mode.");
  1539     log_info(cds)("CDS heap data need to be relocated because the archive was created with an incompatible oop encoding mode.");
  1271     _heap_pointers_need_patching = true;
  1540     _heap_pointers_need_patching = true;
  1272   } else {
  1541   } else {
  1273     MemRegion range = get_heap_regions_range_with_current_oop_encoding_mode();
  1542     MemRegion range = get_heap_regions_range_with_current_oop_encoding_mode();
  1274     if (!heap_reserved.contains(range)) {
  1543     if (!CompressedOops::is_in(range)) {
  1275       log_info(cds)("CDS heap data need to be relocated because");
  1544       log_info(cds)("CDS heap data need to be relocated because");
  1276       log_info(cds)("the desired range " PTR_FORMAT " - "  PTR_FORMAT, p2i(range.start()), p2i(range.end()));
  1545       log_info(cds)("the desired range " PTR_FORMAT " - "  PTR_FORMAT, p2i(range.start()), p2i(range.end()));
  1277       log_info(cds)("is outside of the heap " PTR_FORMAT " - "  PTR_FORMAT, p2i(heap_reserved.start()), p2i(heap_reserved.end()));
  1546       log_info(cds)("is outside of the heap " PTR_FORMAT " - "  PTR_FORMAT, p2i(CompressedOops::begin()), p2i(CompressedOops::end()));
  1278       _heap_pointers_need_patching = true;
  1547       _heap_pointers_need_patching = true;
  1279     }
  1548     }
  1280   }
  1549   }
  1281 
  1550 
  1282   ptrdiff_t delta = 0;
  1551   ptrdiff_t delta = 0;
  1288     //
  1557     //
  1289     // At dump time, the archived heap regions were near the top of the heap.
  1558     // At dump time, the archived heap regions were near the top of the heap.
  1290     // At run time, they may not be inside the heap, so we move them so
  1559     // At run time, they may not be inside the heap, so we move them so
  1291     // that they are now near the top of the runtime time. This can be done by
  1560     // that they are now near the top of the runtime time. This can be done by
  1292     // the simple math of adding the delta as shown above.
  1561     // the simple math of adding the delta as shown above.
  1293     address dumptime_heap_end = (address)_header->_heap_reserved.end();
  1562     address dumptime_heap_end = header()->heap_end();
  1294     address runtime_heap_end = (address)heap_reserved.end();
  1563     address runtime_heap_end = (address)CompressedOops::end();
  1295     delta = runtime_heap_end - dumptime_heap_end;
  1564     delta = runtime_heap_end - dumptime_heap_end;
  1296   }
  1565   }
  1297 
  1566 
  1298   log_info(cds)("CDS heap data relocation delta = " INTX_FORMAT " bytes", delta);
  1567   log_info(cds)("CDS heap data relocation delta = " INTX_FORMAT " bytes", delta);
  1299   HeapShared::init_narrow_oop_decoding(narrow_oop_base() + delta, narrow_oop_shift());
  1568   HeapShared::init_narrow_oop_decoding(narrow_oop_base() + delta, narrow_oop_shift());
  1300 
  1569 
  1301   CDSFileMapRegion* si = space_at(MetaspaceShared::first_closed_archive_heap_region);
  1570   FileMapRegion* si = space_at(MetaspaceShared::first_closed_archive_heap_region);
  1302   address relocated_closed_heap_region_bottom = start_address_as_decoded_from_archive(si);
  1571   address relocated_closed_heap_region_bottom = start_address_as_decoded_from_archive(si);
  1303   if (!is_aligned(relocated_closed_heap_region_bottom, HeapRegion::GrainBytes)) {
  1572   if (!is_aligned(relocated_closed_heap_region_bottom, HeapRegion::GrainBytes)) {
  1304     // Align the bottom of the closed archive heap regions at G1 region boundary.
  1573     // Align the bottom of the closed archive heap regions at G1 region boundary.
  1305     // This will avoid the situation where the highest open region and the lowest
  1574     // This will avoid the situation where the highest open region and the lowest
  1306     // closed region sharing the same G1 region. Otherwise we will fail to map the
  1575     // closed region sharing the same G1 region. Otherwise we will fail to map the
  1351 }
  1620 }
  1352 
  1621 
  1353 bool FileMapInfo::map_heap_data(MemRegion **heap_mem, int first,
  1622 bool FileMapInfo::map_heap_data(MemRegion **heap_mem, int first,
  1354                                 int max, int* num, bool is_open_archive) {
  1623                                 int max, int* num, bool is_open_archive) {
  1355   MemRegion * regions = new MemRegion[max];
  1624   MemRegion * regions = new MemRegion[max];
  1356   CDSFileMapRegion* si;
  1625   FileMapRegion* si;
  1357   int region_num = 0;
  1626   int region_num = 0;
  1358 
  1627 
  1359   for (int i = first;
  1628   for (int i = first;
  1360            i < first + max; i++) {
  1629            i < first + max; i++) {
  1361     si = space_at(i);
  1630     si = space_at(i);
  1362     size_t size = si->_used;
  1631     size_t size = si->used();
  1363     if (size > 0) {
  1632     if (size > 0) {
  1364       HeapWord* start = (HeapWord*)start_address_as_decoded_from_archive(si);
  1633       HeapWord* start = (HeapWord*)start_address_as_decoded_from_archive(si);
  1365       regions[region_num] = MemRegion(start, size / HeapWordSize);
  1634       regions[region_num] = MemRegion(start, size / HeapWordSize);
  1366       region_num ++;
  1635       region_num ++;
  1367       log_info(cds)("Trying to map heap data: region[%d] at " INTPTR_FORMAT ", size = " SIZE_FORMAT_W(8) " bytes",
  1636       log_info(cds)("Trying to map heap data: region[%d] at " INTPTR_FORMAT ", size = " SIZE_FORMAT_W(8) " bytes",
  1390   // for mapped regions as they are part of the reserved java heap, which is
  1659   // for mapped regions as they are part of the reserved java heap, which is
  1391   // already recorded.
  1660   // already recorded.
  1392   for (int i = 0; i < region_num; i++) {
  1661   for (int i = 0; i < region_num; i++) {
  1393     si = space_at(first + i);
  1662     si = space_at(first + i);
  1394     char* addr = (char*)regions[i].start();
  1663     char* addr = (char*)regions[i].start();
  1395     char* base = os::map_memory(_fd, _full_path, si->_file_offset,
  1664     char* base = os::map_memory(_fd, _full_path, si->file_offset(),
  1396                                 addr, regions[i].byte_size(), si->_read_only,
  1665                                 addr, regions[i].byte_size(), si->read_only(),
  1397                                 si->_allow_exec);
  1666                                 si->allow_exec());
  1398     if (base == NULL || base != addr) {
  1667     if (base == NULL || base != addr) {
  1399       // dealloc the regions from java heap
  1668       // dealloc the regions from java heap
  1400       dealloc_archive_heap_regions(regions, region_num, is_open_archive);
  1669       dealloc_archive_heap_regions(regions, region_num, is_open_archive);
  1401       log_info(cds)("UseSharedSpaces: Unable to map at required address in java heap. "
  1670       log_info(cds)("UseSharedSpaces: Unable to map at required address in java heap. "
  1402                     INTPTR_FORMAT ", size = " SIZE_FORMAT " bytes",
  1671                     INTPTR_FORMAT ", size = " SIZE_FORMAT " bytes",
  1403                     p2i(addr), regions[i].byte_size());
  1672                     p2i(addr), regions[i].byte_size());
  1404       return false;
  1673       return false;
  1405     }
  1674     }
  1406 
  1675 
  1407     if (VerifySharedSpaces && !region_crc_check(addr, regions[i].byte_size(), si->_crc)) {
  1676     if (VerifySharedSpaces && !region_crc_check(addr, regions[i].byte_size(), si->crc())) {
  1408       // dealloc the regions from java heap
  1677       // dealloc the regions from java heap
  1409       dealloc_archive_heap_regions(regions, region_num, is_open_archive);
  1678       dealloc_archive_heap_regions(regions, region_num, is_open_archive);
  1410       log_info(cds)("UseSharedSpaces: mapped heap regions are corrupt");
  1679       log_info(cds)("UseSharedSpaces: mapped heap regions are corrupt");
  1411       return false;
  1680       return false;
  1412     }
  1681     }
  1433 }
  1702 }
  1434 
  1703 
  1435 void FileMapInfo::patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges,
  1704 void FileMapInfo::patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges,
  1436                                                         int first_region_idx) {
  1705                                                         int first_region_idx) {
  1437   for (int i=0; i<num_ranges; i++) {
  1706   for (int i=0; i<num_ranges; i++) {
  1438     CDSFileMapRegion* si = space_at(i + first_region_idx);
  1707     FileMapRegion* si = space_at(i + first_region_idx);
  1439     HeapShared::patch_archived_heap_embedded_pointers(ranges[i], (address)si->_oopmap,
  1708     HeapShared::patch_archived_heap_embedded_pointers(ranges[i], (address)si->oopmap(),
  1440                                                       si->_oopmap_size_in_bits);
  1709                                                       si->oopmap_size_in_bits());
  1441   }
  1710   }
  1442 }
  1711 }
  1443 
  1712 
  1444 // This internally allocates objects using SystemDictionary::Object_klass(), so it
  1713 // This internally allocates objects using SystemDictionary::Object_klass(), so it
  1445 // must be called after the well-known classes are resolved.
  1714 // must be called after the well-known classes are resolved.
  1479   return true;
  1748   return true;
  1480 }
  1749 }
  1481 
  1750 
  1482 bool FileMapInfo::verify_region_checksum(int i) {
  1751 bool FileMapInfo::verify_region_checksum(int i) {
  1483   assert(VerifySharedSpaces, "sanity");
  1752   assert(VerifySharedSpaces, "sanity");
  1484 
  1753   size_t sz = space_at(i)->used();
  1485   size_t sz = space_at(i)->_used;
       
  1486 
  1754 
  1487   if (sz == 0) {
  1755   if (sz == 0) {
  1488     return true; // no data
  1756     return true; // no data
  1489   }
  1757   } else {
  1490 
  1758     return region_crc_check(region_addr(i), sz, space_at(i)->crc());
  1491   return region_crc_check(region_addr(i), sz, space_at(i)->_crc);
  1759   }
  1492 }
  1760 }
  1493 
  1761 
  1494 void FileMapInfo::unmap_regions(int regions[], char* saved_base[], size_t len) {
  1762 void FileMapInfo::unmap_regions(int regions[], char* saved_base[], size_t len) {
  1495   for (int i = 0; i < (int)len; i++) {
  1763   for (int i = 0; i < (int)len; i++) {
  1496     if (saved_base[i] != NULL) {
  1764     if (saved_base[i] != NULL) {
  1501 
  1769 
  1502 // Unmap a memory region in the address space.
  1770 // Unmap a memory region in the address space.
  1503 
  1771 
  1504 void FileMapInfo::unmap_region(int i) {
  1772 void FileMapInfo::unmap_region(int i) {
  1505   assert(!HeapShared::is_heap_region(i), "sanity");
  1773   assert(!HeapShared::is_heap_region(i), "sanity");
  1506   CDSFileMapRegion* si = space_at(i);
  1774   FileMapRegion* si = space_at(i);
  1507   size_t used = si->_used;
  1775   size_t used = si->used();
  1508   size_t size = align_up(used, os::vm_allocation_granularity());
  1776   size_t size = align_up(used, os::vm_allocation_granularity());
  1509 
  1777 
  1510   if (used == 0) {
  1778   if (used == 0) {
  1511     return;
  1779     return;
  1512   }
  1780   }
  1531 FileMapInfo* FileMapInfo::_dynamic_archive_info = NULL;
  1799 FileMapInfo* FileMapInfo::_dynamic_archive_info = NULL;
  1532 bool FileMapInfo::_heap_pointers_need_patching = false;
  1800 bool FileMapInfo::_heap_pointers_need_patching = false;
  1533 SharedPathTable FileMapInfo::_shared_path_table;
  1801 SharedPathTable FileMapInfo::_shared_path_table;
  1534 bool FileMapInfo::_validating_shared_path_table = false;
  1802 bool FileMapInfo::_validating_shared_path_table = false;
  1535 bool FileMapInfo::_memory_mapping_failed = false;
  1803 bool FileMapInfo::_memory_mapping_failed = false;
       
  1804 GrowableArray<const char*>* FileMapInfo::_non_existent_class_paths = NULL;
  1536 
  1805 
  1537 // Open the shared archive file, read and validate the header
  1806 // Open the shared archive file, read and validate the header
  1538 // information (version, boot classpath, etc.).  If initialization
  1807 // information (version, boot classpath, etc.).  If initialization
  1539 // fails, shared spaces are disabled and the file is closed. [See
  1808 // fails, shared spaces are disabled and the file is closed. [See
  1540 // fail_continue.]
  1809 // fail_continue.]
  1541 //
  1810 //
  1542 // Validation of the archive is done in two steps:
  1811 // Validation of the archive is done in two steps:
  1543 //
  1812 //
  1544 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
  1813 // [1] validate_header() - done here.
  1545 // [2] validate_shared_path_table - this is done later, because the table is in the RW
  1814 // [2] validate_shared_path_table - this is done later, because the table is in the RW
  1546 //     region of the archive, which is not mapped yet.
  1815 //     region of the archive, which is not mapped yet.
  1547 bool FileMapInfo::initialize(bool is_static) {
  1816 bool FileMapInfo::initialize(bool is_static) {
  1548   assert(UseSharedSpaces, "UseSharedSpaces expected.");
  1817   assert(UseSharedSpaces, "UseSharedSpaces expected.");
  1549 
  1818 
  1559   if (!open_for_read()) {
  1828   if (!open_for_read()) {
  1560     return false;
  1829     return false;
  1561   }
  1830   }
  1562 
  1831 
  1563   init_from_file(_fd, is_static);
  1832   init_from_file(_fd, is_static);
  1564   if (!validate_header(is_static)) {
  1833   // UseSharedSpaces could be disabled if the checking of some of the header fields in
       
  1834   // init_from_file has failed.
       
  1835   if (!UseSharedSpaces || !validate_header(is_static)) {
  1565     return false;
  1836     return false;
  1566   }
  1837   }
  1567   return true;
  1838   return true;
  1568 }
  1839 }
  1569 
  1840 
  1570 char* FileMapInfo::region_addr(int idx) {
  1841 char* FileMapInfo::region_addr(int idx) {
  1571   CDSFileMapRegion* si = space_at(idx);
  1842   FileMapRegion* si = space_at(idx);
  1572   if (HeapShared::is_heap_region(idx)) {
  1843   if (HeapShared::is_heap_region(idx)) {
  1573     assert(DumpSharedSpaces, "The following doesn't work at runtime");
  1844     assert(DumpSharedSpaces, "The following doesn't work at runtime");
  1574     return si->_used > 0 ?
  1845     return si->used() > 0 ?
  1575           (char*)start_address_as_decoded_with_current_oop_encoding_mode(si) : NULL;
  1846           (char*)start_address_as_decoded_with_current_oop_encoding_mode(si) : NULL;
  1576   } else {
  1847   } else {
  1577     return si->_addr._base;
  1848     return si->base();
  1578   }
  1849   }
  1579 }
  1850 }
  1580 
  1851 
  1581 int FileMapHeader::compute_crc() {
  1852 int FileMapHeader::compute_crc() {
  1582   char* start = (char*)this;
  1853   char* start = (char*)this;
  1587   return crc;
  1858   return crc;
  1588 }
  1859 }
  1589 
  1860 
  1590 // This function should only be called during run time with UseSharedSpaces enabled.
  1861 // This function should only be called during run time with UseSharedSpaces enabled.
  1591 bool FileMapHeader::validate() {
  1862 bool FileMapHeader::validate() {
  1592   if (VerifySharedSpaces && compute_crc() != _crc) {
  1863 
  1593     FileMapInfo::fail_continue("Header checksum verification failed.");
       
  1594     return false;
       
  1595   }
       
  1596 
       
  1597   if (!Arguments::has_jimage()) {
       
  1598     FileMapInfo::fail_continue("The shared archive file cannot be used with an exploded module build.");
       
  1599     return false;
       
  1600   }
       
  1601 
       
  1602   if (_version != CURRENT_CDS_ARCHIVE_VERSION) {
       
  1603     FileMapInfo::fail_continue("The shared archive file is the wrong version.");
       
  1604     return false;
       
  1605   }
       
  1606   if (_magic != CDS_ARCHIVE_MAGIC && _magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
       
  1607     FileMapInfo::fail_continue("The shared archive file has a bad magic number.");
       
  1608     return false;
       
  1609   }
       
  1610   char header_version[JVM_IDENT_MAX];
       
  1611   get_header_version(header_version);
       
  1612   if (strncmp(_jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) {
       
  1613     log_info(class, path)("expected: %s", header_version);
       
  1614     log_info(class, path)("actual:   %s", _jvm_ident);
       
  1615     FileMapInfo::fail_continue("The shared archive file was created by a different"
       
  1616                   " version or build of HotSpot");
       
  1617     return false;
       
  1618   }
       
  1619   if (_obj_alignment != ObjectAlignmentInBytes) {
  1864   if (_obj_alignment != ObjectAlignmentInBytes) {
  1620     FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d"
  1865     FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d"
  1621                   " does not equal the current ObjectAlignmentInBytes of " INTX_FORMAT ".",
  1866                   " does not equal the current ObjectAlignmentInBytes of " INTX_FORMAT ".",
  1622                   _obj_alignment, ObjectAlignmentInBytes);
  1867                   _obj_alignment, ObjectAlignmentInBytes);
  1623     return false;
  1868     return false;
  1667 
  1912 
  1668   return true;
  1913   return true;
  1669 }
  1914 }
  1670 
  1915 
  1671 bool FileMapInfo::validate_header(bool is_static) {
  1916 bool FileMapInfo::validate_header(bool is_static) {
  1672   bool status = _header->validate();
  1917   return header()->validate();
  1673 
       
  1674   if (status) {
       
  1675     if (!ClassLoader::check_shared_paths_misc_info(_paths_misc_info, _header->_paths_misc_info_size, is_static)) {
       
  1676       if (!PrintSharedArchiveAndExit) {
       
  1677         fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)");
       
  1678         status = false;
       
  1679       }
       
  1680     }
       
  1681   }
       
  1682 
       
  1683   if (_paths_misc_info != NULL) {
       
  1684     FREE_C_HEAP_ARRAY(char, _paths_misc_info);
       
  1685     _paths_misc_info = NULL;
       
  1686   }
       
  1687   return status;
       
  1688 }
  1918 }
  1689 
  1919 
  1690 // Check if a given address is within one of the shared regions
  1920 // Check if a given address is within one of the shared regions
  1691 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
  1921 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
  1692   assert(idx == MetaspaceShared::ro ||
  1922   assert(idx == MetaspaceShared::ro ||
  1693          idx == MetaspaceShared::rw ||
  1923          idx == MetaspaceShared::rw ||
  1694          idx == MetaspaceShared::mc ||
  1924          idx == MetaspaceShared::mc ||
  1695          idx == MetaspaceShared::md, "invalid region index");
  1925          idx == MetaspaceShared::md, "invalid region index");
  1696   char* base = region_addr(idx);
  1926   char* base = region_addr(idx);
  1697   if (p >= base && p < base + space_at(idx)->_used) {
  1927   if (p >= base && p < base + space_at(idx)->used()) {
  1698     return true;
  1928     return true;
  1699   }
  1929   }
  1700   return false;
  1930   return false;
  1701 }
  1931 }
  1702 
  1932 
  1710     for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) {
  1940     for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) {
  1711       if (!HeapShared::is_heap_region(i)) {
  1941       if (!HeapShared::is_heap_region(i)) {
  1712         char *addr = map_info->region_addr(i);
  1942         char *addr = map_info->region_addr(i);
  1713         if (addr != NULL) {
  1943         if (addr != NULL) {
  1714           map_info->unmap_region(i);
  1944           map_info->unmap_region(i);
  1715           map_info->space_at(i)->_addr._base = NULL;
  1945           map_info->space_at(i)->mark_invalid();
  1716         }
  1946         }
  1717       }
  1947       }
  1718     }
  1948     }
  1719     // Dealloc the archive heap regions only without unmapping. The regions are part
  1949     // Dealloc the archive heap regions only without unmapping. The regions are part
  1720     // of the java heap. Unmapping of the heap regions are managed by GC.
  1950     // of the java heap. Unmapping of the heap regions are managed by GC.
  1734 
  1964 
  1735 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
  1965 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
  1736   ClassPathEntry* ent = _classpath_entries_for_jvmti[i];
  1966   ClassPathEntry* ent = _classpath_entries_for_jvmti[i];
  1737   if (ent == NULL) {
  1967   if (ent == NULL) {
  1738     if (i == 0) {
  1968     if (i == 0) {
  1739       ent = ClassLoader:: get_jrt_entry();
  1969       ent = ClassLoader::get_jrt_entry();
  1740       assert(ent != NULL, "must be");
  1970       assert(ent != NULL, "must be");
  1741     } else {
  1971     } else {
  1742       SharedClassPathEntry* scpe = shared_path(i);
  1972       SharedClassPathEntry* scpe = shared_path(i);
  1743       assert(scpe->is_jar(), "must be"); // other types of scpe will not produce archived classes
  1973       assert(scpe->is_jar(), "must be"); // other types of scpe will not produce archived classes
  1744 
  1974 
  1747       if (os::stat(path, &st) != 0) {
  1977       if (os::stat(path, &st) != 0) {
  1748         char *msg = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, strlen(path) + 128); ;
  1978         char *msg = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, strlen(path) + 128); ;
  1749         jio_snprintf(msg, strlen(path) + 127, "error in opening JAR file %s", path);
  1979         jio_snprintf(msg, strlen(path) + 127, "error in opening JAR file %s", path);
  1750         THROW_MSG_(vmSymbols::java_io_IOException(), msg, NULL);
  1980         THROW_MSG_(vmSymbols::java_io_IOException(), msg, NULL);
  1751       } else {
  1981       } else {
  1752         ent = ClassLoader::create_class_path_entry(path, &st, /*throw_exception=*/true, false, CHECK_NULL);
  1982         ent = ClassLoader::create_class_path_entry(path, &st, /*throw_exception=*/true, false, false, CHECK_NULL);
  1753       }
  1983       }
  1754     }
  1984     }
  1755 
  1985 
  1756     MutexLocker mu(CDSClassFileStream_lock, THREAD);
  1986     MutexLocker mu(CDSClassFileStream_lock, THREAD);
  1757     if (_classpath_entries_for_jvmti[i] == NULL) {
  1987     if (_classpath_entries_for_jvmti[i] == NULL) {