src/hotspot/share/memory/filemap.cpp
changeset 59070 22ee476cc664
parent 58447 319173c62caa
child 59128 ac11b83e0f38
equal deleted inserted replaced
59069:e0d59f0c2b7d 59070:22ee476cc664
    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/archiveUtils.inline.hpp"
    37 #include "memory/dynamicArchive.hpp"
    38 #include "memory/dynamicArchive.hpp"
    38 #include "memory/filemap.hpp"
    39 #include "memory/filemap.hpp"
    39 #include "memory/heapShared.inline.hpp"
    40 #include "memory/heapShared.inline.hpp"
    40 #include "memory/iterator.inline.hpp"
    41 #include "memory/iterator.inline.hpp"
    41 #include "memory/metadataFactory.hpp"
    42 #include "memory/metadataFactory.hpp"
    53 #include "runtime/mutexLocker.hpp"
    54 #include "runtime/mutexLocker.hpp"
    54 #include "runtime/os.inline.hpp"
    55 #include "runtime/os.inline.hpp"
    55 #include "runtime/vm_version.hpp"
    56 #include "runtime/vm_version.hpp"
    56 #include "services/memTracker.hpp"
    57 #include "services/memTracker.hpp"
    57 #include "utilities/align.hpp"
    58 #include "utilities/align.hpp"
       
    59 #include "utilities/bitMap.inline.hpp"
    58 #include "utilities/classpathStream.hpp"
    60 #include "utilities/classpathStream.hpp"
    59 #include "utilities/defaultStream.hpp"
    61 #include "utilities/defaultStream.hpp"
    60 #if INCLUDE_G1GC
    62 #if INCLUDE_G1GC
    61 #include "gc/g1/g1CollectedHeap.hpp"
    63 #include "gc/g1/g1CollectedHeap.hpp"
    62 #include "gc/g1/heapRegion.hpp"
    64 #include "gc/g1/heapRegion.hpp"
    66 # include <errno.h>
    68 # include <errno.h>
    67 
    69 
    68 #ifndef O_BINARY       // if defined (Win32) use binary files.
    70 #ifndef O_BINARY       // if defined (Win32) use binary files.
    69 #define O_BINARY 0     // otherwise do nothing.
    71 #define O_BINARY 0     // otherwise do nothing.
    70 #endif
    72 #endif
    71 
       
    72 extern address JVM_FunctionAtStart();
       
    73 extern address JVM_FunctionAtEnd();
       
    74 
    73 
    75 // Complain and stop. All error conditions occurring during the writing of
    74 // Complain and stop. All error conditions occurring during the writing of
    76 // an archive file should stop the process.  Unrecoverable errors during
    75 // an archive file should stop the process.  Unrecoverable errors during
    77 // the reading of the archive file should stop the process.
    76 // the reading of the archive file should stop the process.
    78 
    77 
   102 // If we continue, then disable shared spaces and close the file.
   101 // If we continue, then disable shared spaces and close the file.
   103 
   102 
   104 void FileMapInfo::fail_continue(const char *msg, ...) {
   103 void FileMapInfo::fail_continue(const char *msg, ...) {
   105   va_list ap;
   104   va_list ap;
   106   va_start(ap, msg);
   105   va_start(ap, msg);
   107   if (_dynamic_archive_info == NULL) {
       
   108     MetaspaceShared::set_archive_loading_failed();
       
   109   } else {
       
   110     // _dynamic_archive_info has been setup after mapping the base archive
       
   111     DynamicArchive::disable();
       
   112   }
       
   113   if (PrintSharedArchiveAndExit && _validating_shared_path_table) {
   106   if (PrintSharedArchiveAndExit && _validating_shared_path_table) {
   114     // If we are doing PrintSharedArchiveAndExit and some of the classpath entries
   107     // If we are doing PrintSharedArchiveAndExit and some of the classpath entries
   115     // do not validate, we can still continue "limping" to validate the remaining
   108     // do not validate, we can still continue "limping" to validate the remaining
   116     // entries. No need to quit.
   109     // entries. No need to quit.
   117     tty->print("[");
   110     tty->print("[");
   126         LogStream ls(Log(cds)::info());
   119         LogStream ls(Log(cds)::info());
   127         ls.print("UseSharedSpaces: ");
   120         ls.print("UseSharedSpaces: ");
   128         ls.vprint_cr(msg, ap);
   121         ls.vprint_cr(msg, ap);
   129       }
   122       }
   130     }
   123     }
   131     if (_dynamic_archive_info == NULL) {
       
   132       UseSharedSpaces = false;
       
   133       assert(current_info() != NULL, "singleton must be registered");
       
   134       current_info()->close();
       
   135     } else {
       
   136       // We are failing when loading the top archive, but the base archive should
       
   137       // continue to work.
       
   138       log_warning(cds, dynamic)("Unable to use shared archive. The top archive failed to load: %s", _dynamic_archive_info->_full_path);
       
   139     }
       
   140   }
   124   }
   141   va_end(ap);
   125   va_end(ap);
   142 }
   126 }
   143 
   127 
   144 // Fill in the fileMapInfo structure with data about this VM instance.
   128 // Fill in the fileMapInfo structure with data about this VM instance.
   225   _compact_strings = CompactStrings;
   209   _compact_strings = CompactStrings;
   226   _narrow_oop_mode = CompressedOops::mode();
   210   _narrow_oop_mode = CompressedOops::mode();
   227   _narrow_oop_base = CompressedOops::base();
   211   _narrow_oop_base = CompressedOops::base();
   228   _narrow_oop_shift = CompressedOops::shift();
   212   _narrow_oop_shift = CompressedOops::shift();
   229   _max_heap_size = MaxHeapSize;
   213   _max_heap_size = MaxHeapSize;
   230   _narrow_klass_base = CompressedKlassPointers::base();
       
   231   _narrow_klass_shift = CompressedKlassPointers::shift();
   214   _narrow_klass_shift = CompressedKlassPointers::shift();
   232   _shared_path_table = mapinfo->_shared_path_table;
       
   233   if (HeapShared::is_heap_object_archiving_allowed()) {
   215   if (HeapShared::is_heap_object_archiving_allowed()) {
   234     _heap_end = CompressedOops::end();
   216     _heap_end = CompressedOops::end();
   235   }
   217   }
   236 
   218 
   237   // The following fields are for sanity checks for whether this archive
   219   // The following fields are for sanity checks for whether this archive
   247   _max_used_path_index = ClassLoaderExt::max_used_path_index();
   229   _max_used_path_index = ClassLoaderExt::max_used_path_index();
   248 
   230 
   249   _verify_local = BytecodeVerificationLocal;
   231   _verify_local = BytecodeVerificationLocal;
   250   _verify_remote = BytecodeVerificationRemote;
   232   _verify_remote = BytecodeVerificationRemote;
   251   _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
   233   _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
   252   _shared_base_address = SharedBaseAddress;
   234   _requested_base_address = (char*)SharedBaseAddress;
       
   235   _mapped_base_address = (char*)SharedBaseAddress;
   253   _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;
   236   _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;
   254   // the following 2 fields will be set in write_header for dynamic archive header
   237   // the following 2 fields will be set in write_header for dynamic archive header
   255   _base_archive_name_size = 0;
   238   _base_archive_name_size = 0;
   256   _base_archive_is_default = false;
   239   _base_archive_is_default = false;
       
   240 
       
   241   if (!DynamicDumpSharedSpaces) {
       
   242     set_shared_path_table(mapinfo->_shared_path_table);
       
   243   }
   257 }
   244 }
   258 
   245 
   259 void SharedClassPathEntry::init_as_non_existent(const char* path, TRAPS) {
   246 void SharedClassPathEntry::init_as_non_existent(const char* path, TRAPS) {
   260   _type = non_existent_entry;
   247   _type = non_existent_entry;
   261   set_name(path, THREAD);
   248   set_name(path, THREAD);
   613     }
   600     }
   614   }
   601   }
   615   return path_array;
   602   return path_array;
   616 }
   603 }
   617 
   604 
   618 bool FileMapInfo::fail(const char* msg, const char* name) {
   605 bool FileMapInfo::classpath_failure(const char* msg, const char* name) {
   619   ClassLoader::trace_class_path(msg, name);
   606   ClassLoader::trace_class_path(msg, name);
   620   MetaspaceShared::set_archive_loading_failed();
   607   if (PrintSharedArchiveAndExit) {
       
   608     MetaspaceShared::set_archive_loading_failed();
       
   609   }
   621   return false;
   610   return false;
   622 }
   611 }
   623 
   612 
   624 bool FileMapInfo::check_paths(int shared_path_start_idx, int num_paths, GrowableArray<const char*>* rp_array) {
   613 bool FileMapInfo::check_paths(int shared_path_start_idx, int num_paths, GrowableArray<const char*>* rp_array) {
   625   int i = 0;
   614   int i = 0;
   690     }
   679     }
   691   }
   680   }
   692 
   681 
   693   if (mismatch) {
   682   if (mismatch) {
   694     // The paths are different
   683     // The paths are different
   695     return fail("[BOOT classpath mismatch, actual =", runtime_boot_path);
   684     return classpath_failure("[BOOT classpath mismatch, actual =", runtime_boot_path);
   696   }
   685   }
   697   return true;
   686   return true;
   698 }
   687 }
   699 
   688 
   700 bool FileMapInfo::validate_app_class_paths(int shared_app_paths_len) {
   689 bool FileMapInfo::validate_app_class_paths(int shared_app_paths_len) {
   701   const char *appcp = Arguments::get_appclasspath();
   690   const char *appcp = Arguments::get_appclasspath();
   702   assert(appcp != NULL, "NULL app classpath");
   691   assert(appcp != NULL, "NULL app classpath");
   703   int rp_len = num_paths(appcp);
   692   int rp_len = num_paths(appcp);
   704   bool mismatch = false;
   693   bool mismatch = false;
   705   if (rp_len < shared_app_paths_len) {
   694   if (rp_len < shared_app_paths_len) {
   706     return fail("Run time APP classpath is shorter than the one at dump time: ", appcp);
   695     return classpath_failure("Run time APP classpath is shorter than the one at dump time: ", appcp);
   707   }
   696   }
   708   if (shared_app_paths_len != 0 && rp_len != 0) {
   697   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.
   698     // Prefix is OK: E.g., dump with -cp foo.jar, but run with -cp foo.jar:bar.jar.
   710     ResourceMark rm;
   699     ResourceMark rm;
   711     GrowableArray<const char*>* rp_array = create_path_array(appcp);
   700     GrowableArray<const char*>* rp_array = create_path_array(appcp);
   712     if (rp_array->length() == 0) {
   701     if (rp_array->length() == 0) {
   713       // None of the jar file specified in the runtime -cp exists.
   702       // 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);
   703       return classpath_failure("None of the jar file specified in the runtime -cp exists: -Djava.class.path=", appcp);
   715     }
   704     }
   716 
   705 
   717     // Handling of non-existent entries in the classpath: we eliminate all the non-existent
   706     // 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)
   707     // 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
   708     // and the runtime classpath (FileMapInfo::create_path_array), and check the remaining
   724     // run 2: -cp x.jar:NE4:b.jar      -> x.jar:b.jar -> mismatched
   713     // run 2: -cp x.jar:NE4:b.jar      -> x.jar:b.jar -> mismatched
   725 
   714 
   726     int j = header()->app_class_paths_start_index();
   715     int j = header()->app_class_paths_start_index();
   727     mismatch = check_paths(j, shared_app_paths_len, rp_array);
   716     mismatch = check_paths(j, shared_app_paths_len, rp_array);
   728     if (mismatch) {
   717     if (mismatch) {
   729       return fail("[APP classpath mismatch, actual: -Djava.class.path=", appcp);
   718       return classpath_failure("[APP classpath mismatch, actual: -Djava.class.path=", appcp);
   730     }
   719     }
   731   }
   720   }
   732   return true;
   721   return true;
   733 }
   722 }
   734 
   723 
   950   _shared_path_table = _current_info->header()->shared_path_table();
   939   _shared_path_table = _current_info->header()->shared_path_table();
   951 }
   940 }
   952 
   941 
   953 // Read the FileMapInfo information from the file.
   942 // Read the FileMapInfo information from the file.
   954 
   943 
   955 bool FileMapInfo::init_from_file(int fd, bool is_static) {
   944 bool FileMapInfo::init_from_file(int fd) {
   956   size_t sz = is_static ? sizeof(FileMapHeader) : sizeof(DynamicArchiveHeader);
   945   size_t sz = is_static() ? sizeof(FileMapHeader) : sizeof(DynamicArchiveHeader);
   957   size_t n = os::read(fd, header(), (unsigned int)sz);
   946   size_t n = os::read(fd, header(), (unsigned int)sz);
   958   if (n != sz) {
   947   if (n != sz) {
   959     fail_continue("Unable to read the file header.");
   948     fail_continue("Unable to read the file header.");
   960     return false;
   949     return false;
   961   }
   950   }
   963   if (!Arguments::has_jimage()) {
   952   if (!Arguments::has_jimage()) {
   964     FileMapInfo::fail_continue("The shared archive file cannot be used with an exploded module build.");
   953     FileMapInfo::fail_continue("The shared archive file cannot be used with an exploded module build.");
   965     return false;
   954     return false;
   966   }
   955   }
   967 
   956 
   968   unsigned int expected_magic = is_static ? CDS_ARCHIVE_MAGIC : CDS_DYNAMIC_ARCHIVE_MAGIC;
   957   unsigned int expected_magic = is_static() ? CDS_ARCHIVE_MAGIC : CDS_DYNAMIC_ARCHIVE_MAGIC;
   969   if (header()->magic() != expected_magic) {
   958   if (header()->magic() != expected_magic) {
   970     log_info(cds)("_magic expected: 0x%08x", expected_magic);
   959     log_info(cds)("_magic expected: 0x%08x", expected_magic);
   971     log_info(cds)("         actual: 0x%08x", header()->magic());
   960     log_info(cds)("         actual: 0x%08x", header()->magic());
   972     FileMapInfo::fail_continue("The shared archive file has a bad magic number.");
   961     FileMapInfo::fail_continue("The shared archive file has a bad magic number.");
   973     return false;
   962     return false;
  1014     }
  1003     }
  1015   }
  1004   }
  1016 
  1005 
  1017   _file_offset = n + header()->base_archive_name_size(); // accounts for the size of _base_archive_name
  1006   _file_offset = n + header()->base_archive_name_size(); // accounts for the size of _base_archive_name
  1018 
  1007 
  1019   if (is_static) {
  1008   if (is_static()) {
  1020     // just checking the last region is sufficient since the archive is written
  1009     // just checking the last region is sufficient since the archive is written
  1021     // in sequential order
  1010     // in sequential order
  1022     size_t len = lseek(fd, 0, SEEK_END);
  1011     size_t len = lseek(fd, 0, SEEK_END);
  1023     FileMapRegion* si = space_at(MetaspaceShared::last_valid_region);
  1012     FileMapRegion* si = space_at(MetaspaceShared::last_valid_region);
  1024     // The last space might be empty
  1013     // The last space might be empty
  1025     if (si->file_offset() > len || len - si->file_offset() < si->used()) {
  1014     if (si->file_offset() > len || len - si->file_offset() < si->used()) {
  1026       fail_continue("The shared archive file has been truncated.");
  1015       fail_continue("The shared archive file has been truncated.");
  1027       return false;
  1016       return false;
  1028     }
  1017     }
  1029 
       
  1030     SharedBaseAddress = header()->shared_base_address();
       
  1031   }
  1018   }
  1032 
  1019 
  1033   return true;
  1020   return true;
  1034 }
  1021 }
  1035 
  1022 
  1038     fail_stop("Unable to seek to position " SIZE_FORMAT, pos);
  1025     fail_stop("Unable to seek to position " SIZE_FORMAT, pos);
  1039   }
  1026   }
  1040 }
  1027 }
  1041 
  1028 
  1042 // Read the FileMapInfo information from the file.
  1029 // Read the FileMapInfo information from the file.
  1043 bool FileMapInfo::open_for_read(const char* path) {
  1030 bool FileMapInfo::open_for_read() {
  1044   if (_file_open) {
  1031   if (_file_open) {
  1045     return true;
  1032     return true;
  1046   }
  1033   }
  1047   if (path == NULL) {
  1034   if (is_static()) {
  1048     _full_path = Arguments::GetSharedArchivePath();
  1035     _full_path = Arguments::GetSharedArchivePath();
  1049   } else {
  1036   } else {
  1050     _full_path = path;
  1037     _full_path = Arguments::GetSharedDynamicArchivePath();
  1051   }
  1038   }
  1052   int fd = os::open(_full_path, O_RDONLY | O_BINARY, 0);
  1039   int fd = os::open(_full_path, O_RDONLY | O_BINARY, 0);
  1053   if (fd < 0) {
  1040   if (fd < 0) {
  1054     if (errno == ENOENT) {
  1041     if (is_static()) {
  1055       // Not locating the shared archive is ok.
  1042       if (errno == ENOENT) {
  1056       fail_continue("Specified shared archive not found (%s).", _full_path);
  1043         // Not locating the shared archive is ok.
       
  1044         fail_continue("Specified shared archive not found (%s).", _full_path);
       
  1045       } else {
       
  1046         fail_continue("Failed to open shared archive file (%s).",
       
  1047                       os::strerror(errno));
       
  1048       }
  1057     } else {
  1049     } else {
  1058       fail_continue("Failed to open shared archive file (%s).",
  1050       log_warning(cds, dynamic)("specified dynamic archive doesn't exist: %s", _full_path);
  1059                     os::strerror(errno));
       
  1060     }
  1051     }
  1061     return false;
  1052     return false;
  1062   }
  1053   }
  1063 
  1054 
  1064   _fd = fd;
  1055   _fd = fd;
  1125   if (base_archive_name != NULL) {
  1116   if (base_archive_name != NULL) {
  1126     write_bytes(base_archive_name, header()->base_archive_name_size());
  1117     write_bytes(base_archive_name, header()->base_archive_name_size());
  1127   }
  1118   }
  1128 }
  1119 }
  1129 
  1120 
  1130 void FileMapRegion::init(bool is_heap_region, char* base, size_t size, bool read_only,
  1121 size_t FileMapRegion::used_aligned() const {
       
  1122   return align_up(used(), os::vm_allocation_granularity());
       
  1123 }
       
  1124 
       
  1125 void FileMapRegion::init(int region_index, char* base, size_t size, bool read_only,
  1131                          bool allow_exec, int crc) {
  1126                          bool allow_exec, int crc) {
  1132   _is_heap_region = is_heap_region;
  1127   _is_heap_region = HeapShared::is_heap_region(region_index);
  1133 
  1128   _is_bitmap_region = (region_index == MetaspaceShared::bm);
  1134   if (is_heap_region) {
  1129   _mapping_offset = 0;
       
  1130 
       
  1131   if (_is_heap_region) {
  1135     assert(!DynamicDumpSharedSpaces, "must be");
  1132     assert(!DynamicDumpSharedSpaces, "must be");
  1136     assert((base - (char*)CompressedKlassPointers::base()) % HeapWordSize == 0, "Sanity");
  1133     assert((base - (char*)CompressedKlassPointers::base()) % HeapWordSize == 0, "Sanity");
  1137     if (base != NULL) {
  1134     if (base != NULL) {
  1138       _addr._offset = (intx)CompressedOops::encode_not_null((oop)base);
  1135       _mapping_offset = (size_t)CompressedOops::encode_not_null((oop)base);
  1139     } else {
  1136       assert(_mapping_offset >> 32 == 0, "must be 32-bit only");
  1140       _addr._offset = 0;
  1137     }
  1141     }
  1138   } else {
  1142   } else {
  1139     if (base != NULL) {
  1143     _addr._base = base;
  1140       assert(base >= (char*)SharedBaseAddress, "must be");
       
  1141       _mapping_offset = base - (char*)SharedBaseAddress;
       
  1142     }
  1144   }
  1143   }
  1145   _used = size;
  1144   _used = size;
  1146   _read_only = read_only;
  1145   _read_only = read_only;
  1147   _allow_exec = allow_exec;
  1146   _allow_exec = allow_exec;
  1148   _crc = crc;
  1147   _crc = crc;
       
  1148   _mapped_from_file = false;
       
  1149   _mapped_base = NULL;
  1149 }
  1150 }
  1150 
  1151 
  1151 void FileMapInfo::write_region(int region, char* base, size_t size,
  1152 void FileMapInfo::write_region(int region, char* base, size_t size,
  1152                                bool read_only, bool allow_exec) {
  1153                                bool read_only, bool allow_exec) {
  1153   Arguments::assert_is_dumping_archive();
  1154   Arguments::assert_is_dumping_archive();
  1154 
  1155 
  1155   FileMapRegion* si = space_at(region);
  1156   FileMapRegion* si = space_at(region);
  1156   char* target_base = base;
  1157   char* target_base;
  1157   if (DynamicDumpSharedSpaces) {
  1158 
  1158     assert(!HeapShared::is_heap_region(region), "dynamic archive doesn't support heap regions");
  1159   if (region == MetaspaceShared::bm) {
  1159     target_base = DynamicArchive::buffer_to_target(base);
  1160     target_base = NULL; // always NULL for bm region.
       
  1161   } else {
       
  1162     if (DynamicDumpSharedSpaces) {
       
  1163       assert(!HeapShared::is_heap_region(region), "dynamic archive doesn't support heap regions");
       
  1164       target_base = DynamicArchive::buffer_to_target(base);
       
  1165     } else {
       
  1166       target_base = base;
       
  1167     }
  1160   }
  1168   }
  1161 
  1169 
  1162   si->set_file_offset(_file_offset);
  1170   si->set_file_offset(_file_offset);
  1163   log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08)
  1171   char* requested_base = (target_base == NULL) ? NULL : target_base + MetaspaceShared::final_delta();
       
  1172   log_info(cds)("Shared file region  %d: " SIZE_FORMAT_HEX_W(08)
  1164                 " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),
  1173                 " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),
  1165                 region, size, p2i(target_base), _file_offset);
  1174                 region, size, p2i(requested_base), _file_offset);
  1166 
  1175 
  1167   int crc = ClassLoader::crc32(0, base, (jint)size);
  1176   int crc = ClassLoader::crc32(0, base, (jint)size);
  1168   si->init(HeapShared::is_heap_region(region), target_base, size, read_only, allow_exec, crc);
  1177   si->init(region, target_base, size, read_only, allow_exec, crc);
  1169 
  1178 
  1170   if (base != NULL) {
  1179   if (base != NULL) {
  1171     write_bytes_aligned(base, size);
  1180     write_bytes_aligned(base, size);
  1172   }
  1181   }
       
  1182 }
       
  1183 
       
  1184 
       
  1185 void FileMapInfo::write_bitmap_region(const CHeapBitMap* ptrmap) {
       
  1186   ResourceMark rm;
       
  1187   size_t size_in_bits = ptrmap->size();
       
  1188   size_t size_in_bytes = ptrmap->size_in_bytes();
       
  1189   uintptr_t* buffer = (uintptr_t*)NEW_RESOURCE_ARRAY(char, size_in_bytes);
       
  1190   ptrmap->write_to(buffer, size_in_bytes);
       
  1191   header()->set_ptrmap_size_in_bits(size_in_bits);
       
  1192 
       
  1193   log_info(cds)("ptrmap = " INTPTR_FORMAT " (" SIZE_FORMAT " bytes)",
       
  1194                 p2i(buffer), size_in_bytes);
       
  1195   write_region(MetaspaceShared::bm, (char*)buffer, size_in_bytes, /*read_only=*/true, /*allow_exec=*/false);
  1173 }
  1196 }
  1174 
  1197 
  1175 // Write out the given archive heap memory regions.  GC code combines multiple
  1198 // Write out the given archive heap memory regions.  GC code combines multiple
  1176 // consecutive archive GC regions into one MemRegion whenever possible and
  1199 // consecutive archive GC regions into one MemRegion whenever possible and
  1177 // produces the 'heap_mem' array.
  1200 // produces the 'heap_mem' array.
  1227       start = (char*)heap_mem->at(arr_idx).start();
  1250       start = (char*)heap_mem->at(arr_idx).start();
  1228       size = heap_mem->at(arr_idx).byte_size();
  1251       size = heap_mem->at(arr_idx).byte_size();
  1229       total_size += size;
  1252       total_size += size;
  1230     }
  1253     }
  1231 
  1254 
  1232     log_info(cds)("Archive heap region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes",
  1255     log_info(cds)("Archive heap region %d: " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes",
  1233                   i, p2i(start), p2i(start + size), size);
  1256                   i, p2i(start), p2i(start + size), size);
  1234     write_region(i, start, size, false, false);
  1257     write_region(i, start, size, false, false);
  1235     if (size > 0) {
  1258     if (size > 0) {
  1236       space_at(i)->init_oopmap(oopmaps->at(arr_idx)._oopmap,
  1259       address oopmap = oopmaps->at(arr_idx)._oopmap;
       
  1260       assert(oopmap >= (address)SharedBaseAddress, "must be");
       
  1261       space_at(i)->init_oopmap(oopmap - (address)SharedBaseAddress,
  1237                                oopmaps->at(arr_idx)._oopmap_size_in_bits);
  1262                                oopmaps->at(arr_idx)._oopmap_size_in_bits);
  1238     }
  1263     }
  1239   }
  1264   }
  1240   return total_size;
  1265   return total_size;
  1241 }
  1266 }
  1283   align_file_position();
  1308   align_file_position();
  1284   write_bytes(buffer, nbytes);
  1309   write_bytes(buffer, nbytes);
  1285   align_file_position();
  1310   align_file_position();
  1286 }
  1311 }
  1287 
  1312 
       
  1313 void FileMapInfo::set_final_requested_base(char* b) {
       
  1314   header()->set_final_requested_base(b);
       
  1315 }
  1288 
  1316 
  1289 // Close the shared archive file.  This does NOT unmap mapped regions.
  1317 // Close the shared archive file.  This does NOT unmap mapped regions.
  1290 
  1318 
  1291 void FileMapInfo::close() {
  1319 void FileMapInfo::close() {
  1292   if (_file_open) {
  1320   if (_file_open) {
  1329   }
  1357   }
  1330   si->set_read_only(false);
  1358   si->set_read_only(false);
  1331   return true;
  1359   return true;
  1332 }
  1360 }
  1333 
  1361 
  1334 // Map the whole region at once, assumed to be allocated contiguously.
       
  1335 ReservedSpace FileMapInfo::reserve_shared_memory() {
       
  1336   char* requested_addr = region_addr(0);
       
  1337   size_t size = FileMapInfo::core_spaces_size();
       
  1338 
       
  1339   // Reserve the space first, then map otherwise map will go right over some
       
  1340   // other reserved memory (like the code cache).
       
  1341   ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
       
  1342   if (!rs.is_reserved()) {
       
  1343     fail_continue("Unable to reserve shared space at required address "
       
  1344                   INTPTR_FORMAT, p2i(requested_addr));
       
  1345     return rs;
       
  1346   }
       
  1347   // the reserved virtual memory is for mapping class data sharing archive
       
  1348   MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
       
  1349 
       
  1350   return rs;
       
  1351 }
       
  1352 
       
  1353 // Memory map a region in the address space.
  1362 // Memory map a region in the address space.
  1354 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode",
  1363 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "Bitmap",
  1355                                             "String1", "String2", "OpenArchive1", "OpenArchive2" };
  1364                                             "String1", "String2", "OpenArchive1", "OpenArchive2" };
  1356 
  1365 
  1357 char* FileMapInfo::map_regions(int regions[], char* saved_base[], size_t len) {
  1366 MapArchiveResult FileMapInfo::map_regions(int regions[], int num_regions, char* mapped_base_address, ReservedSpace rs) {
  1358   char* prev_top = NULL;
  1367   DEBUG_ONLY(FileMapRegion* last_region = NULL);
  1359   char* curr_base;
  1368   intx addr_delta = mapped_base_address - header()->requested_base_address();
  1360   char* curr_top;
  1369 
  1361   int i = 0;
  1370   // Make sure we don't attempt to use header()->mapped_base_address() unless
  1362   for (i = 0; i < (int)len; i++) {
  1371   // it's been successfully mapped.
  1363     curr_base = map_region(regions[i], &curr_top);
  1372   DEBUG_ONLY(header()->set_mapped_base_address((char*)(uintptr_t)0xdeadbeef);)
  1364     if (curr_base == NULL) {
  1373 
  1365       return NULL;
  1374   for (int r = 0; r < num_regions; r++) {
  1366     }
  1375     int idx = regions[r];
  1367     if (i > 0) {
  1376     MapArchiveResult result = map_region(idx, addr_delta, mapped_base_address, rs);
  1368       // We require that mc->rw->ro->md to be laid out consecutively, with no
  1377     if (result != MAP_ARCHIVE_SUCCESS) {
  1369       // gaps between them. That way, we can ensure that the OS won't be able to
  1378       return result;
  1370       // allocate any new memory spaces inside _shared_metaspace_{base,top}, which
  1379     }
  1371       // would mess up the simple comparision in MetaspaceShared::is_in_shared_metaspace().
  1380     FileMapRegion* si = space_at(idx);
  1372       assert(curr_base == prev_top, "must be");
  1381     DEBUG_ONLY(if (last_region != NULL) {
  1373     }
  1382         // Ensure that the OS won't be able to allocate new memory spaces between any mapped
  1374     log_info(cds)("Mapped region #%d at base %p top %p", regions[i], curr_base, curr_top);
  1383         // regions, or else it would mess up the simple comparision in MetaspaceObj::is_shared().
  1375     saved_base[i] = curr_base;
  1384         assert(si->mapped_base() == last_region->mapped_end(), "must have no gaps");
  1376     prev_top = curr_top;
  1385       }
  1377   }
  1386       last_region = si;)
  1378   return curr_top;
  1387     log_info(cds)("Mapped %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)", is_static() ? "static " : "dynamic",
  1379 }
  1388                   idx, p2i(si->mapped_base()), p2i(si->mapped_end()),
  1380 
  1389                   shared_region_name[idx]);
  1381 char* FileMapInfo::map_region(int i, char** top_ret) {
  1390 
       
  1391   }
       
  1392 
       
  1393   DEBUG_ONLY(if (addr_delta == 0 && ArchiveRelocationMode == 1) {
       
  1394       // This is for simulating mmap failures at the requested address. We do it here (instead
       
  1395       // of MetaspaceShared::map_archives) so we can thoroughly test the code for failure handling
       
  1396       // (releasing all allocated resource, etc).
       
  1397       log_info(cds)("ArchiveRelocationMode == 1: always map archive(s) at an alternative address");
       
  1398       return MAP_ARCHIVE_MMAP_FAILURE;
       
  1399     });
       
  1400 
       
  1401   header()->set_mapped_base_address(header()->requested_base_address() + addr_delta);
       
  1402   if (addr_delta != 0 && !relocate_pointers(addr_delta)) {
       
  1403     return MAP_ARCHIVE_OTHER_FAILURE;
       
  1404   }
       
  1405 
       
  1406   return MAP_ARCHIVE_SUCCESS;
       
  1407 }
       
  1408 
       
  1409 bool FileMapInfo::read_region(int i, char* base, size_t size) {
       
  1410   assert(MetaspaceShared::use_windows_memory_mapping(), "used by windows only");
       
  1411   FileMapRegion* si = space_at(i);
       
  1412   log_info(cds)("Commit %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)%s",
       
  1413                 is_static() ? "static " : "dynamic", i, p2i(base), p2i(base + size),
       
  1414                 shared_region_name[i], si->allow_exec() ? " exec" : "");
       
  1415   if (!os::commit_memory(base, size, si->allow_exec())) {
       
  1416     log_error(cds)("Failed to commit %s region #%d (%s)", is_static() ? "static " : "dynamic",
       
  1417                    i, shared_region_name[i]);
       
  1418     return false;
       
  1419   }
       
  1420   if (lseek(_fd, (long)si->file_offset(), SEEK_SET) != (int)si->file_offset() ||
       
  1421       read_bytes(base, size) != size) {
       
  1422     return false;
       
  1423   }
       
  1424   return true;
       
  1425 }
       
  1426 
       
  1427 MapArchiveResult FileMapInfo::map_region(int i, intx addr_delta, char* mapped_base_address, ReservedSpace rs) {
  1382   assert(!HeapShared::is_heap_region(i), "sanity");
  1428   assert(!HeapShared::is_heap_region(i), "sanity");
  1383   FileMapRegion* si = space_at(i);
  1429   FileMapRegion* si = space_at(i);
  1384   size_t used = si->used();
  1430   size_t size = si->used_aligned();
  1385   size_t alignment = os::vm_allocation_granularity();
  1431   char *requested_addr = mapped_base_address + si->mapping_offset();
  1386   size_t size = align_up(used, alignment);
  1432   assert(si->mapped_base() == NULL, "must be not mapped yet");
  1387   char *requested_addr = region_addr(i);
  1433   assert(requested_addr != NULL, "must be specified");
  1388 
  1434 
  1389 #ifdef _WINDOWS
  1435   si->set_mapped_from_file(false);
  1390   // Windows cannot remap read-only shared memory to read-write when required for
  1436 
  1391   // RedefineClasses, which is also used by JFR.  Always map windows regions as RW.
  1437   if (MetaspaceShared::use_windows_memory_mapping()) {
  1392   si->set_read_only(false);
  1438     // Windows cannot remap read-only shared memory to read-write when required for
  1393 #else
  1439     // RedefineClasses, which is also used by JFR.  Always map windows regions as RW.
  1394   // If a tool agent is in use (debugging enabled), or JFR, we must map the address space RW
       
  1395   if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space() ||
       
  1396       Arguments::has_jfr_option()) {
       
  1397     si->set_read_only(false);
  1440     si->set_read_only(false);
  1398   }
  1441   } else if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space() ||
  1399 #endif // _WINDOWS
  1442              Arguments::has_jfr_option()) {
  1400 
  1443     // If a tool agent is in use (debugging enabled), or JFR, we must map the address space RW
  1401   // map the contents of the CDS archive in this memory
  1444     si->set_read_only(false);
  1402   char *base = os::map_memory(_fd, _full_path, si->file_offset(),
  1445   } else if (addr_delta != 0) {
  1403                               requested_addr, size, si->read_only(),
  1446     si->set_read_only(false); // Need to patch the pointers
  1404                               si->allow_exec());
  1447   }
  1405   if (base == NULL || base != requested_addr) {
  1448 
  1406     fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
  1449   if (rs.is_reserved()) {
  1407     _memory_mapping_failed = true;
  1450     assert(rs.contains(requested_addr) && rs.contains(requested_addr + size - 1), "must be");
       
  1451     MemTracker::record_virtual_memory_type((address)requested_addr, mtClassShared);
       
  1452   }
       
  1453 
       
  1454   if (MetaspaceShared::use_windows_memory_mapping() && addr_delta != 0) {
       
  1455     // This is the second time we try to map the archive(s). We have already created a ReservedSpace
       
  1456     // that covers all the FileMapRegions to ensure all regions can be mapped. However, Windows
       
  1457     // can't mmap into a ReservedSpace, so we just os::read() the data. We're going to patch all the
       
  1458     // regions anyway, so there's no benefit for mmap anyway.
       
  1459     if (!read_region(i, requested_addr, size)) {
       
  1460       return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
       
  1461     }
       
  1462   } else {
       
  1463     char* base = os::map_memory(_fd, _full_path, si->file_offset(),
       
  1464                                 requested_addr, size, si->read_only(),
       
  1465                                 si->allow_exec());
       
  1466     if (base != requested_addr) {
       
  1467       log_info(cds)("Unable to map %s shared space at required address.", shared_region_name[i]);
       
  1468       _memory_mapping_failed = true;
       
  1469       return MAP_ARCHIVE_MMAP_FAILURE;
       
  1470     }
       
  1471     si->set_mapped_from_file(true);
       
  1472   }
       
  1473   si->set_mapped_base(requested_addr);
       
  1474 
       
  1475   if (!rs.is_reserved()) {
       
  1476     // When mapping on Windows with (addr_delta == 0), we don't reserve the address space for the regions
       
  1477     // (Windows can't mmap into a ReservedSpace). In this case, NMT requires we call it after
       
  1478     // os::map_memory has succeeded.
       
  1479     assert(MetaspaceShared::use_windows_memory_mapping(), "Windows memory mapping only");
       
  1480     MemTracker::record_virtual_memory_type((address)requested_addr, mtClassShared);
       
  1481   }
       
  1482 
       
  1483   if (VerifySharedSpaces && !verify_region_checksum(i)) {
       
  1484     return MAP_ARCHIVE_OTHER_FAILURE;
       
  1485   }
       
  1486 
       
  1487   return MAP_ARCHIVE_SUCCESS;
       
  1488 }
       
  1489 
       
  1490 char* FileMapInfo::map_relocation_bitmap(size_t& bitmap_size) {
       
  1491   FileMapRegion* si = space_at(MetaspaceShared::bm);
       
  1492   bitmap_size = si->used_aligned();
       
  1493   bool read_only = true, allow_exec = false;
       
  1494   char* requested_addr = NULL; // allow OS to pick any location
       
  1495   char* bitmap_base = os::map_memory(_fd, _full_path, si->file_offset(),
       
  1496                                      requested_addr, bitmap_size, read_only, allow_exec);
       
  1497   if (bitmap_base == NULL) {
       
  1498     log_error(cds)("failed to map relocation bitmap");
  1408     return NULL;
  1499     return NULL;
  1409   }
  1500   }
  1410 #ifdef _WINDOWS
  1501 
  1411   // This call is Windows-only because the memory_type gets recorded for the other platforms
  1502   if (VerifySharedSpaces && !region_crc_check(bitmap_base, bitmap_size, si->crc())) {
  1412   // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
  1503     log_error(cds)("relocation bitmap CRC error");
  1413   MemTracker::record_virtual_memory_type((address)base, mtClassShared);
  1504     if (!os::unmap_memory(bitmap_base, bitmap_size)) {
  1414 #endif
  1505       fatal("os::unmap_memory of relocation bitmap failed");
  1415 
  1506     }
  1416   if (VerifySharedSpaces && !verify_region_checksum(i)) {
       
  1417     return NULL;
  1507     return NULL;
  1418   }
  1508   }
  1419 
  1509 
  1420   *top_ret = base + size;
  1510   return bitmap_base;
  1421   return base;
  1511 }
       
  1512 
       
  1513 bool FileMapInfo::relocate_pointers(intx addr_delta) {
       
  1514   log_debug(cds, reloc)("runtime archive relocation start");
       
  1515   size_t bitmap_size;
       
  1516   char* bitmap_base = map_relocation_bitmap(bitmap_size);
       
  1517 
       
  1518   if (bitmap_base == NULL) {
       
  1519     return false;
       
  1520   } else {
       
  1521     size_t ptrmap_size_in_bits = header()->ptrmap_size_in_bits();
       
  1522     log_debug(cds, reloc)("mapped relocation bitmap @ " INTPTR_FORMAT " (" SIZE_FORMAT
       
  1523                           " bytes = " SIZE_FORMAT " bits)",
       
  1524                           p2i(bitmap_base), bitmap_size, ptrmap_size_in_bits);
       
  1525 
       
  1526     BitMapView ptrmap((BitMap::bm_word_t*)bitmap_base, ptrmap_size_in_bits);
       
  1527 
       
  1528     // Patch all pointers in the the mapped region that are marked by ptrmap.
       
  1529     address patch_base = (address)mapped_base();
       
  1530     address patch_end  = (address)mapped_end();
       
  1531 
       
  1532     // the current value of the pointers to be patched must be within this
       
  1533     // range (i.e., must be between the requesed base address, and the of the current archive).
       
  1534     // Note: top archive may point to objects in the base archive, but not the other way around.
       
  1535     address valid_old_base = (address)header()->requested_base_address();
       
  1536     address valid_old_end  = valid_old_base + mapping_end_offset();
       
  1537 
       
  1538     // after patching, the pointers must point inside this range
       
  1539     // (the requested location of the archive, as mapped at runtime).
       
  1540     address valid_new_base = (address)header()->mapped_base_address();
       
  1541     address valid_new_end  = (address)mapped_end();
       
  1542 
       
  1543     SharedDataRelocator<false> patcher((address*)patch_base, (address*)patch_end, valid_old_base, valid_old_end,
       
  1544                                        valid_new_base, valid_new_end, addr_delta);
       
  1545     ptrmap.iterate(&patcher);
       
  1546 
       
  1547     if (!os::unmap_memory(bitmap_base, bitmap_size)) {
       
  1548       fatal("os::unmap_memory of relocation bitmap failed");
       
  1549     }
       
  1550     log_debug(cds, reloc)("runtime archive relocation done");
       
  1551     return true;
       
  1552   }
  1422 }
  1553 }
  1423 
  1554 
  1424 size_t FileMapInfo::read_bytes(void* buffer, size_t count) {
  1555 size_t FileMapInfo::read_bytes(void* buffer, size_t count) {
  1425   assert(_file_open, "Archive file is not open");
  1556   assert(_file_open, "Archive file is not open");
  1426   size_t n = os::read(_fd, buffer, (unsigned int)count);
  1557   size_t n = os::read(_fd, buffer, (unsigned int)count);
  1432   _file_offset += count;
  1563   _file_offset += count;
  1433   return count;
  1564   return count;
  1434 }
  1565 }
  1435 
  1566 
  1436 address FileMapInfo::decode_start_address(FileMapRegion* spc, bool with_current_oop_encoding_mode) {
  1567 address FileMapInfo::decode_start_address(FileMapRegion* spc, bool with_current_oop_encoding_mode) {
       
  1568   size_t offset = spc->mapping_offset();
       
  1569   assert((offset >> 32) == 0, "must be 32-bit only");
       
  1570   uint n = (uint)offset;
  1437   if (with_current_oop_encoding_mode) {
  1571   if (with_current_oop_encoding_mode) {
  1438     return (address)CompressedOops::decode_not_null(spc->offset());
  1572     return (address)CompressedOops::decode_not_null(n);
  1439   } else {
  1573   } else {
  1440     return (address)HeapShared::decode_from_archive(spc->offset());
  1574     return (address)HeapShared::decode_from_archive(n);
  1441   }
  1575   }
  1442 }
  1576 }
  1443 
  1577 
  1444 static MemRegion *closed_archive_heap_ranges = NULL;
  1578 static MemRegion *closed_archive_heap_ranges = NULL;
  1445 static MemRegion *open_archive_heap_ranges = NULL;
  1579 static MemRegion *open_archive_heap_ranges = NULL;
  1703 
  1837 
  1704 void FileMapInfo::patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges,
  1838 void FileMapInfo::patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges,
  1705                                                         int first_region_idx) {
  1839                                                         int first_region_idx) {
  1706   for (int i=0; i<num_ranges; i++) {
  1840   for (int i=0; i<num_ranges; i++) {
  1707     FileMapRegion* si = space_at(i + first_region_idx);
  1841     FileMapRegion* si = space_at(i + first_region_idx);
  1708     HeapShared::patch_archived_heap_embedded_pointers(ranges[i], (address)si->oopmap(),
  1842     HeapShared::patch_archived_heap_embedded_pointers(ranges[i], (address)(SharedBaseAddress + si->oopmap_offset()),
  1709                                                       si->oopmap_size_in_bits());
  1843                                                       si->oopmap_size_in_bits());
  1710   }
  1844   }
  1711 }
  1845 }
  1712 
  1846 
  1713 // This internally allocates objects using SystemDictionary::Object_klass(), so it
  1847 // This internally allocates objects using SystemDictionary::Object_klass(), so it
  1757   } else {
  1891   } else {
  1758     return region_crc_check(region_addr(i), sz, space_at(i)->crc());
  1892     return region_crc_check(region_addr(i), sz, space_at(i)->crc());
  1759   }
  1893   }
  1760 }
  1894 }
  1761 
  1895 
  1762 void FileMapInfo::unmap_regions(int regions[], char* saved_base[], size_t len) {
  1896 void FileMapInfo::unmap_regions(int regions[], int num_regions) {
  1763   for (int i = 0; i < (int)len; i++) {
  1897   for (int r = 0; r < num_regions; r++) {
  1764     if (saved_base[i] != NULL) {
  1898     int idx = regions[r];
  1765       unmap_region(regions[i]);
  1899     unmap_region(idx);
  1766     }
       
  1767   }
  1900   }
  1768 }
  1901 }
  1769 
  1902 
  1770 // Unmap a memory region in the address space.
  1903 // Unmap a memory region in the address space.
  1771 
  1904 
  1772 void FileMapInfo::unmap_region(int i) {
  1905 void FileMapInfo::unmap_region(int i) {
  1773   assert(!HeapShared::is_heap_region(i), "sanity");
  1906   assert(!HeapShared::is_heap_region(i), "sanity");
  1774   FileMapRegion* si = space_at(i);
  1907   FileMapRegion* si = space_at(i);
       
  1908   char* mapped_base = si->mapped_base();
  1775   size_t used = si->used();
  1909   size_t used = si->used();
  1776   size_t size = align_up(used, os::vm_allocation_granularity());
  1910   size_t size = align_up(used, os::vm_allocation_granularity());
  1777 
  1911 
  1778   if (used == 0) {
  1912   if (mapped_base != NULL && size > 0 && si->mapped_from_file()) {
  1779     return;
  1913     log_info(cds)("Unmapping region #%d at base " INTPTR_FORMAT " (%s)", i, p2i(mapped_base),
  1780   }
  1914                   shared_region_name[i]);
  1781 
  1915     if (!os::unmap_memory(mapped_base, size)) {
  1782   char* addr = region_addr(i);
  1916       fatal("os::unmap_memory failed");
  1783   if (!os::unmap_memory(addr, size)) {
  1917     }
  1784     fail_stop("Unable to unmap shared space.");
  1918     si->set_mapped_base(NULL);
  1785   }
  1919   }
  1786 }
  1920 }
  1787 
  1921 
  1788 void FileMapInfo::assert_mark(bool check) {
  1922 void FileMapInfo::assert_mark(bool check) {
  1789   if (!check) {
  1923   if (!check) {
  1811 // Validation of the archive is done in two steps:
  1945 // Validation of the archive is done in two steps:
  1812 //
  1946 //
  1813 // [1] validate_header() - done here.
  1947 // [1] validate_header() - done here.
  1814 // [2] validate_shared_path_table - this is done later, because the table is in the RW
  1948 // [2] validate_shared_path_table - this is done later, because the table is in the RW
  1815 //     region of the archive, which is not mapped yet.
  1949 //     region of the archive, which is not mapped yet.
  1816 bool FileMapInfo::initialize(bool is_static) {
  1950 bool FileMapInfo::initialize() {
  1817   assert(UseSharedSpaces, "UseSharedSpaces expected.");
  1951   assert(UseSharedSpaces, "UseSharedSpaces expected.");
  1818 
  1952 
  1819   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
  1953   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
  1820     // CDS assumes that no classes resolved in SystemDictionary::resolve_well_known_classes
  1954     // CDS assumes that no classes resolved in SystemDictionary::resolve_well_known_classes
  1821     // are replaced at runtime by JVMTI ClassFileLoadHook. All of those classes are resolved
  1955     // are replaced at runtime by JVMTI ClassFileLoadHook. All of those classes are resolved
  1826   }
  1960   }
  1827 
  1961 
  1828   if (!open_for_read()) {
  1962   if (!open_for_read()) {
  1829     return false;
  1963     return false;
  1830   }
  1964   }
  1831 
  1965   if (!init_from_file(_fd)) {
  1832   init_from_file(_fd, is_static);
  1966     return false;
  1833   // UseSharedSpaces could be disabled if the checking of some of the header fields in
  1967   }
  1834   // init_from_file has failed.
  1968   if (!validate_header()) {
  1835   if (!UseSharedSpaces || !validate_header(is_static)) {
       
  1836     return false;
  1969     return false;
  1837   }
  1970   }
  1838   return true;
  1971   return true;
  1839 }
  1972 }
  1840 
  1973 
  1843   if (HeapShared::is_heap_region(idx)) {
  1976   if (HeapShared::is_heap_region(idx)) {
  1844     assert(DumpSharedSpaces, "The following doesn't work at runtime");
  1977     assert(DumpSharedSpaces, "The following doesn't work at runtime");
  1845     return si->used() > 0 ?
  1978     return si->used() > 0 ?
  1846           (char*)start_address_as_decoded_with_current_oop_encoding_mode(si) : NULL;
  1979           (char*)start_address_as_decoded_with_current_oop_encoding_mode(si) : NULL;
  1847   } else {
  1980   } else {
  1848     return si->base();
  1981     return si->mapped_base();
  1849   }
  1982   }
       
  1983 }
       
  1984 
       
  1985 FileMapRegion* FileMapInfo::first_core_space() const {
       
  1986   return is_static() ? space_at(MetaspaceShared::mc) : space_at(MetaspaceShared::rw);
       
  1987 }
       
  1988 
       
  1989 FileMapRegion* FileMapInfo::last_core_space() const {
       
  1990   return is_static() ? space_at(MetaspaceShared::md) : space_at(MetaspaceShared::mc);
  1850 }
  1991 }
  1851 
  1992 
  1852 int FileMapHeader::compute_crc() {
  1993 int FileMapHeader::compute_crc() {
  1853   char* start = (char*)this;
  1994   char* start = (char*)this;
  1854   // start computing from the field after _crc
  1995   // start computing from the field after _crc
  1858   return crc;
  1999   return crc;
  1859 }
  2000 }
  1860 
  2001 
  1861 // This function should only be called during run time with UseSharedSpaces enabled.
  2002 // This function should only be called during run time with UseSharedSpaces enabled.
  1862 bool FileMapHeader::validate() {
  2003 bool FileMapHeader::validate() {
  1863 
       
  1864   if (_obj_alignment != ObjectAlignmentInBytes) {
  2004   if (_obj_alignment != ObjectAlignmentInBytes) {
  1865     FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d"
  2005     FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d"
  1866                   " does not equal the current ObjectAlignmentInBytes of " INTX_FORMAT ".",
  2006                   " does not equal the current ObjectAlignmentInBytes of " INTX_FORMAT ".",
  1867                   _obj_alignment, ObjectAlignmentInBytes);
  2007                   _obj_alignment, ObjectAlignmentInBytes);
  1868     return false;
  2008     return false;
  1911   }
  2051   }
  1912 
  2052 
  1913   return true;
  2053   return true;
  1914 }
  2054 }
  1915 
  2055 
  1916 bool FileMapInfo::validate_header(bool is_static) {
  2056 bool FileMapInfo::validate_header() {
  1917   return header()->validate();
  2057   return header()->validate();
  1918 }
  2058 }
  1919 
  2059 
  1920 // Check if a given address is within one of the shared regions
  2060 // Check if a given address is within one of the shared regions
  1921 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
  2061 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
  1930   return false;
  2070   return false;
  1931 }
  2071 }
  1932 
  2072 
  1933 // Unmap mapped regions of shared space.
  2073 // Unmap mapped regions of shared space.
  1934 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
  2074 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
  1935   MetaspaceShared::set_shared_metaspace_range(NULL, NULL);
  2075   MetaspaceShared::set_shared_metaspace_range(NULL, NULL, NULL);
  1936 
  2076 
  1937   FileMapInfo *map_info = FileMapInfo::current_info();
  2077   FileMapInfo *map_info = FileMapInfo::current_info();
  1938   if (map_info) {
  2078   if (map_info) {
  1939     map_info->fail_continue("%s", msg);
  2079     map_info->fail_continue("%s", msg);
  1940     for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) {
  2080     for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) {
  1941       if (!HeapShared::is_heap_region(i)) {
  2081       if (!HeapShared::is_heap_region(i)) {
  1942         char *addr = map_info->region_addr(i);
  2082         map_info->unmap_region(i);
  1943         if (addr != NULL) {
       
  1944           map_info->unmap_region(i);
       
  1945           map_info->space_at(i)->mark_invalid();
       
  1946         }
       
  1947       }
  2083       }
  1948     }
  2084     }
  1949     // Dealloc the archive heap regions only without unmapping. The regions are part
  2085     // Dealloc the archive heap regions only without unmapping. The regions are part
  1950     // of the java heap. Unmapping of the heap regions are managed by GC.
  2086     // of the java heap. Unmapping of the heap regions are managed by GC.
  1951     map_info->dealloc_archive_heap_regions(open_archive_heap_ranges,
  2087     map_info->dealloc_archive_heap_regions(open_archive_heap_ranges,