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