< prev index next >

src/hotspot/share/cds/metaspaceShared.cpp

Print this page

   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 

  25 #include "cds/aotArtifactFinder.hpp"
  26 #include "cds/aotClassLinker.hpp"
  27 #include "cds/aotClassLocation.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/cdsConfig.hpp"
  35 #include "cds/cdsProtectionDomain.hpp"
  36 #include "cds/classListParser.hpp"
  37 #include "cds/classListWriter.hpp"
  38 #include "cds/cppVtables.hpp"
  39 #include "cds/dumpAllocStats.hpp"
  40 #include "cds/dynamicArchive.hpp"
  41 #include "cds/filemap.hpp"
  42 #include "cds/finalImageRecipes.hpp"
  43 #include "cds/heapShared.hpp"
  44 #include "cds/lambdaFormInvokers.hpp"
  45 #include "cds/metaspaceShared.hpp"
  46 #include "classfile/classLoaderDataGraph.hpp"
  47 #include "classfile/classLoaderDataShared.hpp"
  48 #include "classfile/classLoaderExt.hpp"
  49 #include "classfile/javaClasses.inline.hpp"
  50 #include "classfile/loaderConstraints.hpp"
  51 #include "classfile/modules.hpp"
  52 #include "classfile/placeholders.hpp"
  53 #include "classfile/stringTable.hpp"
  54 #include "classfile/symbolTable.hpp"
  55 #include "classfile/systemDictionary.hpp"
  56 #include "classfile/systemDictionaryShared.hpp"
  57 #include "classfile/vmClasses.hpp"
  58 #include "classfile/vmSymbols.hpp"
  59 #include "code/codeCache.hpp"



  60 #include "gc/shared/gcVMOperations.hpp"
  61 #include "interpreter/bytecodeStream.hpp"
  62 #include "interpreter/bytecodes.hpp"
  63 #include "jvm_io.h"
  64 #include "logging/log.hpp"
  65 #include "logging/logMessage.hpp"
  66 #include "logging/logStream.hpp"
  67 #include "memory/memoryReserver.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/objArrayOop.hpp"
  77 #include "oops/oop.inline.hpp"
  78 #include "oops/oopHandle.hpp"

  79 #include "prims/jvmtiExport.hpp"

  80 #include "runtime/arguments.hpp"
  81 #include "runtime/globals.hpp"
  82 #include "runtime/globals_extension.hpp"
  83 #include "runtime/handles.inline.hpp"
  84 #include "runtime/javaCalls.hpp"
  85 #include "runtime/os.inline.hpp"
  86 #include "runtime/safepointVerifiers.hpp"
  87 #include "runtime/sharedRuntime.hpp"
  88 #include "runtime/vmOperations.hpp"
  89 #include "runtime/vmThread.hpp"
  90 #include "sanitizers/leak.hpp"
  91 #include "utilities/align.hpp"
  92 #include "utilities/bitMap.inline.hpp"
  93 #include "utilities/defaultStream.hpp"
  94 #include "utilities/macros.hpp"
  95 #include "utilities/ostream.hpp"
  96 #include "utilities/resourceHash.hpp"
  97 
  98 ReservedSpace MetaspaceShared::_symbol_rs;
  99 VirtualSpace MetaspaceShared::_symbol_vs;
 100 bool MetaspaceShared::_archive_loading_failed = false;
 101 bool MetaspaceShared::_remapped_readwrite = false;
 102 void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
 103 intx MetaspaceShared::_relocation_delta;
 104 char* MetaspaceShared::_requested_base_address;
 105 Array<Method*>* MetaspaceShared::_archived_method_handle_intrinsics = nullptr;
 106 bool MetaspaceShared::_use_optimized_module_handling = true;

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

 282   }
 283 
 284   // Arguments::default_SharedBaseAddress() is hard-coded in cds_globals.hpp. It must be carefully
 285   // picked that (a) the align_up() below will always return a valid value; (b) none of
 286   // the following asserts will fail.
 287   log_warning(cds)("SharedBaseAddress (" INTPTR_FORMAT ") is %s. Reverted to " INTPTR_FORMAT,
 288                    p2i((void*)SharedBaseAddress), err,
 289                    p2i((void*)Arguments::default_SharedBaseAddress()));
 290 
 291   specified_base = (char*)Arguments::default_SharedBaseAddress();
 292   aligned_base = align_up(specified_base, alignment);
 293 
 294   // Make sure the default value of SharedBaseAddress specified in globals.hpp is sane.
 295   assert(!shared_base_too_high(specified_base, aligned_base, cds_max), "Sanity");
 296   assert(shared_base_valid(aligned_base), "Sanity");
 297   return aligned_base;
 298 }
 299 
 300 void MetaspaceShared::initialize_for_static_dump() {
 301   assert(CDSConfig::is_dumping_static_archive(), "sanity");

















 302   log_info(cds)("Core region alignment: %zu", core_region_alignment());

 303   // The max allowed size for CDS archive. We use this to limit SharedBaseAddress
 304   // to avoid address space wrap around.
 305   size_t cds_max;
 306   const size_t reserve_alignment = core_region_alignment();
 307 
 308 #ifdef _LP64
 309   const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
 310   cds_max = align_down(UnscaledClassSpaceMax, reserve_alignment);
 311 #else
 312   // We don't support archives larger than 256MB on 32-bit due to limited
 313   //  virtual address space.
 314   cds_max = align_down(256*M, reserve_alignment);
 315 #endif
 316 
 317   _requested_base_address = compute_shared_base(cds_max);
 318   SharedBaseAddress = (size_t)_requested_base_address;
 319 
 320   size_t symbol_rs_size = LP64_ONLY(3 * G) NOT_LP64(128 * M);
 321   _symbol_rs = MemoryReserver::reserve(symbol_rs_size,
 322                                        os::vm_allocation_granularity(),

 474   // in archived Metadata objects.
 475   CppVtables::serialize(soc);
 476   soc->do_tag(--tag);
 477 
 478   // Dump/restore miscellaneous metadata.
 479   JavaClasses::serialize_offsets(soc);
 480   Universe::serialize(soc);
 481   soc->do_tag(--tag);
 482 
 483   // Dump/restore references to commonly used names and signatures.
 484   vmSymbols::serialize(soc);
 485   soc->do_tag(--tag);
 486 
 487   // Dump/restore the symbol/string/subgraph_info tables
 488   SymbolTable::serialize_shared_table_header(soc);
 489   StringTable::serialize_shared_table_header(soc);
 490   HeapShared::serialize_tables(soc);
 491   SystemDictionaryShared::serialize_dictionary_headers(soc);
 492   AOTLinkedClassBulkLoader::serialize(soc, true);
 493   FinalImageRecipes::serialize(soc);

 494   InstanceMirrorKlass::serialize_offsets(soc);
 495 
 496   // Dump/restore well known classes (pointers)
 497   SystemDictionaryShared::serialize_vm_classes(soc);
 498   soc->do_tag(--tag);
 499 
 500   CDS_JAVA_HEAP_ONLY(ClassLoaderDataShared::serialize(soc);)
 501   soc->do_ptr((void**)&_archived_method_handle_intrinsics);
 502 
 503   LambdaFormInvokers::serialize(soc);


 504   soc->do_tag(666);
 505 }
 506 
 507 static void rewrite_nofast_bytecode(const methodHandle& method) {
 508   BytecodeStream bcs(method);
 509   while (!bcs.is_last_bytecode()) {
 510     Bytecodes::Code opcode = bcs.next();
 511     switch (opcode) {
 512     case Bytecodes::_getfield:      *bcs.bcp() = Bytecodes::_nofast_getfield;      break;
 513     case Bytecodes::_putfield:      *bcs.bcp() = Bytecodes::_nofast_putfield;      break;
 514     case Bytecodes::_aload_0:       *bcs.bcp() = Bytecodes::_nofast_aload_0;       break;
 515     case Bytecodes::_iload: {
 516       if (!bcs.is_wide()) {
 517         *bcs.bcp() = Bytecodes::_nofast_iload;
 518       }
 519       break;
 520     }
 521     default: break;
 522     }
 523   }

 558     VM_Operation(), _heap_info(), _map_info(nullptr), _builder(b) {}
 559 
 560   bool skip_operation() const { return false; }
 561 
 562   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
 563   ArchiveHeapInfo* heap_info()  { return &_heap_info; }
 564   FileMapInfo* map_info() const { return _map_info; }
 565   void doit();   // outline because gdb sucks
 566   bool allow_nested_vm_operations() const { return true; }
 567 }; // class VM_PopulateDumpSharedSpace
 568 
 569 class StaticArchiveBuilder : public ArchiveBuilder {
 570 public:
 571   StaticArchiveBuilder() : ArchiveBuilder() {}
 572 
 573   virtual void iterate_roots(MetaspaceClosure* it) {
 574     AOTArtifactFinder::all_cached_classes_do(it);
 575     SystemDictionaryShared::dumptime_classes_do(it);
 576     Universe::metaspace_pointers_do(it);
 577     vmSymbols::metaspace_pointers_do(it);

 578 
 579     // The above code should find all the symbols that are referenced by the
 580     // archived classes. We just need to add the extra symbols which
 581     // may not be used by any of the archived classes -- these are usually
 582     // symbols that we anticipate to be used at run time, so we can store
 583     // them in the RO region, to be shared across multiple processes.
 584     if (_extra_symbols != nullptr) {
 585       for (int i = 0; i < _extra_symbols->length(); i++) {
 586         it->push(_extra_symbols->adr_at(i));
 587       }
 588     }
 589 
 590     for (int i = 0; i < _pending_method_handle_intrinsics->length(); i++) {
 591       it->push(_pending_method_handle_intrinsics->adr_at(i));
 592     }
 593   }
 594 };
 595 
 596 char* VM_PopulateDumpSharedSpace::dump_early_read_only_tables() {
 597   ArchiveBuilder::OtherROAllocMark mark;
 598 
 599   CDS_JAVA_HEAP_ONLY(Modules::dump_archived_module_info());
 600 
 601   DumpRegion* ro_region = ArchiveBuilder::current()->ro_region();
 602   char* start = ro_region->top();
 603   WriteClosure wc(ro_region);
 604   MetaspaceShared::early_serialize(&wc);
 605   return start;
 606 }
 607 
 608 char* VM_PopulateDumpSharedSpace::dump_read_only_tables(AOTClassLocationConfig*& cl_config) {
 609   ArchiveBuilder::OtherROAllocMark mark;
 610 
 611   SystemDictionaryShared::write_to_archive();
 612   cl_config = AOTClassLocationConfig::dumptime()->write_to_archive();
 613   AOTClassLinker::write_to_archive();
 614   if (CDSConfig::is_dumping_preimage_static_archive()) {
 615     FinalImageRecipes::record_recipes();
 616   }



 617   MetaspaceShared::write_method_handle_intrinsics();
 618 
 619   // Write lambform lines into archive
 620   LambdaFormInvokers::dump_static_archive_invokers();
 621 




 622   // Write the other data to the output array.
 623   DumpRegion* ro_region = ArchiveBuilder::current()->ro_region();
 624   char* start = ro_region->top();
 625   WriteClosure wc(ro_region);
 626   MetaspaceShared::serialize(&wc);
 627 
 628   return start;
 629 }
 630 
 631 void VM_PopulateDumpSharedSpace::doit() {
 632   if (!CDSConfig::is_dumping_final_static_archive()) {
 633     guarantee(!CDSConfig::is_using_archive(), "We should not be using an archive when we dump");
 634   }
 635 
 636   DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm);
 637 
 638   _pending_method_handle_intrinsics = new (mtClassShared) GrowableArray<Method*>(256, mtClassShared);
 639   if (CDSConfig::is_dumping_aot_linked_classes()) {
 640     // When dumping AOT-linked classes, some classes may have direct references to a method handle
 641     // intrinsic. The easiest thing is to save all of them into the AOT cache.

 664   CppVtables::dumptime_init(&_builder);
 665 
 666   _builder.sort_metadata_objs();
 667   _builder.dump_rw_metadata();
 668   _builder.dump_ro_metadata();
 669   _builder.relocate_metaspaceobj_embedded_pointers();
 670 
 671   log_info(cds)("Make classes shareable");
 672   _builder.make_klasses_shareable();
 673   MetaspaceShared::make_method_handle_intrinsics_shareable();
 674 
 675   dump_java_heap_objects();
 676   dump_shared_symbol_table(_builder.symbols());
 677 
 678   char* early_serialized_data = dump_early_read_only_tables();
 679   AOTClassLocationConfig* cl_config;
 680   char* serialized_data = dump_read_only_tables(cl_config);
 681 
 682   SystemDictionaryShared::adjust_lambda_proxy_class_dictionary();
 683 



 684   // The vtable clones contain addresses of the current process.
 685   // We don't want to write these addresses into the archive.
 686   CppVtables::zero_archived_vtables();
 687 
 688   // Write the archive file
 689   const char* static_archive;
 690   if (CDSConfig::is_dumping_final_static_archive()) {
 691     static_archive = AOTCache;




 692     FileMapInfo::free_current_info();
 693   } else {
 694     static_archive = CDSConfig::static_archive_path();
 695   }
 696   assert(static_archive != nullptr, "SharedArchiveFile not set?");
 697   _map_info = new FileMapInfo(static_archive, true);
 698   _map_info->populate_header(MetaspaceShared::core_region_alignment());
 699   _map_info->set_early_serialized_data(early_serialized_data);
 700   _map_info->set_serialized_data(serialized_data);
 701   _map_info->set_cloned_vtables(CppVtables::vtables_serialized_base());
 702   _map_info->header()->set_class_location_config(cl_config);
 703 }
 704 
 705 class CollectCLDClosure : public CLDClosure {
 706   GrowableArray<ClassLoaderData*> _loaded_cld;
 707   GrowableArray<OopHandle> _loaded_cld_handles; // keep the CLDs alive
 708   Thread* _current_thread;
 709 public:
 710   CollectCLDClosure(Thread* thread) : _current_thread(thread) {}
 711   ~CollectCLDClosure() {
 712     for (int i = 0; i < _loaded_cld_handles.length(); i++) {
 713       _loaded_cld_handles.at(i).release(Universe::vm_global());
 714     }
 715   }
 716   void do_cld(ClassLoaderData* cld) {
 717     assert(cld->is_alive(), "must be");
 718     _loaded_cld.append(cld);
 719     _loaded_cld_handles.append(OopHandle(Universe::vm_global(), cld->holder()));
 720   }
 721 
 722   int nof_cld() const                { return _loaded_cld.length(); }
 723   ClassLoaderData* cld_at(int index) { return _loaded_cld.at(index); }
 724 };
 725 
 726 // Check if we can eagerly link this class at dump time, so we can avoid the
 727 // runtime linking overhead (especially verification)
 728 bool MetaspaceShared::may_be_eagerly_linked(InstanceKlass* ik) {



 729   if (!ik->can_be_verified_at_dumptime()) {
 730     // For old classes, try to leave them in the unlinked state, so
 731     // we can still store them in the archive. They must be
 732     // linked/verified at runtime.
 733     return false;
 734   }

 735   if (CDSConfig::is_dumping_dynamic_archive() && ik->is_shared_unregistered_class()) {
 736     // Linking of unregistered classes at this stage may cause more
 737     // classes to be resolved, resulting in calls to ClassLoader.loadClass()
 738     // that may not be expected by custom class loaders.
 739     //
 740     // It's OK to do this for the built-in loaders as we know they can
 741     // tolerate this.
 742     return false;
 743   }
 744   return true;
 745 }
 746 
 747 bool MetaspaceShared::link_class_for_cds(InstanceKlass* ik, TRAPS) {
 748   // Link the class to cause the bytecodes to be rewritten and the
 749   // cpcache to be created. Class verification is done according
 750   // to -Xverify setting.
 751   bool res = MetaspaceShared::try_link_class(THREAD, ik);
 752   AOTConstantPoolResolver::dumptime_resolve_constants(ik, CHECK_(false));
 753   return res;
 754 }
 755 
 756 void MetaspaceShared::link_shared_classes(bool jcmd_request, TRAPS) {
 757   AOTClassLinker::initialize();
 758 
 759   if (!jcmd_request && !CDSConfig::is_dumping_final_static_archive()) {
 760     LambdaFormInvokers::regenerate_holder_classes(CHECK);
 761   }
 762 
 763   // Collect all loaded ClassLoaderData.
 764   CollectCLDClosure collect_cld(THREAD);
 765   {
 766     // ClassLoaderDataGraph::loaded_cld_do requires ClassLoaderDataGraph_lock.
 767     // We cannot link the classes while holding this lock (or else we may run into deadlock).
 768     // Therefore, we need to first collect all the CLDs, and then link their classes after
 769     // releasing the lock.
 770     MutexLocker lock(ClassLoaderDataGraph_lock);
 771     ClassLoaderDataGraph::loaded_cld_do(&collect_cld);
 772   }
 773 
 774   while (true) {
 775     bool has_linked = false;
 776     for (int i = 0; i < collect_cld.nof_cld(); i++) {
 777       ClassLoaderData* cld = collect_cld.cld_at(i);
 778       for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) {
 779         if (klass->is_instance_klass()) {
 780           InstanceKlass* ik = InstanceKlass::cast(klass);
 781           if (may_be_eagerly_linked(ik)) {
 782             has_linked |= link_class_for_cds(ik, CHECK);



 783           }
 784         }
 785       }
 786     }
 787 
 788     if (!has_linked) {
 789       break;
 790     }
 791     // Class linking includes verification which may load more classes.
 792     // Keep scanning until we have linked no more classes.
 793   }
 794 














 795   if (CDSConfig::is_dumping_final_static_archive()) {
 796     FinalImageRecipes::apply_recipes(CHECK);
 797   }
 798 }
 799 
 800 void MetaspaceShared::prepare_for_dumping() {
 801   assert(CDSConfig::is_dumping_archive(), "sanity");
 802   CDSConfig::check_unsupported_dumping_module_options();
 803 }
 804 
 805 // Preload classes from a list, populate the shared spaces and dump to a
 806 // file.
 807 void MetaspaceShared::preload_and_dump(TRAPS) {
 808   CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
 809   ResourceMark rm(THREAD);







 810   StaticArchiveBuilder builder;
 811   preload_and_dump_impl(builder, THREAD);
 812   if (HAS_PENDING_EXCEPTION) {
 813     if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 814       log_error(cds)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
 815                      "%zuM", MaxHeapSize/M);
 816       MetaspaceShared::writing_error();
 817     } else {
 818       log_error(cds)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
 819                      java_lang_String::as_utf8_string(java_lang_Throwable::message(PENDING_EXCEPTION)));
 820       MetaspaceShared::writing_error("Unexpected exception, use -Xlog:cds,exceptions=trace for detail");
 821     }
 822   }
 823 
 824   if (CDSConfig::new_aot_flags_used()) {
 825     if (CDSConfig::is_dumping_preimage_static_archive()) {



 826       tty->print_cr("AOTConfiguration recorded: %s", AOTConfiguration);
 827       vm_exit(0);
 828     } else {
 829       // The JLI launcher only recognizes the "old" -Xshare:dump flag.
 830       // When the new -XX:AOTMode=create flag is used, we can't return
 831       // to the JLI launcher, as the launcher will fail when trying to
 832       // run the main class, which is not what we want.
 833       tty->print_cr("AOTCache creation is complete: %s", AOTCache);
 834       vm_exit(0);
 835     }
 836   }
 837 }
 838 
 839 #if INCLUDE_CDS_JAVA_HEAP && defined(_LP64)
 840 void MetaspaceShared::adjust_heap_sizes_for_dumping() {
 841   if (!CDSConfig::is_dumping_heap() || UseCompressedOops) {
 842     return;
 843   }
 844   // CDS heap dumping requires all string oops to have an offset
 845   // from the heap bottom that can be encoded in 32-bit.
 846   julong max_heap_size = (julong)(4 * G);
 847 

 914     }
 915   }
 916 
 917   // Some classes are used at CDS runtime but are not loaded, and therefore archived, at
 918   // dumptime. We can perform dummmy calls to these classes at dumptime to ensure they
 919   // are archived.
 920   exercise_runtime_cds_code(CHECK);
 921 
 922   log_info(cds)("Loading classes to share: done.");
 923 }
 924 
 925 void MetaspaceShared::exercise_runtime_cds_code(TRAPS) {
 926   // Exercise the manifest processing code
 927   const char* dummy = "Manifest-Version: 1.0\n";
 928   CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
 929 
 930   // Exercise FileSystem and URL code
 931   CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
 932 }
 933 







 934 void MetaspaceShared::preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) {






 935   if (CDSConfig::is_dumping_classic_static_archive()) {
 936     // We are running with -Xshare:dump
 937     preload_classes(CHECK);
 938 
 939     if (SharedArchiveConfigFile) {
 940       log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
 941       read_extra_data(THREAD, SharedArchiveConfigFile);
 942       log_info(cds)("Reading extra data: done.");
 943     }
 944   }
 945 
 946   if (CDSConfig::is_dumping_preimage_static_archive()) {
 947     log_info(cds)("Reading lambda form invokers from JDK default classlist ...");
 948     char default_classlist[JVM_MAXPATHLEN];
 949     get_default_classlist(default_classlist, sizeof(default_classlist));
 950     struct stat statbuf;
 951     if (os::stat(default_classlist, &statbuf) == 0) {
 952       ClassListParser::parse_classlist(default_classlist,
 953                                        ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
 954     }
 955   }
 956 
 957   if (CDSConfig::is_dumping_final_static_archive()) {
 958     if (ExtraSharedClassListFile) {
 959       log_info(cds)("Loading extra classes from %s ...", ExtraSharedClassListFile);
 960       ClassListParser::parse_classlist(ExtraSharedClassListFile,
 961                                        ClassListParser::_parse_all, CHECK);
 962     }
 963   }
 964 












 965   // Rewrite and link classes
 966   log_info(cds)("Rewriting and linking classes ...");
 967 
 968   // Link any classes which got missed. This would happen if we have loaded classes that
 969   // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
 970   // fails verification, all other interfaces that were not specified in the classlist but
 971   // are implemented by K are not verified.
 972   link_shared_classes(false/*not from jcmd*/, CHECK);
 973   log_info(cds)("Rewriting and linking classes: done");
 974 


















 975 #if INCLUDE_CDS_JAVA_HEAP
 976   if (CDSConfig::is_dumping_heap()) {
 977     if (!HeapShared::is_archived_boot_layer_available(THREAD)) {
 978       log_info(cds)("archivedBootLayer not available, disabling full module graph");
 979       CDSConfig::stop_dumping_full_module_graph();
 980     }
 981     HeapShared::init_for_dumping(CHECK);
 982     ArchiveHeapWriter::init();
 983     if (CDSConfig::is_dumping_full_module_graph()) {
 984       ClassLoaderDataShared::ensure_module_entry_tables_exist();
 985       HeapShared::reset_archived_object_states(CHECK);
 986     }
 987 
 988     if (CDSConfig::is_dumping_invokedynamic()) {
 989       // This assert means that the MethodType and MethodTypeForm tables won't be
 990       // updated concurrently when we are saving their contents into a side table.
 991       assert(CDSConfig::allow_only_single_java_thread(), "Required");
 992 
 993       JavaValue result(T_VOID);
 994       JavaCalls::call_static(&result, vmClasses::MethodType_klass(),
 995                              vmSymbols::createArchivedObjects(),
 996                              vmSymbols::void_method_signature(),
 997                              CHECK);
 998 

 999       // java.lang.Class::reflectionFactory cannot be archived yet. We set this field
1000       // to null, and it will be initialized again at runtime.
1001       log_debug(cds)("Resetting Class::reflectionFactory");
1002       TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates");
1003       Symbol* method_sig = vmSymbols::void_method_signature();

1004       JavaCalls::call_static(&result, vmClasses::Class_klass(),
1005                              method_name, method_sig, CHECK);
1006 
1007       // Perhaps there is a way to avoid hard-coding these names here.
1008       // See discussion in JDK-8342481.
1009     }
1010 
1011     // Do this at the very end, when no Java code will be executed. Otherwise
1012     // some new strings may be added to the intern table.
1013     StringTable::allocate_shared_strings_array(CHECK);
1014   } else {
1015     log_info(cds)("Not dumping heap, reset CDSConfig::_is_using_optimized_module_handling");
1016     CDSConfig::stop_using_optimized_module_handling();
1017   }
1018 #endif
1019 
1020   VM_PopulateDumpSharedSpace op(builder);
1021   VMThread::execute(&op);


1022 
1023   if (!write_static_archive(&builder, op.map_info(), op.heap_info())) {






























1024     THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
1025   }
1026 }
1027 
1028 bool MetaspaceShared::write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info) {
1029   // relocate the data so that it can be mapped to MetaspaceShared::requested_base_address()
1030   // without runtime relocation.
1031   builder->relocate_to_requested();
1032 
1033   map_info->open_for_write();
1034   if (!map_info->is_open()) {
1035     return false;
1036   }
1037   builder->write_archive(map_info, heap_info);
1038 
1039   if (AllowArchivingWithJavaAgent) {
1040     log_warning(cds)("This %s was created with AllowArchivingWithJavaAgent. It should be used "
1041             "for testing purposes only and should not be used in a production environment", CDSConfig::type_of_archive_being_loaded());
1042   }
1043   return true;
1044 }
1045 




















































































1046 // Returns true if the class's status has changed.
1047 bool MetaspaceShared::try_link_class(JavaThread* current, InstanceKlass* ik) {
1048   ExceptionMark em(current);
1049   JavaThread* THREAD = current; // For exception macros.
1050   assert(CDSConfig::is_dumping_archive(), "sanity");
1051 
1052   if (ik->is_shared() && !CDSConfig::is_dumping_final_static_archive()) {
1053     assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
1054     return false;
1055   }
1056 
1057   if (ik->is_loaded() && !ik->is_linked() && ik->can_be_verified_at_dumptime() &&
1058       !SystemDictionaryShared::has_class_failed_verification(ik)) {
1059     bool saved = BytecodeVerificationLocal;
1060     if (ik->is_shared_unregistered_class() && ik->class_loader() == nullptr) {
1061       // The verification decision is based on BytecodeVerificationRemote
1062       // for non-system classes. Since we are using the null classloader
1063       // to load non-system classes for customized class loaders during dumping,
1064       // we need to temporarily change BytecodeVerificationLocal to be the same as
1065       // BytecodeVerificationRemote. Note this can cause the parent system

1071     if (HAS_PENDING_EXCEPTION) {
1072       ResourceMark rm(THREAD);
1073       log_warning(cds)("Preload Warning: Verification failed for %s",
1074                     ik->external_name());
1075       CLEAR_PENDING_EXCEPTION;
1076       SystemDictionaryShared::set_class_has_failed_verification(ik);
1077     } else {
1078       assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1079       ik->compute_has_loops_flag_for_methods();
1080     }
1081     BytecodeVerificationLocal = saved;
1082     return true;
1083   } else {
1084     return false;
1085   }
1086 }
1087 
1088 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1089   if (CDSConfig::is_dumping_heap()) {
1090     HeapShared::write_heap(&_heap_info);
1091   } else {
1092     CDSConfig::log_reasons_for_not_dumping_heap();
1093   }
1094 }
1095 
1096 void MetaspaceShared::set_shared_metaspace_range(void* base, void *static_top, void* top) {
1097   assert(base <= static_top && static_top <= top, "must be");
1098   _shared_metaspace_static_top = static_top;
1099   MetaspaceObj::set_shared_metaspace_range(base, top);
1100 }
1101 
1102 bool MetaspaceShared::is_shared_dynamic(void* p) {
1103   if ((p < MetaspaceObj::shared_metaspace_top()) &&
1104       (p >= _shared_metaspace_static_top)) {
1105     return true;
1106   } else {
1107     return false;
1108   }
1109 }
1110 
1111 bool MetaspaceShared::is_shared_static(void* p) {

1139 // This function is called when the JVM is unable to write the specified CDS archive due to an
1140 // unrecoverable error.
1141 void MetaspaceShared::unrecoverable_writing_error(const char* message) {
1142   writing_error(message);
1143   vm_direct_exit(1);
1144 }
1145 
1146 // This function is called when the JVM is unable to write the specified CDS archive due to a
1147 // an error. The error will be propagated
1148 void MetaspaceShared::writing_error(const char* message) {
1149   log_error(cds)("An error has occurred while writing the shared archive file.");
1150   if (message != nullptr) {
1151     log_error(cds)("%s", message);
1152   }
1153 }
1154 
1155 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
1156   assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
1157   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1158 
1159   FileMapInfo* static_mapinfo = open_static_archive();
1160   FileMapInfo* dynamic_mapinfo = nullptr;
1161 
1162   if (static_mapinfo != nullptr) {
1163     log_info(cds)("Core region alignment: %zu", static_mapinfo->core_region_alignment());
1164     dynamic_mapinfo = open_dynamic_archive();
1165 
1166     // First try to map at the requested address
1167     result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1168     if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1169       // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1170       // by the OS.
1171       log_info(cds)("Try to map archive(s) at an alternative address");
1172       result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1173     }
1174   }
1175 
1176   if (result == MAP_ARCHIVE_SUCCESS) {
1177     bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());
1178     char* cds_base = static_mapinfo->mapped_base();
1179     char* cds_end =  dynamic_mapped ? dynamic_mapinfo->mapped_end() : static_mapinfo->mapped_end();

1183     _relocation_delta = static_mapinfo->relocation_delta();
1184     _requested_base_address = static_mapinfo->requested_base_address();
1185     if (dynamic_mapped) {
1186       // turn AutoCreateSharedArchive off if successfully mapped
1187       AutoCreateSharedArchive = false;
1188     }
1189   } else {
1190     set_shared_metaspace_range(nullptr, nullptr, nullptr);
1191     if (CDSConfig::is_dumping_dynamic_archive()) {
1192       log_warning(cds)("-XX:ArchiveClassesAtExit is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info.");
1193     }
1194     UseSharedSpaces = false;
1195     // The base archive cannot be mapped. We cannot dump the dynamic shared archive.
1196     AutoCreateSharedArchive = false;
1197     CDSConfig::disable_dumping_dynamic_archive();
1198     log_info(cds)("Unable to map shared spaces");
1199     if (PrintSharedArchiveAndExit) {
1200       MetaspaceShared::unrecoverable_loading_error("Unable to use shared archive.");
1201     } else if (RequireSharedSpaces) {
1202       MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");




1203     }
1204   }
1205 
1206   // If mapping failed and -XShare:on, the vm should exit
1207   bool has_failed = false;
1208   if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1209     has_failed = true;
1210     delete static_mapinfo;
1211   }
1212   if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1213     has_failed = true;
1214     delete dynamic_mapinfo;
1215   }
1216   if (RequireSharedSpaces && has_failed) {

1217       MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1218   }
1219 }
1220 
1221 FileMapInfo* MetaspaceShared::open_static_archive() {




1222   const char* static_archive = CDSConfig::static_archive_path();
1223   assert(static_archive != nullptr, "sanity");
1224   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1225   if (!mapinfo->initialize()) {
1226     delete(mapinfo);
1227     return nullptr;


1228   }
1229   return mapinfo;
1230 }
1231 
1232 FileMapInfo* MetaspaceShared::open_dynamic_archive() {
1233   if (CDSConfig::is_dumping_dynamic_archive()) {
1234     return nullptr;
1235   }
1236   const char* dynamic_archive = CDSConfig::dynamic_archive_path();
1237   if (dynamic_archive == nullptr) {
1238     return nullptr;
1239   }
1240 
1241   FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1242   if (!mapinfo->initialize()) {
1243     delete(mapinfo);
1244     if (RequireSharedSpaces) {
1245       MetaspaceShared::unrecoverable_loading_error("Failed to initialize dynamic archive");
1246     }
1247     return nullptr;
1248   }
1249   return mapinfo;

1655       MemoryReserver::release(archive_space_rs);
1656       archive_space_rs = {};
1657     }
1658     if (class_space_rs.is_reserved()) {
1659       log_debug(cds)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
1660       MemoryReserver::release(class_space_rs);
1661       class_space_rs = {};
1662     }
1663   }
1664 }
1665 
1666 static int archive_regions[]     = { MetaspaceShared::rw, MetaspaceShared::ro };
1667 static int archive_regions_count = 2;
1668 
1669 MapArchiveResult MetaspaceShared::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
1670   assert(CDSConfig::is_using_archive(), "must be runtime");
1671   if (mapinfo == nullptr) {
1672     return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
1673   }
1674 




1675   mapinfo->set_is_mapped(false);
1676   if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
1677     log_info(cds)("Unable to map CDS archive -- core_region_alignment() expected: %zu"
1678                   " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment());
1679     return MAP_ARCHIVE_OTHER_FAILURE;
1680   }
1681 
1682   MapArchiveResult result =
1683     mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
1684 
1685   if (result != MAP_ARCHIVE_SUCCESS) {
1686     unmap_archive(mapinfo);
1687     return result;
1688   }
1689 
1690   if (!mapinfo->validate_class_location()) {
1691     unmap_archive(mapinfo);
1692     return MAP_ARCHIVE_OTHER_FAILURE;
1693   }
1694 

1732 };
1733 
1734 // Read the miscellaneous data from the shared file, and
1735 // serialize it out to its various destinations.
1736 
1737 void MetaspaceShared::initialize_shared_spaces() {
1738   FileMapInfo *static_mapinfo = FileMapInfo::current_info();
1739 
1740   // Verify various attributes of the archive, plus initialize the
1741   // shared string/symbol tables.
1742   char* buffer = static_mapinfo->serialized_data();
1743   intptr_t* array = (intptr_t*)buffer;
1744   ReadClosure rc(&array, (intptr_t)SharedBaseAddress);
1745   serialize(&rc);
1746 
1747   // Finish up archived heap initialization. These must be
1748   // done after ReadClosure.
1749   static_mapinfo->patch_heap_embedded_pointers();
1750   ArchiveHeapLoader::finish_initialization();
1751   Universe::load_archived_object_instances();

1752 
1753   // Close the mapinfo file
1754   static_mapinfo->close();
1755 
1756   static_mapinfo->unmap_region(MetaspaceShared::bm);
1757 
1758   FileMapInfo *dynamic_mapinfo = FileMapInfo::dynamic_info();
1759   if (dynamic_mapinfo != nullptr) {
1760     intptr_t* buffer = (intptr_t*)dynamic_mapinfo->serialized_data();
1761     ReadClosure rc(&buffer, (intptr_t)SharedBaseAddress);
1762     ArchiveBuilder::serialize_dynamic_archivable_items(&rc);
1763     DynamicArchive::setup_array_klasses();
1764     dynamic_mapinfo->close();
1765     dynamic_mapinfo->unmap_region(MetaspaceShared::bm);
1766   }
1767 
1768   LogStreamHandle(Info, cds) lsh;
1769   if (lsh.is_enabled()) {
1770     lsh.print("Using AOT-linked classes: %s (static archive: %s aot-linked classes",
1771               BOOL_TO_STR(CDSConfig::is_using_aot_linked_classes()),

1782     // Read stored LF format lines stored in static archive
1783     LambdaFormInvokers::read_static_archive_invokers();
1784   }
1785 
1786   if (PrintSharedArchiveAndExit) {
1787     // Print archive names
1788     if (dynamic_mapinfo != nullptr) {
1789       tty->print_cr("\n\nBase archive name: %s", CDSConfig::static_archive_path());
1790       tty->print_cr("Base archive version %d", static_mapinfo->version());
1791     } else {
1792       tty->print_cr("Static archive name: %s", static_mapinfo->full_path());
1793       tty->print_cr("Static archive version %d", static_mapinfo->version());
1794     }
1795 
1796     SystemDictionaryShared::print_shared_archive(tty);
1797     if (dynamic_mapinfo != nullptr) {
1798       tty->print_cr("\n\nDynamic archive name: %s", dynamic_mapinfo->full_path());
1799       tty->print_cr("Dynamic archive version %d", dynamic_mapinfo->version());
1800       SystemDictionaryShared::print_shared_archive(tty, false/*dynamic*/);
1801     }






1802 
1803     // collect shared symbols and strings
1804     CountSharedSymbols cl;
1805     SymbolTable::shared_symbols_do(&cl);
1806     tty->print_cr("Number of shared symbols: %d", cl.total());
1807     tty->print_cr("Number of shared strings: %zu", StringTable::shared_entry_count());
1808     tty->print_cr("VM version: %s\r\n", static_mapinfo->vm_version());
1809     if (FileMapInfo::current_info() == nullptr || _archive_loading_failed) {
1810       tty->print_cr("archive is invalid");
1811       vm_exit(1);
1812     } else {
1813       tty->print_cr("archive is valid");
1814       vm_exit(0);
1815     }
1816   }
1817 }
1818 
1819 // JVM/TI RedefineClasses() support:
1820 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
1821   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");

   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "cds/aotClassInitializer.hpp"
  26 #include "cds/aotArtifactFinder.hpp"
  27 #include "cds/aotClassLinker.hpp"
  28 #include "cds/aotClassLocation.hpp"
  29 #include "cds/aotConstantPoolResolver.hpp"
  30 #include "cds/aotLinkedClassBulkLoader.hpp"
  31 #include "cds/archiveBuilder.hpp"
  32 #include "cds/archiveHeapLoader.hpp"
  33 #include "cds/archiveHeapWriter.hpp"
  34 #include "cds/cds_globals.hpp"
  35 #include "cds/cdsAccess.hpp"
  36 #include "cds/cdsConfig.hpp"
  37 #include "cds/cdsProtectionDomain.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/memoryReserver.hpp"
  73 #include "memory/metaspace.hpp"
  74 #include "memory/metaspaceClosure.hpp"
  75 #include "memory/resourceArea.hpp"
  76 #include "memory/universe.hpp"
  77 #include "nmt/memTracker.hpp"
  78 #include "oops/compressedKlass.hpp"
  79 #include "oops/instanceMirrorKlass.hpp"
  80 #include "oops/klass.inline.hpp"
  81 #include "oops/method.inline.hpp"
  82 #include "oops/objArrayOop.hpp"
  83 #include "oops/oop.inline.hpp"
  84 #include "oops/oopHandle.hpp"
  85 #include "oops/trainingData.hpp"
  86 #include "prims/jvmtiExport.hpp"
  87 #include "prims/whitebox.hpp"
  88 #include "runtime/arguments.hpp"
  89 #include "runtime/globals.hpp"
  90 #include "runtime/globals_extension.hpp"
  91 #include "runtime/handles.inline.hpp"
  92 #include "runtime/javaCalls.hpp"
  93 #include "runtime/os.inline.hpp"
  94 #include "runtime/safepointVerifiers.hpp"
  95 #include "runtime/sharedRuntime.hpp"
  96 #include "runtime/vmOperations.hpp"
  97 #include "runtime/vmThread.hpp"
  98 #include "sanitizers/leak.hpp"
  99 #include "utilities/align.hpp"
 100 #include "utilities/bitMap.inline.hpp"
 101 #include "utilities/defaultStream.hpp"
 102 #include "utilities/macros.hpp"
 103 #include "utilities/ostream.hpp"
 104 #include "utilities/resourceHash.hpp"
 105 
 106 ReservedSpace MetaspaceShared::_symbol_rs;
 107 VirtualSpace MetaspaceShared::_symbol_vs;
 108 bool MetaspaceShared::_archive_loading_failed = false;
 109 bool MetaspaceShared::_remapped_readwrite = false;
 110 void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
 111 intx MetaspaceShared::_relocation_delta;
 112 char* MetaspaceShared::_requested_base_address;
 113 Array<Method*>* MetaspaceShared::_archived_method_handle_intrinsics = nullptr;
 114 bool MetaspaceShared::_use_optimized_module_handling = true;
 115 int volatile MetaspaceShared::_preimage_static_archive_dumped = 0;
 116 
 117 // The CDS archive is divided into the following regions:
 118 //     rw  - read-write metadata
 119 //     ro  - read-only metadata and read-only tables
 120 //     hp  - heap region
 121 //     bm  - bitmap for relocating the above 7 regions.
 122 //
 123 // The rw and ro regions are linearly allocated, in the order of rw->ro.
 124 // These regions are aligned with MetaspaceShared::core_region_alignment().
 125 //
 126 // These 2 regions are populated in the following steps:
 127 // [0] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
 128 //     temporarily allocated outside of the shared regions.
 129 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
 130 // [2] C++ vtables are copied into the rw region.
 131 // [3] ArchiveBuilder copies RW metadata into the rw region.
 132 // [4] ArchiveBuilder copies RO metadata into the ro region.
 133 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
 134 //     are copied into the ro region as read-only tables.
 135 //

 291   }
 292 
 293   // Arguments::default_SharedBaseAddress() is hard-coded in cds_globals.hpp. It must be carefully
 294   // picked that (a) the align_up() below will always return a valid value; (b) none of
 295   // the following asserts will fail.
 296   log_warning(cds)("SharedBaseAddress (" INTPTR_FORMAT ") is %s. Reverted to " INTPTR_FORMAT,
 297                    p2i((void*)SharedBaseAddress), err,
 298                    p2i((void*)Arguments::default_SharedBaseAddress()));
 299 
 300   specified_base = (char*)Arguments::default_SharedBaseAddress();
 301   aligned_base = align_up(specified_base, alignment);
 302 
 303   // Make sure the default value of SharedBaseAddress specified in globals.hpp is sane.
 304   assert(!shared_base_too_high(specified_base, aligned_base, cds_max), "Sanity");
 305   assert(shared_base_valid(aligned_base), "Sanity");
 306   return aligned_base;
 307 }
 308 
 309 void MetaspaceShared::initialize_for_static_dump() {
 310   assert(CDSConfig::is_dumping_static_archive(), "sanity");
 311 
 312   if (CDSConfig::is_dumping_preimage_static_archive() || CDSConfig::is_dumping_final_static_archive()) {
 313     if (!((UseG1GC || UseParallelGC || UseSerialGC || UseEpsilonGC || UseShenandoahGC) && UseCompressedClassPointers)) {
 314       const char* error;
 315       if (CDSConfig::is_experimental_leyden_workflow()) {
 316         error = "Cannot create the CacheDataStore";
 317       } else if (CDSConfig::is_dumping_preimage_static_archive()) {
 318         error = "Cannot create the AOT configuration file";
 319       } else {
 320         error = "Cannot create the AOT cache";
 321       }
 322 
 323       vm_exit_during_initialization(error,
 324                                     "UseCompressedClassPointers must be enabled, and collector must be G1, Parallel, Serial, Epsilon, or Shenandoah");
 325     }
 326   }
 327 
 328   log_info(cds)("Core region alignment: %zu", core_region_alignment());
 329 
 330   // The max allowed size for CDS archive. We use this to limit SharedBaseAddress
 331   // to avoid address space wrap around.
 332   size_t cds_max;
 333   const size_t reserve_alignment = core_region_alignment();
 334 
 335 #ifdef _LP64
 336   const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
 337   cds_max = align_down(UnscaledClassSpaceMax, reserve_alignment);
 338 #else
 339   // We don't support archives larger than 256MB on 32-bit due to limited
 340   //  virtual address space.
 341   cds_max = align_down(256*M, reserve_alignment);
 342 #endif
 343 
 344   _requested_base_address = compute_shared_base(cds_max);
 345   SharedBaseAddress = (size_t)_requested_base_address;
 346 
 347   size_t symbol_rs_size = LP64_ONLY(3 * G) NOT_LP64(128 * M);
 348   _symbol_rs = MemoryReserver::reserve(symbol_rs_size,
 349                                        os::vm_allocation_granularity(),

 501   // in archived Metadata objects.
 502   CppVtables::serialize(soc);
 503   soc->do_tag(--tag);
 504 
 505   // Dump/restore miscellaneous metadata.
 506   JavaClasses::serialize_offsets(soc);
 507   Universe::serialize(soc);
 508   soc->do_tag(--tag);
 509 
 510   // Dump/restore references to commonly used names and signatures.
 511   vmSymbols::serialize(soc);
 512   soc->do_tag(--tag);
 513 
 514   // Dump/restore the symbol/string/subgraph_info tables
 515   SymbolTable::serialize_shared_table_header(soc);
 516   StringTable::serialize_shared_table_header(soc);
 517   HeapShared::serialize_tables(soc);
 518   SystemDictionaryShared::serialize_dictionary_headers(soc);
 519   AOTLinkedClassBulkLoader::serialize(soc, true);
 520   FinalImageRecipes::serialize(soc);
 521   TrainingData::serialize_training_data(soc);
 522   InstanceMirrorKlass::serialize_offsets(soc);
 523 
 524   // Dump/restore well known classes (pointers)
 525   SystemDictionaryShared::serialize_vm_classes(soc);
 526   soc->do_tag(--tag);
 527 
 528   CDS_JAVA_HEAP_ONLY(ClassLoaderDataShared::serialize(soc);)
 529   soc->do_ptr((void**)&_archived_method_handle_intrinsics);
 530 
 531   LambdaFormInvokers::serialize(soc);
 532   AdapterHandlerLibrary::serialize_shared_table_header(soc);
 533 
 534   soc->do_tag(666);
 535 }
 536 
 537 static void rewrite_nofast_bytecode(const methodHandle& method) {
 538   BytecodeStream bcs(method);
 539   while (!bcs.is_last_bytecode()) {
 540     Bytecodes::Code opcode = bcs.next();
 541     switch (opcode) {
 542     case Bytecodes::_getfield:      *bcs.bcp() = Bytecodes::_nofast_getfield;      break;
 543     case Bytecodes::_putfield:      *bcs.bcp() = Bytecodes::_nofast_putfield;      break;
 544     case Bytecodes::_aload_0:       *bcs.bcp() = Bytecodes::_nofast_aload_0;       break;
 545     case Bytecodes::_iload: {
 546       if (!bcs.is_wide()) {
 547         *bcs.bcp() = Bytecodes::_nofast_iload;
 548       }
 549       break;
 550     }
 551     default: break;
 552     }
 553   }

 588     VM_Operation(), _heap_info(), _map_info(nullptr), _builder(b) {}
 589 
 590   bool skip_operation() const { return false; }
 591 
 592   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
 593   ArchiveHeapInfo* heap_info()  { return &_heap_info; }
 594   FileMapInfo* map_info() const { return _map_info; }
 595   void doit();   // outline because gdb sucks
 596   bool allow_nested_vm_operations() const { return true; }
 597 }; // class VM_PopulateDumpSharedSpace
 598 
 599 class StaticArchiveBuilder : public ArchiveBuilder {
 600 public:
 601   StaticArchiveBuilder() : ArchiveBuilder() {}
 602 
 603   virtual void iterate_roots(MetaspaceClosure* it) {
 604     AOTArtifactFinder::all_cached_classes_do(it);
 605     SystemDictionaryShared::dumptime_classes_do(it);
 606     Universe::metaspace_pointers_do(it);
 607     vmSymbols::metaspace_pointers_do(it);
 608     TrainingData::iterate_roots(it);
 609 
 610     // The above code should find all the symbols that are referenced by the
 611     // archived classes. We just need to add the extra symbols which
 612     // may not be used by any of the archived classes -- these are usually
 613     // symbols that we anticipate to be used at run time, so we can store
 614     // them in the RO region, to be shared across multiple processes.
 615     if (_extra_symbols != nullptr) {
 616       for (int i = 0; i < _extra_symbols->length(); i++) {
 617         it->push(_extra_symbols->adr_at(i));
 618       }
 619     }
 620 
 621     for (int i = 0; i < _pending_method_handle_intrinsics->length(); i++) {
 622       it->push(_pending_method_handle_intrinsics->adr_at(i));
 623     }
 624   }
 625 };
 626 
 627 char* VM_PopulateDumpSharedSpace::dump_early_read_only_tables() {
 628   ArchiveBuilder::OtherROAllocMark mark;
 629 
 630   CDS_JAVA_HEAP_ONLY(Modules::dump_archived_module_info());
 631 
 632   DumpRegion* ro_region = ArchiveBuilder::current()->ro_region();
 633   char* start = ro_region->top();
 634   WriteClosure wc(ro_region);
 635   MetaspaceShared::early_serialize(&wc);
 636   return start;
 637 }
 638 
 639 char* VM_PopulateDumpSharedSpace::dump_read_only_tables(AOTClassLocationConfig*& cl_config) {
 640   ArchiveBuilder::OtherROAllocMark mark;
 641 
 642   SystemDictionaryShared::write_to_archive();
 643   cl_config = AOTClassLocationConfig::dumptime()->write_to_archive();
 644   AOTClassLinker::write_to_archive();
 645   if (CDSConfig::is_dumping_preimage_static_archive()) {
 646     FinalImageRecipes::record_recipes();
 647   }
 648 
 649   TrainingData::dump_training_data();
 650 
 651   MetaspaceShared::write_method_handle_intrinsics();
 652 
 653   // Write lambform lines into archive
 654   LambdaFormInvokers::dump_static_archive_invokers();
 655 
 656   if (CDSConfig::is_dumping_adapters()) {
 657     AdapterHandlerLibrary::archive_adapter_table();
 658   }
 659 
 660   // Write the other data to the output array.
 661   DumpRegion* ro_region = ArchiveBuilder::current()->ro_region();
 662   char* start = ro_region->top();
 663   WriteClosure wc(ro_region);
 664   MetaspaceShared::serialize(&wc);
 665 
 666   return start;
 667 }
 668 
 669 void VM_PopulateDumpSharedSpace::doit() {
 670   if (!CDSConfig::is_dumping_final_static_archive()) {
 671     guarantee(!CDSConfig::is_using_archive(), "We should not be using an archive when we dump");
 672   }
 673 
 674   DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm);
 675 
 676   _pending_method_handle_intrinsics = new (mtClassShared) GrowableArray<Method*>(256, mtClassShared);
 677   if (CDSConfig::is_dumping_aot_linked_classes()) {
 678     // When dumping AOT-linked classes, some classes may have direct references to a method handle
 679     // intrinsic. The easiest thing is to save all of them into the AOT cache.

 702   CppVtables::dumptime_init(&_builder);
 703 
 704   _builder.sort_metadata_objs();
 705   _builder.dump_rw_metadata();
 706   _builder.dump_ro_metadata();
 707   _builder.relocate_metaspaceobj_embedded_pointers();
 708 
 709   log_info(cds)("Make classes shareable");
 710   _builder.make_klasses_shareable();
 711   MetaspaceShared::make_method_handle_intrinsics_shareable();
 712 
 713   dump_java_heap_objects();
 714   dump_shared_symbol_table(_builder.symbols());
 715 
 716   char* early_serialized_data = dump_early_read_only_tables();
 717   AOTClassLocationConfig* cl_config;
 718   char* serialized_data = dump_read_only_tables(cl_config);
 719 
 720   SystemDictionaryShared::adjust_lambda_proxy_class_dictionary();
 721 
 722   log_info(cds)("Make training data shareable");
 723   _builder.make_training_data_shareable();
 724 
 725   // The vtable clones contain addresses of the current process.
 726   // We don't want to write these addresses into the archive.
 727   CppVtables::zero_archived_vtables();
 728 
 729   // Write the archive file
 730   const char* static_archive;
 731   if (CDSConfig::is_dumping_final_static_archive()) {
 732     if (CDSConfig::is_experimental_leyden_workflow()) {
 733       static_archive = CacheDataStore;
 734     } else {
 735       static_archive = AOTCache;
 736     }
 737     FileMapInfo::free_current_info();
 738   } else {
 739     static_archive = CDSConfig::static_archive_path();
 740   }
 741   assert(static_archive != nullptr, "SharedArchiveFile not set?");
 742   _map_info = new FileMapInfo(static_archive, true);
 743   _map_info->populate_header(MetaspaceShared::core_region_alignment());
 744   _map_info->set_early_serialized_data(early_serialized_data);
 745   _map_info->set_serialized_data(serialized_data);
 746   _map_info->set_cloned_vtables(CppVtables::vtables_serialized_base());
 747   _map_info->header()->set_class_location_config(cl_config);
 748 }
 749 
 750 class CollectCLDClosure : public CLDClosure {
 751   GrowableArray<ClassLoaderData*> _loaded_cld;
 752   GrowableArray<OopHandle> _loaded_cld_handles; // keep the CLDs alive
 753   Thread* _current_thread;
 754 public:
 755   CollectCLDClosure(Thread* thread) : _current_thread(thread) {}
 756   ~CollectCLDClosure() {
 757     for (int i = 0; i < _loaded_cld_handles.length(); i++) {
 758       _loaded_cld_handles.at(i).release(Universe::vm_global());
 759     }
 760   }
 761   void do_cld(ClassLoaderData* cld) {
 762     assert(cld->is_alive(), "must be");
 763     _loaded_cld.append(cld);
 764     _loaded_cld_handles.append(OopHandle(Universe::vm_global(), cld->holder()));
 765   }
 766 
 767   int nof_cld() const                { return _loaded_cld.length(); }
 768   ClassLoaderData* cld_at(int index) { return _loaded_cld.at(index); }
 769 };
 770 
 771 // Check if we can eagerly link this class at dump time, so we can avoid the
 772 // runtime linking overhead (especially verification)
 773 bool MetaspaceShared::may_be_eagerly_linked(InstanceKlass* ik) {
 774   if (CDSConfig::preserve_all_dumptime_verification_states(ik)) {
 775     assert(ik->can_be_verified_at_dumptime(), "sanity");
 776   }
 777   if (!ik->can_be_verified_at_dumptime()) {
 778     // For old classes, try to leave them in the unlinked state, so
 779     // we can still store them in the archive. They must be
 780     // linked/verified at runtime.
 781     return false;
 782   }
 783 
 784   if (CDSConfig::is_dumping_dynamic_archive() && ik->is_shared_unregistered_class()) {
 785     // Linking of unregistered classes at this stage may cause more
 786     // classes to be resolved, resulting in calls to ClassLoader.loadClass()
 787     // that may not be expected by custom class loaders.
 788     //
 789     // It's OK to do this for the built-in loaders as we know they can
 790     // tolerate this.
 791     return false;
 792   }
 793   return true;
 794 }
 795 








 796 
 797 void MetaspaceShared::link_shared_classes(TRAPS) {
 798   AOTClassLinker::initialize();
 799 




 800   // Collect all loaded ClassLoaderData.
 801   CollectCLDClosure collect_cld(THREAD);
 802   {
 803     // ClassLoaderDataGraph::loaded_cld_do requires ClassLoaderDataGraph_lock.
 804     // We cannot link the classes while holding this lock (or else we may run into deadlock).
 805     // Therefore, we need to first collect all the CLDs, and then link their classes after
 806     // releasing the lock.
 807     MutexLocker lock(ClassLoaderDataGraph_lock);
 808     ClassLoaderDataGraph::loaded_cld_do(&collect_cld);
 809   }
 810 
 811   while (true) {
 812     bool has_linked = false;
 813     for (int i = 0; i < collect_cld.nof_cld(); i++) {
 814       ClassLoaderData* cld = collect_cld.cld_at(i);
 815       for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) {
 816         if (klass->is_instance_klass()) {
 817           InstanceKlass* ik = InstanceKlass::cast(klass);
 818           if (may_be_eagerly_linked(ik)) {
 819             has_linked |= try_link_class(THREAD, ik);
 820           }
 821           if (CDSConfig::is_dumping_heap() && ik->is_linked() && !ik->is_initialized()) {
 822             AOTClassInitializer::maybe_preinit_class(ik, CHECK);
 823           }
 824         }
 825       }
 826     }
 827 
 828     if (!has_linked) {
 829       break;
 830     }
 831     // Class linking includes verification which may load more classes.
 832     // Keep scanning until we have linked no more classes.
 833   }
 834 
 835   // Resolve constant pool entries -- we don't load any new classes during this stage
 836   for (int i = 0; i < collect_cld.nof_cld(); i++) {
 837     ClassLoaderData* cld = collect_cld.cld_at(i);
 838     for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) {
 839       if (klass->is_instance_klass()) {
 840         InstanceKlass* ik = InstanceKlass::cast(klass);
 841         AOTConstantPoolResolver::dumptime_resolve_constants(ik, CHECK);
 842         if (CDSConfig::is_dumping_preimage_static_archive()) {
 843           FinalImageRecipes::add_reflection_data_flags(ik, CHECK);
 844         }
 845       }
 846     }
 847   }
 848 
 849   if (CDSConfig::is_dumping_final_static_archive()) {
 850     FinalImageRecipes::apply_recipes(CHECK);
 851   }
 852 }
 853 
 854 void MetaspaceShared::prepare_for_dumping() {
 855   assert(CDSConfig::is_dumping_archive(), "sanity");
 856   CDSConfig::check_unsupported_dumping_module_options();
 857 }
 858 
 859 // Preload classes from a list, populate the shared spaces and dump to a
 860 // file.
 861 void MetaspaceShared::preload_and_dump(TRAPS) {
 862   CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
 863   ResourceMark rm(THREAD);
 864   HandleMark hm(THREAD);
 865 
 866   if (CDSConfig::is_dumping_final_static_archive() && PrintTrainingInfo) {
 867     tty->print_cr("==================== archived_training_data ** before dumping ====================");
 868     TrainingData::print_archived_training_data_on(tty);
 869   }
 870 
 871   StaticArchiveBuilder builder;
 872   preload_and_dump_impl(builder, THREAD);
 873   if (HAS_PENDING_EXCEPTION) {
 874     if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 875       log_error(cds)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
 876                      "%zuM", MaxHeapSize/M);
 877       MetaspaceShared::writing_error();
 878     } else {
 879       log_error(cds)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
 880                      java_lang_String::as_utf8_string(java_lang_Throwable::message(PENDING_EXCEPTION)));
 881       MetaspaceShared::writing_error("Unexpected exception, use -Xlog:cds,exceptions=trace for detail");
 882     }
 883   }
 884 
 885   if (CDSConfig::new_aot_flags_used()) {
 886     if (CDSConfig::is_dumping_preimage_static_archive()) {
 887       // We are in the JVM that runs the training run. Continue execution,
 888       // so that it can finish all clean-up and return the correct exit
 889       // code to the OS.
 890       tty->print_cr("AOTConfiguration recorded: %s", AOTConfiguration);

 891     } else {
 892       // The JLI launcher only recognizes the "old" -Xshare:dump flag.
 893       // When the new -XX:AOTMode=create flag is used, we can't return
 894       // to the JLI launcher, as the launcher will fail when trying to
 895       // run the main class, which is not what we want.
 896       tty->print_cr("AOTCache creation is complete: %s", AOTCache);
 897       vm_exit(0);
 898     }
 899   }
 900 }
 901 
 902 #if INCLUDE_CDS_JAVA_HEAP && defined(_LP64)
 903 void MetaspaceShared::adjust_heap_sizes_for_dumping() {
 904   if (!CDSConfig::is_dumping_heap() || UseCompressedOops) {
 905     return;
 906   }
 907   // CDS heap dumping requires all string oops to have an offset
 908   // from the heap bottom that can be encoded in 32-bit.
 909   julong max_heap_size = (julong)(4 * G);
 910 

 977     }
 978   }
 979 
 980   // Some classes are used at CDS runtime but are not loaded, and therefore archived, at
 981   // dumptime. We can perform dummmy calls to these classes at dumptime to ensure they
 982   // are archived.
 983   exercise_runtime_cds_code(CHECK);
 984 
 985   log_info(cds)("Loading classes to share: done.");
 986 }
 987 
 988 void MetaspaceShared::exercise_runtime_cds_code(TRAPS) {
 989   // Exercise the manifest processing code
 990   const char* dummy = "Manifest-Version: 1.0\n";
 991   CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
 992 
 993   // Exercise FileSystem and URL code
 994   CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
 995 }
 996 
 997 bool MetaspaceShared::is_recording_preimage_static_archive() {
 998   if (CDSConfig::is_dumping_preimage_static_archive()) {
 999       return _preimage_static_archive_dumped == 0;
1000   }
1001   return false;
1002 }
1003 
1004 void MetaspaceShared::preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) {
1005   if (CDSConfig::is_dumping_preimage_static_archive()) {
1006     if (Atomic::cmpxchg(&_preimage_static_archive_dumped, 0, 1) != 0) {
1007       return;
1008     }
1009   }
1010 
1011   if (CDSConfig::is_dumping_classic_static_archive()) {
1012     // We are running with -Xshare:dump
1013     preload_classes(CHECK);
1014 
1015     if (SharedArchiveConfigFile) {
1016       log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
1017       read_extra_data(THREAD, SharedArchiveConfigFile);
1018       log_info(cds)("Reading extra data: done.");
1019     }
1020   }
1021 
1022   if (CDSConfig::is_dumping_preimage_static_archive()) {
1023     log_info(cds)("Reading lambda form invokers from JDK default classlist ...");
1024     char default_classlist[JVM_MAXPATHLEN];
1025     get_default_classlist(default_classlist, sizeof(default_classlist));
1026     struct stat statbuf;
1027     if (os::stat(default_classlist, &statbuf) == 0) {
1028       ClassListParser::parse_classlist(default_classlist,
1029                                        ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
1030     }
1031   }
1032 
1033   if (CDSConfig::is_dumping_final_static_archive()) {
1034     if (ExtraSharedClassListFile) {
1035       log_info(cds)("Loading extra classes from %s ...", ExtraSharedClassListFile);
1036       ClassListParser::parse_classlist(ExtraSharedClassListFile,
1037                                        ClassListParser::_parse_all, CHECK);
1038     }
1039   }
1040 
1041 #if INCLUDE_CDS_JAVA_HEAP
1042   if (CDSConfig::is_dumping_heap()) {
1043     assert(CDSConfig::allow_only_single_java_thread(), "Required");
1044     if (!HeapShared::is_archived_boot_layer_available(THREAD)) {
1045       log_info(cds)("archivedBootLayer not available, disabling full module graph");
1046       CDSConfig::stop_dumping_full_module_graph();
1047     }
1048     // Do this before link_shared_classes(), as the following line may load new classes.
1049     HeapShared::init_for_dumping(CHECK);
1050   }
1051 #endif
1052 
1053   // Rewrite and link classes
1054   log_info(cds)("Rewriting and linking classes ...");
1055 
1056   // Link any classes which got missed. This would happen if we have loaded classes that
1057   // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1058   // fails verification, all other interfaces that were not specified in the classlist but
1059   // are implemented by K are not verified.
1060   link_shared_classes(CHECK);
1061   log_info(cds)("Rewriting and linking classes: done");
1062 
1063   if (CDSConfig::is_dumping_final_static_archive()) {
1064     assert(RecordTraining == false, "must be");
1065     if (CDSConfig::is_dumping_aot_linked_classes()) {
1066       RecordTraining = true;
1067     }
1068   }
1069 
1070   TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
1071 
1072   if (CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
1073     // Lambda form invoker regeneration may load extra classes and execute
1074     // a lot of Java code. We don't want these to be included into the AOT cache.
1075     // This should be done after capturing the training data table, so we won't pollute the
1076     // profile.
1077     SystemDictionaryShared::ignore_new_classes();
1078     LambdaFormInvokers::regenerate_holder_classes(CHECK);
1079   }
1080 
1081 #if INCLUDE_CDS_JAVA_HEAP
1082   if (CDSConfig::is_dumping_heap()) {





1083     ArchiveHeapWriter::init();
1084     if (CDSConfig::is_dumping_full_module_graph()) {
1085       ClassLoaderDataShared::ensure_module_entry_tables_exist();
1086       HeapShared::reset_archived_object_states(CHECK);
1087     }
1088 
1089     if (ArchiveLoaderLookupCache) {
1090       SystemDictionaryShared::create_loader_positive_lookup_cache(CHECK);
1091     }







1092 
1093     if (CDSConfig::is_dumping_invokedynamic()) {
1094       // java.lang.Class::reflectionFactory cannot be archived yet. We set this field
1095       // to null, and it will be initialized again at runtime.
1096       log_debug(cds)("Resetting Class::reflectionFactory");
1097       TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates");
1098       Symbol* method_sig = vmSymbols::void_method_signature();
1099       JavaValue result(T_VOID);
1100       JavaCalls::call_static(&result, vmClasses::Class_klass(),
1101                              method_name, method_sig, CHECK);
1102 
1103       // Perhaps there is a way to avoid hard-coding these names here.
1104       // See discussion in JDK-8342481.
1105     }
1106 
1107     // Do this at the very end, when no Java code will be executed. Otherwise
1108     // some new strings may be added to the intern table.
1109     StringTable::allocate_shared_strings_array(CHECK);
1110   } else {
1111     log_info(cds)("Not dumping heap, reset CDSConfig::_is_using_optimized_module_handling");
1112     CDSConfig::stop_using_optimized_module_handling();
1113   }
1114 #endif
1115 
1116   VM_PopulateDumpSharedSpace op(builder);
1117   VMThread::execute(&op);
1118   FileMapInfo* mapinfo = op.map_info();
1119   ArchiveHeapInfo* heap_info = op.heap_info();
1120 
1121   if (CDSConfig::is_dumping_final_static_archive()) {
1122     RecordTraining = false;
1123     if (StoreCachedCode) {
1124       if (log_is_enabled(Info, cds, jit)) {
1125         CDSAccess::test_heap_access_api();
1126       }
1127 
1128       // We have just created the final image. Let's run the AOT compiler
1129       if (PrintTrainingInfo) {
1130         tty->print_cr("==================== archived_training_data ** after dumping ====================");
1131         TrainingData::print_archived_training_data_on(tty);
1132       }
1133 
1134       CDSConfig::enable_dumping_cached_code();
1135       {
1136         builder.start_cc_region();
1137         Precompiler::compile_cached_code(&builder, CHECK);
1138         // Write the contents to cached code region and close SCCache before packing the region
1139         SCCache::close();
1140         builder.end_cc_region();
1141       }
1142       CDSConfig::disable_dumping_cached_code();
1143     }
1144   }
1145 
1146   bool status = write_static_archive(&builder, mapinfo, heap_info);
1147   if (status && CDSConfig::is_experimental_leyden_workflow() && CDSConfig::is_dumping_preimage_static_archive()) {
1148     fork_and_dump_final_static_archive();
1149   }
1150 
1151   if (!status) {
1152     THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
1153   }
1154 }
1155 
1156 bool MetaspaceShared::write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info) {
1157   // relocate the data so that it can be mapped to MetaspaceShared::requested_base_address()
1158   // without runtime relocation.
1159   builder->relocate_to_requested();
1160 
1161   map_info->open_for_write();
1162   if (!map_info->is_open()) {
1163     return false;
1164   }
1165   builder->write_archive(map_info, heap_info);
1166 
1167   if (AllowArchivingWithJavaAgent) {
1168     log_warning(cds)("This %s was created with AllowArchivingWithJavaAgent. It should be used "
1169             "for testing purposes only and should not be used in a production environment", CDSConfig::type_of_archive_being_loaded());
1170   }
1171   return true;
1172 }
1173 
1174 static void print_java_launcher(outputStream* st) {
1175   st->print("%s%sbin%sjava", Arguments::get_java_home(), os::file_separator(), os::file_separator());
1176 }
1177 
1178 static void print_vm_arguments(outputStream* st) {
1179   const char* cp = Arguments::get_appclasspath();
1180   if (cp != nullptr && strlen(cp) > 0 && strcmp(cp, ".") != 0) {
1181     st->print(" -cp ");  st->print_raw(cp);
1182   }
1183   for (int i = 0; i < Arguments::num_jvm_flags(); i++) {
1184     st->print(" %s", Arguments::jvm_flags_array()[i]);
1185   }
1186   for (int i = 0; i < Arguments::num_jvm_args(); i++) {
1187     st->print(" %s", Arguments::jvm_args_array()[i]);
1188   }
1189 }
1190 
1191 void MetaspaceShared::fork_and_dump_final_static_archive() {
1192   assert(CDSConfig::is_dumping_preimage_static_archive(), "sanity");
1193 
1194   ResourceMark rm;
1195   stringStream ss;
1196   print_java_launcher(&ss);
1197   print_vm_arguments(&ss);
1198   ss.print(" -XX:CDSPreimage=%s", SharedArchiveFile);
1199 
1200   const char* cmd = ss.freeze();
1201   if (CDSManualFinalImage) {
1202     tty->print_cr("-XX:+CDSManualFinalImage is specified");
1203     tty->print_cr("Please manually execute the following command to create the final CDS image:");
1204     tty->print("    "); tty->print_raw_cr(cmd);
1205 
1206     // The following is useful if the dumping was trigger by a script that builds
1207     // a complex command-line.
1208     tty->print_cr("Note: to recreate the preimage only:");
1209     tty->print_cr("    rm -f %s", CacheDataStore);
1210     tty->print("    ");
1211     print_java_launcher(tty);
1212     print_vm_arguments(tty);
1213     if (Arguments::java_command() != nullptr) {
1214       tty->print(" %s", Arguments::java_command());
1215     }
1216     tty->cr();
1217   } else {
1218     // FIXME: space characters are not properly quoated. E.g.,
1219     //      java -Dfoo='a b c' HelloWorld
1220     log_info(cds)("Launching child process to create final CDS image:");
1221     log_info(cds)("    %s", cmd);
1222     int status = os::fork_and_exec(cmd);
1223     if (status != 0) {
1224       log_error(cds)("Child process finished; status = %d", status);
1225       log_error(cds)("To reproduce the error");
1226       ResourceMark rm;
1227       LogStream ls(Log(cds)::error());
1228       ls.print("    "); ls.print_raw_cr(cmd);
1229 
1230       // The following is useful if the dumping was trigger by a script that builds
1231       // a complex command-line.
1232       ls.print_cr("Note: to recreate the preimage only:");
1233       ls.print_cr("    rm -f %s", CacheDataStore);
1234       ls.print("    ");
1235       print_java_launcher(&ls);
1236       print_vm_arguments(&ls);
1237       ls.print(" -XX:+UnlockDiagnosticVMOptions -XX:+CDSManualFinalImage");
1238       if (Arguments::java_command() != nullptr) {
1239         ls.print(" %s", Arguments::java_command());
1240       }
1241       ls.cr();
1242 
1243       vm_direct_exit(status);
1244     } else {
1245       log_info(cds)("Child process finished; status = %d", status);
1246       // On Windows, need WRITE permission to remove the file.
1247       WINDOWS_ONLY(chmod(SharedArchiveFile, _S_IREAD | _S_IWRITE));
1248       status = remove(SharedArchiveFile);
1249       if (status != 0) {
1250         log_error(cds)("Failed to remove CDSPreimage file %s", SharedArchiveFile);
1251       } else {
1252         log_info(cds)("Removed CDSPreimage file %s", SharedArchiveFile);
1253       }
1254     }
1255   }
1256 }
1257 
1258 // Returns true if the class's status has changed.
1259 bool MetaspaceShared::try_link_class(JavaThread* current, InstanceKlass* ik) {
1260   ExceptionMark em(current);
1261   JavaThread* THREAD = current; // For exception macros.
1262   assert(CDSConfig::is_dumping_archive(), "sanity");
1263 
1264   if (ik->is_shared() && !CDSConfig::is_dumping_final_static_archive()) {
1265     assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
1266     return false;
1267   }
1268 
1269   if (ik->is_loaded() && !ik->is_linked() && ik->can_be_verified_at_dumptime() &&
1270       !SystemDictionaryShared::has_class_failed_verification(ik)) {
1271     bool saved = BytecodeVerificationLocal;
1272     if (ik->is_shared_unregistered_class() && ik->class_loader() == nullptr) {
1273       // The verification decision is based on BytecodeVerificationRemote
1274       // for non-system classes. Since we are using the null classloader
1275       // to load non-system classes for customized class loaders during dumping,
1276       // we need to temporarily change BytecodeVerificationLocal to be the same as
1277       // BytecodeVerificationRemote. Note this can cause the parent system

1283     if (HAS_PENDING_EXCEPTION) {
1284       ResourceMark rm(THREAD);
1285       log_warning(cds)("Preload Warning: Verification failed for %s",
1286                     ik->external_name());
1287       CLEAR_PENDING_EXCEPTION;
1288       SystemDictionaryShared::set_class_has_failed_verification(ik);
1289     } else {
1290       assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1291       ik->compute_has_loops_flag_for_methods();
1292     }
1293     BytecodeVerificationLocal = saved;
1294     return true;
1295   } else {
1296     return false;
1297   }
1298 }
1299 
1300 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1301   if (CDSConfig::is_dumping_heap()) {
1302     HeapShared::write_heap(&_heap_info);
1303   } else if (!CDSConfig::is_dumping_preimage_static_archive()) {
1304     CDSConfig::log_reasons_for_not_dumping_heap();
1305   }
1306 }
1307 
1308 void MetaspaceShared::set_shared_metaspace_range(void* base, void *static_top, void* top) {
1309   assert(base <= static_top && static_top <= top, "must be");
1310   _shared_metaspace_static_top = static_top;
1311   MetaspaceObj::set_shared_metaspace_range(base, top);
1312 }
1313 
1314 bool MetaspaceShared::is_shared_dynamic(void* p) {
1315   if ((p < MetaspaceObj::shared_metaspace_top()) &&
1316       (p >= _shared_metaspace_static_top)) {
1317     return true;
1318   } else {
1319     return false;
1320   }
1321 }
1322 
1323 bool MetaspaceShared::is_shared_static(void* p) {

1351 // This function is called when the JVM is unable to write the specified CDS archive due to an
1352 // unrecoverable error.
1353 void MetaspaceShared::unrecoverable_writing_error(const char* message) {
1354   writing_error(message);
1355   vm_direct_exit(1);
1356 }
1357 
1358 // This function is called when the JVM is unable to write the specified CDS archive due to a
1359 // an error. The error will be propagated
1360 void MetaspaceShared::writing_error(const char* message) {
1361   log_error(cds)("An error has occurred while writing the shared archive file.");
1362   if (message != nullptr) {
1363     log_error(cds)("%s", message);
1364   }
1365 }
1366 
1367 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
1368   assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
1369   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1370 
1371   FileMapInfo* static_mapinfo = FileMapInfo::current_info();
1372   FileMapInfo* dynamic_mapinfo = nullptr;
1373 
1374   if (static_mapinfo != nullptr) {
1375     log_info(cds)("Core region alignment: %zu", static_mapinfo->core_region_alignment());
1376     dynamic_mapinfo = open_dynamic_archive();
1377 
1378     // First try to map at the requested address
1379     result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1380     if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1381       // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1382       // by the OS.
1383       log_info(cds)("Try to map archive(s) at an alternative address");
1384       result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1385     }
1386   }
1387 
1388   if (result == MAP_ARCHIVE_SUCCESS) {
1389     bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());
1390     char* cds_base = static_mapinfo->mapped_base();
1391     char* cds_end =  dynamic_mapped ? dynamic_mapinfo->mapped_end() : static_mapinfo->mapped_end();

1395     _relocation_delta = static_mapinfo->relocation_delta();
1396     _requested_base_address = static_mapinfo->requested_base_address();
1397     if (dynamic_mapped) {
1398       // turn AutoCreateSharedArchive off if successfully mapped
1399       AutoCreateSharedArchive = false;
1400     }
1401   } else {
1402     set_shared_metaspace_range(nullptr, nullptr, nullptr);
1403     if (CDSConfig::is_dumping_dynamic_archive()) {
1404       log_warning(cds)("-XX:ArchiveClassesAtExit is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info.");
1405     }
1406     UseSharedSpaces = false;
1407     // The base archive cannot be mapped. We cannot dump the dynamic shared archive.
1408     AutoCreateSharedArchive = false;
1409     CDSConfig::disable_dumping_dynamic_archive();
1410     log_info(cds)("Unable to map shared spaces");
1411     if (PrintSharedArchiveAndExit) {
1412       MetaspaceShared::unrecoverable_loading_error("Unable to use shared archive.");
1413     } else if (RequireSharedSpaces) {
1414       MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1415     } else if (CDSConfig::is_dumping_final_static_archive()) {
1416       assert(CDSPreimage != nullptr, "must be");
1417       log_error(cds)("Unable to map shared spaces for CDSPreimage = %s", CDSPreimage);
1418       MetaspaceShared::unrecoverable_loading_error();
1419     }
1420   }
1421 
1422   // If mapping failed and -XShare:on, the vm should exit
1423   bool has_failed = false;
1424   if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1425     has_failed = true;
1426     delete static_mapinfo;
1427   }
1428   if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1429     has_failed = true;
1430     delete dynamic_mapinfo;
1431   }
1432   if (RequireSharedSpaces && has_failed) {
1433     // static archive mapped but dynamic archive failed
1434       MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1435   }
1436 }
1437 
1438 // This is called very early at VM start up to get the size of the cached_code region
1439 void MetaspaceShared::open_static_archive() {
1440   if (!UseSharedSpaces) {
1441     return;
1442   }
1443   const char* static_archive = CDSConfig::static_archive_path();
1444   assert(static_archive != nullptr, "sanity");
1445   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1446   if (!mapinfo->initialize()) {
1447     delete(mapinfo);
1448   } else {
1449     FileMapRegion* r = mapinfo->region_at(MetaspaceShared::cc);
1450     CDSAccess::set_cached_code_size(r->used_aligned());
1451   }

1452 }
1453 
1454 FileMapInfo* MetaspaceShared::open_dynamic_archive() {
1455   if (CDSConfig::is_dumping_dynamic_archive()) {
1456     return nullptr;
1457   }
1458   const char* dynamic_archive = CDSConfig::dynamic_archive_path();
1459   if (dynamic_archive == nullptr) {
1460     return nullptr;
1461   }
1462 
1463   FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1464   if (!mapinfo->initialize()) {
1465     delete(mapinfo);
1466     if (RequireSharedSpaces) {
1467       MetaspaceShared::unrecoverable_loading_error("Failed to initialize dynamic archive");
1468     }
1469     return nullptr;
1470   }
1471   return mapinfo;

1877       MemoryReserver::release(archive_space_rs);
1878       archive_space_rs = {};
1879     }
1880     if (class_space_rs.is_reserved()) {
1881       log_debug(cds)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
1882       MemoryReserver::release(class_space_rs);
1883       class_space_rs = {};
1884     }
1885   }
1886 }
1887 
1888 static int archive_regions[]     = { MetaspaceShared::rw, MetaspaceShared::ro };
1889 static int archive_regions_count = 2;
1890 
1891 MapArchiveResult MetaspaceShared::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
1892   assert(CDSConfig::is_using_archive(), "must be runtime");
1893   if (mapinfo == nullptr) {
1894     return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
1895   }
1896 
1897   if (!mapinfo->validate_aot_class_linking()) {
1898     return MAP_ARCHIVE_OTHER_FAILURE;
1899   }
1900 
1901   mapinfo->set_is_mapped(false);
1902   if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
1903     log_info(cds)("Unable to map CDS archive -- core_region_alignment() expected: %zu"
1904                   " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment());
1905     return MAP_ARCHIVE_OTHER_FAILURE;
1906   }
1907 
1908   MapArchiveResult result =
1909     mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
1910 
1911   if (result != MAP_ARCHIVE_SUCCESS) {
1912     unmap_archive(mapinfo);
1913     return result;
1914   }
1915 
1916   if (!mapinfo->validate_class_location()) {
1917     unmap_archive(mapinfo);
1918     return MAP_ARCHIVE_OTHER_FAILURE;
1919   }
1920 

1958 };
1959 
1960 // Read the miscellaneous data from the shared file, and
1961 // serialize it out to its various destinations.
1962 
1963 void MetaspaceShared::initialize_shared_spaces() {
1964   FileMapInfo *static_mapinfo = FileMapInfo::current_info();
1965 
1966   // Verify various attributes of the archive, plus initialize the
1967   // shared string/symbol tables.
1968   char* buffer = static_mapinfo->serialized_data();
1969   intptr_t* array = (intptr_t*)buffer;
1970   ReadClosure rc(&array, (intptr_t)SharedBaseAddress);
1971   serialize(&rc);
1972 
1973   // Finish up archived heap initialization. These must be
1974   // done after ReadClosure.
1975   static_mapinfo->patch_heap_embedded_pointers();
1976   ArchiveHeapLoader::finish_initialization();
1977   Universe::load_archived_object_instances();
1978   SCCache::initialize();
1979 
1980   // Close the mapinfo file
1981   static_mapinfo->close();
1982 
1983   static_mapinfo->unmap_region(MetaspaceShared::bm);
1984 
1985   FileMapInfo *dynamic_mapinfo = FileMapInfo::dynamic_info();
1986   if (dynamic_mapinfo != nullptr) {
1987     intptr_t* buffer = (intptr_t*)dynamic_mapinfo->serialized_data();
1988     ReadClosure rc(&buffer, (intptr_t)SharedBaseAddress);
1989     ArchiveBuilder::serialize_dynamic_archivable_items(&rc);
1990     DynamicArchive::setup_array_klasses();
1991     dynamic_mapinfo->close();
1992     dynamic_mapinfo->unmap_region(MetaspaceShared::bm);
1993   }
1994 
1995   LogStreamHandle(Info, cds) lsh;
1996   if (lsh.is_enabled()) {
1997     lsh.print("Using AOT-linked classes: %s (static archive: %s aot-linked classes",
1998               BOOL_TO_STR(CDSConfig::is_using_aot_linked_classes()),

2009     // Read stored LF format lines stored in static archive
2010     LambdaFormInvokers::read_static_archive_invokers();
2011   }
2012 
2013   if (PrintSharedArchiveAndExit) {
2014     // Print archive names
2015     if (dynamic_mapinfo != nullptr) {
2016       tty->print_cr("\n\nBase archive name: %s", CDSConfig::static_archive_path());
2017       tty->print_cr("Base archive version %d", static_mapinfo->version());
2018     } else {
2019       tty->print_cr("Static archive name: %s", static_mapinfo->full_path());
2020       tty->print_cr("Static archive version %d", static_mapinfo->version());
2021     }
2022 
2023     SystemDictionaryShared::print_shared_archive(tty);
2024     if (dynamic_mapinfo != nullptr) {
2025       tty->print_cr("\n\nDynamic archive name: %s", dynamic_mapinfo->full_path());
2026       tty->print_cr("Dynamic archive version %d", dynamic_mapinfo->version());
2027       SystemDictionaryShared::print_shared_archive(tty, false/*dynamic*/);
2028     }
2029     TrainingData::print_archived_training_data_on(tty);
2030 
2031     if (LoadCachedCode) {
2032       tty->print_cr("\n\nCached Code");
2033       SCCache::print_on(tty);
2034     }
2035 
2036     // collect shared symbols and strings
2037     CountSharedSymbols cl;
2038     SymbolTable::shared_symbols_do(&cl);
2039     tty->print_cr("Number of shared symbols: %d", cl.total());
2040     tty->print_cr("Number of shared strings: %zu", StringTable::shared_entry_count());
2041     tty->print_cr("VM version: %s\r\n", static_mapinfo->vm_version());
2042     if (FileMapInfo::current_info() == nullptr || _archive_loading_failed) {
2043       tty->print_cr("archive is invalid");
2044       vm_exit(1);
2045     } else {
2046       tty->print_cr("archive is valid");
2047       vm_exit(0);
2048     }
2049   }
2050 }
2051 
2052 // JVM/TI RedefineClasses() support:
2053 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
2054   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
< prev index next >