< 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/aotMetaspace.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 "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 "gc/shared/gcVMOperations.hpp"
  66 #include "interpreter/bytecodes.hpp"
  67 #include "interpreter/bytecodeStream.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/hashTable.hpp"
 102 #include "utilities/macros.hpp"
 103 #include "utilities/ostream.hpp"
 104 
 105 #include <sys/stat.h>
 106 
 107 ReservedSpace AOTMetaspace::_symbol_rs;
 108 VirtualSpace AOTMetaspace::_symbol_vs;
 109 bool AOTMetaspace::_archive_loading_failed = false;
 110 bool AOTMetaspace::_remapped_readwrite = false;
 111 void* AOTMetaspace::_aot_metaspace_static_top = nullptr;
 112 intx AOTMetaspace::_relocation_delta;
 113 char* AOTMetaspace::_requested_base_address;
 114 Array<Method*>* AOTMetaspace::_archived_method_handle_intrinsics = nullptr;
 115 bool AOTMetaspace::_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 AOTMetaspace::core_region_alignment().
 125 //
 126 // These 2 regions are populated in the following steps:
 127 // [0] All classes are loaded in AOTMetaspace::load_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 AOTMetaspace::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,

 773     //
 774     // It's OK to do this for the built-in loaders as we know they can
 775     // tolerate this.
 776     return false;
 777   }
 778   return true;
 779 }
 780 
 781 void AOTMetaspace::link_all_loaded_classes(JavaThread* current) {
 782   while (true) {
 783     ResourceMark rm(current);
 784     CollectClassesForLinking collect_classes;
 785     bool has_linked = false;
 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 = java_lang_Class::as_InstanceKlass(mirror.resolve());
 790       if (may_be_eagerly_linked(ik)) {
 791         has_linked |= try_link_class(current, ik);
 792       }



 793     }
 794 
 795     if (!has_linked) {
 796       break;
 797     }
 798     // Class linking includes verification which may load more classes.
 799     // Keep scanning until we have linked no more classes.
 800   }
 801 }
 802 
 803 void AOTMetaspace::link_shared_classes(TRAPS) {
 804   AOTClassLinker::initialize();
 805   AOTClassInitializer::init_test_class(CHECK);
 806 




 807   link_all_loaded_classes(THREAD);
 808 
 809   // Eargerly resolve all string constants in constant pools
 810   {
 811     ResourceMark rm(THREAD);
 812     CollectClassesForLinking collect_classes;
 813     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 814     for (int i = 0; i < mirrors->length(); i++) {
 815       OopHandle mirror = mirrors->at(i);
 816       InstanceKlass* ik = java_lang_Class::as_InstanceKlass(mirror.resolve());
 817       AOTConstantPoolResolver::preresolve_string_cp_entries(ik, CHECK);



 818     }
 819   }
 820 
 821   if (CDSConfig::is_dumping_final_static_archive()) {
 822     FinalImageRecipes::apply_recipes(CHECK);
 823   }
 824 }
 825 
 826 void AOTMetaspace::dump_static_archive(TRAPS) {
 827   CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
 828   ResourceMark rm(THREAD);
 829   HandleMark hm(THREAD);
 830 
 831  if (CDSConfig::is_dumping_final_static_archive() && AOTPrintTrainingInfo) {
 832    tty->print_cr("==================== archived_training_data ** before dumping ====================");
 833    TrainingData::print_archived_training_data_on(tty);
 834  }
 835 
 836   StaticArchiveBuilder builder;
 837   dump_static_archive_impl(builder, THREAD);
 838   if (HAS_PENDING_EXCEPTION) {
 839     if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 840       aot_log_error(aot)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
 841                      "%zuM", MaxHeapSize/M);
 842       AOTMetaspace::writing_error();
 843     } else {
 844       oop message = java_lang_Throwable::message(PENDING_EXCEPTION);
 845       aot_log_error(aot)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
 846                          message == nullptr ? "(null)" : java_lang_String::as_utf8_string(message));
 847       AOTMetaspace::writing_error(err_msg("Unexpected exception, use -Xlog:aot%s,exceptions=trace for detail",
 848                                              CDSConfig::new_aot_flags_used() ? "" : ",cds"));
 849     }
 850   }
 851 
 852   if (CDSConfig::new_aot_flags_used()) {
 853     if (CDSConfig::is_dumping_preimage_static_archive()) {
 854       // We are in the JVM that runs the training run. Continue execution,

 927     }
 928   }
 929 
 930   // Some classes are used at CDS runtime but are not yet loaded at this point.
 931   // We can perform dummmy calls to these classes at dumptime to ensure they
 932   // are archived.
 933   exercise_runtime_cds_code(CHECK);
 934 
 935   aot_log_info(aot)("Loading classes to share: done.");
 936 }
 937 
 938 void AOTMetaspace::exercise_runtime_cds_code(TRAPS) {
 939   // Exercise the manifest processing code
 940   const char* dummy = "Manifest-Version: 1.0\n";
 941   CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
 942 
 943   // Exercise FileSystem and URL code
 944   CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
 945 }
 946 


















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







 948   if (CDSConfig::is_dumping_classic_static_archive()) {
 949     // We are running with -Xshare:dump
 950     load_classes(CHECK);
 951 
 952     if (SharedArchiveConfigFile) {
 953       log_info(aot)("Reading extra data from %s ...", SharedArchiveConfigFile);
 954       read_extra_data(THREAD, SharedArchiveConfigFile);
 955       log_info(aot)("Reading extra data: done.");
 956     }
 957   }
 958 
 959   if (CDSConfig::is_dumping_preimage_static_archive()) {
 960     log_info(aot)("Reading lambda form invokers from JDK default classlist ...");
 961     char default_classlist[JVM_MAXPATHLEN];
 962     get_default_classlist(default_classlist, JVM_MAXPATHLEN);
 963     struct stat statbuf;
 964     if (os::stat(default_classlist, &statbuf) == 0) {
 965       ClassListParser::parse_classlist(default_classlist,
 966                                        ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
 967     }

 994   // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
 995   // fails verification, all other interfaces that were not specified in the classlist but
 996   // are implemented by K are not verified.
 997   link_shared_classes(CHECK);
 998   log_info(aot)("Rewriting and linking classes: done");
 999   TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
1000 
1001   if (CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
1002     LambdaFormInvokers::regenerate_holder_classes(CHECK);
1003   }
1004 
1005 #if INCLUDE_CDS_JAVA_HEAP
1006   if (CDSConfig::is_dumping_heap()) {
1007     ArchiveHeapWriter::init();
1008 
1009     if (CDSConfig::is_dumping_full_module_graph()) {
1010       ClassLoaderDataShared::ensure_module_entry_tables_exist();
1011       HeapShared::reset_archived_object_states(CHECK);
1012     }
1013 




1014     AOTReferenceObjSupport::initialize(CHECK);
1015     AOTReferenceObjSupport::stabilize_cached_reference_objects(CHECK);
1016 
1017     if (CDSConfig::is_initing_classes_at_dump_time()) {
1018       // java.lang.Class::reflectionFactory cannot be archived yet. We set this field
1019       // to null, and it will be initialized again at runtime.
1020       log_debug(aot)("Resetting Class::reflectionFactory");
1021       TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates");
1022       Symbol* method_sig = vmSymbols::void_method_signature();
1023       JavaValue result(T_VOID);
1024       JavaCalls::call_static(&result, vmClasses::Class_klass(),
1025                              method_name, method_sig, CHECK);
1026 
1027       // Perhaps there is a way to avoid hard-coding these names here.
1028       // See discussion in JDK-8342481.
1029     }
1030 
1031     // Do this at the very end, when no Java code will be executed. Otherwise
1032     // some new strings may be added to the intern table.
1033     StringTable::allocate_shared_strings_array(CHECK);
1034   } else {
1035     log_info(aot)("Not dumping heap, reset CDSConfig::_is_using_optimized_module_handling");
1036     CDSConfig::stop_using_optimized_module_handling();
1037   }
1038 #endif
1039 
1040   VM_PopulateDumpSharedSpace op(builder);
1041   VMThread::execute(&op);








1042 
1043   if (AOTCodeCache::is_on_for_dump() && CDSConfig::is_dumping_final_static_archive()) {
1044     CDSConfig::enable_dumping_aot_code();
1045     {
1046       builder.start_ac_region();
1047       // Write the contents to AOT code region and close AOTCodeCache before packing the region
1048       AOTCodeCache::close();
1049       builder.end_ac_region();













1050     }
1051     CDSConfig::disable_dumping_aot_code();
1052   }
1053 
1054   bool status = write_static_archive(&builder, op.map_info(), op.heap_info());
1055   if (status && CDSConfig::is_dumping_preimage_static_archive()) {
1056     tty->print_cr("%s AOTConfiguration recorded: %s",
1057                   CDSConfig::has_temp_aot_config_file() ? "Temporary" : "", AOTConfiguration);
1058     if (CDSConfig::is_single_command_training()) {
1059       fork_and_dump_final_static_archive(CHECK);
1060     }
1061   }
1062 
1063   if (!status) {
1064     THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
1065   }
1066 }
1067 
1068 bool AOTMetaspace::write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info) {
1069   // relocate the data so that it can be mapped to AOTMetaspace::requested_base_address()
1070   // without runtime relocation.
1071   builder->relocate_to_requested();
1072 
1073   map_info->open_as_output();
1074   if (!map_info->is_open()) {

1232     if (HAS_PENDING_EXCEPTION) {
1233       ResourceMark rm(THREAD);
1234       aot_log_warning(aot)("Preload Warning: Verification failed for %s",
1235                     ik->external_name());
1236       CLEAR_PENDING_EXCEPTION;
1237       SystemDictionaryShared::set_class_has_failed_verification(ik);
1238     } else {
1239       assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1240       ik->compute_has_loops_flag_for_methods();
1241     }
1242     BytecodeVerificationLocal = saved;
1243     return true;
1244   } else {
1245     return false;
1246   }
1247 }
1248 
1249 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1250   if (CDSConfig::is_dumping_heap()) {
1251     HeapShared::write_heap(&_heap_info);
1252   } else {
1253     CDSConfig::log_reasons_for_not_dumping_heap();
1254   }
1255 }
1256 
1257 void AOTMetaspace::set_aot_metaspace_range(void* base, void *static_top, void* top) {
1258   assert(base <= static_top && static_top <= top, "must be");
1259   _aot_metaspace_static_top = static_top;
1260   MetaspaceObj::set_aot_metaspace_range(base, top);
1261 }
1262 
1263 bool AOTMetaspace::in_aot_cache_dynamic_region(void* p) {
1264   if ((p < MetaspaceObj::aot_metaspace_top()) &&
1265       (p >= _aot_metaspace_static_top)) {
1266     return true;
1267   } else {
1268     return false;
1269   }
1270 }
1271 
1272 bool AOTMetaspace::in_aot_cache_static_region(void* p) {

1328 // This function is called when the JVM is unable to write the specified CDS archive due to an
1329 // unrecoverable error.
1330 void AOTMetaspace::unrecoverable_writing_error(const char* message) {
1331   writing_error(message);
1332   vm_direct_exit(1);
1333 }
1334 
1335 // This function is called when the JVM is unable to write the specified CDS archive due to a
1336 // an error. The error will be propagated
1337 void AOTMetaspace::writing_error(const char* message) {
1338   aot_log_error(aot)("An error has occurred while writing the shared archive file.");
1339   if (message != nullptr) {
1340     aot_log_error(aot)("%s", message);
1341   }
1342 }
1343 
1344 void AOTMetaspace::initialize_runtime_shared_and_meta_spaces() {
1345   assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
1346   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1347 
1348   FileMapInfo* static_mapinfo = open_static_archive();
1349   FileMapInfo* dynamic_mapinfo = nullptr;
1350 
1351   if (static_mapinfo != nullptr) {
1352     aot_log_info(aot)("Core region alignment: %zu", static_mapinfo->core_region_alignment());
1353     dynamic_mapinfo = open_dynamic_archive();
1354 
1355     aot_log_info(aot)("ArchiveRelocationMode: %d", ArchiveRelocationMode);
1356 
1357     // First try to map at the requested address
1358     result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1359     if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1360       // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1361       // by the OS.
1362       aot_log_info(aot)("Try to map archive(s) at an alternative address");
1363       result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1364     }
1365   }
1366 
1367   if (result == MAP_ARCHIVE_SUCCESS) {
1368     bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());

1391     } else {
1392       if (RequireSharedSpaces) {
1393         AOTMetaspace::unrecoverable_loading_error("Unable to map shared spaces");
1394       } else {
1395         report_loading_error("Unable to map shared spaces");
1396       }
1397     }
1398   }
1399 
1400   // If mapping failed and -XShare:on, the vm should exit
1401   bool has_failed = false;
1402   if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1403     has_failed = true;
1404     delete static_mapinfo;
1405   }
1406   if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1407     has_failed = true;
1408     delete dynamic_mapinfo;
1409   }
1410   if (RequireSharedSpaces && has_failed) {

1411       AOTMetaspace::unrecoverable_loading_error("Unable to map shared spaces");
1412   }
1413 }
1414 
1415 FileMapInfo* AOTMetaspace::open_static_archive() {




1416   const char* static_archive = CDSConfig::input_static_archive_path();
1417   assert(static_archive != nullptr, "sanity");
1418   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1419   if (!mapinfo->open_as_input()) {
1420     delete(mapinfo);
1421     log_info(cds)("Opening of static archive %s failed", static_archive);
1422     return nullptr;


1423   }
1424   return mapinfo;
1425 }
1426 
1427 FileMapInfo* AOTMetaspace::open_dynamic_archive() {
1428   if (CDSConfig::is_dumping_dynamic_archive()) {
1429     return nullptr;
1430   }
1431   const char* dynamic_archive = CDSConfig::input_dynamic_archive_path();
1432   if (dynamic_archive == nullptr) {
1433     return nullptr;
1434   }
1435 
1436   FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1437   if (!mapinfo->open_as_input()) {
1438     delete(mapinfo);
1439     if (RequireSharedSpaces) {
1440       AOTMetaspace::unrecoverable_loading_error("Failed to initialize dynamic archive");
1441     }
1442     return nullptr;
1443   }
1444   return mapinfo;

1895       MemoryReserver::release(archive_space_rs);
1896       archive_space_rs = {};
1897     }
1898     if (class_space_rs.is_reserved()) {
1899       aot_log_debug(aot)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
1900       MemoryReserver::release(class_space_rs);
1901       class_space_rs = {};
1902     }
1903   }
1904 }
1905 
1906 static int archive_regions[]     = { AOTMetaspace::rw, AOTMetaspace::ro };
1907 static int archive_regions_count = 2;
1908 
1909 MapArchiveResult AOTMetaspace::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
1910   assert(CDSConfig::is_using_archive(), "must be runtime");
1911   if (mapinfo == nullptr) {
1912     return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
1913   }
1914 




1915   mapinfo->set_is_mapped(false);
1916   if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
1917     report_loading_error("Unable to map CDS archive -- core_region_alignment() expected: %zu"
1918                          " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment());
1919     return MAP_ARCHIVE_OTHER_FAILURE;
1920   }
1921 
1922   MapArchiveResult result =
1923     mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
1924 
1925   if (result != MAP_ARCHIVE_SUCCESS) {
1926     unmap_archive(mapinfo);
1927     return result;
1928   }
1929 
1930   if (!mapinfo->validate_class_location()) {
1931     unmap_archive(mapinfo);
1932     return MAP_ARCHIVE_OTHER_FAILURE;
1933   }
1934 

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

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

 796     //
 797     // It's OK to do this for the built-in loaders as we know they can
 798     // tolerate this.
 799     return false;
 800   }
 801   return true;
 802 }
 803 
 804 void AOTMetaspace::link_all_loaded_classes(JavaThread* current) {
 805   while (true) {
 806     ResourceMark rm(current);
 807     CollectClassesForLinking collect_classes;
 808     bool has_linked = false;
 809     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 810     for (int i = 0; i < mirrors->length(); i++) {
 811       OopHandle mirror = mirrors->at(i);
 812       InstanceKlass* ik = java_lang_Class::as_InstanceKlass(mirror.resolve());
 813       if (may_be_eagerly_linked(ik)) {
 814         has_linked |= try_link_class(current, ik);
 815       }
 816       if (CDSConfig::is_dumping_heap() && ik->is_linked() && !ik->is_initialized()) {
 817         AOTClassInitializer::maybe_preinit_class(ik, current);
 818       }
 819     }
 820 
 821     if (!has_linked) {
 822       break;
 823     }
 824     // Class linking includes verification which may load more classes.
 825     // Keep scanning until we have linked no more classes.
 826   }
 827 }
 828 
 829 void AOTMetaspace::link_shared_classes(TRAPS) {
 830   AOTClassLinker::initialize();
 831   AOTClassInitializer::init_test_class(CHECK);
 832 
 833   if (CDSConfig::is_dumping_final_static_archive()) {
 834     FinalImageRecipes::apply_recipes(CHECK);
 835   }
 836 
 837   link_all_loaded_classes(THREAD);
 838 
 839   // Eargerly resolve all string constants in constant pools
 840   {
 841     ResourceMark rm(THREAD);
 842     CollectClassesForLinking collect_classes;
 843     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 844     for (int i = 0; i < mirrors->length(); i++) {
 845       OopHandle mirror = mirrors->at(i);
 846       InstanceKlass* ik = java_lang_Class::as_InstanceKlass(mirror.resolve());
 847       AOTConstantPoolResolver::preresolve_string_cp_entries(ik, CHECK);
 848       if (CDSConfig::is_dumping_preimage_static_archive()) {
 849         FinalImageRecipes::add_reflection_data_flags(ik, CHECK);
 850       }
 851     }
 852   }




 853 }
 854 
 855 void AOTMetaspace::dump_static_archive(TRAPS) {
 856   CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
 857   ResourceMark rm(THREAD);
 858   HandleMark hm(THREAD);
 859 
 860   if (CDSConfig::is_dumping_final_static_archive() && AOTPrintTrainingInfo) {
 861     tty->print_cr("==================== archived_training_data ** before dumping ====================");
 862     TrainingData::print_archived_training_data_on(tty);
 863   }
 864 
 865   StaticArchiveBuilder builder;
 866   dump_static_archive_impl(builder, THREAD);
 867   if (HAS_PENDING_EXCEPTION) {
 868     if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 869       aot_log_error(aot)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
 870                      "%zuM", MaxHeapSize/M);
 871       AOTMetaspace::writing_error();
 872     } else {
 873       oop message = java_lang_Throwable::message(PENDING_EXCEPTION);
 874       aot_log_error(aot)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
 875                          message == nullptr ? "(null)" : java_lang_String::as_utf8_string(message));
 876       AOTMetaspace::writing_error(err_msg("Unexpected exception, use -Xlog:aot%s,exceptions=trace for detail",
 877                                              CDSConfig::new_aot_flags_used() ? "" : ",cds"));
 878     }
 879   }
 880 
 881   if (CDSConfig::new_aot_flags_used()) {
 882     if (CDSConfig::is_dumping_preimage_static_archive()) {
 883       // We are in the JVM that runs the training run. Continue execution,

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

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

1130   }
1131 
1132   bool status = write_static_archive(&builder, mapinfo, heap_info);
1133   if (status && CDSConfig::is_dumping_preimage_static_archive()) {
1134     tty->print_cr("%s AOTConfiguration recorded: %s",
1135                   CDSConfig::has_temp_aot_config_file() ? "Temporary" : "", AOTConfiguration);
1136     if (CDSConfig::is_single_command_training()) {
1137       fork_and_dump_final_static_archive(CHECK);
1138     }
1139   }
1140 
1141   if (!status) {
1142     THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
1143   }
1144 }
1145 
1146 bool AOTMetaspace::write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info) {
1147   // relocate the data so that it can be mapped to AOTMetaspace::requested_base_address()
1148   // without runtime relocation.
1149   builder->relocate_to_requested();
1150 
1151   map_info->open_as_output();
1152   if (!map_info->is_open()) {

1310     if (HAS_PENDING_EXCEPTION) {
1311       ResourceMark rm(THREAD);
1312       aot_log_warning(aot)("Preload Warning: Verification failed for %s",
1313                     ik->external_name());
1314       CLEAR_PENDING_EXCEPTION;
1315       SystemDictionaryShared::set_class_has_failed_verification(ik);
1316     } else {
1317       assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1318       ik->compute_has_loops_flag_for_methods();
1319     }
1320     BytecodeVerificationLocal = saved;
1321     return true;
1322   } else {
1323     return false;
1324   }
1325 }
1326 
1327 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1328   if (CDSConfig::is_dumping_heap()) {
1329     HeapShared::write_heap(&_heap_info);
1330   } else if (!CDSConfig::is_dumping_preimage_static_archive()) {
1331     CDSConfig::log_reasons_for_not_dumping_heap();
1332   }
1333 }
1334 
1335 void AOTMetaspace::set_aot_metaspace_range(void* base, void *static_top, void* top) {
1336   assert(base <= static_top && static_top <= top, "must be");
1337   _aot_metaspace_static_top = static_top;
1338   MetaspaceObj::set_aot_metaspace_range(base, top);
1339 }
1340 
1341 bool AOTMetaspace::in_aot_cache_dynamic_region(void* p) {
1342   if ((p < MetaspaceObj::aot_metaspace_top()) &&
1343       (p >= _aot_metaspace_static_top)) {
1344     return true;
1345   } else {
1346     return false;
1347   }
1348 }
1349 
1350 bool AOTMetaspace::in_aot_cache_static_region(void* p) {

1406 // This function is called when the JVM is unable to write the specified CDS archive due to an
1407 // unrecoverable error.
1408 void AOTMetaspace::unrecoverable_writing_error(const char* message) {
1409   writing_error(message);
1410   vm_direct_exit(1);
1411 }
1412 
1413 // This function is called when the JVM is unable to write the specified CDS archive due to a
1414 // an error. The error will be propagated
1415 void AOTMetaspace::writing_error(const char* message) {
1416   aot_log_error(aot)("An error has occurred while writing the shared archive file.");
1417   if (message != nullptr) {
1418     aot_log_error(aot)("%s", message);
1419   }
1420 }
1421 
1422 void AOTMetaspace::initialize_runtime_shared_and_meta_spaces() {
1423   assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
1424   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1425 
1426   FileMapInfo* static_mapinfo = FileMapInfo::current_info();
1427   FileMapInfo* dynamic_mapinfo = nullptr;
1428 
1429   if (static_mapinfo != nullptr) {
1430     aot_log_info(aot)("Core region alignment: %zu", static_mapinfo->core_region_alignment());
1431     dynamic_mapinfo = open_dynamic_archive();
1432 
1433     aot_log_info(aot)("ArchiveRelocationMode: %d", ArchiveRelocationMode);
1434 
1435     // First try to map at the requested address
1436     result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1437     if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1438       // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1439       // by the OS.
1440       aot_log_info(aot)("Try to map archive(s) at an alternative address");
1441       result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1442     }
1443   }
1444 
1445   if (result == MAP_ARCHIVE_SUCCESS) {
1446     bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());

1469     } else {
1470       if (RequireSharedSpaces) {
1471         AOTMetaspace::unrecoverable_loading_error("Unable to map shared spaces");
1472       } else {
1473         report_loading_error("Unable to map shared spaces");
1474       }
1475     }
1476   }
1477 
1478   // If mapping failed and -XShare:on, the vm should exit
1479   bool has_failed = false;
1480   if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1481     has_failed = true;
1482     delete static_mapinfo;
1483   }
1484   if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1485     has_failed = true;
1486     delete dynamic_mapinfo;
1487   }
1488   if (RequireSharedSpaces && has_failed) {
1489     // static archive mapped but dynamic archive failed
1490       AOTMetaspace::unrecoverable_loading_error("Unable to map shared spaces");
1491   }
1492 }
1493 
1494 // This is called very early at VM start up to get the size of the cached_code region
1495 void AOTMetaspace::open_static_archive() {
1496   if (!UseSharedSpaces) { // FIXME -- is this still needed??
1497     return;
1498   }
1499   const char* static_archive = CDSConfig::input_static_archive_path();
1500   assert(static_archive != nullptr, "sanity");
1501   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1502   if (!mapinfo->open_as_input()) {
1503     delete(mapinfo);
1504     log_info(cds)("Opening of static archive %s failed", static_archive);
1505   } else {
1506     FileMapRegion* r = mapinfo->region_at(AOTMetaspace::ac);
1507     AOTCacheAccess::set_aot_code_region_size(r->used_aligned());
1508   }

1509 }
1510 
1511 FileMapInfo* AOTMetaspace::open_dynamic_archive() {
1512   if (CDSConfig::is_dumping_dynamic_archive()) {
1513     return nullptr;
1514   }
1515   const char* dynamic_archive = CDSConfig::input_dynamic_archive_path();
1516   if (dynamic_archive == nullptr) {
1517     return nullptr;
1518   }
1519 
1520   FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1521   if (!mapinfo->open_as_input()) {
1522     delete(mapinfo);
1523     if (RequireSharedSpaces) {
1524       AOTMetaspace::unrecoverable_loading_error("Failed to initialize dynamic archive");
1525     }
1526     return nullptr;
1527   }
1528   return mapinfo;

1979       MemoryReserver::release(archive_space_rs);
1980       archive_space_rs = {};
1981     }
1982     if (class_space_rs.is_reserved()) {
1983       aot_log_debug(aot)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
1984       MemoryReserver::release(class_space_rs);
1985       class_space_rs = {};
1986     }
1987   }
1988 }
1989 
1990 static int archive_regions[]     = { AOTMetaspace::rw, AOTMetaspace::ro };
1991 static int archive_regions_count = 2;
1992 
1993 MapArchiveResult AOTMetaspace::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
1994   assert(CDSConfig::is_using_archive(), "must be runtime");
1995   if (mapinfo == nullptr) {
1996     return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
1997   }
1998 
1999   if (!mapinfo->validate_aot_class_linking()) {
2000     return MAP_ARCHIVE_OTHER_FAILURE;
2001   }
2002 
2003   mapinfo->set_is_mapped(false);
2004   if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
2005     report_loading_error("Unable to map CDS archive -- core_region_alignment() expected: %zu"
2006                          " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment());
2007     return MAP_ARCHIVE_OTHER_FAILURE;
2008   }
2009 
2010   MapArchiveResult result =
2011     mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
2012 
2013   if (result != MAP_ARCHIVE_SUCCESS) {
2014     unmap_archive(mapinfo);
2015     return result;
2016   }
2017 
2018   if (!mapinfo->validate_class_location()) {
2019     unmap_archive(mapinfo);
2020     return MAP_ARCHIVE_OTHER_FAILURE;
2021   }
2022 
< prev index next >