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

 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,

 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) {

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 

   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,

 744       _mirrors.at(i).release(Universe::vm_global());
 745     }
 746   }
 747 
 748   void do_cld(ClassLoaderData* cld) {
 749     assert(cld->is_alive(), "must be");
 750   }
 751 
 752   void do_klass(Klass* k) {
 753     if (k->is_instance_klass()) {
 754       _mirrors.append(OopHandle(Universe::vm_global(), k->java_mirror()));
 755     }
 756   }
 757 
 758   const GrowableArray<OopHandle>* mirrors() const { return &_mirrors; }
 759 };
 760 
 761 // Check if we can eagerly link this class at dump time, so we can avoid the
 762 // runtime linking overhead (especially verification)
 763 bool MetaspaceShared::may_be_eagerly_linked(InstanceKlass* ik) {
 764   if (CDSConfig::preserve_all_dumptime_verification_states(ik)) {
 765     assert(ik->can_be_verified_at_dumptime(), "sanity");
 766   }
 767   if (!ik->can_be_verified_at_dumptime()) {
 768     // For old classes, try to leave them in the unlinked state, so
 769     // we can still store them in the archive. They must be
 770     // linked/verified at runtime.
 771     return false;
 772   }
 773   if (CDSConfig::is_dumping_dynamic_archive() && ik->defined_by_other_loaders()) {
 774     // Linking of unregistered classes at this stage may cause more
 775     // classes to be resolved, resulting in calls to ClassLoader.loadClass()
 776     // that may not be expected by custom class loaders.
 777     //
 778     // It's OK to do this for the built-in loaders as we know they can
 779     // tolerate this.
 780     return false;
 781   }
 782   return true;
 783 }
 784 
 785 void MetaspaceShared::link_shared_classes(TRAPS) {
 786   AOTClassLinker::initialize();
 787   AOTClassInitializer::init_test_class(CHECK);
 788 
 789   if (CDSConfig::is_dumping_final_static_archive()) {
 790     FinalImageRecipes::apply_recipes(CHECK);
 791   }
 792 
 793   while (true) {
 794     ResourceMark rm(THREAD);
 795     CollectClassesForLinking collect_classes;
 796     bool has_linked = false;
 797     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 798     for (int i = 0; i < mirrors->length(); i++) {
 799       OopHandle mirror = mirrors->at(i);
 800       InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
 801       if (may_be_eagerly_linked(ik)) {
 802         has_linked |= try_link_class(THREAD, ik);
 803       }
 804       if (CDSConfig::is_dumping_heap() && ik->is_linked() && !ik->is_initialized()) {
 805         AOTClassInitializer::maybe_preinit_class(ik, CHECK);
 806       }
 807     }
 808 
 809     if (!has_linked) {
 810       break;
 811     }
 812     // Class linking includes verification which may load more classes.
 813     // Keep scanning until we have linked no more classes.
 814   }
 815 
 816   // Eargerly resolve all string constants in constant pools
 817   {
 818     ResourceMark rm(THREAD);
 819     CollectClassesForLinking collect_classes;
 820     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 821     for (int i = 0; i < mirrors->length(); i++) {
 822       OopHandle mirror = mirrors->at(i);
 823       InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
 824       AOTConstantPoolResolver::preresolve_string_cp_entries(ik, CHECK);
 825       if (CDSConfig::is_dumping_preimage_static_archive()) {
 826         FinalImageRecipes::add_reflection_data_flags(ik, CHECK);
 827       }
 828     }
 829   }




 830 }
 831 
 832 // Preload classes from a list, populate the shared spaces and dump to a
 833 // file.
 834 void MetaspaceShared::preload_and_dump(TRAPS) {
 835   CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
 836   ResourceMark rm(THREAD);
 837   HandleMark hm(THREAD);
 838 
 839   if (CDSConfig::is_dumping_final_static_archive() && AOTPrintTrainingInfo) {
 840     tty->print_cr("==================== archived_training_data ** before dumping ====================");
 841     TrainingData::print_archived_training_data_on(tty);
 842   }
 843 
 844   StaticArchiveBuilder builder;
 845   preload_and_dump_impl(builder, THREAD);
 846   if (HAS_PENDING_EXCEPTION) {
 847     if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 848       aot_log_error(aot)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
 849                      "%zuM", MaxHeapSize/M);
 850       MetaspaceShared::writing_error();
 851     } else {
 852       oop message = java_lang_Throwable::message(PENDING_EXCEPTION);
 853       aot_log_error(aot)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
 854                          message == nullptr ? "(null)" : java_lang_String::as_utf8_string(message));
 855       MetaspaceShared::writing_error(err_msg("Unexpected exception, use -Xlog:aot%s,exceptions=trace for detail",
 856                                              CDSConfig::new_aot_flags_used() ? "" : ",cds"));
 857     }
 858   }
 859 
 860   if (CDSConfig::new_aot_flags_used()) {
 861     if (CDSConfig::is_dumping_preimage_static_archive()) {
 862       // We are in the JVM that runs the training run. Continue execution,

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

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

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

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

1384 // This function is called when the JVM is unable to write the specified CDS archive due to an
1385 // unrecoverable error.
1386 void MetaspaceShared::unrecoverable_writing_error(const char* message) {
1387   writing_error(message);
1388   vm_direct_exit(1);
1389 }
1390 
1391 // This function is called when the JVM is unable to write the specified CDS archive due to a
1392 // an error. The error will be propagated
1393 void MetaspaceShared::writing_error(const char* message) {
1394   aot_log_error(aot)("An error has occurred while writing the shared archive file.");
1395   if (message != nullptr) {
1396     aot_log_error(aot)("%s", message);
1397   }
1398 }
1399 
1400 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
1401   assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
1402   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1403 
1404   FileMapInfo* static_mapinfo = FileMapInfo::current_info();
1405   FileMapInfo* dynamic_mapinfo = nullptr;
1406 
1407   if (static_mapinfo != nullptr) {
1408     aot_log_info(aot)("Core region alignment: %zu", static_mapinfo->core_region_alignment());
1409     dynamic_mapinfo = open_dynamic_archive();
1410 
1411     aot_log_info(aot)("ArchiveRelocationMode: %d", ArchiveRelocationMode);
1412 
1413     // First try to map at the requested address
1414     result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1415     if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1416       // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1417       // by the OS.
1418       aot_log_info(aot)("Try to map archive(s) at an alternative address");
1419       result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1420     }
1421   }
1422 
1423   if (result == MAP_ARCHIVE_SUCCESS) {
1424     bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());

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

1486 }
1487 
1488 FileMapInfo* MetaspaceShared::open_dynamic_archive() {
1489   if (CDSConfig::is_dumping_dynamic_archive()) {
1490     return nullptr;
1491   }
1492   const char* dynamic_archive = CDSConfig::input_dynamic_archive_path();
1493   if (dynamic_archive == nullptr) {
1494     return nullptr;
1495   }
1496 
1497   FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1498   if (!mapinfo->open_as_input()) {
1499     delete(mapinfo);
1500     if (RequireSharedSpaces) {
1501       MetaspaceShared::unrecoverable_loading_error("Failed to initialize dynamic archive");
1502     }
1503     return nullptr;
1504   }
1505   return mapinfo;

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