< prev index next >

src/hotspot/share/cds/metaspaceShared.cpp

Print this page

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

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

 242     err = "invalid for this platform";
 243   } else {
 244     return aligned_base;
 245   }
 246 
 247   log_warning(cds)("SharedBaseAddress (" INTPTR_FORMAT ") is %s. Reverted to " INTPTR_FORMAT,
 248                    p2i((void*)SharedBaseAddress), err,
 249                    p2i((void*)Arguments::default_SharedBaseAddress()));
 250 
 251   specified_base = (char*)Arguments::default_SharedBaseAddress();
 252   aligned_base = align_up(specified_base, MetaspaceShared::core_region_alignment());
 253 
 254   // Make sure the default value of SharedBaseAddress specified in globals.hpp is sane.
 255   assert(!shared_base_too_high(specified_base, aligned_base, cds_max), "Sanity");
 256   assert(shared_base_valid(aligned_base), "Sanity");
 257   return aligned_base;
 258 }
 259 
 260 void MetaspaceShared::initialize_for_static_dump() {
 261   assert(CDSConfig::is_dumping_static_archive(), "sanity");








 262   log_info(cds)("Core region alignment: " SIZE_FORMAT, core_region_alignment());
 263   // The max allowed size for CDS archive. We use this to limit SharedBaseAddress
 264   // to avoid address space wrap around.
 265   size_t cds_max;
 266   const size_t reserve_alignment = core_region_alignment();
 267 
 268 #ifdef _LP64
 269   const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
 270   cds_max = align_down(UnscaledClassSpaceMax, reserve_alignment);
 271 #else
 272   // We don't support archives larger than 256MB on 32-bit due to limited
 273   //  virtual address space.
 274   cds_max = align_down(256*M, reserve_alignment);
 275 #endif
 276 
 277   _requested_base_address = compute_shared_base(cds_max);
 278   SharedBaseAddress = (size_t)_requested_base_address;
 279 
 280   size_t symbol_rs_size = LP64_ONLY(3 * G) NOT_LP64(128 * M);
 281   _symbol_rs = ReservedSpace(symbol_rs_size);

 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         ClassLoaderExt::init_num_module_paths(info->header()->num_module_paths());
 305       }
 306     }
 307   }
 308 }
 309 
 310 static GrowableArrayCHeap<OopHandle, mtClassShared>* _extra_interned_strings = nullptr;
 311 static GrowableArrayCHeap<Symbol*, mtClassShared>* _extra_symbols = 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 // Read/write a data stream for restoring/preserving metadata pointers and
 363 // miscellaneous data from/to the shared archive file.
 364 
 365 void MetaspaceShared::serialize(SerializeClosure* soc) {
 366   int tag = 0;
 367   soc->do_tag(--tag);
 368 
 369   // Verify the sizes of various metadata in the system.
 370   soc->do_tag(sizeof(Method));
 371   soc->do_tag(sizeof(ConstMethod));
 372   soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE));
 373   soc->do_tag(sizeof(ConstantPool));
 374   soc->do_tag(sizeof(ConstantPoolCache));
 375   soc->do_tag(objArrayOopDesc::base_offset_in_bytes());
 376   soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE));
 377   soc->do_tag(sizeof(Symbol));
 378 
 379   // Need to do this first, as subsequent steps may call virtual functions
 380   // in archived Metadata objects.
 381   CppVtables::serialize(soc);
 382   soc->do_tag(--tag);
 383 
 384   // Dump/restore miscellaneous metadata.
 385   JavaClasses::serialize_offsets(soc);
 386   Universe::serialize(soc);
 387   soc->do_tag(--tag);
 388 
 389   // Dump/restore references to commonly used names and signatures.
 390   vmSymbols::serialize(soc);
 391   soc->do_tag(--tag);
 392 
 393   // Dump/restore the symbol/string/subgraph_info tables
 394   SymbolTable::serialize_shared_table_header(soc);
 395   StringTable::serialize_shared_table_header(soc);
 396   HeapShared::serialize_tables(soc);
 397   SystemDictionaryShared::serialize_dictionary_headers(soc);
 398 


 399   InstanceMirrorKlass::serialize_offsets(soc);
 400 
 401   // Dump/restore well known classes (pointers)
 402   SystemDictionaryShared::serialize_vm_classes(soc);
 403   soc->do_tag(--tag);
 404 
 405   CDS_JAVA_HEAP_ONLY(Modules::serialize(soc);)
 406   CDS_JAVA_HEAP_ONLY(ClassLoaderDataShared::serialize(soc);)


 407 
 408   LambdaFormInvokers::serialize(soc);
 409   soc->do_tag(666);
 410 }
 411 
 412 static void rewrite_nofast_bytecode(const methodHandle& method) {
 413   BytecodeStream bcs(method);
 414   while (!bcs.is_last_bytecode()) {
 415     Bytecodes::Code opcode = bcs.next();
 416     switch (opcode) {
 417     case Bytecodes::_getfield:      *bcs.bcp() = Bytecodes::_nofast_getfield;      break;
 418     case Bytecodes::_putfield:      *bcs.bcp() = Bytecodes::_nofast_putfield;      break;
 419     case Bytecodes::_aload_0:       *bcs.bcp() = Bytecodes::_nofast_aload_0;       break;
 420     case Bytecodes::_iload: {
 421       if (!bcs.is_wide()) {
 422         *bcs.bcp() = Bytecodes::_nofast_iload;
 423       }
 424       break;
 425     }
 426     default: break;

 462     VM_Operation(), _heap_info(), _map_info(nullptr), _builder(b) {}
 463 
 464   bool skip_operation() const { return false; }
 465 
 466   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
 467   ArchiveHeapInfo* heap_info()  { return &_heap_info; }
 468   FileMapInfo* map_info() const { return _map_info; }
 469   void doit();   // outline because gdb sucks
 470   bool allow_nested_vm_operations() const { return true; }
 471 }; // class VM_PopulateDumpSharedSpace
 472 
 473 class StaticArchiveBuilder : public ArchiveBuilder {
 474 public:
 475   StaticArchiveBuilder() : ArchiveBuilder() {}
 476 
 477   virtual void iterate_roots(MetaspaceClosure* it) {
 478     FileMapInfo::metaspace_pointers_do(it);
 479     SystemDictionaryShared::dumptime_classes_do(it);
 480     Universe::metaspace_pointers_do(it);
 481     vmSymbols::metaspace_pointers_do(it);

 482 
 483     // The above code should find all the symbols that are referenced by the
 484     // archived classes. We just need to add the extra symbols which
 485     // may not be used by any of the archived classes -- these are usually
 486     // symbols that we anticipate to be used at run time, so we can store
 487     // them in the RO region, to be shared across multiple processes.
 488     if (_extra_symbols != nullptr) {
 489       for (int i = 0; i < _extra_symbols->length(); i++) {
 490         it->push(_extra_symbols->adr_at(i));
 491       }
 492     }




 493   }
 494 };
 495 
 496 char* VM_PopulateDumpSharedSpace::dump_read_only_tables() {
 497   ArchiveBuilder::OtherROAllocMark mark;
 498 
 499   SystemDictionaryShared::write_to_archive();







 500 
 501   // Write lambform lines into archive
 502   LambdaFormInvokers::dump_static_archive_invokers();
 503   // Write module name into archive
 504   CDS_JAVA_HEAP_ONLY(Modules::dump_main_module_name();)
 505   // Write the other data to the output array.
 506   DumpRegion* ro_region = ArchiveBuilder::current()->ro_region();
 507   char* start = ro_region->top();
 508   WriteClosure wc(ro_region);
 509   MetaspaceShared::serialize(&wc);
 510 
 511   return start;
 512 }
 513 
 514 void VM_PopulateDumpSharedSpace::doit() {
 515   guarantee(!CDSConfig::is_using_archive(), "We should not be using an archive when we dump");
 516 
 517   DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm);
 518 





















 519   FileMapInfo::check_nonempty_dir_in_shared_path_table();
 520 
 521   NOT_PRODUCT(SystemDictionary::verify();)
 522 
 523   // Block concurrent class unloading from changing the _dumptime_table
 524   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
 525   SystemDictionaryShared::check_excluded_classes();

 526 
 527   _builder.gather_source_objs();
 528   _builder.reserve_buffer();
 529 
 530   CppVtables::dumptime_init(&_builder);
 531 
 532   _builder.sort_metadata_objs();
 533   _builder.dump_rw_metadata();
 534   _builder.dump_ro_metadata();
 535   _builder.relocate_metaspaceobj_embedded_pointers();
 536 
 537   dump_java_heap_objects(_builder.klasses());
 538   dump_shared_symbol_table(_builder.symbols());
 539 
 540   log_info(cds)("Make classes shareable");
 541   _builder.make_klasses_shareable();

 542 
 543   char* serialized_data = dump_read_only_tables();
 544 
 545   SystemDictionaryShared::adjust_lambda_proxy_class_dictionary();
 546 



 547   // The vtable clones contain addresses of the current process.
 548   // We don't want to write these addresses into the archive.
 549   CppVtables::zero_archived_vtables();
 550 
 551   // Write the archive file
 552   const char* static_archive = CDSConfig::static_archive_path();







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



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

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






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



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




























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







 661   StaticArchiveBuilder builder;
 662   preload_and_dump_impl(builder, THREAD);
 663   if (HAS_PENDING_EXCEPTION) {
 664     if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 665       log_error(cds)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
 666                      SIZE_FORMAT "M", MaxHeapSize/M);
 667       MetaspaceShared::writing_error();
 668     } else {
 669       log_error(cds)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
 670                      java_lang_String::as_utf8_string(java_lang_Throwable::message(PENDING_EXCEPTION)));
 671       MetaspaceShared::writing_error("Unexpected exception, use -Xlog:cds,exceptions=trace for detail");
 672     }
 673   }
 674 }
 675 
 676 #if INCLUDE_CDS_JAVA_HEAP && defined(_LP64)
 677 void MetaspaceShared::adjust_heap_sizes_for_dumping() {
 678   if (!CDSConfig::is_dumping_heap() || UseCompressedOops) {
 679     return;
 680   }

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









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





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







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
















 793     // Do this at the very end, when no Java code will be executed. Otherwise
 794     // some new strings may be added to the intern table.
 795     StringTable::allocate_shared_strings_array(CHECK);
 796   } else {
 797     log_info(cds)("Not dumping heap, reset CDSConfig::_is_using_optimized_module_handling");
 798     CDSConfig::stop_using_optimized_module_handling();
 799   }
 800 #endif
 801 
 802   // Dummy call to load classes used at CDS runtime
 803   JavaValue result(T_OBJECT);
 804   Handle path_string = java_lang_String::create_from_str("dummy.jar", CHECK);
 805   JavaCalls::call_static(&result,
 806                          vmClasses::jdk_internal_loader_ClassLoaders_klass(),
 807                          vmSymbols::toFileURL_name(),
 808                          vmSymbols::toFileURL_signature(),
 809                          path_string,
 810                          CHECK);
 811 
 812   VM_PopulateDumpSharedSpace op(builder);
 813   VMThread::execute(&op);



























 814 
 815   if (!write_static_archive(&builder, op.map_info(), op.heap_info())) {







 816     THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
 817   }
 818 }
 819 
 820 bool MetaspaceShared::write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info) {
 821   // relocate the data so that it can be mapped to MetaspaceShared::requested_base_address()
 822   // without runtime relocation.
 823   builder->relocate_to_requested();
 824 
 825   map_info->open_for_write();
 826   if (!map_info->is_open()) {
 827     return false;
 828   }
 829   builder->write_archive(map_info, heap_info);
 830 
 831   if (AllowArchivingWithJavaAgent) {
 832     log_warning(cds)("This archive was created with AllowArchivingWithJavaAgent. It should be used "
 833             "for testing purposes only and should not be used in a production environment");
 834   }
 835   return true;
 836 }
 837 




















































































 838 // Returns true if the class's status has changed.
 839 bool MetaspaceShared::try_link_class(JavaThread* current, InstanceKlass* ik) {
 840   ExceptionMark em(current);
 841   JavaThread* THREAD = current; // For exception macros.
 842   assert(CDSConfig::is_dumping_archive(), "sanity");
 843   if (!ik->is_shared() && ik->is_loaded() && !ik->is_linked() && ik->can_be_verified_at_dumptime() &&






 844       !SystemDictionaryShared::has_class_failed_verification(ik)) {
 845     bool saved = BytecodeVerificationLocal;
 846     if (ik->is_shared_unregistered_class() && ik->class_loader() == nullptr) {
 847       // The verification decision is based on BytecodeVerificationRemote
 848       // for non-system classes. Since we are using the null classloader
 849       // to load non-system classes for customized class loaders during dumping,
 850       // we need to temporarily change BytecodeVerificationLocal to be the same as
 851       // BytecodeVerificationRemote. Note this can cause the parent system
 852       // classes also being verified. The extra overhead is acceptable during
 853       // dumping.
 854       BytecodeVerificationLocal = BytecodeVerificationRemote;
 855     }
 856     ik->link_class(THREAD);
 857     if (HAS_PENDING_EXCEPTION) {
 858       ResourceMark rm(THREAD);
 859       log_warning(cds)("Preload Warning: Verification failed for %s",
 860                     ik->external_name());
 861       CLEAR_PENDING_EXCEPTION;
 862       SystemDictionaryShared::set_class_has_failed_verification(ik);
 863     }
 864     ik->compute_has_loops_flag_for_methods();
 865     BytecodeVerificationLocal = saved;
 866     return true;
 867   } else {
 868     return false;
 869   }
 870 }
 871 
 872 #if INCLUDE_CDS_JAVA_HEAP
 873 void VM_PopulateDumpSharedSpace::dump_java_heap_objects(GrowableArray<Klass*>* klasses) {
 874   if(!HeapShared::can_write()) {
 875     log_info(cds)(

 876       "Archived java heap is not supported as UseG1GC "
 877       "and UseCompressedClassPointers are required."
 878       "Current settings: UseG1GC=%s, UseCompressedClassPointers=%s.",
 879       BOOL_TO_STR(UseG1GC), BOOL_TO_STR(UseCompressedClassPointers));

 880     return;
 881   }
 882   // Find all the interned strings that should be dumped.
 883   int i;
 884   for (i = 0; i < klasses->length(); i++) {
 885     Klass* k = klasses->at(i);
 886     if (k->is_instance_klass()) {
 887       InstanceKlass* ik = InstanceKlass::cast(k);
 888       if (ik->is_linked()) {
 889         ik->constants()->add_dumped_interned_strings();
 890       }
 891     }
 892   }
 893   if (_extra_interned_strings != nullptr) {
 894     for (i = 0; i < _extra_interned_strings->length(); i ++) {
 895       OopHandle string = _extra_interned_strings->at(i);
 896       HeapShared::add_to_dumped_interned_strings(string.resolve());
 897     }
 898   }
 899 

 942 // This function is called when the JVM is unable to write the specified CDS archive due to an
 943 // unrecoverable error.
 944 void MetaspaceShared::unrecoverable_writing_error(const char* message) {
 945   writing_error(message);
 946   vm_direct_exit(1);
 947 }
 948 
 949 // This function is called when the JVM is unable to write the specified CDS archive due to a
 950 // an error. The error will be propagated
 951 void MetaspaceShared::writing_error(const char* message) {
 952   log_error(cds)("An error has occurred while writing the shared archive file.");
 953   if (message != nullptr) {
 954     log_error(cds)("%s", message);
 955   }
 956 }
 957 
 958 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
 959   assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
 960   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
 961 
 962   FileMapInfo* static_mapinfo = open_static_archive();
 963   FileMapInfo* dynamic_mapinfo = nullptr;
 964 
 965   if (static_mapinfo != nullptr) {
 966     log_info(cds)("Core region alignment: " SIZE_FORMAT, static_mapinfo->core_region_alignment());
 967     dynamic_mapinfo = open_dynamic_archive();
 968 
 969     // First try to map at the requested address
 970     result = map_archives(static_mapinfo, dynamic_mapinfo, true);
 971     if (result == MAP_ARCHIVE_MMAP_FAILURE) {
 972       // Mapping has failed (probably due to ASLR). Let's map at an address chosen
 973       // by the OS.
 974       log_info(cds)("Try to map archive(s) at an alternative address");
 975       result = map_archives(static_mapinfo, dynamic_mapinfo, false);
 976     }
 977   }
 978 
 979   if (result == MAP_ARCHIVE_SUCCESS) {
 980     bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());
 981     char* cds_base = static_mapinfo->mapped_base();
 982     char* cds_end =  dynamic_mapped ? dynamic_mapinfo->mapped_end() : static_mapinfo->mapped_end();

 989       FileMapInfo::set_shared_path_table(dynamic_mapinfo);
 990       // turn AutoCreateSharedArchive off if successfully mapped
 991       AutoCreateSharedArchive = false;
 992     } else {
 993       FileMapInfo::set_shared_path_table(static_mapinfo);
 994     }
 995   } else {
 996     set_shared_metaspace_range(nullptr, nullptr, nullptr);
 997     if (CDSConfig::is_dumping_dynamic_archive()) {
 998       log_warning(cds)("-XX:ArchiveClassesAtExit is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info.");
 999     }
1000     UseSharedSpaces = false;
1001     // The base archive cannot be mapped. We cannot dump the dynamic shared archive.
1002     AutoCreateSharedArchive = false;
1003     CDSConfig::disable_dumping_dynamic_archive();
1004     log_info(cds)("Unable to map shared spaces");
1005     if (PrintSharedArchiveAndExit) {
1006       MetaspaceShared::unrecoverable_loading_error("Unable to use shared archive.");
1007     } else if (RequireSharedSpaces) {
1008       MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");




1009     }
1010   }
1011 
1012   // If mapping failed and -XShare:on, the vm should exit
1013   bool has_failed = false;
1014   if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1015     has_failed = true;
1016     delete static_mapinfo;
1017   }
1018   if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1019     has_failed = true;
1020     delete dynamic_mapinfo;
1021   }
1022   if (RequireSharedSpaces && has_failed) {

1023       MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1024   }
1025 }
1026 
1027 FileMapInfo* MetaspaceShared::open_static_archive() {





1028   const char* static_archive = CDSConfig::static_archive_path();
1029   assert(static_archive != nullptr, "sanity");
1030   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1031   if (!mapinfo->initialize()) {
1032     delete(mapinfo);
1033     return nullptr;


1034   }
1035   return mapinfo;
1036 }
1037 
1038 FileMapInfo* MetaspaceShared::open_dynamic_archive() {
1039   if (CDSConfig::is_dumping_dynamic_archive()) {
1040     return nullptr;
1041   }
1042   const char* dynamic_archive = CDSConfig::dynamic_archive_path();
1043   if (dynamic_archive == nullptr) {
1044     return nullptr;
1045   }
1046 
1047   FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1048   if (!mapinfo->initialize()) {
1049     delete(mapinfo);
1050     if (RequireSharedSpaces) {
1051       MetaspaceShared::unrecoverable_loading_error("Failed to initialize dynamic archive");
1052     }
1053     return nullptr;
1054   }
1055   return mapinfo;

1434     if (archive_space_rs.is_reserved()) {
1435       log_debug(cds)("Released shared space (archive) " INTPTR_FORMAT, p2i(archive_space_rs.base()));
1436       archive_space_rs.release();
1437     }
1438     if (class_space_rs.is_reserved()) {
1439       log_debug(cds)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
1440       class_space_rs.release();
1441     }
1442   }
1443 }
1444 
1445 static int archive_regions[]     = { MetaspaceShared::rw, MetaspaceShared::ro };
1446 static int archive_regions_count = 2;
1447 
1448 MapArchiveResult MetaspaceShared::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
1449   assert(CDSConfig::is_using_archive(), "must be runtime");
1450   if (mapinfo == nullptr) {
1451     return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
1452   }
1453 




1454   mapinfo->set_is_mapped(false);
1455   if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
1456     log_info(cds)("Unable to map CDS archive -- core_region_alignment() expected: " SIZE_FORMAT
1457                   " actual: " SIZE_FORMAT, mapinfo->core_region_alignment(), core_region_alignment());
1458     return MAP_ARCHIVE_OTHER_FAILURE;
1459   }
1460 
1461   MapArchiveResult result =
1462     mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
1463 
1464   if (result != MAP_ARCHIVE_SUCCESS) {
1465     unmap_archive(mapinfo);
1466     return result;
1467   }
1468 
1469   if (!mapinfo->validate_shared_path_table()) {
1470     unmap_archive(mapinfo);
1471     return MAP_ARCHIVE_OTHER_FAILURE;
1472   }
1473 

1498 };
1499 
1500 // Read the miscellaneous data from the shared file, and
1501 // serialize it out to its various destinations.
1502 
1503 void MetaspaceShared::initialize_shared_spaces() {
1504   FileMapInfo *static_mapinfo = FileMapInfo::current_info();
1505 
1506   // Verify various attributes of the archive, plus initialize the
1507   // shared string/symbol tables.
1508   char* buffer = static_mapinfo->serialized_data();
1509   intptr_t* array = (intptr_t*)buffer;
1510   ReadClosure rc(&array);
1511   serialize(&rc);
1512 
1513   // Finish up archived heap initialization. These must be
1514   // done after ReadClosure.
1515   static_mapinfo->patch_heap_embedded_pointers();
1516   ArchiveHeapLoader::finish_initialization();
1517   Universe::load_archived_object_instances();

1518 
1519   // Close the mapinfo file
1520   static_mapinfo->close();
1521 
1522   static_mapinfo->unmap_region(MetaspaceShared::bm);
1523 
1524   FileMapInfo *dynamic_mapinfo = FileMapInfo::dynamic_info();
1525   if (dynamic_mapinfo != nullptr) {
1526     intptr_t* buffer = (intptr_t*)dynamic_mapinfo->serialized_data();
1527     ReadClosure rc(&buffer);
1528     ArchiveBuilder::serialize_dynamic_archivable_items(&rc);
1529     DynamicArchive::setup_array_klasses();
1530     dynamic_mapinfo->close();
1531     dynamic_mapinfo->unmap_region(MetaspaceShared::bm);
1532   }
1533 






1534   // Set up LambdaFormInvokers::_lambdaform_lines for dynamic dump
1535   if (CDSConfig::is_dumping_dynamic_archive()) {
1536     // Read stored LF format lines stored in static archive
1537     LambdaFormInvokers::read_static_archive_invokers();
1538   }
1539 
1540   if (PrintSharedArchiveAndExit) {
1541     // Print archive names
1542     if (dynamic_mapinfo != nullptr) {
1543       tty->print_cr("\n\nBase archive name: %s", CDSConfig::static_archive_path());
1544       tty->print_cr("Base archive version %d", static_mapinfo->version());
1545     } else {
1546       tty->print_cr("Static archive name: %s", static_mapinfo->full_path());
1547       tty->print_cr("Static archive version %d", static_mapinfo->version());
1548     }
1549 
1550     SystemDictionaryShared::print_shared_archive(tty);
1551     if (dynamic_mapinfo != nullptr) {
1552       tty->print_cr("\n\nDynamic archive name: %s", dynamic_mapinfo->full_path());
1553       tty->print_cr("Dynamic archive version %d", dynamic_mapinfo->version());
1554       SystemDictionaryShared::print_shared_archive(tty, false/*dynamic*/);
1555     }






1556 
1557     // collect shared symbols and strings
1558     CountSharedSymbols cl;
1559     SymbolTable::shared_symbols_do(&cl);
1560     tty->print_cr("Number of shared symbols: %d", cl.total());
1561     tty->print_cr("Number of shared strings: %zu", StringTable::shared_entry_count());
1562     tty->print_cr("VM version: %s\r\n", static_mapinfo->vm_version());
1563     if (FileMapInfo::current_info() == nullptr || _archive_loading_failed) {
1564       tty->print_cr("archive is invalid");
1565       vm_exit(1);
1566     } else {
1567       tty->print_cr("archive is valid");
1568       vm_exit(0);
1569     }
1570   }
1571 }
1572 
1573 // JVM/TI RedefineClasses() support:
1574 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
1575   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");

   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/aotClassInitializer.hpp"
  27 #include "cds/aotClassLinker.hpp"
  28 #include "cds/aotConstantPoolResolver.hpp"
  29 #include "cds/aotLinkedClassBulkLoader.hpp"
  30 #include "cds/archiveBuilder.hpp"
  31 #include "cds/archiveHeapLoader.hpp"
  32 #include "cds/archiveHeapWriter.hpp"
  33 #include "cds/cds_globals.hpp"
  34 #include "cds/cdsAccess.hpp"
  35 #include "cds/cdsConfig.hpp"
  36 #include "cds/cdsProtectionDomain.hpp"
  37 #include "cds/cds_globals.hpp"
  38 #include "cds/classListParser.hpp"
  39 #include "cds/classListWriter.hpp"

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

 254     err = "invalid for this platform";
 255   } else {
 256     return aligned_base;
 257   }
 258 
 259   log_warning(cds)("SharedBaseAddress (" INTPTR_FORMAT ") is %s. Reverted to " INTPTR_FORMAT,
 260                    p2i((void*)SharedBaseAddress), err,
 261                    p2i((void*)Arguments::default_SharedBaseAddress()));
 262 
 263   specified_base = (char*)Arguments::default_SharedBaseAddress();
 264   aligned_base = align_up(specified_base, MetaspaceShared::core_region_alignment());
 265 
 266   // Make sure the default value of SharedBaseAddress specified in globals.hpp is sane.
 267   assert(!shared_base_too_high(specified_base, aligned_base, cds_max), "Sanity");
 268   assert(shared_base_valid(aligned_base), "Sanity");
 269   return aligned_base;
 270 }
 271 
 272 void MetaspaceShared::initialize_for_static_dump() {
 273   assert(CDSConfig::is_dumping_static_archive(), "sanity");
 274 
 275   if (CDSConfig::is_dumping_preimage_static_archive() || CDSConfig::is_dumping_final_static_archive()) {
 276     if (!((UseG1GC || UseParallelGC || UseSerialGC || UseEpsilonGC || UseShenandoahGC) && UseCompressedClassPointers)) {
 277       vm_exit_during_initialization("Cannot create the CacheDataStore",
 278                                     "UseCompressedClassPointers must be enabled, and collector must be G1, Parallel, Serial, Epsilon, or Shenandoah");
 279     }
 280   }
 281 
 282   log_info(cds)("Core region alignment: " SIZE_FORMAT, core_region_alignment());
 283   // The max allowed size for CDS archive. We use this to limit SharedBaseAddress
 284   // to avoid address space wrap around.
 285   size_t cds_max;
 286   const size_t reserve_alignment = core_region_alignment();
 287 
 288 #ifdef _LP64
 289   const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
 290   cds_max = align_down(UnscaledClassSpaceMax, reserve_alignment);
 291 #else
 292   // We don't support archives larger than 256MB on 32-bit due to limited
 293   //  virtual address space.
 294   cds_max = align_down(256*M, reserve_alignment);
 295 #endif
 296 
 297   _requested_base_address = compute_shared_base(cds_max);
 298   SharedBaseAddress = (size_t)_requested_base_address;
 299 
 300   size_t symbol_rs_size = LP64_ONLY(3 * G) NOT_LP64(128 * M);
 301   _symbol_rs = ReservedSpace(symbol_rs_size);

 312     int size = FileMapInfo::get_number_of_shared_paths();
 313     if (size > 0) {
 314       CDSProtectionDomain::allocate_shared_data_arrays(size, CHECK);
 315       if (!CDSConfig::is_dumping_dynamic_archive()) {
 316         FileMapInfo* info;
 317         if (FileMapInfo::dynamic_info() == nullptr) {
 318           info = FileMapInfo::current_info();
 319         } else {
 320           info = FileMapInfo::dynamic_info();
 321         }
 322         ClassLoaderExt::init_paths_start_index(info->app_class_paths_start_index());
 323         ClassLoaderExt::init_app_module_paths_start_index(info->app_module_paths_start_index());
 324         ClassLoaderExt::init_num_module_paths(info->header()->num_module_paths());
 325       }
 326     }
 327   }
 328 }
 329 
 330 static GrowableArrayCHeap<OopHandle, mtClassShared>* _extra_interned_strings = nullptr;
 331 static GrowableArrayCHeap<Symbol*, mtClassShared>* _extra_symbols = nullptr;
 332 static GrowableArray<Method*>* _method_handle_intrinsics = nullptr;
 333 
 334 void MetaspaceShared::read_extra_data(JavaThread* current, const char* filename) {
 335   _extra_interned_strings = new GrowableArrayCHeap<OopHandle, mtClassShared>(10000);
 336   _extra_symbols = new GrowableArrayCHeap<Symbol*, mtClassShared>(1000);
 337 
 338   HashtableTextDump reader(filename);
 339   reader.check_version("VERSION: 1.0");
 340 
 341   while (reader.remain() > 0) {
 342     int utf8_length;
 343     int prefix_type = reader.scan_prefix(&utf8_length);
 344     ResourceMark rm(current);
 345     if (utf8_length == 0x7fffffff) {
 346       // buf_len will overflown 32-bit value.
 347       log_error(cds)("string length too large: %d", utf8_length);
 348       MetaspaceShared::unrecoverable_loading_error();
 349     }
 350     int buf_len = utf8_length+1;
 351     char* utf8_buffer = NEW_RESOURCE_ARRAY(char, buf_len);
 352     reader.get_utf8(utf8_buffer, utf8_length);

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

 506     VM_Operation(), _heap_info(), _map_info(nullptr), _builder(b) {}
 507 
 508   bool skip_operation() const { return false; }
 509 
 510   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
 511   ArchiveHeapInfo* heap_info()  { return &_heap_info; }
 512   FileMapInfo* map_info() const { return _map_info; }
 513   void doit();   // outline because gdb sucks
 514   bool allow_nested_vm_operations() const { return true; }
 515 }; // class VM_PopulateDumpSharedSpace
 516 
 517 class StaticArchiveBuilder : public ArchiveBuilder {
 518 public:
 519   StaticArchiveBuilder() : ArchiveBuilder() {}
 520 
 521   virtual void iterate_roots(MetaspaceClosure* it) {
 522     FileMapInfo::metaspace_pointers_do(it);
 523     SystemDictionaryShared::dumptime_classes_do(it);
 524     Universe::metaspace_pointers_do(it);
 525     vmSymbols::metaspace_pointers_do(it);
 526     TrainingData::iterate_roots(it);
 527 
 528     // The above code should find all the symbols that are referenced by the
 529     // archived classes. We just need to add the extra symbols which
 530     // may not be used by any of the archived classes -- these are usually
 531     // symbols that we anticipate to be used at run time, so we can store
 532     // them in the RO region, to be shared across multiple processes.
 533     if (_extra_symbols != nullptr) {
 534       for (int i = 0; i < _extra_symbols->length(); i++) {
 535         it->push(_extra_symbols->adr_at(i));
 536       }
 537     }
 538 
 539     for (int i = 0; i < _method_handle_intrinsics->length(); i++) {
 540       it->push(_method_handle_intrinsics->adr_at(i));
 541     }
 542   }
 543 };
 544 
 545 char* VM_PopulateDumpSharedSpace::dump_read_only_tables() {
 546   ArchiveBuilder::OtherROAllocMark mark;
 547 
 548   SystemDictionaryShared::write_to_archive();
 549   AOTClassLinker::write_to_archive();
 550   if (CDSConfig::is_dumping_preimage_static_archive()) {
 551     FinalImageRecipes::record_recipes();
 552   }
 553   AOTLinkedClassBulkLoader::record_unregistered_classes();
 554   TrainingData::dump_training_data();
 555   MetaspaceShared::write_method_handle_intrinsics();
 556 
 557   // Write lambform lines into archive
 558   LambdaFormInvokers::dump_static_archive_invokers();
 559   // Write module name into archive
 560   CDS_JAVA_HEAP_ONLY(Modules::dump_main_module_name();)
 561   // Write the other data to the output array.
 562   DumpRegion* ro_region = ArchiveBuilder::current()->ro_region();
 563   char* start = ro_region->top();
 564   WriteClosure wc(ro_region);
 565   MetaspaceShared::serialize(&wc);
 566 
 567   return start;
 568 }
 569 
 570 void VM_PopulateDumpSharedSpace::doit() {
 571   //guarantee(!CDSConfig::is_using_archive(), "We should not be using an archive when we dump");
 572 
 573   DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm);
 574 
 575   _method_handle_intrinsics = new (mtClassShared) GrowableArray<Method*>(256, mtClassShared);
 576   SystemDictionary::get_all_method_handle_intrinsics(_method_handle_intrinsics);
 577   _method_handle_intrinsics->sort([] (Method** a, Method** b) -> int {
 578     Symbol* a_holder = (*a)->method_holder()->name();
 579     Symbol* b_holder = (*b)->method_holder()->name();
 580     if (a_holder != b_holder) {
 581       return a_holder->cmp(b_holder);
 582     }
 583     Symbol* a_name = (*a)->name();
 584     Symbol* b_name = (*b)->name();
 585     if (a_name != b_name) {
 586       return a_name->cmp(b_name);
 587     }
 588     Symbol* a_signature = (*a)->signature();
 589     Symbol* b_signature = (*b)->signature();
 590     if (a_signature != b_signature) {
 591       return a_signature->cmp(b_signature);
 592     }
 593     return 0;
 594   });
 595 
 596   FileMapInfo::check_nonempty_dir_in_shared_path_table();
 597 
 598   NOT_PRODUCT(SystemDictionary::verify();)
 599 
 600   // Block concurrent class unloading from changing the _dumptime_table
 601   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
 602 
 603   SystemDictionaryShared::find_all_archivable_classes();
 604 
 605   _builder.gather_source_objs();
 606   _builder.reserve_buffer();
 607 
 608   CppVtables::dumptime_init(&_builder);
 609 
 610   _builder.sort_metadata_objs();
 611   _builder.dump_rw_metadata();
 612   _builder.dump_ro_metadata();
 613   _builder.relocate_metaspaceobj_embedded_pointers();
 614 
 615   dump_java_heap_objects(_builder.klasses());
 616   dump_shared_symbol_table(_builder.symbols());
 617 
 618   log_info(cds)("Make classes shareable");
 619   _builder.make_klasses_shareable();
 620   MetaspaceShared::make_method_handle_intrinsics_shareable();
 621 
 622   char* serialized_data = dump_read_only_tables();
 623 
 624   SystemDictionaryShared::adjust_lambda_proxy_class_dictionary();
 625 
 626   log_info(cds)("Make training data shareable");
 627   _builder.make_training_data_shareable();
 628 
 629   // The vtable clones contain addresses of the current process.
 630   // We don't want to write these addresses into the archive.
 631   CppVtables::zero_archived_vtables();
 632 
 633   // Write the archive file
 634   const char* static_archive;
 635   if (CDSConfig::is_dumping_final_static_archive()) {
 636     static_archive = CacheDataStore;
 637     assert(FileMapInfo::current_info() != nullptr, "sanity");
 638     delete FileMapInfo::current_info();
 639   } else {
 640     static_archive = CDSConfig::static_archive_path();
 641   }
 642   assert(static_archive != nullptr, "SharedArchiveFile not set?");
 643   _map_info = new FileMapInfo(static_archive, true);
 644   _map_info->populate_header(MetaspaceShared::core_region_alignment());
 645   _map_info->set_serialized_data(serialized_data);
 646   _map_info->set_cloned_vtables(CppVtables::vtables_serialized_base());
 647 }
 648 
 649 class CollectCLDClosure : public CLDClosure {
 650   GrowableArray<ClassLoaderData*> _loaded_cld;
 651   GrowableArray<OopHandle> _loaded_cld_handles; // keep the CLDs alive
 652   Thread* _current_thread;
 653 public:
 654   CollectCLDClosure(Thread* thread) : _current_thread(thread) {}
 655   ~CollectCLDClosure() {
 656     for (int i = 0; i < _loaded_cld_handles.length(); i++) {
 657       _loaded_cld_handles.at(i).release(Universe::vm_global());
 658     }
 659   }
 660   void do_cld(ClassLoaderData* cld) {
 661     assert(cld->is_alive(), "must be");
 662     _loaded_cld.append(cld);
 663     _loaded_cld_handles.append(OopHandle(Universe::vm_global(), cld->holder()));
 664   }
 665 
 666   int nof_cld() const                { return _loaded_cld.length(); }
 667   ClassLoaderData* cld_at(int index) { return _loaded_cld.at(index); }
 668 };
 669 
 670 // Check if we can eagerly link this class at dump time, so we can avoid the
 671 // runtime linking overhead (especially verification)
 672 bool MetaspaceShared::may_be_eagerly_linked(InstanceKlass* ik) {
 673   if (CDSConfig::preserve_all_dumptime_verification_states(ik)) {
 674     assert(ik->can_be_verified_at_dumptime(), "sanity");
 675   }
 676   if (!ik->can_be_verified_at_dumptime()) {
 677     // For old classes, try to leave them in the unlinked state, so
 678     // we can still store them in the archive. They must be
 679     // linked/verified at runtime.
 680     return false;
 681   }
 682 
 683   if (CDSConfig::is_dumping_dynamic_archive() && ik->is_shared_unregistered_class()) {
 684     // Linking of unregistered classes at this stage may cause more
 685     // classes to be resolved, resulting in calls to ClassLoader.loadClass()
 686     // that may not be expected by custom class loaders.
 687     //
 688     // It's OK to do this for the built-in loaders as we know they can
 689     // tolerate this.
 690     return false;
 691   }
 692   return true;
 693 }
 694 









 695 void MetaspaceShared::link_shared_classes(bool jcmd_request, TRAPS) {
 696   AOTClassLinker::initialize();
 697 
 698   if (!jcmd_request && !CDSConfig::is_dumping_dynamic_archive()
 699       && !CDSConfig::is_dumping_preimage_static_archive()
 700       && !CDSConfig::is_dumping_final_static_archive()) {
 701     // If we have regenerated invoker classes in the dynamic archive,
 702     // they will conflict with the resolved CONSTANT_Klass references that are stored
 703     // in the static archive. This is not easy to handle. Let's disable
 704     // it for dynamic archive for now.
 705     LambdaFormInvokers::regenerate_holder_classes(CHECK);
 706   }
 707 
 708   // Collect all loaded ClassLoaderData.
 709   CollectCLDClosure collect_cld(THREAD);
 710   {
 711     // ClassLoaderDataGraph::loaded_cld_do requires ClassLoaderDataGraph_lock.
 712     // We cannot link the classes while holding this lock (or else we may run into deadlock).
 713     // Therefore, we need to first collect all the CLDs, and then link their classes after
 714     // releasing the lock.
 715     MutexLocker lock(ClassLoaderDataGraph_lock);
 716     ClassLoaderDataGraph::loaded_cld_do(&collect_cld);
 717   }
 718 
 719   while (true) {
 720     bool has_linked = false;
 721     for (int i = 0; i < collect_cld.nof_cld(); i++) {
 722       ClassLoaderData* cld = collect_cld.cld_at(i);
 723       for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) {
 724         if (klass->is_instance_klass()) {
 725           InstanceKlass* ik = InstanceKlass::cast(klass);
 726           if (may_be_eagerly_linked(ik)) {
 727             has_linked |= try_link_class(THREAD, ik);
 728           }
 729           if (CDSConfig::is_dumping_heap() && ik->is_linked() && !ik->is_initialized()) {
 730             AOTClassInitializer::maybe_preinit_class(ik, CHECK);
 731           }
 732         }
 733       }
 734     }
 735 
 736     if (!has_linked) {
 737       break;
 738     }
 739     // Class linking includes verification which may load more classes.
 740     // Keep scanning until we have linked no more classes.
 741   }
 742 
 743   // Resolve constant pool entries -- we don't load any new classes during this stage
 744   for (int i = 0; i < collect_cld.nof_cld(); i++) {
 745     ClassLoaderData* cld = collect_cld.cld_at(i);
 746     for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) {
 747       if (klass->is_instance_klass()) {
 748         InstanceKlass* ik = InstanceKlass::cast(klass);
 749         AOTConstantPoolResolver::dumptime_resolve_constants(ik, CHECK);
 750         if (CDSConfig::is_dumping_preimage_static_archive()) {
 751           FinalImageRecipes::add_reflection_data_flags(ik, CHECK);
 752         }
 753       }
 754     }
 755   }
 756 
 757   if (CDSConfig::is_dumping_preimage_static_archive()) {
 758     // Do this after all classes are verified by the above loop.
 759     // Any classes loaded from here on will be automatically excluded, so
 760     // there's no need to force verification or resolve CP entries.
 761     RecordTraining = false;
 762     SystemDictionaryShared::ignore_new_classes();
 763     LambdaFormInvokers::regenerate_holder_classes(CHECK);
 764     RecordTraining = true;
 765   }
 766 
 767   if (CDSConfig::is_dumping_final_static_archive()) {
 768     FinalImageRecipes::apply_recipes(CHECK);
 769   }
 770 }
 771 
 772 void MetaspaceShared::prepare_for_dumping() {
 773   assert(CDSConfig::is_dumping_archive(), "sanity");
 774   CDSConfig::check_unsupported_dumping_module_options();
 775   ClassLoader::initialize_shared_path(JavaThread::current());
 776 }
 777 
 778 // Preload classes from a list, populate the shared spaces and dump to a
 779 // file.
 780 void MetaspaceShared::preload_and_dump(TRAPS) {
 781   ResourceMark rm(THREAD);
 782   HandleMark hm(THREAD);
 783 
 784   if (CDSConfig::is_dumping_final_static_archive() && PrintTrainingInfo) {
 785     tty->print_cr("==================== archived_training_data ** before dumping ====================");
 786     TrainingData::print_archived_training_data_on(tty);
 787   }
 788 
 789   StaticArchiveBuilder builder;
 790   preload_and_dump_impl(builder, THREAD);
 791   if (HAS_PENDING_EXCEPTION) {
 792     if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 793       log_error(cds)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
 794                      SIZE_FORMAT "M", MaxHeapSize/M);
 795       MetaspaceShared::writing_error();
 796     } else {
 797       log_error(cds)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
 798                      java_lang_String::as_utf8_string(java_lang_Throwable::message(PENDING_EXCEPTION)));
 799       MetaspaceShared::writing_error("Unexpected exception, use -Xlog:cds,exceptions=trace for detail");
 800     }
 801   }
 802 }
 803 
 804 #if INCLUDE_CDS_JAVA_HEAP && defined(_LP64)
 805 void MetaspaceShared::adjust_heap_sizes_for_dumping() {
 806   if (!CDSConfig::is_dumping_heap() || UseCompressedOops) {
 807     return;
 808   }

 871                                      ClassListParser::_parse_all, CHECK);
 872   }
 873   if (classlist_path != default_classlist) {
 874     struct stat statbuf;
 875     if (os::stat(default_classlist, &statbuf) == 0) {
 876       // File exists, let's use it.
 877       ClassListParser::parse_classlist(default_classlist,
 878                                        ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
 879     }
 880   }
 881 
 882   // Exercise the manifest processing code to ensure classes used by CDS at runtime
 883   // are always archived
 884   const char* dummy = "Manifest-Version: 1.0\n";
 885   CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
 886 
 887   log_info(cds)("Loading classes to share: done.");
 888 }
 889 
 890 void MetaspaceShared::preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) {
 891   if (CDSConfig::is_dumping_classic_static_archive()) {
 892     // We are running with -Xshare:dump
 893     preload_classes(CHECK);
 894 
 895     if (SharedArchiveConfigFile) {
 896       log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
 897       read_extra_data(THREAD, SharedArchiveConfigFile);
 898       log_info(cds)("Reading extra data: done.");
 899     }
 900   }
 901 
 902   if (CDSConfig::is_dumping_preimage_static_archive()) {
 903     log_info(cds)("Reading lambda form invokers of in JDK default classlist ...");
 904     char default_classlist[JVM_MAXPATHLEN];
 905     get_default_classlist(default_classlist, sizeof(default_classlist));
 906     struct stat statbuf;
 907     if (os::stat(default_classlist, &statbuf) == 0) {
 908       ClassListParser::parse_classlist(default_classlist,
 909                                        ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
 910     }
 911   }
 912 
 913   // Rewrite and link classes
 914   log_info(cds)("Rewriting and linking classes ...");

 915   // Link any classes which got missed. This would happen if we have loaded classes that
 916   // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
 917   // fails verification, all other interfaces that were not specified in the classlist but
 918   // are implemented by K are not verified.
 919   link_shared_classes(false/*not from jcmd*/, CHECK);
 920   log_info(cds)("Rewriting and linking classes: done");
 921 
 922   if (CDSConfig::is_dumping_final_static_archive()) {
 923     assert(RecordTraining == false, "must be");
 924     RecordTraining = true;
 925   }
 926 
 927   TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
 928 
 929 #if INCLUDE_CDS_JAVA_HEAP
 930   if (CDSConfig::is_dumping_heap()) {
 931     if (!HeapShared::is_archived_boot_layer_available(THREAD)) {
 932       log_info(cds)("archivedBootLayer not available, disabling full module graph");
 933       CDSConfig::stop_dumping_full_module_graph();
 934     }
 935     HeapShared::init_for_dumping(CHECK);
 936     ArchiveHeapWriter::init();
 937     if (CDSConfig::is_dumping_full_module_graph()) {
 938       HeapShared::reset_archived_object_states(CHECK);
 939     }
 940 
 941     if (ArchiveLoaderLookupCache) {
 942       SystemDictionaryShared::create_loader_positive_lookup_cache(CHECK);
 943     }
 944 
 945     if (CDSConfig::is_dumping_invokedynamic()) {
 946       // This makes sure that the MethodType and MethodTypeForm tables won't be updated
 947       // concurrently when we are saving their contents into a side table.
 948       assert(CDSConfig::allow_only_single_java_thread(), "Required");
 949 
 950       JavaValue result(T_VOID);
 951       JavaCalls::call_static(&result, vmClasses::MethodType_klass(),
 952                              vmSymbols::createArchivedObjects(),
 953                              vmSymbols::void_method_signature(),
 954                              CHECK);
 955     }
 956 
 957     // Do this at the very end, when no Java code will be executed. Otherwise
 958     // some new strings may be added to the intern table.
 959     StringTable::allocate_shared_strings_array(CHECK);
 960   } else {
 961     log_info(cds)("Not dumping heap, reset CDSConfig::_is_using_optimized_module_handling");
 962     CDSConfig::stop_using_optimized_module_handling();
 963   }
 964 #endif
 965 
 966   // Dummy call to load classes used at CDS runtime
 967   JavaValue result(T_OBJECT);
 968   Handle path_string = java_lang_String::create_from_str("dummy.jar", CHECK);
 969   JavaCalls::call_static(&result,
 970                          vmClasses::jdk_internal_loader_ClassLoaders_klass(),
 971                          vmSymbols::toFileURL_name(),
 972                          vmSymbols::toFileURL_signature(),
 973                          path_string,
 974                          CHECK);
 975 
 976   VM_PopulateDumpSharedSpace op(builder);
 977   VMThread::execute(&op);
 978   FileMapInfo* mapinfo = op.map_info();
 979   ArchiveHeapInfo* heap_info = op.heap_info();
 980   bool status;
 981   if (CDSConfig::is_dumping_preimage_static_archive()) {
 982     if ((status = write_static_archive(&builder, mapinfo, heap_info))) {
 983       fork_and_dump_final_static_archive();
 984     }
 985   } else if (CDSConfig::is_dumping_final_static_archive()) {
 986     RecordTraining = false;
 987     if (StoreCachedCode && CachedCodeFile != nullptr) { // FIXME: new workflow -- remove the CachedCodeFile flag
 988       if (log_is_enabled(Info, cds, jit)) {
 989         CDSAccess::test_heap_access_api();
 990       }
 991 
 992       // We have just created the final image. Let's run the AOT compiler
 993       if (PrintTrainingInfo) {
 994         tty->print_cr("==================== archived_training_data ** after dumping ====================");
 995         TrainingData::print_archived_training_data_on(tty);
 996       }
 997 
 998       CDSConfig::enable_dumping_cached_code();
 999       {
1000         builder.start_cc_region();
1001         Precompiler::compile_cached_code(&builder, CHECK);
1002         builder.end_cc_region();
1003       }
1004       CDSConfig::disable_dumping_cached_code();
1005 
1006       SCCache::close(); // Write final data and close archive
1007     }
1008     status = write_static_archive(&builder, mapinfo, heap_info);
1009   } else {
1010     status = write_static_archive(&builder, mapinfo, heap_info);
1011   }
1012 
1013   if (!status) {
1014     THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
1015   }
1016 }
1017 
1018 bool MetaspaceShared::write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info) {
1019   // relocate the data so that it can be mapped to MetaspaceShared::requested_base_address()
1020   // without runtime relocation.
1021   builder->relocate_to_requested();
1022 
1023   map_info->open_for_write();
1024   if (!map_info->is_open()) {
1025     return false;
1026   }
1027   builder->write_archive(map_info, heap_info);
1028 
1029   if (AllowArchivingWithJavaAgent) {
1030     log_warning(cds)("This archive was created with AllowArchivingWithJavaAgent. It should be used "
1031             "for testing purposes only and should not be used in a production environment");
1032   }
1033   return true;
1034 }
1035 
1036 static void print_java_launcher(outputStream* st) {
1037   st->print("%s%sbin%sjava", Arguments::get_java_home(), os::file_separator(), os::file_separator());
1038 }
1039 
1040 static void print_vm_arguments(outputStream* st) {
1041   const char* cp = Arguments::get_appclasspath();
1042   if (cp != nullptr && strlen(cp) > 0 && strcmp(cp, ".") != 0) {
1043     st->print(" -cp ");  st->print_raw(cp);
1044   }
1045   for (int i = 0; i < Arguments::num_jvm_flags(); i++) {
1046     st->print(" %s", Arguments::jvm_flags_array()[i]);
1047   }
1048   for (int i = 0; i < Arguments::num_jvm_args(); i++) {
1049     st->print(" %s", Arguments::jvm_args_array()[i]);
1050   }
1051 }
1052 
1053 void MetaspaceShared::fork_and_dump_final_static_archive() {
1054   assert(CDSConfig::is_dumping_preimage_static_archive(), "sanity");
1055 
1056   ResourceMark rm;
1057   stringStream ss;
1058   print_java_launcher(&ss);
1059   print_vm_arguments(&ss);
1060   ss.print(" -XX:CDSPreimage=%s", SharedArchiveFile);
1061 
1062   const char* cmd = ss.freeze();
1063   if (CDSManualFinalImage) {
1064     tty->print_cr("-XX:+CDSManualFinalImage is specified");
1065     tty->print_cr("Please manually execute the following command to create the final CDS image:");
1066     tty->print("    "); tty->print_raw_cr(cmd);
1067 
1068     // The following is useful if the dumping was trigger by a script that builds
1069     // a complex command-line.
1070     tty->print_cr("Note: to recreate the preimage only:");
1071     tty->print_cr("    rm -f %s", CacheDataStore);
1072     tty->print("    ");
1073     print_java_launcher(tty);
1074     print_vm_arguments(tty);
1075     if (Arguments::java_command() != nullptr) {
1076       tty->print(" %s", Arguments::java_command());
1077     }
1078     tty->cr();
1079   } else {
1080     // FIXME: space characters are not properly quoated. E.g.,
1081     //      java -Dfoo='a b c' HelloWorld
1082     log_info(cds)("Launching child process to create final CDS image:");
1083     log_info(cds)("    %s", cmd);
1084     int status = os::fork_and_exec(cmd);
1085     if (status != 0) {
1086       log_error(cds)("Child process finished; status = %d", status);
1087       log_error(cds)("To reproduce the error");
1088       ResourceMark rm;
1089       LogStream ls(Log(cds)::error());
1090       ls.print("    "); ls.print_raw_cr(cmd);
1091 
1092       // The following is useful if the dumping was trigger by a script that builds
1093       // a complex command-line.
1094       ls.print_cr("Note: to recreate the preimage only:");
1095       ls.print_cr("    rm -f %s", CacheDataStore);
1096       ls.print("    ");
1097       print_java_launcher(&ls);
1098       print_vm_arguments(&ls);
1099       ls.print(" -XX:+UnlockDiagnosticVMOptions -XX:+CDSManualFinalImage");
1100       if (Arguments::java_command() != nullptr) {
1101         ls.print(" %s", Arguments::java_command());
1102       }
1103       ls.cr();
1104 
1105       vm_direct_exit(status);
1106     } else {
1107       log_info(cds)("Child process finished; status = %d", status);
1108       // On Windows, need WRITE permission to remove the file.
1109       WINDOWS_ONLY(chmod(SharedArchiveFile, _S_IREAD | _S_IWRITE));
1110       status = remove(SharedArchiveFile);
1111       if (status != 0) {
1112         log_error(cds)("Failed to remove CDSPreimage file %s", SharedArchiveFile);
1113       } else {
1114         log_info(cds)("Removed CDSPreimage file %s", SharedArchiveFile);
1115       }
1116     }
1117   }
1118 }
1119 
1120 // Returns true if the class's status has changed.
1121 bool MetaspaceShared::try_link_class(JavaThread* current, InstanceKlass* ik) {
1122   ExceptionMark em(current);
1123   JavaThread* THREAD = current; // For exception macros.
1124   assert(CDSConfig::is_dumping_archive(), "sanity");
1125 
1126   if (ik->is_shared() && !CDSConfig::is_dumping_final_static_archive()) {
1127     assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
1128     return false;
1129   }
1130 
1131   if (ik->is_loaded() && !ik->is_linked() && ik->can_be_verified_at_dumptime() &&
1132       !SystemDictionaryShared::has_class_failed_verification(ik)) {
1133     bool saved = BytecodeVerificationLocal;
1134     if (ik->is_shared_unregistered_class() && ik->class_loader() == nullptr) {
1135       // The verification decision is based on BytecodeVerificationRemote
1136       // for non-system classes. Since we are using the null classloader
1137       // to load non-system classes for customized class loaders during dumping,
1138       // we need to temporarily change BytecodeVerificationLocal to be the same as
1139       // BytecodeVerificationRemote. Note this can cause the parent system
1140       // classes also being verified. The extra overhead is acceptable during
1141       // dumping.
1142       BytecodeVerificationLocal = BytecodeVerificationRemote;
1143     }
1144     ik->link_class(THREAD);
1145     if (HAS_PENDING_EXCEPTION) {
1146       ResourceMark rm(THREAD);
1147       log_warning(cds)("Preload Warning: Verification failed for %s",
1148                     ik->external_name());
1149       CLEAR_PENDING_EXCEPTION;
1150       SystemDictionaryShared::set_class_has_failed_verification(ik);
1151     }
1152     ik->compute_has_loops_flag_for_methods();
1153     BytecodeVerificationLocal = saved;
1154     return true;
1155   } else {
1156     return false;
1157   }
1158 }
1159 
1160 #if INCLUDE_CDS_JAVA_HEAP
1161 void VM_PopulateDumpSharedSpace::dump_java_heap_objects(GrowableArray<Klass*>* klasses) {
1162   if(!HeapShared::can_write()) {
1163     if (!CDSConfig::is_dumping_preimage_static_archive()) {
1164      log_info(cds)(
1165       "Archived java heap is not supported as UseG1GC "
1166       "and UseCompressedClassPointers are required."
1167       "Current settings: UseG1GC=%s, UseCompressedClassPointers=%s.",
1168       BOOL_TO_STR(UseG1GC), BOOL_TO_STR(UseCompressedClassPointers));
1169     }
1170     return;
1171   }
1172   // Find all the interned strings that should be dumped.
1173   int i;
1174   for (i = 0; i < klasses->length(); i++) {
1175     Klass* k = klasses->at(i);
1176     if (k->is_instance_klass()) {
1177       InstanceKlass* ik = InstanceKlass::cast(k);
1178       if (ik->is_linked()) {
1179         ik->constants()->add_dumped_interned_strings();
1180       }
1181     }
1182   }
1183   if (_extra_interned_strings != nullptr) {
1184     for (i = 0; i < _extra_interned_strings->length(); i ++) {
1185       OopHandle string = _extra_interned_strings->at(i);
1186       HeapShared::add_to_dumped_interned_strings(string.resolve());
1187     }
1188   }
1189 

1232 // This function is called when the JVM is unable to write the specified CDS archive due to an
1233 // unrecoverable error.
1234 void MetaspaceShared::unrecoverable_writing_error(const char* message) {
1235   writing_error(message);
1236   vm_direct_exit(1);
1237 }
1238 
1239 // This function is called when the JVM is unable to write the specified CDS archive due to a
1240 // an error. The error will be propagated
1241 void MetaspaceShared::writing_error(const char* message) {
1242   log_error(cds)("An error has occurred while writing the shared archive file.");
1243   if (message != nullptr) {
1244     log_error(cds)("%s", message);
1245   }
1246 }
1247 
1248 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
1249   assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
1250   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1251 
1252   FileMapInfo* static_mapinfo = FileMapInfo::current_info();
1253   FileMapInfo* dynamic_mapinfo = nullptr;
1254 
1255   if (static_mapinfo != nullptr) {
1256     log_info(cds)("Core region alignment: " SIZE_FORMAT, static_mapinfo->core_region_alignment());
1257     dynamic_mapinfo = open_dynamic_archive();
1258 
1259     // First try to map at the requested address
1260     result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1261     if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1262       // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1263       // by the OS.
1264       log_info(cds)("Try to map archive(s) at an alternative address");
1265       result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1266     }
1267   }
1268 
1269   if (result == MAP_ARCHIVE_SUCCESS) {
1270     bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());
1271     char* cds_base = static_mapinfo->mapped_base();
1272     char* cds_end =  dynamic_mapped ? dynamic_mapinfo->mapped_end() : static_mapinfo->mapped_end();

1279       FileMapInfo::set_shared_path_table(dynamic_mapinfo);
1280       // turn AutoCreateSharedArchive off if successfully mapped
1281       AutoCreateSharedArchive = false;
1282     } else {
1283       FileMapInfo::set_shared_path_table(static_mapinfo);
1284     }
1285   } else {
1286     set_shared_metaspace_range(nullptr, nullptr, nullptr);
1287     if (CDSConfig::is_dumping_dynamic_archive()) {
1288       log_warning(cds)("-XX:ArchiveClassesAtExit is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info.");
1289     }
1290     UseSharedSpaces = false;
1291     // The base archive cannot be mapped. We cannot dump the dynamic shared archive.
1292     AutoCreateSharedArchive = false;
1293     CDSConfig::disable_dumping_dynamic_archive();
1294     log_info(cds)("Unable to map shared spaces");
1295     if (PrintSharedArchiveAndExit) {
1296       MetaspaceShared::unrecoverable_loading_error("Unable to use shared archive.");
1297     } else if (RequireSharedSpaces) {
1298       MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1299     } else if (CDSConfig::is_dumping_final_static_archive()) {
1300       assert(CDSPreimage != nullptr, "must be");
1301       log_error(cds)("Unable to map shared spaces for CDSPreimage = %s", CDSPreimage);
1302       MetaspaceShared::unrecoverable_loading_error();
1303     }
1304   }
1305 
1306   // If mapping failed and -XShare:on, the vm should exit
1307   bool has_failed = false;
1308   if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1309     has_failed = true;
1310     delete static_mapinfo;
1311   }
1312   if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1313     has_failed = true;
1314     delete dynamic_mapinfo;
1315   }
1316   if (RequireSharedSpaces && has_failed) {
1317     // static archive mapped but dynamic archive failed
1318       MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1319   }
1320 }
1321 
1322 // This is called very early at VM start up to get the size of the cached_code region, which
1323 // is used in CodeCache::initialize_heaps()
1324 void MetaspaceShared::open_static_archive() {
1325   if (!UseSharedSpaces) {
1326     return;
1327   }
1328   const char* static_archive = CDSConfig::static_archive_path();
1329   assert(static_archive != nullptr, "sanity");
1330   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1331   if (!mapinfo->initialize()) {
1332     delete(mapinfo);
1333   } else {
1334     FileMapRegion* r = mapinfo->region_at(MetaspaceShared::cc);
1335     CDSAccess::set_cached_code_size(r->used_aligned());
1336   }

1337 }
1338 
1339 FileMapInfo* MetaspaceShared::open_dynamic_archive() {
1340   if (CDSConfig::is_dumping_dynamic_archive()) {
1341     return nullptr;
1342   }
1343   const char* dynamic_archive = CDSConfig::dynamic_archive_path();
1344   if (dynamic_archive == nullptr) {
1345     return nullptr;
1346   }
1347 
1348   FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1349   if (!mapinfo->initialize()) {
1350     delete(mapinfo);
1351     if (RequireSharedSpaces) {
1352       MetaspaceShared::unrecoverable_loading_error("Failed to initialize dynamic archive");
1353     }
1354     return nullptr;
1355   }
1356   return mapinfo;

1735     if (archive_space_rs.is_reserved()) {
1736       log_debug(cds)("Released shared space (archive) " INTPTR_FORMAT, p2i(archive_space_rs.base()));
1737       archive_space_rs.release();
1738     }
1739     if (class_space_rs.is_reserved()) {
1740       log_debug(cds)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
1741       class_space_rs.release();
1742     }
1743   }
1744 }
1745 
1746 static int archive_regions[]     = { MetaspaceShared::rw, MetaspaceShared::ro };
1747 static int archive_regions_count = 2;
1748 
1749 MapArchiveResult MetaspaceShared::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
1750   assert(CDSConfig::is_using_archive(), "must be runtime");
1751   if (mapinfo == nullptr) {
1752     return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
1753   }
1754 
1755   if (!mapinfo->validate_aot_class_linking()) {
1756     return MAP_ARCHIVE_OTHER_FAILURE;
1757   }
1758 
1759   mapinfo->set_is_mapped(false);
1760   if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
1761     log_info(cds)("Unable to map CDS archive -- core_region_alignment() expected: " SIZE_FORMAT
1762                   " actual: " SIZE_FORMAT, mapinfo->core_region_alignment(), core_region_alignment());
1763     return MAP_ARCHIVE_OTHER_FAILURE;
1764   }
1765 
1766   MapArchiveResult result =
1767     mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
1768 
1769   if (result != MAP_ARCHIVE_SUCCESS) {
1770     unmap_archive(mapinfo);
1771     return result;
1772   }
1773 
1774   if (!mapinfo->validate_shared_path_table()) {
1775     unmap_archive(mapinfo);
1776     return MAP_ARCHIVE_OTHER_FAILURE;
1777   }
1778 

1803 };
1804 
1805 // Read the miscellaneous data from the shared file, and
1806 // serialize it out to its various destinations.
1807 
1808 void MetaspaceShared::initialize_shared_spaces() {
1809   FileMapInfo *static_mapinfo = FileMapInfo::current_info();
1810 
1811   // Verify various attributes of the archive, plus initialize the
1812   // shared string/symbol tables.
1813   char* buffer = static_mapinfo->serialized_data();
1814   intptr_t* array = (intptr_t*)buffer;
1815   ReadClosure rc(&array);
1816   serialize(&rc);
1817 
1818   // Finish up archived heap initialization. These must be
1819   // done after ReadClosure.
1820   static_mapinfo->patch_heap_embedded_pointers();
1821   ArchiveHeapLoader::finish_initialization();
1822   Universe::load_archived_object_instances();
1823   SCCache::new_workflow_load_cache();
1824 
1825   // Close the mapinfo file
1826   static_mapinfo->close();
1827 
1828   static_mapinfo->unmap_region(MetaspaceShared::bm);
1829 
1830   FileMapInfo *dynamic_mapinfo = FileMapInfo::dynamic_info();
1831   if (dynamic_mapinfo != nullptr) {
1832     intptr_t* buffer = (intptr_t*)dynamic_mapinfo->serialized_data();
1833     ReadClosure rc(&buffer);
1834     ArchiveBuilder::serialize_dynamic_archivable_items(&rc);
1835     DynamicArchive::setup_array_klasses();
1836     dynamic_mapinfo->close();
1837     dynamic_mapinfo->unmap_region(MetaspaceShared::bm);
1838   }
1839 
1840   log_info(cds)("Using AOT-linked classes: %s (%s%s)",
1841                 CDSConfig::is_using_aot_linked_classes() ? "true" : "false",
1842                 static_mapinfo->header()->has_aot_linked_classes() ? "static archive: true" : "static archive: false",
1843                 (dynamic_mapinfo == nullptr) ? "" :
1844                    (dynamic_mapinfo->header()->has_aot_linked_classes() ? ", dynamic archive: true" : ", dynamic archive: false"));
1845 
1846   // Set up LambdaFormInvokers::_lambdaform_lines for dynamic dump
1847   if (CDSConfig::is_dumping_dynamic_archive()) {
1848     // Read stored LF format lines stored in static archive
1849     LambdaFormInvokers::read_static_archive_invokers();
1850   }
1851 
1852   if (PrintSharedArchiveAndExit) {
1853     // Print archive names
1854     if (dynamic_mapinfo != nullptr) {
1855       tty->print_cr("\n\nBase archive name: %s", CDSConfig::static_archive_path());
1856       tty->print_cr("Base archive version %d", static_mapinfo->version());
1857     } else {
1858       tty->print_cr("Static archive name: %s", static_mapinfo->full_path());
1859       tty->print_cr("Static archive version %d", static_mapinfo->version());
1860     }
1861 
1862     SystemDictionaryShared::print_shared_archive(tty);
1863     if (dynamic_mapinfo != nullptr) {
1864       tty->print_cr("\n\nDynamic archive name: %s", dynamic_mapinfo->full_path());
1865       tty->print_cr("Dynamic archive version %d", dynamic_mapinfo->version());
1866       SystemDictionaryShared::print_shared_archive(tty, false/*dynamic*/);
1867     }
1868     TrainingData::print_archived_training_data_on(tty);
1869 
1870     if (LoadCachedCode) {
1871       tty->print_cr("\n\nCached Code file: %s", CachedCodeFile);
1872       SCCache::print_on(tty);
1873     }
1874 
1875     // collect shared symbols and strings
1876     CountSharedSymbols cl;
1877     SymbolTable::shared_symbols_do(&cl);
1878     tty->print_cr("Number of shared symbols: %d", cl.total());
1879     tty->print_cr("Number of shared strings: %zu", StringTable::shared_entry_count());
1880     tty->print_cr("VM version: %s\r\n", static_mapinfo->vm_version());
1881     if (FileMapInfo::current_info() == nullptr || _archive_loading_failed) {
1882       tty->print_cr("archive is invalid");
1883       vm_exit(1);
1884     } else {
1885       tty->print_cr("archive is valid");
1886       vm_exit(0);
1887     }
1888   }
1889 }
1890 
1891 // JVM/TI RedefineClasses() support:
1892 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
1893   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
< prev index next >