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