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