< prev index next >

src/hotspot/share/memory/filemap.cpp

Print this page


  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "classfile/classFileStream.hpp"
  28 #include "classfile/classLoader.inline.hpp"
  29 #include "classfile/classLoaderData.inline.hpp"
  30 #include "classfile/classLoaderExt.hpp"
  31 #include "classfile/symbolTable.hpp"
  32 #include "classfile/systemDictionaryShared.hpp"
  33 #include "classfile/altHashing.hpp"
  34 #include "logging/log.hpp"
  35 #include "logging/logStream.hpp"
  36 #include "logging/logMessage.hpp"

  37 #include "memory/dynamicArchive.hpp"
  38 #include "memory/filemap.hpp"
  39 #include "memory/heapShared.inline.hpp"
  40 #include "memory/iterator.inline.hpp"
  41 #include "memory/metadataFactory.hpp"
  42 #include "memory/metaspaceClosure.hpp"
  43 #include "memory/metaspaceShared.hpp"
  44 #include "memory/oopFactory.hpp"
  45 #include "memory/universe.hpp"
  46 #include "oops/compressedOops.hpp"
  47 #include "oops/compressedOops.inline.hpp"
  48 #include "oops/objArrayOop.hpp"
  49 #include "oops/oop.inline.hpp"
  50 #include "prims/jvmtiExport.hpp"
  51 #include "runtime/arguments.hpp"
  52 #include "runtime/java.hpp"
  53 #include "runtime/mutexLocker.hpp"
  54 #include "runtime/os.inline.hpp"
  55 #include "runtime/vm_version.hpp"
  56 #include "services/memTracker.hpp"
  57 #include "utilities/align.hpp"

  58 #include "utilities/classpathStream.hpp"
  59 #include "utilities/defaultStream.hpp"
  60 #if INCLUDE_G1GC
  61 #include "gc/g1/g1CollectedHeap.hpp"
  62 #include "gc/g1/heapRegion.hpp"
  63 #endif
  64 
  65 # include <sys/stat.h>
  66 # include <errno.h>
  67 
  68 #ifndef O_BINARY       // if defined (Win32) use binary files.
  69 #define O_BINARY 0     // otherwise do nothing.
  70 #endif
  71 
  72 extern address JVM_FunctionAtStart();
  73 extern address JVM_FunctionAtEnd();
  74 
  75 // Complain and stop. All error conditions occurring during the writing of
  76 // an archive file should stop the process.  Unrecoverable errors during
  77 // the reading of the archive file should stop the process.
  78 
  79 static void fail_exit(const char *msg, va_list ap) {
  80   // This occurs very early during initialization: tty is not initialized.
  81   jio_fprintf(defaultStream::error_stream(),
  82               "An error has occurred while processing the"
  83               " shared archive file.\n");
  84   jio_vfprintf(defaultStream::error_stream(), msg, ap);
  85   jio_fprintf(defaultStream::error_stream(), "\n");
  86   // Do not change the text of the below message because some tests check for it.
  87   vm_exit_during_initialization("Unable to use shared archive.", NULL);
  88 }
  89 
  90 
  91 void FileMapInfo::fail_stop(const char *msg, ...) {
  92         va_list ap;
  93   va_start(ap, msg);
  94   fail_exit(msg, ap);   // Never returns.
  95   va_end(ap);           // for completeness.
  96 }
  97 
  98 
  99 // Complain and continue.  Recoverable errors during the reading of the
 100 // archive file may continue (with sharing disabled).
 101 //
 102 // If we continue, then disable shared spaces and close the file.
 103 
 104 void FileMapInfo::fail_continue(const char *msg, ...) {
 105   va_list ap;
 106   va_start(ap, msg);
 107   if (_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) {
 114     // If we are doing PrintSharedArchiveAndExit and some of the classpath entries
 115     // do not validate, we can still continue "limping" to validate the remaining
 116     // entries. No need to quit.
 117     tty->print("[");
 118     tty->vprint(msg, ap);
 119     tty->print_cr("]");
 120   } else {
 121     if (RequireSharedSpaces) {
 122       fail_exit(msg, ap);
 123     } else {
 124       if (log_is_enabled(Info, cds)) {
 125         ResourceMark rm;
 126         LogStream ls(Log(cds)::info());
 127         ls.print("UseSharedSpaces: ");
 128         ls.vprint_cr(msg, ap);
 129       }
 130     }
 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   }
 141   va_end(ap);
 142 }
 143 
 144 // Fill in the fileMapInfo structure with data about this VM instance.
 145 
 146 // This method copies the vm version info into header_version.  If the version is too
 147 // long then a truncated version, which has a hash code appended to it, is copied.
 148 //
 149 // Using a template enables this method to verify that header_version is an array of
 150 // length JVM_IDENT_MAX.  This ensures that the code that writes to the CDS file and
 151 // the code that reads the CDS file will both use the same size buffer.  Hence, will
 152 // use identical truncation.  This is necessary for matching of truncated versions.
 153 template <int N> static void get_header_version(char (&header_version) [N]) {
 154   assert(N == JVM_IDENT_MAX, "Bad header_version size");
 155 
 156   const char *vm_version = VM_Version::internal_vm_info_string();
 157   const int version_len = (int)strlen(vm_version);
 158 
 159   memset(header_version, 0, JVM_IDENT_MAX);


 210 }
 211 
 212 void FileMapInfo::populate_header(size_t alignment) {
 213   header()->populate(this, alignment);
 214 }
 215 
 216 void FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
 217   if (DynamicDumpSharedSpaces) {
 218     _magic = CDS_DYNAMIC_ARCHIVE_MAGIC;
 219   } else {
 220     _magic = CDS_ARCHIVE_MAGIC;
 221   }
 222   _version = CURRENT_CDS_ARCHIVE_VERSION;
 223   _alignment = alignment;
 224   _obj_alignment = ObjectAlignmentInBytes;
 225   _compact_strings = CompactStrings;
 226   _narrow_oop_mode = CompressedOops::mode();
 227   _narrow_oop_base = CompressedOops::base();
 228   _narrow_oop_shift = CompressedOops::shift();
 229   _max_heap_size = MaxHeapSize;
 230   _narrow_klass_base = CompressedKlassPointers::base();
 231   _narrow_klass_shift = CompressedKlassPointers::shift();
 232   _shared_path_table = mapinfo->_shared_path_table;
 233   if (HeapShared::is_heap_object_archiving_allowed()) {
 234     _heap_end = CompressedOops::end();
 235   }
 236 
 237   // The following fields are for sanity checks for whether this archive
 238   // will function correctly with this JVM and the bootclasspath it's
 239   // invoked with.
 240 
 241   // JVM version string ... changes on each build.
 242   get_header_version(_jvm_ident);
 243 
 244   _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
 245   _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
 246   _num_module_paths = ClassLoader::num_module_path_entries();
 247   _max_used_path_index = ClassLoaderExt::max_used_path_index();
 248 
 249   _verify_local = BytecodeVerificationLocal;
 250   _verify_remote = BytecodeVerificationRemote;
 251   _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
 252   _shared_base_address = SharedBaseAddress;

 253   _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;
 254   // the following 2 fields will be set in write_header for dynamic archive header
 255   _base_archive_name_size = 0;
 256   _base_archive_is_default = false;




 257 }
 258 
 259 void SharedClassPathEntry::init_as_non_existent(const char* path, TRAPS) {
 260   _type = non_existent_entry;
 261   set_name(path, THREAD);
 262 }
 263 
 264 void SharedClassPathEntry::init(bool is_modules_image,
 265                                 ClassPathEntry* cpe, TRAPS) {
 266   Arguments::assert_is_dumping_archive();
 267   _timestamp = 0;
 268   _filesize  = 0;
 269   _from_class_path_attr = false;
 270 
 271   struct stat st;
 272   if (os::stat(cpe->name(), &st) == 0) {
 273     if ((st.st_mode & S_IFMT) == S_IFDIR) {
 274       _type = dir_entry;
 275     } else {
 276       // The timestamp of the modules_image is not checked at runtime.


 600   return npaths;
 601 }
 602 
 603 GrowableArray<const char*>* FileMapInfo::create_path_array(const char* paths) {
 604   GrowableArray<const char*>* path_array =  new(ResourceObj::RESOURCE_AREA, mtInternal)
 605       GrowableArray<const char*>(10);
 606 
 607   ClasspathStream cp_stream(paths);
 608   while (cp_stream.has_next()) {
 609     const char* path = cp_stream.get_next();
 610     struct stat st;
 611     if (os::stat(path, &st) == 0) {
 612       path_array->append(path);
 613     }
 614   }
 615   return path_array;
 616 }
 617 
 618 bool FileMapInfo::fail(const char* msg, const char* name) {
 619   ClassLoader::trace_class_path(msg, name);
 620   MetaspaceShared::set_archive_loading_failed();
 621   return false;
 622 }
 623 
 624 bool FileMapInfo::check_paths(int shared_path_start_idx, int num_paths, GrowableArray<const char*>* rp_array) {
 625   int i = 0;
 626   int j = shared_path_start_idx;
 627   bool mismatch = false;
 628   while (i < num_paths && !mismatch) {
 629     while (shared_path(j)->from_class_path_attr()) {
 630       // shared_path(j) was expanded from the JAR file attribute "Class-Path:"
 631       // during dump time. It's not included in the -classpath VM argument.
 632       j++;
 633     }
 634     if (!os::same_files(shared_path(j)->name(), rp_array->at(i))) {
 635       mismatch = true;
 636     }
 637     i++;
 638     j++;
 639   }
 640   return mismatch;


 935       fail_continue("Unable to read the base archive name from the header.");
 936       FREE_C_HEAP_ARRAY(char, *base_archive_name);
 937       *base_archive_name = NULL;
 938       os::free(dynamic_header);
 939       os::close(fd);
 940       return false;
 941     }
 942   }
 943 
 944   os::free(dynamic_header);
 945   os::close(fd);
 946   return true;
 947 }
 948 
 949 void FileMapInfo::restore_shared_path_table() {
 950   _shared_path_table = _current_info->header()->shared_path_table();
 951 }
 952 
 953 // Read the FileMapInfo information from the file.
 954 
 955 bool FileMapInfo::init_from_file(int fd, bool is_static) {
 956   size_t sz = is_static ? sizeof(FileMapHeader) : sizeof(DynamicArchiveHeader);
 957   size_t n = os::read(fd, header(), (unsigned int)sz);
 958   if (n != sz) {
 959     fail_continue("Unable to read the file header.");
 960     return false;
 961   }
 962 
 963   if (!Arguments::has_jimage()) {
 964     FileMapInfo::fail_continue("The shared archive file cannot be used with an exploded module build.");
 965     return false;
 966   }
 967 
 968   unsigned int expected_magic = is_static ? CDS_ARCHIVE_MAGIC : CDS_DYNAMIC_ARCHIVE_MAGIC;
 969   if (header()->magic() != expected_magic) {
 970     log_info(cds)("_magic expected: 0x%08x", expected_magic);
 971     log_info(cds)("         actual: 0x%08x", header()->magic());
 972     FileMapInfo::fail_continue("The shared archive file has a bad magic number.");
 973     return false;
 974   }
 975 
 976   if (header()->version() != CURRENT_CDS_ARCHIVE_VERSION) {
 977     log_info(cds)("_version expected: %d", CURRENT_CDS_ARCHIVE_VERSION);
 978     log_info(cds)("           actual: %d", header()->version());
 979     fail_continue("The shared archive file has the wrong version.");
 980     return false;
 981   }
 982 
 983   if (header()->header_size() != sz) {
 984     log_info(cds)("_header_size expected: " SIZE_FORMAT, sz);
 985     log_info(cds)("               actual: " SIZE_FORMAT, header()->header_size());
 986     FileMapInfo::fail_continue("The shared archive file has an incorrect header size.");
 987     return false;
 988   }


 999   if (strncmp(actual_ident, expected_ident, JVM_IDENT_MAX-1) != 0) {
1000     log_info(cds)("_jvm_ident expected: %s", expected_ident);
1001     log_info(cds)("             actual: %s", actual_ident);
1002     FileMapInfo::fail_continue("The shared archive file was created by a different"
1003                   " version or build of HotSpot");
1004     return false;
1005   }
1006 
1007   if (VerifySharedSpaces) {
1008     int expected_crc = header()->compute_crc();
1009     if (expected_crc != header()->crc()) {
1010       log_info(cds)("_crc expected: %d", expected_crc);
1011       log_info(cds)("       actual: %d", header()->crc());
1012       FileMapInfo::fail_continue("Header checksum verification failed.");
1013       return false;
1014     }
1015   }
1016 
1017   _file_offset = n + header()->base_archive_name_size(); // accounts for the size of _base_archive_name
1018 
1019   if (is_static) {
1020     // just checking the last region is sufficient since the archive is written
1021     // in sequential order
1022     size_t len = lseek(fd, 0, SEEK_END);
1023     FileMapRegion* si = space_at(MetaspaceShared::last_valid_region);
1024     // The last space might be empty
1025     if (si->file_offset() > len || len - si->file_offset() < si->used()) {
1026       fail_continue("The shared archive file has been truncated.");
1027       return false;
1028     }
1029 
1030     SharedBaseAddress = header()->shared_base_address();
1031   }
1032 
1033   return true;
1034 }
1035 
1036 void FileMapInfo::seek_to_position(size_t pos) {
1037   if (lseek(_fd, (long)pos, SEEK_SET) < 0) {
1038     fail_stop("Unable to seek to position " SIZE_FORMAT, pos);
1039   }
1040 }
1041 
1042 // Read the FileMapInfo information from the file.
1043 bool FileMapInfo::open_for_read(const char* path) {
1044   if (_file_open) {
1045     return true;
1046   }
1047   if (path == NULL) {
1048     _full_path = Arguments::GetSharedArchivePath();
1049   } else {
1050     _full_path = path;
1051   }
1052   int fd = os::open(_full_path, O_RDONLY | O_BINARY, 0);
1053   if (fd < 0) {

1054     if (errno == ENOENT) {
1055       // Not locating the shared archive is ok.
1056       fail_continue("Specified shared archive not found (%s).", _full_path);
1057     } else {
1058       fail_continue("Failed to open shared archive file (%s).",
1059                     os::strerror(errno));
1060     }



1061     return false;
1062   }
1063 
1064   _fd = fd;
1065   _file_open = true;
1066   return true;
1067 }
1068 
1069 // Write the FileMapInfo information to the file.
1070 
1071 void FileMapInfo::open_for_write(const char* path) {
1072   if (path == NULL) {
1073     _full_path = Arguments::GetSharedArchivePath();
1074   } else {
1075     _full_path = path;
1076   }
1077   LogMessage(cds) msg;
1078   if (msg.is_info()) {
1079     msg.info("Dumping shared data to file: ");
1080     msg.info("   %s", _full_path);


1110 
1111 // Write the header to the file, seek to the next allocation boundary.
1112 
1113 void FileMapInfo::write_header() {
1114   _file_offset = 0;
1115   seek_to_position(_file_offset);
1116   char* base_archive_name = NULL;
1117   if (header()->magic() == CDS_DYNAMIC_ARCHIVE_MAGIC) {
1118     base_archive_name = (char*)Arguments::GetSharedArchivePath();
1119     header()->set_base_archive_name_size(strlen(base_archive_name) + 1);
1120     header()->set_base_archive_is_default(FLAG_IS_DEFAULT(SharedArchiveFile));
1121   }
1122 
1123   assert(is_file_position_aligned(), "must be");
1124   write_bytes(header(), header()->header_size());
1125   if (base_archive_name != NULL) {
1126     write_bytes(base_archive_name, header()->base_archive_name_size());
1127   }
1128 }
1129 




1130 void FileMapRegion::init(bool is_heap_region, char* base, size_t size, bool read_only,
1131                          bool allow_exec, int crc) {
1132   _is_heap_region = is_heap_region;

1133 
1134   if (is_heap_region) {
1135     assert(!DynamicDumpSharedSpaces, "must be");
1136     assert((base - (char*)CompressedKlassPointers::base()) % HeapWordSize == 0, "Sanity");
1137     if (base != NULL) {
1138       _addr._offset = (intx)CompressedOops::encode_not_null((oop)base);
1139     } else {
1140       _addr._offset = 0;
1141     }
1142   } else {
1143     _addr._base = base;



1144   }
1145   _used = size;
1146   _read_only = read_only;
1147   _allow_exec = allow_exec;
1148   _crc = crc;


1149 }
1150 
1151 void FileMapInfo::write_region(int region, char* base, size_t size,
1152                                bool read_only, bool allow_exec) {
1153   Arguments::assert_is_dumping_archive();
1154 
1155   FileMapRegion* si = space_at(region);
1156   char* target_base = base;
1157   if (DynamicDumpSharedSpaces) {



1158     assert(!HeapShared::is_heap_region(region), "dynamic archive doesn't support heap regions");
1159     target_base = DynamicArchive::buffer_to_target(base);
1160   }
1161 
1162   si->set_file_offset(_file_offset);

1163   log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08)
1164                 " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),
1165                 region, size, p2i(target_base), _file_offset);
1166 
1167   int crc = ClassLoader::crc32(0, base, (jint)size);
1168   si->init(HeapShared::is_heap_region(region), target_base, size, read_only, allow_exec, crc);
1169 
1170   if (base != NULL) {
1171     write_bytes_aligned(base, size);
1172   }
1173 }
1174 














1175 // Write out the given archive heap memory regions.  GC code combines multiple
1176 // consecutive archive GC regions into one MemRegion whenever possible and
1177 // produces the 'heap_mem' array.
1178 //
1179 // If the archive heap memory size is smaller than a single dump time GC region
1180 // size, there is only one MemRegion in the array.
1181 //
1182 // If the archive heap memory size is bigger than one dump time GC region size,
1183 // the 'heap_mem' array may contain more than one consolidated MemRegions. When
1184 // the first/bottom archive GC region is a partial GC region (with the empty
1185 // portion at the higher address within the region), one MemRegion is used for
1186 // the bottom partial archive GC region. The rest of the consecutive archive
1187 // GC regions are combined into another MemRegion.
1188 //
1189 // Here's the mapping from (archive heap GC regions) -> (GrowableArray<MemRegion> *regions).
1190 //   + We have 1 or more archive heap regions: ah0, ah1, ah2 ..... ahn
1191 //   + We have 1 or 2 consolidated heap memory regions: r0 and r1
1192 //
1193 // If there's a single archive GC region (ah0), then r0 == ah0, and r1 is empty.
1194 // Otherwise:


1212   if(arr_len > max_num_regions) {
1213     fail_stop("Unable to write archive heap memory regions: "
1214               "number of memory regions exceeds maximum due to fragmentation. "
1215               "Please increase java heap size "
1216               "(current MaxHeapSize is " SIZE_FORMAT ", InitialHeapSize is " SIZE_FORMAT ").",
1217               MaxHeapSize, InitialHeapSize);
1218   }
1219 
1220   size_t total_size = 0;
1221   for (int i = first_region_id, arr_idx = 0;
1222            i < first_region_id + max_num_regions;
1223            i++, arr_idx++) {
1224     char* start = NULL;
1225     size_t size = 0;
1226     if (arr_idx < arr_len) {
1227       start = (char*)heap_mem->at(arr_idx).start();
1228       size = heap_mem->at(arr_idx).byte_size();
1229       total_size += size;
1230     }
1231 
1232     log_info(cds)("Archive heap region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes",
1233                   i, p2i(start), p2i(start + size), size);
1234     write_region(i, start, size, false, false);
1235     if (size > 0) {
1236       space_at(i)->init_oopmap(oopmaps->at(arr_idx)._oopmap,


1237                                oopmaps->at(arr_idx)._oopmap_size_in_bits);
1238     }
1239   }
1240   return total_size;
1241 }
1242 
1243 // Dump bytes to file -- at the current file position.
1244 
1245 void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) {
1246   assert(_file_open, "must be");
1247   size_t n = os::write(_fd, buffer, (unsigned int)nbytes);
1248   if (n != nbytes) {
1249     // If the shared archive is corrupted, close it and remove it.
1250     close();
1251     remove(_full_path);
1252     fail_stop("Unable to write to shared archive file.");
1253   }
1254   _file_offset += nbytes;
1255 }
1256 


1268   if (new_file_offset != _file_offset) {
1269     _file_offset = new_file_offset;
1270     // Seek one byte back from the target and write a byte to insure
1271     // that the written file is the correct length.
1272     _file_offset -= 1;
1273     seek_to_position(_file_offset);
1274     char zero = 0;
1275     write_bytes(&zero, 1);
1276   }
1277 }
1278 
1279 
1280 // Dump bytes to file -- at the current file position.
1281 
1282 void FileMapInfo::write_bytes_aligned(const void* buffer, size_t nbytes) {
1283   align_file_position();
1284   write_bytes(buffer, nbytes);
1285   align_file_position();
1286 }
1287 



1288 
1289 // Close the shared archive file.  This does NOT unmap mapped regions.
1290 
1291 void FileMapInfo::close() {
1292   if (_file_open) {
1293     if (::close(_fd) < 0) {
1294       fail_stop("Unable to close the shared archive file.");
1295     }
1296     _file_open = false;
1297     _fd = -1;
1298   }
1299 }
1300 
1301 
1302 // JVM/TI RedefineClasses() support:
1303 // Remap the shared readonly space to shared readwrite, private.
1304 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
1305   int idx = MetaspaceShared::ro;
1306   FileMapRegion* si = space_at(idx);
1307   if (!si->read_only()) {


1314     return false;
1315   }
1316   char *addr = region_addr(idx);
1317   char *base = os::remap_memory(_fd, _full_path, si->file_offset(),
1318                                 addr, size, false /* !read_only */,
1319                                 si->allow_exec());
1320   close();
1321   // These have to be errors because the shared region is now unmapped.
1322   if (base == NULL) {
1323     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1324     vm_exit(1);
1325   }
1326   if (base != addr) {
1327     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1328     vm_exit(1);
1329   }
1330   si->set_read_only(false);
1331   return true;
1332 }
1333 
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.
1354 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode",
1355                                             "String1", "String2", "OpenArchive1", "OpenArchive2" };
1356 
1357 char* FileMapInfo::map_regions(int regions[], char* saved_base[], size_t len) {
1358   char* prev_top = NULL;
1359   char* curr_base;
1360   char* curr_top;
1361   int i = 0;
1362   for (i = 0; i < (int)len; i++) {
1363     curr_base = map_region(regions[i], &curr_top);
1364     if (curr_base == NULL) {
1365       return NULL;


1366     }
1367     if (i > 0) {
1368       // We require that mc->rw->ro->md to be laid out consecutively, with no
1369       // gaps between them. That way, we can ensure that the OS won't be able to
1370       // allocate any new memory spaces inside _shared_metaspace_{base,top}, which
1371       // would mess up the simple comparision in MetaspaceShared::is_in_shared_metaspace().
1372       assert(curr_base == prev_top, "must be");
1373     }
1374     log_info(cds)("Mapped region #%d at base %p top %p", regions[i], curr_base, curr_top);
1375     saved_base[i] = curr_base;
1376     prev_top = curr_top;

1377   }
1378   return curr_top;














1379 }
1380 
1381 char* FileMapInfo::map_region(int i, char** top_ret) {


















1382   assert(!HeapShared::is_heap_region(i), "sanity");
1383   FileMapRegion* si = space_at(i);
1384   size_t used = si->used();
1385   size_t alignment = os::vm_allocation_granularity();
1386   size_t size = align_up(used, alignment);
1387   char *requested_addr = region_addr(i);


1388 
1389 #ifdef _WINDOWS
1390   // Windows cannot remap read-only shared memory to read-write when required for
1391   // RedefineClasses, which is also used by JFR.  Always map windows regions as RW.
1392   si->set_read_only(false);
1393 #else
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);


1398   }
1399 #endif // _WINDOWS
1400 
1401   // map the contents of the CDS archive in this memory
1402   char *base = os::map_memory(_fd, _full_path, si->file_offset(),













1403                               requested_addr, size, si->read_only(),
1404                               si->allow_exec());
1405   if (base == NULL || base != requested_addr) {
1406     fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
1407     _memory_mapping_failed = true;
1408     return NULL;










1409   }
1410 #ifdef _WINDOWS
1411   // This call is Windows-only because the memory_type gets recorded for the other platforms
1412   // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
1413   MemTracker::record_virtual_memory_type((address)base, mtClassShared);
1414 #endif
1415 
1416   if (VerifySharedSpaces && !verify_region_checksum(i)) {



















1417     return NULL;
1418   }













1419 
1420   *top_ret = base + size;
1421   return base;




























1422 }
1423 
1424 size_t FileMapInfo::read_bytes(void* buffer, size_t count) {
1425   assert(_file_open, "Archive file is not open");
1426   size_t n = os::read(_fd, buffer, (unsigned int)count);
1427   if (n != count) {
1428     // Close the file if there's a problem reading it.
1429     close();
1430     return 0;
1431   }
1432   _file_offset += count;
1433   return count;
1434 }
1435 
1436 address FileMapInfo::decode_start_address(FileMapRegion* spc, bool with_current_oop_encoding_mode) {



1437   if (with_current_oop_encoding_mode) {
1438     return (address)CompressedOops::decode_not_null(spc->offset());
1439   } else {
1440     return (address)HeapShared::decode_from_archive(spc->offset());
1441   }
1442 }
1443 
1444 static MemRegion *closed_archive_heap_ranges = NULL;
1445 static MemRegion *open_archive_heap_ranges = NULL;
1446 static int num_closed_archive_heap_ranges = 0;
1447 static int num_open_archive_heap_ranges = 0;
1448 
1449 #if INCLUDE_CDS_JAVA_HEAP
1450 bool FileMapInfo::has_heap_regions() {
1451   return (space_at(MetaspaceShared::first_closed_archive_heap_region)->used() > 0);
1452 }
1453 
1454 // Returns the address range of the archived heap regions computed using the
1455 // current oop encoding mode. This range may be different than the one seen at
1456 // dump time due to encoding mode differences. The result is used in determining
1457 // if/how these regions should be relocated at run time.
1458 MemRegion FileMapInfo::get_heap_regions_range_with_current_oop_encoding_mode() {
1459   address start = (address) max_uintx;
1460   address end   = NULL;


1688 }
1689 
1690 void FileMapInfo::patch_archived_heap_embedded_pointers() {
1691   if (!_heap_pointers_need_patching) {
1692     return;
1693   }
1694 
1695   patch_archived_heap_embedded_pointers(closed_archive_heap_ranges,
1696                                         num_closed_archive_heap_ranges,
1697                                         MetaspaceShared::first_closed_archive_heap_region);
1698 
1699   patch_archived_heap_embedded_pointers(open_archive_heap_ranges,
1700                                         num_open_archive_heap_ranges,
1701                                         MetaspaceShared::first_open_archive_heap_region);
1702 }
1703 
1704 void FileMapInfo::patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges,
1705                                                         int first_region_idx) {
1706   for (int i=0; i<num_ranges; i++) {
1707     FileMapRegion* si = space_at(i + first_region_idx);
1708     HeapShared::patch_archived_heap_embedded_pointers(ranges[i], (address)si->oopmap(),
1709                                                       si->oopmap_size_in_bits());
1710   }
1711 }
1712 
1713 // This internally allocates objects using SystemDictionary::Object_klass(), so it
1714 // must be called after the well-known classes are resolved.
1715 void FileMapInfo::fixup_mapped_heap_regions() {
1716   // If any closed regions were found, call the fill routine to make them parseable.
1717   // Note that closed_archive_heap_ranges may be non-NULL even if no ranges were found.
1718   if (num_closed_archive_heap_ranges != 0) {
1719     assert(closed_archive_heap_ranges != NULL,
1720            "Null closed_archive_heap_ranges array with non-zero count");
1721     G1CollectedHeap::heap()->fill_archive_regions(closed_archive_heap_ranges,
1722                                                   num_closed_archive_heap_ranges);
1723   }
1724 
1725   // do the same for mapped open archive heap regions
1726   if (num_open_archive_heap_ranges != 0) {
1727     assert(open_archive_heap_ranges != NULL, "NULL open_archive_heap_ranges array with non-zero count");
1728     G1CollectedHeap::heap()->fill_archive_regions(open_archive_heap_ranges,


1742 bool FileMapInfo::region_crc_check(char* buf, size_t size, int expected_crc) {
1743   int crc = ClassLoader::crc32(0, buf, (jint)size);
1744   if (crc != expected_crc) {
1745     fail_continue("Checksum verification failed.");
1746     return false;
1747   }
1748   return true;
1749 }
1750 
1751 bool FileMapInfo::verify_region_checksum(int i) {
1752   assert(VerifySharedSpaces, "sanity");
1753   size_t sz = space_at(i)->used();
1754 
1755   if (sz == 0) {
1756     return true; // no data
1757   } else {
1758     return region_crc_check(region_addr(i), sz, space_at(i)->crc());
1759   }
1760 }
1761 
1762 void FileMapInfo::unmap_regions(int regions[], char* saved_base[], size_t len) {
1763   for (int i = 0; i < (int)len; i++) {
1764     if (saved_base[i] != NULL) {
1765       unmap_region(regions[i]);
1766     }
1767   }
1768 }
1769 
1770 // Unmap a memory region in the address space.
1771 
1772 void FileMapInfo::unmap_region(int i) {
1773   assert(!HeapShared::is_heap_region(i), "sanity");
1774   FileMapRegion* si = space_at(i);

1775   size_t used = si->used();
1776   size_t size = align_up(used, os::vm_allocation_granularity());
1777 
1778   if (used == 0) {
1779     return;



1780   }
1781 
1782   char* addr = region_addr(i);
1783   if (!os::unmap_memory(addr, size)) {
1784     fail_stop("Unable to unmap shared space.");
1785   }
1786 }
1787 
1788 void FileMapInfo::assert_mark(bool check) {
1789   if (!check) {
1790     fail_stop("Mark mismatch while restoring from shared file.");
1791   }
1792 }
1793 
1794 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
1795   _shared_path_table.metaspace_pointers_do(it);
1796 }
1797 
1798 FileMapInfo* FileMapInfo::_current_info = NULL;
1799 FileMapInfo* FileMapInfo::_dynamic_archive_info = NULL;
1800 bool FileMapInfo::_heap_pointers_need_patching = false;
1801 SharedPathTable FileMapInfo::_shared_path_table;
1802 bool FileMapInfo::_validating_shared_path_table = false;
1803 bool FileMapInfo::_memory_mapping_failed = false;
1804 GrowableArray<const char*>* FileMapInfo::_non_existent_class_paths = NULL;
1805 
1806 // Open the shared archive file, read and validate the header
1807 // information (version, boot classpath, etc.).  If initialization
1808 // fails, shared spaces are disabled and the file is closed. [See
1809 // fail_continue.]
1810 //
1811 // Validation of the archive is done in two steps:
1812 //
1813 // [1] validate_header() - done here.
1814 // [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.
1816 bool FileMapInfo::initialize(bool is_static) {
1817   assert(UseSharedSpaces, "UseSharedSpaces expected.");
1818 
1819   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
1821     // are replaced at runtime by JVMTI ClassFileLoadHook. All of those classes are resolved
1822     // during the JVMTI "early" stage, so we can still use CDS if
1823     // JvmtiExport::has_early_class_hook_env() is false.
1824     FileMapInfo::fail_continue("CDS is disabled because early JVMTI ClassFileLoadHook is in use.");
1825     return false;
1826   }
1827 
1828   if (!open_for_read()) {
1829     return false;
1830   }
1831 
1832   init_from_file(_fd, is_static);
1833   // UseSharedSpaces could be disabled if the checking of some of the header fields in
1834   // init_from_file has failed.
1835   if (!UseSharedSpaces || !validate_header(is_static)) {
1836     return false;
1837   }
1838   return true;
1839 }
1840 
1841 char* FileMapInfo::region_addr(int idx) {
1842   FileMapRegion* si = space_at(idx);
1843   if (HeapShared::is_heap_region(idx)) {
1844     assert(DumpSharedSpaces, "The following doesn't work at runtime");
1845     return si->used() > 0 ?
1846           (char*)start_address_as_decoded_with_current_oop_encoding_mode(si) : NULL;
1847   } else {
1848     return si->base();
1849   }
1850 }
1851 








1852 int FileMapHeader::compute_crc() {
1853   char* start = (char*)this;
1854   // start computing from the field after _crc
1855   char* buf = (char*)&_crc + sizeof(_crc);
1856   size_t sz = _header_size - (buf - start);
1857   int crc = ClassLoader::crc32(0, buf, (jint)sz);
1858   return crc;
1859 }
1860 
1861 // This function should only be called during run time with UseSharedSpaces enabled.
1862 bool FileMapHeader::validate() {
1863 
1864   if (_obj_alignment != ObjectAlignmentInBytes) {
1865     FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d"
1866                   " does not equal the current ObjectAlignmentInBytes of " INTX_FORMAT ".",
1867                   _obj_alignment, ObjectAlignmentInBytes);
1868     return false;
1869   }
1870   if (_compact_strings != CompactStrings) {
1871     FileMapInfo::fail_continue("The shared archive file's CompactStrings setting (%s)"
1872                   " does not equal the current CompactStrings setting (%s).",
1873                   _compact_strings ? "enabled" : "disabled",
1874                   CompactStrings   ? "enabled" : "disabled");
1875     return false;
1876   }
1877 
1878   // This must be done after header validation because it might change the
1879   // header data
1880   const char* prop = Arguments::get_property("java.system.class.loader");
1881   if (prop != NULL) {
1882     warning("Archived non-system classes are disabled because the "
1883             "java.system.class.loader property is specified (value = \"%s\"). "


1896   }
1897 
1898   // Java agents are allowed during run time. Therefore, the following condition is not
1899   // checked: (!_allow_archiving_with_java_agent && AllowArchivingWithJavaAgent)
1900   // Note: _allow_archiving_with_java_agent is set in the shared archive during dump time
1901   // while AllowArchivingWithJavaAgent is set during the current run.
1902   if (_allow_archiving_with_java_agent && !AllowArchivingWithJavaAgent) {
1903     FileMapInfo::fail_continue("The setting of the AllowArchivingWithJavaAgent is different "
1904                                "from the setting in the shared archive.");
1905     return false;
1906   }
1907 
1908   if (_allow_archiving_with_java_agent) {
1909     warning("This archive was created with AllowArchivingWithJavaAgent. It should be used "
1910             "for testing purposes only and should not be used in a production environment");
1911   }
1912 
1913   return true;
1914 }
1915 
1916 bool FileMapInfo::validate_header(bool is_static) {
1917   return header()->validate();
1918 }
1919 
1920 // Check if a given address is within one of the shared regions
1921 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
1922   assert(idx == MetaspaceShared::ro ||
1923          idx == MetaspaceShared::rw ||
1924          idx == MetaspaceShared::mc ||
1925          idx == MetaspaceShared::md, "invalid region index");
1926   char* base = region_addr(idx);
1927   if (p >= base && p < base + space_at(idx)->used()) {
1928     return true;
1929   }
1930   return false;
1931 }
1932 
1933 // Unmap mapped regions of shared space.
1934 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
1935   MetaspaceShared::set_shared_metaspace_range(NULL, NULL);
1936 
1937   FileMapInfo *map_info = FileMapInfo::current_info();
1938   if (map_info) {
1939     map_info->fail_continue("%s", msg);
1940     for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) {
1941       if (!HeapShared::is_heap_region(i)) {
1942         char *addr = map_info->region_addr(i);
1943         if (addr != NULL) {
1944           map_info->unmap_region(i);
1945           map_info->space_at(i)->mark_invalid();
1946         }
1947       }
1948     }
1949     // 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.
1951     map_info->dealloc_archive_heap_regions(open_archive_heap_ranges,
1952                                            num_open_archive_heap_ranges,
1953                                            true);
1954     map_info->dealloc_archive_heap_regions(closed_archive_heap_ranges,
1955                                            num_closed_archive_heap_ranges,
1956                                            false);
1957   } else if (DumpSharedSpaces) {
1958     fail_stop("%s", msg);
1959   }
1960 }
1961 
1962 #if INCLUDE_JVMTI
1963 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = NULL;
1964 
1965 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
1966   ClassPathEntry* ent = _classpath_entries_for_jvmti[i];




  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "classfile/classFileStream.hpp"
  28 #include "classfile/classLoader.inline.hpp"
  29 #include "classfile/classLoaderData.inline.hpp"
  30 #include "classfile/classLoaderExt.hpp"
  31 #include "classfile/symbolTable.hpp"
  32 #include "classfile/systemDictionaryShared.hpp"
  33 #include "classfile/altHashing.hpp"
  34 #include "logging/log.hpp"
  35 #include "logging/logStream.hpp"
  36 #include "logging/logMessage.hpp"
  37 #include "memory/archiveUtils.hpp"
  38 #include "memory/dynamicArchive.hpp"
  39 #include "memory/filemap.hpp"
  40 #include "memory/heapShared.inline.hpp"
  41 #include "memory/iterator.inline.hpp"
  42 #include "memory/metadataFactory.hpp"
  43 #include "memory/metaspaceClosure.hpp"
  44 #include "memory/metaspaceShared.hpp"
  45 #include "memory/oopFactory.hpp"
  46 #include "memory/universe.hpp"
  47 #include "oops/compressedOops.hpp"
  48 #include "oops/compressedOops.inline.hpp"
  49 #include "oops/objArrayOop.hpp"
  50 #include "oops/oop.inline.hpp"
  51 #include "prims/jvmtiExport.hpp"
  52 #include "runtime/arguments.hpp"
  53 #include "runtime/java.hpp"
  54 #include "runtime/mutexLocker.hpp"
  55 #include "runtime/os.inline.hpp"
  56 #include "runtime/vm_version.hpp"
  57 #include "services/memTracker.hpp"
  58 #include "utilities/align.hpp"
  59 #include "utilities/bitMap.inline.hpp"
  60 #include "utilities/classpathStream.hpp"
  61 #include "utilities/defaultStream.hpp"
  62 #if INCLUDE_G1GC
  63 #include "gc/g1/g1CollectedHeap.hpp"
  64 #include "gc/g1/heapRegion.hpp"
  65 #endif
  66 
  67 # include <sys/stat.h>
  68 # include <errno.h>
  69 
  70 #ifndef O_BINARY       // if defined (Win32) use binary files.
  71 #define O_BINARY 0     // otherwise do nothing.
  72 #endif
  73 



  74 // Complain and stop. All error conditions occurring during the writing of
  75 // an archive file should stop the process.  Unrecoverable errors during
  76 // the reading of the archive file should stop the process.
  77 
  78 static void fail_exit(const char *msg, va_list ap) {
  79   // This occurs very early during initialization: tty is not initialized.
  80   jio_fprintf(defaultStream::error_stream(),
  81               "An error has occurred while processing the"
  82               " shared archive file.\n");
  83   jio_vfprintf(defaultStream::error_stream(), msg, ap);
  84   jio_fprintf(defaultStream::error_stream(), "\n");
  85   // Do not change the text of the below message because some tests check for it.
  86   vm_exit_during_initialization("Unable to use shared archive.", NULL);
  87 }
  88 
  89 
  90 void FileMapInfo::fail_stop(const char *msg, ...) {
  91         va_list ap;
  92   va_start(ap, msg);
  93   fail_exit(msg, ap);   // Never returns.
  94   va_end(ap);           // for completeness.
  95 }
  96 
  97 
  98 // Complain and continue.  Recoverable errors during the reading of the
  99 // archive file may continue (with sharing disabled).
 100 //
 101 // If we continue, then disable shared spaces and close the file.
 102 
 103 void FileMapInfo::fail_continue(const char *msg, ...) {
 104   va_list ap;
 105   va_start(ap, msg);






 106   if (PrintSharedArchiveAndExit && _validating_shared_path_table) {
 107     // If we are doing PrintSharedArchiveAndExit and some of the classpath entries
 108     // do not validate, we can still continue "limping" to validate the remaining
 109     // entries. No need to quit.
 110     tty->print("[");
 111     tty->vprint(msg, ap);
 112     tty->print_cr("]");
 113   } else {
 114     if (RequireSharedSpaces) {
 115       fail_exit(msg, ap);
 116     } else {
 117       if (log_is_enabled(Info, cds)) {
 118         ResourceMark rm;
 119         LogStream ls(Log(cds)::info());
 120         ls.print("UseSharedSpaces: ");
 121         ls.vprint_cr(msg, ap);
 122       }
 123     }









 124   }
 125   va_end(ap);
 126 }
 127 
 128 // Fill in the fileMapInfo structure with data about this VM instance.
 129 
 130 // This method copies the vm version info into header_version.  If the version is too
 131 // long then a truncated version, which has a hash code appended to it, is copied.
 132 //
 133 // Using a template enables this method to verify that header_version is an array of
 134 // length JVM_IDENT_MAX.  This ensures that the code that writes to the CDS file and
 135 // the code that reads the CDS file will both use the same size buffer.  Hence, will
 136 // use identical truncation.  This is necessary for matching of truncated versions.
 137 template <int N> static void get_header_version(char (&header_version) [N]) {
 138   assert(N == JVM_IDENT_MAX, "Bad header_version size");
 139 
 140   const char *vm_version = VM_Version::internal_vm_info_string();
 141   const int version_len = (int)strlen(vm_version);
 142 
 143   memset(header_version, 0, JVM_IDENT_MAX);


 194 }
 195 
 196 void FileMapInfo::populate_header(size_t alignment) {
 197   header()->populate(this, alignment);
 198 }
 199 
 200 void FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
 201   if (DynamicDumpSharedSpaces) {
 202     _magic = CDS_DYNAMIC_ARCHIVE_MAGIC;
 203   } else {
 204     _magic = CDS_ARCHIVE_MAGIC;
 205   }
 206   _version = CURRENT_CDS_ARCHIVE_VERSION;
 207   _alignment = alignment;
 208   _obj_alignment = ObjectAlignmentInBytes;
 209   _compact_strings = CompactStrings;
 210   _narrow_oop_mode = CompressedOops::mode();
 211   _narrow_oop_base = CompressedOops::base();
 212   _narrow_oop_shift = CompressedOops::shift();
 213   _max_heap_size = MaxHeapSize;

 214   _narrow_klass_shift = CompressedKlassPointers::shift();

 215   if (HeapShared::is_heap_object_archiving_allowed()) {
 216     _heap_end = CompressedOops::end();
 217   }
 218 
 219   // The following fields are for sanity checks for whether this archive
 220   // will function correctly with this JVM and the bootclasspath it's
 221   // invoked with.
 222 
 223   // JVM version string ... changes on each build.
 224   get_header_version(_jvm_ident);
 225 
 226   _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
 227   _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
 228   _num_module_paths = ClassLoader::num_module_path_entries();
 229   _max_used_path_index = ClassLoaderExt::max_used_path_index();
 230 
 231   _verify_local = BytecodeVerificationLocal;
 232   _verify_remote = BytecodeVerificationRemote;
 233   _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
 234   _requested_base_address = (char*)SharedBaseAddress;
 235   _mapped_base_address = (char*)SharedBaseAddress;
 236   _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;
 237   // the following 2 fields will be set in write_header for dynamic archive header
 238   _base_archive_name_size = 0;
 239   _base_archive_is_default = false;
 240 
 241   if (!DynamicDumpSharedSpaces) {
 242     set_shared_path_table(mapinfo->_shared_path_table);
 243   }
 244 }
 245 
 246 void SharedClassPathEntry::init_as_non_existent(const char* path, TRAPS) {
 247   _type = non_existent_entry;
 248   set_name(path, THREAD);
 249 }
 250 
 251 void SharedClassPathEntry::init(bool is_modules_image,
 252                                 ClassPathEntry* cpe, TRAPS) {
 253   Arguments::assert_is_dumping_archive();
 254   _timestamp = 0;
 255   _filesize  = 0;
 256   _from_class_path_attr = false;
 257 
 258   struct stat st;
 259   if (os::stat(cpe->name(), &st) == 0) {
 260     if ((st.st_mode & S_IFMT) == S_IFDIR) {
 261       _type = dir_entry;
 262     } else {
 263       // The timestamp of the modules_image is not checked at runtime.


 587   return npaths;
 588 }
 589 
 590 GrowableArray<const char*>* FileMapInfo::create_path_array(const char* paths) {
 591   GrowableArray<const char*>* path_array =  new(ResourceObj::RESOURCE_AREA, mtInternal)
 592       GrowableArray<const char*>(10);
 593 
 594   ClasspathStream cp_stream(paths);
 595   while (cp_stream.has_next()) {
 596     const char* path = cp_stream.get_next();
 597     struct stat st;
 598     if (os::stat(path, &st) == 0) {
 599       path_array->append(path);
 600     }
 601   }
 602   return path_array;
 603 }
 604 
 605 bool FileMapInfo::fail(const char* msg, const char* name) {
 606   ClassLoader::trace_class_path(msg, name);

 607   return false;
 608 }
 609 
 610 bool FileMapInfo::check_paths(int shared_path_start_idx, int num_paths, GrowableArray<const char*>* rp_array) {
 611   int i = 0;
 612   int j = shared_path_start_idx;
 613   bool mismatch = false;
 614   while (i < num_paths && !mismatch) {
 615     while (shared_path(j)->from_class_path_attr()) {
 616       // shared_path(j) was expanded from the JAR file attribute "Class-Path:"
 617       // during dump time. It's not included in the -classpath VM argument.
 618       j++;
 619     }
 620     if (!os::same_files(shared_path(j)->name(), rp_array->at(i))) {
 621       mismatch = true;
 622     }
 623     i++;
 624     j++;
 625   }
 626   return mismatch;


 921       fail_continue("Unable to read the base archive name from the header.");
 922       FREE_C_HEAP_ARRAY(char, *base_archive_name);
 923       *base_archive_name = NULL;
 924       os::free(dynamic_header);
 925       os::close(fd);
 926       return false;
 927     }
 928   }
 929 
 930   os::free(dynamic_header);
 931   os::close(fd);
 932   return true;
 933 }
 934 
 935 void FileMapInfo::restore_shared_path_table() {
 936   _shared_path_table = _current_info->header()->shared_path_table();
 937 }
 938 
 939 // Read the FileMapInfo information from the file.
 940 
 941 bool FileMapInfo::init_from_file(int fd) {
 942   size_t sz = is_static() ? sizeof(FileMapHeader) : sizeof(DynamicArchiveHeader);
 943   size_t n = os::read(fd, header(), (unsigned int)sz);
 944   if (n != sz) {
 945     fail_continue("Unable to read the file header.");
 946     return false;
 947   }
 948 
 949   if (!Arguments::has_jimage()) {
 950     FileMapInfo::fail_continue("The shared archive file cannot be used with an exploded module build.");
 951     return false;
 952   }
 953 
 954   unsigned int expected_magic = is_static() ? CDS_ARCHIVE_MAGIC : CDS_DYNAMIC_ARCHIVE_MAGIC;
 955   if (header()->magic() != expected_magic) {
 956     log_info(cds)("_magic expected: 0x%08x", expected_magic);
 957     log_info(cds)("         actual: 0x%08x", header()->magic());
 958     FileMapInfo::fail_continue("The shared archive file has a bad magic number.");
 959     return false;
 960   }
 961 
 962   if (header()->version() != CURRENT_CDS_ARCHIVE_VERSION) {
 963     log_info(cds)("_version expected: %d", CURRENT_CDS_ARCHIVE_VERSION);
 964     log_info(cds)("           actual: %d", header()->version());
 965     fail_continue("The shared archive file has the wrong version.");
 966     return false;
 967   }
 968 
 969   if (header()->header_size() != sz) {
 970     log_info(cds)("_header_size expected: " SIZE_FORMAT, sz);
 971     log_info(cds)("               actual: " SIZE_FORMAT, header()->header_size());
 972     FileMapInfo::fail_continue("The shared archive file has an incorrect header size.");
 973     return false;
 974   }


 985   if (strncmp(actual_ident, expected_ident, JVM_IDENT_MAX-1) != 0) {
 986     log_info(cds)("_jvm_ident expected: %s", expected_ident);
 987     log_info(cds)("             actual: %s", actual_ident);
 988     FileMapInfo::fail_continue("The shared archive file was created by a different"
 989                   " version or build of HotSpot");
 990     return false;
 991   }
 992 
 993   if (VerifySharedSpaces) {
 994     int expected_crc = header()->compute_crc();
 995     if (expected_crc != header()->crc()) {
 996       log_info(cds)("_crc expected: %d", expected_crc);
 997       log_info(cds)("       actual: %d", header()->crc());
 998       FileMapInfo::fail_continue("Header checksum verification failed.");
 999       return false;
1000     }
1001   }
1002 
1003   _file_offset = n + header()->base_archive_name_size(); // accounts for the size of _base_archive_name
1004 
1005   if (is_static()) {
1006     // just checking the last region is sufficient since the archive is written
1007     // in sequential order
1008     size_t len = lseek(fd, 0, SEEK_END);
1009     FileMapRegion* si = space_at(MetaspaceShared::last_valid_region);
1010     // The last space might be empty
1011     if (si->file_offset() > len || len - si->file_offset() < si->used()) {
1012       fail_continue("The shared archive file has been truncated.");
1013       return false;
1014     }


1015   }
1016 
1017   return true;
1018 }
1019 
1020 void FileMapInfo::seek_to_position(size_t pos) {
1021   if (lseek(_fd, (long)pos, SEEK_SET) < 0) {
1022     fail_stop("Unable to seek to position " SIZE_FORMAT, pos);
1023   }
1024 }
1025 
1026 // Read the FileMapInfo information from the file.
1027 bool FileMapInfo::open_for_read() {
1028   if (_file_open) {
1029     return true;
1030   }
1031   if (is_static()) {
1032     _full_path = Arguments::GetSharedArchivePath();
1033   } else {
1034     _full_path = Arguments::GetSharedDynamicArchivePath();
1035   }
1036   int fd = os::open(_full_path, O_RDONLY | O_BINARY, 0);
1037   if (fd < 0) {
1038     if (is_static()) {
1039       if (errno == ENOENT) {
1040         // Not locating the shared archive is ok.
1041         fail_continue("Specified shared archive not found (%s).", _full_path);
1042       } else {
1043         fail_continue("Failed to open shared archive file (%s).",
1044                       os::strerror(errno));
1045       }
1046     } else {
1047       log_warning(cds, dynamic)("specified dynamic archive doesn't exist: %s", _full_path);
1048     }
1049     return false;
1050   }
1051 
1052   _fd = fd;
1053   _file_open = true;
1054   return true;
1055 }
1056 
1057 // Write the FileMapInfo information to the file.
1058 
1059 void FileMapInfo::open_for_write(const char* path) {
1060   if (path == NULL) {
1061     _full_path = Arguments::GetSharedArchivePath();
1062   } else {
1063     _full_path = path;
1064   }
1065   LogMessage(cds) msg;
1066   if (msg.is_info()) {
1067     msg.info("Dumping shared data to file: ");
1068     msg.info("   %s", _full_path);


1098 
1099 // Write the header to the file, seek to the next allocation boundary.
1100 
1101 void FileMapInfo::write_header() {
1102   _file_offset = 0;
1103   seek_to_position(_file_offset);
1104   char* base_archive_name = NULL;
1105   if (header()->magic() == CDS_DYNAMIC_ARCHIVE_MAGIC) {
1106     base_archive_name = (char*)Arguments::GetSharedArchivePath();
1107     header()->set_base_archive_name_size(strlen(base_archive_name) + 1);
1108     header()->set_base_archive_is_default(FLAG_IS_DEFAULT(SharedArchiveFile));
1109   }
1110 
1111   assert(is_file_position_aligned(), "must be");
1112   write_bytes(header(), header()->header_size());
1113   if (base_archive_name != NULL) {
1114     write_bytes(base_archive_name, header()->base_archive_name_size());
1115   }
1116 }
1117 
1118 size_t FileMapRegion::used_aligned() const {
1119   return align_up(used(), os::vm_allocation_granularity());
1120 }
1121 
1122 void FileMapRegion::init(bool is_heap_region, char* base, size_t size, bool read_only,
1123                          bool allow_exec, int crc) {
1124   _is_heap_region = is_heap_region;
1125   _mapping_offset = 0;
1126 
1127   if (is_heap_region) {
1128     assert(!DynamicDumpSharedSpaces, "must be");
1129     assert((base - (char*)CompressedKlassPointers::base()) % HeapWordSize == 0, "Sanity");
1130     if (base != NULL) {
1131       _mapping_offset = (size_t)CompressedOops::encode_not_null((oop)base);
1132       assert(_mapping_offset >> 32 == 0, "must be 32-bit only");

1133     }
1134   } else {
1135     if (base != NULL) {
1136       assert(base >= (char*)SharedBaseAddress, "must be");
1137       _mapping_offset = base - (char*)SharedBaseAddress;
1138     } 
1139   }
1140   _used = size;
1141   _read_only = read_only;
1142   _allow_exec = allow_exec;
1143   _crc = crc;
1144   _mapped_from_file = false;
1145   _mapped_base = NULL;
1146 }
1147 
1148 void FileMapInfo::write_region(int region, char* base, size_t size,
1149                                bool read_only, bool allow_exec) {
1150   Arguments::assert_is_dumping_archive();
1151 
1152   FileMapRegion* si = space_at(region);
1153   char* target_base = base;
1154 
1155   if (region == MetaspaceShared::bm) {
1156     target_base = NULL;
1157   } else if (DynamicDumpSharedSpaces) {
1158     assert(!HeapShared::is_heap_region(region), "dynamic archive doesn't support heap regions");
1159     target_base = DynamicArchive::buffer_to_target(base);
1160   }
1161 
1162   si->set_file_offset(_file_offset);
1163   char* requested_base = (target_base == NULL) ? NULL : target_base + MetaspaceShared::final_delta();
1164   log_info(cds)("Shared file region  %d: " SIZE_FORMAT_HEX_W(08)
1165                 " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),
1166                 region, size, p2i(requested_base), _file_offset);
1167 
1168   int crc = ClassLoader::crc32(0, base, (jint)size);
1169   si->init(HeapShared::is_heap_region(region), target_base, size, read_only, allow_exec, crc);
1170 
1171   if (base != NULL) {
1172     write_bytes_aligned(base, size);
1173   }
1174 }
1175 
1176 
1177 void FileMapInfo::write_bitmap_region(const CHeapBitMap* ptrmap) {
1178   ResourceMark rm;
1179   size_t size_in_bits = ptrmap->size();
1180   size_t size_in_bytes = ptrmap->size_in_bytes();
1181   uintptr_t* buffer = (uintptr_t*)NEW_RESOURCE_ARRAY(char, size_in_bytes);
1182   ptrmap->write_to(buffer, size_in_bytes);
1183   header()->set_ptrmap_size_in_bits(size_in_bits);
1184 
1185   log_info(cds)("ptrmap = " INTPTR_FORMAT " (" SIZE_FORMAT " bytes)",
1186                 p2i(buffer), size_in_bytes);
1187   write_region(MetaspaceShared::bm, (char*)buffer, size_in_bytes, /*read_only=*/true, /*allow_exec=*/false);
1188 }
1189 
1190 // Write out the given archive heap memory regions.  GC code combines multiple
1191 // consecutive archive GC regions into one MemRegion whenever possible and
1192 // produces the 'heap_mem' array.
1193 //
1194 // If the archive heap memory size is smaller than a single dump time GC region
1195 // size, there is only one MemRegion in the array.
1196 //
1197 // If the archive heap memory size is bigger than one dump time GC region size,
1198 // the 'heap_mem' array may contain more than one consolidated MemRegions. When
1199 // the first/bottom archive GC region is a partial GC region (with the empty
1200 // portion at the higher address within the region), one MemRegion is used for
1201 // the bottom partial archive GC region. The rest of the consecutive archive
1202 // GC regions are combined into another MemRegion.
1203 //
1204 // Here's the mapping from (archive heap GC regions) -> (GrowableArray<MemRegion> *regions).
1205 //   + We have 1 or more archive heap regions: ah0, ah1, ah2 ..... ahn
1206 //   + We have 1 or 2 consolidated heap memory regions: r0 and r1
1207 //
1208 // If there's a single archive GC region (ah0), then r0 == ah0, and r1 is empty.
1209 // Otherwise:


1227   if(arr_len > max_num_regions) {
1228     fail_stop("Unable to write archive heap memory regions: "
1229               "number of memory regions exceeds maximum due to fragmentation. "
1230               "Please increase java heap size "
1231               "(current MaxHeapSize is " SIZE_FORMAT ", InitialHeapSize is " SIZE_FORMAT ").",
1232               MaxHeapSize, InitialHeapSize);
1233   }
1234 
1235   size_t total_size = 0;
1236   for (int i = first_region_id, arr_idx = 0;
1237            i < first_region_id + max_num_regions;
1238            i++, arr_idx++) {
1239     char* start = NULL;
1240     size_t size = 0;
1241     if (arr_idx < arr_len) {
1242       start = (char*)heap_mem->at(arr_idx).start();
1243       size = heap_mem->at(arr_idx).byte_size();
1244       total_size += size;
1245     }
1246 
1247     log_info(cds)("Archive heap region %d: " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes",
1248                   i, p2i(start), p2i(start + size), size);
1249     write_region(i, start, size, false, false);
1250     if (size > 0) {
1251       address oopmap = oopmaps->at(arr_idx)._oopmap;
1252       assert(oopmap >= (address)SharedBaseAddress, "must be");
1253       space_at(i)->init_oopmap(oopmap - (address)SharedBaseAddress,
1254                                oopmaps->at(arr_idx)._oopmap_size_in_bits);
1255     }
1256   }
1257   return total_size;
1258 }
1259 
1260 // Dump bytes to file -- at the current file position.
1261 
1262 void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) {
1263   assert(_file_open, "must be");
1264   size_t n = os::write(_fd, buffer, (unsigned int)nbytes);
1265   if (n != nbytes) {
1266     // If the shared archive is corrupted, close it and remove it.
1267     close();
1268     remove(_full_path);
1269     fail_stop("Unable to write to shared archive file.");
1270   }
1271   _file_offset += nbytes;
1272 }
1273 


1285   if (new_file_offset != _file_offset) {
1286     _file_offset = new_file_offset;
1287     // Seek one byte back from the target and write a byte to insure
1288     // that the written file is the correct length.
1289     _file_offset -= 1;
1290     seek_to_position(_file_offset);
1291     char zero = 0;
1292     write_bytes(&zero, 1);
1293   }
1294 }
1295 
1296 
1297 // Dump bytes to file -- at the current file position.
1298 
1299 void FileMapInfo::write_bytes_aligned(const void* buffer, size_t nbytes) {
1300   align_file_position();
1301   write_bytes(buffer, nbytes);
1302   align_file_position();
1303 }
1304 
1305 void FileMapInfo::set_final_requested_base(char* b) {
1306   header()->set_final_requested_base(b);
1307 }
1308 
1309 // Close the shared archive file.  This does NOT unmap mapped regions.
1310 
1311 void FileMapInfo::close() {
1312   if (_file_open) {
1313     if (::close(_fd) < 0) {
1314       fail_stop("Unable to close the shared archive file.");
1315     }
1316     _file_open = false;
1317     _fd = -1;
1318   }
1319 }
1320 
1321 
1322 // JVM/TI RedefineClasses() support:
1323 // Remap the shared readonly space to shared readwrite, private.
1324 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
1325   int idx = MetaspaceShared::ro;
1326   FileMapRegion* si = space_at(idx);
1327   if (!si->read_only()) {


1334     return false;
1335   }
1336   char *addr = region_addr(idx);
1337   char *base = os::remap_memory(_fd, _full_path, si->file_offset(),
1338                                 addr, size, false /* !read_only */,
1339                                 si->allow_exec());
1340   close();
1341   // These have to be errors because the shared region is now unmapped.
1342   if (base == NULL) {
1343     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1344     vm_exit(1);
1345   }
1346   if (base != addr) {
1347     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1348     vm_exit(1);
1349   }
1350   si->set_read_only(false);
1351   return true;
1352 }
1353 



















1354 // Memory map a region in the address space.
1355 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "Bitmap",
1356                                             "String1", "String2", "OpenArchive1", "OpenArchive2" };
1357 
1358 MapArchiveResult FileMapInfo::map_regions(int regions[], int num_regions, char* mapped_base_address, ReservedSpace rs) {
1359   FileMapRegion* last_region = NULL;
1360   intx addr_delta = mapped_base_address - header()->requested_base_address();
1361 
1362   DEBUG_ONLY(header()->set_mapped_base_address((char*)(uintptr_t)0xdeadbeef);)
1363 
1364   for (int r = 0; r < num_regions; r++) {
1365     int idx = regions[r];
1366     MapArchiveResult result = map_region(idx, addr_delta, mapped_base_address, rs);
1367     if (result != MAP_ARCHIVE_SUCCESS) {
1368       return result;
1369     }
1370     FileMapRegion* si = space_at(idx);
1371     if (last_region != NULL) {
1372       // Ensure that the OS won't be able to allocate new memory spaces between any mapped
1373       // regions, or else it would mess up the simple comparision in MetaspaceObj::is_shared().
1374       assert(si->mapped_base() == last_region->mapped_end(), "must have no gaps");

1375     }
1376     log_info(cds)("Mapped %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)", is_static() ? "static " : "dynamic",
1377                   idx, p2i(si->mapped_base()), p2i(si->mapped_end()),
1378                   shared_region_name[idx]);
1379     last_region = si;
1380   }
1381 
1382   DEBUG_ONLY(if (addr_delta == 0 && SharedBaseAddress == 0) {
1383       // This is for simulating mmap failures at the requested location, so we can thoroughly
1384       // test the code for failure handling (releasing all allocated resource) and retry mapping
1385       // at an alternative address picked by the OS.
1386       log_info(cds)("SharedBaseAddress == 0: always map archive(s) at an alternative address");
1387       return MAP_ARCHIVE_MMAP_FAILURE;
1388     });
1389 
1390   header()->set_mapped_base_address(header()->requested_base_address() + addr_delta);
1391   if (addr_delta != 0 && !relocate_pointers(addr_delta)) {
1392     return MAP_ARCHIVE_OTHER_FAILURE;
1393   }
1394 
1395   return MAP_ARCHIVE_SUCCESS;
1396 }
1397 
1398 bool FileMapInfo::read_region(int i, char* base, size_t size) {
1399   assert(MetaspaceShared::use_windows_memory_mapping(), "used by windows only");
1400   FileMapRegion* si = space_at(i);
1401   log_info(cds)("Commit %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)%s",
1402                 is_static() ? "static " : "dynamic", i, p2i(base), p2i(base + size),
1403                 shared_region_name[i], si->allow_exec() ? " exec" : "");
1404   if (!os::commit_memory(base, size, si->allow_exec())) {
1405     log_error(cds)("Failed to commit %s region #%d (%s)", is_static() ? "static " : "dynamic",
1406                    i, shared_region_name[i]);
1407     return false;
1408   }
1409   if (lseek(_fd, (long)si->file_offset(), SEEK_SET) != (int)si->file_offset() ||
1410       read_bytes(base, size) != size) {
1411     return false;
1412   }
1413   return true;
1414 }
1415 
1416 MapArchiveResult FileMapInfo::map_region(int i, intx addr_delta, char* mapped_base_address, ReservedSpace rs) {
1417   assert(!HeapShared::is_heap_region(i), "sanity");
1418   FileMapRegion* si = space_at(i);
1419   size_t size = si->used_aligned();
1420   char *requested_addr = mapped_base_address + si->mapping_offset();
1421   assert(si->mapped_base() == NULL, "must be not mapped yet");
1422   assert(requested_addr != NULL, "must be specified");
1423 
1424   si->set_mapped_from_file(false);
1425 
1426   if (MetaspaceShared::use_windows_memory_mapping()) {
1427     // Windows cannot remap read-only shared memory to read-write when required for
1428     // RedefineClasses, which is also used by JFR.  Always map windows regions as RW.
1429     si->set_read_only(false);
1430   } else if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space() ||


1431              Arguments::has_jfr_option()) {
1432     // If a tool agent is in use (debugging enabled), or JFR, we must map the address space RW    
1433     si->set_read_only(false);
1434   } else if (addr_delta != 0) {
1435     si->set_read_only(false); // Need to patch the pointers
1436   }

1437 
1438   if (rs.is_reserved()) {
1439     assert(rs.contains(requested_addr) && rs.contains(requested_addr + size - 1), "must be");
1440     MemTracker::record_virtual_memory_type((address)requested_addr, mtClassShared);
1441   }
1442 
1443   if (MetaspaceShared::use_windows_memory_mapping() && addr_delta != 0) {
1444     // This is the second time we try to map the archive(s). We have already created a ReservedSpace
1445     // that covers all the FileMapRegions to ensure all regions can be mapped. However, Windows
1446     // can't mmap into a ReservedSpace, so we just os::read() the data. We're going to patch all the
1447     // regions anyway, so there's no benefit for mmap anyway.
1448     if (!read_region(i, requested_addr, size)) {
1449       return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
1450     }
1451   } else {
1452     char* base = os::map_memory(_fd, _full_path, si->file_offset(),
1453                                 requested_addr, size, si->read_only(),
1454                                 si->allow_exec());
1455     if (base != requested_addr) {
1456       log_info(cds)("Unable to map %s shared space at required address.", shared_region_name[i]);
1457       _memory_mapping_failed = true;
1458       return MAP_ARCHIVE_MMAP_FAILURE;
1459     }
1460     si->set_mapped_from_file(true);
1461   }
1462   si->set_mapped_base(requested_addr);
1463 
1464   if (!rs.is_reserved()) {
1465     // When mapping on Windows with (addr_delta == 0), we don't reserve the address space for the regions
1466     // (Windows can't mmap into a ReservedSpace). In this case, NMT requires we call it after
1467     // os::map_memory has succeeded.
1468     MemTracker::record_virtual_memory_type((address)requested_addr, mtClassShared);
1469   }





1470 
1471   if (VerifySharedSpaces && !verify_region_checksum(i)) {
1472     return MAP_ARCHIVE_OTHER_FAILURE;
1473   }
1474 
1475   return MAP_ARCHIVE_SUCCESS;
1476 }
1477 
1478 char* FileMapInfo::map_relocation_bitmap(size_t& bitmap_size) {
1479   FileMapRegion* si = space_at(MetaspaceShared::bm);
1480   bitmap_size = si->used_aligned();
1481   bool read_only = true, allow_exec = false;
1482   char* requested_addr = NULL; // allow OS to pick any location
1483   char* bitmap_base = os::map_memory(_fd, _full_path, si->file_offset(),
1484                                      requested_addr, bitmap_size, read_only, allow_exec);
1485 
1486   if (VerifySharedSpaces && bitmap_base != NULL && !region_crc_check(bitmap_base, bitmap_size, si->crc())) {
1487     log_error(cds)("relocation bitmap CRC error");
1488     if (!os::unmap_memory(bitmap_base, bitmap_size)) {
1489       fatal("os::unmap_memory of relocation bitmap failed");
1490     }
1491     return NULL;
1492   }
1493   return bitmap_base;
1494 }
1495 
1496 bool FileMapInfo::relocate_pointers(intx addr_delta) {
1497   log_debug(cds, reloc)("runtime archive relocation start");
1498   size_t bitmap_size;
1499   char* bitmap_base = map_relocation_bitmap(bitmap_size);
1500 
1501   if (bitmap_base != NULL) {
1502     size_t ptrmap_size_in_bits = header()->ptrmap_size_in_bits();
1503     log_debug(cds, reloc)("mapped relocation bitmap @ " INTPTR_FORMAT " (" SIZE_FORMAT
1504                           " bytes = " SIZE_FORMAT " bits)",
1505                           p2i(bitmap_base), bitmap_size, ptrmap_size_in_bits);
1506 
1507     BitMapView ptrmap((BitMap::bm_word_t*)bitmap_base, ptrmap_size_in_bits);
1508 
1509     // Patch all pointers in the the mapped region that are marked by ptrmap.
1510     address patch_base = (address)mapped_base();
1511     address patch_end  = (address)mapped_end();
1512 
1513     // debug only -- the current value of the pointers to be patched must be within this
1514     // range (i.e., must be between the requesed base address, and the of the current archive).
1515     // Note: top archive may point to objects in the base archive, but not the other way around.
1516     address valid_old_base = (address)header()->requested_base_address();
1517     address valid_old_end  = valid_old_base + mapping_end_offset();
1518 
1519     // debug only -- after patching, the pointers must point inside this range
1520     // (the requested location of the archive, as mapped at runtime).
1521     address valid_new_base = (address)header()->mapped_base_address();
1522     address valid_new_end  = (address)mapped_end();
1523 
1524     SharedDataRelocator patcher((address*)patch_base, (address*)patch_end, valid_old_base, valid_old_end,
1525                                 valid_new_base, valid_new_end, addr_delta);
1526     ptrmap.iterate(&patcher);
1527 
1528     if (!os::unmap_memory(bitmap_base, bitmap_size)) {
1529       fatal("os::unmap_memory of relocation bitmap failed");
1530     }
1531     log_debug(cds, reloc)("runtime archive relocation done");
1532     return true;
1533   } else {
1534     log_error(cds)("failed to map relocation bitmap");
1535     return false;
1536   }
1537 }
1538 
1539 size_t FileMapInfo::read_bytes(void* buffer, size_t count) {
1540   assert(_file_open, "Archive file is not open");
1541   size_t n = os::read(_fd, buffer, (unsigned int)count);
1542   if (n != count) {
1543     // Close the file if there's a problem reading it.
1544     close();
1545     return 0;
1546   }
1547   _file_offset += count;
1548   return count;
1549 }
1550 
1551 address FileMapInfo::decode_start_address(FileMapRegion* spc, bool with_current_oop_encoding_mode) {
1552   size_t offset = spc->mapping_offset();
1553   assert((offset >> 32) == 0, "must be 32-bit only");
1554   uint n = (uint)offset;
1555   if (with_current_oop_encoding_mode) {
1556     return (address)CompressedOops::decode_not_null(n);
1557   } else {
1558     return (address)HeapShared::decode_from_archive(n);
1559   }
1560 }
1561 
1562 static MemRegion *closed_archive_heap_ranges = NULL;
1563 static MemRegion *open_archive_heap_ranges = NULL;
1564 static int num_closed_archive_heap_ranges = 0;
1565 static int num_open_archive_heap_ranges = 0;
1566 
1567 #if INCLUDE_CDS_JAVA_HEAP
1568 bool FileMapInfo::has_heap_regions() {
1569   return (space_at(MetaspaceShared::first_closed_archive_heap_region)->used() > 0);
1570 }
1571 
1572 // Returns the address range of the archived heap regions computed using the
1573 // current oop encoding mode. This range may be different than the one seen at
1574 // dump time due to encoding mode differences. The result is used in determining
1575 // if/how these regions should be relocated at run time.
1576 MemRegion FileMapInfo::get_heap_regions_range_with_current_oop_encoding_mode() {
1577   address start = (address) max_uintx;
1578   address end   = NULL;


1806 }
1807 
1808 void FileMapInfo::patch_archived_heap_embedded_pointers() {
1809   if (!_heap_pointers_need_patching) {
1810     return;
1811   }
1812 
1813   patch_archived_heap_embedded_pointers(closed_archive_heap_ranges,
1814                                         num_closed_archive_heap_ranges,
1815                                         MetaspaceShared::first_closed_archive_heap_region);
1816 
1817   patch_archived_heap_embedded_pointers(open_archive_heap_ranges,
1818                                         num_open_archive_heap_ranges,
1819                                         MetaspaceShared::first_open_archive_heap_region);
1820 }
1821 
1822 void FileMapInfo::patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges,
1823                                                         int first_region_idx) {
1824   for (int i=0; i<num_ranges; i++) {
1825     FileMapRegion* si = space_at(i + first_region_idx);
1826     HeapShared::patch_archived_heap_embedded_pointers(ranges[i], (address)(SharedBaseAddress + si->oopmap_offset()),
1827                                                       si->oopmap_size_in_bits());
1828   }
1829 }
1830 
1831 // This internally allocates objects using SystemDictionary::Object_klass(), so it
1832 // must be called after the well-known classes are resolved.
1833 void FileMapInfo::fixup_mapped_heap_regions() {
1834   // If any closed regions were found, call the fill routine to make them parseable.
1835   // Note that closed_archive_heap_ranges may be non-NULL even if no ranges were found.
1836   if (num_closed_archive_heap_ranges != 0) {
1837     assert(closed_archive_heap_ranges != NULL,
1838            "Null closed_archive_heap_ranges array with non-zero count");
1839     G1CollectedHeap::heap()->fill_archive_regions(closed_archive_heap_ranges,
1840                                                   num_closed_archive_heap_ranges);
1841   }
1842 
1843   // do the same for mapped open archive heap regions
1844   if (num_open_archive_heap_ranges != 0) {
1845     assert(open_archive_heap_ranges != NULL, "NULL open_archive_heap_ranges array with non-zero count");
1846     G1CollectedHeap::heap()->fill_archive_regions(open_archive_heap_ranges,


1860 bool FileMapInfo::region_crc_check(char* buf, size_t size, int expected_crc) {
1861   int crc = ClassLoader::crc32(0, buf, (jint)size);
1862   if (crc != expected_crc) {
1863     fail_continue("Checksum verification failed.");
1864     return false;
1865   }
1866   return true;
1867 }
1868 
1869 bool FileMapInfo::verify_region_checksum(int i) {
1870   assert(VerifySharedSpaces, "sanity");
1871   size_t sz = space_at(i)->used();
1872 
1873   if (sz == 0) {
1874     return true; // no data
1875   } else {
1876     return region_crc_check(region_addr(i), sz, space_at(i)->crc());
1877   }
1878 }
1879 
1880 void FileMapInfo::unmap_regions(int regions[], int num_regions) {
1881   for (int r = 0; r < num_regions; r++) {
1882     int idx = regions[r];
1883     unmap_region(idx);

1884   }
1885 }
1886 
1887 // Unmap a memory region in the address space.
1888 
1889 void FileMapInfo::unmap_region(int i) {
1890   assert(!HeapShared::is_heap_region(i), "sanity");
1891   FileMapRegion* si = space_at(i);
1892   char* mapped_base = si->mapped_base();
1893   size_t used = si->used();
1894   size_t size = align_up(used, os::vm_allocation_granularity());
1895 
1896   if (mapped_base != NULL && size > 0 && si->mapped_from_file()) {
1897     log_info(cds)("Unmapping region #%d at base " INTPTR_FORMAT " (%s)", i, p2i(mapped_base),
1898                   shared_region_name[i]);
1899     if (!os::unmap_memory(mapped_base, size)) {
1900       fatal("os::unmap_memory failed");
1901     }
1902     si->set_mapped_base(NULL);



1903   }
1904 }
1905 
1906 void FileMapInfo::assert_mark(bool check) {
1907   if (!check) {
1908     fail_stop("Mark mismatch while restoring from shared file.");
1909   }
1910 }
1911 
1912 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
1913   _shared_path_table.metaspace_pointers_do(it);
1914 }
1915 
1916 FileMapInfo* FileMapInfo::_current_info = NULL;
1917 FileMapInfo* FileMapInfo::_dynamic_archive_info = NULL;
1918 bool FileMapInfo::_heap_pointers_need_patching = false;
1919 SharedPathTable FileMapInfo::_shared_path_table;
1920 bool FileMapInfo::_validating_shared_path_table = false;
1921 bool FileMapInfo::_memory_mapping_failed = false;
1922 GrowableArray<const char*>* FileMapInfo::_non_existent_class_paths = NULL;
1923 
1924 // Open the shared archive file, read and validate the header
1925 // information (version, boot classpath, etc.).  If initialization
1926 // fails, shared spaces are disabled and the file is closed. [See
1927 // fail_continue.]
1928 //
1929 // Validation of the archive is done in two steps:
1930 //
1931 // [1] validate_header() - done here.
1932 // [2] validate_shared_path_table - this is done later, because the table is in the RW
1933 //     region of the archive, which is not mapped yet.
1934 bool FileMapInfo::initialize() {
1935   assert(UseSharedSpaces, "UseSharedSpaces expected.");
1936 
1937   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
1938     // CDS assumes that no classes resolved in SystemDictionary::resolve_well_known_classes
1939     // are replaced at runtime by JVMTI ClassFileLoadHook. All of those classes are resolved
1940     // during the JVMTI "early" stage, so we can still use CDS if
1941     // JvmtiExport::has_early_class_hook_env() is false.
1942     FileMapInfo::fail_continue("CDS is disabled because early JVMTI ClassFileLoadHook is in use.");
1943     return false;
1944   }
1945 
1946   if (!open_for_read()) {
1947     return false;
1948   }
1949   if (!init_from_file(_fd)) {
1950     return false;
1951   }
1952   if (!validate_header()) {

1953     return false;
1954   }
1955   return true;
1956 }
1957 
1958 char* FileMapInfo::region_addr(int idx) {
1959   FileMapRegion* si = space_at(idx);
1960   if (HeapShared::is_heap_region(idx)) {
1961     assert(DumpSharedSpaces, "The following doesn't work at runtime");
1962     return si->used() > 0 ?
1963           (char*)start_address_as_decoded_with_current_oop_encoding_mode(si) : NULL;
1964   } else {
1965     return si->mapped_base();
1966   }
1967 }
1968 
1969 FileMapRegion* FileMapInfo::first_core_space() const {
1970   return is_static() ? space_at(MetaspaceShared::mc) : space_at(MetaspaceShared::rw);
1971 }
1972 
1973 FileMapRegion* FileMapInfo::last_core_space() const {
1974   return is_static() ? space_at(MetaspaceShared::md) : space_at(MetaspaceShared::mc);
1975 }
1976 
1977 int FileMapHeader::compute_crc() {
1978   char* start = (char*)this;
1979   // start computing from the field after _crc
1980   char* buf = (char*)&_crc + sizeof(_crc);
1981   size_t sz = _header_size - (buf - start);
1982   int crc = ClassLoader::crc32(0, buf, (jint)sz);
1983   return crc;
1984 }
1985 
1986 // This function should only be called during run time with UseSharedSpaces enabled.
1987 bool FileMapHeader::validate() {

1988   if (_obj_alignment != ObjectAlignmentInBytes) {
1989     FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d"
1990                   " does not equal the current ObjectAlignmentInBytes of " INTX_FORMAT ".",
1991                   _obj_alignment, ObjectAlignmentInBytes);
1992     return false;
1993   }
1994   if (_compact_strings != CompactStrings) {
1995     FileMapInfo::fail_continue("The shared archive file's CompactStrings setting (%s)"
1996                   " does not equal the current CompactStrings setting (%s).",
1997                   _compact_strings ? "enabled" : "disabled",
1998                   CompactStrings   ? "enabled" : "disabled");
1999     return false;
2000   }
2001 
2002   // This must be done after header validation because it might change the
2003   // header data
2004   const char* prop = Arguments::get_property("java.system.class.loader");
2005   if (prop != NULL) {
2006     warning("Archived non-system classes are disabled because the "
2007             "java.system.class.loader property is specified (value = \"%s\"). "


2020   }
2021 
2022   // Java agents are allowed during run time. Therefore, the following condition is not
2023   // checked: (!_allow_archiving_with_java_agent && AllowArchivingWithJavaAgent)
2024   // Note: _allow_archiving_with_java_agent is set in the shared archive during dump time
2025   // while AllowArchivingWithJavaAgent is set during the current run.
2026   if (_allow_archiving_with_java_agent && !AllowArchivingWithJavaAgent) {
2027     FileMapInfo::fail_continue("The setting of the AllowArchivingWithJavaAgent is different "
2028                                "from the setting in the shared archive.");
2029     return false;
2030   }
2031 
2032   if (_allow_archiving_with_java_agent) {
2033     warning("This archive was created with AllowArchivingWithJavaAgent. It should be used "
2034             "for testing purposes only and should not be used in a production environment");
2035   }
2036 
2037   return true;
2038 }
2039 
2040 bool FileMapInfo::validate_header() {
2041   return header()->validate();
2042 }
2043 
2044 // Check if a given address is within one of the shared regions
2045 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
2046   assert(idx == MetaspaceShared::ro ||
2047          idx == MetaspaceShared::rw ||
2048          idx == MetaspaceShared::mc ||
2049          idx == MetaspaceShared::md, "invalid region index");
2050   char* base = region_addr(idx);
2051   if (p >= base && p < base + space_at(idx)->used()) {
2052     return true;
2053   }
2054   return false;
2055 }
2056 
2057 // Unmap mapped regions of shared space.
2058 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
2059   MetaspaceShared::set_shared_metaspace_range(NULL, NULL, NULL);
2060 
2061   FileMapInfo *map_info = FileMapInfo::current_info();
2062   if (map_info) {
2063     map_info->fail_continue("%s", msg);
2064     for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) {
2065       if (!HeapShared::is_heap_region(i)) {


2066         map_info->unmap_region(i);


2067       }
2068     }
2069     // Dealloc the archive heap regions only without unmapping. The regions are part
2070     // of the java heap. Unmapping of the heap regions are managed by GC.
2071     map_info->dealloc_archive_heap_regions(open_archive_heap_ranges,
2072                                            num_open_archive_heap_ranges,
2073                                            true);
2074     map_info->dealloc_archive_heap_regions(closed_archive_heap_ranges,
2075                                            num_closed_archive_heap_ranges,
2076                                            false);
2077   } else if (DumpSharedSpaces) {
2078     fail_stop("%s", msg);
2079   }
2080 }
2081 
2082 #if INCLUDE_JVMTI
2083 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = NULL;
2084 
2085 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
2086   ClassPathEntry* ent = _classpath_entries_for_jvmti[i];


< prev index next >