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