< prev index next >

src/hotspot/share/cds/metaspaceShared.cpp

Print this page

   1 /*
   2  * Copyright (c) 2012, 2023, 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/instanceMirrorKlass.hpp"
  71 #include "oops/klass.inline.hpp"

  72 #include "oops/objArrayOop.hpp"
  73 #include "oops/oop.inline.hpp"
  74 #include "oops/oopHandle.hpp"

  75 #include "prims/jvmtiExport.hpp"

  76 #include "runtime/arguments.hpp"
  77 #include "runtime/globals.hpp"
  78 #include "runtime/globals_extension.hpp"
  79 #include "runtime/handles.inline.hpp"

  80 #include "runtime/os.inline.hpp"
  81 #include "runtime/safepointVerifiers.hpp"
  82 #include "runtime/sharedRuntime.hpp"
  83 #include "runtime/vmOperations.hpp"
  84 #include "runtime/vmThread.hpp"
  85 #include "sanitizers/leak.hpp"
  86 #include "utilities/align.hpp"
  87 #include "utilities/bitMap.inline.hpp"
  88 #include "utilities/defaultStream.hpp"
  89 #include "utilities/ostream.hpp"
  90 #include "utilities/resourceHash.hpp"
  91 
  92 ReservedSpace MetaspaceShared::_symbol_rs;
  93 VirtualSpace MetaspaceShared::_symbol_vs;
  94 bool MetaspaceShared::_archive_loading_failed = false;
  95 bool MetaspaceShared::_remapped_readwrite = false;
  96 void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
  97 intx MetaspaceShared::_relocation_delta;
  98 char* MetaspaceShared::_requested_base_address;
  99 bool MetaspaceShared::_use_optimized_module_handling = true;

 100 
 101 // The CDS archive is divided into the following regions:
 102 //     rw  - read-write metadata
 103 //     ro  - read-only metadata and read-only tables
 104 //     hp  - heap region
 105 //     bm  - bitmap for relocating the above 7 regions.
 106 //
 107 // The rw and ro regions are linearly allocated, in the order of rw->ro.
 108 // These regions are aligned with MetaspaceShared::core_region_alignment().
 109 //
 110 // These 2 regions are populated in the following steps:
 111 // [0] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
 112 //     temporarily allocated outside of the shared regions.
 113 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
 114 // [2] C++ vtables are copied into the rw region.
 115 // [3] ArchiveBuilder copies RW metadata into the rw region.
 116 // [4] ArchiveBuilder copies RO metadata into the ro region.
 117 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
 118 //     are copied into the ro region as read-only tables.
 119 //

 282   if (UseSharedSpaces) {
 283     int size = FileMapInfo::get_number_of_shared_paths();
 284     if (size > 0) {
 285       CDSProtectionDomain::allocate_shared_data_arrays(size, CHECK);
 286       if (!CDSConfig::is_dumping_dynamic_archive()) {
 287         FileMapInfo* info;
 288         if (FileMapInfo::dynamic_info() == nullptr) {
 289           info = FileMapInfo::current_info();
 290         } else {
 291           info = FileMapInfo::dynamic_info();
 292         }
 293         ClassLoaderExt::init_paths_start_index(info->app_class_paths_start_index());
 294         ClassLoaderExt::init_app_module_paths_start_index(info->app_module_paths_start_index());
 295       }
 296     }
 297   }
 298 }
 299 
 300 static GrowableArrayCHeap<OopHandle, mtClassShared>* _extra_interned_strings = nullptr;
 301 static GrowableArrayCHeap<Symbol*, mtClassShared>* _extra_symbols = nullptr;

 302 
 303 void MetaspaceShared::read_extra_data(JavaThread* current, const char* filename) {
 304   _extra_interned_strings = new GrowableArrayCHeap<OopHandle, mtClassShared>(10000);
 305   _extra_symbols = new GrowableArrayCHeap<Symbol*, mtClassShared>(1000);
 306 
 307   HashtableTextDump reader(filename);
 308   reader.check_version("VERSION: 1.0");
 309 
 310   while (reader.remain() > 0) {
 311     int utf8_length;
 312     int prefix_type = reader.scan_prefix(&utf8_length);
 313     ResourceMark rm(current);
 314     if (utf8_length == 0x7fffffff) {
 315       // buf_len will overflown 32-bit value.
 316       log_error(cds)("string length too large: %d", utf8_length);
 317       MetaspaceShared::unrecoverable_loading_error();
 318     }
 319     int buf_len = utf8_length+1;
 320     char* utf8_buffer = NEW_RESOURCE_ARRAY(char, buf_len);
 321     reader.get_utf8(utf8_buffer, utf8_length);

 332       if (HAS_PENDING_EXCEPTION) {
 333         log_warning(cds, heap)("[line %d] extra interned string allocation failed; size too large: %d",
 334                                reader.last_line_no(), utf8_length);
 335         CLEAR_PENDING_EXCEPTION;
 336       } else {
 337 #if INCLUDE_CDS_JAVA_HEAP
 338         if (ArchiveHeapWriter::is_string_too_large_to_archive(str)) {
 339           log_warning(cds, heap)("[line %d] extra interned string ignored; size too large: %d",
 340                                  reader.last_line_no(), utf8_length);
 341           continue;
 342         }
 343         // Make sure this string is included in the dumped interned string table.
 344         assert(str != nullptr, "must succeed");
 345         _extra_interned_strings->append(OopHandle(Universe::vm_global(), str));
 346 #endif
 347       }
 348     }
 349   }
 350 }
 351 



















 352 // Read/write a data stream for restoring/preserving metadata pointers and
 353 // miscellaneous data from/to the shared archive file.
 354 
 355 void MetaspaceShared::serialize(SerializeClosure* soc) {
 356   int tag = 0;
 357   soc->do_tag(--tag);
 358 
 359   // Verify the sizes of various metadata in the system.
 360   soc->do_tag(sizeof(Method));
 361   soc->do_tag(sizeof(ConstMethod));
 362   soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE));
 363   soc->do_tag(sizeof(ConstantPool));
 364   soc->do_tag(sizeof(ConstantPoolCache));
 365   soc->do_tag(objArrayOopDesc::base_offset_in_bytes());
 366   soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE));
 367   soc->do_tag(sizeof(Symbol));
 368 
 369   // Need to do this first, as subsequent steps may call virtual functions
 370   // in archived Metadata objects.
 371   CppVtables::serialize(soc);
 372   soc->do_tag(--tag);
 373 
 374   // Dump/restore miscellaneous metadata.
 375   JavaClasses::serialize_offsets(soc);

 376   Universe::serialize(soc);
 377   soc->do_tag(--tag);
 378 
 379   // Dump/restore references to commonly used names and signatures.
 380   vmSymbols::serialize(soc);
 381   soc->do_tag(--tag);
 382 
 383   // Dump/restore the symbol/string/subgraph_info tables
 384   SymbolTable::serialize_shared_table_header(soc);
 385   StringTable::serialize_shared_table_header(soc);
 386   HeapShared::serialize_tables(soc);
 387   SystemDictionaryShared::serialize_dictionary_headers(soc);
 388 

 389   InstanceMirrorKlass::serialize_offsets(soc);
 390 
 391   // Dump/restore well known classes (pointers)
 392   SystemDictionaryShared::serialize_vm_classes(soc);
 393   soc->do_tag(--tag);
 394 
 395   CDS_JAVA_HEAP_ONLY(Modules::serialize(soc);)
 396   CDS_JAVA_HEAP_ONLY(ClassLoaderDataShared::serialize(soc);)


 397 
 398   LambdaFormInvokers::serialize(soc);
 399   soc->do_tag(666);
 400 }
 401 
 402 static void rewrite_nofast_bytecode(const methodHandle& method) {
 403   BytecodeStream bcs(method);
 404   while (!bcs.is_last_bytecode()) {
 405     Bytecodes::Code opcode = bcs.next();
 406     switch (opcode) {
 407     case Bytecodes::_getfield:      *bcs.bcp() = Bytecodes::_nofast_getfield;      break;
 408     case Bytecodes::_putfield:      *bcs.bcp() = Bytecodes::_nofast_putfield;      break;
 409     case Bytecodes::_aload_0:       *bcs.bcp() = Bytecodes::_nofast_aload_0;       break;
 410     case Bytecodes::_iload: {
 411       if (!bcs.is_wide()) {
 412         *bcs.bcp() = Bytecodes::_nofast_iload;
 413       }
 414       break;
 415     }
 416     default: break;

 419 }
 420 
 421 // [1] Rewrite all bytecodes as needed, so that the ConstMethod* will not be modified
 422 //     at run time by RewriteBytecodes/RewriteFrequentPairs
 423 // [2] Assign a fingerprint, so one doesn't need to be assigned at run-time.
 424 void MetaspaceShared::rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread* thread, InstanceKlass* ik) {
 425   for (int i = 0; i < ik->methods()->length(); i++) {
 426     methodHandle m(thread, ik->methods()->at(i));
 427     if (ik->can_be_verified_at_dumptime() && ik->is_linked()) {
 428       rewrite_nofast_bytecode(m);
 429     }
 430     Fingerprinter fp(m);
 431     // The side effect of this call sets method's fingerprint field.
 432     fp.fingerprint();
 433   }
 434 }
 435 
 436 class VM_PopulateDumpSharedSpace : public VM_Operation {
 437 private:
 438   ArchiveHeapInfo _heap_info;
 439 
 440   void dump_java_heap_objects(GrowableArray<Klass*>* klasses) NOT_CDS_JAVA_HEAP_RETURN;
 441   void dump_shared_symbol_table(GrowableArray<Symbol*>* symbols) {
 442     log_info(cds)("Dumping symbol table ...");
 443     SymbolTable::write_to_archive(symbols);
 444   }
 445   char* dump_read_only_tables();
 446 

 447 public:
 448 
 449   VM_PopulateDumpSharedSpace() : VM_Operation(), _heap_info() {}

 450 
 451   bool skip_operation() const { return false; }
 452 
 453   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
 454   void doit();   // outline because gdb sucks
 455   bool allow_nested_vm_operations() const { return true; }


 456 }; // class VM_PopulateDumpSharedSpace
 457 
 458 class StaticArchiveBuilder : public ArchiveBuilder {
 459 public:
 460   StaticArchiveBuilder() : ArchiveBuilder() {}
 461 
 462   virtual void iterate_roots(MetaspaceClosure* it) {
 463     FileMapInfo::metaspace_pointers_do(it);
 464     SystemDictionaryShared::dumptime_classes_do(it);
 465     Universe::metaspace_pointers_do(it);
 466     vmSymbols::metaspace_pointers_do(it);

 467 
 468     // The above code should find all the symbols that are referenced by the
 469     // archived classes. We just need to add the extra symbols which
 470     // may not be used by any of the archived classes -- these are usually
 471     // symbols that we anticipate to be used at run time, so we can store
 472     // them in the RO region, to be shared across multiple processes.
 473     if (_extra_symbols != nullptr) {
 474       for (int i = 0; i < _extra_symbols->length(); i++) {
 475         it->push(_extra_symbols->adr_at(i));
 476       }
 477     }




 478   }
 479 };
 480 
 481 char* VM_PopulateDumpSharedSpace::dump_read_only_tables() {
 482   ArchiveBuilder::OtherROAllocMark mark;
 483 
 484   SystemDictionaryShared::write_to_archive();




 485 
 486   // Write lambform lines into archive
 487   LambdaFormInvokers::dump_static_archive_invokers();
 488   // Write module name into archive
 489   CDS_JAVA_HEAP_ONLY(Modules::dump_main_module_name();)
 490   // Write the other data to the output array.
 491   DumpRegion* ro_region = ArchiveBuilder::current()->ro_region();
 492   char* start = ro_region->top();
 493   WriteClosure wc(ro_region);
 494   MetaspaceShared::serialize(&wc);
 495 
 496   return start;
 497 }
 498 
 499 void VM_PopulateDumpSharedSpace::doit() {
 500   DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm);
 501 
 502   FileMapInfo::check_nonempty_dir_in_shared_path_table();
 503 
 504   NOT_PRODUCT(SystemDictionary::verify();)
 505 
 506   // Block concurrent class unloading from changing the _dumptime_table
 507   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
 508   SystemDictionaryShared::check_excluded_classes();
 509 
 510   StaticArchiveBuilder builder;
 511   builder.gather_source_objs();
 512   builder.reserve_buffer();
 513 
 514   char* cloned_vtables = CppVtables::dumptime_init(&builder);
 515 
 516   // Initialize random for updating the hash of symbols
 517   os::init_random(0x12345678);
 518 
 519   builder.dump_rw_metadata();
 520   builder.dump_ro_metadata();
 521   builder.relocate_metaspaceobj_embedded_pointers();
 522 
 523   dump_java_heap_objects(builder.klasses());
 524   dump_shared_symbol_table(builder.symbols());








 525 
 526   log_info(cds)("Make classes shareable");
 527   builder.make_klasses_shareable();

 528 
 529   char* serialized_data = dump_read_only_tables();
 530 
 531   SystemDictionaryShared::adjust_lambda_proxy_class_dictionary();
 532 






 533   // The vtable clones contain addresses of the current process.
 534   // We don't want to write these addresses into the archive.
 535   CppVtables::zero_archived_vtables();
 536 
 537   // relocate the data so that it can be mapped to MetaspaceShared::requested_base_address()
 538   // without runtime relocation.
 539   builder.relocate_to_requested();
 540 
 541   // Write the archive file
 542   const char* static_archive = CDSConfig::static_archive_path();
 543   assert(static_archive != nullptr, "SharedArchiveFile not set?");
 544   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
 545   mapinfo->populate_header(MetaspaceShared::core_region_alignment());
 546   mapinfo->set_serialized_data(serialized_data);
 547   mapinfo->set_cloned_vtables(cloned_vtables);
 548   mapinfo->open_for_write();
 549   builder.write_archive(mapinfo, &_heap_info);
 550 
 551   if (PrintSystemDictionaryAtExit) {
 552     SystemDictionary::print();
 553   }
 554 
 555   if (AllowArchivingWithJavaAgent) {
 556     log_warning(cds)("This archive was created with AllowArchivingWithJavaAgent. It should be used "
 557             "for testing purposes only and should not be used in a production environment");
 558   }





 559 }
 560 
 561 class CollectCLDClosure : public CLDClosure {
 562   GrowableArray<ClassLoaderData*> _loaded_cld;
 563   GrowableArray<OopHandle> _loaded_cld_handles; // keep the CLDs alive
 564   Thread* _current_thread;
 565 public:
 566   CollectCLDClosure(Thread* thread) : _current_thread(thread) {}
 567   ~CollectCLDClosure() {
 568     for (int i = 0; i < _loaded_cld_handles.length(); i++) {
 569       _loaded_cld_handles.at(i).release(Universe::vm_global());
 570     }
 571   }
 572   void do_cld(ClassLoaderData* cld) {
 573     assert(cld->is_alive(), "must be");
 574     _loaded_cld.append(cld);
 575     _loaded_cld_handles.append(OopHandle(Universe::vm_global(), cld->holder()));
 576   }
 577 
 578   int nof_cld() const                { return _loaded_cld.length(); }
 579   ClassLoaderData* cld_at(int index) { return _loaded_cld.at(index); }
 580 };
 581 
 582 // Check if we can eagerly link this class at dump time, so we can avoid the
 583 // runtime linking overhead (especially verification)
 584 bool MetaspaceShared::may_be_eagerly_linked(InstanceKlass* ik) {



 585   if (!ik->can_be_verified_at_dumptime()) {
 586     // For old classes, try to leave them in the unlinked state, so
 587     // we can still store them in the archive. They must be
 588     // linked/verified at runtime.
 589     return false;
 590   }

 591   if (CDSConfig::is_dumping_dynamic_archive() && ik->is_shared_unregistered_class()) {
 592     // Linking of unregistered classes at this stage may cause more
 593     // classes to be resolved, resulting in calls to ClassLoader.loadClass()
 594     // that may not be expected by custom class loaders.
 595     //
 596     // It's OK to do this for the built-in loaders as we know they can
 597     // tolerate this.
 598     return false;
 599   }
 600   return true;
 601 }
 602 
 603 bool MetaspaceShared::link_class_for_cds(InstanceKlass* ik, TRAPS) {
 604   // Link the class to cause the bytecodes to be rewritten and the
 605   // cpcache to be created. Class verification is done according
 606   // to -Xverify setting.
 607   bool res = MetaspaceShared::try_link_class(THREAD, ik);
 608   ClassPrelinker::dumptime_resolve_constants(ik, CHECK_(false));
 609   return res;
 610 }
 611 
 612 void MetaspaceShared::link_shared_classes(bool jcmd_request, TRAPS) {
 613   ClassPrelinker::initialize();
 614 
 615   if (!jcmd_request) {




 616     LambdaFormInvokers::regenerate_holder_classes(CHECK);
 617   }
 618 
 619   // Collect all loaded ClassLoaderData.
 620   CollectCLDClosure collect_cld(THREAD);
 621   {
 622     // ClassLoaderDataGraph::loaded_cld_do requires ClassLoaderDataGraph_lock.
 623     // We cannot link the classes while holding this lock (or else we may run into deadlock).
 624     // Therefore, we need to first collect all the CLDs, and then link their classes after
 625     // releasing the lock.
 626     MutexLocker lock(ClassLoaderDataGraph_lock);
 627     ClassLoaderDataGraph::loaded_cld_do(&collect_cld);
 628   }
 629 
 630   while (true) {
 631     bool has_linked = false;
 632     for (int i = 0; i < collect_cld.nof_cld(); i++) {
 633       ClassLoaderData* cld = collect_cld.cld_at(i);
 634       for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) {
 635         if (klass->is_instance_klass()) {
 636           InstanceKlass* ik = InstanceKlass::cast(klass);
 637           if (may_be_eagerly_linked(ik)) {
 638             has_linked |= link_class_for_cds(ik, CHECK);



 639           }
 640         }
 641       }
 642     }
 643 
 644     if (!has_linked) {
 645       break;
 646     }
 647     // Class linking includes verification which may load more classes.
 648     // Keep scanning until we have linked no more classes.
 649   }


















 650 }
 651 
 652 void MetaspaceShared::prepare_for_dumping() {
 653   assert(CDSConfig::is_dumping_archive(), "sanity");
 654   CDSConfig::check_unsupported_dumping_properties();
 655   ClassLoader::initialize_shared_path(JavaThread::current());
 656 }
 657 
 658 // Preload classes from a list, populate the shared spaces and dump to a
 659 // file.
 660 void MetaspaceShared::preload_and_dump() {
 661   EXCEPTION_MARK;
 662   ResourceMark rm(THREAD);
 663   preload_and_dump_impl(THREAD);








 664   if (HAS_PENDING_EXCEPTION) {
 665     if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 666       log_error(cds)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
 667                      SIZE_FORMAT "M", MaxHeapSize/M);
 668       MetaspaceShared::unrecoverable_writing_error();
 669     } else {
 670       log_error(cds)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
 671                      java_lang_String::as_utf8_string(java_lang_Throwable::message(PENDING_EXCEPTION)));
 672       MetaspaceShared::unrecoverable_writing_error("VM exits due to exception, use -Xlog:cds,exceptions=trace for detail");
 673     }
 674   }
 675 }
 676 
 677 #if INCLUDE_CDS_JAVA_HEAP && defined(_LP64)
 678 void MetaspaceShared::adjust_heap_sizes_for_dumping() {
 679   if (!CDSConfig::is_dumping_heap() || UseCompressedOops) {
 680     return;
 681   }
 682   // CDS heap dumping requires all string oops to have an offset
 683   // from the heap bottom that can be encoded in 32-bit.

 744                                                     ClassListParser::_parse_all, CHECK);
 745   }
 746   if (classlist_path != default_classlist) {
 747     struct stat statbuf;
 748     if (os::stat(default_classlist, &statbuf) == 0) {
 749       // File exists, let's use it.
 750       class_count += ClassListParser::parse_classlist(default_classlist,
 751                                                       ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
 752     }
 753   }
 754 
 755   // Exercise the manifest processing code to ensure classes used by CDS at runtime
 756   // are always archived
 757   const char* dummy = "Manifest-Version: 1.0\n";
 758   CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
 759 
 760   log_info(cds)("Loading classes to share: done.");
 761   log_info(cds)("Shared spaces: preloaded %d classes", class_count);
 762 }
 763 
 764 void MetaspaceShared::preload_and_dump_impl(TRAPS) {
 765   preload_classes(CHECK);


 766 
 767   if (SharedArchiveConfigFile) {
 768     log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
 769     read_extra_data(THREAD, SharedArchiveConfigFile);
 770     log_info(cds)("Reading extra data: done.");












 771   }

 772 
 773   // Rewrite and link classes
 774   log_info(cds)("Rewriting and linking classes ...");
 775 
 776   // Link any classes which got missed. This would happen if we have loaded classes that
 777   // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
 778   // fails verification, all other interfaces that were not specified in the classlist but
 779   // are implemented by K are not verified.
 780   link_shared_classes(false/*not from jcmd*/, CHECK);
 781   log_info(cds)("Rewriting and linking classes: done");
 782 










 783 #if INCLUDE_CDS_JAVA_HEAP
 784   if (CDSConfig::is_dumping_heap()) {
 785     if (!HeapShared::is_archived_boot_layer_available(THREAD)) {
 786       log_info(cds)("archivedBootLayer not available, disabling full module graph");
 787       CDSConfig::disable_dumping_full_module_graph();
 788     }
 789     HeapShared::init_for_dumping(CHECK);
 790     ArchiveHeapWriter::init();
 791     if (CDSConfig::is_dumping_full_module_graph()) {
 792       HeapShared::reset_archived_object_states(CHECK);
 793     }
 794 




 795     // Do this at the very end, when no Java code will be executed. Otherwise
 796     // some new strings may be added to the intern table.
 797     StringTable::allocate_shared_strings_array(CHECK);
 798   }
 799 #endif
 800 
 801   VM_PopulateDumpSharedSpace op;
 802   VMThread::execute(&op);

































































































 803 }
 804 
 805 // Returns true if the class's status has changed.
 806 bool MetaspaceShared::try_link_class(JavaThread* current, InstanceKlass* ik) {
 807   ExceptionMark em(current);
 808   JavaThread* THREAD = current; // For exception macros.
 809   assert(CDSConfig::is_dumping_archive(), "sanity");
 810   if (!ik->is_shared() && ik->is_loaded() && !ik->is_linked() && ik->can_be_verified_at_dumptime() &&






 811       !SystemDictionaryShared::has_class_failed_verification(ik)) {
 812     bool saved = BytecodeVerificationLocal;
 813     if (ik->is_shared_unregistered_class() && ik->class_loader() == nullptr) {
 814       // The verification decision is based on BytecodeVerificationRemote
 815       // for non-system classes. Since we are using the null classloader
 816       // to load non-system classes for customized class loaders during dumping,
 817       // we need to temporarily change BytecodeVerificationLocal to be the same as
 818       // BytecodeVerificationRemote. Note this can cause the parent system
 819       // classes also being verified. The extra overhead is acceptable during
 820       // dumping.
 821       BytecodeVerificationLocal = BytecodeVerificationRemote;
 822     }
 823     ik->link_class(THREAD);
 824     if (HAS_PENDING_EXCEPTION) {
 825       ResourceMark rm(THREAD);
 826       log_warning(cds)("Preload Warning: Verification failed for %s",
 827                     ik->external_name());
 828       CLEAR_PENDING_EXCEPTION;
 829       SystemDictionaryShared::set_class_has_failed_verification(ik);
 830     }

 903   if (message != nullptr) {
 904     log_error(cds)("%s", message);
 905   }
 906   vm_exit_during_initialization("Unable to use shared archive.", nullptr);
 907 }
 908 
 909 // This function is called when the JVM is unable to write the specified CDS archive due to an
 910 // unrecoverable error.
 911 void MetaspaceShared::unrecoverable_writing_error(const char* message) {
 912   log_error(cds)("An error has occurred while writing the shared archive file.");
 913   if (message != nullptr) {
 914     log_error(cds)("%s", message);
 915   }
 916   vm_direct_exit(1);
 917 }
 918 
 919 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
 920   assert(UseSharedSpaces, "Must be called when UseSharedSpaces is enabled");
 921   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
 922 
 923   FileMapInfo* static_mapinfo = open_static_archive();
 924   FileMapInfo* dynamic_mapinfo = nullptr;
 925 
 926   if (static_mapinfo != nullptr) {
 927     log_info(cds)("Core region alignment: " SIZE_FORMAT, static_mapinfo->core_region_alignment());
 928     dynamic_mapinfo = open_dynamic_archive();
 929 
 930     // First try to map at the requested address
 931     result = map_archives(static_mapinfo, dynamic_mapinfo, true);
 932     if (result == MAP_ARCHIVE_MMAP_FAILURE) {
 933       // Mapping has failed (probably due to ASLR). Let's map at an address chosen
 934       // by the OS.
 935       log_info(cds)("Try to map archive(s) at an alternative address");
 936       result = map_archives(static_mapinfo, dynamic_mapinfo, false);
 937     }
 938   }
 939 
 940   if (result == MAP_ARCHIVE_SUCCESS) {
 941     bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());
 942     char* cds_base = static_mapinfo->mapped_base();
 943     char* cds_end =  dynamic_mapped ? dynamic_mapinfo->mapped_end() : static_mapinfo->mapped_end();

 950       FileMapInfo::set_shared_path_table(dynamic_mapinfo);
 951       // turn AutoCreateSharedArchive off if successfully mapped
 952       AutoCreateSharedArchive = false;
 953     } else {
 954       FileMapInfo::set_shared_path_table(static_mapinfo);
 955     }
 956   } else {
 957     set_shared_metaspace_range(nullptr, nullptr, nullptr);
 958     if (CDSConfig::is_dumping_dynamic_archive()) {
 959       log_warning(cds)("-XX:ArchiveClassesAtExit is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info.");
 960     }
 961     UseSharedSpaces = false;
 962     // The base archive cannot be mapped. We cannot dump the dynamic shared archive.
 963     AutoCreateSharedArchive = false;
 964     CDSConfig::disable_dumping_dynamic_archive();
 965     log_info(cds)("Unable to map shared spaces");
 966     if (PrintSharedArchiveAndExit) {
 967       MetaspaceShared::unrecoverable_loading_error("Unable to use shared archive.");
 968     } else if (RequireSharedSpaces) {
 969       MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");




 970     }
 971   }
 972 
 973   // If mapping failed and -XShare:on, the vm should exit
 974   bool has_failed = false;
 975   if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
 976     has_failed = true;
 977     delete static_mapinfo;
 978   }
 979   if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
 980     has_failed = true;
 981     delete dynamic_mapinfo;
 982   }
 983   if (RequireSharedSpaces && has_failed) {

 984       MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
 985   }
 986 }
 987 
 988 FileMapInfo* MetaspaceShared::open_static_archive() {





 989   const char* static_archive = CDSConfig::static_archive_path();
 990   assert(static_archive != nullptr, "sanity");
 991   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
 992   if (!mapinfo->initialize()) {
 993     delete(mapinfo);
 994     return nullptr;


 995   }
 996   return mapinfo;
 997 }
 998 
 999 FileMapInfo* MetaspaceShared::open_dynamic_archive() {
1000   if (CDSConfig::is_dumping_dynamic_archive()) {
1001     return nullptr;
1002   }
1003   const char* dynamic_archive = CDSConfig::dynamic_archive_path();
1004   if (dynamic_archive == nullptr) {
1005     return nullptr;
1006   }
1007 
1008   FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1009   if (!mapinfo->initialize()) {
1010     delete(mapinfo);
1011     if (RequireSharedSpaces) {
1012       MetaspaceShared::unrecoverable_loading_error("Failed to initialize dynamic archive");
1013     }
1014     return nullptr;
1015   }
1016   return mapinfo;

1451 
1452 // Read the miscellaneous data from the shared file, and
1453 // serialize it out to its various destinations.
1454 
1455 void MetaspaceShared::initialize_shared_spaces() {
1456   FileMapInfo *static_mapinfo = FileMapInfo::current_info();
1457 
1458   // Verify various attributes of the archive, plus initialize the
1459   // shared string/symbol tables.
1460   char* buffer = static_mapinfo->serialized_data();
1461   intptr_t* array = (intptr_t*)buffer;
1462   ReadClosure rc(&array);
1463   serialize(&rc);
1464 
1465   // Finish up archived heap initialization. These must be
1466   // done after ReadClosure.
1467   static_mapinfo->patch_heap_embedded_pointers();
1468   ArchiveHeapLoader::finish_initialization();
1469 
1470   CDS_JAVA_HEAP_ONLY(Universe::update_archived_basic_type_mirrors());



1471 
1472   // Close the mapinfo file
1473   static_mapinfo->close();
1474 
1475   static_mapinfo->unmap_region(MetaspaceShared::bm);
1476 
1477   FileMapInfo *dynamic_mapinfo = FileMapInfo::dynamic_info();
1478   if (dynamic_mapinfo != nullptr) {
1479     intptr_t* buffer = (intptr_t*)dynamic_mapinfo->serialized_data();
1480     ReadClosure rc(&buffer);
1481     ArchiveBuilder::serialize_dynamic_archivable_items(&rc);
1482     DynamicArchive::setup_array_klasses();
1483     dynamic_mapinfo->close();
1484     dynamic_mapinfo->unmap_region(MetaspaceShared::bm);
1485   }
1486 
1487   // Set up LambdaFormInvokers::_lambdaform_lines for dynamic dump
1488   if (CDSConfig::is_dumping_dynamic_archive()) {
1489     // Read stored LF format lines stored in static archive
1490     LambdaFormInvokers::read_static_archive_invokers();
1491   }
1492 
1493   if (PrintSharedArchiveAndExit) {
1494     // Print archive names
1495     if (dynamic_mapinfo != nullptr) {
1496       tty->print_cr("\n\nBase archive name: %s", CDSConfig::static_archive_path());
1497       tty->print_cr("Base archive version %d", static_mapinfo->version());
1498     } else {
1499       tty->print_cr("Static archive name: %s", static_mapinfo->full_path());
1500       tty->print_cr("Static archive version %d", static_mapinfo->version());
1501     }
1502 
1503     SystemDictionaryShared::print_shared_archive(tty);
1504     if (dynamic_mapinfo != nullptr) {
1505       tty->print_cr("\n\nDynamic archive name: %s", dynamic_mapinfo->full_path());
1506       tty->print_cr("Dynamic archive version %d", dynamic_mapinfo->version());
1507       SystemDictionaryShared::print_shared_archive(tty, false/*dynamic*/);
1508     }






1509 
1510     // collect shared symbols and strings
1511     CountSharedSymbols cl;
1512     SymbolTable::shared_symbols_do(&cl);
1513     tty->print_cr("Number of shared symbols: %d", cl.total());
1514     tty->print_cr("Number of shared strings: %zu", StringTable::shared_entry_count());
1515     tty->print_cr("VM version: %s\r\n", static_mapinfo->vm_version());
1516     if (FileMapInfo::current_info() == nullptr || _archive_loading_failed) {
1517       tty->print_cr("archive is invalid");
1518       vm_exit(1);
1519     } else {
1520       tty->print_cr("archive is valid");
1521       vm_exit(0);
1522     }
1523   }
1524 }
1525 
1526 // JVM/TI RedefineClasses() support:
1527 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
1528   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");

   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/cdsAccess.hpp"
  31 #include "cds/cdsConfig.hpp"
  32 #include "cds/cdsProtectionDomain.hpp"
  33 #include "cds/cds_globals.hpp"
  34 #include "cds/classListParser.hpp"
  35 #include "cds/classListWriter.hpp"
  36 #include "cds/classPrelinker.hpp"
  37 #include "cds/cppVtables.hpp"
  38 #include "cds/dumpAllocStats.hpp"
  39 #include "cds/dynamicArchive.hpp"
  40 #include "cds/filemap.hpp"
  41 #include "cds/heapShared.hpp"
  42 #include "cds/lambdaFormInvokers.hpp"
  43 #include "cds/metaspaceShared.hpp"
  44 #include "classfile/classLoaderDataGraph.hpp"
  45 #include "classfile/classLoaderDataShared.hpp"
  46 #include "classfile/classLoaderExt.hpp"
  47 #include "classfile/javaClasses.inline.hpp"
  48 #include "classfile/loaderConstraints.hpp"
  49 #include "classfile/modules.hpp"
  50 #include "classfile/placeholders.hpp"
  51 #include "classfile/stringTable.hpp"
  52 #include "classfile/symbolTable.hpp"
  53 #include "classfile/systemDictionary.hpp"
  54 #include "classfile/systemDictionaryShared.hpp"
  55 #include "classfile/vmClasses.hpp"
  56 #include "classfile/vmSymbols.hpp"
  57 #include "code/codeCache.hpp"
  58 #include "code/SCCache.hpp"
  59 #include "compiler/compileBroker.hpp"
  60 #include "compiler/precompiler.hpp"
  61 #include "gc/shared/gcVMOperations.hpp"
  62 #include "interpreter/bytecodeStream.hpp"
  63 #include "interpreter/bytecodes.hpp"
  64 #include "jvm_io.h"
  65 #include "logging/log.hpp"
  66 #include "logging/logMessage.hpp"
  67 #include "logging/logStream.hpp"
  68 #include "memory/metaspace.hpp"
  69 #include "memory/metaspaceClosure.hpp"
  70 #include "memory/resourceArea.hpp"
  71 #include "memory/universe.hpp"
  72 #include "nmt/memTracker.hpp"
  73 #include "oops/compressedKlass.hpp"
  74 #include "oops/instanceMirrorKlass.hpp"
  75 #include "oops/klass.inline.hpp"
  76 #include "oops/method.inline.hpp"
  77 #include "oops/objArrayOop.hpp"
  78 #include "oops/oop.inline.hpp"
  79 #include "oops/oopHandle.hpp"
  80 #include "oops/trainingData.hpp"
  81 #include "prims/jvmtiExport.hpp"
  82 #include "prims/whitebox.hpp"
  83 #include "runtime/arguments.hpp"
  84 #include "runtime/globals.hpp"
  85 #include "runtime/globals_extension.hpp"
  86 #include "runtime/handles.inline.hpp"
  87 #include "runtime/javaCalls.hpp"
  88 #include "runtime/os.inline.hpp"
  89 #include "runtime/safepointVerifiers.hpp"
  90 #include "runtime/sharedRuntime.hpp"
  91 #include "runtime/vmOperations.hpp"
  92 #include "runtime/vmThread.hpp"
  93 #include "sanitizers/leak.hpp"
  94 #include "utilities/align.hpp"
  95 #include "utilities/bitMap.inline.hpp"
  96 #include "utilities/defaultStream.hpp"
  97 #include "utilities/ostream.hpp"
  98 #include "utilities/resourceHash.hpp"
  99 
 100 ReservedSpace MetaspaceShared::_symbol_rs;
 101 VirtualSpace MetaspaceShared::_symbol_vs;
 102 bool MetaspaceShared::_archive_loading_failed = false;
 103 bool MetaspaceShared::_remapped_readwrite = false;
 104 void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
 105 intx MetaspaceShared::_relocation_delta;
 106 char* MetaspaceShared::_requested_base_address;
 107 bool MetaspaceShared::_use_optimized_module_handling = true;
 108 Array<Method*>* MetaspaceShared::_archived_method_handle_intrinsics = nullptr;
 109 
 110 // The CDS archive is divided into the following regions:
 111 //     rw  - read-write metadata
 112 //     ro  - read-only metadata and read-only tables
 113 //     hp  - heap region
 114 //     bm  - bitmap for relocating the above 7 regions.
 115 //
 116 // The rw and ro regions are linearly allocated, in the order of rw->ro.
 117 // These regions are aligned with MetaspaceShared::core_region_alignment().
 118 //
 119 // These 2 regions are populated in the following steps:
 120 // [0] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
 121 //     temporarily allocated outside of the shared regions.
 122 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
 123 // [2] C++ vtables are copied into the rw region.
 124 // [3] ArchiveBuilder copies RW metadata into the rw region.
 125 // [4] ArchiveBuilder copies RO metadata into the ro region.
 126 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
 127 //     are copied into the ro region as read-only tables.
 128 //

 291   if (UseSharedSpaces) {
 292     int size = FileMapInfo::get_number_of_shared_paths();
 293     if (size > 0) {
 294       CDSProtectionDomain::allocate_shared_data_arrays(size, CHECK);
 295       if (!CDSConfig::is_dumping_dynamic_archive()) {
 296         FileMapInfo* info;
 297         if (FileMapInfo::dynamic_info() == nullptr) {
 298           info = FileMapInfo::current_info();
 299         } else {
 300           info = FileMapInfo::dynamic_info();
 301         }
 302         ClassLoaderExt::init_paths_start_index(info->app_class_paths_start_index());
 303         ClassLoaderExt::init_app_module_paths_start_index(info->app_module_paths_start_index());
 304       }
 305     }
 306   }
 307 }
 308 
 309 static GrowableArrayCHeap<OopHandle, mtClassShared>* _extra_interned_strings = nullptr;
 310 static GrowableArrayCHeap<Symbol*, mtClassShared>* _extra_symbols = nullptr;
 311 static GrowableArray<Method*>* _method_handle_intrinsics = nullptr;
 312 
 313 void MetaspaceShared::read_extra_data(JavaThread* current, const char* filename) {
 314   _extra_interned_strings = new GrowableArrayCHeap<OopHandle, mtClassShared>(10000);
 315   _extra_symbols = new GrowableArrayCHeap<Symbol*, mtClassShared>(1000);
 316 
 317   HashtableTextDump reader(filename);
 318   reader.check_version("VERSION: 1.0");
 319 
 320   while (reader.remain() > 0) {
 321     int utf8_length;
 322     int prefix_type = reader.scan_prefix(&utf8_length);
 323     ResourceMark rm(current);
 324     if (utf8_length == 0x7fffffff) {
 325       // buf_len will overflown 32-bit value.
 326       log_error(cds)("string length too large: %d", utf8_length);
 327       MetaspaceShared::unrecoverable_loading_error();
 328     }
 329     int buf_len = utf8_length+1;
 330     char* utf8_buffer = NEW_RESOURCE_ARRAY(char, buf_len);
 331     reader.get_utf8(utf8_buffer, utf8_length);

 342       if (HAS_PENDING_EXCEPTION) {
 343         log_warning(cds, heap)("[line %d] extra interned string allocation failed; size too large: %d",
 344                                reader.last_line_no(), utf8_length);
 345         CLEAR_PENDING_EXCEPTION;
 346       } else {
 347 #if INCLUDE_CDS_JAVA_HEAP
 348         if (ArchiveHeapWriter::is_string_too_large_to_archive(str)) {
 349           log_warning(cds, heap)("[line %d] extra interned string ignored; size too large: %d",
 350                                  reader.last_line_no(), utf8_length);
 351           continue;
 352         }
 353         // Make sure this string is included in the dumped interned string table.
 354         assert(str != nullptr, "must succeed");
 355         _extra_interned_strings->append(OopHandle(Universe::vm_global(), str));
 356 #endif
 357       }
 358     }
 359   }
 360 }
 361 
 362 void MetaspaceShared::make_method_handle_intrinsics_shareable() {
 363   for (int i = 0; i < _method_handle_intrinsics->length(); i++) {
 364     Method* m = ArchiveBuilder::current()->get_buffered_addr(_method_handle_intrinsics->at(i));
 365     m->remove_unshareable_info();
 366     // Each method has its own constant pool (which is distinct from m->method_holder()->constants());
 367     m->constants()->remove_unshareable_info();
 368   }
 369 }
 370 
 371 void MetaspaceShared::write_method_handle_intrinsics() {
 372   int len = _method_handle_intrinsics->length();
 373   _archived_method_handle_intrinsics = ArchiveBuilder::new_ro_array<Method*>(len);
 374   for (int i = 0; i < len; i++) {
 375     ArchiveBuilder::current()->write_pointer_in_buffer(_archived_method_handle_intrinsics->adr_at(i),
 376                                                        _method_handle_intrinsics->at(i));
 377   }
 378   log_info(cds)("Archived %d method handle intrinsics", len);
 379 }
 380 
 381 // Read/write a data stream for restoring/preserving metadata pointers and
 382 // miscellaneous data from/to the shared archive file.
 383 
 384 void MetaspaceShared::serialize(SerializeClosure* soc) {
 385   int tag = 0;
 386   soc->do_tag(--tag);
 387 
 388   // Verify the sizes of various metadata in the system.
 389   soc->do_tag(sizeof(Method));
 390   soc->do_tag(sizeof(ConstMethod));
 391   soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE));
 392   soc->do_tag(sizeof(ConstantPool));
 393   soc->do_tag(sizeof(ConstantPoolCache));
 394   soc->do_tag(objArrayOopDesc::base_offset_in_bytes());
 395   soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE));
 396   soc->do_tag(sizeof(Symbol));
 397 
 398   // Need to do this first, as subsequent steps may call virtual functions
 399   // in archived Metadata objects.
 400   CppVtables::serialize(soc);
 401   soc->do_tag(--tag);
 402 
 403   // Dump/restore miscellaneous metadata.
 404   JavaClasses::serialize_offsets(soc);
 405   HeapShared::serialize_misc_info(soc);
 406   Universe::serialize(soc);
 407   soc->do_tag(--tag);
 408 
 409   // Dump/restore references to commonly used names and signatures.
 410   vmSymbols::serialize(soc);
 411   soc->do_tag(--tag);
 412 
 413   // Dump/restore the symbol/string/subgraph_info tables
 414   SymbolTable::serialize_shared_table_header(soc);
 415   StringTable::serialize_shared_table_header(soc);
 416   HeapShared::serialize_tables(soc);
 417   SystemDictionaryShared::serialize_dictionary_headers(soc);
 418   ClassPrelinker::serialize(soc, true);
 419   TrainingData::serialize_training_data(soc);
 420   InstanceMirrorKlass::serialize_offsets(soc);
 421 
 422   // Dump/restore well known classes (pointers)
 423   SystemDictionaryShared::serialize_vm_classes(soc);
 424   soc->do_tag(--tag);
 425 
 426   CDS_JAVA_HEAP_ONLY(Modules::serialize(soc);)
 427   CDS_JAVA_HEAP_ONLY(ClassLoaderDataShared::serialize(soc);)
 428   soc->do_ptr((void**)&_archived_method_handle_intrinsics);
 429 
 430 
 431   LambdaFormInvokers::serialize(soc);
 432   soc->do_tag(666);
 433 }
 434 
 435 static void rewrite_nofast_bytecode(const methodHandle& method) {
 436   BytecodeStream bcs(method);
 437   while (!bcs.is_last_bytecode()) {
 438     Bytecodes::Code opcode = bcs.next();
 439     switch (opcode) {
 440     case Bytecodes::_getfield:      *bcs.bcp() = Bytecodes::_nofast_getfield;      break;
 441     case Bytecodes::_putfield:      *bcs.bcp() = Bytecodes::_nofast_putfield;      break;
 442     case Bytecodes::_aload_0:       *bcs.bcp() = Bytecodes::_nofast_aload_0;       break;
 443     case Bytecodes::_iload: {
 444       if (!bcs.is_wide()) {
 445         *bcs.bcp() = Bytecodes::_nofast_iload;
 446       }
 447       break;
 448     }
 449     default: break;

 452 }
 453 
 454 // [1] Rewrite all bytecodes as needed, so that the ConstMethod* will not be modified
 455 //     at run time by RewriteBytecodes/RewriteFrequentPairs
 456 // [2] Assign a fingerprint, so one doesn't need to be assigned at run-time.
 457 void MetaspaceShared::rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread* thread, InstanceKlass* ik) {
 458   for (int i = 0; i < ik->methods()->length(); i++) {
 459     methodHandle m(thread, ik->methods()->at(i));
 460     if (ik->can_be_verified_at_dumptime() && ik->is_linked()) {
 461       rewrite_nofast_bytecode(m);
 462     }
 463     Fingerprinter fp(m);
 464     // The side effect of this call sets method's fingerprint field.
 465     fp.fingerprint();
 466   }
 467 }
 468 
 469 class VM_PopulateDumpSharedSpace : public VM_Operation {
 470 private:
 471   ArchiveHeapInfo _heap_info;

 472   void dump_java_heap_objects(GrowableArray<Klass*>* klasses) NOT_CDS_JAVA_HEAP_RETURN;
 473   void dump_shared_symbol_table(GrowableArray<Symbol*>* symbols) {
 474     log_info(cds)("Dumping symbol table ...");
 475     SymbolTable::write_to_archive(symbols);
 476   }
 477   char* dump_read_only_tables();
 478   StaticArchiveBuilder& _builder;
 479   FileMapInfo* _mapinfo;
 480 public:
 481 
 482   VM_PopulateDumpSharedSpace(StaticArchiveBuilder& b) :
 483     VM_Operation(), _heap_info(), _builder(b), _mapinfo(nullptr) {}
 484 
 485   bool skip_operation() const { return false; }
 486 
 487   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
 488   void doit();   // outline because gdb sucks
 489   bool allow_nested_vm_operations() const { return true; }
 490   FileMapInfo* mapinfo() const { return _mapinfo; }
 491   ArchiveHeapInfo* heap_info() { return &_heap_info; }
 492 }; // class VM_PopulateDumpSharedSpace
 493 
 494 class StaticArchiveBuilder : public ArchiveBuilder {
 495 public:
 496   StaticArchiveBuilder() : ArchiveBuilder() {}
 497 
 498   virtual void iterate_roots(MetaspaceClosure* it) {
 499     FileMapInfo::metaspace_pointers_do(it);
 500     SystemDictionaryShared::dumptime_classes_do(it);
 501     Universe::metaspace_pointers_do(it);
 502     vmSymbols::metaspace_pointers_do(it);
 503     TrainingData::iterate_roots(it);
 504 
 505     // The above code should find all the symbols that are referenced by the
 506     // archived classes. We just need to add the extra symbols which
 507     // may not be used by any of the archived classes -- these are usually
 508     // symbols that we anticipate to be used at run time, so we can store
 509     // them in the RO region, to be shared across multiple processes.
 510     if (_extra_symbols != nullptr) {
 511       for (int i = 0; i < _extra_symbols->length(); i++) {
 512         it->push(_extra_symbols->adr_at(i));
 513       }
 514     }
 515 
 516     for (int i = 0; i < _method_handle_intrinsics->length(); i++) {
 517       it->push(_method_handle_intrinsics->adr_at(i));
 518     }
 519   }
 520 };
 521 
 522 char* VM_PopulateDumpSharedSpace::dump_read_only_tables() {
 523   ArchiveBuilder::OtherROAllocMark mark;
 524 
 525   SystemDictionaryShared::write_to_archive();
 526   ClassPrelinker::record_initiated_klasses(true);
 527   ClassPrelinker::record_unregistered_klasses();
 528   TrainingData::dump_training_data();
 529   MetaspaceShared::write_method_handle_intrinsics();
 530 
 531   // Write lambform lines into archive
 532   LambdaFormInvokers::dump_static_archive_invokers();
 533   // Write module name into archive
 534   CDS_JAVA_HEAP_ONLY(Modules::dump_main_module_name();)
 535   // Write the other data to the output array.
 536   DumpRegion* ro_region = ArchiveBuilder::current()->ro_region();
 537   char* start = ro_region->top();
 538   WriteClosure wc(ro_region);
 539   MetaspaceShared::serialize(&wc);
 540 
 541   return start;
 542 }
 543 
 544 void VM_PopulateDumpSharedSpace::doit() {
 545   DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm);
 546 
 547   FileMapInfo::check_nonempty_dir_in_shared_path_table();
 548 
 549   NOT_PRODUCT(SystemDictionary::verify();)
 550 
 551   // Block concurrent class unloading from changing the _dumptime_table
 552   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
 553   SystemDictionaryShared::check_excluded_classes();
 554 
 555   _builder.gather_source_objs();
 556   _builder.reserve_buffer();

 557 
 558   char* cloned_vtables = CppVtables::dumptime_init(&_builder);
 559 
 560   // Initialize random for updating the hash of symbols
 561   os::init_random(0x12345678);
 562 
 563   _builder.dump_rw_metadata();
 564   _builder.dump_ro_metadata();
 565   _builder.relocate_metaspaceobj_embedded_pointers();
 566 
 567   dump_java_heap_objects(_builder.klasses());
 568   dump_shared_symbol_table(_builder.symbols());
 569 
 570   {
 571     ArchiveBuilder::OtherROAllocMark mark;
 572     ClassPrelinker::record_preloaded_klasses(true);
 573     if (CDSConfig::is_dumping_preimage_static_archive()) {
 574       ClassPrelinker::record_final_image_eager_linkage();
 575     }
 576   }
 577 
 578   log_info(cds)("Make classes shareable");
 579   _builder.make_klasses_shareable();
 580   MetaspaceShared::make_method_handle_intrinsics_shareable();
 581 
 582   char* serialized_data = dump_read_only_tables();
 583 
 584   SystemDictionaryShared::adjust_lambda_proxy_class_dictionary();
 585 
 586   log_info(cds)("Adjust method info dictionary");
 587   SystemDictionaryShared::adjust_method_info_dictionary();
 588 
 589   log_info(cds)("Adjust training data dictionary");
 590   TrainingData::adjust_training_data_dictionary();
 591 
 592   // The vtable clones contain addresses of the current process.
 593   // We don't want to write these addresses into the archive.
 594   CppVtables::zero_archived_vtables();
 595 




 596   // Write the archive file
 597   const char* static_archive;
 598   if (CDSConfig::is_dumping_final_static_archive()) {
 599     static_archive = CacheDataStore;
 600     assert(FileMapInfo::current_info() != nullptr, "sanity");
 601     delete FileMapInfo::current_info();
 602   } else {
 603     static_archive = CDSConfig::static_archive_path();









 604   }
 605   assert(static_archive != nullptr, "SharedArchiveFile not set?");
 606   _mapinfo = new FileMapInfo(static_archive, true);
 607   _mapinfo->populate_header(MetaspaceShared::core_region_alignment());
 608   _mapinfo->set_serialized_data(serialized_data);
 609   _mapinfo->set_cloned_vtables(cloned_vtables);
 610 }
 611 
 612 class CollectCLDClosure : public CLDClosure {
 613   GrowableArray<ClassLoaderData*> _loaded_cld;
 614   GrowableArray<OopHandle> _loaded_cld_handles; // keep the CLDs alive
 615   Thread* _current_thread;
 616 public:
 617   CollectCLDClosure(Thread* thread) : _current_thread(thread) {}
 618   ~CollectCLDClosure() {
 619     for (int i = 0; i < _loaded_cld_handles.length(); i++) {
 620       _loaded_cld_handles.at(i).release(Universe::vm_global());
 621     }
 622   }
 623   void do_cld(ClassLoaderData* cld) {
 624     assert(cld->is_alive(), "must be");
 625     _loaded_cld.append(cld);
 626     _loaded_cld_handles.append(OopHandle(Universe::vm_global(), cld->holder()));
 627   }
 628 
 629   int nof_cld() const                { return _loaded_cld.length(); }
 630   ClassLoaderData* cld_at(int index) { return _loaded_cld.at(index); }
 631 };
 632 
 633 // Check if we can eagerly link this class at dump time, so we can avoid the
 634 // runtime linking overhead (especially verification)
 635 bool MetaspaceShared::may_be_eagerly_linked(InstanceKlass* ik) {
 636   if (CDSConfig::preserve_all_dumptime_verification_states(ik)) {
 637     assert(ik->can_be_verified_at_dumptime(), "sanity");
 638   }
 639   if (!ik->can_be_verified_at_dumptime()) {
 640     // For old classes, try to leave them in the unlinked state, so
 641     // we can still store them in the archive. They must be
 642     // linked/verified at runtime.
 643     return false;
 644   }
 645 
 646   if (CDSConfig::is_dumping_dynamic_archive() && ik->is_shared_unregistered_class()) {
 647     // Linking of unregistered classes at this stage may cause more
 648     // classes to be resolved, resulting in calls to ClassLoader.loadClass()
 649     // that may not be expected by custom class loaders.
 650     //
 651     // It's OK to do this for the built-in loaders as we know they can
 652     // tolerate this.
 653     return false;
 654   }
 655   return true;
 656 }
 657 









 658 void MetaspaceShared::link_shared_classes(bool jcmd_request, TRAPS) {
 659   ClassPrelinker::initialize();
 660 
 661   if (!jcmd_request && !CDSConfig::is_dumping_dynamic_archive()) {
 662     // If we have regenerated invoker classes in the dynamic archive,
 663     // they will conflict with the resolved CONSTANT_Klass references that are stored
 664     // in the static archive. This is not easy to handle. Let's disable
 665     // it for dynamic archive for now.
 666     LambdaFormInvokers::regenerate_holder_classes(CHECK);
 667   }
 668 
 669   // Collect all loaded ClassLoaderData.
 670   CollectCLDClosure collect_cld(THREAD);
 671   {
 672     // ClassLoaderDataGraph::loaded_cld_do requires ClassLoaderDataGraph_lock.
 673     // We cannot link the classes while holding this lock (or else we may run into deadlock).
 674     // Therefore, we need to first collect all the CLDs, and then link their classes after
 675     // releasing the lock.
 676     MutexLocker lock(ClassLoaderDataGraph_lock);
 677     ClassLoaderDataGraph::loaded_cld_do(&collect_cld);
 678   }
 679 
 680   while (true) {
 681     bool has_linked = false;
 682     for (int i = 0; i < collect_cld.nof_cld(); i++) {
 683       ClassLoaderData* cld = collect_cld.cld_at(i);
 684       for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) {
 685         if (klass->is_instance_klass()) {
 686           InstanceKlass* ik = InstanceKlass::cast(klass);
 687           if (may_be_eagerly_linked(ik)) {
 688             has_linked |= try_link_class(THREAD, ik);
 689           }
 690           if (CDSConfig::is_dumping_heap() && ik->is_linked() && !ik->is_initialized()) {
 691             ClassPrelinker::maybe_preinit_class(ik, CHECK);
 692           }
 693         }
 694       }
 695     }
 696 
 697     if (!has_linked) {
 698       break;
 699     }
 700     // Class linking includes verification which may load more classes.
 701     // Keep scanning until we have linked no more classes.
 702   }
 703 
 704   // Resolve constant pool entries -- we don't load any new classes during this stage
 705   for (int i = 0; i < collect_cld.nof_cld(); i++) {
 706     ClassLoaderData* cld = collect_cld.cld_at(i);
 707     for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) {
 708       if (klass->is_instance_klass()) {
 709         InstanceKlass* ik = InstanceKlass::cast(klass);
 710         ClassPrelinker::dumptime_resolve_constants(ik, CHECK);
 711         if (CDSConfig::is_dumping_preimage_static_archive()) {
 712           ClassPrelinker::record_reflection_data_flags_for_preimage(ik, CHECK);
 713         }
 714       }
 715     }
 716   }
 717 
 718   if (CDSConfig::is_dumping_final_static_archive()) {
 719     ClassPrelinker::apply_final_image_eager_linkage(CHECK);
 720   }
 721 }
 722 
 723 void MetaspaceShared::prepare_for_dumping() {
 724   assert(CDSConfig::is_dumping_archive(), "sanity");
 725   CDSConfig::check_unsupported_dumping_properties();
 726   ClassLoader::initialize_shared_path(JavaThread::current());
 727 }
 728 
 729 // Preload classes from a list, populate the shared spaces and dump to a
 730 // file.
 731 void MetaspaceShared::preload_and_dump() {
 732   EXCEPTION_MARK;
 733   ResourceMark rm(THREAD);
 734   HandleMark hm(THREAD);
 735 
 736   if (CDSConfig::is_dumping_final_static_archive() && PrintTrainingInfo) {
 737     tty->print_cr("==================== archived_training_data ** before dumping ====================");
 738     TrainingData::print_archived_training_data_on(tty);
 739   }
 740 
 741   StaticArchiveBuilder builder;
 742   preload_and_dump_impl(builder, THREAD);
 743   if (HAS_PENDING_EXCEPTION) {
 744     if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 745       log_error(cds)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
 746                      SIZE_FORMAT "M", MaxHeapSize/M);
 747       MetaspaceShared::unrecoverable_writing_error();
 748     } else {
 749       log_error(cds)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
 750                      java_lang_String::as_utf8_string(java_lang_Throwable::message(PENDING_EXCEPTION)));
 751       MetaspaceShared::unrecoverable_writing_error("VM exits due to exception, use -Xlog:cds,exceptions=trace for detail");
 752     }
 753   }
 754 }
 755 
 756 #if INCLUDE_CDS_JAVA_HEAP && defined(_LP64)
 757 void MetaspaceShared::adjust_heap_sizes_for_dumping() {
 758   if (!CDSConfig::is_dumping_heap() || UseCompressedOops) {
 759     return;
 760   }
 761   // CDS heap dumping requires all string oops to have an offset
 762   // from the heap bottom that can be encoded in 32-bit.

 823                                                     ClassListParser::_parse_all, CHECK);
 824   }
 825   if (classlist_path != default_classlist) {
 826     struct stat statbuf;
 827     if (os::stat(default_classlist, &statbuf) == 0) {
 828       // File exists, let's use it.
 829       class_count += ClassListParser::parse_classlist(default_classlist,
 830                                                       ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
 831     }
 832   }
 833 
 834   // Exercise the manifest processing code to ensure classes used by CDS at runtime
 835   // are always archived
 836   const char* dummy = "Manifest-Version: 1.0\n";
 837   CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
 838 
 839   log_info(cds)("Loading classes to share: done.");
 840   log_info(cds)("Shared spaces: preloaded %d classes", class_count);
 841 }
 842 
 843 void MetaspaceShared::preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) {
 844   if (CDSConfig::is_dumping_classic_static_archive()) {
 845     // We are running with -Xshare:dump
 846     preload_classes(CHECK);
 847 
 848     if (SharedArchiveConfigFile) {
 849       log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
 850       read_extra_data(THREAD, SharedArchiveConfigFile);
 851       log_info(cds)("Reading extra data: done.");
 852     }
 853   }
 854 
 855 #if INCLUDE_CDS_JAVA_HEAP
 856   if (CDSConfig::is_dumping_heap() && ArchiveInvokeDynamic) {
 857     // We also assume no other Java threads are running
 858     // This makes sure that the MethodType and MethodTypeForm objects are clean.
 859     JavaValue result(T_VOID);
 860     JavaCalls::call_static(&result, vmClasses::MethodType_klass(),
 861                            vmSymbols::dumpSharedArchive(),
 862                            vmSymbols::void_method_signature(),
 863                            CHECK);
 864   }
 865 #endif
 866 
 867   // Rewrite and link classes
 868   log_info(cds)("Rewriting and linking classes ...");

 869   // Link any classes which got missed. This would happen if we have loaded classes that
 870   // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
 871   // fails verification, all other interfaces that were not specified in the classlist but
 872   // are implemented by K are not verified.
 873   link_shared_classes(false/*not from jcmd*/, CHECK);
 874   log_info(cds)("Rewriting and linking classes: done");
 875 
 876   if (CDSConfig::is_dumping_final_static_archive()) {
 877     assert(RecordTraining == false, "must be");
 878     RecordTraining = true;
 879   }
 880 
 881   TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
 882 
 883   _method_handle_intrinsics = new (mtClassShared) GrowableArray<Method*>(256, mtClassShared);
 884   SystemDictionary::get_all_method_handle_intrinsics(_method_handle_intrinsics);
 885 
 886 #if INCLUDE_CDS_JAVA_HEAP
 887   if (CDSConfig::is_dumping_heap()) {
 888     if (!HeapShared::is_archived_boot_layer_available(THREAD)) {
 889       log_info(cds)("archivedBootLayer not available, disabling full module graph");
 890       CDSConfig::disable_dumping_full_module_graph();
 891     }
 892     HeapShared::init_for_dumping(CHECK);
 893     ArchiveHeapWriter::init();
 894     if (CDSConfig::is_dumping_full_module_graph()) {
 895       HeapShared::reset_archived_object_states(CHECK);
 896     }
 897 
 898     if (ArchiveLoaderLookupCache) {
 899       SystemDictionaryShared::create_loader_positive_lookup_cache(CHECK);
 900     }
 901 
 902     // Do this at the very end, when no Java code will be executed. Otherwise
 903     // some new strings may be added to the intern table.
 904     StringTable::allocate_shared_strings_array(CHECK);
 905   }
 906 #endif
 907 
 908   VM_PopulateDumpSharedSpace op(builder);
 909   VMThread::execute(&op);
 910   FileMapInfo* mapinfo = op.mapinfo();
 911   ArchiveHeapInfo* heap_info = op.heap_info();
 912 
 913   if (CDSConfig::is_dumping_preimage_static_archive()) {
 914     write_static_archive(&builder, mapinfo, heap_info);
 915     fork_and_dump_final_static_archive();
 916   } else if (CDSConfig::is_dumping_final_static_archive()) {
 917     RecordTraining = false;
 918     if (StoreCachedCode && CachedCodeFile != nullptr) { // FIXME: new workflow -- remove the CachedCodeFile flag
 919       if (log_is_enabled(Info, cds, jit)) {
 920         CDSAccess::test_heap_access_api();
 921       }
 922 
 923       // We have just created the final image. Let's run the AOT compiler
 924       if (PrintTrainingInfo) {
 925         tty->print_cr("==================== archived_training_data ** after dumping ====================");
 926         TrainingData::print_archived_training_data_on(tty);
 927       }
 928 
 929       CDSConfig::enable_dumping_cached_code();
 930       {
 931         builder.start_cc_region();
 932         Precompiler::compile_cached_code(&builder, CHECK);
 933         builder.end_cc_region();
 934       }
 935       CDSConfig::disable_dumping_cached_code();
 936 
 937       SCCache::close(); // Write final data and close archive
 938     }
 939     write_static_archive(&builder, mapinfo, heap_info);
 940   } else {
 941     write_static_archive(&builder, mapinfo, heap_info);
 942   }
 943 }
 944 
 945 void MetaspaceShared::write_static_archive(ArchiveBuilder* builder, FileMapInfo *mapinfo, ArchiveHeapInfo* heap_info) {
 946   // relocate the data so that it can be mapped to MetaspaceShared::requested_base_address()
 947   // without runtime relocation.
 948   builder->relocate_to_requested();
 949 
 950   mapinfo->open_for_write();
 951   builder->write_archive(mapinfo, heap_info);
 952 
 953   if (PrintSystemDictionaryAtExit) {
 954     SystemDictionary::print();
 955   }
 956 
 957   if (AllowArchivingWithJavaAgent) {
 958     log_warning(cds)("This archive was created with AllowArchivingWithJavaAgent. It should be used "
 959             "for testing purposes only and should not be used in a production environment");
 960   }
 961 }
 962 
 963 void MetaspaceShared::fork_and_dump_final_static_archive() {
 964   assert(CDSConfig::is_dumping_preimage_static_archive(), "sanity");
 965 
 966   ResourceMark rm;
 967   stringStream st;
 968   st.print("%s%sbin%sjava", Arguments::get_java_home(), os::file_separator(), os::file_separator());
 969   const char* cp = Arguments::get_appclasspath();
 970   if (cp != nullptr && strlen(cp) > 0 && strcmp(cp, ".") != 0) {
 971     st.print(" -cp ");  st.print_raw(cp);
 972   }
 973   for (int i = 0; i < Arguments::num_jvm_flags(); i++) {
 974     st.print(" %s", Arguments::jvm_flags_array()[i]);
 975   }
 976   for (int i = 0; i < Arguments::num_jvm_args(); i++) {
 977     st.print(" %s", Arguments::jvm_args_array()[i]);
 978   }
 979   st.print(" -XX:CDSPreimage=%s", SharedArchiveFile);
 980 
 981   const char* cmd = st.freeze();
 982   if (CDSManualFinalImage) {
 983     tty->print_cr("-XX:+CDSManualFinalImage is specified");
 984     tty->print_cr("Please manually execute the following command to create the final CDS image:");
 985     tty->print("    "); tty->print_raw_cr(cmd);
 986   } else {
 987     log_info(cds)("Launching child process to create final CDS image:");
 988     log_info(cds)("    %s", cmd);
 989     int status = os::fork_and_exec(cmd);
 990     if (status != 0) {
 991       log_error(cds)("Child process finished; status = %d", status);
 992       log_error(cds)("To reproduce the error");
 993       ResourceMark rm;
 994       LogStream ls(Log(cds)::error());
 995       ls.print("    "); ls.print_raw_cr(cmd);
 996       vm_direct_exit(status);
 997     } else {
 998       log_info(cds)("Child process finished; status = %d", status);
 999       status = remove(SharedArchiveFile);
1000       if (status != 0) {
1001         log_error(cds)("Failed to remove CDSPreimage file %s", SharedArchiveFile);
1002       } else {
1003         log_info(cds)("Removed CDSPreimage file %s", SharedArchiveFile);
1004       }
1005     }
1006   }
1007 }
1008 
1009 // Returns true if the class's status has changed.
1010 bool MetaspaceShared::try_link_class(JavaThread* current, InstanceKlass* ik) {
1011   ExceptionMark em(current);
1012   JavaThread* THREAD = current; // For exception macros.
1013   assert(CDSConfig::is_dumping_archive(), "sanity");
1014 
1015   if (ik->is_shared() && !CDSConfig::is_dumping_final_static_archive()) {
1016     assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
1017     return false;
1018   }
1019 
1020   if (ik->is_loaded() && !ik->is_linked() && ik->can_be_verified_at_dumptime() &&
1021       !SystemDictionaryShared::has_class_failed_verification(ik)) {
1022     bool saved = BytecodeVerificationLocal;
1023     if (ik->is_shared_unregistered_class() && ik->class_loader() == nullptr) {
1024       // The verification decision is based on BytecodeVerificationRemote
1025       // for non-system classes. Since we are using the null classloader
1026       // to load non-system classes for customized class loaders during dumping,
1027       // we need to temporarily change BytecodeVerificationLocal to be the same as
1028       // BytecodeVerificationRemote. Note this can cause the parent system
1029       // classes also being verified. The extra overhead is acceptable during
1030       // dumping.
1031       BytecodeVerificationLocal = BytecodeVerificationRemote;
1032     }
1033     ik->link_class(THREAD);
1034     if (HAS_PENDING_EXCEPTION) {
1035       ResourceMark rm(THREAD);
1036       log_warning(cds)("Preload Warning: Verification failed for %s",
1037                     ik->external_name());
1038       CLEAR_PENDING_EXCEPTION;
1039       SystemDictionaryShared::set_class_has_failed_verification(ik);
1040     }

1113   if (message != nullptr) {
1114     log_error(cds)("%s", message);
1115   }
1116   vm_exit_during_initialization("Unable to use shared archive.", nullptr);
1117 }
1118 
1119 // This function is called when the JVM is unable to write the specified CDS archive due to an
1120 // unrecoverable error.
1121 void MetaspaceShared::unrecoverable_writing_error(const char* message) {
1122   log_error(cds)("An error has occurred while writing the shared archive file.");
1123   if (message != nullptr) {
1124     log_error(cds)("%s", message);
1125   }
1126   vm_direct_exit(1);
1127 }
1128 
1129 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
1130   assert(UseSharedSpaces, "Must be called when UseSharedSpaces is enabled");
1131   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1132 
1133   FileMapInfo* static_mapinfo = FileMapInfo::current_info();
1134   FileMapInfo* dynamic_mapinfo = nullptr;
1135 
1136   if (static_mapinfo != nullptr) {
1137     log_info(cds)("Core region alignment: " SIZE_FORMAT, static_mapinfo->core_region_alignment());
1138     dynamic_mapinfo = open_dynamic_archive();
1139 
1140     // First try to map at the requested address
1141     result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1142     if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1143       // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1144       // by the OS.
1145       log_info(cds)("Try to map archive(s) at an alternative address");
1146       result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1147     }
1148   }
1149 
1150   if (result == MAP_ARCHIVE_SUCCESS) {
1151     bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());
1152     char* cds_base = static_mapinfo->mapped_base();
1153     char* cds_end =  dynamic_mapped ? dynamic_mapinfo->mapped_end() : static_mapinfo->mapped_end();

1160       FileMapInfo::set_shared_path_table(dynamic_mapinfo);
1161       // turn AutoCreateSharedArchive off if successfully mapped
1162       AutoCreateSharedArchive = false;
1163     } else {
1164       FileMapInfo::set_shared_path_table(static_mapinfo);
1165     }
1166   } else {
1167     set_shared_metaspace_range(nullptr, nullptr, nullptr);
1168     if (CDSConfig::is_dumping_dynamic_archive()) {
1169       log_warning(cds)("-XX:ArchiveClassesAtExit is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info.");
1170     }
1171     UseSharedSpaces = false;
1172     // The base archive cannot be mapped. We cannot dump the dynamic shared archive.
1173     AutoCreateSharedArchive = false;
1174     CDSConfig::disable_dumping_dynamic_archive();
1175     log_info(cds)("Unable to map shared spaces");
1176     if (PrintSharedArchiveAndExit) {
1177       MetaspaceShared::unrecoverable_loading_error("Unable to use shared archive.");
1178     } else if (RequireSharedSpaces) {
1179       MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1180     } else if (CDSConfig::is_dumping_final_static_archive()) {
1181       assert(CDSPreimage != nullptr, "must be");
1182       log_error(cds)("Unable to map shared spaces for CDSPreimage = %s", CDSPreimage);
1183       MetaspaceShared::unrecoverable_loading_error();
1184     }
1185   }
1186 
1187   // If mapping failed and -XShare:on, the vm should exit
1188   bool has_failed = false;
1189   if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1190     has_failed = true;
1191     delete static_mapinfo;
1192   }
1193   if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1194     has_failed = true;
1195     delete dynamic_mapinfo;
1196   }
1197   if (RequireSharedSpaces && has_failed) {
1198     // static archive mapped but dynamic archive failed
1199       MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1200   }
1201 }
1202 
1203 // This is called very early at VM start up to get the size of the cached_code region, which
1204 // is used in CodeCache::initialize_heaps()
1205 void MetaspaceShared::open_static_archive() {
1206   if (!UseSharedSpaces) {
1207     return;
1208   }
1209   const char* static_archive = CDSConfig::static_archive_path();
1210   assert(static_archive != nullptr, "sanity");
1211   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1212   if (!mapinfo->initialize()) {
1213     delete(mapinfo);
1214   } else {
1215     FileMapRegion* r = mapinfo->region_at(MetaspaceShared::cc);
1216     CDSAccess::set_cached_code_size(r->used());
1217   }

1218 }
1219 
1220 FileMapInfo* MetaspaceShared::open_dynamic_archive() {
1221   if (CDSConfig::is_dumping_dynamic_archive()) {
1222     return nullptr;
1223   }
1224   const char* dynamic_archive = CDSConfig::dynamic_archive_path();
1225   if (dynamic_archive == nullptr) {
1226     return nullptr;
1227   }
1228 
1229   FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1230   if (!mapinfo->initialize()) {
1231     delete(mapinfo);
1232     if (RequireSharedSpaces) {
1233       MetaspaceShared::unrecoverable_loading_error("Failed to initialize dynamic archive");
1234     }
1235     return nullptr;
1236   }
1237   return mapinfo;

1672 
1673 // Read the miscellaneous data from the shared file, and
1674 // serialize it out to its various destinations.
1675 
1676 void MetaspaceShared::initialize_shared_spaces() {
1677   FileMapInfo *static_mapinfo = FileMapInfo::current_info();
1678 
1679   // Verify various attributes of the archive, plus initialize the
1680   // shared string/symbol tables.
1681   char* buffer = static_mapinfo->serialized_data();
1682   intptr_t* array = (intptr_t*)buffer;
1683   ReadClosure rc(&array);
1684   serialize(&rc);
1685 
1686   // Finish up archived heap initialization. These must be
1687   // done after ReadClosure.
1688   static_mapinfo->patch_heap_embedded_pointers();
1689   ArchiveHeapLoader::finish_initialization();
1690 
1691   CDS_JAVA_HEAP_ONLY(Universe::update_archived_basic_type_mirrors());
1692   CDS_JAVA_HEAP_ONLY(Universe::update_exception_instances());
1693 
1694   SCCache::new_workflow_load_cache();
1695 
1696   // Close the mapinfo file
1697   static_mapinfo->close();
1698 
1699   static_mapinfo->unmap_region(MetaspaceShared::bm);
1700 
1701   FileMapInfo *dynamic_mapinfo = FileMapInfo::dynamic_info();
1702   if (dynamic_mapinfo != nullptr) {
1703     intptr_t* buffer = (intptr_t*)dynamic_mapinfo->serialized_data();
1704     ReadClosure rc(&buffer);
1705     ArchiveBuilder::serialize_dynamic_archivable_items(&rc);
1706     DynamicArchive::setup_array_klasses();
1707     dynamic_mapinfo->close();
1708     dynamic_mapinfo->unmap_region(MetaspaceShared::bm);
1709   }
1710 
1711   // Set up LambdaFormInvokers::_lambdaform_lines for dynamic dump
1712   if (CDSConfig::is_dumping_dynamic_archive()) {
1713     // Read stored LF format lines stored in static archive
1714     LambdaFormInvokers::read_static_archive_invokers();
1715   }
1716 
1717   if (PrintSharedArchiveAndExit) {
1718     // Print archive names
1719     if (dynamic_mapinfo != nullptr) {
1720       tty->print_cr("\n\nBase archive name: %s", CDSConfig::static_archive_path());
1721       tty->print_cr("Base archive version %d", static_mapinfo->version());
1722     } else {
1723       tty->print_cr("Static archive name: %s", static_mapinfo->full_path());
1724       tty->print_cr("Static archive version %d", static_mapinfo->version());
1725     }
1726 
1727     SystemDictionaryShared::print_shared_archive(tty);
1728     if (dynamic_mapinfo != nullptr) {
1729       tty->print_cr("\n\nDynamic archive name: %s", dynamic_mapinfo->full_path());
1730       tty->print_cr("Dynamic archive version %d", dynamic_mapinfo->version());
1731       SystemDictionaryShared::print_shared_archive(tty, false/*dynamic*/);
1732     }
1733     TrainingData::print_archived_training_data_on(tty);
1734 
1735     if (LoadCachedCode) {
1736       tty->print_cr("\n\nCached Code file: %s", CachedCodeFile);
1737       SCCache::print_on(tty);
1738     }
1739 
1740     // collect shared symbols and strings
1741     CountSharedSymbols cl;
1742     SymbolTable::shared_symbols_do(&cl);
1743     tty->print_cr("Number of shared symbols: %d", cl.total());
1744     tty->print_cr("Number of shared strings: %zu", StringTable::shared_entry_count());
1745     tty->print_cr("VM version: %s\r\n", static_mapinfo->vm_version());
1746     if (FileMapInfo::current_info() == nullptr || _archive_loading_failed) {
1747       tty->print_cr("archive is invalid");
1748       vm_exit(1);
1749     } else {
1750       tty->print_cr("archive is valid");
1751       vm_exit(0);
1752     }
1753   }
1754 }
1755 
1756 // JVM/TI RedefineClasses() support:
1757 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
1758   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
< prev index next >