< 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/aotLogging.hpp"
  32 #include "cds/aotReferenceObjSupport.hpp"
  33 #include "cds/archiveBuilder.hpp"
  34 #include "cds/archiveHeapLoader.hpp"
  35 #include "cds/archiveHeapWriter.hpp"
  36 #include "cds/cds_globals.hpp"
  37 #include "cds/cdsConfig.hpp"
  38 #include "cds/cdsProtectionDomain.hpp"
  39 #include "cds/classListParser.hpp"
  40 #include "cds/classListWriter.hpp"
  41 #include "cds/cppVtables.hpp"
  42 #include "cds/dumpAllocStats.hpp"
  43 #include "cds/dynamicArchive.hpp"
  44 #include "cds/filemap.hpp"
  45 #include "cds/finalImageRecipes.hpp"
  46 #include "cds/heapShared.hpp"
  47 #include "cds/lambdaFormInvokers.hpp"
  48 #include "cds/lambdaProxyClassDictionary.hpp"
  49 #include "cds/metaspaceShared.hpp"
  50 #include "classfile/classLoaderDataGraph.hpp"
  51 #include "classfile/classLoaderDataShared.hpp"
  52 #include "classfile/classLoaderExt.hpp"
  53 #include "classfile/javaClasses.inline.hpp"
  54 #include "classfile/loaderConstraints.hpp"
  55 #include "classfile/modules.hpp"
  56 #include "classfile/placeholders.hpp"
  57 #include "classfile/stringTable.hpp"
  58 #include "classfile/symbolTable.hpp"
  59 #include "classfile/systemDictionary.hpp"
  60 #include "classfile/systemDictionaryShared.hpp"
  61 #include "classfile/vmClasses.hpp"
  62 #include "classfile/vmSymbols.hpp"
  63 #include "code/aotCodeCache.hpp"
  64 #include "code/codeCache.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/oopFactory.hpp"
  76 #include "memory/resourceArea.hpp"
  77 #include "memory/universe.hpp"
  78 #include "nmt/memTracker.hpp"
  79 #include "oops/compressedKlass.hpp"
  80 #include "oops/instanceMirrorKlass.hpp"
  81 #include "oops/klass.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 "runtime/arguments.hpp"
  88 #include "runtime/globals.hpp"
  89 #include "runtime/globals_extension.hpp"
  90 #include "runtime/handles.inline.hpp"
  91 #include "runtime/javaCalls.hpp"
  92 #include "runtime/os.inline.hpp"
  93 #include "runtime/safepointVerifiers.hpp"
  94 #include "runtime/sharedRuntime.hpp"
  95 #include "runtime/vmOperations.hpp"
  96 #include "runtime/vmThread.hpp"
  97 #include "sanitizers/leak.hpp"

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


 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 //

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















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

 682 
 683   log_info(cds)("Make training data shareable");
 684   _builder.make_training_data_shareable();
 685 
 686   // The vtable clones contain addresses of the current process.
 687   // We don't want to write these addresses into the archive.
 688   CppVtables::zero_archived_vtables();
 689 
 690   // Write the archive file
 691   if (CDSConfig::is_dumping_final_static_archive()) {
 692     FileMapInfo::free_current_info(); // FIXME: should not free current info
 693   }
 694   const char* static_archive = CDSConfig::output_archive_path();
 695   assert(static_archive != nullptr, "sanity");
 696   _map_info = new FileMapInfo(static_archive, true);
 697   _map_info->populate_header(MetaspaceShared::core_region_alignment());
 698   _map_info->set_early_serialized_data(early_serialized_data);
 699   _map_info->set_serialized_data(serialized_data);
 700   _map_info->set_cloned_vtables(CppVtables::vtables_serialized_base());
 701   _map_info->header()->set_class_location_config(cl_config);


 702 }
 703 
 704 class CollectClassesForLinking : public KlassClosure {
 705   GrowableArray<OopHandle> _mirrors;
 706 
 707 public:
 708    CollectClassesForLinking() : _mirrors() {
 709      // ClassLoaderDataGraph::loaded_classes_do_keepalive() requires ClassLoaderDataGraph_lock.
 710      // We cannot link the classes while holding this lock (or else we may run into deadlock).
 711      // Therefore, we need to first collect all the classes, keeping them alive by
 712      // holding onto their java_mirrors in global OopHandles. We then link the classes after
 713      // releasing the lock.
 714      MutexLocker lock(ClassLoaderDataGraph_lock);
 715      ClassLoaderDataGraph::loaded_classes_do_keepalive(this);
 716    }
 717 
 718   ~CollectClassesForLinking() {
 719     for (int i = 0; i < _mirrors.length(); i++) {
 720       _mirrors.at(i).release(Universe::vm_global());
 721     }
 722   }
 723 
 724   void do_cld(ClassLoaderData* cld) {
 725     assert(cld->is_alive(), "must be");
 726   }
 727 
 728   void do_klass(Klass* k) {
 729     if (k->is_instance_klass()) {
 730       _mirrors.append(OopHandle(Universe::vm_global(), k->java_mirror()));
 731     }
 732   }
 733 
 734   const GrowableArray<OopHandle>* mirrors() const { return &_mirrors; }
 735 };
 736 
 737 // Check if we can eagerly link this class at dump time, so we can avoid the
 738 // runtime linking overhead (especially verification)
 739 bool MetaspaceShared::may_be_eagerly_linked(InstanceKlass* ik) {



 740   if (!ik->can_be_verified_at_dumptime()) {
 741     // For old classes, try to leave them in the unlinked state, so
 742     // we can still store them in the archive. They must be
 743     // linked/verified at runtime.
 744     return false;
 745   }
 746   if (CDSConfig::is_dumping_dynamic_archive() && ik->defined_by_other_loaders()) {
 747     // Linking of unregistered classes at this stage may cause more
 748     // classes to be resolved, resulting in calls to ClassLoader.loadClass()
 749     // that may not be expected by custom class loaders.
 750     //
 751     // It's OK to do this for the built-in loaders as we know they can
 752     // tolerate this.
 753     return false;
 754   }
 755   return true;
 756 }
 757 
 758 void MetaspaceShared::link_shared_classes(TRAPS) {
 759   AOTClassLinker::initialize();
 760   AOTClassInitializer::init_test_class(CHECK);
 761 




 762   while (true) {
 763     ResourceMark rm(THREAD);
 764     CollectClassesForLinking collect_classes;
 765     bool has_linked = false;
 766     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 767     for (int i = 0; i < mirrors->length(); i++) {
 768       OopHandle mirror = mirrors->at(i);
 769       InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
 770       if (may_be_eagerly_linked(ik)) {
 771         has_linked |= try_link_class(THREAD, ik);
 772       }



 773     }
 774 
 775     if (!has_linked) {
 776       break;
 777     }
 778     // Class linking includes verification which may load more classes.
 779     // Keep scanning until we have linked no more classes.
 780   }
 781 
 782   // Eargerly resolve all string constants in constant pools
 783   {
 784     ResourceMark rm(THREAD);
 785     CollectClassesForLinking collect_classes;
 786     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 787     for (int i = 0; i < mirrors->length(); i++) {
 788       OopHandle mirror = mirrors->at(i);
 789       InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
 790       AOTConstantPoolResolver::preresolve_string_cp_entries(ik, CHECK);



 791     }
 792   }
 793 
 794   if (CDSConfig::is_dumping_final_static_archive()) {
 795     FinalImageRecipes::apply_recipes(CHECK);
 796   }
 797 }
 798 
 799 // Preload classes from a list, populate the shared spaces and dump to a
 800 // file.
 801 void MetaspaceShared::preload_and_dump(TRAPS) {
 802   CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
 803   ResourceMark rm(THREAD);
 804  HandleMark hm(THREAD);
 805 
 806  if (CDSConfig::is_dumping_final_static_archive() && AOTPrintTrainingInfo) {
 807    tty->print_cr("==================== archived_training_data ** before dumping ====================");
 808    TrainingData::print_archived_training_data_on(tty);
 809  }
 810 
 811   StaticArchiveBuilder builder;
 812   preload_and_dump_impl(builder, THREAD);
 813   if (HAS_PENDING_EXCEPTION) {
 814     if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 815       aot_log_error(aot)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
 816                      "%zuM", MaxHeapSize/M);
 817       MetaspaceShared::writing_error();
 818     } else {
 819       oop message = java_lang_Throwable::message(PENDING_EXCEPTION);
 820       aot_log_error(aot)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
 821                          message == nullptr ? "(null)" : java_lang_String::as_utf8_string(message));
 822       MetaspaceShared::writing_error(err_msg("Unexpected exception, use -Xlog:aot%s,exceptions=trace for detail",
 823                                              CDSConfig::new_aot_flags_used() ? "" : ",cds"));
 824     }
 825   }
 826 
 827   if (CDSConfig::new_aot_flags_used()) {
 828     if (CDSConfig::is_dumping_preimage_static_archive()) {
 829       // We are in the JVM that runs the training run. Continue execution,
 830       // so that it can finish all clean-up and return the correct exit
 831       // code to the OS.
 832     } else {
 833       // The JLI launcher only recognizes the "old" -Xshare:dump flag.
 834       // When the new -XX:AOTMode=create flag is used, we can't return
 835       // to the JLI launcher, as the launcher will fail when trying to
 836       // run the main class, which is not what we want.
 837       struct stat st;
 838       if (os::stat(AOTCache, &st) != 0) {
 839         tty->print_cr("AOTCache creation failed: %s", AOTCache);
 840         vm_exit(0);
 841       } else {
 842         tty->print_cr("AOTCache creation is complete: %s " INT64_FORMAT " bytes", AOTCache, (int64_t)(st.st_size));
 843         vm_exit(0);
 844       }

 845     }
 846   }
 847 }
 848 
 849 #if INCLUDE_CDS_JAVA_HEAP && defined(_LP64)
 850 void MetaspaceShared::adjust_heap_sizes_for_dumping() {
 851   if (!CDSConfig::is_dumping_heap() || UseCompressedOops) {
 852     return;
 853   }
 854   // CDS heap dumping requires all string oops to have an offset
 855   // from the heap bottom that can be encoded in 32-bit.
 856   julong max_heap_size = (julong)(4 * G);
 857 
 858   if (MinHeapSize > max_heap_size) {
 859     log_debug(aot)("Setting MinHeapSize to 4G for CDS dumping, original size = %zuM", MinHeapSize/M);
 860     FLAG_SET_ERGO(MinHeapSize, max_heap_size);
 861   }
 862   if (InitialHeapSize > max_heap_size) {
 863     log_debug(aot)("Setting InitialHeapSize to 4G for CDS dumping, original size = %zuM", InitialHeapSize/M);
 864     FLAG_SET_ERGO(InitialHeapSize, max_heap_size);

 903     }
 904   }
 905 
 906   // Some classes are used at CDS runtime but are not loaded, and therefore archived, at
 907   // dumptime. We can perform dummmy calls to these classes at dumptime to ensure they
 908   // are archived.
 909   exercise_runtime_cds_code(CHECK);
 910 
 911   aot_log_info(aot)("Loading classes to share: done.");
 912 }
 913 
 914 void MetaspaceShared::exercise_runtime_cds_code(TRAPS) {
 915   // Exercise the manifest processing code
 916   const char* dummy = "Manifest-Version: 1.0\n";
 917   CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
 918 
 919   // Exercise FileSystem and URL code
 920   CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
 921 }
 922 


















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







 924   if (CDSConfig::is_dumping_classic_static_archive()) {
 925     // We are running with -Xshare:dump
 926     preload_classes(CHECK);
 927 
 928     if (SharedArchiveConfigFile) {
 929       log_info(aot)("Reading extra data from %s ...", SharedArchiveConfigFile);
 930       read_extra_data(THREAD, SharedArchiveConfigFile);
 931       log_info(aot)("Reading extra data: done.");
 932     }
 933   }
 934 
 935   if (CDSConfig::is_dumping_preimage_static_archive()) {
 936     log_info(aot)("Reading lambda form invokers from JDK default classlist ...");
 937     char default_classlist[JVM_MAXPATHLEN];
 938     get_default_classlist(default_classlist, JVM_MAXPATHLEN);
 939     struct stat statbuf;
 940     if (os::stat(default_classlist, &statbuf) == 0) {
 941       ClassListParser::parse_classlist(default_classlist,
 942                                        ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
 943     }

 970   // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
 971   // fails verification, all other interfaces that were not specified in the classlist but
 972   // are implemented by K are not verified.
 973   link_shared_classes(CHECK);
 974   log_info(aot)("Rewriting and linking classes: done");
 975   TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
 976 
 977   if (CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
 978     LambdaFormInvokers::regenerate_holder_classes(CHECK);
 979   }
 980 
 981 #if INCLUDE_CDS_JAVA_HEAP
 982   if (CDSConfig::is_dumping_heap()) {
 983     ArchiveHeapWriter::init();
 984 
 985     if (CDSConfig::is_dumping_full_module_graph()) {
 986       ClassLoaderDataShared::ensure_module_entry_tables_exist();
 987       HeapShared::reset_archived_object_states(CHECK);
 988     }
 989 




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








1018 
1019   if (AOTCodeCache::is_on_for_dump() && CDSConfig::is_dumping_final_static_archive()) {
1020     CDSConfig::enable_dumping_aot_code();
1021     {
1022       builder.start_ac_region();
1023       // Write the contents to AOT code region and close AOTCodeCache before packing the region
1024       AOTCodeCache::close();
1025       builder.end_ac_region();













1026     }
1027     CDSConfig::disable_dumping_aot_code();
1028   }
1029 
1030   bool status = write_static_archive(&builder, op.map_info(), op.heap_info());
1031   if (status && CDSConfig::is_dumping_preimage_static_archive()) {
1032     tty->print_cr("%s AOTConfiguration recorded: %s",
1033                   CDSConfig::has_temp_aot_config_file() ? "Temporary" : "", AOTConfiguration);
1034     if (CDSConfig::is_single_command_training()) {
1035       fork_and_dump_final_static_archive(CHECK);
1036     }
1037   }
1038 
1039   if (!status) {
1040     THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
1041   }
1042 }
1043 
1044 bool MetaspaceShared::write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info) {
1045   // relocate the data so that it can be mapped to MetaspaceShared::requested_base_address()
1046   // without runtime relocation.
1047   builder->relocate_to_requested();
1048 
1049   map_info->open_as_output();
1050   if (!map_info->is_open()) {

1213     if (HAS_PENDING_EXCEPTION) {
1214       ResourceMark rm(THREAD);
1215       aot_log_warning(aot)("Preload Warning: Verification failed for %s",
1216                     ik->external_name());
1217       CLEAR_PENDING_EXCEPTION;
1218       SystemDictionaryShared::set_class_has_failed_verification(ik);
1219     } else {
1220       assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1221       ik->compute_has_loops_flag_for_methods();
1222     }
1223     BytecodeVerificationLocal = saved;
1224     return true;
1225   } else {
1226     return false;
1227   }
1228 }
1229 
1230 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1231   if (CDSConfig::is_dumping_heap()) {
1232     HeapShared::write_heap(&_heap_info);
1233   } else {
1234     CDSConfig::log_reasons_for_not_dumping_heap();
1235   }
1236 }
1237 
1238 void MetaspaceShared::set_shared_metaspace_range(void* base, void *static_top, void* top) {
1239   assert(base <= static_top && static_top <= top, "must be");
1240   _shared_metaspace_static_top = static_top;
1241   MetaspaceObj::set_shared_metaspace_range(base, top);
1242 }
1243 
1244 bool MetaspaceShared::is_shared_dynamic(void* p) {
1245   if ((p < MetaspaceObj::shared_metaspace_top()) &&
1246       (p >= _shared_metaspace_static_top)) {
1247     return true;
1248   } else {
1249     return false;
1250   }
1251 }
1252 
1253 bool MetaspaceShared::is_shared_static(void* p) {

1271   } else if (CDSConfig::new_aot_flags_used()) {
1272     vm_exit_during_initialization("Unable to use AOT cache.", nullptr);
1273   } else {
1274     vm_exit_during_initialization("Unable to use shared archive.", nullptr);
1275   }
1276 }
1277 
1278 void MetaspaceShared::report_loading_error(const char* format, ...) {
1279   // When using AOT cache, errors messages are always printed on the error channel.
1280   LogStream ls_aot(LogLevel::Error, LogTagSetMapping<LOG_TAGS(aot)>::tagset());
1281 
1282   // If we are loading load the default CDS archive, it may fail due to incompatible VM options.
1283   // Print at the info level to avoid excessive verbosity.
1284   // However, if the user has specified a CDS archive (or AOT cache), they would be interested in
1285   // knowing that the loading fails, so we print at the error level.
1286   LogLevelType level = (!CDSConfig::is_using_archive() || CDSConfig::is_using_only_default_archive()) ?
1287                         LogLevel::Info : LogLevel::Error;
1288   LogStream ls_cds(level, LogTagSetMapping<LOG_TAGS(cds)>::tagset());
1289 
1290   LogStream& ls = CDSConfig::new_aot_flags_used() ? ls_aot : ls_cds;




1291   va_list ap;
1292   va_start(ap, format);
1293 
1294   static bool printed_error = false;
1295   if (!printed_error) { // No need for locks. Loading error checks happen only in main thread.
1296     ls.print_cr("An error has occurred while processing the %s. Run with -Xlog:%s for details.",
1297                 CDSConfig::type_of_archive_being_loaded(), CDSConfig::new_aot_flags_used() ? "aot" : "aot,cds");
1298     printed_error = true;
1299   }
1300   ls.vprint_cr(format, ap);
1301 
1302   va_end(ap);
1303 }
1304 
1305 // This function is called when the JVM is unable to write the specified CDS archive due to an
1306 // unrecoverable error.
1307 void MetaspaceShared::unrecoverable_writing_error(const char* message) {
1308   writing_error(message);
1309   vm_direct_exit(1);
1310 }
1311 
1312 // This function is called when the JVM is unable to write the specified CDS archive due to a
1313 // an error. The error will be propagated
1314 void MetaspaceShared::writing_error(const char* message) {
1315   aot_log_error(aot)("An error has occurred while writing the shared archive file.");
1316   if (message != nullptr) {
1317     aot_log_error(aot)("%s", message);
1318   }
1319 }
1320 
1321 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
1322   assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
1323   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1324 
1325   FileMapInfo* static_mapinfo = open_static_archive();
1326   FileMapInfo* dynamic_mapinfo = nullptr;
1327 
1328   if (static_mapinfo != nullptr) {
1329     aot_log_info(aot)("Core region alignment: %zu", static_mapinfo->core_region_alignment());
1330     dynamic_mapinfo = open_dynamic_archive();
1331 
1332     aot_log_info(aot)("ArchiveRelocationMode: %d", ArchiveRelocationMode);
1333 
1334     // First try to map at the requested address
1335     result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1336     if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1337       // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1338       // by the OS.
1339       aot_log_info(aot)("Try to map archive(s) at an alternative address");
1340       result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1341     }
1342   }
1343 
1344   if (result == MAP_ARCHIVE_SUCCESS) {
1345     bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());

1368     } else {
1369       if (RequireSharedSpaces) {
1370         MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1371       } else {
1372         report_loading_error("Unable to map shared spaces");
1373       }
1374     }
1375   }
1376 
1377   // If mapping failed and -XShare:on, the vm should exit
1378   bool has_failed = false;
1379   if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1380     has_failed = true;
1381     delete static_mapinfo;
1382   }
1383   if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1384     has_failed = true;
1385     delete dynamic_mapinfo;
1386   }
1387   if (RequireSharedSpaces && has_failed) {

1388       MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1389   }
1390 }
1391 
1392 FileMapInfo* MetaspaceShared::open_static_archive() {




1393   const char* static_archive = CDSConfig::input_static_archive_path();
1394   assert(static_archive != nullptr, "sanity");
1395   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1396   if (!mapinfo->open_as_input()) {
1397     delete(mapinfo);
1398     return nullptr;


1399   }
1400   return mapinfo;
1401 }
1402 
1403 FileMapInfo* MetaspaceShared::open_dynamic_archive() {
1404   if (CDSConfig::is_dumping_dynamic_archive()) {
1405     return nullptr;
1406   }
1407   const char* dynamic_archive = CDSConfig::input_dynamic_archive_path();
1408   if (dynamic_archive == nullptr) {
1409     return nullptr;
1410   }
1411 
1412   FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1413   if (!mapinfo->open_as_input()) {
1414     delete(mapinfo);
1415     if (RequireSharedSpaces) {
1416       MetaspaceShared::unrecoverable_loading_error("Failed to initialize dynamic archive");
1417     }
1418     return nullptr;
1419   }
1420   return mapinfo;

1866       MemoryReserver::release(archive_space_rs);
1867       archive_space_rs = {};
1868     }
1869     if (class_space_rs.is_reserved()) {
1870       aot_log_debug(aot)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
1871       MemoryReserver::release(class_space_rs);
1872       class_space_rs = {};
1873     }
1874   }
1875 }
1876 
1877 static int archive_regions[]     = { MetaspaceShared::rw, MetaspaceShared::ro };
1878 static int archive_regions_count = 2;
1879 
1880 MapArchiveResult MetaspaceShared::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
1881   assert(CDSConfig::is_using_archive(), "must be runtime");
1882   if (mapinfo == nullptr) {
1883     return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
1884   }
1885 




1886   mapinfo->set_is_mapped(false);
1887   if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
1888     report_loading_error("Unable to map CDS archive -- core_region_alignment() expected: %zu"
1889                          " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment());
1890     return MAP_ARCHIVE_OTHER_FAILURE;
1891   }
1892 
1893   MapArchiveResult result =
1894     mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
1895 
1896   if (result != MAP_ARCHIVE_SUCCESS) {
1897     unmap_archive(mapinfo);
1898     return result;
1899   }
1900 
1901   if (!mapinfo->validate_class_location()) {
1902     unmap_archive(mapinfo);
1903     return MAP_ARCHIVE_OTHER_FAILURE;
1904   }
1905 

1997 
1998   if (PrintSharedArchiveAndExit) {
1999     // Print archive names
2000     if (dynamic_mapinfo != nullptr) {
2001       tty->print_cr("\n\nBase archive name: %s", CDSConfig::input_static_archive_path());
2002       tty->print_cr("Base archive version %d", static_mapinfo->version());
2003     } else {
2004       tty->print_cr("Static archive name: %s", static_mapinfo->full_path());
2005       tty->print_cr("Static archive version %d", static_mapinfo->version());
2006     }
2007 
2008     SystemDictionaryShared::print_shared_archive(tty);
2009     if (dynamic_mapinfo != nullptr) {
2010       tty->print_cr("\n\nDynamic archive name: %s", dynamic_mapinfo->full_path());
2011       tty->print_cr("Dynamic archive version %d", dynamic_mapinfo->version());
2012       SystemDictionaryShared::print_shared_archive(tty, false/*dynamic*/);
2013     }
2014 
2015     TrainingData::print_archived_training_data_on(tty);
2016 
2017     if (AOTCodeCache::is_on_for_use()) {
2018       tty->print_cr("\n\nAOT Code");
2019       AOTCodeCache::print_on(tty);
2020     }
2021 
2022     // collect shared symbols and strings
2023     CountSharedSymbols cl;
2024     SymbolTable::shared_symbols_do(&cl);
2025     tty->print_cr("Number of shared symbols: %d", cl.total());
2026     tty->print_cr("Number of shared strings: %zu", StringTable::shared_entry_count());
2027     tty->print_cr("VM version: %s\r\n", static_mapinfo->vm_version());
2028     if (FileMapInfo::current_info() == nullptr || _archive_loading_failed) {
2029       tty->print_cr("archive is invalid");
2030       vm_exit(1);
2031     } else {
2032       tty->print_cr("archive is valid");
2033       vm_exit(0);
2034     }
2035   }
2036 }
2037 
2038 // JVM/TI RedefineClasses() support:
2039 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
2040   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/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/aotLogging.hpp"
  34 #include "cds/aotReferenceObjSupport.hpp"
  35 #include "cds/archiveBuilder.hpp"
  36 #include "cds/archiveHeapLoader.hpp"
  37 #include "cds/archiveHeapWriter.hpp"
  38 #include "cds/cds_globals.hpp"
  39 #include "cds/cdsConfig.hpp"
  40 #include "cds/cdsProtectionDomain.hpp"
  41 #include "cds/classListParser.hpp"
  42 #include "cds/classListWriter.hpp"
  43 #include "cds/cppVtables.hpp"
  44 #include "cds/dumpAllocStats.hpp"
  45 #include "cds/dynamicArchive.hpp"
  46 #include "cds/filemap.hpp"
  47 #include "cds/finalImageRecipes.hpp"
  48 #include "cds/heapShared.hpp"
  49 #include "cds/lambdaFormInvokers.hpp"
  50 #include "cds/lambdaProxyClassDictionary.hpp"
  51 #include "cds/metaspaceShared.hpp"
  52 #include "classfile/classLoaderDataGraph.hpp"
  53 #include "classfile/classLoaderDataShared.hpp"
  54 #include "classfile/classLoaderExt.hpp"
  55 #include "classfile/javaClasses.inline.hpp"
  56 #include "classfile/loaderConstraints.hpp"
  57 #include "classfile/modules.hpp"
  58 #include "classfile/placeholders.hpp"
  59 #include "classfile/stringTable.hpp"
  60 #include "classfile/symbolTable.hpp"
  61 #include "classfile/systemDictionary.hpp"
  62 #include "classfile/systemDictionaryShared.hpp"
  63 #include "classfile/vmClasses.hpp"
  64 #include "classfile/vmSymbols.hpp"
  65 #include "code/aotCodeCache.hpp"
  66 #include "code/codeCache.hpp"
  67 #include "compiler/compileBroker.hpp"
  68 #include "compiler/precompiler.hpp"
  69 #include "gc/shared/gcVMOperations.hpp"
  70 #include "interpreter/bytecodeStream.hpp"
  71 #include "interpreter/bytecodes.hpp"
  72 #include "jvm_io.h"
  73 #include "logging/log.hpp"
  74 #include "logging/logMessage.hpp"
  75 #include "logging/logStream.hpp"
  76 #include "memory/memoryReserver.hpp"
  77 #include "memory/metaspace.hpp"
  78 #include "memory/metaspaceClosure.hpp"
  79 #include "memory/oopFactory.hpp"
  80 #include "memory/resourceArea.hpp"
  81 #include "memory/universe.hpp"
  82 #include "nmt/memTracker.hpp"
  83 #include "oops/compressedKlass.hpp"
  84 #include "oops/instanceMirrorKlass.hpp"
  85 #include "oops/klass.inline.hpp"
  86 #include "oops/method.inline.hpp"
  87 #include "oops/objArrayOop.hpp"
  88 #include "oops/oop.inline.hpp"
  89 #include "oops/oopHandle.hpp"
  90 #include "oops/trainingData.hpp"
  91 #include "prims/jvmtiExport.hpp"
  92 #include "prims/whitebox.hpp"
  93 #include "runtime/arguments.hpp"
  94 #include "runtime/globals.hpp"
  95 #include "runtime/globals_extension.hpp"
  96 #include "runtime/handles.inline.hpp"
  97 #include "runtime/javaCalls.hpp"
  98 #include "runtime/os.inline.hpp"
  99 #include "runtime/safepointVerifiers.hpp"
 100 #include "runtime/sharedRuntime.hpp"
 101 #include "runtime/vmOperations.hpp"
 102 #include "runtime/vmThread.hpp"
 103 #include "sanitizers/leak.hpp"
 104 #include "services/management.hpp"
 105 #include "utilities/align.hpp"
 106 #include "utilities/bitMap.inline.hpp"
 107 #include "utilities/defaultStream.hpp"
 108 #include "utilities/macros.hpp"
 109 #include "utilities/ostream.hpp"
 110 #include "utilities/resourceHash.hpp"
 111 
 112 #include <sys/stat.h>
 113 
 114 ReservedSpace MetaspaceShared::_symbol_rs;
 115 VirtualSpace MetaspaceShared::_symbol_vs;
 116 bool MetaspaceShared::_archive_loading_failed = false;
 117 bool MetaspaceShared::_remapped_readwrite = false;
 118 void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
 119 intx MetaspaceShared::_relocation_delta;
 120 char* MetaspaceShared::_requested_base_address;
 121 Array<Method*>* MetaspaceShared::_archived_method_handle_intrinsics = nullptr;
 122 bool MetaspaceShared::_use_optimized_module_handling = true;
 123 int volatile MetaspaceShared::_preimage_static_archive_dumped = 0;
 124 jlong MetaspaceShared::_preimage_static_archive_recording_duration = 0;
 125 
 126 // The CDS archive is divided into the following regions:
 127 //     rw  - read-write metadata
 128 //     ro  - read-only metadata and read-only tables
 129 //     hp  - heap region
 130 //     bm  - bitmap for relocating the above 7 regions.
 131 //
 132 // The rw and ro regions are linearly allocated, in the order of rw->ro.
 133 // These regions are aligned with MetaspaceShared::core_region_alignment().
 134 //
 135 // These 2 regions are populated in the following steps:
 136 // [0] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
 137 //     temporarily allocated outside of the shared regions.
 138 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
 139 // [2] C++ vtables are copied into the rw region.
 140 // [3] ArchiveBuilder copies RW metadata into the rw region.
 141 // [4] ArchiveBuilder copies RO metadata into the ro region.
 142 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
 143 //     are copied into the ro region as read-only tables.
 144 //

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

 706 
 707   log_info(cds)("Make training data shareable");
 708   _builder.make_training_data_shareable();
 709 
 710   // The vtable clones contain addresses of the current process.
 711   // We don't want to write these addresses into the archive.
 712   CppVtables::zero_archived_vtables();
 713 
 714   // Write the archive file
 715   if (CDSConfig::is_dumping_final_static_archive()) {
 716     FileMapInfo::free_current_info(); // FIXME: should not free current info
 717   }
 718   const char* static_archive = CDSConfig::output_archive_path();
 719   assert(static_archive != nullptr, "sanity");
 720   _map_info = new FileMapInfo(static_archive, true);
 721   _map_info->populate_header(MetaspaceShared::core_region_alignment());
 722   _map_info->set_early_serialized_data(early_serialized_data);
 723   _map_info->set_serialized_data(serialized_data);
 724   _map_info->set_cloned_vtables(CppVtables::vtables_serialized_base());
 725   _map_info->header()->set_class_location_config(cl_config);
 726 
 727   HeapShared::delete_tables_with_raw_oops();
 728 }
 729 
 730 class CollectClassesForLinking : public KlassClosure {
 731   GrowableArray<OopHandle> _mirrors;
 732 
 733 public:
 734    CollectClassesForLinking() : _mirrors() {
 735      // ClassLoaderDataGraph::loaded_classes_do_keepalive() requires ClassLoaderDataGraph_lock.
 736      // We cannot link the classes while holding this lock (or else we may run into deadlock).
 737      // Therefore, we need to first collect all the classes, keeping them alive by
 738      // holding onto their java_mirrors in global OopHandles. We then link the classes after
 739      // releasing the lock.
 740      MutexLocker lock(ClassLoaderDataGraph_lock);
 741      ClassLoaderDataGraph::loaded_classes_do_keepalive(this);
 742    }
 743 
 744   ~CollectClassesForLinking() {
 745     for (int i = 0; i < _mirrors.length(); i++) {
 746       _mirrors.at(i).release(Universe::vm_global());
 747     }
 748   }
 749 
 750   void do_cld(ClassLoaderData* cld) {
 751     assert(cld->is_alive(), "must be");
 752   }
 753 
 754   void do_klass(Klass* k) {
 755     if (k->is_instance_klass()) {
 756       _mirrors.append(OopHandle(Universe::vm_global(), k->java_mirror()));
 757     }
 758   }
 759 
 760   const GrowableArray<OopHandle>* mirrors() const { return &_mirrors; }
 761 };
 762 
 763 // Check if we can eagerly link this class at dump time, so we can avoid the
 764 // runtime linking overhead (especially verification)
 765 bool MetaspaceShared::may_be_eagerly_linked(InstanceKlass* ik) {
 766   if (CDSConfig::preserve_all_dumptime_verification_states(ik)) {
 767     assert(ik->can_be_verified_at_dumptime(), "sanity");
 768   }
 769   if (!ik->can_be_verified_at_dumptime()) {
 770     // For old classes, try to leave them in the unlinked state, so
 771     // we can still store them in the archive. They must be
 772     // linked/verified at runtime.
 773     return false;
 774   }
 775   if (CDSConfig::is_dumping_dynamic_archive() && ik->defined_by_other_loaders()) {
 776     // Linking of unregistered classes at this stage may cause more
 777     // classes to be resolved, resulting in calls to ClassLoader.loadClass()
 778     // that may not be expected by custom class loaders.
 779     //
 780     // It's OK to do this for the built-in loaders as we know they can
 781     // tolerate this.
 782     return false;
 783   }
 784   return true;
 785 }
 786 
 787 void MetaspaceShared::link_shared_classes(TRAPS) {
 788   AOTClassLinker::initialize();
 789   AOTClassInitializer::init_test_class(CHECK);
 790 
 791   if (CDSConfig::is_dumping_final_static_archive()) {
 792     FinalImageRecipes::apply_recipes(CHECK);
 793   }
 794 
 795   while (true) {
 796     ResourceMark rm(THREAD);
 797     CollectClassesForLinking collect_classes;
 798     bool has_linked = false;
 799     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 800     for (int i = 0; i < mirrors->length(); i++) {
 801       OopHandle mirror = mirrors->at(i);
 802       InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
 803       if (may_be_eagerly_linked(ik)) {
 804         has_linked |= try_link_class(THREAD, ik);
 805       }
 806       if (CDSConfig::is_dumping_heap() && ik->is_linked() && !ik->is_initialized()) {
 807         AOTClassInitializer::maybe_preinit_class(ik, CHECK);
 808       }
 809     }
 810 
 811     if (!has_linked) {
 812       break;
 813     }
 814     // Class linking includes verification which may load more classes.
 815     // Keep scanning until we have linked no more classes.
 816   }
 817 
 818   // Eargerly resolve all string constants in constant pools
 819   {
 820     ResourceMark rm(THREAD);
 821     CollectClassesForLinking collect_classes;
 822     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 823     for (int i = 0; i < mirrors->length(); i++) {
 824       OopHandle mirror = mirrors->at(i);
 825       InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
 826       AOTConstantPoolResolver::preresolve_string_cp_entries(ik, CHECK);
 827       if (CDSConfig::is_dumping_preimage_static_archive()) {
 828         FinalImageRecipes::add_reflection_data_flags(ik, CHECK);
 829       }
 830     }
 831   }




 832 }
 833 
 834 // Preload classes from a list, populate the shared spaces and dump to a
 835 // file.
 836 void MetaspaceShared::preload_and_dump(TRAPS) {
 837   CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
 838   ResourceMark rm(THREAD);
 839   HandleMark hm(THREAD);
 840 
 841   if (CDSConfig::is_dumping_final_static_archive() && AOTPrintTrainingInfo) {
 842     tty->print_cr("==================== archived_training_data ** before dumping ====================");
 843     TrainingData::print_archived_training_data_on(tty);
 844   }
 845 
 846   StaticArchiveBuilder builder;
 847   preload_and_dump_impl(builder, THREAD);
 848   if (HAS_PENDING_EXCEPTION) {
 849     if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 850       aot_log_error(aot)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
 851                      "%zuM", MaxHeapSize/M);
 852       MetaspaceShared::writing_error();
 853     } else {
 854       oop message = java_lang_Throwable::message(PENDING_EXCEPTION);
 855       aot_log_error(aot)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
 856                          message == nullptr ? "(null)" : java_lang_String::as_utf8_string(message));
 857       MetaspaceShared::writing_error(err_msg("Unexpected exception, use -Xlog:aot%s,exceptions=trace for detail",
 858                                              CDSConfig::new_aot_flags_used() ? "" : ",cds"));
 859     }
 860   }
 861 
 862   if (CDSConfig::new_aot_flags_used()) {
 863     if (CDSConfig::is_dumping_preimage_static_archive()) {
 864       // We are in the JVM that runs the training run. Continue execution,
 865       // so that it can finish all clean-up and return the correct exit
 866       // code to the OS.
 867     } else {
 868       // The JLI launcher only recognizes the "old" -Xshare:dump flag.
 869       // When the new -XX:AOTMode=create flag is used, we can't return
 870       // to the JLI launcher, as the launcher will fail when trying to
 871       // run the main class, which is not what we want.
 872       struct stat st;
 873       if (os::stat(AOTCache, &st) != 0) {
 874         tty->print_cr("AOTCache creation failed: %s", AOTCache);

 875       } else {
 876         tty->print_cr("AOTCache creation is complete: %s " INT64_FORMAT " bytes", AOTCache, (int64_t)(st.st_size));

 877       }
 878       vm_direct_exit(0);
 879     }
 880   }
 881 }
 882 
 883 #if INCLUDE_CDS_JAVA_HEAP && defined(_LP64)
 884 void MetaspaceShared::adjust_heap_sizes_for_dumping() {
 885   if (!CDSConfig::is_dumping_heap() || UseCompressedOops) {
 886     return;
 887   }
 888   // CDS heap dumping requires all string oops to have an offset
 889   // from the heap bottom that can be encoded in 32-bit.
 890   julong max_heap_size = (julong)(4 * G);
 891 
 892   if (MinHeapSize > max_heap_size) {
 893     log_debug(aot)("Setting MinHeapSize to 4G for CDS dumping, original size = %zuM", MinHeapSize/M);
 894     FLAG_SET_ERGO(MinHeapSize, max_heap_size);
 895   }
 896   if (InitialHeapSize > max_heap_size) {
 897     log_debug(aot)("Setting InitialHeapSize to 4G for CDS dumping, original size = %zuM", InitialHeapSize/M);
 898     FLAG_SET_ERGO(InitialHeapSize, max_heap_size);

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

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

1111   }
1112 
1113   bool status = write_static_archive(&builder, mapinfo, heap_info);
1114   if (status && CDSConfig::is_dumping_preimage_static_archive()) {
1115     tty->print_cr("%s AOTConfiguration recorded: %s",
1116                   CDSConfig::has_temp_aot_config_file() ? "Temporary" : "", AOTConfiguration);
1117     if (CDSConfig::is_single_command_training()) {
1118       fork_and_dump_final_static_archive(CHECK);
1119     }
1120   }
1121 
1122   if (!status) {
1123     THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
1124   }
1125 }
1126 
1127 bool MetaspaceShared::write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info) {
1128   // relocate the data so that it can be mapped to MetaspaceShared::requested_base_address()
1129   // without runtime relocation.
1130   builder->relocate_to_requested();
1131 
1132   map_info->open_as_output();
1133   if (!map_info->is_open()) {

1296     if (HAS_PENDING_EXCEPTION) {
1297       ResourceMark rm(THREAD);
1298       aot_log_warning(aot)("Preload Warning: Verification failed for %s",
1299                     ik->external_name());
1300       CLEAR_PENDING_EXCEPTION;
1301       SystemDictionaryShared::set_class_has_failed_verification(ik);
1302     } else {
1303       assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1304       ik->compute_has_loops_flag_for_methods();
1305     }
1306     BytecodeVerificationLocal = saved;
1307     return true;
1308   } else {
1309     return false;
1310   }
1311 }
1312 
1313 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1314   if (CDSConfig::is_dumping_heap()) {
1315     HeapShared::write_heap(&_heap_info);
1316   } else if (!CDSConfig::is_dumping_preimage_static_archive()) {
1317     CDSConfig::log_reasons_for_not_dumping_heap();
1318   }
1319 }
1320 
1321 void MetaspaceShared::set_shared_metaspace_range(void* base, void *static_top, void* top) {
1322   assert(base <= static_top && static_top <= top, "must be");
1323   _shared_metaspace_static_top = static_top;
1324   MetaspaceObj::set_shared_metaspace_range(base, top);
1325 }
1326 
1327 bool MetaspaceShared::is_shared_dynamic(void* p) {
1328   if ((p < MetaspaceObj::shared_metaspace_top()) &&
1329       (p >= _shared_metaspace_static_top)) {
1330     return true;
1331   } else {
1332     return false;
1333   }
1334 }
1335 
1336 bool MetaspaceShared::is_shared_static(void* p) {

1354   } else if (CDSConfig::new_aot_flags_used()) {
1355     vm_exit_during_initialization("Unable to use AOT cache.", nullptr);
1356   } else {
1357     vm_exit_during_initialization("Unable to use shared archive.", nullptr);
1358   }
1359 }
1360 
1361 void MetaspaceShared::report_loading_error(const char* format, ...) {
1362   // When using AOT cache, errors messages are always printed on the error channel.
1363   LogStream ls_aot(LogLevel::Error, LogTagSetMapping<LOG_TAGS(aot)>::tagset());
1364 
1365   // If we are loading load the default CDS archive, it may fail due to incompatible VM options.
1366   // Print at the info level to avoid excessive verbosity.
1367   // However, if the user has specified a CDS archive (or AOT cache), they would be interested in
1368   // knowing that the loading fails, so we print at the error level.
1369   LogLevelType level = (!CDSConfig::is_using_archive() || CDSConfig::is_using_only_default_archive()) ?
1370                         LogLevel::Info : LogLevel::Error;
1371   LogStream ls_cds(level, LogTagSetMapping<LOG_TAGS(cds)>::tagset());
1372 
1373   LogStream& ls = CDSConfig::new_aot_flags_used() ? ls_aot : ls_cds;
1374   if (!ls.is_enabled()) {
1375     return;
1376   }
1377 
1378   va_list ap;
1379   va_start(ap, format);
1380 
1381   static bool printed_error = false;
1382   if (!printed_error) { // No need for locks. Loading error checks happen only in main thread.
1383     ls.print_cr("An error has occurred while processing the %s. Run with -Xlog:%s for details.",
1384                 CDSConfig::type_of_archive_being_loaded(), CDSConfig::new_aot_flags_used() ? "aot" : "aot,cds");
1385     printed_error = true;
1386   }
1387   ls.vprint_cr(format, ap);
1388 
1389   va_end(ap);
1390 }
1391 
1392 // This function is called when the JVM is unable to write the specified CDS archive due to an
1393 // unrecoverable error.
1394 void MetaspaceShared::unrecoverable_writing_error(const char* message) {
1395   writing_error(message);
1396   vm_direct_exit(1);
1397 }
1398 
1399 // This function is called when the JVM is unable to write the specified CDS archive due to a
1400 // an error. The error will be propagated
1401 void MetaspaceShared::writing_error(const char* message) {
1402   aot_log_error(aot)("An error has occurred while writing the shared archive file.");
1403   if (message != nullptr) {
1404     aot_log_error(aot)("%s", message);
1405   }
1406 }
1407 
1408 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
1409   assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
1410   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1411 
1412   FileMapInfo* static_mapinfo = FileMapInfo::current_info();
1413   FileMapInfo* dynamic_mapinfo = nullptr;
1414 
1415   if (static_mapinfo != nullptr) {
1416     aot_log_info(aot)("Core region alignment: %zu", static_mapinfo->core_region_alignment());
1417     dynamic_mapinfo = open_dynamic_archive();
1418 
1419     aot_log_info(aot)("ArchiveRelocationMode: %d", ArchiveRelocationMode);
1420 
1421     // First try to map at the requested address
1422     result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1423     if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1424       // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1425       // by the OS.
1426       aot_log_info(aot)("Try to map archive(s) at an alternative address");
1427       result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1428     }
1429   }
1430 
1431   if (result == MAP_ARCHIVE_SUCCESS) {
1432     bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());

1455     } else {
1456       if (RequireSharedSpaces) {
1457         MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1458       } else {
1459         report_loading_error("Unable to map shared spaces");
1460       }
1461     }
1462   }
1463 
1464   // If mapping failed and -XShare:on, the vm should exit
1465   bool has_failed = false;
1466   if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1467     has_failed = true;
1468     delete static_mapinfo;
1469   }
1470   if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1471     has_failed = true;
1472     delete dynamic_mapinfo;
1473   }
1474   if (RequireSharedSpaces && has_failed) {
1475     // static archive mapped but dynamic archive failed
1476       MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1477   }
1478 }
1479 
1480 // This is called very early at VM start up to get the size of the cached_code region
1481 void MetaspaceShared::open_static_archive() {
1482   if (!UseSharedSpaces) { // FIXME -- is this still needed??
1483     return;
1484   }
1485   const char* static_archive = CDSConfig::input_static_archive_path();
1486   assert(static_archive != nullptr, "sanity");
1487   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1488   if (!mapinfo->open_as_input()) {
1489     delete(mapinfo);
1490   } else {
1491     FileMapRegion* r = mapinfo->region_at(MetaspaceShared::ac);
1492     AOTCacheAccess::set_aot_code_region_size(r->used_aligned());
1493   }

1494 }
1495 
1496 FileMapInfo* MetaspaceShared::open_dynamic_archive() {
1497   if (CDSConfig::is_dumping_dynamic_archive()) {
1498     return nullptr;
1499   }
1500   const char* dynamic_archive = CDSConfig::input_dynamic_archive_path();
1501   if (dynamic_archive == nullptr) {
1502     return nullptr;
1503   }
1504 
1505   FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1506   if (!mapinfo->open_as_input()) {
1507     delete(mapinfo);
1508     if (RequireSharedSpaces) {
1509       MetaspaceShared::unrecoverable_loading_error("Failed to initialize dynamic archive");
1510     }
1511     return nullptr;
1512   }
1513   return mapinfo;

1959       MemoryReserver::release(archive_space_rs);
1960       archive_space_rs = {};
1961     }
1962     if (class_space_rs.is_reserved()) {
1963       aot_log_debug(aot)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
1964       MemoryReserver::release(class_space_rs);
1965       class_space_rs = {};
1966     }
1967   }
1968 }
1969 
1970 static int archive_regions[]     = { MetaspaceShared::rw, MetaspaceShared::ro };
1971 static int archive_regions_count = 2;
1972 
1973 MapArchiveResult MetaspaceShared::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
1974   assert(CDSConfig::is_using_archive(), "must be runtime");
1975   if (mapinfo == nullptr) {
1976     return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
1977   }
1978 
1979   if (!mapinfo->validate_aot_class_linking()) {
1980     return MAP_ARCHIVE_OTHER_FAILURE;
1981   }
1982 
1983   mapinfo->set_is_mapped(false);
1984   if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
1985     report_loading_error("Unable to map CDS archive -- core_region_alignment() expected: %zu"
1986                          " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment());
1987     return MAP_ARCHIVE_OTHER_FAILURE;
1988   }
1989 
1990   MapArchiveResult result =
1991     mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
1992 
1993   if (result != MAP_ARCHIVE_SUCCESS) {
1994     unmap_archive(mapinfo);
1995     return result;
1996   }
1997 
1998   if (!mapinfo->validate_class_location()) {
1999     unmap_archive(mapinfo);
2000     return MAP_ARCHIVE_OTHER_FAILURE;
2001   }
2002 

2094 
2095   if (PrintSharedArchiveAndExit) {
2096     // Print archive names
2097     if (dynamic_mapinfo != nullptr) {
2098       tty->print_cr("\n\nBase archive name: %s", CDSConfig::input_static_archive_path());
2099       tty->print_cr("Base archive version %d", static_mapinfo->version());
2100     } else {
2101       tty->print_cr("Static archive name: %s", static_mapinfo->full_path());
2102       tty->print_cr("Static archive version %d", static_mapinfo->version());
2103     }
2104 
2105     SystemDictionaryShared::print_shared_archive(tty);
2106     if (dynamic_mapinfo != nullptr) {
2107       tty->print_cr("\n\nDynamic archive name: %s", dynamic_mapinfo->full_path());
2108       tty->print_cr("Dynamic archive version %d", dynamic_mapinfo->version());
2109       SystemDictionaryShared::print_shared_archive(tty, false/*dynamic*/);
2110     }
2111 
2112     TrainingData::print_archived_training_data_on(tty);
2113 
2114     AOTCodeCache::print_on(tty);



2115 
2116     // collect shared symbols and strings
2117     CountSharedSymbols cl;
2118     SymbolTable::shared_symbols_do(&cl);
2119     tty->print_cr("Number of shared symbols: %d", cl.total());
2120     tty->print_cr("Number of shared strings: %zu", StringTable::shared_entry_count());
2121     tty->print_cr("VM version: %s\r\n", static_mapinfo->vm_version());
2122     if (FileMapInfo::current_info() == nullptr || _archive_loading_failed) {
2123       tty->print_cr("archive is invalid");
2124       vm_exit(1);
2125     } else {
2126       tty->print_cr("archive is valid");
2127       vm_exit(0);
2128     }
2129   }
2130 }
2131 
2132 // JVM/TI RedefineClasses() support:
2133 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
2134   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
< prev index next >