< 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/aotClassInitializer.hpp"
  27 #include "cds/aotClassLinker.hpp"
  28 #include "cds/aotClassLocation.hpp"
  29 #include "cds/aotConstantPoolResolver.hpp"
  30 #include "cds/aotLinkedClassBulkLoader.hpp"
  31 #include "cds/aotReferenceObjSupport.hpp"
  32 #include "cds/archiveBuilder.hpp"
  33 #include "cds/archiveHeapLoader.hpp"
  34 #include "cds/archiveHeapWriter.hpp"
  35 #include "cds/cds_globals.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/lambdaProxyClassDictionary.hpp"
  48 #include "cds/metaspaceShared.hpp"
  49 #include "classfile/classLoaderDataGraph.hpp"
  50 #include "classfile/classLoaderDataShared.hpp"
  51 #include "classfile/classLoaderExt.hpp"
  52 #include "classfile/javaClasses.inline.hpp"
  53 #include "classfile/loaderConstraints.hpp"
  54 #include "classfile/modules.hpp"
  55 #include "classfile/placeholders.hpp"
  56 #include "classfile/stringTable.hpp"
  57 #include "classfile/symbolTable.hpp"
  58 #include "classfile/systemDictionary.hpp"
  59 #include "classfile/systemDictionaryShared.hpp"
  60 #include "classfile/vmClasses.hpp"
  61 #include "classfile/vmSymbols.hpp"
  62 #include "code/aotCodeCache.hpp"
  63 #include "code/codeCache.hpp"


  64 #include "gc/shared/gcVMOperations.hpp"
  65 #include "interpreter/bytecodeStream.hpp"
  66 #include "interpreter/bytecodes.hpp"
  67 #include "jvm_io.h"
  68 #include "logging/log.hpp"
  69 #include "logging/logMessage.hpp"
  70 #include "logging/logStream.hpp"
  71 #include "memory/memoryReserver.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/objArrayOop.hpp"
  81 #include "oops/oop.inline.hpp"
  82 #include "oops/oopHandle.hpp"

  83 #include "prims/jvmtiExport.hpp"

  84 #include "runtime/arguments.hpp"
  85 #include "runtime/globals.hpp"
  86 #include "runtime/globals_extension.hpp"
  87 #include "runtime/handles.inline.hpp"
  88 #include "runtime/javaCalls.hpp"
  89 #include "runtime/os.inline.hpp"
  90 #include "runtime/safepointVerifiers.hpp"
  91 #include "runtime/sharedRuntime.hpp"
  92 #include "runtime/vmOperations.hpp"
  93 #include "runtime/vmThread.hpp"
  94 #include "sanitizers/leak.hpp"

  95 #include "utilities/align.hpp"
  96 #include "utilities/bitMap.inline.hpp"
  97 #include "utilities/defaultStream.hpp"
  98 #include "utilities/macros.hpp"
  99 #include "utilities/ostream.hpp"
 100 #include "utilities/resourceHash.hpp"
 101 
 102 #include <sys/stat.h>
 103 
 104 ReservedSpace MetaspaceShared::_symbol_rs;
 105 VirtualSpace MetaspaceShared::_symbol_vs;
 106 bool MetaspaceShared::_archive_loading_failed = false;
 107 bool MetaspaceShared::_remapped_readwrite = false;
 108 void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
 109 intx MetaspaceShared::_relocation_delta;
 110 char* MetaspaceShared::_requested_base_address;
 111 Array<Method*>* MetaspaceShared::_archived_method_handle_intrinsics = nullptr;
 112 bool MetaspaceShared::_use_optimized_module_handling = true;


 113 
 114 // The CDS archive is divided into the following regions:
 115 //     rw  - read-write metadata
 116 //     ro  - read-only metadata and read-only tables
 117 //     hp  - heap region
 118 //     bm  - bitmap for relocating the above 7 regions.
 119 //
 120 // The rw and ro regions are linearly allocated, in the order of rw->ro.
 121 // These regions are aligned with MetaspaceShared::core_region_alignment().
 122 //
 123 // These 2 regions are populated in the following steps:
 124 // [0] All classes are loaded in MetaspaceShared::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.
 132 //

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

















 293   log_info(cds)("Core region alignment: %zu", core_region_alignment());
 294   // The max allowed size for CDS archive. We use this to limit SharedBaseAddress
 295   // to avoid address space wrap around.
 296   size_t cds_max;
 297   const size_t reserve_alignment = core_region_alignment();
 298 
 299 #ifdef _LP64
 300   const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
 301   cds_max = align_down(UnscaledClassSpaceMax, reserve_alignment);
 302 #else
 303   // We don't support archives larger than 256MB on 32-bit due to limited
 304   //  virtual address space.
 305   cds_max = align_down(256*M, reserve_alignment);
 306 #endif
 307 
 308   _requested_base_address = compute_shared_base(cds_max);
 309   SharedBaseAddress = (size_t)_requested_base_address;
 310 
 311   size_t symbol_rs_size = LP64_ONLY(3 * G) NOT_LP64(128 * M);
 312   _symbol_rs = MemoryReserver::reserve(symbol_rs_size,

 465   // in archived Metadata objects.
 466   CppVtables::serialize(soc);
 467   soc->do_tag(--tag);
 468 
 469   // Dump/restore miscellaneous metadata.
 470   JavaClasses::serialize_offsets(soc);
 471   Universe::serialize(soc);
 472   soc->do_tag(--tag);
 473 
 474   // Dump/restore references to commonly used names and signatures.
 475   vmSymbols::serialize(soc);
 476   soc->do_tag(--tag);
 477 
 478   // Dump/restore the symbol/string/subgraph_info tables
 479   SymbolTable::serialize_shared_table_header(soc);
 480   StringTable::serialize_shared_table_header(soc);
 481   HeapShared::serialize_tables(soc);
 482   SystemDictionaryShared::serialize_dictionary_headers(soc);
 483   AOTLinkedClassBulkLoader::serialize(soc, true);
 484   FinalImageRecipes::serialize(soc);

 485   InstanceMirrorKlass::serialize_offsets(soc);
 486 
 487   // Dump/restore well known classes (pointers)
 488   SystemDictionaryShared::serialize_vm_classes(soc);
 489   soc->do_tag(--tag);
 490 
 491   CDS_JAVA_HEAP_ONLY(ClassLoaderDataShared::serialize(soc);)
 492   soc->do_ptr((void**)&_archived_method_handle_intrinsics);
 493 
 494   LambdaFormInvokers::serialize(soc);
 495   AdapterHandlerLibrary::serialize_shared_table_header(soc);
 496 
 497   soc->do_tag(666);
 498 }
 499 
 500 static void rewrite_nofast_bytecode(const methodHandle& method) {
 501   BytecodeStream bcs(method);
 502   while (!bcs.is_last_bytecode()) {
 503     Bytecodes::Code opcode = bcs.next();
 504     switch (opcode) {

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

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



 610   MetaspaceShared::write_method_handle_intrinsics();
 611 
 612   // Write lambform lines into archive
 613   LambdaFormInvokers::dump_static_archive_invokers();
 614 
 615   if (AOTCodeCache::is_dumping_adapters()) {
 616     AdapterHandlerLibrary::dump_aot_adapter_table();
 617   }
 618 
 619   // Write the other data to the output array.
 620   DumpRegion* ro_region = ArchiveBuilder::current()->ro_region();
 621   char* start = ro_region->top();
 622   WriteClosure wc(ro_region);
 623   MetaspaceShared::serialize(&wc);
 624 
 625   return start;
 626 }
 627 
 628 void VM_PopulateDumpSharedSpace::doit() {
 629   if (!CDSConfig::is_dumping_final_static_archive()) {
 630     guarantee(!CDSConfig::is_using_archive(), "We should not be using an archive when we dump");
 631   }
 632 
 633   DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm);
 634 
 635   _pending_method_handle_intrinsics = new (mtClassShared) GrowableArray<Method*>(256, mtClassShared);

 664   _builder.dump_rw_metadata();
 665   _builder.dump_ro_metadata();
 666   _builder.relocate_metaspaceobj_embedded_pointers();
 667 
 668   log_info(cds)("Make classes shareable");
 669   _builder.make_klasses_shareable();
 670   MetaspaceShared::make_method_handle_intrinsics_shareable();
 671 
 672   dump_java_heap_objects();
 673   dump_shared_symbol_table(_builder.symbols());
 674 
 675   char* early_serialized_data = dump_early_read_only_tables();
 676   AOTClassLocationConfig* cl_config;
 677   char* serialized_data = dump_read_only_tables(cl_config);
 678 
 679   if (CDSConfig::is_dumping_lambdas_in_legacy_mode()) {
 680     log_info(cds)("Adjust lambda proxy class dictionary");
 681     LambdaProxyClassDictionary::adjust_dumptime_table();
 682   }
 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   if (CDSConfig::is_dumping_final_static_archive()) {
 690     FileMapInfo::free_current_info(); // FIXME: should not free current info
 691   }
 692   const char* static_archive = CDSConfig::output_archive_path();
 693   assert(static_archive != nullptr, "sanity");
 694   _map_info = new FileMapInfo(static_archive, true);
 695   _map_info->populate_header(MetaspaceShared::core_region_alignment());
 696   _map_info->set_early_serialized_data(early_serialized_data);
 697   _map_info->set_serialized_data(serialized_data);
 698   _map_info->set_cloned_vtables(CppVtables::vtables_serialized_base());
 699   _map_info->header()->set_class_location_config(cl_config);
 700 }
 701 
 702 class CollectClassesForLinking : public KlassClosure {
 703   GrowableArray<OopHandle> _mirrors;

 718       _mirrors.at(i).release(Universe::vm_global());
 719     }
 720   }
 721 
 722   void do_cld(ClassLoaderData* cld) {
 723     assert(cld->is_alive(), "must be");
 724   }
 725 
 726   void do_klass(Klass* k) {
 727     if (k->is_instance_klass()) {
 728       _mirrors.append(OopHandle(Universe::vm_global(), k->java_mirror()));
 729     }
 730   }
 731 
 732   const GrowableArray<OopHandle>* mirrors() const { return &_mirrors; }
 733 };
 734 
 735 // Check if we can eagerly link this class at dump time, so we can avoid the
 736 // runtime linking overhead (especially verification)
 737 bool MetaspaceShared::may_be_eagerly_linked(InstanceKlass* ik) {



 738   if (!ik->can_be_verified_at_dumptime()) {
 739     // For old classes, try to leave them in the unlinked state, so
 740     // we can still store them in the archive. They must be
 741     // linked/verified at runtime.
 742     return false;
 743   }

 744   if (CDSConfig::is_dumping_dynamic_archive() && ik->is_shared_unregistered_class()) {
 745     // Linking of unregistered classes at this stage may cause more
 746     // classes to be resolved, resulting in calls to ClassLoader.loadClass()
 747     // that may not be expected by custom class loaders.
 748     //
 749     // It's OK to do this for the built-in loaders as we know they can
 750     // tolerate this.
 751     return false;
 752   }
 753   return true;
 754 }
 755 
 756 void MetaspaceShared::link_shared_classes(TRAPS) {
 757   AOTClassLinker::initialize();
 758   AOTClassInitializer::init_test_class(CHECK);
 759 
 760   while (true) {
 761     ResourceMark rm(THREAD);
 762     CollectClassesForLinking collect_classes;
 763     bool has_linked = false;
 764     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 765     for (int i = 0; i < mirrors->length(); i++) {
 766       OopHandle mirror = mirrors->at(i);
 767       InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
 768       if (may_be_eagerly_linked(ik)) {
 769         has_linked |= try_link_class(THREAD, ik);
 770       }



 771     }
 772 
 773     if (!has_linked) {
 774       break;
 775     }
 776     // Class linking includes verification which may load more classes.
 777     // Keep scanning until we have linked no more classes.
 778   }
 779 
 780   // Resolve constant pool entries -- we don't load any new classes during this stage
 781   {
 782     ResourceMark rm(THREAD);
 783     CollectClassesForLinking collect_classes;
 784     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 785     for (int i = 0; i < mirrors->length(); i++) {
 786       OopHandle mirror = mirrors->at(i);
 787       InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
 788       AOTConstantPoolResolver::dumptime_resolve_constants(ik, CHECK);



 789     }
 790   }
 791 
 792   if (CDSConfig::is_dumping_final_static_archive()) {
 793     FinalImageRecipes::apply_recipes(CHECK);
 794   }
 795 }
 796 
 797 // Preload classes from a list, populate the shared spaces and dump to a
 798 // file.
 799 void MetaspaceShared::preload_and_dump(TRAPS) {
 800   CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
 801   ResourceMark rm(THREAD);







 802   StaticArchiveBuilder builder;
 803   preload_and_dump_impl(builder, THREAD);
 804   if (HAS_PENDING_EXCEPTION) {
 805     if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 806       log_error(cds)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
 807                      "%zuM", MaxHeapSize/M);
 808       MetaspaceShared::writing_error();
 809     } else {
 810       log_error(cds)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
 811                      java_lang_String::as_utf8_string(java_lang_Throwable::message(PENDING_EXCEPTION)));
 812       MetaspaceShared::writing_error("Unexpected exception, use -Xlog:cds,exceptions=trace for detail");
 813     }



 814   }
 815 
 816   if (CDSConfig::new_aot_flags_used()) {
 817     if (CDSConfig::is_dumping_preimage_static_archive()) {
 818       // We are in the JVM that runs the training run. Continue execution,
 819       // so that it can finish all clean-up and return the correct exit
 820       // code to the OS.
 821       tty->print_cr("AOTConfiguration recorded: %s", AOTConfiguration);
 822     } else {
 823       // The JLI launcher only recognizes the "old" -Xshare:dump flag.
 824       // When the new -XX:AOTMode=create flag is used, we can't return
 825       // to the JLI launcher, as the launcher will fail when trying to
 826       // run the main class, which is not what we want.
 827       struct stat st;
 828       if (os::stat(AOTCache, &st) != 0) {
 829         tty->print_cr("AOTCache creation failed: %s", AOTCache);
 830         vm_exit(0);
 831       } else {
 832         tty->print_cr("AOTCache creation is complete: %s " INT64_FORMAT " bytes", AOTCache, (int64_t)(st.st_size));
 833         vm_exit(0);
 834       }
 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) {

 893     }
 894   }
 895 
 896   // Some classes are used at CDS runtime but are not loaded, and therefore archived, at
 897   // dumptime. We can perform dummmy calls to these classes at dumptime to ensure they
 898   // are archived.
 899   exercise_runtime_cds_code(CHECK);
 900 
 901   log_info(cds)("Loading classes to share: done.");
 902 }
 903 
 904 void MetaspaceShared::exercise_runtime_cds_code(TRAPS) {
 905   // Exercise the manifest processing code
 906   const char* dummy = "Manifest-Version: 1.0\n";
 907   CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
 908 
 909   // Exercise FileSystem and URL code
 910   CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
 911 }
 912 


















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







 914   if (CDSConfig::is_dumping_classic_static_archive()) {
 915     // We are running with -Xshare:dump
 916     preload_classes(CHECK);
 917 
 918     if (SharedArchiveConfigFile) {
 919       log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
 920       read_extra_data(THREAD, SharedArchiveConfigFile);
 921       log_info(cds)("Reading extra data: done.");
 922     }
 923   }
 924 
 925   if (CDSConfig::is_dumping_preimage_static_archive()) {
 926     log_info(cds)("Reading lambda form invokers from JDK default classlist ...");
 927     char default_classlist[JVM_MAXPATHLEN];
 928     get_default_classlist(default_classlist, JVM_MAXPATHLEN);
 929     struct stat statbuf;
 930     if (os::stat(default_classlist, &statbuf) == 0) {
 931       ClassListParser::parse_classlist(default_classlist,
 932                                        ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
 933     }

 946 #endif
 947 
 948   if (CDSConfig::is_dumping_final_static_archive()) {
 949     if (ExtraSharedClassListFile) {
 950       log_info(cds)("Loading extra classes from %s ...", ExtraSharedClassListFile);
 951       ClassListParser::parse_classlist(ExtraSharedClassListFile,
 952                                        ClassListParser::_parse_all, CHECK);
 953     }
 954   }
 955 
 956   // Rewrite and link classes
 957   log_info(cds)("Rewriting and linking classes ...");
 958 
 959   // Link any classes which got missed. This would happen if we have loaded classes that
 960   // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
 961   // fails verification, all other interfaces that were not specified in the classlist but
 962   // are implemented by K are not verified.
 963   link_shared_classes(CHECK);
 964   log_info(cds)("Rewriting and linking classes: done");
 965 


 966   if (CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
 967     LambdaFormInvokers::regenerate_holder_classes(CHECK);
 968   }
 969 
 970 #if INCLUDE_CDS_JAVA_HEAP
 971   if (CDSConfig::is_dumping_heap()) {
 972     ArchiveHeapWriter::init();
 973 
 974     if (CDSConfig::is_dumping_full_module_graph()) {
 975       ClassLoaderDataShared::ensure_module_entry_tables_exist();
 976       HeapShared::reset_archived_object_states(CHECK);
 977     }
 978 




 979     AOTReferenceObjSupport::initialize(CHECK);
 980     AOTReferenceObjSupport::stabilize_cached_reference_objects(CHECK);
 981 
 982     if (CDSConfig::is_initing_classes_at_dump_time()) {
 983       // java.lang.Class::reflectionFactory cannot be archived yet. We set this field
 984       // to null, and it will be initialized again at runtime.
 985       log_debug(cds)("Resetting Class::reflectionFactory");
 986       TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates");
 987       Symbol* method_sig = vmSymbols::void_method_signature();
 988       JavaValue result(T_VOID);
 989       JavaCalls::call_static(&result, vmClasses::Class_klass(),
 990                              method_name, method_sig, CHECK);
 991 
 992       // Perhaps there is a way to avoid hard-coding these names here.
 993       // See discussion in JDK-8342481.
 994     }
 995 
 996     // Do this at the very end, when no Java code will be executed. Otherwise
 997     // some new strings may be added to the intern table.
 998     StringTable::allocate_shared_strings_array(CHECK);
 999   } else {
1000     log_info(cds)("Not dumping heap, reset CDSConfig::_is_using_optimized_module_handling");
1001     CDSConfig::stop_using_optimized_module_handling();
1002   }
1003 #endif
1004 
1005   VM_PopulateDumpSharedSpace op(builder);
1006   VMThread::execute(&op);


1007 
1008   if (AOTCodeCache::is_on_for_dump() && CDSConfig::is_dumping_final_static_archive()) {
1009     CDSConfig::enable_dumping_aot_code();
1010     {
1011       builder.start_ac_region();
1012       // Write the contents to AOT code region and close AOTCodeCache before packing the region
1013       AOTCodeCache::close();
1014       builder.end_ac_region();



























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































































































































































































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

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

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

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




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

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




1220   const char* static_archive = CDSConfig::input_static_archive_path();
1221   assert(static_archive != nullptr, "sanity");
1222   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1223   if (!mapinfo->open_as_input()) {
1224     delete(mapinfo);
1225     return nullptr;


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

1693       MemoryReserver::release(archive_space_rs);
1694       archive_space_rs = {};
1695     }
1696     if (class_space_rs.is_reserved()) {
1697       log_debug(cds)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
1698       MemoryReserver::release(class_space_rs);
1699       class_space_rs = {};
1700     }
1701   }
1702 }
1703 
1704 static int archive_regions[]     = { MetaspaceShared::rw, MetaspaceShared::ro };
1705 static int archive_regions_count = 2;
1706 
1707 MapArchiveResult MetaspaceShared::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
1708   assert(CDSConfig::is_using_archive(), "must be runtime");
1709   if (mapinfo == nullptr) {
1710     return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
1711   }
1712 




1713   mapinfo->set_is_mapped(false);
1714   if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
1715     log_info(cds)("Unable to map CDS archive -- core_region_alignment() expected: %zu"
1716                   " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment());
1717     return MAP_ARCHIVE_OTHER_FAILURE;
1718   }
1719 
1720   MapArchiveResult result =
1721     mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
1722 
1723   if (result != MAP_ARCHIVE_SUCCESS) {
1724     unmap_archive(mapinfo);
1725     return result;
1726   }
1727 
1728   if (!mapinfo->validate_class_location()) {
1729     unmap_archive(mapinfo);
1730     return MAP_ARCHIVE_OTHER_FAILURE;
1731   }
1732 

1821     // Read stored LF format lines stored in static archive
1822     LambdaFormInvokers::read_static_archive_invokers();
1823   }
1824 
1825   if (PrintSharedArchiveAndExit) {
1826     // Print archive names
1827     if (dynamic_mapinfo != nullptr) {
1828       tty->print_cr("\n\nBase archive name: %s", CDSConfig::input_static_archive_path());
1829       tty->print_cr("Base archive version %d", static_mapinfo->version());
1830     } else {
1831       tty->print_cr("Static archive name: %s", static_mapinfo->full_path());
1832       tty->print_cr("Static archive version %d", static_mapinfo->version());
1833     }
1834 
1835     SystemDictionaryShared::print_shared_archive(tty);
1836     if (dynamic_mapinfo != nullptr) {
1837       tty->print_cr("\n\nDynamic archive name: %s", dynamic_mapinfo->full_path());
1838       tty->print_cr("Dynamic archive version %d", dynamic_mapinfo->version());
1839       SystemDictionaryShared::print_shared_archive(tty, false/*dynamic*/);
1840     }

1841 
1842     if (AOTCodeCache::is_on_for_use()) {
1843       tty->print_cr("\n\nAOT Code");
1844       AOTCodeCache::print_on(tty);
1845     }
1846 
1847     // collect shared symbols and strings
1848     CountSharedSymbols cl;
1849     SymbolTable::shared_symbols_do(&cl);
1850     tty->print_cr("Number of shared symbols: %d", cl.total());
1851     tty->print_cr("Number of shared strings: %zu", StringTable::shared_entry_count());
1852     tty->print_cr("VM version: %s\r\n", static_mapinfo->vm_version());
1853     if (FileMapInfo::current_info() == nullptr || _archive_loading_failed) {
1854       tty->print_cr("archive is invalid");
1855       vm_exit(1);
1856     } else {
1857       tty->print_cr("archive is valid");
1858       vm_exit(0);
1859     }
1860   }

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

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

 493   // in archived Metadata objects.
 494   CppVtables::serialize(soc);
 495   soc->do_tag(--tag);
 496 
 497   // Dump/restore miscellaneous metadata.
 498   JavaClasses::serialize_offsets(soc);
 499   Universe::serialize(soc);
 500   soc->do_tag(--tag);
 501 
 502   // Dump/restore references to commonly used names and signatures.
 503   vmSymbols::serialize(soc);
 504   soc->do_tag(--tag);
 505 
 506   // Dump/restore the symbol/string/subgraph_info tables
 507   SymbolTable::serialize_shared_table_header(soc);
 508   StringTable::serialize_shared_table_header(soc);
 509   HeapShared::serialize_tables(soc);
 510   SystemDictionaryShared::serialize_dictionary_headers(soc);
 511   AOTLinkedClassBulkLoader::serialize(soc, true);
 512   FinalImageRecipes::serialize(soc);
 513   TrainingData::serialize(soc);
 514   InstanceMirrorKlass::serialize_offsets(soc);
 515 
 516   // Dump/restore well known classes (pointers)
 517   SystemDictionaryShared::serialize_vm_classes(soc);
 518   soc->do_tag(--tag);
 519 
 520   CDS_JAVA_HEAP_ONLY(ClassLoaderDataShared::serialize(soc);)
 521   soc->do_ptr((void**)&_archived_method_handle_intrinsics);
 522 
 523   LambdaFormInvokers::serialize(soc);
 524   AdapterHandlerLibrary::serialize_shared_table_header(soc);
 525 
 526   soc->do_tag(666);
 527 }
 528 
 529 static void rewrite_nofast_bytecode(const methodHandle& method) {
 530   BytecodeStream bcs(method);
 531   while (!bcs.is_last_bytecode()) {
 532     Bytecodes::Code opcode = bcs.next();
 533     switch (opcode) {

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

 697   _builder.dump_rw_metadata();
 698   _builder.dump_ro_metadata();
 699   _builder.relocate_metaspaceobj_embedded_pointers();
 700 
 701   log_info(cds)("Make classes shareable");
 702   _builder.make_klasses_shareable();
 703   MetaspaceShared::make_method_handle_intrinsics_shareable();
 704 
 705   dump_java_heap_objects();
 706   dump_shared_symbol_table(_builder.symbols());
 707 
 708   char* early_serialized_data = dump_early_read_only_tables();
 709   AOTClassLocationConfig* cl_config;
 710   char* serialized_data = dump_read_only_tables(cl_config);
 711 
 712   if (CDSConfig::is_dumping_lambdas_in_legacy_mode()) {
 713     log_info(cds)("Adjust lambda proxy class dictionary");
 714     LambdaProxyClassDictionary::adjust_dumptime_table();
 715   }
 716 
 717   log_info(cds)("Make training data shareable");
 718   _builder.make_training_data_shareable();
 719 
 720   // The vtable clones contain addresses of the current process.
 721   // We don't want to write these addresses into the archive.
 722   CppVtables::zero_archived_vtables();
 723 
 724   // Write the archive file
 725   if (CDSConfig::is_dumping_final_static_archive()) {
 726     FileMapInfo::free_current_info(); // FIXME: should not free current info
 727   }
 728   const char* static_archive = CDSConfig::output_archive_path();
 729   assert(static_archive != nullptr, "sanity");
 730   _map_info = new FileMapInfo(static_archive, true);
 731   _map_info->populate_header(MetaspaceShared::core_region_alignment());
 732   _map_info->set_early_serialized_data(early_serialized_data);
 733   _map_info->set_serialized_data(serialized_data);
 734   _map_info->set_cloned_vtables(CppVtables::vtables_serialized_base());
 735   _map_info->header()->set_class_location_config(cl_config);
 736 }
 737 
 738 class CollectClassesForLinking : public KlassClosure {
 739   GrowableArray<OopHandle> _mirrors;

 754       _mirrors.at(i).release(Universe::vm_global());
 755     }
 756   }
 757 
 758   void do_cld(ClassLoaderData* cld) {
 759     assert(cld->is_alive(), "must be");
 760   }
 761 
 762   void do_klass(Klass* k) {
 763     if (k->is_instance_klass()) {
 764       _mirrors.append(OopHandle(Universe::vm_global(), k->java_mirror()));
 765     }
 766   }
 767 
 768   const GrowableArray<OopHandle>* mirrors() const { return &_mirrors; }
 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 void MetaspaceShared::link_shared_classes(TRAPS) {
 797   AOTClassLinker::initialize();
 798   AOTClassInitializer::init_test_class(CHECK);
 799 
 800   while (true) {
 801     ResourceMark rm(THREAD);
 802     CollectClassesForLinking collect_classes;
 803     bool has_linked = false;
 804     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 805     for (int i = 0; i < mirrors->length(); i++) {
 806       OopHandle mirror = mirrors->at(i);
 807       InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
 808       if (may_be_eagerly_linked(ik)) {
 809         has_linked |= try_link_class(THREAD, ik);
 810       }
 811       if (CDSConfig::is_dumping_heap() && ik->is_linked() && !ik->is_initialized()) {
 812         AOTClassInitializer::maybe_preinit_class(ik, CHECK);
 813       }
 814     }
 815 
 816     if (!has_linked) {
 817       break;
 818     }
 819     // Class linking includes verification which may load more classes.
 820     // Keep scanning until we have linked no more classes.
 821   }
 822 
 823   // Resolve constant pool entries -- we don't load any new classes during this stage
 824   {
 825     ResourceMark rm(THREAD);
 826     CollectClassesForLinking collect_classes;
 827     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 828     for (int i = 0; i < mirrors->length(); i++) {
 829       OopHandle mirror = mirrors->at(i);
 830       InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
 831       AOTConstantPoolResolver::dumptime_resolve_constants(ik, CHECK);
 832       if (CDSConfig::is_dumping_preimage_static_archive()) {
 833         FinalImageRecipes::add_reflection_data_flags(ik, CHECK);
 834       }
 835     }
 836   }
 837 
 838   if (CDSConfig::is_dumping_final_static_archive()) {
 839     FinalImageRecipes::apply_recipes(CHECK);
 840   }
 841 }
 842 
 843 // Preload classes from a list, populate the shared spaces and dump to a
 844 // file.
 845 void MetaspaceShared::preload_and_dump(TRAPS) {
 846   CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
 847   ResourceMark rm(THREAD);
 848   HandleMark hm(THREAD);
 849 
 850   if (CDSConfig::is_dumping_final_static_archive() && AOTPrintTrainingInfo) {
 851     tty->print_cr("==================== archived_training_data ** before dumping ====================");
 852     TrainingData::print_archived_training_data_on(tty);
 853   }
 854 
 855   StaticArchiveBuilder builder;
 856   preload_and_dump_impl(builder, THREAD);
 857   if (HAS_PENDING_EXCEPTION) {
 858     if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 859       log_error(cds)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
 860                      "%zuM", MaxHeapSize/M);
 861       MetaspaceShared::writing_error();
 862     } else {
 863       log_error(cds)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
 864                      java_lang_String::as_utf8_string(java_lang_Throwable::message(PENDING_EXCEPTION)));
 865       MetaspaceShared::writing_error("Unexpected exception, use -Xlog:cds,exceptions=trace for detail");
 866     }
 867     if (CDSConfig::is_experimental_leyden_workflow()) {
 868       vm_exit(1);
 869     }
 870   }
 871 
 872   if (CDSConfig::new_aot_flags_used()) {
 873     if (CDSConfig::is_dumping_preimage_static_archive()) {
 874       // We are in the JVM that runs the training run. Continue execution,
 875       // so that it can finish all clean-up and return the correct exit
 876       // code to the OS.

 877     } else {
 878       // The JLI launcher only recognizes the "old" -Xshare:dump flag.
 879       // When the new -XX:AOTMode=create flag is used, we can't return
 880       // to the JLI launcher, as the launcher will fail when trying to
 881       // run the main class, which is not what we want.
 882       struct stat st;
 883       if (os::stat(AOTCache, &st) != 0) {
 884         tty->print_cr("AOTCache creation failed: %s", AOTCache);
 885         vm_exit(0);
 886       } else {
 887         tty->print_cr("AOTCache creation is complete: %s " INT64_FORMAT " bytes", AOTCache, (int64_t)(st.st_size));
 888         vm_exit(0);
 889       }
 890     }
 891   }
 892 }
 893 
 894 #if INCLUDE_CDS_JAVA_HEAP && defined(_LP64)
 895 void MetaspaceShared::adjust_heap_sizes_for_dumping() {
 896   if (!CDSConfig::is_dumping_heap() || UseCompressedOops) {

 948     }
 949   }
 950 
 951   // Some classes are used at CDS runtime but are not loaded, and therefore archived, at
 952   // dumptime. We can perform dummmy calls to these classes at dumptime to ensure they
 953   // are archived.
 954   exercise_runtime_cds_code(CHECK);
 955 
 956   log_info(cds)("Loading classes to share: done.");
 957 }
 958 
 959 void MetaspaceShared::exercise_runtime_cds_code(TRAPS) {
 960   // Exercise the manifest processing code
 961   const char* dummy = "Manifest-Version: 1.0\n";
 962   CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
 963 
 964   // Exercise FileSystem and URL code
 965   CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
 966 }
 967 
 968 bool MetaspaceShared::is_recording_preimage_static_archive() {
 969   if (CDSConfig::is_dumping_preimage_static_archive()) {
 970       return _preimage_static_archive_dumped == 0;
 971   }
 972   return false;
 973 }
 974 
 975 jlong MetaspaceShared::get_preimage_static_archive_recording_duration() {
 976   if (CDSConfig::is_dumping_preimage_static_archive()) {
 977     if (_preimage_static_archive_recording_duration == 0) {
 978       // The recording has not yet finished so return the current elapsed time.
 979       return Management::ticks_to_ms(os::elapsed_counter());
 980     }
 981     return _preimage_static_archive_recording_duration;
 982   }
 983   return 0;
 984 }
 985 
 986 void MetaspaceShared::preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) {
 987   if (CDSConfig::is_dumping_preimage_static_archive()) {
 988     if (Atomic::cmpxchg(&_preimage_static_archive_dumped, 0, 1) != 0) {
 989       return;
 990     }
 991     _preimage_static_archive_recording_duration = Management::ticks_to_ms(os::elapsed_counter());
 992   }
 993 
 994   if (CDSConfig::is_dumping_classic_static_archive()) {
 995     // We are running with -Xshare:dump
 996     preload_classes(CHECK);
 997 
 998     if (SharedArchiveConfigFile) {
 999       log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
1000       read_extra_data(THREAD, SharedArchiveConfigFile);
1001       log_info(cds)("Reading extra data: done.");
1002     }
1003   }
1004 
1005   if (CDSConfig::is_dumping_preimage_static_archive()) {
1006     log_info(cds)("Reading lambda form invokers from JDK default classlist ...");
1007     char default_classlist[JVM_MAXPATHLEN];
1008     get_default_classlist(default_classlist, JVM_MAXPATHLEN);
1009     struct stat statbuf;
1010     if (os::stat(default_classlist, &statbuf) == 0) {
1011       ClassListParser::parse_classlist(default_classlist,
1012                                        ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
1013     }

1026 #endif
1027 
1028   if (CDSConfig::is_dumping_final_static_archive()) {
1029     if (ExtraSharedClassListFile) {
1030       log_info(cds)("Loading extra classes from %s ...", ExtraSharedClassListFile);
1031       ClassListParser::parse_classlist(ExtraSharedClassListFile,
1032                                        ClassListParser::_parse_all, CHECK);
1033     }
1034   }
1035 
1036   // Rewrite and link classes
1037   log_info(cds)("Rewriting and linking classes ...");
1038 
1039   // Link any classes which got missed. This would happen if we have loaded classes that
1040   // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1041   // fails verification, all other interfaces that were not specified in the classlist but
1042   // are implemented by K are not verified.
1043   link_shared_classes(CHECK);
1044   log_info(cds)("Rewriting and linking classes: done");
1045 
1046   TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
1047 
1048   if (CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
1049     LambdaFormInvokers::regenerate_holder_classes(CHECK);
1050   }
1051 
1052 #if INCLUDE_CDS_JAVA_HEAP
1053   if (CDSConfig::is_dumping_heap()) {
1054     ArchiveHeapWriter::init();
1055 
1056     if (CDSConfig::is_dumping_full_module_graph()) {
1057       ClassLoaderDataShared::ensure_module_entry_tables_exist();
1058       HeapShared::reset_archived_object_states(CHECK);
1059     }
1060 
1061     if (ArchiveLoaderLookupCache) {
1062       SystemDictionaryShared::create_loader_positive_lookup_cache(CHECK);
1063     }
1064 
1065     AOTReferenceObjSupport::initialize(CHECK);
1066     AOTReferenceObjSupport::stabilize_cached_reference_objects(CHECK);
1067 
1068     if (CDSConfig::is_initing_classes_at_dump_time()) {
1069       // java.lang.Class::reflectionFactory cannot be archived yet. We set this field
1070       // to null, and it will be initialized again at runtime.
1071       log_debug(cds)("Resetting Class::reflectionFactory");
1072       TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates");
1073       Symbol* method_sig = vmSymbols::void_method_signature();
1074       JavaValue result(T_VOID);
1075       JavaCalls::call_static(&result, vmClasses::Class_klass(),
1076                              method_name, method_sig, CHECK);
1077 
1078       // Perhaps there is a way to avoid hard-coding these names here.
1079       // See discussion in JDK-8342481.
1080     }
1081 
1082     // Do this at the very end, when no Java code will be executed. Otherwise
1083     // some new strings may be added to the intern table.
1084     StringTable::allocate_shared_strings_array(CHECK);
1085   } else {
1086     log_info(cds)("Not dumping heap, reset CDSConfig::_is_using_optimized_module_handling");
1087     CDSConfig::stop_using_optimized_module_handling();
1088   }
1089 #endif
1090 
1091   VM_PopulateDumpSharedSpace op(builder);
1092   VMThread::execute(&op);
1093   FileMapInfo* mapinfo = op.map_info();
1094   ArchiveHeapInfo* heap_info = op.heap_info();
1095 
1096   if (CDSConfig::is_dumping_final_static_archive()) {
1097     if (AOTCodeCache::is_caching_enabled()) {
1098       if (log_is_enabled(Info, cds, jit)) {
1099         AOTCacheAccess::test_heap_access_api();
1100       }
1101 
1102       // We have just created the final image. Let's run the AOT compiler
1103       if (AOTPrintTrainingInfo) {
1104         tty->print_cr("==================== archived_training_data ** after dumping ====================");
1105         TrainingData::print_archived_training_data_on(tty);
1106       }
1107 
1108       CDSConfig::enable_dumping_aot_code();
1109       {
1110         builder.start_ac_region();
1111         Precompiler::compile_cached_code(&builder, CHECK);
1112         // Write the contents to aot code region and close AOTCodeCache before packing the region
1113         AOTCodeCache::close();
1114         builder.end_ac_region();
1115       }
1116       CDSConfig::disable_dumping_aot_code();
1117     }
1118   }
1119 
1120   bool status = write_static_archive(&builder, mapinfo, heap_info);
1121   if (status && CDSConfig::is_dumping_preimage_static_archive()) {
1122     if (CDSConfig::is_experimental_leyden_workflow()) {
1123       fork_and_dump_final_static_archive_experimental_leyden_workflow(CHECK);
1124     } else {
1125       tty->print_cr("%s AOTConfiguration recorded: %s",
1126                     CDSConfig::has_temp_aot_config_file() ? "Temporary" : "", AOTConfiguration);
1127       if (CDSConfig::is_one_step_training()) {
1128         fork_and_dump_final_static_archive(CHECK);
1129       }
1130     }

1131   }
1132 
1133   if (!status) {
1134     THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
1135   }
1136 }
1137 
1138 bool MetaspaceShared::write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info) {
1139   // relocate the data so that it can be mapped to MetaspaceShared::requested_base_address()
1140   // without runtime relocation.
1141   builder->relocate_to_requested();
1142 
1143   map_info->open_as_output();
1144   if (!map_info->is_open()) {
1145     return false;
1146   }
1147   builder->write_archive(map_info, heap_info);
1148 
1149   if (AllowArchivingWithJavaAgent) {
1150     log_warning(cds)("This %s was created with AllowArchivingWithJavaAgent. It should be used "
1151             "for testing purposes only and should not be used in a production environment", CDSConfig::type_of_archive_being_loaded());
1152   }
1153   return true;
1154 }
1155 
1156 static void print_java_launcher(outputStream* st) {
1157   st->print("%s%sbin%sjava", Arguments::get_java_home(), os::file_separator(), os::file_separator());
1158 }
1159 
1160 static void append_args(GrowableArray<Handle>* args, const char* arg, TRAPS) {
1161   Handle string = java_lang_String::create_from_str(arg, CHECK);
1162   args->append(string);
1163 }
1164 
1165 // Pass all options in Arguments::jvm_args_array() to a child JVM process
1166 // using the JAVA_TOOL_OPTIONS environment variable.
1167 static int exec_jvm_with_java_tool_options(const char* java_launcher_path, TRAPS) {
1168   ResourceMark rm(THREAD);
1169   HandleMark hm(THREAD);
1170   GrowableArray<Handle> args;
1171 
1172   const char* cp = Arguments::get_appclasspath();
1173   if (cp != nullptr && strlen(cp) > 0 && strcmp(cp, ".") != 0) {
1174     // We cannot use "-cp", because "-cp" is only interpreted by the java launcher,
1175     // and is not interpreter by arguments.cpp when it loads args from JAVA_TOOL_OPTIONS
1176     stringStream ss;
1177     ss.print("-Djava.class.path=");
1178     ss.print_raw(cp);
1179     append_args(&args, ss.freeze(), CHECK_0);
1180     // CDS$ProcessLauncher::execWithJavaToolOptions() must unset CLASSPATH, which has
1181     // a higher priority than -Djava.class.path=
1182   }
1183 
1184   // Pass all arguments. These include those from JAVA_TOOL_OPTIONS and _JAVA_OPTIONS.
1185   for (int i = 0; i < Arguments::num_jvm_args(); i++) {
1186     const char* arg = Arguments::jvm_args_array()[i];
1187     if (strncmp("-XX:AOTMode", arg, 11) == 0) {
1188       // Filter it out. We will set AOTMode=create below.
1189     } else {
1190       append_args(&args, arg, CHECK_0);
1191     }
1192   }
1193 
1194   // We don't pass Arguments::jvm_flags_array(), as those will be added by
1195   // the child process when it loads .hotspotrc
1196 
1197   if (CDSConfig::is_experimental_leyden_workflow()) {
1198     stringStream ss;
1199     ss.print("-XX:CDSPreimage=");
1200     ss.print_raw(CDSPreimage);
1201     append_args(&args, ss.freeze(), CHECK_0);
1202   } else {
1203     if (CDSConfig::has_temp_aot_config_file()) {
1204       stringStream ss;
1205       ss.print("-XX:AOTConfiguration=");
1206       ss.print_raw(AOTConfiguration);
1207       append_args(&args, ss.freeze(), CHECK_0);
1208     }
1209     append_args(&args, "-XX:AOTMode=create", CHECK_0);
1210   }
1211 
1212   Symbol* klass_name = SymbolTable::new_symbol("jdk/internal/misc/CDS$ProcessLauncher");
1213   Klass* k = SystemDictionary::resolve_or_fail(klass_name, true, CHECK_0);
1214   Symbol* methodName = SymbolTable::new_symbol("execWithJavaToolOptions");
1215   Symbol* methodSignature = SymbolTable::new_symbol("(Ljava/lang/String;[Ljava/lang/String;)I");
1216 
1217   Handle launcher = java_lang_String::create_from_str(java_launcher_path, CHECK_0);
1218   objArrayOop array = oopFactory::new_objArray(vmClasses::String_klass(), args.length(), CHECK_0);
1219   for (int i = 0; i < args.length(); i++) {
1220     array->obj_at_put(i, args.at(i)());
1221   }
1222   objArrayHandle launcher_args(THREAD, array);
1223 
1224   // The following call will pass all options inside the JAVA_TOOL_OPTIONS env variable to
1225   // the child process. It will also clear the _JAVA_OPTIONS and CLASSPATH env variables for
1226   // the child process.
1227   //
1228   // Note: the env variables are set only for the child process. They are not changed
1229   // for the current process. See java.lang.ProcessBuilder::environment().
1230   JavaValue result(T_OBJECT);
1231   JavaCallArguments javacall_args(2);
1232   javacall_args.push_oop(launcher);
1233   javacall_args.push_oop(launcher_args);
1234   JavaCalls::call_static(&result,
1235                           InstanceKlass::cast(k),
1236                           methodName,
1237                           methodSignature,
1238                           &javacall_args,
1239                           CHECK_0);
1240   return result.get_jint();
1241 }
1242 
1243 // This is for debugging purposes only (-XX:+CDSManualFinalImage) so we don't bother
1244 // quoating any special characters. The user should avoid using special chars.
1245 static void print_vm_arguments(outputStream* st) {
1246   const char* cp = Arguments::get_appclasspath();
1247   if (cp != nullptr && strlen(cp) > 0 && strcmp(cp, ".") != 0) {
1248     st->print(" -cp ");  st->print_raw(cp);
1249   }
1250   for (int i = 0; i < Arguments::num_jvm_args(); i++) {
1251     st->print(" %s", Arguments::jvm_args_array()[i]);
1252   }
1253 }
1254 
1255 void MetaspaceShared::fork_and_dump_final_static_archive_experimental_leyden_workflow(TRAPS) {
1256   assert(CDSConfig::is_dumping_preimage_static_archive(), "sanity");
1257 
1258   ResourceMark rm;
1259   stringStream ss;
1260   print_java_launcher(&ss);
1261 
1262   if (CDSManualFinalImage) {
1263     print_vm_arguments(&ss);
1264     ss.print(" -XX:CDSPreimage=%s", SharedArchiveFile);
1265     const char* cmd = ss.freeze();
1266 
1267     tty->print_cr("-XX:+CDSManualFinalImage is specified");
1268     tty->print_cr("Please manually execute the following command to create the final CDS image:");
1269     tty->print("    "); tty->print_raw_cr(cmd);
1270 
1271     // The following is useful if the dumping was trigger by a script that builds
1272     // a complex command-line.
1273     tty->print_cr("Note: to recreate the preimage only:");
1274     tty->print_cr("    rm -f %s", CacheDataStore);
1275     tty->print("    ");
1276     print_java_launcher(tty);
1277     print_vm_arguments(tty);
1278     if (Arguments::java_command() != nullptr) {
1279       tty->print(" %s", Arguments::java_command());
1280     }
1281     tty->cr();
1282   } else {
1283     const char* cmd = ss.freeze();
1284     log_info(cds)("Launching child process to create final CDS image:");
1285     log_info(cds)("    %s", cmd);
1286     int status = exec_jvm_with_java_tool_options(cmd, CHECK);
1287     if (status != 0) {
1288       log_error(cds)("Child process finished; status = %d", status);
1289       log_error(cds)("To reproduce the error");
1290       ResourceMark rm;
1291       LogStream ls(Log(cds)::error());
1292       ls.print("    "); ls.print_raw_cr(cmd);
1293 
1294       // The following is useful if the dumping was trigger by a script that builds
1295       // a complex command-line.
1296       ls.print_cr("Note: to recreate the preimage only:");
1297       ls.print_cr("    rm -f %s", CacheDataStore);
1298       ls.print("    ");
1299       print_java_launcher(&ls);
1300       print_vm_arguments(&ls);
1301       ls.print(" -XX:+UnlockDiagnosticVMOptions -XX:+CDSManualFinalImage");
1302       if (Arguments::java_command() != nullptr) {
1303         ls.print(" %s", Arguments::java_command());
1304       }
1305       ls.cr();
1306 
1307       vm_direct_exit(status);
1308     } else {
1309       log_info(cds)("Child process finished; status = %d", status);
1310       // On Windows, need WRITE permission to remove the file.
1311       WINDOWS_ONLY(chmod(CDSPreimage, _S_IREAD | _S_IWRITE));
1312       status = remove(CDSPreimage);
1313       if (status != 0) {
1314         log_error(cds)("Failed to remove CDSPreimage file %s", CDSPreimage);
1315       } else {
1316         log_info(cds)("Removed CDSPreimage file %s", CDSPreimage);
1317       }
1318     }
1319   }
1320 }
1321 
1322 void MetaspaceShared::fork_and_dump_final_static_archive(TRAPS) {
1323   assert(CDSConfig::is_dumping_preimage_static_archive(), "sanity");
1324 
1325   ResourceMark rm;
1326   stringStream ss;
1327   print_java_launcher(&ss);
1328   const char* cmd = ss.freeze();
1329   tty->print_cr("Launching child process %s to assemble AOT cache %s using configuration %s", cmd, AOTCacheOutput, AOTConfiguration);
1330   int status = exec_jvm_with_java_tool_options(cmd, CHECK);
1331   if (status != 0) {
1332     log_error(cds)("Child process failed; status = %d", status);
1333     // We leave the temp config file for debugging
1334   } else if (CDSConfig::has_temp_aot_config_file()) {
1335     const char* tmp_config = AOTConfiguration;
1336     // On Windows, need WRITE permission to remove the file.
1337     WINDOWS_ONLY(chmod(tmp_config, _S_IREAD | _S_IWRITE));
1338     status = remove(tmp_config);
1339     if (status != 0) {
1340       log_error(cds)("Failed to remove temporary AOT configuration file %s", tmp_config);
1341     } else {
1342       tty->print_cr("Removed temporary AOT configuration file %s", tmp_config);
1343     }
1344   }
1345 }
1346 
1347 // Returns true if the class's status has changed.
1348 bool MetaspaceShared::try_link_class(JavaThread* current, InstanceKlass* ik) {
1349   ExceptionMark em(current);
1350   JavaThread* THREAD = current; // For exception macros.
1351   assert(CDSConfig::is_dumping_archive(), "sanity");
1352 
1353   if (ik->is_shared() && !CDSConfig::is_dumping_final_static_archive()) {
1354     assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
1355     return false;
1356   }
1357 
1358   if (ik->is_loaded() && !ik->is_linked() && ik->can_be_verified_at_dumptime() &&
1359       !SystemDictionaryShared::has_class_failed_verification(ik)) {
1360     bool saved = BytecodeVerificationLocal;
1361     if (ik->is_shared_unregistered_class() && ik->class_loader() == nullptr) {
1362       // The verification decision is based on BytecodeVerificationRemote
1363       // for non-system classes. Since we are using the null classloader
1364       // to load non-system classes for customized class loaders during dumping,
1365       // we need to temporarily change BytecodeVerificationLocal to be the same as
1366       // BytecodeVerificationRemote. Note this can cause the parent system

1372     if (HAS_PENDING_EXCEPTION) {
1373       ResourceMark rm(THREAD);
1374       log_warning(cds)("Preload Warning: Verification failed for %s",
1375                     ik->external_name());
1376       CLEAR_PENDING_EXCEPTION;
1377       SystemDictionaryShared::set_class_has_failed_verification(ik);
1378     } else {
1379       assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1380       ik->compute_has_loops_flag_for_methods();
1381     }
1382     BytecodeVerificationLocal = saved;
1383     return true;
1384   } else {
1385     return false;
1386   }
1387 }
1388 
1389 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1390   if (CDSConfig::is_dumping_heap()) {
1391     HeapShared::write_heap(&_heap_info);
1392   } else if (!CDSConfig::is_dumping_preimage_static_archive()) {
1393     CDSConfig::log_reasons_for_not_dumping_heap();
1394   }
1395 }
1396 
1397 void MetaspaceShared::set_shared_metaspace_range(void* base, void *static_top, void* top) {
1398   assert(base <= static_top && static_top <= top, "must be");
1399   _shared_metaspace_static_top = static_top;
1400   MetaspaceObj::set_shared_metaspace_range(base, top);
1401 }
1402 
1403 bool MetaspaceShared::is_shared_dynamic(void* p) {
1404   if ((p < MetaspaceObj::shared_metaspace_top()) &&
1405       (p >= _shared_metaspace_static_top)) {
1406     return true;
1407   } else {
1408     return false;
1409   }
1410 }
1411 
1412 bool MetaspaceShared::is_shared_static(void* p) {

1440 // This function is called when the JVM is unable to write the specified CDS archive due to an
1441 // unrecoverable error.
1442 void MetaspaceShared::unrecoverable_writing_error(const char* message) {
1443   writing_error(message);
1444   vm_direct_exit(1);
1445 }
1446 
1447 // This function is called when the JVM is unable to write the specified CDS archive due to a
1448 // an error. The error will be propagated
1449 void MetaspaceShared::writing_error(const char* message) {
1450   log_error(cds)("An error has occurred while writing the shared archive file.");
1451   if (message != nullptr) {
1452     log_error(cds)("%s", message);
1453   }
1454 }
1455 
1456 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
1457   assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
1458   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1459 
1460   FileMapInfo* static_mapinfo = FileMapInfo::current_info();
1461   FileMapInfo* dynamic_mapinfo = nullptr;
1462 
1463   if (static_mapinfo != nullptr) {
1464     log_info(cds)("Core region alignment: %zu", static_mapinfo->core_region_alignment());
1465     dynamic_mapinfo = open_dynamic_archive();
1466 
1467     log_info(cds)("ArchiveRelocationMode: %d", ArchiveRelocationMode);
1468 
1469     // First try to map at the requested address
1470     result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1471     if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1472       // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1473       // by the OS.
1474       log_info(cds)("Try to map archive(s) at an alternative address");
1475       result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1476     }
1477   }
1478 
1479   if (result == MAP_ARCHIVE_SUCCESS) {
1480     bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());

1486     _relocation_delta = static_mapinfo->relocation_delta();
1487     _requested_base_address = static_mapinfo->requested_base_address();
1488     if (dynamic_mapped) {
1489       // turn AutoCreateSharedArchive off if successfully mapped
1490       AutoCreateSharedArchive = false;
1491     }
1492   } else {
1493     set_shared_metaspace_range(nullptr, nullptr, nullptr);
1494     if (CDSConfig::is_dumping_dynamic_archive()) {
1495       log_warning(cds)("-XX:ArchiveClassesAtExit is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info.");
1496     }
1497     UseSharedSpaces = false;
1498     // The base archive cannot be mapped. We cannot dump the dynamic shared archive.
1499     AutoCreateSharedArchive = false;
1500     CDSConfig::disable_dumping_dynamic_archive();
1501     log_info(cds)("Unable to map shared spaces");
1502     if (PrintSharedArchiveAndExit) {
1503       MetaspaceShared::unrecoverable_loading_error("Unable to use shared archive.");
1504     } else if (RequireSharedSpaces) {
1505       MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1506     } else if (CDSConfig::is_dumping_final_static_archive()) {
1507       assert(CDSPreimage != nullptr, "must be");
1508       log_error(cds)("Unable to map shared spaces for CDSPreimage = %s", CDSPreimage);
1509       MetaspaceShared::unrecoverable_loading_error();
1510     }
1511   }
1512 
1513   // If mapping failed and -XShare:on, the vm should exit
1514   bool has_failed = false;
1515   if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1516     has_failed = true;
1517     delete static_mapinfo;
1518   }
1519   if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1520     has_failed = true;
1521     delete dynamic_mapinfo;
1522   }
1523   if (RequireSharedSpaces && has_failed) {
1524     // static archive mapped but dynamic archive failed
1525       MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1526   }
1527 }
1528 
1529 // This is called very early at VM start up to get the size of the cached_code region
1530 void MetaspaceShared::open_static_archive() {
1531   if (!UseSharedSpaces) { // FIXME -- is this still needed??
1532     return;
1533   }
1534   const char* static_archive = CDSConfig::input_static_archive_path();
1535   assert(static_archive != nullptr, "sanity");
1536   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1537   if (!mapinfo->open_as_input()) {
1538     delete(mapinfo);
1539   } else {
1540     FileMapRegion* r = mapinfo->region_at(MetaspaceShared::ac);
1541     AOTCacheAccess::set_aot_code_region_size(r->used_aligned());
1542   }

1543 }
1544 
1545 FileMapInfo* MetaspaceShared::open_dynamic_archive() {
1546   if (CDSConfig::is_dumping_dynamic_archive()) {
1547     return nullptr;
1548   }
1549   const char* dynamic_archive = CDSConfig::input_dynamic_archive_path();
1550   if (dynamic_archive == nullptr) {
1551     return nullptr;
1552   }
1553 
1554   FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1555   if (!mapinfo->open_as_input()) {
1556     delete(mapinfo);
1557     if (RequireSharedSpaces) {
1558       MetaspaceShared::unrecoverable_loading_error("Failed to initialize dynamic archive");
1559     }
1560     return nullptr;
1561   }
1562   return mapinfo;

2008       MemoryReserver::release(archive_space_rs);
2009       archive_space_rs = {};
2010     }
2011     if (class_space_rs.is_reserved()) {
2012       log_debug(cds)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
2013       MemoryReserver::release(class_space_rs);
2014       class_space_rs = {};
2015     }
2016   }
2017 }
2018 
2019 static int archive_regions[]     = { MetaspaceShared::rw, MetaspaceShared::ro };
2020 static int archive_regions_count = 2;
2021 
2022 MapArchiveResult MetaspaceShared::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
2023   assert(CDSConfig::is_using_archive(), "must be runtime");
2024   if (mapinfo == nullptr) {
2025     return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
2026   }
2027 
2028   if (!mapinfo->validate_aot_class_linking()) {
2029     return MAP_ARCHIVE_OTHER_FAILURE;
2030   }
2031 
2032   mapinfo->set_is_mapped(false);
2033   if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
2034     log_info(cds)("Unable to map CDS archive -- core_region_alignment() expected: %zu"
2035                   " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment());
2036     return MAP_ARCHIVE_OTHER_FAILURE;
2037   }
2038 
2039   MapArchiveResult result =
2040     mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
2041 
2042   if (result != MAP_ARCHIVE_SUCCESS) {
2043     unmap_archive(mapinfo);
2044     return result;
2045   }
2046 
2047   if (!mapinfo->validate_class_location()) {
2048     unmap_archive(mapinfo);
2049     return MAP_ARCHIVE_OTHER_FAILURE;
2050   }
2051 

2140     // Read stored LF format lines stored in static archive
2141     LambdaFormInvokers::read_static_archive_invokers();
2142   }
2143 
2144   if (PrintSharedArchiveAndExit) {
2145     // Print archive names
2146     if (dynamic_mapinfo != nullptr) {
2147       tty->print_cr("\n\nBase archive name: %s", CDSConfig::input_static_archive_path());
2148       tty->print_cr("Base archive version %d", static_mapinfo->version());
2149     } else {
2150       tty->print_cr("Static archive name: %s", static_mapinfo->full_path());
2151       tty->print_cr("Static archive version %d", static_mapinfo->version());
2152     }
2153 
2154     SystemDictionaryShared::print_shared_archive(tty);
2155     if (dynamic_mapinfo != nullptr) {
2156       tty->print_cr("\n\nDynamic archive name: %s", dynamic_mapinfo->full_path());
2157       tty->print_cr("Dynamic archive version %d", dynamic_mapinfo->version());
2158       SystemDictionaryShared::print_shared_archive(tty, false/*dynamic*/);
2159     }
2160     TrainingData::print_archived_training_data_on(tty);
2161 
2162     if (AOTCodeCache::is_on_for_use()) {
2163       tty->print_cr("\n\nAOT Code");
2164       AOTCodeCache::print_on(tty);
2165     }
2166 
2167     // collect shared symbols and strings
2168     CountSharedSymbols cl;
2169     SymbolTable::shared_symbols_do(&cl);
2170     tty->print_cr("Number of shared symbols: %d", cl.total());
2171     tty->print_cr("Number of shared strings: %zu", StringTable::shared_entry_count());
2172     tty->print_cr("VM version: %s\r\n", static_mapinfo->vm_version());
2173     if (FileMapInfo::current_info() == nullptr || _archive_loading_failed) {
2174       tty->print_cr("archive is invalid");
2175       vm_exit(1);
2176     } else {
2177       tty->print_cr("archive is valid");
2178       vm_exit(0);
2179     }
2180   }
< prev index next >