< 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 "services/management.hpp"
 100 #include "utilities/align.hpp"
 101 #include "utilities/bitMap.inline.hpp"
 102 #include "utilities/defaultStream.hpp"
 103 #include "utilities/hashTable.hpp"
 104 #include "utilities/macros.hpp"
 105 #include "utilities/ostream.hpp"
 106 
 107 #include <sys/stat.h>

 881     //
 882     // It's OK to do this for the built-in loaders as we know they can
 883     // tolerate this.
 884     return false;
 885   }
 886   return true;
 887 }
 888 
 889 void AOTMetaspace::link_all_loaded_classes(JavaThread* current) {
 890   while (true) {
 891     ResourceMark rm(current);
 892     CollectClassesForLinking collect_classes;
 893     bool has_linked = false;
 894     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 895     for (int i = 0; i < mirrors->length(); i++) {
 896       OopHandle mirror = mirrors->at(i);
 897       InstanceKlass* ik = java_lang_Class::as_InstanceKlass(mirror.resolve());
 898       if (may_be_eagerly_linked(ik)) {
 899         has_linked |= try_link_class(current, ik);
 900       }



 901     }
 902 
 903     if (!has_linked) {
 904       break;
 905     }
 906     // Class linking includes verification which may load more classes.
 907     // Keep scanning until we have linked no more classes.
 908   }
 909 }
 910 
 911 void AOTMetaspace::link_shared_classes(TRAPS) {
 912   AOTClassLinker::initialize();
 913   AOTClassInitializer::init_test_class(CHECK);
 914 
 915   if (CDSConfig::is_dumping_final_static_archive()) {
 916     // - Load and link all classes used in the training run.
 917     // - Initialize @AOTSafeClassInitializer classes that were
 918     //   initialized in the training run.
 919     // - Perform per-class optimization such as AOT-resolution of
 920     //   constant pool entries that were resolved during the training run.

 923     // Because the AOT assembly phase does not run the same exact code as in the
 924     // training run (e.g., we use different lambda form invoker classes;
 925     // generated lambda form classes are not recorded in FinalImageRecipes),
 926     // the recipes do not cover all classes that have been loaded so far. As
 927     // a result, we might have some unlinked classes at this point. Since we
 928     // require cached classes to be linked, all such classes will be linked
 929     // by the following step.
 930   }
 931 
 932   link_all_loaded_classes(THREAD);
 933 
 934   // Eargerly resolve all string constants in constant pools
 935   {
 936     ResourceMark rm(THREAD);
 937     CollectClassesForLinking collect_classes;
 938     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 939     for (int i = 0; i < mirrors->length(); i++) {
 940       OopHandle mirror = mirrors->at(i);
 941       InstanceKlass* ik = java_lang_Class::as_InstanceKlass(mirror.resolve());
 942       AOTConstantPoolResolver::preresolve_string_cp_entries(ik, CHECK);



 943     }
 944   }
 945 }
 946 
 947 void AOTMetaspace::dump_static_archive(TRAPS) {
 948   CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
 949   ResourceMark rm(THREAD);
 950   HandleMark hm(THREAD);
 951 
 952  if (CDSConfig::is_dumping_final_static_archive() && AOTPrintTrainingInfo) {
 953    tty->print_cr("==================== archived_training_data ** before dumping ====================");
 954    TrainingData::print_archived_training_data_on(tty);
 955  }
 956 
 957   StaticArchiveBuilder builder;
 958   dump_static_archive_impl(builder, THREAD);
 959   if (HAS_PENDING_EXCEPTION) {
 960     if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 961       aot_log_error(aot)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
 962                      "%zuM", MaxHeapSize/M);
 963       AOTMetaspace::writing_error();
 964     } else {
 965       oop message = java_lang_Throwable::message(PENDING_EXCEPTION);
 966       aot_log_error(aot)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
 967                          message == nullptr ? "(null)" : java_lang_String::as_utf8_string(message));
 968       AOTMetaspace::writing_error(err_msg("Unexpected exception, use -Xlog:aot%s,exceptions=trace for detail",
 969                                              CDSConfig::new_aot_flags_used() ? "" : ",cds"));
 970     }
 971   }
 972 
 973   if (CDSConfig::new_aot_flags_used()) {
 974     if (CDSConfig::is_dumping_preimage_static_archive()) {
 975       // We are in the JVM that runs the training run. Continue execution,

1134   // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1135   // fails verification, all other interfaces that were not specified in the classlist but
1136   // are implemented by K are not verified.
1137   link_shared_classes(CHECK);
1138   log_info(aot)("Rewriting and linking classes: done");
1139   TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
1140 
1141   if (CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
1142     LambdaFormInvokers::regenerate_holder_classes(CHECK);
1143   }
1144 
1145 #if INCLUDE_CDS_JAVA_HEAP
1146   if (CDSConfig::is_dumping_heap()) {
1147     HeapShared::init_heap_writer();
1148     if (CDSConfig::is_dumping_full_module_graph()) {
1149       ClassLoaderDataShared::ensure_module_entry_tables_exist();
1150       ClassLoaderDataShared::build_tables(CHECK);
1151       HeapShared::reset_archived_object_states(CHECK);
1152     }
1153 




1154     AOTReferenceObjSupport::initialize(CHECK);
1155     AOTReferenceObjSupport::stabilize_cached_reference_objects(CHECK);
1156 
1157     if (CDSConfig::is_dumping_aot_linked_classes()) {
1158       // java.lang.Class::reflectionFactory cannot be archived yet. We set this field
1159       // to null, and it will be initialized again at runtime.
1160       log_debug(aot)("Resetting Class::reflectionFactory");
1161       TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates");
1162       Symbol* method_sig = vmSymbols::void_method_signature();
1163       JavaValue result(T_VOID);
1164       JavaCalls::call_static(&result, vmClasses::Class_klass(),
1165                              method_name, method_sig, CHECK);
1166 
1167       // Perhaps there is a way to avoid hard-coding these names here.
1168       // See discussion in JDK-8342481.
1169     }
1170   } else {
1171     log_info(aot)("Not dumping heap, reset CDSConfig::_is_using_optimized_module_handling");
1172     CDSConfig::stop_using_optimized_module_handling();
1173   }
1174 #endif
1175 
1176   if (!CDSConfig::is_dumping_preimage_static_archive()) {
1177     if (CDSConfig::is_dumping_final_static_archive()) {
1178       FileMapInfo::free_current_info(); // FIXME: should not free current info
1179     }
1180     open_output_mapinfo();
1181   }
1182 
1183   VM_PopulateDumpSharedSpace op(builder, _output_mapinfo);
1184   VMThread::execute(&op);
1185 
1186   if (AOTCodeCache::is_on_for_dump() && CDSConfig::is_dumping_final_static_archive()) {
1187     CDSConfig::enable_dumping_aot_code();
1188     {
1189       builder.start_ac_region();
1190       // Write the contents to AOT code region and close AOTCodeCache before packing the region
1191       AOTCodeCache::close();
1192       builder.end_ac_region();















1193     }
1194     CDSConfig::disable_dumping_aot_code();
1195   }
1196 
1197   bool status = write_static_archive(&builder, _output_mapinfo, op.mapped_heap_info(), op.streamed_heap_info());
1198   assert(!_output_mapinfo->is_open(), "Must be closed already");
1199   _output_mapinfo = nullptr;
1200   if (status && CDSConfig::is_dumping_preimage_static_archive()) {
1201     tty->print_cr("%s AOTConfiguration recorded: %s",
1202                   CDSConfig::has_temp_aot_config_file() ? "Temporary" : "", AOTConfiguration);
1203     if (CDSConfig::is_single_command_training()) {
1204       fork_and_dump_final_static_archive(CHECK);
1205     }
1206   }
1207 
1208   if (!status) {
1209     THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
1210   }
1211 }
1212 
1213 bool AOTMetaspace::write_static_archive(ArchiveBuilder* builder,
1214                                         FileMapInfo* map_info,

1382       aot_log_warning(aot)("Preload Warning: Verification failed for %s because a %s was thrown: %s",
1383                             ik->external_name(),
1384                             current->pending_exception()->klass()->external_name(),
1385                             message == nullptr ? "(no message)" : java_lang_String::as_utf8_string(message));
1386       CLEAR_PENDING_EXCEPTION;
1387       SystemDictionaryShared::set_class_has_failed_verification(ik);
1388     } else {
1389       assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1390       ik->compute_has_loops_flag_for_methods();
1391     }
1392     BytecodeVerificationLocal = saved;
1393     return true;
1394   } else {
1395     return false;
1396   }
1397 }
1398 
1399 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1400   if (CDSConfig::is_dumping_heap()) {
1401     HeapShared::write_heap(&_mapped_heap_info, &_streamed_heap_info);
1402   } else {
1403     CDSConfig::log_reasons_for_not_dumping_heap();
1404   }
1405 }
1406 
1407 void AOTMetaspace::set_aot_metaspace_range(void* base, void *static_top, void* top) {
1408   assert(base <= static_top && static_top <= top, "must be");
1409   _aot_metaspace_static_top = static_top;
1410   MetaspaceObj::set_aot_metaspace_range(base, top);
1411 }
1412 
1413 bool AOTMetaspace::in_aot_cache_dynamic_region(void* p) {
1414   if ((p < MetaspaceObj::aot_metaspace_top()) &&
1415       (p >= _aot_metaspace_static_top)) {
1416     return true;
1417   } else {
1418     return false;
1419   }
1420 }
1421 
1422 bool AOTMetaspace::in_aot_cache_static_region(void* p) {

1478 // This function is called when the JVM is unable to write the specified CDS archive due to an
1479 // unrecoverable error.
1480 void AOTMetaspace::unrecoverable_writing_error(const char* message) {
1481   writing_error(message);
1482   vm_direct_exit(1);
1483 }
1484 
1485 // This function is called when the JVM is unable to write the specified CDS archive due to a
1486 // an error. The error will be propagated
1487 void AOTMetaspace::writing_error(const char* message) {
1488   aot_log_error(aot)("An error has occurred while writing the shared archive file.");
1489   if (message != nullptr) {
1490     aot_log_error(aot)("%s", message);
1491   }
1492 }
1493 
1494 void AOTMetaspace::initialize_runtime_shared_and_meta_spaces() {
1495   assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
1496   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1497 
1498   FileMapInfo* static_mapinfo = open_static_archive();
1499   FileMapInfo* dynamic_mapinfo = nullptr;
1500 
1501   if (static_mapinfo != nullptr) {
1502     aot_log_info(aot)("Core region alignment: %zu", static_mapinfo->core_region_alignment());
1503     dynamic_mapinfo = open_dynamic_archive();
1504 
1505     aot_log_info(aot)("ArchiveRelocationMode: %d", ArchiveRelocationMode);
1506 
1507     // First try to map at the requested address
1508     result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1509     if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1510       // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1511       // by the OS.
1512       aot_log_info(aot)("Try to map archive(s) at an alternative address");
1513       result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1514     }
1515   }
1516 
1517   if (result == MAP_ARCHIVE_SUCCESS) {
1518     bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());

1541     } else {
1542       if (RequireSharedSpaces) {
1543         AOTMetaspace::unrecoverable_loading_error("Unable to map shared spaces");
1544       } else {
1545         report_loading_error("Unable to map shared spaces");
1546       }
1547     }
1548   }
1549 
1550   // If mapping failed and -XShare:on, the vm should exit
1551   bool has_failed = false;
1552   if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1553     has_failed = true;
1554     delete static_mapinfo;
1555   }
1556   if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1557     has_failed = true;
1558     delete dynamic_mapinfo;
1559   }
1560   if (RequireSharedSpaces && has_failed) {

1561       AOTMetaspace::unrecoverable_loading_error("Unable to map shared spaces");
1562   }
1563 }
1564 
1565 FileMapInfo* AOTMetaspace::open_static_archive() {




1566   const char* static_archive = CDSConfig::input_static_archive_path();
1567   assert(static_archive != nullptr, "sanity");
1568   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1569   if (!mapinfo->open_as_input()) {
1570     delete(mapinfo);
1571     log_info(cds)("Opening of static archive %s failed", static_archive);
1572     return nullptr;


1573   }
1574   return mapinfo;
1575 }
1576 
1577 FileMapInfo* AOTMetaspace::open_dynamic_archive() {
1578   if (CDSConfig::is_dumping_dynamic_archive()) {
1579     return nullptr;
1580   }
1581   const char* dynamic_archive = CDSConfig::input_dynamic_archive_path();
1582   if (dynamic_archive == nullptr) {
1583     return nullptr;
1584   }
1585 
1586   FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1587   if (!mapinfo->open_as_input()) {
1588     delete(mapinfo);
1589     if (RequireSharedSpaces) {
1590       AOTMetaspace::unrecoverable_loading_error("Failed to initialize dynamic archive");
1591     }
1592     return nullptr;
1593   }
1594   return mapinfo;

2065       MemoryReserver::release(archive_space_rs);
2066       archive_space_rs = {};
2067     }
2068     if (class_space_rs.is_reserved()) {
2069       aot_log_debug(aot)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
2070       MemoryReserver::release(class_space_rs);
2071       class_space_rs = {};
2072     }
2073   }
2074 }
2075 
2076 static int archive_regions[]     = { AOTMetaspace::rw, AOTMetaspace::ro };
2077 static int archive_regions_count = 2;
2078 
2079 MapArchiveResult AOTMetaspace::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
2080   assert(CDSConfig::is_using_archive(), "must be runtime");
2081   if (mapinfo == nullptr) {
2082     return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
2083   }
2084 




2085   mapinfo->set_is_mapped(false);
2086   if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
2087     report_loading_error("Unable to map CDS archive -- core_region_alignment() expected: %zu"
2088                          " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment());
2089     return MAP_ARCHIVE_OTHER_FAILURE;
2090   }
2091 
2092   MapArchiveResult result =
2093     mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
2094 
2095   if (result != MAP_ARCHIVE_SUCCESS) {
2096     unmap_archive(mapinfo);
2097     return result;
2098   }
2099 
2100   if (!mapinfo->validate_class_location()) {
2101     unmap_archive(mapinfo);
2102     return MAP_ARCHIVE_OTHER_FAILURE;
2103   }
2104 

   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>

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

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

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

1224   }
1225 
1226   bool status = write_static_archive(&builder, _output_mapinfo, op.mapped_heap_info(), op.streamed_heap_info());
1227   assert(!_output_mapinfo->is_open(), "Must be closed already");
1228   _output_mapinfo = nullptr;
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,

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

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

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

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

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