1 /* 2 * Copyright (c) 2012, 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/aotArtifactFinder.hpp" 26 #include "cds/aotClassInitializer.hpp" 27 #include "cds/aotClassLinker.hpp" 28 #include "cds/aotClassLocation.hpp" 29 #include "cds/aotConstantPoolResolver.hpp" 30 #include "cds/aotLinkedClassBulkLoader.hpp" 31 #include "cds/archiveBuilder.hpp" 32 #include "cds/archiveHeapLoader.hpp" 33 #include "cds/archiveHeapWriter.hpp" 34 #include "cds/cds_globals.hpp" 35 #include "cds/cdsConfig.hpp" 36 #include "cds/cdsProtectionDomain.hpp" 37 #include "cds/classListParser.hpp" 38 #include "cds/classListWriter.hpp" 39 #include "cds/cppVtables.hpp" 40 #include "cds/dumpAllocStats.hpp" 41 #include "cds/dynamicArchive.hpp" 42 #include "cds/filemap.hpp" 43 #include "cds/finalImageRecipes.hpp" 44 #include "cds/heapShared.hpp" 45 #include "cds/lambdaFormInvokers.hpp" 46 #include "cds/lambdaProxyClassDictionary.hpp" 47 #include "cds/metaspaceShared.hpp" 48 #include "classfile/classLoaderDataGraph.hpp" 49 #include "classfile/classLoaderDataShared.hpp" 50 #include "classfile/classLoaderExt.hpp" 51 #include "classfile/javaClasses.inline.hpp" 52 #include "classfile/loaderConstraints.hpp" 53 #include "classfile/modules.hpp" 54 #include "classfile/placeholders.hpp" 55 #include "classfile/stringTable.hpp" 56 #include "classfile/symbolTable.hpp" 57 #include "classfile/systemDictionary.hpp" 58 #include "classfile/systemDictionaryShared.hpp" 59 #include "classfile/vmClasses.hpp" 60 #include "classfile/vmSymbols.hpp" 61 #include "code/codeCache.hpp" 62 #include "gc/shared/gcVMOperations.hpp" 63 #include "interpreter/bytecodeStream.hpp" 64 #include "interpreter/bytecodes.hpp" 65 #include "jvm_io.h" 66 #include "logging/log.hpp" 67 #include "logging/logMessage.hpp" 68 #include "logging/logStream.hpp" 69 #include "memory/memoryReserver.hpp" 70 #include "memory/metaspace.hpp" 71 #include "memory/metaspaceClosure.hpp" 72 #include "memory/resourceArea.hpp" 73 #include "memory/universe.hpp" 74 #include "nmt/memTracker.hpp" 75 #include "oops/compressedKlass.hpp" 76 #include "oops/flatArrayKlass.hpp" 77 #include "oops/inlineKlass.hpp" 78 #include "oops/instanceMirrorKlass.hpp" 79 #include "oops/klass.inline.hpp" 80 #include "oops/objArrayOop.hpp" 81 #include "oops/oop.inline.hpp" 82 #include "oops/oopHandle.hpp" 83 #include "prims/jvmtiExport.hpp" 84 #include "runtime/arguments.hpp" 85 #include "runtime/globals.hpp" 86 #include "runtime/globals_extension.hpp" 87 #include "runtime/handles.inline.hpp" 88 #include "runtime/javaCalls.hpp" 89 #include "runtime/os.inline.hpp" 90 #include "runtime/safepointVerifiers.hpp" 91 #include "runtime/sharedRuntime.hpp" 92 #include "runtime/vmOperations.hpp" 93 #include "runtime/vmThread.hpp" 94 #include "sanitizers/leak.hpp" 95 #include "utilities/align.hpp" 96 #include "utilities/bitMap.inline.hpp" 97 #include "utilities/defaultStream.hpp" 98 #include "utilities/macros.hpp" 99 #include "utilities/ostream.hpp" 100 #include "utilities/resourceHash.hpp" 101 102 #include <sys/stat.h> 103 104 ReservedSpace MetaspaceShared::_symbol_rs; 105 VirtualSpace MetaspaceShared::_symbol_vs; 106 bool MetaspaceShared::_archive_loading_failed = false; 107 bool MetaspaceShared::_remapped_readwrite = false; 108 void* MetaspaceShared::_shared_metaspace_static_top = nullptr; 109 intx MetaspaceShared::_relocation_delta; 110 char* MetaspaceShared::_requested_base_address; 111 Array<Method*>* MetaspaceShared::_archived_method_handle_intrinsics = nullptr; 112 bool MetaspaceShared::_use_optimized_module_handling = true; 113 114 // The CDS archive is divided into the following regions: 115 // rw - read-write metadata 116 // ro - read-only metadata and read-only tables 117 // hp - heap region 118 // bm - bitmap for relocating the above 7 regions. 119 // 120 // The rw and ro regions are linearly allocated, in the order of rw->ro. 121 // These regions are aligned with MetaspaceShared::core_region_alignment(). 122 // 123 // These 2 regions are populated in the following steps: 124 // [0] All classes are loaded in MetaspaceShared::loadable_descriptors(). All metadata are 125 // temporarily allocated outside of the shared regions. 126 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions. 127 // [2] C++ vtables are copied into the rw region. 128 // [3] ArchiveBuilder copies RW metadata into the rw region. 129 // [4] ArchiveBuilder copies RO metadata into the ro region. 130 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data 131 // are copied into the ro region as read-only tables. 132 // 133 // The heap region is written by HeapShared::write_heap(). 134 // 135 // The bitmap region is used to relocate the ro/rw/hp regions. 136 137 static DumpRegion _symbol_region("symbols"); 138 139 char* MetaspaceShared::symbol_space_alloc(size_t num_bytes) { 140 return _symbol_region.allocate(num_bytes); 141 } 142 143 // os::vm_allocation_granularity() is usually 4K for most OSes. However, some platforms 144 // such as linux-aarch64 and macos-x64 ... 145 // it can be either 4K or 64K and on macos-aarch64 it is 16K. To generate archives that are 146 // compatible for both settings, an alternative cds core region alignment can be enabled 147 // at building time: 148 // --enable-compactible-cds-alignment 149 // Upon successful configuration, the compactible alignment then can be defined in: 150 // os_linux_aarch64.cpp 151 // os_bsd_x86.cpp 152 size_t MetaspaceShared::core_region_alignment() { 153 return os::cds_core_region_alignment(); 154 } 155 156 size_t MetaspaceShared::protection_zone_size() { 157 return os::cds_core_region_alignment(); 158 } 159 160 static bool shared_base_valid(char* shared_base) { 161 // We check user input for SharedBaseAddress at dump time. 162 163 // At CDS runtime, "shared_base" will be the (attempted) mapping start. It will also 164 // be the encoding base, since the headers of archived base objects (and with Lilliput, 165 // the prototype mark words) carry pre-computed narrow Klass IDs that refer to the mapping 166 // start as base. 167 // 168 // On AARCH64, The "shared_base" may not be later usable as encoding base, depending on the 169 // total size of the reserved area and the precomputed_narrow_klass_shift. This is checked 170 // before reserving memory. Here we weed out values already known to be invalid later. 171 return AARCH64_ONLY(is_aligned(shared_base, 4 * G)) NOT_AARCH64(true); 172 } 173 174 class DumpClassListCLDClosure : public CLDClosure { 175 static const int INITIAL_TABLE_SIZE = 1987; 176 static const int MAX_TABLE_SIZE = 61333; 177 178 fileStream *_stream; 179 ResizeableResourceHashtable<InstanceKlass*, bool, 180 AnyObj::C_HEAP, mtClassShared> _dumped_classes; 181 182 void dump(InstanceKlass* ik) { 183 bool created; 184 _dumped_classes.put_if_absent(ik, &created); 185 if (!created) { 186 return; 187 } 188 if (_dumped_classes.maybe_grow()) { 189 log_info(cds, hashtables)("Expanded _dumped_classes table to %d", _dumped_classes.table_size()); 190 } 191 if (ik->java_super()) { 192 dump(ik->java_super()); 193 } 194 Array<InstanceKlass*>* interfaces = ik->local_interfaces(); 195 int len = interfaces->length(); 196 for (int i = 0; i < len; i++) { 197 dump(interfaces->at(i)); 198 } 199 ClassListWriter::write_to_stream(ik, _stream); 200 } 201 202 public: 203 DumpClassListCLDClosure(fileStream* f) 204 : CLDClosure(), _dumped_classes(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE) { 205 _stream = f; 206 } 207 208 void do_cld(ClassLoaderData* cld) { 209 for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) { 210 if (klass->is_instance_klass()) { 211 dump(InstanceKlass::cast(klass)); 212 } 213 } 214 } 215 }; 216 217 void MetaspaceShared::dump_loaded_classes(const char* file_name, TRAPS) { 218 fileStream stream(file_name, "w"); 219 if (stream.is_open()) { 220 MutexLocker lock(ClassLoaderDataGraph_lock); 221 MutexLocker lock2(ClassListFile_lock, Mutex::_no_safepoint_check_flag); 222 DumpClassListCLDClosure collect_classes(&stream); 223 ClassLoaderDataGraph::loaded_cld_do(&collect_classes); 224 } else { 225 THROW_MSG(vmSymbols::java_io_IOException(), "Failed to open file"); 226 } 227 } 228 229 static bool shared_base_too_high(char* specified_base, char* aligned_base, size_t cds_max) { 230 // Caller should have checked that aligned_base was successfully aligned and is not nullptr. 231 // Comparing specified_base with nullptr is UB. 232 assert(aligned_base != nullptr, "sanity"); 233 assert(aligned_base >= specified_base, "sanity"); 234 235 if (max_uintx - uintx(aligned_base) < uintx(cds_max)) { 236 // Not enough address space to hold an archive of cds_max bytes from aligned_base. 237 return true; 238 } else { 239 return false; 240 } 241 } 242 243 static char* compute_shared_base(size_t cds_max) { 244 char* specified_base = (char*)SharedBaseAddress; 245 size_t alignment = MetaspaceShared::core_region_alignment(); 246 if (UseCompressedClassPointers) { 247 alignment = MAX2(alignment, Metaspace::reserve_alignment()); 248 } 249 250 if (SharedBaseAddress == 0) { 251 // Special meaning of -XX:SharedBaseAddress=0 -> Always map archive at os-selected address. 252 return specified_base; 253 } 254 255 char* aligned_base = can_align_up(specified_base, alignment) 256 ? align_up(specified_base, alignment) 257 : nullptr; 258 259 if (aligned_base != specified_base) { 260 log_info(cds)("SharedBaseAddress (" INTPTR_FORMAT ") aligned up to " INTPTR_FORMAT, 261 p2i(specified_base), p2i(aligned_base)); 262 } 263 264 const char* err = nullptr; 265 if (aligned_base == nullptr) { 266 err = "too high"; 267 } else if (shared_base_too_high(specified_base, aligned_base, cds_max)) { 268 err = "too high"; 269 } else if (!shared_base_valid(aligned_base)) { 270 err = "invalid for this platform"; 271 } else { 272 return aligned_base; 273 } 274 275 // Arguments::default_SharedBaseAddress() is hard-coded in cds_globals.hpp. It must be carefully 276 // picked that (a) the align_up() below will always return a valid value; (b) none of 277 // the following asserts will fail. 278 log_warning(cds)("SharedBaseAddress (" INTPTR_FORMAT ") is %s. Reverted to " INTPTR_FORMAT, 279 p2i((void*)SharedBaseAddress), err, 280 p2i((void*)Arguments::default_SharedBaseAddress())); 281 282 specified_base = (char*)Arguments::default_SharedBaseAddress(); 283 aligned_base = align_up(specified_base, alignment); 284 285 // Make sure the default value of SharedBaseAddress specified in globals.hpp is sane. 286 assert(!shared_base_too_high(specified_base, aligned_base, cds_max), "Sanity"); 287 assert(shared_base_valid(aligned_base), "Sanity"); 288 return aligned_base; 289 } 290 291 void MetaspaceShared::initialize_for_static_dump() { 292 assert(CDSConfig::is_dumping_static_archive(), "sanity"); 293 log_info(cds)("Core region alignment: %zu", core_region_alignment()); 294 // The max allowed size for CDS archive. We use this to limit SharedBaseAddress 295 // to avoid address space wrap around. 296 size_t cds_max; 297 const size_t reserve_alignment = core_region_alignment(); 298 299 #ifdef _LP64 300 const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1); 301 cds_max = align_down(UnscaledClassSpaceMax, reserve_alignment); 302 #else 303 // We don't support archives larger than 256MB on 32-bit due to limited 304 // virtual address space. 305 cds_max = align_down(256*M, reserve_alignment); 306 #endif 307 308 _requested_base_address = compute_shared_base(cds_max); 309 SharedBaseAddress = (size_t)_requested_base_address; 310 311 size_t symbol_rs_size = LP64_ONLY(3 * G) NOT_LP64(128 * M); 312 _symbol_rs = MemoryReserver::reserve(symbol_rs_size, 313 os::vm_allocation_granularity(), 314 os::vm_page_size(), 315 mtClassShared); 316 if (!_symbol_rs.is_reserved()) { 317 log_error(cds)("Unable to reserve memory for symbols: %zu bytes.", symbol_rs_size); 318 MetaspaceShared::unrecoverable_writing_error(); 319 } 320 _symbol_region.init(&_symbol_rs, &_symbol_vs); 321 } 322 323 // Called by universe_post_init() 324 void MetaspaceShared::post_initialize(TRAPS) { 325 if (CDSConfig::is_using_archive()) { 326 int size = AOTClassLocationConfig::runtime()->length(); 327 if (size > 0) { 328 CDSProtectionDomain::allocate_shared_data_arrays(size, CHECK); 329 } 330 } 331 } 332 333 // Extra java.lang.Strings to be added to the archive 334 static GrowableArrayCHeap<OopHandle, mtClassShared>* _extra_interned_strings = nullptr; 335 // Extra Symbols to be added to the archive 336 static GrowableArrayCHeap<Symbol*, mtClassShared>* _extra_symbols = nullptr; 337 // Methods managed by SystemDictionary::find_method_handle_intrinsic() to be added to the archive 338 static GrowableArray<Method*>* _pending_method_handle_intrinsics = nullptr; 339 340 void MetaspaceShared::read_extra_data(JavaThread* current, const char* filename) { 341 _extra_interned_strings = new GrowableArrayCHeap<OopHandle, mtClassShared>(10000); 342 _extra_symbols = new GrowableArrayCHeap<Symbol*, mtClassShared>(1000); 343 344 HashtableTextDump reader(filename); 345 reader.check_version("VERSION: 1.0"); 346 347 while (reader.remain() > 0) { 348 int utf8_length; 349 int prefix_type = reader.scan_prefix(&utf8_length); 350 ResourceMark rm(current); 351 if (utf8_length == 0x7fffffff) { 352 // buf_len will overflown 32-bit value. 353 log_error(cds)("string length too large: %d", utf8_length); 354 MetaspaceShared::unrecoverable_loading_error(); 355 } 356 int buf_len = utf8_length+1; 357 char* utf8_buffer = NEW_RESOURCE_ARRAY(char, buf_len); 358 reader.get_utf8(utf8_buffer, utf8_length); 359 utf8_buffer[utf8_length] = '\0'; 360 361 if (prefix_type == HashtableTextDump::SymbolPrefix) { 362 _extra_symbols->append(SymbolTable::new_permanent_symbol(utf8_buffer)); 363 } else{ 364 assert(prefix_type == HashtableTextDump::StringPrefix, "Sanity"); 365 ExceptionMark em(current); 366 JavaThread* THREAD = current; // For exception macros. 367 oop str = StringTable::intern(utf8_buffer, THREAD); 368 369 if (HAS_PENDING_EXCEPTION) { 370 log_warning(cds, heap)("[line %d] extra interned string allocation failed; size too large: %d", 371 reader.last_line_no(), utf8_length); 372 CLEAR_PENDING_EXCEPTION; 373 } else { 374 #if INCLUDE_CDS_JAVA_HEAP 375 if (ArchiveHeapWriter::is_string_too_large_to_archive(str)) { 376 log_warning(cds, heap)("[line %d] extra interned string ignored; size too large: %d", 377 reader.last_line_no(), utf8_length); 378 continue; 379 } 380 // Make sure this string is included in the dumped interned string table. 381 assert(str != nullptr, "must succeed"); 382 _extra_interned_strings->append(OopHandle(Universe::vm_global(), str)); 383 #endif 384 } 385 } 386 } 387 } 388 389 void MetaspaceShared::make_method_handle_intrinsics_shareable() { 390 for (int i = 0; i < _pending_method_handle_intrinsics->length(); i++) { 391 Method* m = ArchiveBuilder::current()->get_buffered_addr(_pending_method_handle_intrinsics->at(i)); 392 m->remove_unshareable_info(); 393 // Each method has its own constant pool (which is distinct from m->method_holder()->constants()); 394 m->constants()->remove_unshareable_info(); 395 } 396 } 397 398 void MetaspaceShared::write_method_handle_intrinsics() { 399 int len = _pending_method_handle_intrinsics->length(); 400 _archived_method_handle_intrinsics = ArchiveBuilder::new_ro_array<Method*>(len); 401 int word_size = _archived_method_handle_intrinsics->size(); 402 for (int i = 0; i < len; i++) { 403 Method* m = _pending_method_handle_intrinsics->at(i); 404 ArchiveBuilder::current()->write_pointer_in_buffer(_archived_method_handle_intrinsics->adr_at(i), m); 405 word_size += m->size() + m->constMethod()->size() + m->constants()->size(); 406 if (m->constants()->cache() != nullptr) { 407 word_size += m->constants()->cache()->size(); 408 } 409 } 410 log_info(cds)("Archived %d method handle intrinsics (%d bytes)", len, word_size * BytesPerWord); 411 } 412 413 // About "serialize" -- 414 // 415 // This is (probably a badly named) way to read/write a data stream of pointers and 416 // miscellaneous data from/to the shared archive file. The usual code looks like this: 417 // 418 // // These two global C++ variables are initialized during dump time. 419 // static int _archived_int; 420 // static MetaspaceObj* archived_ptr; 421 // 422 // void MyClass::serialize(SerializeClosure* soc) { 423 // soc->do_int(&_archived_int); 424 // soc->do_int(&_archived_ptr); 425 // } 426 // 427 // At dumptime, these two variables are stored into the CDS archive. 428 // At runtime, these two variables are loaded from the CDS archive. 429 // In addition, the pointer is relocated as necessary. 430 // 431 // Some of the xxx::serialize() functions may have side effects and assume that 432 // the archive is already mapped. For example, SymbolTable::serialize_shared_table_header() 433 // unconditionally makes the set of archived symbols available. Therefore, we put most 434 // of these xxx::serialize() functions inside MetaspaceShared::serialize(), which 435 // is called AFTER we made the decision to map the archive. 436 // 437 // However, some of the "serialized" data are used to decide whether an archive should 438 // be mapped or not (e.g., for checking if the -Djdk.module.main property is compatible 439 // with the archive). The xxx::serialize() functions for these data must be put inside 440 // MetaspaceShared::early_serialize(). Such functions must not produce side effects that 441 // assume we will always decides to map the archive. 442 443 void MetaspaceShared::early_serialize(SerializeClosure* soc) { 444 int tag = 0; 445 soc->do_tag(--tag); 446 CDS_JAVA_HEAP_ONLY(Modules::serialize_archived_module_info(soc);) 447 soc->do_tag(666); 448 } 449 450 void MetaspaceShared::serialize(SerializeClosure* soc) { 451 int tag = 0; 452 soc->do_tag(--tag); 453 454 // Verify the sizes of various metadata in the system. 455 soc->do_tag(sizeof(Method)); 456 soc->do_tag(sizeof(ConstMethod)); 457 soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE)); 458 soc->do_tag(sizeof(ConstantPool)); 459 soc->do_tag(sizeof(ConstantPoolCache)); 460 soc->do_tag(objArrayOopDesc::base_offset_in_bytes()); 461 soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE)); 462 soc->do_tag(sizeof(Symbol)); 463 464 // Need to do this first, as subsequent steps may call virtual functions 465 // in archived Metadata objects. 466 CppVtables::serialize(soc); 467 soc->do_tag(--tag); 468 469 // Dump/restore miscellaneous metadata. 470 JavaClasses::serialize_offsets(soc); 471 Universe::serialize(soc); 472 soc->do_tag(--tag); 473 474 // Dump/restore references to commonly used names and signatures. 475 vmSymbols::serialize(soc); 476 soc->do_tag(--tag); 477 478 // Dump/restore the symbol/string/subgraph_info tables 479 SymbolTable::serialize_shared_table_header(soc); 480 StringTable::serialize_shared_table_header(soc); 481 HeapShared::serialize_tables(soc); 482 SystemDictionaryShared::serialize_dictionary_headers(soc); 483 AOTLinkedClassBulkLoader::serialize(soc, true); 484 FinalImageRecipes::serialize(soc); 485 InstanceMirrorKlass::serialize_offsets(soc); 486 487 // Dump/restore well known classes (pointers) 488 SystemDictionaryShared::serialize_vm_classes(soc); 489 soc->do_tag(--tag); 490 491 CDS_JAVA_HEAP_ONLY(ClassLoaderDataShared::serialize(soc);) 492 soc->do_ptr((void**)&_archived_method_handle_intrinsics); 493 494 LambdaFormInvokers::serialize(soc); 495 soc->do_tag(666); 496 } 497 498 static void rewrite_nofast_bytecode(const methodHandle& method) { 499 BytecodeStream bcs(method); 500 while (!bcs.is_last_bytecode()) { 501 Bytecodes::Code opcode = bcs.next(); 502 switch (opcode) { 503 case Bytecodes::_getfield: *bcs.bcp() = Bytecodes::_nofast_getfield; break; 504 case Bytecodes::_putfield: *bcs.bcp() = Bytecodes::_nofast_putfield; break; 505 case Bytecodes::_aload_0: *bcs.bcp() = Bytecodes::_nofast_aload_0; break; 506 case Bytecodes::_iload: { 507 if (!bcs.is_wide()) { 508 *bcs.bcp() = Bytecodes::_nofast_iload; 509 } 510 break; 511 } 512 default: break; 513 } 514 } 515 } 516 517 // [1] Rewrite all bytecodes as needed, so that the ConstMethod* will not be modified 518 // at run time by RewriteBytecodes/RewriteFrequentPairs 519 // [2] Assign a fingerprint, so one doesn't need to be assigned at run-time. 520 void MetaspaceShared::rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread* thread, InstanceKlass* ik) { 521 for (int i = 0; i < ik->methods()->length(); i++) { 522 methodHandle m(thread, ik->methods()->at(i)); 523 if (ik->can_be_verified_at_dumptime() && ik->is_linked()) { 524 rewrite_nofast_bytecode(m); 525 } 526 Fingerprinter fp(m); 527 // The side effect of this call sets method's fingerprint field. 528 fp.fingerprint(); 529 } 530 } 531 532 class VM_PopulateDumpSharedSpace : public VM_Operation { 533 private: 534 ArchiveHeapInfo _heap_info; 535 FileMapInfo* _map_info; 536 StaticArchiveBuilder& _builder; 537 538 void dump_java_heap_objects(); 539 void dump_shared_symbol_table(GrowableArray<Symbol*>* symbols) { 540 log_info(cds)("Dumping symbol table ..."); 541 SymbolTable::write_to_archive(symbols); 542 } 543 char* dump_early_read_only_tables(); 544 char* dump_read_only_tables(AOTClassLocationConfig*& cl_config); 545 546 public: 547 548 VM_PopulateDumpSharedSpace(StaticArchiveBuilder& b) : 549 VM_Operation(), _heap_info(), _map_info(nullptr), _builder(b) {} 550 551 bool skip_operation() const { return false; } 552 553 VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; } 554 ArchiveHeapInfo* heap_info() { return &_heap_info; } 555 FileMapInfo* map_info() const { return _map_info; } 556 void doit(); // outline because gdb sucks 557 bool allow_nested_vm_operations() const { return true; } 558 }; // class VM_PopulateDumpSharedSpace 559 560 class StaticArchiveBuilder : public ArchiveBuilder { 561 public: 562 StaticArchiveBuilder() : ArchiveBuilder() {} 563 564 virtual void iterate_roots(MetaspaceClosure* it) { 565 AOTArtifactFinder::all_cached_classes_do(it); 566 SystemDictionaryShared::dumptime_classes_do(it); 567 Universe::metaspace_pointers_do(it); 568 vmSymbols::metaspace_pointers_do(it); 569 570 // The above code should find all the symbols that are referenced by the 571 // archived classes. We just need to add the extra symbols which 572 // may not be used by any of the archived classes -- these are usually 573 // symbols that we anticipate to be used at run time, so we can store 574 // them in the RO region, to be shared across multiple processes. 575 if (_extra_symbols != nullptr) { 576 for (int i = 0; i < _extra_symbols->length(); i++) { 577 it->push(_extra_symbols->adr_at(i)); 578 } 579 } 580 581 for (int i = 0; i < _pending_method_handle_intrinsics->length(); i++) { 582 it->push(_pending_method_handle_intrinsics->adr_at(i)); 583 } 584 } 585 }; 586 587 char* VM_PopulateDumpSharedSpace::dump_early_read_only_tables() { 588 ArchiveBuilder::OtherROAllocMark mark; 589 590 CDS_JAVA_HEAP_ONLY(Modules::dump_archived_module_info()); 591 592 DumpRegion* ro_region = ArchiveBuilder::current()->ro_region(); 593 char* start = ro_region->top(); 594 WriteClosure wc(ro_region); 595 MetaspaceShared::early_serialize(&wc); 596 return start; 597 } 598 599 char* VM_PopulateDumpSharedSpace::dump_read_only_tables(AOTClassLocationConfig*& cl_config) { 600 ArchiveBuilder::OtherROAllocMark mark; 601 602 SystemDictionaryShared::write_to_archive(); 603 cl_config = AOTClassLocationConfig::dumptime()->write_to_archive(); 604 AOTClassLinker::write_to_archive(); 605 if (CDSConfig::is_dumping_preimage_static_archive()) { 606 FinalImageRecipes::record_recipes(); 607 } 608 MetaspaceShared::write_method_handle_intrinsics(); 609 610 // Write lambform lines into archive 611 LambdaFormInvokers::dump_static_archive_invokers(); 612 613 // Write the other data to the output array. 614 DumpRegion* ro_region = ArchiveBuilder::current()->ro_region(); 615 char* start = ro_region->top(); 616 WriteClosure wc(ro_region); 617 MetaspaceShared::serialize(&wc); 618 619 return start; 620 } 621 622 void VM_PopulateDumpSharedSpace::doit() { 623 if (!CDSConfig::is_dumping_final_static_archive()) { 624 guarantee(!CDSConfig::is_using_archive(), "We should not be using an archive when we dump"); 625 } 626 627 DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm); 628 629 _pending_method_handle_intrinsics = new (mtClassShared) GrowableArray<Method*>(256, mtClassShared); 630 if (CDSConfig::is_dumping_method_handles()) { 631 // When dumping AOT-linked classes, some classes may have direct references to a method handle 632 // intrinsic. The easiest thing is to save all of them into the AOT cache. 633 SystemDictionary::get_all_method_handle_intrinsics(_pending_method_handle_intrinsics); 634 } 635 636 AOTClassLocationConfig::dumptime_check_nonempty_dirs(); 637 638 NOT_PRODUCT(SystemDictionary::verify();) 639 640 // Block concurrent class unloading from changing the _dumptime_table 641 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 642 643 #if INCLUDE_CDS_JAVA_HEAP 644 if (CDSConfig::is_dumping_heap() && _extra_interned_strings != nullptr) { 645 for (int i = 0; i < _extra_interned_strings->length(); i ++) { 646 OopHandle string = _extra_interned_strings->at(i); 647 HeapShared::add_to_dumped_interned_strings(string.resolve()); 648 } 649 } 650 #endif 651 652 _builder.gather_source_objs(); 653 _builder.reserve_buffer(); 654 655 CppVtables::dumptime_init(&_builder); 656 657 _builder.sort_metadata_objs(); 658 _builder.dump_rw_metadata(); 659 _builder.dump_ro_metadata(); 660 _builder.relocate_metaspaceobj_embedded_pointers(); 661 662 log_info(cds)("Make classes shareable"); 663 _builder.make_klasses_shareable(); 664 MetaspaceShared::make_method_handle_intrinsics_shareable(); 665 666 dump_java_heap_objects(); 667 dump_shared_symbol_table(_builder.symbols()); 668 669 char* early_serialized_data = dump_early_read_only_tables(); 670 AOTClassLocationConfig* cl_config; 671 char* serialized_data = dump_read_only_tables(cl_config); 672 673 if (CDSConfig::is_dumping_lambdas_in_legacy_mode()) { 674 log_info(cds)("Adjust lambda proxy class dictionary"); 675 LambdaProxyClassDictionary::adjust_dumptime_table(); 676 } 677 678 // The vtable clones contain addresses of the current process. 679 // We don't want to write these addresses into the archive. 680 CppVtables::zero_archived_vtables(); 681 682 // Write the archive file 683 if (CDSConfig::is_dumping_final_static_archive()) { 684 FileMapInfo::free_current_info(); // FIXME: should not free current info 685 } 686 const char* static_archive = CDSConfig::output_archive_path(); 687 assert(static_archive != nullptr, "sanity"); 688 _map_info = new FileMapInfo(static_archive, true); 689 _map_info->populate_header(MetaspaceShared::core_region_alignment()); 690 _map_info->set_early_serialized_data(early_serialized_data); 691 _map_info->set_serialized_data(serialized_data); 692 _map_info->set_cloned_vtables(CppVtables::vtables_serialized_base()); 693 _map_info->header()->set_class_location_config(cl_config); 694 } 695 696 class CollectClassesForLinking : public KlassClosure { 697 GrowableArray<OopHandle> _mirrors; 698 699 public: 700 CollectClassesForLinking() : _mirrors() { 701 // ClassLoaderDataGraph::loaded_classes_do_keepalive() requires ClassLoaderDataGraph_lock. 702 // We cannot link the classes while holding this lock (or else we may run into deadlock). 703 // Therefore, we need to first collect all the classes, keeping them alive by 704 // holding onto their java_mirrors in global OopHandles. We then link the classes after 705 // releasing the lock. 706 MutexLocker lock(ClassLoaderDataGraph_lock); 707 ClassLoaderDataGraph::loaded_classes_do_keepalive(this); 708 } 709 710 ~CollectClassesForLinking() { 711 for (int i = 0; i < _mirrors.length(); i++) { 712 _mirrors.at(i).release(Universe::vm_global()); 713 } 714 } 715 716 void do_cld(ClassLoaderData* cld) { 717 assert(cld->is_alive(), "must be"); 718 } 719 720 void do_klass(Klass* k) { 721 if (k->is_instance_klass()) { 722 _mirrors.append(OopHandle(Universe::vm_global(), k->java_mirror())); 723 } 724 } 725 726 const GrowableArray<OopHandle>* mirrors() const { return &_mirrors; } 727 }; 728 729 // Check if we can eagerly link this class at dump time, so we can avoid the 730 // runtime linking overhead (especially verification) 731 bool MetaspaceShared::may_be_eagerly_linked(InstanceKlass* ik) { 732 if (!ik->can_be_verified_at_dumptime()) { 733 // For old classes, try to leave them in the unlinked state, so 734 // we can still store them in the archive. They must be 735 // linked/verified at runtime. 736 return false; 737 } 738 if (CDSConfig::is_dumping_dynamic_archive() && ik->is_shared_unregistered_class()) { 739 // Linking of unregistered classes at this stage may cause more 740 // classes to be resolved, resulting in calls to ClassLoader.loadClass() 741 // that may not be expected by custom class loaders. 742 // 743 // It's OK to do this for the built-in loaders as we know they can 744 // tolerate this. 745 return false; 746 } 747 return true; 748 } 749 750 void MetaspaceShared::link_shared_classes(TRAPS) { 751 AOTClassLinker::initialize(); 752 AOTClassInitializer::init_test_class(CHECK); 753 754 while (true) { 755 ResourceMark rm(THREAD); 756 CollectClassesForLinking collect_classes; 757 bool has_linked = false; 758 const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors(); 759 for (int i = 0; i < mirrors->length(); i++) { 760 OopHandle mirror = mirrors->at(i); 761 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve())); 762 if (may_be_eagerly_linked(ik)) { 763 has_linked |= try_link_class(THREAD, ik); 764 } 765 } 766 767 if (!has_linked) { 768 break; 769 } 770 // Class linking includes verification which may load more classes. 771 // Keep scanning until we have linked no more classes. 772 } 773 774 // Resolve constant pool entries -- we don't load any new classes during this stage 775 { 776 ResourceMark rm(THREAD); 777 CollectClassesForLinking collect_classes; 778 const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors(); 779 for (int i = 0; i < mirrors->length(); i++) { 780 OopHandle mirror = mirrors->at(i); 781 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve())); 782 AOTConstantPoolResolver::dumptime_resolve_constants(ik, CHECK); 783 } 784 } 785 786 if (CDSConfig::is_dumping_final_static_archive()) { 787 FinalImageRecipes::apply_recipes(CHECK); 788 } 789 } 790 791 // Preload classes from a list, populate the shared spaces and dump to a 792 // file. 793 void MetaspaceShared::preload_and_dump(TRAPS) { 794 CDSConfig::DumperThreadMark dumper_thread_mark(THREAD); 795 ResourceMark rm(THREAD); 796 StaticArchiveBuilder builder; 797 preload_and_dump_impl(builder, THREAD); 798 if (HAS_PENDING_EXCEPTION) { 799 if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) { 800 log_error(cds)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = " 801 "%zuM", MaxHeapSize/M); 802 MetaspaceShared::writing_error(); 803 } else { 804 log_error(cds)("%s: %s", PENDING_EXCEPTION->klass()->external_name(), 805 java_lang_String::as_utf8_string(java_lang_Throwable::message(PENDING_EXCEPTION))); 806 MetaspaceShared::writing_error("Unexpected exception, use -Xlog:cds,exceptions=trace for detail"); 807 } 808 } 809 810 if (CDSConfig::new_aot_flags_used()) { 811 if (CDSConfig::is_dumping_preimage_static_archive()) { 812 // We are in the JVM that runs the training run. Continue execution, 813 // so that it can finish all clean-up and return the correct exit 814 // code to the OS. 815 tty->print_cr("AOTConfiguration recorded: %s", AOTConfiguration); 816 } else { 817 // The JLI launcher only recognizes the "old" -Xshare:dump flag. 818 // When the new -XX:AOTMode=create flag is used, we can't return 819 // to the JLI launcher, as the launcher will fail when trying to 820 // run the main class, which is not what we want. 821 struct stat st; 822 if (os::stat(AOTCache, &st) != 0) { 823 tty->print_cr("AOTCache creation failed: %s", AOTCache); 824 vm_exit(0); 825 } else { 826 tty->print_cr("AOTCache creation is complete: %s " INT64_FORMAT " bytes", AOTCache, (int64_t)(st.st_size)); 827 vm_exit(0); 828 } 829 } 830 } 831 } 832 833 #if INCLUDE_CDS_JAVA_HEAP && defined(_LP64) 834 void MetaspaceShared::adjust_heap_sizes_for_dumping() { 835 if (!CDSConfig::is_dumping_heap() || UseCompressedOops) { 836 return; 837 } 838 // CDS heap dumping requires all string oops to have an offset 839 // from the heap bottom that can be encoded in 32-bit. 840 julong max_heap_size = (julong)(4 * G); 841 842 if (MinHeapSize > max_heap_size) { 843 log_debug(cds)("Setting MinHeapSize to 4G for CDS dumping, original size = %zuM", MinHeapSize/M); 844 FLAG_SET_ERGO(MinHeapSize, max_heap_size); 845 } 846 if (InitialHeapSize > max_heap_size) { 847 log_debug(cds)("Setting InitialHeapSize to 4G for CDS dumping, original size = %zuM", InitialHeapSize/M); 848 FLAG_SET_ERGO(InitialHeapSize, max_heap_size); 849 } 850 if (MaxHeapSize > max_heap_size) { 851 log_debug(cds)("Setting MaxHeapSize to 4G for CDS dumping, original size = %zuM", MaxHeapSize/M); 852 FLAG_SET_ERGO(MaxHeapSize, max_heap_size); 853 } 854 } 855 #endif // INCLUDE_CDS_JAVA_HEAP && _LP64 856 857 void MetaspaceShared::get_default_classlist(char* default_classlist, const size_t buf_size) { 858 const char* filesep = os::file_separator(); 859 jio_snprintf(default_classlist, buf_size, "%s%slib%sclasslist", 860 Arguments::get_java_home(), filesep, filesep); 861 } 862 863 void MetaspaceShared::loadable_descriptors(TRAPS) { 864 char default_classlist[JVM_MAXPATHLEN]; 865 const char* classlist_path; 866 867 get_default_classlist(default_classlist, JVM_MAXPATHLEN); 868 if (SharedClassListFile == nullptr) { 869 classlist_path = default_classlist; 870 } else { 871 classlist_path = SharedClassListFile; 872 } 873 874 log_info(cds)("Loading classes to share ..."); 875 ClassListParser::parse_classlist(classlist_path, 876 ClassListParser::_parse_all, CHECK); 877 if (ExtraSharedClassListFile) { 878 ClassListParser::parse_classlist(ExtraSharedClassListFile, 879 ClassListParser::_parse_all, CHECK); 880 } 881 if (classlist_path != default_classlist) { 882 struct stat statbuf; 883 if (os::stat(default_classlist, &statbuf) == 0) { 884 // File exists, let's use it. 885 ClassListParser::parse_classlist(default_classlist, 886 ClassListParser::_parse_lambda_forms_invokers_only, CHECK); 887 } 888 } 889 890 // Some classes are used at CDS runtime but are not loaded, and therefore archived, at 891 // dumptime. We can perform dummmy calls to these classes at dumptime to ensure they 892 // are archived. 893 exercise_runtime_cds_code(CHECK); 894 895 log_info(cds)("Loading classes to share: done."); 896 } 897 898 void MetaspaceShared::exercise_runtime_cds_code(TRAPS) { 899 // Exercise the manifest processing code 900 const char* dummy = "Manifest-Version: 1.0\n"; 901 CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK); 902 903 // Exercise FileSystem and URL code 904 CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK); 905 } 906 907 void MetaspaceShared::preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) { 908 if (CDSConfig::is_dumping_classic_static_archive()) { 909 // We are running with -Xshare:dump 910 loadable_descriptors(CHECK); 911 912 if (SharedArchiveConfigFile) { 913 log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile); 914 read_extra_data(THREAD, SharedArchiveConfigFile); 915 log_info(cds)("Reading extra data: done."); 916 } 917 } 918 919 if (CDSConfig::is_dumping_preimage_static_archive()) { 920 log_info(cds)("Reading lambda form invokers from JDK default classlist ..."); 921 char default_classlist[JVM_MAXPATHLEN]; 922 get_default_classlist(default_classlist, JVM_MAXPATHLEN); 923 struct stat statbuf; 924 if (os::stat(default_classlist, &statbuf) == 0) { 925 ClassListParser::parse_classlist(default_classlist, 926 ClassListParser::_parse_lambda_forms_invokers_only, CHECK); 927 } 928 } 929 930 #if INCLUDE_CDS_JAVA_HEAP 931 if (CDSConfig::is_dumping_heap()) { 932 assert(CDSConfig::allow_only_single_java_thread(), "Required"); 933 if (!HeapShared::is_archived_boot_layer_available(THREAD)) { 934 log_info(cds)("archivedBootLayer not available, disabling full module graph"); 935 CDSConfig::stop_dumping_full_module_graph(); 936 } 937 // Do this before link_shared_classes(), as the following line may load new classes. 938 HeapShared::init_for_dumping(CHECK); 939 } 940 #endif 941 942 if (CDSConfig::is_dumping_final_static_archive()) { 943 if (ExtraSharedClassListFile) { 944 log_info(cds)("Loading extra classes from %s ...", ExtraSharedClassListFile); 945 ClassListParser::parse_classlist(ExtraSharedClassListFile, 946 ClassListParser::_parse_all, CHECK); 947 } 948 } 949 950 // Rewrite and link classes 951 log_info(cds)("Rewriting and linking classes ..."); 952 953 // Link any classes which got missed. This would happen if we have loaded classes that 954 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K 955 // fails verification, all other interfaces that were not specified in the classlist but 956 // are implemented by K are not verified. 957 link_shared_classes(CHECK); 958 log_info(cds)("Rewriting and linking classes: done"); 959 960 if (CDSConfig::is_dumping_regenerated_lambdaform_invokers()) { 961 LambdaFormInvokers::regenerate_holder_classes(CHECK); 962 } 963 964 #if INCLUDE_CDS_JAVA_HEAP 965 if (CDSConfig::is_dumping_heap()) { 966 ArchiveHeapWriter::init(); 967 if (CDSConfig::is_dumping_full_module_graph()) { 968 ClassLoaderDataShared::ensure_module_entry_tables_exist(); 969 HeapShared::reset_archived_object_states(CHECK); 970 } 971 972 if (CDSConfig::is_dumping_method_handles()) { 973 // This assert means that the MethodType and MethodTypeForm tables won't be 974 // updated concurrently when we are saving their contents into a side table. 975 assert(CDSConfig::allow_only_single_java_thread(), "Required"); 976 977 JavaValue result(T_VOID); 978 JavaCalls::call_static(&result, vmClasses::MethodType_klass(), 979 vmSymbols::createArchivedObjects(), 980 vmSymbols::void_method_signature(), 981 CHECK); 982 } 983 984 if (CDSConfig::is_initing_classes_at_dump_time()) { 985 // java.lang.Class::reflectionFactory cannot be archived yet. We set this field 986 // to null, and it will be initialized again at runtime. 987 log_debug(cds)("Resetting Class::reflectionFactory"); 988 TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates"); 989 Symbol* method_sig = vmSymbols::void_method_signature(); 990 JavaValue result(T_VOID); 991 JavaCalls::call_static(&result, vmClasses::Class_klass(), 992 method_name, method_sig, CHECK); 993 994 // Perhaps there is a way to avoid hard-coding these names here. 995 // See discussion in JDK-8342481. 996 } 997 998 // Do this at the very end, when no Java code will be executed. Otherwise 999 // some new strings may be added to the intern table. 1000 StringTable::allocate_shared_strings_array(CHECK); 1001 } else { 1002 log_info(cds)("Not dumping heap, reset CDSConfig::_is_using_optimized_module_handling"); 1003 CDSConfig::stop_using_optimized_module_handling(); 1004 } 1005 #endif 1006 1007 VM_PopulateDumpSharedSpace op(builder); 1008 VMThread::execute(&op); 1009 1010 if (!write_static_archive(&builder, op.map_info(), op.heap_info())) { 1011 THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping"); 1012 } 1013 } 1014 1015 bool MetaspaceShared::write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info) { 1016 // relocate the data so that it can be mapped to MetaspaceShared::requested_base_address() 1017 // without runtime relocation. 1018 builder->relocate_to_requested(); 1019 1020 map_info->open_as_output(); 1021 if (!map_info->is_open()) { 1022 return false; 1023 } 1024 builder->write_archive(map_info, heap_info); 1025 1026 if (AllowArchivingWithJavaAgent) { 1027 log_warning(cds)("This %s was created with AllowArchivingWithJavaAgent. It should be used " 1028 "for testing purposes only and should not be used in a production environment", CDSConfig::type_of_archive_being_loaded()); 1029 } 1030 return true; 1031 } 1032 1033 // Returns true if the class's status has changed. 1034 bool MetaspaceShared::try_link_class(JavaThread* current, InstanceKlass* ik) { 1035 ExceptionMark em(current); 1036 JavaThread* THREAD = current; // For exception macros. 1037 assert(CDSConfig::is_dumping_archive(), "sanity"); 1038 1039 if (ik->is_shared() && !CDSConfig::is_dumping_final_static_archive()) { 1040 assert(CDSConfig::is_dumping_dynamic_archive(), "must be"); 1041 return false; 1042 } 1043 1044 if (ik->is_loaded() && !ik->is_linked() && ik->can_be_verified_at_dumptime() && 1045 !SystemDictionaryShared::has_class_failed_verification(ik)) { 1046 bool saved = BytecodeVerificationLocal; 1047 if (ik->is_shared_unregistered_class() && ik->class_loader() == nullptr) { 1048 // The verification decision is based on BytecodeVerificationRemote 1049 // for non-system classes. Since we are using the null classloader 1050 // to load non-system classes for customized class loaders during dumping, 1051 // we need to temporarily change BytecodeVerificationLocal to be the same as 1052 // BytecodeVerificationRemote. Note this can cause the parent system 1053 // classes also being verified. The extra overhead is acceptable during 1054 // dumping. 1055 BytecodeVerificationLocal = BytecodeVerificationRemote; 1056 } 1057 ik->link_class(THREAD); 1058 if (HAS_PENDING_EXCEPTION) { 1059 ResourceMark rm(THREAD); 1060 log_warning(cds)("Preload Warning: Verification failed for %s", 1061 ik->external_name()); 1062 CLEAR_PENDING_EXCEPTION; 1063 SystemDictionaryShared::set_class_has_failed_verification(ik); 1064 } else { 1065 assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity"); 1066 ik->compute_has_loops_flag_for_methods(); 1067 } 1068 BytecodeVerificationLocal = saved; 1069 return true; 1070 } else { 1071 return false; 1072 } 1073 } 1074 1075 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() { 1076 if (CDSConfig::is_valhalla_preview()) { 1077 log_info(cds)("Archived java heap is not yet supported with Valhalla preview"); 1078 return; 1079 } 1080 1081 if (CDSConfig::is_dumping_heap()) { 1082 HeapShared::write_heap(&_heap_info); 1083 } else { 1084 CDSConfig::log_reasons_for_not_dumping_heap(); 1085 } 1086 } 1087 1088 void MetaspaceShared::set_shared_metaspace_range(void* base, void *static_top, void* top) { 1089 assert(base <= static_top && static_top <= top, "must be"); 1090 _shared_metaspace_static_top = static_top; 1091 MetaspaceObj::set_shared_metaspace_range(base, top); 1092 } 1093 1094 bool MetaspaceShared::is_shared_dynamic(void* p) { 1095 if ((p < MetaspaceObj::shared_metaspace_top()) && 1096 (p >= _shared_metaspace_static_top)) { 1097 return true; 1098 } else { 1099 return false; 1100 } 1101 } 1102 1103 bool MetaspaceShared::is_shared_static(void* p) { 1104 if (is_in_shared_metaspace(p) && !is_shared_dynamic(p)) { 1105 return true; 1106 } else { 1107 return false; 1108 } 1109 } 1110 1111 // This function is called when the JVM is unable to load the specified archive(s) due to one 1112 // of the following conditions. 1113 // - There's an error that indicates that the archive(s) files were corrupt or otherwise damaged. 1114 // - When -XX:+RequireSharedSpaces is specified, AND the JVM cannot load the archive(s) due 1115 // to version or classpath mismatch. 1116 void MetaspaceShared::unrecoverable_loading_error(const char* message) { 1117 log_error(cds)("An error has occurred while processing the %s.", CDSConfig::type_of_archive_being_loaded()); 1118 if (message != nullptr) { 1119 log_error(cds)("%s", message); 1120 } 1121 1122 if (CDSConfig::is_dumping_final_static_archive()) { 1123 vm_exit_during_initialization("Must be a valid AOT configuration generated by the current JVM", AOTConfiguration); 1124 } else if (CDSConfig::new_aot_flags_used()) { 1125 vm_exit_during_initialization("Unable to use AOT cache.", nullptr); 1126 } else { 1127 vm_exit_during_initialization("Unable to use shared archive.", nullptr); 1128 } 1129 } 1130 1131 // This function is called when the JVM is unable to write the specified CDS archive due to an 1132 // unrecoverable error. 1133 void MetaspaceShared::unrecoverable_writing_error(const char* message) { 1134 writing_error(message); 1135 vm_direct_exit(1); 1136 } 1137 1138 // This function is called when the JVM is unable to write the specified CDS archive due to a 1139 // an error. The error will be propagated 1140 void MetaspaceShared::writing_error(const char* message) { 1141 log_error(cds)("An error has occurred while writing the shared archive file."); 1142 if (message != nullptr) { 1143 log_error(cds)("%s", message); 1144 } 1145 } 1146 1147 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() { 1148 assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled"); 1149 MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE; 1150 1151 FileMapInfo* static_mapinfo = open_static_archive(); 1152 FileMapInfo* dynamic_mapinfo = nullptr; 1153 1154 if (static_mapinfo != nullptr) { 1155 log_info(cds)("Core region alignment: %zu", static_mapinfo->core_region_alignment()); 1156 dynamic_mapinfo = open_dynamic_archive(); 1157 1158 log_info(cds)("ArchiveRelocationMode: %d", ArchiveRelocationMode); 1159 1160 // First try to map at the requested address 1161 result = map_archives(static_mapinfo, dynamic_mapinfo, true); 1162 if (result == MAP_ARCHIVE_MMAP_FAILURE) { 1163 // Mapping has failed (probably due to ASLR). Let's map at an address chosen 1164 // by the OS. 1165 log_info(cds)("Try to map archive(s) at an alternative address"); 1166 result = map_archives(static_mapinfo, dynamic_mapinfo, false); 1167 } 1168 } 1169 1170 if (result == MAP_ARCHIVE_SUCCESS) { 1171 bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped()); 1172 char* cds_base = static_mapinfo->mapped_base(); 1173 char* cds_end = dynamic_mapped ? dynamic_mapinfo->mapped_end() : static_mapinfo->mapped_end(); 1174 // Register CDS memory region with LSan. 1175 LSAN_REGISTER_ROOT_REGION(cds_base, cds_end - cds_base); 1176 set_shared_metaspace_range(cds_base, static_mapinfo->mapped_end(), cds_end); 1177 _relocation_delta = static_mapinfo->relocation_delta(); 1178 _requested_base_address = static_mapinfo->requested_base_address(); 1179 if (dynamic_mapped) { 1180 // turn AutoCreateSharedArchive off if successfully mapped 1181 AutoCreateSharedArchive = false; 1182 } 1183 } else { 1184 set_shared_metaspace_range(nullptr, nullptr, nullptr); 1185 if (CDSConfig::is_dumping_dynamic_archive()) { 1186 log_warning(cds)("-XX:ArchiveClassesAtExit is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."); 1187 } 1188 UseSharedSpaces = false; 1189 // The base archive cannot be mapped. We cannot dump the dynamic shared archive. 1190 AutoCreateSharedArchive = false; 1191 CDSConfig::disable_dumping_dynamic_archive(); 1192 log_info(cds)("Unable to map shared spaces"); 1193 if (PrintSharedArchiveAndExit) { 1194 MetaspaceShared::unrecoverable_loading_error("Unable to use shared archive."); 1195 } else if (RequireSharedSpaces) { 1196 MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces"); 1197 } 1198 } 1199 1200 // If mapping failed and -XShare:on, the vm should exit 1201 bool has_failed = false; 1202 if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) { 1203 has_failed = true; 1204 delete static_mapinfo; 1205 } 1206 if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) { 1207 has_failed = true; 1208 delete dynamic_mapinfo; 1209 } 1210 if (RequireSharedSpaces && has_failed) { 1211 MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces"); 1212 } 1213 } 1214 1215 FileMapInfo* MetaspaceShared::open_static_archive() { 1216 const char* static_archive = CDSConfig::input_static_archive_path(); 1217 assert(static_archive != nullptr, "sanity"); 1218 FileMapInfo* mapinfo = new FileMapInfo(static_archive, true); 1219 if (!mapinfo->open_as_input()) { 1220 delete(mapinfo); 1221 return nullptr; 1222 } 1223 return mapinfo; 1224 } 1225 1226 FileMapInfo* MetaspaceShared::open_dynamic_archive() { 1227 if (CDSConfig::is_dumping_dynamic_archive()) { 1228 return nullptr; 1229 } 1230 const char* dynamic_archive = CDSConfig::input_dynamic_archive_path(); 1231 if (dynamic_archive == nullptr) { 1232 return nullptr; 1233 } 1234 1235 FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false); 1236 if (!mapinfo->open_as_input()) { 1237 delete(mapinfo); 1238 if (RequireSharedSpaces) { 1239 MetaspaceShared::unrecoverable_loading_error("Failed to initialize dynamic archive"); 1240 } 1241 return nullptr; 1242 } 1243 return mapinfo; 1244 } 1245 1246 // use_requested_addr: 1247 // true = map at FileMapHeader::_requested_base_address 1248 // false = map at an alternative address picked by OS. 1249 MapArchiveResult MetaspaceShared::map_archives(FileMapInfo* static_mapinfo, FileMapInfo* dynamic_mapinfo, 1250 bool use_requested_addr) { 1251 if (use_requested_addr && static_mapinfo->requested_base_address() == nullptr) { 1252 log_info(cds)("Archive(s) were created with -XX:SharedBaseAddress=0. Always map at os-selected address."); 1253 return MAP_ARCHIVE_MMAP_FAILURE; 1254 } 1255 1256 PRODUCT_ONLY(if (ArchiveRelocationMode == 1 && use_requested_addr) { 1257 // For product build only -- this is for benchmarking the cost of doing relocation. 1258 // For debug builds, the check is done below, after reserving the space, for better test coverage 1259 // (see comment below). 1260 log_info(cds)("ArchiveRelocationMode == 1: always map archive(s) at an alternative address"); 1261 return MAP_ARCHIVE_MMAP_FAILURE; 1262 }); 1263 1264 if (ArchiveRelocationMode == 2 && !use_requested_addr) { 1265 log_info(cds)("ArchiveRelocationMode == 2: never map archive(s) at an alternative address"); 1266 return MAP_ARCHIVE_MMAP_FAILURE; 1267 }; 1268 1269 if (dynamic_mapinfo != nullptr) { 1270 // Ensure that the OS won't be able to allocate new memory spaces between the two 1271 // archives, or else it would mess up the simple comparison in MetaspaceObj::is_shared(). 1272 assert(static_mapinfo->mapping_end_offset() == dynamic_mapinfo->mapping_base_offset(), "no gap"); 1273 } 1274 1275 ReservedSpace total_space_rs, archive_space_rs, class_space_rs; 1276 MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE; 1277 size_t prot_zone_size = 0; 1278 char* mapped_base_address = reserve_address_space_for_archives(static_mapinfo, 1279 dynamic_mapinfo, 1280 use_requested_addr, 1281 total_space_rs, 1282 archive_space_rs, 1283 class_space_rs); 1284 if (mapped_base_address == nullptr) { 1285 result = MAP_ARCHIVE_MMAP_FAILURE; 1286 log_debug(cds)("Failed to reserve spaces (use_requested_addr=%u)", (unsigned)use_requested_addr); 1287 } else { 1288 1289 if (Metaspace::using_class_space()) { 1290 prot_zone_size = protection_zone_size(); 1291 } 1292 1293 #ifdef ASSERT 1294 // Some sanity checks after reserving address spaces for archives 1295 // and class space. 1296 assert(archive_space_rs.is_reserved(), "Sanity"); 1297 if (Metaspace::using_class_space()) { 1298 assert(archive_space_rs.base() == mapped_base_address && 1299 archive_space_rs.size() > protection_zone_size(), 1300 "Archive space must lead and include the protection zone"); 1301 // Class space must closely follow the archive space. Both spaces 1302 // must be aligned correctly. 1303 assert(class_space_rs.is_reserved() && class_space_rs.size() > 0, 1304 "A class space should have been reserved"); 1305 assert(class_space_rs.base() >= archive_space_rs.end(), 1306 "class space should follow the cds archive space"); 1307 assert(is_aligned(archive_space_rs.base(), 1308 core_region_alignment()), 1309 "Archive space misaligned"); 1310 assert(is_aligned(class_space_rs.base(), 1311 Metaspace::reserve_alignment()), 1312 "class space misaligned"); 1313 } 1314 #endif // ASSERT 1315 1316 log_info(cds)("Reserved archive_space_rs [" INTPTR_FORMAT " - " INTPTR_FORMAT "] (%zu) bytes%s", 1317 p2i(archive_space_rs.base()), p2i(archive_space_rs.end()), archive_space_rs.size(), 1318 (prot_zone_size > 0 ? " (includes protection zone)" : "")); 1319 log_info(cds)("Reserved class_space_rs [" INTPTR_FORMAT " - " INTPTR_FORMAT "] (%zu) bytes", 1320 p2i(class_space_rs.base()), p2i(class_space_rs.end()), class_space_rs.size()); 1321 1322 if (MetaspaceShared::use_windows_memory_mapping()) { 1323 // We have now reserved address space for the archives, and will map in 1324 // the archive files into this space. 1325 // 1326 // Special handling for Windows: on Windows we cannot map a file view 1327 // into an existing memory mapping. So, we unmap the address range we 1328 // just reserved again, which will make it available for mapping the 1329 // archives. 1330 // Reserving this range has not been for naught however since it makes 1331 // us reasonably sure the address range is available. 1332 // 1333 // But still it may fail, since between unmapping the range and mapping 1334 // in the archive someone else may grab the address space. Therefore 1335 // there is a fallback in FileMap::map_region() where we just read in 1336 // the archive files sequentially instead of mapping it in. We couple 1337 // this with use_requested_addr, since we're going to patch all the 1338 // pointers anyway so there's no benefit to mmap. 1339 if (use_requested_addr) { 1340 assert(!total_space_rs.is_reserved(), "Should not be reserved for Windows"); 1341 log_info(cds)("Windows mmap workaround: releasing archive space."); 1342 MemoryReserver::release(archive_space_rs); 1343 // Mark as not reserved 1344 archive_space_rs = {}; 1345 // The protection zone is part of the archive: 1346 // See comment above, the Windows way of loading CDS is to mmap the individual 1347 // parts of the archive into the address region we just vacated. The protection 1348 // zone will not be mapped (and, in fact, does not exist as physical region in 1349 // the archive). Therefore, after removing the archive space above, we must 1350 // re-reserve the protection zone part lest something else gets mapped into that 1351 // area later. 1352 if (prot_zone_size > 0) { 1353 assert(prot_zone_size >= os::vm_allocation_granularity(), "must be"); // not just page size! 1354 char* p = os::attempt_reserve_memory_at(mapped_base_address, prot_zone_size, 1355 false, MemTag::mtClassShared); 1356 assert(p == mapped_base_address || p == nullptr, "must be"); 1357 if (p == nullptr) { 1358 log_debug(cds)("Failed to re-reserve protection zone"); 1359 return MAP_ARCHIVE_MMAP_FAILURE; 1360 } 1361 } 1362 } 1363 } 1364 1365 if (prot_zone_size > 0) { 1366 os::commit_memory(mapped_base_address, prot_zone_size, false); // will later be protected 1367 // Before mapping the core regions into the newly established address space, we mark 1368 // start and the end of the future protection zone with canaries. That way we easily 1369 // catch mapping errors (accidentally mapping data into the future protection zone). 1370 *(mapped_base_address) = 'P'; 1371 *(mapped_base_address + prot_zone_size - 1) = 'P'; 1372 } 1373 1374 MapArchiveResult static_result = map_archive(static_mapinfo, mapped_base_address, archive_space_rs); 1375 MapArchiveResult dynamic_result = (static_result == MAP_ARCHIVE_SUCCESS) ? 1376 map_archive(dynamic_mapinfo, mapped_base_address, archive_space_rs) : MAP_ARCHIVE_OTHER_FAILURE; 1377 1378 DEBUG_ONLY(if (ArchiveRelocationMode == 1 && use_requested_addr) { 1379 // This is for simulating mmap failures at the requested address. In 1380 // debug builds, we do it here (after all archives have possibly been 1381 // mapped), so we can thoroughly test the code for failure handling 1382 // (releasing all allocated resource, etc). 1383 log_info(cds)("ArchiveRelocationMode == 1: always map archive(s) at an alternative address"); 1384 if (static_result == MAP_ARCHIVE_SUCCESS) { 1385 static_result = MAP_ARCHIVE_MMAP_FAILURE; 1386 } 1387 if (dynamic_result == MAP_ARCHIVE_SUCCESS) { 1388 dynamic_result = MAP_ARCHIVE_MMAP_FAILURE; 1389 } 1390 }); 1391 1392 if (static_result == MAP_ARCHIVE_SUCCESS) { 1393 if (dynamic_result == MAP_ARCHIVE_SUCCESS) { 1394 result = MAP_ARCHIVE_SUCCESS; 1395 } else if (dynamic_result == MAP_ARCHIVE_OTHER_FAILURE) { 1396 assert(dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped(), "must have failed"); 1397 // No need to retry mapping the dynamic archive again, as it will never succeed 1398 // (bad file, etc) -- just keep the base archive. 1399 log_warning(cds, dynamic)("Unable to use shared archive. The top archive failed to load: %s", 1400 dynamic_mapinfo->full_path()); 1401 result = MAP_ARCHIVE_SUCCESS; 1402 // TODO, we can give the unused space for the dynamic archive to class_space_rs, but there's no 1403 // easy API to do that right now. 1404 } else { 1405 result = MAP_ARCHIVE_MMAP_FAILURE; 1406 } 1407 } else if (static_result == MAP_ARCHIVE_OTHER_FAILURE) { 1408 result = MAP_ARCHIVE_OTHER_FAILURE; 1409 } else { 1410 result = MAP_ARCHIVE_MMAP_FAILURE; 1411 } 1412 } 1413 1414 if (result == MAP_ARCHIVE_SUCCESS) { 1415 SharedBaseAddress = (size_t)mapped_base_address; 1416 #ifdef _LP64 1417 if (Metaspace::using_class_space()) { 1418 assert(prot_zone_size > 0 && 1419 *(mapped_base_address) == 'P' && 1420 *(mapped_base_address + prot_zone_size - 1) == 'P', 1421 "Protection zone was overwritten?"); 1422 // Set up ccs in metaspace. 1423 Metaspace::initialize_class_space(class_space_rs); 1424 1425 // Set up compressed Klass pointer encoding: the encoding range must 1426 // cover both archive and class space. 1427 const address encoding_base = (address)mapped_base_address; 1428 const address klass_range_start = encoding_base + prot_zone_size; 1429 const size_t klass_range_size = (address)class_space_rs.end() - klass_range_start; 1430 if (INCLUDE_CDS_JAVA_HEAP || UseCompactObjectHeaders) { 1431 // The CDS archive may contain narrow Klass IDs that were precomputed at archive generation time: 1432 // - every archived java object header (only if INCLUDE_CDS_JAVA_HEAP) 1433 // - every archived Klass' prototype (only if +UseCompactObjectHeaders) 1434 // 1435 // In order for those IDs to still be valid, we need to dictate base and shift: base should be the 1436 // mapping start (including protection zone), shift should be the shift used at archive generation time. 1437 CompressedKlassPointers::initialize_for_given_encoding( 1438 klass_range_start, klass_range_size, 1439 encoding_base, ArchiveBuilder::precomputed_narrow_klass_shift() // precomputed encoding, see ArchiveBuilder 1440 ); 1441 } else { 1442 // Let JVM freely choose encoding base and shift 1443 CompressedKlassPointers::initialize(klass_range_start, klass_range_size); 1444 } 1445 CompressedKlassPointers::establish_protection_zone(encoding_base, prot_zone_size); 1446 1447 // map_or_load_heap_region() compares the current narrow oop and klass encodings 1448 // with the archived ones, so it must be done after all encodings are determined. 1449 static_mapinfo->map_or_load_heap_region(); 1450 } 1451 #endif // _LP64 1452 log_info(cds)("initial optimized module handling: %s", CDSConfig::is_using_optimized_module_handling() ? "enabled" : "disabled"); 1453 log_info(cds)("initial full module graph: %s", CDSConfig::is_using_full_module_graph() ? "enabled" : "disabled"); 1454 } else { 1455 unmap_archive(static_mapinfo); 1456 unmap_archive(dynamic_mapinfo); 1457 release_reserved_spaces(total_space_rs, archive_space_rs, class_space_rs); 1458 } 1459 1460 return result; 1461 } 1462 1463 1464 // This will reserve two address spaces suitable to house Klass structures, one 1465 // for the cds archives (static archive and optionally dynamic archive) and 1466 // optionally one move for ccs. 1467 // 1468 // Since both spaces must fall within the compressed class pointer encoding 1469 // range, they are allocated close to each other. 1470 // 1471 // Space for archives will be reserved first, followed by a potential gap, 1472 // followed by the space for ccs: 1473 // 1474 // +-- Base address A B End 1475 // | | | | 1476 // v v v v 1477 // +-------------+--------------+ +----------------------+ 1478 // | static arc | [dyn. arch] | [gap] | compr. class space | 1479 // +-------------+--------------+ +----------------------+ 1480 // 1481 // (The gap may result from different alignment requirements between metaspace 1482 // and CDS) 1483 // 1484 // If UseCompressedClassPointers is disabled, only one address space will be 1485 // reserved: 1486 // 1487 // +-- Base address End 1488 // | | 1489 // v v 1490 // +-------------+--------------+ 1491 // | static arc | [dyn. arch] | 1492 // +-------------+--------------+ 1493 // 1494 // Base address: If use_archive_base_addr address is true, the Base address is 1495 // determined by the address stored in the static archive. If 1496 // use_archive_base_addr address is false, this base address is determined 1497 // by the platform. 1498 // 1499 // If UseCompressedClassPointers=1, the range encompassing both spaces will be 1500 // suitable to en/decode narrow Klass pointers: the base will be valid for 1501 // encoding, the range [Base, End) and not surpass the max. range for that encoding. 1502 // 1503 // Return: 1504 // 1505 // - On success: 1506 // - total_space_rs will be reserved as whole for archive_space_rs and 1507 // class_space_rs if UseCompressedClassPointers is true. 1508 // On Windows, try reserve archive_space_rs and class_space_rs 1509 // separately first if use_archive_base_addr is true. 1510 // - archive_space_rs will be reserved and large enough to host static and 1511 // if needed dynamic archive: [Base, A). 1512 // archive_space_rs.base and size will be aligned to CDS reserve 1513 // granularity. 1514 // - class_space_rs: If UseCompressedClassPointers=1, class_space_rs will 1515 // be reserved. Its start address will be aligned to metaspace reserve 1516 // alignment, which may differ from CDS alignment. It will follow the cds 1517 // archive space, close enough such that narrow class pointer encoding 1518 // covers both spaces. 1519 // If UseCompressedClassPointers=0, class_space_rs remains unreserved. 1520 // - On error: null is returned and the spaces remain unreserved. 1521 char* MetaspaceShared::reserve_address_space_for_archives(FileMapInfo* static_mapinfo, 1522 FileMapInfo* dynamic_mapinfo, 1523 bool use_archive_base_addr, 1524 ReservedSpace& total_space_rs, 1525 ReservedSpace& archive_space_rs, 1526 ReservedSpace& class_space_rs) { 1527 1528 address const base_address = (address) (use_archive_base_addr ? static_mapinfo->requested_base_address() : nullptr); 1529 const size_t archive_space_alignment = core_region_alignment(); 1530 1531 // Size and requested location of the archive_space_rs (for both static and dynamic archives) 1532 size_t archive_end_offset = (dynamic_mapinfo == nullptr) ? static_mapinfo->mapping_end_offset() : dynamic_mapinfo->mapping_end_offset(); 1533 size_t archive_space_size = align_up(archive_end_offset, archive_space_alignment); 1534 1535 if (!Metaspace::using_class_space()) { 1536 // Get the simple case out of the way first: 1537 // no compressed class space, simple allocation. 1538 1539 // When running without class space, requested archive base should be aligned to cds core alignment. 1540 assert(is_aligned(base_address, archive_space_alignment), 1541 "Archive base address unaligned: " PTR_FORMAT ", needs alignment: %zu.", 1542 p2i(base_address), archive_space_alignment); 1543 1544 archive_space_rs = MemoryReserver::reserve((char*)base_address, 1545 archive_space_size, 1546 archive_space_alignment, 1547 os::vm_page_size()); 1548 if (archive_space_rs.is_reserved()) { 1549 assert(base_address == nullptr || 1550 (address)archive_space_rs.base() == base_address, "Sanity"); 1551 // Register archive space with NMT. 1552 MemTracker::record_virtual_memory_tag(archive_space_rs, mtClassShared); 1553 return archive_space_rs.base(); 1554 } 1555 return nullptr; 1556 } 1557 1558 #ifdef _LP64 1559 1560 // Complex case: two spaces adjacent to each other, both to be addressable 1561 // with narrow class pointers. 1562 // We reserve the whole range spanning both spaces, then split that range up. 1563 1564 const size_t class_space_alignment = Metaspace::reserve_alignment(); 1565 1566 // When running with class space, requested archive base must satisfy both cds core alignment 1567 // and class space alignment. 1568 const size_t base_address_alignment = MAX2(class_space_alignment, archive_space_alignment); 1569 assert(is_aligned(base_address, base_address_alignment), 1570 "Archive base address unaligned: " PTR_FORMAT ", needs alignment: %zu.", 1571 p2i(base_address), base_address_alignment); 1572 1573 size_t class_space_size = CompressedClassSpaceSize; 1574 assert(CompressedClassSpaceSize > 0 && 1575 is_aligned(CompressedClassSpaceSize, class_space_alignment), 1576 "CompressedClassSpaceSize malformed: %zu", CompressedClassSpaceSize); 1577 1578 const size_t ccs_begin_offset = align_up(archive_space_size, class_space_alignment); 1579 const size_t gap_size = ccs_begin_offset - archive_space_size; 1580 1581 // Reduce class space size if it would not fit into the Klass encoding range 1582 constexpr size_t max_encoding_range_size = 4 * G; 1583 guarantee(archive_space_size < max_encoding_range_size - class_space_alignment, "Archive too large"); 1584 if ((archive_space_size + gap_size + class_space_size) > max_encoding_range_size) { 1585 class_space_size = align_down(max_encoding_range_size - archive_space_size - gap_size, class_space_alignment); 1586 log_info(metaspace)("CDS initialization: reducing class space size from %zu to %zu", 1587 CompressedClassSpaceSize, class_space_size); 1588 FLAG_SET_ERGO(CompressedClassSpaceSize, class_space_size); 1589 } 1590 1591 const size_t total_range_size = 1592 archive_space_size + gap_size + class_space_size; 1593 1594 // Test that class space base address plus shift can be decoded by aarch64, when restored. 1595 const int precomputed_narrow_klass_shift = ArchiveBuilder::precomputed_narrow_klass_shift(); 1596 if (!CompressedKlassPointers::check_klass_decode_mode(base_address, precomputed_narrow_klass_shift, 1597 total_range_size)) { 1598 log_info(cds)("CDS initialization: Cannot use SharedBaseAddress " PTR_FORMAT " with precomputed shift %d.", 1599 p2i(base_address), precomputed_narrow_klass_shift); 1600 use_archive_base_addr = false; 1601 } 1602 1603 assert(total_range_size > ccs_begin_offset, "must be"); 1604 if (use_windows_memory_mapping() && use_archive_base_addr) { 1605 if (base_address != nullptr) { 1606 // On Windows, we cannot safely split a reserved memory space into two (see JDK-8255917). 1607 // Hence, we optimistically reserve archive space and class space side-by-side. We only 1608 // do this for use_archive_base_addr=true since for use_archive_base_addr=false case 1609 // caller will not split the combined space for mapping, instead read the archive data 1610 // via sequential file IO. 1611 address ccs_base = base_address + archive_space_size + gap_size; 1612 archive_space_rs = MemoryReserver::reserve((char*)base_address, 1613 archive_space_size, 1614 archive_space_alignment, 1615 os::vm_page_size()); 1616 class_space_rs = MemoryReserver::reserve((char*)ccs_base, 1617 class_space_size, 1618 class_space_alignment, 1619 os::vm_page_size()); 1620 } 1621 if (!archive_space_rs.is_reserved() || !class_space_rs.is_reserved()) { 1622 release_reserved_spaces(total_space_rs, archive_space_rs, class_space_rs); 1623 return nullptr; 1624 } 1625 MemTracker::record_virtual_memory_tag(archive_space_rs, mtClassShared); 1626 MemTracker::record_virtual_memory_tag(class_space_rs, mtClass); 1627 } else { 1628 if (use_archive_base_addr && base_address != nullptr) { 1629 total_space_rs = MemoryReserver::reserve((char*) base_address, 1630 total_range_size, 1631 base_address_alignment, 1632 os::vm_page_size()); 1633 } else { 1634 // We did not manage to reserve at the preferred address, or were instructed to relocate. In that 1635 // case we reserve wherever possible, but the start address needs to be encodable as narrow Klass 1636 // encoding base since the archived heap objects contain narrow Klass IDs pre-calculated toward the start 1637 // of the shared Metaspace. That prevents us from using zero-based encoding and therefore we won't 1638 // try allocating in low-address regions. 1639 total_space_rs = Metaspace::reserve_address_space_for_compressed_classes(total_range_size, false /* optimize_for_zero_base */); 1640 } 1641 1642 if (!total_space_rs.is_reserved()) { 1643 return nullptr; 1644 } 1645 1646 // Paranoid checks: 1647 assert(!use_archive_base_addr || (address)total_space_rs.base() == base_address, 1648 "Sanity (" PTR_FORMAT " vs " PTR_FORMAT ")", p2i(base_address), p2i(total_space_rs.base())); 1649 assert(is_aligned(total_space_rs.base(), base_address_alignment), "Sanity"); 1650 assert(total_space_rs.size() == total_range_size, "Sanity"); 1651 1652 // Now split up the space into ccs and cds archive. For simplicity, just leave 1653 // the gap reserved at the end of the archive space. Do not do real splitting. 1654 archive_space_rs = total_space_rs.first_part(ccs_begin_offset, 1655 (size_t)archive_space_alignment); 1656 class_space_rs = total_space_rs.last_part(ccs_begin_offset); 1657 MemTracker::record_virtual_memory_split_reserved(total_space_rs.base(), total_space_rs.size(), 1658 ccs_begin_offset, mtClassShared, mtClass); 1659 } 1660 assert(is_aligned(archive_space_rs.base(), archive_space_alignment), "Sanity"); 1661 assert(is_aligned(archive_space_rs.size(), archive_space_alignment), "Sanity"); 1662 assert(is_aligned(class_space_rs.base(), class_space_alignment), "Sanity"); 1663 assert(is_aligned(class_space_rs.size(), class_space_alignment), "Sanity"); 1664 1665 1666 return archive_space_rs.base(); 1667 1668 #else 1669 ShouldNotReachHere(); 1670 return nullptr; 1671 #endif 1672 1673 } 1674 1675 void MetaspaceShared::release_reserved_spaces(ReservedSpace& total_space_rs, 1676 ReservedSpace& archive_space_rs, 1677 ReservedSpace& class_space_rs) { 1678 if (total_space_rs.is_reserved()) { 1679 log_debug(cds)("Released shared space (archive + class) " INTPTR_FORMAT, p2i(total_space_rs.base())); 1680 MemoryReserver::release(total_space_rs); 1681 total_space_rs = {}; 1682 } else { 1683 if (archive_space_rs.is_reserved()) { 1684 log_debug(cds)("Released shared space (archive) " INTPTR_FORMAT, p2i(archive_space_rs.base())); 1685 MemoryReserver::release(archive_space_rs); 1686 archive_space_rs = {}; 1687 } 1688 if (class_space_rs.is_reserved()) { 1689 log_debug(cds)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base())); 1690 MemoryReserver::release(class_space_rs); 1691 class_space_rs = {}; 1692 } 1693 } 1694 } 1695 1696 static int archive_regions[] = { MetaspaceShared::rw, MetaspaceShared::ro }; 1697 static int archive_regions_count = 2; 1698 1699 MapArchiveResult MetaspaceShared::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) { 1700 assert(CDSConfig::is_using_archive(), "must be runtime"); 1701 if (mapinfo == nullptr) { 1702 return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded. 1703 } 1704 1705 mapinfo->set_is_mapped(false); 1706 if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) { 1707 log_info(cds)("Unable to map CDS archive -- core_region_alignment() expected: %zu" 1708 " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment()); 1709 return MAP_ARCHIVE_OTHER_FAILURE; 1710 } 1711 1712 MapArchiveResult result = 1713 mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs); 1714 1715 if (result != MAP_ARCHIVE_SUCCESS) { 1716 unmap_archive(mapinfo); 1717 return result; 1718 } 1719 1720 if (!mapinfo->validate_class_location()) { 1721 unmap_archive(mapinfo); 1722 return MAP_ARCHIVE_OTHER_FAILURE; 1723 } 1724 1725 if (mapinfo->is_static()) { 1726 // Currently, only static archive uses early serialized data. 1727 char* buffer = mapinfo->early_serialized_data(); 1728 intptr_t* array = (intptr_t*)buffer; 1729 ReadClosure rc(&array, (intptr_t)mapped_base_address); 1730 early_serialize(&rc); 1731 } 1732 1733 if (!mapinfo->validate_aot_class_linking()) { 1734 unmap_archive(mapinfo); 1735 return MAP_ARCHIVE_OTHER_FAILURE; 1736 } 1737 1738 mapinfo->set_is_mapped(true); 1739 return MAP_ARCHIVE_SUCCESS; 1740 } 1741 1742 void MetaspaceShared::unmap_archive(FileMapInfo* mapinfo) { 1743 assert(CDSConfig::is_using_archive(), "must be runtime"); 1744 if (mapinfo != nullptr) { 1745 mapinfo->unmap_regions(archive_regions, archive_regions_count); 1746 mapinfo->unmap_region(MetaspaceShared::bm); 1747 mapinfo->set_is_mapped(false); 1748 } 1749 } 1750 1751 // For -XX:PrintSharedArchiveAndExit 1752 class CountSharedSymbols : public SymbolClosure { 1753 private: 1754 int _count; 1755 public: 1756 CountSharedSymbols() : _count(0) {} 1757 void do_symbol(Symbol** sym) { 1758 _count++; 1759 } 1760 int total() { return _count; } 1761 1762 }; 1763 1764 // Read the miscellaneous data from the shared file, and 1765 // serialize it out to its various destinations. 1766 1767 void MetaspaceShared::initialize_shared_spaces() { 1768 FileMapInfo *static_mapinfo = FileMapInfo::current_info(); 1769 1770 // Verify various attributes of the archive, plus initialize the 1771 // shared string/symbol tables. 1772 char* buffer = static_mapinfo->serialized_data(); 1773 intptr_t* array = (intptr_t*)buffer; 1774 ReadClosure rc(&array, (intptr_t)SharedBaseAddress); 1775 serialize(&rc); 1776 1777 // Finish up archived heap initialization. These must be 1778 // done after ReadClosure. 1779 static_mapinfo->patch_heap_embedded_pointers(); 1780 ArchiveHeapLoader::finish_initialization(); 1781 Universe::load_archived_object_instances(); 1782 1783 // Close the mapinfo file 1784 static_mapinfo->close(); 1785 1786 static_mapinfo->unmap_region(MetaspaceShared::bm); 1787 1788 FileMapInfo *dynamic_mapinfo = FileMapInfo::dynamic_info(); 1789 if (dynamic_mapinfo != nullptr) { 1790 intptr_t* buffer = (intptr_t*)dynamic_mapinfo->serialized_data(); 1791 ReadClosure rc(&buffer, (intptr_t)SharedBaseAddress); 1792 ArchiveBuilder::serialize_dynamic_archivable_items(&rc); 1793 DynamicArchive::setup_array_klasses(); 1794 dynamic_mapinfo->close(); 1795 dynamic_mapinfo->unmap_region(MetaspaceShared::bm); 1796 } 1797 1798 LogStreamHandle(Info, cds) lsh; 1799 if (lsh.is_enabled()) { 1800 lsh.print("Using AOT-linked classes: %s (static archive: %s aot-linked classes", 1801 BOOL_TO_STR(CDSConfig::is_using_aot_linked_classes()), 1802 static_mapinfo->header()->has_aot_linked_classes() ? "has" : "no"); 1803 if (dynamic_mapinfo != nullptr) { 1804 lsh.print(", dynamic archive: %s aot-linked classes", 1805 dynamic_mapinfo->header()->has_aot_linked_classes() ? "has" : "no"); 1806 } 1807 lsh.print_cr(")"); 1808 } 1809 1810 // Set up LambdaFormInvokers::_lambdaform_lines for dynamic dump 1811 if (CDSConfig::is_dumping_dynamic_archive()) { 1812 // Read stored LF format lines stored in static archive 1813 LambdaFormInvokers::read_static_archive_invokers(); 1814 } 1815 1816 if (PrintSharedArchiveAndExit) { 1817 // Print archive names 1818 if (dynamic_mapinfo != nullptr) { 1819 tty->print_cr("\n\nBase archive name: %s", CDSConfig::input_static_archive_path()); 1820 tty->print_cr("Base archive version %d", static_mapinfo->version()); 1821 } else { 1822 tty->print_cr("Static archive name: %s", static_mapinfo->full_path()); 1823 tty->print_cr("Static archive version %d", static_mapinfo->version()); 1824 } 1825 1826 SystemDictionaryShared::print_shared_archive(tty); 1827 if (dynamic_mapinfo != nullptr) { 1828 tty->print_cr("\n\nDynamic archive name: %s", dynamic_mapinfo->full_path()); 1829 tty->print_cr("Dynamic archive version %d", dynamic_mapinfo->version()); 1830 SystemDictionaryShared::print_shared_archive(tty, false/*dynamic*/); 1831 } 1832 1833 // collect shared symbols and strings 1834 CountSharedSymbols cl; 1835 SymbolTable::shared_symbols_do(&cl); 1836 tty->print_cr("Number of shared symbols: %d", cl.total()); 1837 tty->print_cr("Number of shared strings: %zu", StringTable::shared_entry_count()); 1838 tty->print_cr("VM version: %s\r\n", static_mapinfo->vm_version()); 1839 if (FileMapInfo::current_info() == nullptr || _archive_loading_failed) { 1840 tty->print_cr("archive is invalid"); 1841 vm_exit(1); 1842 } else { 1843 tty->print_cr("archive is valid"); 1844 vm_exit(0); 1845 } 1846 } 1847 } 1848 1849 // JVM/TI RedefineClasses() support: 1850 bool MetaspaceShared::remap_shared_readonly_as_readwrite() { 1851 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 1852 1853 if (CDSConfig::is_using_archive()) { 1854 // remap the shared readonly space to shared readwrite, private 1855 FileMapInfo* mapinfo = FileMapInfo::current_info(); 1856 if (!mapinfo->remap_shared_readonly_as_readwrite()) { 1857 return false; 1858 } 1859 if (FileMapInfo::dynamic_info() != nullptr) { 1860 mapinfo = FileMapInfo::dynamic_info(); 1861 if (!mapinfo->remap_shared_readonly_as_readwrite()) { 1862 return false; 1863 } 1864 } 1865 _remapped_readwrite = true; 1866 } 1867 return true; 1868 } 1869 1870 void MetaspaceShared::print_on(outputStream* st) { 1871 if (CDSConfig::is_using_archive()) { 1872 st->print("CDS archive(s) mapped at: "); 1873 address base = (address)MetaspaceObj::shared_metaspace_base(); 1874 address static_top = (address)_shared_metaspace_static_top; 1875 address top = (address)MetaspaceObj::shared_metaspace_top(); 1876 st->print("[" PTR_FORMAT "-" PTR_FORMAT "-" PTR_FORMAT "), ", p2i(base), p2i(static_top), p2i(top)); 1877 st->print("size %zu, ", top - base); 1878 st->print("SharedBaseAddress: " PTR_FORMAT ", ArchiveRelocationMode: %d.", SharedBaseAddress, ArchiveRelocationMode); 1879 } else { 1880 st->print("CDS archive(s) not mapped"); 1881 } 1882 st->cr(); 1883 }