src/hotspot/share/memory/filemap.cpp
changeset 54927 1512d88b24c6
parent 54838 d7819bedfaaf
child 55296 357c9dcb6eb9
child 58678 9cf78a70fa4f
equal deleted inserted replaced
54926:d4e7ccaf1445 54927:1512d88b24c6
    32 #include "classfile/systemDictionaryShared.hpp"
    32 #include "classfile/systemDictionaryShared.hpp"
    33 #include "classfile/altHashing.hpp"
    33 #include "classfile/altHashing.hpp"
    34 #include "logging/log.hpp"
    34 #include "logging/log.hpp"
    35 #include "logging/logStream.hpp"
    35 #include "logging/logStream.hpp"
    36 #include "logging/logMessage.hpp"
    36 #include "logging/logMessage.hpp"
       
    37 #include "memory/dynamicArchive.hpp"
    37 #include "memory/filemap.hpp"
    38 #include "memory/filemap.hpp"
    38 #include "memory/heapShared.inline.hpp"
    39 #include "memory/heapShared.inline.hpp"
    39 #include "memory/iterator.inline.hpp"
    40 #include "memory/iterator.inline.hpp"
    40 #include "memory/metadataFactory.hpp"
    41 #include "memory/metadataFactory.hpp"
    41 #include "memory/metaspaceClosure.hpp"
    42 #include "memory/metaspaceClosure.hpp"
    42 #include "memory/metaspaceShared.hpp"
    43 #include "memory/metaspaceShared.hpp"
    43 #include "memory/oopFactory.hpp"
    44 #include "memory/oopFactory.hpp"
    44 #include "memory/universe.hpp"
    45 #include "memory/universe.hpp"
       
    46 #include "oops/compressedOops.hpp"
    45 #include "oops/compressedOops.inline.hpp"
    47 #include "oops/compressedOops.inline.hpp"
    46 #include "oops/objArrayOop.hpp"
    48 #include "oops/objArrayOop.hpp"
    47 #include "oops/oop.inline.hpp"
    49 #include "oops/oop.inline.hpp"
    48 #include "prims/jvmtiExport.hpp"
    50 #include "prims/jvmtiExport.hpp"
    49 #include "runtime/arguments.hpp"
    51 #include "runtime/arguments.hpp"
    99 // If we continue, then disable shared spaces and close the file.
   101 // If we continue, then disable shared spaces and close the file.
   100 
   102 
   101 void FileMapInfo::fail_continue(const char *msg, ...) {
   103 void FileMapInfo::fail_continue(const char *msg, ...) {
   102   va_list ap;
   104   va_list ap;
   103   va_start(ap, msg);
   105   va_start(ap, msg);
   104   MetaspaceShared::set_archive_loading_failed();
   106   if (_dynamic_archive_info == NULL) {
       
   107     MetaspaceShared::set_archive_loading_failed();
       
   108   } else {
       
   109     // _dynamic_archive_info has been setup after mapping the base archive
       
   110     DynamicArchive::disable();
       
   111   }
   105   if (PrintSharedArchiveAndExit && _validating_shared_path_table) {
   112   if (PrintSharedArchiveAndExit && _validating_shared_path_table) {
   106     // If we are doing PrintSharedArchiveAndExit and some of the classpath entries
   113     // If we are doing PrintSharedArchiveAndExit and some of the classpath entries
   107     // do not validate, we can still continue "limping" to validate the remaining
   114     // do not validate, we can still continue "limping" to validate the remaining
   108     // entries. No need to quit.
   115     // entries. No need to quit.
   109     tty->print("[");
   116     tty->print("[");
   118         LogStream ls(Log(cds)::info());
   125         LogStream ls(Log(cds)::info());
   119         ls.print("UseSharedSpaces: ");
   126         ls.print("UseSharedSpaces: ");
   120         ls.vprint_cr(msg, ap);
   127         ls.vprint_cr(msg, ap);
   121       }
   128       }
   122     }
   129     }
   123     UseSharedSpaces = false;
   130     if (_dynamic_archive_info == NULL) {
   124     assert(current_info() != NULL, "singleton must be registered");
   131       UseSharedSpaces = false;
   125     current_info()->close();
   132       assert(current_info() != NULL, "singleton must be registered");
       
   133       current_info()->close();
       
   134     } else {
       
   135       // We are failing when loading the top archive, but the base archive should
       
   136       // continue to work.
       
   137       log_warning(cds, dynamic)("Unable to use shared archive. The top archive failed to load: %s", _dynamic_archive_info->_full_path);
       
   138     }
   126   }
   139   }
   127   va_end(ap);
   140   va_end(ap);
   128 }
   141 }
   129 
   142 
   130 // Fill in the fileMapInfo structure with data about this VM instance.
   143 // Fill in the fileMapInfo structure with data about this VM instance.
   157     sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash);
   170     sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash);
   158     header_version[JVM_IDENT_MAX-1] = 0;  // Null terminate.
   171     header_version[JVM_IDENT_MAX-1] = 0;  // Null terminate.
   159   }
   172   }
   160 }
   173 }
   161 
   174 
   162 FileMapInfo::FileMapInfo() {
   175 FileMapInfo::FileMapInfo(bool is_static) {
   163   assert(_current_info == NULL, "must be singleton"); // not thread safe
       
   164   _current_info = this;
       
   165   memset((void*)this, 0, sizeof(FileMapInfo));
   176   memset((void*)this, 0, sizeof(FileMapInfo));
       
   177   _is_static = is_static;
       
   178   size_t header_size;
       
   179   if (is_static) {
       
   180     assert(_current_info == NULL, "must be singleton"); // not thread safe
       
   181     _current_info = this;
       
   182     header_size = sizeof(FileMapHeader);
       
   183   } else {
       
   184     assert(_dynamic_archive_info == NULL, "must be singleton"); // not thread safe
       
   185     _dynamic_archive_info = this;
       
   186     header_size = sizeof(DynamicArchiveHeader);
       
   187   }
       
   188   _header = (FileMapHeader*)os::malloc(header_size, mtInternal);
       
   189   memset((void*)_header, 0, header_size);
       
   190   _header->_header_size = header_size;
       
   191   _header->_version = INVALID_CDS_ARCHIVE_VERSION;
       
   192   _header->_has_platform_or_app_classes = true;
   166   _file_offset = 0;
   193   _file_offset = 0;
   167   _file_open = false;
   194   _file_open = false;
   168   _header = (FileMapHeader*)os::malloc(sizeof(FileMapHeader), mtInternal);
       
   169   _header->_version = INVALID_CDS_ARCHIVE_VERSION;
       
   170   _header->_has_platform_or_app_classes = true;
       
   171 }
   195 }
   172 
   196 
   173 FileMapInfo::~FileMapInfo() {
   197 FileMapInfo::~FileMapInfo() {
   174   assert(_current_info == this, "must be singleton"); // not thread safe
   198   if (_is_static) {
   175   _current_info = NULL;
   199     assert(_current_info == this, "must be singleton"); // not thread safe
       
   200     _current_info = NULL;
       
   201   } else {
       
   202     assert(_dynamic_archive_info == this, "must be singleton"); // not thread safe
       
   203     _dynamic_archive_info = NULL;
       
   204   }
   176 }
   205 }
   177 
   206 
   178 void FileMapInfo::populate_header(size_t alignment) {
   207 void FileMapInfo::populate_header(size_t alignment) {
   179   _header->populate(this, alignment);
   208   _header->populate(this, alignment);
   180 }
   209 }
   181 
   210 
   182 void FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
   211 void FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
   183   _magic = CDS_ARCHIVE_MAGIC;
   212   if (DynamicDumpSharedSpaces) {
       
   213     _magic = CDS_DYNAMIC_ARCHIVE_MAGIC;
       
   214   } else {
       
   215     _magic = CDS_ARCHIVE_MAGIC;
       
   216   }
   184   _version = CURRENT_CDS_ARCHIVE_VERSION;
   217   _version = CURRENT_CDS_ARCHIVE_VERSION;
   185   _alignment = alignment;
   218   _alignment = alignment;
   186   _obj_alignment = ObjectAlignmentInBytes;
   219   _obj_alignment = ObjectAlignmentInBytes;
   187   _compact_strings = CompactStrings;
   220   _compact_strings = CompactStrings;
   188   _narrow_oop_mode = CompressedOops::mode();
   221   _narrow_oop_mode = CompressedOops::mode();
   189   _narrow_oop_base = CompressedOops::base();
   222   _narrow_oop_base = CompressedOops::base();
   190   _narrow_oop_shift = CompressedOops::shift();
   223   _narrow_oop_shift = CompressedOops::shift();
   191   _max_heap_size = MaxHeapSize;
   224   _max_heap_size = MaxHeapSize;
   192   _narrow_klass_base = CompressedKlassPointers::base();
   225   _narrow_klass_base = CompressedKlassPointers::base();
   193   _narrow_klass_shift = CompressedKlassPointers::shift();
   226   _narrow_klass_shift = CompressedKlassPointers::shift();
   194   _shared_path_table_size = mapinfo->_shared_path_table_size;
       
   195   _shared_path_table = mapinfo->_shared_path_table;
   227   _shared_path_table = mapinfo->_shared_path_table;
   196   _shared_path_entry_size = mapinfo->_shared_path_entry_size;
       
   197   if (HeapShared::is_heap_object_archiving_allowed()) {
   228   if (HeapShared::is_heap_object_archiving_allowed()) {
   198     _heap_reserved = Universe::heap()->reserved_region();
   229     _heap_reserved = Universe::heap()->reserved_region();
   199   }
   230   }
   200 
   231 
   201   // The following fields are for sanity checks for whether this archive
   232   // The following fields are for sanity checks for whether this archive
   206   get_header_version(_jvm_ident);
   237   get_header_version(_jvm_ident);
   207 
   238 
   208   ClassLoaderExt::finalize_shared_paths_misc_info();
   239   ClassLoaderExt::finalize_shared_paths_misc_info();
   209   _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
   240   _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
   210   _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
   241   _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
       
   242   _num_module_paths = ClassLoader::num_module_path_entries();
   211   _max_used_path_index = ClassLoaderExt::max_used_path_index();
   243   _max_used_path_index = ClassLoaderExt::max_used_path_index();
   212 
   244 
   213   _verify_local = BytecodeVerificationLocal;
   245   _verify_local = BytecodeVerificationLocal;
   214   _verify_remote = BytecodeVerificationRemote;
   246   _verify_remote = BytecodeVerificationRemote;
   215   _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
   247   _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
   216   _shared_base_address = SharedBaseAddress;
   248   _shared_base_address = SharedBaseAddress;
   217   _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;
   249   _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;
       
   250   // the following 2 fields will be set in write_header for dynamic archive header
       
   251   _base_archive_name_size = 0;
       
   252   _base_archive_is_default = false;
   218 }
   253 }
   219 
   254 
   220 void SharedClassPathEntry::init(const char* name, bool is_modules_image, TRAPS) {
   255 void SharedClassPathEntry::init(const char* name, bool is_modules_image, TRAPS) {
   221   assert(DumpSharedSpaces, "dump time only");
   256   assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only");
   222   _timestamp = 0;
   257   _timestamp = 0;
   223   _filesize  = 0;
   258   _filesize  = 0;
   224 
   259 
   225   struct stat st;
   260   struct stat st;
   226   if (os::stat(name, &st) == 0) {
   261   if (os::stat(name, &st) == 0) {
   295 
   330 
   296   if (PrintSharedArchiveAndExit && !ok) {
   331   if (PrintSharedArchiveAndExit && !ok) {
   297     // If PrintSharedArchiveAndExit is enabled, don't report failure to the
   332     // If PrintSharedArchiveAndExit is enabled, don't report failure to the
   298     // caller. Please see above comments for more details.
   333     // caller. Please see above comments for more details.
   299     ok = true;
   334     ok = true;
       
   335     MetaspaceShared::set_archive_loading_failed();
   300   }
   336   }
   301   return ok;
   337   return ok;
   302 }
   338 }
   303 
   339 
   304 void SharedClassPathEntry::metaspace_pointers_do(MetaspaceClosure* it) {
   340 void SharedClassPathEntry::metaspace_pointers_do(MetaspaceClosure* it) {
   305   it->push(&_name);
   341   it->push(&_name);
   306   it->push(&_manifest);
   342   it->push(&_manifest);
   307 }
   343 }
   308 
   344 
   309 void FileMapInfo::allocate_shared_path_table() {
   345 void SharedPathTable::metaspace_pointers_do(MetaspaceClosure* it) {
   310   assert(DumpSharedSpaces, "Sanity");
   346   it->push(&_table);
   311 
   347   for (int i=0; i<_size; i++) {
   312   Thread* THREAD = Thread::current();
   348     path_at(i)->metaspace_pointers_do(it);
   313   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
   349   }
   314   ClassPathEntry* jrt = ClassLoader::get_jrt_entry();
   350 }
   315 
   351 
   316   assert(jrt != NULL,
   352 void SharedPathTable::dumptime_init(ClassLoaderData* loader_data, Thread* THREAD) {
   317          "No modular java runtime image present when allocating the CDS classpath entry table");
   353   size_t entry_size = sizeof(SharedClassPathEntry);
   318 
       
   319   size_t entry_size = sizeof(SharedClassPathEntry); // assert ( should be 8 byte aligned??)
       
   320   int num_boot_classpath_entries = ClassLoader::num_boot_classpath_entries();
   354   int num_boot_classpath_entries = ClassLoader::num_boot_classpath_entries();
   321   int num_app_classpath_entries = ClassLoader::num_app_classpath_entries();
   355   int num_app_classpath_entries = ClassLoader::num_app_classpath_entries();
   322   int num_module_path_entries = ClassLoader::num_module_path_entries();
   356   int num_module_path_entries = ClassLoader::num_module_path_entries();
   323   int num_entries = num_boot_classpath_entries + num_app_classpath_entries + num_module_path_entries;
   357   int num_entries = num_boot_classpath_entries + num_app_classpath_entries + num_module_path_entries;
   324   size_t bytes = entry_size * num_entries;
   358   size_t bytes = entry_size * num_entries;
   325 
   359 
   326   _shared_path_table = MetadataFactory::new_array<u8>(loader_data, (int)(bytes + 7 / 8), THREAD);
   360   _table = MetadataFactory::new_array<u8>(loader_data, (int)(bytes + 7 / 8), THREAD);
   327   _shared_path_table_size = num_entries;
   361   _size = num_entries;
   328   _shared_path_entry_size = entry_size;
   362 }
       
   363 
       
   364 void FileMapInfo::allocate_shared_path_table() {
       
   365   assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "Sanity");
       
   366 
       
   367   Thread* THREAD = Thread::current();
       
   368   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
       
   369   ClassPathEntry* jrt = ClassLoader::get_jrt_entry();
       
   370 
       
   371   assert(jrt != NULL,
       
   372          "No modular java runtime image present when allocating the CDS classpath entry table");
       
   373 
       
   374   _shared_path_table.dumptime_init(loader_data, THREAD);
   329 
   375 
   330   // 1. boot class path
   376   // 1. boot class path
   331   int i = 0;
   377   int i = 0;
   332   ClassPathEntry* cpe = jrt;
   378   ClassPathEntry* cpe = jrt;
   333   while (cpe != NULL) {
   379   while (cpe != NULL) {
   341       update_shared_classpath(cpe, ent, THREAD);
   387       update_shared_classpath(cpe, ent, THREAD);
   342     }
   388     }
   343     cpe = ClassLoader::get_next_boot_classpath_entry(cpe);
   389     cpe = ClassLoader::get_next_boot_classpath_entry(cpe);
   344     i++;
   390     i++;
   345   }
   391   }
   346   assert(i == num_boot_classpath_entries,
   392   assert(i == ClassLoader::num_boot_classpath_entries(),
   347          "number of boot class path entry mismatch");
   393          "number of boot class path entry mismatch");
   348 
   394 
   349   // 2. app class path
   395   // 2. app class path
   350   ClassPathEntry *acpe = ClassLoader::app_classpath_entries();
   396   ClassPathEntry *acpe = ClassLoader::app_classpath_entries();
   351   while (acpe != NULL) {
   397   while (acpe != NULL) {
   367     EXCEPTION_MARK;
   413     EXCEPTION_MARK;
   368     update_shared_classpath(mpe, ent, THREAD);
   414     update_shared_classpath(mpe, ent, THREAD);
   369     mpe = mpe->next();
   415     mpe = mpe->next();
   370     i++;
   416     i++;
   371   }
   417   }
   372   assert(i == num_entries, "number of shared path entry mismatch");
   418   assert(i == _shared_path_table.size(), "number of shared path entry mismatch");
   373 }
   419 }
   374 
   420 
   375 void FileMapInfo::check_nonempty_dir_in_shared_path_table() {
   421 void FileMapInfo::check_nonempty_dir_in_shared_path_table() {
   376   assert(DumpSharedSpaces, "dump time only");
   422   assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only");
   377 
   423 
   378   bool has_nonempty_dir = false;
   424   bool has_nonempty_dir = false;
   379 
   425 
   380   int last = _shared_path_table_size - 1;
   426   int last = _shared_path_table.size() - 1;
   381   if (last > ClassLoaderExt::max_used_path_index()) {
   427   if (last > ClassLoaderExt::max_used_path_index()) {
   382      // no need to check any path beyond max_used_path_index
   428      // no need to check any path beyond max_used_path_index
   383      last = ClassLoaderExt::max_used_path_index();
   429      last = ClassLoaderExt::max_used_path_index();
   384   }
   430   }
   385 
   431 
   477 
   523 
   478 bool FileMapInfo::validate_shared_path_table() {
   524 bool FileMapInfo::validate_shared_path_table() {
   479   assert(UseSharedSpaces, "runtime only");
   525   assert(UseSharedSpaces, "runtime only");
   480 
   526 
   481   _validating_shared_path_table = true;
   527   _validating_shared_path_table = true;
       
   528 
       
   529   // Load the shared path table info from the archive header
   482   _shared_path_table = _header->_shared_path_table;
   530   _shared_path_table = _header->_shared_path_table;
   483   _shared_path_entry_size = _header->_shared_path_entry_size;
   531   if (DynamicDumpSharedSpaces) {
   484   _shared_path_table_size = _header->_shared_path_table_size;
   532     // Only support dynamic dumping with the usage of the default CDS archive
       
   533     // or a simple base archive.
       
   534     // If the base layer archive contains additional path component besides
       
   535     // the runtime image and the -cp, dynamic dumping is disabled.
       
   536     //
       
   537     // When dynamic archiving is enabled, the _shared_path_table is overwritten
       
   538     // 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");
       
   540     if (_header->_app_class_paths_start_index > 1) {
       
   541       DynamicDumpSharedSpaces = false;
       
   542       warning(
       
   543         "Dynamic archiving is disabled because base layer archive has appended boot classpath");
       
   544     }
       
   545     if (_header->_num_module_paths > 0) {
       
   546       DynamicDumpSharedSpaces = false;
       
   547       warning(
       
   548         "Dynamic archiving is disabled because base layer archive has module path");
       
   549     }
       
   550   }
   485 
   551 
   486   int module_paths_start_index = _header->_app_module_paths_start_index;
   552   int module_paths_start_index = _header->_app_module_paths_start_index;
   487 
   553 
   488   // validate the path entries up to the _max_used_path_index
   554   // validate the path entries up to the _max_used_path_index
   489   for (int i=0; i < _header->_max_used_path_index + 1; i++) {
   555   for (int i=0; i < _header->_max_used_path_index + 1; i++) {
   490     if (i < module_paths_start_index) {
   556     if (i < module_paths_start_index) {
   491       if (shared_path(i)->validate()) {
   557       if (shared_path(i)->validate()) {
   492         log_info(class, path)("ok");
   558         log_info(class, path)("ok");
   493       } else {
   559       } else {
   494         assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
   560         if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) {
       
   561           assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
       
   562         }
   495         return false;
   563         return false;
   496       }
   564       }
   497     } else if (i >= module_paths_start_index) {
   565     } else if (i >= module_paths_start_index) {
   498       if (shared_path(i)->validate(false /* not a class path entry */)) {
   566       if (shared_path(i)->validate(false /* not a class path entry */)) {
   499         log_info(class, path)("ok");
   567         log_info(class, path)("ok");
   500       } else {
   568       } else {
   501         assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
   569         if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) {
       
   570           assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
       
   571         }
   502         return false;
   572         return false;
   503       }
   573       }
   504     }
   574     }
   505   }
   575   }
   506 
   576 
   508 
   578 
   509 #if INCLUDE_JVMTI
   579 #if INCLUDE_JVMTI
   510   if (_classpath_entries_for_jvmti != NULL) {
   580   if (_classpath_entries_for_jvmti != NULL) {
   511     os::free(_classpath_entries_for_jvmti);
   581     os::free(_classpath_entries_for_jvmti);
   512   }
   582   }
   513   size_t sz = sizeof(ClassPathEntry*) *  _shared_path_table_size;
   583   size_t sz = sizeof(ClassPathEntry*) * get_number_of_shared_paths();
   514   _classpath_entries_for_jvmti = (ClassPathEntry**)os::malloc(sz, mtClass);
   584   _classpath_entries_for_jvmti = (ClassPathEntry**)os::malloc(sz, mtClass);
   515   memset(_classpath_entries_for_jvmti, 0, sz);
   585   memset((void*)_classpath_entries_for_jvmti, 0, sz);
   516 #endif
   586 #endif
   517 
   587 
   518   return true;
   588   return true;
   519 }
   589 }
   520 
   590 
       
   591 bool FileMapInfo::same_files(const char* file1, const char* file2) {
       
   592   if (strcmp(file1, file2) == 0) {
       
   593     return true;
       
   594   }
       
   595 
       
   596   bool is_same = false;
       
   597   // if the two paths diff only in case
       
   598   struct stat st1;
       
   599   struct stat st2;
       
   600   int ret1;
       
   601   int ret2;
       
   602   ret1 = os::stat(file1, &st1);
       
   603   ret2 = os::stat(file2, &st2);
       
   604   if (ret1 < 0 || ret2 < 0) {
       
   605     // one of the files is invalid. So they are not the same.
       
   606     is_same = false;
       
   607   } else if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) {
       
   608     // different files
       
   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 }
       
   623 
       
   624 bool FileMapInfo::check_archive(const char* archive_name, bool is_static) {
       
   625   int fd = os::open(archive_name, O_RDONLY | O_BINARY, 0);
       
   626   if (fd < 0) {
       
   627     // do not vm_exit_during_initialization here because Arguments::init_shared_archive_paths()
       
   628     // requires a shared archive name. The open_for_read() function will log a message regarding
       
   629     // failure in opening a shared archive.
       
   630     return false;
       
   631   }
       
   632 
       
   633   size_t sz = is_static ? sizeof(FileMapHeader) : sizeof(DynamicArchiveHeader);
       
   634   void* header = os::malloc(sz, mtInternal);
       
   635   memset(header, 0, sz);
       
   636   size_t n = os::read(fd, header, (unsigned int)sz);
       
   637   if (n != sz) {
       
   638     os::free(header);
       
   639     os::close(fd);
       
   640     vm_exit_during_initialization("Unable to read header from shared archive", archive_name);
       
   641     return false;
       
   642   }
       
   643   if (is_static) {
       
   644     FileMapHeader* static_header = (FileMapHeader*)header;
       
   645     if (static_header->_magic != CDS_ARCHIVE_MAGIC) {
       
   646       os::free(header);
       
   647       os::close(fd);
       
   648       vm_exit_during_initialization("Not a base shared archive", archive_name);
       
   649       return false;
       
   650     }
       
   651   } else {
       
   652     DynamicArchiveHeader* dynamic_header = (DynamicArchiveHeader*)header;
       
   653     if (dynamic_header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
       
   654       os::free(header);
       
   655       os::close(fd);
       
   656       vm_exit_during_initialization("Not a top shared archive", archive_name);
       
   657       return false;
       
   658     }
       
   659   }
       
   660   os::free(header);
       
   661   os::close(fd);
       
   662   return true;
       
   663 }
       
   664 
       
   665 bool FileMapInfo::get_base_archive_name_from_header(const char* archive_name,
       
   666                                                     int* size, char** base_archive_name) {
       
   667   int fd = os::open(archive_name, O_RDONLY | O_BINARY, 0);
       
   668   if (fd < 0) {
       
   669     *size = 0;
       
   670     return false;
       
   671   }
       
   672 
       
   673   // read the header as a dynamic archive header
       
   674   size_t sz = sizeof(DynamicArchiveHeader);
       
   675   DynamicArchiveHeader* dynamic_header = (DynamicArchiveHeader*)os::malloc(sz, mtInternal);
       
   676   size_t n = os::read(fd, dynamic_header, (unsigned int)sz);
       
   677   if (n != sz) {
       
   678     fail_continue("Unable to read the file header.");
       
   679     os::free(dynamic_header);
       
   680     os::close(fd);
       
   681     return false;
       
   682   }
       
   683   if (dynamic_header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
       
   684     // Not a dynamic header, no need to proceed further.
       
   685     *size = 0;
       
   686     os::free(dynamic_header);
       
   687     os::close(fd);
       
   688     return false;
       
   689   }
       
   690   if (dynamic_header->_base_archive_is_default) {
       
   691     *base_archive_name = Arguments::get_default_shared_archive_path();
       
   692   } 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
       
   697     size_t name_size = dynamic_header->_base_archive_name_size;
       
   698     if (name_size == 0) {
       
   699       os::free(dynamic_header);
       
   700       os::close(fd);
       
   701       return false;
       
   702     }
       
   703     *base_archive_name = NEW_C_HEAP_ARRAY(char, name_size, mtInternal);
       
   704     n = os::read(fd, *base_archive_name, (unsigned int)name_size);
       
   705     if (n != name_size) {
       
   706       fail_continue("Unable to read the base archive name from the header.");
       
   707       FREE_C_HEAP_ARRAY(char, *base_archive_name);
       
   708       *base_archive_name = NULL;
       
   709       os::free(dynamic_header);
       
   710       os::close(fd);
       
   711       return false;
       
   712     }
       
   713   }
       
   714 
       
   715   os::free(dynamic_header);
       
   716   os::close(fd);
       
   717   return true;
       
   718 }
       
   719 
       
   720 void FileMapInfo::restore_shared_path_table() {
       
   721   _shared_path_table = _current_info->_header->_shared_path_table;
       
   722 }
       
   723 
   521 // Read the FileMapInfo information from the file.
   724 // Read the FileMapInfo information from the file.
   522 
   725 
   523 bool FileMapInfo::init_from_file(int fd) {
   726 bool FileMapInfo::init_from_file(int fd, bool is_static) {
   524   size_t sz = sizeof(FileMapHeader);
   727   size_t sz = is_static ? sizeof(FileMapHeader) : sizeof(DynamicArchiveHeader);
   525   size_t n = os::read(fd, _header, (unsigned int)sz);
   728   size_t n = os::read(fd, _header, (unsigned int)sz);
   526   if (n != sz) {
   729   if (n != sz) {
   527     fail_continue("Unable to read the file header.");
   730     fail_continue("Unable to read the file header.");
   528     return false;
   731     return false;
   529   }
   732   }
   530   if (_header->_version != CURRENT_CDS_ARCHIVE_VERSION) {
   733   if (_header->_version != CURRENT_CDS_ARCHIVE_VERSION) {
   531     fail_continue("The shared archive file has the wrong version.");
   734     fail_continue("The shared archive file has the wrong version.");
   532     return false;
   735     return false;
   533   }
   736   }
   534   _file_offset = (long)n;
   737   _file_offset = n;
   535 
   738 
   536   size_t info_size = _header->_paths_misc_info_size;
   739   size_t info_size = _header->_paths_misc_info_size;
   537   _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass);
   740   _paths_misc_info = NEW_C_HEAP_ARRAY(char, info_size, mtClass);
   538   if (_paths_misc_info == NULL) {
       
   539     fail_continue("Unable to read the file header.");
       
   540     return false;
       
   541   }
       
   542   n = os::read(fd, _paths_misc_info, (unsigned int)info_size);
   741   n = os::read(fd, _paths_misc_info, (unsigned int)info_size);
   543   if (n != info_size) {
   742   if (n != info_size) {
   544     fail_continue("Unable to read the shared path info header.");
   743     fail_continue("Unable to read the shared path info header.");
   545     FREE_C_HEAP_ARRAY(char, _paths_misc_info);
   744     FREE_C_HEAP_ARRAY(char, _paths_misc_info);
   546     _paths_misc_info = NULL;
   745     _paths_misc_info = NULL;
   547     return false;
   746     return false;
   548   }
   747   }
   549 
   748   _file_offset += n + _header->_base_archive_name_size; // accounts for the size of _base_archive_name
   550   size_t len = lseek(fd, 0, SEEK_END);
   749 
   551   CDSFileMapRegion* si = space_at(MetaspaceShared::last_valid_region);
   750   if (is_static) {
   552   // The last space might be empty
   751     if (_header->_magic != CDS_ARCHIVE_MAGIC) {
   553   if (si->_file_offset > len || len - si->_file_offset < si->_used) {
   752       fail_continue("Incorrect static archive magic number");
   554     fail_continue("The shared archive file has been truncated.");
   753       return false;
   555     return false;
   754     }
   556   }
   755     // just checking the last region is sufficient since the archive is written
   557 
   756     // in sequential order
   558   _file_offset += (long)n;
   757     size_t len = lseek(fd, 0, SEEK_END);
   559   SharedBaseAddress = _header->_shared_base_address;
   758     CDSFileMapRegion* si = space_at(MetaspaceShared::last_valid_region);
       
   759     // The last space might be empty
       
   760     if (si->_file_offset > len || len - si->_file_offset < si->_used) {
       
   761       fail_continue("The shared archive file has been truncated.");
       
   762       return false;
       
   763     }
       
   764 
       
   765     SharedBaseAddress = _header->_shared_base_address;
       
   766   }
       
   767 
   560   return true;
   768   return true;
   561 }
   769 }
   562 
   770 
   563 
   771 
   564 // Read the FileMapInfo information from the file.
   772 // Read the FileMapInfo information from the file.
   565 bool FileMapInfo::open_for_read() {
   773 bool FileMapInfo::open_for_read(const char* path) {
   566   _full_path = Arguments::GetSharedArchivePath();
   774   if (_file_open) {
       
   775     return true;
       
   776   }
       
   777   if (path == NULL) {
       
   778     _full_path = Arguments::GetSharedArchivePath();
       
   779   } else {
       
   780     _full_path = path;
       
   781   }
   567   int fd = os::open(_full_path, O_RDONLY | O_BINARY, 0);
   782   int fd = os::open(_full_path, O_RDONLY | O_BINARY, 0);
   568   if (fd < 0) {
   783   if (fd < 0) {
   569     if (errno == ENOENT) {
   784     if (errno == ENOENT) {
   570       // Not locating the shared archive is ok.
   785       // Not locating the shared archive is ok.
   571       fail_continue("Specified shared archive not found.");
   786       fail_continue("Specified shared archive not found (%s).", _full_path);
   572     } else {
   787     } else {
   573       fail_continue("Failed to open shared archive file (%s).",
   788       fail_continue("Failed to open shared archive file (%s).",
   574                     os::strerror(errno));
   789                     os::strerror(errno));
   575     }
   790     }
   576     return false;
   791     return false;
   579   _fd = fd;
   794   _fd = fd;
   580   _file_open = true;
   795   _file_open = true;
   581   return true;
   796   return true;
   582 }
   797 }
   583 
   798 
   584 
       
   585 // Write the FileMapInfo information to the file.
   799 // Write the FileMapInfo information to the file.
   586 
   800 
   587 void FileMapInfo::open_for_write() {
   801 void FileMapInfo::open_for_write(const char* path) {
   588   _full_path = Arguments::GetSharedArchivePath();
   802   if (path == NULL) {
       
   803     _full_path = Arguments::GetSharedArchivePath();
       
   804   } else {
       
   805     _full_path = path;
       
   806   }
   589   LogMessage(cds) msg;
   807   LogMessage(cds) msg;
   590   if (msg.is_info()) {
   808   if (msg.is_info()) {
   591     msg.info("Dumping shared data to file: ");
   809     msg.info("Dumping shared data to file: ");
   592     msg.info("   %s", _full_path);
   810     msg.info("   %s", _full_path);
   593   }
   811   }
   594 
   812 
   595 #ifdef _WINDOWS  // On Windows, need WRITE permission to remove the file.
   813 #ifdef _WINDOWS  // On Windows, need WRITE permission to remove the file.
   596   chmod(_full_path, _S_IREAD | _S_IWRITE);
   814     chmod(_full_path, _S_IREAD | _S_IWRITE);
   597 #endif
   815 #endif
   598 
   816 
   599   // Use remove() to delete the existing file because, on Unix, this will
   817   // Use remove() to delete the existing file because, on Unix, this will
   600   // allow processes that have it open continued access to the file.
   818   // allow processes that have it open continued access to the file.
   601   remove(_full_path);
   819   remove(_full_path);
   615 void FileMapInfo::write_header() {
   833 void FileMapInfo::write_header() {
   616   int info_size = ClassLoader::get_shared_paths_misc_info_size();
   834   int info_size = ClassLoader::get_shared_paths_misc_info_size();
   617 
   835 
   618   _header->_paths_misc_info_size = info_size;
   836   _header->_paths_misc_info_size = info_size;
   619 
   837 
       
   838   char* base_archive_name = NULL;
       
   839   if (_header->_magic == CDS_DYNAMIC_ARCHIVE_MAGIC) {
       
   840     base_archive_name = (char*)Arguments::GetSharedArchivePath();
       
   841     _header->_base_archive_name_size = (int)strlen(base_archive_name) + 1;
       
   842     _header->_base_archive_is_default = FLAG_IS_DEFAULT(SharedArchiveFile);
       
   843   }
       
   844 
       
   845   assert(is_file_position_aligned(), "must be");
       
   846   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) {
       
   849     write_bytes(base_archive_name, (size_t)_header->_base_archive_name_size);
       
   850   }
   620   align_file_position();
   851   align_file_position();
   621   write_bytes(_header, sizeof(FileMapHeader));
   852 }
   622   write_bytes(ClassLoader::get_shared_paths_misc_info(), (size_t)info_size);
       
   623   align_file_position();
       
   624 }
       
   625 
       
   626 
   853 
   627 // Dump region to file.
   854 // Dump region to file.
   628 
   855 // This is called twice for each region during archiving, once before
       
   856 // the archive file is open (_file_open is false) and once after.
   629 void FileMapInfo::write_region(int region, char* base, size_t size,
   857 void FileMapInfo::write_region(int region, char* base, size_t size,
   630                                bool read_only, bool allow_exec) {
   858                                bool read_only, bool allow_exec) {
       
   859   assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "Dump time only");
       
   860 
   631   CDSFileMapRegion* si = space_at(region);
   861   CDSFileMapRegion* si = space_at(region);
       
   862   char* target_base = base;
       
   863   if (DynamicDumpSharedSpaces) {
       
   864     target_base = DynamicArchive::buffer_to_target(base);
       
   865   }
   632 
   866 
   633   if (_file_open) {
   867   if (_file_open) {
   634     guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
   868     guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
   635     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)
   636                   " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),
   870                   " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),
   637                   region, size, p2i(base), _file_offset);
   871                   region, size, p2i(target_base), _file_offset);
   638   } else {
   872   } else {
   639     si->_file_offset = _file_offset;
   873     si->_file_offset = _file_offset;
   640   }
   874   }
       
   875 
   641   if (HeapShared::is_heap_region(region)) {
   876   if (HeapShared::is_heap_region(region)) {
   642     assert((base - (char*)CompressedOops::base()) % HeapWordSize == 0, "Sanity");
   877     assert((target_base - (char*)CompressedKlassPointers::base()) % HeapWordSize == 0, "Sanity");
   643     if (base != NULL) {
   878     if (target_base != NULL) {
   644       si->_addr._offset = (intx)CompressedOops::encode_not_null((oop)base);
   879       si->_addr._offset = (intx)CompressedOops::encode_not_null((oop)target_base);
   645     } else {
   880     } else {
   646       si->_addr._offset = 0;
   881       si->_addr._offset = 0;
   647     }
   882     }
   648   } else {
   883   } else {
   649     si->_addr._base = base;
   884     si->_addr._base = target_base;
   650   }
   885   }
   651   si->_used = size;
   886   si->_used = size;
   652   si->_read_only = read_only;
   887   si->_read_only = read_only;
   653   si->_allow_exec = allow_exec;
   888   si->_allow_exec = allow_exec;
       
   889 
       
   890   // Use the current 'base' when computing the CRC value and writing out data
   654   si->_crc = ClassLoader::crc32(0, base, (jint)size);
   891   si->_crc = ClassLoader::crc32(0, base, (jint)size);
   655   if (base != NULL) {
   892   if (base != NULL) {
   656     write_bytes_aligned(base, size);
   893     write_bytes_aligned(base, size);
   657   }
   894   }
   658 }
   895 }
   732 
   969 
   733 void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) {
   970 void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) {
   734   if (_file_open) {
   971   if (_file_open) {
   735     size_t n = os::write(_fd, buffer, (unsigned int)nbytes);
   972     size_t n = os::write(_fd, buffer, (unsigned int)nbytes);
   736     if (n != nbytes) {
   973     if (n != nbytes) {
   737       // It is dangerous to leave the corrupted shared archive file around,
   974       // If the shared archive is corrupted, close it and remove it.
   738       // close and remove the file. See bug 6372906.
       
   739       close();
   975       close();
   740       remove(_full_path);
   976       remove(_full_path);
   741       fail_stop("Unable to write to shared archive file.");
   977       fail_stop("Unable to write to shared archive file.");
   742     }
   978     }
   743   }
   979   }
   744   _file_offset += nbytes;
   980   _file_offset += nbytes;
   745 }
   981 }
   746 
   982 
       
   983 bool FileMapInfo::is_file_position_aligned() const {
       
   984   return _file_offset == align_up(_file_offset,
       
   985                                   os::vm_allocation_granularity());
       
   986 }
   747 
   987 
   748 // Align file position to an allocation unit boundary.
   988 // Align file position to an allocation unit boundary.
   749 
   989 
   750 void FileMapInfo::align_file_position() {
   990 void FileMapInfo::align_file_position() {
   751   size_t new_file_offset = align_up(_file_offset,
   991   size_t new_file_offset = align_up(_file_offset,
   841 
  1081 
   842 // Memory map a region in the address space.
  1082 // Memory map a region in the address space.
   843 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "OptionalData",
  1083 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "OptionalData",
   844                                             "String1", "String2", "OpenArchive1", "OpenArchive2" };
  1084                                             "String1", "String2", "OpenArchive1", "OpenArchive2" };
   845 
  1085 
       
  1086 char* FileMapInfo::map_regions(int regions[], char* saved_base[], size_t len) {
       
  1087   char* prev_top = NULL;
       
  1088   char* curr_base;
       
  1089   char* curr_top;
       
  1090   int i = 0;
       
  1091   for (i = 0; i < (int)len; i++) {
       
  1092     curr_base = map_region(regions[i], &curr_top);
       
  1093     if (curr_base == NULL) {
       
  1094       return NULL;
       
  1095     }
       
  1096     if (i > 0) {
       
  1097       // We require that mc->rw->ro->md->od to be laid out consecutively, with no
       
  1098       // 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
       
  1100       // would mess up the simple comparision in MetaspaceShared::is_in_shared_metaspace().
       
  1101       assert(curr_base == prev_top, "must be");
       
  1102     }
       
  1103     log_info(cds)("Mapped region #%d at base %p top %p", regions[i], curr_base, curr_top);
       
  1104     saved_base[i] = curr_base;
       
  1105     prev_top = curr_top;
       
  1106   }
       
  1107   return curr_top;
       
  1108 }
       
  1109 
   846 char* FileMapInfo::map_region(int i, char** top_ret) {
  1110 char* FileMapInfo::map_region(int i, char** top_ret) {
   847   assert(!HeapShared::is_heap_region(i), "sanity");
  1111   assert(!HeapShared::is_heap_region(i), "sanity");
   848   CDSFileMapRegion* si = space_at(i);
  1112   CDSFileMapRegion* si = space_at(i);
   849   size_t used = si->_used;
  1113   size_t used = si->_used;
   850   size_t alignment = os::vm_allocation_granularity();
  1114   size_t alignment = os::vm_allocation_granularity();
   867   char *base = os::map_memory(_fd, _full_path, si->_file_offset,
  1131   char *base = os::map_memory(_fd, _full_path, si->_file_offset,
   868                               requested_addr, size, si->_read_only,
  1132                               requested_addr, size, si->_read_only,
   869                               si->_allow_exec);
  1133                               si->_allow_exec);
   870   if (base == NULL || base != requested_addr) {
  1134   if (base == NULL || base != requested_addr) {
   871     fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
  1135     fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
       
  1136     _memory_mapping_failed = true;
   872     return NULL;
  1137     return NULL;
   873   }
  1138   }
   874 #ifdef _WINDOWS
  1139 #ifdef _WINDOWS
   875   // This call is Windows-only because the memory_type gets recorded for the other platforms
  1140   // This call is Windows-only because the memory_type gets recorded for the other platforms
   876   // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
  1141   // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
   881     return NULL;
  1146     return NULL;
   882   }
  1147   }
   883 
  1148 
   884   *top_ret = base + size;
  1149   *top_ret = base + size;
   885   return base;
  1150   return base;
       
  1151 }
       
  1152 
       
  1153 size_t FileMapInfo::read_bytes(void* buffer, size_t count) {
       
  1154   assert(_file_open, "Archive file is not open");
       
  1155   size_t n = os::read(_fd, buffer, (unsigned int)count);
       
  1156   if (n != count) {
       
  1157     // Close the file if there's a problem reading it.
       
  1158     close();
       
  1159     return 0;
       
  1160   }
       
  1161   _file_offset += count;
       
  1162   return count;
   886 }
  1163 }
   887 
  1164 
   888 address FileMapInfo::decode_start_address(CDSFileMapRegion* spc, bool with_current_oop_encoding_mode) {
  1165 address FileMapInfo::decode_start_address(CDSFileMapRegion* spc, bool with_current_oop_encoding_mode) {
   889   if (with_current_oop_encoding_mode) {
  1166   if (with_current_oop_encoding_mode) {
   890     return (address)CompressedOops::decode_not_null(offset_of_space(spc));
  1167     return (address)CompressedOops::decode_not_null(offset_of_space(spc));
  1124       log_info(cds)("UseSharedSpaces: Unable to map at required address in java heap. "
  1401       log_info(cds)("UseSharedSpaces: Unable to map at required address in java heap. "
  1125                     INTPTR_FORMAT ", size = " SIZE_FORMAT " bytes",
  1402                     INTPTR_FORMAT ", size = " SIZE_FORMAT " bytes",
  1126                     p2i(addr), regions[i].byte_size());
  1403                     p2i(addr), regions[i].byte_size());
  1127       return false;
  1404       return false;
  1128     }
  1405     }
  1129   }
  1406 
  1130 
  1407     if (VerifySharedSpaces && !region_crc_check(addr, regions[i].byte_size(), si->_crc)) {
  1131   if (!verify_mapped_heap_regions(first, region_num)) {
  1408       // dealloc the regions from java heap
  1132     // dealloc the regions from java heap
  1409       dealloc_archive_heap_regions(regions, region_num, is_open_archive);
  1133     dealloc_archive_heap_regions(regions, region_num, is_open_archive);
  1410       log_info(cds)("UseSharedSpaces: mapped heap regions are corrupt");
  1134     log_info(cds)("UseSharedSpaces: mapped heap regions are corrupt");
  1411       return false;
  1135     return false;
  1412     }
  1136   }
  1413   }
  1137 
  1414 
  1138   // the shared heap data is mapped successfully
  1415   // the shared heap data is mapped successfully
  1139   *heap_mem = regions;
  1416   *heap_mem = regions;
  1140   *num = region_num;
  1417   *num = region_num;
  1141   return true;
       
  1142 }
       
  1143 
       
  1144 bool FileMapInfo::verify_mapped_heap_regions(int first, int num) {
       
  1145   assert(num > 0, "sanity");
       
  1146   if (VerifySharedSpaces) {
       
  1147     for (int i = first; i < first + num; i++) {
       
  1148       if (!verify_region_checksum(i)) {
       
  1149         return false;
       
  1150       }
       
  1151     }
       
  1152   }
       
  1153   return true;
  1418   return true;
  1154 }
  1419 }
  1155 
  1420 
  1156 void FileMapInfo::patch_archived_heap_embedded_pointers() {
  1421 void FileMapInfo::patch_archived_heap_embedded_pointers() {
  1157   if (!_heap_pointers_need_patching) {
  1422   if (!_heap_pointers_need_patching) {
  1203     G1CollectedHeap::heap()->dealloc_archive_regions(regions, num, is_open);
  1468     G1CollectedHeap::heap()->dealloc_archive_regions(regions, num, is_open);
  1204   }
  1469   }
  1205 }
  1470 }
  1206 #endif // INCLUDE_CDS_JAVA_HEAP
  1471 #endif // INCLUDE_CDS_JAVA_HEAP
  1207 
  1472 
       
  1473 bool FileMapInfo::region_crc_check(char* buf, size_t size, int expected_crc) {
       
  1474   int crc = ClassLoader::crc32(0, buf, (jint)size);
       
  1475   if (crc != expected_crc) {
       
  1476     fail_continue("Checksum verification failed.");
       
  1477     return false;
       
  1478   }
       
  1479   return true;
       
  1480 }
       
  1481 
  1208 bool FileMapInfo::verify_region_checksum(int i) {
  1482 bool FileMapInfo::verify_region_checksum(int i) {
  1209   assert(VerifySharedSpaces, "sanity");
  1483   assert(VerifySharedSpaces, "sanity");
  1210 
  1484 
  1211   size_t sz = space_at(i)->_used;
  1485   size_t sz = space_at(i)->_used;
  1212 
  1486 
  1213   if (sz == 0) {
  1487   if (sz == 0) {
  1214     return true; // no data
  1488     return true; // no data
  1215   }
  1489   }
  1216   if ((HeapShared::is_closed_archive_heap_region(i) &&
  1490 
  1217        !HeapShared::closed_archive_heap_region_mapped()) ||
  1491   return region_crc_check(region_addr(i), sz, space_at(i)->_crc);
  1218       (HeapShared::is_open_archive_heap_region(i) &&
  1492 }
  1219        !HeapShared::open_archive_heap_region_mapped())) {
  1493 
  1220     return true; // archived heap data is not mapped
  1494 void FileMapInfo::unmap_regions(int regions[], char* saved_base[], size_t len) {
  1221   }
  1495   for (int i = 0; i < (int)len; i++) {
  1222   const char* buf = region_addr(i);
  1496     if (saved_base[i] != NULL) {
  1223   int crc = ClassLoader::crc32(0, buf, (jint)sz);
  1497       unmap_region(regions[i]);
  1224   if (crc != space_at(i)->_crc) {
  1498     }
  1225     fail_continue("Checksum verification failed.");
  1499   }
  1226     return false;
       
  1227   }
       
  1228   return true;
       
  1229 }
  1500 }
  1230 
  1501 
  1231 // Unmap a memory region in the address space.
  1502 // Unmap a memory region in the address space.
  1232 
  1503 
  1233 void FileMapInfo::unmap_region(int i) {
  1504 void FileMapInfo::unmap_region(int i) {
  1251     fail_stop("Mark mismatch while restoring from shared file.");
  1522     fail_stop("Mark mismatch while restoring from shared file.");
  1252   }
  1523   }
  1253 }
  1524 }
  1254 
  1525 
  1255 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
  1526 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
  1256   it->push(&_shared_path_table);
  1527   _shared_path_table.metaspace_pointers_do(it);
  1257   for (int i=0; i<_shared_path_table_size; i++) {
  1528 }
  1258     shared_path(i)->metaspace_pointers_do(it);
       
  1259   }
       
  1260 }
       
  1261 
       
  1262 
  1529 
  1263 FileMapInfo* FileMapInfo::_current_info = NULL;
  1530 FileMapInfo* FileMapInfo::_current_info = NULL;
       
  1531 FileMapInfo* FileMapInfo::_dynamic_archive_info = NULL;
  1264 bool FileMapInfo::_heap_pointers_need_patching = false;
  1532 bool FileMapInfo::_heap_pointers_need_patching = false;
  1265 Array<u8>* FileMapInfo::_shared_path_table = NULL;
  1533 SharedPathTable FileMapInfo::_shared_path_table;
  1266 int FileMapInfo::_shared_path_table_size = 0;
       
  1267 size_t FileMapInfo::_shared_path_entry_size = 0x1234baad;
       
  1268 bool FileMapInfo::_validating_shared_path_table = false;
  1534 bool FileMapInfo::_validating_shared_path_table = false;
       
  1535 bool FileMapInfo::_memory_mapping_failed = false;
  1269 
  1536 
  1270 // Open the shared archive file, read and validate the header
  1537 // Open the shared archive file, read and validate the header
  1271 // information (version, boot classpath, etc.).  If initialization
  1538 // information (version, boot classpath, etc.).  If initialization
  1272 // fails, shared spaces are disabled and the file is closed. [See
  1539 // fails, shared spaces are disabled and the file is closed. [See
  1273 // fail_continue.]
  1540 // fail_continue.]
  1275 // Validation of the archive is done in two steps:
  1542 // Validation of the archive is done in two steps:
  1276 //
  1543 //
  1277 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
  1544 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
  1278 // [2] validate_shared_path_table - this is done later, because the table is in the RW
  1545 // [2] validate_shared_path_table - this is done later, because the table is in the RW
  1279 //     region of the archive, which is not mapped yet.
  1546 //     region of the archive, which is not mapped yet.
  1280 bool FileMapInfo::initialize() {
  1547 bool FileMapInfo::initialize(bool is_static) {
  1281   assert(UseSharedSpaces, "UseSharedSpaces expected.");
  1548   assert(UseSharedSpaces, "UseSharedSpaces expected.");
  1282 
  1549 
  1283   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
  1550   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
  1284     // CDS assumes that no classes resolved in SystemDictionary::resolve_well_known_classes
  1551     // CDS assumes that no classes resolved in SystemDictionary::resolve_well_known_classes
  1285     // are replaced at runtime by JVMTI ClassFileLoadHook. All of those classes are resolved
  1552     // are replaced at runtime by JVMTI ClassFileLoadHook. All of those classes are resolved
  1291 
  1558 
  1292   if (!open_for_read()) {
  1559   if (!open_for_read()) {
  1293     return false;
  1560     return false;
  1294   }
  1561   }
  1295 
  1562 
  1296   init_from_file(_fd);
  1563   init_from_file(_fd, is_static);
  1297   if (!validate_header()) {
  1564   if (!validate_header(is_static)) {
  1298     return false;
  1565     return false;
  1299   }
  1566   }
  1300   return true;
  1567   return true;
  1301 }
  1568 }
  1302 
  1569 
  1313 
  1580 
  1314 int FileMapHeader::compute_crc() {
  1581 int FileMapHeader::compute_crc() {
  1315   char* start = (char*)this;
  1582   char* start = (char*)this;
  1316   // start computing from the field after _crc
  1583   // start computing from the field after _crc
  1317   char* buf = (char*)&_crc + sizeof(_crc);
  1584   char* buf = (char*)&_crc + sizeof(_crc);
  1318   size_t sz = sizeof(FileMapHeader) - (buf - start);
  1585   size_t sz = _header_size - (buf - start);
  1319   int crc = ClassLoader::crc32(0, buf, (jint)sz);
  1586   int crc = ClassLoader::crc32(0, buf, (jint)sz);
  1320   return crc;
  1587   return crc;
  1321 }
  1588 }
  1322 
  1589 
  1323 // This function should only be called during run time with UseSharedSpaces enabled.
  1590 // This function should only be called during run time with UseSharedSpaces enabled.
  1334 
  1601 
  1335   if (_version != CURRENT_CDS_ARCHIVE_VERSION) {
  1602   if (_version != CURRENT_CDS_ARCHIVE_VERSION) {
  1336     FileMapInfo::fail_continue("The shared archive file is the wrong version.");
  1603     FileMapInfo::fail_continue("The shared archive file is the wrong version.");
  1337     return false;
  1604     return false;
  1338   }
  1605   }
  1339   if (_magic != CDS_ARCHIVE_MAGIC) {
  1606   if (_magic != CDS_ARCHIVE_MAGIC && _magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
  1340     FileMapInfo::fail_continue("The shared archive file has a bad magic number.");
  1607     FileMapInfo::fail_continue("The shared archive file has a bad magic number.");
  1341     return false;
  1608     return false;
  1342   }
  1609   }
  1343   char header_version[JVM_IDENT_MAX];
  1610   char header_version[JVM_IDENT_MAX];
  1344   get_header_version(header_version);
  1611   get_header_version(header_version);
  1399   }
  1666   }
  1400 
  1667 
  1401   return true;
  1668   return true;
  1402 }
  1669 }
  1403 
  1670 
  1404 bool FileMapInfo::validate_header() {
  1671 bool FileMapInfo::validate_header(bool is_static) {
  1405   bool status = _header->validate();
  1672   bool status = _header->validate();
  1406 
  1673 
  1407   if (status) {
  1674   if (status) {
  1408     if (!ClassLoader::check_shared_paths_misc_info(_paths_misc_info, _header->_paths_misc_info_size)) {
  1675     if (!ClassLoader::check_shared_paths_misc_info(_paths_misc_info, _header->_paths_misc_info_size, is_static)) {
  1409       if (!PrintSharedArchiveAndExit) {
  1676       if (!PrintSharedArchiveAndExit) {
  1410         fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)");
  1677         fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)");
  1411         status = false;
  1678         status = false;
  1412       }
  1679       }
  1413     }
  1680     }
  1433   return false;
  1700   return false;
  1434 }
  1701 }
  1435 
  1702 
  1436 // Unmap mapped regions of shared space.
  1703 // Unmap mapped regions of shared space.
  1437 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
  1704 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
  1438   MetaspaceObj::set_shared_metaspace_range(NULL, NULL);
  1705   MetaspaceShared::set_shared_metaspace_range(NULL, NULL);
  1439 
  1706 
  1440   FileMapInfo *map_info = FileMapInfo::current_info();
  1707   FileMapInfo *map_info = FileMapInfo::current_info();
  1441   if (map_info) {
  1708   if (map_info) {
  1442     map_info->fail_continue("%s", msg);
  1709     map_info->fail_continue("%s", msg);
  1443     for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) {
  1710     for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) {
  1500 }
  1767 }
  1501 
  1768 
  1502 ClassFileStream* FileMapInfo::open_stream_for_jvmti(InstanceKlass* ik, Handle class_loader, TRAPS) {
  1769 ClassFileStream* FileMapInfo::open_stream_for_jvmti(InstanceKlass* ik, Handle class_loader, TRAPS) {
  1503   int path_index = ik->shared_classpath_index();
  1770   int path_index = ik->shared_classpath_index();
  1504   assert(path_index >= 0, "should be called for shared built-in classes only");
  1771   assert(path_index >= 0, "should be called for shared built-in classes only");
  1505   assert(path_index < (int)_shared_path_table_size, "sanity");
  1772   assert(path_index < (int)get_number_of_shared_paths(), "sanity");
  1506 
  1773 
  1507   ClassPathEntry* cpe = get_classpath_entry_for_jvmti(path_index, CHECK_NULL);
  1774   ClassPathEntry* cpe = get_classpath_entry_for_jvmti(path_index, CHECK_NULL);
  1508   assert(cpe != NULL, "must be");
  1775   assert(cpe != NULL, "must be");
  1509 
  1776 
  1510   Symbol* name = ik->name();
  1777   Symbol* name = ik->name();