1 /*
   2  * Copyright (c) 2003, 2026, 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 "cds/aotClassLocation.hpp"
  26 #include "cds/aotLogging.hpp"
  27 #include "cds/aotMappedHeapLoader.hpp"
  28 #include "cds/aotMappedHeapWriter.hpp"
  29 #include "cds/aotMetaspace.hpp"
  30 #include "cds/archiveBuilder.hpp"
  31 #include "cds/archiveUtils.inline.hpp"
  32 #include "cds/cds_globals.hpp"
  33 #include "cds/cdsConfig.hpp"
  34 #include "cds/dynamicArchive.hpp"
  35 #include "cds/filemap.hpp"
  36 #include "cds/heapShared.inline.hpp"
  37 #include "classfile/altHashing.hpp"
  38 #include "classfile/classFileStream.hpp"
  39 #include "classfile/classLoader.hpp"
  40 #include "classfile/classLoader.inline.hpp"
  41 #include "classfile/classLoaderData.inline.hpp"
  42 #include "classfile/symbolTable.hpp"
  43 #include "classfile/systemDictionaryShared.hpp"
  44 #include "classfile/vmClasses.hpp"
  45 #include "classfile/vmSymbols.hpp"
  46 #include "compiler/compilerDefinitions.inline.hpp"
  47 #include "jvm.h"
  48 #include "logging/log.hpp"
  49 #include "logging/logMessage.hpp"
  50 #include "logging/logStream.hpp"
  51 #include "memory/iterator.inline.hpp"
  52 #include "memory/metadataFactory.hpp"
  53 #include "memory/metaspaceClosure.hpp"
  54 #include "memory/oopFactory.hpp"
  55 #include "memory/universe.hpp"
  56 #include "nmt/memTracker.hpp"
  57 #include "oops/access.hpp"
  58 #include "oops/compressedKlass.hpp"
  59 #include "oops/compressedOops.hpp"
  60 #include "oops/compressedOops.inline.hpp"
  61 #include "oops/objArrayOop.hpp"
  62 #include "oops/oop.inline.hpp"
  63 #include "oops/trainingData.hpp"
  64 #include "oops/typeArrayKlass.hpp"
  65 #include "prims/jvmtiExport.hpp"
  66 #include "runtime/arguments.hpp"
  67 #include "runtime/globals_extension.hpp"
  68 #include "runtime/java.hpp"
  69 #include "runtime/javaCalls.hpp"
  70 #include "runtime/mutexLocker.hpp"
  71 #include "runtime/os.hpp"
  72 #include "runtime/vm_version.hpp"
  73 #include "utilities/align.hpp"
  74 #include "utilities/bitMap.inline.hpp"
  75 #include "utilities/classpathStream.hpp"
  76 #include "utilities/defaultStream.hpp"
  77 #include "utilities/ostream.hpp"
  78 #if INCLUDE_G1GC
  79 #include "gc/g1/g1CollectedHeap.hpp"
  80 #include "gc/g1/g1HeapRegion.hpp"
  81 #endif
  82 
  83 #include <errno.h>
  84 #include <sys/stat.h>
  85 
  86 #ifndef O_BINARY       // if defined (Win32) use binary files.
  87 #define O_BINARY 0     // otherwise do nothing.
  88 #endif
  89 
  90 inline void CDSMustMatchFlags::do_print(outputStream* st, bool v) {
  91   st->print("%s", v ? "true" : "false");
  92 }
  93 
  94 #ifdef _LP64
  95 inline void CDSMustMatchFlags::do_print(outputStream* st, uint v) {
  96   st->print("%u", v);
  97 }
  98 #endif
  99 
 100 inline void CDSMustMatchFlags::do_print(outputStream* st, intx v) {
 101   st->print("%zd", v);
 102 }
 103 
 104 inline void CDSMustMatchFlags::do_print(outputStream* st, uintx v) {
 105   st->print("%zu", v);
 106 }
 107 
 108 inline void CDSMustMatchFlags::do_print(outputStream* st, double v) {
 109   st->print("%f", v);
 110 }
 111 
 112 void CDSMustMatchFlags::init() {
 113   assert(CDSConfig::is_dumping_archive(), "sanity");
 114   _max_name_width = 0;
 115 
 116 #define INIT_CDS_MUST_MATCH_FLAG(n) \
 117   _v_##n = n; \
 118   _max_name_width = MAX2(_max_name_width,strlen(#n));
 119   CDS_MUST_MATCH_FLAGS_DO(INIT_CDS_MUST_MATCH_FLAG);
 120 #undef INIT_CDS_MUST_MATCH_FLAG
 121 }
 122 
 123 bool CDSMustMatchFlags::runtime_check() const {
 124 #define CHECK_CDS_MUST_MATCH_FLAG(n) \
 125   if (_v_##n != n) { \
 126     ResourceMark rm; \
 127     stringStream ss; \
 128     ss.print("VM option %s is different between dumptime (", #n);  \
 129     do_print(&ss, _v_ ## n); \
 130     ss.print(") and runtime ("); \
 131     do_print(&ss, n); \
 132     ss.print(")"); \
 133     log_info(cds)("%s", ss.as_string()); \
 134     return false; \
 135   }
 136   CDS_MUST_MATCH_FLAGS_DO(CHECK_CDS_MUST_MATCH_FLAG);
 137 #undef CHECK_CDS_MUST_MATCH_FLAG
 138 
 139   return true;
 140 }
 141 
 142 void CDSMustMatchFlags::print_info() const {
 143   LogTarget(Info, cds) lt;
 144   if (lt.is_enabled()) {
 145     LogStream ls(lt);
 146     ls.print_cr("Recorded VM flags during dumptime:");
 147     print(&ls);
 148   }
 149 }
 150 
 151 void CDSMustMatchFlags::print(outputStream* st) const {
 152 #define PRINT_CDS_MUST_MATCH_FLAG(n) \
 153   st->print("- %-s ", #n);                   \
 154   st->sp(int(_max_name_width - strlen(#n))); \
 155   do_print(st, _v_##n);                      \
 156   st->cr();
 157   CDS_MUST_MATCH_FLAGS_DO(PRINT_CDS_MUST_MATCH_FLAG);
 158 #undef PRINT_CDS_MUST_MATCH_FLAG
 159 }
 160 
 161 // Fill in the fileMapInfo structure with data about this VM instance.
 162 
 163 // This method copies the vm version info into header_version.  If the version is too
 164 // long then a truncated version, which has a hash code appended to it, is copied.
 165 //
 166 // Using a template enables this method to verify that header_version is an array of
 167 // length JVM_IDENT_MAX.  This ensures that the code that writes to the CDS file and
 168 // the code that reads the CDS file will both use the same size buffer.  Hence, will
 169 // use identical truncation.  This is necessary for matching of truncated versions.
 170 template <int N> static void get_header_version(char (&header_version) [N]) {
 171   assert(N == JVM_IDENT_MAX, "Bad header_version size");
 172 
 173   const char *vm_version = VM_Version::internal_vm_info_string();
 174   const int version_len = (int)strlen(vm_version);
 175 
 176   memset(header_version, 0, JVM_IDENT_MAX);
 177 
 178   if (version_len < (JVM_IDENT_MAX-1)) {
 179     strcpy(header_version, vm_version);
 180 
 181   } else {
 182     // Get the hash value.  Use a static seed because the hash needs to return the same
 183     // value over multiple jvm invocations.
 184     uint32_t hash = AltHashing::halfsiphash_32(8191, (const uint8_t*)vm_version, version_len);
 185 
 186     // Truncate the ident, saving room for the 8 hex character hash value.
 187     strncpy(header_version, vm_version, JVM_IDENT_MAX-9);
 188 
 189     // Append the hash code as eight hex digits.
 190     os::snprintf_checked(&header_version[JVM_IDENT_MAX-9], 9, "%08x", hash);
 191   }
 192 
 193   assert(header_version[JVM_IDENT_MAX-1] == 0, "must be");
 194 }
 195 
 196 FileMapInfo::FileMapInfo(const char* full_path, bool is_static) :
 197   _is_static(is_static), _file_open(false), _is_mapped(false), _fd(-1), _file_offset(0),
 198   _full_path(full_path), _base_archive_name(nullptr), _header(nullptr) {
 199   if (_is_static) {
 200     assert(_current_info == nullptr, "must be singleton"); // not thread safe
 201     _current_info = this;
 202   } else {
 203     assert(_dynamic_archive_info == nullptr, "must be singleton"); // not thread safe
 204     _dynamic_archive_info = this;
 205   }
 206 }
 207 
 208 FileMapInfo::~FileMapInfo() {
 209   if (_is_static) {
 210     assert(_current_info == this, "must be singleton"); // not thread safe
 211     _current_info = nullptr;
 212   } else {
 213     assert(_dynamic_archive_info == this, "must be singleton"); // not thread safe
 214     _dynamic_archive_info = nullptr;
 215   }
 216 
 217   if (_header != nullptr) {
 218     os::free(_header);
 219   }
 220 
 221   if (_file_open) {
 222     ::close(_fd);
 223   }
 224 }
 225 
 226 void FileMapInfo::free_current_info() {
 227   assert(CDSConfig::is_dumping_final_static_archive(), "only supported in this mode");
 228   assert(_current_info != nullptr, "sanity");
 229   delete _current_info;
 230   assert(_current_info == nullptr, "sanity"); // Side effect expected from the above "delete" operator.
 231 }
 232 
 233 void FileMapInfo::populate_header(size_t core_region_alignment) {
 234   assert(_header == nullptr, "Sanity check");
 235   size_t c_header_size;
 236   size_t header_size;
 237   size_t base_archive_name_size = 0;
 238   size_t base_archive_name_offset = 0;
 239   if (is_static()) {
 240     c_header_size = sizeof(FileMapHeader);
 241     header_size = c_header_size;
 242   } else {
 243     // dynamic header including base archive name for non-default base archive
 244     c_header_size = sizeof(DynamicArchiveHeader);
 245     header_size = c_header_size;
 246 
 247     const char* default_base_archive_name = CDSConfig::default_archive_path();
 248     const char* current_base_archive_name = CDSConfig::input_static_archive_path();
 249     if (!os::same_files(current_base_archive_name, default_base_archive_name)) {
 250       base_archive_name_size = strlen(current_base_archive_name) + 1;
 251       header_size += base_archive_name_size;
 252       base_archive_name_offset = c_header_size;
 253     }
 254   }
 255   _header = (FileMapHeader*)os::malloc(header_size, mtInternal);
 256   memset((void*)_header, 0, header_size);
 257   _header->populate(this,
 258                     core_region_alignment,
 259                     header_size,
 260                     base_archive_name_size,
 261                     base_archive_name_offset);
 262 }
 263 
 264 void FileMapHeader::populate(FileMapInfo *info, size_t core_region_alignment,
 265                              size_t header_size, size_t base_archive_name_size,
 266                              size_t base_archive_name_offset) {
 267   // 1. We require _generic_header._magic to be at the beginning of the file
 268   // 2. FileMapHeader also assumes that _generic_header is at the beginning of the file
 269   assert(offset_of(FileMapHeader, _generic_header) == 0, "must be");
 270   set_header_size((unsigned int)header_size);
 271   set_base_archive_name_offset((unsigned int)base_archive_name_offset);
 272   set_base_archive_name_size((unsigned int)base_archive_name_size);
 273   if (CDSConfig::is_dumping_dynamic_archive()) {
 274     set_magic(CDS_DYNAMIC_ARCHIVE_MAGIC);
 275   } else if (CDSConfig::is_dumping_preimage_static_archive()) {
 276     set_magic(CDS_PREIMAGE_ARCHIVE_MAGIC);
 277   } else {
 278     set_magic(CDS_ARCHIVE_MAGIC);
 279   }
 280   set_version(CURRENT_CDS_ARCHIVE_VERSION);
 281 
 282   if (!info->is_static() && base_archive_name_size != 0) {
 283     // copy base archive name
 284     copy_base_archive_name(CDSConfig::input_static_archive_path());
 285   }
 286   _core_region_alignment = core_region_alignment;
 287   _obj_alignment = ObjectAlignmentInBytes;
 288   _compact_strings = CompactStrings;
 289   _compact_headers = UseCompactObjectHeaders;
 290 #if INCLUDE_CDS_JAVA_HEAP
 291   if (CDSConfig::is_dumping_heap()) {
 292     _object_streaming_mode = HeapShared::is_writing_streaming_mode();
 293     _narrow_oop_mode = AOTMappedHeapWriter::narrow_oop_mode();
 294     _narrow_oop_base = AOTMappedHeapWriter::narrow_oop_base();
 295     _narrow_oop_shift = AOTMappedHeapWriter::narrow_oop_shift();
 296   }
 297 #endif
 298   _compressed_oops = UseCompressedOops;
 299   _compatible_oop_compression = AOTCompatibleOopCompression;
 300   _narrow_klass_pointer_bits = CompressedKlassPointers::narrow_klass_pointer_bits();
 301   _narrow_klass_shift = ArchiveBuilder::precomputed_narrow_klass_shift();
 302 
 303   // Which JIT compier is used
 304   _type_profile_level = TypeProfileLevel;
 305   _type_profile_args_limit = TypeProfileArgsLimit;
 306   _type_profile_parms_limit = TypeProfileParmsLimit;
 307   _type_profile_width = TypeProfileWidth;
 308   _bci_profile_width = BciProfileWidth;
 309   _profile_traps = ProfileTraps;
 310   _type_profile_casts = TypeProfileCasts;
 311   _spec_trap_limit_extra_entries = SpecTrapLimitExtraEntries;
 312   _max_heap_size = MaxHeapSize;
 313   _use_optimized_module_handling = CDSConfig::is_using_optimized_module_handling();
 314   _has_aot_linked_classes = CDSConfig::is_dumping_aot_linked_classes();
 315   _has_full_module_graph = CDSConfig::is_dumping_full_module_graph();
 316   _has_valhalla_patched_classes = Arguments::is_valhalla_enabled();
 317 
 318   // The following fields are for sanity checks for whether this archive
 319   // will function correctly with this JVM and the bootclasspath it's
 320   // invoked with.
 321 
 322   // JVM version string ... changes on each build.
 323   get_header_version(_jvm_ident);
 324 
 325   _verify_local = BytecodeVerificationLocal;
 326   _verify_remote = BytecodeVerificationRemote;
 327   _has_platform_or_app_classes = AOTClassLocationConfig::dumptime()->has_platform_or_app_classes();
 328   _requested_base_address = (char*)SharedBaseAddress;
 329   _mapped_base_address = (char*)SharedBaseAddress;
 330   _must_match.init();
 331 }
 332 
 333 void FileMapHeader::copy_base_archive_name(const char* archive) {
 334   assert(base_archive_name_size() != 0, "_base_archive_name_size not set");
 335   assert(base_archive_name_offset() != 0, "_base_archive_name_offset not set");
 336   assert(header_size() > sizeof(*this), "_base_archive_name_size not included in header size?");
 337   memcpy((char*)this + base_archive_name_offset(), archive, base_archive_name_size());
 338 }
 339 
 340 void FileMapHeader::print(outputStream* st) {
 341   ResourceMark rm;
 342 
 343   st->print_cr("- magic:                          0x%08x", magic());
 344   st->print_cr("- crc:                            0x%08x", crc());
 345   st->print_cr("- version:                        0x%x", version());
 346   st->print_cr("- header_size:                    " UINT32_FORMAT, header_size());
 347   st->print_cr("- base_archive_name_offset:       " UINT32_FORMAT, base_archive_name_offset());
 348   st->print_cr("- base_archive_name_size:         " UINT32_FORMAT, base_archive_name_size());
 349 
 350   for (int i = 0; i < NUM_CDS_REGIONS; i++) {
 351     FileMapRegion* r = region_at(i);
 352     r->print(st, i);
 353   }
 354   st->print_cr("============ end regions ======== ");
 355 
 356   st->print_cr("- core_region_alignment:                    %zu", _core_region_alignment);
 357   st->print_cr("- obj_alignment:                            %d", _obj_alignment);
 358   st->print_cr("- narrow_oop_base:                          " INTPTR_FORMAT, p2i(_narrow_oop_base));
 359   st->print_cr("- narrow_oop_shift                          %d", _narrow_oop_shift);
 360   st->print_cr("- compact_strings:                          %d", _compact_strings);
 361   st->print_cr("- compact_headers:                          %d", _compact_headers);
 362   st->print_cr("- max_heap_size:                            %zu", _max_heap_size);
 363   st->print_cr("- narrow_oop_mode:                          %d", _narrow_oop_mode);
 364   st->print_cr("- compressed_oops:                          %d", _compressed_oops);
 365   st->print_cr("- narrow_klass_pointer_bits:                %d", _narrow_klass_pointer_bits);
 366   st->print_cr("- narrow_klass_shift:                       %d", _narrow_klass_shift);
 367   st->print_cr("- cloned_vtables:                           %u", cast_to_u4(_cloned_vtables));
 368   st->print_cr("- early_serialized_data:                    %u", cast_to_u4(_early_serialized_data));
 369   st->print_cr("- serialized_data:                          %u", cast_to_u4(_serialized_data));
 370   st->print_cr("- jvm_ident:                                %s", _jvm_ident);
 371   st->print_cr("- class_location_config:                    %d", cast_to_u4(_class_location_config));
 372   st->print_cr("- verify_local:                             %d", _verify_local);
 373   st->print_cr("- verify_remote:                            %d", _verify_remote);
 374   st->print_cr("- has_platform_or_app_classes:              %d", _has_platform_or_app_classes);
 375   st->print_cr("- requested_base_address:                   " INTPTR_FORMAT, p2i(_requested_base_address));
 376   st->print_cr("- mapped_base_address:                      " INTPTR_FORMAT, p2i(_mapped_base_address));
 377 
 378   st->print_cr("- object_streaming_mode:                    %d", _object_streaming_mode);
 379   st->print_cr("- mapped_heap_header");
 380   st->print_cr("  - root_segments");
 381   st->print_cr("    - roots_count:                          %d", _mapped_heap_header.root_segments().roots_count());
 382   st->print_cr("    - base_offset:                          0x%zx", _mapped_heap_header.root_segments().base_offset());
 383   st->print_cr("    - count:                                %zu", _mapped_heap_header.root_segments().count());
 384   st->print_cr("    - max_size_elems:                       %d", _mapped_heap_header.root_segments().max_size_in_elems());
 385   st->print_cr("    - max_size_bytes:                       %zu", _mapped_heap_header.root_segments().max_size_in_bytes());
 386   st->print_cr("  - oopmap_start_pos:                       %zu", _mapped_heap_header.oopmap_start_pos());
 387   st->print_cr("  - oopmap_ptrmap_pos:                      %zu", _mapped_heap_header.ptrmap_start_pos());
 388   st->print_cr("- streamed_heap_header");
 389   st->print_cr("  - forwarding_offset:                      %zu", _streamed_heap_header.forwarding_offset());
 390   st->print_cr("  - roots_offset:                           %zu", _streamed_heap_header.roots_offset());
 391   st->print_cr("  - num_roots:                              %zu", _streamed_heap_header.num_roots());
 392   st->print_cr("  - root_highest_object_index_table_offset: %zu", _streamed_heap_header.root_highest_object_index_table_offset());
 393   st->print_cr("  - num_archived_objects:                   %zu", _streamed_heap_header.num_archived_objects());
 394 
 395   st->print_cr("- _rw_ptrmap_start_pos:                     %zu", _rw_ptrmap_start_pos);
 396   st->print_cr("- _ro_ptrmap_start_pos:                     %zu", _ro_ptrmap_start_pos);
 397   st->print_cr("- use_optimized_module_handling:            %d", _use_optimized_module_handling);
 398   st->print_cr("- has_full_module_graph                     %d", _has_full_module_graph);
 399   st->print_cr("- has_valhalla_patched_classes              %d", _has_valhalla_patched_classes);
 400   _must_match.print(st);
 401   st->print_cr("- has_aot_linked_classes                    %d", _has_aot_linked_classes);
 402 }
 403 
 404 bool FileMapInfo::validate_class_location() {
 405   assert(CDSConfig::is_using_archive(), "runtime only");
 406 
 407   AOTClassLocationConfig* config = header()->class_location_config();
 408   bool has_extra_module_paths = false;
 409   if (!config->validate(full_path(), header()->has_aot_linked_classes(), &has_extra_module_paths)) {
 410     if (PrintSharedArchiveAndExit) {
 411       AOTMetaspace::set_archive_loading_failed();
 412       return true;
 413     } else {
 414       return false;
 415     }
 416   }
 417 
 418   if (header()->has_full_module_graph() && has_extra_module_paths) {
 419     CDSConfig::stop_using_optimized_module_handling();
 420     AOTMetaspace::report_loading_error("optimized module handling: disabled because extra module path(s) are specified");
 421   }
 422 
 423   if (CDSConfig::is_dumping_dynamic_archive()) {
 424     // Only support dynamic dumping with the usage of the default CDS archive
 425     // or a simple base archive.
 426     // If the base layer archive contains additional path component besides
 427     // the runtime image and the -cp, dynamic dumping is disabled.
 428     if (config->num_boot_classpaths() > 0) {
 429       CDSConfig::disable_dumping_dynamic_archive();
 430       aot_log_warning(aot)(
 431         "Dynamic archiving is disabled because base layer archive has appended boot classpath");
 432     }
 433     if (config->num_module_paths() > 0) {
 434       if (has_extra_module_paths) {
 435         CDSConfig::disable_dumping_dynamic_archive();
 436         aot_log_warning(aot)(
 437           "Dynamic archiving is disabled because base layer archive has a different module path");
 438       }
 439     }
 440   }
 441 
 442 #if INCLUDE_JVMTI
 443   if (_classpath_entries_for_jvmti != nullptr) {
 444     os::free(_classpath_entries_for_jvmti);
 445   }
 446   size_t sz = sizeof(ClassPathEntry*) * AOTClassLocationConfig::runtime()->length();
 447   _classpath_entries_for_jvmti = (ClassPathEntry**)os::malloc(sz, mtClass);
 448   memset((void*)_classpath_entries_for_jvmti, 0, sz);
 449 #endif
 450 
 451   return true;
 452 }
 453 
 454 // A utility class for reading/validating the GenericCDSFileMapHeader portion of
 455 // a CDS archive's header. The file header of all CDS archives with versions from
 456 // CDS_GENERIC_HEADER_SUPPORTED_MIN_VERSION (12) are guaranteed to always start
 457 // with GenericCDSFileMapHeader. This makes it possible to read important information
 458 // from a CDS archive created by a different version of HotSpot, so that we can
 459 // automatically regenerate the archive as necessary (JDK-8261455).
 460 class FileHeaderHelper {
 461   int _fd;
 462   bool _is_valid;
 463   bool _is_static;
 464   GenericCDSFileMapHeader* _header;
 465   const char* _archive_name;
 466   const char* _base_archive_name;
 467 
 468 public:
 469   FileHeaderHelper(const char* archive_name, bool is_static) {
 470     _fd = -1;
 471     _is_valid = false;
 472     _header = nullptr;
 473     _base_archive_name = nullptr;
 474     _archive_name = archive_name;
 475     _is_static = is_static;
 476   }
 477 
 478   ~FileHeaderHelper() {
 479     if (_header != nullptr) {
 480       FREE_C_HEAP_ARRAY(_header);
 481     }
 482     if (_fd != -1) {
 483       ::close(_fd);
 484     }
 485   }
 486 
 487   bool initialize() {
 488     assert(_archive_name != nullptr, "Archive name is null");
 489     _fd = os::open(_archive_name, O_RDONLY | O_BINARY, 0);
 490     if (_fd < 0) {
 491       AOTMetaspace::report_loading_error("Specified %s not found (%s)", CDSConfig::type_of_archive_being_loaded(), _archive_name);
 492       return false;
 493     }
 494     return initialize(_fd);
 495   }
 496 
 497   // for an already opened file, do not set _fd
 498   bool initialize(int fd) {
 499     assert(_archive_name != nullptr, "Archive name is null");
 500     assert(fd != -1, "Archive must be opened already");
 501     // First read the generic header so we know the exact size of the actual header.
 502     const char* file_type = CDSConfig::type_of_archive_being_loaded();
 503     GenericCDSFileMapHeader gen_header;
 504     size_t size = sizeof(GenericCDSFileMapHeader);
 505     os::lseek(fd, 0, SEEK_SET);
 506     size_t n = ::read(fd, (void*)&gen_header, (unsigned int)size);
 507     if (n != size) {
 508       aot_log_warning(aot)("Unable to read generic CDS file map header from %s", file_type);
 509       return false;
 510     }
 511 
 512     if (gen_header._magic != CDS_ARCHIVE_MAGIC &&
 513         gen_header._magic != CDS_DYNAMIC_ARCHIVE_MAGIC &&
 514         gen_header._magic != CDS_PREIMAGE_ARCHIVE_MAGIC) {
 515       aot_log_warning(aot)("The %s has a bad magic number: %#x", file_type, gen_header._magic);
 516       return false;
 517     }
 518 
 519     if (gen_header._version < CDS_GENERIC_HEADER_SUPPORTED_MIN_VERSION) {
 520       aot_log_warning(aot)("Cannot handle %s version 0x%x. Must be at least 0x%x.",
 521                        file_type, gen_header._version, CDS_GENERIC_HEADER_SUPPORTED_MIN_VERSION);
 522       return false;
 523     }
 524 
 525     if (gen_header._version !=  CURRENT_CDS_ARCHIVE_VERSION) {
 526       aot_log_warning(aot)("The %s version 0x%x does not match the required version 0x%x.",
 527                        file_type, gen_header._version, CURRENT_CDS_ARCHIVE_VERSION);
 528     }
 529 
 530     size_t filelen = os::lseek(fd, 0, SEEK_END);
 531     if (gen_header._header_size >= filelen) {
 532       aot_log_warning(aot)("Archive file header larger than archive file");
 533       return false;
 534     }
 535 
 536     // Read the actual header and perform more checks
 537     size = gen_header._header_size;
 538     _header = (GenericCDSFileMapHeader*)NEW_C_HEAP_ARRAY(char, size, mtInternal);
 539     os::lseek(fd, 0, SEEK_SET);
 540     n = ::read(fd, (void*)_header, (unsigned int)size);
 541     if (n != size) {
 542       aot_log_warning(aot)("Unable to read file map header from %s", file_type);
 543       return false;
 544     }
 545 
 546     if (!check_header_crc()) {
 547       return false;
 548     }
 549 
 550     if (!check_and_init_base_archive_name()) {
 551       return false;
 552     }
 553 
 554     // All fields in the GenericCDSFileMapHeader has been validated.
 555     _is_valid = true;
 556     return true;
 557   }
 558 
 559   GenericCDSFileMapHeader* get_generic_file_header() {
 560     assert(_header != nullptr && _is_valid, "must be a valid archive file");
 561     return _header;
 562   }
 563 
 564   const char* base_archive_name() {
 565     assert(_header != nullptr && _is_valid, "must be a valid archive file");
 566     return _base_archive_name;
 567   }
 568 
 569   bool is_static_archive() const {
 570     return _header->_magic == CDS_ARCHIVE_MAGIC;
 571   }
 572 
 573   bool is_dynamic_archive() const {
 574     return _header->_magic == CDS_DYNAMIC_ARCHIVE_MAGIC;
 575   }
 576 
 577   bool is_preimage_static_archive() const {
 578     return _header->_magic == CDS_PREIMAGE_ARCHIVE_MAGIC;
 579   }
 580 
 581  private:
 582   bool check_header_crc() const {
 583     if (VerifySharedSpaces) {
 584       FileMapHeader* header = (FileMapHeader*)_header;
 585       int actual_crc = header->compute_crc();
 586       if (actual_crc != header->crc()) {
 587         aot_log_info(aot)("_crc expected: %d", header->crc());
 588         aot_log_info(aot)("       actual: %d", actual_crc);
 589         aot_log_warning(aot)("Header checksum verification failed.");
 590         return false;
 591       }
 592     }
 593     return true;
 594   }
 595 
 596   bool check_and_init_base_archive_name() {
 597     unsigned int name_offset = _header->_base_archive_name_offset;
 598     unsigned int name_size   = _header->_base_archive_name_size;
 599     unsigned int header_size = _header->_header_size;
 600 
 601     if (name_offset + name_size < name_offset) {
 602       aot_log_warning(aot)("base_archive_name offset/size overflow: " UINT32_FORMAT "/" UINT32_FORMAT,
 603                                  name_offset, name_size);
 604       return false;
 605     }
 606 
 607     if (is_static_archive() || is_preimage_static_archive()) {
 608       if (name_offset != 0) {
 609         aot_log_warning(aot)("static shared archive must have zero _base_archive_name_offset");
 610         return false;
 611       }
 612       if (name_size != 0) {
 613         aot_log_warning(aot)("static shared archive must have zero _base_archive_name_size");
 614         return false;
 615       }
 616     } else {
 617       assert(is_dynamic_archive(), "must be");
 618       if ((name_size == 0 && name_offset != 0) ||
 619           (name_size != 0 && name_offset == 0)) {
 620         // If either is zero, both must be zero. This indicates that we are using the default base archive.
 621         aot_log_warning(aot)("Invalid base_archive_name offset/size: " UINT32_FORMAT "/" UINT32_FORMAT,
 622                                    name_offset, name_size);
 623         return false;
 624       }
 625       if (name_size > 0) {
 626         if (name_offset + name_size > header_size) {
 627           aot_log_warning(aot)("Invalid base_archive_name offset/size (out of range): "
 628                                      UINT32_FORMAT " + " UINT32_FORMAT " > " UINT32_FORMAT ,
 629                                      name_offset, name_size, header_size);
 630           return false;
 631         }
 632         const char* name = ((const char*)_header) + _header->_base_archive_name_offset;
 633         if (name[name_size - 1] != '\0' || strlen(name) != name_size - 1) {
 634           aot_log_warning(aot)("Base archive name is damaged");
 635           return false;
 636         }
 637         if (!os::file_exists(name)) {
 638           aot_log_warning(aot)("Base archive %s does not exist", name);
 639           return false;
 640         }
 641         _base_archive_name = name;
 642       }
 643     }
 644 
 645     return true;
 646   }
 647 };
 648 
 649 // Return value:
 650 // false:
 651 //      <archive_name> is not a valid archive. *base_archive_name is set to null.
 652 // true && (*base_archive_name) == nullptr:
 653 //      <archive_name> is a valid static archive.
 654 // true && (*base_archive_name) != nullptr:
 655 //      <archive_name> is a valid dynamic archive.
 656 bool FileMapInfo::get_base_archive_name_from_header(const char* archive_name,
 657                                                     const char** base_archive_name) {
 658   FileHeaderHelper file_helper(archive_name, false);
 659   *base_archive_name = nullptr;
 660 
 661   if (!file_helper.initialize()) {
 662     return false;
 663   }
 664   GenericCDSFileMapHeader* header = file_helper.get_generic_file_header();
 665   switch (header->_magic) {
 666   case CDS_PREIMAGE_ARCHIVE_MAGIC:
 667     return false; // This is a binary config file, not a proper archive
 668   case CDS_DYNAMIC_ARCHIVE_MAGIC:
 669     break;
 670   default:
 671     assert(header->_magic == CDS_ARCHIVE_MAGIC, "must be");
 672     if (AutoCreateSharedArchive) {
 673      aot_log_warning(aot)("AutoCreateSharedArchive is ignored because %s is a static archive", archive_name);
 674     }
 675     return true;
 676   }
 677 
 678   const char* base = file_helper.base_archive_name();
 679   if (base == nullptr) {
 680     *base_archive_name = CDSConfig::default_archive_path();
 681   } else {
 682     *base_archive_name = os::strdup_check_oom(base);
 683   }
 684 
 685   return true;
 686 }
 687 
 688 bool FileMapInfo::is_preimage_static_archive(const char* file) {
 689   FileHeaderHelper file_helper(file, false);
 690   if (!file_helper.initialize()) {
 691     return false;
 692   }
 693   return file_helper.is_preimage_static_archive();
 694 }
 695 
 696 // Read the FileMapInfo information from the file.
 697 
 698 bool FileMapInfo::init_from_file(int fd) {
 699   FileHeaderHelper file_helper(_full_path, _is_static);
 700   if (!file_helper.initialize(fd)) {
 701     aot_log_warning(aot)("Unable to read the file header.");
 702     return false;
 703   }
 704   GenericCDSFileMapHeader* gen_header = file_helper.get_generic_file_header();
 705 
 706   const char* file_type = CDSConfig::type_of_archive_being_loaded();
 707   if (_is_static) {
 708     if ((gen_header->_magic == CDS_ARCHIVE_MAGIC) ||
 709         (gen_header->_magic == CDS_PREIMAGE_ARCHIVE_MAGIC && CDSConfig::is_dumping_final_static_archive())) {
 710       // Good
 711     } else {
 712       if (CDSConfig::new_aot_flags_used()) {
 713         aot_log_warning(aot)("Not a valid %s (%s)", file_type, _full_path);
 714       } else {
 715         aot_log_warning(aot)("Not a base shared archive: %s", _full_path);
 716       }
 717       return false;
 718     }
 719   } else {
 720     if (gen_header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
 721       aot_log_warning(aot)("Not a top shared archive: %s", _full_path);
 722       return false;
 723     }
 724   }
 725 
 726   _header = (FileMapHeader*)os::malloc(gen_header->_header_size, mtInternal);
 727   os::lseek(fd, 0, SEEK_SET); // reset to begin of the archive
 728   size_t size = gen_header->_header_size;
 729   size_t n = ::read(fd, (void*)_header, (unsigned int)size);
 730   if (n != size) {
 731     aot_log_warning(aot)("Failed to read file header from the top archive file\n");
 732     return false;
 733   }
 734 
 735   if (header()->version() != CURRENT_CDS_ARCHIVE_VERSION) {
 736     aot_log_info(aot)("_version expected: 0x%x", CURRENT_CDS_ARCHIVE_VERSION);
 737     aot_log_info(aot)("           actual: 0x%x", header()->version());
 738     aot_log_warning(aot)("The %s has the wrong version.", file_type);
 739     return false;
 740   }
 741 
 742   unsigned int base_offset = header()->base_archive_name_offset();
 743   unsigned int name_size = header()->base_archive_name_size();
 744   unsigned int header_size = header()->header_size();
 745   if (base_offset != 0 && name_size != 0) {
 746     if (header_size != base_offset + name_size) {
 747       aot_log_info(aot)("_header_size: " UINT32_FORMAT, header_size);
 748       aot_log_info(aot)("base_archive_name_size: " UINT32_FORMAT, header()->base_archive_name_size());
 749       aot_log_info(aot)("base_archive_name_offset: " UINT32_FORMAT, header()->base_archive_name_offset());
 750       aot_log_warning(aot)("The %s has an incorrect header size.", file_type);
 751       return false;
 752     }
 753   }
 754 
 755   const char* actual_ident = header()->jvm_ident();
 756 
 757   if (actual_ident[JVM_IDENT_MAX-1] != 0) {
 758     aot_log_warning(aot)("JVM version identifier is corrupted.");
 759     return false;
 760   }
 761 
 762   char expected_ident[JVM_IDENT_MAX];
 763   get_header_version(expected_ident);
 764   if (strncmp(actual_ident, expected_ident, JVM_IDENT_MAX-1) != 0) {
 765     aot_log_info(aot)("_jvm_ident expected: %s", expected_ident);
 766     aot_log_info(aot)("             actual: %s", actual_ident);
 767     aot_log_warning(aot)("The %s was created by a different"
 768                   " version or build of HotSpot", file_type);
 769     return false;
 770   }
 771 
 772   _file_offset = header()->header_size(); // accounts for the size of _base_archive_name
 773 
 774   size_t len = os::lseek(fd, 0, SEEK_END);
 775 
 776   for (int i = 0; i < AOTMetaspace::n_regions; i++) {
 777     FileMapRegion* r = region_at(i);
 778     if (r->file_offset() > len || len - r->file_offset() < r->used()) {
 779       aot_log_warning(aot)("The %s has been truncated.", file_type);
 780       return false;
 781     }
 782   }
 783 
 784   if (!header()->check_must_match_flags()) {
 785     return false;
 786   }
 787 
 788   return true;
 789 }
 790 
 791 void FileMapInfo::seek_to_position(size_t pos) {
 792   if (os::lseek(_fd, (jlong)pos, SEEK_SET) < 0) {
 793     aot_log_error(aot)("Unable to seek to position %zu (errno=%d: %s)", pos, errno, os::strerror(errno));
 794     AOTMetaspace::unrecoverable_loading_error();
 795   }
 796 }
 797 
 798 // Read the FileMapInfo information from the file.
 799 bool FileMapInfo::open_for_read() {
 800   if (_file_open) {
 801     return true;
 802   }
 803   const char* file_type = CDSConfig::type_of_archive_being_loaded();
 804   const char* info = CDSConfig::is_dumping_final_static_archive() ?
 805     "AOTConfiguration file " : "";
 806   aot_log_info(aot)("trying to map %s%s", info, _full_path);
 807   int fd = os::open(_full_path, O_RDONLY | O_BINARY, 0);
 808   if (fd < 0) {
 809     if (errno == ENOENT) {
 810       aot_log_info(aot)("Specified %s not found (%s)", file_type, _full_path);
 811     } else {
 812       aot_log_warning(aot)("Failed to open %s (%s)", file_type,
 813                     os::strerror(errno));
 814     }
 815     return false;
 816   } else {
 817     aot_log_info(aot)("Opened %s %s.", file_type, _full_path);
 818   }
 819 
 820   _fd = fd;
 821   _file_open = true;
 822   return true;
 823 }
 824 
 825 // Write the FileMapInfo information to the file.
 826 
 827 void FileMapInfo::open_as_output() {
 828   if (CDSConfig::new_aot_flags_used()) {
 829     if (CDSConfig::is_dumping_preimage_static_archive()) {
 830       log_info(aot)("Writing binary AOTConfiguration file: %s",  _full_path);
 831     } else {
 832       log_info(aot)("Writing AOTCache file: %s",  _full_path);
 833     }
 834   } else {
 835     aot_log_info(aot)("Dumping shared data to file: %s", _full_path);
 836   }
 837 
 838 #ifdef _WINDOWS  // On Windows, need WRITE permission to remove the file.
 839   chmod(_full_path, _S_IREAD | _S_IWRITE);
 840 #endif
 841 
 842   // Use remove() to delete the existing file because, on Unix, this will
 843   // allow processes that have it open continued access to the file.
 844   remove(_full_path);
 845   int fd = os::open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
 846   if (fd < 0) {
 847     aot_log_error(aot)("Unable to create %s %s: (%s).", CDSConfig::type_of_archive_being_written(), _full_path,
 848                    os::strerror(errno));
 849     AOTMetaspace::writing_error();
 850     return;
 851   }
 852   _fd = fd;
 853   _file_open = true;
 854 }
 855 
 856 void FileMapInfo::prepare_for_writing() {
 857   // Seek past the header. We will write the header after all regions are written
 858   // and their CRCs computed.
 859   size_t header_bytes = header()->header_size();
 860 
 861   header_bytes = align_up(header_bytes, AOTMetaspace::core_region_alignment());
 862   _file_offset = header_bytes;
 863   seek_to_position(_file_offset);
 864 }
 865 
 866 // Write the header to the file, seek to the next allocation boundary.
 867 
 868 void FileMapInfo::write_header() {
 869   _file_offset = 0;
 870   seek_to_position(_file_offset);
 871   assert(is_file_position_aligned(), "must be");
 872   write_bytes(header(), header()->header_size());
 873 }
 874 
 875 size_t FileMapRegion::used_aligned() const {
 876   return align_up(used(), AOTMetaspace::core_region_alignment());
 877 }
 878 
 879 void FileMapRegion::init(int region_index, size_t mapping_offset, size_t size, bool read_only,
 880                          bool allow_exec, int crc) {
 881   _is_heap_region = HeapShared::is_heap_region(region_index);
 882   _is_bitmap_region = (region_index == AOTMetaspace::bm);
 883   _mapping_offset = mapping_offset;
 884   _used = size;
 885   _read_only = read_only;
 886   _allow_exec = allow_exec;
 887   _crc = crc;
 888   _mapped_from_file = false;
 889   _mapped_base = nullptr;
 890   _in_reserved_space = false;
 891 }
 892 
 893 void FileMapRegion::init_oopmap(size_t offset, size_t size_in_bits) {
 894   _oopmap_offset = offset;
 895   _oopmap_size_in_bits = size_in_bits;
 896 }
 897 
 898 void FileMapRegion::init_ptrmap(size_t offset, size_t size_in_bits) {
 899   _ptrmap_offset = offset;
 900   _ptrmap_size_in_bits = size_in_bits;
 901 }
 902 
 903 bool FileMapRegion::check_region_crc(char* base) const {
 904   // This function should be called after the region has been properly
 905   // loaded into memory via FileMapInfo::map_region() or FileMapInfo::read_region().
 906   // I.e., this->mapped_base() must be valid.
 907   size_t sz = used();
 908   if (sz == 0) {
 909     return true;
 910   }
 911 
 912   assert(base != nullptr, "must be initialized");
 913   int crc = ClassLoader::crc32(0, base, (jint)sz);
 914   if (crc != this->crc()) {
 915     aot_log_warning(aot)("Checksum verification failed.");
 916     return false;
 917   }
 918   return true;
 919 }
 920 
 921 static const char* region_name(int region_index) {
 922   static const char* names[] = {
 923     "rw", "ro", "bm", "hp", "ac"
 924   };
 925   const int num_regions = sizeof(names)/sizeof(names[0]);
 926   assert(0 <= region_index && region_index < num_regions, "sanity");
 927 
 928   return names[region_index];
 929 }
 930 
 931 BitMapView FileMapInfo::bitmap_view(int region_index, bool is_oopmap) {
 932   FileMapRegion* r = region_at(region_index);
 933   char* bitmap_base = is_static() ? FileMapInfo::current_info()->map_bitmap_region() : FileMapInfo::dynamic_info()->map_bitmap_region();
 934   bitmap_base += is_oopmap ? r->oopmap_offset() : r->ptrmap_offset();
 935   size_t size_in_bits = is_oopmap ? r->oopmap_size_in_bits() : r->ptrmap_size_in_bits();
 936 
 937   aot_log_debug(aot, reloc)("mapped %s relocation %smap @ " INTPTR_FORMAT " (%zu bits)",
 938                         region_name(region_index), is_oopmap ? "oop" : "ptr",
 939                         p2i(bitmap_base), size_in_bits);
 940 
 941   return BitMapView((BitMap::bm_word_t*)(bitmap_base), size_in_bits);
 942 }
 943 
 944 BitMapView FileMapInfo::oopmap_view(int region_index) {
 945     return bitmap_view(region_index, /*is_oopmap*/true);
 946   }
 947 
 948 BitMapView FileMapInfo::ptrmap_view(int region_index) {
 949   return bitmap_view(region_index, /*is_oopmap*/false);
 950 }
 951 
 952 void FileMapRegion::print(outputStream* st, int region_index) {
 953   st->print_cr("============ region ============= %d \"%s\"", region_index, region_name(region_index));
 954   st->print_cr("- crc:                            0x%08x", _crc);
 955   st->print_cr("- read_only:                      %d", _read_only);
 956   st->print_cr("- allow_exec:                     %d", _allow_exec);
 957   st->print_cr("- is_heap_region:                 %d", _is_heap_region);
 958   st->print_cr("- is_bitmap_region:               %d", _is_bitmap_region);
 959   st->print_cr("- mapped_from_file:               %d", _mapped_from_file);
 960   st->print_cr("- file_offset:                    0x%zx", _file_offset);
 961   st->print_cr("- mapping_offset:                 0x%zx", _mapping_offset);
 962   st->print_cr("- used:                           %zu", _used);
 963   st->print_cr("- oopmap_offset:                  0x%zx", _oopmap_offset);
 964   st->print_cr("- oopmap_size_in_bits:            %zu", _oopmap_size_in_bits);
 965   st->print_cr("- ptrmap_offset:                  0x%zx", _ptrmap_offset);
 966   st->print_cr("- ptrmap_size_in_bits:            %zu", _ptrmap_size_in_bits);
 967   st->print_cr("- mapped_base:                    " INTPTR_FORMAT, p2i(_mapped_base));
 968 }
 969 
 970 void FileMapInfo::write_region(int region, char* base, size_t size,
 971                                bool read_only, bool allow_exec) {
 972   assert(CDSConfig::is_dumping_archive(), "sanity");
 973 
 974   FileMapRegion* r = region_at(region);
 975   char* requested_base;
 976   size_t mapping_offset = 0;
 977 
 978   if (region == AOTMetaspace::bm) {
 979     requested_base = nullptr; // always null for bm region
 980   } else if (size == 0) {
 981     // This is an unused region (e.g., a heap region when !INCLUDE_CDS_JAVA_HEAP)
 982     requested_base = nullptr;
 983   } else if (HeapShared::is_heap_region(region)) {
 984     assert(CDSConfig::is_dumping_heap(), "sanity");
 985 #if INCLUDE_CDS_JAVA_HEAP
 986     assert(!CDSConfig::is_dumping_dynamic_archive(), "must be");
 987     if (HeapShared::is_writing_mapping_mode()) {
 988       requested_base = (char*)AOTMappedHeapWriter::requested_address();
 989       if (UseCompressedOops) {
 990         mapping_offset = (size_t)((address)requested_base - AOTMappedHeapWriter::narrow_oop_base());
 991         assert((mapping_offset >> CompressedOops::shift()) << CompressedOops::shift() == mapping_offset, "must be");
 992       }
 993     } else {
 994       requested_base = nullptr;
 995     }
 996 #endif // INCLUDE_CDS_JAVA_HEAP
 997   } else {
 998     char* requested_SharedBaseAddress = (char*)AOTMetaspace::requested_base_address();
 999     requested_base = ArchiveBuilder::current()->to_requested(base);
1000     assert(requested_base >= requested_SharedBaseAddress, "must be");
1001     mapping_offset = requested_base - requested_SharedBaseAddress;
1002   }
1003 
1004   r->set_file_offset(_file_offset);
1005   int crc = ClassLoader::crc32(0, base, (jint)size);
1006   if (size > 0) {
1007     aot_log_info(aot)("Shared file region (%s) %d: %8zu"
1008                    " bytes, addr " INTPTR_FORMAT " file offset 0x%08" PRIxPTR
1009                    " crc 0x%08x",
1010                    region_name(region), region, size, p2i(requested_base), _file_offset, crc);
1011   } else {
1012     aot_log_info(aot)("Shared file region (%s) %d: %8zu"
1013                    " bytes", region_name(region), region, size);
1014   }
1015 
1016   r->init(region, mapping_offset, size, read_only, allow_exec, crc);
1017 
1018   if (base != nullptr) {
1019     write_bytes_aligned(base, size);
1020   }
1021 }
1022 
1023 static size_t write_bitmap(const CHeapBitMap* map, char* output, size_t offset) {
1024   size_t size_in_bytes = map->size_in_bytes();
1025   map->write_to((BitMap::bm_word_t*)(output + offset), size_in_bytes);
1026   return offset + size_in_bytes;
1027 }
1028 
1029 // The sorting code groups the objects with non-null oop/ptrs together.
1030 // Relevant bitmaps then have lots of leading and trailing zeros, which
1031 // we do not have to store.
1032 size_t FileMapInfo::remove_bitmap_zeros(CHeapBitMap* map) {
1033   BitMap::idx_t first_set = map->find_first_set_bit(0);
1034   BitMap::idx_t last_set  = map->find_last_set_bit(0);
1035   size_t old_size = map->size();
1036 
1037   // Slice and resize bitmap
1038   map->truncate(first_set, last_set + 1);
1039 
1040   assert(map->at(0), "First bit should be set");
1041   assert(map->at(map->size() - 1), "Last bit should be set");
1042   assert(map->size() <= old_size, "sanity");
1043 
1044   return first_set;
1045 }
1046 
1047 char* FileMapInfo::write_bitmap_region(CHeapBitMap* rw_ptrmap,
1048                                        CHeapBitMap* ro_ptrmap,
1049                                        AOTMappedHeapInfo* mapped_heap_info,
1050                                        AOTStreamedHeapInfo* streamed_heap_info,
1051                                        size_t &size_in_bytes) {
1052   size_t removed_rw_leading_zeros = remove_bitmap_zeros(rw_ptrmap);
1053   size_t removed_ro_leading_zeros = remove_bitmap_zeros(ro_ptrmap);
1054   header()->set_rw_ptrmap_start_pos(removed_rw_leading_zeros);
1055   header()->set_ro_ptrmap_start_pos(removed_ro_leading_zeros);
1056   size_in_bytes = rw_ptrmap->size_in_bytes() + ro_ptrmap->size_in_bytes();
1057 
1058   if (mapped_heap_info != nullptr && mapped_heap_info->is_used()) {
1059     // Remove leading and trailing zeros
1060     assert(HeapShared::is_writing_mapping_mode(), "unexpected dumping mode");
1061     size_t removed_oop_leading_zeros = remove_bitmap_zeros(mapped_heap_info->oopmap());
1062     size_t removed_ptr_leading_zeros = remove_bitmap_zeros(mapped_heap_info->ptrmap());
1063     mapped_heap_info->set_oopmap_start_pos(removed_oop_leading_zeros);
1064     mapped_heap_info->set_ptrmap_start_pos(removed_ptr_leading_zeros);
1065 
1066     size_in_bytes += mapped_heap_info->oopmap()->size_in_bytes();
1067     size_in_bytes += mapped_heap_info->ptrmap()->size_in_bytes();
1068   } else if (streamed_heap_info != nullptr && streamed_heap_info->is_used()) {
1069     assert(HeapShared::is_writing_streaming_mode(), "unexpected dumping mode");
1070 
1071     size_in_bytes += streamed_heap_info->oopmap()->size_in_bytes();
1072   }
1073 
1074   // The bitmap region contains up to 4 parts:
1075   // rw_ptrmap:                  metaspace pointers inside the read-write region
1076   // ro_ptrmap:                  metaspace pointers inside the read-only region
1077   // *_heap_info->oopmap():      Java oop pointers in the heap region
1078   // mapped_heap_info->ptrmap(): metaspace pointers in the heap region
1079   char* buffer = NEW_C_HEAP_ARRAY(char, size_in_bytes, mtClassShared);
1080   size_t written = 0;
1081 
1082   region_at(AOTMetaspace::rw)->init_ptrmap(0, rw_ptrmap->size());
1083   written = write_bitmap(rw_ptrmap, buffer, written);
1084 
1085   region_at(AOTMetaspace::ro)->init_ptrmap(written, ro_ptrmap->size());
1086   written = write_bitmap(ro_ptrmap, buffer, written);
1087 
1088   if (mapped_heap_info != nullptr && mapped_heap_info->is_used()) {
1089     assert(HeapShared::is_writing_mapping_mode(), "unexpected dumping mode");
1090     FileMapRegion* r = region_at(AOTMetaspace::hp);
1091 
1092     r->init_oopmap(written, mapped_heap_info->oopmap()->size());
1093     written = write_bitmap(mapped_heap_info->oopmap(), buffer, written);
1094 
1095     r->init_ptrmap(written, mapped_heap_info->ptrmap()->size());
1096     written = write_bitmap(mapped_heap_info->ptrmap(), buffer, written);
1097   } else if (streamed_heap_info != nullptr && streamed_heap_info->is_used()) {
1098     assert(HeapShared::is_writing_streaming_mode(), "unexpected dumping mode");
1099     FileMapRegion* r = region_at(AOTMetaspace::hp);
1100 
1101     r->init_oopmap(written, streamed_heap_info->oopmap()->size());
1102     written = write_bitmap(streamed_heap_info->oopmap(), buffer, written);
1103   }
1104 
1105   write_region(AOTMetaspace::bm, (char*)buffer, size_in_bytes, /*read_only=*/true, /*allow_exec=*/false);
1106   return buffer;
1107 }
1108 
1109 #if INCLUDE_CDS_JAVA_HEAP
1110 size_t FileMapInfo::write_mapped_heap_region(AOTMappedHeapInfo* heap_info) {
1111   char* buffer_start = heap_info->buffer_start();
1112   size_t buffer_size = heap_info->buffer_byte_size();
1113   write_region(AOTMetaspace::hp, buffer_start, buffer_size, false, false);
1114   header()->set_mapped_heap_header(heap_info->create_header());
1115   return buffer_size;
1116 }
1117 
1118 size_t FileMapInfo::write_streamed_heap_region(AOTStreamedHeapInfo* heap_info) {
1119   char* buffer_start = heap_info->buffer_start();
1120   size_t buffer_size = heap_info->buffer_byte_size();
1121   write_region(AOTMetaspace::hp, buffer_start, buffer_size, true, false);
1122   header()->set_streamed_heap_header(heap_info->create_header());
1123   return buffer_size;
1124 }
1125 #endif // INCLUDE_CDS_JAVA_HEAP
1126 
1127 // Dump bytes to file -- at the current file position.
1128 
1129 void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) {
1130   assert(_file_open, "must be");
1131   if (!os::write(_fd, buffer, nbytes)) {
1132     // If the shared archive is corrupted, close it and remove it.
1133     close();
1134     remove(_full_path);
1135 
1136     if (CDSConfig::is_dumping_preimage_static_archive()) {
1137       AOTMetaspace::writing_error("Unable to write to AOT configuration file.");
1138     } else if (CDSConfig::new_aot_flags_used()) {
1139       AOTMetaspace::writing_error("Unable to write to AOT cache.");
1140     } else {
1141       AOTMetaspace::writing_error("Unable to write to shared archive.");
1142     }
1143   }
1144   _file_offset += nbytes;
1145 }
1146 
1147 bool FileMapInfo::is_file_position_aligned() const {
1148   return _file_offset == align_up(_file_offset,
1149                                   AOTMetaspace::core_region_alignment());
1150 }
1151 
1152 // Align file position to an allocation unit boundary.
1153 
1154 void FileMapInfo::align_file_position() {
1155   assert(_file_open, "must be");
1156   size_t new_file_offset = align_up(_file_offset,
1157                                     AOTMetaspace::core_region_alignment());
1158   if (new_file_offset != _file_offset) {
1159     _file_offset = new_file_offset;
1160     // Seek one byte back from the target and write a byte to insure
1161     // that the written file is the correct length.
1162     _file_offset -= 1;
1163     seek_to_position(_file_offset);
1164     char zero = 0;
1165     write_bytes(&zero, 1);
1166   }
1167 }
1168 
1169 
1170 // Dump bytes to file -- at the current file position.
1171 
1172 void FileMapInfo::write_bytes_aligned(const void* buffer, size_t nbytes) {
1173   align_file_position();
1174   write_bytes(buffer, nbytes);
1175   align_file_position();
1176 }
1177 
1178 // Close the shared archive file.  This does NOT unmap mapped regions.
1179 
1180 void FileMapInfo::close() {
1181   if (_file_open) {
1182     if (::close(_fd) < 0) {
1183       AOTMetaspace::unrecoverable_loading_error("Unable to close the shared archive file.");
1184     }
1185     _file_open = false;
1186     _fd = -1;
1187   }
1188 }
1189 
1190 /*
1191  * Same as os::map_memory() but also pretouches if AlwaysPreTouch is enabled.
1192  */
1193 static char* map_memory(int fd, const char* file_name, size_t file_offset,
1194                         char* addr, size_t bytes, bool read_only,
1195                         bool allow_exec, MemTag mem_tag) {
1196   char* mem = os::map_memory(fd, file_name, file_offset, addr, bytes,
1197                              mem_tag, AlwaysPreTouch ? false : read_only,
1198                              allow_exec);
1199   if (mem != nullptr && AlwaysPreTouch) {
1200     os::pretouch_memory(mem, mem + bytes);
1201   }
1202   return mem;
1203 }
1204 
1205 char* FileMapInfo::map_heap_region(FileMapRegion* r, char* addr, size_t bytes) {
1206   return ::map_memory(_fd,
1207                     _full_path,
1208                     r->file_offset(),
1209                     addr,
1210                     bytes,
1211                     r->read_only(),
1212                     r->allow_exec(),
1213                     mtJavaHeap);
1214 }
1215 
1216 // JVM/TI RedefineClasses() support:
1217 // Remap the shared readonly space to shared readwrite, private.
1218 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
1219   int idx = AOTMetaspace::ro;
1220   FileMapRegion* r = region_at(idx);
1221   if (!r->read_only()) {
1222     // the space is already readwrite so we are done
1223     return true;
1224   }
1225   size_t size = r->used_aligned();
1226   if (!open_for_read()) {
1227     return false;
1228   }
1229   char *addr = r->mapped_base();
1230   // This path should not be reached for Windows; see JDK-8222379.
1231   assert(WINDOWS_ONLY(false) NOT_WINDOWS(true), "Don't call on Windows");
1232   // Replace old mapping with new one that is writable.
1233   char *base = os::map_memory(_fd, _full_path, r->file_offset(),
1234                               addr, size, mtNone, false /* !read_only */,
1235                               r->allow_exec());
1236   close();
1237   // These have to be errors because the shared region is now unmapped.
1238   if (base == nullptr) {
1239     aot_log_error(aot)("Unable to remap shared readonly space (errno=%d).", errno);
1240     vm_exit(1);
1241   }
1242   if (base != addr) {
1243     aot_log_error(aot)("Unable to remap shared readonly space (errno=%d).", errno);
1244     vm_exit(1);
1245   }
1246   r->set_read_only(false);
1247   return true;
1248 }
1249 
1250 // Memory map a region in the address space.
1251 static const char* shared_region_name[] = { "ReadWrite", "ReadOnly", "Bitmap", "Heap", "Code" };
1252 
1253 MapArchiveResult FileMapInfo::map_regions(int regions[], int num_regions, char* mapped_base_address, ReservedSpace rs) {
1254   DEBUG_ONLY(FileMapRegion* last_region = nullptr);
1255   intx addr_delta = mapped_base_address - header()->requested_base_address();
1256 
1257   // Make sure we don't attempt to use header()->mapped_base_address() unless
1258   // it's been successfully mapped.
1259   DEBUG_ONLY(header()->set_mapped_base_address((char*)(uintptr_t)0xdeadbeef);)
1260 
1261   for (int i = 0; i < num_regions; i++) {
1262     int idx = regions[i];
1263     MapArchiveResult result = map_region(idx, addr_delta, mapped_base_address, rs);
1264     if (result != MAP_ARCHIVE_SUCCESS) {
1265       return result;
1266     }
1267     FileMapRegion* r = region_at(idx);
1268     DEBUG_ONLY(if (last_region != nullptr) {
1269         // Ensure that the OS won't be able to allocate new memory spaces between any mapped
1270         // regions, or else it would mess up the simple comparison in MetaspaceObj::in_aot_cache().
1271         assert(r->mapped_base() == last_region->mapped_end(), "must have no gaps");
1272       }
1273       last_region = r;)
1274     aot_log_info(aot)("Mapped %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)", is_static() ? "static " : "dynamic",
1275                   idx, p2i(r->mapped_base()), p2i(r->mapped_end()),
1276                   shared_region_name[idx]);
1277 
1278   }
1279 
1280   header()->set_mapped_base_address(header()->requested_base_address() + addr_delta);
1281   if (addr_delta != 0 && !relocate_pointers_in_core_regions(addr_delta)) {
1282     return MAP_ARCHIVE_OTHER_FAILURE;
1283   }
1284 
1285   return MAP_ARCHIVE_SUCCESS;
1286 }
1287 
1288 bool FileMapInfo::read_region(int i, char* base, size_t size, bool do_commit) {
1289   FileMapRegion* r = region_at(i);
1290   if (do_commit) {
1291     aot_log_info(aot)("Commit %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)%s",
1292                   is_static() ? "static " : "dynamic", i, p2i(base), p2i(base + size),
1293                   shared_region_name[i], r->allow_exec() ? " exec" : "");
1294     if (!os::commit_memory(base, size, r->allow_exec())) {
1295       aot_log_error(aot)("Failed to commit %s region #%d (%s)", is_static() ? "static " : "dynamic",
1296                      i, shared_region_name[i]);
1297       return false;
1298     }
1299   }
1300   if (os::lseek(_fd, (long)r->file_offset(), SEEK_SET) != (int)r->file_offset() ||
1301       read_bytes(base, size) != size) {
1302     return false;
1303   }
1304 
1305   if (VerifySharedSpaces && !r->check_region_crc(base)) {
1306     return false;
1307   }
1308 
1309   r->set_mapped_from_file(false);
1310   r->set_mapped_base(base);
1311 
1312   return true;
1313 }
1314 
1315 MapArchiveResult FileMapInfo::map_region(int i, intx addr_delta, char* mapped_base_address, ReservedSpace rs) {
1316   assert(!HeapShared::is_heap_region(i), "sanity");
1317   FileMapRegion* r = region_at(i);
1318   size_t size = r->used_aligned();
1319   char *requested_addr = mapped_base_address + r->mapping_offset();
1320   assert(!is_mapped(), "must be not mapped yet");
1321   assert(requested_addr != nullptr, "must be specified");
1322 
1323   r->set_mapped_from_file(false);
1324   r->set_in_reserved_space(false);
1325 
1326   if (AOTMetaspace::use_windows_memory_mapping()) {
1327     // Windows cannot remap read-only shared memory to read-write when required for
1328     // RedefineClasses, which is also used by JFR.  Always map windows regions as RW.
1329     r->set_read_only(false);
1330   } else if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space() ||
1331              Arguments::has_jfr_option()) {
1332     // If a tool agent is in use (debugging enabled), or JFR, we must map the address space RW
1333     r->set_read_only(false);
1334   } else if (addr_delta != 0) {
1335     r->set_read_only(false); // Need to patch the pointers
1336   }
1337 
1338   if (AOTMetaspace::use_windows_memory_mapping() && rs.is_reserved()) {
1339     // This is the second time we try to map the archive(s). We have already created a ReservedSpace
1340     // that covers all the FileMapRegions to ensure all regions can be mapped. However, Windows
1341     // can't mmap into a ReservedSpace, so we just ::read() the data. We're going to patch all the
1342     // regions anyway, so there's no benefit for mmap anyway.
1343     if (!read_region(i, requested_addr, size, /* do_commit = */ true)) {
1344       AOTMetaspace::report_loading_error("Failed to read %s shared space into reserved space at " INTPTR_FORMAT,
1345                                             shared_region_name[i], p2i(requested_addr));
1346       return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
1347     } else {
1348       assert(r->mapped_base() != nullptr, "must be initialized");
1349     }
1350   } else {
1351     // Note that this may either be a "fresh" mapping into unreserved address
1352     // space (Windows, first mapping attempt), or a mapping into pre-reserved
1353     // space (Posix). See also comment in AOTMetaspace::map_archives().
1354     char* base = map_memory(_fd, _full_path, r->file_offset(),
1355                             requested_addr, size, r->read_only(),
1356                             r->allow_exec(), mtClassShared);
1357     if (base != requested_addr) {
1358       AOTMetaspace::report_loading_error("Unable to map %s shared space at " INTPTR_FORMAT,
1359                                             shared_region_name[i], p2i(requested_addr));
1360       _memory_mapping_failed = true;
1361       return MAP_ARCHIVE_MMAP_FAILURE;
1362     }
1363 
1364     if (VerifySharedSpaces && !r->check_region_crc(requested_addr)) {
1365       return MAP_ARCHIVE_OTHER_FAILURE;
1366     }
1367 
1368     r->set_mapped_from_file(true);
1369     r->set_mapped_base(requested_addr);
1370   }
1371 
1372   if (rs.is_reserved()) {
1373     char* mapped_base = r->mapped_base();
1374     assert(rs.base() <= mapped_base && mapped_base + size <= rs.end(),
1375            PTR_FORMAT " <= " PTR_FORMAT " < " PTR_FORMAT " <= " PTR_FORMAT,
1376            p2i(rs.base()), p2i(mapped_base), p2i(mapped_base + size), p2i(rs.end()));
1377     r->set_in_reserved_space(rs.is_reserved());
1378   }
1379   return MAP_ARCHIVE_SUCCESS;
1380 }
1381 
1382 // The return value is the location of the archive relocation bitmap.
1383 char* FileMapInfo::map_auxiliary_region(int region_index, bool read_only) {
1384   FileMapRegion* r = region_at(region_index);
1385   if (r->mapped_base() != nullptr) {
1386     return r->mapped_base();
1387   }
1388   const char* region_name = shared_region_name[region_index];
1389   bool allow_exec = false;
1390   char* requested_addr = nullptr; // allow OS to pick any location
1391   char* mapped_base = map_memory(_fd, _full_path, r->file_offset(),
1392                                  requested_addr, r->used_aligned(), read_only, allow_exec, mtClassShared);
1393   if (mapped_base == nullptr) {
1394     AOTMetaspace::report_loading_error("failed to map %d region", region_index);
1395     return nullptr;
1396   }
1397 
1398   if (VerifySharedSpaces && !r->check_region_crc(mapped_base)) {
1399     aot_log_error(aot)("region %d CRC error", region_index);
1400     os::unmap_memory(mapped_base, r->used_aligned());
1401     return nullptr;
1402   }
1403 
1404   r->set_mapped_from_file(true);
1405   r->set_mapped_base(mapped_base);
1406   aot_log_info(aot)("Mapped %s region #%d at base %zu top %zu (%s)",
1407                 is_static() ? "static " : "dynamic",
1408                 region_index, p2i(r->mapped_base()), p2i(r->mapped_end()),
1409                 region_name);
1410   return mapped_base;
1411 }
1412 
1413 char* FileMapInfo::map_bitmap_region() {
1414   return map_auxiliary_region(AOTMetaspace::bm, false);
1415 }
1416 
1417 bool FileMapInfo::map_aot_code_region(ReservedSpace rs) {
1418   FileMapRegion* r = region_at(AOTMetaspace::ac);
1419   assert(r->used() > 0 && r->used_aligned() == rs.size(), "must be");
1420 
1421   if (UseCompressedOops) {
1422     precond(header()->compatible_oop_compression() == AOTCompatibleOopCompression);
1423   }
1424 
1425   char* requested_base = rs.base();
1426   assert(requested_base != nullptr, "should be inside code cache");
1427 
1428   char* mapped_base;
1429   if (AOTMetaspace::use_windows_memory_mapping()) {
1430     if (!read_region(AOTMetaspace::ac, requested_base, r->used_aligned(), /* do_commit = */ true)) {
1431       AOTMetaspace::report_loading_error("Failed to read aot code shared space into reserved space at " INTPTR_FORMAT,
1432                                             p2i(requested_base));
1433       return false;
1434     }
1435     mapped_base = requested_base;
1436   } else {
1437     // We do not execute in-place in the AOT code region.
1438     // AOT code is copied to the CodeCache for execution.
1439     bool read_only = false, allow_exec = false;
1440     mapped_base = map_memory(_fd, _full_path, r->file_offset(),
1441                              requested_base, r->used_aligned(), read_only, allow_exec, mtClassShared);
1442   }
1443   if (mapped_base == nullptr) {
1444     AOTMetaspace::report_loading_error("failed to map aot code region");
1445     return false;
1446   } else {
1447     assert(mapped_base == requested_base, "must be");
1448 
1449     if (VerifySharedSpaces && !r->check_region_crc(mapped_base)) {
1450       aot_log_error(aot)("region %d CRC error", AOTMetaspace::ac);
1451       os::unmap_memory(mapped_base, r->used_aligned());
1452       return false;
1453     }
1454 
1455     r->set_mapped_from_file(true);
1456     r->set_mapped_base(mapped_base);
1457     aot_log_info(aot)("Mapped static  region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
1458                   AOTMetaspace::ac, p2i(r->mapped_base()), p2i(r->mapped_end()),
1459                   shared_region_name[AOTMetaspace::ac]);
1460     return true;
1461   }
1462 }
1463 
1464 class SharedDataRelocationTask : public ArchiveWorkerTask {
1465 private:
1466   BitMapView* const _rw_bm;
1467   BitMapView* const _ro_bm;
1468   SharedDataRelocator* const _rw_reloc;
1469   SharedDataRelocator* const _ro_reloc;
1470 
1471 public:
1472   SharedDataRelocationTask(BitMapView* rw_bm, BitMapView* ro_bm, SharedDataRelocator* rw_reloc, SharedDataRelocator* ro_reloc) :
1473                            ArchiveWorkerTask("Shared Data Relocation"),
1474                            _rw_bm(rw_bm), _ro_bm(ro_bm), _rw_reloc(rw_reloc), _ro_reloc(ro_reloc) {}
1475 
1476   void work(int chunk, int max_chunks) override {
1477     work_on(chunk, max_chunks, _rw_bm, _rw_reloc);
1478     work_on(chunk, max_chunks, _ro_bm, _ro_reloc);
1479   }
1480 
1481   void work_on(int chunk, int max_chunks, BitMapView* bm, SharedDataRelocator* reloc) {
1482     BitMap::idx_t size  = bm->size();
1483     BitMap::idx_t start = MIN2(size, size * chunk / max_chunks);
1484     BitMap::idx_t end   = MIN2(size, size * (chunk + 1) / max_chunks);
1485     assert(end > start, "Sanity: no empty slices");
1486     bm->iterate(reloc, start, end);
1487   }
1488 };
1489 
1490 // This is called when we cannot map the archive at the requested[ base address (usually 0x800000000).
1491 // We relocate all pointers in the 2 core regions (ro, rw).
1492 bool FileMapInfo::relocate_pointers_in_core_regions(intx addr_delta) {
1493   aot_log_debug(aot, reloc)("runtime archive relocation start");
1494   char* bitmap_base = map_bitmap_region();
1495 
1496   if (bitmap_base == nullptr) {
1497     return false; // OOM, or CRC check failure
1498   } else {
1499     BitMapView rw_ptrmap = ptrmap_view(AOTMetaspace::rw);
1500     BitMapView ro_ptrmap = ptrmap_view(AOTMetaspace::ro);
1501 
1502     FileMapRegion* rw_region = first_core_region();
1503     FileMapRegion* ro_region = last_core_region();
1504 
1505     // Patch all pointers inside the RW region
1506     address rw_patch_base = (address)rw_region->mapped_base();
1507     address rw_patch_end  = (address)rw_region->mapped_end();
1508 
1509     // Patch all pointers inside the RO region
1510     address ro_patch_base = (address)ro_region->mapped_base();
1511     address ro_patch_end  = (address)ro_region->mapped_end();
1512 
1513     // the current value of the pointers to be patched must be within this
1514     // range (i.e., must be between the requested base address and the address of the current archive).
1515     // Note: top archive may point to objects in the base archive, but not the other way around.
1516     address valid_old_base = (address)header()->requested_base_address();
1517     address valid_old_end  = valid_old_base + mapping_end_offset();
1518 
1519     // after patching, the pointers must point inside this range
1520     // (the requested location of the archive, as mapped at runtime).
1521     address valid_new_base = (address)header()->mapped_base_address();
1522     address valid_new_end  = (address)mapped_end();
1523 
1524     SharedDataRelocator rw_patcher((address*)rw_patch_base + header()->rw_ptrmap_start_pos(), (address*)rw_patch_end, valid_old_base, valid_old_end,
1525                                 valid_new_base, valid_new_end, addr_delta);
1526     SharedDataRelocator ro_patcher((address*)ro_patch_base + header()->ro_ptrmap_start_pos(), (address*)ro_patch_end, valid_old_base, valid_old_end,
1527                                 valid_new_base, valid_new_end, addr_delta);
1528 
1529     if (AOTCacheParallelRelocation) {
1530       ArchiveWorkers workers;
1531       SharedDataRelocationTask task(&rw_ptrmap, &ro_ptrmap, &rw_patcher, &ro_patcher);
1532       workers.run_task(&task);
1533     } else {
1534       rw_ptrmap.iterate(&rw_patcher);
1535       ro_ptrmap.iterate(&ro_patcher);
1536     }
1537 
1538     // The AOTMetaspace::bm region will be unmapped in AOTMetaspace::initialize_shared_spaces().
1539 
1540     aot_log_debug(aot, reloc)("runtime archive relocation done");
1541     return true;
1542   }
1543 }
1544 
1545 size_t FileMapInfo::read_bytes(void* buffer, size_t count) {
1546   assert(_file_open, "Archive file is not open");
1547   size_t n = ::read(_fd, buffer, (unsigned int)count);
1548   if (n != count) {
1549     // Close the file if there's a problem reading it.
1550     close();
1551     return 0;
1552   }
1553   _file_offset += count;
1554   return count;
1555 }
1556 
1557 // Get the total size in bytes of all mapped read only region
1558 size_t FileMapInfo::readonly_total() {
1559   size_t total = 0;
1560   if (current_info() != nullptr && current_info()->is_mapped()) {
1561     FileMapRegion* r = FileMapInfo::current_info()->region_at(AOTMetaspace::ro);
1562     if (r->read_only()) total += r->used();
1563   }
1564   if (dynamic_info() != nullptr && current_info()->is_mapped()) {
1565     FileMapRegion* r = FileMapInfo::dynamic_info()->region_at(AOTMetaspace::ro);
1566     if (r->read_only()) total += r->used();
1567   }
1568   return total;
1569 }
1570 
1571 #if INCLUDE_CDS_JAVA_HEAP
1572 
1573 bool FileMapInfo::has_heap_region() {
1574   return (region_at(AOTMetaspace::hp)->used() > 0);
1575 }
1576 
1577 static void on_heap_region_loading_error() {
1578   if (CDSConfig::is_using_aot_linked_classes()) {
1579     // It's too late to recover -- we have already committed to use the archived metaspace objects, but
1580     // the archived heap objects cannot be loaded, so we don't have the archived FMG to guarantee that
1581     // all AOT-linked classes are visible.
1582     //
1583     // We get here because the heap is too small. The app will fail anyway. So let's quit.
1584     aot_log_error(aot)("%s has aot-linked classes but the archived "
1585                        "heap objects cannot be loaded. Try increasing your heap size.",
1586                        CDSConfig::type_of_archive_being_loaded());
1587     AOTMetaspace::unrecoverable_loading_error();
1588   }
1589   CDSConfig::stop_using_full_module_graph();
1590 }
1591 
1592 void FileMapInfo::stream_heap_region() {
1593   assert(object_streaming_mode(), "This should only be done for the streaming approach");
1594 
1595   if (map_auxiliary_region(AOTMetaspace::hp, /*readonly=*/true) != nullptr) {
1596     HeapShared::initialize_streaming();
1597   } else {
1598     on_heap_region_loading_error();
1599   }
1600 }
1601 
1602 void FileMapInfo::map_or_load_heap_region() {
1603   assert(!object_streaming_mode(), "This should only be done for the mapping approach");
1604   bool success = false;
1605 
1606   if (AOTMappedHeapLoader::can_map()) {
1607     success = AOTMappedHeapLoader::map_heap_region(this);
1608   } else if (AOTMappedHeapLoader::can_load()) {
1609     success = AOTMappedHeapLoader::load_heap_region(this);
1610   }
1611 
1612   if (!success) {
1613     on_heap_region_loading_error();
1614   }
1615 }
1616 
1617 bool FileMapInfo::can_use_heap_region() {
1618   if (!has_heap_region()) {
1619     return false;
1620   }
1621 
1622   if (!object_streaming_mode() && !AOTMappedHeapLoader::can_use()) {
1623     // Currently this happens only when using ZGC with an AOT cache generated with -XX:-AOTStreamableObjects
1624     AOTMetaspace::report_loading_error("CDS heap data cannot be used by the selected GC. "
1625                                        "Please choose a different GC or rebuild AOT cache "
1626                                        "with -XX:+AOTStreamableObjects");
1627     return false;
1628   }
1629 
1630   if (CDSConfig::is_using_aot_linked_classes()) {
1631     assert(!JvmtiExport::should_post_class_file_load_hook(), "already checked");
1632     assert(CDSConfig::is_using_full_module_graph(), "already checked");
1633   } else {
1634     if (JvmtiExport::should_post_class_file_load_hook()) {
1635       AOTMetaspace::report_loading_error("CDS heap data is disabled because JVMTI ClassFileLoadHook is in use.");
1636       return false;
1637     }
1638     if (!CDSConfig::is_using_full_module_graph()) {
1639       if (CDSConfig::is_dumping_final_static_archive()) {
1640         // We are loading the preimage static archive, which has no KlassSubGraphs.
1641         // See CDSConfig::is_dumping_klass_subgraphs()
1642       } else {
1643         AOTMetaspace::report_loading_error("CDS heap data is disabled because archived full module graph is not used.");
1644         return false;
1645       }
1646     }
1647   }
1648 
1649   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
1650     ShouldNotReachHere(); // CDS should have been disabled.
1651     // The archived objects are mapped at JVM start-up, but we don't know if
1652     // j.l.String or j.l.Class might be replaced by the ClassFileLoadHook,
1653     // which would make the archived String or mirror objects invalid. Let's be safe and not
1654     // use the archived objects. These 2 classes are loaded during the JVMTI "early" stage.
1655     //
1656     // If JvmtiExport::has_early_class_hook_env() is false, the classes of some objects
1657     // in the archived subgraphs may be replaced by the ClassFileLoadHook. But that's OK
1658     // because we won't install an archived object subgraph if the klass of any of the
1659     // referenced objects are replaced. See HeapShared::initialize_from_archived_subgraph().
1660   }
1661 
1662   // We pre-compute narrow Klass IDs with the runtime mapping start intended to be the base, and a shift of
1663   // HeapShared::precomputed_narrow_klass_shift. We enforce this encoding at runtime (see
1664   // CompressedKlassPointers::initialize_for_given_encoding()). Therefore, the following assertions must
1665   // hold:
1666   address archive_narrow_klass_base = (address)header()->mapped_base_address();
1667   const int archive_narrow_klass_pointer_bits = header()->narrow_klass_pointer_bits();
1668   const int archive_narrow_klass_shift = header()->narrow_klass_shift();
1669 
1670   aot_log_info(aot)("CDS archive was created with max heap size = %zuM, and the following configuration:",
1671                 max_heap_size()/M);
1672 
1673   aot_log_info(aot)("    narrow_klass_base at mapping start address, narrow_klass_pointer_bits = %d, narrow_klass_shift = %d",
1674                 archive_narrow_klass_pointer_bits, archive_narrow_klass_shift);
1675   if (UseCompressedOops) {
1676     aot_log_info(aot)("    narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
1677                       narrow_oop_mode(), p2i(narrow_oop_base()), narrow_oop_shift());
1678     aot_log_info(aot)("    AOTCompatibleOopCompression = %s", header()->compatible_oop_compression() ? "true" : "false");
1679   }
1680   aot_log_info(aot)("The current max heap size = %zuM, G1HeapRegion::GrainBytes = %zu",
1681                 MaxHeapSize/M, G1HeapRegion::GrainBytes);
1682   aot_log_info(aot)("    narrow_klass_base = " PTR_FORMAT ", arrow_klass_pointer_bits = %d, narrow_klass_shift = %d",
1683                 p2i(CompressedKlassPointers::base()), CompressedKlassPointers::narrow_klass_pointer_bits(), CompressedKlassPointers::shift());
1684   if (UseCompressedOops) {
1685     aot_log_info(aot)("    narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
1686                       CompressedOops::mode(), p2i(CompressedOops::base()), CompressedOops::shift());
1687     aot_log_info(aot)("    AOTCompatibleOopCompression = %s", AOTCompatibleOopCompression ? "true" : "false");
1688   }
1689   if (!object_streaming_mode()) {
1690     aot_log_info(aot)("    heap range = [" PTR_FORMAT " - "  PTR_FORMAT "]",
1691                       UseCompressedOops ? p2i(CompressedOops::begin()) :
1692                       UseG1GC ? p2i((address)G1CollectedHeap::heap()->reserved().start()) : 0L,
1693                       UseCompressedOops ? p2i(CompressedOops::end()) :
1694                       UseG1GC ? p2i((address)G1CollectedHeap::heap()->reserved().end()) : 0L);
1695   }
1696 
1697   int err = 0;
1698   if ( archive_narrow_klass_base != CompressedKlassPointers::base() ||
1699        (err = 1, archive_narrow_klass_pointer_bits != CompressedKlassPointers::narrow_klass_pointer_bits()) ||
1700        (err = 2, archive_narrow_klass_shift != CompressedKlassPointers::shift()) ) {
1701     stringStream ss;
1702     switch (err) {
1703     case 0:
1704       ss.print("Unexpected encoding base encountered (" PTR_FORMAT ", expected " PTR_FORMAT ")",
1705                p2i(CompressedKlassPointers::base()), p2i(archive_narrow_klass_base));
1706       break;
1707     case 1:
1708       ss.print("Unexpected narrow Klass bit length encountered (%d, expected %d)",
1709                CompressedKlassPointers::narrow_klass_pointer_bits(), archive_narrow_klass_pointer_bits);
1710       break;
1711     case 2:
1712       ss.print("Unexpected narrow Klass shift encountered (%d, expected %d)",
1713                CompressedKlassPointers::shift(), archive_narrow_klass_shift);
1714       break;
1715     default:
1716       ShouldNotReachHere();
1717     };
1718     if (CDSConfig::new_aot_flags_used()) {
1719       LogTarget(Info, aot) lt;
1720       if (lt.is_enabled()) {
1721         LogStream ls(lt);
1722         ls.print_raw(ss.base());
1723         header()->print(&ls);
1724       }
1725     } else {
1726       LogTarget(Info, cds) lt;
1727       if (lt.is_enabled()) {
1728         LogStream ls(lt);
1729         ls.print_raw(ss.base());
1730         header()->print(&ls);
1731       }
1732     }
1733     assert(false, "%s", ss.base());
1734   }
1735 
1736   return true;
1737 }
1738 
1739 #endif // INCLUDE_CDS_JAVA_HEAP
1740 
1741 // Unmap a memory region in the address space.
1742 
1743 void FileMapInfo::unmap_regions(int regions[], int num_regions) {
1744   for (int r = 0; r < num_regions; r++) {
1745     int idx = regions[r];
1746     unmap_region(idx);
1747   }
1748 }
1749 
1750 void FileMapInfo::unmap_region(int i) {
1751   FileMapRegion* r = region_at(i);
1752   char* mapped_base = r->mapped_base();
1753   size_t size = r->used_aligned();
1754 
1755   if (mapped_base != nullptr) {
1756     if (size > 0 && r->mapped_from_file()) {
1757       aot_log_info(aot)("Unmapping region #%d at base " INTPTR_FORMAT " (%s)", i, p2i(mapped_base),
1758                     shared_region_name[i]);
1759       if (r->in_reserved_space()) {
1760         // This region was mapped inside a ReservedSpace. Its memory will be freed when the ReservedSpace
1761         // is released. Zero it so that we don't accidentally read its content.
1762         aot_log_info(aot)("Region #%d (%s) is in a reserved space, it will be freed when the space is released", i, shared_region_name[i]);
1763       } else {
1764         os::unmap_memory(mapped_base, size);
1765       }
1766     }
1767     r->set_mapped_base(nullptr);
1768   }
1769 }
1770 
1771 void FileMapInfo::assert_mark(bool check) {
1772   if (!check) {
1773     AOTMetaspace::unrecoverable_loading_error("Mark mismatch while restoring from shared file.");
1774   }
1775 }
1776 
1777 FileMapInfo* FileMapInfo::_current_info = nullptr;
1778 FileMapInfo* FileMapInfo::_dynamic_archive_info = nullptr;
1779 bool FileMapInfo::_memory_mapping_failed = false;
1780 
1781 // Open the shared archive file, read and validate the header
1782 // information (version, boot classpath, etc.). If initialization
1783 // fails, shared spaces are disabled and the file is closed.
1784 //
1785 // Validation of the archive is done in two steps:
1786 //
1787 // [1] validate_header() - done here.
1788 // [2] validate_shared_path_table - this is done later, because the table is in the RO
1789 //     region of the archive, which is not mapped yet.
1790 bool FileMapInfo::open_as_input() {
1791   assert(CDSConfig::is_using_archive(), "UseSharedSpaces expected.");
1792   assert(Arguments::has_jimage(), "The shared archive file cannot be used with an exploded module build.");
1793 
1794   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
1795     // CDS assumes that no classes resolved in vmClasses::resolve_all()
1796     // are replaced at runtime by JVMTI ClassFileLoadHook. All of those classes are resolved
1797     // during the JVMTI "early" stage, so we can still use CDS if
1798     // JvmtiExport::has_early_class_hook_env() is false.
1799     AOTMetaspace::report_loading_error("CDS is disabled because early JVMTI ClassFileLoadHook is in use.");
1800     return false;
1801   }
1802 
1803   if (!open_for_read() || !init_from_file(_fd) || !validate_header()) {
1804     if (_is_static) {
1805       AOTMetaspace::report_loading_error("Loading static archive failed.");
1806       return false;
1807     } else {
1808       AOTMetaspace::report_loading_error("Loading dynamic archive failed.");
1809       if (AutoCreateSharedArchive) {
1810         CDSConfig::enable_dumping_dynamic_archive(_full_path);
1811       }
1812       return false;
1813     }
1814   }
1815 
1816   return true;
1817 }
1818 
1819 bool FileMapInfo::validate_aot_class_linking() {
1820   // These checks need to be done after FileMapInfo::initialize(), which gets called before Universe::heap()
1821   // is available.
1822   if (header()->has_aot_linked_classes()) {
1823     const char* archive_type = CDSConfig::type_of_archive_being_loaded();
1824     CDSConfig::set_has_aot_linked_classes(true);
1825     if (JvmtiExport::should_post_class_file_load_hook()) {
1826       aot_log_error(aot)("%s has aot-linked classes. It cannot be used when JVMTI ClassFileLoadHook is in use.",
1827                      archive_type);
1828       return false;
1829     }
1830     if (JvmtiExport::has_early_vmstart_env()) {
1831       aot_log_error(aot)("%s has aot-linked classes. It cannot be used when JVMTI early vm start is in use.",
1832                      archive_type);
1833       return false;
1834     }
1835     if (!CDSConfig::is_using_full_module_graph()) {
1836       aot_log_error(aot)("%s has aot-linked classes. It cannot be used when archived full module graph is not used.",
1837                      archive_type);
1838       return false;
1839     }
1840 
1841     const char* prop = Arguments::get_property("java.security.manager");
1842     if (prop != nullptr && strcmp(prop, "disallow") != 0) {
1843       aot_log_error(aot)("%s has aot-linked classes. It cannot be used with -Djava.security.manager=%s.",
1844                      archive_type, prop);
1845       return false;
1846     }
1847 
1848 #if INCLUDE_JVMTI
1849     if (Arguments::has_jdwp_agent()) {
1850       aot_log_error(aot)("%s has aot-linked classes. It cannot be used with JDWP agent", archive_type);
1851       return false;
1852     }
1853 #endif
1854   }
1855 
1856   return true;
1857 }
1858 
1859 // The 2 core spaces are RW->RO
1860 FileMapRegion* FileMapInfo::first_core_region() const {
1861   return region_at(AOTMetaspace::rw);
1862 }
1863 
1864 FileMapRegion* FileMapInfo::last_core_region() const {
1865   return region_at(AOTMetaspace::ro);
1866 }
1867 
1868 void FileMapInfo::print(outputStream* st) const {
1869   header()->print(st);
1870   if (!is_static()) {
1871     dynamic_header()->print(st);
1872   }
1873 }
1874 
1875 int FileMapHeader::compute_crc() {
1876   char* start = (char*)this;
1877   // start computing from the field after _header_size to end of base archive name.
1878   char* buf = (char*)&(_generic_header._header_size) + sizeof(_generic_header._header_size);
1879   size_t sz = header_size() - (buf - start);
1880   int crc = ClassLoader::crc32(0, buf, (jint)sz);
1881   return crc;
1882 }
1883 
1884 // This function should only be called during run time with UseSharedSpaces enabled.
1885 bool FileMapHeader::validate() {
1886   const char* file_type = CDSConfig::type_of_archive_being_loaded();
1887   if (_obj_alignment != ObjectAlignmentInBytes) {
1888     AOTMetaspace::report_loading_error("The %s's ObjectAlignmentInBytes of %d"
1889                                           " does not equal the current ObjectAlignmentInBytes of %d.",
1890                                           file_type, _obj_alignment, ObjectAlignmentInBytes);
1891     return false;
1892   }
1893   if (_compact_strings != CompactStrings) {
1894     AOTMetaspace::report_loading_error("The %s's CompactStrings setting (%s)"
1895                                           " does not equal the current CompactStrings setting (%s).", file_type,
1896                                           _compact_strings ? "enabled" : "disabled",
1897                                           CompactStrings   ? "enabled" : "disabled");
1898     return false;
1899   }
1900   if (TrainingData::have_data()) {
1901     if (_type_profile_level != TypeProfileLevel) {
1902       AOTMetaspace::report_loading_error("The %s's TypeProfileLevel setting (%d)"
1903                                             " does not equal the current TypeProfileLevel setting (%d).", file_type,
1904                                             _type_profile_level, TypeProfileLevel);
1905       return false;
1906     }
1907     if (_type_profile_args_limit != TypeProfileArgsLimit) {
1908       AOTMetaspace::report_loading_error("The %s's TypeProfileArgsLimit setting (%d)"
1909                                             " does not equal the current TypeProfileArgsLimit setting (%d).", file_type,
1910                                             _type_profile_args_limit, TypeProfileArgsLimit);
1911       return false;
1912     }
1913     if (_type_profile_parms_limit != TypeProfileParmsLimit) {
1914       AOTMetaspace::report_loading_error("The %s's TypeProfileParamsLimit setting (%d)"
1915                                             " does not equal the current TypeProfileParamsLimit setting (%d).", file_type,
1916                                             _type_profile_args_limit, TypeProfileArgsLimit);
1917       return false;
1918 
1919     }
1920     if (_type_profile_width != TypeProfileWidth) {
1921       AOTMetaspace::report_loading_error("The %s's TypeProfileWidth setting (%d)"
1922                                             " does not equal the current TypeProfileWidth setting (%d).", file_type,
1923                                             (int)_type_profile_width, (int)TypeProfileWidth);
1924       return false;
1925 
1926     }
1927     if (_bci_profile_width != BciProfileWidth) {
1928       AOTMetaspace::report_loading_error("The %s's BciProfileWidth setting (%d)"
1929                                             " does not equal the current BciProfileWidth setting (%d).", file_type,
1930                                             (int)_bci_profile_width, (int)BciProfileWidth);
1931       return false;
1932     }
1933     if (_type_profile_casts != TypeProfileCasts) {
1934       AOTMetaspace::report_loading_error("The %s's TypeProfileCasts setting (%s)"
1935                                             " does not equal the current TypeProfileCasts setting (%s).", file_type,
1936                                             _type_profile_casts ? "enabled" : "disabled",
1937                                             TypeProfileCasts    ? "enabled" : "disabled");
1938 
1939       return false;
1940 
1941     }
1942     if (_profile_traps != ProfileTraps) {
1943       AOTMetaspace::report_loading_error("The %s's ProfileTraps setting (%s)"
1944                                             " does not equal the current ProfileTraps setting (%s).", file_type,
1945                                             _profile_traps ? "enabled" : "disabled",
1946                                             ProfileTraps   ? "enabled" : "disabled");
1947 
1948       return false;
1949     }
1950     if (_spec_trap_limit_extra_entries != SpecTrapLimitExtraEntries) {
1951       AOTMetaspace::report_loading_error("The %s's SpecTrapLimitExtraEntries setting (%d)"
1952                                             " does not equal the current SpecTrapLimitExtraEntries setting (%d).", file_type,
1953                                             _spec_trap_limit_extra_entries, SpecTrapLimitExtraEntries);
1954       return false;
1955 
1956     }
1957   }
1958 
1959   // This must be done after header validation because it might change the
1960   // header data
1961   const char* prop = Arguments::get_property("java.system.class.loader");
1962   if (prop != nullptr) {
1963     if (has_aot_linked_classes()) {
1964       AOTMetaspace::report_loading_error("%s has aot-linked classes. It cannot be used when the "
1965                                             "java.system.class.loader property is specified.",
1966                                             CDSConfig::type_of_archive_being_loaded());
1967       return false;
1968     }
1969     aot_log_warning(aot)("Archived non-system classes are disabled because the "
1970             "java.system.class.loader property is specified (value = \"%s\"). "
1971             "To use archived non-system classes, this property must not be set", prop);
1972     _has_platform_or_app_classes = false;
1973   }
1974 
1975 
1976   if (!_verify_local && BytecodeVerificationLocal) {
1977     //  we cannot load boot classes, so there's no point of using the CDS archive
1978     AOTMetaspace::report_loading_error("The %s's BytecodeVerificationLocal setting (%s)"
1979                                           " does not equal the current BytecodeVerificationLocal setting (%s).", file_type,
1980                                           _verify_local ? "enabled" : "disabled",
1981                                           BytecodeVerificationLocal ? "enabled" : "disabled");
1982     return false;
1983   }
1984 
1985   // For backwards compatibility, we don't check the BytecodeVerificationRemote setting
1986   // if the archive only contains system classes.
1987   if (_has_platform_or_app_classes
1988       && !_verify_remote // we didn't verify the archived platform/app classes
1989       && BytecodeVerificationRemote) { // but we want to verify all loaded platform/app classes
1990     aot_log_info(aot)("The %s was created with less restrictive "
1991                                "verification setting than the current setting.", file_type);
1992     // Pretend that we didn't have any archived platform/app classes, so they won't be loaded
1993     // by SystemDictionaryShared.
1994     _has_platform_or_app_classes = false;
1995   }
1996 
1997   aot_log_info(aot)("The %s was created with UseCompressedOops = %d, UseCompactObjectHeaders = %d",
1998                           file_type, compressed_oops(), compact_headers());
1999   if (compressed_oops() != UseCompressedOops) {
2000     aot_log_warning(aot)("Unable to use %s.\nThe saved state of UseCompressedOops (%d) is "
2001                                "different from runtime (%d), CDS will be disabled.", file_type,
2002                                compressed_oops(), UseCompressedOops);
2003     return false;
2004   }
2005 
2006   if (is_static()) {
2007     const char* err = nullptr;
2008     if (Arguments::is_valhalla_enabled()) {
2009       if (!_has_valhalla_patched_classes) {
2010         err = "not created";
2011       }
2012     } else {
2013       if (_has_valhalla_patched_classes) {
2014         err = "created";
2015       }
2016     }
2017     if (err != nullptr) {
2018       log_warning(cds)("This archive was %s with --enable-preview. It is "
2019                          "incompatible with the current JVM setting", err);
2020       return false;
2021     }
2022   }
2023 
2024   if (compact_headers() != UseCompactObjectHeaders) {
2025     aot_log_warning(aot)("Unable to use %s.\nThe %s's UseCompactObjectHeaders setting (%s)"
2026                      " does not equal the current UseCompactObjectHeaders setting (%s).", file_type, file_type,
2027                      _compact_headers          ? "enabled" : "disabled",
2028                      UseCompactObjectHeaders   ? "enabled" : "disabled");
2029     return false;
2030   }
2031 
2032   if (!_use_optimized_module_handling && !CDSConfig::is_dumping_final_static_archive()) {
2033     CDSConfig::stop_using_optimized_module_handling();
2034     aot_log_info(aot)("optimized module handling: disabled because archive was created without optimized module handling");
2035   }
2036 
2037   if (is_static()) {
2038     // Only the static archive can contain the full module graph.
2039     if (!_has_full_module_graph) {
2040       CDSConfig::stop_using_full_module_graph("archive was created without full module graph");
2041     }
2042   }
2043 
2044   return true;
2045 }
2046 
2047 bool FileMapInfo::validate_header() {
2048   if (!header()->validate()) {
2049     return false;
2050   }
2051   if (_is_static) {
2052     return true;
2053   } else {
2054     return DynamicArchive::validate(this);
2055   }
2056 }
2057 
2058 #if INCLUDE_JVMTI
2059 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = nullptr;
2060 
2061 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
2062   if (i == 0) {
2063     // index 0 corresponds to the ClassPathImageEntry which is a globally shared object
2064     // and should never be deleted.
2065     return ClassLoader::get_jrt_entry();
2066   }
2067   ClassPathEntry* ent = _classpath_entries_for_jvmti[i];
2068   if (ent == nullptr) {
2069     const AOTClassLocation* cl = AOTClassLocationConfig::runtime()->class_location_at(i);
2070     const char* path = cl->path();
2071     struct stat st;
2072     if (os::stat(path, &st) != 0) {
2073       char *msg = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, strlen(path) + 128);
2074       jio_snprintf(msg, strlen(path) + 127, "error in finding JAR file %s", path);
2075       THROW_MSG_(vmSymbols::java_io_IOException(), msg, nullptr);
2076     } else {
2077       ent = ClassLoader::create_class_path_entry(THREAD, path, &st);
2078       if (ent == nullptr) {
2079         char *msg = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, strlen(path) + 128);
2080         jio_snprintf(msg, strlen(path) + 127, "error in opening JAR file %s", path);
2081         THROW_MSG_(vmSymbols::java_io_IOException(), msg, nullptr);
2082       }
2083     }
2084 
2085     MutexLocker mu(THREAD, CDSClassFileStream_lock);
2086     if (_classpath_entries_for_jvmti[i] == nullptr) {
2087       _classpath_entries_for_jvmti[i] = ent;
2088     } else {
2089       // Another thread has beat me to creating this entry
2090       delete ent;
2091       ent = _classpath_entries_for_jvmti[i];
2092     }
2093   }
2094 
2095   return ent;
2096 }
2097 
2098 ClassFileStream* FileMapInfo::open_stream_for_jvmti(InstanceKlass* ik, Handle class_loader, TRAPS) {
2099   int path_index = ik->shared_classpath_index();
2100   assert(path_index >= 0, "should be called for shared built-in classes only");
2101   assert(path_index < AOTClassLocationConfig::runtime()->length(), "sanity");
2102 
2103   ClassPathEntry* cpe = get_classpath_entry_for_jvmti(path_index, CHECK_NULL);
2104   assert(cpe != nullptr, "must be");
2105 
2106   Symbol* name = ik->name();
2107   const char* const class_name = name->as_C_string();
2108   const char* const file_name = ClassLoader::file_name_for_class_name(class_name,
2109                                                                       name->utf8_length());
2110   ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader());
2111   const AOTClassLocation* cl = AOTClassLocationConfig::runtime()->class_location_at(path_index);
2112   ClassFileStream* cfs;
2113   if (class_loader() != nullptr && cl->is_multi_release_jar()) {
2114     // This class was loaded from a multi-release JAR file during dump time. The
2115     // process for finding its classfile is complex. Let's defer to the Java code
2116     // in java.lang.ClassLoader.
2117     cfs = get_stream_from_class_loader(class_loader, cpe, file_name, CHECK_NULL);
2118   } else {
2119     cfs = cpe->open_stream_for_loader(THREAD, file_name, loader_data);
2120   }
2121   assert(cfs != nullptr, "must be able to read the classfile data of shared classes for built-in loaders.");
2122   log_debug(aot, jvmti)("classfile data for %s [%d: %s] = %d bytes", class_name, path_index,
2123                         cfs->source(), cfs->length());
2124   return cfs;
2125 }
2126 
2127 ClassFileStream* FileMapInfo::get_stream_from_class_loader(Handle class_loader,
2128                                                            ClassPathEntry* cpe,
2129                                                            const char* file_name,
2130                                                            TRAPS) {
2131   JavaValue result(T_OBJECT);
2132   oop class_name = java_lang_String::create_oop_from_str(file_name, THREAD);
2133   Handle h_class_name = Handle(THREAD, class_name);
2134 
2135   // byte[] ClassLoader.getResourceAsByteArray(String name)
2136   JavaCalls::call_virtual(&result,
2137                           class_loader,
2138                           vmClasses::ClassLoader_klass(),
2139                           vmSymbols::getResourceAsByteArray_name(),
2140                           vmSymbols::getResourceAsByteArray_signature(),
2141                           h_class_name,
2142                           CHECK_NULL);
2143   assert(result.get_type() == T_OBJECT, "just checking");
2144   oop obj = result.get_oop();
2145   assert(obj != nullptr, "ClassLoader.getResourceAsByteArray should not return null");
2146 
2147   // copy from byte[] to a buffer
2148   typeArrayOop ba = typeArrayOop(obj);
2149   jint len = ba->length();
2150   u1* buffer = NEW_RESOURCE_ARRAY(u1, len);
2151   ArrayAccess<>::arraycopy_to_native<>(ba, typeArrayOopDesc::element_offset<jbyte>(0), buffer, len);
2152 
2153   return new ClassFileStream(buffer, len, cpe->name());
2154 }
2155 #endif