< prev index next >

src/hotspot/share/cds/aotMetaspace.cpp

Print this page

   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "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/aotMapLogger.hpp"
  33 #include "cds/aotMappedHeapLoader.hpp"
  34 #include "cds/aotMetaspace.hpp"
  35 #include "cds/aotReferenceObjSupport.hpp"
  36 #include "cds/archiveBuilder.hpp"
  37 #include "cds/cds_globals.hpp"
  38 #include "cds/cdsConfig.hpp"
  39 #include "cds/cdsProtectionDomain.hpp"
  40 #include "cds/classListParser.hpp"
  41 #include "cds/classListWriter.hpp"
  42 #include "cds/cppVtables.hpp"
  43 #include "cds/dumpAllocStats.hpp"
  44 #include "cds/dynamicArchive.hpp"
  45 #include "cds/filemap.hpp"
  46 #include "cds/finalImageRecipes.hpp"
  47 #include "cds/heapShared.inline.hpp"
  48 #include "cds/lambdaFormInvokers.hpp"
  49 #include "cds/lambdaProxyClassDictionary.hpp"
  50 #include "classfile/classLoaderDataGraph.hpp"
  51 #include "classfile/classLoaderDataShared.hpp"
  52 #include "classfile/javaClasses.inline.hpp"
  53 #include "classfile/loaderConstraints.hpp"
  54 #include "classfile/modules.hpp"
  55 #include "classfile/placeholders.hpp"
  56 #include "classfile/stringTable.hpp"
  57 #include "classfile/symbolTable.hpp"
  58 #include "classfile/systemDictionary.hpp"
  59 #include "classfile/systemDictionaryShared.hpp"
  60 #include "classfile/vmClasses.hpp"
  61 #include "classfile/vmSymbols.hpp"
  62 #include "code/aotCodeCache.hpp"
  63 #include "code/codeCache.hpp"


  64 #include "gc/shared/gcVMOperations.hpp"
  65 #include "interpreter/bytecodes.hpp"
  66 #include "interpreter/bytecodeStream.hpp"
  67 #include "jvm_io.h"
  68 #include "logging/log.hpp"
  69 #include "logging/logMessage.hpp"
  70 #include "logging/logStream.hpp"
  71 #include "memory/memoryReserver.hpp"
  72 #include "memory/metaspace.hpp"
  73 #include "memory/metaspaceClosure.hpp"
  74 #include "memory/oopFactory.hpp"
  75 #include "memory/resourceArea.hpp"
  76 #include "memory/universe.hpp"
  77 #include "nmt/memTracker.hpp"
  78 #include "oops/compressedKlass.hpp"
  79 #include "oops/constantPool.inline.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/resolvedFieldEntry.hpp"
  86 #include "oops/trainingData.hpp"
  87 #include "prims/jvmtiExport.hpp"

  88 #include "runtime/arguments.hpp"
  89 #include "runtime/globals.hpp"
  90 #include "runtime/globals_extension.hpp"
  91 #include "runtime/handles.inline.hpp"
  92 #include "runtime/javaCalls.hpp"
  93 #include "runtime/os.inline.hpp"
  94 #include "runtime/safepointVerifiers.hpp"
  95 #include "runtime/sharedRuntime.hpp"
  96 #include "runtime/vmOperations.hpp"
  97 #include "runtime/vmThread.hpp"
  98 #include "sanitizers/leak.hpp"

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


 117 
 118 // The CDS archive is divided into the following regions:
 119 //     rw  - read-write metadata
 120 //     ro  - read-only metadata and read-only tables
 121 //     hp  - heap region
 122 //     bm  - bitmap for relocating the above 7 regions.
 123 //
 124 // The rw and ro regions are linearly allocated, in the order of rw->ro.
 125 // These regions are aligned with AOTMetaspace::core_region_alignment().
 126 //
 127 // These 2 regions are populated in the following steps:
 128 // [0] All classes are loaded in AOTMetaspace::load_classes(). All metadata are
 129 //     temporarily allocated outside of the shared regions.
 130 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
 131 // [2] C++ vtables are copied into the rw region.
 132 // [3] ArchiveBuilder copies RW metadata into the rw region.
 133 // [4] ArchiveBuilder copies RO metadata into the ro region.
 134 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
 135 //     are copied into the ro region as read-only tables.
 136 //

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
















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

 860     //
 861     // It's OK to do this for the built-in loaders as we know they can
 862     // tolerate this.
 863     return false;
 864   }
 865   return true;
 866 }
 867 
 868 void AOTMetaspace::link_all_loaded_classes(JavaThread* current) {
 869   while (true) {
 870     ResourceMark rm(current);
 871     CollectClassesForLinking collect_classes;
 872     bool has_linked = false;
 873     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 874     for (int i = 0; i < mirrors->length(); i++) {
 875       OopHandle mirror = mirrors->at(i);
 876       InstanceKlass* ik = java_lang_Class::as_InstanceKlass(mirror.resolve());
 877       if (may_be_eagerly_linked(ik)) {
 878         has_linked |= try_link_class(current, ik);
 879       }



 880     }
 881 
 882     if (!has_linked) {
 883       break;
 884     }
 885     // Class linking includes verification which may load more classes.
 886     // Keep scanning until we have linked no more classes.
 887   }
 888 }
 889 
 890 void AOTMetaspace::link_shared_classes(TRAPS) {
 891   AOTClassLinker::initialize();
 892   AOTClassInitializer::init_test_class(CHECK);
 893 
 894   if (CDSConfig::is_dumping_final_static_archive()) {
 895     // - Load and link all classes used in the training run.
 896     // - Initialize @AOTSafeClassInitializer classes that were
 897     //   initialized in the training run.
 898     // - Perform per-class optimization such as AOT-resolution of
 899     //   constant pool entries that were resolved during the training run.

 902     // Because the AOT assembly phase does not run the same exact code as in the
 903     // training run (e.g., we use different lambda form invoker classes;
 904     // generated lambda form classes are not recorded in FinalImageRecipes),
 905     // the recipes do not cover all classes that have been loaded so far. As
 906     // a result, we might have some unlinked classes at this point. Since we
 907     // require cached classes to be linked, all such classes will be linked
 908     // by the following step.
 909   }
 910 
 911   link_all_loaded_classes(THREAD);
 912 
 913   // Eargerly resolve all string constants in constant pools
 914   {
 915     ResourceMark rm(THREAD);
 916     CollectClassesForLinking collect_classes;
 917     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 918     for (int i = 0; i < mirrors->length(); i++) {
 919       OopHandle mirror = mirrors->at(i);
 920       InstanceKlass* ik = java_lang_Class::as_InstanceKlass(mirror.resolve());
 921       AOTConstantPoolResolver::preresolve_string_cp_entries(ik, CHECK);



 922     }
 923   }
 924 }
 925 
 926 void AOTMetaspace::dump_static_archive(TRAPS) {
 927   CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
 928   ResourceMark rm(THREAD);
 929   HandleMark hm(THREAD);
 930 
 931  if (CDSConfig::is_dumping_final_static_archive() && AOTPrintTrainingInfo) {
 932    tty->print_cr("==================== archived_training_data ** before dumping ====================");
 933    TrainingData::print_archived_training_data_on(tty);
 934  }
 935 
 936   StaticArchiveBuilder builder;
 937   dump_static_archive_impl(builder, THREAD);
 938   if (HAS_PENDING_EXCEPTION) {
 939     if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 940       aot_log_error(aot)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
 941                      "%zuM", MaxHeapSize/M);
 942       AOTMetaspace::writing_error();
 943     } else {
 944       oop message = java_lang_Throwable::message(PENDING_EXCEPTION);
 945       aot_log_error(aot)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
 946                          message == nullptr ? "(null)" : java_lang_String::as_utf8_string(message));
 947       AOTMetaspace::writing_error(err_msg("Unexpected exception, use -Xlog:aot%s,exceptions=trace for detail",
 948                                              CDSConfig::new_aot_flags_used() ? "" : ",cds"));
 949     }
 950   }
 951 
 952   if (CDSConfig::new_aot_flags_used()) {
 953     if (CDSConfig::is_dumping_preimage_static_archive()) {
 954       // We are in the JVM that runs the training run. Continue execution,

1027     }
1028   }
1029 
1030   // Some classes are used at CDS runtime but are not yet loaded at this point.
1031   // We can perform dummmy calls to these classes at dumptime to ensure they
1032   // are archived.
1033   exercise_runtime_cds_code(CHECK);
1034 
1035   aot_log_info(aot)("Loading classes to share: done.");
1036 }
1037 
1038 void AOTMetaspace::exercise_runtime_cds_code(TRAPS) {
1039   // Exercise the manifest processing code
1040   const char* dummy = "Manifest-Version: 1.0\n";
1041   CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
1042 
1043   // Exercise FileSystem and URL code
1044   CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
1045 }
1046 


















1047 void AOTMetaspace::dump_static_archive_impl(StaticArchiveBuilder& builder, TRAPS) {







1048   if (CDSConfig::is_dumping_classic_static_archive()) {
1049     // We are running with -Xshare:dump
1050     load_classes(CHECK);
1051 
1052     if (SharedArchiveConfigFile) {
1053       log_info(aot)("Reading extra data from %s ...", SharedArchiveConfigFile);
1054       read_extra_data(THREAD, SharedArchiveConfigFile);
1055       log_info(aot)("Reading extra data: done.");
1056     }
1057   }
1058 
1059   if (CDSConfig::is_dumping_preimage_static_archive()) {
1060     log_info(aot)("Reading lambda form invokers from JDK default classlist ...");
1061     char default_classlist[JVM_MAXPATHLEN];
1062     get_default_classlist(default_classlist, JVM_MAXPATHLEN);
1063     struct stat statbuf;
1064     if (os::stat(default_classlist, &statbuf) == 0) {
1065       ClassListParser::parse_classlist(default_classlist,
1066                                        ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
1067     }

1093   // Link any classes which got missed. This would happen if we have loaded classes that
1094   // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1095   // fails verification, all other interfaces that were not specified in the classlist but
1096   // are implemented by K are not verified.
1097   link_shared_classes(CHECK);
1098   log_info(aot)("Rewriting and linking classes: done");
1099   TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
1100 
1101   if (CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
1102     LambdaFormInvokers::regenerate_holder_classes(CHECK);
1103   }
1104 
1105 #if INCLUDE_CDS_JAVA_HEAP
1106   if (CDSConfig::is_dumping_heap()) {
1107     HeapShared::init_heap_writer();
1108     if (CDSConfig::is_dumping_full_module_graph()) {
1109       ClassLoaderDataShared::ensure_module_entry_tables_exist();
1110       HeapShared::reset_archived_object_states(CHECK);
1111     }
1112 




1113     AOTReferenceObjSupport::initialize(CHECK);
1114     AOTReferenceObjSupport::stabilize_cached_reference_objects(CHECK);
1115 
1116     if (CDSConfig::is_initing_classes_at_dump_time()) {
1117       // java.lang.Class::reflectionFactory cannot be archived yet. We set this field
1118       // to null, and it will be initialized again at runtime.
1119       log_debug(aot)("Resetting Class::reflectionFactory");
1120       TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates");
1121       Symbol* method_sig = vmSymbols::void_method_signature();
1122       JavaValue result(T_VOID);
1123       JavaCalls::call_static(&result, vmClasses::Class_klass(),
1124                              method_name, method_sig, CHECK);
1125 
1126       // Perhaps there is a way to avoid hard-coding these names here.
1127       // See discussion in JDK-8342481.
1128     }
1129 
1130     if (HeapShared::is_writing_mapping_mode()) {
1131       // Do this at the very end, when no Java code will be executed. Otherwise
1132       // some new strings may be added to the intern table.
1133       StringTable::allocate_shared_strings_array(CHECK);
1134     }
1135   } else {
1136     log_info(aot)("Not dumping heap, reset CDSConfig::_is_using_optimized_module_handling");
1137     CDSConfig::stop_using_optimized_module_handling();
1138   }
1139 #endif
1140 
1141   VM_PopulateDumpSharedSpace op(builder);
1142   VMThread::execute(&op);
1143 
1144   if (AOTCodeCache::is_on_for_dump() && CDSConfig::is_dumping_final_static_archive()) {
1145     CDSConfig::enable_dumping_aot_code();
1146     {
1147       builder.start_ac_region();
1148       // Write the contents to AOT code region and close AOTCodeCache before packing the region
1149       AOTCodeCache::close();
1150       builder.end_ac_region();















1151     }
1152     CDSConfig::disable_dumping_aot_code();
1153   }
1154 
1155   bool status = write_static_archive(&builder, op.map_info(), op.mapped_heap_info(), op.streamed_heap_info());
1156   if (status && CDSConfig::is_dumping_preimage_static_archive()) {
1157     tty->print_cr("%s AOTConfiguration recorded: %s",
1158                   CDSConfig::has_temp_aot_config_file() ? "Temporary" : "", AOTConfiguration);
1159     if (CDSConfig::is_single_command_training()) {
1160       fork_and_dump_final_static_archive(CHECK);
1161     }
1162   }
1163 
1164   if (!status) {
1165     THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
1166   }
1167 }
1168 
1169 bool AOTMetaspace::write_static_archive(ArchiveBuilder* builder,
1170                                         FileMapInfo* map_info,
1171                                         ArchiveMappedHeapInfo* mapped_heap_info,
1172                                         ArchiveStreamedHeapInfo* streamed_heap_info) {

1336     if (HAS_PENDING_EXCEPTION) {
1337       ResourceMark rm(THREAD);
1338       aot_log_warning(aot)("Preload Warning: Verification failed for %s",
1339                     ik->external_name());
1340       CLEAR_PENDING_EXCEPTION;
1341       SystemDictionaryShared::set_class_has_failed_verification(ik);
1342     } else {
1343       assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1344       ik->compute_has_loops_flag_for_methods();
1345     }
1346     BytecodeVerificationLocal = saved;
1347     return true;
1348   } else {
1349     return false;
1350   }
1351 }
1352 
1353 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1354   if (CDSConfig::is_dumping_heap()) {
1355     HeapShared::write_heap(&_mapped_heap_info, &_streamed_heap_info);
1356   } else {
1357     CDSConfig::log_reasons_for_not_dumping_heap();
1358   }
1359 }
1360 
1361 void AOTMetaspace::set_aot_metaspace_range(void* base, void *static_top, void* top) {
1362   assert(base <= static_top && static_top <= top, "must be");
1363   _aot_metaspace_static_top = static_top;
1364   MetaspaceObj::set_aot_metaspace_range(base, top);
1365 }
1366 
1367 bool AOTMetaspace::in_aot_cache_dynamic_region(void* p) {
1368   if ((p < MetaspaceObj::aot_metaspace_top()) &&
1369       (p >= _aot_metaspace_static_top)) {
1370     return true;
1371   } else {
1372     return false;
1373   }
1374 }
1375 
1376 bool AOTMetaspace::in_aot_cache_static_region(void* p) {

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

1495     } else {
1496       if (RequireSharedSpaces) {
1497         AOTMetaspace::unrecoverable_loading_error("Unable to map shared spaces");
1498       } else {
1499         report_loading_error("Unable to map shared spaces");
1500       }
1501     }
1502   }
1503 
1504   // If mapping failed and -XShare:on, the vm should exit
1505   bool has_failed = false;
1506   if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1507     has_failed = true;
1508     delete static_mapinfo;
1509   }
1510   if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1511     has_failed = true;
1512     delete dynamic_mapinfo;
1513   }
1514   if (RequireSharedSpaces && has_failed) {

1515       AOTMetaspace::unrecoverable_loading_error("Unable to map shared spaces");
1516   }
1517 }
1518 
1519 FileMapInfo* AOTMetaspace::open_static_archive() {




1520   const char* static_archive = CDSConfig::input_static_archive_path();
1521   assert(static_archive != nullptr, "sanity");
1522   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1523   if (!mapinfo->open_as_input()) {
1524     delete(mapinfo);
1525     log_info(cds)("Opening of static archive %s failed", static_archive);
1526     return nullptr;


1527   }
1528   return mapinfo;
1529 }
1530 
1531 FileMapInfo* AOTMetaspace::open_dynamic_archive() {
1532   if (CDSConfig::is_dumping_dynamic_archive()) {
1533     return nullptr;
1534   }
1535   const char* dynamic_archive = CDSConfig::input_dynamic_archive_path();
1536   if (dynamic_archive == nullptr) {
1537     return nullptr;
1538   }
1539 
1540   FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1541   if (!mapinfo->open_as_input()) {
1542     delete(mapinfo);
1543     if (RequireSharedSpaces) {
1544       AOTMetaspace::unrecoverable_loading_error("Failed to initialize dynamic archive");
1545     }
1546     return nullptr;
1547   }
1548   return mapinfo;

2019       MemoryReserver::release(archive_space_rs);
2020       archive_space_rs = {};
2021     }
2022     if (class_space_rs.is_reserved()) {
2023       aot_log_debug(aot)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
2024       MemoryReserver::release(class_space_rs);
2025       class_space_rs = {};
2026     }
2027   }
2028 }
2029 
2030 static int archive_regions[]     = { AOTMetaspace::rw, AOTMetaspace::ro };
2031 static int archive_regions_count = 2;
2032 
2033 MapArchiveResult AOTMetaspace::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
2034   assert(CDSConfig::is_using_archive(), "must be runtime");
2035   if (mapinfo == nullptr) {
2036     return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
2037   }
2038 




2039   mapinfo->set_is_mapped(false);
2040   if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
2041     report_loading_error("Unable to map CDS archive -- core_region_alignment() expected: %zu"
2042                          " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment());
2043     return MAP_ARCHIVE_OTHER_FAILURE;
2044   }
2045 
2046   MapArchiveResult result =
2047     mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
2048 
2049   if (result != MAP_ARCHIVE_SUCCESS) {
2050     unmap_archive(mapinfo);
2051     return result;
2052   }
2053 
2054   if (!mapinfo->validate_class_location()) {
2055     unmap_archive(mapinfo);
2056     return MAP_ARCHIVE_OTHER_FAILURE;
2057   }
2058 

   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/aotCacheAccess.hpp"
  27 #include "cds/aotClassInitializer.hpp"
  28 #include "cds/aotClassLinker.hpp"
  29 #include "cds/aotClassLocation.hpp"
  30 #include "cds/aotConstantPoolResolver.hpp"
  31 #include "cds/aotLinkedClassBulkLoader.hpp"
  32 #include "cds/aotLogging.hpp"
  33 #include "cds/aotMapLogger.hpp"
  34 #include "cds/aotMappedHeapLoader.hpp"
  35 #include "cds/aotMetaspace.hpp"
  36 #include "cds/aotReferenceObjSupport.hpp"
  37 #include "cds/archiveBuilder.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.inline.hpp"
  49 #include "cds/lambdaFormInvokers.hpp"
  50 #include "cds/lambdaProxyClassDictionary.hpp"
  51 #include "classfile/classLoaderDataGraph.hpp"
  52 #include "classfile/classLoaderDataShared.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 "compiler/compileBroker.hpp"
  66 #include "compiler/precompiler.hpp"
  67 #include "gc/shared/gcVMOperations.hpp"
  68 #include "interpreter/bytecodes.hpp"
  69 #include "interpreter/bytecodeStream.hpp"
  70 #include "jvm_io.h"
  71 #include "logging/log.hpp"
  72 #include "logging/logMessage.hpp"
  73 #include "logging/logStream.hpp"
  74 #include "memory/memoryReserver.hpp"
  75 #include "memory/metaspace.hpp"
  76 #include "memory/metaspaceClosure.hpp"
  77 #include "memory/oopFactory.hpp"
  78 #include "memory/resourceArea.hpp"
  79 #include "memory/universe.hpp"
  80 #include "nmt/memTracker.hpp"
  81 #include "oops/compressedKlass.hpp"
  82 #include "oops/constantPool.inline.hpp"
  83 #include "oops/instanceMirrorKlass.hpp"
  84 #include "oops/klass.inline.hpp"
  85 #include "oops/method.inline.hpp"
  86 #include "oops/objArrayOop.hpp"
  87 #include "oops/oop.inline.hpp"
  88 #include "oops/oopHandle.hpp"
  89 #include "oops/resolvedFieldEntry.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/hashTable.hpp"
 109 #include "utilities/macros.hpp"
 110 #include "utilities/ostream.hpp"
 111 
 112 #include <sys/stat.h>
 113 
 114 ReservedSpace AOTMetaspace::_symbol_rs;
 115 VirtualSpace AOTMetaspace::_symbol_vs;
 116 bool AOTMetaspace::_archive_loading_failed = false;
 117 bool AOTMetaspace::_remapped_readwrite = false;
 118 void* AOTMetaspace::_aot_metaspace_static_top = nullptr;
 119 intx AOTMetaspace::_relocation_delta;
 120 char* AOTMetaspace::_requested_base_address;
 121 Array<Method*>* AOTMetaspace::_archived_method_handle_intrinsics = nullptr;
 122 bool AOTMetaspace::_use_optimized_module_handling = true;
 123 int volatile AOTMetaspace::_preimage_static_archive_dumped = 0;
 124 jlong AOTMetaspace::_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 AOTMetaspace::core_region_alignment().
 134 //
 135 // These 2 regions are populated in the following steps:
 136 // [0] All classes are loaded in AOTMetaspace::load_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 AOTMetaspace::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 (!((UseEpsilonGC || UseG1GC || UseParallelGC || UseSerialGC || UseShenandoahGC || UseZGC) && 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 "
 317           "Epsilon, G1, Parallel, Serial, Shenandoah, or ZGC");
 318     }
 319   }
 320 
 321   aot_log_info(aot)("Core region alignment: %zu", core_region_alignment());
 322   // The max allowed size for CDS archive. We use this to limit SharedBaseAddress
 323   // to avoid address space wrap around.
 324   size_t cds_max;
 325   const size_t reserve_alignment = core_region_alignment();
 326 
 327 #ifdef _LP64
 328   const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
 329   cds_max = align_down(UnscaledClassSpaceMax, reserve_alignment);
 330 #else
 331   // We don't support archives larger than 256MB on 32-bit due to limited
 332   //  virtual address space.
 333   cds_max = align_down(256*M, reserve_alignment);
 334 #endif
 335 
 336   _requested_base_address = compute_shared_base(cds_max);
 337   SharedBaseAddress = (size_t)_requested_base_address;
 338 
 339   size_t symbol_rs_size = LP64_ONLY(3 * G) NOT_LP64(128 * M);
 340   _symbol_rs = MemoryReserver::reserve(symbol_rs_size,

 884     //
 885     // It's OK to do this for the built-in loaders as we know they can
 886     // tolerate this.
 887     return false;
 888   }
 889   return true;
 890 }
 891 
 892 void AOTMetaspace::link_all_loaded_classes(JavaThread* current) {
 893   while (true) {
 894     ResourceMark rm(current);
 895     CollectClassesForLinking collect_classes;
 896     bool has_linked = false;
 897     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 898     for (int i = 0; i < mirrors->length(); i++) {
 899       OopHandle mirror = mirrors->at(i);
 900       InstanceKlass* ik = java_lang_Class::as_InstanceKlass(mirror.resolve());
 901       if (may_be_eagerly_linked(ik)) {
 902         has_linked |= try_link_class(current, ik);
 903       }
 904       if (CDSConfig::is_dumping_heap() && ik->is_linked() && !ik->is_initialized()) {
 905         AOTClassInitializer::maybe_preinit_class(ik, current);
 906       }
 907     }
 908 
 909     if (!has_linked) {
 910       break;
 911     }
 912     // Class linking includes verification which may load more classes.
 913     // Keep scanning until we have linked no more classes.
 914   }
 915 }
 916 
 917 void AOTMetaspace::link_shared_classes(TRAPS) {
 918   AOTClassLinker::initialize();
 919   AOTClassInitializer::init_test_class(CHECK);
 920 
 921   if (CDSConfig::is_dumping_final_static_archive()) {
 922     // - Load and link all classes used in the training run.
 923     // - Initialize @AOTSafeClassInitializer classes that were
 924     //   initialized in the training run.
 925     // - Perform per-class optimization such as AOT-resolution of
 926     //   constant pool entries that were resolved during the training run.

 929     // Because the AOT assembly phase does not run the same exact code as in the
 930     // training run (e.g., we use different lambda form invoker classes;
 931     // generated lambda form classes are not recorded in FinalImageRecipes),
 932     // the recipes do not cover all classes that have been loaded so far. As
 933     // a result, we might have some unlinked classes at this point. Since we
 934     // require cached classes to be linked, all such classes will be linked
 935     // by the following step.
 936   }
 937 
 938   link_all_loaded_classes(THREAD);
 939 
 940   // Eargerly resolve all string constants in constant pools
 941   {
 942     ResourceMark rm(THREAD);
 943     CollectClassesForLinking collect_classes;
 944     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 945     for (int i = 0; i < mirrors->length(); i++) {
 946       OopHandle mirror = mirrors->at(i);
 947       InstanceKlass* ik = java_lang_Class::as_InstanceKlass(mirror.resolve());
 948       AOTConstantPoolResolver::preresolve_string_cp_entries(ik, CHECK);
 949       if (CDSConfig::is_dumping_preimage_static_archive()) {
 950         FinalImageRecipes::add_reflection_data_flags(ik, CHECK);
 951       }
 952     }
 953   }
 954 }
 955 
 956 void AOTMetaspace::dump_static_archive(TRAPS) {
 957   CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
 958   ResourceMark rm(THREAD);
 959   HandleMark hm(THREAD);
 960 
 961   if (CDSConfig::is_dumping_final_static_archive() && AOTPrintTrainingInfo) {
 962     tty->print_cr("==================== archived_training_data ** before dumping ====================");
 963     TrainingData::print_archived_training_data_on(tty);
 964   }
 965 
 966   StaticArchiveBuilder builder;
 967   dump_static_archive_impl(builder, THREAD);
 968   if (HAS_PENDING_EXCEPTION) {
 969     if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 970       aot_log_error(aot)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
 971                      "%zuM", MaxHeapSize/M);
 972       AOTMetaspace::writing_error();
 973     } else {
 974       oop message = java_lang_Throwable::message(PENDING_EXCEPTION);
 975       aot_log_error(aot)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
 976                          message == nullptr ? "(null)" : java_lang_String::as_utf8_string(message));
 977       AOTMetaspace::writing_error(err_msg("Unexpected exception, use -Xlog:aot%s,exceptions=trace for detail",
 978                                              CDSConfig::new_aot_flags_used() ? "" : ",cds"));
 979     }
 980   }
 981 
 982   if (CDSConfig::new_aot_flags_used()) {
 983     if (CDSConfig::is_dumping_preimage_static_archive()) {
 984       // We are in the JVM that runs the training run. Continue execution,

1057     }
1058   }
1059 
1060   // Some classes are used at CDS runtime but are not yet loaded at this point.
1061   // We can perform dummmy calls to these classes at dumptime to ensure they
1062   // are archived.
1063   exercise_runtime_cds_code(CHECK);
1064 
1065   aot_log_info(aot)("Loading classes to share: done.");
1066 }
1067 
1068 void AOTMetaspace::exercise_runtime_cds_code(TRAPS) {
1069   // Exercise the manifest processing code
1070   const char* dummy = "Manifest-Version: 1.0\n";
1071   CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
1072 
1073   // Exercise FileSystem and URL code
1074   CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
1075 }
1076 
1077 bool AOTMetaspace::is_recording_preimage_static_archive() {
1078   if (CDSConfig::is_dumping_preimage_static_archive()) {
1079       return _preimage_static_archive_dumped == 0;
1080   }
1081   return false;
1082 }
1083 
1084 jlong AOTMetaspace::get_preimage_static_archive_recording_duration() {
1085   if (CDSConfig::is_dumping_preimage_static_archive()) {
1086     if (_preimage_static_archive_recording_duration == 0) {
1087       // The recording has not yet finished so return the current elapsed time.
1088       return Management::ticks_to_ms(os::elapsed_counter());
1089     }
1090     return _preimage_static_archive_recording_duration;
1091   }
1092   return 0;
1093 }
1094 
1095 void AOTMetaspace::dump_static_archive_impl(StaticArchiveBuilder& builder, TRAPS) {
1096   if (CDSConfig::is_dumping_preimage_static_archive()) {
1097     if (AtomicAccess::cmpxchg(&_preimage_static_archive_dumped, 0, 1) != 0) {
1098       return;
1099     }
1100     _preimage_static_archive_recording_duration = Management::ticks_to_ms(os::elapsed_counter());
1101   }
1102 
1103   if (CDSConfig::is_dumping_classic_static_archive()) {
1104     // We are running with -Xshare:dump
1105     load_classes(CHECK);
1106 
1107     if (SharedArchiveConfigFile) {
1108       log_info(aot)("Reading extra data from %s ...", SharedArchiveConfigFile);
1109       read_extra_data(THREAD, SharedArchiveConfigFile);
1110       log_info(aot)("Reading extra data: done.");
1111     }
1112   }
1113 
1114   if (CDSConfig::is_dumping_preimage_static_archive()) {
1115     log_info(aot)("Reading lambda form invokers from JDK default classlist ...");
1116     char default_classlist[JVM_MAXPATHLEN];
1117     get_default_classlist(default_classlist, JVM_MAXPATHLEN);
1118     struct stat statbuf;
1119     if (os::stat(default_classlist, &statbuf) == 0) {
1120       ClassListParser::parse_classlist(default_classlist,
1121                                        ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
1122     }

1148   // Link any classes which got missed. This would happen if we have loaded classes that
1149   // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1150   // fails verification, all other interfaces that were not specified in the classlist but
1151   // are implemented by K are not verified.
1152   link_shared_classes(CHECK);
1153   log_info(aot)("Rewriting and linking classes: done");
1154   TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
1155 
1156   if (CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
1157     LambdaFormInvokers::regenerate_holder_classes(CHECK);
1158   }
1159 
1160 #if INCLUDE_CDS_JAVA_HEAP
1161   if (CDSConfig::is_dumping_heap()) {
1162     HeapShared::init_heap_writer();
1163     if (CDSConfig::is_dumping_full_module_graph()) {
1164       ClassLoaderDataShared::ensure_module_entry_tables_exist();
1165       HeapShared::reset_archived_object_states(CHECK);
1166     }
1167 
1168     if (ArchiveLoaderLookupCache) {
1169       SystemDictionaryShared::create_loader_positive_lookup_cache(CHECK);
1170     }
1171 
1172     AOTReferenceObjSupport::initialize(CHECK);
1173     AOTReferenceObjSupport::stabilize_cached_reference_objects(CHECK);
1174 
1175     if (CDSConfig::is_initing_classes_at_dump_time()) {
1176       // java.lang.Class::reflectionFactory cannot be archived yet. We set this field
1177       // to null, and it will be initialized again at runtime.
1178       log_debug(aot)("Resetting Class::reflectionFactory");
1179       TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates");
1180       Symbol* method_sig = vmSymbols::void_method_signature();
1181       JavaValue result(T_VOID);
1182       JavaCalls::call_static(&result, vmClasses::Class_klass(),
1183                              method_name, method_sig, CHECK);
1184 
1185       // Perhaps there is a way to avoid hard-coding these names here.
1186       // See discussion in JDK-8342481.
1187     }
1188 
1189     if (HeapShared::is_writing_mapping_mode()) {
1190       // Do this at the very end, when no Java code will be executed. Otherwise
1191       // some new strings may be added to the intern table.
1192       StringTable::allocate_shared_strings_array(CHECK);
1193     }
1194   } else {
1195     log_info(aot)("Not dumping heap, reset CDSConfig::_is_using_optimized_module_handling");
1196     CDSConfig::stop_using_optimized_module_handling();
1197   }
1198 #endif
1199 
1200   VM_PopulateDumpSharedSpace op(builder);
1201   VMThread::execute(&op);
1202 
1203   if (CDSConfig::is_dumping_final_static_archive()) {
1204     if (AOTCodeCache::is_caching_enabled()) {
1205       // We have just created the final image. Let's run the AOT compiler
1206       if (AOTPrintTrainingInfo) {
1207         tty->print_cr("==================== archived_training_data ** after dumping ====================");
1208         TrainingData::print_archived_training_data_on(tty);
1209       }
1210 
1211       {
1212         builder.start_ac_region();
1213         if (AOTCodeCache::is_dumping_code()) {
1214           CDSConfig::enable_dumping_aot_code();
1215           log_info(aot)("Compiling AOT code");
1216           Precompiler::compile_aot_code(&builder, CHECK);
1217           log_info(aot)("Finished compiling AOT code");
1218           CDSConfig::disable_dumping_aot_code();
1219         }
1220         // Write the contents to aot code region and close AOTCodeCache before packing the region
1221         AOTCodeCache::close();
1222         log_info(aot)("Dumped AOT code Cache");
1223         builder.end_ac_region();
1224       }
1225     }

1226   }
1227 
1228   bool status = write_static_archive(&builder, op.map_info(), op.mapped_heap_info(), op.streamed_heap_info());
1229   if (status && CDSConfig::is_dumping_preimage_static_archive()) {
1230     tty->print_cr("%s AOTConfiguration recorded: %s",
1231                   CDSConfig::has_temp_aot_config_file() ? "Temporary" : "", AOTConfiguration);
1232     if (CDSConfig::is_single_command_training()) {
1233       fork_and_dump_final_static_archive(CHECK);
1234     }
1235   }
1236 
1237   if (!status) {
1238     THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
1239   }
1240 }
1241 
1242 bool AOTMetaspace::write_static_archive(ArchiveBuilder* builder,
1243                                         FileMapInfo* map_info,
1244                                         ArchiveMappedHeapInfo* mapped_heap_info,
1245                                         ArchiveStreamedHeapInfo* streamed_heap_info) {

1409     if (HAS_PENDING_EXCEPTION) {
1410       ResourceMark rm(THREAD);
1411       aot_log_warning(aot)("Preload Warning: Verification failed for %s",
1412                     ik->external_name());
1413       CLEAR_PENDING_EXCEPTION;
1414       SystemDictionaryShared::set_class_has_failed_verification(ik);
1415     } else {
1416       assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1417       ik->compute_has_loops_flag_for_methods();
1418     }
1419     BytecodeVerificationLocal = saved;
1420     return true;
1421   } else {
1422     return false;
1423   }
1424 }
1425 
1426 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1427   if (CDSConfig::is_dumping_heap()) {
1428     HeapShared::write_heap(&_mapped_heap_info, &_streamed_heap_info);
1429   } else if (!CDSConfig::is_dumping_preimage_static_archive()) {
1430     CDSConfig::log_reasons_for_not_dumping_heap();
1431   }
1432 }
1433 
1434 void AOTMetaspace::set_aot_metaspace_range(void* base, void *static_top, void* top) {
1435   assert(base <= static_top && static_top <= top, "must be");
1436   _aot_metaspace_static_top = static_top;
1437   MetaspaceObj::set_aot_metaspace_range(base, top);
1438 }
1439 
1440 bool AOTMetaspace::in_aot_cache_dynamic_region(void* p) {
1441   if ((p < MetaspaceObj::aot_metaspace_top()) &&
1442       (p >= _aot_metaspace_static_top)) {
1443     return true;
1444   } else {
1445     return false;
1446   }
1447 }
1448 
1449 bool AOTMetaspace::in_aot_cache_static_region(void* p) {

1505 // This function is called when the JVM is unable to write the specified CDS archive due to an
1506 // unrecoverable error.
1507 void AOTMetaspace::unrecoverable_writing_error(const char* message) {
1508   writing_error(message);
1509   vm_direct_exit(1);
1510 }
1511 
1512 // This function is called when the JVM is unable to write the specified CDS archive due to a
1513 // an error. The error will be propagated
1514 void AOTMetaspace::writing_error(const char* message) {
1515   aot_log_error(aot)("An error has occurred while writing the shared archive file.");
1516   if (message != nullptr) {
1517     aot_log_error(aot)("%s", message);
1518   }
1519 }
1520 
1521 void AOTMetaspace::initialize_runtime_shared_and_meta_spaces() {
1522   assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
1523   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1524 
1525   FileMapInfo* static_mapinfo = FileMapInfo::current_info();
1526   FileMapInfo* dynamic_mapinfo = nullptr;
1527 
1528   if (static_mapinfo != nullptr) {
1529     aot_log_info(aot)("Core region alignment: %zu", static_mapinfo->core_region_alignment());
1530     dynamic_mapinfo = open_dynamic_archive();
1531 
1532     aot_log_info(aot)("ArchiveRelocationMode: %d", ArchiveRelocationMode);
1533 
1534     // First try to map at the requested address
1535     result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1536     if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1537       // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1538       // by the OS.
1539       aot_log_info(aot)("Try to map archive(s) at an alternative address");
1540       result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1541     }
1542   }
1543 
1544   if (result == MAP_ARCHIVE_SUCCESS) {
1545     bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());

1568     } else {
1569       if (RequireSharedSpaces) {
1570         AOTMetaspace::unrecoverable_loading_error("Unable to map shared spaces");
1571       } else {
1572         report_loading_error("Unable to map shared spaces");
1573       }
1574     }
1575   }
1576 
1577   // If mapping failed and -XShare:on, the vm should exit
1578   bool has_failed = false;
1579   if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1580     has_failed = true;
1581     delete static_mapinfo;
1582   }
1583   if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1584     has_failed = true;
1585     delete dynamic_mapinfo;
1586   }
1587   if (RequireSharedSpaces && has_failed) {
1588     // static archive mapped but dynamic archive failed
1589       AOTMetaspace::unrecoverable_loading_error("Unable to map shared spaces");
1590   }
1591 }
1592 
1593 // This is called very early at VM start up to get the size of the cached_code region
1594 void AOTMetaspace::open_static_archive() {
1595   if (!UseSharedSpaces) { // FIXME -- is this still needed??
1596     return;
1597   }
1598   const char* static_archive = CDSConfig::input_static_archive_path();
1599   assert(static_archive != nullptr, "sanity");
1600   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1601   if (!mapinfo->open_as_input()) {
1602     delete(mapinfo);
1603     log_info(cds)("Opening of static archive %s failed", static_archive);
1604   } else {
1605     FileMapRegion* r = mapinfo->region_at(AOTMetaspace::ac);
1606     AOTCacheAccess::set_aot_code_region_size(r->used_aligned());
1607   }

1608 }
1609 
1610 FileMapInfo* AOTMetaspace::open_dynamic_archive() {
1611   if (CDSConfig::is_dumping_dynamic_archive()) {
1612     return nullptr;
1613   }
1614   const char* dynamic_archive = CDSConfig::input_dynamic_archive_path();
1615   if (dynamic_archive == nullptr) {
1616     return nullptr;
1617   }
1618 
1619   FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1620   if (!mapinfo->open_as_input()) {
1621     delete(mapinfo);
1622     if (RequireSharedSpaces) {
1623       AOTMetaspace::unrecoverable_loading_error("Failed to initialize dynamic archive");
1624     }
1625     return nullptr;
1626   }
1627   return mapinfo;

2098       MemoryReserver::release(archive_space_rs);
2099       archive_space_rs = {};
2100     }
2101     if (class_space_rs.is_reserved()) {
2102       aot_log_debug(aot)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
2103       MemoryReserver::release(class_space_rs);
2104       class_space_rs = {};
2105     }
2106   }
2107 }
2108 
2109 static int archive_regions[]     = { AOTMetaspace::rw, AOTMetaspace::ro };
2110 static int archive_regions_count = 2;
2111 
2112 MapArchiveResult AOTMetaspace::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
2113   assert(CDSConfig::is_using_archive(), "must be runtime");
2114   if (mapinfo == nullptr) {
2115     return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
2116   }
2117 
2118   if (!mapinfo->validate_aot_class_linking()) {
2119     return MAP_ARCHIVE_OTHER_FAILURE;
2120   }
2121 
2122   mapinfo->set_is_mapped(false);
2123   if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
2124     report_loading_error("Unable to map CDS archive -- core_region_alignment() expected: %zu"
2125                          " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment());
2126     return MAP_ARCHIVE_OTHER_FAILURE;
2127   }
2128 
2129   MapArchiveResult result =
2130     mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
2131 
2132   if (result != MAP_ARCHIVE_SUCCESS) {
2133     unmap_archive(mapinfo);
2134     return result;
2135   }
2136 
2137   if (!mapinfo->validate_class_location()) {
2138     unmap_archive(mapinfo);
2139     return MAP_ARCHIVE_OTHER_FAILURE;
2140   }
2141 
< prev index next >