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